Jan Dittberner
fa6961463e
- make mail aliases and pop3 accounts domain properties - add class for system users - move GNVDomain class to gnuviech package - add more logging - add password hashing, passwd and shadow functions to gnuviech.tools git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@87 a67ec6bc-e5d5-0310-a910-815c51eb3124
62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
from gnuviech import tools
|
|
|
|
class UserNotInPasswdError(Exception): pass
|
|
|
|
class NoPasswordInShadowError(Exception): pass
|
|
|
|
class SystemUser:
|
|
def __init__(self, prefs, username):
|
|
self.prefs = prefs
|
|
self.logger = prefs.getLogger(self)
|
|
self.getUser(username)
|
|
self.logger.debug(str(self))
|
|
|
|
def getUser(self, username):
|
|
pwdfile = open(self.prefs.BASEPREFIX+"/etc/passwd", "r")
|
|
for line in pwdfile.readlines():
|
|
pwdline = tools.splitPasswdLine(line)
|
|
self.logger.debug("PWDLINE: %s" % pwdline)
|
|
if pwdline["loginname"] == username:
|
|
self.username = pwdline["loginname"]
|
|
self.password = self.getPassword()
|
|
self.uid = pwdline["uid"]
|
|
self.gid = pwdline["gid"]
|
|
self.fullname = pwdline["fullname"]
|
|
self.homedir = pwdline["homedir"]
|
|
self.shell = pwdline["shell"]
|
|
return
|
|
pwdfile.close()
|
|
raise UserNotInPasswdError
|
|
|
|
def getPassword(self):
|
|
shadowfile = open(self.prefs.BASEPREFIX+"/etc/shadow", "r")
|
|
for line in shadowfile.readlines():
|
|
shadowline = tools.splitShadowLine(line)
|
|
self.logger.debug("SHADOWLINE: %s" % shadowline)
|
|
if shadowline["loginname"] == self.username:
|
|
shadowfile.close()
|
|
return shadowline["passwordhash"]
|
|
shadowfile.close()
|
|
raise NoPasswordInShadowError
|
|
|
|
def createUser(prefs, username, type):
|
|
line = ":".join((username, "x",
|
|
str(prefs.getNextSysId(type)),
|
|
str(prefs.getGroupId(type)),
|
|
prefs.getFullName(type, username),
|
|
prefs.getHomeDir(type, username),
|
|
prefs.getShell(type)))
|
|
passwdfile = open(prefs.BASEPREFIX+"/etc/passwd", "a")
|
|
passwdfile.write("%s\n" % line)
|
|
passwdfile.close()
|
|
createShadowItem(prefs, username, type, tools.generatePassword())
|
|
return SystemUser(prefs, username)
|
|
|
|
def createShadowItem(prefs, username, type, password):
|
|
line = ":".join((username,
|
|
tools.hashPassword(password, "md5"),
|
|
str(tools.daysSince1970()),
|
|
"0", "99999", "7", "", "", ""))
|
|
shadowfile = open(prefs.BASEPREFIX+"/etc/shadow", "a")
|
|
shadowfile.write("%s\n" % line)
|
|
shadowfile.close()
|