4 import string, re, time, ldap, getopt, sys, os, pwd;
5 from userdir_ldap import *;
6 from userdir_gpg import *;
8 # This tries to search for a free UID. There are two possible ways to do
9 # this, one is to fetch all the entires and pick the highest, the other
10 # is to randomly guess uids until one is free. This uses the former.
11 # Regrettably ldap doesn't have an integer attribute comparision function
12 # so we can only cut the search down slightly
14 # [JT] This is broken with Woody LDAP and the Schema; for now just
15 # search through all UIDs.
17 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
18 "uidNumber=*",["uidNumber"]);
21 ID = int(GetAttr(I,"uidNumber","0"));
24 return HighestUID + 1;
27 AdminUser = pwd.getpwuid(os.getuid())[0];
31 OldGPGKeyRings = GPGKeyRings;
32 userdir_gpg.GPGKeyRings = [];
33 (options, arguments) = getopt.getopt(sys.argv[1:], "u:ma")
34 for (switch, val) in options:
37 elif (switch == '-m'):
39 elif (switch == '-a'):
40 userdir_gpg.GPGKeyRings = OldGPGKeyRings;
42 print "Accessing LDAP directory as '" + AdminUser + "'";
43 Password = getpass(AdminUser + "'s password: ");
45 # Connect to the ldap server
46 l = ldap.open(LDAPServer);
47 UserDn = "uid=" + AdminUser + "," + BaseDn;
48 l.simple_bind_s(UserDn,Password);
50 # Locate the key of the user we are adding
51 GPGBasicOptions[0] = "--batch" # Permit loading of the config file
53 Foo = raw_input("Who are you going to add (for a GPG search)? ");
57 Keys = GPGKeySearch(Foo);
60 print "Sorry, that search did not turn up any keys";
63 print "Sorry, more than one key was found, please specify the key to use by\nfingerprint:";
69 print "A matching key was found:"
70 GPGPrintKeyInfo(Keys[0]);
73 # Crack up the email address from the key into a best guess
74 # first/middle/last name
75 Addr = SplitEmail(Keys[0][2]);
76 (cn,mn,sn) = NameSplit(re.sub('["]','',Addr[0]))
77 email = Addr[1] + '@' + Addr[2];
81 gidNumber = str(DefaultGID);
84 # Decide if we should use IDEA encryption
86 while len(Keys[0][1]) < 40:
87 Res = raw_input("Use PGP2.x compatibility [no]? ");
94 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + Keys[0][1]);
96 print "*** This key already belongs to",GetAttr(Attrs[0],"uid");
97 account = GetAttr(Attrs[0],"uid");
99 # Try to get a uniq account name
102 Res = raw_input("Login account [" + account + "]? ");
105 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
107 privsub = "%s@debian.org"%(account);
109 Res = raw_input("That account already exists, update [no]? ");
111 # Update mode, fetch the default values from the directory
113 privsub = GetAttr(Attrs[0],"privateSub");
114 gidNumber = GetAttr(Attrs[0],"gidNumber");
115 uidNumber = GetAttr(Attrs[0],"uidNumber");
116 email = GetAttr(Attrs[0],"emailForward");
117 cn = GetAttr(Attrs[0],"cn");
118 sn = GetAttr(Attrs[0],"sn");
119 mn = GetAttr(Attrs[0],"mn");
120 if privsub == None or privsub == "":
124 # Prompt for the first/last name and email address
125 Res = raw_input("First name [" + cn + "]? ");
128 Res = raw_input("Middle name [" + mn + "]? ");
131 Res = raw_input("Last name [" + sn + "]? ");
134 Res = raw_input("Email forwarding address [" + email + "]? ");
138 # Debian-Private subscription
139 Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
144 Res = raw_input("Group ID Number [" + gidNumber + "]? ");
150 uidNumber = GetFreeID(l);
152 # Generate a random password
153 if Update == 0 or ForceMail == 1:
154 Password = raw_input("User's Password (Enter for random)? ");
157 print "Randomizing and encrypting password"
158 Password = GenPass();
159 Pass = HashPass(Password);
161 # Use GPG to encrypt it, pass the fingerprint to ID it
162 CryptedPass = GPGEncrypt("Your new password is '" + Password + "'\n",\
163 "0x"+Keys[0][1],UsePGP2);
165 if CryptedPass == None:
166 raise "Error","Password Encryption failed"
168 Pass = HashPass(Password);
169 CryptedPass = "Your password has been set to the previously agreed value.";
174 # Now we have all the bits of information.
176 FullName = "%s %s %s" % (cn,mn,sn);
178 FullName = "%s %s" % (cn,sn);
179 print "------------";
180 print "Final information collected:"
181 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
182 print " Assigned UID:",uidNumber," GID:", gidNumber;
183 print " Email forwarded to:",email;
184 print " Private Subscription:",privsub;
185 print " GECOS Field: \"%s,,,,\"" % (FullName);
186 print " Login Shell: /bin/bash";
187 print " Key Fingerprint:",Keys[0][1];
188 Res = raw_input("Continue [no]? ");
192 # Initialize the substitution Map
194 Subst["__REALNAME__"] = FullName;
195 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
196 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
197 Subst["__LOGIN__"] = account;
198 Subst["__PRIVATE__"] = privsub;
199 Subst["__EMAIL__"] = email;
200 Subst["__PASSWORD__"] = CryptedPass;
202 # Submit the modification request
203 Dn = "uid=" + account + "," + BaseDn;
204 print "Updating LDAP directory..",
209 Details = [("uid",account),
211 ("top","inetOrgPerson","debianAccount","shadowAccount","debianDeveloper")),
212 ("uidNumber",str(uidNumber)),
213 ("gidNumber",str(gidNumber)),
214 ("gecos",FullName+",,,,"),
215 ("loginShell","/bin/bash"),
216 ("keyFingerPrint",Keys[0][1]),
219 ("emailForward",email),
220 ("shadowLastChange",str(int(time.time()/24/60/60))),
222 ("shadowMax","99999"),
223 ("shadowWarning","7"),
224 ("privateSub",privsub),
225 ("userPassword","{crypt}"+Pass)];
227 Details.append(("mn",mn));
231 Rec = [(ldap.MOD_REPLACE,"uidNumber",str(uidNumber)),
232 (ldap.MOD_REPLACE,"gidNumber",str(gidNumber)),
233 (ldap.MOD_REPLACE,"gecos",FullName+",,,,"),
234 (ldap.MOD_REPLACE,"loginShell","/bin/bash"),
235 (ldap.MOD_REPLACE,"keyFingerPrint",Keys[0][1]),
236 (ldap.MOD_REPLACE,"cn",cn),
237 (ldap.MOD_REPLACE,"mn",mn),
238 (ldap.MOD_REPLACE,"sn",sn),
239 (ldap.MOD_REPLACE,"emailForward",email),
240 (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
241 (ldap.MOD_REPLACE,"shadowMin","0"),
242 (ldap.MOD_REPLACE,"shadowMax","99999"),
243 (ldap.MOD_REPLACE,"shadowWarning","7"),
244 (ldap.MOD_REPLACE,"shadowInactive",""),
245 (ldap.MOD_REPLACE,"shadowExpire","")];
247 Rec.append((ldap.MOD_REPLACE,"privateSub",privsub));
249 Rec.append((ldap.MOD_REPLACE,"userPassword","{crypt}"+Pass));
255 # Abort email sends for an update operation
256 if Update == 1 and ForceMail == 0:
257 print "Account is not new, Not sending mails"
260 # Send the Welcome message
261 print "Sending Welcome Email"
262 Reply = TemplateSubst(Subst,open(TemplatesDir+"/welcome-message-"+gidNumber,"r").read());
263 Child = os.popen("/usr/sbin/sendmail -t","w");
264 #Child = os.popen("cat","w");
266 if Child.close() != None:
267 raise Error, "Sendmail gave a non-zero return code";