telex.c 6.0 KB

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