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|gluck)\.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):
73 F = open(File + ".tdb.tmp","w");
77 if PasswdAttrs == None:
82 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
85 # Do not let people try to buffer overflow some busted passwd parser.
86 if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginshell")) > 50:
89 Line = "%s:x:%s:%s:%s:%s%s:%s" % (GetAttr(x,"uid"),\
90 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
91 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
92 GetAttr(x,"loginshell"));
94 Line = Sanitize(Line) + "\n";
95 F.write("0%u %s" % (I,Line));
96 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
97 F.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
100 # Oops, something unspeakable happened.
106 # Generate the shadow list
107 def GenShadow(l,File):
110 OldMask = os.umask(0077);
111 F = open(File + ".tdb.tmp","w",0600);
114 # Fetch all the users
116 if PasswdAttrs == None:
120 for x in PasswdAttrs:
121 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
124 Pass = GetAttr(x,"userpassword");
125 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
129 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
130 Pass,GetAttr(x,"shadowlastchange"),\
131 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
132 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
133 GetAttr(x,"shadowexpire"));
134 Line = Sanitize(Line) + "\n";
135 F.write("0%u %s" % (I,Line));
136 F.write(".%s %s" % (GetAttr(x,"uid"),Line));
139 # Oops, something unspeakable happened.
145 # Generate the shadow list
146 def GenSSHShadow(l,File):
149 OldMask = os.umask(0077);
150 F = open(File + ".tmp","w",0600);
153 # Fetch all the users
155 if PasswdAttrs == None:
158 for x in PasswdAttrs:
159 if x[1].has_key("uidnumber") == 0 or \
160 x[1].has_key("sshrsaauthkey") == 0:
162 for I in x[1]["sshrsaauthkey"]:
163 User = GetAttr(x,"uid");
164 Line = "%s: %s" %(User,I);
165 Line = Sanitize(Line) + "\n";
167 # Oops, something unspeakable happened.
173 # Generate the group list
174 def GenGroup(l,File):
177 F = open(File + ".tdb.tmp","w");
179 # Generate the GroupMap
181 for x in GroupIDMap.keys():
184 # Fetch all the users
186 if PasswdAttrs == None:
189 # Sort them into a list of groups having a set of users
190 for x in PasswdAttrs:
191 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
193 if x[1].has_key("supplementarygid") == 0:
196 for I in x[1]["supplementarygid"]:
197 if GroupMap.has_key(I):
198 GroupMap[I].append(GetAttr(x,"uid"));
200 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
202 # Output the group file.
204 for x in GroupMap.keys():
205 if GroupIDMap.has_key(x) == 0:
207 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
209 for I in GroupMap[x]:
210 Line = Line + ("%s%s" % (Comma,I));
212 Line = Sanitize(Line) + "\n";
213 F.write("0%u %s" % (J,Line));
214 F.write(".%s %s" % (x,Line));
215 F.write("=%u %s" % (GroupIDMap[x],Line));
218 # Oops, something unspeakable happened.
224 # Generate the email forwarding list
225 def GenForward(l,File):
229 OldMask = os.umask(0022);
230 F = open(File + ".tmp","w",0644);
233 # Fetch all the users
235 if PasswdAttrs == None:
238 # Write out the email address for each user
239 for x in PasswdAttrs:
240 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
243 # Do not allow people to try to buffer overflow busted parsers
244 if len(GetAttr(x,"emailforward")) > 200:
247 # Check the forwarding address
248 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
250 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
251 Line = Sanitize(Line) + "\n";
254 # Oops, something unspeakable happened.
260 def GenAllForward(l,File):
263 OldMask = os.umask(0022);
264 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
267 # Fetch all the users
269 if PasswdAttrs == None:
272 # Write out the email address for each user
273 for x in PasswdAttrs:
274 if x[1].has_key("emailforward") == 0:
277 # Do not allow people to try to buffer overflow busted parsers
278 Forward = GetAttr(x,"emailforward");
279 if len(Forward) > 200:
282 # Check the forwarding address
283 if EmailCheck.match(Forward) == None:
286 User = GetAttr(x,"uid");
287 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
289 # Oops, something unspeakable happened.
293 if Fdb.close() != None:
294 raise "cdbmake gave an error";
296 # Generate the anon XEarth marker file
297 def GenMarkers(l,File):
301 F = open(File + ".tmp","w");
304 # Fetch all the users
306 if PasswdAttrs == None:
309 # Write out the position for each user
310 for x in PasswdAttrs:
311 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
314 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
315 Line = Sanitize(Line) + "\n";
320 # Oops, something unspeakable happened.
326 # Generate the debian-private subscription list
327 def GenPrivate(l,File):
331 F = open(File + ".tmp","w");
334 # Fetch all the users
336 if PasswdAttrs == None:
339 # Write out the position for each user
340 for x in PasswdAttrs:
341 if x[1].has_key("privatesub") == 0:
344 # If the account is locked, do not write it
345 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
348 # If the account has no PGP key, do not write it
349 if x[1].has_key("keyfingerprint") == 0:
352 # Must be in the Debian group (yuk, hard coded for now)
353 if GetAttr(x,"gidnumber") != "800":
357 Line = "%s"%(GetAttr(x,"privatesub"));
358 Line = Sanitize(Line) + "\n";
363 # Oops, something unspeakable happened.
369 # Generate the DNS Zone file
370 def GenDNS(l,File,HomePrefix):
373 F = open(File + ".tmp","w");
375 # Fetch all the users
377 if PasswdAttrs == None:
380 # Write out the zone file entry for each user
381 for x in PasswdAttrs:
382 if x[1].has_key("dnszoneentry") == 0:
385 F.write("; %s\n"%(EmailAddress(x)));
386 for z in x[1]["dnszoneentry"]:
387 Split = string.split(string.lower(z));
388 if string.lower(Split[1]) == 'in':
389 for y in range(0,len(Split)):
392 Line = string.join(Split," ") + "\n";
395 Host = Split[0] + DNSZone;
396 if BSMTPCheck.match(Line) != None:
397 F.write("; Has BSMTP\n");
399 # Write some identification information
400 if string.lower(Split[2]) == "a":
401 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
402 for y in x[1]["keyfingerprint"]:
403 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
406 Line = "; Err %s"%(str(Split));
411 F.write("; Errors\n");
414 # Oops, something unspeakable happened.
420 # Generate the BSMTP file
421 def GenBSMTP(l,File,HomePrefix):
424 F = open(File + ".tmp","w");
426 # Fetch all the users
428 if PasswdAttrs == None:
431 # Write out the zone file entry for each user
432 for x in PasswdAttrs:
433 if x[1].has_key("dnszoneentry") == 0:
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";
444 Host = Split[0] + DNSZone;
445 if BSMTPCheck.match(Line) != None:
446 F.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
447 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
450 F.write("; Errors\n");
453 # Oops, something unspeakable happened.
459 # Generate the shadow list
460 def GenSSHKnown(l,File):
463 OldMask = os.umask(0022);
464 F = open(File + ".tmp","w",0644);
467 # Fetch all the hosts
468 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
469 ["hostname","sshrsahostkey"]);
475 if x[1].has_key("hostname") == 0 or \
476 x[1].has_key("sshrsahostkey") == 0:
478 Host = GetAttr(x,"hostname");
479 SHost = string.find(Host,".");
480 for I in x[1]["sshrsahostkey"]:
482 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
484 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
485 Line = Sanitize(Line) + "\n";
487 # Oops, something unspeakable happened.
494 # Connect to the ldap server
495 l = ldap.open(LDAPServer);
496 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
497 Pass = string.split(string.strip(F.readline())," ");
499 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
501 # Fetch all the groups
503 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
504 ["gid","gidnumber"]);
506 # Generate the GroupMap and GroupIDMap
508 if x[1].has_key("gidnumber") == 0:
510 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
512 # Fetch all the users
513 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
514 ["uid","uidnumber","gidnumber","supplementarygid",\
515 "gecos","loginshell","userpassword","shadowlastchange",\
516 "shadowmin","shadowmax","shadowwarning","shadowinactive",
517 "shadowexpire","emailforward","latitude","longitude",\
518 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
519 "keyfingerprint","privatesub"]);
521 # Open the control file
522 if len(sys.argv) == 1:
523 F = open(GenerateConf,"r");
525 F = open(sys.argv[1],"r")
527 # Generate global things
528 GlobalDir = GenerateDir+"/";
529 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
530 GenAllForward(l,GlobalDir+"mail-forward.cdb");
531 GenMarkers(l,GlobalDir+"markers");
532 GenPrivate(l,GlobalDir+"debian-private");
533 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
536 GenForward(l,GlobalDir+"forward-alias");
542 Line = string.strip(Line);
548 Split = string.split(Line," ");
549 OutDir = GenerateDir + '/' + Split[0] + '/';
550 try: os.mkdir(OutDir);
553 # Get the group list and convert any named groups to numerics
561 if GroupIDMap.has_key(I):
562 GroupList[str(GroupIDMap[I])] = None;
564 global Allowed,CurrentHost;
566 CurrentHost = Split[0];
569 GenPasswd(l,OutDir+"passwd",Split[1]);
571 GenGroup(l,OutDir+"group");
572 GenShadow(l,OutDir+"shadow");
574 # Link in global things
575 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
576 DoLink(GlobalDir,OutDir,"markers");
577 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
578 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
581 DoLink(GlobalDir,OutDir,"forward-alias");
583 if ExtraList.has_key("[DNS]"):
584 GenDNS(l,OutDir+"dns-zone",Split[1]);
586 if ExtraList.has_key("[BSMTP]"):
587 GenBSMTP(l,OutDir+"bsmtp",Split[1]);
589 if ExtraList.has_key("[PRIVATE]"):
590 DoLink(GlobalDir,OutDir,"debian-private");