Skip to content

Commit bd12e58

Browse files
author
stroeder
committed
Reference newer LDAPv3 RFC 4515
1 parent 914ef85 commit bd12e58

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lib/ldap/filter.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
filters.py - misc stuff for handling LDAP filter strings (see RFC2254)
3+
4+
See http://www.python-ldap.org/ for details.
5+
6+
\$Id: filter.py,v 1.9 2011/07/22 07:20:53 stroeder Exp $
7+
8+
Compability:
9+
- Tested with Python 2.0+
10+
"""
11+
12+
from ldap import __version__
13+
14+
15+
def escape_filter_chars(assertion_value,escape_mode=0):
16+
"""
17+
Replace all special characters found in assertion_value
18+
by quoted notation.
19+
20+
escape_mode
21+
If 0 only special chars mentioned in RFC 4515 are escaped.
22+
If 1 all NON-ASCII chars are escaped.
23+
If 2 all chars are escaped.
24+
"""
25+
if escape_mode:
26+
r = []
27+
if escape_mode==1:
28+
for c in assertion_value:
29+
if c < '0' or c > 'z' or c in "\\*()":
30+
c = "\\%02x" % ord(c)
31+
r.append(c)
32+
elif escape_mode==2:
33+
for c in assertion_value:
34+
r.append("\\%02x" % ord(c))
35+
else:
36+
raise ValueError('escape_mode must be 0, 1 or 2.')
37+
s = ''.join(r)
38+
else:
39+
s = assertion_value.replace('\\', r'\5c')
40+
s = s.replace(r'*', r'\2a')
41+
s = s.replace(r'(', r'\28')
42+
s = s.replace(r')', r'\29')
43+
s = s.replace('\x00', r'\00')
44+
return s
45+
46+
47+
def filter_format(filter_template,assertion_values):
48+
"""
49+
filter_template
50+
String containing %s as placeholder for assertion values.
51+
assertion_values
52+
List or tuple of assertion values. Length must match
53+
count of %s in filter_template.
54+
"""
55+
return filter_template % (tuple(map(escape_filter_chars,assertion_values)))

0 commit comments

Comments
 (0)