1 |
douglas |
602 |
#!/usr/bin/env python |
2 |
|
|
# CCS Computer Science |
3 |
|
|
# Shells |
4 |
|
|
# |
5 |
|
|
# Douglas Thrift |
6 |
|
|
# |
7 |
|
|
# $Id$ |
8 |
|
|
|
9 |
|
|
import admin |
10 |
|
|
import common |
11 |
|
|
import copy |
12 |
|
|
import ldap |
13 |
|
|
import ldap.modlist |
14 |
|
|
|
15 |
|
|
if __name__ == '__main__': |
16 |
douglas |
681 |
parser = admin.parser() |
17 |
douglas |
602 |
|
18 |
douglas |
687 |
parser.add_option('-l', '--list', action = 'store_const', const = 'list', help = 'list the available shells and their per system paths', dest = 'command') |
19 |
|
|
parser.add_option('-u', '--update', action = 'store_const', const = 'update', help = 'update shells in the LDAP database', dest = 'command') |
20 |
douglas |
602 |
|
21 |
douglas |
687 |
command = parser.parse_args()[0].command |
22 |
douglas |
602 |
|
23 |
douglas |
687 |
if not command: |
24 |
|
|
parser.error('no option specified') |
25 |
|
|
elif command == 'list': |
26 |
douglas |
681 |
for shell, shells in common.SHELLS: |
27 |
|
|
print 'shell:', shell |
28 |
douglas |
602 |
|
29 |
douglas |
681 |
for system, shell in zip(common.SYSTEMS, shells): |
30 |
|
|
print '%20s:' % system, shell if shell else '{user defined}' |
31 |
|
|
elif command == 'update': |
32 |
|
|
shells = list(common.SHELLS) |
33 |
|
|
shells[-1] = ('custom', dict(common.SHELLS)['bash']) |
34 |
douglas |
602 |
|
35 |
douglas |
681 |
try: |
36 |
|
|
connection = admin.ldap_connection() |
37 |
douglas |
602 |
|
38 |
douglas |
681 |
for shell, shells in shells: |
39 |
|
|
for dn, old_entry in connection.search_s(admin.PEOPLE, ldap.SCOPE_ONELEVEL, '(loginShell=%s)' % shell, ['uid'] + admin.SHELLS): |
40 |
douglas |
602 |
|
41 |
douglas |
681 |
if shell != 'custom': |
42 |
|
|
new_entry = dict(zip(admin.SHELLS, map(lambda shell: [shell], shells))) |
43 |
|
|
else: |
44 |
|
|
new_entry = copy.copy(old_entry) |
45 |
douglas |
602 |
|
46 |
douglas |
681 |
for type, _shell in zip(admin.SHELLS, shells): |
47 |
|
|
if type not in new_entry: |
48 |
|
|
new_entry[type] = _shell |
49 |
|
|
|
50 |
|
|
modlist = ldap.modlist.modifyModlist(old_entry, new_entry, ('uid',)) |
51 |
|
|
|
52 |
|
|
if modlist: |
53 |
|
|
print old_entry['uid'][0], modlist |
54 |
|
|
|
55 |
|
|
connection.modify_s(dn, modlist) |
56 |
|
|
|
57 |
|
|
connection.unbind_s() |
58 |
|
|
except (IOError, ldap.LDAPError), error: |
59 |
|
|
admin.error(error) |