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.

dn.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. """
  2. """
  3. # Created on 2014.09.08
  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 string import hexdigits, ascii_letters, digits
  25. from .. import SEQUENCE_TYPES
  26. from ..core.exceptions import LDAPInvalidDnError
  27. STATE_ANY = 0
  28. STATE_ESCAPE = 1
  29. STATE_ESCAPE_HEX = 2
  30. def _add_ava(ava, decompose, remove_space, space_around_equal):
  31. if not ava:
  32. return ''
  33. space = ' ' if space_around_equal else ''
  34. attr_name, _, value = ava.partition('=')
  35. if decompose:
  36. if remove_space:
  37. component = (attr_name.strip(), value.strip())
  38. else:
  39. component = (attr_name, value)
  40. else:
  41. if remove_space:
  42. component = attr_name.strip() + space + '=' + space + value.strip()
  43. else:
  44. component = attr_name + space + '=' + space + value
  45. return component
  46. def to_dn(iterator, decompose=False, remove_space=False, space_around_equal=False, separate_rdn=False):
  47. """
  48. Convert an iterator to a list of dn parts
  49. if decompose=True return a list of tuple (one for each dn component) else return a list of strings
  50. if remove_space=True removes unneeded spaces
  51. if space_around_equal=True add spaces around equal in returned strings
  52. if separate_rdn=True consider multiple RDNs as different component of DN
  53. """
  54. dn = []
  55. component = ''
  56. escape_sequence = False
  57. for c in iterator:
  58. if c == '\\': # escape sequence
  59. escape_sequence = True
  60. elif escape_sequence and c != ' ':
  61. escape_sequence = False
  62. elif c == '+' and separate_rdn:
  63. dn.append(_add_ava(component, decompose, remove_space, space_around_equal))
  64. component = ''
  65. continue
  66. elif c == ',':
  67. if '=' in component:
  68. dn.append(_add_ava(component, decompose, remove_space, space_around_equal))
  69. component = ''
  70. continue
  71. component += c
  72. dn.append(_add_ava(component, decompose, remove_space, space_around_equal))
  73. return dn
  74. def _find_first_unescaped(dn, char, pos):
  75. while True:
  76. pos = dn.find(char, pos)
  77. if pos == -1:
  78. break # no char found
  79. if pos > 0 and dn[pos - 1] != '\\': # unescaped char
  80. break
  81. pos += 1
  82. return pos
  83. def _find_last_unescaped(dn, char, start, stop=0):
  84. while True:
  85. stop = dn.rfind(char, start, stop)
  86. if stop == -1:
  87. break
  88. if stop >= 0 and dn[stop - 1] != '\\':
  89. break
  90. if stop < start:
  91. stop = -1
  92. break
  93. return stop
  94. def _get_next_ava(dn):
  95. comma = _find_first_unescaped(dn, ',', 0)
  96. plus = _find_first_unescaped(dn, '+', 0)
  97. if plus > 0 and (plus < comma or comma == -1):
  98. equal = _find_first_unescaped(dn, '=', plus + 1)
  99. if equal > plus + 1:
  100. plus = _find_last_unescaped(dn, '+', plus, equal)
  101. return dn[:plus], '+'
  102. if comma > 0:
  103. equal = _find_first_unescaped(dn, '=', comma + 1)
  104. if equal > comma + 1:
  105. comma = _find_last_unescaped(dn, ',', comma, equal)
  106. return dn[:comma], ','
  107. return dn, ''
  108. def _split_ava(ava, escape=False, strip=True):
  109. equal = ava.find('=')
  110. while equal > 0: # not first character
  111. if ava[equal - 1] != '\\': # not an escaped equal so it must be an ava separator
  112. # attribute_type1 = ava[0:equal].strip() if strip else ava[0:equal]
  113. if strip:
  114. attribute_type = ava[0:equal].strip()
  115. attribute_value = _escape_attribute_value(ava[equal + 1:].strip()) if escape else ava[equal + 1:].strip()
  116. else:
  117. attribute_type = ava[0:equal]
  118. attribute_value = _escape_attribute_value(ava[equal + 1:]) if escape else ava[equal + 1:]
  119. return attribute_type, attribute_value
  120. equal = ava.find('=', equal + 1)
  121. return '', (ava.strip if strip else ava) # if no equal found return only value
  122. def _validate_attribute_type(attribute_type):
  123. if not attribute_type:
  124. raise LDAPInvalidDnError('attribute type not present')
  125. if attribute_type == '<GUID': # patch for AD DirSync
  126. return True
  127. for c in attribute_type:
  128. if not (c in ascii_letters or c in digits or c == '-'): # allowed uppercase and lowercase letters, digits and hyphen as per RFC 4512
  129. raise LDAPInvalidDnError('character \'' + c + '\' not allowed in attribute type')
  130. if attribute_type[0] in digits or attribute_type[0] == '-': # digits and hyphen not allowed as first character
  131. raise LDAPInvalidDnError('character \'' + attribute_type[0] + '\' not allowed as first character of attribute type')
  132. return True
  133. def _validate_attribute_value(attribute_value):
  134. if not attribute_value:
  135. return False
  136. if attribute_value[0] == '#': # only hex characters are valid
  137. for c in attribute_value:
  138. if 'c' not in hexdigits: # allowed only hex digits as per RFC 4514
  139. raise LDAPInvalidDnError('character ' + c + ' not allowed in hex representation of attribute value')
  140. if len(attribute_value) % 2 == 0: # string must be # + HEX HEX (an odd number of chars)
  141. raise LDAPInvalidDnError('hex representation must be in the form of <HEX><HEX> pairs')
  142. if attribute_value[0] == ' ': # space cannot be used as first or last character
  143. raise LDAPInvalidDnError('SPACE not allowed as first character of attribute value')
  144. if attribute_value[-1] == ' ':
  145. raise LDAPInvalidDnError('SPACE not allowed as last character of attribute value')
  146. state = STATE_ANY
  147. for c in attribute_value:
  148. if state == STATE_ANY:
  149. if c == '\\':
  150. state = STATE_ESCAPE
  151. elif c in '"#+,;<=>\00':
  152. raise LDAPInvalidDnError('special characters ' + c + ' must be escaped')
  153. elif state == STATE_ESCAPE:
  154. if c in hexdigits:
  155. state = STATE_ESCAPE_HEX
  156. elif c in ' "#+,;<=>\\\00':
  157. state = STATE_ANY
  158. else:
  159. raise LDAPInvalidDnError('invalid escaped character ' + c)
  160. elif state == STATE_ESCAPE_HEX:
  161. if c in hexdigits:
  162. state = STATE_ANY
  163. else:
  164. raise LDAPInvalidDnError('invalid escaped character ' + c)
  165. # final state
  166. if state != STATE_ANY:
  167. raise LDAPInvalidDnError('invalid final character')
  168. return True
  169. def _escape_attribute_value(attribute_value):
  170. if not attribute_value:
  171. return ''
  172. if attribute_value[0] == '#': # with leading SHARP only pairs of hex characters are valid
  173. valid_hex = True
  174. if len(attribute_value) % 2 == 0: # string must be # + HEX HEX (an odd number of chars)
  175. valid_hex = False
  176. if valid_hex:
  177. for c in attribute_value:
  178. if c not in hexdigits: # allowed only hex digits as per RFC 4514
  179. valid_hex = False
  180. break
  181. if valid_hex:
  182. return attribute_value
  183. state = STATE_ANY
  184. escaped = ''
  185. tmp_buffer = ''
  186. for c in attribute_value:
  187. if state == STATE_ANY:
  188. if c == '\\':
  189. state = STATE_ESCAPE
  190. elif c in '"#+,;<=>\00':
  191. escaped += '\\' + c
  192. else:
  193. escaped += c
  194. elif state == STATE_ESCAPE:
  195. if c in hexdigits:
  196. tmp_buffer = c
  197. state = STATE_ESCAPE_HEX
  198. elif c in ' "#+,;<=>\\\00':
  199. escaped += '\\' + c
  200. state = STATE_ANY
  201. else:
  202. escaped += '\\\\' + c
  203. elif state == STATE_ESCAPE_HEX:
  204. if c in hexdigits:
  205. escaped += '\\' + tmp_buffer + c
  206. else:
  207. escaped += '\\\\' + tmp_buffer + c
  208. tmp_buffer = ''
  209. state = STATE_ANY
  210. # final state
  211. if state == STATE_ESCAPE:
  212. escaped += '\\\\'
  213. elif state == STATE_ESCAPE_HEX:
  214. escaped += '\\\\' + tmp_buffer
  215. if escaped[0] == ' ': # leading SPACE must be escaped
  216. escaped = '\\' + escaped
  217. if escaped[-1] == ' ' and len(escaped) > 1 and escaped[-2] != '\\': # trailing SPACE must be escaped
  218. escaped = escaped[:-1] + '\\ '
  219. return escaped
  220. def parse_dn(dn, escape=False, strip=True):
  221. rdns = []
  222. avas = []
  223. while dn:
  224. ava, separator = _get_next_ava(dn) # if returned ava doesn't containg any unescaped equal it'a appended to last ava in avas
  225. dn = dn[len(ava) + 1:]
  226. if _find_first_unescaped(ava, '=', 0) > 0 or len(avas) == 0:
  227. avas.append((ava, separator))
  228. else:
  229. avas[len(avas) - 1] = (avas[len(avas) - 1][0] + avas[len(avas) - 1][1] + ava, separator)
  230. for ava, separator in avas:
  231. attribute_type, attribute_value = _split_ava(ava, escape, strip)
  232. if not _validate_attribute_type(attribute_type):
  233. raise LDAPInvalidDnError('unable to validate attribute type in ' + ava)
  234. if not _validate_attribute_value(attribute_value):
  235. raise LDAPInvalidDnError('unable to validate attribute value in ' + ava)
  236. rdns.append((attribute_type, attribute_value, separator))
  237. dn = dn[len(ava) + 1:]
  238. if not rdns:
  239. raise LDAPInvalidDnError('empty dn')
  240. return rdns
  241. def safe_dn(dn, decompose=False, reverse=False):
  242. """
  243. normalize and escape a dn, if dn is a sequence it is joined.
  244. the reverse parameter changes the join direction of the sequence
  245. """
  246. if isinstance(dn, SEQUENCE_TYPES):
  247. components = [rdn for rdn in dn]
  248. if reverse:
  249. dn = ','.join(reversed(components))
  250. else:
  251. dn = ','.join(components)
  252. if decompose:
  253. escaped_dn = []
  254. else:
  255. escaped_dn = ''
  256. if dn.startswith('<GUID=') and dn.endswith('>'): # Active Directory allows looking up objects by putting its GUID in a specially-formatted DN (e.g. '<GUID=7b95f0d5-a3ed-486c-919c-077b8c9731f2>')
  257. escaped_dn = dn
  258. elif '@' not in dn and '\\' not in dn: # active directory UPN (User Principal Name) consist of an account, the at sign (@) and a domain, or the domain level logn name domain\username
  259. for component in parse_dn(dn, escape=True):
  260. if decompose:
  261. escaped_dn.append((component[0], component[1], component[2]))
  262. else:
  263. escaped_dn += component[0] + '=' + component[1] + component[2]
  264. elif '@' in dn and '=' not in dn and len(dn.split('@')) != 2:
  265. raise LDAPInvalidDnError('Active Directory User Principal Name must consist of name@domain')
  266. elif '\\' in dn and '=' not in dn and len(dn.split('\\')) != 2:
  267. raise LDAPInvalidDnError('Active Directory Domain Level Logon Name must consist of name\\domain')
  268. else:
  269. escaped_dn = dn
  270. return escaped_dn
  271. def safe_rdn(dn, decompose=False):
  272. """Returns a list of rdn for the dn, usually there is only one rdn, but it can be more than one when the + sign is used"""
  273. escaped_rdn = []
  274. one_more = True
  275. for component in parse_dn(dn, escape=True):
  276. if component[2] == '+' or one_more:
  277. if decompose:
  278. escaped_rdn.append((component[0], component[1]))
  279. else:
  280. escaped_rdn.append(component[0] + '=' + component[1])
  281. if component[2] == '+':
  282. one_more = True
  283. else:
  284. one_more = False
  285. break
  286. if one_more:
  287. raise LDAPInvalidDnError('bad dn ' + str(dn))
  288. return escaped_rdn
  289. def escape_rdn(rdn):
  290. """
  291. Escape rdn characters to prevent injection according to RFC 4514.
  292. """
  293. # '/' must be handled first or the escape slashes will be escaped!
  294. for char in ['\\', ',', '+', '"', '<', '>', ';', '=', '\x00']:
  295. rdn = rdn.replace(char, '\\' + char)
  296. if rdn[0] == '#' or rdn[0] == ' ':
  297. rdn = ''.join(('\\', rdn))
  298. if rdn[-1] == ' ':
  299. rdn = ''.join((rdn[:-1], '\\ '))
  300. return rdn