3 # Generates passwd, shadow and group files from the ldap directory.
5 import string, re, time, ldap, getopt, sys, os, pwd, posix;
6 from userdir_ldap import *;
13 EmailCheck = re.compile("^([^ <>@]+@[^ ,<>@]+)?$");
14 BSMTPCheck = re.compile(".*mx 0 klecker\.debian\.org\..*",re.DOTALL);
15 DNSZone = ".debian.net"
18 return string.translate(Str,string.maketrans("\n\r\t","$$$"));
20 def DoLink(From,To,File):
21 try: posix.remove(To+File);
23 posix.link(From+File,To+File);
25 # See if this user is in the group list
26 def IsInGroup(DnRecord):
27 global Allowed,CurrentHost;
31 # See if the primary group is in the list
32 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
35 # Check the host based ACL
36 if DnRecord[1].has_key("allowedhosts") != 0:
37 for I in DnRecord[1]["allowedhosts"]:
41 # See if there are supplementary groups
42 if DnRecord[1].has_key("supplementarygid") == 0:
45 # Check the supplementary groups
46 for I in DnRecord[1]["supplementarygid"]:
47 if Allowed.has_key(I):
56 try: os.remove(File + ".tmp");
58 try: os.remove(File + ".tdb.tmp");
64 os.rename(File + ".tmp",File);
67 os.rename(File + ".tdb.tmp",File+".tdb");
69 # Generate the password list
70 def GenPasswd(l,File,HomePrefix):
74 F = open(File + ".tmp","w");
75 Fdb = 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\n" % (GetAttr(x,"uid"),\
92 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
93 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
94 GetAttr(x,"loginshell"));
96 Fdb.write("0%u %s" % (I,Line));
97 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
98 Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
101 # Oops, something unspeakable happened.
107 # Generate the shadow list
108 def GenShadow(l,File):
112 OldMask = os.umask(0077);
113 F = open(File + ".tmp","w",0600);
114 Fdb = open(File + ".tdb.tmp","w",0600);
117 # Fetch all the users
119 if PasswdAttrs == None:
123 for x in PasswdAttrs:
124 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
127 Pass = GetAttr(x,"userpassword");
128 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
132 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
133 Pass,GetAttr(x,"shadowlastchange"),\
134 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
135 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
136 GetAttr(x,"shadowexpire"));
137 Line = Sanitize(Line) + "\n";
139 Fdb.write("0%u %s" % (I,Line));
140 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
143 # Oops, something unspeakable happened.
149 # Generate the shadow list
150 def GenSSHShadow(l,File):
154 OldMask = os.umask(0077);
155 F = open(File + ".tmp","w",0600);
156 Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
159 # Fetch all the users
161 if PasswdAttrs == None:
165 for x in PasswdAttrs:
166 if x[1].has_key("uidnumber") == 0 or \
167 x[1].has_key("sshrsaauthkey") == 0:
169 for I in x[1]["sshrsaauthkey"]:
170 User = GetAttr(x,"uid");
171 Line = "%s: %s" %(User,I);
172 Line = Sanitize(Line) + "\n";
174 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(I),User,I));
176 # Oops, something unspeakable happened.
180 if Fdb.close() != None:
181 raise "cdbmake gave an error";
184 def GenSSH2Shadow(l,File):
188 OldMask = os.umask(0077);
189 F = open(File + ".tmp","w",0600);
190 Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
193 # Fetch all the users
195 if PasswdAttrs == None:
199 for x in PasswdAttrs:
200 if x[1].has_key("uidnumber") == 0 or \
201 x[1].has_key("sshdsaauthkey") == 0:
203 for I in x[1]["sshdsaauthkey"]:
204 User = GetAttr(x,"uid");
205 Line = "%s: %s" %(User,I);
206 Line = Sanitize(Line) + "\n";
208 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(I),User,I));
210 # Oops, something unspeakable happened.
214 if Fdb.close() != None:
215 raise "cdbmake gave an error";
218 # Generate the group list
219 def GenGroup(l,File):
223 F = open(File + ".tmp","w");
224 Fdb = open(File + ".tdb.tmp","w");
226 # Generate the GroupMap
228 for x in GroupIDMap.keys():
231 # Fetch all the users
233 if PasswdAttrs == None:
236 # Sort them into a list of groups having a set of users
237 for x in PasswdAttrs:
238 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
240 if x[1].has_key("supplementarygid") == 0:
243 for I in x[1]["supplementarygid"]:
244 if GroupMap.has_key(I):
245 GroupMap[I].append(GetAttr(x,"uid"));
247 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
249 # Output the group file.
251 for x in GroupMap.keys():
252 if GroupIDMap.has_key(x) == 0:
254 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
256 for I in GroupMap[x]:
257 Line = Line + ("%s%s" % (Comma,I));
259 Line = Sanitize(Line) + "\n";
261 Fdb.write("0%u %s" % (Counter,Line));
262 Fdb.write(".%s %s" % (x,Line));
263 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
264 Counter = Counter + 1;
266 # Oops, something unspeakable happened.
272 # Generate the email forwarding list
273 def GenForward(l,File):
277 OldMask = os.umask(0022);
278 F = open(File + ".tmp","w",0644);
281 # Fetch all the users
283 if PasswdAttrs == None:
286 # Write out the email address for each user
287 for x in PasswdAttrs:
288 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
291 # Do not allow people to try to buffer overflow busted parsers
292 if len(GetAttr(x,"emailforward")) > 200:
295 # Check the forwarding address
296 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
298 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
299 Line = Sanitize(Line) + "\n";
302 # Oops, something unspeakable happened.
308 def GenAllForward(l,File):
311 OldMask = os.umask(0022);
312 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
315 # Fetch all the users
317 if PasswdAttrs == None:
320 # Write out the email address for each user
321 for x in PasswdAttrs:
322 if x[1].has_key("emailforward") == 0:
325 # Do not allow people to try to buffer overflow busted parsers
326 Forward = GetAttr(x,"emailforward");
327 if len(Forward) > 200:
330 # Check the forwarding address
331 if EmailCheck.match(Forward) == None:
334 User = GetAttr(x,"uid");
335 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
337 # Oops, something unspeakable happened.
341 if Fdb.close() != None:
342 raise "cdbmake gave an error";
344 # Generate the anon XEarth marker file
345 def GenMarkers(l,File):
349 F = open(File + ".tmp","w");
352 # Fetch all the users
354 if PasswdAttrs == None:
357 # Write out the position for each user
358 for x in PasswdAttrs:
359 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
362 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
363 Line = Sanitize(Line) + "\n";
368 # Oops, something unspeakable happened.
374 # Generate the debian-private subscription list
375 def GenPrivate(l,File):
379 F = open(File + ".tmp","w");
382 # Fetch all the users
384 if PasswdAttrs == None:
387 # Write out the position for each user
388 for x in PasswdAttrs:
389 if x[1].has_key("privatesub") == 0:
392 # If the account is locked, do not write it
393 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
396 # If the account has no PGP key, do not write it
397 if x[1].has_key("keyfingerprint") == 0:
400 # Must be in the Debian group (yuk, hard coded for now)
401 if GetAttr(x,"gidnumber") != "800":
405 Line = "%s"%(GetAttr(x,"privatesub"));
406 Line = Sanitize(Line) + "\n";
411 # Oops, something unspeakable happened.
417 # Generate the DNS Zone file
418 def GenDNS(l,File,BSMTPFile,HomePrefix):
422 F = open(File + ".tmp","w");
423 FB = open(BSMTPFile + ".tmp","w");
425 # Fetch all the users
427 if PasswdAttrs == None:
430 # Write out the zone file entry for each user
431 for x in PasswdAttrs:
432 if x[1].has_key("dnszoneentry") == 0:
435 F.write("; %s\n"%(EmailAddress(x)));
436 for z in x[1]["dnszoneentry"]:
437 Split = string.split(string.lower(z));
438 if string.lower(Split[1]) == 'in':
439 for y in range(0,len(Split)):
442 Line = string.join(Split," ") + "\n";
445 Host = Split[0] + DNSZone;
446 if BSMTPCheck.match(Line) != None:
447 F.write("; Has BSMTP\n");
448 FB.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
449 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
451 # Write some identification information
452 if string.lower(Split[2]) == "a":
453 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
454 for y in x[1]["keyfingerprint"]:
455 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
458 Line = "; Err %s"%(str(Split));
463 F.write("; Errors\n");
466 # Oops, something unspeakable happened.
469 Die(BSMTPFile,FB,None);
472 Done(BSMTPFile,FB,None);
474 # Connect to the ldap server
475 l = ldap.open(LDAPServer);
476 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
477 Pass = string.split(string.strip(F.readline())," ");
479 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
481 # Fetch all the groups
483 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
484 ["gid","gidnumber"]);
486 # Generate the GroupMap and GroupIDMap
488 if x[1].has_key("gidnumber") == 0:
490 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
492 # Fetch all the users
493 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
494 ["uid","uidnumber","gidnumber","supplementarygid",\
495 "gecos","loginshell","userpassword","shadowlastchange",\
496 "shadowmin","shadowmax","shadowwarning","shadowinactive",
497 "shadowexpire","emailforward","latitude","longitude",\
498 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
499 "keyfingerprint","privatesub","sshdsaauthkey"]);
501 # Open the control file
502 if len(sys.argv) == 1:
503 F = open(GenerateConf,"r");
505 F = open(sys.argv[1],"r")
507 # Generate global things
508 GlobalDir = GenerateDir+"/";
509 GenSSH2Shadow(l,GlobalDir+"ssh-dsa-shadow");
510 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
511 GenAllForward(l,GlobalDir+"mail-forward.cdb");
512 GenMarkers(l,GlobalDir+"markers");
513 GenPrivate(l,GlobalDir+"debian-private");
516 GenForward(l,GlobalDir+"forward-alias");
522 Line = string.strip(Line);
528 Split = string.split(Line," ");
529 OutDir = GenerateDir + '/' + Split[0] + '/';
530 try: os.mkdir(OutDir);
533 # Get the group list and convert any named groups to numerics
541 if GroupIDMap.has_key(I):
542 GroupList[str(GroupIDMap[I])] = None;
544 global Allowed,CurrentHost;
546 CurrentHost = Split[0];
549 GenPasswd(l,OutDir+"passwd",Split[1]);
551 GenGroup(l,OutDir+"group");
552 GenShadow(l,OutDir+"shadow");
554 # Link in global things
555 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
556 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow.cdb");
557 DoLink(GlobalDir,OutDir,"markers");
558 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
561 DoLink(GlobalDir,OutDir,"forward-alias");
563 if ExtraList.has_key("[DNS]"):
564 GenDNS(l,OutDir+"dns-zone",OutDir+"bsmtp",Split[1]);
566 if ExtraList.has_key("[PRIVATE]"):
567 DoLink(GlobalDir,OutDir,"debian-private");