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>
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.
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.
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.
22 import time, ldap, getopt, sys, os, pwd
23 from userdir_ldap import *
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
31 # [JT] This is broken with Woody LDAP and the Schema; for now just
32 # search through all UIDs.
34 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
35 "uidNumber=*",["uidNumber"])
38 ID = int(GetAttr(I,"uidNumber","0"))
44 AdminUser = pwd.getpwuid(os.getuid())[0]
47 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
48 for (switch, val) in options:
52 l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
55 account = raw_input("Who are you going to add? ")
59 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account)
63 print "That account already exists."
65 Res = raw_input("Name for GECOS field? ")
70 Res = raw_input("Group ID Number? ")
72 gidNumber = Group2GID(l, Res)
74 print "Can't figure out which gid %s is" % Res
78 uidNumber = GetFreeID(l)
80 # Now we have all the bits of information.
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]? ")
89 print "Not adding %s" % cn
92 # Submit the modification request
93 Dn = "uid=" + account + "," + BaseDn
94 print "Updating LDAP directory..",
97 Details = [("uid",account),
98 ("objectClass", RoleObjectClasses),
99 ("uidNumber",str(uidNumber)),
100 ("gidNumber",str(gidNumber)),
102 ("loginShell","/bin/false"),
104 ("shadowLastChange",str(int(time.time()/24/60/60))),
106 ("shadowMax","99999"),
107 ("shadowWarning","7"),
108 ("userPassword","{crypt}*")]