1
0
Fork 0

- added gnuviech package

- better code structure
- class for domain
- class for preferences
- password generation function


git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@82 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
Jan Dittberner 2004-12-25 23:09:19 +00:00
parent e18a12269f
commit a7edeca826
8 changed files with 235 additions and 74 deletions

101
backend/GNVAdm.py Normal file
View file

@ -0,0 +1,101 @@
"""Package for GNUViech Admin main types and functions
(c) Copyright 2004 Jan Dittberner, IT-Consulting & Solutions
Germany
"""
import os
import gnuviech
class DomainNotExistentError(Exception): pass
class DomainFileNotExistentError(Exception): pass
class GNVDomain:
"""Represents a domain in the GNUViech admin tool"""
def __init__(self, domain):
"""Initializes the domain object"""
self.name = domain
try:
self.findUser()
except gnuviech.NoAdmDirError:
gnuviech.setupDirs()
self.__init__(domain)
except DomainFileNotExistentError:
self.createDomainFile()
self.__init__(domain)
except DomainNotExistentError:
self.createUser()
def __repr__(self):
retval = "Domain "+self.name
if not self.username is None:
retval += ", User "+self.username
else:
retval += ", new domain"
return retval
def createDomainFile(self):
"""Create the domain user id map file."""
file = open(gnuviech.GNVPrefs.GVADMDIR+"domains", "w")
file.close()
def createUser(self):
"""Create a user for the domain."""
file = open(gnuviech.GNVPrefs.GVADMDIR+"domains", "r")
id = 0
for line in file.readlines():
(key, value) = line.split(":")
if (int(value) > id): id = int(value)
file.close()
id += 1
file = open(gnuviech.GNVPrefs.GVADMDIR+"domains", "a")
file.write("%s:%d\n" % (self.name, id))
file.close()
self.findUser()
def findUser(self):
"""Finds the user for the domain."""
self.username = None
if (os.access(gnuviech.GNVPrefs.GVADMDIR, os.R_OK)):
try:
domainsfile = open(gnuviech.GNVPrefs.GVADMDIR+"domains", "r")
for line in domainsfile.readlines():
(key, value) = line.split(":")
if (key == self.name):
self.username = "%s%02d" % (
gnuviech.GNVPrefs.USERPREFIX,
int(value))
domainsfile.close()
if self.username is None:
raise DomainNotExistentError
except IOError:
raise DomainFileNotExistentError
else:
raise gnuviech.NoAdmDirError
def getMaxPop3Id(self):
maxid = 0
try:
passwdfile = open(gnuviech.GNVPrefs.BASEPREFIX+"/etc/passwd", "r")
for line in passwdfile.readlines():
(login, passwd, uid, gid, name, dir, shell) = line.split(":")
if login.startswith(self.username + "p"):
id = int(login[len(self.username):])
print id
if (id > maxid): maxid = id
except IOError:
pass
return maxid
def getNextUser(self, usertype):
"""Gets the next user for the given type."""
if (usertype == "web"):
return self.username
if (usertype == "pop3"):
return "%sp%d" % (self.username, self.getMaxPop3Id()+1)
if __name__ == "__main__":
dom = GNVDomain("dittberner.info")
print dom

View file

@ -1,37 +0,0 @@
"""Package for GNUViech Admin main types and functions
(c) Copyright 2004 Jan Dittberner, IT-Consulting & Solutions
Germany
"""
import os
execfile('gvadm.preferences')
class GNVDomain:
"""Represents a domain in the GNUViech admin tool"""
def __init__(self, domain):
"""Initializes the domain object"""
self.domainname = domain
self.findUser()
def __repr__(self):
return "Domain "+self.domainname+", User "+self.username
def findUser(self):
"""Finds the user for the domain or creates a new one."""
if (os.access(GVADMDIR, os.R_OK)):
try:
domainsfile = open(GVADMDIR+"domains", "r")
self.username = "<from file>"
except IOError:
print "domain file not accessible"
self.username = "<unknown>"
else:
print "The directory "+GVADMDIR+" is not accessible!"
self.username = "<unknown>"
if __name__ == "__main__":
dom = GNVDomain("test.de")
print dom

View file

