diff --git a/mkapachepw.py b/mkapachepw.py index 779dd47..e9b7a4b 100755 --- a/mkapachepw.py +++ b/mkapachepw.py @@ -1,12 +1,15 @@ #!/usr/bin/env python # mkapachepw.py +# Generate Apache-Compatible Password And Group Files +# From Unix System Passwords And Groups. +# # Copyright (c) 2005 TundraWare Inc. All Rights Reserved. # For Updates See: http://www.tundraware.com/Software/mkapachepw # Program Information PROGNAME = "mkapachepw" -RCSID = "$Id: mkapachepw.py,v 1.102 2005/04/01 18:17:55 root Exp $" +RCSID = "$Id: mkapachepw.py,v 1.103 2005/04/01 22:21:35 root Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -22,8 +25,11 @@ # Variables User Might Change # #----------------------------------------------------------# -STARTUID = 100 # User IDs below this ignored -STARTGID = 100 # Group IDS below this ignored +GRFILE = "./groups" # Group output file +PWFILE = "./passwords" # Password output file +STARTUID = 100 # User IDs below this ignored +STARTGID = 100 # Group IDS below this ignored + #------------------- Nothing Below Here Should Need Changing ------------------# @@ -39,6 +45,7 @@ import pwd import sys import tconfpy +import time #----------------------------------------------------------# @@ -87,10 +94,11 @@ ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, - "usage: " + PROGNAME + " [-fhv] where,\n", - " -d dump debug output", - " -f file configuration file to use", + "usage: " + PROGNAME + " [-fghpv] where,\n", + " -f file configuration file to use (default: none)", + " -g # smallest GID to include in output (default: 100)", " -h print this help information", + " -p # smallest UID to include in output (default: 100)", " -v print detailed version information", ] @@ -100,7 +108,7 @@ #----------------------------------------------------------# CFGFILE = "" # Default is no config file -DEBUG = False # Request debug output + #--------------------------- Code Begins Here ---------------------------------# @@ -152,19 +160,21 @@ OPTIONS = envopt.split() + OPTIONS try: - opts, args = getopt.getopt(OPTIONS, '-df:hv') + opts, args = getopt.getopt(OPTIONS, '-f:g:hp:v') except getopt.GetoptError: Usage() sys.exit(1) for opt, val in opts: - if opt == "-d": - DEBUG = True if opt == "-f": CFGFILE=val + if opt == "-g": + STARTGID=int(val) if opt == "-h": Usage() sys.exit(0) + if opt == "-p": + STARTUID=int(val) if opt == "-v": print RCSID sys.exit(0) @@ -192,47 +202,54 @@ ##### groups = {} -gid2name = {} - for group in grp.getgrall(): + gname, gpw, gid, gmembers = group[:4] - gid2name[gid] = gname - - groups[gname] = [] + groups[gid] = (gname, []) + for member in gmembers: - groups[gname].append(member) + groups[gid][1].append(member) ##### # Build A List Of Users ##### users = {} - for user in pwd.getpwall(): uname, pw, uid, gid = user[:4] - gname = gid2name[gid] - - users[uname] = pw + users[uid] = (uname, pw) - if uname not in groups[gname]: - groups[gname].append(uname) + if uname not in groups[gid][1]: + groups[gid][1].append(uname) + ##### -# Dump Results If Asked +# Write Out The Files ##### +# Files Should Be Read-Only -if DEBUG: - for x in users: - print x, users[x] - print +os.umask(0377) +# Group File - grlist = gid2name.keys() - grlist.sort() +grfile = open(GRFILE, "w") +grfile.write("# Created By %s %s On %s\n" % (PROGNAME, VERSION, time.asctime())) - for y in grlist: - x = gid2name[y] - print "%s: %s" % (x, " ".join(groups[x])) - print +for gid in groups: + if gid >= STARTGID: + grfile.write("%s: %s\n" % (groups[gid][0], " ".join(groups[gid][1]))) + +grfile.close() + +# Password File + +pwfile = open(PWFILE, "w") +pwfile.write("# Created By %s %s On %s\n" % (PROGNAME, VERSION, time.asctime())) + +for uid in users: + if uid >= STARTUID: + pwfile.write("%s:%s\n" % users[uid][:]) + +pwfile.close()