1 | #!/usr/bin/python |
---|
2 | import ldap |
---|
3 | import ldap.filter |
---|
4 | import pwd |
---|
5 | import sys |
---|
6 | |
---|
7 | actuallyRun = False |
---|
8 | |
---|
9 | def delete_record(l, full_name): |
---|
10 | if actuallyRun: |
---|
11 | print "Deleting %s..." % (full_name,) |
---|
12 | l.delete_s(full_name) |
---|
13 | else: |
---|
14 | print "Would have deleted %s" % (full_name,) |
---|
15 | |
---|
16 | def findUser(l, username): |
---|
17 | # Try to delete the |
---|
18 | user_record, = ll.search_s( |
---|
19 | "ou=People,dc=scripts,dc=mit,dc=edu", |
---|
20 | ldap.SCOPE_SUBTREE, |
---|
21 | ldap.filter.filter_format( |
---|
22 | "(&(objectClass=posixAccount)" + |
---|
23 | "(uid=%s))", |
---|
24 | [username])) |
---|
25 | |
---|
26 | return user_record |
---|
27 | |
---|
28 | def findGroup(l, username): |
---|
29 | group_record, = ll.search_s( |
---|
30 | "ou=Groups,dc=scripts,dc=mit,dc=edu", |
---|
31 | ldap.SCOPE_SUBTREE, |
---|
32 | ldap.filter.filter_format( |
---|
33 | "(&(objectClass=posixGroup)" + |
---|
34 | "(cn=%s))", |
---|
35 | [username])) |
---|
36 | |
---|
37 | return group_record |
---|
38 | |
---|
39 | def findVhost(l, full_name): |
---|
40 | host_records = ll.search_s( |
---|
41 | "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", |
---|
42 | ldap.SCOPE_SUBTREE, |
---|
43 | ldap.filter.filter_format( |
---|
44 | "(&(objectClass=scriptsVhost)" + |
---|
45 | "(scriptsVhostAccount=%s))", |
---|
46 | [full_name])) |
---|
47 | |
---|
48 | return host_records |
---|
49 | |
---|
50 | if __name__ == '__main__': |
---|
51 | (self, user) = sys.argv |
---|
52 | |
---|
53 | print "Binding to ldap..." |
---|
54 | |
---|
55 | ll = ldap.initialize("ldapi://%2fvar%2frun%2fslapd-scripts.socket/") |
---|
56 | ll.simple_bind_s("cn=Directory Manager", open('/etc/signup-ldap-pw').read()) |
---|
57 | |
---|
58 | print "Finding user '%s'..." % (user,) |
---|
59 | user_record = findUser(ll, user) |
---|
60 | |
---|
61 | print "Finding group '%s'..." % (user,) |
---|
62 | group_record = findGroup(ll, user) |
---|
63 | |
---|
64 | print "Searching for vhost configurations..." |
---|
65 | vhost_configs = findVhost(ll, user_record[0]) |
---|
66 | |
---|
67 | print "Deleting..." |
---|
68 | delete_record(ll, user_record[0]) |
---|
69 | delete_record(ll, group_record[0]) |
---|
70 | |
---|
71 | for vhost in vhost_configs: |
---|
72 | delete_record(ll, vhost[0]) |
---|