diff --git a/mkapachepw.py b/mkapachepw.py index 1c35aaa..f7ed375 100755 --- a/mkapachepw.py +++ b/mkapachepw.py @@ -9,7 +9,7 @@ # Program Information PROGNAME = "mkapachepw" -RCSID = "$Id: mkapachepw.py,v 1.105 2005/04/02 00:26:03 root Exp $" +RCSID = "$Id: mkapachepw.py,v 1.106 2005/04/02 09:03:53 root Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -25,10 +25,10 @@ # Variables User Might Change # #----------------------------------------------------------# -GRFILE = "./.htgroups" # Group output file -PWFILE = "./.htpasswords" # Password output file -STARTUID = 100 # User IDs below this ignored -STARTGID = 100 # Group IDS below this ignored +GRFILE = "./.htgroups" # Group output file +PWFILE = "./.htpasswords" # Password output file +STARTUID = 100 # User IDs below this ignored +STARTGID = 100 # Group IDS below this ignored @@ -45,7 +45,6 @@ import pwd from socket import getfqdn import sys -import tconfpy import time @@ -71,6 +70,7 @@ # Literals ##### +TIMESTAMP = "# Created By %s %s On %s At %s\n" % (PROGNAME, VERSION, getfqdn(), time.asctime()) #----------------------------------------------------------# @@ -95,7 +95,9 @@ ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-fghpv] where,\n", + "usage: " + PROGNAME + " [-GUfghpv] where,\n", + " -G list of groups to include (+group) or exclude (-group) (default: none)", + " -U list of users to include (+user) or exclude (-user) (default: none)", " -f file configuration file to use (default: none)", " -g # smallest GID to include in output (default: 100)", " -h print this help information", @@ -108,7 +110,16 @@ # Global Variables & Data Structures # #----------------------------------------------------------# -CFGFILE = "" # Default is no config file +CFGFILE = "" # Default is no config file + +groups = {} # Place to store group information +groups_excluded = [] # List of GIDs *not* to include in output +groups_included = [] # List of GIDs to *always* include in output + +users = {} # Place to store user information +users_excluded = [] # List of UIDs *not* to include in output +users_included = [] # List of UIDs to *always* include in output + #--------------------------- Code Begins Here ---------------------------------# @@ -146,10 +157,85 @@ # End of 'Usage()' +##### +# Process An Enumerated List Of Groups/Users To Include Or Exclude +##### + +def ProcessEnumeratedList(items, master, excluded, included, name): + + for item in items.split(): + orig = item + + # Exclude Processing + + if item[0] == '-': + item = item[1:] + savein = excluded + + # Include Processing + + elif item[0] == '+': + item = item[1:] + savein = included + + # Bad Format + + else: + ErrorMsg("'%s' Must Be Prefixed With '+' or '-' To Indicate Desired Action." % item) + sys.exit(2) + + # See if it's a number + try: + item = int(item) + if item not in master: + ErrorMsg("%s '%s' Does Not Exist!" % (name, orig[1:])) + sys.exit(2) + + # If not, assume it is a name and look it up + except ValueError: + print item + + if item not in savein: + savein.append(item) + +# End of 'ProcessEnumeratedList(()' + + #----------------------------------------------------------# # Program Entry Point # #----------------------------------------------------------# + +##### +# Build An Internal List Of Groups And Users Before Doing Anything Else. +# Command Line Parsing May Need This Information. +##### + +##### +# Build List Of Groups +##### + +for group in grp.getgrall(): + + gname, gpw, gid, gmembers = group[:4] + + groups[gid] = (gname, []) + for member in gmembers: + groups[gid][1].append(member) + +##### +# Build A List Of Users +##### + +for user in pwd.getpwall(): + + uname, pw, uid, gid = user[:4] + + users[uid] = (uname, pw) + if uname not in groups[gid][1]: + groups[gid][1].append(uname) + + ##### # Command line processing - Process any options set in the # environment first, and then those given on the command line @@ -161,12 +247,16 @@ OPTIONS = envopt.split() + OPTIONS try: - opts, args = getopt.getopt(OPTIONS, '-f:g:hp:v') + opts, args = getopt.getopt(OPTIONS, '-G:U:f:g:hp:v') except getopt.GetoptError: Usage() sys.exit(1) for opt, val in opts: + if opt == "-G": + ProcessEnumeratedList(val, groups, groups_excluded, groups_included, "Group") + if opt == "-U": + ProcessEnumeratedList(val, users, users_excluded, users_included, "User") if opt == "-f": CFGFILE=val if opt == "-g": @@ -180,49 +270,8 @@ print RCSID sys.exit(0) +print groups_excluded, groups_included, users_excluded, users_included -# Process the configuration file, if any - -if CFGFILE: - - retval = tconfpy.ParseConfig(CFGFILE, CallingProgram="%s %s " % (PROGNAME, VERSION)) - - # Print any errors or warning generated by the parse - - for x in (retval.ErrMsgs, retval.WarnMsgs): - for y in x: - print y - - # If there were any errors, we're done - if retval.ErrMsgs: - sys.exit(0) - - -##### -# Build List Of Groups -##### - -groups = {} -for group in grp.getgrall(): - - gname, gpw, gid, gmembers = group[:4] - groups[gid] = (gname, []) - - for member in gmembers: - groups[gid][1].append(member) - -##### -# Build A List Of Users -##### - -users = {} -for user in pwd.getpwall(): - - uname, pw, uid, gid = user[:4] - users[uid] = (uname, pw) - - if uname not in groups[gid][1]: - groups[gid][1].append(uname) ##### @@ -232,7 +281,6 @@ # Files Should Be Read-Only os.umask(0377) -TIMESTAMP = "# Created By %s %s On %s At %s\n" % (PROGNAME, VERSION, getfqdn(), time.asctime()) # Group File