@ -0,0 +1,67 @@
"""GNUViech administration tool helper classes.
(c) 2004 Jan Dittberner <jan@gnuviech.info>
"""
import os
class GNVPrefs:
"""This class has static variables for the settings of the GNUViech
administration tool. These settings can be customized in the file
gvadm.preferences."""
# define standard values
PWDMINLENGTH = 6
PWDMAXLENGTH = 12
PWDCHARS = range(ord('a'), ord('z'))
PWDCHARS.extend(range(ord('A'), ord('Z')))
PWDCHARS.extend(range(ord('0'), ord('9')))
PWDCHARS.extend((ord('@'), ord('#'), ord(','), ord('.'), ord('*'),
ord('+'), ord('-'), ord('='), ord('!'), ord('$'),
ord('"')))
USERPREFIX = "usr"
BASEPREFIX = ""
GVADMDIR = BASEPREFIX+"/etc/gvadm/"
EXIMCONFDIR = BASEPREFIX+"/etc/exim/"
VIRTUALDOMDIR = EXIMCONFDIR+"virtual/"
HOMEDIR = BASEPREFIX+"/home"
POPHOMEDIR = HOMEDIR+"/mail/"
WEBHOMEDIR = HOMEDIR+"/www/"
WEBLOGDIR = WEBHOMEDIR+"logs/"
WEBSTATSDIR = WEBHOMEDIR+"stats/"
# load custom settings
execfile("gvadm.preferences")
def __repr__(self):
items = dir(self)
items.sort()
return "gnuviech.GNVPrefs\n\t" + "\n\t".join(["%s = %s" %
(item, getattr(self, item)) for item in items if getattr(self, item).__class__ in (str, int, list, dict)]) + "\n"
class NoAdmDirError(Exception):
"""This exception is raised if the admin directory does'nt exist."""
pass
def setupDirs():
"""Setup the directories and files required for proper operation of the
GNUViech administration tool."""
for directory in (GNVPrefs.BASEPREFIX,
GNVPrefs.BASEPREFIX+"/etc",
GNVPrefs.GVADMDIR,
GNVPrefs.EXIMCONFDIR,
GNVPrefs.VIRTUALDOMDIR,
GNVPrefs.HOMEDIR,
GNVPrefs.POPHOMEDIR,
GNVPrefs.WEBHOMEDIR,
GNVPrefs.WEBLOGDIR,
GNVPrefs.WEBSTATSDIR):
if (not os.access(directory, os.R_OK & os.X_OK)):
print "making %s." % directory
os.mkdir(directory)
for required in (GNVPrefs.BASEPREFIX+"/etc/passwd",
GNVPrefs.BASEPREFIX+"/etc/shadow",
GNVPrefs.EXIMCONFDIR+"eximpasswords"):
if (not os.access(required, os.R_OK)):
print "creating %s." % required
file = open(required, "w")
file.close()

20
backend/gnuviech/tools.py Normal file
View file

@ -0,0 +1,20 @@
"""Tool functions for GNUViech administration tool
(c) 2004 Jan Dittberner <jan@gnuviech.info>
"""
from random import Random
from gnuviech import GNVPrefs
def generatePassword():
r = Random()
devrnd = open("/dev/random", "r")
r.seed(ord(devrnd.read(1)))
devrnd.close()
return "".join([chr(char) for char in
r.sample(GNVPrefs.PWDCHARS,
r.randint(GNVPrefs.PWDMINLENGTH,
GNVPrefs.PWDMAXLENGTH))])
# regex für email check
# p = re.compile(u"^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9\-]+(\.|[a-zA-Z0-9\-]+)*\.[a-z]{2,5})$")

View file

@ -1,10 +0,0 @@
USERPREFIX = "usr"
BASEPREFIX = ""
GVADMDIR = BASEPREFIX+"/etc/gvadm/"
EXIMCONFDIR = BASEPREFIX+"/etc/exim/"
VIRTUALDOMDIR = EXIMCONFDIR+"virtual/"
HOMEDIR = BASEPREFIX+"/home"
POPHOMEDIR = HOMEDIR+"/mail/"
WEBHOMEDIR = HOMEDIR+"/www/"
WEBLOGDIR = WEBHOMEDIR+"logs/"
WEBSTATSDIR = WEBHOMEDIR+"stats/"

View file

@ -1,2 +0,0 @@
import mailtools

View file

