A set of copyright headers
[mirror/userdir-ldap.git] / ud-useradd
index 8cc425b..2676933 100755 (executable)
@@ -4,6 +4,7 @@
 #   Copyright (c) 1999-2000  Jason Gunthorpe <jgg@debian.org>
 #   Copyright (c) 2001-2003  James Troup <troup@debian.org>
 #   Copyright (c) 2004  Joey Schulze <joey@infodrom.org>
+#   Copyright (c) 2008,2009,2010 Peter Palfrader <peter@palfrader.org>
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import string, re, time, ldap, getopt, sys, os, pwd;
+import re, time, ldap, getopt, sys, os, pwd;
+import email.Header
+
 from userdir_ldap import *;
 from userdir_gpg import *;
 
+HavePrivateList = getattr(ConfModule, "haveprivatelist", True)
+
 # This tries to search for a free UID. There are two possible ways to do
 # this, one is to fetch all the entires and pick the highest, the other
 # is to randomly guess uids until one is free. This uses the former.
@@ -67,10 +72,10 @@ for (switch, val) in options:
    elif (switch == '-n'):
       NoAutomaticIDs = 1;
 
-l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
+l = passwdAccessLDAP(BaseDn, AdminUser)
 
 # Locate the key of the user we are adding
-SetKeyrings(["/org/keyring.debian.org/keyrings/debian-keyring.gpg"])
+SetKeyrings(ConfModule.add_keyrings.split(":"))
 while (1):
    Foo = raw_input("Who are you going to add (for a GPG search)? ");
    if Foo == "":
@@ -97,10 +102,10 @@ while (1):
 # first/middle/last name
 Addr = SplitEmail(Keys[0][2]);
 (cn,mn,sn) = NameSplit(re.sub('["]','',Addr[0]))
-email = Addr[1] + '@' + Addr[2];
+emailaddr = Addr[1] + '@' + Addr[2];
 account = Addr[1];
 
-privsub = email;
+privsub = emailaddr
 gidNumber = 0;
 uidNumber = 0;
 
@@ -138,7 +143,7 @@ while 1:
       privsub = GetAttr(Attrs[0],"privateSub");
       gidNumber = GetAttr(Attrs[0],"gidNumber");
       uidNumber = GetAttr(Attrs[0],"uidNumber");
-      email = GetAttr(Attrs[0],"emailForward");
+      emailaddr = GetAttr(Attrs[0],"emailForward");
       cn = GetAttr(Attrs[0],"cn");
       sn = GetAttr(Attrs[0],"sn");
       mn = GetAttr(Attrs[0],"mn");
@@ -153,19 +158,24 @@ Res = raw_input("First name [" + cn + "]? ");
 if Res != "":
    cn = Res;
 Res = raw_input("Middle name [" + mn + "]? ");
-if Res != "":
+if Res == " ":
+   mn = ""
+elif Res != "":
    mn = Res;
 Res = raw_input("Last name [" + sn + "]? ");
 if Res != "":
    sn = Res;
-Res = raw_input("Email forwarding address [" + email + "]? ");
+Res = raw_input("Email forwarding address [" + emailaddr + "]? ");
 if Res != "":
-   email = Res;
+   emailaddr = Res;
 
 # Debian-Private subscription
-Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
-if Res != "":
-   privsub = Res;
+if HavePrivateList:
+   Res = raw_input("Subscribe to debian-private (space is none) [" + privsub + "]? ");
+   if Res != "":
+      privsub = Res;
+else:
+   privsub = " "
 
 (uidNumber, generatedGID) = GetFreeID(l)
 if not gidNumber:
@@ -183,7 +193,7 @@ if NoAutomaticIDs:
    Res = raw_input("Group ID Number (default group is %s, new usergroup %s) [%s]" % (DefaultGID, generatedGID, gidNumber));
    if Res != "":
       if Res.isdigit():
-         gidNumber = Res;
+         gidNumber = int(Res);
       else:
          gidNumber = Group2GID(l, Res);
    
@@ -221,8 +231,9 @@ print "------------";
 print "Final information collected:"
 print " %s <%s@%s>:" % (FullName,account,EmailAppend);
 print "   Assigned UID:",uidNumber," GID:", gidNumber;
-print "   Email forwarded to:",email;
-print "   Private Subscription:",privsub;
+print "   Email forwarded to:",emailaddr
+if HavePrivateList:
+   print "   Private Subscription:",privsub;
 print "   GECOS Field: \"%s,,,,\"" % (FullName);
 print "   Login Shell: /bin/bash";
 print "   Key Fingerprint:",Keys[0][1];
@@ -232,12 +243,20 @@ if Res != "yes":
 
 # Initialize the substitution Map
 Subst = {}
+
+encrealname = ''
+try:
+  encrealname = FullName.decode('us-ascii')
+except UnicodeError:
+  encrealname = str(email.Header.Header(FullName, 'utf-8', 200))
+
+Subst["__ENCODED_REALNAME__"] = encrealname
 Subst["__REALNAME__"] = FullName;
 Subst["__WHOAMI__"] = pwd.getpwuid(os.getuid())[0];
 Subst["__DATE__"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",time.gmtime(time.time()));
 Subst["__LOGIN__"] = account;
 Subst["__PRIVATE__"] = privsub;
-Subst["__EMAIL__"] = email;
+Subst["__EMAIL__"] = emailaddr
 Subst["__PASSWORD__"] = CryptedPass;
 
 # Submit the modification request
@@ -256,7 +275,7 @@ if Update == 0:
               ("keyFingerPrint",Keys[0][1]),
               ("cn",cn),
               ("sn",sn),
-              ("emailForward",email),
+              ("emailForward",emailaddr),
               ("shadowLastChange",str(int(time.time()/24/60/60))),
               ("shadowMin","0"),
               ("shadowMax","99999"),
@@ -266,13 +285,12 @@ if Update == 0:
       Details.append(("mn",mn));
    if privsub != " ":
       Details.append(("privateSub",privsub))
+   l.add_s(Dn,Details);
 
    #Add user group if needed, then the actual user:
    if UserGroup == 1:
       Dn = "gid=" + account + "," + BaseDn;
       l.add_s(Dn,[("gid",account), ("gidNumber",str(gidNumber)), ("objectClass", GroupObjectClasses)])
-
-   l.add_s(Dn,Details);
 else:
    # Modification
    Rec = [(ldap.MOD_REPLACE,"uidNumber",str(uidNumber)),
@@ -283,7 +301,7 @@ else:
           (ldap.MOD_REPLACE,"cn",cn),
           (ldap.MOD_REPLACE,"mn",mn),
           (ldap.MOD_REPLACE,"sn",sn),
-          (ldap.MOD_REPLACE,"emailForward",email),
+          (ldap.MOD_REPLACE,"emailForward",emailaddr),
           (ldap.MOD_REPLACE,"shadowLastChange",str(int(time.time()/24/60/60))),
           (ldap.MOD_REPLACE,"shadowMin","0"),
           (ldap.MOD_REPLACE,"shadowMax","99999"),
@@ -306,7 +324,10 @@ if Update == 1 and ForceMail == 0:
 
 # Send the Welcome message
 print "Sending Welcome Email"
-Reply = TemplateSubst(Subst,open(TemplatesDir + "/welcome-message-%d" % gidNumber, "r").read())
+templatepath = TemplatesDir + "/welcome-message-%d" % int(gidNumber)
+if not os.path.exists(templatepath):
+   templatepath = TemplatesDir + "/welcome-message"
+Reply = TemplateSubst(Subst,open(templatepath, "r").read())
 Child = os.popen("/usr/sbin/sendmail -t","w");
 #Child = os.popen("cat","w");
 Child.write(Reply);