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("allowedHost") != 0:
39 for I in DnRecord[1]["allowedHost"]:
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 the account is locked, do not write it.
162 # This is a partial stop-gap. The ssh also needs to change this
163 # to ignore ~/.ssh/authorized* files.
164 if (string.find(GetAttr(x,"userPassword"),"*LK*") != -1):
167 if x[1].has_key("uidNumber") == 0 or \
168 x[1].has_key("sshRSAAuthKey") == 0:
170 for I in x[1]["sshRSAAuthKey"]:
171 User = GetAttr(x,"uid");
172 Line = "%s: %s" %(User,I);
173 Line = Sanitize(Line) + "\n";
175 # Oops, something unspeakable happened.
181 # Generate the group list
182 def GenGroup(l,File):
185 F = open(File + ".tdb.tmp","w");
187 # Generate the GroupMap
189 for x in GroupIDMap.keys():
192 # Fetch all the users
194 if PasswdAttrs == None:
197 # Sort them into a list of groups having a set of users
198 for x in PasswdAttrs:
199 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
201 if x[1].has_key("supplementaryGid") == 0:
204 for I in x[1]["supplementaryGid"]:
205 if GroupMap.has_key(I):
206 GroupMap[I].append(GetAttr(x,"uid"));
208 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
210 # Output the group file.
212 for x in GroupMap.keys():
213 if GroupIDMap.has_key(x) == 0:
215 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
217 for I in GroupMap[x]:
218 Line = Line + ("%s%s" % (Comma,I));
220 Line = Sanitize(Line) + "\n";
221 F.write("0%u %s" % (J,Line));
222 F.write(".%s %s" % (x,Line));
223 F.write("=%u %s" % (GroupIDMap[x],Line));
226 # Oops, something unspeakable happened.
232 # Generate the email forwarding list
233 def GenForward(l,File):
237 OldMask = os.umask(0022);
238 F = open(File + ".tmp","w",0644);
241 # Fetch all the users
243 if PasswdAttrs == None:
246 # Write out the email address for each user
247 for x in PasswdAttrs:
248 if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
251 # Do not allow people to try to buffer overflow busted parsers
252 if len(GetAttr(x,"emailForward")) > 200:
255 # Check the forwarding address
256 if EmailCheck.match(GetAttr(x,"emailForward")) == None:
258 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
259 Line = Sanitize(Line) + "\n";
262 # Oops, something unspeakable happened.
268 def GenAllForward(l,File):
271 OldMask = os.umask(0022);
272 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
275 # Fetch all the users
277 if PasswdAttrs == None:
280 # Write out the email address for each user
281 for x in PasswdAttrs:
282 if x[1].has_key("emailForward") == 0:
285 # Do not allow people to try to buffer overflow busted parsers
286 Forward = GetAttr(x,"emailForward");
287 if len(Forward) > 200:
290 # Check the forwarding address
291 if EmailCheck.match(Forward) == None:
294 User = GetAttr(x,"uid");
295 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
297 # Oops, something unspeakable happened.
301 if Fdb.close() != None:
302 raise "cdbmake gave an error";
304 # Generate the anon XEarth marker file
305 def GenMarkers(l,File):
309 F = open(File + ".tmp","w");
312 # Fetch all the users
314 if PasswdAttrs == None:
317 # Write out the position for each user
318 for x in PasswdAttrs:
319 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
322 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
323 Line = Sanitize(Line) + "\n";
328 # Oops, something unspeakable happened.
334 # Generate the debian-private subscription list
335 def GenPrivate(l,File):
339 F = open(File + ".tmp","w");
342 # Fetch all the users
344 if PasswdAttrs == None:
347 # Write out the position for each user
348 for x in PasswdAttrs:
349 if x[1].has_key("privateSub") == 0:
352 # If the account is locked, do not write it
353 if (string.find(GetAttr(x,"userPassword"),"*LK*") != -1):
356 # If the account has no PGP key, do not write it
357 if x[1].has_key("keyFingerPrint") == 0:
360 # Must be in the Debian group (yuk, hard coded for now)
361 if GetAttr(x,"gidNumber") != "800":
365 Line = "%s"%(GetAttr(x,"privateSub"));
366 Line = Sanitize(Line) + "\n";
371 # Oops, something unspeakable happened.
377 # Generate the DNS Zone file
378 def GenDNS(l,File,HomePrefix):
381 F = open(File + ".tmp","w");
383 # Fetch all the users
385 if PasswdAttrs == None:
388 # Write out the zone file entry for each user
389 for x in PasswdAttrs:
390 if x[1].has_key("dnsZoneEntry") == 0:
393 F.write("; %s\n"%(EmailAddress(x)));
394 for z in x[1]["dnsZoneEntry"]:
395 Split = string.split(string.lower(z));
396 if string.lower(Split[1]) == 'in':
397 for y in range(0,len(Split)):
400 Line = string.join(Split," ") + "\n";
403 Host = Split[0] + DNSZone;
404 if BSMTPCheck.match(Line) != None:
405 F.write("; Has BSMTP\n");
407 # Write some identification information
408 if string.lower(Split[2]) == "a":
409 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
410 for y in x[1]["keyFingerPrint"]:
411 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
414 Line = "; Err %s"%(str(Split));
419 F.write("; Errors\n");
422 # Oops, something unspeakable happened.
428 # Generate the BSMTP file
429 def GenBSMTP(l,File,HomePrefix):
432 F = open(File + ".tmp","w");
434 # Fetch all the users
436 if PasswdAttrs == None:
439 # Write out the zone file entry for each user
440 for x in PasswdAttrs:
441 if x[1].has_key("dnsZoneEntry") == 0:
444 for z in x[1]["dnsZoneEntry"]:
445 Split = string.split(string.lower(z));
446 if string.lower(Split[1]) == 'in':
447 for y in range(0,len(Split)):
450 Line = string.join(Split," ") + "\n";
452 Host = Split[0] + DNSZone;
453 if BSMTPCheck.match(Line) != None:
454 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
455 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
458 F.write("; Errors\n");
461 # Oops, something unspeakable happened.
467 # Generate the shadow list
468 def GenSSHKnown(l,File):
471 OldMask = os.umask(0022);
472 F = open(File + ".tmp","w",0644);
475 # Fetch all the hosts
476 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
477 ["hostname","sshRSAHostKey"]);
483 if x[1].has_key("hostname") == 0 or \
484 x[1].has_key("sshRSAHostKey") == 0:
486 Host = GetAttr(x,"hostname");
487 SHost = string.find(Host,".");
488 for I in x[1]["sshRSAHostKey"]:
490 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
492 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
493 Line = Sanitize(Line) + "\n";
495 # Oops, something unspeakable happened.
502 # Connect to the ldap server
503 l = ldap.open(LDAPServer);
504 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
505 Pass = string.split(string.strip(F.readline())," ");
507 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
509 # Fetch all the groups
511 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
512 ["gid","gidNumber"]);
514 # Generate the GroupMap and GroupIDMap
516 if x[1].has_key("gidNumber") == 0:
518 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
520 # Fetch all the users
521 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
522 ["uid","uidNumber","gidNumber","supplementaryGid",\
523 "gecos","loginShell","userPassword","shadowLastChange",\
524 "shadowMin","shadowMax","shadowWarning","shadowinactive",
525 "shadowexpire","emailForward","latitude","longitude",\
526 "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
527 "keyFingerPrint","privateSub"]);
529 # Open the control file
530 if len(sys.argv) == 1:
531 F = open(GenerateConf,"r");
533 F = open(sys.argv[1],"r")
535 # Generate global things
536 GlobalDir = GenerateDir+"/";
537 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
538 GenAllForward(l,GlobalDir+"mail-forward.cdb");
539 GenMarkers(l,GlobalDir+"markers");
540 GenPrivate(l,GlobalDir+"debian-private");
541 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
544 GenForward(l,GlobalDir+"forward-alias");
550 Line = string.strip(Line);
556 Split = string.split(Line," ");
557 OutDir = GenerateDir + '/' + Split[0] + '/';
558 try: os.mkdir(OutDir);
561 # Get the group list and convert any named groups to numerics
569 if GroupIDMap.has_key(I):
570 GroupList[str(GroupIDMap[I])] = None;
573 CurrentHost = Split[0];
576 GenPasswd(l,OutDir+"passwd",Split[1]);
578 GenGroup(l,OutDir+"group");
579 if CurrentHost == "haydn.debian.org" or CurrentHost == "costa.debian.org":
581 GenShadow(l,OutDir+"shadow");
583 # Link in global things
584 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
585 DoLink(GlobalDir,OutDir,"markers");
586 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
587 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
590 DoLink(GlobalDir,OutDir,"forward-alias");
592 if ExtraList.has_key("[DNS]"):
593 GenDNS(l,OutDir+"dns-zone",Split[1]);
595 if ExtraList.has_key("[BSMTP]"):
596 GenBSMTP(l,OutDir+"bsmtp",Split[1]);
598 if ExtraList.has_key("[PRIVATE]"):
599 DoLink(GlobalDir,OutDir,"debian-private");