numericstring.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2009 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. /* numericstring.c - Numeric String syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int numstr_filter_ava(Slapi_PBlock *pb, struct berval *bvfilter, Slapi_Value **bvals, int ftype, Slapi_Value **retVal);
  18. static int numstr_filter_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value **bvals);
  19. static int numstr_values2keys(Slapi_PBlock *pb, Slapi_Value **val, Slapi_Value ***ivals, int ftype);
  20. static int numstr_assertion2keys(Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype);
  21. static int numstr_assertion2keys_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value ***ivals);
  22. static int numstr_compare(struct berval *v1, struct berval *v2);
  23. static int numstr_validate(struct berval *val);
  24. static void numstr_normalize(
  25. Slapi_PBlock *pb,
  26. char *s,
  27. int trim_spaces,
  28. char **alt);
  29. /* the first name is the official one from RFC 4517 */
  30. static char *names[] = {"Numeric String", "numstr", NUMERICSTRING_SYNTAX_OID, 0};
  31. #define NUMERICSTRINGMATCH_OID "2.5.13.8"
  32. #define NUMERICSTRINGORDERINGMATCH_OID "2.5.13.9"
  33. #define NUMERICSTRINGSUBSTRINGSMATCH_OID "2.5.13.10"
  34. static Slapi_PluginDesc pdesc = {"numstr-syntax", VENDOR,
  35. DS_PACKAGE_VERSION, "numeric string attribute syntax plugin"};
  36. static const char *numericStringMatch_names[] = {"numericStringMatch", NUMERICSTRINGMATCH_OID, NULL};
  37. static const char *numericStringOrderingMatch_names[] = {"numericStringOrderingMatch", NUMERICSTRINGORDERINGMATCH_OID, NULL};
  38. static const char *numericStringSubstringsMatch_names[] = {"numericStringSubstringsMatch", NUMERICSTRINGSUBSTRINGSMATCH_OID, NULL};
  39. static char *numericStringSubstringsMatch_syntaxes[] = {NUMERICSTRING_SYNTAX_OID, NULL};
  40. static struct mr_plugin_def mr_plugin_table[] = {
  41. {
  42. {
  43. NUMERICSTRINGMATCH_OID,
  44. NULL /* no alias? */,
  45. "numericStringMatch",
  46. "The rule evaluates to TRUE if and only if the prepared "
  47. "attribute value character string and the prepared assertion value character "
  48. "string have the same number of characters and corresponding characters have "
  49. "the same code point.",
  50. NUMERICSTRING_SYNTAX_OID,
  51. 0 /* not obsolete */,
  52. NULL /* numstr syntax only for now */
  53. },
  54. {"numericStringMatch-mr",
  55. VENDOR,
  56. DS_PACKAGE_VERSION,
  57. "numericStringMatch matching rule plugin"}, /* plugin desc */
  58. numericStringMatch_names, /* matching rule name/oid/aliases */
  59. NULL,
  60. NULL,
  61. numstr_filter_ava,
  62. NULL,
  63. numstr_values2keys,
  64. numstr_assertion2keys,
  65. NULL,
  66. numstr_compare,
  67. NULL /* mr_normalise */
  68. },
  69. {
  70. {
  71. NUMERICSTRINGORDERINGMATCH_OID,
  72. NULL /* no alias? */,
  73. "numericStringOrderingMatch",
  74. "The rule evaluates to TRUE if and only if, "
  75. "in the code point collation order, the prepared attribute value character "
  76. "string appears earlier than the prepared assertion value character string; "
  77. "i.e., the attribute value is less than the assertion value.",
  78. NUMERICSTRING_SYNTAX_OID,
  79. 0 /* not obsolete */,
  80. NULL /* numstr syntax only for now */
  81. },
  82. {"numericStringOrderingMatch-mr",
  83. VENDOR,
  84. DS_PACKAGE_VERSION,
  85. "numericStringOrderingMatch matching rule plugin"}, /* plugin desc */
  86. numericStringOrderingMatch_names, /* matching rule name/oid/aliases */
  87. NULL,
  88. NULL,
  89. numstr_filter_ava,
  90. NULL,
  91. numstr_values2keys,
  92. numstr_assertion2keys,
  93. NULL,
  94. numstr_compare,
  95. NULL /* mr_normalise */
  96. },
  97. {
  98. {NUMERICSTRINGSUBSTRINGSMATCH_OID,
  99. NULL /* no alias? */,
  100. "numericStringSubstringsMatch",
  101. "The rule evaluates to TRUE if and only if (1) "
  102. "the prepared substrings of the assertion value match disjoint portions of "
  103. "the prepared attribute value, (2) an initial substring, if present, matches "
  104. "the beginning of the prepared attribute value character string, and (3) a "
  105. "final substring, if present, matches the end of the prepared attribute value "
  106. "character string.",
  107. "1.3.6.1.4.1.1466.115.121.1.58",
  108. 0 /* not obsolete */,
  109. numericStringSubstringsMatch_syntaxes}, /* matching rule desc */
  110. {
  111. "numericStringSubstringsMatch-mr",
  112. VENDOR,
  113. DS_PACKAGE_VERSION,
  114. "numericStringSubstringsMatch matching rule plugin"}, /* plugin desc */
  115. numericStringSubstringsMatch_names, /* matching rule name/oid/aliases */
  116. NULL,
  117. NULL,
  118. NULL,
  119. numstr_filter_sub,
  120. numstr_values2keys,
  121. NULL,
  122. numstr_assertion2keys_sub,
  123. numstr_compare,
  124. NULL /* mr_normalise */
  125. },
  126. };
  127. static size_t mr_plugin_table_size = sizeof(mr_plugin_table) / sizeof(mr_plugin_table[0]);
  128. static int
  129. matching_rule_plugin_init(Slapi_PBlock *pb)
  130. {
  131. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  132. }
  133. static int
  134. register_matching_rule_plugins(void)
  135. {
  136. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  137. }
  138. int
  139. numstr_init(Slapi_PBlock *pb)
  140. {
  141. int rc, flags;
  142. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "=> numstr_init\n");
  143. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  144. (void *)SLAPI_PLUGIN_VERSION_01);
  145. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  146. (void *)&pdesc);
  147. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  148. (void *)numstr_filter_ava);
  149. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  150. (void *)numstr_values2keys);
  151. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  152. (void *)numstr_assertion2keys);
  153. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  154. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  155. (void *)&flags);
  156. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  157. (void *)names);
  158. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_OID,
  159. (void *)NUMERICSTRING_SYNTAX_OID);
  160. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  161. (void *)numstr_compare);
  162. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  163. (void *)numstr_validate);
  164. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  165. (void *)numstr_normalize);
  166. rc |= register_matching_rule_plugins();
  167. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "<= numstr_init %d\n", rc);
  168. return (rc);
  169. }
  170. static int
  171. numstr_filter_ava(Slapi_PBlock *pb, struct berval *bvfilter, Slapi_Value **bvals, int ftype, Slapi_Value **retVal)
  172. {
  173. int filter_normalized = 0;
  174. int syntax = SYNTAX_SI | SYNTAX_CES;
  175. if (pb) {
  176. slapi_pblock_get(pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  177. &filter_normalized);
  178. if (filter_normalized) {
  179. syntax |= SYNTAX_NORM_FILT;
  180. }
  181. }
  182. return (string_filter_ava(bvfilter, bvals, syntax,
  183. ftype, retVal));
  184. }
  185. static int
  186. numstr_filter_sub(
  187. Slapi_PBlock *pb,
  188. char *initial,
  189. char **any,
  190. char * final,
  191. Slapi_Value **bvals)
  192. {
  193. return (string_filter_sub(pb, initial, any, final, bvals, SYNTAX_SI | SYNTAX_CES));
  194. }
  195. static int
  196. numstr_values2keys(Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals, int ftype)
  197. {
  198. return (string_values2keys(pb, vals, ivals, SYNTAX_SI | SYNTAX_CES,
  199. ftype));
  200. }
  201. static int
  202. numstr_assertion2keys(Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype)
  203. {
  204. return (string_assertion2keys_ava(pb, val, ivals,
  205. SYNTAX_SI | SYNTAX_CES, ftype));
  206. }
  207. static int
  208. numstr_assertion2keys_sub(
  209. Slapi_PBlock *pb,
  210. char *initial,
  211. char **any,
  212. char * final,
  213. Slapi_Value ***ivals)
  214. {
  215. return (string_assertion2keys_sub(pb, initial, any, final, ivals,
  216. SYNTAX_SI | SYNTAX_CES));
  217. }
  218. static int
  219. numstr_compare(
  220. struct berval *v1,
  221. struct berval *v2)
  222. {
  223. return value_cmp(v1, v2, SYNTAX_SI | SYNTAX_CES, 3 /* Normalise both values */);
  224. }
  225. /* return 0 if valid, non-0 if invalid */
  226. static int
  227. numstr_validate(
  228. struct berval *val)
  229. {
  230. int rc = 0; /* assume the value is valid */
  231. const char *p = NULL;
  232. /* Per RFC4517:
  233. *
  234. * NumericString = 1*(DIGIT / SPACE)
  235. */
  236. if (val != NULL) {
  237. for (p = val->bv_val; p < &(val->bv_val[val->bv_len]); p++) {
  238. if (!isdigit(*p) && !IS_SPACE(*p)) {
  239. rc = 1;
  240. goto exit;
  241. }
  242. }
  243. } else {
  244. rc = 1;
  245. }
  246. exit:
  247. return (rc);
  248. }
  249. static void
  250. numstr_normalize(
  251. Slapi_PBlock *pb __attribute__((unused)),
  252. char *s,
  253. int trim_spaces,
  254. char **alt)
  255. {
  256. value_normalize_ext(s, SYNTAX_SI | SYNTAX_CES, trim_spaces, alt);
  257. return;
  258. }