readd support for tdb files (needed)
[mirror/userdir-ldap.git] / ud-generate
index d556f8b..7011dd0 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|gluck)\.debian\.org\..*",re.DOTALL);
+DNSZone = ".debian.net"
+
 def Sanitize(Str):
   return string.translate(Str,string.maketrans("\n\r\t","$$$"));
 
@@ -65,10 +69,8 @@ def Done(File,F,Fdb):
 # Generate the password list
 def GenPasswd(l,File,HomePrefix):
   F = None;
-  Fdb = None;
   try:
-   F = open(File + ".tmp","w");
-   Fdb = open(File + ".tdb.tmp","w");
+   F = open(File + ".tdb.tmp","w");
 
    # Fetch all the users
    global PasswdAttrs;
@@ -84,30 +86,29 @@ def GenPasswd(l,File,HomePrefix):
       if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginshell")) > 50:
          continue;
 
-      Line = "%s:x:%s:%s:%s:%s%s:%s\n" % (GetAttr(x,"uid"),\
+      Line = "%s:x:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
               GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
               GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
               GetAttr(x,"loginshell"));
-      F.write(Line);
-      Fdb.write("0%u %s" % (I,Line));
-      Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
-      Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
+
+      Line = Sanitize(Line) + "\n";
+      F.write("0%u %s" % (I,Line));
+      F.write(".%s %s" % (GetAttr(x,"uid"),Line));
+      F.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
       I = I + 1;
 
   # Oops, something unspeakable happened.
   except:
-   Die(File,F,Fdb);
+   Die(File,None,F);
    raise;
-  Done(File,F,Fdb);
+  Done(File,None,F);
 
 # Generate the shadow list
 def GenShadow(l,File):
   F = None;
-  Fdb = None;
   try:
    OldMask = os.umask(0077);
-   F = open(File + ".tmp","w",0600);
-   Fdb = open(File + ".tdb.tmp","w",0600);
+   F = open(File + ".tdb.tmp","w",0600);
    os.umask(OldMask);
 
    # Fetch all the users
@@ -131,25 +132,22 @@ def GenShadow(l,File):
               GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
               GetAttr(x,"shadowexpire"));
       Line = Sanitize(Line) + "\n";
-      F.write(Line);
-      Fdb.write("0%u %s" % (I,Line));
-      Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
+      F.write("0%u %s" % (I,Line));
+      F.write(".%s %s" % (GetAttr(x,"uid"),Line));
       I = I + 1;
 
   # Oops, something unspeakable happened.
   except:
-   Die(File,F,Fdb);
+   Die(File,None,F);
    raise;
-  Done(File,F,Fdb);
+  Done(File,None,F);
 
 # Generate the shadow list
 def GenSSHShadow(l,File):
   F = None;
-  Fdb = None;
   try:
    OldMask = os.umask(0077);
    F = open(File + ".tmp","w",0600);
-   Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
    os.umask(OldMask);
 
    # Fetch all the users
@@ -157,7 +155,6 @@ def GenSSHShadow(l,File):
    if PasswdAttrs == None:
       raise "No Users";
 
-   I = 0;
    for x in PasswdAttrs:
       if x[1].has_key("uidnumber") == 0 or \
          x[1].has_key("sshrsaauthkey") == 0:
@@ -167,23 +164,17 @@ def GenSSHShadow(l,File):
          Line = "%s: %s" %(User,I);
          Line = Sanitize(Line) + "\n";
          F.write(Line);
-         Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(I),User,I));
-   Fdb.write("\n");
   # Oops, something unspeakable happened.
   except:
-   Die(File,F,Fdb);
+   Die(File,F,None);
    raise;
-  if Fdb.close() != None:
-     raise "cdbmake gave an error";
   Done(File,F,None);
 
 # Generate the group list
 def GenGroup(l,File):
   F = None;
-  Fdb = None;
   try:
-   F = open(File + ".tmp","w");
-   Fdb = open(File + ".tdb.tmp","w");
+   F = open(File + ".tdb.tmp","w");
 
    # Generate the GroupMap
    GroupMap = {};
@@ -209,7 +200,7 @@ def GenGroup(l,File):
             print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
            
    # Output the group file.
