Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

conv.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. """
  2. """
  3. # Created on 2014.04.26
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from base64 import b64encode, b64decode
  25. import datetime
  26. import re
  27. from .. import SEQUENCE_TYPES, STRING_TYPES, NUMERIC_TYPES, get_config_parameter
  28. from ..utils.ciDict import CaseInsensitiveDict
  29. from ..core.exceptions import LDAPDefinitionError
  30. def to_unicode(obj, encoding=None, from_server=False):
  31. """Try to convert bytes (and str in python2) to unicode.
  32. Return object unmodified if python3 string, else raise an exception
  33. """
  34. conf_default_client_encoding = get_config_parameter('DEFAULT_CLIENT_ENCODING')
  35. conf_default_server_encoding = get_config_parameter('DEFAULT_SERVER_ENCODING')
  36. conf_additional_server_encodings = get_config_parameter('ADDITIONAL_SERVER_ENCODINGS')
  37. conf_additional_client_encodings = get_config_parameter('ADDITIONAL_CLIENT_ENCODINGS')
  38. if isinstance(obj, NUMERIC_TYPES):
  39. obj = str(obj)
  40. if isinstance(obj, (bytes, bytearray)):
  41. if from_server: # data from server
  42. if encoding is None:
  43. encoding = conf_default_server_encoding
  44. try:
  45. return obj.decode(encoding)
  46. except UnicodeDecodeError:
  47. for encoding in conf_additional_server_encodings: # AD could have DN not encoded in utf-8 (even if this is not allowed by RFC4510)
  48. try:
  49. return obj.decode(encoding)
  50. except UnicodeDecodeError:
  51. pass
  52. raise UnicodeError("Unable to convert server data to unicode: %r" % obj)
  53. else: # data from client
  54. if encoding is None:
  55. encoding = conf_default_client_encoding
  56. try:
  57. return obj.decode(encoding)
  58. except UnicodeDecodeError:
  59. for encoding in conf_additional_client_encodings: # tries additional encodings
  60. try:
  61. return obj.decode(encoding)
  62. except UnicodeDecodeError:
  63. pass
  64. raise UnicodeError("Unable to convert client data to unicode: %r" % obj)
  65. if isinstance(obj, STRING_TYPES): # python3 strings, python 2 unicode
  66. return obj
  67. raise UnicodeError("Unable to convert type %s to unicode: %r" % (type(obj).__class__.__name__, obj))
  68. def to_raw(obj, encoding='utf-8'):
  69. """Tries to convert to raw bytes from unicode"""
  70. if isinstance(obj, NUMERIC_TYPES):
  71. obj = str(obj)
  72. if not (isinstance(obj, bytes)):
  73. if isinstance(obj, SEQUENCE_TYPES):
  74. return [to_raw(element) for element in obj]
  75. elif isinstance(obj, STRING_TYPES):
  76. return obj.encode(encoding)
  77. return obj
  78. def escape_filter_chars(text, encoding=None):
  79. """ Escape chars mentioned in RFC4515. """
  80. if encoding is None:
  81. encoding = get_config_parameter('DEFAULT_ENCODING')
  82. try:
  83. text = to_unicode(text, encoding)
  84. escaped = text.replace('\\', '\\5c')
  85. escaped = escaped.replace('*', '\\2a')
  86. escaped = escaped.replace('(', '\\28')
  87. escaped = escaped.replace(')', '\\29')
  88. escaped = escaped.replace('\x00', '\\00')
  89. except Exception: # probably raw bytes values, return escaped bytes value
  90. escaped = to_unicode(escape_bytes(text))
  91. # escape all octets greater than 0x7F that are not part of a valid UTF-8
  92. # escaped = ''.join(c if c <= ord(b'\x7f') else escape_bytes(to_raw(to_unicode(c, encoding))) for c in escaped)
  93. return escaped
  94. def escape_bytes(bytes_value):
  95. """ Convert a byte sequence to a properly escaped for LDAP (format BACKSLASH HEX HEX) string"""
  96. if bytes_value:
  97. if str is not bytes: # Python 3
  98. if isinstance(bytes_value, str):
  99. bytes_value = bytearray(bytes_value, encoding='utf-8')
  100. escaped = '\\'.join([('%02x' % int(b)) for b in bytes_value])
  101. else: # Python 2
  102. if isinstance(bytes_value, unicode):
  103. bytes_value = bytes_value.encode('utf-8')
  104. escaped = '\\'.join([('%02x' % ord(b)) for b in bytes_value])
  105. else:
  106. escaped = ''
  107. return ('\\' + escaped) if escaped else ''
  108. def prepare_for_stream(value):
  109. if str is not bytes: # Python 3
  110. return value
  111. else: # Python 2
  112. return value.decode()
  113. def json_encode_b64(obj):
  114. try:
  115. return dict(encoding='base64', encoded=b64encode(obj))
  116. except Exception as e:
  117. raise LDAPDefinitionError('unable to encode ' + str(obj) + ' - ' + str(e))
  118. # noinspection PyProtectedMember
  119. def check_json_dict(json_dict):
  120. # needed for python 2
  121. for k, v in json_dict.items():
  122. if isinstance(v, dict):
  123. check_json_dict(v)
  124. elif isinstance(v, CaseInsensitiveDict):
  125. check_json_dict(v._store)
  126. elif isinstance(v, SEQUENCE_TYPES):
  127. for i, e in enumerate(v):
  128. if isinstance(e, dict):
  129. check_json_dict(e)
  130. elif isinstance(e, CaseInsensitiveDict):
  131. check_json_dict(e._store)
  132. else:
  133. v[i] = format_json(e)
  134. else:
  135. json_dict[k] = format_json(v)
  136. def json_hook(obj):
  137. if hasattr(obj, 'keys') and len(list(obj.keys())) == 2 and 'encoding' in obj.keys() and 'encoded' in obj.keys():
  138. return b64decode(obj['encoded'])
  139. return obj
  140. # noinspection PyProtectedMember
  141. def format_json(obj):
  142. if isinstance(obj, CaseInsensitiveDict):
  143. return obj._store
  144. if isinstance(obj, datetime.datetime):
  145. return str(obj)
  146. if isinstance(obj, int):
  147. return obj
  148. if str is bytes: # Python 2
  149. if isinstance(obj, long): # long exists only in python2
  150. return obj
  151. try:
  152. if str is not bytes: # Python 3
  153. if isinstance(obj, bytes):
  154. # return check_escape(str(obj, 'utf-8', errors='strict'))
  155. return str(obj, 'utf-8', errors='strict')
  156. raise LDAPDefinitionError('unable to serialize ' + str(obj))
  157. else: # Python 2
  158. if isinstance(obj, unicode):
  159. return obj
  160. else:
  161. # return unicode(check_escape(obj))
  162. return unicode(obj)
  163. except (TypeError, UnicodeDecodeError):
  164. pass
  165. try:
  166. return json_encode_b64(bytes(obj))
  167. except Exception:
  168. pass
  169. raise LDAPDefinitionError('unable to serialize ' + str(obj))
  170. def is_filter_escaped(text):
  171. if not type(text) == ((str is not bytes) and str or unicode): # requires str for Python 3 and unicode for Python 2
  172. raise ValueError('unicode input expected')
  173. return all(c not in text for c in '()*\0') and not re.search('\\\\([^0-9a-fA-F]|(.[^0-9a-fA-F]))', text)
  174. def ldap_escape_to_bytes(text):
  175. bytesequence = bytearray()
  176. if text.startswith('\\'):
  177. byte_values = text.split('\\')
  178. for value in byte_values[1:]:
  179. if len(value) != 2 and not value.isdigit():
  180. raise LDAPDefinitionError('badly formatted LDAP byte escaped sequence')
  181. bytesequence.append(int(value, 16))
  182. return bytes(bytesequence)
  183. raise LDAPDefinitionError('badly formatted LDAP byte escaped sequence')