tel.c 10 KB

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