syntax.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /* syntax.h - string syntax definitions */
  13. #ifndef _LIBSYNTAX_H_
  14. #define _LIBSYNTAX_H_
  15. #include "slap.h"
  16. #include "slapi-plugin.h"
  17. #define SYNTAX_CIS 1
  18. #define SYNTAX_CES 2
  19. #define SYNTAX_TEL 4 /* telephone number: used with SYNTAX_CIS */
  20. #define SYNTAX_DN 8 /* distinguished name: used with SYNTAX_CIS */
  21. #define SYNTAX_SI 16 /* space insensitive: used with SYNTAX_CIS */
  22. #define SYNTAX_INT 32 /* INTEGER */
  23. #define SYNTAX_NORM_FILT 64 /* filter already normalized */
  24. #define SUBBEGIN 3
  25. #define SUBMIDDLE 3
  26. #define SUBEND 3
  27. #define SYNTAX_PLUGIN_SUBSYSTEM "syntax-plugin"
  28. /* The following are derived from RFC 4512, section 1.4. */
  29. #define IS_LEADKEYCHAR(c) (isalpha(c))
  30. #define IS_KEYCHAR(c) (isalnum(c) || (c == '-'))
  31. #define IS_SPACE(c) ((c == ' '))
  32. #define IS_LDIGIT(c) ((c != '0') && isdigit(c))
  33. #define IS_SHARP(c) ((c == '#'))
  34. #define IS_DOLLAR(c) ((c == '$'))
  35. #define IS_SQUOTE(c) ((c == '\''))
  36. #define IS_ESC(c) ((c == '\\'))
  37. #define IS_LPAREN(c) ((c == '('))
  38. #define IS_RPAREN(c) ((c == ')'))
  39. #define IS_COLON(c) ((c == ':'))
  40. #define IS_UTF0(c) (((unsigned char)(c) >= (unsigned char)'\x80') && ((unsigned char)(c) <= (unsigned char)'\xBF'))
  41. #define IS_UTF1(c) (!((unsigned char)(c)&128))
  42. /* These are only checking the first byte of the multibyte character. They
  43. * do not verify that the entire multibyte character is correct. */
  44. #define IS_UTF2(c) (((unsigned char)(c) >= (unsigned char)'\xC2') && ((unsigned char)(c) <= (unsigned char)'\xDF'))
  45. #define IS_UTF3(c) (((unsigned char)(c) >= (unsigned char)'\xE0') && ((unsigned char)(c) <= (unsigned char)'\xEF'))
  46. #define IS_UTF4(c) (((unsigned char)(c) >= (unsigned char)'\xF0') && ((unsigned char)(c) <= (unsigned char)'\xF4'))
  47. #define IS_UTFMB(c) (IS_UTF2(c) || IS_UTF3(c) || IS_UTF4(c))
  48. #define IS_UTF8(c) (IS_UTF1(c) || IS_UTFMB(c))
  49. /* The following are derived from RFC 4514, section 3. */
  50. #define IS_ESCAPED(c) ((c == '"') || (c == '+') || (c == ',') || \
  51. (c == ';') || (c == '<') || (c == '>'))
  52. #define IS_SPECIAL(c) (IS_ESCAPED(c) || IS_SPACE(c) || \
  53. IS_SHARP(c) || (c == '='))
  54. #define IS_LUTF1(c) (IS_UTF1(c) && !IS_ESCAPED(c) && !IS_SPACE(c) && \
  55. !IS_SHARP(c) && !IS_ESC(c))
  56. #define IS_TUTF1(c) (IS_UTF1(c) && !IS_ESCAPED(c) && !IS_SPACE(c) && \
  57. !IS_ESC(c))
  58. #define IS_SUTF1(c) (IS_UTF1(c) && !IS_ESCAPED(c) && !IS_ESC(c))
  59. /* Per RFC 4517:
  60. *
  61. * PrintableCharacter = ALPHA / DIGIT / SQUOTE / LPAREN / RPAREN /
  62. * PLUS / COMMA / HYPHEN / DOT / EQUALS /
  63. * SLASH / COLON / QUESTION / SPACE
  64. */
  65. #define IS_PRINTABLE(c) (isalnum(c) || (c == '\'') || IS_LPAREN(c) || \
  66. IS_RPAREN(c) || (c == '+') || (c == ',') || (c == '-') || (c == '.') || \
  67. (c == '=') || (c == '/') || (c == ':') || (c == '?') || IS_SPACE(c))
  68. int string_filter_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value **bvals, int syntax);
  69. int string_filter_ava(struct berval *bvfilter, Slapi_Value **bvals, int syntax, int ftype, Slapi_Value **retVal);
  70. int string_values2keys(Slapi_PBlock *pb, Slapi_Value **bvals, Slapi_Value ***ivals, int syntax, int ftype);
  71. int string_assertion2keys_ava(Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int syntax, int ftype);
  72. int string_assertion2keys_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value ***ivals, int syntax);
  73. int value_cmp(struct berval *v1, struct berval *v2, int syntax, int normalize);
  74. void value_normalize(char *s, int syntax, int trim_leading_blanks);
  75. void value_normalize_ext(char *s, int syntax, int trim_leading_blanks, char **alt);
  76. char *first_word(char *s);
  77. char *next_word(char *s);
  78. char *phonetic(char *s);
  79. /* Validation helper functions */
  80. int keystring_validate(const char *begin, const char *end);
  81. int numericoid_validate(const char *begin, const char *end);
  82. int utf8char_validate(const char *begin, const char *end, const char **last);
  83. int utf8string_validate(const char *begin, const char *end, const char **last);
  84. int distinguishedname_validate(const char *begin, const char *end);
  85. int rdn_validate(const char *begin, const char *end, const char **last);
  86. int bitstring_validate_internal(const char *begin, const char *end);
  87. struct mr_plugin_def
  88. {
  89. Slapi_MatchingRuleEntry mr_def_entry; /* for slapi_matchingrule_register */
  90. Slapi_PluginDesc mr_plg_desc; /* for SLAPI_PLUGIN_DESCRIPTION */
  91. const char **mr_names; /* list of oid and names, NULL terminated SLAPI_PLUGIN_MR_NAMES */
  92. /* these are optional for new style mr plugins */
  93. IFP mr_filter_create; /* old style factory function SLAPI_PLUGIN_MR_FILTER_CREATE_FN */
  94. IFP mr_indexer_create; /* old style factory function SLAPI_PLUGIN_MR_INDEXER_CREATE_FN */
  95. /* new style syntax plugin functions */
  96. /* not all functions will apply to all matching rule types */
  97. /* e.g. a SUBSTR rule will not have a filter_ava func */
  98. IFP mr_filter_ava; /* SLAPI_PLUGIN_MR_FILTER_AVA */
  99. IFP mr_filter_sub; /* SLAPI_PLUGIN_MR_FILTER_SUB */
  100. IFP mr_values2keys; /* SLAPI_PLUGIN_MR_VALUES2KEYS */
  101. IFP mr_assertion2keys_ava; /* SLAPI_PLUGIN_MR_ASSERTION2KEYS_AVA */
  102. IFP mr_assertion2keys_sub; /* SLAPI_PLUGIN_MR_ASSERTION2KEYS_SUB */
  103. IFP mr_compare; /* SLAPI_PLUGIN_MR_COMPARE - only for ORDERING */
  104. VFPV mr_normalize;
  105. };
  106. int syntax_register_matching_rule_plugins(struct mr_plugin_def mr_plugin_table[], size_t mr_plugin_table_size, IFP matching_rule_plugin_init);
  107. int syntax_matching_rule_plugin_init(Slapi_PBlock *pb, struct mr_plugin_def mr_plugin_table[], size_t mr_plugin_table_size);
  108. #endif
  109. #ifdef UNSUPPORTED_MATCHING_RULES
  110. /* list of names/oids/aliases for each matching rule */
  111. static const char *keywordMatch_names[] = {"keywordMatch", "2.5.13.33", NULL};
  112. static const char *wordMatch_names[] = {"wordMatch", "2.5.13.32", NULL};
  113. /* table of matching rule plugin defs for mr register and plugin register */
  114. static struct mr_plugin_def mr_plugin_table[] = {
  115. {{"2.5.13.33",
  116. NULL,
  117. "keywordMatch",
  118. "The keywordMatch rule compares an assertion value of the Directory"
  119. "String syntax to an attribute value of a syntax (e.g., the Directory"
  120. "String syntax) whose corresponding ASN.1 type is DirectoryString."
  121. "The rule evaluates to TRUE if and only if the assertion value"
  122. "character string matches any keyword in the attribute value. The"
  123. "identification of keywords in the attribute value and the exactness"
  124. "of the match are both implementation specific.",
  125. "1.3.6.1.4.1.1466.115.121.1.15",
  126. 0}, /* matching rule desc */
  127. {
  128. "keywordMatch-mr",
  129. VENDOR,
  130. DS_PACKAGE_VERSION,
  131. "keywordMatch matching rule plugin"}, /* plugin desc */
  132. keywordMatch_names, /* matching rule name/oid/aliases */
  133. NULL,
  134. NULL,
  135. mr_filter_ava,
  136. mr_filter_sub,
  137. mr_values2keys,
  138. mr_assertion2keys_ava,
  139. mr_assertion2keys_sub,
  140. mr_compare,
  141. keywordMatch_syntaxes},
  142. {{"2.5.13.32",
  143. NULL,
  144. "wordMatch",
  145. "The wordMatch rule compares an assertion value of the Directory"
  146. "String syntax to an attribute value of a syntax (e.g., the Directory"
  147. "String syntax) whose corresponding ASN.1 type is DirectoryString."
  148. "The rule evaluates to TRUE if and only if the assertion value word"
  149. "matches, according to the semantics of caseIgnoreMatch, any word in"
  150. "the attribute value. The precise definition of a word is"
  151. "implementation specific.",
  152. "1.3.6.1.4.1.1466.115.121.1.15",
  153. 0}, /* matching rule desc */
  154. {
  155. "wordMatch-mr",
  156. VENDOR,
  157. DS_PACKAGE_VERSION,
  158. "wordMatch matching rule plugin"}, /* plugin desc */
  159. wordMatch_names, /* matching rule name/oid/aliases */
  160. NULL,
  161. NULL,
  162. mr_filter_ava,
  163. mr_filter_sub,
  164. mr_values2keys,
  165. mr_assertion2keys_ava,
  166. mr_assertion2keys_sub,
  167. mr_compare,
  168. wordMatch_syntaxes},
  169. };
  170. #endif /* UNSUPPORTED_MATCHING_RULES */