facsimile.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /* facsimile.c - Facsimile Telephone Number syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int facsimile_filter_ava(Slapi_PBlock *pb, struct berval *bvfilter, Slapi_Value **bvals, int ftype, Slapi_Value **retVal);
  18. static int facsimile_filter_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value **bvals);
  19. static int facsimile_values2keys(Slapi_PBlock *pb, Slapi_Value **val, Slapi_Value ***ivals, int ftype);
  20. static int facsimile_assertion2keys_ava(Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype);
  21. static int facsimile_assertion2keys_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value ***ivals);
  22. static int facsimile_compare(struct berval *v1, struct berval *v2);
  23. static int facsimile_validate(struct berval *val);
  24. static int fax_parameter_validate(const char *start, const char *end);
  25. static void facsimile_normalize(
  26. Slapi_PBlock *pb,
  27. char *s,
  28. int trim_spaces,
  29. char **alt);
  30. /* the first name is the official one from RFC 4517 */
  31. static char *names[] = {"Facsimile Telephone Number", "facsimile", FACSIMILE_SYNTAX_OID, 0};
  32. static Slapi_PluginDesc pdesc = {"facsimile-syntax", VENDOR, DS_PACKAGE_VERSION,
  33. "Facsimile Telephone Number attribute syntax plugin"};
  34. int
  35. facsimile_init(Slapi_PBlock *pb)
  36. {
  37. int rc, flags;
  38. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "=> facsimile_init\n");
  39. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  40. (void *)SLAPI_PLUGIN_VERSION_01);
  41. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  42. (void *)&pdesc);
  43. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  44. (void *)facsimile_filter_ava);
  45. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  46. (void *)facsimile_filter_sub);
  47. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  48. (void *)facsimile_values2keys);
  49. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  50. (void *)facsimile_assertion2keys_ava);
  51. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  52. (void *)facsimile_assertion2keys_sub);
  53. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  54. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  55. (void *)&flags);
  56. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  57. (void *)names);
  58. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_OID,
  59. (void *)FACSIMILE_SYNTAX_OID);
  60. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  61. (void *)facsimile_compare);
  62. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  63. (void *)facsimile_validate);
  64. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  65. (void *)facsimile_normalize);
  66. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "<= facsimile_init %d\n", rc);
  67. return (rc);
  68. }
  69. static int
  70. facsimile_filter_ava(
  71. Slapi_PBlock *pb,
  72. struct berval *bvfilter,
  73. Slapi_Value **bvals,
  74. int ftype,
  75. Slapi_Value **retVal)
  76. {
  77. int filter_normalized = 0;
  78. int syntax = SYNTAX_CIS;
  79. if (pb) {
  80. slapi_pblock_get(pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  81. &filter_normalized);
  82. if (filter_normalized) {
  83. syntax |= SYNTAX_NORM_FILT;
  84. }
  85. }
  86. return (string_filter_ava(bvfilter, bvals, syntax,
  87. ftype, retVal));
  88. }
  89. static int
  90. facsimile_filter_sub(
  91. Slapi_PBlock *pb,
  92. char *initial,
  93. char **any,
  94. char * final,
  95. Slapi_Value **bvals)
  96. {
  97. return (string_filter_sub(pb, initial, any, final, bvals, SYNTAX_CIS));
  98. }
  99. static int
  100. facsimile_values2keys(
  101. Slapi_PBlock *pb,
  102. Slapi_Value **vals,
  103. Slapi_Value ***ivals,
  104. int ftype)
  105. {
  106. return (string_values2keys(pb, vals, ivals, SYNTAX_CIS,
  107. ftype));
  108. }
  109. static int
  110. facsimile_assertion2keys_ava(
  111. Slapi_PBlock *pb,
  112. Slapi_Value *val,
  113. Slapi_Value ***ivals,
  114. int ftype)
  115. {
  116. return (string_assertion2keys_ava(pb, val, ivals,
  117. SYNTAX_CIS, ftype));
  118. }
  119. static int
  120. facsimile_assertion2keys_sub(
  121. Slapi_PBlock *pb,
  122. char *initial,
  123. char **any,
  124. char * final,
  125. Slapi_Value ***ivals)
  126. {
  127. return (string_assertion2keys_sub(pb, initial, any, final, ivals,
  128. SYNTAX_CIS));
  129. }
  130. static int
  131. facsimile_compare(
  132. struct berval *v1,
  133. struct berval *v2)
  134. {
  135. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  136. }
  137. static int
  138. facsimile_validate(
  139. struct berval *val)
  140. {
  141. int rc = 0; /* assume the value is valid */
  142. uint i = 0;
  143. /* Per RFC4517:
  144. *
  145. * fax-number = telephone-number *( DOLLAR fax-parameter )
  146. * telephone-number = PrintableString
  147. * fax-parameter = "twoDimensional" /
  148. * "fineResolution" /
  149. * "unlimitedLength" /
  150. * "b4Length" /
  151. * "a3Width" /
  152. * "b4Width" /
  153. * "uncompressed"
  154. */
  155. /* Don't allow a 0 length string */
  156. if ((val == NULL) || (val->bv_len == 0)) {
  157. rc = 1;
  158. goto exit;
  159. }
  160. /* Make sure all chars are a PrintableCharacter */
  161. for (i = 0; i < val->bv_len; i++) {
  162. if (!IS_PRINTABLE(val->bv_val[i])) {
  163. if (!IS_DOLLAR(val->bv_val[i])) {
  164. rc = 1;
  165. goto exit;
  166. } else {
  167. /* Process the fax-parameters */
  168. const char *start = NULL;
  169. const char *end = &(val->bv_val[val->bv_len - 1]);
  170. const char *p = &(val->bv_val[i]);
  171. /* The value must have a printable string first,
  172. * so we can't allow it to start with a '$'. We
  173. * also need to ensure that the string does not
  174. * end with this '$'. */
  175. if ((i == 0) || (p == end)) {
  176. rc = 1;
  177. goto exit;
  178. }
  179. /* We're guaranteed to have at least one character
  180. * past p. This is where the fax-parameter should
  181. * start. */
  182. start = p + 1;
  183. for (p = start; p <= end; p++) {
  184. if (p == end) {
  185. /* Ensure start to p is a valid fax-parameter, then
  186. * exit since we're at the end. */
  187. rc = fax_parameter_validate(start, p);
  188. goto exit;
  189. } else if (*p == '$') {
  190. /* Ensure start to p-1 is a valid fax-parameter */
  191. if ((rc = fax_parameter_validate(start, p - 1)) != 0) {
  192. goto exit;
  193. }
  194. /* We're guaranteed to have another character, which
  195. * should be the beginning of the next fax-parameter.
  196. * Adjust the start pointer to point to the beginning
  197. * of this fax-parameter. */
  198. start = p + 1;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. exit:
  205. return rc;
  206. }
  207. /*
  208. * fax_parameter_validate()
  209. *
  210. * Returns 0 if the string from start to end is a valid
  211. * fax-parameter, otherwise returns 1.
  212. */
  213. static int
  214. fax_parameter_validate(const char *start, const char *end)
  215. {
  216. int rc = 0; /* Assume string is valid */
  217. size_t length = 0;
  218. if ((start == NULL) || (end == NULL)) {
  219. rc = 1;
  220. goto exit;
  221. }
  222. /* Per RFC4517:
  223. *
  224. * fax-parameter = "twoDimensional" /
  225. * "fineResolution" /
  226. * "unlimitedLength" /
  227. * "b4Length" /
  228. * "a3Width" /
  229. * "b4Width" /
  230. * "uncompressed"
  231. */
  232. /* Check length first for efficiency. */
  233. length = end - start + 1;
  234. switch (length) {
  235. case 7:
  236. if ((strncmp(start, "a3Width", length) != 0) &&
  237. (strncmp(start, "b4Width", length) != 0)) {
  238. rc = 1;
  239. }
  240. break;
  241. case 8:
  242. if (strncmp(start, "b4Length", length) != 0) {
  243. rc = 1;
  244. }
  245. break;
  246. case 12:
  247. if (strncmp(start, "uncompressed", length) != 0) {
  248. rc = 1;
  249. }
  250. break;
  251. case 14:
  252. if ((strncmp(start, "twoDimensional", length) != 0) &&
  253. (strncmp(start, "fineResolution", length) != 0)) {
  254. rc = 1;
  255. }
  256. break;
  257. case 15:
  258. if (strncmp(start, "unlimitedLength", length) != 0) {
  259. rc = 1;
  260. }
  261. break;
  262. default:
  263. rc = 1;
  264. break;
  265. }
  266. exit:
  267. return rc;
  268. }
  269. static void
  270. facsimile_normalize(
  271. Slapi_PBlock *pb __attribute__((unused)),
  272. char *s,
  273. int trim_spaces,
  274. char **alt)
  275. {
  276. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  277. return;
  278. }