1.0.3 (upstream) is enough
[mirror/userdir-ldap.git] / ud-generate
index d556f8b..afc6b16 100755 (executable)
@@ -2,7 +2,7 @@
 # -*- mode: python -*-
 # Generates passwd, shadow and group files from the ldap directory.
 
-import string, re, time, ldap, getopt, sys, os, pwd, posix;
+import string, re, time, ldap, getopt, sys, os, pwd, posix, socket;
 from userdir_ldap import *;
 
 PasswdAttrs = None;
@@ -10,6 +10,10 @@ GroupIDMap = {};
 Allowed = None;
 CurrentHost = "";
 
+EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
+BSMTPCheck = re.compile(".*mx 0 klecker\.debian\.org\..*",re.DOTALL);
+DNSZone = ".debian.net"
+
 def Sanitize(Str):
   return string.translate(Str,string.maketrans("\n\r\t","$$$"));
 
@@ -254,6 +258,9 @@ def GenForward(l,File):
       if len(GetAttr(x,"emailforward")) > 200:
          continue;
 
+      # Check the forwarding address
+      if EmailCheck.match(GetAttr(x,"emailforward")) == None:
+         continue;
       Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
       Line = Sanitize(Line) + "\n";
       F.write(Line);
@@ -286,6 +293,10 @@ def GenAllForward(l,File):
       if len(Forward) > 200:
          continue;
 
+      # Check the forwarding address
+      if EmailCheck.match(Forward) == None:
+         continue;
+        
       User = GetAttr(x,"uid");
       Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
    Fdb.write("\n");
@@ -370,13 +381,13 @@ def GenPrivate(l,File):
   Done(File,F,Fdb);
 
 # Generate the DNS Zone file
-def GenDNS(l,File):
+def GenDNS(l,File,BSMTPFile,HomePrefix):
   F = None;
-  Fdb = None;
+  FB = None;
   try:
    F = open(File + ".tmp","w");
-   Fdb = None;
-
+   FB = open(BSMTPFile + ".tmp","w");
+   
    # Fetch all the users
    global PasswdAttrs;
    if PasswdAttrs == None:
@@ -397,8 +408,14 @@ def GenDNS(l,File):
                Line = string.join(Split," ") + "\n";
                F.write(Line);
               
-              # Write some identication information
-               if string.lower(Split[2]) != "cname":
+              Host = Split[0] + DNSZone;
+              if BSMTPCheck.match(Line) != None:
+                  F.write("; Has BSMTP\n");
+                  FB.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
+                              GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
+                              
+              # Write some identification information
+               if string.lower(Split[2]) == "a":
                  Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
                   for y in x[1]["keyfingerprint"]:
                     Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
@@ -409,13 +426,58 @@ def GenDNS(l,File):
 
          F.write("\n");
       except:
+        F.write("; Errors\n");
          pass;
       
   # Oops, something unspeakable happened.
+  except:
+   Die(File,F,None);
+   Die(BSMTPFile,FB,None);
+   raise;
+  Done(File,F,None);
+  Done(BSMTPFile,FB,None);
+
+# Generate the shadow list
+def GenSSHKnown(l,File):
+  F = None;
+  Fdb = None;
+  try:
+   OldMask = os.umask(0022);
+   F = open(File + ".tmp","w",0644);
+   Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
+   os.umask(OldMask);
+
+   # Fetch all the hosts
+   HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
+                ["hostname","sshrsahostkey"]);
+   
+   if HostKeys == None:
+      raise "No Hosts";
+
+   I = 0;
+   for x in HostKeys:
+      if x[1].has_key("hostname") == 0 or \
+         x[1].has_key("sshrsahostkey") == 0:
+         continue;
+      Host = GetAttr(x,"hostname");
+      SHost = string.find(Host,".");
+      for I in x[1]["sshrsahostkey"]:
+         if SHost == None:
+            Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
+         else:
+            Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
+         Line = Sanitize(Line) + "\n";
+         F.write(Line);
+         Fdb.write("+%d,%d:%s->%s\n"%(len(Host),len(I),Host,I));
+   Fdb.write("\n");
+  # Oops, something unspeakable happened.
   except:
    Die(File,F,Fdb);
    raise;
-  Done(File,F,Fdb);
+  if Fdb.close() != None:
+     raise "cdbmake gave an error";
+  Done(File,F,None);
+
 
 # Connect to the ldap server
 l = ldap.open(LDAPServer);
@@ -455,8 +517,8 @@ GlobalDir = GenerateDir+"/";
 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
 GenAllForward(l,GlobalDir+"mail-forward.cdb");
 GenMarkers(l,GlobalDir+"markers");
-GenDNS(l,GlobalDir+"dns-zone");
 GenPrivate(l,GlobalDir+"debian-private");
+GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
 
 # Compatibility.
 GenForward(l,GlobalDir+"forward-alias");
@@ -502,12 +564,13 @@ while(1):
    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow.cdb");
    DoLink(GlobalDir,OutDir,"markers");
    DoLink(GlobalDir,OutDir,"mail-forward.cdb");
+   DoLink(GlobalDir,OutDir,"ssh_known_hosts");
 
    # Compatibility.
    DoLink(GlobalDir,OutDir,"forward-alias");
 
    if ExtraList.has_key("[DNS]"):
-      DoLink(GlobalDir,OutDir,"dns-zone");
+      GenDNS(l,OutDir+"dns-zone",OutDir+"bsmtp",Split[1]);
       
    if ExtraList.has_key("[PRIVATE]"):
       DoLink(GlobalDir,OutDir,"debian-private");