1
0

ces.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* ces.c - caseexactstring syntax routines. Implements support for:
  13. * - IA5String
  14. * - URI (DEPRECATED - This is non-standard and isn't used in the default schema.) */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include "syntax.h"
  19. /* this is used in proposed schema, but there is no official
  20. OID yet - so for now, use our private MR OID namespace */
  21. #define CASEEXACTIA5SUBSTRINGSMATCH_OID "2.16.840.1.113730.3.3.1"
  22. static int ces_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  23. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  24. static int ces_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  25. char *final, Slapi_Value **bvals );
  26. static int ces_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  27. Slapi_Value ***ivals, int ftype );
  28. static int ces_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  29. Slapi_Value ***ivals, int ftype );
  30. static int ces_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  31. char *final, Slapi_Value ***ivals );
  32. static int ces_compare(struct berval *v1, struct berval *v2);
  33. static int ia5_validate(struct berval *val);
  34. static void ces_normalize(
  35. Slapi_PBlock *pb,
  36. char *s,
  37. int trim_spaces,
  38. char **alt
  39. );
  40. /* the first name is the official one from RFC 2252 */
  41. static char *ia5_names[] = { "IA5String", "ces", "caseexactstring",
  42. IA5STRING_SYNTAX_OID, 0 };
  43. /* the first name is the official one from RFC 2252 */
  44. static char *uri_names[] = { "URI", "1.3.6.1.4.1.4401.1.1.1",0};
  45. static Slapi_PluginDesc ia5_pdesc = { "ces-syntax", VENDOR,
  46. DS_PACKAGE_VERSION, "caseExactString attribute syntax plugin" };
  47. static Slapi_PluginDesc uri_pdesc = { "uri-syntax", VENDOR,
  48. DS_PACKAGE_VERSION, "uri attribute syntax plugin" };
  49. static const char *caseExactIA5Match_names[] = {"caseExactIA5Match", "1.3.6.1.4.1.1466.109.114.1", NULL};
  50. static const char *caseExactMatch_names[] = {"caseExactMatch", "2.5.13.5", NULL};
  51. static const char *caseExactOrderingMatch_names[] = {"caseExactOrderingMatch", "2.5.13.6", NULL};
  52. static const char *caseExactSubstringsMatch_names[] = {"caseExactSubstringsMatch", "2.5.13.7", NULL};
  53. static const char *caseExactIA5SubstringsMatch_names[] = {"caseExactIA5SubstringsMatch", CASEEXACTIA5SUBSTRINGSMATCH_OID, NULL};
  54. static char *dirStringCompat_syntaxes[] = {COUNTRYSTRING_SYNTAX_OID,
  55. PRINTABLESTRING_SYNTAX_OID,NULL};
  56. static char *ia5String_syntaxes[] = {IA5STRING_SYNTAX_OID,NULL};
  57. static char *caseExactSubstrings_syntaxes[] = {IA5STRING_SYNTAX_OID, /* allow IA5 to use cesubstrs e.g. krbPrincipalName */
  58. COUNTRYSTRING_SYNTAX_OID,
  59. DIRSTRING_SYNTAX_OID,
  60. PRINTABLESTRING_SYNTAX_OID,NULL};
  61. /* for some reason vendorName and vendorVersion are dirstring but want
  62. to use EQUALITY caseExactIA5Match ???? RFC 3045
  63. also the old definition of automountInformation from 60autofs.ldif
  64. does the same thing */
  65. static char *caseExactIA5Match_syntaxes[] = {DIRSTRING_SYNTAX_OID, NULL};
  66. static struct mr_plugin_def mr_plugin_table[] = {
  67. {{"1.3.6.1.4.1.1466.109.114.1", NULL, "caseExactIA5Match", "The caseExactIA5Match rule compares an assertion value of the IA5 "
  68. "String syntax to an attribute value of a syntax (e.g., the IA5 String "
  69. "syntax) whose corresponding ASN.1 type is IA5String. "
  70. "The rule evaluates to TRUE if and only if the prepared attribute "
  71. "value character string and the prepared assertion value character "
  72. "string have the same number of characters and corresponding "
  73. "characters have the same code point. "
  74. "In preparing the attribute value and assertion value for comparison, "
  75. "characters are not case folded in the Map preparation step, and only "
  76. "Insignificant Space Handling is applied in the Insignificant "
  77. "Character Handling step.",
  78. IA5STRING_SYNTAX_OID, 0, caseExactIA5Match_syntaxes}, /* matching rule desc */
  79. {"caseExactIA5Match-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactIA5Match matching rule plugin"}, /* plugin desc */
  80. caseExactIA5Match_names, /* matching rule name/oid/aliases */
  81. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  82. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  83. {{"2.5.13.5", NULL, "caseExactMatch", "The caseExactMatch rule compares an assertion value of the Directory "
  84. "String syntax to an attribute value of a syntax (e.g., the Directory "
  85. "String, Printable String, Country String, or Telephone Number syntax) "
  86. "whose corresponding ASN.1 type is DirectoryString or one of the "
  87. "alternative string types of DirectoryString, such as PrintableString "
  88. "(the other alternatives do not correspond to any syntax defined in "
  89. "this document). "
  90. "The rule evaluates to TRUE if and only if the prepared attribute "
  91. "value character string and the prepared assertion value character "
  92. "string have the same number of characters and corresponding "
  93. "characters have the same code point. "
  94. "In preparing the attribute value and assertion value for comparison, "
  95. "characters are not case folded in the Map preparation step, and only "
  96. "Insignificant Space Handling is applied in the Insignificant "
  97. "Character Handling step.",
  98. DIRSTRING_SYNTAX_OID, 0, dirStringCompat_syntaxes}, /* matching rule desc */
  99. {"caseExactMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactMatch matching rule plugin"}, /* plugin desc */
  100. caseExactMatch_names, /* matching rule name/oid/aliases */
  101. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  102. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  103. {{"2.5.13.6", NULL, "caseExactOrderingMatch", "The caseExactOrderingMatch rule compares an assertion value of the "
  104. "Directory String syntax to an attribute value of a syntax (e.g., the "
  105. "Directory String, Printable String, Country String, or Telephone "
  106. "Number syntax) whose corresponding ASN.1 type is DirectoryString or "
  107. "one of its alternative string types. "
  108. "The rule evaluates to TRUE if and only if, in the code point "
  109. "collation order, the prepared attribute value character string "
  110. "appears earlier than the prepared assertion value character string; "
  111. "i.e., the attribute value is \"less than\" the assertion value. "
  112. "In preparing the attribute value and assertion value for comparison, "
  113. "characters are not case folded in the Map preparation step, and only "
  114. "Insignificant Space Handling is applied in the Insignificant "
  115. "Character Handling step.",
  116. DIRSTRING_SYNTAX_OID, 0, dirStringCompat_syntaxes}, /* matching rule desc */
  117. {"caseExactOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactOrderingMatch matching rule plugin"}, /* plugin desc */
  118. caseExactOrderingMatch_names, /* matching rule name/oid/aliases */
  119. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  120. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  121. {{"2.5.13.7", NULL, "caseExactSubstringsMatch", "The caseExactSubstringsMatch rule compares an assertion value of the "
  122. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  123. "the Directory String, Printable String, Country String, or Telephone "
  124. "Number syntax) whose corresponding ASN.1 type is DirectoryString or "
  125. "one of its alternative string types. "
  126. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  127. "of the assertion value match disjoint portions of the prepared "
  128. "attribute value character string in the order of the substrings in "
  129. "the assertion value, (2) an <initial> substring, if present, matches "
  130. "the beginning of the prepared attribute value character string, and "
  131. "(3) a <final> substring, if present, matches the end of the prepared "
  132. "attribute value character string. A prepared substring matches a "
  133. "portion of the prepared attribute value character string if "
  134. "corresponding characters have the same code point. "
  135. "In preparing the attribute value and assertion value substrings for "
  136. "comparison, characters are not case folded in the Map preparation "
  137. "step, and only Insignificant Space Handling is applied in the "
  138. "Insignificant Character Handling step.",
  139. "1.3.6.1.4.1.1466.115.121.1.58", 0, caseExactSubstrings_syntaxes}, /* matching rule desc */
  140. {"caseExactSubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactSubstringsMatch matching rule plugin"}, /* plugin desc */
  141. caseExactSubstringsMatch_names, /* matching rule name/oid/aliases */
  142. NULL, NULL, NULL, ces_filter_sub, ces_values2keys,
  143. NULL, ces_assertion2keys_sub, ces_compare, ces_normalize},
  144. {{CASEEXACTIA5SUBSTRINGSMATCH_OID, NULL, "caseExactIA5SubstringsMatch", "The caseExactIA5SubstringsMatch rule compares an assertion value of the "
  145. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  146. "the IA5 syntax) whose corresponding ASN.1 type is IA5 String or "
  147. "one of its alternative string types. "
  148. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  149. "of the assertion value match disjoint portions of the prepared "
  150. "attribute value character string in the order of the substrings in "
  151. "the assertion value, (2) an <initial> substring, if present, matches "
  152. "the beginning of the prepared attribute value character string, and "
  153. "(3) a <final> substring, if present, matches the end of the prepared "
  154. "attribute value character string. A prepared substring matches a "
  155. "portion of the prepared attribute value character string if "
  156. "corresponding characters have the same code point. "
  157. "In preparing the attribute value and assertion value substrings for "
  158. "comparison, characters are not case folded in the Map preparation "
  159. "step, and only Insignificant Space Handling is applied in the "
  160. "Insignificant Character Handling step.",
  161. "1.3.6.1.4.1.1466.115.121.1.58", 0, ia5String_syntaxes}, /* matching rule desc */
  162. {"caseExactIA5SubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactIA5SubstringsMatch matching rule plugin"}, /* plugin desc */
  163. caseExactIA5SubstringsMatch_names, /* matching rule name/oid/aliases */
  164. NULL, NULL, NULL, ces_filter_sub, ces_values2keys,
  165. NULL, ces_assertion2keys_sub, ces_compare, ces_normalize}
  166. };
  167. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  168. static int
  169. matching_rule_plugin_init(Slapi_PBlock *pb)
  170. {
  171. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  172. }
  173. static int
  174. register_matching_rule_plugins()
  175. {
  176. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  177. }
  178. /*
  179. * register_ces_like_plugin(): register all items for a cis-like plugin.
  180. */
  181. static int
  182. register_ces_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  183. char **names, char *oid, void *validate_fn )
  184. {
  185. int rc, flags;
  186. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  187. (void *) SLAPI_PLUGIN_VERSION_01 );
  188. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  189. (void *) pdescp );
  190. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  191. (void *) ces_filter_ava );
  192. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  193. (void *) ces_filter_sub );
  194. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  195. (void *) ces_values2keys );
  196. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  197. (void *) ces_assertion2keys_ava );
  198. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  199. (void *) ces_assertion2keys_sub );
  200. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  201. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  202. (void *) &flags );
  203. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  204. (void *) names );
  205. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  206. (void *) oid );
  207. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  208. (void *) ces_compare );
  209. if (validate_fn != NULL) {
  210. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  211. (void *)validate_fn );
  212. }
  213. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  214. (void *)ces_normalize );
  215. return( rc );
  216. }
  217. int
  218. ces_init( Slapi_PBlock *pb )
  219. {
  220. int rc;
  221. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> ces_init\n", 0, 0, 0 );
  222. rc = register_ces_like_plugin(pb,&ia5_pdesc,ia5_names,IA5STRING_SYNTAX_OID, ia5_validate);
  223. rc |= register_matching_rule_plugins();
  224. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= ces_init %d\n", rc, 0, 0 );
  225. return( rc );
  226. }
  227. int
  228. uri_init( Slapi_PBlock *pb )
  229. {
  230. int rc;
  231. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> uri_init\n", 0, 0, 0 );
  232. rc = register_ces_like_plugin(pb,&uri_pdesc,uri_names,
  233. "1.3.6.1.4.1.4401.1.1.1", NULL);
  234. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= uri_init %d\n", rc, 0, 0 );
  235. return( rc );
  236. }
  237. static int
  238. ces_filter_ava(
  239. Slapi_PBlock *pb,
  240. struct berval *bvfilter,
  241. Slapi_Value **bvals,
  242. int ftype,
  243. Slapi_Value **retVal
  244. )
  245. {
  246. int filter_normalized = 0;
  247. int syntax = SYNTAX_CES;
  248. if (pb) {
  249. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  250. &filter_normalized );
  251. if (filter_normalized) {
  252. syntax |= SYNTAX_NORM_FILT;
  253. }
  254. }
  255. return( string_filter_ava( bvfilter, bvals, syntax, ftype,
  256. retVal) );
  257. }
  258. static int
  259. ces_filter_sub(
  260. Slapi_PBlock *pb,
  261. char *initial,
  262. char **any,
  263. char *final,
  264. Slapi_Value **bvals
  265. )
  266. {
  267. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CES ) );
  268. }
  269. static int
  270. ces_values2keys(
  271. Slapi_PBlock *pb,
  272. Slapi_Value **vals,
  273. Slapi_Value ***ivals,
  274. int ftype
  275. )
  276. {
  277. return( string_values2keys( pb, vals, ivals, SYNTAX_CES, ftype ) );
  278. }
  279. static int
  280. ces_assertion2keys_ava(
  281. Slapi_PBlock *pb,
  282. Slapi_Value *val,
  283. Slapi_Value ***ivals,
  284. int ftype
  285. )
  286. {
  287. return(string_assertion2keys_ava( pb, val, ivals, SYNTAX_CES, ftype ));
  288. }
  289. static int
  290. ces_assertion2keys_sub(
  291. Slapi_PBlock *pb,
  292. char *initial,
  293. char **any,
  294. char *final,
  295. Slapi_Value ***ivals
  296. )
  297. {
  298. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  299. SYNTAX_CES ) );
  300. }
  301. static int ces_compare(
  302. struct berval *v1,
  303. struct berval *v2
  304. )
  305. {
  306. return value_cmp(v1,v2,SYNTAX_CES,3 /* Normalise both values */);
  307. }
  308. static int
  309. ia5_validate(
  310. struct berval *val
  311. )
  312. {
  313. int rc = 0; /* assume the value is valid */
  314. int i = 0;
  315. if (val == NULL) {
  316. rc = 1;
  317. goto exit;
  318. }
  319. /* Per RFC 4517:
  320. *
  321. * IA5String = *(%x00-7F)
  322. */
  323. for (i=0; i < val->bv_len; i++) {
  324. if (!IS_UTF1(val->bv_val[i])) {
  325. rc = 1;
  326. goto exit;
  327. }
  328. }
  329. exit:
  330. return rc;
  331. }
  332. static void ces_normalize(
  333. Slapi_PBlock *pb,
  334. char *s,
  335. int trim_spaces,
  336. char **alt
  337. )
  338. {
  339. value_normalize_ext(s, SYNTAX_CES, trim_spaces, alt);
  340. return;
  341. }