4 # Copyright (c) 1999-2000 Jason Gunthorpe <jgg@debian.org>
5 # Copyright (c) 2001-2003 James Troup <troup@debian.org>
6 # Copyright (c) 2004 Joey Schulze <joey@infodrom.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 import string, re, time, ldap, getopt, sys, os, pwd;
23 from userdir_ldap import *;
24 from userdir_gpg import *;
26 # This tries to search for a free UID. There are two possible ways to do
27 # this, one is to fetch all the entires and pick the highest, the other
28 # is to randomly guess uids until one is free. This uses the former.
29 # Regrettably ldap doesn't have an integer attribute comparision function
30 # so we can only cut the search down slightly
32 # [JT] This is broken with Woody LDAP and the Schema; for now just
33 # search through all UIDs.
35 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
36 "uidNumber=*",["uidNumber"]);
39 ID = int(GetAttr(I,"uidNumber","0"));
42 return HighestUID + 1;
45 AdminUser = pwd.getpwuid(os.getuid())[0];
49 OldGPGKeyRings = GPGKeyRings;
50 userdir_gpg.GPGKeyRings = [];
51 (options, arguments) = getopt.getopt(sys.argv[1:], "u:ma")
52 for (switch, val) in options:
55 elif (switch == '-m'):
57 elif (switch == '-a'):
58 userdir_gpg.GPGKeyRings = OldGPGKeyRings;
60 print "Accessing LDAP directory as '" + AdminUser + "'";
62 Password = getpass(AdminUser + "'s password: ");
64 if len(Password) == 0:
67 l = ldap.open(LDAPServer);
68 UserDn = "uid=" + AdminUser + "," + BaseDn;
70 # Connect to the ldap server
72 l.simple_bind_s(UserDn,Password);
73 except ldap.INVALID_CREDENTIALS:
77 # Locate the key of the user we are adding
78 SetKeyrings(["/org/keyring.debian.org/keyrings/debian-keyring.gpg"])
80 Foo = raw_input("Who are you going to add (for a GPG search)? ");
84 Keys = GPGKeySearch(Foo);
87 print "Sorry, that search did not turn up any keys."
88 print "Has it been added to the Debian keyring already?"
91 print "Sorry, more than one key was found, please specify the key to use by\nfingerprint:";
97 print "A matching key was found:"
98 GPGPrintKeyInfo(Keys[0]);
101 # Crack up the email address from the key into a best guess
102 # first/middle/last name
103 Addr = SplitEmail(Keys[0][2]);
104 (cn,mn,sn) = NameSplit(re.sub('["]','',Addr[0]))
105 email = Addr[1] + '@' + Addr[2];
109 gidNumber = str(DefaultGID);
112 # Decide if we should use IDEA encryption
114 while len(Keys[0][1]) < 40:
115 Res = raw_input("Use PGP2.x compatibility [no]? ");
122 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + Keys[0][1]);
124 print "*** This key already belongs to",GetAttr(Attrs[0],"uid");
125 account = GetAttr(Attrs[0],"uid");
127 # Try to get a uniq account name
130 Res = raw_input("Login account [" + account + "]? ");
133 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
135 privsub = "%s@debian.org"%(account);
137 Res = raw_input("That account already exists, update [No/yes]? ");
139 # Update mode, fetch the default values from the directory
141 privsub = GetAttr(Attrs[0],"privateSub");
142 gidNumber = GetAttr(Attrs[0],"gidNumber");
143 uidNumber = GetAttr(Attrs[0],"uidNumber");
144 email = GetAttr(Attrs[0],"emailForward");
145 cn = GetAttr(Attrs[0],"cn");
146 sn = GetAttr(Attrs[0],"sn");
147 mn = GetAttr(Attrs[0],"mn");
148 if privsub == None or privsub == "":
152 # Prompt for the first/last name and email address
153 Res = raw_input("First name [" + cn + "]? ");
156 Res = raw_input("Middle name [" + mn + "]? ");
159 Res = raw_input("Last name [" + sn + "]? ");
162 Res = raw_input("Email forwarding address [" + email + "]? ");
166 # Debian-Private subscription
167 Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
172 Res = raw_input("Group ID Number [" + gidNumber + "]? ");
174 gidNumber = Group2GID(Res);
178 uidNumber = GetFreeID(l);
180 # Generate a random password
181 if Update == 0 or ForceMail == 1:
182 Password = raw_input("User's Password (Enter for random)? ");
185 print "Randomizing and encrypting password"
186 Password = GenPass();
187 Pass = HashPass(Password);
189 # Use GPG to encrypt it, pass the fingerprint to ID it
190 CryptedPass = GPGEncrypt("Your new password is '" + Password + "'\n",\
191 "0x"+Keys[0][1],UsePGP2);
193 if CryptedPass == None:
194 raise "Error","Password Encryption failed"
196 Pass = HashPass(Password);
197 CryptedPass = "Your password has been set to the previously agreed value.";
202 # Now we have all the bits of information.
204 FullName = "%s %s %s" % (cn,mn,sn);
206 FullName = "%s %s" % (cn,sn);
207 print "------------";
208 print "Final information collected:"
209 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
210 print " Assigned UID:",uidNumber," GID:", gidNumber;
211 print " Email forwarded to:",email;
212 print " Private Subscription:",privsub;
213 print " GECOS Field: \"%s,,,,\"" % (FullName);
214 print " Login Shell: /bin/bash";
215 print " Key Fingerprint:",Keys[0][1];
216 Res = raw_input("Continue [No/yes]? ");
220 # Initialize the substitution Map
222 Subst["__REALNAME__"] = FullName;
223 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
224 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
225 Subst["__LOGIN__"] = account;
226 Subst["__PRIVATE__"] = privsub;
227 Subst["__EMAIL__"] = email;
228 Subst["__PASSWORD__"] = CryptedPass;
230 # Submit the modification request
231 Dn = "uid=" + account + "," + BaseDn;
232 print "Updating LDAP directory..",
237 Details = [("uid",account),
239 ("top","inetOrgPerson","debianAccount","shadowAccount","debianDeveloper")),
240 ("uidNumber",str(uidNumber)),
241 ("gidNumber",str(gidNumber)),
242 ("gecos",FullName+",,,,"),
243 ("loginShell","/bin/bash"),
244 ("keyFingerPrint",Keys[0][1]),
247 ("emailForward",email),
248 ("shadowLastChange",str(int(time.time()/24/60/60))),
250 ("shadowMax","99999"),
251 ("shadowWarning","7"),
252 ("userPassword","{crypt}"+Pass)];
254 Details.append(("mn",mn));
256 Details.append(("privateSub",privsub))
260 Rec = [(ldap.MOD_REPLACE,"uidNumber",str(uidNumber)),
261 (ldap.MOD_REPLACE,"gidNumber",str(gidNumber)),
262 (ldap.MOD_REPLACE,"gecos",FullName+",,,,"),
263 (ldap.MOD_REPLACE,"loginShell","/bin/bash"),
264 (ldap.MOD_REPLACE,"keyFingerPrint",Keys[0][1]),
265 (ldap.MOD_REPLACE,"cn",cn),
266 (ldap.MOD_REPLACE,"mn",mn),
267 (ldap.MOD_REPLACE,"sn",sn),
268 (ldap.MOD_REPLACE,"emailForward",email),
269 (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
270 (ldap.MOD_REPLACE,"shadowMin","0"),
271 (ldap.MOD_REPLACE,"shadowMax","99999"),
272 (ldap.MOD_REPLACE,"shadowWarning","7"),
273 (ldap.MOD_REPLACE,"shadowInactive",""),
274 (ldap.MOD_REPLACE,"shadowExpire","")];
276 Rec.append((ldap.MOD_REPLACE,"privateSub",privsub));
278 Rec.append((ldap.MOD_REPLACE,"userPassword","{crypt}"+Pass));
284 # Abort email sends for an update operation
285 if Update == 1 and ForceMail == 0:
286 print "Account is not new, Not sending mails"
289 # Send the Welcome message
290 print "Sending Welcome Email"
291 Reply = TemplateSubst(Subst,open(TemplatesDir+"/welcome-message-"+gidNumber,"r").read());
292 Child = os.popen("/usr/sbin/sendmail -t","w");
293 #Child = os.popen("cat","w");
295 if Child.close() != None:
296 raise Error, "Sendmail gave a non-zero return code";