Generate ssh-rsa-shadow, debianhosts and ssh_known_hosts even for untrusted 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          continue;
185
186       if x[1].has_key("uidNumber") == 0 or \
187          x[1].has_key("sshRSAAuthKey") == 0:
188          continue;
189       for I in x[1]["sshRSAAuthKey"]:
190          User = GetAttr(x,"uid");
191          Line = "%s: %s" %(User,I);
192          Line = Sanitize(Line) + "\n";
193          F.write(Line);
194   # Oops, something unspeakable happened.
195   except:
196    Die(File,F,None);
197    raise;
198   Done(File,F,None);
199
200 # Generate the group list
201 def GenGroup(l,File):
202   F = None;
203   try:
204    F = open(File + ".tdb.tmp","w");
205
206    # Generate the GroupMap
207    GroupMap = {};
208    for x in GroupIDMap.keys():
209       GroupMap[x] = [];
210       
211    # Fetch all the users
212    global PasswdAttrs;
213    if PasswdAttrs == None:
214       raise "No Users";
215
216    # Sort them into a list of groups having a set of users
217    for x in PasswdAttrs:
218       if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
219          continue;
220       if x[1].has_key("supplementaryGid") == 0:
221          continue;
222          
223       for I in x[1]["supplementaryGid"]:
224          if GroupMap.has_key(I):
225             GroupMap[I].append(GetAttr(x,"uid"));
226          else:
227             print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
228             
229    # Output the group file.
230    J = 0;
231    for x in GroupMap.keys():
232       if GroupIDMap.has_key(x) == 0:
233          continue;
234       Line = "%s:x:%u:" % (x,GroupIDMap[x]);
235       Comma = '';
236       for I in GroupMap[x]:
237         Line = Line + ("%s%s" % (Comma,I));
238         Comma = ',';
239       Line = Sanitize(Line) + "\n";
240       F.write("0%u %s" % (J,Line));
241       F.write(".%s %s" % (x,Line));
242       F.write("=%u %s" % (GroupIDMap[x],Line));
243       J = J + 1;
244       
245   # Oops, something unspeakable happened.
246   except:
247    Die(File,None,F);
248    raise;
249   Done(File,None,F);
250
251 # Generate the email forwarding list
252 def GenForward(l,File):
253   F = None;
254   try:
255    OldMask = os.umask(0022);
256    F = open(File + ".tmp","w",0644);
257    os.umask(OldMask);
258
259    # Fetch all the users
260    global PasswdAttrs;
261    if PasswdAttrs == None:
262       raise "No Users";
263
264    # Write out the email address for each user
265    for x in PasswdAttrs:
266       if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
267          continue;
268       
269       # Do not allow people to try to buffer overflow busted parsers
270       if len(GetAttr(x,"emailForward")) > 200:
271          continue;
272
273       # Check the forwarding address
274       if EmailCheck.match(GetAttr(x,"emailForward")) == None:
275          continue;
276       Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
277       Line = Sanitize(Line) + "\n";
278       F.write(Line);
279       
280   # Oops, something unspeakable happened.
281   except:
282    Die(File,F,None);
283    raise;
284   Done(File,F,None);
285
286 def GenAllForward(l,File):
287   Fdb = None;
288   try:
289    OldMask = os.umask(0022);
290    Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
291    os.umask(OldMask);
292
293    # Fetch all the users
294    global PasswdAttrs;
295    if PasswdAttrs == None:
296       raise "No Users";
297
298    # Write out the email address for each user
299    for x in PasswdAttrs:
300       if x[1].has_key("emailForward") == 0:
301          continue;
302       
303       # Do not allow people to try to buffer overflow busted parsers
304       Forward = GetAttr(x,"emailForward");
305       if len(Forward) > 200:
306          continue;
307
308       # Check the forwarding address
309       if EmailCheck.match(Forward) == None:
310          continue;
311          
312       User = GetAttr(x,"uid");
313       Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
314    Fdb.write("\n");
315   # Oops, something unspeakable happened.
316   except:
317     Fdb.close();
318     raise;
319   if Fdb.close() != None:
320     raise "cdbmake gave an error";
321
322 # Generate the anon XEarth marker file 
323 def GenMarkers(l,File):
324   F = None;
325   try:
326    F = open(File + ".tmp","w");
327
328    # Fetch all the users
329    global PasswdAttrs;
330    if PasswdAttrs == None:
331       raise "No Users";
332
333    # Write out the position for each user
334    for x in PasswdAttrs:
335       if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
336          continue;       
337       try:
338          Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
339          Line = Sanitize(Line) + "\n";
340          F.write(Line);
341       except:
342          pass;
343       
344   # Oops, something unspeakable happened.
345   except:
346    Die(File,F,None);
347    raise;
348   Done(File,F,None);
349
350 # Generate the debian-private subscription list
351 def GenPrivate(l,File):
352   F = None;
353   try:
354    F = open(File + ".tmp","w");
355
356    # Fetch all the users
357    global PasswdAttrs;
358    if PasswdAttrs == None:
359       raise "No Users";
360
361    # Write out the position for each user
362    for x in PasswdAttrs:
363       if x[1].has_key("privateSub") == 0:
364          continue;
365
366       # If the account is locked, do not write it
367       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1) \
368              or (string.find(GetAttr(x,"userPassword"),"*PK*")  != -1):
369          continue;
370
371       # If the account has no PGP key, do not write it
372       if x[1].has_key("keyFingerPrint") == 0:
373          continue;
374
375       # Must be in the Debian group (yuk, hard coded for now)
376       if GetAttr(x,"gidNumber") != "800":
377          continue;
378
379       try:
380          Line = "%s"%(GetAttr(x,"privateSub"));
381          Line = Sanitize(Line) + "\n";
382          F.write(Line);
383       except:
384          pass;
385       
386   # Oops, something unspeakable happened.
387   except:
388    Die(File,F,None);
389    raise;
390   Done(File,F,None);
391
392 # Generate the list of local addresses that refuse all mail
393 def GenMailDisable(l,File):
394   F = None;
395   try:
396    F = open(File + ".tmp","w");
397
398    # Fetch all the users
399    global PasswdAttrs;
400    if PasswdAttrs == None:
401       raise "No Users";
402
403    for x in PasswdAttrs:
404       Reason = None
405       
406       # If the account is locked, disable incoming mail
407       if (string.find(GetAttr(x,"userPassword"),"*LK*")  != -1):
408          Reason = "user account locked"
409       else:
410          if x[1].has_key("mailDisableMessage"):
411             Reason = GetAttr(x,"mailDisableMessage")
412          else:
413             continue
414
415       # Must be in the Debian group (yuk, hard coded for now)
416       if GetAttr(x,"gidNumber") != "800":
417          continue;
418
419       try:
420          Line = "%s: %s"%(GetAttr(x,"uid"),Reason);
421          Line = Sanitize(Line) + "\n";
422          F.write(Line);
423       except:
424          pass;
425       
426   # Oops, something unspeakable happened.
427   except:
428    Die(File,F,None);
429    raise;
430   Done(File,F,None);
431
432 # Generate a list of uids that should have boolean affects applied
433 def GenMailBool(l,File,Key):
434   F = None;
435   try:
436    F = open(File + ".tmp","w");
437
438    # Fetch all the users
439    global PasswdAttrs;
440    if PasswdAttrs == None:
441       raise "No Users";
442
443    for x in PasswdAttrs:
444       Reason = None
445       
446       if x[1].has_key(Key) == 0:
447          continue
448
449       # Must be in the Debian group (yuk, hard coded for now)
450       if GetAttr(x,"gidNumber") != "800":
451          continue
452
453       if GetAttr(x,Key) != "TRUE":
454          continue
455
456       try:
457          Line = "%s"%(GetAttr(x,"uid"));
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 hosts for RBL or whitelist purposes.
470 def GenMailList(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       try:
491          found = 0
492          Line = None
493          for z in x[1][Key]:
494              if Key == "mailWhitelist":
495                  if re.match('^[-\w.]+(/[\d]+)?$',z) == None:
496                      continue
497              else:
498                  if re.match('^[-\w.]+$',z) == None:
499                      continue
500              if found == 0:
501                  found = 1
502                  Line = GetAttr(x,"uid")
503              else:
504                  Line += " "
505              Line += ": " + z
506              if Key == "mailRHSBL":
507                  Line += "/$sender_address_domain"
508
509          if Line != None:
510              Line = Sanitize(Line) + "\n";
511              F.write(Line);
512       except:
513          pass;
514       
515   # Oops, something unspeakable happened.
516   except:
517    Die(File,F,None);
518    raise;
519   Done(File,F,None);
520
521 # Generate the DNS Zone file
522 def GenDNS(l,File,HomePrefix):
523   F = None;
524   try:
525    F = open(File + ".tmp","w");
526    
527    # Fetch all the users
528    global PasswdAttrs;
529    if PasswdAttrs == None:
530       raise "No Users";
531
532    # Write out the zone file entry for each user
533    for x in PasswdAttrs:
534       if x[1].has_key("dnsZoneEntry") == 0:
535          continue;
536
537       # If the account has no PGP key, do not write it
538       if x[1].has_key("keyFingerPrint") == 0:
539          continue;
540       try:
541          F.write("; %s\n"%(EmailAddress(x)));
542          for z in x[1]["dnsZoneEntry"]:
543             Split = string.split(string.lower(z));
544             if string.lower(Split[1]) == 'in':
545                for y in range(0,len(Split)):
546                   if Split[y] == "$":
547                      Split[y] = "\n\t";
548                Line = string.join(Split," ") + "\n";
549                F.write(Line);
550                
551                Host = Split[0] + DNSZone;
552                if BSMTPCheck.match(Line) != None:
553                    F.write("; Has BSMTP\n");
554                                
555                # Write some identification information
556                if string.lower(Split[2]) == "a":
557                   Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
558                   for y in x[1]["keyFingerPrint"]:
559                      Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
560                   F.write(Line);
561             else:
562                Line = "; Err %s"%(str(Split));
563                F.write(Line);
564
565          F.write("\n");
566       except:
567          F.write("; Errors\n");
568          pass;
569       
570   # Oops, something unspeakable happened.
571   except:
572    Die(File,F,None);
573    raise;
574   Done(File,F,None);
575
576 # Generate the DNS SSHFP records
577 def GenSSHFP(l,File,HomePrefix):
578   F = None
579   try:
580    F = open(File + ".tmp","w")
581    
582    # Fetch all the hosts
583    global HostAttrs
584    if HostAttrs == None:
585       raise "No Hosts"
586
587    for x in HostAttrs:
588       if x[1].has_key("hostname") == 0 or \
589          x[1].has_key("sshRSAHostKey") == 0:
590          continue
591       Host = GetAttr(x,"hostname");
592       Algorithm = None
593       for I in x[1]["sshRSAHostKey"]:
594          Split = string.split(I)
595          if Split[0] == 'ssh-rsa':
596             Algorithm = 1
597          if Split[0] == 'ssh-dss':
598             Algorithm = 2
599          if Algorithm == None:
600             continue
601          Fingerprint = sha.new(base64.decodestring(Split[1])).hexdigest()
602          Line = "%s. IN SSHFP %u 1 %s" % (Host,Algorithm,Fingerprint)
603          Line = Sanitize(Line) + "\n"
604          F.write(Line)
605   # Oops, something unspeakable happened.
606   except:
607    Die(File,F,None)
608    raise;
609   Done(File,F,None)
610
611 # Generate the BSMTP file
612 def GenBSMTP(l,File,HomePrefix):
613   F = None;
614   try:
615    F = open(File + ".tmp","w");
616    
617    # Fetch all the users
618    global PasswdAttrs;
619    if PasswdAttrs == None:
620       raise "No Users";
621
622    # Write out the zone file entry for each user
623    for x in PasswdAttrs:
624       if x[1].has_key("dnsZoneEntry") == 0:
625          continue;
626
627       # If the account has no PGP key, do not write it
628       if x[1].has_key("keyFingerPrint") == 0:
629          continue;
630       try:
631          for z in x[1]["dnsZoneEntry"]:
632             Split = string.split(string.lower(z));
633             if string.lower(Split[1]) == 'in':
634                for y in range(0,len(Split)):
635                   if Split[y] == "$":
636                      Split[y] = "\n\t";
637                Line = string.join(Split," ") + "\n";
638                
639                Host = Split[0] + DNSZone;
640                if BSMTPCheck.match(Line) != None:
641                    F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
642                                GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
643                                
644       except:
645          F.write("; Errors\n");
646          pass;
647       
648   # Oops, something unspeakable happened.
649   except:
650    Die(File,F,None);
651    raise;
652   Done(File,F,None);
653
654 # Generate the ssh known hosts file
655 def GenSSHKnown(l,File):
656   F = None;
657   try:
658    OldMask = os.umask(0022);
659    F = open(File + ".tmp","w",0644);
660    os.umask(OldMask);
661
662    global HostAttrs
663    if HostAttrs == None:
664       raise "No Hosts";
665    
666    for x in HostAttrs:
667       if x[1].has_key("hostname") == 0 or \
668          x[1].has_key("sshRSAHostKey") == 0:
669          continue;
670       Host = GetAttr(x,"hostname");
671       SHost = string.find(Host,".");
672       for I in x[1]["sshRSAHostKey"]:
673          if SHost == None:
674             Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
675          else:
676             Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
677          Line = Sanitize(Line) + "\n";
678          F.write(Line);
679   # Oops, something unspeakable happened.
680   except:
681    Die(File,F,None);
682    raise;
683   Done(File,F,None);
684
685 # Generate the debianhosts file (list of all IP addresses)
686 def GenHosts(l,File):
687   F = None;
688   try:
689    OldMask = os.umask(0022);
690    F = open(File + ".tmp","w",0644);
691    os.umask(OldMask);
692
693    # Fetch all the hosts
694    HostNames = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"hostname=*",\
695                 ["hostname"]);
696    
697    if HostNames == None:
698       raise "No Hosts";
699
700    for x in HostNames:
701       if x[1].has_key("hostname") == 0:
702          continue;
703       Host = GetAttr(x,"hostname");
704       try:
705         Addr = socket.gethostbyname(Host);
706         F.write(Addr + "\n");
707       except:
708         pass
709   # Oops, something unspeakable happened.
710   except:
711    Die(File,F,None);
712    raise;
713   Done(File,F,None);
714
715 # Connect to the ldap server
716 l = ldap.open(LDAPServer);
717 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
718 Pass = string.split(string.strip(F.readline())," ");
719 F.close();
720 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
721
722 # Fetch all the groups
723 GroupIDMap = {};
724 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
725                   ["gid","gidNumber"]);
726
727 # Generate the GroupMap and GroupIDMap
728 for x in Attrs:
729    if x[1].has_key("gidNumber") == 0:
730       continue;
731    GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
732
733 # Fetch all the users
734 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
735                 ["uid","uidNumber","gidNumber","supplementaryGid",\
736                  "gecos","loginShell","userPassword","shadowLastChange",\
737                  "shadowMin","shadowMax","shadowWarning","shadowinactive",
738                  "shadowexpire","emailForward","latitude","longitude",\
739                  "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
740                  "keyFingerPrint","privateSub","mailDisableMessage",\
741                  "mailGreylisting","mailCallout","mailRBL","mailRHSBL",\
742                  "mailWhitelist"]);
743 # Fetch all the hosts
744 HostAttrs    = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
745                 ["hostname","sshRSAHostKey"]);
746
747 # Open the control file
748 if len(sys.argv) == 1:
749    F = open(GenerateConf,"r");
750 else:
751    F = open(sys.argv[1],"r")
752
753 # Generate global things
754 GlobalDir = GenerateDir+"/";
755 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
756 GenAllForward(l,GlobalDir+"mail-forward.cdb");
757 GenMarkers(l,GlobalDir+"markers");
758 GenPrivate(l,GlobalDir+"debian-private");
759 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
760 GenHosts(l,GlobalDir+"debianhosts");
761 GenMailDisable(l,GlobalDir+"mail-disable");
762 GenMailBool(l,GlobalDir+"mail-greylist","mailGreylisting");
763 GenMailBool(l,GlobalDir+"mail-callout","mailCallout");
764 GenMailList(l,GlobalDir+"mail-rbl","mailRBL");
765 GenMailList(l,GlobalDir+"mail-rhsbl","mailRHSBL");
766 GenMailList(l,GlobalDir+"mail-whitelist","mailWhitelist");
767
768 # Compatibility.
769 GenForward(l,GlobalDir+"forward-alias");
770    
771 while(1):
772    Line = F.readline();
773    if Line == "":
774       break;
775    Line = string.strip(Line);
776    if Line == "":
777       continue;
778    if Line[0] == '#':
779       continue;
780
781    Split = string.split(Line," ");
782    OutDir = GenerateDir + '/' + Split[0] + '/';
783    try: os.mkdir(OutDir);
784    except: pass;
785
786    # Get the group list and convert any named groups to numerics
787    GroupList = {};
788    ExtraList = {};
789    for I in Split[2:]:
790       if I[0] == '[':
791          ExtraList[I] = None;
792          continue;
793       GroupList[I] = None;
794       if GroupIDMap.has_key(I):
795          GroupList[str(GroupIDMap[I])] = None;
796
797    Allowed = GroupList;
798    if Allowed == {}:
799      Allowed = None
800    CurrentHost = Split[0];
801
802    DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
803    DoLink(GlobalDir,OutDir,"debianhosts");
804    DoLink(GlobalDir,OutDir,"ssh_known_hosts");
805
806    sys.stdout.flush();
807    GenPasswd(l,OutDir+"passwd",Split[1]);
808    sys.stdout.flush();
809    GenGroup(l,OutDir+"group");
810    if ExtraList.has_key("[UNTRUSTED]"):
811         continue;
812    GenShadow(l,OutDir+"shadow");
813         
814    # Link in global things   
815    DoLink(GlobalDir,OutDir,"markers");
816    DoLink(GlobalDir,OutDir,"mail-forward.cdb");
817    DoLink(GlobalDir,OutDir,"mail-disable");
818    DoLink(GlobalDir,OutDir,"mail-greylist");
819    DoLink(GlobalDir,OutDir,"mail-callout");
820    DoLink(GlobalDir,OutDir,"mail-rbl");
821    DoLink(GlobalDir,OutDir,"mail-rhsbl");
822    DoLink(GlobalDir,OutDir,"mail-whitelist");
823
824    # Compatibility.
825    DoLink(GlobalDir,OutDir,"forward-alias");
826
827    if ExtraList.has_key("[DNS]"):
828       GenDNS(l,OutDir+"dns-zone",Split[1]);
829       GenSSHFP(l,OutDir+"dns-sshfp",Split[1])
830       
831    if ExtraList.has_key("[BSMTP]"):
832       GenBSMTP(l,OutDir+"bsmtp",Split[1])
833
834    if ExtraList.has_key("[PRIVATE]"):
835       DoLink(GlobalDir,OutDir,"debian-private")