tel.c 9.8 KB

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