Completed a possible implementation for command line ex/inclusion.
1 parent e6b0dcb commit 4d7c25d8b78fa18da9b74b1f80cb02fdc448f374
@root root authored on 4 Apr 2005
Showing 1 changed file
View
129
mkapachepw.py
 
# Program Information
 
PROGNAME = "mkapachepw"
RCSID = "$Id: mkapachepw.py,v 1.106 2005/04/02 09:03:53 root Exp $"
RCSID = "$Id: mkapachepw.py,v 1.107 2005/04/04 21:41:37 root Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
 
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 ---------------------------------#
 
# End of 'Usage()'
 
 
#####
# Process An Enumerated List Of Groups/Users To Include Or Exclude
#####
 
def ProcessEnumeratedList(items, master, excluded, included, name):
# Process An Enumerated List Of Groups/Users To Include Or Exclude.
#
# The 'items' argument must be a string with the names or numbers to
# process, with a '-' or '+' prepended to indicate Delete or Add,
# respectively.
#####
 
def ProcessEnumeratedList(items, master, lookup, name):
 
for item in items.split():
orig = item
# Exclude Processing
# Verify argument is in correct format and determine type of
# operation desired.
 
if item[0] == '-':
item = item[1:]
savein = excluded
 
# Include Processing
additem = False
 
elif item[0] == '+':
item = item[1:]
savein = included
 
# Bad Format
additem = True
 
else:
ErrorMsg("'%s' Must Be Prefixed With '+' or '-' To Indicate Desired Action." % item)
sys.exit(2)
 
# See if it's a number
item = item[1:] # We just need the item ID portion
 
# See if it's a GID/UID (a number)
try:
item = int(item)
 
# Make sure it even exists
 
if item not in master:
ErrorMsg("%s '%s' Does Not Exist!" % (name, orig[1:]))
ErrorMsg("'%s' Is An Invalid %s ID." % (item, name))
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)
 
# Handle the case where the name does not exist
try:
item = lookup(item)[2]
 
except:
ErrorMsg("'%s' Is An Invalid %s Name." % (orig[1:], name))
sys.exit(2)
 
 
print additem, item
 
# Do the actual in/exclusion
 
# Include
if additem:
master[item][2] = True # Mark entry as protected
 
# Exclude
else:
del master[item]
 
# End of 'ProcessEnumeratedList(()'
 
 
# Build An Internal List Of Groups And Users Before Doing Anything Else.
# Command Line Parsing May Need This Information.
#####
 
Protected = False
 
#####
# Build List Of Groups
#####
 
for group in grp.getgrall():
 
gname, gpw, gid, gmembers = group[:4]
 
groups[gid] = (gname, [])
groups[gid] = [gname, [], Protected]
for member in gmembers:
groups[gid][1].append(member)
 
#####
for user in pwd.getpwall():
 
uname, pw, uid, gid = user[:4]
 
users[uid] = (uname, pw)
users[uid] = [uname, pw, Protected]
if uname not in groups[gid][1]:
groups[gid][1].append(uname)
 
 
sys.exit(1)
 
for opt, val in opts:
if opt == "-G":
ProcessEnumeratedList(val, groups, groups_excluded, groups_included, "Group")
ProcessEnumeratedList(val, groups, grp.getgrnam, "Group")
if opt == "-U":
ProcessEnumeratedList(val, users, users_excluded, users_included, "User")
ProcessEnumeratedList(val, users, pwd.getpwnam, "User")
if opt == "-f":
CFGFILE=val
if opt == "-g":
STARTGID=int(val)
if opt == "-v":
print RCSID
sys.exit(0)
 
print groups_excluded, groups_included, users_excluded, users_included
 
 
print users, groups
 
#####
# Write Out The Files
#####
 
grfile = open(GRFILE, "w")
grfile.write(TIMESTAMP)
 
# Write out groups if they are either protected or >= specified starting ID
 
for gid in groups:
if gid >= STARTGID:
if (groups[gid][2]) or (gid >= STARTGID):
grfile.write("%s: %s\n" % (groups[gid][0], " ".join(groups[gid][1])))
 
grfile.close()
 
 
pwfile = open(PWFILE, "w")
pwfile.write(TIMESTAMP)
 
# Write out users if they are either protected or >= specified starting ID
 
for uid in users:
if uid >= STARTUID:
pwfile.write("%s:%s\n" % users[uid][:])
print users[uid]
if (users[uid][2]) or (uid >= STARTUID):
pwfile.write("%s:%s\n" % tuple(users[uid])[:2])
 
pwfile.close()