1
0

deliverymethod.c 7.6 KB

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