fix typo
[mirror/userdir-ldap.git] / ud-generate
index 85650c1..5ae6428 100755 (executable)
@@ -28,7 +28,7 @@
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp, fcntl
+import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp, fcntl, dbm
 from userdir_ldap import *
 from userdir_exceptions import *
 import UDLdap
@@ -71,6 +71,7 @@ isSSHFP = re.compile("^\s*IN\s+SSHFP")
 DNSZone = ".debian.net"
 Keyrings = ConfModule.sync_keyrings.split(":")
 GitoliteSSHRestrictions = getattr(ConfModule, "gitolitesshrestrictions", None)
+GitoliteExportHosts = re.compile(getattr(ConfModule, "gitoliteexporthosts", "."))
 MX_remap = json.loads(ConfModule.MX_remap)
 
 def prettify(elem):
@@ -336,7 +337,7 @@ def GenShadowSudo(accounts, File, untrusted, current_host):
    Done(File, F, None)
 
 # Generate the sudo passwd file
-def GenSSHGitolite(accounts, File):
+def GenSSHGitolite(accounts, hosts, File):
    F = None
    try:
       OldMask = os.umask(0022)
@@ -357,6 +358,15 @@ def GenSSHGitolite(accounts, File):
                line = Sanitize(line) + "\n"
                F.write(line)
 
+         for dn, attrs in hosts:
+            if not 'sshRSAHostKey' in attrs: continue
+            hostname = "host-" + attrs['hostname'][0]
+            prefix = GitoliteSSHRestrictions.replace('@@USER@@', hostname)
+            for I in attrs["sshRSAHostKey"]:
+               line = "%s %s"%(prefix, I)
+               line = Sanitize(line) + "\n"
+               F.write(line)
+
    # Oops, something unspeakable happened.
    except:
       Die(File, F, None)
@@ -408,31 +418,18 @@ def GenVoipPassword(accounts, File):
       F = open(File, "w", 0600)
       os.umask(OldMask)
 
-      root = Element('include')
-
       for a in accounts:
          if not 'voipPassword' in a: continue
          if not a.pw_active(): continue
 
          Pass = str(a['voipPassword'])
-         user = Element('user')
-         user.attrib['id'] = "%s" % (a['uid'])
-         root.append(user)
-         params = Element('params')
-         user.append(params)
-         param = Element('param')
-         params.append(param)
-         param.attrib['name'] = "a1-hash"
-         param.attrib['value'] = "%s" % (Pass)
-         variables = Element('variables')
-         user.append(variables)
-         variable = Element('variable')
-         variable.attrib['name'] = "toll_allow"
-         variable.attrib['value'] = "domestic,international,local"
-         variables.append(variable)
-
-      F.write("%s" % (prettify(root)))
+         realm = 'sip.debian.org'
 
+         A1 = "%s:%s%:%s" % (a['uid'], realm, Pass)
+         HA1 = hashlib.md5(A1).hexdigest()
+         Line = "%s:%s:%s:AUTHORIZED" % (a['uid'], HA1, Pass)
+         Line = Sanitize(Line) + "\n"
+         F.write("%s" % (Line))
 
    except:
       Die(File, None, F)
@@ -635,6 +632,34 @@ def GenCDB(accounts, File, key):
    if Fdb.close() != None:
       raise "cdbmake gave an error"
 
+def GenDBM(accounts, File, key):
+   Fdb = None
+   OldMask = os.umask(0022)
+   fn = os.path.join(File).encode('ascii', 'ignore')
+   try:
+      posix.remove(fn)
+   except:
+      pass
+
+   try:
+      Fdb = dbm.open(fn, "c")
+      os.umask(OldMask)
+
+      # Write out the email address for each user
+      for a in accounts:
+         if not key in a: continue
+         value = a[key]
+         user = a['uid']
+         Fdb[user] = value
+
+      Fdb.close()
+   except:
+      # python-dbm names the files Fdb.db.db so we want to them to be Fdb.db
+      os.remove(File + ".db")
+      raise
+   # python-dbm names the files Fdb.db.db so we want to them to be Fdb.db
+   os.rename (File + ".db", File)
+
 # Generate the anon XEarth marker file
 def GenMarkers(accounts, File):
    F = None
@@ -1159,7 +1184,9 @@ def generate_all(global_dir, ldap_conn):
 
    GenMailDisable(accounts, global_dir + "mail-disable")
    GenCDB(accounts, global_dir + "mail-forward.cdb", 'emailForward')
