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 *;
13 # See if this user is in the group list
14 def IsInGroup(DnRecord):
15 global Allowed,CurrentHost;
19 # See if the primary group is in the list
20 if Allowed.has_key(GetAttr(DnRecord,"gidnumber")) != 0:
23 # Check the host based ACL
24 if DnRecord[1].has_key("allowedhosts") != 0:
25 for I in DnRecord[1]["allowedhosts"]:
29 # See if there are supplementary groups
30 if DnRecord[1].has_key("supplementarygid") == 0:
33 # Check the supplementary groups
34 for I in DnRecord[1]["supplementarygid"]:
35 if Allowed.has_key(I):
44 try: os.remove(File + ".tmp");
46 try: os.remove(File + ".tdb.tmp");
52 os.rename(File + ".tmp",File);
55 os.rename(File + ".tdb.tmp",File+".tdb");
57 # Generate the password list
58 def GenPasswd(l,File,HomePrefix):
62 F = open(File + ".tmp","w");
63 Fdb = open(File + ".tdb.tmp","w");
67 if PasswdAttrs == None:
72 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
75 Line = "%s:x:%s:%s:%s:%s%s:%s\n" % (GetAttr(x,"uid"),\
76 GetAttr(x,"uidnumber"),GetAttr(x,"gidnumber"),\
77 GetAttr(x,"gecos"),HomePrefix,GetAttr(x,"uid"),\
78 GetAttr(x,"loginshell"));
80 Fdb.write("0%u %s" % (I,Line));
81 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
82 Fdb.write("=%s %s" % (GetAttr(x,"uidnumber"),Line));
85 # Oops, something unspeakable happened.
91 # Generate the shadow list
92 def GenShadow(l,File):
96 OldMask = os.umask(0077);
97 F = open(File + ".tmp","w",0600);
98 Fdb = open(File + ".tdb.tmp","w",0600);
101 # Fetch all the users
103 if PasswdAttrs == None:
107 for x in PasswdAttrs:
108 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
111 Pass = GetAttr(x,"userpassword");
112 if Pass[0:7] != "{crypt}":
116 Line = "%s:%s:%s:%s:%s:%s:%s:%s:\n" % (GetAttr(x,"uid"),\
117 Pass,GetAttr(x,"shadowlastchange"),\
118 GetAttr(x,"shadowmin"),GetAttr(x,"shadowmax"),\
119 GetAttr(x,"shadowwarning"),GetAttr(x,"shadowinactive"),\
120 GetAttr(x,"shadowexpire"));
122 Fdb.write("0%u %s" % (I,Line));
123 Fdb.write(".%s %s" % (GetAttr(x,"uid"),Line));
126 # Oops, something unspeakable happened.
132 # Generate the group list
133 def GenGroup(l,File):
137 F = open(File + ".tmp","w");
138 Fdb = open(File + ".tdb.tmp","w");
140 # Generate the GroupMap
142 for x in GroupIDMap.keys():
145 # Fetch all the users
147 if PasswdAttrs == None:
150 # Sort them into a list of groups having a set of users
151 for x in PasswdAttrs:
152 if x[1].has_key("uidnumber") == 0 or IsInGroup(x) == 0:
154 if x[1].has_key("supplementarygid") == 0:
157 for I in x[1]["supplementarygid"]:
158 if GroupMap.has_key(I):
159 GroupMap[I].append(GetAttr(x,"uid"));
161 print "Group does not exist ",I,"but",GetAttr(x,"uid"),"is in it";
163 # Output the group file.
165 for x in GroupMap.keys():
166 Line = "%s:x:%u:" % (x,GroupIDMap[x]);
168 for I in GroupMap[x]:
169 Line = Line + ("%s%s" % (Comma,I));
173 Fdb.write("0%u %s" % (Counter,Line));
174 Fdb.write(".%s %s" % (x,Line));
175 Fdb.write("=%u %s" % (GroupIDMap[x],Line));
176 Counter = Counter + 1;
178 # Oops, something unspeakable happened.
184 # Generate the email forwarding list
185 def GenForward(l,File):
189 OldMask = os.umask(0022);
190 F = open(File + ".tmp","w",0644);
194 # Fetch all the users
196 if PasswdAttrs == None:
199 # Write out the email address for each user
200 for x in PasswdAttrs:
201 if x[1].has_key("emailforward") == 0 or IsInGroup(x) == 0:
203 Line = "%s: %s\n" % (GetAttr(x,"uid"),GetAttr(x,"emailforward"));
206 # Oops, something unspeakable happened.
212 # Generate the anon XEarth marker file
213 def GenMarkers(l,File):
217 F = open(File + ".tmp","w");
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("latitude") == 0 or x[1].has_key("longitude") == 0:
230 F.write("%8s %8s \"\"\n"%(DecDegree(x,"latitude",1),DecDegree(x,"longitude",1)));
234 # Oops, something unspeakable happened.
240 # Connect to the ldap server
241 l = ldap.open(LDAPServer);
242 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
243 Pass = string.split(string.strip(F.readline())," ");
245 l.simple_bind_s("uid="+Pass[0]+","+BaseDn,Pass[1]);
247 # Fetch all the groups
249 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=*",\
250 ["gid","gidnumber"]);
252 # Generate the GroupMap and GroupIDMap
254 if x[1].has_key("gidnumber") == 0:
256 GroupIDMap[x[1]["gid"][0]] = int(x[1]["gidnumber"][0]);
258 # Fetch all the users
259 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=*",\
260 ["uid","uidnumber","gidnumber","supplementarygid",\
261 "gecos","loginshell","userpassword","shadowlastchange",\
262 "shadowmin","shadowmax","shadowwarning","shadowinactive",
263 "shadowexpire","emailforward","latitude","longitude",\
266 # Open the control file
267 if len(sys.argv) == 1:
268 F = open(GenerateConf,"r");
270 F = open(sys.argv[1],"r")
275 Line = string.strip(Line);
281 Split = string.split(Line," ");
282 OutDir = GenerateDir + '/' + Split[0] + '/';
283 try: os.mkdir(OutDir);
286 # Get the group list and convert any named groups to numerics
290 if GroupIDMap.has_key(I):
291 GroupList[str(GroupIDMap[I])] = None;
293 global Allowed,CurrentHost;
295 CurrentHost = Split[0];
297 GenPasswd(l,OutDir+"passwd",Split[1]);
298 GenGroup(l,OutDir+"group");
299 GenShadow(l,OutDir+"shadow");
300 GenForward(l,OutDir+"forward-alias");
301 GenMarkers(l,OutDir+"markers");