Installed command line support and some of the logic for
explicitly listing included- and excluded users or groups.
1 parent 7f8a998 commit e6b0dcbd04ef22aacb8584588a2e946aeee209b9
@root root authored on 2 Apr 2005
Showing 1 changed file
View
206
mkapachepw.py
 
# 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
 
#----------------------------------------------------------#
# 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
 
 
 
#------------------- Nothing Below Here Should Need Changing ------------------#
import os
import pwd
from socket import getfqdn
import sys
import tconfpy
import time
 
 
#----------------------------------------------------------#
#####
# Literals
#####
 
TIMESTAMP = "# Created By %s %s On %s At %s\n" % (PROGNAME, VERSION, getfqdn(), time.asctime())
 
 
#----------------------------------------------------------#
# Prompts, & Application Strings #
# Usage Prompts
#####
 
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",
" -p # smallest UID to include in output (default: 100)",
#----------------------------------------------------------#
# 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 ---------------------------------#
 
 
# 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
if envopt:
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":
STARTGID=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)
print groups_excluded, groups_included, users_excluded, users_included
 
 
 
#####
# 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")