From bd12e58aa5801f036d182ec6ce2d1aad87694d32 Mon Sep 17 00:00:00 2001 From: stroeder Date: Fri, 22 Jul 2011 07:20:53 +0000 Subject: [PATCH] Reference newer LDAPv3 RFC 4515 --- Lib/ldap/filter.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Lib/ldap/filter.py diff --git a/Lib/ldap/filter.py b/Lib/ldap/filter.py new file mode 100644 index 0000000..23f34c6 --- /dev/null +++ b/Lib/ldap/filter.py @@ -0,0 +1,55 @@ +""" +filters.py - misc stuff for handling LDAP filter strings (see RFC2254) + +See http://www.python-ldap.org/ for details. + +\$Id: filter.py,v 1.9 2011/07/22 07:20:53 stroeder Exp $ + +Compability: +- Tested with Python 2.0+ +""" + +from ldap import __version__ + + +def escape_filter_chars(assertion_value,escape_mode=0): + """ + Replace all special characters found in assertion_value + by quoted notation. + + escape_mode + If 0 only special chars mentioned in RFC 4515 are escaped. + If 1 all NON-ASCII chars are escaped. + If 2 all chars are escaped. + """ + if escape_mode: + r = [] + if escape_mode==1: + for c in assertion_value: + if c < '0' or c > 'z' or c in "\\*()": + c = "\\%02x" % ord(c) + r.append(c) + elif escape_mode==2: + for c in assertion_value: + r.append("\\%02x" % ord(c)) + else: + raise ValueError('escape_mode must be 0, 1 or 2.') + s = ''.join(r) + else: + s = assertion_value.replace('\\', r'\5c') + s = s.replace(r'*', r'\2a') + s = s.replace(r'(', r'\28') + s = s.replace(r')', r'\29') + s = s.replace('\x00', r'\00') + return s + + +def filter_format(filter_template,assertion_values): + """ + filter_template + String containing %s as placeholder for assertion values. + assertion_values + List or tuple of assertion values. Length must match + count of %s in filter_template. + """ + return filter_template % (tuple(map(escape_filter_chars,assertion_values)))