First complete implementation with standalone code integrated as Validate() function.
1 parent a754178 commit da926c029fc7114bc9323c32af9005eff37e16e7
@tundra tundra authored on 2 Mar 2005
Showing 1 changed file
View
113
validate-upg.py
 
# 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
 
 
import getopt
import os
import sys
import tconfpy
import crypt, grp, pwd
 
 
#----------------------------------------------------------#
# Aliases & Redefinitions #
#####
# Constants
#####
 
FALSE = 0 == 1 # Booleans
TRUE = not FALSE
 
 
 
#####
# Literals
#####
 
OK = ""
BAD = "Invalid User Name, Password, Or Group"
 
 
#----------------------------------------------------------#
# Prompts, & Application Strings #
# Usage Prompts
#####
 
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",
]
 
 
#----------------------------------------------------------#
# Global Variables & Data Structures #
#----------------------------------------------------------#
 
CFGFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME) # conf file
 
SHOWRECORD = False
 
#--------------------------- Code Begins Here ---------------------------------#
 
 
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, '-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)
 
# Process the configuration file
 
retval = tconfpy.ParseConfig(CFGFILE)
 
# Print any errors or warning generated by the parse
 
for x in (retval.Errors, retval.Warnings):
for y in x:
y = "%s %s %s" % (PROGNAME, VERSION, " ".join(y.split()[2:]))
print y
 
# If there were any errors, we're done
if retval.Errors:
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)