Added code to produce output files, observing STARTGID and STARTUID.
Added command line options to set STARTGID and STARTUID.
1 parent 8711d05 commit b3f2ed1c5acbb9f0ab6b7a53de672217760f154d
@root root authored on 1 Apr 2005
Showing 1 changed file
View
117
mkapachepw.py
#!/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
 
#----------------------------------------------------------#
# 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 ------------------#
 
import os
import pwd
import sys
import tconfpy
import time
 
 
#----------------------------------------------------------#
# Aliases & Redefinitions #
# Usage Prompts
#####
 
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",
]
 
 
# Global Variables & Data Structures #
#----------------------------------------------------------#
 
CFGFILE = "" # Default is no config file
DEBUG = False # Request debug output
 
 
#--------------------------- Code Begins Here ---------------------------------#
 
 
if envopt:
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)
 
# Build List Of Groups
#####
 
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
 
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 "%s: %s" % (x, " ".join(groups[x]))
print
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)
 
# Group File
 
grfile = open(GRFILE, "w")
grfile.write("# Created By %s %s On %s\n" % (PROGNAME, VERSION, time.asctime()))
 
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()