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 formar.
11 # Regrettably ldap doesn't have an integer attribute comparision function
12 # so we can only cut the search down slightly
15 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
16 "uidnumber>="+str(HighestUID),["uidnumber"]);
19 ID = int(GetAttr(I,"uidnumber","0"));
22 return HighestUID + 1;
25 AdminUser = pwd.getpwuid(os.getuid())[0];
29 (options, arguments) = getopt.getopt(sys.argv[1:], "u:m")
30 for (switch, val) in options:
33 elif (switch == '-m'):
36 print "Accessing LDAP directory as '" + AdminUser + "'";
37 Password = getpass(AdminUser + "'s password: ");
39 # Connect to the ldap server
40 l = ldap.open(LDAPServer);
41 UserDn = "uid=" + AdminUser + "," + BaseDn;
42 l.simple_bind_s(UserDn,Password);
44 # Locate the key of the user we are adding
45 GPGBasicOptions[0] = "--batch" # Permit loading of the config file
47 Foo = raw_input("Who are you going to add (for a GPG search)? ");
51 Keys = GPGKeySearch(Foo);
54 print "Sorry, that search did not turn up any keys";
57 print "Sorry, more than one key was found, please specify the key to use by\nfingerprint:";
63 print "A matching key was found:"
64 GPGPrintKeyInfo(Keys[0]);
67 # Crack up the email address from the key into a best guess
68 # first/middle/last name
69 Addr = SplitEmail(Keys[0][2]);
70 (cn,mn,sn) = NameSplit(re.sub('["]','',Addr[0]))
71 email = Addr[1] + '@' + Addr[2];
75 gidnumber = str(DefaultGID);
78 # Decide if we should use IDEA encryption
80 while len(Keys[0][1]) < 40:
81 Res = raw_input("Use PGP2.x compatibility [no]? ");
88 # Try to get a uniq account name
91 Res = raw_input("Login account [" + account + "]? ");
94 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
97 Res = raw_input("That account already exists, update [no]? ");
99 # Update mode, fetch the default values from the directory
101 privsub = GetAttr(Attrs[0],"privatesub");
102 gidnumber = GetAttr(Attrs[0],"gidnumber");
103 uidnumber = GetAttr(Attrs[0],"uidnumber");
104 email = GetAttr(Attrs[0],"emailforward");
105 cn = GetAttr(Attrs[0],"cn");
106 sn = GetAttr(Attrs[0],"sn");
107 mn = GetAttr(Attrs[0],"mn");
108 if privsub == None or privsub == "":
112 # Prompt for the first/last name and email address
113 Res = raw_input("First name [" + cn + "]? ");
116 Res = raw_input("Middle name [" + mn + "]? ");
119 Res = raw_input("Last name [" + sn + "]? ");
122 Res = raw_input("Email forwarding address [" + email + "]? ");
126 # Debian-Private subscription
127 Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
132 Res = raw_input("Group ID Number [" + gidnumber + "]? ");
138 uidnumber = GetFreeID(l);
140 # Generate a random password
141 if Update == 0 or ForceMail == 1:
142 Password = raw_input("User's Password (Enter for random)? ");
145 print "Randomizing and encrypting password"
146 Password = GenPass();
147 Pass = HashPass(Password);
148 print "PASS: ", Password;
150 # Use GPG to encrypt it, pass the fingerprint to ID it
151 CryptedPass = GPGEncrypt("Your new password is '" + Password + "'\n",\
152 "0x"+Keys[0][1],UsePGP2);
154 if CryptedPass == None:
155 raise "Error","Password Encryption failed"
157 Pass = HashPass(Password);
158 CryptedPass = "Your password has been set to the previously agreed value.";
163 # Now we have all the bits of information.
165 FullName = "%s %s %s" % (cn,mn,sn);
167 FullName = "%s %s" % (cn,sn);
168 print "------------";
169 print "Final information collected:"
170 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
171 print " Assigned UID:",uidnumber," GID:", gidnumber;
172 print " Email forwarded to:",email;
173 print " Private Subscription:",privsub;
174 print " GECOS Field: \"%s,,,,\"" % (FullName);
175 print " Login Shell: /bin/bash";
176 print " Key Fingerprint:",Keys[0][1];
177 Res = raw_input("Continue [no]? ");
181 # Initialize the substitution Map
183 Subst["__REALNAME__"] = FullName;
184 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
185 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
186 Subst["__LOGIN__"] = account;
187 Subst["__PRIVATE__"] = privsub;
188 Subst["__EMAIL__"] = email;
189 Subst["__PASSWORD__"] = CryptedPass;
190 Subst["__LISTPASS__"] = string.strip(open(pwd.getpwuid(os.getuid())[5]+"/.debian-lists_passwd","r").read());
192 # Generate the LDAP request
193 Rec = [(ldap.MOD_REPLACE,"uid",account),
194 (ldap.MOD_REPLACE,"uidNumber",str(uidnumber)),
195 (ldap.MOD_REPLACE,"gidNumber",str(gidnumber)),
196 (ldap.MOD_REPLACE,"gecos",FullName+",,,,"),
197 (ldap.MOD_REPLACE,"loginShell","/bin/bash"),
198 (ldap.MOD_REPLACE,"keyfingerprint",Keys[0][1]),
199 (ldap.MOD_REPLACE,"cn",cn),
200 (ldap.MOD_REPLACE,"mn",mn),
201 (ldap.MOD_REPLACE,"sn",sn),
202 (ldap.MOD_REPLACE,"emailforward",email),
203 (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
204 (ldap.MOD_REPLACE,"shadowMin","0"),
205 (ldap.MOD_REPLACE,"shadowMax","99999"),
206 (ldap.MOD_REPLACE,"shadowWarning","7"),
207 (ldap.MOD_REPLACE,"shadowInactive",""),
208 (ldap.MOD_REPLACE,"shadowExpire","")];
210 Rec.append((ldap.MOD_REPLACE,"privatesub",privsub));
212 Rec.append((ldap.MOD_REPLACE,"userPassword","{crypt}"+Pass));
214 # Submit the modification request
215 Dn = "uid=" + account + "," + BaseDn;
216 print "Updating LDAP directory..",
219 l.add_s(Dn,[("uid",account),
220 ("objectclass","top"),
221 ("objectclass","account"),
222 ("objectclass","posixAccount"),
223 ("objectclass","shadowAccount"),
224 ("objectclass","debiandeveloper")]);
225 except ldap.ALREADY_EXISTS:
228 # Send the modify request
232 # Abort email sends for an update operation
233 if Update == 1 and ForceMail == 0:
234 print "Account is not new, Not sending mails"
237 # Do the subscription/welcome message
239 Sub = TemplateSubst(Subst,open(TemplatesDir+"/list-subscribe","r").read());
240 Child = os.popen("/usr/sbin/sendmail -t","w");
242 if Child.close() != None:
243 raise Error, "Sendmail gave a non-zero return code";
245 # Send the Welcome message
246 print "Sending Welcome Email"
247 Reply = TemplateSubst(Subst,open(TemplatesDir+"/welcome-message-"+gidnumber,"r").read());
248 Child = os.popen("/usr/sbin/sendmail -t","w");
249 #Child = os.popen("cat","w");
251 if Child.close() != None:
252 raise Error, "Sendmail gave a non-zero return code";