-   Counter = 0; 
+   J = 0;
    for x in GroupMap.keys():
       if GroupIDMap.has_key(x) == 0:
          continue;
@@ -219,17 +210,16 @@ def GenGroup(l,File):
         Line = Line + ("%s%s" % (Comma,I));
         Comma = ',';
       Line = Sanitize(Line) + "\n";
-      F.write(Line);
-      Fdb.write("0%u %s" % (Counter,Line));
-      Fdb.write(".%s %s" % (x,Line));
-      Fdb.write("=%u %s" % (GroupIDMap[x],Line));
-      Counter = Counter + 1;
+      F.write("0%u %s" % (J,Line));
+      F.write(".%s %s" % (x,Line));
+      F.write("=%u %s" % (GroupIDMap[x],Line));
+      J = J + 1;
       
   # Oops, something unspeakable happened.
   except:
-   Die(File,F,Fdb);
+   Die(File,None,F);
    raise;
-  Done(File,F,Fdb);
+  Done(File,None,F);
 
 # Generate the email forwarding list
 def GenForward(l,File):
@@ -254,6 +244,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 +279,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 +367,11 @@ def GenPrivate(l,File):
   Done(File,F,Fdb);
 
 # Generate the DNS Zone file
-def GenDNS(l,File):
+def GenDNS(l,File,HomePrefix):
   F = None;
-  Fdb = None;
   try:
    F = open(File + ".tmp","w");
-   Fdb = None;
-
+   
    # Fetch all the users
    global PasswdAttrs;
    if PasswdAttrs == None:
@@ -397,8 +392,12 @@ 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");
+                              
+              # 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 +408,88 @@ def GenDNS(l,File):
 
          F.write("\n");
       except:
+        F.write("; Errors\n");
          pass;
       
   # Oops, something unspeakable happened.
   except:
-   Die(File,F,Fdb);
+   Die(File,F,None);
    raise;
-  Done(File,F,Fdb);
+  Done(File,F,None);
+
+# Generate the BSMTP file
+def GenBSMTP(l,File,HomePrefix):
+  F = None;
+  try:
+   F = open(File + ".tmp","w");
+   
+   # Fetch all the users
+   global PasswdAttrs;
+   if PasswdAttrs == None:
+      raise "No Users";
+
+   # Write out the zone file entry for each user
+   for x in PasswdAttrs:
+      if x[1].has_key("dnszoneentry") == 0:
+         continue;
+      try:
+         for z in x[1]["dnszoneentry"]:
+            Split = string.split(string.lower(z));
+           if string.lower(Split[1]) == 'in':
+               for y in range(0,len(Split)):
+                  if Split[y] == "$":
+                     Split[y] = "\n\t";
+               Line = string.join(Split," ") + "\n";
+              
+              Host = Split[0] + DNSZone;
+              if BSMTPCheck.match(Line) != None:
+                  F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
+                              GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
+                              
+      except:
+        F.write("; Errors\n");
+         pass;
+      
+  # Oops, something unspeakable happened.
+  except:
+   Die(File,F,None);
+   raise;
+  Done(File,F,None);
+
+# Generate the shadow list
+def GenSSHKnown(l,File):
+  F = None;
+  try:
+   OldMask = os.umask(0022);
+   F = open(File + ".tmp","w",0644);
+   os.umask(OldMask);
+
+   # Fetch all the hosts
+   HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
+                ["hostname","sshrsahostkey"]);
+   
+   if HostKeys == None:
+      raise "No Hosts";
+
+   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);
+  # Oops, something unspeakable happened.
+  except:
+   Die(File,F,None);
+   raise;
+  Done(File,F,None);
+
 
 # Connect to the ldap server
 l = ldap.open(LDAPServer);
@@ -455,8 +529,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");
@@ -499,15 +573,19 @@ while(1):
        
    # Link in global things   
    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
-   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",Split[1]);
       
+   if ExtraList.has_key("[BSMTP]"):
+      GenBSMTP(l,OutDir+"bsmtp",Split[1]);
+
    if ExtraList.has_key("[PRIVATE]"):
       DoLink(GlobalDir,OutDir,"debian-private");
+