3 # Generates passwd, shadow and group files from the ldap directory.
5 import string, re, time, ldap, getopt, sys, os, posix, pwd;
6 from userdir_ldap import *;
11 # See if this user is in the group list
12 def IsInGroup(DnRecord,Allowed):
13 # See if the primary group is in the list
14 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
17 # See if there are supplementary groups
18 if DnRecord[1].has_key("supplementarygid") == 0:
21 # Check the supplementary groups
22 for I in DnRecord[1]["supplementarygid"]:
23 if Allowed.has_key(I):
32 try: os.remove(File + ".tmp");
34 try: os.remove(File + ".tdb.tmp");
40 os.rename(File + ".tmp",File);
43 os.rename(File + ".tdb.tmp",File+".tdb");
45 # Generate the password list
46 def GenPasswd(l,File,HomePrefix,Allowed):
50 F = open(File + ".tmp","w");
51 Fdb = open(File + ".tdb.tmp","w");
55 if PasswdAttrs == None:
60 if x[1].has_key("uidnumber") == 0 or IsInGroup(x,Allowed) == 0:
63 Line = "%s:x:%s:%s:%s:%s%s:%s\n" % (GetAttr(x,"uid"),\
64 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
65 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
66 GetAttr(x,"loginshell"));
68 Fdb.write("0%u %s" % (I,Line));
69 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
70 Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
73 # Oops, something unspeakable happened.
79 # Generate the shadow list
80 def GenShadow(l,File,Allowed):
84 OldMask = os.umask(0077);
85 F = open(File + ".tmp","w",0600);
86 Fdb = open(File + ".tdb.tmp","w",0600);
91 if PasswdAttrs == None:
96 if x[1].has_key("uidnumber") == 0 or IsInGroup(x,Allowed) == 0:
99 Pass = GetAttr(x,"userpassword");
100 if Pass[0:7] != "{crypt}":
104 Line = "%s:%s:%s:%s:%s:%s:%s:%s:\n" % (GetAttr(x,"uid"),\
105 Pass,GetAttr(x,"shadowlastchange"),\
106 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
107 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
108 GetAttr(x,"shadowexpire"));
110 Fdb.write("0%u %s" % (I,Line));
111 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
114 # Oops, something unspeakable happened.
120 # Generate the group list
121 def GenGroup(l,File,Allowed):
125 F = open(File + ".tmp","w");
126 Fdb = open(File + ".tdb.tmp","w");
128 # Generate the GroupMap
130 for x in GroupIDMap.keys():
133 # Fetch all the users
135 if PasswdAttrs == None:
138 # Sort them into a list of groups having a set of users
139 for x in PasswdAttrs:
140 if x[1].has_key("uidnumber") == 0 or IsInGroup(x,Allowed) == 0:
142 if x[1].has_key("supplementarygid") == 0:
145 for I in x[1]["supplementarygid"]:
146 if GroupMap.has_key(I):
147 GroupMap[I].append(GetAttr(x,"uid"));
149 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
151 # Output the group file.
153 for x in GroupMap.keys():
154 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
156 for I in GroupMap[x]:
157 Line = Line + ("%s%s" % (Comma,I));
161 Fdb.write("0%u %s" % (Counter,Line));
162 Fdb.write(".%s %s" % (x,Line));
163 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
164 Counter = Counter + 1;
166 # Oops, something unspeakable happened.
172 # Generate the email forwarding list
173 def GenForward(l,File,Allowed):
177 F = open(File + ".tmp","w");
180 # Fetch all the users
182 if PasswdAttrs == None:
185 # Write out the email address for each user
186 for x in PasswdAttrs:
187 if x[1].has_key("emailforward") == 0 or IsInGroup(x,Allowed) == 0:
189 Line = "%s: %s\n" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
192 # Oops, something unspeakable happened.
198 # Generate the anon XEarth marker file
199 def GenMarkers(l,File):
203 F = open(File + ".tmp","w");
206 # Fetch all the users
208 if PasswdAttrs == None:
211 # Write out the email address for each user
212 for x in PasswdAttrs:
213 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
216 F.write("%8s %8s \"\"\n"%(DecDegree(x,"latitude",1),DecDegree(x,"longitude",1)));
220 # Oops, something unspeakable happened.
226 # Connect to the ldap server
227 l = ldap.open(LDAPServer);
228 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
229 Pass = string.split(string.strip(F.readline())," ");
231 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
233 # Fetch all the groups
235 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
236 ["gid","gidnumber"]);
238 # Generate the GroupMap and GroupIDMap
240 if x[1].has_key("gidnumber") == 0:
242 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
244 # Fetch all the users
245 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
246 ["uid","uidnumber","gidnumber","supplementarygid",\
247 "gecos","loginshell","userpassword","shadowlastchange",\
248 "shadowmin","shadowmax","shadowwarning","shadowinactive",
249 "shadowexpire","emailforward","latitude","longitude"]);
251 # Open the control file
252 if len(sys.argv) == 1:
253 F = open(GenerateConf,"r");
255 F = open(sys.argv[1],"r")
260 Line = string.strip(Line);
266 Split = string.split(Line," ");
267 OutDir = GenerateDir + '/' + Split[0] + '/';
268 try: os.mkdir(OutDir);
271 # Get the group list and convert any named groups to numerics
275 if GroupIDMap.has_key(I):
276 GroupList[str(GroupIDMap[I])] = None;
278 GenPasswd(l,OutDir+"passwd",Split[1],GroupList);
279 GenGroup(l,OutDir+"group",GroupList);
280 GenShadow(l,OutDir+"shadow",GroupList);
281 GenForward(l,OutDir+"forward-alias",GroupList);
282 GenMarkers(l,OutDir+"markers");