|
7 | 7 | from ldap.pkginfo import __version__
|
8 | 8 |
|
9 | 9 | import _ldap
|
10 |
| -assert _ldap.__version__==__version__, \ |
11 |
| - ImportError('ldap %s and _ldap %s version mismatch!' % (__version__,_ldap.__version__)) |
| 10 | +assert _ldap.__version__ == __version__, ImportError( |
| 11 | + 'ldap %s and _ldap %s version mismatch!' % (__version__, _ldap.__version__) |
| 12 | +) |
12 | 13 |
|
13 | 14 | import ldap.functions
|
14 | 15 |
|
15 | 16 |
|
16 |
| -def escape_dn_chars(s): |
17 |
| - """ |
18 |
| - Escape all DN special characters found in s |
19 |
| - with a back-slash (see RFC 4514, section 2.4) |
20 |
| - """ |
21 |
| - if s: |
22 |
| - s = s.replace('\\','\\\\') |
23 |
| - s = s.replace(',' ,'\\,') |
24 |
| - s = s.replace('+' ,'\\+') |
25 |
| - s = s.replace('"' ,'\\"') |
26 |
| - s = s.replace('<' ,'\\<') |
27 |
| - s = s.replace('>' ,'\\>') |
28 |
| - s = s.replace(';' ,'\\;') |
29 |
| - s = s.replace('=' ,'\\=') |
30 |
| - s = s.replace('\000' ,'\\\000') |
31 |
| - if s[0]=='#' or s[0]==' ': |
32 |
| - s = ''.join(('\\',s)) |
33 |
| - if s[-1]==' ': |
34 |
| - s = ''.join((s[:-1],'\\ ')) |
35 |
| - return s |
36 |
| - |
37 |
| - |
38 |
| -def str2dn(dn,flags=0): |
39 |
| - """ |
40 |
| - This function takes a DN as string as parameter and returns |
41 |
| - a decomposed DN. It's the inverse to dn2str(). |
42 |
| -
|
43 |
| - flags describes the format of the dn |
44 |
| -
|
45 |
| - See also the OpenLDAP man-page ldap_str2dn(3) |
46 |
| - """ |
47 |
| - if not dn: |
48 |
| - return [] |
49 |
| - return ldap.functions._ldap_function_call(None,_ldap.str2dn,dn,flags) |
| 17 | +def escape_dn_chars(val): |
| 18 | + """ |
| 19 | + Escape all DN special characters found in s |
| 20 | + with a back-slash (see RFC 4514, section 2.4) |
| 21 | + """ |
| 22 | + if val: |
| 23 | + val = val.replace('\\', '\\\\') |
| 24 | + val = val.replace(',', '\\,') |
| 25 | + val = val.replace('+', '\\+') |
| 26 | + val = val.replace('"', '\\"') |
| 27 | + val = val.replace('<', '\\<') |
| 28 | + val = val.replace('>', '\\>') |
| 29 | + val = val.replace(';', '\\;') |
| 30 | + val = val.replace('=', '\\=') |
| 31 | + val = val.replace('\000', '\\\000') |
| 32 | + if val[0] == '#' or val[0] == ' ': |
| 33 | + val = ''.join(('\\', val)) |
| 34 | + if val[-1] == ' ': |
| 35 | + val = ''.join((val[:-1], '\\ ')) |
| 36 | + return val |
| 37 | + |
| 38 | + |
| 39 | +def str2dn(dn, flags=0): |
| 40 | + """ |
| 41 | + This function takes a DN as string as parameter and returns |
| 42 | + a decomposed DN. It's the inverse to dn2str(). |
| 43 | +
|
| 44 | + flags describes the format of the dn |
| 45 | +
|
| 46 | + See also the OpenLDAP man-page ldap_str2dn(3) |
| 47 | + """ |
| 48 | + if not dn: |
| 49 | + return [] |
| 50 | + return ldap.functions._ldap_function_call(None, _ldap.str2dn, dn, flags) |
50 | 51 |
|
51 | 52 |
|
52 | 53 | def dn2str(dn):
|
53 |
| - """ |
54 |
| - This function takes a decomposed DN as parameter and returns |
55 |
| - a single string. It's the inverse to str2dn() but will always |
56 |
| - return a DN in LDAPv3 format compliant to RFC 4514. |
57 |
| - """ |
58 |
| - return ','.join([ |
59 |
| - '+'.join([ |
60 |
| - '='.join((atype,escape_dn_chars(avalue or ''))) |
61 |
| - for atype,avalue,dummy in rdn]) |
62 |
| - for rdn in dn |
63 |
| - ]) |
64 |
| - |
65 |
| -def explode_dn(dn,notypes=0,flags=0): |
66 |
| - """ |
67 |
| - explode_dn(dn [, notypes=0]) -> list |
68 |
| -
|
69 |
| - This function takes a DN and breaks it up into its component parts. |
70 |
| - The notypes parameter is used to specify that only the component's |
71 |
| - attribute values be returned and not the attribute types. |
72 |
| - """ |
73 |
| - if not dn: |
74 |
| - return [] |
75 |
| - dn_decomp = str2dn(dn,flags) |
76 |
| - rdn_list = [] |
77 |
| - for rdn in dn_decomp: |
| 54 | + """ |
| 55 | + This function takes a decomposed DN as parameter and returns |
| 56 | + a single string. It's the inverse to str2dn() but will always |
| 57 | + return a DN in LDAPv3 format compliant to RFC 4514. |
| 58 | + """ |
| 59 | + return ','.join([ |
| 60 | + '+'.join([ |
| 61 | + '='.join((atype, escape_dn_chars(avalue or ''))) |
| 62 | + for atype, avalue, _ in rdn]) |
| 63 | + for rdn in dn |
| 64 | + ]) |
| 65 | + |
| 66 | +def explode_dn(dn, notypes=0, flags=0): |
| 67 | + """ |
| 68 | + explode_dn(dn [, notypes=0]) -> list |
| 69 | +
|
| 70 | + This function takes a DN and breaks it up into its component parts. |
| 71 | + The notypes parameter is used to specify that only the component's |
| 72 | + attribute values be returned and not the attribute types. |
| 73 | + """ |
| 74 | + if not dn: |
| 75 | + return [] |
| 76 | + dn_decomp = str2dn(dn, flags) |
| 77 | + rdn_list = [] |
| 78 | + for rdn in dn_decomp: |
| 79 | + if notypes: |
| 80 | + rdn_list.append('+'.join([ |
| 81 | + escape_dn_chars(avalue or '') |
| 82 | + for atype, avalue, _ in rdn |
| 83 | + ])) |
| 84 | + else: |
| 85 | + rdn_list.append('+'.join([ |
| 86 | + '='.join((atype, escape_dn_chars(avalue or ''))) |
| 87 | + for atype, avalue, _ in rdn |
| 88 | + ])) |
| 89 | + return rdn_list |
| 90 | + |
| 91 | + |
| 92 | +def explode_rdn(rdn, notypes=0, flags=0): |
| 93 | + """ |
| 94 | + explode_rdn(rdn [, notypes=0]) -> list |
| 95 | +
|
| 96 | + This function takes a RDN and breaks it up into its component parts |
| 97 | + if it is a multi-valued RDN. |
| 98 | + The notypes parameter is used to specify that only the component's |
| 99 | + attribute values be returned and not the attribute types. |
| 100 | + """ |
| 101 | + if not rdn: |
| 102 | + return [] |
| 103 | + rdn_decomp = str2dn(rdn, flags)[0] |
78 | 104 | if notypes:
|
79 |
| - rdn_list.append('+'.join([ |
80 |
| - escape_dn_chars(avalue or '') |
81 |
| - for atype,avalue,dummy in rdn |
82 |
| - ])) |
| 105 | + return [ |
| 106 | + avalue or '' |
| 107 | + for atype, avalue, _ in rdn_decomp |
| 108 | + ] |
| 109 | + return [ |
| 110 | + '='.join((atype, escape_dn_chars(avalue or ''))) |
| 111 | + for atype, avalue, _ in rdn_decomp |
| 112 | + ] |
| 113 | + |
| 114 | + |
| 115 | +def is_dn(val, flags=0): |
| 116 | + """ |
| 117 | + Returns True is `s' can be parsed by ldap.dn.str2dn() like as a |
| 118 | + distinguished host_name (DN), otherwise False is returned. |
| 119 | + """ |
| 120 | + try: |
| 121 | + str2dn(val, flags) |
| 122 | + except Exception: |
| 123 | + return False |
83 | 124 | else:
|
84 |
| - rdn_list.append('+'.join([ |
85 |
| - '='.join((atype,escape_dn_chars(avalue or ''))) |
86 |
| - for atype,avalue,dummy in rdn |
87 |
| - ])) |
88 |
| - return rdn_list |
89 |
| - |
90 |
| - |
91 |
| -def explode_rdn(rdn,notypes=0,flags=0): |
92 |
| - """ |
93 |
| - explode_rdn(rdn [, notypes=0]) -> list |
94 |
| -
|
95 |
| - This function takes a RDN and breaks it up into its component parts |
96 |
| - if it is a multi-valued RDN. |
97 |
| - The notypes parameter is used to specify that only the component's |
98 |
| - attribute values be returned and not the attribute types. |
99 |
| - """ |
100 |
| - if not rdn: |
101 |
| - return [] |
102 |
| - rdn_decomp = str2dn(rdn,flags)[0] |
103 |
| - if notypes: |
104 |
| - return [avalue or '' for atype,avalue,dummy in rdn_decomp] |
105 |
| - else: |
106 |
| - return ['='.join((atype,escape_dn_chars(avalue or ''))) for atype,avalue,dummy in rdn_decomp] |
107 |
| - |
108 |
| - |
109 |
| -def is_dn(s,flags=0): |
110 |
| - """ |
111 |
| - Returns True is `s' can be parsed by ldap.dn.str2dn() like as a |
112 |
| - distinguished host_name (DN), otherwise False is returned. |
113 |
| - """ |
114 |
| - try: |
115 |
| - str2dn(s,flags) |
116 |
| - except Exception: |
117 |
| - return False |
118 |
| - else: |
119 |
| - return True |
| 125 | + return True |
0 commit comments