facsimile.c 8.0 KB

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