nameoptuid.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2009 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. /* nameoptuid.c - Name And Optional UID syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int nameoptuid_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int nameoptuid_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  20. char *final, Slapi_Value **bvals );
  21. static int nameoptuid_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  22. Slapi_Value ***ivals, int ftype );
  23. static int nameoptuid_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  24. Slapi_Value ***ivals, int ftype );
  25. static int nameoptuid_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  26. char *final, Slapi_Value ***ivals );
  27. static int nameoptuid_compare(struct berval *v1, struct berval *v2);
  28. static int nameoptuid_validate(struct berval *val);
  29. static void nameoptuid_normalize(
  30. Slapi_PBlock *pb,
  31. char *s,
  32. int trim_spaces,
  33. char **alt
  34. );
  35. /* the first name is the official one from RFC 4517 */
  36. static char *names[] = { "Name And Optional UID", "nameoptuid", NAMEANDOPTIONALUID_SYNTAX_OID, 0 };
  37. static Slapi_PluginDesc pdesc = { "nameoptuid-syntax", VENDOR, DS_PACKAGE_VERSION,
  38. "Name And Optional UID attribute syntax plugin" };
  39. static const char *uniqueMemberMatch_names[] = {"uniqueMemberMatch", "2.5.13.23", NULL};
  40. static struct mr_plugin_def mr_plugin_table[] = {
  41. {{"2.5.13.23", NULL, "uniqueMemberMatch", "The uniqueMemberMatch rule compares an assertion value of the Name "
  42. "And Optional UID syntax to an attribute value of a syntax (e.g., the "
  43. "Name And Optional UID syntax) whose corresponding ASN.1 type is "
  44. "NameAndOptionalUID. "
  45. "The rule evaluates to TRUE if and only if the <distinguishedName> "
  46. "components of the assertion value and attribute value match according "
  47. "to the distinguishedNameMatch rule and either, (1) the <BitString> "
  48. "component is absent from both the attribute value and assertion "
  49. "value, or (2) the <BitString> component is present in both the "
  50. "attribute value and the assertion value and the <BitString> component "
  51. "of the assertion value matches the <BitString> component of the "
  52. "attribute value according to the bitStringMatch rule. "
  53. "Note that this matching rule has been altered from its description in "
  54. "X.520 [X.520] in order to make the matching rule commutative. Server "
  55. "implementors should consider using the original X.520 semantics "
  56. "(where the matching was less exact) for approximate matching of "
  57. "attributes with uniqueMemberMatch as the equality matching rule.",
  58. NAMEANDOPTIONALUID_SYNTAX_OID, 0, NULL /* no other syntaxes supported */}, /* matching rule desc */
  59. {"uniqueMemberMatch-mr", VENDOR, DS_PACKAGE_VERSION, "uniqueMemberMatch matching rule plugin"}, /* plugin desc */
  60. uniqueMemberMatch_names, /* matching rule name/oid/aliases */
  61. NULL, NULL, nameoptuid_filter_ava, NULL, nameoptuid_values2keys,
  62. nameoptuid_assertion2keys_ava, NULL, nameoptuid_compare},
  63. };
  64. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  65. static int
  66. matching_rule_plugin_init(Slapi_PBlock *pb)
  67. {
  68. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  69. }
  70. static int
  71. register_matching_rule_plugins()
  72. {
  73. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  74. }
  75. int
  76. nameoptuid_init( Slapi_PBlock *pb )
  77. {
  78. int rc, flags;
  79. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> nameoptuid_init\n", 0, 0, 0 );
  80. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  81. (void *) SLAPI_PLUGIN_VERSION_01 );
  82. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  83. (void *)&pdesc );
  84. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  85. (void *) nameoptuid_filter_ava );
  86. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  87. (void *) nameoptuid_filter_sub );
  88. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  89. (void *) nameoptuid_values2keys );
  90. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  91. (void *) nameoptuid_assertion2keys_ava );
  92. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  93. (void *) nameoptuid_assertion2keys_sub );
  94. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  96. (void *) &flags );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  98. (void *) names );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  100. (void *) NAMEANDOPTIONALUID_SYNTAX_OID );
  101. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  102. (void *) nameoptuid_compare );
  103. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  104. (void *) nameoptuid_validate );
  105. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  106. (void *) nameoptuid_normalize );
  107. rc |= register_matching_rule_plugins();
  108. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= nameoptuid_init %d\n", rc, 0, 0 );
  109. return( rc );
  110. }
  111. static int
  112. nameoptuid_filter_ava(
  113. Slapi_PBlock *pb,
  114. struct berval *bvfilter,
  115. Slapi_Value **bvals,
  116. int ftype,
  117. Slapi_Value **retVal
  118. )
  119. {
  120. int filter_normalized = 0;
  121. int syntax = SYNTAX_CIS | SYNTAX_DN;
  122. if (pb) {
  123. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  124. &filter_normalized );
  125. if (filter_normalized) {
  126. syntax |= SYNTAX_NORM_FILT;
  127. }
  128. }
  129. return( string_filter_ava( bvfilter, bvals, syntax, ftype, retVal ) );
  130. }
  131. static int
  132. nameoptuid_filter_sub(
  133. Slapi_PBlock *pb,
  134. char *initial,
  135. char **any,
  136. char *final,
  137. Slapi_Value **bvals
  138. )
  139. {
  140. return( string_filter_sub( pb, initial, any, final, bvals,
  141. SYNTAX_CIS | SYNTAX_DN ) );
  142. }
  143. static int
  144. nameoptuid_values2keys(
  145. Slapi_PBlock *pb,
  146. Slapi_Value **vals,
  147. Slapi_Value ***ivals,
  148. int ftype
  149. )
  150. {
  151. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS | SYNTAX_DN,
  152. ftype ) );
  153. }
  154. static int
  155. nameoptuid_assertion2keys_ava(
  156. Slapi_PBlock *pb,
  157. Slapi_Value *val,
  158. Slapi_Value ***ivals,
  159. int ftype
  160. )
  161. {
  162. return(string_assertion2keys_ava( pb, val, ivals,
  163. SYNTAX_CIS | SYNTAX_DN, ftype ));
  164. }
  165. static int
  166. nameoptuid_assertion2keys_sub(
  167. Slapi_PBlock *pb,
  168. char *initial,
  169. char **any,
  170. char *final,
  171. Slapi_Value ***ivals
  172. )
  173. {
  174. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  175. SYNTAX_CIS | SYNTAX_DN ) );
  176. }
  177. static int nameoptuid_compare(
  178. struct berval *v1,
  179. struct berval *v2
  180. )
  181. {
  182. return value_cmp(v1, v2, SYNTAX_CIS | SYNTAX_DN, 3 /* Normalise both values */);
  183. }
  184. static int
  185. nameoptuid_validate(
  186. struct berval *val
  187. )
  188. {
  189. int rc = 0; /* assume the value is valid */
  190. int got_sharp = 0;
  191. const char *p = NULL;
  192. const char *start = NULL;
  193. const char *end = NULL;
  194. /* Per RFC4517:
  195. *
  196. * NameAndOptionalUID = distinguishedName [ SHARP BitString ]
  197. */
  198. /* Don't allow a 0 length string */
  199. if ((val == NULL) || (val->bv_len == 0)) {
  200. rc = 1;
  201. goto exit;
  202. }
  203. start = &(val->bv_val[0]);
  204. end = &(val->bv_val[val->bv_len - 1]);
  205. /* Find the last SHARP in the value that may be separating
  206. * the distinguishedName from the optional BitString. */
  207. for (p = end; p >= start + 1; p--) {
  208. if (IS_SHARP(*p)) {
  209. got_sharp = 1;
  210. break;
  211. }
  212. }
  213. if (got_sharp) {
  214. /* Try to validate everything after the sharp as
  215. * a BitString. If this fails, we may still have
  216. * a valid value since a sharp is allowed in a
  217. * distinguishedName. If we don't find a valid
  218. * BitString, just validate the entire value as
  219. * a distinguishedName. */
  220. if ((rc = bitstring_validate_internal(p + 1, end)) != 0) {
  221. rc = distinguishedname_validate(start, end);
  222. } else {
  223. rc = distinguishedname_validate(start, p - 1);
  224. }
  225. } else {
  226. /* No optional BitString is present, so validate
  227. * the entire value as a distinguishedName. */
  228. rc = distinguishedname_validate(start, end);
  229. }
  230. exit:
  231. return rc;
  232. }
  233. static void nameoptuid_normalize(
  234. Slapi_PBlock *pb,
  235. char *s,
  236. int trim_spaces,
  237. char **alt
  238. )
  239. {
  240. value_normalize_ext(s, SYNTAX_CIS | SYNTAX_DN, trim_spaces, alt);
  241. return;
  242. }