2004-12-26 00:09:19 +01:00
|
|
|
"""Tool functions for GNUViech administration tool
|
|
|
|
|
|
|
|
(c) 2004 Jan Dittberner <jan@gnuviech.info>
|
|
|
|
"""
|
|
|
|
|
2004-12-26 16:50:53 +01:00
|
|
|
import random, re
|
2004-12-26 00:09:19 +01:00
|
|
|
from gnuviech import GNVPrefs
|
2006-02-23 17:52:32 +01:00
|
|
|
try:
|
|
|
|
from crypt import crypt
|
|
|
|
except ImportError, ie:
|
|
|
|
import sys
|
|
|
|
sys.__stderr__.write("Unsupported platform without crypt: " + sys.platform)
|
|
|
|
sys.exit()
|
2004-12-26 20:29:32 +01:00
|
|
|
from time import time
|
2004-12-26 00:09:19 +01:00
|
|
|
|
|
|
|
def generatePassword():
|
2004-12-26 20:29:32 +01:00
|
|
|
"""Generates a password from the chars in GNVPrefs.PWDCHARS with
|
|
|
|
a length between GNVPrefs.PWDMINLENGTH and GNVPrefs.PWDMAXLENGTH."""
|
2004-12-26 00:09:19 +01:00
|
|
|
return "".join([chr(char) for char in
|
2004-12-26 16:50:53 +01:00
|
|
|
random.sample(GNVPrefs.PWDCHARS,
|
|
|
|
random.randint(GNVPrefs.PWDMINLENGTH,
|
|
|
|
GNVPrefs.PWDMAXLENGTH))])
|
2004-12-26 00:09:19 +01:00
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
def generateSalt():
|
|
|
|
saltchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
salt = []
|
|
|
|
for i in range(8):
|
|
|
|
salt.append(saltchars[random.randint(0, len(saltchars) - 1)])
|
|
|
|
return "".join(salt)
|
|
|
|
|
2004-12-26 16:50:53 +01:00
|
|
|
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)
|
2004-12-26 20:29:32 +01:00
|
|
|
|
|
|
|
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))
|