dn.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /* dn.c - dn syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int dn_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int dn_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  20. char *final, Slapi_Value **bvals );
  21. static int dn_values2keys( Slapi_PBlock *pb, Slapi_Value **vals,
  22. Slapi_Value ***ivals, int ftype );
  23. static int dn_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  24. Slapi_Value ***ivals, int ftype );
  25. static int dn_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  26. char *final, Slapi_Value ***ivals );
  27. static int dn_validate( struct berval *val );
  28. static void dn_normalize(
  29. Slapi_PBlock *pb,
  30. char *s,
  31. int trim_spaces,
  32. char **alt
  33. );
  34. /* the first name is the official one from RFC 2252 */
  35. static char *names[] = { "DN", DN_SYNTAX_OID, 0 };
  36. static Slapi_PluginDesc pdesc = { "dn-syntax", VENDOR,
  37. DS_PACKAGE_VERSION, "distinguished name attribute syntax plugin" };
  38. static const char *distinguishedNameMatch_names[] = {"distinguishedNameMatch", "2.5.13.1", NULL};
  39. static struct mr_plugin_def mr_plugin_table[] = {
  40. {
  41. {
  42. "2.5.13.1",
  43. NULL,
  44. "distinguishedNameMatch",
  45. "The distinguishedNameMatch rule compares an assertion value of the DN "
  46. "syntax to an attribute value of a syntax (e.g., the DN syntax) whose "
  47. "corresponding ASN.1 type is DistinguishedName. "
  48. "The rule evaluates to TRUE if and only if the attribute value and the "
  49. "assertion value have the same number of relative distinguished names "
  50. "and corresponding relative distinguished names (by position) are the "
  51. "same. A relative distinguished name (RDN) of the assertion value is "
  52. "the same as an RDN of the attribute value if and only if they have "
  53. "the same number of attribute value assertions and each attribute "
  54. "value assertion (AVA) of the first RDN is the same as the AVA of the "
  55. "second RDN with the same attribute type. The order of the AVAs is "
  56. "not significant. Also note that a particular attribute type may "
  57. "appear in at most one AVA in an RDN. Two AVAs with the same "
  58. "attribute type are the same if their values are equal according to "
  59. "the equality matching rule of the attribute type. If one or more of "
  60. "the AVA comparisons evaluate to Undefined and the remaining AVA "
  61. "comparisons return TRUE then the distinguishedNameMatch rule "
  62. "evaluates to Undefined.",
  63. DN_SYNTAX_OID,
  64. 0,
  65. NULL /* dn only for now */
  66. }, /* matching rule desc */
  67. {
  68. "distinguishedNameMatch-mr",
  69. VENDOR,
  70. DS_PACKAGE_VERSION,
  71. "distinguishedNameMatch matching rule plugin"
  72. }, /* plugin desc */
  73. distinguishedNameMatch_names, /* matching rule name/oid/aliases */
  74. NULL,
  75. NULL,
  76. dn_filter_ava,
  77. NULL,
  78. dn_values2keys,
  79. dn_assertion2keys_ava,
  80. NULL,
  81. NULL,
  82. NULL /* mr_nomalise */
  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. dn_init( Slapi_PBlock *pb )
  98. {
  99. int rc;
  100. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> dn_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 *) dn_filter_ava );
  107. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  108. (void *) dn_filter_sub );
  109. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  110. (void *) dn_values2keys );
  111. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  112. (void *) dn_assertion2keys_ava );
  113. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  114. (void *) dn_assertion2keys_sub );
  115. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  116. (void *) names );
  117. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  118. (void *) DN_SYNTAX_OID );
  119. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  120. (void *) dn_validate );
  121. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  122. (void *) dn_normalize );
  123. rc |= register_matching_rule_plugins();
  124. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= dn_init %d\n", rc, 0, 0 );
  125. return( rc );
  126. }
  127. static int
  128. dn_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  129. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  130. {
  131. int filter_normalized = 0;
  132. int syntax = SYNTAX_CIS | SYNTAX_DN;
  133. if (pb) {
  134. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  135. &filter_normalized );
  136. if (filter_normalized) {
  137. syntax |= SYNTAX_NORM_FILT;
  138. }
  139. }
  140. return( string_filter_ava( bvfilter, bvals, syntax, ftype, retVal ) );
  141. }
  142. static int
  143. dn_filter_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  144. Slapi_Value **bvals )
  145. {
  146. return( string_filter_sub( pb, initial, any, final, bvals,
  147. SYNTAX_CIS | SYNTAX_DN ) );
  148. }
  149. static int
  150. dn_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals,
  151. int ftype )
  152. {
  153. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS | SYNTAX_DN,
  154. ftype ) );
  155. }
  156. static int
  157. dn_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  158. Slapi_Value ***ivals, int ftype )
  159. {
  160. return( string_assertion2keys_ava( pb, val, ivals,
  161. SYNTAX_CIS | SYNTAX_DN, ftype ) );
  162. }
  163. static int
  164. dn_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  165. Slapi_Value ***ivals )
  166. {
  167. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  168. SYNTAX_CIS | SYNTAX_DN ) );
  169. }
  170. static int dn_validate( struct berval *val )
  171. {
  172. int rc = 0; /* Assume value is valid */
  173. /* A 0 length value is valid for the DN syntax. */
  174. if (val == NULL) {
  175. rc = 1;
  176. } else if (val->bv_len > 0) {
  177. rc = distinguishedname_validate(val->bv_val, &(val->bv_val[val->bv_len - 1]));
  178. }
  179. return rc;
  180. }
  181. static void dn_normalize(
  182. Slapi_PBlock *pb,
  183. char *s,
  184. int trim_spaces,
  185. char **alt
  186. )
  187. {
  188. value_normalize_ext(s, SYNTAX_CIS | SYNTAX_DN, trim_spaces, alt);
  189. return;
  190. }