6139556470c83414eb0b58bee00e065b59262f1d
[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 #
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.
11 #
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.
16 #
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.
20
21 import string, re, time, ldap, getopt, sys, os, pwd;
22 from userdir_ldap import *;
23 from userdir_gpg import *;
24
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
30
31 # [JT] This is broken with Woody LDAP and the Schema; for now just
32 #      search through all GIDs.
33 def GetFreeID(l):
34    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
35                       "gidNumber=*",["gidNumber"]);
36    HighestUID = 0;
37    for I in Attrs:
38       ID = int(GetAttr(I,"gidNumber","0"));
39       if ID > HighestUID and ID < 60000:
40          HighestUID = ID;
41    return HighestUID + 1;
42
43 # Main starts here
44 AdminUser = pwd.getpwuid(os.getuid())[0];
45
46 # Process options
47 ForceMail = 0;
48 OldGPGKeyRings = GPGKeyRings;
49 userdir_gpg.GPGKeyRings = [];
50 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
51 for (switch, val) in options:
52    if (switch == '-u'):
53       AdminUser = val;
54
55 print "Accessing LDAP directory as '" + AdminUser + "'";
56 Password = getpass(AdminUser + "'s password: ");
57
58 # Connect to the ldap server
59 l = ldap.open(LDAPServer);
60 UserDn = "uid=" + AdminUser + "," + BaseDn;
61 l.simple_bind_s(UserDn,Password);
62
63 while 1:
64    Group = raw_input("Group name? ");
65    if Group == "":
66       sys.exit(1);
67
68    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"gid=" + Group);
69    if len(Attrs) == 0:
70       break;
71    print "Group already exists";
72
73 Id = GetFreeID(l);
74 print "Create group %s ID = %d"%(Group,Id);
75
76 # Submit the add request
77 Dn = "gid=" + Group + "," + BaseDn;
78 print "Updating LDAP directory..",
79 sys.stdout.flush();
80 l.add_s(Dn,[("gid",Group),
81             ("gidNumber",str(Id)),
82             ("objectClass",("top", "debianGroup"))]);