deliverymethod.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. 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. delivery_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. delivery_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. delivery_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. delivery_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 delivery_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. delivery_validate(
  179. struct berval *val
  180. )
  181. {
  182. int rc = 0; /* assume the value is valid */
  183. const char *start = NULL;
  184. const char *end = NULL;
  185. const char *p = NULL;
  186. /* Per RFC4517:
  187. *
  188. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  189. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  190. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  191. */
  192. /* Don't allow a 0 length string */
  193. if ((val == NULL) || (val->bv_len == 0)) {
  194. rc = 1;
  195. goto exit;
  196. }
  197. start = &(val->bv_val[0]);
  198. end = &(val->bv_val[val->bv_len - 1]);
  199. /* Loop through each delivery method. */
  200. for (p = start; p <= end; p++) {
  201. if (p == end) {
  202. /* Validate start through p */
  203. rc = pdm_validate(start, p);
  204. goto exit;
  205. } else if (IS_SPACE(*p) || IS_DOLLAR(*p)) {
  206. /* Validate start through p-1. Advance
  207. * pointer to next start char. */
  208. if ((rc = pdm_validate(start, p - 1)) != 0) {
  209. goto exit;
  210. } else {
  211. int got_separator = 0;
  212. /* Advance until we find the
  213. * start of the next pdm. */
  214. for (p++; p <= end; p++) {
  215. /* If we hit the end before encountering
  216. * another pdm, fail. We can do this check
  217. * without looking at what the actual char
  218. * is first since no single char is a valid
  219. * pdm. */
  220. if (p == end) {
  221. rc = 1;
  222. goto exit;
  223. } else if (IS_DOLLAR(*p)) {
  224. /* Only allow one '$' between pdm's. */
  225. if (got_separator) {
  226. rc = 1;
  227. goto exit;
  228. } else {
  229. got_separator = 1;
  230. }
  231. } else if (!IS_SPACE(*p)) {
  232. /* Set start to point to what
  233. * should be the start of the
  234. * next pdm. */
  235. start = p;
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. exit:
  243. return rc;
  244. }
  245. /*
  246. * pdm_validate()
  247. *
  248. * Returns 0 if the string from start to end is a valid
  249. * pdm, otherwise returns 1.
  250. */
  251. static int
  252. pdm_validate(const char *start, const char *end)
  253. {
  254. int rc = 0; /* Assume string is valid */
  255. size_t length = 0;
  256. if ((start == NULL) || (end == NULL)) {
  257. rc = 1;
  258. goto exit;
  259. }
  260. /* Per RFC4517:
  261. *
  262. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  263. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  264. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  265. */
  266. /* Check length first for efficiency. */
  267. length = end - start + 1;
  268. switch (length) {
  269. case 3:
  270. if ((strncmp(start, "any", length) != 0) &&
  271. (strncmp(start, "mhs", length) != 0) &&
  272. (strncmp(start, "ia5", length) != 0)) {
  273. rc = 1;
  274. }
  275. break;
  276. case 5:
  277. if ((strncmp(start, "telex", length) != 0) &&
  278. (strncmp(start, "g3fax", length) != 0) &&
  279. (strncmp(start, "g4fax", length) != 0)) {
  280. rc = 1;
  281. }
  282. break;
  283. case 7:
  284. if (strncmp(start, "teletex", length) != 0) {
  285. rc = 1;
  286. }
  287. break;
  288. case 8:
  289. if ((strncmp(start, "physical", length) != 0) &&
  290. (strncmp(start, "videotex", length) != 0)) {
  291. rc = 1;
  292. }
  293. break;
  294. case 9:
  295. if (strncmp(start, "telephone", length) != 0) {
  296. rc = 1;
  297. }
  298. break;
  299. default:
  300. rc = 1;
  301. break;
  302. }
  303. exit:
  304. return rc;
  305. }
  306. static void delivery_normalize(
  307. Slapi_PBlock *pb,
  308. char *s,
  309. int trim_spaces,
  310. char **alt
  311. )
  312. {
  313. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  314. return;
  315. }