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