dn.c 7.3 KB

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