Fix Proxy Protocol v2 handling
[exim.git] / src / src / routers / rf_get_munge_headers.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "rf_functions.h"
10
11
12/*************************************************
13* Get additional headers for a router *
14*************************************************/
15
76146973 16/* This function is called by routers to sort out the additional headers
0756eb3c
PH
17and header remove list for a particular address.
18
19Arguments:
20 addr the input address
21 rblock the router instance
22 extra_headers points to where to hang the header chain
23 remove_headers points to where to hang the remove list
24
25Returns: OK if no problem
26 DEFER if expanding a string caused a deferment
27 or a big disaster (e.g. expansion failure)
28*/
29
30int
31rf_get_munge_headers(address_item *addr, router_instance *rblock,
32 header_line **extra_headers, uschar **remove_headers)
33{
34/* Default is to retain existing headers */
0756eb3c
PH
35*extra_headers = addr->p.extra_headers;
36
76146973 37if (rblock->extra_headers)
0756eb3c 38 {
76146973
JH
39 uschar * list = rblock->extra_headers;
40 int sep = '\n';
41 uschar * s;
42 int slen;
0756eb3c 43
76146973
JH
44 while ((s = string_nextinlist(&list, &sep, NULL, 0)))
45 if (!(s = expand_string(s)))
0756eb3c 46 {
76146973
JH
47 if (!expand_string_forcedfail)
48 {
49 addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
50 rblock->name, rblock->extra_headers, expand_string_message);
51 return DEFER;
52 }
0756eb3c 53 }
76146973 54 else if ((slen = Ustrlen(s)) > 0)
0756eb3c 55 {
76146973
JH
56 /* Expand succeeded. Put extra headers at the start of the chain because
57 further down it may point to headers from other routers, which may be
58 shared with other addresses. The output function outputs them in reverse
59 order. */
60
61 header_line * h = store_get(sizeof(header_line));
0756eb3c
PH
62
63 /* We used to use string_sprintf() to add the newline if needed, but that
64 causes problems if the header line is exceedingly long (e.g. adding
65 something to a pathologically long line). So avoid it. */
66
67 if (s[slen-1] == '\n')
76146973 68 h->text = s;
0756eb3c 69 else
76146973
JH
70 {
71 h->text = store_get(slen+2);
72 memcpy(h->text, s, slen);
73 h->text[slen++] = '\n';
74 h->text[slen] = 0;
75 }
76
77 h->next = *extra_headers;
0756eb3c
PH
78 h->type = htype_other;
79 h->slen = slen;
80 *extra_headers = h;
81 }
0756eb3c
PH
82 }
83
84/* Default is to retain existing removes */
0756eb3c
PH
85*remove_headers = addr->p.remove_headers;
86
76146973
JH
87/* Expand items from colon-sep list separately, then build new list */
88if (rblock->remove_headers)
0756eb3c 89 {
76146973
JH
90 uschar * list = rblock->remove_headers;
91 int sep = ':';
92 uschar * s;
93 uschar buffer[128];
94
95 while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))))
96 if (!(s = expand_string(s)))
0756eb3c 97 {
76146973
JH
98 if (!expand_string_forcedfail)
99 {
100 addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
101 rblock->name, rblock->remove_headers, expand_string_message);
102 return DEFER;
103 }
0756eb3c 104 }
76146973
JH
105 else if (*s)
106 *remove_headers = string_append_listele(*remove_headers, ':', s);
0756eb3c
PH
107 }
108
109return OK;
110}
111
112/* End of rf_get_munge_headers.c */