ud-mailgate: remove exception for münchen.debian.net
[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, status):
51     try:
52         u = UDLdap.Account.from_search(lc, BaseDn, user)
53     except IndexError, e:
54         sys.stderr.write("Cannot instantiate account from LDAP: %s"%(str(e)))
55         return
56     if not u['accountStatus'] == 'active':
57         sys.stderr.write('%s: Account is not active, skipping.  (details: %s)\n'%(user, u.verbose_status()))
58         return
59
60     print '%s: Setting to %s:'%(user, status)
61     set = {}
62     set['userPassword'] = '{crypt}*LK*'
63     set['shadowLastChange'] = str(int(time.time()/24/60/60))
64     set['shadowExpire'] = '1'
65     set['accountStatus'] = '%s %s'%(status, time.strftime('%Y-%m-%d'))
66     if not ticket is None:
67         set['accountComment'] = "RT#%s"%(ticket)
68
69     rec = []
70     for key in set:
71         print '  %s: %s'%(key, set[key])
72         rec.append( (ldap.MOD_REPLACE, key, set[key]) )
73
74     if u.numkeys() > 0:
75         print '  %s: deleting keyFingerPrint'%(user)
76         rec.append( (ldap.MOD_DELETE, 'keyFingerPrint', None) )
77
78     if dry_run:
79         print '(not committing)'
80     else:
81         lc.modify_s(u.get_dn(), rec)
82         print '%s: done.'%(user)
83
84     sys.stdout.flush()
85
86
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.")
96 parser.add_option("-s", "--status", dest="status", metavar="status",
97   default='retiring',
98   help="Set status to <status> (default: retiring).")
99
100 (options, args) = parser.parse_args()
101
102 if options.no_do:
103     dry_run = True
104
105 lc = connect(options.admin)
106 for user in args:
107     do_one_user(lc, user, options.ticket, options.status)
108
109
110 # vim:set et:
111 # vim:set ts=4:
112 # vim:set shiftwidth=4: