facsimile.c 9.9 KB

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