Initial cut of basic group and user database extraction logic.
1 parent 9394301 commit aeef7cb09c8cfd3e156544fcc154a712bff5aa32
@root root authored on 1 Apr 2005
Showing 1 changed file
View
153
mkapachepw.py
 
# Program Information
 
PROGNAME = "mkapachepw"
RCSID = "$Id: mkapachepw.py,v 1.100 2005/03/31 23:12:16 tundra Exp $"
RCSID = "$Id: mkapachepw.py,v 1.101 2005/04/01 06:08:38 root Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#----------------------------------------------------------#
# Variables User Might Change #
#----------------------------------------------------------#
 
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
import sys
import tconfpy
 
 
#####
 
uTable = [PROGNAME + " " + VERSION + " - %s\n" % COPYRIGHT,
"usage: " + PROGNAME + " [-fhv] where,\n",
" -d dump debug output",
" -f file configuration file to use",
" -h print this help information",
" -v print detailed version information",
]
#----------------------------------------------------------#
# Global Variables & Data Structures #
#----------------------------------------------------------#
 
CFGFILE = os.path.join(os.getenv("HOME"), "." + PROGNAME) # conf file
 
CFGFILE = "" # Default is no config file
DEBUG = False # Request debug output
 
#--------------------------- Code Begins Here ---------------------------------#
 
 
#----------------------------------------------------------#
# 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:hv')
opts, args = getopt.getopt(OPTIONS, '-df:hv')
except getopt.GetoptError:
Usage()
sys.exit(1)
 
for opt, val in opts:
if opt == "-d":
DEBUG = True
if opt == "-f":
CFGFILE=val
if opt == "-h":
Usage()
if opt == "-v":
print RCSID
sys.exit(0)
 
# Process the configuration file
 
retval = tconfpy.ParseConfig(CFGFILE)
 
# Print any errors or warning generated by the parse
 
for x in (retval.Errors, retval.Warnings):
for y in x:
y = "%s %s %s" % (PROGNAME, VERSION, " ".join(y.split()[2:]))
print y
 
# If there were any errors, we're done
if retval.Errors:
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 = {}
gid2name = {}
 
for group in grp.getgrall():
gname, gpw, gid, gmembers = group[:4]
gid2name[gid] = gname
groups[gname] = []
for member in gmembers:
groups[gname].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
 
if uname not in groups[gname]:
groups[gname].append(uname)
 
#####
# Dump Results If Asked
#####
 
 
if DEBUG:
for x in users:
print x, users[x]
print
 
 
grlist = gid2name.keys()
grlist.sort()
 
for y in grlist:
x = gid2name[y]
print x, groups[x]
print