facsimile.c 9.4 KB

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