dn.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. {{"2.5.13.1", NULL, "distinguishedNameMatch", "The distinguishedNameMatch rule compares an assertion value of the DN "
  41. "syntax to an attribute value of a syntax (e.g., the DN syntax) whose "
  42. "corresponding ASN.1 type is DistinguishedName. "
  43. "The rule evaluates to TRUE if and only if the attribute value and the "
  44. "assertion value have the same number of relative distinguished names "
  45. "and corresponding relative distinguished names (by position) are the "
  46. "same. A relative distinguished name (RDN) of the assertion value is "
  47. "the same as an RDN of the attribute value if and only if they have "
  48. "the same number of attribute value assertions and each attribute "
  49. "value assertion (AVA) of the first RDN is the same as the AVA of the "
  50. "second RDN with the same attribute type. The order of the AVAs is "
  51. "not significant. Also note that a particular attribute type may "
  52. "appear in at most one AVA in an RDN. Two AVAs with the same "
  53. "attribute type are the same if their values are equal according to "
  54. "the equality matching rule of the attribute type. If one or more of "
  55. "the AVA comparisons evaluate to Undefined and the remaining AVA "
  56. "comparisons return TRUE then the distinguishedNameMatch rule "
  57. "evaluates to Undefined.", DN_SYNTAX_OID, 0, NULL /* dn only for now */}, /* matching rule desc */
  58. {"distinguishedNameMatch-mr", VENDOR, DS_PACKAGE_VERSION, "distinguishedNameMatch matching rule plugin"}, /* plugin desc */
  59. distinguishedNameMatch_names, /* matching rule name/oid/aliases */
  60. NULL, NULL, dn_filter_ava, NULL, dn_values2keys,
  61. dn_assertion2keys_ava, NULL, NULL},
  62. };
  63. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  64. static int
  65. matching_rule_plugin_init(Slapi_PBlock *pb)
  66. {
  67. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  68. }
  69. static int
  70. register_matching_rule_plugins()
  71. {
  72. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  73. }
  74. int
  75. dn_init( Slapi_PBlock *pb )
  76. {
  77. int rc;
  78. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> dn_init\n", 0, 0, 0 );
  79. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  80. (void *) SLAPI_PLUGIN_VERSION_01 );
  81. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  82. (void *)&pdesc );
  83. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  84. (void *) dn_filter_ava );
  85. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  86. (void *) dn_filter_sub );
  87. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  88. (void *) dn_values2keys );
  89. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  90. (void *) dn_assertion2keys_ava );
  91. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  92. (void *) dn_assertion2keys_sub );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  94. (void *) names );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  96. (void *) DN_SYNTAX_OID );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  98. (void *) dn_validate );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  100. (void *) dn_normalize );
  101. rc |= register_matching_rule_plugins();
  102. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= dn_init %d\n", rc, 0, 0 );
  103. return( rc );
  104. }
  105. static int
  106. dn_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  107. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  108. {
  109. int filter_normalized = 0;
  110. int syntax = SYNTAX_CIS | SYNTAX_DN;
  111. if (pb) {
  112. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  113. &filter_normalized );
  114. if (filter_normalized) {
  115. syntax |= SYNTAX_NORM_FILT;
  116. }
  117. }
  118. return( string_filter_ava( bvfilter, bvals, syntax, ftype, retVal ) );
  119. }
  120. static int
  121. dn_filter_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  122. Slapi_Value **bvals )
  123. {
  124. return( string_filter_sub( pb, initial, any, final, bvals,
  125. SYNTAX_CIS | SYNTAX_DN ) );
  126. }
  127. static int
  128. dn_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals,
  129. int ftype )
  130. {
  131. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS | SYNTAX_DN,
  132. ftype ) );
  133. }
  134. static int
  135. dn_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  136. Slapi_Value ***ivals, int ftype )
  137. {
  138. return( string_assertion2keys_ava( pb, val, ivals,
  139. SYNTAX_CIS | SYNTAX_DN, ftype ) );
  140. }
  141. static int
  142. dn_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  143. Slapi_Value ***ivals )
  144. {
  145. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  146. SYNTAX_CIS | SYNTAX_DN ) );
  147. }
  148. static int dn_validate( struct berval *val )
  149. {
  150. int rc = 0; /* Assume value is valid */
  151. /* A 0 length value is valid for the DN syntax. */
  152. if (val == NULL) {
  153. rc = 1;
  154. } else if (val->bv_len > 0) {
  155. rc = distinguishedname_validate(val->bv_val, &(val->bv_val[val->bv_len - 1]));
  156. }
  157. return rc;
  158. }
  159. static void dn_normalize(
  160. Slapi_PBlock *pb,
  161. char *s,
  162. int trim_spaces,
  163. char **alt
  164. )
  165. {
  166. value_normalize_ext(s, SYNTAX_CIS | SYNTAX_DN, trim_spaces, alt);
  167. return;
  168. }