update for python 2.1
[mirror/userdir-ldap.git] / ud-generate
index 674cc60..a0c735d 100755 (executable)
@@ -2,16 +2,19 @@
 # -*- 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 *;
 
+global Allowed;
+global CurrentHost;
+
 PasswdAttrs = None;
 GroupIDMap = {};
 Allowed = None;
 CurrentHost = "";
 
 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
-BSMTPCheck = re.compile(".*mx 0 klecker\.debian\.org\..*",re.DOTALL);
+BSMTPCheck = re.compile(".*mx 0 (klecker|gluck)\.debian\.org\..*",re.DOTALL);
 DNSZone = ".debian.net"
 
 def Sanitize(Str):
@@ -24,7 +27,6 @@ def DoLink(From,To,File):
 
 # See if this user is in the group list
 def IsInGroup(DnRecord):
-  global Allowed,CurrentHost;
   if Allowed == None:
      return 1;
 
@@ -69,10 +71,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;
@@ -88,30 +88,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
@@ -135,25 +134,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
@@ -161,7 +157,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:
@@ -171,23 +166,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 = {};
@@ -213,7 +202,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;
@@ -223,17 +212,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):
@@ -381,12 +369,10 @@ def GenPrivate(l,File):
   Done(File,F,Fdb);
 
 # Generate the DNS Zone file
-def GenDNS(l,File,BSMTPFile,HomePrefix):
+def GenDNS(l,File,HomePrefix):
   F = None;
-  FB = None;
   try:
    F = open(File + ".tmp","w");
-   FB = open(BSMTPFile + ".tmp","w");
    
    # Fetch all the users
    global PasswdAttrs;
@@ -411,8 +397,6 @@ def GenDNS(l,File,BSMTPFile,HomePrefix):
               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":
@@ -432,10 +416,82 @@ def GenDNS(l,File,BSMTPFile,HomePrefix):
   # Oops, something unspeakable happened.
   except:
    Die(File,F,None);
-   Die(BSMTPFile,FB,None);
    raise;
   Done(File,F,None);
-  Done(BSMTPFile,FB,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);
@@ -476,6 +532,7 @@ GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
 GenAllForward(l,GlobalDir+"mail-forward.cdb");
 GenMarkers(l,GlobalDir+"markers");
 GenPrivate(l,GlobalDir+"debian-private");
+GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
 
 # Compatibility.
 GenForward(l,GlobalDir+"forward-alias");
@@ -506,7 +563,6 @@ while(1):
       if GroupIDMap.has_key(I):
          GroupList[str(GroupIDMap[I])] = None;
 
-   global Allowed,CurrentHost;
    Allowed = GroupList;
    CurrentHost = Split[0];
 
@@ -518,15 +574,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]"):
-      GenDNS(l,OutDir+"dns-zone",OutDir+"bsmtp",Split[1]);
+      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");
+