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;
27 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
28 for (switch, val) in options:
32 print "Accessing LDAP directory as '" + AdminUser + "'";
33 Password = getpass(AdminUser + "'s password: ");
35 # Connect to the ldap server
36 l = ldap.open(LDAPServer);
37 UserDn = "uid=" + AdminUser + "," + BaseDn;
38 l.simple_bind_s(UserDn,Password);
40 # Locate the key of the user we are adding
41 GPGBasicOptions[0] = "--batch" # Permit loading of the config file
43 Foo = raw_input("Who are you going to add (for a GPG search)? ");
47 Keys = GPGKeySearch(Foo);
50 print "Sorry, that search did not turn up any keys";
53 print "Sorry, more than one key was found, please specify the key to use by\nfingerprint:";
59 print "A matching key was found:"
60 GPGPrintKeyInfo(Keys[0]);
63 # Crack up the email address from the key into a best guess
64 # first/middle/last name
65 Addr = SplitEmail(Keys[0][2]);
66 (cn,mn,sn) = NameSplit(re.sub('["]','',Addr[0]))
67 email = Addr[1] + '@' + Addr[2];
71 gidnumber = str(DefaultGID);
74 # Decide if we should use IDEA encryption
76 while len(Keys[0][1]) < 40:
77 Res = raw_input("Use PGP2.x compatibility [no]? ");
84 # Try to get a uniq account name
87 Res = raw_input("Login account [" + account + "]? ");
90 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
93 Res = raw_input("That account already exists, update [no]? ");
95 # Update mode, fetch the default values from the directory
97 privsub = GetAttr(Attrs[0],"privatesub");
98 gidnumber = GetAttr(Attrs[0],"gidnumber");
99 uidnumber = GetAttr(Attrs[0],"uidnumber");
100 email = GetAttr(Attrs[0],"emailforward");
101 cn = GetAttr(Attrs[0],"cn");
102 sn = GetAttr(Attrs[0],"sn");
103 mn = GetAttr(Attrs[0],"mn");
104 if privsub == None or privsub == "":
108 # Prompt for the first/last name and email address
109 Res = raw_input("First name [" + cn + "]? ");
112 Res = raw_input("Middle name [" + mn + "]? ");
115 Res = raw_input("Last name [" + sn + "]? ");
118 Res = raw_input("Email forwarding address [" + email + "]? ");
122 # Debian-Private subscription
123 Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
128 Res = raw_input("Group ID Number [" + gidnumber + "]? ");
134 uidnumber = GetFreeID(l);
136 # Generate a random password
138 Password = raw_input("User's Password (Enter for random)? ");
141 print "Randomizing and encrypting password"
142 Password = GenPass();
143 Pass = HashPass(Password);
144 print "PASS: ", Password;
146 # Use GPG to encrypt it, pass the fingerprint to ID it
147 CryptedPass = GPGEncrypt("Your new password is '" + Password + "'\n",\
148 "0x"+Keys[0][1],UsePGP2);
150 if CryptedPass == None:
151 raise "Error","Password Encryption failed"
153 Pass = HashPass(Password);
154 CryptedPass = "Your password has been set to the previously agreed value.";
159 # Now we have all the bits of information.
161 FullName = "%s %s %s" % (cn,mn,sn);
163 FullName = "%s %s" % (cn,sn);
164 print "------------";
165 print "Final information collected:"
166 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
167 print " Assigned UID:",uidnumber," GID:", gidnumber;
168 print " Email forwarded to:",email;
169 print " Private Subscription:",privsub;
170 print " GECOS Field: \"%s,,,,\"" % (FullName);
171 print " Login Shell: /bin/bash";
172 print " Key Fingerprint:",Keys[0][1];
173 Res = raw_input("Continue [no]? ");
177 # Initialize the substitution Map
179 Subst["__REALNAME__"] = FullName;
180 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
181 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
182 Subst["__LOGIN__"] = account;
183 Subst["__PRIVATE__"] = privsub;
184 Subst["__EMAIL__"] = email;
185 Subst["__PASSWORD__"] = CryptedPass;
186 Subst["__LISTPASS__"] = string.strip(open(pwd.getpwuid(os.getuid())[5]+"/.debian-lists_passwd","r").read());
188 # Generate the LDAP request
189 Rec = [(ldap.MOD_REPLACE,"uid",account),
190 (ldap.MOD_REPLACE,"uidNumber",str(uidnumber)),
191 (ldap.MOD_REPLACE,"gidNumber",str(gidnumber)),
192 (ldap.MOD_REPLACE,"gecos",FullName+",,,,"),
193 (ldap.MOD_REPLACE,"loginShell","/bin/bash"),
194 (ldap.MOD_REPLACE,"keyfingerprint",Keys[0][1]),
195 (ldap.MOD_REPLACE,"cn",cn),
196 (ldap.MOD_REPLACE,"mn",mn),
197 (ldap.MOD_REPLACE,"sn",sn),
198 (ldap.MOD_REPLACE,"emailforward",email),
199 (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
200 (ldap.MOD_REPLACE,"shadowMin","0"),
201 (ldap.MOD_REPLACE,"shadowMax","99999"),
202 (ldap.MOD_REPLACE,"shadowWarning","7"),
203 (ldap.MOD_REPLACE,"shadowInactive",""),
204 (ldap.MOD_REPLACE,"shadowExpire","")];
206 Rec.append((ldap.MOD_REPLACE,"privatesub",privsub));
208 Rec.append((ldap.MOD_REPLACE,"userPassword","{crypt}"+Pass));
210 # Submit the modification request
211 Dn = "uid=" + account + "," + BaseDn;
212 print "Updating LDAP directory..",
215 l.add_s(Dn,[("uid",account),
216 ("objectclass","top"),
217 ("objectclass","account"),
218 ("objectclass","posixAccount"),
219 ("objectclass","shadowAccount"),
220 ("objectclass","debiandeveloper")]);
221 except ldap.ALREADY_EXISTS:
224 # Send the modify request
228 # Abort email sends for an update operation
230 print "Account is not new, Not sending mails"
233 # Do the subscription/welcome message
235 print TemplateSubst(Subst,open("templates/list-subscribe","r").read());
237 # Send the Welcome message
238 print "Sending Welcome Email"
239 Reply = TemplateSubst(Subst,open("templates/welcome-message-"+gidnumber,"r").read());
240 Child = os.popen("/usr/sbin/sendmail -t","w");
241 #Child = os.popen("cat","w");
243 if Child.close() != None:
244 raise Error, "Sendmail gave a non-zero return code";