telex.c 6.9 KB

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