1
0

numericstring.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  49. Slapi_Value ***ivals, int ftype );
  50. static int numstr_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int numstr_compare(struct berval *v1, struct berval *v2);
  53. static int numstr_validate(struct berval *val);
  54. /* the first name is the official one from RFC 4517 */
  55. static char *names[] = { "Numeric String", "numstr", NUMERICSTRING_SYNTAX_OID, 0 };
  56. #define NUMERICSTRINGMATCH_OID "2.5.13.8"
  57. #define NUMERICSTRINGORDERINGMATCH_OID "2.5.13.9"
  58. #define NUMERICSTRINGSUBSTRINGMATCH_OID "2.5.13.10"
  59. static Slapi_PluginDesc pdesc = { "numstr-syntax", PLUGIN_MAGIC_VENDOR_STR,
  60. PRODUCTTEXT, "numeric string attribute syntax plugin" };
  61. static Slapi_MatchingRuleEntry
  62. numericStringMatch = { NUMERICSTRINGMATCH_OID, NULL /* no alias? */,
  63. "numericStringMatch", "The rule evaluates to TRUE if and only if the prepared "
  64. "attribute value character string and the prepared assertion value character "
  65. "string have the same number of characters and corresponding characters have "
  66. "the same code point.",
  67. NUMERICSTRING_SYNTAX_OID, 0 /* not obsolete */ };
  68. static Slapi_MatchingRuleEntry
  69. numericStringOrderingMatch = { NUMERICSTRINGORDERINGMATCH_OID, NULL /* no alias? */,
  70. "numericStringOrderingMatch", "The rule evaluates to TRUE if and only if, "
  71. "in the code point collation order, the prepared attribute value character "
  72. "string appears earlier than the prepared assertion value character string; "
  73. "i.e., the attribute value is less than the assertion value.",
  74. NUMERICSTRING_SYNTAX_OID, 0 /* not obsolete */ };
  75. static Slapi_MatchingRuleEntry
  76. numericStringSubstringMatch = { NUMERICSTRINGSUBSTRINGMATCH_OID, NULL /* no alias? */,
  77. "numericStringSubstringMatch", "The rule evaluates to TRUE if and only if (1) "
  78. "the prepared substrings of the assertion value match disjoint portions of "
  79. "the prepared attribute value, (2) an initial substring, if present, matches "
  80. "the beginning of the prepared attribute value character string, and (3) a "
  81. "final substring, if present, matches the end of the prepared attribute value "
  82. "character string.",
  83. NUMERICSTRING_SYNTAX_OID, 0 /* not obsolete */ };
  84. int
  85. numstr_init( Slapi_PBlock *pb )
  86. {
  87. int rc, flags;
  88. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> numstr_init\n", 0, 0, 0 );
  89. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  90. (void *) SLAPI_PLUGIN_VERSION_01 );
  91. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  92. (void *)&pdesc );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  94. (void *) numstr_filter_ava );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  96. (void *) numstr_values2keys );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  98. (void *) numstr_assertion2keys );
  99. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  100. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  101. (void *) &flags );
  102. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  103. (void *) names );
  104. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  105. (void *) INTEGER_SYNTAX_OID );
  106. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  107. (void *) numstr_compare );
  108. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  109. (void *) numstr_validate );
  110. /* also register this plugin for matching rules */
  111. rc |= slapi_matchingrule_register(&numericStringMatch);
  112. rc |= slapi_matchingrule_register(&numericStringOrderingMatch);
  113. rc |= slapi_matchingrule_register(&numericStringSubstringMatch);
  114. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= numstr_init %d\n", rc, 0, 0 );
  115. return( rc );
  116. }
  117. static int
  118. numstr_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  119. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  120. {
  121. return( string_filter_ava( bvfilter, bvals, SYNTAX_SI | SYNTAX_CES,
  122. ftype, retVal ) );
  123. }
  124. static int
  125. numstr_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals, int ftype )
  126. {
  127. return( string_values2keys( pb, vals, ivals, SYNTAX_SI | SYNTAX_CES,
  128. ftype ) );
  129. }
  130. static int
  131. numstr_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype )
  132. {
  133. return(string_assertion2keys_ava( pb, val, ivals,
  134. SYNTAX_SI | SYNTAX_CES, ftype ));
  135. }
  136. static int numstr_compare(
  137. struct berval *v1,
  138. struct berval *v2
  139. )
  140. {
  141. return value_cmp(v1, v2, SYNTAX_SI | SYNTAX_CES, 3 /* Normalise both values */);
  142. }
  143. /* return 0 if valid, non-0 if invalid */
  144. static int numstr_validate(
  145. struct berval *val
  146. )
  147. {
  148. int rc = 0; /* assume the value is valid */
  149. const char *p = NULL;
  150. /* Per RFC4517:
  151. *
  152. * NumericString = 1*(DIGIT / SPACE)
  153. */
  154. if (val != NULL) {
  155. for (p = val->bv_val; p < &(val->bv_val[val->bv_len]); p++) {
  156. if (!isdigit(*p) && !IS_SPACE(*p)) {
  157. rc = 1;
  158. goto exit;
  159. }
  160. }
  161. } else {
  162. rc = 1;
  163. }
  164. exit:
  165. return(rc);
  166. }