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>
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.
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.
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.
23 import time, ldap, getopt, sys, os, pwd
24 from userdir_ldap import *
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
32 # [JT] This is broken with Woody LDAP and the Schema; for now just
33 # search through all UIDs.
35 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,
36 "uidNumber=*",["uidNumber"])
39 ID = int(GetAttr(I,"uidNumber","0"))
45 AdminUser = pwd.getpwuid(os.getuid())[0]
48 (options, arguments) = getopt.getopt(sys.argv[1:], "u:")
49 for (switch, val) in options:
53 l = passwdAccessLDAP(BaseDn, AdminUser)
56 account = raw_input("Who are you going to add? ")
60 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid=" + account)
64 print "That account already exists."
66 Res = raw_input("Name for GECOS field? ")
71 Res = raw_input("Group ID Number? ")
73 gidNumber = Group2GID(l, Res)
75 print "Can't figure out which gid %s is" % Res
79 uidNumber = GetFreeID(l)
81 # Now we have all the bits of information.
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]? ")
90 print "Not adding %s" % cn
93 # Submit the modification request
94 Dn = "uid=" + account + "," + BaseDn
95 print "Updating LDAP directory..",
98 Details = [("uid",account),
99 ("objectClass", RoleObjectClasses),
100 ("uidNumber",str(uidNumber)),
101 ("gidNumber",str(gidNumber)),
103 ("loginShell","/bin/false"),
105 ("shadowLastChange",str(int(time.time()/24/60/60))),
107 ("shadowMax","99999"),
108 ("shadowWarning","7"),
109 ("userPassword","{crypt}*")]