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 *;
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 # Generate the group list
185 def GenGroup(l,File):
189 F = open(File + ".tmp","w");
190 Fdb = open(File + ".tdb.tmp","w");
192 # Generate the GroupMap
194 for x in GroupIDMap.keys():
197 # Fetch all the users
199 if PasswdAttrs == None:
202 # Sort them into a list of groups having a set of users
203 for x in PasswdAttrs:
204 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
206 if x[1].has_key("supplementarygid") == 0:
209 for I in x[1]["supplementarygid"]:
210 if GroupMap.has_key(I):
211 GroupMap[I].append(GetAttr(x,"uid"));
213 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
215 # Output the group file.
217 for x in GroupMap.keys():
218 if GroupIDMap.has_key(x) == 0:
220 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
222 for I in GroupMap[x]:
223 Line = Line + ("%s%s" % (Comma,I));
225 Line = Sanitize(Line) + "\n";
227 Fdb.write("0%u %s" % (Counter,Line));
228 Fdb.write(".%s %s" % (x,Line));
229 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
230 Counter = Counter + 1;
232 # Oops, something unspeakable happened.
238 # Generate the email forwarding list
239 def GenForward(l,File):
243 OldMask = os.umask(0022);
244 F = open(File + ".tmp","w",0644);
247 # Fetch all the users
249 if PasswdAttrs == None:
252 # Write out the email address for each user
253 for x in PasswdAttrs:
254 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
257 # Do not allow people to try to buffer overflow busted parsers
258 if len(GetAttr(x,"emailforward")) > 200:
261 # Check the forwarding address
262 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
264 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
265 Line = Sanitize(Line) + "\n";
268 # Oops, something unspeakable happened.
274 def GenAllForward(l,File):
277 OldMask = os.umask(0022);
278 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
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:
291 # Do not allow people to try to buffer overflow busted parsers
292 Forward = GetAttr(x,"emailforward");
293 if len(Forward) > 200:
296 # Check the forwarding address
297 if EmailCheck.match(Forward) == None:
300 User = GetAttr(x,"uid");
301 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
303 # Oops, something unspeakable happened.
307 if Fdb.close() != None:
308 raise "cdbmake gave an error";
310 # Generate the anon XEarth marker file
311 def GenMarkers(l,File):
315 F = open(File + ".tmp","w");
318 # Fetch all the users
320 if PasswdAttrs == None:
323 # Write out the position for each user
324 for x in PasswdAttrs:
325 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
328 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
329 Line = Sanitize(Line) + "\n";
334 # Oops, something unspeakable happened.
340 # Generate the debian-private subscription list
341 def GenPrivate(l,File):
345 F = open(File + ".tmp","w");
348 # Fetch all the users
350 if PasswdAttrs == None:
353 # Write out the position for each user
354 for x in PasswdAttrs:
355 if x[1].has_key("privatesub") == 0:
358 # If the account is locked, do not write it
359 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
362 # If the account has no PGP key, do not write it
363 if x[1].has_key("keyfingerprint") == 0:
366 # Must be in the Debian group (yuk, hard coded for now)
367 if GetAttr(x,"gidnumber") != "800":
371 Line = "%s"%(GetAttr(x,"privatesub"));
372 Line = Sanitize(Line) + "\n";
377 # Oops, something unspeakable happened.
383 # Generate the DNS Zone file
384 def GenDNS(l,File,BSMTPFile,HomePrefix):
388 F = open(File + ".tmp","w");
389 FB = open(BSMTPFile + ".tmp","w");
391 # Fetch all the users
393 if PasswdAttrs == None:
396 # Write out the zone file entry for each user
397 for x in PasswdAttrs:
398 if x[1].has_key("dnszoneentry") == 0:
401 F.write("; %s\n"%(EmailAddress(x)));
402 for z in x[1]["dnszoneentry"]:
403 Split = string.split(string.lower(z));
404 if string.lower(Split[1]) == 'in':
405 for y in range(0,len(Split)):
408 Line = string.join(Split," ") + "\n";
411 Host = Split[0] + DNSZone;
412 if BSMTPCheck.match(Line) != None:
413 F.write("; Has BSMTP\n");
414 FB.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
415 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
417 # Write some identification information
418 if string.lower(Split[2]) == "a":
419 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
420 for y in x[1]["keyfingerprint"]:
421 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
424 Line = "; Err %s"%(str(Split));
429 F.write("; Errors\n");
432 # Oops, something unspeakable happened.
435 Die(BSMTPFile,FB,None);
438 Done(BSMTPFile,FB,None);
440 # Generate the shadow list
441 def GenSSHKnown(l,File):
445 OldMask = os.umask(0022);
446 F = open(File + ".tmp","w",0644);
447 Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
450 # Fetch all the hosts
451 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
452 ["hostname","sshrsahostkey"]);
459 if x[1].has_key("hostname") == 0 or \
460 x[1].has_key("sshrsahostkey") == 0:
462 Host = GetAttr(x,"hostname");
463 SHost = string.find(Host,".");
464 for I in x[1]["sshrsahostkey"]:
466 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
468 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
469 Line = Sanitize(Line) + "\n";
471 Fdb.write("+%d,%d:%s->%s\n"%(len(Host),len(I),Host,I));
473 # Oops, something unspeakable happened.
477 if Fdb.close() != None:
478 raise "cdbmake gave an error";
482 # Connect to the ldap server
483 l = ldap.open(LDAPServer);
484 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
485 Pass = string.split(string.strip(F.readline())," ");
487 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
489 # Fetch all the groups
491 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
492 ["gid","gidnumber"]);
494 # Generate the GroupMap and GroupIDMap
496 if x[1].has_key("gidnumber") == 0:
498 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
500 # Fetch all the users
501 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
502 ["uid","uidnumber","gidnumber","supplementarygid",\
503 "gecos","loginshell","userpassword","shadowlastchange",\
504 "shadowmin","shadowmax","shadowwarning","shadowinactive",
505 "shadowexpire","emailforward","latitude","longitude",\
506 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
507 "keyfingerprint","privatesub"]);
509 # Open the control file
510 if len(sys.argv) == 1:
511 F = open(GenerateConf,"r");
513 F = open(sys.argv[1],"r")
515 # Generate global things
516 GlobalDir = GenerateDir+"/";
517 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
518 GenAllForward(l,GlobalDir+"mail-forward.cdb");
519 GenMarkers(l,GlobalDir+"markers");
520 GenPrivate(l,GlobalDir+"debian-private");
521 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
524 GenForward(l,GlobalDir+"forward-alias");
530 Line = string.strip(Line);
536 Split = string.split(Line," ");
537 OutDir = GenerateDir + '/' + Split[0] + '/';
538 try: os.mkdir(OutDir);
541 # Get the group list and convert any named groups to numerics
549 if GroupIDMap.has_key(I):
550 GroupList[str(GroupIDMap[I])] = None;
552 global Allowed,CurrentHost;
554 CurrentHost = Split[0];
557 GenPasswd(l,OutDir+"passwd",Split[1]);
559 GenGroup(l,OutDir+"group");
560 GenShadow(l,OutDir+"shadow");
562 # Link in global things
563 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
564 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow.cdb");
565 DoLink(GlobalDir,OutDir,"markers");
566 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
567 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
570 DoLink(GlobalDir,OutDir,"forward-alias");
572 if ExtraList.has_key("[DNS]"):
573 GenDNS(l,OutDir+"dns-zone",OutDir+"bsmtp",Split[1]);
575 if ExtraList.has_key("[PRIVATE]"):
576 DoLink(GlobalDir,OutDir,"debian-private");