deliverymethod.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /* deliverymethod.c - Delivery Method syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int delivery_filter_ava(Slapi_PBlock *pb, struct berval *bvfilter, Slapi_Value **bvals, int ftype, Slapi_Value **retVal);
  18. static int delivery_filter_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value **bvals);
  19. static int delivery_values2keys(Slapi_PBlock *pb, Slapi_Value **val, Slapi_Value ***ivals, int ftype);
  20. static int delivery_assertion2keys_ava(Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype);
  21. static int delivery_assertion2keys_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value ***ivals);
  22. static int delivery_compare(struct berval *v1, struct berval *v2);
  23. static int delivery_validate(struct berval *val);
  24. static int pdm_validate(const char *start, const char *end);
  25. static void delivery_normalize(
  26. Slapi_PBlock *pb,
  27. char *s,
  28. int trim_spaces,
  29. char **alt);
  30. /* the first name is the official one from RFC 4517 */
  31. static char *names[] = {"Delivery Method", "delivery", DELIVERYMETHOD_SYNTAX_OID, 0};
  32. static Slapi_PluginDesc pdesc = {"delivery-syntax", VENDOR, DS_PACKAGE_VERSION,
  33. "Delivery Method attribute syntax plugin"};
  34. int
  35. delivery_init(Slapi_PBlock *pb)
  36. {
  37. int rc, flags;
  38. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "=> delivery_init\n");
  39. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  40. (void *)SLAPI_PLUGIN_VERSION_01);
  41. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  42. (void *)&pdesc);
  43. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  44. (void *)delivery_filter_ava);
  45. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  46. (void *)delivery_filter_sub);
  47. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  48. (void *)delivery_values2keys);
  49. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  50. (void *)delivery_assertion2keys_ava);
  51. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  52. (void *)delivery_assertion2keys_sub);
  53. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  54. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  55. (void *)&flags);
  56. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  57. (void *)names);
  58. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_OID,
  59. (void *)DELIVERYMETHOD_SYNTAX_OID);
  60. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  61. (void *)delivery_compare);
  62. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  63. (void *)delivery_validate);
  64. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  65. (void *)delivery_normalize);
  66. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM, "<= delivery_init %d\n", rc);
  67. return (rc);
  68. }
  69. static int
  70. delivery_filter_ava(
  71. Slapi_PBlock *pb,
  72. struct berval *bvfilter,
  73. Slapi_Value **bvals,
  74. int ftype,
  75. Slapi_Value **retVal)
  76. {
  77. int filter_normalized = 0;
  78. int syntax = SYNTAX_CIS;
  79. if (pb) {
  80. slapi_pblock_get(pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  81. &filter_normalized);
  82. if (filter_normalized) {
  83. syntax |= SYNTAX_NORM_FILT;
  84. }
  85. }
  86. return (string_filter_ava(bvfilter, bvals, syntax,
  87. ftype, retVal));
  88. }
  89. static int
  90. delivery_filter_sub(
  91. Slapi_PBlock *pb,
  92. char *initial,
  93. char **any,
  94. char * final,
  95. Slapi_Value **bvals)
  96. {
  97. return (string_filter_sub(pb, initial, any, final, bvals, SYNTAX_CIS));
  98. }
  99. static int
  100. delivery_values2keys(
  101. Slapi_PBlock *pb,
  102. Slapi_Value **vals,
  103. Slapi_Value ***ivals,
  104. int ftype)
  105. {
  106. return (string_values2keys(pb, vals, ivals, SYNTAX_CIS,
  107. ftype));
  108. }
  109. static int
  110. delivery_assertion2keys_ava(
  111. Slapi_PBlock *pb,
  112. Slapi_Value *val,
  113. Slapi_Value ***ivals,
  114. int ftype)
  115. {
  116. return (string_assertion2keys_ava(pb, val, ivals,
  117. SYNTAX_CIS, ftype));
  118. }
  119. static int
  120. delivery_assertion2keys_sub(
  121. Slapi_PBlock *pb,
  122. char *initial,
  123. char **any,
  124. char * final,
  125. Slapi_Value ***ivals)
  126. {
  127. return (string_assertion2keys_sub(pb, initial, any, final, ivals,
  128. SYNTAX_CIS));
  129. }
  130. static int
  131. delivery_compare(
  132. struct berval *v1,
  133. struct berval *v2)
  134. {
  135. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  136. }
  137. static int
  138. delivery_validate(
  139. struct berval *val)
  140. {
  141. int rc = 0; /* assume the value is valid */
  142. const char *start = NULL;
  143. const char *end = NULL;
  144. const char *p = NULL;
  145. /* Per RFC4517:
  146. *
  147. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  148. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  149. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  150. */
  151. /* Don't allow a 0 length string */
  152. if ((val == NULL) || (val->bv_len == 0)) {
  153. rc = 1;
  154. goto exit;
  155. }
  156. start = &(val->bv_val[0]);
  157. end = &(val->bv_val[val->bv_len - 1]);
  158. /* Loop through each delivery method. */
  159. for (p = start; p <= end; p++) {
  160. if (p == end) {
  161. /* Validate start through p */
  162. rc = pdm_validate(start, p);
  163. goto exit;
  164. } else if (IS_SPACE(*p) || IS_DOLLAR(*p)) {
  165. /* Validate start through p-1. Advance
  166. * pointer to next start char. */
  167. if ((rc = pdm_validate(start, p - 1)) != 0) {
  168. goto exit;
  169. } else {
  170. int got_separator = 0;
  171. /* Advance until we find the
  172. * start of the next pdm. */
  173. for (p++; p <= end; p++) {
  174. /* If we hit the end before encountering
  175. * another pdm, fail. We can do this check
  176. * without looking at what the actual char
  177. * is first since no single char is a valid
  178. * pdm. */
  179. if (p == end) {
  180. rc = 1;
  181. goto exit;
  182. } else if (IS_DOLLAR(*p)) {
  183. /* Only allow one '$' between pdm's. */
  184. if (got_separator) {
  185. rc = 1;
  186. goto exit;
  187. } else {
  188. got_separator = 1;
  189. }
  190. } else if (!IS_SPACE(*p)) {
  191. /* Set start to point to what
  192. * should be the start of the
  193. * next pdm. */
  194. start = p;
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. exit:
  202. return rc;
  203. }
  204. /*
  205. * pdm_validate()
  206. *
  207. * Returns 0 if the string from start to end is a valid
  208. * pdm, otherwise returns 1.
  209. */
  210. static int
  211. pdm_validate(const char *start, const char *end)
  212. {
  213. int rc = 0; /* Assume string is valid */
  214. size_t length = 0;
  215. if ((start == NULL) || (end == NULL)) {
  216. rc = 1;
  217. goto exit;
  218. }
  219. /* Per RFC4517:
  220. *
  221. * DeliveryMethod = pdm *( WSP DOLLAR WSP pdm )
  222. * pdm = "any" / "mhs" / "physical" / "telex" / "teletex" /
  223. * "g3fax" / "g4fax" / "ia5" / "videotex" / "telephone"
  224. */
  225. /* Check length first for efficiency. */
  226. length = end - start + 1;
  227. switch (length) {
  228. case 3:
  229. if ((strncmp(start, "any", length) != 0) &&
  230. (strncmp(start, "mhs", length) != 0) &&
  231. (strncmp(start, "ia5", length) != 0)) {
  232. rc = 1;
  233. }
  234. break;
  235. case 5:
  236. if ((strncmp(start, "telex", length) != 0) &&
  237. (strncmp(start, "g3fax", length) != 0) &&
  238. (strncmp(start, "g4fax", length) != 0)) {
  239. rc = 1;
  240. }
  241. break;
  242. case 7:
  243. if (strncmp(start, "teletex", length) != 0) {
  244. rc = 1;
  245. }
  246. break;
  247. case 8:
  248. if ((strncmp(start, "physical", length) != 0) &&
  249. (strncmp(start, "videotex", length) != 0)) {
  250. rc = 1;
  251. }
  252. break;
  253. case 9:
  254. if (strncmp(start, "telephone", length) != 0) {
  255. rc = 1;
  256. }
  257. break;
  258. default:
  259. rc = 1;
  260. break;
  261. }
  262. exit:
  263. return rc;
  264. }
  265. static void
  266. delivery_normalize(
  267. Slapi_PBlock *pb __attribute__((unused)),
  268. char *s,
  269. int trim_spaces,
  270. char **alt)
  271. {
  272. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  273. return;
  274. }