tel.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /* tel.c - telephonenumber syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int tel_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int tel_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  20. char *final, Slapi_Value **bvals );
  21. static int tel_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  22. Slapi_Value ***ivals, int ftype );
  23. static int tel_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  24. Slapi_Value ***ivals, int ftype );
  25. static int tel_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  26. char *final, Slapi_Value ***ivals );
  27. static int tel_compare(struct berval *v1, struct berval *v2);
  28. static int tel_validate(struct berval *val);
  29. static void tel_normalize(
  30. Slapi_PBlock *pb,
  31. char *s,
  32. int trim_spaces,
  33. char **alt
  34. );
  35. /* the first name is the official one from RFC 2252 */
  36. static char *names[] = { "TelephoneNumber", "tel", TELEPHONE_SYNTAX_OID, 0 };
  37. static Slapi_PluginDesc pdesc = { "tele-syntax", VENDOR, DS_PACKAGE_VERSION,
  38. "telephoneNumber attribute syntax plugin" };
  39. static const char *telephoneNumberMatch_names[] = {"telephoneNumberMatch", "2.5.13.20", NULL};
  40. static const char *telephoneNumberSubstringsMatch_names[] = {"telephoneNumberSubstringsMatch", "2.5.13.21", NULL};
  41. static char *telephoneNumberSubstringsMatch_syntaxes[] = {TELEPHONE_SYNTAX_OID, NULL};
  42. static struct mr_plugin_def mr_plugin_table[] = {
  43. {
  44. {
  45. "2.5.13.20",
  46. NULL,
  47. "telephoneNumberMatch",
  48. "The telephoneNumberMatch rule compares an assertion value of the "
  49. "Telephone Number syntax to an attribute value of a syntax (e.g., the "
  50. "Telephone Number syntax) whose corresponding ASN.1 type is a "
  51. "PrintableString representing a telephone number. "
  52. "The rule evaluates to TRUE if and only if the prepared attribute "
  53. "value character string and the prepared assertion value character "
  54. "string have the same number of characters and corresponding "
  55. "characters have the same code point. "
  56. "In preparing the attribute value and assertion value for comparison, "
  57. "characters are case folded in the Map preparation step, and only "
  58. "telephoneNumber Insignificant Character Handling is applied in the "
  59. "Insignificant Character Handling step.",
  60. TELEPHONE_SYNTAX_OID,
  61. 0,
  62. NULL /* tel syntax only */
  63. }, /* matching rule desc */
  64. {
  65. "telephoneNumberMatch-mr",
  66. VENDOR,
  67. DS_PACKAGE_VERSION,
  68. "telephoneNumberMatch matching rule plugin"
  69. }, /* plugin desc */
  70. telephoneNumberMatch_names, /* matching rule name/oid/aliases */
  71. NULL, /* mr_filter_create */
  72. NULL, /* mr_indexer_create */
  73. tel_filter_ava, /* mr_filter_ava */
  74. NULL, /* mr_filter_sub */
  75. tel_values2keys, /* mr_values2keys */
  76. tel_assertion2keys_ava, /* mr_assertion2keys_ava */
  77. NULL, /* mr_assertion2keys_sub */
  78. tel_compare, /* mr_compare */
  79. NULL /* mr_normalize */
  80. },
  81. {
  82. {
  83. "2.5.13.21",
  84. NULL,
  85. "telephoneNumberSubstringsMatch",
  86. "The telephoneNumberSubstringsMatch rule compares an assertion value "
  87. "of the Substring Assertion syntax to an attribute value of a syntax "
  88. "(e.g., the Telephone Number syntax) whose corresponding ASN.1 type is "
  89. "a PrintableString representing a telephone number. "
  90. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  91. "of the assertion value match disjoint portions of the prepared "
  92. "attribute value character string in the order of the substrings in "
  93. "the assertion value, (2) an <initial> substring, if present, matches "
  94. "the beginning of the prepared attribute value character string, and "
  95. "(3) a <final> substring, if present, matches the end of the prepared "
  96. "attribute value character string. A prepared substring matches a "
  97. "portion of the prepared attribute value character string if "
  98. "corresponding characters have the same code point. "
  99. "In preparing the attribute value and assertion value substrings for "
  100. "comparison, characters are case folded in the Map preparation step, "
  101. "and only telephoneNumber Insignificant Character Handling is applied "
  102. "in the Insignificant Character Handling step.",
  103. "1.3.6.1.4.1.1466.115.121.1.58",
  104. 0,
  105. telephoneNumberSubstringsMatch_syntaxes
  106. }, /* matching rule desc */
  107. {
  108. "telephoneNumberSubstringsMatch-mr",
  109. VENDOR,
  110. DS_PACKAGE_VERSION,
  111. "telephoneNumberSubstringsMatch matching rule plugin"
  112. }, /* plugin desc */
  113. telephoneNumberSubstringsMatch_names, /* matching rule name/oid/aliases */
  114. NULL, /* IFP mr_filter_create; */
  115. NULL, /* IFP mr_indexer_create; */
  116. NULL, /* mr_filter_ava */
  117. tel_filter_sub, /* mr_filter_sub */
  118. tel_values2keys, /* mr_values2keys */
  119. NULL, /*mr_assertion2keys_ava */
  120. tel_assertion2keys_sub, /* mr_assertion2keys_sub */
  121. tel_compare, /* mr_compare */
  122. NULL /* mr_normalize */
  123. },
  124. };
  125. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  126. static int
  127. matching_rule_plugin_init(Slapi_PBlock *pb)
  128. {
  129. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  130. }
  131. static int
  132. register_matching_rule_plugins(void)
  133. {
  134. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  135. }
  136. int
  137. tel_init( Slapi_PBlock *pb )
  138. {
  139. int rc, flags;
  140. LDAPDebug(LDAP_DEBUG_PLUGIN, "=> tel_init\n", 0, 0, 0 );
  141. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  142. (void *) SLAPI_PLUGIN_VERSION_01 );
  143. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  144. (void *)&pdesc );
  145. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  146. (void *) tel_filter_ava );
  147. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  148. (void *) tel_filter_sub );
  149. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  150. (void *) tel_values2keys );
  151. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  152. (void *) tel_assertion2keys_ava );
  153. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  154. (void *) tel_assertion2keys_sub );
  155. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  156. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  157. (void *) &flags );
  158. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  159. (void *) names );
  160. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  161. (void *) TELEPHONE_SYNTAX_OID );
  162. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  163. (void *) tel_compare );
  164. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  165. (void *) tel_validate );
  166. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  167. (void *) tel_normalize );
  168. rc |= register_matching_rule_plugins();
  169. LDAPDebug(LDAP_DEBUG_PLUGIN, "<= tel_init %d\n", rc, 0, 0 );
  170. return( rc );
  171. }
  172. static int
  173. tel_filter_ava(
  174. Slapi_PBlock *pb,
  175. struct berval *bvfilter,
  176. Slapi_Value **bvals,
  177. int ftype,
  178. Slapi_Value **retVal
  179. )
  180. {
  181. int filter_normalized = 0;
  182. int syntax = SYNTAX_TEL | SYNTAX_CIS;
  183. if (pb) {
  184. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  185. &filter_normalized );
  186. if (filter_normalized) {
  187. syntax |= SYNTAX_NORM_FILT;
  188. }
  189. }
  190. return( string_filter_ava( bvfilter, bvals, syntax,
  191. ftype, retVal ) );
  192. }
  193. static int
  194. tel_filter_sub(
  195. Slapi_PBlock *pb,
  196. char *initial,
  197. char **any,
  198. char *final,
  199. Slapi_Value **bvals
  200. )
  201. {
  202. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_TEL | SYNTAX_CIS ) );
  203. }
  204. static int
  205. tel_values2keys(
  206. Slapi_PBlock *pb,
  207. Slapi_Value **vals,
  208. Slapi_Value ***ivals,
  209. int ftype
  210. )
  211. {
  212. return( string_values2keys( pb, vals, ivals, SYNTAX_TEL | SYNTAX_CIS,
  213. ftype ) );
  214. }
  215. static int
  216. tel_assertion2keys_ava(
  217. Slapi_PBlock *pb,
  218. Slapi_Value *val,
  219. Slapi_Value ***ivals,
  220. int ftype
  221. )
  222. {
  223. return(string_assertion2keys_ava( pb, val, ivals,
  224. SYNTAX_TEL | SYNTAX_CIS, ftype ));
  225. }
  226. static int
  227. tel_assertion2keys_sub(
  228. Slapi_PBlock *pb,
  229. char *initial,
  230. char **any,
  231. char *final,
  232. Slapi_Value ***ivals
  233. )
  234. {
  235. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  236. SYNTAX_TEL | SYNTAX_CIS ) );
  237. }
  238. static int tel_compare(
  239. struct berval *v1,
  240. struct berval *v2
  241. )
  242. {
  243. return value_cmp(v1, v2, SYNTAX_TEL|SYNTAX_CIS, 3 /* Normalise both values */);
  244. }
  245. static int
  246. tel_validate(
  247. struct berval *val
  248. )
  249. {
  250. int rc = 0; /* assume the value is valid */
  251. uint i = 0;
  252. /* Per RFC4517:
  253. *
  254. * TelephoneNumber = PrintableString
  255. * PrintableString = 1*PrintableCharacter
  256. */
  257. /* Don't allow a 0 length string */
  258. if ((val == NULL) || (val->bv_len == 0)) {
  259. rc = 1;
  260. goto exit;
  261. }
  262. /* Make sure all chars are a PrintableCharacter */
  263. for (i=0; i < val->bv_len; i++) {
  264. if (!IS_PRINTABLE(val->bv_val[i])) {
  265. rc = 1;
  266. goto exit;
  267. }
  268. }
  269. exit:
  270. return rc;
  271. }
  272. static void tel_normalize(
  273. Slapi_PBlock *pb,
  274. char *s,
  275. int trim_spaces,
  276. char **alt
  277. )
  278. {
  279. value_normalize_ext(s, SYNTAX_TEL|SYNTAX_CIS, trim_spaces, alt);
  280. return;
  281. }