Merged from debian branch
[mirror/userdir-ldap.git] / ud-roleadd
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
4 #   Copyright (c) 1999-2000  Jason Gunthorpe <jgg@debian.org>
5 #   Copyright (c) 2001-2003  James Troup <troup@debian.org>
6 #   Copyright (c) 2004-2005  Joey Schulze <joey@infodrom.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 time, ldap, getopt, sys, os, pwd
23 from userdir_ldap 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 UIDs.
33 def GetFreeID(l):
34    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
35                       "uidNumber=*",["uidNumber"])
36    HighestUID = 0
37    for I in Attrs:
38       ID = int(GetAttr(I,"uidNumber","0"))
39       if ID > HighestUID:
40          HighestUID = ID
41    return HighestUID + 1
42
43 # Main starts here
44 AdminUser = pwd.getpwuid(os.getuid())[0]
45
46 # Process options
47 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
48 for (switch, val) in options:
49    if (switch == '-u'):
50       AdminUser = val
51
52 l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
53
54 while 1:
55    account = raw_input("Who are you going to add? ")
56    if account == "":
57       sys.exit(0)
58
59    Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account)
60    if len(Attrs) == 0:
61       break
62
63    print "That account already exists."
64
65 Res = raw_input("Name for GECOS field? ")
66 if Res != "":
67    cn = Res
68
69 # GID
70 Res = raw_input("Group ID Number? ")
71 if Res != "":
72    gidNumber = Group2GID(l, Res)
73    if gidNumber == -1:
74       print "Can't figure out which gid %s is" % Res
75       sys.exit(1)
76
77 # UID
78 uidNumber = GetFreeID(l)
79
80 # Now we have all the bits of information.
81 print "------------"
82 print "Final information collected:"
83 print " Username %s:" % cn
84 print "   Assigned UID:",uidNumber," GID:", gidNumber
85 print "   GECOS Field: \"%s,,,,\"" % cn
86 print "   Login Shell: /bin/false"
87 Res = raw_input("Continue [No/yes]? ")
88 if Res != "yes":
89    print "Not adding %s" % cn
90    sys.exit(1)
91
92 # Submit the modification request
93 Dn = "uid=" + account + "," + BaseDn
94 print "Updating LDAP directory..",
95 sys.stdout.flush()
96
97 Details = [("uid",account),
98            ("objectClass", RoleObjectClasses),
99            ("uidNumber",str(uidNumber)),
100            ("gidNumber",str(gidNumber)),
101            ("gecos",cn+",,,,"),
102            ("loginShell","/bin/false"),
103            ("cn",cn),
104            ("shadowLastChange",str(int(time.time()/24/60/60))),
105            ("shadowMin","0"),
106            ("shadowMax","99999"),
107            ("shadowWarning","7"),
108            ("userPassword","{crypt}*")]
109 l.add_s(Dn,Details)
110
111 print