3 # Copyright (c) 2010 Peter Palfrader <peter@palfrader.org>
5 # This script, non-interactively, sets a great many accounts to
6 # 'retiring', locking their password, removing keys, setting shadow
7 # information to expired and setting accountstatus appropriatly.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 from userdir_ldap import *;
36 binddn = "uid=%s,%s"%(user, BaseDn)
38 if 'LDAP_PASSWORD' in os.environ:
39 bindpw = os.environ['LDAP_PASSWORD']
41 bindpw = getpass.getpass(user + "'s password: ")
44 l.simple_bind_s(binddn, bindpw)
45 except ldap.LDAPError, e:
46 sys.stderr.write("LDAP error: %s\n"%(e.args[0]['desc']))
50 def do_one_user(lc, user, ticket):
52 u = UDLdap.Account.from_search(lc, BaseDn, user)
54 sys.stderr.write("Cannot instantiate account from LDAP: %s"%(str(e)))
56 if not u['accountStatus'] == 'active':
57 sys.stderr.write('%s: Account is not active, skipping. (details: %s)\n'%(user, u.verbose_status()))
60 print '%s: Setting to retiring:'%(user)
62 set['userPassword'] = '{crypt}*LK*'
63 set['shadowLastChange'] = str(int(time.time()/24/60/60))
64 set['shadowExpire'] = '1'
65 set['accountStatus'] = 'retiring %s'%(time.strftime('%Y-%m-%d'))
66 if not ticket is None:
67 set['accountComment'] = "RT#%s"%(ticket)
71 print ' %s: %s'%(key, set[key])
72 rec.append( (ldap.MOD_REPLACE, key, set[key]) )
75 print ' %s: deleting keyFingerPrint'%(user)
76 rec.append( (ldap.MOD_DELETE, 'keyFingerPrint', None) )
79 print '(not committing)'
81 lc.modify_s(u.get_dn(), rec)
82 print '%s: done.'%(user)
87 parser = optparse.OptionParser()
88 parser.set_usage("%prog [--admin-user <binduser>] [--no-do] <account> [<account> ...]")
89 parser.add_option("-a", "--admin-user", dest="admin", metavar="admin",
90 help="User to bind as.",
91 default=pwd.getpwuid(os.getuid()).pw_name)
92 parser.add_option("-n", "--no-do", action="store_true",
93 help="Do not actually change anything.")
94 parser.add_option("-r", "--rt-ticket", dest="ticket", metavar="ticket#",
95 help="Ticket number for accountComment.")
97 (options, args) = parser.parse_args()
102 lc = connect(options.admin)
104 do_one_user(lc, user, options.ticket)
109 # vim:set shiftwidth=4: