1
0

numericstring.c 8.2 KB

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