/[anoncvs]/projects/dispense2/sql-edition/servers/LDAPConnector.py


UCC Code Repository

Contents of /projects/dispense2/sql-edition/servers/LDAPConnector.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download) (as text)
Sat Aug 24 06:51:35 2013 UTC (7 years, 8 months ago) by tpg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +16 -0 lines
File MIME type: text/x-python
subprocess.Popen, new pin code, disabled users, bugfixes

1 #!/usr/bin/env python2.4
2
3 import ldap
4 import ldap.filter
5
6 LDAP_TIMEOUT = 10
7
8 def get_ldap_connection():
9 ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, '/etc/ssl/UCC-CA.crt')
10 ldap.set_option(ldap.OPT_X_TLS,1)
11 ldap.set_option(ldap.OPT_X_TLS_ALLOW,1)
12 #ldap.set_option(ldap.OPT_DEBUG_LEVEL,255)
13 conn = ldap.initialize('ldaps://mussel.ucc.gu.uwa.edu.au/')
14
15 binddn = 'cn=mifareagent,ou=profile,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au'
16 passfile = open('/etc/dispense2/ldap.passwd')
17 password = passfile.readline().strip()
18 passfile.close()
19
20 conn.simple_bind_s(binddn, password)
21 return conn
22
23 def get_uid(card_id):
24 ldapconn = get_ldap_connection()
25
26 basedn = 'ou=People,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au'
27 filter = ldap.filter.filter_format('(uccDispenseMIFARE=%s)', (card_id, ))
28 attrs = ('uidNumber',)
29
30 results = ldapconn.search_st(basedn, ldap.SCOPE_SUBTREE, filter, attrs, timeout=LDAP_TIMEOUT)
31
32 ldapconn.unbind()
33
34 if len(results) != 1:
35 raise ValueError, "no UID found for card ID"
36
37 return results[0][1]['uidNumber'][0]
38
39 def get_uname(uid):
40 ldapconn = get_ldap_connection()
41
42 basedn = 'ou=People,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au'
43 filter = ldap.filter.filter_format('(uidNumber=%s)', (uid, ))
44 attrs = ('uid',)
45
46 results = ldapconn.search_st(basedn, ldap.SCOPE_SUBTREE, filter, attrs, timeout=LDAP_TIMEOUT)
47
48 ldapconn.unbind()
49
50 if len(results) != 1:
51 raise ValueError, "no username found for user id"
52
53 return results[0][1]['uid'][0]
54
55 def set_card_id(uidNumber, card_id):
56 ldapconn = get_ldap_connection()
57
58 # fix uidNumber for three/four digit uids
59 uidNumber = str(int(uidNumber))
60 basedn = 'ou=People,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au'
61 filter = ldap.filter.filter_format('(uidNumber=%s)', (uidNumber, ))
62 attrs = ('objectClass', )
63
64 results = ldapconn.search_st(basedn, ldap.SCOPE_SUBTREE, filter, attrs, timeout=LDAP_TIMEOUT)
65
66 if len(results) != 1:
67 raise "ValueError", 'error in uidNumber'
68
69 user_dn = results[0][0]
70
71 mod_attrs = []
72
73 # Does it have the correct object class?
74 if 'uccDispenseAccount' not in results[0][1]['objectClass']:
75 # Add uccDispenseAccount objectclass
76 mod_attrs.append((ldap.MOD_ADD, 'objectClass', 'uccDispenseAccount'))
77
78 # Add MIFARE Card ID
79 mod_attrs.append((ldap.MOD_ADD, 'uccDispenseMIFARE', card_id))
80
81 # Use a double-try here to work around something that's fixed in Python 2.5
82 try:
83 try:
84 ldapconn.modify_s(user_dn, mod_attrs)
85 except ldap.TYPE_OR_VALUE_EXISTS, e:
86 pass
87 finally:
88 ldapconn.unbind()
89
90 if __name__ == '__main__':
91 set_card_id('11126', '\x01\x02\x03\x04\x05\x06')
92 print get_uid('\x01\x02\x03\x04\x05\x06')

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26