Newer
Older
validate-upg / validate-upg.py
#!/usr/bin/env python
# validate-upg.py
# Copyright (c) 2005 TundraWare Inc.  All Rights Reserved.
# For Updates See:  http://www.tundraware.com/Software/validate-upg

# Program Information

PROGNAME = "validate-upg"
RCSID = "$Id: validate-upg.py,v 1.101 2005/03/02 07:40:51 tundra Exp $"
VERSION = RCSID.split()[2]

# Copyright Information

CPRT         = "(c)"
DATE         = "2005"
OWNER        = "TundraWare Inc."
RIGHTS       = "All Rights Reserved"
COPYRIGHT    = "Copyright %s %s %s  %s. " % (CPRT, DATE, OWNER, RIGHTS)


#----------------------------------------------------------#
#            Variables User Might Change                   #
#----------------------------------------------------------#



#------------------- Nothing Below Here Should Need Changing ------------------#


#----------------------------------------------------------#
#                       Imports                            #
#----------------------------------------------------------#

import getopt
import os
import sys
import crypt, grp, pwd


#----------------------------------------------------------#
#                 Aliases & Redefinitions                  #
#----------------------------------------------------------#



#----------------------------------------------------------#
#                Constants & Literals                      #
#----------------------------------------------------------#



#####
# Constants
#####


#####
# Literals
#####

OK    = ""
BAD   = "Invalid User Name, Password, Or Group"


#----------------------------------------------------------#
#              Prompts, & Application Strings              #
#----------------------------------------------------------#


#####
# Error Messages
#####

eERROR   =  "ERROR"


#####
# Informational Messages
#####


#####
# Usage Prompts
#####

uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT,
          "usage:  " + PROGNAME + " [-hpv] username password [group]",
          "    where,\n",
          "          -h       print this help information",
          "          -p       display complete password record",
          "          -v       print detailed version information",
          ]


#----------------------------------------------------------#
#          Global Variables & Data Structures              #
#----------------------------------------------------------#

SHOWRECORD = False

#--------------------------- Code Begins Here ---------------------------------#


#----------------------------------------------------------#
#             Object Base Class Definitions                #
#----------------------------------------------------------#

    

#----------------------------------------------------------#
#             Supporting Function Definitions              #
#----------------------------------------------------------#


#####
# Print An Error Message
#####

def ErrorMsg(emsg):
    print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg

# End of 'ErrorMsg()'


#####
# Print Usage Information
#####

def Usage():
    for line in uTable:
        print line

# End of 'Usage()'

#####
# Validate A Username, Password, Group Tuple
#####

def Validate(name, password, group):
    
    retcode = OK

    try:

        # Get the user record from the password database
        
        pwrecord    = pwd.getpwnam(name)
        if SHOWRECORD:
            print pwrecord

        # Is the password correct for the named user?
        
        if crypt.crypt(password, pwrecord[1]) != pwrecord[1]:
            retcode = BAD    

        # Is the user a member of the named group?
        # This is an optional test

        if group:
            grouprecord = grp.getgrnam(group)

            if (pwrecord[3] != grouprecord[2]) and (name not in grouprecord[3]):
                retcode = BAD

    # If we get here, it means name or group does not exist
    except:
        retcode = BAD

    return retcode

# End of 'Validate()'


#----------------------------------------------------------#
#                    Program Entry Point                   #
#----------------------------------------------------------#

# Command line processing - Process any options set in the
# environment first, and then those given on the command line

OPTIONS = sys.argv[1:]

envopt = os.getenv(PROGNAME.upper())
if envopt:
    OPTIONS = envopt.split() + OPTIONS

try:
    opts, args = getopt.getopt(OPTIONS, '-hpv')
except getopt.GetoptError:
    Usage()
    sys.exit(1)

for opt, val in opts:
    if opt == "-h":
        Usage()
        sys.exit(2)
    if opt == "-p":
        SHOWRECORD = True
    if opt == "-v":
        print RCSID
        sys.exit(2)


# Passing username, password mandatory, group is optional

if not 2 <= len(args) <= 3:
    Usage()
    sys.exit(1)

# Setup the passed parameters

if len(args) == 2:
    args.append(None)
    
retval =  Validate(*args)
print retval

# Set exit value accordingly

if retval:
    sys.exit(1)
else:
    sys.exit(0)