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 former.
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 "gidnumber>="+str(HighestUID),["gidnumber"]);
19 ID = int(GetAttr(I,"gidnumber","0"));
20 if ID > HighestUID and ID < 60000:
22 return HighestUID + 1;
25 AdminUser = pwd.getpwuid(os.getuid())[0];
29 OldGPGKeyRings = GPGKeyRings;
30 userdir_gpg.GPGKeyRings = [];
31 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
32 for (switch, val) in options:
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);
45 Group = raw_input("Group name?");
49 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=" + Group);
52 print "Group already exists";
55 print "Create group %s ID = %d"%(Group,Id);
57 # Submit the add request
58 Dn = "gid=" + Group + "," + BaseDn;
59 print "Updating LDAP directory..",
61 l.add_s(Dn,[("gid",Group),
62 ("gidnumber",str(Id)),
63 ("objectclass","top"),
64 ("objectclass","posixGroup")]);