tel.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. {{"2.5.13.20", NULL, "telephoneNumberMatch", "The telephoneNumberMatch rule compares an assertion value of the "
  44. "Telephone Number syntax to an attribute value of a syntax (e.g., the "
  45. "Telephone Number syntax) whose corresponding ASN.1 type is a "
  46. "PrintableString representing a telephone number. "
  47. "The rule evaluates to TRUE if and only if the prepared attribute "
  48. "value character string and the prepared assertion value character "
  49. "string have the same number of characters and corresponding "
  50. "characters have the same code point. "
  51. "In preparing the attribute value and assertion value for comparison, "
  52. "characters are case folded in the Map preparation step, and only "
  53. "telephoneNumber Insignificant Character Handling is applied in the "
  54. "Insignificant Character Handling step.",
  55. TELEPHONE_SYNTAX_OID, 0, NULL /* tel syntax only */}, /* matching rule desc */
  56. {"telephoneNumberMatch-mr", VENDOR, DS_PACKAGE_VERSION, "telephoneNumberMatch matching rule plugin"}, /* plugin desc */
  57. telephoneNumberMatch_names, /* matching rule name/oid/aliases */
  58. NULL, NULL, tel_filter_ava, NULL, tel_values2keys,
  59. tel_assertion2keys_ava, NULL, tel_compare},
  60. {{"2.5.13.21", NULL, "telephoneNumberSubstringsMatch", "The telephoneNumberSubstringsMatch rule compares an assertion value "
  61. "of the Substring Assertion syntax to an attribute value of a syntax "
  62. "(e.g., the Telephone Number syntax) whose corresponding ASN.1 type is "
  63. "a PrintableString representing a telephone number. "
  64. "The rule evaluates to TRUE if and only if (1) the prepared substrings "
  65. "of the assertion value match disjoint portions of the prepared "
  66. "attribute value character string in the order of the substrings in "
  67. "the assertion value, (2) an <initial> substring, if present, matches "
  68. "the beginning of the prepared attribute value character string, and "
  69. "(3) a <final> substring, if present, matches the end of the prepared "
  70. "attribute value character string. A prepared substring matches a "
  71. "portion of the prepared attribute value character string if "
  72. "corresponding characters have the same code point. "
  73. "In preparing the attribute value and assertion value substrings for "
  74. "comparison, characters are case folded in the Map preparation step, "
  75. "and only telephoneNumber Insignificant Character Handling is applied "
  76. "in the Insignificant Character Handling step.",
  77. "1.3.6.1.4.1.1466.115.121.1.58", 0, telephoneNumberSubstringsMatch_syntaxes}, /* matching rule desc */
  78. {"telephoneNumberSubstringsMatch-mr", VENDOR, DS_PACKAGE_VERSION, "telephoneNumberSubstringsMatch matching rule plugin"}, /* plugin desc */
  79. telephoneNumberSubstringsMatch_names, /* matching rule name/oid/aliases */
  80. NULL, NULL, NULL, tel_filter_sub, tel_values2keys,
  81. NULL, tel_assertion2keys_sub, tel_compare},
  82. };
  83. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  84. static int
  85. matching_rule_plugin_init(Slapi_PBlock *pb)
  86. {
  87. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  88. }
  89. static int
  90. register_matching_rule_plugins()
  91. {
  92. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  93. }
  94. int
  95. tel_init( Slapi_PBlock *pb )
  96. {
  97. int rc, flags;
  98. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> tel_init\n", 0, 0, 0 );
  99. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  100. (void *) SLAPI_PLUGIN_VERSION_01 );
  101. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  102. (void *)&pdesc );
  103. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  104. (void *) tel_filter_ava );
  105. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  106. (void *) tel_filter_sub );
  107. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  108. (void *) tel_values2keys );
  109. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  110. (void *) tel_assertion2keys_ava );
  111. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  112. (void *) tel_assertion2keys_sub );
  113. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  114. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  115. (void *) &flags );
  116. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  117. (void *) names );
  118. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  119. (void *) TELEPHONE_SYNTAX_OID );
  120. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  121. (void *) tel_compare );
  122. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  123. (void *) tel_validate );
  124. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  125. (void *) tel_normalize );
  126. rc |= register_matching_rule_plugins();
  127. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= tel_init %d\n", rc, 0, 0 );
  128. return( rc );
  129. }
  130. static int
  131. tel_filter_ava(
  132. Slapi_PBlock *pb,
  133. struct berval *bvfilter,
  134. Slapi_Value **bvals,
  135. int ftype,
  136. Slapi_Value **retVal
  137. )
  138. {
  139. int filter_normalized = 0;
  140. int syntax = SYNTAX_TEL | SYNTAX_CIS;
  141. if (pb) {
  142. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  143. &filter_normalized );
  144. if (filter_normalized) {
  145. syntax |= SYNTAX_NORM_FILT;
  146. }
  147. }
  148. return( string_filter_ava( bvfilter, bvals, syntax,
  149. ftype, retVal ) );
  150. }
  151. static int
  152. tel_filter_sub(
  153. Slapi_PBlock *pb,
  154. char *initial,
  155. char **any,
  156. char *final,
  157. Slapi_Value **bvals
  158. )
  159. {
  160. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_TEL | SYNTAX_CIS ) );
  161. }
  162. static int
  163. tel_values2keys(
  164. Slapi_PBlock *pb,
  165. Slapi_Value **vals,
  166. Slapi_Value ***ivals,
  167. int ftype
  168. )
  169. {
  170. return( string_values2keys( pb, vals, ivals, SYNTAX_TEL | SYNTAX_CIS,
  171. ftype ) );
  172. }
  173. static int
  174. tel_assertion2keys_ava(
  175. Slapi_PBlock *pb,
  176. Slapi_Value *val,
  177. Slapi_Value ***ivals,
  178. int ftype
  179. )
  180. {
  181. return(string_assertion2keys_ava( pb, val, ivals,
  182. SYNTAX_TEL | SYNTAX_CIS, ftype ));
  183. }
  184. static int
  185. tel_assertion2keys_sub(
  186. Slapi_PBlock *pb,
  187. char *initial,
  188. char **any,
  189. char *final,
  190. Slapi_Value ***ivals
  191. )
  192. {
  193. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  194. SYNTAX_TEL | SYNTAX_CIS ) );
  195. }
  196. static int tel_compare(
  197. struct berval *v1,
  198. struct berval *v2
  199. )
  200. {
  201. return value_cmp(v1, v2, SYNTAX_TEL|SYNTAX_CIS, 3 /* Normalise both values */);
  202. }
  203. static int
  204. tel_validate(
  205. struct berval *val
  206. )
  207. {
  208. int rc = 0; /* assume the value is valid */
  209. int i = 0;
  210. /* Per RFC4517:
  211. *
  212. * TelephoneNumber = PrintableString
  213. * PrintableString = 1*PrintableCharacter
  214. */
  215. /* Don't allow a 0 length string */
  216. if ((val == NULL) || (val->bv_len == 0)) {
  217. rc = 1;
  218. goto exit;
  219. }
  220. /* Make sure all chars are a PrintableCharacter */
  221. for (i=0; i < val->bv_len; i++) {
  222. if (!IS_PRINTABLE(val->bv_val[i])) {
  223. rc = 1;
  224. goto exit;
  225. }
  226. }
  227. exit:
  228. return rc;
  229. }
  230. static void tel_normalize(
  231. Slapi_PBlock *pb,
  232. char *s,
  233. int trim_spaces,
  234. char **alt
  235. )
  236. {
  237. value_normalize_ext(s, SYNTAX_TEL|SYNTAX_CIS, trim_spaces, alt);
  238. return;
  239. }