ces.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* ces.c - caseexactstring syntax routines. Implements support for:
  42. * - IA5String
  43. * - URI (DEPRECATED - This is non-standard and isn't used in the default schema.) */
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <sys/types.h>
  47. #include "syntax.h"
  48. /* this is used in proposed schema, but there is no official
  49. OID yet - so for now, use our private MR OID namespace */
  50. #define CASEEXACTIA5SUBSTRINGSMATCH_OID "2.16.840.1.113730.3.3.1"
  51. static int ces_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  52. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  53. static int ces_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  54. char *final, Slapi_Value **bvals );
  55. static int ces_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  56. Slapi_Value ***ivals, int ftype );
  57. static int ces_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  58. Slapi_Value ***ivals, int ftype );
  59. static int ces_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  60. char *final, Slapi_Value ***ivals );
  61. static int ces_compare(struct berval *v1, struct berval *v2);
  62. static int ia5_validate(struct berval *val);
  63. static void ces_normalize(
  64. Slapi_PBlock *pb,
  65. char *s,
  66. int trim_spaces,
  67. char **alt
  68. );
  69. /* the first name is the official one from RFC 2252 */
  70. static char *ia5_names[] = { "IA5String", "ces", "caseexactstring",
  71. IA5STRING_SYNTAX_OID, 0 };
  72. /* the first name is the official one from RFC 2252 */
  73. static char *uri_names[] = { "URI", "1.3.6.1.4.1.4401.1.1.1",0};
  74. static Slapi_PluginDesc ia5_pdesc = { "ces-syntax", VENDOR,
  75. DS_PACKAGE_VERSION, "caseExactString attribute syntax plugin" };
  76. static Slapi_PluginDesc uri_pdesc = { "uri-syntax", VENDOR,
  77. DS_PACKAGE_VERSION, "uri attribute syntax plugin" };
  78. static const char *caseExactIA5Match_names[] = {"caseExactIA5Match", "1.3.6.1.4.1.1466.109.114.1", NULL};
  79. static const char *caseExactMatch_names[] = {"caseExactMatch", "2.5.13.5", NULL};
  80. static const char *caseExactOrderingMatch_names[] = {"caseExactOrderingMatch", "2.5.13.6", NULL};
  81. static const char *caseExactSubstringsMatch_names[] = {"caseExactSubstringsMatch", "2.5.13.7", NULL};
  82. static const char *caseExactIA5SubstringsMatch_names[] = {"caseExactIA5SubstringsMatch", CASEEXACTIA5SUBSTRINGSMATCH_OID, NULL};
  83. static char *dirStringCompat_syntaxes[] = {COUNTRYSTRING_SYNTAX_OID,
  84. PRINTABLESTRING_SYNTAX_OID,NULL};
  85. static char *ia5String_syntaxes[] = {IA5STRING_SYNTAX_OID,NULL};
  86. static char *caseExactSubstrings_syntaxes[] = {IA5STRING_SYNTAX_OID, /* allow IA5 to use cesubstrs e.g. krbPrincipalName */
  87. COUNTRYSTRING_SYNTAX_OID,
  88. DIRSTRING_SYNTAX_OID,
  89. PRINTABLESTRING_SYNTAX_OID,NULL};
  90. /* for some reason vendorName and vendorVersion are dirstring but want
  91. to use EQUALITY caseExactIA5Match ???? RFC 3045
  92. also the old definition of automountInformation from 60autofs.ldif
  93. does the same thing */
  94. static char *caseExactIA5Match_syntaxes[] = {DIRSTRING_SYNTAX_OID, NULL};
  95. static struct mr_plugin_def mr_plugin_table[] = {
  96. {{"1.3.6.1.4.1.1466.109.114.1", NULL, "caseExactIA5Match", "The caseExactIA5Match rule compares an assertion value of the IA5 "
  97. "String syntax to an attribute value of a syntax (e.g., the IA5 String "
  98. "syntax) whose corresponding ASN.1 type is IA5String. "
  99. "The rule evaluates to TRUE if and only if the prepared attribute "
  100. "value character string and the prepared assertion value character "
  101. "string have the same number of characters and corresponding "
  102. "characters have the same code point. "
  103. "In preparing the attribute value and assertion value for comparison, "
  104. "characters are not case folded in the Map preparation step, and only "
  105. "Insignificant Space Handling is applied in the Insignificant "
  106. "Character Handling step.",
  107. IA5STRING_SYNTAX_OID, 0, caseExactIA5Match_syntaxes}, /* matching rule desc */
  108. {"caseExactIA5Match-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactIA5Match matching rule plugin"}, /* plugin desc */
  109. caseExactIA5Match_names, /* matching rule name/oid/aliases */
  110. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  111. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  112. {{"2.5.13.5", NULL, "caseExactMatch", "The caseExactMatch rule compares an assertion value of the Directory "
  113. "String syntax to an attribute value of a syntax (e.g., the Directory "
  114. "String, Printable String, Country String, or Telephone Number syntax) "
  115. "whose corresponding ASN.1 type is DirectoryString or one of the "
  116. "alternative string types of DirectoryString, such as PrintableString "
  117. "(the other alternatives do not correspond to any syntax defined in "
  118. "this document). "
  119. "The rule evaluates to TRUE if and only if the prepared attribute "
  120. "value character string and the prepared assertion value character "
  121. "string have the same number of characters and corresponding "
  122. "characters have the same code point. "
  123. "In preparing the attribute value and assertion value for comparison, "
  124. "characters are not case folded in the Map preparation step, and only "
  125. "Insignificant Space Handling is applied in the Insignificant "
  126. "Character Handling step.",
  127. DIRSTRING_SYNTAX_OID, 0, dirStringCompat_syntaxes}, /* matching rule desc */
  128. {"caseExactMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactMatch matching rule plugin"}, /* plugin desc */
  129. caseExactMatch_names, /* matching rule name/oid/aliases */
  130. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  131. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  132. {{"2.5.13.6", NULL, "caseExactOrderingMatch", "The caseExactOrderingMatch rule compares an assertion value of the "
  133. "Directory String syntax to an attribute value of a syntax (e.g., the "
  134. "Directory String, Printable String, Country String, or Telephone "
  135. "Number syntax) whose corresponding ASN.1 type is DirectoryString or "
  136. "one of its alternative string types. "
  137. "The rule evaluates to TRUE if and only if, in the code point "
  138. "collation order, the prepared attribute value character string "
  139. "appears earlier than the prepared assertion value character string; "
  140. "i.e., the attribute value is \"less than\" the assertion value. "
  141. "In preparing the attribute value and assertion value for comparison, "
  142. "characters are not case folded in the Map preparation step, and only "
  143. "Insignificant Space Handling is applied in the Insignificant "
  144. "Character Handling step.",
  145. DIRSTRING_SYNTAX_OID, 0, dirStringCompat_syntaxes}, /* matching rule desc */
  146. {"caseExactOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactOrderingMatch matching rule plugin"}, /* plugin desc */
  147. caseExactOrderingMatch_names, /* matching rule name/oid/aliases */
  148. NULL, NULL, ces_filter_ava, NULL, ces_values2keys,
  149. ces_assertion2keys_ava, NULL, ces_compare, ces_normalize},
  150. {{"2.5.13.7", NULL, "caseExactSubstringsMatch", "The caseExactSubstringsMatch rule compares an assertion value of the "
  151. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  152. "the Directory String, Printable String, Country String, or Telephone "
  153. "Number syntax) whose corresponding ASN.1 type is DirectoryString or "
  154. "one of its alternative string types. "
  155. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  156. "of the assertion value match disjoint portions of the prepared "
  157. "attribute value character string in the order of the substrings in "
  158. "the assertion value, (2) an <initial> substring, if present, matches "
  159. "the beginning of the prepared attribute value character string, and "
  160. "(3) a <final> substring, if present, matches the end of the prepared "
  161. "attribute value character string. A prepared substring matches a "
  162. "portion of the prepared attribute value character string if "
  163. "corresponding characters have the same code point. "
  164. "In preparing the attribute value and assertion value substrings for "
  165. "comparison, characters are not case folded in the Map preparation "
  166. "step, and only Insignificant Space Handling is applied in the "
  167. "Insignificant Character Handling step.",
  168. "1.3.6.1.4.1.1466.115.121.1.58", 0, caseExactSubstrings_syntaxes}, /* matching rule desc */
  169. {"caseExactSubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactSubstringsMatch matching rule plugin"}, /* plugin desc */
  170. caseExactSubstringsMatch_names, /* matching rule name/oid/aliases */
  171. NULL, NULL, NULL, ces_filter_sub, ces_values2keys,
  172. NULL, ces_assertion2keys_sub, ces_compare, ces_normalize},
  173. {{CASEEXACTIA5SUBSTRINGSMATCH_OID, NULL, "caseExactIA5SubstringsMatch", "The caseExactIA5SubstringsMatch rule compares an assertion value of the "
  174. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  175. "the IA5 syntax) whose corresponding ASN.1 type is IA5 String or "
  176. "one of its alternative string types. "
  177. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  178. "of the assertion value match disjoint portions of the prepared "
  179. "attribute value character string in the order of the substrings in "
  180. "the assertion value, (2) an <initial> substring, if present, matches "
  181. "the beginning of the prepared attribute value character string, and "
  182. "(3) a <final> substring, if present, matches the end of the prepared "
  183. "attribute value character string. A prepared substring matches a "
  184. "portion of the prepared attribute value character string if "
  185. "corresponding characters have the same code point. "
  186. "In preparing the attribute value and assertion value substrings for "
  187. "comparison, characters are not case folded in the Map preparation "
  188. "step, and only Insignificant Space Handling is applied in the "
  189. "Insignificant Character Handling step.",
  190. "1.3.6.1.4.1.1466.115.121.1.58", 0, ia5String_syntaxes}, /* matching rule desc */
  191. {"caseExactIA5SubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "caseExactIA5SubstringsMatch matching rule plugin"}, /* plugin desc */
  192. caseExactIA5SubstringsMatch_names, /* matching rule name/oid/aliases */
  193. NULL, NULL, NULL, ces_filter_sub, ces_values2keys,
  194. NULL, ces_assertion2keys_sub, ces_compare, ces_normalize}
  195. };
  196. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  197. static int
  198. matching_rule_plugin_init(Slapi_PBlock *pb)
  199. {
  200. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  201. }
  202. static int
  203. register_matching_rule_plugins()
  204. {
  205. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  206. }
  207. /*
  208. * register_ces_like_plugin(): register all items for a cis-like plugin.
  209. */
  210. static int
  211. register_ces_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  212. char **names, char *oid, void *validate_fn )
  213. {
  214. int rc, flags;
  215. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  216. (void *) SLAPI_PLUGIN_VERSION_01 );
  217. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  218. (void *) pdescp );
  219. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  220. (void *) ces_filter_ava );
  221. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  222. (void *) ces_filter_sub );
  223. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  224. (void *) ces_values2keys );
  225. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  226. (void *) ces_assertion2keys_ava );
  227. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  228. (void *) ces_assertion2keys_sub );
  229. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  230. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  231. (void *) &flags );
  232. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  233. (void *) names );
  234. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  235. (void *) oid );
  236. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  237. (void *) ces_compare );
  238. if (validate_fn != NULL) {
  239. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  240. (void *)validate_fn );
  241. }
  242. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  243. (void *)ces_normalize );
  244. return( rc );
  245. }
  246. int
  247. ces_init( Slapi_PBlock *pb )
  248. {
  249. int rc;
  250. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> ces_init\n", 0, 0, 0 );
  251. rc = register_ces_like_plugin(pb,&ia5_pdesc,ia5_names,IA5STRING_SYNTAX_OID, ia5_validate);
  252. rc |= register_matching_rule_plugins();
  253. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= ces_init %d\n", rc, 0, 0 );
  254. return( rc );
  255. }
  256. int
  257. uri_init( Slapi_PBlock *pb )
  258. {
  259. int rc;
  260. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> uri_init\n", 0, 0, 0 );
  261. rc = register_ces_like_plugin(pb,&uri_pdesc,uri_names,
  262. "1.3.6.1.4.1.4401.1.1.1", NULL);
  263. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= uri_init %d\n", rc, 0, 0 );
  264. return( rc );
  265. }
  266. static int
  267. ces_filter_ava(
  268. Slapi_PBlock *pb,
  269. struct berval *bvfilter,
  270. Slapi_Value **bvals,
  271. int ftype,
  272. Slapi_Value **retVal
  273. )
  274. {
  275. int filter_normalized = 0;
  276. int syntax = SYNTAX_CES;
  277. if (pb) {
  278. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  279. &filter_normalized );
  280. if (filter_normalized) {
  281. syntax |= SYNTAX_NORM_FILT;
  282. }
  283. }
  284. return( string_filter_ava( bvfilter, bvals, syntax, ftype,
  285. retVal) );
  286. }
  287. static int
  288. ces_filter_sub(
  289. Slapi_PBlock *pb,
  290. char *initial,
  291. char **any,
  292. char *final,
  293. Slapi_Value **bvals
  294. )
  295. {
  296. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CES ) );
  297. }
  298. static int
  299. ces_values2keys(
  300. Slapi_PBlock *pb,
  301. Slapi_Value **vals,
  302. Slapi_Value ***ivals,
  303. int ftype
  304. )
  305. {
  306. return( string_values2keys( pb, vals, ivals, SYNTAX_CES, ftype ) );
  307. }
  308. static int
  309. ces_assertion2keys_ava(
  310. Slapi_PBlock *pb,
  311. Slapi_Value *val,
  312. Slapi_Value ***ivals,
  313. int ftype
  314. )
  315. {
  316. return(string_assertion2keys_ava( pb, val, ivals, SYNTAX_CES, ftype ));
  317. }
  318. static int
  319. ces_assertion2keys_sub(
  320. Slapi_PBlock *pb,
  321. char *initial,
  322. char **any,
  323. char *final,
  324. Slapi_Value ***ivals
  325. )
  326. {
  327. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  328. SYNTAX_CES ) );
  329. }
  330. static int ces_compare(
  331. struct berval *v1,
  332. struct berval *v2
  333. )
  334. {
  335. return value_cmp(v1,v2,SYNTAX_CES,3 /* Normalise both values */);
  336. }
  337. static int
  338. ia5_validate(
  339. struct berval *val
  340. )
  341. {
  342. int rc = 0; /* assume the value is valid */
  343. int i = 0;
  344. if (val == NULL) {
  345. rc = 1;
  346. goto exit;
  347. }
  348. /* Per RFC 4517:
  349. *
  350. * IA5String = *(%x00-7F)
  351. */
  352. for (i=0; i < val->bv_len; i++) {
  353. if (!IS_UTF1(val->bv_val[i])) {
  354. rc = 1;
  355. goto exit;
  356. }
  357. }
  358. exit:
  359. return rc;
  360. }
  361. static void ces_normalize(
  362. Slapi_PBlock *pb,
  363. char *s,
  364. int trim_spaces,
  365. char **alt
  366. )
  367. {
  368. value_normalize_ext(s, SYNTAX_CES, trim_spaces, alt);
  369. return;
  370. }