1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2021 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include "rf_functions.h"
14 /*************************************************
15 * Get additional headers for a router *
16 *************************************************/
18 /* This function is called by routers to sort out the additional headers
19 and header remove list for a particular address.
22 addr the input address
23 rblock the router instance
24 extra_headers points to where to hang the header chain
25 remove_headers points to where to hang the remove list
27 Returns: OK if no problem
28 DEFER if expanding a string caused a deferment
29 or a big disaster (e.g. expansion failure)
33 rf_get_munge_headers(address_item *addr, router_instance *rblock,
34 header_line **extra_headers, uschar **remove_headers)
36 /* Default is to retain existing headers */
37 *extra_headers = addr->prop.extra_headers;
39 if (rblock->extra_headers)
41 const uschar * list = rblock->extra_headers;
46 while ((s = string_nextinlist(&list, &sep, NULL, 0)))
47 if (!(s = expand_string(t = s)))
49 if (!f.expand_string_forcedfail)
51 addr->message = string_sprintf(
52 "%s router failed to expand add_headers item \"%s\": %s",
53 rblock->drinst.name, t, expand_string_message);
57 else if ((slen = Ustrlen(s)) > 0)
59 /* Expand succeeded. Put extra headers at the start of the chain because
60 further down it may point to headers from other routers, which may be
61 shared with other addresses. The output function outputs them in reverse
64 header_line * h = store_get(sizeof(header_line), GET_UNTAINTED);
66 /* We used to use string_sprintf() to add the newline if needed, but that
67 causes problems if the header line is exceedingly long (e.g. adding
68 something to a pathologically long line). So avoid it. */
70 if (s[slen-1] == '\n')
74 h->text = store_get(slen+2, s);
75 memcpy(h->text, s, slen);
76 h->text[slen++] = '\n';
80 h->next = *extra_headers;
81 h->type = htype_other;
87 /* Default is to retain existing removes */
88 *remove_headers = addr->prop.remove_headers;
90 /* Expand items from colon-sep list separately, then build new list */
91 if (rblock->remove_headers)
93 const uschar * list = rblock->remove_headers;
99 g = string_cat(NULL, *remove_headers);
101 while ((s = string_nextinlist(&list, &sep, NULL, 0)))
102 if (!(s = expand_string(t = s)))
104 if (!f.expand_string_forcedfail)
106 addr->message = string_sprintf(
107 "%s router failed to expand remove_headers item \"%s\": %s",
108 rblock->drinst.name, t, expand_string_message);
113 g = string_append_listele(g, ':', s);
116 *remove_headers = g->s;
124 /* End of rf_get_munge_headers.c */