1 |
douglas |
596 |
#!/usr/bin/env python |
2 |
|
|
# CCS Computer Science |
3 |
|
|
# |
4 |
|
|
# Douglas Thrift |
5 |
|
|
# |
6 |
|
|
# $Id$ |
7 |
|
|
|
8 |
|
|
from __future__ import with_statement |
9 |
douglas |
602 |
import admin |
10 |
douglas |
598 |
import ldap |
11 |
douglas |
596 |
|
12 |
|
|
try: |
13 |
|
|
from _users import users |
14 |
|
|
except ImportError: |
15 |
|
|
users = set() |
16 |
|
|
|
17 |
douglas |
598 |
def user(user): |
18 |
|
|
if user in users: |
19 |
|
|
return True |
20 |
|
|
|
21 |
douglas |
602 |
connection = admin.ldap_connection() |
22 |
douglas |
598 |
|
23 |
|
|
try: |
24 |
douglas |
602 |
return bool(connection.search_s(admin.PEOPLE, ldap.SCOPE_ONELEVEL, '(uid=%s)' % user, ('uid',), 1)) |
25 |
douglas |
598 |
finally: |
26 |
|
|
connection.unbind_s() |
27 |
|
|
|
28 |
douglas |
596 |
if __name__ == '__main__': |
29 |
|
|
for file in ('/etc/passwd', '/etc/group'): |
30 |
|
|
with open(file, 'rb') as file: |
31 |
|
|
for line in file: |
32 |
|
|
if line.startswith('#'): |
33 |
|
|
continue |
34 |
|
|
|
35 |
|
|
users.update(line.split(':', 1)[:1]) |
36 |
|
|
|
37 |
|
|
with open('_users.py', 'wb') as file: |
38 |
|
|
file.write('users = set((\n') |
39 |
|
|
|
40 |
|
|
for user in sorted(users): |
41 |
|
|
file.write('\t%s,\n' % repr(user)) |
42 |
|
|
|
43 |
|
|
file.write('))\n') |