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 + ".tmp","w");
77 if PasswdAttrs == None:
81 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
84 # Do not let people try to buffer overflow some busted passwd parser.
85 if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginshell")) > 50:
88 Line = "%s:x:%s:%s:%s:%s%s:%s\n" % (GetAttr(x,"uid"),\
89 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
90 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
91 GetAttr(x,"loginshell"));
94 # Oops, something unspeakable happened.
100 # Generate the shadow list
101 def GenShadow(l,File):
104 OldMask = os.umask(0077);
105 F = open(File + ".tmp","w",0600);
108 # Fetch all the users
110 if PasswdAttrs == None:
113 for x in PasswdAttrs:
114 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
117 Pass = GetAttr(x,"userpassword");
118 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
122 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
123 Pass,GetAttr(x,"shadowlastchange"),\
124 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
125 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
126 GetAttr(x,"shadowexpire"));
127 Line = Sanitize(Line) + "\n";
130 # Oops, something unspeakable happened.
136 # Generate the shadow list
137 def GenSSHShadow(l,File):
140 OldMask = os.umask(0077);
141 F = open(File + ".tmp","w",0600);
144 # Fetch all the users
146 if PasswdAttrs == None:
149 for x in PasswdAttrs:
150 if x[1].has_key("uidnumber") == 0 or \
151 x[1].has_key("sshrsaauthkey") == 0:
153 for I in x[1]["sshrsaauthkey"]:
154 User = GetAttr(x,"uid");
155 Line = "%s: %s" %(User,I);
156 Line = Sanitize(Line) + "\n";
158 # Oops, something unspeakable happened.
164 # Generate the group list
165 def GenGroup(l,File):
168 F = open(File + ".tmp","w");
170 # Generate the GroupMap
172 for x in GroupIDMap.keys():
175 # Fetch all the users
177 if PasswdAttrs == None:
180 # Sort them into a list of groups having a set of users
181 for x in PasswdAttrs:
182 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
184 if x[1].has_key("supplementarygid") == 0:
187 for I in x[1]["supplementarygid"]:
188 if GroupMap.has_key(I):
189 GroupMap[I].append(GetAttr(x,"uid"));
191 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
193 # Output the group file.
194 for x in GroupMap.keys():
195 if GroupIDMap.has_key(x) == 0:
197 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
199 for I in GroupMap[x]:
200 Line = Line + ("%s%s" % (Comma,I));
202 Line = Sanitize(Line) + "\n";
205 # Oops, something unspeakable happened.
211 # Generate the email forwarding list
212 def GenForward(l,File):
216 OldMask = os.umask(0022);
217 F = open(File + ".tmp","w",0644);
220 # Fetch all the users
222 if PasswdAttrs == None:
225 # Write out the email address for each user
226 for x in PasswdAttrs:
227 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
230 # Do not allow people to try to buffer overflow busted parsers
231 if len(GetAttr(x,"emailforward")) > 200:
234 # Check the forwarding address
235 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
237 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
238 Line = Sanitize(Line) + "\n";
241 # Oops, something unspeakable happened.
247 def GenAllForward(l,File):
250 OldMask = os.umask(0022);
251 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
254 # Fetch all the users
256 if PasswdAttrs == None:
259 # Write out the email address for each user
260 for x in PasswdAttrs:
261 if x[1].has_key("emailforward") == 0:
264 # Do not allow people to try to buffer overflow busted parsers
265 Forward = GetAttr(x,"emailforward");
266 if len(Forward) > 200:
269 # Check the forwarding address
270 if EmailCheck.match(Forward) == None:
273 User = GetAttr(x,"uid");
274 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
276 # Oops, something unspeakable happened.
280 if Fdb.close() != None:
281 raise "cdbmake gave an error";
283 # Generate the anon XEarth marker file
284 def GenMarkers(l,File):
288 F = open(File + ".tmp","w");
291 # Fetch all the users
293 if PasswdAttrs == None:
296 # Write out the position for each user
297 for x in PasswdAttrs:
298 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
301 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
302 Line = Sanitize(Line) + "\n";
307 # Oops, something unspeakable happened.
313 # Generate the debian-private subscription list
314 def GenPrivate(l,File):
318 F = open(File + ".tmp","w");
321 # Fetch all the users
323 if PasswdAttrs == None:
326 # Write out the position for each user
327 for x in PasswdAttrs:
328 if x[1].has_key("privatesub") == 0:
331 # If the account is locked, do not write it
332 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
335 # If the account has no PGP key, do not write it
336 if x[1].has_key("keyfingerprint") == 0:
339 # Must be in the Debian group (yuk, hard coded for now)
340 if GetAttr(x,"gidnumber") != "800":
344 Line = "%s"%(GetAttr(x,"privatesub"));
345 Line = Sanitize(Line) + "\n";
350 # Oops, something unspeakable happened.
356 # Generate the DNS Zone file
357 def GenDNS(l,File,BSMTPFile,HomePrefix):
361 F = open(File + ".tmp","w");
362 FB = open(BSMTPFile + ".tmp","w");
364 # Fetch all the users
366 if PasswdAttrs == None:
369 # Write out the zone file entry for each user
370 for x in PasswdAttrs:
371 if x[1].has_key("dnszoneentry") == 0:
374 F.write("; %s\n"%(EmailAddress(x)));
375 for z in x[1]["dnszoneentry"]:
376 Split = string.split(string.lower(z));
377 if string.lower(Split[1]) == 'in':
378 for y in range(0,len(Split)):
381 Line = string.join(Split," ") + "\n";
384 Host = Split[0] + DNSZone;
385 if BSMTPCheck.match(Line) != None:
386 F.write("; Has BSMTP\n");
387 FB.write("%s: user=%s group=Debian file=%s%s/bsmtp/%s\n"%(Host,
388 GetAttr(x,"uid"),HomePrefix,GetAttr(x,"uid"),Host));
390 # Write some identification information
391 if string.lower(Split[2]) == "a":
392 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
393 for y in x[1]["keyfingerprint"]:
394 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
397 Line = "; Err %s"%(str(Split));
402 F.write("; Errors\n");
405 # Oops, something unspeakable happened.
408 Die(BSMTPFile,FB,None);
411 Done(BSMTPFile,FB,None);
413 # Generate the shadow list
414 def GenSSHKnown(l,File):
417 OldMask = os.umask(0022);
418 F = open(File + ".tmp","w",0644);
421 # Fetch all the hosts
422 HostKeys = l.search_s(HostBaseDn,ldap.SCOPE_ONELEVEL,"sshrsahostkey=*",\
423 ["hostname","sshrsahostkey"]);
429 if x[1].has_key("hostname") == 0 or \
430 x[1].has_key("sshrsahostkey") == 0:
432 Host = GetAttr(x,"hostname");
433 SHost = string.find(Host,".");
434 for I in x[1]["sshrsahostkey"]:
436 Line = "%s,%s %s" %(Host,socket.gethostbyname(Host),I);
438 Line = "%s,%s,%s %s" %(Host,Host[0:SHost],socket.gethostbyname(Host),I);
439 Line = Sanitize(Line) + "\n";
441 # Oops, something unspeakable happened.
448 # Connect to the ldap server
449 l = ldap.open(LDAPServer);
450 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
451 Pass = string.split(string.strip(F.readline())," ");
453 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
455 # Fetch all the groups
457 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
458 ["gid","gidnumber"]);
460 # Generate the GroupMap and GroupIDMap
462 if x[1].has_key("gidnumber") == 0:
464 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
466 # Fetch all the users
467 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
468 ["uid","uidnumber","gidnumber","supplementarygid",\
469 "gecos","loginshell","userpassword","shadowlastchange",\
470 "shadowmin","shadowmax","shadowwarning","shadowinactive",
471 "shadowexpire","emailforward","latitude","longitude",\
472 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
473 "keyfingerprint","privatesub"]);
475 # Open the control file
476 if len(sys.argv) == 1:
477 F = open(GenerateConf,"r");
479 F = open(sys.argv[1],"r")
481 # Generate global things
482 GlobalDir = GenerateDir+"/";
483 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
484 GenAllForward(l,GlobalDir+"mail-forward.cdb");
485 GenMarkers(l,GlobalDir+"markers");
486 GenPrivate(l,GlobalDir+"debian-private");
487 GenSSHKnown(l,GlobalDir+"ssh_known_hosts");
490 GenForward(l,GlobalDir+"forward-alias");
496 Line = string.strip(Line);
502 Split = string.split(Line," ");
503 OutDir = GenerateDir + '/' + Split[0] + '/';
504 try: os.mkdir(OutDir);
507 # Get the group list and convert any named groups to numerics
515 if GroupIDMap.has_key(I):
516 GroupList[str(GroupIDMap[I])] = None;
518 global Allowed,CurrentHost;
520 CurrentHost = Split[0];
523 GenPasswd(l,OutDir+"passwd",Split[1]);
525 GenGroup(l,OutDir+"group");
526 GenShadow(l,OutDir+"shadow");
528 # Link in global things
529 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
530 DoLink(GlobalDir,OutDir,"markers");
531 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
532 DoLink(GlobalDir,OutDir,"ssh_known_hosts");
535 DoLink(GlobalDir,OutDir,"forward-alias");
537 if ExtraList.has_key("[DNS]"):
538 GenDNS(l,OutDir+"dns-zone",OutDir+"bsmtp",Split[1]);
540 if ExtraList.has_key("[PRIVATE]"):
541 DoLink(GlobalDir,OutDir,"debian-private");