telex.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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) 2009 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* telex.c - Telex Number syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int telex_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int telex_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int telex_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int telex_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int telex_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int telex_compare(struct berval *v1, struct berval *v2);
  57. static int telex_validate(struct berval *val);
  58. static void telex_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 4517 */
  65. static char *names[] = { "Telex Number", "telexnumber", TELEXNUMBER_SYNTAX_OID, 0 };
  66. static Slapi_PluginDesc pdesc = { "telex-syntax", VENDOR, DS_PACKAGE_VERSION,
  67. "Telex Number attribute syntax plugin" };
  68. int
  69. telex_init( Slapi_PBlock *pb )
  70. {
  71. int rc, flags;
  72. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> telex_init\n", 0, 0, 0 );
  73. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  74. (void *) SLAPI_PLUGIN_VERSION_01 );
  75. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  76. (void *)&pdesc );
  77. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  78. (void *) telex_filter_ava );
  79. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  80. (void *) telex_filter_sub );
  81. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  82. (void *) telex_values2keys );
  83. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  84. (void *) telex_assertion2keys_ava );
  85. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  86. (void *) telex_assertion2keys_sub );
  87. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  88. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  89. (void *) &flags );
  90. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  91. (void *) names );
  92. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  93. (void *) TELEXNUMBER_SYNTAX_OID );
  94. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  95. (void *) telex_compare );
  96. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  97. (void *) telex_validate );
  98. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  99. (void *) telex_normalize );
  100. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= telex_init %d\n", rc, 0, 0 );
  101. return( rc );
  102. }
  103. static int
  104. telex_filter_ava(
  105. Slapi_PBlock *pb,
  106. struct berval *bvfilter,
  107. Slapi_Value **bvals,
  108. int ftype,
  109. Slapi_Value **retVal
  110. )
  111. {
  112. int filter_normalized = 0;
  113. int syntax = SYNTAX_CIS;
  114. if (pb) {
  115. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  116. &filter_normalized );
  117. if (filter_normalized) {
  118. syntax |= SYNTAX_NORM_FILT;
  119. }
  120. }
  121. return( string_filter_ava( bvfilter, bvals, syntax,
  122. ftype, retVal ) );
  123. }
  124. static int
  125. telex_filter_sub(
  126. Slapi_PBlock *pb,
  127. char *initial,
  128. char **any,
  129. char *final,
  130. Slapi_Value **bvals
  131. )
  132. {
  133. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  134. }
  135. static int
  136. telex_values2keys(
  137. Slapi_PBlock *pb,
  138. Slapi_Value **vals,
  139. Slapi_Value ***ivals,
  140. int ftype
  141. )
  142. {
  143. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS,
  144. ftype ) );
  145. }
  146. static int
  147. telex_assertion2keys_ava(
  148. Slapi_PBlock *pb,
  149. Slapi_Value *val,
  150. Slapi_Value ***ivals,
  151. int ftype
  152. )
  153. {
  154. return(string_assertion2keys_ava( pb, val, ivals,
  155. SYNTAX_CIS, ftype ));
  156. }
  157. static int
  158. telex_assertion2keys_sub(
  159. Slapi_PBlock *pb,
  160. char *initial,
  161. char **any,
  162. char *final,
  163. Slapi_Value ***ivals
  164. )
  165. {
  166. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  167. SYNTAX_CIS ) );
  168. }
  169. static int telex_compare(
  170. struct berval *v1,
  171. struct berval *v2
  172. )
  173. {
  174. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  175. }
  176. static int
  177. telex_validate(
  178. struct berval *val
  179. )
  180. {
  181. int rc = 0; /* assume the value is valid */
  182. const char *start = NULL;
  183. const char *end = NULL;
  184. const char *p = NULL;
  185. const char *p2 = NULL;
  186. int num_dollars = 0;
  187. /* Per RFC4517:
  188. *
  189. * telex-number = actual-number DOLLAR country-code
  190. * DOLLAR answerback
  191. * actual-number = PrintableString
  192. * country-code = PrintableString
  193. * answerback = PrintableString
  194. */
  195. /* Don't allow a 0 length string */
  196. if ((val == NULL) || (val->bv_len == 0)) {
  197. rc = 1;
  198. goto exit;
  199. }
  200. start = &(val->bv_val[0]);
  201. end = &(val->bv_val[val->bv_len - 1]);
  202. /* Look for the DOLLAR separators. */
  203. for (p = start; p <= end; p++) {
  204. if (IS_DOLLAR(*p)) {
  205. num_dollars++;
  206. /* Ensure we don't have an empty element. */
  207. if ((p == start) || (p == end)) {
  208. rc = 1;
  209. goto exit;
  210. }
  211. for (p2 = start; p2 < p; p2++) {
  212. if (!IS_PRINTABLE(*p2)) {
  213. rc = 1;
  214. goto exit;
  215. }
  216. }
  217. /* Reset start to the beginning
  218. * of the next element. We're
  219. * guaranteed to have another
  220. * char after p. */
  221. start = p + 1;
  222. if (num_dollars == 2) {
  223. /* Validate the answerback element
  224. * and exit. */
  225. for (p2 = start; p2 <= end; p2++) {
  226. if (!IS_PRINTABLE(*p2)) {
  227. rc = 1;
  228. goto exit;
  229. }
  230. }
  231. /* We've hit the end and it's
  232. * all valid. We're done. */
  233. goto exit;
  234. }
  235. }
  236. }
  237. /* Make sure we found all three elements. */
  238. if (num_dollars != 2) {
  239. rc = 1;
  240. goto exit;
  241. }
  242. exit:
  243. return rc;
  244. }
  245. static void telex_normalize(
  246. Slapi_PBlock *pb,
  247. char *s,
  248. int trim_spaces,
  249. char **alt
  250. )
  251. {
  252. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  253. return;
  254. }