1
0
Fork 0
gnuviechadmin-historic/backend/gnuviech/sysuser.py

63 lines
2.4 KiB
Python
Raw Normal View History

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()