deliverymethod.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /* deliverymethod.c - Delivery Method syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int delivery_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int delivery_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int delivery_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int delivery_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int delivery_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int delivery_compare(struct berval *v1, struct berval *v2);
  57. static int delivery_validate(struct berval *val);
  58. static int pdm_validate(const char *start, const char *end);
  59. static void delivery_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[] = { "Delivery Method", "delivery", DELIVERYMETHOD_SYNTAX_OID, 0 };
  67. static Slapi_PluginDesc pdesc = { "delivery-syntax", VENDOR, DS_PACKAGE_VERSION,
  68. "Delivery Method attribute syntax plugin" };
  69. int
  70. delivery_init( Slapi_PBlock *pb )
  71. {
  72. int rc, flags;
  73. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> delivery_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 *) delivery_filter_ava );
  80. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  81. (void *) delivery_filter_sub );
  82. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  83. (void *) delivery_values2keys );
  84. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  85. (void *) delivery_assertion2keys_ava );
  86. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  87. (void *) delivery_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 *) DELIVERYMETHOD_SYNTAX_OID );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  96. (void *) delivery_compare );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  98. (void *) delivery_validate );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  100. (void *) delivery_normalize );
  101. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= delivery_init %d\n", rc, 0, 0 );
  102. return( rc );
  103. }
  104. static int
  105. delivery_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. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  116. if (filter_normalized) {
  117. syntax |= SYNTAX_NORM_FILT;
  118. }
  119. return( string_filter_ava( bvfilter, bvals, syntax,
  120. ftype, retVal ) );
  121. }
  122. static int
  123. delivery_filter_sub(
  124. Slapi_PBlock *pb,
  125. char *initial,
  126. char **any,
  127. char *final,
  128. Slapi_Value **bvals
  129. )
  130. {
  131. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  132. }
  133. static int
  134. delivery_values2keys(
  135. Slapi_PBlock *pb,
  136. Slapi_Value **vals,
  137. Slapi_Value ***ivals,
  138. int ftype
  139. )
  140. {
  141. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS,
  142. ftype ) );
  143. }
  144. static int
  145. delivery_assertion2keys_ava(
  146. Slapi_PBlock *pb,
  147. Slapi_Value *val,
  148. Slapi_Value ***ivals,
  149. int ftype
  150. )
  151. {
  152. return(string_assertion2keys_ava( pb, val, ivals,
  153. SYNTAX_CIS, ftype ));
  154. }
  155. static int
  156. delivery_assertion2keys_sub(
  157. Slapi_PBlock *pb,
  158. char *initial,
  159. char **any,
  160. char *final,
  161. Slapi_Value ***ivals
  162. )
  163. {
  164. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  165. SYNTAX_CIS ) );
  166. }
  167. static int delivery_compare(
  168. struct berval *v1,
  169. struct berval *v2
  170. )
  171. {
  172. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  173. }
  174. static int
  175. delivery_validate(
  176. struct berval *val
  177. )
  178. {
  179. int rc = 0; /* assume the value is valid */
  180. const char *start = NULL;
  181. const char *end = NULL;
  182. const char *p = NULL;
  183. /* Per RFC4517:
  184. *
  185. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  186. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  187. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  188. */
  189. /* Don't allow a 0 length string */
  190. if ((val == NULL) || (val->bv_len == 0)) {
  191. rc = 1;
  192. goto exit;
  193. }
  194. start = &(val->bv_val[0]);
  195. end = &(val->bv_val[val->bv_len - 1]);
  196. /* Loop through each delivery method. */
  197. for (p = start; p <= end; p++) {
  198. if (p == end) {
  199. /* Validate start through p */
  200. rc = pdm_validate(start, p);
  201. goto exit;
  202. } else if (IS_SPACE(*p) || IS_DOLLAR(*p)) {
  203. /* Validate start through p-1. Advance
  204. * pointer to next start char. */
  205. if ((rc = pdm_validate(start, p - 1)) != 0) {
  206. goto exit;
  207. } else {
  208. int got_separator = 0;
  209. /* Advance until we find the
  210. * start of the next pdm. */
  211. for (p++; p <= end; p++) {
  212. /* If we hit the end before encountering
  213. * another pdm, fail. We can do this check
  214. * without looking at what the actual char
  215. * is first since no single char is a valid
  216. * pdm. */
  217. if (p == end) {
  218. rc = 1;
  219. goto exit;
  220. } else if (IS_DOLLAR(*p)) {
  221. /* Only allow one '$' between pdm's. */
  222. if (got_separator) {
  223. rc = 1;
  224. goto exit;
  225. } else {
  226. got_separator = 1;
  227. }
  228. } else if (!IS_SPACE(*p)) {
  229. /* Set start to point to what
  230. * should be the start of the
  231. * next pdm. */
  232. start = p;
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. exit:
  240. return rc;
  241. }
  242. /*
  243. * pdm_validate()
  244. *
  245. * Returns 0 if the string from start to end is a valid
  246. * pdm, otherwise returns 1.
  247. */
  248. static int
  249. pdm_validate(const char *start, const char *end)
  250. {
  251. int rc = 0; /* Assume string is valid */
  252. size_t length = 0;
  253. if ((start == NULL) || (end == NULL)) {
  254. rc = 1;
  255. goto exit;
  256. }
  257. /* Per RFC4517:
  258. *
  259. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  260. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  261. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  262. */
  263. /* Check length first for efficiency. */
  264. length = end - start + 1;
  265. switch (length) {
  266. case 3:
  267. if ((strncmp(start, "any", length) != 0) &&
  268. (strncmp(start, "mhs", length) != 0) &&
  269. (strncmp(start, "ia5", length) != 0)) {
  270. rc = 1;
  271. }
  272. break;
  273. case 5:
  274. if ((strncmp(start, "telex", length) != 0) &&
  275. (strncmp(start, "g3fax", length) != 0) &&
  276. (strncmp(start, "g4fax", length) != 0)) {
  277. rc = 1;
  278. }
  279. break;
  280. case 7:
  281. if (strncmp(start, "teletex", length) != 0) {
  282. rc = 1;
  283. }
  284. break;
  285. case 8:
  286. if ((strncmp(start, "physical", length) != 0) &&
  287. (strncmp(start, "videotex", length) != 0)) {
  288. rc = 1;
  289. }
  290. break;
  291. case 9:
  292. if (strncmp(start, "telephone", length) != 0) {
  293. rc = 1;
  294. }
  295. break;
  296. default:
  297. rc = 1;
  298. break;
  299. }
  300. exit:
  301. return rc;
  302. }
  303. static void delivery_normalize(
  304. Slapi_PBlock *pb,
  305. char *s,
  306. int trim_spaces,
  307. char **alt
  308. )
  309. {
  310. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  311. return;
  312. }