Merge branch 'refactor-udgen'
[mirror/userdir-ldap.git] / ud-lock
1 #!/usr/bin/python
2
3 # Copyright (c) 2010 Peter Palfrader <peter@palfrader.org>
4
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.
8
9
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.
14 #
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.
19 #
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.
23
24 import sys
25 import optparse
26 import os
27 import pwd
28 import time
29 from userdir_ldap import *;
30 import UDLdap
31
32 dry_run = False
33
34 def connect(user):
35     l = connectLDAP()
36     binddn = "uid=%s,%s"%(user, BaseDn)
37     bindpw = None
38     if 'LDAP_PASSWORD' in os.environ:
39         bindpw = os.environ['LDAP_PASSWORD']
40     else:
41         bindpw = getpass.getpass(user + "'s password: ")
42
43     try:
44        l.simple_bind_s(binddn, bindpw)
45     except ldap.LDAPError, e:
46        sys.stderr.write("LDAP error: %s\n"%(e.args[0]['desc']))
47        sys.exit(1)
48     return l
49
50 def do_one_user(lc, user, ticket):
51     u = UDLdap.Account.from_search(lc, BaseDn, user)
52     if not u['accountStatus'] == 'active':
53         sys.stderr.write('%s: Account is not active, skipping.  (details: %s)\n'%(user, u.verbose_status()))
54         return
55
56     print '%s: Setting to retiring:'%(user)
57     set = {}
58     set['userPassword'] = '{crypt}*LK*'
59     set['shadowLastChange'] = str(int(time.time()/24/60/60))
60     set['shadowExpire'] = '1'
61     set['accountStatus'] = 'retiring %s'%(time.strftime('%Y-%m-%d'))
62     if not ticket is None:
63         set['accountComment'] = "RT#%s"%(ticket)
64
65     rec = []
66     for key in set:
67         print '  %s: %s'%(key, set[key])
68         rec.append( (ldap.MOD_REPLACE, key, set[key]) )
69
70     if u.numkeys() > 0:
71         print '  %s: deleting keyFingerPrint'%(user)
72         rec.append( (ldap.MOD_DELETE, 'keyFingerPrint', None) )
73
74     if dry_run:
75         print '(not committing)'
76     else:
77         lc.modify_s(u.get_dn(), rec)
78         print '%s: done.'%(user)
79
80     sys.stdout.flush()
81
82
83 parser = optparse.OptionParser()
84 parser.set_usage("%prog [--admin-user <binduser>] [--no-do] <account> [<account> ...]")
85 parser.add_option("-a", "--admin-user", dest="admin", metavar="admin",
86   help="User to bind as.",
87   default=pwd.getpwuid(os.getuid()).pw_name)
88 parser.add_option("-n", "--no-do", action="store_true",
89   help="Do not actually change anything.")
90 parser.add_option("-r", "--rt-ticket", dest="ticket", metavar="ticket#",
91   help="Ticket number for accountComment.")
92
93 (options, args) = parser.parse_args()
94
95 if options.no_do:
96     dry_run = True
97
98 lc = connect(options.admin)
99 for user in args:
100     do_one_user(lc, user, options.ticket)
101
102
103 # vim:set et:
104 # vim:set ts=4:
105 # vim:set shiftwidth=4: