# 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;
from userdir_ldap import *;
from userdir_gpg import *;
# search through all UIDs.
def GetFreeID(l):
Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
- "uidNumber=*",["uidNumber"]);
+ "uidNumber=*",["uidNumber", "gidNumber"]);
HighestUID = 0;
+ gids = [];
for I in Attrs:
ID = int(GetAttr(I,"uidNumber","0"));
+ gids.append(int(GetAttr(I, "gidNumber","0")))
if ID > HighestUID:
HighestUID = ID;
- return HighestUID + 1;
+
+ resGID = HighestUID + 1;
+ while resGID in gids:
+ resGID += 1
+
+ return (HighestUID + 1, resGID);
# Main starts here
AdminUser = pwd.getpwuid(os.getuid())[0];
# Process options
ForceMail = 0;
+NoAutomaticIDs = 0;
OldGPGKeyRings = GPGKeyRings;
userdir_gpg.GPGKeyRings = [];
-(options, arguments) = getopt.getopt(sys.argv[1:], "u:ma")
+(options, arguments) = getopt.getopt(sys.argv[1:], "u:man")
for (switch, val) in options:
if (switch == '-u'):
AdminUser = val;
ForceMail = 1;
elif (switch == '-a'):
userdir_gpg.GPGKeyRings = OldGPGKeyRings;
+ elif (switch == '-n'):
+ NoAutomaticIDs = 1;
-print "Accessing LDAP directory as '" + AdminUser + "'";
-while (1):
- Password = getpass(AdminUser + "'s password: ");
-
- if len(Password) == 0:
- sys.exit(0)
-
- l = ldap.open(LDAPServer);
- UserDn = "uid=" + AdminUser + "," + BaseDn;
-
- # Connect to the ldap server
- try:
- l.simple_bind_s(UserDn,Password);
- except ldap.INVALID_CREDENTIALS:
- continue
- break
+l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
# Locate the key of the user we are adding
SetKeyrings(["/org/keyring.debian.org/keyrings/debian-keyring.gpg"])
account = Addr[1];
privsub = email;
-gidNumber = str(DefaultGID);
+gidNumber = 0;
uidNumber = 0;
# Decide if we should use IDEA encryption
UsePGP2 = 0;
while len(Keys[0][1]) < 40:
- Res = raw_input("Use PGP2.x compatibility [no]? ");
+ Res = raw_input("Use PGP2.x compatibility [No/yes]? ");
if Res == "yes":
UsePGP2 = 1;
break;
if Res == "":
break;
+Update = 0
Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + Keys[0][1]);
if len(Attrs) != 0:
print "*** This key already belongs to",GetAttr(Attrs[0],"uid");
account = GetAttr(Attrs[0],"uid");
+ Update = 1
# Try to get a uniq account name
-Update=0
while 1:
- Res = raw_input("Login account [" + account + "]? ");
- if Res != "":
- account = Res;
+ if Update == 0:
+ Res = raw_input("Login account [" + account + "]? ");
+ if Res != "":
+ account = Res;
Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account);
if len(Attrs) == 0:
privsub = "%s@debian.org"%(account);
if privsub == None or privsub == "":
privsub = " ";
break;
+ else:
+ sys.exit(1)
# Prompt for the first/last name and email address
Res = raw_input("First name [" + cn + "]? ");
if Res != "":
privsub = Res;
-# GID
-Res = raw_input("Group ID Number [" + gidNumber + "]? ");
-if Res != "":
- gidNumber = Group2GID(Res);
-
-# UID
-if uidNumber == 0:
- uidNumber = GetFreeID(l);
+(uidNumber, generatedGID) = GetFreeID(l)
+if not gidNumber:
+ gidNumber = DefaultGID
+UserGroup = 0
+
+if NoAutomaticIDs:
+ # UID
+ if not Update:
+ Res = raw_input("User ID Number [%s]? " % (uidNumber));
+ if Res != "":
+ uidNumber = Res;
+
+ # GID
+ Res = raw_input("Group ID Number (default group is %s, new usergroup %s) [%s]" % (DefaultGID, generatedGID, gidNumber));
+ if Res != "":
+ if Res.isdigit():
+ gidNumber = Res;
+ else:
+ gidNumber = Group2GID(l, Res);
+
+ if gidNumber == generatedGID:
+ UserGroup = 1
# Generate a random password
if Update == 0 or ForceMail == 1:
if Update == 0:
# New account
Details = [("uid",account),
- ("objectClass",
- ("top","inetOrgPerson","debianAccount","shadowAccount","debianDeveloper")),
+ ("objectClass", UserObjectClasses),
("uidNumber",str(uidNumber)),
("gidNumber",str(gidNumber)),
("gecos",FullName+",,,,"),
Details.append(("mn",mn));
if privsub != " ":
Details.append(("privateSub",privsub))
+
+ #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
# Send the Welcome message
print "Sending Welcome Email"
-Reply = TemplateSubst(Subst,open(TemplatesDir+"/welcome-message-"+gidNumber,"r").read());
+Reply = TemplateSubst(Subst,open(TemplatesDir + "/welcome-message-%d" % gidNumber, "r").read())
Child = os.popen("/usr/sbin/sendmail -t","w");
#Child = os.popen("cat","w");
Child.write(Reply);