numericstring.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2009 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* numericstring.c - Numeric String syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int numstr_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int numstr_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int numstr_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int numstr_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int numstr_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int numstr_compare(struct berval *v1, struct berval *v2);
  57. static int numstr_validate(struct berval *val);
  58. static void numstr_normalize(
  59. Slapi_PBlock *pb,
  60. char *s,
  61. int trim_spaces,
  62. char **alt
  63. );
  64. /* the first name is the official one from RFC 4517 */
  65. static char *names[] = { "Numeric String", "numstr", NUMERICSTRING_SYNTAX_OID, 0 };
  66. #define NUMERICSTRINGMATCH_OID "2.5.13.8"
  67. #define NUMERICSTRINGORDERINGMATCH_OID "2.5.13.9"
  68. #define NUMERICSTRINGSUBSTRINGSMATCH_OID "2.5.13.10"
  69. static Slapi_PluginDesc pdesc = { "numstr-syntax", VENDOR,
  70. DS_PACKAGE_VERSION, "numeric string attribute syntax plugin" };
  71. static const char *numericStringMatch_names[] = {"numericStringMatch", NUMERICSTRINGMATCH_OID, NULL};
  72. static const char *numericStringOrderingMatch_names[] = {"numericStringOrderingMatch", NUMERICSTRINGORDERINGMATCH_OID, NULL};
  73. static const char *numericStringSubstringsMatch_names[] = {"numericStringSubstringsMatch", NUMERICSTRINGSUBSTRINGSMATCH_OID, NULL};
  74. static char *numericStringSubstringsMatch_syntaxes[] = {NUMERICSTRING_SYNTAX_OID,NULL};
  75. static struct mr_plugin_def mr_plugin_table[] = {
  76. {{NUMERICSTRINGMATCH_OID, NULL /* no alias? */,
  77. "numericStringMatch", "The rule evaluates to TRUE if and only if the prepared "
  78. "attribute value character string and the prepared assertion value character "
  79. "string have the same number of characters and corresponding characters have "
  80. "the same code point.",
  81. NUMERICSTRING_SYNTAX_OID, 0 /* not obsolete */, NULL /* numstr syntax only for now */ },
  82. {"numericStringMatch-mr", VENDOR, DS_PACKAGE_VERSION, "numericStringMatch matching rule plugin"}, /* plugin desc */
  83. numericStringMatch_names, /* matching rule name/oid/aliases */
  84. NULL, NULL, numstr_filter_ava, NULL, numstr_values2keys,
  85. numstr_assertion2keys, NULL, numstr_compare},
  86. {{NUMERICSTRINGORDERINGMATCH_OID, NULL /* no alias? */,
  87. "numericStringOrderingMatch", "The rule evaluates to TRUE if and only if, "
  88. "in the code point collation order, the prepared attribute value character "
  89. "string appears earlier than the prepared assertion value character string; "
  90. "i.e., the attribute value is less than the assertion value.",
  91. NUMERICSTRING_SYNTAX_OID, 0 /* not obsolete */, NULL /* numstr syntax only for now */ },
  92. {"numericStringOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "numericStringOrderingMatch matching rule plugin"}, /* plugin desc */
  93. numericStringOrderingMatch_names, /* matching rule name/oid/aliases */
  94. NULL, NULL, numstr_filter_ava, NULL, numstr_values2keys,
  95. numstr_assertion2keys, NULL, numstr_compare},
  96. {{NUMERICSTRINGSUBSTRINGSMATCH_OID, NULL /* no alias? */,
  97. "numericStringSubstringsMatch", "The rule evaluates to TRUE if and only if (1) "
  98. "the prepared substrings of the assertion value match disjoint portions of "
  99. "the prepared attribute value, (2) an initial substring, if present, matches "
  100. "the beginning of the prepared attribute value character string, and (3) a "
  101. "final substring, if present, matches the end of the prepared attribute value "
  102. "character string.",
  103. "1.3.6.1.4.1.1466.115.121.1.58", 0 /* not obsolete */, numericStringSubstringsMatch_syntaxes}, /* matching rule desc */
  104. {"numericStringSubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "numericStringSubstringsMatch matching rule plugin"}, /* plugin desc */
  105. numericStringSubstringsMatch_names, /* matching rule name/oid/aliases */
  106. NULL, NULL, NULL, numstr_filter_sub, numstr_values2keys,
  107. NULL, numstr_assertion2keys_sub, numstr_compare},
  108. };
  109. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  110. static int
  111. matching_rule_plugin_init(Slapi_PBlock *pb)
  112. {
  113. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  114. }
  115. static int
  116. register_matching_rule_plugins()
  117. {
  118. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  119. }
  120. int
  121. numstr_init( Slapi_PBlock *pb )
  122. {
  123. int rc, flags;
  124. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> numstr_init\n", 0, 0, 0 );
  125. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  126. (void *) SLAPI_PLUGIN_VERSION_01 );
  127. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  128. (void *)&pdesc );
  129. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  130. (void *) numstr_filter_ava );
  131. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  132. (void *) numstr_values2keys );
  133. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  134. (void *) numstr_assertion2keys );
  135. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  136. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  137. (void *) &flags );
  138. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  139. (void *) names );
  140. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  141. (void *) NUMERICSTRING_SYNTAX_OID );
  142. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  143. (void *) numstr_compare );
  144. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  145. (void *) numstr_validate );
  146. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  147. (void *) numstr_normalize );
  148. rc |= register_matching_rule_plugins();
  149. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= numstr_init %d\n", rc, 0, 0 );
  150. return( rc );
  151. }
  152. static int
  153. numstr_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  154. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  155. {
  156. int filter_normalized = 0;
  157. int syntax = SYNTAX_SI | SYNTAX_CES;
  158. if (pb) {
  159. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  160. &filter_normalized );
  161. if (filter_normalized) {
  162. syntax |= SYNTAX_NORM_FILT;
  163. }
  164. }
  165. return( string_filter_ava( bvfilter, bvals, syntax,
  166. ftype, retVal ) );
  167. }
  168. static int
  169. numstr_filter_sub(
  170. Slapi_PBlock *pb,
  171. char *initial,
  172. char **any,
  173. char *final,
  174. Slapi_Value **bvals
  175. )
  176. {
  177. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_SI | SYNTAX_CES ) );
  178. }
  179. static int
  180. numstr_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals, int ftype )
  181. {
  182. return( string_values2keys( pb, vals, ivals, SYNTAX_SI | SYNTAX_CES,
  183. ftype ) );
  184. }
  185. static int
  186. numstr_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype )
  187. {
  188. return(string_assertion2keys_ava( pb, val, ivals,
  189. SYNTAX_SI | SYNTAX_CES, ftype ));
  190. }
  191. static int
  192. numstr_assertion2keys_sub(
  193. Slapi_PBlock *pb,
  194. char *initial,
  195. char **any,
  196. char *final,
  197. Slapi_Value ***ivals
  198. )
  199. {
  200. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  201. SYNTAX_SI | SYNTAX_CES ) );
  202. }
  203. static int numstr_compare(
  204. struct berval *v1,
  205. struct berval *v2
  206. )
  207. {
  208. return value_cmp(v1, v2, SYNTAX_SI | SYNTAX_CES, 3 /* Normalise both values */);
  209. }
  210. /* return 0 if valid, non-0 if invalid */
  211. static int numstr_validate(
  212. struct berval *val
  213. )
  214. {
  215. int rc = 0; /* assume the value is valid */
  216. const char *p = NULL;
  217. /* Per RFC4517:
  218. *
  219. * NumericString = 1*(DIGIT / SPACE)
  220. */
  221. if (val != NULL) {
  222. for (p = val->bv_val; p < &(val->bv_val[val->bv_len]); p++) {
  223. if (!isdigit(*p) && !IS_SPACE(*p)) {
  224. rc = 1;
  225. goto exit;
  226. }
  227. }
  228. } else {
  229. rc = 1;
  230. }
  231. exit:
  232. return(rc);
  233. }
  234. static void numstr_normalize(
  235. Slapi_PBlock *pb,
  236. char *s,
  237. int trim_spaces,
  238. char **alt
  239. )
  240. {
  241. value_normalize_ext(s, SYNTAX_SI|SYNTAX_CES, trim_spaces, alt);
  242. return;
  243. }