3 # Copyright (c) 2015 Peter Palfrader <peter@palfrader.org>
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
27 from userdir_ldap import *;
28 from string import Template
32 return datetime.timedelta(days=i)
34 parser = argparse.ArgumentParser(description='Query/Extend a guest account.')
35 parser.add_argument('uid', metavar='UID',
36 help="user's uid to be extended")
37 parser.add_argument('-x', '--extend', metavar='DAYS',
40 help="days to be extended")
41 args = parser.parse_args()
46 x = l.search_s(BaseBaseDn,ldap.SCOPE_SUBTREE, "uid="+uid, [])
48 print >>sys.stderr, "No hit."
51 print >>sys.stderr, "More than one hit!?"
60 print >> sys.stderr, "Current info:"
61 print >> sys.stderr, dn
64 print >> sys.stderr, " {:<16}: {}".format(a, i)
66 if 'supplementaryGid' not in attrs or 'guest' not in attrs['supplementaryGid']:
67 print >>sys.stderr, "Account is not a guest-account,"
69 if 'shadowExpire' not in attrs:
70 print >>sys.stderr, "Account does not expire."
73 epoch = datetime.date(1970, 1, 1)
74 shadowExpire = epoch + days(int(attrs['shadowExpire'][0]))
76 if 'allowedHost' in attrs:
77 for entry in attrs['allowedHost']:
78 list = entry.split(None,1)
79 if len(list) == 1: continue
82 parsed = datetime.datetime.strptime(expire, '%Y%m%d')
84 print >>sys.stderr, "Cannot parse expiry date in '%s' in hostACL entry."%(entry, )
85 allowedHost[h] = parsed
89 print >>sys.stderr, "Unix account expires on %s."%(shadowExpire,)
90 print >>sys.stderr, "Allowed hosts: "
91 for h in sorted(allowedHost):
92 print >>sys.stderr, " %s: %s"%(h, allowedHost[h].strftime('%Y-%m-%d'))
94 if args.extend is None:
96 print >>sys.stderr, "Use -x to extend account."
99 print >>sys.stderr, "Extending for %d days"%(args.extend)
101 today = datetime.date.today()
102 until = today + days(args.extend)
107 print "changetype: modify"
109 print "replace: shadowLastChange"
110 print "shadowLastChange: %d"%( (today - epoch).days )
113 print "replace: shadowExpire"
114 print "shadowExpire: %d"%( (until - epoch).days )
117 print "replace: allowedHost"
118 for h in sorted(allowedHost):
119 print "allowedHost: %s %s"%(h, until.strftime('%Y%m%d'))
124 print >> sys.stderr, "Maybe paste (or pipe) this into"
125 print >> sys.stderr, "ldapmodify -ZZ -x -D uid=$USER,ou=users,dc=debian,dc=org -W -h db.debian.org"
129 # vim:set shiftwidth=4: