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