#!/usr/bin/env python
#
# $Id: wimd_pwfile.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Read a WhackyChat server password file.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: wimd_pwfile.py,v $',
'rcs_id' : '$Id: wimd_pwfile.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'UNSPECIFIED',
'created' : 'Sun, 01-Apr-2001 13:26:16 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.4 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
import string
import UserDict
import crypt
import whrandom
#
# Import Local modules
#
#
# Module
#
class wimd_pwfile(UserDict.UserDict):
"""
This class provides easy access to a wimd
style password file.
"""
#
# Define the "enum" to get to various fields.
#
MONIKER, PASSWORD, UID, GROUP, UNKNOWN, EMAIL = range(6)
def __init__(self, filename):
print 'reading password file %s' % filename
self.filename = filename
self.random_generator = whrandom.whrandom()
UserDict.UserDict.__init__(self)
self.parse_file()
return
def parse_file(self):
file = open(self.filename, 'rt')
contents = file.readlines()
user_info = {}
for line in contents:
line = string.strip(line)
if line and line[0] != '#':
parts = string.split(line, ':')
user_info[parts[2]] = parts
self.data = user_info
file.close()
return
def store(self):
"""
Store the info we have back to the file
from which we read it.
"""
output=open(self.filename, 'wt')
uids = self.keys()
uids.sort()
for uid in uids:
output.write('%s\n' % string.join(self[uid], ':'))
output.close()
return
#
# Methods to return a particular piece of information
# about a user
#
def get_field(self, uid, field):
return self[uid][field]
def get_moniker(self, uid):
return self.get_field(uid, self.MONIKER)
def get_password(self, uid):
return self.get_field(uid, self.PASSWORD)
def get_group(self, uid):
return self.get_field(uid, self.GROUP)
def get_unknown(self, uid):
return self.get_field(uid, self.UNKNOWN)
def get_email(self, uid):
return self.get_field(uid, self.EMAIL)
#
# Methods to set a particular piece of information
# about a user
#
def set_field(self, uid, field, value):
rec = self[uid]
new_rec = tuple(rec[:field]) + (value,) + tuple(rec[field+1:])
self[uid] = new_rec
return
def set_moniker(self, uid, value):
return self.set_field(uid, self.MONIKER, value)
def set_password(self, uid, value):
salt = self.random_generator.choice(string.letters) + \
self.random_generator.choice(string.letters)
value = crypt.crypt(value, salt)
return self.set_field(uid, self.PASSWORD, value)
def set_group(self, uid, value):
return self.set_field(uid, self.GROUP, value)
def set_unknown(self, uid, value):
return self.set_field(uid, self.UNKNOWN, value)
def set_email(self, uid, value):
return self.set_field(uid, self.EMAIL, value)
|