nameoptuid.c 8.7 KB

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