3 # Generates passwd, shadow and group files from the ldap directory.
5 import string, re, time, ldap, getopt, sys, os, pwd;
6 from userdir_ldap import *;
14 return string.translate(Str,string.maketrans("\n\r\t","$$$"));
16 # See if this user is in the group list
17 def IsInGroup(DnRecord):
18 global Allowed,CurrentHost;
22 # See if the primary group is in the list
23 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
26 # Check the host based ACL
27 if DnRecord[1].has_key("allowedhosts") != 0:
28 for I in DnRecord[1]["allowedhosts"]:
32 # See if there are supplementary groups
33 if DnRecord[1].has_key("supplementarygid") == 0:
36 # Check the supplementary groups
37 for I in DnRecord[1]["supplementarygid"]:
38 if Allowed.has_key(I):
47 try: os.remove(File + ".tmp");
49 try: os.remove(File + ".tdb.tmp");
55 os.rename(File + ".tmp",File);
58 os.rename(File + ".tdb.tmp",File+".tdb");
60 # Generate the password list
61 def GenPasswd(l,File,HomePrefix):
65 F = open(File + ".tmp","w");
66 Fdb = open(File + ".tdb.tmp","w");
70 if PasswdAttrs == None:
75 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
78 # Do not let people try to buffer overflow some busted passwd parser.
79 if len(GetAttr(x,"gecos")) > 100 or len(GetAttr(x,"loginshell")) > 50:
82 Line = "%s:x:%s:%s:%s:%s%s:%s\n" % (GetAttr(x,"uid"),\
83 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
84 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
85 GetAttr(x,"loginshell"));
87 Fdb.write("0%u %s" % (I,Line));
88 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
89 Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
92 # Oops, something unspeakable happened.
98 # Generate the shadow list
99 def GenShadow(l,File):
103 OldMask = os.umask(0077);
104 F = open(File + ".tmp","w",0600);
105 Fdb = open(File + ".tdb.tmp","w",0600);
108 # Fetch all the users
110 if PasswdAttrs == None:
114 for x in PasswdAttrs:
115 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
118 Pass = GetAttr(x,"userpassword");
119 if Pass[0:7] != "{crypt}" or len(Pass) > 50:
123 Line = "%s:%s:%s:%s:%s:%s:%s:%s:" % (GetAttr(x,"uid"),\
124 Pass,GetAttr(x,"shadowlastchange"),\
125 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
126 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
127 GetAttr(x,"shadowexpire"));
128 Line = Sanitize(Line) + "\n";
130 Fdb.write("0%u %s" % (I,Line));
131 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
134 # Oops, something unspeakable happened.
140 # Generate the shadow list
141 def GenSSHShadow(l,File):
145 OldMask = os.umask(0077);
146 F = open(File + ".tmp","w",0600);
150 # Fetch all the users
152 if PasswdAttrs == None:
156 for x in PasswdAttrs:
157 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0 or \
158 x[1].has_key("sshrsaauthkey") == 0:
160 for I in x[1]["sshrsaauthkey"]:
161 Line = "%s: %s" %(GetAttr(x,"uid"),I);
162 Line = Sanitize(Line) + "\n";
165 # Oops, something unspeakable happened.
171 # Generate the group list
172 def GenGroup(l,File):
176 F = open(File + ".tmp","w");
177 Fdb = 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";
214 Fdb.write("0%u %s" % (Counter,Line));
215 Fdb.write(".%s %s" % (x,Line));
216 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
217 Counter = Counter + 1;
219 # Oops, something unspeakable happened.
225 # Generate the email forwarding list
226 def GenForward(l,File):
230 OldMask = os.umask(0022);
231 F = open(File + ".tmp","w",0644);
235 # Fetch all the users
237 if PasswdAttrs == None:
240 # Write out the email address for each user
241 for x in PasswdAttrs:
242 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
245 # Do not allow people to try to buffer overflow busted parsers
246 if len(GetAttr(x,"emailforward")) > 200:
249 Line = "%s: %s" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
250 Line = Sanitize(Line) + "\n";
253 # Oops, something unspeakable happened.
259 # Generate the anon XEarth marker file
260 def GenMarkers(l,File):
264 F = open(File + ".tmp","w");
267 # Fetch all the users
269 if PasswdAttrs == None:
272 # Write out the position for each user
273 for x in PasswdAttrs:
274 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
277 Line = "%8s %8s \"\""%(DecDegree(GetAttr(x,"latitude"),1),DecDegree(GetAttr(x,"longitude"),1));
278 Line = Sanitize(Line) + "\n";
283 # Oops, something unspeakable happened.
289 # Generate the DNS Zone file
294 F = open(File + ".tmp","w");
297 # Fetch all the users
299 if PasswdAttrs == None:
302 # Write out the zone file entry for each user
303 for x in PasswdAttrs:
304 if x[1].has_key("dnszoneentry") == 0:
307 F.write("; %s\n"%(EmailAddress(x)));
308 for z in x[1]["dnszoneentry"]:
309 Split = string.split(string.lower(z));
310 if string.lower(Split[1]) == 'in':
311 for y in range(0,len(Split)):
314 Line = string.join(Split," ") + "\n";
317 # Write some identication information
318 if string.lower(Split[2]) != "cname":
319 Line = "%s IN TXT \"%s\"\n"%(Split[0],EmailAddress(x));
320 for y in x[1]["keyfingerprint"]:
321 Line = Line + "%s IN TXT \"PGP %s\"\n"%(Split[0],FormatPGPKey(y));
324 Line = "; Err %s"%(str(Split));
331 # Oops, something unspeakable happened.
337 # Connect to the ldap server
338 l = ldap.open(LDAPServer);
339 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
340 Pass = string.split(string.strip(F.readline())," ");
342 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
344 # Fetch all the groups
346 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
347 ["gid","gidnumber"]);
349 # Generate the GroupMap and GroupIDMap
351 if x[1].has_key("gidnumber") == 0:
353 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
355 # Fetch all the users
356 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
357 ["uid","uidnumber","gidnumber","supplementarygid",\
358 "gecos","loginshell","userpassword","shadowlastchange",\
359 "shadowmin","shadowmax","shadowwarning","shadowinactive",
360 "shadowexpire","emailforward","latitude","longitude",\
361 "allowedhosts","sshrsaauthkey","dnszoneentry","cn","sn",\
364 # Open the control file
365 if len(sys.argv) == 1:
366 F = open(GenerateConf,"r");
368 F = open(sys.argv[1],"r")
373 Line = string.strip(Line);
379 Split = string.split(Line," ");
380 OutDir = GenerateDir + '/' + Split[0] + '/';
381 try: os.mkdir(OutDir);
384 # Get the group list and convert any named groups to numerics
392 if GroupIDMap.has_key(I):
393 GroupList[str(GroupIDMap[I])] = None;
395 global Allowed,CurrentHost;
397 CurrentHost = Split[0];
399 GenPasswd(l,OutDir+"passwd",Split[1]);
400 GenGroup(l,OutDir+"group");
401 GenShadow(l,OutDir+"shadow");
402 GenSSHShadow(l,OutDir+"ssh-rsa-shadow");
403 GenForward(l,OutDir+"forward-alias");
404 GenMarkers(l,OutDir+"markers");
406 if ExtraList.has_key("[DNS]"):
407 GenDNS(l,OutDir+"dns-zone");