Import from samosa: case sensitive spelling of fields
[mirror/userdir-ldap.git] / ud-generate
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # Generates passwd, shadow and group files from the ldap directory.
4
5 import string, re, time, ldap, getopt, sys, os, pwd, posix, socket;
6 from userdir_ldap import *;
7
8 global Allowed;
9 global CurrentHost;
10
11 PasswdAttrs = None;
12 GroupIDMap = {};
13 Allowed = None;
14 CurrentHost = "";
15
16 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
17 BSMTPCheck = re.compile(".*mx 0 (klecker|gluck)\.debian\.org\..*",re.DOTALL);
18 DNSZone = ".debian.net"
19
20 def Sanitize(Str):
21   return string.translate(Str,string.maketrans("\n\r\t","$$$"));
22
23 def DoLink(From,To,File):
24    try: posix.remove(To+File);
25    except: pass;
26    posix.link(From+File,To+File);
27
28 # See if this user is in the group list
29 def IsInGroup(DnRecord):
30   if Allowed == None:
31      return 1;
32
33   # See if the primary group is in the list
34   if Allowed.has_key(GetAttr(DnRecord,"gidNumber")) != 0:
35      return 1;
36
37   # Check the host based ACL
38   if DnRecord[1].has_key("allowedHost") != 0:
39      for I in DnRecord[1]["allowedHost"]:
40         if CurrentHost == I:
41            return 1;
42
43   # See if there are supplementary groups
44   if DnRecord[1].has_key("supplementaryGid") == 0:
45      return 0;
46
47   # Check the supplementary groups
48   for I in DnRecord[1]["supplementaryGid"]:
49      if Allowed.has_key(I):
50         return 1;
51   return 0;
52
53 def Die(File,F,Fdb):
54    if F != None:
55       F.close();
56    if Fdb != None:
57       Fdb.close();
58    try: os.remove(File + ".tmp");
59    except: pass;
60    try: os.remove(File + ".tdb.tmp");
61    except: pass;
62
63 def Done(File,F,Fdb):
64   if F != None:
65     F.close();
66     os.rename(File + ".tmp",File);
67   if Fdb != None:
68     Fdb.close();
69     os.rename(File + ".tdb.tmp",File+".tdb");
70   
71 # Generate the password list
72 def GenPasswd(l,File,HomePrefix):
73   F = None;
74   try:
75    F = open(File + ".tdb.tmp","w");
76
77    # Fetch all the users
78    global PasswdAttrs;
79    if PasswdAttrs == None:
80       raise "No Users";
81
82    I = 0;
83    for x in PasswdAttrs:
84       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
85          continue;
86
87       # Do not let people try to buffer overflow some busted passwd parser.
88       if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginShell")) > 50:
89          continue;
90
91       Line = "%s:x:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
92               GetAttr(x,"uidNumber"),GetAttr(x,"gidNumber"),\
93               GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
94               GetAttr(x,"loginShell"));
95
96       Line = Sanitize(Line) + "\n";
97       F.write("0%u %s" % (I,Line));
98       F.write(".%s %s" % (GetAttr(x,"uid"),Line));
99       F.write("=%s %s" % (GetAttr(x,"uidNumber"),Line));
100       I = I + 1;
101
102   # Oops, something unspeakable happened.
103   except:
104    Die(File,None,F);
105    raise;
106   Done(File,None,F);
107
108 # Generate the shadow list
109 def GenShadow(l,File):
110   F = None;
111   try:
112    OldMask = os.umask(0077);
113    F = open(File + ".tdb.tmp","w",0600);
114    os.umask(OldMask);
115
116    # Fetch all the users
117    global PasswdAttrs;
118    if PasswdAttrs == None:
119       raise "No Users";
120
121    I = 0;
122    for x in PasswdAttrs:
123       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
124          continue;
125          
126       Pass = GetAttr(x,"userPassword");
127       if Pass[0:7] != "{crypt}" or len(Pass) > 50:
128          Pass = '*';
129       else:
130          Pass = Pass[7:];
131       Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
132               Pass,GetAttr(x,"shadowLastChange"),\
133               GetAttr(x,"shadowMin"),GetAttr(x,"shadowMax"),\
134               GetAttr(x,"shadowWarning"),GetAttr(x,"shadowinactive"),\
135               GetAttr(x,"shadowexpire"));
136       Line = Sanitize(Line) + "\n";
137       F.write("0%u %s" % (I,Line));
138       F.write(".%s %s" % (GetAttr(x,"uid"),Line));
139       I = I + 1;
140
141   # Oops, something unspeakable happened.
142   except:
143    Die(File,None,F);
144    raise;
145   Done(File,None,F);
146
147 # Generate the shadow list
148 def GenSSHShadow(l,File):
149   F = None;
150   try:
151    OldMask = os.umask(0077);
152    F = open(File + ".tmp","w",0600);
153    os.umask(OldMask);
154
155    # Fetch all the users
156    global PasswdAttrs;
157    if PasswdAttrs == None:
158       raise "No Users";
159
160    for x in PasswdAttrs:
161      if x[1].has_key("uidNumber") == 0 or \
162         x[1].has_key("sshRSAAuthKey") == 0:
163          continue;
164
165       if x[1].has_key("uidNumber") == 0 or \
166          x[1].has_key("sshRSAAuthKey") == 0:
167          continue;
168       for I in x[1]["sshRSAAuthKey"]:
169          User = GetAttr(x,"uid");
170          Line = "%s: %s" %(User,I);
171          Line = Sanitize(Line) + "\n";
172          F.write(Line);
173   # Oops, something unspeakable happened.
174   except:
175    Die(File,F,None);
176    raise;
177   Done(File,F,None);
178
179 # Generate the group list
180 def GenGroup(l,File):
181   F = None;
182   try:
183    F = open(File + ".tdb.tmp","w");
184
185    # Generate the GroupMap
186    GroupMap = {};
187    for x in GroupIDMap.keys():
188       GroupMap[x] = [];
189       
190    # Fetch all the users
191    global PasswdAttrs;
192    if PasswdAttrs == None:
193       raise "No Users";
194
195    # Sort them into a list of groups having a set of users
196    for x in PasswdAttrs:
197       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
198          continue;
199       if x[1].has_key("supplementaryGid") == 0:
200          continue;
201          
202       for I in x[1]["supplementaryGid"]:
203          if GroupMap.has_key(I):
204             GroupMap[I].append(GetAttr(x,"uid"));
205          else:
206             print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
207             
208    # Output the group file.
209    J = 0;
210    for x in GroupMap.keys():
211       if GroupIDMap.has_key(x) == 0:
212          continue;
213       Line = "%s:x:%u:" % (x,GroupIDMap[x]);
214       Comma = '';
215       for I in GroupMap[x]:
216         Line = Line + ("%s%s" % (Comma,I));
217         Comma = ',';
218       Line = Sanitize(Line) + "\n";
219       F.write("0%u %s" % (J,Line));
220       F.write(".%s %s" % (x,Line));
221       F.write("=%u %s" % (GroupIDMap[x],Line));
222       J = J + 1;
223       
224   # Oops, something unspeakable happened.
225   except:
226    Die(File,None,F);
227    raise;
228   Done(File,None,F);
229
230 # Generate the email forwarding list
231 def GenForward(l,File):
232   F = None;
233   Fdb = None;
234   try:
235    OldMask = os.umask(0022);
236    F = open(File + ".tmp","w",0644);
237    os.umask(OldMask);
238
239    # Fetch all the users
240    global PasswdAttrs;
241    if PasswdAttrs == None:
242       raise "No Users";
243
244    # Write out the email address for each user
245    for x in PasswdAttrs:
246       if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
247          continue;
248       
249       # Do not allow people to try to buffer overflow busted parsers
250       if len(GetAttr(x,"emailForward")) > 200:
251          continue;
252
253       # Check the forwarding address
254       if EmailCheck.match(GetAttr(x,"emailForward")) == None:
255          continue;
256       Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
257       Line = Sanitize(Line) + "\n";
258       F.write(Line);
259       
260   # Oops, something unspeakable happened.
261   except:
262    Die(File,F,Fdb);
263    raise;
264   Done(File,F,Fdb);
265
266 def GenAllForward(l,File):
267   Fdb = None;
268   try:
269    OldMask = os.umask(0022);
270    Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
271    os.umask(OldMask);
272
273    # Fetch all the users
274    global PasswdAttrs;
275    if PasswdAttrs == None:
276       raise "No Users";
277
278    # Write out the email address for each user
279    for x in PasswdAttrs:
280       if x[1].has_key("emailForward") == 0:
281          continue;
282       
283       # Do not allow people to try to buffer overflow busted parsers
284       Forward = GetAttr(x,"emailForward");
285       if len(Forward) > 200:
286          continue;
287
288       # Check the forwarding address
289       if EmailCheck.match(Forward) == None:
290          continue;
291          
292       User = GetAttr(x,"uid");
293       Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
294    Fdb.write("\n");
295   # Oops, something unspeakable happened.
296   except:
297     Fdb.close();
298     raise;
299   if Fdb.close() != None:
300     raise "cdbmake gave an error";
301
302 # Generate the anon XEarth marker file 
303 def GenMarkers(l,File):
304   F = None;
305   Fdb = None;
306   try:
307    F = open(File + ".tmp","w");
308    Fdb = None;
309
310    # Fetch all the users
311    global PasswdAttrs;
312    if PasswdAttrs == None:
313       raise "No Users";
314
315    # Write out the position for each user
316    for x in PasswdAttrs:
317       if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
318          continue;       
319       try:
320          Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
321          Line = Sanitize(Line) + "\n";
322          F.write(Line);
323       except:
324          pass;
325       
326   # Oops, something unspeakable happened.
327   except:
328    Die(File,F,Fdb);
329    raise;
330   Done(File,F,Fdb);
331
332 # Generate the debian-private subscription list
333 def GenPrivate(l,File):
334   F = None;
335   Fdb = None;
336   try:
337    F = open(File + ".tmp","w");
338    Fdb = None;
339
340    # Fetch all the users
341    global PasswdAttrs;
342    if PasswdAttrs == None:
343       raise "No Users";
344
345    # Write out the position for each user
346    for x in PasswdAttrs:
347       if x[1].has_key("privateSub") == 0:
348          continue;
349
350       # If the account is locked, do not write it
351       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1):
352          continue;
353
354       # If the account has no PGP key, do not write it
355       if x[1].has_key("keyFingerPrint") == 0:
356          continue;
357
358       # Must be in the Debian group (yuk, hard coded for now)
359       if GetAttr(x,"gidNumber") != "800":
360          continue;
361
362       try:
363          Line = "%s"%(GetAttr(x,"privateSub"));
364          Line = Sanitize(Line) + "\n";
365          F.write(Line);
366       except:
367          pass;
368       
369   # Oops, something unspeakable happened.
370   except:
371    Die(File,F,Fdb);
372    raise;
373   Done(File,F,Fdb);
374
375 # Generate the DNS Zone file
376 def GenDNS(l,File,HomePrefix):
377   F = None;
378   try:
379    F = open(File + ".tmp","w");
380    
381    # Fetch all the users
382    global PasswdAttrs;
383    if PasswdAttrs == None:
384       raise "No Users";
385
386    # Write out the zone file entry for each user
387    for x in PasswdAttrs:
388       if x[1].has_key("dnsZoneEntry") == 0:
389          continue;
390       try:
391          F.write("; %s\n"%(EmailAddress(x)));
392          for z in x[1]["dnsZoneEntry"]:
393             Split = string.split(string.lower(z));
394             if string.lower(Split[1]) == 'in':
395                for y in range(0,len(Split)):
396                   if Split[y] == "$":
397                      Split[y] = "\n\t";
398                Line = string.join(Split," ") + "\n";
399                F.write(Line);
400                
401                Host = Split[0] + DNSZone;
402                if BSMTPCheck.match(Line) != None:
403                    F.write("; Has BSMTP\n");
404                                
405                # Write some identification information
406                if string.lower(Split[2]) == "a":
407                   Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
408                   for y in x[1]["keyFingerPrint"]:
409                      Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
410                   F.write(Line);
411             else:
412                Line = "; Err %s"%(str(Split));
413                F.write(Line);
414
415          F.write("\n");
416       except:
417          F.write("; Errors\n");
418          pass;
419       
420   # Oops, something unspeakable happened.
421   except:
422    Die(File,F,None);
423    raise;
424   Done(File,F,None);
425
426 # Generate the BSMTP file
427 def GenBSMTP(l,File,HomePrefix):
428   F = None;
429   try:
430    F = open(File + ".tmp","w");
431    
432    # Fetch all the users
433    global PasswdAttrs;
434    if PasswdAttrs == None:
435       raise "No Users";
436
437    # Write out the zone file entry for each user
438    for x in PasswdAttrs:
439       if x[1].has_key("dnsZoneEntry") == 0:
440          continue;
441       try:
442          for z in x[1]["dnsZoneEntry"]:
443             Split = string.split(string.lower(z));
444             if string.lower(Split[1]) == 'in':
445                for y in range(0,len(Split)):
446                   if Split[y] == "$":
447                      Split[y] = "\n\t";
448                Line = string.join(Split," ") + "\n";
449                
450                Host = Split[0] + DNSZone;
451                if BSMTPCheck.match(Line) != None:
452                    F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
453                                GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
454                                
455       except:
456          F.write("; Errors\n");
457          pass;
458       
459   # Oops, something unspeakable happened.
460   except:
461    Die(File,F,None);
462    raise;
463   Done(File,F,None);
464
465 # Generate the shadow list
466 def GenSSHKnown(l,File):
467   F = None;
468   try:
469    OldMask = os.umask(0022);
470    F = open(File + ".tmp","w",0644);
471    os.umask(OldMask);
472
473    # Fetch all the hosts
474    HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
475                 ["hostname","sshRSAHostKey"]);
476    
477    if HostKeys == None:
478       raise "No Hosts";
479
480    for x in HostKeys:
481       if x[1].has_key("hostname") == 0 or \
482          x[1].has_key("sshRSAHostKey") == 0:
483          continue;
484       Host = GetAttr(x,"hostname");
485       SHost = string.find(Host,".");
486       for I in x[1]["sshRSAHostKey"]:
487          if SHost == None:
488             Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
489          else:
490             Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
491          Line = Sanitize(Line) + "\n";
492          F.write(Line);
493   # Oops, something unspeakable happened.
494   except:
495    Die(File,F,None);
496    raise;
497   Done(File,F,None);
498
499
500 # Connect to the ldap server
501 l = ldap.open(LDAPServer);
502 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
503 Pass = string.split(string.strip(F.readline())," ");
504 F.close();
505 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
506
507 # Fetch all the groups
508 GroupIDMap = {};
509 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
510                   ["gid","gidNumber"]);
511
512 # Generate the GroupMap and GroupIDMap
513 for x in Attrs:
514    if x[1].has_key("gidNumber") == 0:
515       continue;
516    GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
517
518 # Fetch all the users
519 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
520                 ["uid","uidNumber","gidNumber","supplementaryGid",\
521                  "gecos","loginShell","userPassword","shadowLastChange",\
522                  "shadowMin","shadowMax","shadowWarning","shadowinactive",
523                  "shadowexpire","emailForward","latitude","longitude",\
524                  "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
525                  "keyFingerPrint","privateSub"]);
526
527 # Open the control file
528 if len(sys.argv) == 1:
529    F = open(GenerateConf,"r");
530 else:
531    F = open(sys.argv[1],"r")
532
533 # Generate global things
534 GlobalDir = GenerateDir+"/";
535 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
536 GenAllForward(l,GlobalDir+"mail-forward.cdb");
537 GenMarkers(l,GlobalDir+"markers");
538 GenPrivate(l,GlobalDir+"debian-private");
539 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
540
541 # Compatibility.
542 GenForward(l,GlobalDir+"forward-alias");
543    
544 while(1):
545    Line = F.readline();
546    if Line == "":
547       break;
548    Line = string.strip(Line);
549    if Line == "":
550       continue;
551    if Line[0] == '#':
552       continue;
553
554    Split = string.split(Line," ");
555    OutDir = GenerateDir + '/' + Split[0] + '/';
556    try: os.mkdir(OutDir);
557    except: pass;
558
559    # Get the group list and convert any named groups to numerics
560    GroupList = {};
561    ExtraList = {};
562    for I in Split[2:]:
563       if I[0] == '[':
564          ExtraList[I] = None;
565          continue;
566       GroupList[I] = None;
567       if GroupIDMap.has_key(I):
568          GroupList[str(GroupIDMap[I])] = None;
569
570    Allowed = GroupList;
571    CurrentHost = Split[0];
572
573    sys.stdout.flush();
574    GenPasswd(l,OutDir+"passwd",Split[1]);
575    sys.stdout.flush();
576    GenGroup(l,OutDir+"group");
577    GenShadow(l,OutDir+"shadow");
578         
579    # Link in global things   
580    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
581    DoLink(GlobalDir,OutDir,"markers");
582    DoLink(GlobalDir,OutDir,"mail-forward.cdb");
583    DoLink(GlobalDir,OutDir,"ssh_known_hosts");
584
585    # Compatibility.
586    DoLink(GlobalDir,OutDir,"forward-alias");
587
588    if ExtraList.has_key("[DNS]"):
589       GenDNS(l,OutDir+"dns-zone",Split[1]);
590       
591    if ExtraList.has_key("[BSMTP]"):
592       GenBSMTP(l,OutDir+"bsmtp",Split[1]);
593
594    if ExtraList.has_key("[PRIVATE]"):
595       DoLink(GlobalDir,OutDir,"debian-private");
596