int.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* int.c - integer syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int int_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int int_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  20. Slapi_Value ***ivals, int ftype );
  21. static int int_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val,
  22. Slapi_Value ***ivals, int ftype );
  23. static int int_compare(struct berval *v1, struct berval *v2);
  24. static int int_validate(struct berval *val);
  25. static void int_normalize(
  26. Slapi_PBlock *pb,
  27. char *s,
  28. int trim_spaces,
  29. char **alt
  30. );
  31. /* the first name is the official one from RFC 2252 */
  32. static char *names[] = { "INTEGER", "int", INTEGER_SYNTAX_OID, 0 };
  33. #define INTEGERMATCH_OID "2.5.13.14"
  34. #define INTEGERORDERINGMATCH_OID "2.5.13.15"
  35. static Slapi_PluginDesc pdesc = { "int-syntax", VENDOR,
  36. DS_PACKAGE_VERSION, "integer attribute syntax plugin" };
  37. static const char *integerMatch_names[] = {"integerMatch", INTEGERMATCH_OID, NULL};
  38. static const char *integerOrderingMatch_names[] = {"integerOrderingMatch", INTEGERORDERINGMATCH_OID, NULL};
  39. static const char *integerFirstComponentMatch_names[] = {"integerFirstComponentMatch", "2.5.13.29", NULL};
  40. /* hack for now until we can support all of the rfc4517 syntaxes */
  41. static char *integerFirstComponentMatch_syntaxes[] = {DIRSTRING_SYNTAX_OID, NULL};
  42. static struct mr_plugin_def mr_plugin_table[] = {
  43. {
  44. {
  45. INTEGERMATCH_OID,
  46. NULL /* no alias? */,
  47. "integerMatch",
  48. "The rule evaluates to TRUE if and only if the attribute value and the assertion value are the same integer value.",
  49. INTEGER_SYNTAX_OID,
  50. 0 /* not obsolete */,
  51. NULL /* no other compatible syntaxes */
  52. },
  53. {
  54. "integerMatch-mr",
  55. VENDOR,
  56. DS_PACKAGE_VERSION,
  57. "integerMatch matching rule plugin"
  58. },
  59. integerMatch_names, /* matching rule name/oid/aliases */
  60. NULL,
  61. NULL,
  62. int_filter_ava,
  63. NULL,
  64. int_values2keys,
  65. int_assertion2keys,
  66. NULL,
  67. int_compare,
  68. NULL /* mr_normalise */
  69. },
  70. {
  71. {
  72. INTEGERORDERINGMATCH_OID,
  73. NULL /* no alias? */,
  74. "integerOrderingMatch",
  75. "The rule evaluates to TRUE if and only if the integer value of the attribute value is less than the integer value of the assertion value.",
  76. INTEGER_SYNTAX_OID,
  77. 0 /* not obsolete */,
  78. NULL /* no other compatible syntaxes */
  79. },
  80. {
  81. "integerOrderingMatch-mr",
  82. VENDOR,
  83. DS_PACKAGE_VERSION,
  84. "integerOrderingMatch matching rule plugin"
  85. },
  86. integerOrderingMatch_names, /* matching rule name/oid/aliases */
  87. NULL,
  88. NULL,
  89. int_filter_ava,
  90. NULL,
  91. int_values2keys,
  92. int_assertion2keys,
  93. NULL,
  94. int_compare,
  95. NULL /* mr_normalise */
  96. },
  97. /* NOTE: THIS IS BROKEN - WE DON'T SUPPORT THE FIRSTCOMPONENT match */
  98. {
  99. {
  100. "2.5.13.29",
  101. NULL,
  102. "integerFirstComponentMatch",
  103. "The integerFirstComponentMatch rule compares an assertion value of "
  104. "the Integer syntax to an attribute value of a syntax (e.g., the DIT "
  105. "Structure Rule Description syntax) whose corresponding ASN.1 type is "
  106. "a SEQUENCE with a mandatory first component of the INTEGER ASN.1 "
  107. "type. "
  108. "Note that the assertion syntax of this matching rule differs from the "
  109. "attribute syntax of attributes for which this is the equality "
  110. "matching rule. "
  111. "The rule evaluates to TRUE if and only if the assertion value and the "
  112. "first component of the attribute value are the same integer value.",
  113. INTEGER_SYNTAX_OID,
  114. 0,
  115. integerFirstComponentMatch_syntaxes
  116. }, /* matching rule desc */
  117. {
  118. "integerFirstComponentMatch-mr",
  119. VENDOR,
  120. DS_PACKAGE_VERSION,
  121. "integerFirstComponentMatch matching rule plugin"
  122. }, /* plugin desc */
  123. integerFirstComponentMatch_names, /* matching rule name/oid/aliases */
  124. NULL,
  125. NULL,
  126. int_filter_ava,
  127. NULL,
  128. int_values2keys,
  129. int_assertion2keys,
  130. NULL,
  131. int_compare,
  132. NULL /* mr_normalise */
  133. },
  134. };
  135. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  136. static int
  137. matching_rule_plugin_init(Slapi_PBlock *pb)
  138. {
  139. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  140. }
  141. static int
  142. register_matching_rule_plugins(void)
  143. {
  144. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  145. }
  146. int
  147. int_init( Slapi_PBlock *pb )
  148. {
  149. int rc, flags;
  150. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> int_init\n", 0, 0, 0 );
  151. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  152. (void *) SLAPI_PLUGIN_VERSION_01 );
  153. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  154. (void *)&pdesc );
  155. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  156. (void *) int_filter_ava );
  157. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  158. (void *) int_values2keys );
  159. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  160. (void *) int_assertion2keys );
  161. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  162. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  163. (void *) &flags );
  164. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  165. (void *) names );
  166. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  167. (void *) INTEGER_SYNTAX_OID );
  168. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  169. (void *) int_compare );
  170. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  171. (void *) int_validate );
  172. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  173. (void *) int_normalize );
  174. rc |= register_matching_rule_plugins();
  175. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= int_init %d\n", rc, 0, 0 );
  176. return( rc );
  177. }
  178. static int
  179. int_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  180. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  181. {
  182. int filter_normalized = 0;
  183. int syntax = SYNTAX_INT | SYNTAX_CES;
  184. if (pb) {
  185. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  186. &filter_normalized );
  187. if (filter_normalized) {
  188. syntax |= SYNTAX_NORM_FILT;
  189. }
  190. }
  191. return( string_filter_ava( bvfilter, bvals, syntax,
  192. ftype, retVal ) );
  193. }
  194. static int
  195. int_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals, int ftype )
  196. {
  197. return( string_values2keys( pb, vals, ivals, SYNTAX_INT | SYNTAX_CES,
  198. ftype ) );
  199. }
  200. static int
  201. int_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype )
  202. {
  203. return(string_assertion2keys_ava( pb, val, ivals,
  204. SYNTAX_INT | SYNTAX_CES, ftype ));
  205. }
  206. static int int_compare(
  207. struct berval *v1,
  208. struct berval *v2
  209. )
  210. {
  211. return value_cmp(v1, v2, SYNTAX_INT|SYNTAX_CES, 3 /* Normalise both values */);
  212. }
  213. /* return 0 if valid, non-0 if invalid */
  214. static int int_validate(
  215. struct berval *val
  216. )
  217. {
  218. int rc = 0; /* assume the value is valid */
  219. char *p = NULL;
  220. char *end = NULL;
  221. /* Per RFC4517:
  222. *
  223. * Integer = (HYPHEN LDIGIT *DIGIT) / number
  224. * number = DIGIT / (LDIGIT 1*DIGIT)
  225. */
  226. if ((val != NULL) && (val->bv_len > 0)) {
  227. p = val->bv_val;
  228. end = &(val->bv_val[val->bv_len - 1]);
  229. /* If the first character is HYPHEN, we need
  230. * to make sure the next char is a LDIGIT. */
  231. if (*p == '-') {
  232. p++;
  233. if ((p > end) || !IS_LDIGIT(*p)) {
  234. rc = 1;
  235. goto exit;
  236. }
  237. p++;
  238. } else if (*p == '0') {
  239. /* 0 is allowed by itself, but not as
  240. * a leading 0 before other digits */
  241. if (p != end) {
  242. rc = 1;
  243. }
  244. /* We're done here */
  245. goto exit;
  246. }
  247. /* Now we can simply allow the rest to be DIGIT */
  248. for (; p <= end; p++) {
  249. if (!isdigit(*p)) {
  250. rc = 1;
  251. goto exit;
  252. }
  253. }
  254. } else {
  255. rc = 1;
  256. }
  257. exit:
  258. return(rc);
  259. }
  260. static void int_normalize(
  261. Slapi_PBlock *pb,
  262. char *s,
  263. int trim_spaces,
  264. char **alt
  265. )
  266. {
  267. value_normalize_ext(s, SYNTAX_INT|SYNTAX_CES, trim_spaces, alt);
  268. return;
  269. }