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 x[1].has_key("uidNumber") == 0 or \
162 x[1].has_key("sshRSAAuthKey") == 0:
165 if x[1].has_key("uidNumber") == 0 or \
166 x[1].has_key("sshRSAAuthKey") == 0:
168 for I in x[1]["sshRSAAuthKey"]:
169 User = GetAttr(x,"uid");
170 Line = "%s: %s" %(User,I);
171 Line = Sanitize(Line) + "\n";
173 # Oops, something unspeakable happened.
179 # Generate the group list
180 def GenGroup(l,File):
183 F = open(File + ".tdb.tmp","w");
185 # Generate the GroupMap
187 for x in GroupIDMap.keys():
190 # Fetch all the users
192 if PasswdAttrs == None:
195 # Sort them into a list of groups having a set of users
196 for x in PasswdAttrs:
197 if x[1].has_key("uidNumber") == 0 or IsInGroup(x) == 0:
199 if x[1].has_key("supplementaryGid") == 0:
202 for I in x[1]["supplementaryGid"]:
203 if GroupMap.has_key(I):
204 GroupMap[I].append(GetAttr(x,"uid"));
206 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
208 # Output the group file.
210 for x in GroupMap.keys():
211 if GroupIDMap.has_key(x) == 0:
213 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
215 for I in GroupMap[x]:
216 Line = Line + ("%s%s" % (Comma,I));
218 Line = Sanitize(Line) + "\n";
219 F.write("0%u %s" % (J,Line));
220 F.write(".%s %s" % (x,Line));
221 F.write("=%u %s" % (GroupIDMap[x],Line));
224 # Oops, something unspeakable happened.
230 # Generate the email forwarding list
231 def GenForward(l,File):
235 OldMask = os.umask(0022);
236 F = open(File + ".tmp","w",0644);
239 # Fetch all the users
241 if PasswdAttrs == None:
244 # Write out the email address for each user
245 for x in PasswdAttrs:
246 if x[1].has_key("emailForward") == 0 or IsInGroup(x) == 0:
249 # Do not allow people to try to buffer overflow busted parsers
250 if len(GetAttr(x,"emailForward")) > 200:
253 # Check the forwarding address
254 if EmailCheck.match(GetAttr(x,"emailForward")) == None:
256 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailForward"));
257 Line = Sanitize(Line) + "\n";
260 # Oops, something unspeakable happened.
266 def GenAllForward(l,File):
269 OldMask = os.umask(0022);
270 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
273 # Fetch all the users
275 if PasswdAttrs == None:
278 # Write out the email address for each user
279 for x in PasswdAttrs:
280 if x[1].has_key("emailForward") == 0:
283 # Do not allow people to try to buffer overflow busted parsers
284 Forward = GetAttr(x,"emailForward");
285 if len(Forward) > 200:
288 # Check the forwarding address
289 if EmailCheck.match(Forward) == None:
292 User = GetAttr(x,"uid");
293 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
295 # Oops, something unspeakable happened.
299 if Fdb.close() != None:
300 raise "cdbmake gave an error";
302 # Generate the anon XEarth marker file
303 def GenMarkers(l,File):
307 F = open(File + ".tmp","w");
310 # Fetch all the users
312 if PasswdAttrs == None:
315 # Write out the position for each user
316 for x in PasswdAttrs:
317 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
320 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
321 Line = Sanitize(Line) + "\n";
326 # Oops, something unspeakable happened.
332 # Generate the debian-private subscription list
333 def GenPrivate(l,File):
337 F = open(File + ".tmp","w");
340 # Fetch all the users
342 if PasswdAttrs == None:
345 # Write out the position for each user
346 for x in PasswdAttrs:
347 if x[1].has_key("privateSub") == 0:
350 # If the account is locked, do not write it
351 if (string.find(GetAttr(x,"userPassword"),"*LK*") != -1):
354 # If the account has no PGP key, do not write it
355 if x[1].has_key("keyFingerPrint") == 0:
358 # Must be in the Debian group (yuk, hard coded for now)
359 if GetAttr(x,"gidNumber") != "800":
363 Line = "%s"%(GetAttr(x,"privateSub"));
364 Line = Sanitize(Line) + "\n";
369 # Oops, something unspeakable happened.
375 # Generate the DNS Zone file
376 def GenDNS(l,File,HomePrefix):
379 F = open(File + ".tmp","w");
381 # Fetch all the users
383 if PasswdAttrs == None:
386 # Write out the zone file entry for each user
387 for x in PasswdAttrs:
388 if x[1].has_key("dnsZoneEntry") == 0:
391 F.write("; %s\n"%(EmailAddress(x)));
392 for z in x[1]["dnsZoneEntry"]:
393 Split = string.split(string.lower(z));
394 if string.lower(Split[1]) == 'in':
395 for y in range(0,len(Split)):
398 Line = string.join(Split," ") + "\n";
401 Host = Split[0] + DNSZone;
402 if BSMTPCheck.match(Line) != None:
403 F.write("; Has BSMTP\n");
405 # Write some identification information
406 if string.lower(Split[2]) == "a":
407 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
408 for y in x[1]["keyFingerPrint"]:
409 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
412 Line = "; Err %s"%(str(Split));
417 F.write("; Errors\n");
420 # Oops, something unspeakable happened.
426 # Generate the BSMTP file
427 def GenBSMTP(l,File,HomePrefix):
430 F = open(File + ".tmp","w");
432 # Fetch all the users
434 if PasswdAttrs == None:
437 # Write out the zone file entry for each user
438 for x in PasswdAttrs:
439 if x[1].has_key("dnsZoneEntry") == 0:
442 for z in x[1]["dnsZoneEntry"]:
443 Split = string.split(string.lower(z));
444 if string.lower(Split[1]) == 'in':
445 for y in range(0,len(Split)):
448 Line = string.join(Split," ") + "\n";
450 Host = Split[0] + DNSZone;
451 if BSMTPCheck.match(Line) != None:
452 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
453 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
456 F.write("; Errors\n");
459 # Oops, something unspeakable happened.
465 # Generate the shadow list
466 def GenSSHKnown(l,File):
469 OldMask = os.umask(0022);
470 F = open(File + ".tmp","w",0644);
473 # Fetch all the hosts
474 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshRSAHostKey=*",\
475 ["hostname","sshRSAHostKey"]);
481 if x[1].has_key("hostname") == 0 or \
482 x[1].has_key("sshRSAHostKey") == 0:
484 Host = GetAttr(x,"hostname");
485 SHost = string.find(Host,".");
486 for I in x[1]["sshRSAHostKey"]:
488 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
490 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
491 Line = Sanitize(Line) + "\n";
493 # Oops, something unspeakable happened.
500 # Connect to the ldap server
501 l = ldap.open(LDAPServer);
502 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
503 Pass = string.split(string.strip(F.readline())," ");
505 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
507 # Fetch all the groups
509 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
510 ["gid","gidNumber"]);
512 # Generate the GroupMap and GroupIDMap
514 if x[1].has_key("gidNumber") == 0:
516 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidNumber"][0]);
518 # Fetch all the users
519 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
520 ["uid","uidNumber","gidNumber","supplementaryGid",\
521 "gecos","loginShell","userPassword","shadowLastChange",\
522 "shadowMin","shadowMax","shadowWarning","shadowinactive",
523 "shadowexpire","emailForward","latitude","longitude",\
524 "allowedHost","sshRSAAuthKey","dnsZoneEntry","cn","sn",\
525 "keyFingerPrint","privateSub"]);
527 # Open the control file
528 if len(sys.argv) == 1:
529 F = open(GenerateConf,"r");
531 F = open(sys.argv[1],"r")
533 # Generate global things
534 GlobalDir = GenerateDir+"/";
535 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
536 GenAllForward(l,GlobalDir+"mail-forward.cdb");
537 GenMarkers(l,GlobalDir+"markers");
538 GenPrivate(l,GlobalDir+"debian-private");
539 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
542 GenForward(l,GlobalDir+"forward-alias");
548 Line = string.strip(Line);
554 Split = string.split(Line," ");
555 OutDir = GenerateDir + '/' + Split[0] + '/';
556 try: os.mkdir(OutDir);
559 # Get the group list and convert any named groups to numerics
567 if GroupIDMap.has_key(I):
568 GroupList[str(GroupIDMap[I])] = None;
571 CurrentHost = Split[0];
574 GenPasswd(l,OutDir+"passwd",Split[1]);
576 GenGroup(l,OutDir+"group");
577 GenShadow(l,OutDir+"shadow");
579 # Link in global things
580 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
581 DoLink(GlobalDir,OutDir,"markers");
582 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
583 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
586 DoLink(GlobalDir,OutDir,"forward-alias");
588 if ExtraList.has_key("[DNS]"):
589 GenDNS(l,OutDir+"dns-zone",Split[1]);
591 if ExtraList.has_key("[BSMTP]"):
592 GenBSMTP(l,OutDir+"bsmtp",Split[1]);
594 if ExtraList.has_key("[PRIVATE]"):
595 DoLink(GlobalDir,OutDir,"debian-private");