Add ud-guest-upgrade
[mirror/userdir-ldap.git] / ud-guest-upgrade
1 #!/usr/bin/python
2
3 # Copyright (c) 2015 Peter Palfrader <peter@palfrader.org>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 import argparse
25 import os, pwd, sys
26 from userdir_ldap import *;
27 from string import Template
28
29 parser = argparse.ArgumentParser(description='Upgrade a guest account.')
30 parser.add_argument('uid', metavar='UID',
31                    help="user's uid to be upgraded")
32 args = parser.parse_args()
33 uid = args.uid
34
35 l = connectLDAP()
36
37 x = l.search_s(BaseBaseDn,ldap.SCOPE_SUBTREE, "uid="+uid, [])
38 if len(x) == 0:
39     print >>sys.stderr, "No hit."
40     sys.exit(1)
41 elif len(x) > 1:
42     print >>sys.stderr, "More than one hit!?"
43     sys.exit(1)
44
45
46 dn = x[0][0]
47 attrs = x[0][1]
48
49 keys = attrs.keys()
50 keys.sort()
51 print >> sys.stderr, "Current info:"
52 print >> sys.stderr, dn
53 for a in keys:
54     for i in attrs[a]:
55         print >> sys.stderr, "  {:<16}: {}".format(a, i)
56
57 if 'supplementaryGid' not in attrs or 'guest' not in attrs['supplementaryGid']:
58     print >>sys.stderr, "Account is not a guest-account,"
59     sys.exit(1)
60
61 print >> sys.stderr
62 print >> sys.stderr
63 print "dn:", dn
64 print "changetype: modify"
65 print "delete: allowedHost"
66 print "-"
67 print "delete: shadowExpire"
68 print "-"
69 print "replace: supplementaryGid"
70 for gid in attrs['supplementaryGid']:
71     if gid == "guest": gid = "Debian"
72     print "supplementaryGid:", gid
73 print "-"
74 print "add: privateSub"
75 print "privateSub:", uid+"@debian.org"
76 print "-"
77 print
78
79 print >> sys.stderr
80 print >> sys.stderr, "Maybe paste (or pipe) this into"
81 print >> sys.stderr, "ldapmodify -ZZ -x -D uid=$USER,ou=users,dc=debian,dc=org -W -h db.debian.org"
82
83 # vim:set et:
84 # vim:set ts=4:
85 # vim:set shiftwidth=4: