int.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. {{INTEGERMATCH_OID, NULL /* no alias? */,
  44. "integerMatch", "The rule evaluates to TRUE if and only if the attribute value and the assertion value are the same integer value.",
  45. INTEGER_SYNTAX_OID, 0 /* not obsolete */, NULL /* no other compatible syntaxes */ },
  46. {"integerMatch-mr", VENDOR, DS_PACKAGE_VERSION, "integerMatch matching rule plugin" },
  47. integerMatch_names, /* matching rule name/oid/aliases */
  48. NULL, NULL, int_filter_ava, NULL, int_values2keys,
  49. int_assertion2keys, NULL, int_compare},
  50. {{INTEGERORDERINGMATCH_OID, NULL /* no alias? */,
  51. "integerOrderingMatch", "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.",
  52. INTEGER_SYNTAX_OID, 0 /* not obsolete */, NULL /* no other compatible syntaxes */ },
  53. {"integerOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "integerOrderingMatch matching rule plugin" },
  54. integerOrderingMatch_names, /* matching rule name/oid/aliases */
  55. NULL, NULL, int_filter_ava, NULL, int_values2keys,
  56. int_assertion2keys, NULL, int_compare},
  57. /* NOTE: THIS IS BROKEN - WE DON'T SUPPORT THE FIRSTCOMPONENT match */
  58. {{"2.5.13.29", NULL, "integerFirstComponentMatch", "The integerFirstComponentMatch rule compares an assertion value of "
  59. "the Integer syntax to an attribute value of a syntax (e.g., the DIT "
  60. "Structure Rule Description syntax) whose corresponding ASN.1 type is "
  61. "a SEQUENCE with a mandatory first component of the INTEGER ASN.1 "
  62. "type. "
  63. "Note that the assertion syntax of this matching rule differs from the "
  64. "attribute syntax of attributes for which this is the equality "
  65. "matching rule. "
  66. "The rule evaluates to TRUE if and only if the assertion value and the "
  67. "first component of the attribute value are the same integer value.",
  68. INTEGER_SYNTAX_OID, 0, integerFirstComponentMatch_syntaxes}, /* matching rule desc */
  69. {"integerFirstComponentMatch-mr", VENDOR, DS_PACKAGE_VERSION, "integerFirstComponentMatch matching rule plugin"}, /* plugin desc */
  70. integerFirstComponentMatch_names, /* matching rule name/oid/aliases */
  71. NULL, NULL, int_filter_ava, NULL, int_values2keys,
  72. int_assertion2keys, NULL, int_compare},
  73. };
  74. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  75. static int
  76. matching_rule_plugin_init(Slapi_PBlock *pb)
  77. {
  78. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  79. }
  80. static int
  81. register_matching_rule_plugins()
  82. {
  83. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  84. }
  85. int
  86. int_init( Slapi_PBlock *pb )
  87. {
  88. int rc, flags;
  89. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> int_init\n", 0, 0, 0 );
  90. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  91. (void *) SLAPI_PLUGIN_VERSION_01 );
  92. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  93. (void *)&pdesc );
  94. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  95. (void *) int_filter_ava );
  96. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  97. (void *) int_values2keys );
  98. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  99. (void *) int_assertion2keys );
  100. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  101. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  102. (void *) &flags );
  103. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  104. (void *) names );
  105. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  106. (void *) INTEGER_SYNTAX_OID );
  107. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  108. (void *) int_compare );
  109. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  110. (void *) int_validate );
  111. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  112. (void *) int_normalize );
  113. rc |= register_matching_rule_plugins();
  114. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= int_init %d\n", rc, 0, 0 );
  115. return( rc );
  116. }
  117. static int
  118. int_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  119. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  120. {
  121. int filter_normalized = 0;
  122. int syntax = SYNTAX_INT | SYNTAX_CES;
  123. if (pb) {
  124. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  125. &filter_normalized );
  126. if (filter_normalized) {
  127. syntax |= SYNTAX_NORM_FILT;
  128. }
  129. }
  130. return( string_filter_ava( bvfilter, bvals, syntax,
  131. ftype, retVal ) );
  132. }
  133. static int
  134. int_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals, int ftype )
  135. {
  136. return( string_values2keys( pb, vals, ivals, SYNTAX_INT | SYNTAX_CES,
  137. ftype ) );
  138. }
  139. static int
  140. int_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype )
  141. {
  142. return(string_assertion2keys_ava( pb, val, ivals,
  143. SYNTAX_INT | SYNTAX_CES, ftype ));
  144. }
  145. static int int_compare(
  146. struct berval *v1,
  147. struct berval *v2
  148. )
  149. {
  150. return value_cmp(v1, v2, SYNTAX_INT|SYNTAX_CES, 3 /* Normalise both values */);
  151. }
  152. /* return 0 if valid, non-0 if invalid */
  153. static int int_validate(
  154. struct berval *val
  155. )
  156. {
  157. int rc = 0; /* assume the value is valid */
  158. char *p = NULL;
  159. char *end = NULL;
  160. /* Per RFC4517:
  161. *
  162. * Integer = (HYPHEN LDIGIT *DIGIT) / number
  163. * number = DIGIT / (LDIGIT 1*DIGIT)
  164. */
  165. if ((val != NULL) && (val->bv_len > 0)) {
  166. p = val->bv_val;
  167. end = &(val->bv_val[val->bv_len - 1]);
  168. /* If the first character is HYPHEN, we need
  169. * to make sure the next char is a LDIGIT. */
  170. if (*p == '-') {
  171. p++;
  172. if ((p > end) || !IS_LDIGIT(*p)) {
  173. rc = 1;
  174. goto exit;
  175. }
  176. p++;
  177. } else if (*p == '0') {
  178. /* 0 is allowed by itself, but not as
  179. * a leading 0 before other digits */
  180. if (p != end) {
  181. rc = 1;
  182. }
  183. /* We're done here */
  184. goto exit;
  185. }
  186. /* Now we can simply allow the rest to be DIGIT */
  187. for (; p <= end; p++) {
  188. if (!isdigit(*p)) {
  189. rc = 1;
  190. goto exit;
  191. }
  192. }
  193. } else {
  194. rc = 1;
  195. }
  196. exit:
  197. return(rc);
  198. }
  199. static void int_normalize(
  200. Slapi_PBlock *pb,
  201. char *s,
  202. int trim_spaces,
  203. char **alt
  204. )
  205. {
  206. value_normalize_ext(s, SYNTAX_INT|SYNTAX_CES, trim_spaces, alt);
  207. return;
  208. }