change *PK* to !, and fully implement it. remove obsolete/broken ud-killcrypt
[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 the list of local addresses that refuse all mail
394 def GenMailDisable(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    for x in PasswdAttrs:
405       Reason = None
406       
407       # If the account is locked, disable incoming mail
408       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1):
409          if GetAttr(x,"uid") == "luther":
410             continue
411          else:
412             Reason = "user account locked"
413       else:
414          if x[1].has_key("mailDisableMessage"):
415             Reason = GetAttr(x,"mailDisableMessage")
416          else:
417             continue
418
419       # Must be in the Debian group (yuk, hard coded for now)
420       if GetAttr(x,"gidNumber") != "800":
421          continue;
422
423       try:
424          Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
425          Line = Sanitize(Line) + "\n";
426          F.write(Line);
427       except:
428          pass;
429       
430   # Oops, something unspeakable happened.
431   except:
432    Die(File,F,None);
433    raise;
434   Done(File,F,None);
435
436 # Generate a list of uids that should have boolean affects applied
437 def GenMailBool(l,File,Key):
438   F = None;
439   try:
440    F = open(File + ".tmp","w");
441
442    # Fetch all the users
443    global PasswdAttrs;
444    if PasswdAttrs == None:
445       raise "No Users";
446
447    for x in PasswdAttrs:
448       Reason = None
449       
450       if x[1].has_key(Key) == 0:
451          continue
452
453       # Must be in the Debian group (yuk, hard coded for now)
454       if GetAttr(x,"gidNumber") != "800":
455          continue
456
457       if GetAttr(x,Key) != "TRUE":
458          continue
459
460       try:
461          Line = "%s"%(GetAttr(x,"uid"));
462          Line = Sanitize(Line) + "\n";
463          F.write(Line);
464       except:
465          pass;
466       
467   # Oops, something unspeakable happened.
468   except:
469    Die(File,F,None);
470    raise;
471   Done(File,F,None);
472
473 # Generate a list of hosts for RBL or whitelist purposes.
474 def GenMailList(l,File,Key):
475   F = None;
476   try:
477    F = open(File + ".tmp","w");
478
479    # Fetch all the users
480    global PasswdAttrs;
481    if PasswdAttrs == None:
482       raise "No Users";
483
484    for x in PasswdAttrs:
485       Reason = None
486       
487       if x[1].has_key(Key) == 0:
488          continue
489
490       # Must be in the Debian group (yuk, hard coded for now)
491       if GetAttr(x,"gidNumber") != "800":
492          continue
493
494       try:
495          found = 0
496          Line = None
497          for z in x[1][Key]:
498              if Key == "mailWhitelist":
499                  if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
500                      continue
501              else:
502                  if re.match('^[-\w.]+$',z) == None:
503                      continue
504              if found == 0:
505                  found = 1
506                  Line = GetAttr(x,"uid")
507              else:
508                  Line += " "
509              Line += ": " + z
510              if Key == "mailRHSBL":
511                  Line += "/$sender_address_domain"
512
513          if Line != None:
514              Line = Sanitize(Line) + "\n";
515              F.write(Line);
516       except:
517          pass;
518       
519   # Oops, something unspeakable happened.
520   except:
521    Die(File,F,None);
522    raise;
523   Done(File,F,None);
524
525 # Generate the DNS Zone file
526 def GenDNS(l,File,HomePrefix):
527   F = None;
528   try:
529    F = open(File + ".tmp","w");
530    
531    # Fetch all the users
532    global PasswdAttrs;
533    if PasswdAttrs == None:
534       raise "No Users";
535
536    # Write out the zone file entry for each user
537    for x in PasswdAttrs:
538       if x[1].has_key("dnsZoneEntry") == 0:
539          continue;
540
541       # If the account has no PGP key, do not write it
542       if x[1].has_key("keyFingerPrint") == 0:
543          continue;
544       try:
545          F.write("; %s\n"%(EmailAddress(x)));
546          for z in x[1]["dnsZoneEntry"]:
547             Split = string.split(string.lower(z));
548             if string.lower(Split[1]) == 'in':
549                for y in range(0,len(Split)):
550                   if Split[y] == "$":
551                      Split[y] = "\n\t";
552                Line = string.join(Split," ") + "\n";
553                F.write(Line);
554                
555                Host = Split[0] + DNSZone;
556                if BSMTPCheck.match(Line) != None:
557                    F.write("; Has BSMTP\n");
558                                
559                # Write some identification information
560                if string.lower(Split[2]) == "a":
561                   Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
562                   for y in x[1]["keyFingerPrint"]:
563                      Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
564                   F.write(Line);
565             else:
566                Line = "; Err %s"%(str(Split));
567                F.write(Line);
568
569          F.write("\n");
570       except:
571          F.write("; Errors\n");
572          pass;
573       
574   # Oops, something unspeakable happened.
575   except:
576    Die(File,F,None);
577    raise;
578   Done(File,F,None);
579
580 # Generate the DNS SSHFP records
581 def GenSSHFP(l,File,HomePrefix):
582   F = None
583   try:
584    F = open(File + ".tmp","w")
585    
586    # Fetch all the hosts
587    global HostAttrs
588    if HostAttrs == None:
589       raise "No Hosts"
590
591    for x in HostAttrs:
592       if x[1].has_key("hostname") == 0 or \
593          x[1].has_key("sshRSAHostKey") == 0:
594          continue
595       Host = GetAttr(x,"hostname");
596       Algorithm = None
597       for I in x[1]["sshRSAHostKey"]:
598          Split = string.split(I)
599          if Split[0] == 'ssh-rsa':
600             Algorithm = 1
601          if Split[0] == 'ssh-dss':
602             Algorithm = 2
603          if Algorithm == None:
604             continue
605          Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
606          Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
607          Line = Sanitize(Line) + "\n"
608          F.write(Line)
609   # Oops, something unspeakable happened.
610   except:
611    Die(File,F,None)
612    raise;
613   Done(File,F,None)
614
615 # Generate the BSMTP file
616 def GenBSMTP(l,File,HomePrefix):
617   F = None;
618   try:
619    F = open(File + ".tmp","w");
620    
621    # Fetch all the users
622    global PasswdAttrs;
623    if PasswdAttrs == None:
624       raise "No Users";
625
626    # Write out the zone file entry for each user
627    for x in PasswdAttrs:
628       if x[1].has_key("dnsZoneEntry") == 0:
629          continue;
630
631       # If the account has no PGP key, do not write it
632       if x[1].has_key("keyFingerPrint") == 0:
633          continue;
634       try:
635          for z in x[1]["dnsZoneEntry"]:
636             Split = string.split(string.lower(z));
637             if string.lower(Split[1]) == 'in':
638                for y in range(0,len(Split)):
639                   if Split[y] == "$":
640                      Split[y] = "\n\t";
641                Line = string.join(Split," ") + "\n";
642                
643                Host = Split[0] + DNSZone;
644                if BSMTPCheck.match(Line) != None:
645                    F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
646                                GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
647                                
648       except:
649          F.write("; Errors\n");
650          pass;
651       
652   # Oops, something unspeakable happened.
653   except:
654    Die(File,F,None);
655    raise;
656   Done(File,F,None);
657
658 # Generate the ssh known hosts file
659 def GenSSHKnown(l,File):
660   F = None;
661   try:
662    OldMask = os.umask(0022);
663    F = open(File + ".tmp","w",0644);
664    os.umask(OldMask);
665
666    global HostAttrs
667    if HostAttrs == None:
668       raise "No Hosts";
669    
670    for x in HostAttrs:
671       if x[1].has_key("hostname") == 0 or \
672          x[1].has_key("sshRSAHostKey") == 0:
673          continue;
674       Host = GetAttr(x,"hostname");
675       SHost = string.find(Host,".");
676       for I in x[1]["sshRSAHostKey"]:
677          if SHost == None:
678             Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
679          else:
680             Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
681          Line = Sanitize(Line) + "\n";
682          F.write(Line);
683   # Oops, something unspeakable happened.
684   except:
685    Die(File,F,None);
686    raise;
687   Done(File,F,None);
688
689 # Generate the debianhosts file (list of all IP addresses)
690 def GenHosts(l,File):
691   F = None;
692   try:
693    OldMask = os.umask(0022);
694    F = open(File + ".tmp","w",0644);
695    os.umask(OldMask);
696
697    # Fetch all the hosts
698    HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
699                 ["hostname"]);
700    
701    if HostNames == None:
702       raise "No Hosts";
703
704    for x in HostNames:
705       if x[1].has_key("hostname") == 0:
706          continue;
707       Host = GetAttr(x,"hostname");
708       try:
709         Addr = socket.gethostbyname(Host);
710         F.write(Addr + "\n");
711       except:
712         pass
713   # Oops, something unspeakable happened.
714   except:
715    Die(File,F,None);
716    raise;
717   Done(File,F,None);
718
719 # Connect to the ldap server
720 l = ldap.open(LDAPServer);
721 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
722 Pass = string.split(string.strip(F.readline())," ");
723 F.close();
724 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
725
726 # Fetch all the groups
727 GroupIDMap = {};
728 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
729                   ["gid","gidNumber"]);
730
731 # Generate the GroupMap and GroupIDMap
732 for x in Attrs:
733    if x[1].has_key("gidNumber") == 0:
734       continue;
735    GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
736
737 # Fetch all the users
738 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
739                 ["uid","uidNumber","gidNumber","supplementaryGid",\
740                  "gecos","loginShell","userPassword","shadowLastChange",\
741                  "shadowMin","shadowMax","shadowWarning","shadowinactive",
742                  "shadowexpire","emailForward","latitude","longitude",\
743                  "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
744                  "keyFingerPrint","privateSub","mailDisableMessage",\
745                  "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
746                  "mailWhitelist"]);
747 # Fetch all the hosts
748 HostAttrs    = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
749                 ["hostname","sshRSAHostKey"]);
750
751 # Open the control file
752 if len(sys.argv) == 1:
753    F = open(GenerateConf,"r");
754 else:
755    F = open(sys.argv[1],"r")
756
757 # Generate global things
758 GlobalDir = GenerateDir+"/";
759 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
760 GenAllForward(l,GlobalDir+"mail-forward.cdb");
761 GenMarkers(l,GlobalDir+"markers");
762 GenPrivate(l,GlobalDir+"debian-private");
763 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
764 GenHosts(l,GlobalDir+"debianhosts");
765 GenMailDisable(l,GlobalDir+"mail-disable");
766 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
767 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
768 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
769 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
770 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
771
772 # Compatibility.
773 GenForward(l,GlobalDir+"forward-alias");
774    
775 while(1):
776    Line = F.readline();
777    if Line == "":
778       break;
779    Line = string.strip(Line);
780    if Line == "":
781       continue;
782    if Line[0] == '#':
783       continue;
784
785    Split = string.split(Line," ");
786    OutDir = GenerateDir + '/' + Split[0] + '/';
787    try: os.mkdir(OutDir);
788    except: pass;
789
790    # Get the group list and convert any named groups to numerics
791    GroupList = {};
792    ExtraList = {};
793    for I in Split[2:]:
794       if I[0] == '[':
795          ExtraList[I] = None;
796          continue;
797       GroupList[I] = None;
798       if GroupIDMap.has_key(I):
799          GroupList[str(GroupIDMap[I])] = None;
800
801    Allowed = GroupList;
802    if Allowed == {}:
803      Allowed = None
804    CurrentHost = Split[0];
805
806    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
807    DoLink(GlobalDir,OutDir,"debianhosts");
808    DoLink(GlobalDir,OutDir,"ssh_known_hosts");
809
810    sys.stdout.flush();
811    GenPasswd(l,OutDir+"passwd",Split[1]);
812    sys.stdout.flush();
813    GenGroup(l,OutDir+"group");
814    if ExtraList.has_key("[UNTRUSTED]"):
815         continue;
816    if not ExtraList.has_key("[NOPASSWD]"):
817      GenShadow(l,OutDir+"shadow");
818         
819    # Link in global things   
820    DoLink(GlobalDir,OutDir,"markers");
821    DoLink(GlobalDir,OutDir,"mail-forward.cdb");
822    DoLink(GlobalDir,OutDir,"mail-disable");
823    DoLink(GlobalDir,OutDir,"mail-greylist");
824    DoLink(GlobalDir,OutDir,"mail-callout");
825    DoLink(GlobalDir,OutDir,"mail-rbl");
826    DoLink(GlobalDir,OutDir,"mail-rhsbl");
827    DoLink(GlobalDir,OutDir,"mail-whitelist");
828
829    # Compatibility.
830    DoLink(GlobalDir,OutDir,"forward-alias");
831
832    if ExtraList.has_key("[DNS]"):
833       GenDNS(l,OutDir+"dns-zone",Split[1]);
834       GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
835       
836    if ExtraList.has_key("[BSMTP]"):
837       GenBSMTP(l,OutDir+"bsmtp",Split[1])
838
839    if ExtraList.has_key("[PRIVATE]"):
840       DoLink(GlobalDir,OutDir,"debian-private")