ldap-dn.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .. % $Id: ldap-dn.rst,v 1.2 2009/04/17 12:14:52 stroeder Exp $
  2. :mod:`ldap.dn` LDAP Distinguished Name handling
  3. ====================================================
  4. .. module:: ldap.dn
  5. :synopsis: LDAP Distinguished Name handling.
  6. .. moduleauthor:: python-ldap project (see http://www.python-ldap.org/)
  7. .. % Author of the module code;
  8. .. seealso::
  9. For LDAPv3 DN syntax see:
  10. :rfc:`4514` - Lightweight Directory Access Protocol (LDAP): String Representation of Distinguished Names
  11. For LDAPv2 DN syntax (obsoleted by LDAPv3) see:
  12. :rfc:`1779` - A String Representation of Distinguished Names
  13. The :mod:`ldap.dn` module defines the following functions:
  14. .. function:: escape_dn_chars(s)
  15. This function escapes characters in string *s* which are special in LDAP
  16. distinguished names. You should use this function when building LDAP DN strings
  17. from arbitrary input.
  18. .. % -> string
  19. .. function:: str2dn(s [, flags=:const:`0`])
  20. This function takes *s* and breaks it up into its component parts down to AVA
  21. level. The optional parameter *flags* describes the DN format of s (see
  22. :ref:`ldap-dn-flags`).
  23. .. % -> list
  24. .. function:: dn2str(dn)
  25. This function takes a decomposed DN in *dn* and returns a single string. It's
  26. the inverse to :func:`str2dn`. Special characters are escaped with the help of
  27. function :func:`escape_dn_chars`.
  28. .. % -> string
  29. .. function:: explode_dn(dn [, notypes=:const:`0` [, flags=:const:`0`]])
  30. This function takes *dn* and breaks it up into its component parts. Each part
  31. is known as an RDN (Relative Distinguished Name). The optional *notypes*
  32. parameter is used to specify that only the RDN values be returned and not
  33. their types. The optional parameter *flags* describes the DN format of s (see
  34. :ref:`ldap-dn-flags`). This function is emulated by function
  35. :func:`str2dn` since the function ldap_explode_dn() in the C library is
  36. deprecated.
  37. .. % -> list
  38. .. function:: explode_rdn(rdn [, notypes=:const:`0` [, flags=:const:`0`]])
  39. This function takes a (multi-valued) *rdn* and breaks it up into a list of
  40. characteristic attributes. The optional *notypes* parameter is used to specify
  41. that only the RDN values be returned and not their types. The optional *flags*
  42. parameter describes the DN format of s (see :ref:`ldap-dn-flags`). This
  43. function is emulated by function :func:`str2dn` since the function
  44. ldap_explode_rdn() in the C library is deprecated.
  45. .. % -> list
  46. .. _ldap-dn-example:
  47. Examples
  48. ^^^^^^^^^
  49. Splitting a LDAPv3 DN to AVA level:
  50. >>> ldap.dn.str2dn('cn=Michael Str\xc3\xb6der,dc=stroeder,dc=com',flags=ldap.DN_FORMAT_LDAPV3)
  51. [[('cn', 'Michael Str\xc3\xb6der', 4)], [('dc', 'stroeder', 1)], [('dc', 'com', 1)]]
  52. Splitting a LDAPv2 DN into RDN parts:
  53. >>> ldap.dn.explode_dn('cn=Michael Stroeder;dc=stroeder;dc=com',flags=ldap.DN_FORMAT_LDAPV2)
  54. ['cn=Michael Stroeder', 'dc=stroeder', 'dc=com']
  55. Splitting a multi-valued RDN:
  56. >>> ldap.dn.explode_rdn('cn=Michael Stroeder+mail=michael@stroeder.com',flags=ldap.DN_FORMAT_LDAPV2)
  57. ['cn=Michael Stroeder', 'mail=michael@stroeder.com']
  58. Splitting a LDAPv3 DN with a multi-valued RDN into its AVA parts:
  59. >>> ldap.dn.str2dn('cn=Michael Stroeder+mail=michael@stroeder.com,dc=stroeder,dc=com')
  60. [[('cn', 'Michael Stroeder', 1), ('mail', 'michael@stroeder.com', 1)], [('dc', 'stroeder', 1)], [('dc', 'com', 1)]]