ldap.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. pygments.lexers.ldap
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Pygments lexers for LDAP.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups, default
  10. from pygments.token import Operator, Comment, Keyword, Literal, Name, String, \
  11. Number, Punctuation, Whitespace, Escape
  12. __all__ = ['LdifLexer', 'LdaprcLexer']
  13. class LdifLexer(RegexLexer):
  14. """
  15. Lexer for LDIF
  16. """
  17. name = 'LDIF'
  18. aliases = ['ldif']
  19. filenames = ['*.ldif']
  20. mimetypes = ["text/x-ldif"]
  21. url = "https://datatracker.ietf.org/doc/html/rfc2849"
  22. version_added = '2.17'
  23. tokens = {
  24. 'root': [
  25. (r'\s*\n', Whitespace),
  26. (r'(-)(\n)', bygroups(Punctuation, Whitespace)),
  27. (r'(#.*)(\n)', bygroups(Comment.Single, Whitespace)),
  28. (r'(version)(:)([ \t]*)(.*)([ \t]*\n)', bygroups(Keyword,
  29. Punctuation, Whitespace, Number.Integer, Whitespace)),
  30. (r'(control)(:)([ \t]*)([\.0-9]+)([ \t]+)((?:true|false)?)([ \t]*)',
  31. bygroups(Keyword, Punctuation, Whitespace, Name.Other, Whitespace, Keyword, Whitespace), "after-control"),
  32. (r'(deleteoldrdn)(:)([ \n]*)([0-1]+)([ \t]*\n)',
  33. bygroups(Keyword, Punctuation, Whitespace, Number, Whitespace)),
  34. (r'(add|delete|replace)(::?)(\s*)(.*)([ \t]*\n)', bygroups(
  35. Keyword, Punctuation, Whitespace, Name.Attribute, Whitespace)),
  36. (r'(changetype)(:)([ \t]*)([a-z]*)([ \t]*\n)',
  37. bygroups(Keyword, Punctuation, Whitespace, Keyword, Whitespace)),
  38. (r'(dn|newrdn)(::)', bygroups(Keyword, Punctuation), "base64-dn"),
  39. (r'(dn|newrdn)(:)', bygroups(Keyword, Punctuation), "dn"),
  40. (r'(objectclass)(:)([ \t]*)([^ \t\n]*)([ \t]*\n)', bygroups(
  41. Keyword, Punctuation, Whitespace, Name.Class, Whitespace)),
  42. (r'([a-zA-Z]*|[0-9][0-9\.]*[0-9])(;)',
  43. bygroups(Name.Attribute, Punctuation), "property"),
  44. (r'([a-zA-Z]*|[0-9][0-9\.]*[0-9])(:<)',
  45. bygroups(Name.Attribute, Punctuation), "url"),
  46. (r'([a-zA-Z]*|[0-9][0-9\.]*[0-9])(::?)',
  47. bygroups(Name.Attribute, Punctuation), "value"),
  48. ],
  49. "after-control": [
  50. (r":<", Punctuation, ("#pop", "url")),
  51. (r"::?", Punctuation, ("#pop", "value")),
  52. default("#pop"),
  53. ],
  54. 'property': [
  55. (r'([-a-zA-Z0-9]*)(;)', bygroups(Name.Property, Punctuation)),
  56. (r'([-a-zA-Z0-9]*)(:<)',
  57. bygroups(Name.Property, Punctuation), ("#pop", "url")),
  58. (r'([-a-zA-Z0-9]*)(::?)',
  59. bygroups(Name.Property, Punctuation), ("#pop", "value")),
  60. ],
  61. 'value': [
  62. (r'(\s*)([^\n]+\S)(\n )',
  63. bygroups(Whitespace, String, Whitespace)),
  64. (r'(\s*)([^\n]+\S)(\n)',
  65. bygroups(Whitespace, String, Whitespace), "#pop"),
  66. ],
  67. 'url': [
  68. (r'([ \t]*)(\S*)([ \t]*\n )',
  69. bygroups(Whitespace, Comment.PreprocFile, Whitespace)),
  70. (r'([ \t]*)(\S*)([ \t]*\n)', bygroups(Whitespace,
  71. Comment.PreprocFile, Whitespace), "#pop"),
  72. ],
  73. "dn": [
  74. (r'([ \t]*)([-a-zA-Z0-9\.]+)(=)', bygroups(Whitespace,
  75. Name.Attribute, Operator), ("#pop", "dn-value")),
  76. ],
  77. "dn-value": [
  78. (r'\\[^\n]', Escape),
  79. (r',', Punctuation, ("#pop", "dn")),
  80. (r'\+', Operator, ("#pop", "dn")),
  81. (r'[^,\+\n]+', String),
  82. (r'\n ', Whitespace),
  83. (r'\n', Whitespace, "#pop"),
  84. ],
  85. "base64-dn": [
  86. (r'([ \t]*)([^ \t\n][^ \t\n]*[^\n])([ \t]*\n )',
  87. bygroups(Whitespace, Name, Whitespace)),
  88. (r'([ \t]*)([^ \t\n][^ \t\n]*[^\n])([ \t]*\n)',
  89. bygroups(Whitespace, Name, Whitespace), "#pop"),
  90. ]
  91. }
  92. class LdaprcLexer(RegexLexer):
  93. """
  94. Lexer for OpenLDAP configuration files.
  95. """
  96. name = 'LDAP configuration file'
  97. aliases = ['ldapconf', 'ldaprc']
  98. filenames = ['.ldaprc', 'ldaprc', 'ldap.conf']
  99. mimetypes = ["text/x-ldapconf"]
  100. url = 'https://www.openldap.org/software//man.cgi?query=ldap.conf&sektion=5&apropos=0&manpath=OpenLDAP+2.4-Release'
  101. version_added = '2.17'
  102. _sasl_keywords = r'SASL_(?:MECH|REALM|AUTHCID|AUTHZID|CBINDING)'
  103. _tls_keywords = r'TLS_(?:CACERT|CACERTDIR|CERT|ECNAME|KEY|CIPHER_SUITE|PROTOCOL_MIN|RANDFILE|CRLFILE)'
  104. _literal_keywords = rf'(?:URI|SOCKET_BIND_ADDRESSES|{_sasl_keywords}|{_tls_keywords})'
  105. _boolean_keywords = r'GSSAPI_(?:ALLOW_REMOTE_PRINCIPAL|ENCRYPT|SIGN)|REFERRALS|SASL_NOCANON'
  106. _integer_keywords = r'KEEPALIVE_(?:IDLE|PROBES|INTERVAL)|NETWORK_TIMEOUT|PORT|SIZELIMIT|TIMELIMIT|TIMEOUT'
  107. _secprops = r'none|noanonymous|noplain|noactive|nodict|forwardsec|passcred|(?:minssf|maxssf|maxbufsize)=\d+'
  108. flags = re.IGNORECASE | re.MULTILINE
  109. tokens = {
  110. 'root': [
  111. (r'#.*', Comment.Single),
  112. (r'\s+', Whitespace),
  113. (rf'({_boolean_keywords})(\s+)(on|true|yes|off|false|no)$',
  114. bygroups(Keyword, Whitespace, Keyword.Constant)),
  115. (rf'({_integer_keywords})(\s+)(\d+)',
  116. bygroups(Keyword, Whitespace, Number.Integer)),
  117. (r'(VERSION)(\s+)(2|3)', bygroups(Keyword, Whitespace, Number.Integer)),
  118. # Constants
  119. (r'(DEREF)(\s+)(never|searching|finding|always)',
  120. bygroups(Keyword, Whitespace, Keyword.Constant)),
  121. (rf'(SASL_SECPROPS)(\s+)((?:{_secprops})(?:,{_secprops})*)',
  122. bygroups(Keyword, Whitespace, Keyword.Constant)),
  123. (r'(SASL_CBINDING)(\s+)(none|tls-unique|tls-endpoint)',
  124. bygroups(Keyword, Whitespace, Keyword.Constant)),
  125. (r'(TLS_REQ(?:CERT|SAN))(\s+)(allow|demand|hard|never|try)',
  126. bygroups(Keyword, Whitespace, Keyword.Constant)),
  127. (r'(TLS_CRLCHECK)(\s+)(none|peer|all)',
  128. bygroups(Keyword, Whitespace, Keyword.Constant)),
  129. # Literals
  130. (r'(BASE|BINDDN)(\s+)(\S+)$',
  131. bygroups(Keyword, Whitespace, Literal)),
  132. # Accepts hostname with or without port.
  133. (r'(HOST)(\s+)([a-z0-9]+)((?::(\d+))?)',
  134. bygroups(Keyword, Whitespace, Literal, Number.Integer)),
  135. (rf'({_literal_keywords})(\s+)(\S+)$',
  136. bygroups(Keyword, Whitespace, Literal)),
  137. ],
  138. }