numericstring.c 9.5 KB

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