3 # Generates passwd, shadow and group files from the ldap directory.
5 import string, re, time, ldap, getopt, sys, os, pwd, posix, socket;
6 from userdir_ldap import *;
16 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
17 BSMTPCheck = re.compile(".*mx 0 (klecker|gluck)\.debian\.org\..*",re.DOTALL);
18 DNSZone = ".debian.net"
21 return string.translate(Str,string.maketrans("\n\r\t","$$$"));
23 def DoLink(From,To,File):
24 try: posix.remove(To+File);
26 posix.link(From+File,To+File);
28 # See if this user is in the group list
29 def IsInGroup(DnRecord):
33 # See if the primary group is in the list
34 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
37 # Check the host based ACL
38 if DnRecord[1].has_key("allowedhosts") != 0:
39 for I in DnRecord[1]["allowedhosts"]:
43 # See if there are supplementary groups
44 if DnRecord[1].has_key("supplementarygid") == 0:
47 # Check the supplementary groups
48 for I in DnRecord[1]["supplementarygid"]:
49 if Allowed.has_key(I):
58 try: os.remove(File + ".tmp");
60 try: os.remove(File + ".tdb.tmp");
66 os.rename(File + ".tmp",File);
69 os.rename(File + ".tdb.tmp",File+".tdb");
71 # Generate the password list
72 def GenPasswd(l,File,HomePrefix):
75 F = open(File + ".tdb.tmp","w");
79 if PasswdAttrs == None:
84 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
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:
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"));
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));
102 # Oops, something unspeakable happened.
108 # Generate the shadow list
109 def GenShadow(l,File):
112 OldMask = os.umask(0077);
113 F = open(File + ".tdb.tmp","w",0600);
116 # Fetch all the users
118 if PasswdAttrs == None:
122 for x in PasswdAttrs:
123 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
126 Pass = GetAttr(x,"userpassword");
127 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
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));
141 # Oops, something unspeakable happened.
147 # Generate the shadow list
148 def GenSSHShadow(l,File):
151 OldMask = os.umask(0077);
152 F = open(File + ".tmp","w",0600);
155 # Fetch all the users
157 if PasswdAttrs == None:
160 for x in PasswdAttrs:
161 if x[1].has_key("uidnumber") == 0 or \
162 x[1].has_key("sshrsaauthkey") == 0:
164 for I in x[1]["sshrsaauthkey"]:
165 User = GetAttr(x,"uid");
166 Line = "%s: %s" %(User,I);
167 Line = Sanitize(Line) + "\n";
169 # Oops, something unspeakable happened.
175 # Generate the group list
176 def GenGroup(l,File):
179 F = open(File + ".tdb.tmp","w");
181 # Generate the GroupMap
183 for x in GroupIDMap.keys():
186 # Fetch all the users
188 if PasswdAttrs == None:
191 # Sort them into a list of groups having a set of users
192 for x in PasswdAttrs:
193 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
195 if x[1].has_key("supplementarygid") == 0:
198 for I in x[1]["supplementarygid"]:
199 if GroupMap.has_key(I):
200 GroupMap[I].append(GetAttr(x,"uid"));
202 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
204 # Output the group file.
206 for x in GroupMap.keys():
207 if GroupIDMap.has_key(x) == 0:
209 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
211 for I in GroupMap[x]:
212 Line = Line + ("%s%s" % (Comma,I));
214 Line = Sanitize(Line) + "\n";
215 F.write("0%u %s" % (J,Line));
216 F.write(".%s %s" % (x,Line));
217 F.write("=%u %s" % (GroupIDMap[x],Line));
220 # Oops, something unspeakable happened.
226 # Generate the email forwarding list
227 def GenForward(l,File):
231 OldMask = os.umask(0022);
232 F = open(File + ".tmp","w",0644);
235 # Fetch all the users
237 if PasswdAttrs == None:
240 # Write out the email address for each user
241 for x in PasswdAttrs:
242 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
245 # Do not allow people to try to buffer overflow busted parsers
246 if len(GetAttr(x,"emailforward")) > 200:
249 # Check the forwarding address
250 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
252 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
253 Line = Sanitize(Line) + "\n";
256 # Oops, something unspeakable happened.
262 def GenAllForward(l,File):
265 OldMask = os.umask(0022);
266 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
269 # Fetch all the users
271 if PasswdAttrs == None:
274 # Write out the email address for each user
275 for x in PasswdAttrs:
276 if x[1].has_key("emailforward") == 0:
279 # Do not allow people to try to buffer overflow busted parsers
280 Forward = GetAttr(x,"emailforward");
281 if len(Forward) > 200:
284 # Check the forwarding address
285 if EmailCheck.match(Forward) == None:
288 User = GetAttr(x,"uid");
289 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
291 # Oops, something unspeakable happened.
295 if Fdb.close() != None:
296 raise "cdbmake gave an error";
298 # Generate the anon XEarth marker file
299 def GenMarkers(l,File):
303 F = open(File + ".tmp","w");
306 # Fetch all the users
308 if PasswdAttrs == None:
311 # Write out the position for each user
312 for x in PasswdAttrs:
313 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
316 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
317 Line = Sanitize(Line) + "\n";
322 # Oops, something unspeakable happened.
328 # Generate the debian-private subscription list
329 def GenPrivate(l,File):
333 F = open(File + ".tmp","w");
336 # Fetch all the users
338 if PasswdAttrs == None:
341 # Write out the position for each user
342 for x in PasswdAttrs:
343 if x[1].has_key("privatesub") == 0:
346 # If the account is locked, do not write it
347 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
350 # If the account has no PGP key, do not write it
351 if x[1].has_key("keyfingerprint") == 0:
354 # Must be in the Debian group (yuk, hard coded for now)
355 if GetAttr(x,"gidnumber") != "800":
359 Line = "%s"%(GetAttr(x,"privatesub"));
360 Line = Sanitize(Line) + "\n";
365 # Oops, something unspeakable happened.
371 # Generate the DNS Zone file
372 def GenDNS(l,File,HomePrefix):
375 F = open(File + ".tmp","w");
377 # Fetch all the users
379 if PasswdAttrs == None:
382 # Write out the zone file entry for each user
383 for x in PasswdAttrs:
384 if x[1].has_key("dnszoneentry") == 0:
387 F.write("; %s\n"%(EmailAddress(x)));
388 for z in x[1]["dnszoneentry"]:
389 Split = string.split(string.lower(z));
390 if string.lower(Split[1]) == 'in':
391 for y in range(0,len(Split)):
394 Line = string.join(Split," ") + "\n";
397 Host = Split[0] + DNSZone;
398 if BSMTPCheck.match(Line) != None:
399 F.write("; Has BSMTP\n");
401 # Write some identification information
402 if string.lower(Split[2]) == "a":
403 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
404 for y in x[1]["keyfingerprint"]:
405 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
408 Line = "; Err %s"%(str(Split));
413 F.write("; Errors\n");
416 # Oops, something unspeakable happened.
422 # Generate the BSMTP file
423 def GenBSMTP(l,File,HomePrefix):
426 F = open(File + ".tmp","w");
428 # Fetch all the users
430 if PasswdAttrs == None:
433 # Write out the zone file entry for each user
434 for x in PasswdAttrs:
435 if x[1].has_key("dnszoneentry") == 0:
438 for z in x[1]["dnszoneentry"]:
439 Split = string.split(string.lower(z));
440 if string.lower(Split[1]) == 'in':
441 for y in range(0,len(Split)):
444 Line = string.join(Split," ") + "\n";
446 Host = Split[0] + DNSZone;
447 if BSMTPCheck.match(Line) != None:
448 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
449 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
452 F.write("; Errors\n");
455 # Oops, something unspeakable happened.
461 # Generate the shadow list
462 def GenSSHKnown(l,File):
465 OldMask = os.umask(0022);
466 F = open(File + ".tmp","w",0644);
469 # Fetch all the hosts
470 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
471 ["hostname","sshrsahostkey"]);
477 if x[1].has_key("hostname") == 0 or \
478 x[1].has_key("sshrsahostkey") == 0:
480 Host = GetAttr(x,"hostname");
481 SHost = string.find(Host,".");
482 for I in x[1]["sshrsahostkey"]:
484 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
486 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
487 Line = Sanitize(Line) + "\n";
489 # Oops, something unspeakable happened.
496 # Connect to the ldap server
497 l = ldap.open(LDAPServer);
498 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
499 Pass = string.split(string.strip(F.readline())," ");
501 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
503 # Fetch all the groups
505 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
506 ["gid","gidnumber"]);
508 # Generate the GroupMap and GroupIDMap
510 if x[1].has_key("gidnumber") == 0:
512 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
514 # Fetch all the users
515 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
516 ["uid","uidnumber","gidnumber","supplementarygid",\
517 "gecos","loginshell","userpassword","shadowlastchange",\
518 "shadowmin","shadowmax","shadowwarning","shadowinactive",
519 "shadowexpire","emailforward","latitude","longitude",\
520 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
521 "keyfingerprint","privatesub"]);
523 # Open the control file
524 if len(sys.argv) == 1:
525 F = open(GenerateConf,"r");
527 F = open(sys.argv[1],"r")
529 # Generate global things
530 GlobalDir = GenerateDir+"/";
531 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
532 GenAllForward(l,GlobalDir+"mail-forward.cdb");
533 GenMarkers(l,GlobalDir+"markers");
534 GenPrivate(l,GlobalDir+"debian-private");
535 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
538 GenForward(l,GlobalDir+"forward-alias");
544 Line = string.strip(Line);
550 Split = string.split(Line," ");
551 OutDir = GenerateDir + '/' + Split[0] + '/';
552 try: os.mkdir(OutDir);
555 # Get the group list and convert any named groups to numerics
563 if GroupIDMap.has_key(I):
564 GroupList[str(GroupIDMap[I])] = None;
567 CurrentHost = Split[0];
570 GenPasswd(l,OutDir+"passwd",Split[1]);
572 GenGroup(l,OutDir+"group");
573 GenShadow(l,OutDir+"shadow");
575 # Link in global things
576 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
577 DoLink(GlobalDir,OutDir,"markers");
578 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
579 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
582 DoLink(GlobalDir,OutDir,"forward-alias");
584 if ExtraList.has_key("[DNS]"):
585 GenDNS(l,OutDir+"dns-zone",Split[1]);
587 if ExtraList.has_key("[BSMTP]"):
588 GenBSMTP(l,OutDir+"bsmtp",Split[1]);
590 if ExtraList.has_key("[PRIVATE]"):
591 DoLink(GlobalDir,OutDir,"debian-private");