#!/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.104 2005/04/01 22:30:33 root Exp $" VERSION = RCSID.split()[2] # Copyright Information CPRT = "(c)" DATE = "2005" OWNER = "TundraWare Inc." RIGHTS = "All Rights Reserved" COPYRIGHT = "Copyright %s %s %s %s. " % (CPRT, DATE, OWNER, RIGHTS) #----------------------------------------------------------# # Variables User Might Change # #----------------------------------------------------------# 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 ------------------# #----------------------------------------------------------# # Imports # #----------------------------------------------------------# import getopt import grp import os import pwd from socket import getfqdn import sys import tconfpy import time #----------------------------------------------------------# # Aliases & Redefinitions # #----------------------------------------------------------# #----------------------------------------------------------# # Constants & Literals # #----------------------------------------------------------# ##### # Constants ##### ##### # Literals ##### #----------------------------------------------------------# # Prompts, & Application Strings # #----------------------------------------------------------# ##### # Error Messages ##### eERROR = "ERROR" ##### # Informational Messages ##### ##### # Usage Prompts ##### uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT, "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", ] #----------------------------------------------------------# # Global Variables & Data Structures # #----------------------------------------------------------# CFGFILE = "" # Default is no config file #--------------------------- Code Begins Here ---------------------------------# #----------------------------------------------------------# # Object Base Class Definitions # #----------------------------------------------------------# #----------------------------------------------------------# # Supporting Function Definitions # #----------------------------------------------------------# ##### # Print An Error Message ##### def ErrorMsg(emsg): print PROGNAME + " " + VERSION + " " + eERROR + ": " + emsg # End of 'ErrorMsg()' ##### # Print Usage Information ##### def Usage(): for line in uTable: print line # End of 'Usage()' #----------------------------------------------------------# # 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:g:hp:v') except getopt.GetoptError: Usage() sys.exit(1) for opt, val in opts: 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) # 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) ##### # Write Out The Files ##### # 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 grfile = open(GRFILE, "w") grfile.write(TIMESTAMP) 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(TIMESTAMP) for uid in users: if uid >= STARTUID: pwfile.write("%s:%s\n" % users[uid][:]) pwfile.close()