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

72 lines
2.3 KiB
Python

"""Tool functions for GNUViech administration tool
(c) 2004 Jan Dittberner <jan@gnuviech.info>
"""
import random, re
from gnuviech import GNVPrefs
try:
from crypt import crypt
except ImportError, ie:
import sys
sys.__stderr__.write("Unsupported platform without crypt: " + sys.platform)
sys.exit()
from time import time
def generatePassword():
"""Generates a password from the chars in GNVPrefs.PWDCHARS with
a length between GNVPrefs.PWDMINLENGTH and GNVPrefs.PWDMAXLENGTH."""
return "".join([chr(char) for char in
random.sample(GNVPrefs.PWDCHARS,
random.randint(GNVPrefs.PWDMINLENGTH,
GNVPrefs.PWDMAXLENGTH))])
def generateSalt():
saltchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
salt = []
for i in range(8):
salt.append(saltchars[random.randint(0, len(saltchars) - 1)])
return "".join(salt)
def checkEmail(email):
"""Returns a match object if the given email address is syntactically
correct otherwise it returns None"""
# regex for email check
p = re.compile(r'^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9\-]+(\.|[a-zA-Z0-9\-]+)*\.[a-z]{2,5})$')
return p.search(email)
def splitPasswdLine(line):
loginname, password, uid, gid, fullname, directory, shell = line.split(":")
return {
"loginname" : loginname,
"password" : password,
"uid" : uid,
"gid" : gid,
"fullname" : fullname,
"homedir" : directory,
"shell" : shell
}
def splitShadowLine(line):
(loginname, passwordhash, lastchange, maychange, mustchange, warnexpire,
disabled, disabledsince, reserved) = line.split(":")
return {
"loginname" : loginname,
"passwordhash" : passwordhash,
"lastchange" : lastchange,
"maychange" : maychange,
"mustchange" : mustchange,
"warnexpire" : warnexpire,
"disabled" : disabled,
"disabledsince" : disabledsince,
"reserved" : reserved
}
def hashPassword(password, method="md5"):
if (method == "md5"):
return crypt(password, "$1$%s" % generateSalt())
return crypt(password, generateSalt())
def daysSince1970():
return int(time()/(3600*24))