A set of copyright headers
[mirror/userdir-ldap.git] / ud-useradd
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
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>
7 #   Copyright (c) 2008,2009,2010 Peter Palfrader <peter@palfrader.org>
8 #
9 #   This program is free software; you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation; either version 2 of the License, or
12 #   (at your option) any later version.
13 #
14 #   This program is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #   GNU General Public License for more details.
18 #
19 #   You should have received a copy of the GNU General Public License
20 #   along with this program; if not, write to the Free Software
21 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 import re, time, ldap, getopt, sys, os, pwd;
24 import email.Header
25
26 from userdir_ldap import *;
27 from userdir_gpg import *;
28
29 HavePrivateList = getattr(ConfModule, "haveprivatelist", True)
30
31 # This tries to search for a free UID. There are two possible ways to do
32 # this, one is to fetch all the entires and pick the highest, the other
33 # is to randomly guess uids until one is free. This uses the former.
34 # Regrettably ldap doesn't have an integer attribute comparision function
35 # so we can only cut the search down slightly
36
37 # [JT] This is broken with Woody LDAP and the Schema; for now just
38 #      search through all UIDs.
39 def GetFreeID(l):
40    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
41                       "uidNumber=*",["uidNumber", "gidNumber"]);
42    HighestUID = 0;
43    gids = [];
44    for I in Attrs:
45       ID = int(GetAttr(I,"uidNumber","0"));
46       gids.append(int(GetAttr(I, "gidNumber","0")))
47       if ID > HighestUID:
48          HighestUID = ID;
49
50    resGID = HighestUID + 1;
51    while resGID in gids:
52       resGID += 1
53
54    return (HighestUID + 1, resGID);
55
56 # Main starts here
57 AdminUser = pwd.getpwuid(os.getuid())[0];
58
59 # Process options
60 ForceMail = 0;
61 NoAutomaticIDs = 0;
62 OldGPGKeyRings = GPGKeyRings;
63 userdir_gpg.GPGKeyRings = [];
64 (options, arguments) = getopt.getopt(sys.argv[1:], "u:man")
65 for (switch, val) in options:
66    if (switch == '-u'):
67       AdminUser = val;
68    elif (switch == '-m'):
69       ForceMail = 1;
70    elif (switch == '-a'):
71       userdir_gpg.GPGKeyRings = OldGPGKeyRings;
72    elif (switch == '-n'):
73       NoAutomaticIDs = 1;
74
75 l = passwdAccessLDAP(BaseDn, AdminUser)
76
77 # Locate the key of the user we are adding
78 SetKeyrings(ConfModule.add_keyrings.split(":"))
79 while (1):
80    Foo = raw_input("Who are you going to add (for a GPG search)? ");
81    if Foo == "":
82       sys.exit(0);
83
84    Keys = GPGKeySearch(Foo);
85
86    if len(Keys) == 0:
87       print "Sorry, that search did not turn up any keys."
88       print "Has it been added to the Debian keyring already?"
89       continue;
90    if len(Keys) > 1:
91       print "Sorry, more than one key was found, please specify the key to use by\nfingerprint:";
92       for i in Keys:
93          GPGPrintKeyInfo(i);
94       continue;
95
96    print
97    print "A matching key was found:"
98    GPGPrintKeyInfo(Keys[0]);
99    break;
100
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 emailaddr = Addr[1] + '@' + Addr[2];
106 account = Addr[1];
107
108 privsub = emailaddr
109 gidNumber = 0;
110 uidNumber = 0;
111
112 # Decide if we should use IDEA encryption
113 UsePGP2 = 0;
114 while len(Keys[0][1]) < 40:
115    Res = raw_input("Use PGP2.x compatibility [No/yes]? ");
116    if Res == "yes":
117       UsePGP2 = 1;
118       break;
119    if Res == "":
120       break;
121
122 Update = 0
123 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + Keys[0][1]);
124 if len(Attrs) != 0:
125    print "*** This key already belongs to",GetAttr(Attrs[0],"uid");
126    account = GetAttr(Attrs[0],"uid");
127    Update = 1
128
129 # Try to get a uniq account name
130 while 1:
131    if Update == 0:
132       Res = raw_input("Login account [" + account + "]? ");
133       if Res != "":
134          account = Res;
135    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
136    if len(Attrs) == 0:
137       privsub = "%s@debian.org"%(account);
138       break;
139    Res = raw_input("That account already exists, update [No/yes]? ");
140    if Res == "yes":
141       # Update mode, fetch the default values from the directory
142       Update = 1;
143       privsub = GetAttr(Attrs[0],"privateSub");
144       gidNumber = GetAttr(Attrs[0],"gidNumber");
145       uidNumber = GetAttr(Attrs[0],"uidNumber");
146       emailaddr = GetAttr(Attrs[0],"emailForward");
147       cn = GetAttr(Attrs[0],"cn");
148       sn = GetAttr(Attrs[0],"sn");
149       mn = GetAttr(Attrs[0],"mn");
150       if privsub == None or privsub == "":
151          privsub = " ";
152       break;
153    else:
154       sys.exit(1)
155
156 # Prompt for the first/last name and email address
157 Res = raw_input("First name [" + cn + "]? ");
158 if Res != "":
159    cn = Res;
160 Res = raw_input("Middle name [" + mn + "]? ");
161 if Res == " ":
162    mn = ""
163 elif Res != "":
164    mn = Res;
165 Res = raw_input("Last name [" + sn + "]? ");
166 if Res != "":
167    sn = Res;
168 Res = raw_input("Email forwarding address [" + emailaddr + "]? ");
169 if Res != "":
170    emailaddr = Res;
171
172 # Debian-Private subscription
173 if HavePrivateList:
174    Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
175    if Res != "":
176       privsub = Res;
177 else:
178    privsub = " "
179
180 (uidNumber, generatedGID) = GetFreeID(l)
181 if not gidNumber:
182    gidNumber = DefaultGID
183 UserGroup = 0
184
185 if NoAutomaticIDs:
186    # UID
187    if not Update:
188       Res = raw_input("User ID Number [%s]? " % (uidNumber));
189       if Res != "":
190          uidNumber = Res;
191    
192    # GID
193    Res = raw_input("Group ID Number (default group is %s, new usergroup %s) [%s]" % (DefaultGID, generatedGID, gidNumber));
194    if Res != "":
195       if Res.isdigit():
196          gidNumber = int(Res);
197       else:
198          gidNumber = Group2GID(l, Res);
199    
200    if gidNumber == generatedGID:
201       UserGroup = 1
202
203 # Generate a random password
204 if Update == 0 or ForceMail == 1:
205    Password = raw_input("User's Password (Enter for random)? ");
206
207    if Password == "":
208       print "Randomizing and encrypting password"
209       Password = GenPass();
210       Pass = HashPass(Password);
211
212       # Use GPG to encrypt it, pass the fingerprint to ID it
213       CryptedPass = GPGEncrypt("Your new password is '" + Password + "'\n",\
214                                "0x"+Keys[0][1],UsePGP2);
215       Password = None;
216       if CryptedPass == None:
217         raise "Error","Password Encryption failed"
218    else:
219       Pass = HashPass(Password);
220       CryptedPass = "Your password has been set to the previously agreed value.";
221 else:
222    CryptedPass = "";
223    Pass = None;
224
225 # Now we have all the bits of information.
226 if mn != "":
227    FullName = "%s %s %s" % (cn,mn,sn);
228 else:
229    FullName = "%s %s" % (cn,sn);
230 print "------------";
231 print "Final information collected:"
232 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
233 print "   Assigned UID:",uidNumber," GID:", gidNumber;
234 print "   Email forwarded to:",emailaddr
235 if HavePrivateList:
236    print "   Private Subscription:",privsub;
237 print "   GECOS Field: \"%s,,,,\"" % (FullName);
238 print "   Login Shell: /bin/bash";
239 print "   Key Fingerprint:",Keys[0][1];
240 Res = raw_input("Continue [No/yes]? ");
241 if Res != "yes":
242    sys.exit(1);
243
244 # Initialize the substitution Map
245 Subst = {}
246
247 encrealname = ''
248 try:
249   encrealname = FullName.decode('us-ascii')
250 except UnicodeError:
251   encrealname = str(email.Header.Header(FullName, 'utf-8', 200))
252
253 Subst["__ENCODED_REALNAME__"] = encrealname
254 Subst["__REALNAME__"] = FullName;
255 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
256 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
257 Subst["__LOGIN__"] = account;
258 Subst["__PRIVATE__"] = privsub;
259 Subst["__EMAIL__"] = emailaddr
260 Subst["__PASSWORD__"] = CryptedPass;
261
262 # Submit the modification request
263 Dn = "uid=" + account + "," + BaseDn;
264 print "Updating LDAP directory..",
265 sys.stdout.flush();
266
267 if Update == 0:
268    # New account
269    Details = [("uid",account),
270               ("objectClass", UserObjectClasses),
271               ("uidNumber",str(uidNumber)),
272               ("gidNumber",str(gidNumber)),
273               ("gecos",FullName+",,,,"),
274               ("loginShell","/bin/bash"),
275               ("keyFingerPrint",Keys[0][1]),
276               ("cn",cn),
277               ("sn",sn),
278               ("emailForward",emailaddr),
279               ("shadowLastChange",str(int(time.time()/24/60/60))),
280               ("shadowMin","0"),
281               ("shadowMax","99999"),
282               ("shadowWarning","7"),
283               ("userPassword","{crypt}"+Pass)];
284    if mn:
285       Details.append(("mn",mn));
286    if privsub != " ":
287       Details.append(("privateSub",privsub))
288    l.add_s(Dn,Details);
289
290    #Add user group if needed, then the actual user:
291    if UserGroup == 1:
292       Dn = "gid=" + account + "," + BaseDn;
293       l.add_s(Dn,[("gid",account), ("gidNumber",str(gidNumber)), ("objectClass", GroupObjectClasses)])
294 else:
295    # Modification
296    Rec = [(ldap.MOD_REPLACE,"uidNumber",str(uidNumber)),
297           (ldap.MOD_REPLACE,"gidNumber",str(gidNumber)),
298           (ldap.MOD_REPLACE,"gecos",FullName+",,,,"),
299           (ldap.MOD_REPLACE,"loginShell","/bin/bash"),
300           (ldap.MOD_REPLACE,"keyFingerPrint",Keys[0][1]),
301           (ldap.MOD_REPLACE,"cn",cn),
302           (ldap.MOD_REPLACE,"mn",mn),
303           (ldap.MOD_REPLACE,"sn",sn),
304           (ldap.MOD_REPLACE,"emailForward",emailaddr),
305           (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
306           (ldap.MOD_REPLACE,"shadowMin","0"),
307           (ldap.MOD_REPLACE,"shadowMax","99999"),
308           (ldap.MOD_REPLACE,"shadowWarning","7"),
309           (ldap.MOD_REPLACE,"shadowInactive",""),
310           (ldap.MOD_REPLACE,"shadowExpire","")];
311    if privsub != " ":
312       Rec.append((ldap.MOD_REPLACE,"privateSub",privsub));
313    if Pass != None:
314       Rec.append((ldap.MOD_REPLACE,"userPassword","{crypt}"+Pass));
315    # Do it
316    l.modify_s(Dn,Rec);
317
318 print;
319
320 # Abort email sends for an update operation
321 if Update == 1 and ForceMail == 0:
322    print "Account is not new, Not sending mails"
323    sys.exit(0);
324
325 # Send the Welcome message
326 print "Sending Welcome Email"
327 templatepath = TemplatesDir + "/welcome-message-%d" % int(gidNumber)
328 if not os.path.exists(templatepath):
329    templatepath = TemplatesDir + "/welcome-message"
330 Reply = TemplateSubst(Subst,open(templatepath, "r").read())
331 Child = os.popen("/usr/sbin/sendmail -t","w");
332 #Child = os.popen("cat","w");
333 Child.write(Reply);
334 if Child.close() != None:
335    raise Error, "Sendmail gave a non-zero return code";