#!/usr/bin/python import base64 import cgi import ldap import ldap.modlist import crypt import os import random from usermgmt_conf import * def executeCommand(command): try: f=os.popen(command, 'r') try: command_result=f.readlines() finally: f.close() except IOError: command_result="" return command_result def getNextUidGid(my_ldap) : # get system defaults defs = executeCommand('cat /etc/login.defs') for line in defs: if 'UID_MIN' in line : uid_min = int(line.split()[1]) if 'UID_MAX' in line : uid_max = int(line.split()[1]) if 'GID_MIN' in line : gid_min = int(line.split()[1]) if 'GID_MAX' in line : gid_max = int(line.split()[1]) # get used ids lists users = my_ldap.search_s(ldap_base, ldap.SCOPE_SUBTREE, '(objectClass=posixAccount)', ['uidNumber']) uid_list = [int(user[1]['uidNumber'][0]) for user in users] uid_list.sort() new_uid = uid_min groups = my_ldap.search_s('ou=Group, %s' % ldap_base, ldap.SCOPE_SUBTREE, '(objectClass=posixGroup)', ['gidNumber']) gid_list = [int(group[1]['gidNumber'][0]) for group in groups] gid_list.sort() new_gid = gid_min # guess next unused ids def findNextId(id_list, id_min, id_max): new_id = id_min for id in id_list: if (id >= new_id) and (id < id_max) : new_id = id + 1; elif (new_id >= id_max): print 'Maximum ID reached' raise 'Maximum ID reached' else: break return new_id return (findNextId(uid_list, uid_min, uid_max), findNextId(gid_list, gid_min, gid_max)) def generatePassword(clear) : salt_list = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./' salt = '$1$' random.seed() for i in range(8) : salt += random.choice(salt_list) salt += '$' return "{CRYPT}%s" % crypt.crypt(clear, salt) def setSambaPassword(user, clear): sudo_list = executeCommand('sudo -l') if not 'smbpasswd' in ' '.join(sudo_list): print 'Error : No rights on "smbpasswd" command, modify your sudo config.' else : executeCommand('sudo -u root /usr/bin/smbpasswd -a %s %s' % (user, clear)) #header print "Content-Type: text/html" print print '''