"""GNUViech administration tool helper classes. (c) 2004 Jan Dittberner """ import os from log4py import Logger, FileAppender, LOGLEVEL_DEBUG class GNVPrefs: """This class has static variables for the settings of the GNUViech administration tool. These settings may 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/" LOGDIR = BASEPREFIX+"/var/log" LOGFILE = LOGDIR+"/gnvadm.log" USERTYPES = { "web" : { "minuid" : 10000, "maxuid" : 10999, "group" : "wwwusers", "fullname" : "Webuser %s", "home" : WEBHOMEDIR + "%s", "shell" : "/bin/true", "nohome" : 1, "disabledpass" : 1 }, "pop3" : { "minuid" : 20000, "maxuid" : 29999, "group" : "poponly", "fullname" : "Popuser %s", "home" : POPHOMEDIR + "%s", "shell" : "/bin/true", "nohome" : 1, "disabledpass" : 1 } } # load custom settings execfile("gvadm.preferences") def __init__(self): self.logger = self.getLogger(self) self.setupDirs() 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" def setupDirs(self): """Setup the directories and files required for proper operation of the GNUViech administration tool.""" for directory in (self.BASEPREFIX, self.BASEPREFIX+"/etc", self.BASEPREFIX+"/var", self.GVADMDIR, self.EXIMCONFDIR, self.VIRTUALDOMDIR, self.HOMEDIR, self.POPHOMEDIR, self.WEBHOMEDIR, self.WEBLOGDIR, self.WEBSTATSDIR, self.LOGDIR): if (not os.access(directory, os.R_OK & os.X_OK)): print "making %s." % directory os.mkdir(directory) for required in (self.BASEPREFIX+"/etc/passwd", self.BASEPREFIX+"/etc/shadow", self.EXIMCONFDIR+"eximpasswords"): if (not os.access(required, os.R_OK)): print "creating %s." % required file = open(required, "w") file.close() def getLogger(self, instance): logger = Logger().get_instance(instance) logger.remove_all_targets() logger.add_target(FileAppender(self.LOGFILE)) logger.set_loglevel(LOGLEVEL_DEBUG) return logger def getNextSysId(self, type): nextid = self.USERTYPES[type]["minuid"] file = open(self.BASEPREFIX+"/etc/passwd", "r") for line in file.readlines(): pwdline = tools.splitPasswdLine(line) self.logger.debug(str(pwdline)) uid = int(pwdline["uid"]) if (uid in range(int(self.USERTYPES[type]["minuid"]), int(self.USERTYPES[type]["maxuid"])) and nextid <= uid): nextid = uid+1 return nextid def getGroupId(self, type): pass def getFullName(self, type, username): return self.USERTYPES[type]["fullname"] % username def getHomeDir(self, type, username): return self.USERTYPES[type]["home"] % username def getShell(self, type): return self.USERTYPES[type]["shell"] class NoAdmDirError(Exception): """This exception is raised if the admin directory does'nt exist.""" pass