Generate a disabled-accounts file exported to all hosts
[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 #   Copyright (c) 2000-2001  Jason Gunthorpe <jgg@debian.org>
6 #   Copyright (c) 2003-2004  James Troup <troup@debian.org>
7 #   Copyright (c) 2004-2005,7  Joey Schulze <joey@infodrom.org>
8 #   Copyright (c) 2001-2007  Ryan Murray <rmurray@debian.org>
9 #
10 #   This program is free software; you can redistribute it and/or modify
11 #   it under the terms of the GNU General Public License as published by
12 #   the Free Software Foundation; either version 2 of the License, or
13 #   (at your option) any later version.
14 #
15 #   This program is distributed in the hope that it will be useful,
16 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #   GNU General Public License for more details.
19 #
20 #   You should have received a copy of the GNU General Public License
21 #   along with this program; if not, write to the Free Software
22 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 import string, re, time, ldap, getopt, sys, os, pwd, posix, socket, base64, sha
25 from userdir_ldap import *;
26
27 global Allowed;
28 global CurrentHost;
29
30 PasswdAttrs = None;
31 GroupIDMap = {};
32 Allowed = None;
33 CurrentHost = "";
34
35 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
36 BSMTPCheck = re.compile(".*mx 0 (gluck)\.debian\.org\..*",re.DOTALL);
37 DNSZone = ".debian.net"
38
39 def Sanitize(Str):
40   return string.translate(Str,string.maketrans("\n\r\t","$$$"));
41
42 def DoLink(From,To,File):
43    try: posix.remove(To+File);
44    except: pass;
45    posix.link(From+File,To+File);
46
47 # See if this user is in the group list
48 def IsInGroup(DnRecord):
49   if Allowed == None:
50      return 1;
51
52   # See if the primary group is in the list
53   if Allowed.has_key(GetAttr(DnRecord,"gidNumber")) != 0:
54      return 1;
55
56   # Check the host based ACL
57   if DnRecord[1].has_key("allowedHost") != 0:
58      for I in DnRecord[1]["allowedHost"]:
59         if CurrentHost == I:
60            return 1;
61
62   # See if there are supplementary groups
63   if DnRecord[1].has_key("supplementaryGid") == 0:
64      return 0;
65
66   # Check the supplementary groups
67   for I in DnRecord[1]["supplementaryGid"]:
68      if Allowed.has_key(I):
69         return 1;
70   return 0;
71
72 def Die(File,F,Fdb):
73    if F != None:
74       F.close();
75    if Fdb != None:
76       Fdb.close();
77    try: os.remove(File + ".tmp");
78    except: pass;
79    try: os.remove(File + ".tdb.tmp");
80    except: pass;
81
82 def Done(File,F,Fdb):
83   if F != None:
84     F.close();
85     os.rename(File + ".tmp",File);
86   if Fdb != None:
87     Fdb.close();
88     os.rename(File + ".tdb.tmp",File+".tdb");
89   
90 # Generate the password list
91 def GenPasswd(l,File,HomePrefix):
92   F = None;
93   try:
94    F = open(File + ".tdb.tmp","w");
95
96    # Fetch all the users
97    global PasswdAttrs;
98    if PasswdAttrs == None:
99       raise "No Users";
100
101    I = 0;
102    for x in PasswdAttrs:
103       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
104          continue;
105
106       # Do not let people try to buffer overflow some busted passwd parser.
107       if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginShell")) > 50:
108          continue;
109
110       Line = "%s:x:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
111               GetAttr(x,"uidNumber"),GetAttr(x,"gidNumber"),\
112               GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
113               GetAttr(x,"loginShell"));
114
115       Line = Sanitize(Line) + "\n";
116       F.write("0%u %s" % (I,Line));
117       F.write(".%s %s" % (GetAttr(x,"uid"),Line));
118       F.write("=%s %s" % (GetAttr(x,"uidNumber"),Line));
119       I = I + 1;
120
121   # Oops, something unspeakable happened.
122   except:
123    Die(File,None,F);
124    raise;
125   Done(File,None,F);
126
127 # Generate the shadow list
128 def GenShadow(l,File):
129   F = None;
130   try:
131    OldMask = os.umask(0077);
132    F = open(File + ".tdb.tmp","w",0600);
133    os.umask(OldMask);
134
135    # Fetch all the users
136    global PasswdAttrs;
137    if PasswdAttrs == None:
138       raise "No Users";
139
140    I = 0;
141    for x in PasswdAttrs:
142       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
143          continue;
144          
145       Pass = GetAttr(x,"userPassword");
146       if Pass[0:7] != "{crypt}" or len(Pass) > 50:
147          Pass = '*';
148       else:
149          Pass = Pass[7:];
150       Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
151               Pass,GetAttr(x,"shadowLastChange"),\
152               GetAttr(x,"shadowMin"),GetAttr(x,"shadowMax"),\
153               GetAttr(x,"shadowWarning"),GetAttr(x,"shadowinactive"),\
154               GetAttr(x,"shadowexpire"));
155       Line = Sanitize(Line) + "\n";
156       F.write("0%u %s" % (I,Line));
157       F.write(".%s %s" % (GetAttr(x,"uid"),Line));
158       I = I + 1;
159
160   # Oops, something unspeakable happened.
161   except:
162    Die(File,None,F);
163    raise;
164   Done(File,None,F);
165
166 # Generate the shadow list
167 def GenSSHShadow(l,File):
168   F = None;
169   try:
170    OldMask = os.umask(0077);
171    F = open(File + ".tmp","w",0600);
172    os.umask(OldMask);
173
174    # Fetch all the users
175    global PasswdAttrs;
176    if PasswdAttrs == None:
177       raise "No Users";
178
179    for x in PasswdAttrs:
180       # If the account is locked, do not write it.
181       # This is a partial stop-gap. The ssh also needs to change this
182       # to ignore ~/.ssh/authorized* files.
183       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1) \
184              or GetAttr(x,"userPassword").startswith("!"):
185          continue;
186
187       if x[1].has_key("uidNumber") == 0 or \
188          x[1].has_key("sshRSAAuthKey") == 0:
189          continue;
190       for I in x[1]["sshRSAAuthKey"]:
191          User = GetAttr(x,"uid");
192          Line = "%s: %s" %(User,I);
193          Line = Sanitize(Line) + "\n";
194          F.write(Line);
195   # Oops, something unspeakable happened.
196   except:
197    Die(File,F,None);
198    raise;
199   Done(File,F,None);
200
201 # Generate the group list
202 def GenGroup(l,File):
203   F = None;
204   try:
205    F = open(File + ".tdb.tmp","w");
206
207    # Generate the GroupMap
208    GroupMap = {};
209    for x in GroupIDMap.keys():
210       GroupMap[x] = [];
211       
212    # Fetch all the users
213    global PasswdAttrs;
214    if PasswdAttrs == None:
215       raise "No Users";
216
217    # Sort them into a list of groups having a set of users
218    for x in PasswdAttrs:
219       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
220          continue;
221       if x[1].has_key("supplementaryGid") == 0:
222          continue;
223          
224       for I in x[1]["supplementaryGid"]:
225          if GroupMap.has_key(I):
226             GroupMap[I].append(GetAttr(x,"uid"));
227          else:
228             print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
229             
230    # Output the group file.
231    J = 0;
232    for x in GroupMap.keys():
233       if GroupIDMap.has_key(x) == 0:
234          continue;
235       Line = "%s:x:%u:" % (x,GroupIDMap[x]);
236       Comma = '';
237       for I in GroupMap[x]:
238         Line = Line + ("%s%s" % (Comma,I));
239         Comma = ',';
240       Line = Sanitize(Line) + "\n";
241       F.write("0%u %s" % (J,Line));
242       F.write(".%s %s" % (x,Line));
243       F.write("=%u %s" % (GroupIDMap[x],Line));
244       J = J + 1;
245       
246   # Oops, something unspeakable happened.
247   except:
248    Die(File,None,F);
249    raise;
250   Done(File,None,F);
251
252 # Generate the email forwarding list
253 def GenForward(l,File):
254   F = None;
255   try:
256    OldMask = os.umask(0022);
257    F = open(File + ".tmp","w",0644);
258    os.umask(OldMask);
259
260    # Fetch all the users
261    global PasswdAttrs;
262    if PasswdAttrs == None:
263       raise "No Users";
264
265    # Write out the email address for each user
266    for x in PasswdAttrs:
267       if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
268          continue;
269       
270       # Do not allow people to try to buffer overflow busted parsers
271       if len(GetAttr(x,"emailForward")) > 200:
272          continue;
273
274       # Check the forwarding address
275       if EmailCheck.match(GetAttr(x,"emailForward")) == None:
276          continue;
277       Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
278       Line = Sanitize(Line) + "\n";
279       F.write(Line);
280       
281   # Oops, something unspeakable happened.
282   except:
283    Die(File,F,None);
284    raise;
285   Done(File,F,None);
286
287 def GenAllForward(l,File):
288   Fdb = None;
289   try:
290    OldMask = os.umask(0022);
291    Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
292    os.umask(OldMask);
293
294    # Fetch all the users
295    global PasswdAttrs;
296    if PasswdAttrs == None:
297       raise "No Users";
298
299    # Write out the email address for each user
300    for x in PasswdAttrs:
301       if x[1].has_key("emailForward") == 0:
302          continue;
303       
304       # Do not allow people to try to buffer overflow busted parsers
305       Forward = GetAttr(x,"emailForward");
306       if len(Forward) > 200:
307          continue;
308
309       # Check the forwarding address
310       if EmailCheck.match(Forward) == None:
311          continue;
312          
313       User = GetAttr(x,"uid");
314       Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
315    Fdb.write("\n");
316   # Oops, something unspeakable happened.
317   except:
318     Fdb.close();
319     raise;
320   if Fdb.close() != None:
321     raise "cdbmake gave an error";
322
323 # Generate the anon XEarth marker file 
324 def GenMarkers(l,File):
325   F = None;
326   try:
327    F = open(File + ".tmp","w");
328
329    # Fetch all the users
330    global PasswdAttrs;
331    if PasswdAttrs == None:
332       raise "No Users";
333
334    # Write out the position for each user
335    for x in PasswdAttrs:
336       if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
337          continue;       
338       try:
339          Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
340          Line = Sanitize(Line) + "\n";
341          F.write(Line);
342       except:
343          pass;
344       
345   # Oops, something unspeakable happened.
346   except:
347    Die(File,F,None);
348    raise;
349   Done(File,F,None);
350
351 # Generate the debian-private subscription list
352 def GenPrivate(l,File):
353   F = None;
354   try:
355    F = open(File + ".tmp","w");
356
357    # Fetch all the users
358    global PasswdAttrs;
359    if PasswdAttrs == None:
360       raise "No Users";
361
362    # Write out the position for each user
363    for x in PasswdAttrs:
364       if x[1].has_key("privateSub") == 0:
365          continue;
366
367       # If the account is locked, do not write it
368       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1) \
369              or GetAttr(x,"userPassword").startswith("!"):
370          continue;
371
372       # If the account has no PGP key, do not write it
373       if x[1].has_key("keyFingerPrint") == 0:
374          continue;
375
376       # Must be in the Debian group (yuk, hard coded for now)
377       if GetAttr(x,"gidNumber") != "800":
378          continue;
379
380       try:
381          Line = "%s"%(GetAttr(x,"privateSub"));
382          Line = Sanitize(Line) + "\n";
383          F.write(Line);
384       except:
385          pass;
386       
387   # Oops, something unspeakable happened.
388   except:
389    Die(File,F,None);
390    raise;
391   Done(File,F,None);
392
393 # Generate a list of locked accounts
394 def GenDisabledAccounts(l,File):
395   F = None;
396   try:
397    F = open(File + ".tmp","w");
398
399    # Fetch all the users
400    global PasswdAttrs;
401    if PasswdAttrs == None:
402       raise "No Users";
403
404    I = 0;
405    for x in PasswdAttrs:
406       if x[1].has_key("uidNumber") == 0:
407          continue;
408          
409       Pass = GetAttr(x,"userPassword");
410       Line = ""
411       # *LK* is the reference value for a locked account
412       # password starting with ! is also a locked account
413       if string.find(Pass,"*LK*") != -1 or Pass.startswith("!"):
414          # Format is <login>:<reason>
415          Line = "%s:%s" % (GetAttr(x,"uid"), "Account is locked")
416
417       if Line != "":
418          F.write(Sanitize(Line) + "\n")
419
420   # Oops, something unspeakable happened.
421   except:
422    Die(File,F,None);
423    raise;
424   Done(File,F,None);
425
426 # Generate the list of local addresses that refuse all mail
427 def GenMailDisable(l,File):
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    for x in PasswdAttrs:
438       Reason = None
439       
440       # If the account is locked, disable incoming mail
441       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1):
442          if GetAttr(x,"uid") == "luther":
443             continue
444          else:
445             Reason = "user account locked"
446       else:
447          if x[1].has_key("mailDisableMessage"):
448             Reason = GetAttr(x,"mailDisableMessage")
449          else:
450             continue
451
452       # Must be in the Debian group (yuk, hard coded for now)
453       if GetAttr(x,"gidNumber") != "800":
454          continue;
455
456       try:
457          Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
458          Line = Sanitize(Line) + "\n";
459          F.write(Line);
460       except:
461          pass;
462       
463   # Oops, something unspeakable happened.
464   except:
465    Die(File,F,None);
466    raise;
467   Done(File,F,None);
468
469 # Generate a list of uids that should have boolean affects applied
470 def GenMailBool(l,File,Key):
471   F = None;
472   try:
473    F = open(File + ".tmp","w");
474
475    # Fetch all the users
476    global PasswdAttrs;
477    if PasswdAttrs == None:
478       raise "No Users";
479
480    for x in PasswdAttrs:
481       Reason = None
482       
483       if x[1].has_key(Key) == 0:
484          continue
485
486       # Must be in the Debian group (yuk, hard coded for now)
487       if GetAttr(x,"gidNumber") != "800":
488          continue
489
490       if GetAttr(x,Key) != "TRUE":
491          continue
492
493       try:
494          Line = "%s"%(GetAttr(x,"uid"));
495          Line = Sanitize(Line) + "\n";
496          F.write(Line);
497       except:
498          pass;
499       
500   # Oops, something unspeakable happened.
501   except:
502    Die(File,F,None);
503    raise;
504   Done(File,F,None);
505
506 # Generate a list of hosts for RBL or whitelist purposes.
507 def GenMailList(l,File,Key):
508   F = None;
509   try:
510    F = open(File + ".tmp","w");
511
512    # Fetch all the users
513    global PasswdAttrs;
514    if PasswdAttrs == None:
515       raise "No Users";
516
517    for x in PasswdAttrs:
518       Reason = None
519       
520       if x[1].has_key(Key) == 0:
521          continue
522
523       # Must be in the Debian group (yuk, hard coded for now)
524       if GetAttr(x,"gidNumber") != "800":
525          continue
526
527       try:
528          found = 0
529          Line = None
530          for z in x[1][Key]:
531              if Key == "mailWhitelist":
532                  if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
533                      continue
534              else:
535                  if re.match('^[-\w.]+$',z) == None:
536                      continue
537              if found == 0:
538                  found = 1
539                  Line = GetAttr(x,"uid")
540              else:
541                  Line += " "
542              Line += ": " + z
543              if Key == "mailRHSBL":
544                  Line += "/$sender_address_domain"
545
546          if Line != None:
547              Line = Sanitize(Line) + "\n";
548              F.write(Line);
549       except:
550          pass;
551       
552   # Oops, something unspeakable happened.
553   except:
554    Die(File,F,None);
555    raise;
556   Done(File,F,None);
557
558 # Generate the DNS Zone file
559 def GenDNS(l,File,HomePrefix):
560   F = None;
561   try:
562    F = open(File + ".tmp","w");
563    
564    # Fetch all the users
565    global PasswdAttrs;
566    if PasswdAttrs == None:
567       raise "No Users";
568
569    # Write out the zone file entry for each user
570    for x in PasswdAttrs:
571       if x[1].has_key("dnsZoneEntry") == 0:
572          continue;
573
574       # If the account has no PGP key, do not write it
575       if x[1].has_key("keyFingerPrint") == 0:
576          continue;
577       try:
578          F.write("; %s\n"%(EmailAddress(x)));
579          for z in x[1]["dnsZoneEntry"]:
580             Split = string.split(string.lower(z));
581             if string.lower(Split[1]) == 'in':
582                for y in range(0,len(Split)):
583                   if Split[y] == "$":
584                      Split[y] = "\n\t";
585                Line = string.join(Split," ") + "\n";
586                F.write(Line);
587                
588                Host = Split[0] + DNSZone;
589                if BSMTPCheck.match(Line) != None:
590                    F.write("; Has BSMTP\n");
591                                
592                # Write some identification information
593                if string.lower(Split[2]) == "a":
594                   Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
595                   for y in x[1]["keyFingerPrint"]:
596                      Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
597                   F.write(Line);
598             else:
599                Line = "; Err %s"%(str(Split));
600                F.write(Line);
601
602          F.write("\n");
603       except:
604          F.write("; Errors\n");
605          pass;
606       
607   # Oops, something unspeakable happened.
608   except:
609    Die(File,F,None);
610    raise;
611   Done(File,F,None);
612
613 # Generate the DNS SSHFP records
614 def GenSSHFP(l,File,HomePrefix):
615   F = None
616   try:
617    F = open(File + ".tmp","w")
618    
619    # Fetch all the hosts
620    global HostAttrs
621    if HostAttrs == None:
622       raise "No Hosts"
623
624    for x in HostAttrs:
625       if x[1].has_key("hostname") == 0 or \
626          x[1].has_key("sshRSAHostKey") == 0:
627          continue
628       Host = GetAttr(x,"hostname");
629       Algorithm = None
630       for I in x[1]["sshRSAHostKey"]:
631          Split = string.split(I)
632          if Split[0] == 'ssh-rsa':
633             Algorithm = 1
634          if Split[0] == 'ssh-dss':
635             Algorithm = 2
636          if Algorithm == None:
637             continue
638          Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
639          Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
640          Line = Sanitize(Line) + "\n"
641          F.write(Line)
642   # Oops, something unspeakable happened.
643   except:
644    Die(File,F,None)
645    raise;
646   Done(File,F,None)
647
648 # Generate the BSMTP file
649 def GenBSMTP(l,File,HomePrefix):
650   F = None;
651   try:
652    F = open(File + ".tmp","w");
653    
654    # Fetch all the users
655    global PasswdAttrs;
656    if PasswdAttrs == None:
657       raise "No Users";
658
659    # Write out the zone file entry for each user
660    for x in PasswdAttrs:
661       if x[1].has_key("dnsZoneEntry") == 0:
662          continue;
663
664       # If the account has no PGP key, do not write it
665       if x[1].has_key("keyFingerPrint") == 0:
666          continue;
667       try:
668          for z in x[1]["dnsZoneEntry"]:
669             Split = string.split(string.lower(z));
670             if string.lower(Split[1]) == 'in':
671                for y in range(0,len(Split)):
672                   if Split[y] == "$":
673                      Split[y] = "\n\t";
674                Line = string.join(Split," ") + "\n";
675                
676                Host = Split[0] + DNSZone;
677                if BSMTPCheck.match(Line) != None:
678                    F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
679                                GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
680                                
681       except:
682          F.write("; Errors\n");
683          pass;
684       
685   # Oops, something unspeakable happened.
686   except:
687    Die(File,F,None);
688    raise;
689   Done(File,F,None);
690
691 # Generate the ssh known hosts file
692 def GenSSHKnown(l,File):
693   F = None;
694   try:
695    OldMask = os.umask(0022);
696    F = open(File + ".tmp","w",0644);
697    os.umask(OldMask);
698
699    global HostAttrs
700    if HostAttrs == None:
701       raise "No Hosts";
702    
703    for x in HostAttrs:
704       if x[1].has_key("hostname") == 0 or \
705          x[1].has_key("sshRSAHostKey") == 0:
706          continue;
707       Host = GetAttr(x,"hostname");
708       SHost = string.find(Host,".");
709       for I in x[1]["sshRSAHostKey"]:
710          if SHost == None:
711             Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
712          else:
713             Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
714          Line = Sanitize(Line) + "\n";
715          F.write(Line);
716   # Oops, something unspeakable happened.
717   except:
718    Die(File,F,None);
719    raise;
720   Done(File,F,None);
721
722 # Generate the debianhosts file (list of all IP addresses)
723 def GenHosts(l,File):
724   F = None;
725   try:
726    OldMask = os.umask(0022);
727    F = open(File + ".tmp","w",0644);
728    os.umask(OldMask);
729
730    # Fetch all the hosts
731    HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
732                 ["hostname"]);
733    
734    if HostNames == None:
735       raise "No Hosts";
736
737    for x in HostNames:
738       if x[1].has_key("hostname") == 0:
739          continue;
740       Host = GetAttr(x,"hostname");
741       try:
742         Addr = socket.gethostbyname(Host);
743         F.write(Addr + "\n");
744       except:
745         pass
746   # Oops, something unspeakable happened.
747   except:
748    Die(File,F,None);
749    raise;
750   Done(File,F,None);
751
752 # Connect to the ldap server
753 l = ldap.open(LDAPServer);
754 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
755 Pass = string.split(string.strip(F.readline())," ");
756 F.close();
757 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
758
759 # Fetch all the groups
760 GroupIDMap = {};
761 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
762                   ["gid","gidNumber"]);
763
764 # Generate the GroupMap and GroupIDMap
765 for x in Attrs:
766    if x[1].has_key("gidNumber") == 0:
767       continue;
768    GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
769
770 # Fetch all the users
771 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
772                 ["uid","uidNumber","gidNumber","supplementaryGid",\
773                  "gecos","loginShell","userPassword","shadowLastChange",\
774                  "shadowMin","shadowMax","shadowWarning","shadowinactive",
775                  "shadowexpire","emailForward","latitude","longitude",\
776                  "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
777                  "keyFingerPrint","privateSub","mailDisableMessage",\
778                  "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
779                  "mailWhitelist"]);
780 # Fetch all the hosts
781 HostAttrs    = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
782                 ["hostname","sshRSAHostKey"]);
783
784 # Open the control file
785 if len(sys.argv) == 1:
786    F = open(GenerateConf,"r");
787 else:
788    F = open(sys.argv[1],"r")
789
790 # Generate global things
791 GlobalDir = GenerateDir+"/";
792 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
793 GenAllForward(l,GlobalDir+"mail-forward.cdb");
794 GenMarkers(l,GlobalDir+"markers");
795 GenPrivate(l,GlobalDir+"debian-private");
796 GenDisabledAccounts(l,GlobalDir+"disabled-accounts");
797 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
798 GenHosts(l,GlobalDir+"debianhosts");
799 GenMailDisable(l,GlobalDir+"mail-disable");
800 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
801 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
802 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
803 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
804 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
805
806 # Compatibility.
807 GenForward(l,GlobalDir+"forward-alias");
808    
809 while(1):
810    Line = F.readline();
811    if Line == "":
812       break;
813    Line = string.strip(Line);
814    if Line == "":
815       continue;
816    if Line[0] == '#':
817       continue;
818
819    Split = string.split(Line," ");
820    OutDir = GenerateDir + '/' + Split[0] + '/';
821    try: os.mkdir(OutDir);
822    except: pass;
823
824    # Get the group list and convert any named groups to numerics
825    GroupList = {};
826    ExtraList = {};
827    for I in Split[2:]:
828       if I[0] == '[':
829          ExtraList[I] = None;
830          continue;
831       GroupList[I] = None;
832       if GroupIDMap.has_key(I):
833          GroupList[str(GroupIDMap[I])] = None;
834
835    Allowed = GroupList;
836    if Allowed == {}:
837      Allowed = None
838    CurrentHost = Split[0];
839
840    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
841    DoLink(GlobalDir,OutDir,"debianhosts");
842    DoLink(GlobalDir,OutDir,"ssh_known_hosts");
843    DoLink(GlobalDir,OutDir,"disabled-accounts")
844
845    sys.stdout.flush();
846    GenPasswd(l,OutDir+"passwd",Split[1]);
847    sys.stdout.flush();
848    GenGroup(l,OutDir+"group");
849    if ExtraList.has_key("[UNTRUSTED]"):
850         continue;
851    if not ExtraList.has_key("[NOPASSWD]"):
852      GenShadow(l,OutDir+"shadow");
853         
854    # Link in global things   
855    DoLink(GlobalDir,OutDir,"markers");
856    DoLink(GlobalDir,OutDir,"mail-forward.cdb");
857    DoLink(GlobalDir,OutDir,"mail-disable");
858    DoLink(GlobalDir,OutDir,"mail-greylist");
859    DoLink(GlobalDir,OutDir,"mail-callout");
860    DoLink(GlobalDir,OutDir,"mail-rbl");
861    DoLink(GlobalDir,OutDir,"mail-rhsbl");
862    DoLink(GlobalDir,OutDir,"mail-whitelist");
863
864    # Compatibility.
865    DoLink(GlobalDir,OutDir,"forward-alias");
866
867    if ExtraList.has_key("[DNS]"):
868       GenDNS(l,OutDir+"dns-zone",Split[1]);
869       GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
870       
871    if ExtraList.has_key("[BSMTP]"):
872       GenBSMTP(l,OutDir+"bsmtp",Split[1])
873
874    if ExtraList.has_key("[PRIVATE]"):
875       DoLink(GlobalDir,OutDir,"debian-private")