ces.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. {
  68. {
  69. "1.3.6.1.4.1.1466.109.114.1",
  70. NULL,
  71. "caseExactIA5Match",
  72. "The caseExactIA5Match rule compares an assertion value of the IA5 "
  73. "String syntax to an attribute value of a syntax (e.g., the IA5 String "
  74. "syntax) whose corresponding ASN.1 type is IA5String. "
  75. "The rule evaluates to TRUE if and only if the prepared attribute "
  76. "value character string and the prepared assertion value character "
  77. "string have the same number of characters and corresponding "
  78. "characters have the same code point. "
  79. "In preparing the attribute value and assertion value for comparison, "
  80. "characters are not case folded in the Map preparation step, and only "
  81. "Insignificant Space Handling is applied in the Insignificant "
  82. "Character Handling step.",
  83. IA5STRING_SYNTAX_OID,
  84. 0,
  85. caseExactIA5Match_syntaxes
  86. }, /* matching rule desc */
  87. {
  88. "caseExactIA5Match-mr",
  89. VENDOR,
  90. DS_PACKAGE_VERSION,
  91. "caseExactIA5Match matching rule plugin"
  92. }, /* plugin desc */
  93. caseExactIA5Match_names, /* matching rule name/oid/aliases */
  94. NULL,
  95. NULL,
  96. ces_filter_ava,
  97. NULL,
  98. ces_values2keys,
  99. ces_assertion2keys_ava,
  100. NULL,
  101. ces_compare,
  102. ces_normalize
  103. },
  104. {
  105. {
  106. "2.5.13.5",
  107. NULL,
  108. "caseExactMatch",
  109. "The caseExactMatch rule compares an assertion value of the Directory "
  110. "String syntax to an attribute value of a syntax (e.g., the Directory "
  111. "String, Printable String, Country String, or Telephone Number syntax) "
  112. "whose corresponding ASN.1 type is DirectoryString or one of the "
  113. "alternative string types of DirectoryString, such as PrintableString "
  114. "(the other alternatives do not correspond to any syntax defined in "
  115. "this document). "
  116. "The rule evaluates to TRUE if and only if the prepared attribute "
  117. "value character string and the prepared assertion value character "
  118. "string have the same number of characters and corresponding "
  119. "characters have the same code point. "
  120. "In preparing the attribute value and assertion value for comparison, "
  121. "characters are not case folded in the Map preparation step, and only "
  122. "Insignificant Space Handling is applied in the Insignificant "
  123. "Character Handling step.",
  124. DIRSTRING_SYNTAX_OID,
  125. 0,
  126. dirStringCompat_syntaxes
  127. }, /* matching rule desc */
  128. {
  129. "caseExactMatch-mr",
  130. VENDOR,
  131. DS_PACKAGE_VERSION,
  132. "caseExactMatch matching rule plugin"
  133. }, /* plugin desc */
  134. caseExactMatch_names, /* matching rule name/oid/aliases */
  135. NULL,
  136. NULL,
  137. ces_filter_ava,
  138. NULL,
  139. ces_values2keys,
  140. ces_assertion2keys_ava,
  141. NULL,
  142. ces_compare,
  143. ces_normalize
  144. },
  145. {
  146. {
  147. "2.5.13.6",
  148. NULL,
  149. "caseExactOrderingMatch",
  150. "The caseExactOrderingMatch rule compares an assertion value of the "
  151. "Directory String syntax to an attribute value of a syntax (e.g., the "
  152. "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, in the code point "
  156. "collation order, the prepared attribute value character string "
  157. "appears earlier than the prepared assertion value character string; "
  158. "i.e., the attribute value is \"less than\" the assertion value. "
  159. "In preparing the attribute value and assertion value for comparison, "
  160. "characters are not case folded in the Map preparation step, and only "
  161. "Insignificant Space Handling is applied in the Insignificant "
  162. "Character Handling step.",
  163. DIRSTRING_SYNTAX_OID,
  164. 0,
  165. dirStringCompat_syntaxes
  166. }, /* matching rule desc */
  167. {
  168. "caseExactOrderingMatch-mr",
  169. VENDOR,
  170. DS_PACKAGE_VERSION,
  171. "caseExactOrderingMatch matching rule plugin"
  172. }, /* plugin desc */
  173. caseExactOrderingMatch_names, /* matching rule name/oid/aliases */
  174. NULL,
  175. NULL,
  176. ces_filter_ava,
  177. NULL,
  178. ces_values2keys,
  179. ces_assertion2keys_ava,
  180. NULL,
  181. ces_compare,
  182. ces_normalize
  183. },
  184. {
  185. {
  186. "2.5.13.7",
  187. NULL,
  188. "caseExactSubstringsMatch",
  189. "The caseExactSubstringsMatch rule compares an assertion value of the "
  190. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  191. "the Directory String, Printable String, Country String, or Telephone "
  192. "Number syntax) whose corresponding ASN.1 type is DirectoryString or "
  193. "one of its alternative string types. "
  194. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  195. "of the assertion value match disjoint portions of the prepared "
  196. "attribute value character string in the order of the substrings in "
  197. "the assertion value, (2) an <initial> substring, if present, matches "
  198. "the beginning of the prepared attribute value character string, and "
  199. "(3) a <final> substring, if present, matches the end of the prepared "
  200. "attribute value character string. A prepared substring matches a "
  201. "portion of the prepared attribute value character string if "
  202. "corresponding characters have the same code point. "
  203. "In preparing the attribute value and assertion value substrings for "
  204. "comparison, characters are not case folded in the Map preparation "
  205. "step, and only Insignificant Space Handling is applied in the "
  206. "Insignificant Character Handling step.",
  207. "1.3.6.1.4.1.1466.115.121.1.58",
  208. 0,
  209. caseExactSubstrings_syntaxes
  210. }, /* matching rule desc */
  211. {
  212. "caseExactSubstringsMatch-mr",
  213. VENDOR,
  214. DS_PACKAGE_VERSION,
  215. "caseExactSubstringsMatch matching rule plugin"
  216. }, /* plugin desc */
  217. caseExactSubstringsMatch_names, /* matching rule name/oid/aliases */
  218. NULL,
  219. NULL,
  220. NULL,
  221. ces_filter_sub,
  222. ces_values2keys,
  223. NULL,
  224. ces_assertion2keys_sub,
  225. ces_compare,
  226. ces_normalize
  227. },
  228. {
  229. {
  230. CASEEXACTIA5SUBSTRINGSMATCH_OID,
  231. NULL,
  232. "caseExactIA5SubstringsMatch",
  233. "The caseExactIA5SubstringsMatch rule compares an assertion value of the "
  234. "Substring Assertion syntax to an attribute value of a syntax (e.g., "
  235. "the IA5 syntax) whose corresponding ASN.1 type is IA5 String or "
  236. "one of its alternative string types. "
  237. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  238. "of the assertion value match disjoint portions of the prepared "
  239. "attribute value character string in the order of the substrings in "
  240. "the assertion value, (2) an <initial> substring, if present, matches "
  241. "the beginning of the prepared attribute value character string, and "
  242. "(3) a <final> substring, if present, matches the end of the prepared "
  243. "attribute value character string. A prepared substring matches a "
  244. "portion of the prepared attribute value character string if "
  245. "corresponding characters have the same code point. "
  246. "In preparing the attribute value and assertion value substrings for "
  247. "comparison, characters are not case folded in the Map preparation "
  248. "step, and only Insignificant Space Handling is applied in the "
  249. "Insignificant Character Handling step.",
  250. "1.3.6.1.4.1.1466.115.121.1.58",
  251. 0,
  252. ia5String_syntaxes
  253. }, /* matching rule desc */
  254. {
  255. "caseExactIA5SubstringsMatch-mr",
  256. VENDOR,
  257. DS_PACKAGE_VERSION,
  258. "caseExactIA5SubstringsMatch matching rule plugin"
  259. }, /* plugin desc */
  260. caseExactIA5SubstringsMatch_names, /* matching rule name/oid/aliases */
  261. NULL,
  262. NULL,
  263. NULL,
  264. ces_filter_sub,
  265. ces_values2keys,
  266. NULL,
  267. ces_assertion2keys_sub,
  268. ces_compare,
  269. ces_normalize
  270. }
  271. };
  272. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  273. static int
  274. matching_rule_plugin_init(Slapi_PBlock *pb)
  275. {
  276. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  277. }
  278. static int
  279. register_matching_rule_plugins(void)
  280. {
  281. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  282. }
  283. /*
  284. * register_ces_like_plugin(): register all items for a cis-like plugin.
  285. */
  286. static int
  287. register_ces_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  288. char **names, char *oid, void *validate_fn )
  289. {
  290. int rc, flags;
  291. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  292. (void *) SLAPI_PLUGIN_VERSION_01 );
  293. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  294. (void *) pdescp );
  295. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  296. (void *) ces_filter_ava );
  297. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  298. (void *) ces_filter_sub );
  299. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  300. (void *) ces_values2keys );
  301. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  302. (void *) ces_assertion2keys_ava );
  303. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  304. (void *) ces_assertion2keys_sub );
  305. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  306. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  307. (void *) &flags );
  308. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  309. (void *) names );
  310. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  311. (void *) oid );
  312. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  313. (void *) ces_compare );
  314. if (validate_fn != NULL) {
  315. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  316. (void *)validate_fn );
  317. }
  318. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  319. (void *)ces_normalize );
  320. return( rc );
  321. }
  322. int
  323. ces_init( Slapi_PBlock *pb )
  324. {
  325. int rc;
  326. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> ces_init\n", 0, 0, 0 );
  327. rc = register_ces_like_plugin(pb,&ia5_pdesc,ia5_names,IA5STRING_SYNTAX_OID, ia5_validate);
  328. rc |= register_matching_rule_plugins();
  329. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= ces_init %d\n", rc, 0, 0 );
  330. return( rc );
  331. }
  332. int
  333. uri_init( Slapi_PBlock *pb )
  334. {
  335. int rc;
  336. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> uri_init\n", 0, 0, 0 );
  337. rc = register_ces_like_plugin(pb,&uri_pdesc,uri_names,
  338. "1.3.6.1.4.1.4401.1.1.1", NULL);
  339. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= uri_init %d\n", rc, 0, 0 );
  340. return( rc );
  341. }
  342. static int
  343. ces_filter_ava(
  344. Slapi_PBlock *pb,
  345. struct berval *bvfilter,
  346. Slapi_Value **bvals,
  347. int ftype,
  348. Slapi_Value **retVal
  349. )
  350. {
  351. int filter_normalized = 0;
  352. int syntax = SYNTAX_CES;
  353. if (pb) {
  354. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  355. &filter_normalized );
  356. if (filter_normalized) {
  357. syntax |= SYNTAX_NORM_FILT;
  358. }
  359. }
  360. return( string_filter_ava( bvfilter, bvals, syntax, ftype,
  361. retVal) );
  362. }
  363. static int
  364. ces_filter_sub(
  365. Slapi_PBlock *pb,
  366. char *initial,
  367. char **any,
  368. char *final,
  369. Slapi_Value **bvals
  370. )
  371. {
  372. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CES ) );
  373. }
  374. static int
  375. ces_values2keys(
  376. Slapi_PBlock *pb,
  377. Slapi_Value **vals,
  378. Slapi_Value ***ivals,
  379. int ftype
  380. )
  381. {
  382. return( string_values2keys( pb, vals, ivals, SYNTAX_CES, ftype ) );
  383. }
  384. static int
  385. ces_assertion2keys_ava(
  386. Slapi_PBlock *pb,
  387. Slapi_Value *val,
  388. Slapi_Value ***ivals,
  389. int ftype
  390. )
  391. {
  392. return(string_assertion2keys_ava( pb, val, ivals, SYNTAX_CES, ftype ));
  393. }
  394. static int
  395. ces_assertion2keys_sub(
  396. Slapi_PBlock *pb,
  397. char *initial,
  398. char **any,
  399. char *final,
  400. Slapi_Value ***ivals
  401. )
  402. {
  403. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  404. SYNTAX_CES ) );
  405. }
  406. static int ces_compare(
  407. struct berval *v1,
  408. struct berval *v2
  409. )
  410. {
  411. return value_cmp(v1,v2,SYNTAX_CES,3 /* Normalise both values */);
  412. }
  413. static int
  414. ia5_validate(
  415. struct berval *val
  416. )
  417. {
  418. int rc = 0; /* assume the value is valid */
  419. uint i = 0;
  420. if (val == NULL) {
  421. rc = 1;
  422. goto exit;
  423. }
  424. /* Per RFC 4517:
  425. *
  426. * IA5String = *(%x00-7F)
  427. */
  428. for (i=0; i < val->bv_len; i++) {
  429. if (!IS_UTF1(val->bv_val[i])) {
  430. rc = 1;
  431. goto exit;
  432. }
  433. }
  434. exit:
  435. return rc;
  436. }
  437. static void ces_normalize(
  438. Slapi_PBlock *pb,
  439. char *s,
  440. int trim_spaces,
  441. char **alt
  442. )
  443. {
  444. value_normalize_ext(s, SYNTAX_CES, trim_spaces, alt);
  445. return;
  446. }