| 1 |
# |
| 2 |
# Copyright (C) 2019 FreeIPA Contributors see COPYING for license |
| 3 |
# |
| 4 |
"""ctypes wrapper for libldap_str2dn |
| 5 |
""" |
| 6 |
from __future__ import absolute_import |
| 7 |
|
| 8 |
import ctypes |
| 9 |
import ctypes.util |
| 10 |
|
| 11 |
import six |
| 12 |
|
| 13 |
__all__ = ("str2dn", "dn2str", "DECODING_ERROR", "LDAPError") |
| 14 |
|
| 15 |
# load reentrant ldap client library (libldap_r-*.so.2 or libldap.so.2) |
| 16 |
ldap_lib_filename = next( |
| 17 |
filter(None, map(ctypes.util.find_library, ["ldap_r-2", "ldap"])), None |
| 18 |
) |
| 19 |
|
| 20 |
if ldap_lib_filename is None: |
| 21 |
raise ImportError("libldap_r or libldap shared library missing") |
| 22 |
try: |
| 23 |
lib = ctypes.CDLL(ldap_lib_filename) |
| 24 |
except OSError as e: |
| 25 |
raise ImportError(str(e)) |
| 26 |
|
| 27 |
# constants |
| 28 |
LDAP_AVA_FREE_ATTR = 0x0010 |
| 29 |
LDAP_AVA_FREE_VALUE = 0x0020 |
| 30 |
LDAP_DECODING_ERROR = -4 |
| 31 |
|
| 32 |
# mask for AVA flags |
| 33 |
AVA_MASK = ~(LDAP_AVA_FREE_ATTR | LDAP_AVA_FREE_VALUE) |
| 34 |
|
| 35 |
|
| 36 |
class berval(ctypes.Structure): |
| 37 |
__slots__ = () |
| 38 |
_fields_ = [("bv_len", ctypes.c_ulong), ("bv_value", ctypes.c_char_p)] |
| 39 |
|
| 40 |
def __bytes__(self): |
| 41 |
buf = ctypes.create_string_buffer(self.bv_value, self.bv_len) |
| 42 |
return buf.raw |
| 43 |
|
| 44 |
def __str__(self): |
| 45 |
return self.__bytes__().decode("utf-8") |
| 46 |
|
| 47 |
if six.PY2: |
| 48 |
__unicode__ = __str__ |
| 49 |
__str__ = __bytes__ |
| 50 |
|
| 51 |
|
| 52 |
class LDAPAVA(ctypes.Structure): |
| 53 |
__slots__ = () |
| 54 |
_fields_ = [ |
| 55 |
("la_attr", berval), |
| 56 |
("la_value", berval), |
| 57 |
("la_flags", ctypes.c_uint16), |
| 58 |
] |
| 59 |
|
| 60 |
|
| 61 |
# typedef LDAPAVA** LDAPRDN; |
| 62 |
LDAPRDN = ctypes.POINTER(ctypes.POINTER(LDAPAVA)) |
| 63 |
# typedef LDAPRDN* LDAPDN; |
| 64 |
LDAPDN = ctypes.POINTER(LDAPRDN) |
| 65 |
|
| 66 |
|
| 67 |
def errcheck(result, func, arguments): |
| 68 |
if result != 0: |
| 69 |
if result == LDAP_DECODING_ERROR: |
| 70 |
raise DECODING_ERROR |
| 71 |
else: |
| 72 |
msg = ldap_err2string(result) |
| 73 |
raise LDAPError(msg.decode("utf-8")) |
| 74 |
return result |
| 75 |
|
| 76 |
|
| 77 |
ldap_str2dn = lib.ldap_str2dn |
| 78 |
ldap_str2dn.argtypes = ( |
| 79 |
ctypes.c_char_p, |
| 80 |
ctypes.POINTER(LDAPDN), |
| 81 |
ctypes.c_uint16, |
| 82 |
) |
| 83 |
ldap_str2dn.restype = ctypes.c_int16 |
| 84 |
ldap_str2dn.errcheck = errcheck |
| 85 |
|
| 86 |
ldap_dnfree = lib.ldap_dnfree |
| 87 |
ldap_dnfree.argtypes = (LDAPDN,) |
| 88 |
ldap_dnfree.restype = None |
| 89 |
|
| 90 |
ldap_err2string = lib.ldap_err2string |
| 91 |
ldap_err2string.argtypes = (ctypes.c_int16,) |
| 92 |
ldap_err2string.restype = ctypes.c_char_p |
| 93 |
|
| 94 |
|
| 95 |
class LDAPError(Exception): |
| 96 |
pass |
| 97 |
|
| 98 |
|
| 99 |
class DECODING_ERROR(LDAPError): |
| 100 |
pass |
| 101 |
|
| 102 |
|
| 103 |
# RFC 4514, 2.4 |
| 104 |
_ESCAPE_CHARS = {'"', "+", ",", ";", "<", ">", "'", "\x00"} |
| 105 |
|
| 106 |
|
| 107 |
def _escape_dn(dn): |
| 108 |
if not dn: |
| 109 |
return "" |
| 110 |
result = [] |
| 111 |
# a space or number sign occurring at the beginning of the string |
| 112 |
if dn[0] in {"#", " "}: |
| 113 |
result.append("\\") |
| 114 |
for c in dn: |
| 115 |
if c in _ESCAPE_CHARS: |
| 116 |
result.append("\\") |
| 117 |
result.append(c) |
| 118 |
# a space character occurring at the end of the string |
| 119 |
if len(dn) > 1 and result[-1] == " ": |
| 120 |
# insert before last entry |
| 121 |
result.insert(-1, "\\") |
| 122 |
return "".join(result) |
| 123 |
|
| 124 |
|
| 125 |
def dn2str(dn): |
| 126 |
return ",".join( |
| 127 |
"+".join( |
| 128 |
"=".join((attr, _escape_dn(value))) for attr, value, _flag in rdn |
| 129 |
) |
| 130 |
for rdn in dn |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
def str2dn(dn, flags=0): |
| 135 |
if dn is None: |
| 136 |
return [] |
| 137 |
if isinstance(dn, six.text_type): |
| 138 |
dn = dn.encode("utf-8") |
| 139 |
|
| 140 |
ldapdn = LDAPDN() |
| 141 |
try: |
| 142 |
ldap_str2dn(dn, ctypes.byref(ldapdn), flags) |
| 143 |
|
| 144 |
result = [] |
| 145 |
if not ldapdn: |
| 146 |
# empty DN, str2dn("") == [] |
| 147 |
return result |
| 148 |
|
| 149 |
for rdn in ldapdn: |
| 150 |
if not rdn: |
| 151 |
break |
| 152 |
avas = [] |
| 153 |
for ava_p in rdn: |
| 154 |
if not ava_p: |
| 155 |
break |
| 156 |
ava = ava_p[0] |
| 157 |
avas.append( |
| 158 |
( |
| 159 |
six.text_type(ava.la_attr), |
| 160 |
six.text_type(ava.la_value), |
| 161 |
ava.la_flags & AVA_MASK, |
| 162 |
) |
| 163 |
) |
| 164 |
result.append(avas) |
| 165 |
|
| 166 |
return result |
| 167 |
finally: |
| 168 |
ldap_dnfree(ldapdn) |