@ -1,10 +1,8 @@
#! /usr/bin/env python #! /usr/bin/env python
import os, string import os, string
import GVAdm import gnuviech
from UserDict import UserDict from GNVAdm import GNVDomain
execfile('gvadm.preferences')
# if [ -n $USERPREFIX ]; then # if [ -n $USERPREFIX ]; then
# USERPREFIX="usr" # USERPREFIX="usr"
@ -29,11 +27,15 @@ execfile('gvadm.preferences')
# echo "Herzlich willkommen auf dem GNU-Viech" | mail -s "Willkommen auf dem GNU-Viech" ${NEWUSER} # echo "Herzlich willkommen auf dem GNU-Viech" | mail -s "Willkommen auf dem GNU-Viech" ${NEWUSER}
# echo added new pop3 user $NEWUSER with password $NEWPASS # echo added new pop3 user $NEWUSER with password $NEWPASS
class MailAccount(UserDict): class MailAccount:
def __init__(self, domain): def __init__(self, domain):
"Initialize a MailAccount instance for a given domain" "Initialize a MailAccount instance for a given domain"
UserDict.__init__(self) if (not os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK & os.X_OK)):
self["domainname"] = domain self.setupDirs()
self.domain = domain
def setupDirs(self):
os.mkdir(gnuviech.GNVPrefs.VIRTUALDOMDIR)
class MailAlias(MailAccount): class MailAlias(MailAccount):
"""This represents a mail alias""" """This represents a mail alias"""
@ -41,46 +43,46 @@ class MailAlias(MailAccount):
def __init__(self, domain): def __init__(self, domain):
"Initialize the POPAccount class for a given domain" "Initialize the POPAccount class for a given domain"
MailAccount.__init__(self, domain) MailAccount.__init__(self, domain)
self["aliases"] = {} self.aliases = {}
self.readAll() self.readAll()
def readAll(self): def readAll(self):
"""reads the aliasfile for the given domain""" """reads the aliasfile for the given domain"""
self["aliases"] = {} self.aliases = {}
if (os.access(VIRTUALDOMDIR, os.R_OK)): if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK)):
try: try:
aliasfile = open(VIRTUALDOMDIR+self["domainname"], 'r') aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name , 'r')
for line in aliasfile.readlines(): for line in aliasfile.readlines():
keyvals = string.split(line,":",1) keyvals = string.split(line,":",1)
self["aliases"][keyvals[0]] = keyvals[1].strip() self.aliases[keyvals[0]] = keyvals[1].strip()
aliasfile.close() aliasfile.close()
except IOError: except IOError:
print "couldn't read the aliasfile for "+self["domainname"]+"." print "couldn't read the aliasfile for "+self.domain.name+"."
else: else:
print "couldn't read from "+VIRTUALDOMDIR+"." print "couldn't read from "+gnuviech.GNVPrefs.VIRTUALDOMDIR+"."
def writeAll(self): def writeAll(self):
"""writes the aliasfile for the given domain with the aliases defined """writes the aliasfile for the given domain with the aliases defined
in the dictionary object aliases""" in the dictionary object aliases"""
if (os.access(VIRTUALDOMDIR, os.W_OK)): if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.W_OK)):
try: try:
aliasfile = open(VIRTUALDOMDIR+self["domainname"], 'w') aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name, 'w')
keys = self["aliases"].keys(); keys = self.aliases.keys();
keys.sort(); keys.sort();
for key in keys: for key in keys:
aliasfile.write(key+":"+self["aliases"][key]+"\n") aliasfile.write("%s:%s" % (key, self.aliases[key]) + "\n")
aliasfile.close() aliasfile.close()
except IOError: except IOError:
print "writing to aliasfile failed." print "writing to aliasfile failed."
else: else:
print "no write access to directory "+VIRTUALDOMDIR+"." print "no write access to directory "+gnuviech.GNVPrefs.VIRTUALDOMDIR+"."
def setAlias(self, alias, target): def setAlias(self, alias, target):
"""sets a mail alias for given domain which directs the MTA to the """sets a mail alias for given domain which directs the MTA to the
given target given target
""" """
self.readAll() self.readAll()
self["aliases"][alias]=target self.aliases[alias]=target
self.writeAll() self.writeAll()
class POP3Account(MailAccount): class POP3Account(MailAccount):
@ -90,16 +92,16 @@ class POP3Account(MailAccount):
"""Creates a pop3/imap account for the domain""" """Creates a pop3/imap account for the domain"""
print self print self
print "adding address "+address print "adding address "+address
alias = MailAlias(self["domainname"]) alias = MailAlias(self.domain)
alias.setAlias(address, "usr03") alias.setAlias(address, self.domain.getNextUser("pop3"))
print alias print alias
if __name__ == "__main__": if __name__ == "__main__":
popacc = POP3Account("test.de") popacc = POP3Account(GNVDomain("dittberner.info"))
print popacc print popacc
popacc.create("webmaster") popacc.create("test")
alias = MailAlias("test.de") alias = MailAlias(GNVDomain("dittberner.info"))
print alias print alias
alias.setAlias("klaus", "klaus@dittberner.info") alias.setAlias("klaus", "klaus@dittberner.info")
print alias print alias

20
backend/test.py Normal file
View file

@ -0,0 +1,20 @@
import gnuviech
import gnuviech.tools
if __name__ == "__main__":
print gnuviech.GNVPrefs()
minlen = 0
avglen = 0
maxlen = 0
pwds = 20
for i in range(pwds):
pwd = gnuviech.tools.generatePassword()
print "%02d: %s (%d)" % (i, pwd, len(pwd))
if (minlen == 0) or (len(pwd) < minlen): minlen = len(pwd)
if (len(pwd) > maxlen): maxlen = len(pwd)
avglen += len(pwd)
avglen = avglen/pwds
print """average password length: %d
minimum password length: %d
maximum password length: %d""" % (avglen, minlen, maxlen)