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("^([^ <>@]+@[^ ,<>@]+)?$");
16 return string.translate(Str,string.maketrans("\n\r\t","$$$"));
18 def DoLink(From,To,File):
19 try: posix.remove(To+File);
21 posix.link(From+File,To+File);
23 # See if this user is in the group list
24 def IsInGroup(DnRecord):
25 global Allowed,CurrentHost;
29 # See if the primary group is in the list
30 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
33 # Check the host based ACL
34 if DnRecord[1].has_key("allowedhosts") != 0:
35 for I in DnRecord[1]["allowedhosts"]:
39 # See if there are supplementary groups
40 if DnRecord[1].has_key("supplementarygid") == 0:
43 # Check the supplementary groups
44 for I in DnRecord[1]["supplementarygid"]:
45 if Allowed.has_key(I):
54 try: os.remove(File + ".tmp");
56 try: os.remove(File + ".tdb.tmp");
62 os.rename(File + ".tmp",File);
65 os.rename(File + ".tdb.tmp",File+".tdb");
67 # Generate the password list
68 def GenPasswd(l,File,HomePrefix):
72 F = open(File + ".tmp","w");
73 Fdb = 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\n" % (GetAttr(x,"uid"),\
90 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
91 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
92 GetAttr(x,"loginshell"));
94 Fdb.write("0%u %s" % (I,Line));
95 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
96 Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
99 # Oops, something unspeakable happened.
105 # Generate the shadow list
106 def GenShadow(l,File):
110 OldMask = os.umask(0077);
111 F = open(File + ".tmp","w",0600);
112 Fdb = open(File + ".tdb.tmp","w",0600);
115 # Fetch all the users
117 if PasswdAttrs == None:
121 for x in PasswdAttrs:
122 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
125 Pass = GetAttr(x,"userpassword");
126 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
130 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
131 Pass,GetAttr(x,"shadowlastchange"),\
132 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
133 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
134 GetAttr(x,"shadowexpire"));
135 Line = Sanitize(Line) + "\n";
137 Fdb.write("0%u %s" % (I,Line));
138 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
141 # Oops, something unspeakable happened.
147 # Generate the shadow list
148 def GenSSHShadow(l,File):
152 OldMask = os.umask(0077);
153 F = open(File + ".tmp","w",0600);
154 Fdb = os.popen("cdbmake %s.cdb %s.cdb.tmp"%(File,File),"w");
157 # Fetch all the users
159 if PasswdAttrs == None:
163 for x in PasswdAttrs:
164 if x[1].has_key("uidnumber") == 0 or \
165 x[1].has_key("sshrsaauthkey") == 0:
167 for I in x[1]["sshrsaauthkey"]:
168 User = GetAttr(x,"uid");
169 Line = "%s: %s" %(User,I);
170 Line = Sanitize(Line) + "\n";
172 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(I),User,I));
174 # Oops, something unspeakable happened.
178 if Fdb.close() != None:
179 raise "cdbmake gave an error";
182 # Generate the group list
183 def GenGroup(l,File):
187 F = open(File + ".tmp","w");
188 Fdb = open(File + ".tdb.tmp","w");
190 # Generate the GroupMap
192 for x in GroupIDMap.keys():
195 # Fetch all the users
197 if PasswdAttrs == None:
200 # Sort them into a list of groups having a set of users
201 for x in PasswdAttrs:
202 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
204 if x[1].has_key("supplementarygid") == 0:
207 for I in x[1]["supplementarygid"]:
208 if GroupMap.has_key(I):
209 GroupMap[I].append(GetAttr(x,"uid"));
211 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
213 # Output the group file.
215 for x in GroupMap.keys():
216 if GroupIDMap.has_key(x) == 0:
218 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
220 for I in GroupMap[x]:
221 Line = Line + ("%s%s" % (Comma,I));
223 Line = Sanitize(Line) + "\n";
225 Fdb.write("0%u %s" % (Counter,Line));
226 Fdb.write(".%s %s" % (x,Line));
227 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
228 Counter = Counter + 1;
230 # Oops, something unspeakable happened.
236 # Generate the email forwarding list
237 def GenForward(l,File):
241 OldMask = os.umask(0022);
242 F = open(File + ".tmp","w",0644);
245 # Fetch all the users
247 if PasswdAttrs == None:
250 # Write out the email address for each user
251 for x in PasswdAttrs:
252 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
255 # Do not allow people to try to buffer overflow busted parsers
256 if len(GetAttr(x,"emailforward")) > 200:
259 # Check the forwarding address
260 if EmailCheck.match(GetAttr(x,"emailforward")) == None:
262 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
263 Line = Sanitize(Line) + "\n";
266 # Oops, something unspeakable happened.
272 def GenAllForward(l,File):
275 OldMask = os.umask(0022);
276 Fdb = os.popen("cdbmake %s %s.tmp"%(File,File),"w");
279 # Fetch all the users
281 if PasswdAttrs == None:
284 # Write out the email address for each user
285 for x in PasswdAttrs:
286 if x[1].has_key("emailforward") == 0:
289 # Do not allow people to try to buffer overflow busted parsers
290 Forward = GetAttr(x,"emailforward");
291 if len(Forward) > 200:
294 # Check the forwarding address
295 if EmailCheck.match(Forward) == None:
298 User = GetAttr(x,"uid");
299 Fdb.write("+%d,%d:%s->%s\n"%(len(User),len(Forward),User,Forward));
301 # Oops, something unspeakable happened.
305 if Fdb.close() != None:
306 raise "cdbmake gave an error";
308 # Generate the anon XEarth marker file
309 def GenMarkers(l,File):
313 F = open(File + ".tmp","w");
316 # Fetch all the users
318 if PasswdAttrs == None:
321 # Write out the position for each user
322 for x in PasswdAttrs:
323 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
326 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
327 Line = Sanitize(Line) + "\n";
332 # Oops, something unspeakable happened.
338 # Generate the debian-private subscription list
339 def GenPrivate(l,File):
343 F = open(File + ".tmp","w");
346 # Fetch all the users
348 if PasswdAttrs == None:
351 # Write out the position for each user
352 for x in PasswdAttrs:
353 if x[1].has_key("privatesub") == 0:
356 # If the account is locked, do not write it
357 if (string.find(GetAttr(x,"userpassword"),"*LK*") != -1):
360 # If the account has no PGP key, do not write it
361 if x[1].has_key("keyfingerprint") == 0:
364 # Must be in the Debian group (yuk, hard coded for now)
365 if GetAttr(x,"gidnumber") != "800":
369 Line = "%s"%(GetAttr(x,"privatesub"));
370 Line = Sanitize(Line) + "\n";
375 # Oops, something unspeakable happened.
381 # Generate the DNS Zone file
386 F = open(File + ".tmp","w");
389 # Fetch all the users
391 if PasswdAttrs == None:
394 # Write out the zone file entry for each user
395 for x in PasswdAttrs:
396 if x[1].has_key("dnszoneentry") == 0:
399 F.write("; %s\n"%(EmailAddress(x)));
400 for z in x[1]["dnszoneentry"]:
401 Split = string.split(string.lower(z));
402 if string.lower(Split[1]) == 'in':
403 for y in range(0,len(Split)):
406 Line = string.join(Split," ") + "\n";
409 # Write some identication information
410 if string.lower(Split[2]) != "cname":
411 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
412 for y in x[1]["keyfingerprint"]:
413 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
416 Line = "; Err %s"%(str(Split));
423 # Oops, something unspeakable happened.
429 # Connect to the ldap server
430 l = ldap.open(LDAPServer);
431 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
432 Pass = string.split(string.strip(F.readline())," ");
434 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
436 # Fetch all the groups
438 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
439 ["gid","gidnumber"]);
441 # Generate the GroupMap and GroupIDMap
443 if x[1].has_key("gidnumber") == 0:
445 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
447 # Fetch all the users
448 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
449 ["uid","uidnumber","gidnumber","supplementarygid",\
450 "gecos","loginshell","userpassword","shadowlastchange",\
451 "shadowmin","shadowmax","shadowwarning","shadowinactive",
452 "shadowexpire","emailforward","latitude","longitude",\
453 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
454 "keyfingerprint","privatesub"]);
456 # Open the control file
457 if len(sys.argv) == 1:
458 F = open(GenerateConf,"r");
460 F = open(sys.argv[1],"r")
462 # Generate global things
463 GlobalDir = GenerateDir+"/";
464 GenSSHShadow(l,GlobalDir+"ssh-rsa-shadow");
465 GenAllForward(l,GlobalDir+"mail-forward.cdb");
466 GenMarkers(l,GlobalDir+"markers");
467 GenDNS(l,GlobalDir+"dns-zone");
468 GenPrivate(l,GlobalDir+"debian-private");
471 GenForward(l,GlobalDir+"forward-alias");
477 Line = string.strip(Line);
483 Split = string.split(Line," ");
484 OutDir = GenerateDir + '/' + Split[0] + '/';
485 try: os.mkdir(OutDir);
488 # Get the group list and convert any named groups to numerics
496 if GroupIDMap.has_key(I):
497 GroupList[str(GroupIDMap[I])] = None;
499 global Allowed,CurrentHost;
501 CurrentHost = Split[0];
504 GenPasswd(l,OutDir+"passwd",Split[1]);
506 GenGroup(l,OutDir+"group");
507 GenShadow(l,OutDir+"shadow");
509 # Link in global things
510 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow");
511 DoLink(GlobalDir,OutDir,"ssh-rsa-shadow.cdb");
512 DoLink(GlobalDir,OutDir,"markers");
513 DoLink(GlobalDir,OutDir,"mail-forward.cdb");
516 DoLink(GlobalDir,OutDir,"forward-alias");
518 if ExtraList.has_key("[DNS]"):
519 DoLink(GlobalDir,OutDir,"dns-zone");
521 if ExtraList.has_key("[PRIVATE]"):
522 DoLink(GlobalDir,OutDir,"debian-private");