* Remove use of deprecated functions from the string module
[mirror/userdir-ldap.git] / ud-groupadd
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
4 #   Copyright (c) 2000       Jason Gunthorpe <jgg@debian.org>
5 #   Copyright (c) 2001-2003  James Troup <troup@debian.org>
6 #   Copyright (c) 2004       Joey Schulze <joey@debian.org>
7 #
8 #   This program is free software; you can redistribute it and/or modify
9 #   it under the terms of the GNU General Public License as published by
10 #   the Free Software Foundation; either version 2 of the License, or
11 #   (at your option) any later version.
12 #
13 #   This program is distributed in the hope that it will be useful,
14 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #   GNU General Public License for more details.
17 #
18 #   You should have received a copy of the GNU General Public License
19 #   along with this program; if not, write to the Free Software
20 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 import re, time, ldap, getopt, sys, os, pwd;
23 from userdir_ldap import *;
24 from userdir_gpg import *;
25
26 # This tries to search for a free UID. There are two possible ways to do
27 # this, one is to fetch all the entires and pick the highest, the other
28 # is to randomly guess uids until one is free. This uses the former.
29 # Regrettably ldap doesn't have an integer attribute comparision function
30 # so we can only cut the search down slightly
31
32 # [JT] This is broken with Woody LDAP and the Schema; for now just
33 #      search through all GIDs.
34 def GetFreeID(l):
35    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
36                       "gidNumber=*",["gidNumber"]);
37    HighestUID = 0;
38    for I in Attrs:
39       ID = int(GetAttr(I,"gidNumber","0"));
40       if ID > HighestUID and ID < 60000:
41          HighestUID = ID;
42    return HighestUID + 1;
43
44 # Main starts here
45 AdminUser = pwd.getpwuid(os.getuid())[0];
46
47 # Process options
48 ForceMail = 0;
49 OldGPGKeyRings = GPGKeyRings;
50 userdir_gpg.GPGKeyRings = [];
51 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
52 for (switch, val) in options:
53    if (switch == '-u'):
54       AdminUser = val;
55
56 l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
57
58 while 1:
59    Group = raw_input("Group name? ");
60    if Group == "":
61       sys.exit(1);
62
63    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=" + Group);
64    if len(Attrs) == 0:
65       break;
66    print "Group already exists";
67
68 Id = GetFreeID(l);
69 print "Create group %s ID = %d"%(Group,Id);
70
71 # Submit the add request
72 Dn = "gid=" + Group + "," + BaseDn;
73 print "Updating LDAP directory..",
74 sys.stdout.flush();
75 l.add_s(Dn,[("gid",Group),
76             ("gidNumber",str(Id)),
77             ("objectClass", GroupObjectClasses)])