X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=ud-generate;h=6ac9bde5951b82292d6935d19985b05e8c7ff6cc;hb=6b038cc113dde577b9cc19c62656bf6d2b41362d;hp=fc6dbb20b85d5abc155c5937ad16161d412d24fa;hpb=92ba9e85b9d04a159ca5e15add1b5c8d5eb7ac16;p=mirror%2Fuserdir-ldap.git diff --git a/ud-generate b/ud-generate index fc6dbb2..6ac9bde 100755 --- a/ud-generate +++ b/ud-generate @@ -31,6 +31,7 @@ import string, re, time, ldap, getopt, sys, os, pwd, posix, socket, base64, sha, shutil, errno, tarfile, grp from userdir_ldap import * from userdir_exceptions import * +import UDLdap try: from cStringIO import StringIO except ImportError: @@ -273,12 +274,12 @@ def GenShadowSudo(File, untrusted): global PasswdAttrs for x in PasswdAttrs: + a = UDLdap.Account(x[0], x[1]) Pass = '*' - if x[1].has_key("uidNumber") == 0 or not IsInGroup(x): - continue + if not IsInGroup(x): continue - if x[1].has_key('sudoPassword'): - for entry in x[1]['sudoPassword']: + if 'sudoPassword' in a: + for entry in a['sudoPassword']: Match = re.compile('^('+UUID_FORMAT+') (confirmed:[0-9a-f]{40}|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$').match(entry) if Match == None: continue @@ -287,7 +288,7 @@ def GenShadowSudo(File, untrusted): hosts = Match.group(3) cryptedpass = Match.group(4) - if status != 'confirmed:'+make_passwd_hmac('password-is-confirmed', 'sudo', x[1]['uid'][0], uuid, hosts, cryptedpass): + if status != 'confirmed:'+make_passwd_hmac('password-is-confirmed', 'sudo', a['uid'], uuid, hosts, cryptedpass): continue for_all = hosts == "*" for_this_host = CurrentHost in hosts.split(',') @@ -302,7 +303,7 @@ def GenShadowSudo(File, untrusted): if len(Pass) > 50: Pass = '*' - Line = "%s:%s" % (GetAttr(x, "uid"), Pass) + Line = "%s:%s" % (a['uid'], Pass) Line = Sanitize(Line) + "\n" F.write("%s" % (Line)) @@ -323,21 +324,17 @@ def GenSSHShadow(): safe_makedirs(os.path.join(GlobalDir, 'userkeys')) for x in PasswdAttrs: + a = UDLdap.Account(x[0], x[1]) + if not 'sshRSAAuthKey' in a: continue - if x[1].has_key("uidNumber") == 0 or \ - x[1].has_key("sshRSAAuthKey") == 0: - continue - - User = GetAttr(x, "uid") F = None - try: OldMask = os.umask(0077) - File = os.path.join(GlobalDir, 'userkeys', User) + File = os.path.join(GlobalDir, 'userkeys', a['uid']) F = open(File + ".tmp", "w", 0600) os.umask(OldMask) - for I in x[1]["sshRSAAuthKey"]: + for I in a['sshRSAAuthKey']: MultipleLine = "%s" % I MultipleLine = Sanitize(MultipleLine) + "\n" F.write(MultipleLine) @@ -458,19 +455,16 @@ def GenGroup(File): # Sort them into a list of groups having a set of users for x in PasswdAttrs: - uid = GetAttr(x, "uid") - if 'gidNumber' in x[1]: - GroupHasPrimaryMembers[ int(x[1]["gidNumber"][0]) ] = True - if x[1].has_key("uidNumber") == 0 or not IsInGroup(x): - continue - if x[1].has_key("supplementaryGid") == 0: - continue - + a = UDLdap.Account(x[0], x[1]) + GroupHasPrimaryMembers[ a['gidNumber'] ] = True + if not IsInGroup(x): continue + if not 'supplementaryGid' in a: continue + supgroups=[] - addGroups(supgroups, x[1]["supplementaryGid"], uid) + addGroups(supgroups, a['supplementaryGid'], a['uid']) for g in supgroups: - GroupMap[g].append(uid) - + GroupMap[g].append(a['uid']) + # Output the group file. J = 0 for x in GroupMap.keys(): @@ -533,10 +527,9 @@ def GenForward(File): # Write out the email address for each user for x in PasswdAttrs: - if x[1].has_key("emailForward") == 0: - continue - - Line = "%s: %s" % (GetAttr(x, "uid"), GetAttr(x, "emailForward")) + a = UDLdap.Account(x[0], x[1]) + if not 'emailForward' in a: continue + Line = "%s: %s" % (a['uid'], a['emailForward']) Line = Sanitize(Line) + "\n" F.write(Line) @@ -546,7 +539,7 @@ def GenForward(File): raise Done(File, F, None) -def GenCDB(File, Users, Key): +def GenCDB(File, Users, key): Fdb = None try: OldMask = os.umask(0022) @@ -555,11 +548,11 @@ def GenCDB(File, Users, Key): # Write out the email address for each user for x in Users: - if not Key in x[1]: - continue - Value = GetAttr(x, Key) - User = GetAttr(x, "uid") - Fdb.write("+%d,%d:%s->%s\n" % (len(User), len(Value), User, Value)) + a = UDLdap.Account(x[0], x[1]) + if not key in a: continue + value = a[key] + user = a['uid'] + Fdb.write("+%d,%d:%s->%s\n" % (len(user), len(value), user, value)) Fdb.write("\n") # Oops, something unspeakable happened. @@ -580,10 +573,10 @@ def GenMarkers(File): # Write out the position for each user for x in PasswdAttrs: - if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0: - continue + a = UDLdap.Account(x[0], x[1]) + if not ('latitude' in a and 'longitude' in a): continue try: - Line = "%8s %8s \"\""%(DecDegree(GetAttr(x, "latitude"), 1), DecDegree(GetAttr(x, "longitude"), 1)) + Line = "%8s %8s \"\""%(a.latitude_dec(True), a.longitude_dec(True)) Line = Sanitize(Line) + "\n" F.write(Line) except: @@ -606,15 +599,11 @@ def GenPrivate(File): # Write out the position for each user for x in DebianDDUsers: - if x[1].has_key("privateSub") == 0: - continue - - # If the account has no PGP key, do not write it - if x[1].has_key("keyFingerPrint") == 0: - continue - + a = UDLdap.Account(x[0], x[1]) + if not a.is_active_user(): continue + if not 'privateSub' in a: continue try: - Line = "%s"%(GetAttr(x, "privateSub")) + Line = "%s"%(a['privateSub']) Line = Sanitize(Line) + "\n" F.write(Line) except: @@ -638,22 +627,12 @@ def GenDisabledAccounts(File): I = 0 for x in PasswdAttrs: - if x[1].has_key("uidNumber") == 0: - continue - - Pass = GetAttr(x, "userPassword") - Line = "" - # *LK* is the reference value for a locked account - # password starting with ! is also a locked account - if Pass.find("*LK*") != -1 or Pass.startswith("!"): - # Format is : - Line = "%s:%s" % (GetAttr(x, "uid"), "Account is locked") - DisabledUsers.append(x) - - if Line != "": - F.write(Sanitize(Line) + "\n") - - + a = UDLdap.Account(x[0], x[1]) + if a.pw_active(): continue + Line = "%s:%s" % (a['uid'], "Account is locked") + DisabledUsers.append(x) + F.write(Sanitize(Line) + "\n") + # Oops, something unspeakable happened. except: Die(File, F, None) @@ -670,19 +649,11 @@ def GenMailDisable(File): global PasswdAttrs for x in PasswdAttrs: - Reason = None - - if x[1].has_key("mailDisableMessage"): - Reason = GetAttr(x, "mailDisableMessage") - else: - continue - - try: - Line = "%s: %s"%(GetAttr(x, "uid"), Reason) - Line = Sanitize(Line) + "\n" - F.write(Line) - except: - pass + a = UDLdap.Account(x[0], x[1]) + if not 'mailDisableMessage' in a: continue + Line = "%s: %s"%(a['uid'], a['mailDisableMessage']) + Line = Sanitize(Line) + "\n" + F.write(Line) # Oops, something unspeakable happened. except: @@ -691,7 +662,7 @@ def GenMailDisable(File): Done(File, F, None) # Generate a list of uids that should have boolean affects applied -def GenMailBool(File, Key): +def GenMailBool(File, key): F = None try: F = open(File + ".tmp", "w") @@ -700,21 +671,13 @@ def GenMailBool(File, Key): global PasswdAttrs for x in PasswdAttrs: - Reason = None - - if x[1].has_key(Key) == 0: - continue - - if GetAttr(x, Key) != "TRUE": - continue - - try: - Line = "%s"%(GetAttr(x, "uid")) - Line = Sanitize(Line) + "\n" - F.write(Line) - except: - pass - + a = UDLdap.Account(x[0], x[1]) + if not key in a: continue + if not a[key] == 'TRUE': continue + Line = "%s"%(a['uid']) + Line = Sanitize(Line) + "\n" + F.write(Line) + # Oops, something unspeakable happened. except: Die(File, F, None) @@ -722,7 +685,7 @@ def GenMailBool(File, Key): Done(File, F, None) # Generate a list of hosts for RBL or whitelist purposes. -def GenMailList(File, Key): +def GenMailList(File, key): F = None try: F = open(File + ".tmp", "w") @@ -730,37 +693,20 @@ def GenMailList(File, Key): # Fetch all the users global PasswdAttrs + if key == "mailWhitelist": validregex = re.compile('^[-\w.]+(/[\d]+)?$') + else: validregex = re.compile('^[-\w.]+$') + for x in PasswdAttrs: - Reason = None - - if x[1].has_key(Key) == 0: - continue - - try: - found = 0 - Line = None - for z in x[1][Key]: - if Key == "mailWhitelist": - if re.match('^[-\w.]+(/[\d]+)?$', z) == None: - continue - else: - if re.match('^[-\w.]+$', z) == None: - continue - if found == 0: - found = 1 - Line = GetAttr(x, "uid") - else: - Line += " " - Line += ": " + z - if Key == "mailRHSBL": - Line += "/$sender_address_domain" - - if Line != None: - Line = Sanitize(Line) + "\n" - F.write(Line) - except: - pass - + a = UDLdap.Account(x[0], x[1]) + if not key in a: continue + + filtered = filter(lambda z: validregex.match(z), a[key]) + if len(filtered) == 0: continue + if key == "mailRHSBL": filtered = map(lambda z: z+"/$sender_address_domain", filtered) + line = a['uid'] + ': ' + ' : '.join(filtered) + line = Sanitize(line) + "\n" + F.write(line) + # Oops, something unspeakable happened. except: Die(File, F, None) @@ -1076,9 +1022,14 @@ def GenKeyrings(OutDir): # Connect to the ldap server l = connectLDAP() -F = open(PassDir + "/pass-" + pwd.getpwuid(os.getuid())[0], "r") -Pass = F.readline().strip().split(" ") -F.close() +# for testing purposes it's sometimes useful to pass username/password +# via the environment +if 'UD_CREDENTIALS' in os.environ: + Pass = os.environ['UD_CREDENTIALS'].split() +else: + F = open(PassDir + "/pass-" + pwd.getpwuid(os.getuid())[0], "r") + Pass = F.readline().strip().split(" ") + F.close() l.simple_bind_s("uid=" + Pass[0] + "," + BaseDn, Pass[1]) # Fetch all the groups @@ -1097,7 +1048,7 @@ for x in Attrs: SubGroupMap.setdefault(x[1]["gid"][0], []).extend(x[1]["subGroup"]) # Fetch all the users -PasswdAttrs = l.search_s(BaseDn, ldap.SCOPE_ONELEVEL, "uid=*",\ +PasswdAttrs = l.search_s(BaseDn, ldap.SCOPE_ONELEVEL, "(&(uid=*)(!(uidNumber=0)))",\ ["uid", "uidNumber", "gidNumber", "supplementaryGid",\ "gecos", "loginShell", "userPassword", "shadowLastChange",\ "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", @@ -1123,6 +1074,10 @@ if HostAttrs == None: HostAttrs.sort(lambda x, y: cmp((GetAttr(x, "hostname")).lower(), (GetAttr(y, "hostname")).lower())) +# override globaldir for testing +if 'UD_GENERATEDIR' in os.environ: + GenerateDir = os.environ['UD_GENERATEDIR'] + # Generate global things GlobalDir = GenerateDir + "/" GenDisabledAccounts(GlobalDir + "disabled-accounts")