diff --git a/mkapachepw.py b/mkapachepw.py index db2814f..2d8a465 100755 --- a/mkapachepw.py +++ b/mkapachepw.py @@ -9,7 +9,7 @@ # Program Information PROGNAME = "mkapachepw" -RCSID = "$Id: mkapachepw.py,v 1.116 2005/04/06 06:31:42 toor Exp $" +RCSID = "$Id: mkapachepw.py,v 1.117 2005/04/06 07:52:05 toor Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -86,24 +86,28 @@ ##### -# Error Messages +# Error And Warning Messages ##### -eERROR = "ERROR" - +eABORT = "Aborting ..." +eERROR = "ERROR" eFILEOPEN = "Cannot Open File '%s'." eINVALIDID = "'%s' Is An Invalid %s ID." eINVALIDNAME = "'%s' Is An Invalid %s Name." eINVALIDSTART = "Invalid Starting %s, '%s' - Must Be An Integer Value." eNOPREFIX = "'%s' Must Be Prefixed With '+' or '-' To Indicate Desired Action." +wCOLLIDE = "'%s' Entry In %s Conflicts With Entry Already In %s List." +wOVERWRITE = "Overwriting..." +wWARNING = "WARNING" + ##### # Usage Prompts ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-sGUguIihv] where,\n", + "usage: " + PROGNAME + " [-sGUguIicqhv] where,\n", " -s do not process system password/group files (default: process these files)", " -G list of groups to include (+group | +GID) or exclude (-group | -GID) (default: none)", " -U list of users to include (+user | + UID) or exclude (-user | -UID) (default: none)", @@ -111,6 +115,8 @@ " -u # smallest UID to include in output (default: 100)", " -I file include file containing other group information (default: none)", " -i file include file containing other user information (default: none)", + " -c do not permit entries to be overwritten (default: allow - only warn)", + " -q quiet mode - suppresses warning messages", " -h print this help information", " -v print detailed version information", ] @@ -121,9 +127,13 @@ #----------------------------------------------------------# enumerated = [] # Place to store command line in/exclude enumerations +includes = [] # Place to store names of files to include + groups = {} # Place to store group information users = {} # Place to store user information +ALLOWCOLLISIONS = True # Allow entries to overwrite each other (with warning) +QUIET = False # Suppress display of warning messages SYSFILES = True # Flag to enable/disable inclusion of system group/pw @@ -145,8 +155,16 @@ # Print An Error Message ##### -def ErrorMsg(emsg): - print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg +def ErrorMsg(emsg, Warning=False, Action=eABORT): + + if Warning: + if QUIET: # Quiet mode suppresses warning messages + return + prompt = wWARNING + else: + prompt = eERROR + + print PROGNAME + " " + VERSION + " " + prompt + ": " + emsg + " " + Action # End of 'ErrorMsg()' @@ -275,11 +293,17 @@ OPTIONS = envopt.split() + OPTIONS try: - opts, args = getopt.getopt(OPTIONS, '-sG:U:g:u:I:i:hv') + opts, args = getopt.getopt(OPTIONS, '-sG:U:g:u:I:i:cqhv') except getopt.GetoptError: Usage() sys.exit(1) +# This command line accepts no args +if args: + Usage() + sys.exit(1) + + for opt, val in opts: if opt == "-s": SYSFILES = False @@ -300,13 +324,13 @@ ErrorMsg(eINVALIDSTART % (UID, val)) sys.exit(1) if opt == "-I": - temp = ReadFile(val) - for entry in temp: - groups[entry[0]] = [BOGUSID, entry[1], False] + includes.append([val, groups]) if opt == "-i": - temp = ReadFile(val) - for entry in temp: - users[entry[0]] = [BOGUSID, entry[1][0], False] + includes.append([val, users]) + if opt == "-c": + ALLOWCOLLISIONS = False + if opt == "-q": + QUIET = True if opt == "-h": Usage() sys.exit(0) @@ -314,12 +338,6 @@ print RCSID sys.exit(0) -# This command line accepts no args - -if args: - Usage() - sys.exit(1) - ##### # Build List Of System Groups And Users ##### @@ -353,11 +371,50 @@ gname = grp.getgrgid(gid)[0] users[uname] = [uid, pw, Protected] + if uname not in groups[gname][1]: groups[gname][1].append(uname) ##### +# Process Included Files +##### + +for include in includes: + + # Read the file into a temporary list + filename, db = include[:] + temp = ReadFile(filename) + + # Add each entry to the appropriate in-memory database + for entry in temp: + + # Group entries have a list of members + if db == groups: + members = entry[1] + name = GROUP + + # User entries have a single password + else: + members = entry[1][0] + name = USER + + # See if this entry will overwrite an existing one + # If it will, warn if collisions are permitted + # Error out otherwise + + if entry[0] in db: + + if ALLOWCOLLISIONS: + ErrorMsg(wCOLLIDE % (entry[0], filename, name), Warning=True, Action=wOVERWRITE) + else: + ErrorMsg(wCOLLIDE % (entry[0], filename, name)) + sys.exit(4) + + db[entry[0]] = [BOGUSID, members, False] + + +##### # Process Any Enumerated Inclusions/Exclusions #####