[2140] | 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,) |
---|
[2146] | 12 | l.delete_s(full_name) |
---|
[2140] | 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 findApacheConfig(l, uid): |
---|
| 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=apacheConfig)" + |
---|
| 45 | "(apacheSuexecUid=%s))", |
---|
| 46 | [uid])) |
---|
| 47 | |
---|
| 48 | return host_records |
---|
| 49 | |
---|
| 50 | def findVhost(l, full_name): |
---|
| 51 | host_records = ll.search_s( |
---|
| 52 | "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", |
---|
| 53 | ldap.SCOPE_SUBTREE, |
---|
| 54 | ldap.filter.filter_format( |
---|
| 55 | "(&(objectClass=scriptsVhost)" + |
---|
| 56 | "(scriptsVhostAccount=%s))", |
---|
| 57 | [full_name])) |
---|
| 58 | |
---|
| 59 | return host_records |
---|
| 60 | |
---|
| 61 | if __name__ == '__main__': |
---|
| 62 | (self, user) = sys.argv |
---|
| 63 | |
---|
| 64 | print "Binding to ldap..." |
---|
| 65 | |
---|
| 66 | ll = ldap.initialize("ldapi://%2fvar%2frun%2fslapd-scripts.socket/") |
---|
| 67 | ll.simple_bind_s("cn=Directory Manager", open('/etc/signup-ldap-pw').read()) |
---|
| 68 | |
---|
| 69 | print "Finding user '%s'..." % (user,) |
---|
| 70 | user_record = findUser(ll, user) |
---|
| 71 | |
---|
| 72 | print "Finding group '%s'..." % (user,) |
---|
| 73 | group_record = findGroup(ll, user) |
---|
| 74 | |
---|
| 75 | print "Searching for apache configurations..." |
---|
| 76 | apache_configs = findApacheConfig(ll, user_record[1]['uidNumber'][0]) |
---|
| 77 | |
---|
| 78 | print "Searching for vhost configurations..." |
---|
| 79 | vhost_configs = findVhost(ll, user_record[0]) |
---|
| 80 | |
---|
| 81 | print "Deleting..." |
---|
| 82 | delete_record(ll, user_record[0]) |
---|
| 83 | delete_record(ll, group_record[0]) |
---|
| 84 | |
---|
| 85 | for config in apache_configs: |
---|
| 86 | delete_record(ll, config[0]) |
---|
| 87 | |
---|
| 88 | for vhost in vhost_configs: |
---|
| 89 | delete_record(ll, vhost[0]) |
---|