4 # Copyright (c) 2000 Jason Gunthorpe <jgg@debian.org>
5 # Copyright (c) 2001-2003 James Troup <troup@debian.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import string, re, time, ldap, getopt, sys, os, pwd;
22 from userdir_ldap import *;
23 from userdir_gpg import *;
25 # This tries to search for a free UID. There are two possible ways to do
26 # this, one is to fetch all the entires and pick the highest, the other
27 # is to randomly guess uids until one is free. This uses the former.
28 # Regrettably ldap doesn't have an integer attribute comparision function
29 # so we can only cut the search down slightly
31 # [JT] This is broken with Woody LDAP and the Schema; for now just
32 # search through all GIDs.
34 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
35 "gidNumber=*",["gidNumber"]);
38 ID = int(GetAttr(I,"gidNumber","0"));
39 if ID > HighestUID and ID < 60000:
41 return HighestUID + 1;
44 AdminUser = pwd.getpwuid(os.getuid())[0];
48 OldGPGKeyRings = GPGKeyRings;
49 userdir_gpg.GPGKeyRings = [];
50 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
51 for (switch, val) in options:
55 print "Accessing LDAP directory as '" + AdminUser + "'";
56 Password = getpass(AdminUser + "'s password: ");
58 # Connect to the ldap server
59 l = ldap.open(LDAPServer);
60 UserDn = "uid=" + AdminUser + "," + BaseDn;
61 l.simple_bind_s(UserDn,Password);
64 Group = raw_input("Group name? ");
68 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=" + Group);
71 print "Group already exists";
74 print "Create group %s ID = %d"%(Group,Id);
76 # Submit the add request
77 Dn = "gid=" + Group + "," + BaseDn;
78 print "Updating LDAP directory..",
80 l.add_s(Dn,[("gid",Group),
81 ("gidNumber",str(Id)),
82 ("objectClass",("top", "debianGroup"))]);