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 OldMask = os.umask(0022);
178 F = open(File + ".tmp","w",0644);
182 # Fetch all the users
184 if PasswdAttrs == None:
187 # Write out the email address for each user
188 for x in PasswdAttrs:
189 if x[1].has_key("emailforward") == 0 or IsInGroup(x,Allowed) == 0:
191 Line = "%s: %s\n" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
194 # Oops, something unspeakable happened.
200 # Generate the anon XEarth marker file
201 def GenMarkers(l,File):
205 F = open(File + ".tmp","w");
208 # Fetch all the users
210 if PasswdAttrs == None:
213 # Write out the email address for each user
214 for x in PasswdAttrs:
215 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
218 F.write("%8s %8s \"\"\n"%(DecDegree(x,"latitude",1),DecDegree(x,"longitude",1)));
222 # Oops, something unspeakable happened.
228 # Connect to the ldap server
229 l = ldap.open(LDAPServer);
230 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
231 Pass = string.split(string.strip(F.readline())," ");
233 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
235 # Fetch all the groups
237 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
238 ["gid","gidnumber"]);
240 # Generate the GroupMap and GroupIDMap
242 if x[1].has_key("gidnumber") == 0:
244 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
246 # Fetch all the users
247 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
248 ["uid","uidnumber","gidnumber","supplementarygid",\
249 "gecos","loginshell","userpassword","shadowlastchange",\
250 "shadowmin","shadowmax","shadowwarning","shadowinactive",
251 "shadowexpire","emailforward","latitude","longitude"]);
253 # Open the control file
254 if len(sys.argv) == 1:
255 F = open(GenerateConf,"r");
257 F = open(sys.argv[1],"r")
262 Line = string.strip(Line);
268 Split = string.split(Line," ");
269 OutDir = GenerateDir + '/' + Split[0] + '/';
270 try: os.mkdir(OutDir);
273 # Get the group list and convert any named groups to numerics
277 if GroupIDMap.has_key(I):
278 GroupList[str(GroupIDMap[I])] = None;
280 GenPasswd(l,OutDir+"passwd",Split[1],GroupList);
281 GenGroup(l,OutDir+"group",GroupList);
282 GenShadow(l,OutDir+"shadow",GroupList);
283 GenForward(l,OutDir+"forward-alias",GroupList);
284 GenMarkers(l,OutDir+"markers");