+   GenDBM(accounts, global_dir + "mail-forward.db", 'emailForward')
    GenCDB(accounts, global_dir + "mail-contentinspectionaction.cdb", 'mailContentInspectionAction')
+   GenDBM(accounts, global_dir + "mail-contentinspectionaction.db", 'mailContentInspectionAction')
    GenPrivate(accounts, global_dir + "debian-private")
    GenSSHKnown(host_attrs, global_dir+"authorized_keys", 'authorized_keys', global_dir+'ud-generate.lock')
    GenMailBool(accounts, global_dir + "mail-greylist", "mailGreylisting")
@@ -1181,7 +1208,7 @@ def generate_all(global_dir, ldap_conn):
    GenMarkers(accounts, global_dir + "markers")
    GenSSHKnown(host_attrs, global_dir + "ssh_known_hosts")
    GenHosts(host_attrs, global_dir + "debianhosts")
-   GenSSHGitolite(accounts, global_dir + "ssh-gitolite")
+   GenSSHGitolite(accounts, host_attrs, global_dir + "ssh-gitolite")
 
    GenDNS(accounts, global_dir + "dns-zone")
    GenZoneRecords(host_attrs, global_dir + "dns-sshfp")
@@ -1191,9 +1218,9 @@ def generate_all(global_dir, ldap_conn):
    for host in host_attrs:
       if not "hostname" in host[1]:
          continue
-      generate_host(host, global_dir, accounts, ssh_userkeys)
+      generate_host(host, global_dir, accounts, host_attrs, ssh_userkeys)
 
-def generate_host(host, global_dir, all_accounts, ssh_userkeys):
+def generate_host(host, global_dir, all_accounts, all_hosts, ssh_userkeys):
    current_host = host[1]['hostname'][0]
    OutDir = global_dir + current_host + '/'
    if not os.path.isdir(OutDir):
@@ -1242,7 +1269,9 @@ def generate_host(host, global_dir, all_accounts, ssh_userkeys):
    if not 'NOMARKERS' in ExtraList:
       DoLink(global_dir, OutDir, "markers")
    DoLink(global_dir, OutDir, "mail-forward.cdb")
+   DoLink(global_dir, OutDir, "mail-forward.db")
    DoLink(global_dir, OutDir, "mail-contentinspectionaction.cdb")
+   DoLink(global_dir, OutDir, "mail-contentinspectionaction.db")
    DoLink(global_dir, OutDir, "mail-disable")
    DoLink(global_dir, OutDir, "mail-greylist")
    DoLink(global_dir, OutDir, "mail-callout")
@@ -1251,8 +1280,11 @@ def generate_host(host, global_dir, all_accounts, ssh_userkeys):
    DoLink(global_dir, OutDir, "mail-whitelist")
    DoLink(global_dir, OutDir, "all-accounts.json")
    GenCDB(accounts, OutDir + "user-forward.cdb", 'emailForward')
+   GenDBM(accounts, OutDir + "user-forward.db", 'emailForward')
    GenCDB(accounts, OutDir + "batv-tokens.cdb", 'bATVToken')
+   GenDBM(accounts, OutDir + "batv-tokens.db", 'bATVToken')
    GenCDB(accounts, OutDir + "default-mail-options.cdb", 'mailDefaultOptions')
+   GenDBM(accounts, OutDir + "default-mail-options.db", 'mailDefaultOptions')
 
    # Compatibility.
    DoLink(global_dir, OutDir, "forward-alias")
@@ -1277,7 +1309,8 @@ def generate_host(host, global_dir, all_accounts, ssh_userkeys):
          v = entry.split('=',1)
          if v[0] != 'GITOLITE' or len(v) != 2: continue
          gitolite_accounts = filter(lambda x: IsInGroup(x, [v[1]], current_host), all_accounts)
-         GenSSHGitolite(gitolite_accounts, OutDir + "ssh-gitolite-%s"%(v[1],))
+         gitolite_hosts = filter(lambda x: GitoliteExportHosts.match(x[1]["hostname"][0]), all_hosts)
+         GenSSHGitolite(gitolite_accounts, gitolite_hosts, OutDir + "ssh-gitolite-%s"%(v[1],))
 
    if 'WEB-PASSWORDS' in ExtraList:
       DoLink(global_dir, OutDir, "web-passwords")