diff --git a/validate-upg.py b/validate-upg.py index ac6412d..e6429f0 100755 --- a/validate-upg.py +++ b/validate-upg.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "validate-upg" -RCSID = "$Id: validate-upg.py,v 1.100 2005/03/02 05:55:45 tundra Exp $" +RCSID = "$Id: validate-upg.py,v 1.101 2005/03/02 07:40:51 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -34,7 +34,7 @@ import getopt import os import sys -import tconfpy +import crypt, grp, pwd #----------------------------------------------------------# @@ -53,15 +53,13 @@ # Constants ##### -FALSE = 0 == 1 # Booleans -TRUE = not FALSE - - ##### # Literals ##### +OK = "" +BAD = "Invalid User Name, Password, Or Group" #----------------------------------------------------------# @@ -86,9 +84,10 @@ ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-hv] where,\n", - " -f file configuration file to use", + "usage: " + PROGNAME + " [-hpv] username password [group]", + " where,\n", " -h print this help information", + " -p display complete password record", " -v print detailed version information", ] @@ -97,8 +96,7 @@ # Global Variables & Data Structures # #----------------------------------------------------------# -CFGFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME) # conf file - +SHOWRECORD = False #--------------------------- Code Begins Here ---------------------------------# @@ -134,6 +132,44 @@ # 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 # @@ -143,37 +179,46 @@ # 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, '-f:hv') + opts, args = getopt.getopt(OPTIONS, '-hpv') except getopt.GetoptError: Usage() sys.exit(1) for opt, val in opts: - if opt == "-f": - CFGFILE=val if opt == "-h": Usage() - sys.exit(0) + sys.exit(2) + if opt == "-p": + SHOWRECORD = True if opt == "-v": print RCSID - sys.exit(0) + sys.exit(2) -# Process the configuration file -retval = tconfpy.ParseConfig(CFGFILE) +# Passing username, password mandatory, group is optional -# Print any errors or warning generated by the parse +if not 2 <= len(args) <= 3: + Usage() + sys.exit(1) -for x in (retval.Errors, retval.Warnings): - for y in x: - y = "%s %s %s" % (PROGNAME, VERSION, " ".join(y.split()[2:])) - print y +# Setup the passed parameters -# If there were any errors, we're done -if retval.Errors: +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) +