Fix reporting of 2-phase queue-runner daemon, in daemon start log line and in exiwhat
[exim.git] / src / src / header.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
80fea873 5/* Copyright (c) University of Cambridge 1995 - 2016 */
059ec3d9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8
9#include "exim.h"
10
11
12/*************************************************
13* Test a header for matching name *
14*************************************************/
15
16/* This function tests the name of a header. It is made into a function because
17it isn't just a string comparison: spaces and tabs are permitted between the
18name and the colon. The h->text field should nowadays never be NULL, but check
19it just in case.
20
21Arguments:
22 h points to the header
23 name the name to test
24 len the length of the name
25 notdel if TRUE, force FALSE for deleted headers
26
27Returns: TRUE or FALSE
28*/
29
30BOOL
1ba28e2b 31header_testname(header_line *h, const uschar *name, int len, BOOL notdel)
059ec3d9
PH
32{
33uschar *tt;
34if (h->type == '*' && notdel) return FALSE;
35if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
36tt = h->text + len;
37while (*tt == ' ' || *tt == '\t') tt++;
38return *tt == ':';
39}
40
8523533c
TK
41/* This is a copy of the function above, only that it is possible to pass
42 only the beginning of a header name. It simply does a front-anchored
43 substring match. Arguments and Return codes are the same as for
44 header_testname() above. */
45
46BOOL
1ba28e2b
PP
47header_testname_incomplete(header_line *h, const uschar *name,
48 int len, BOOL notdel)
8523533c 49{
8523533c
TK
50if (h->type == '*' && notdel) return FALSE;
51if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
52return TRUE;
53}
059ec3d9
PH
54
55
56/*************************************************
57* Add new header backend function *
58*************************************************/
59
60/* The header_last variable points to the last header during message reception
61and delivery; otherwise it is NULL. We add new headers only when header_last is
62not NULL. The function may get called sometimes when it is NULL (e.g. during
63address verification where rewriting options exist). When called from a filter,
64there may be multiple header lines in a single string.
65
66This is an internal static function that is the common back end to the external
67functions defined below. The general interface allows the header to be inserted
68before or after a given occurrence of a given header.
69
70(a) if "name" is NULL, the header is added at the end of all the existing
71 headers if "after" is true, or at the start if it is false. The "topnot"
72 flag is not used.
73
74(b) If "name" is not NULL, the first existing header with that name is sought.
75 If "after" is false, the new header is added before it. If "after" is true,
76 a check is made for adjacent headers with the same name, and the new header
77 is added after the last of them. If a header of the given name is not
78 found, the new header is added first if "topnot" is true, and at the bottom
79 otherwise.
80
81Arguments:
82 after TRUE for "after", FALSE for "before"
83 name name if adding at a specific header, else NULL
84 topnot TRUE to add at top if no header found
85 type Exim header type character (htype_something)
86 format sprintf format
87 ap va_list value for format arguments
88
049782c0 89Returns: pointer to header struct (last one, if multiple added)
059ec3d9
PH
90*/
91
049782c0 92static header_line *
059ec3d9 93header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
1ba28e2b 94 const char *format, va_list ap)
059ec3d9 95{
dca6d121 96header_line *h, *new = NULL;
059ec3d9
PH
97header_line **hptr;
98
99uschar *p, *q;
f3ebb786
JH
100uschar * buf = store_get(HEADER_ADD_BUFFER_SIZE, FALSE);
101gstring gs = { .size = HEADER_ADD_BUFFER_SIZE, .ptr = 0, .s = buf };
059ec3d9 102
049782c0 103if (!header_last) return NULL;
059ec3d9 104
f3ebb786 105if (!string_vformat(&gs, SVFMT_REBUFFER, format, ap))
059ec3d9 106 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
d12746bc 107 "%.100s ...", string_from_gstring(&gs));
f3ebb786
JH
108
109if (gs.s != buf) store_release_above(buf);
110gstring_release_unused(&gs);
d12746bc 111string_from_gstring(&gs);
059ec3d9
PH
112
113/* Find where to insert this header */
114
94759fce 115if (!name)
059ec3d9
PH
116 if (after)
117 {
dca6d121 118 hptr = &header_last->next;
059ec3d9
PH
119 h = NULL;
120 }
121 else
122 {
123 hptr = &header_list;
5eb690a1
NM
124
125 /* header_list->text can be NULL if we get here between when the new
4c04137d 126 received header is allocated and when it is actually filled in. We want
5eb690a1
NM
127 that header to be first, so skip it for now. */
128
94759fce 129 if (!header_list->text)
5eb690a1
NM
130 hptr = &header_list->next;
131 h = *hptr;
059ec3d9 132 }
059ec3d9
PH
133
134else
135 {
136 int len = Ustrlen(name);
137
4c04137d 138 /* Find the first non-deleted header with the correct name. */
059ec3d9 139
94759fce
JH
140 for (hptr = &header_list; (h = *hptr); hptr = &h->next)
141 if (header_testname(h, name, len, TRUE))
142 break;
059ec3d9
PH
143
144 /* Handle the case where no header is found. To insert at the bottom, nothing
145 needs to be done. */
146
94759fce 147 if (!h)
059ec3d9
PH
148 {
149 if (topnot)
150 {
151 hptr = &header_list;
152 h = header_list;
153 }
154 }
155
156 /* Handle the case where a header is found. Check for more if "after" is
157 true. In this case, we want to include deleted headers in the block. */
158
159 else if (after)
059ec3d9
PH
160 for (;;)
161 {
94759fce 162 if (!h->next || !header_testname(h, name, len, FALSE)) break;
dca6d121 163 hptr = &h->next;
059ec3d9
PH
164 h = h->next;
165 }
059ec3d9
PH
166 }
167
168/* Loop for multiple header lines, taking care about continuations. At this
169point, we have hptr pointing to the link field that will point to the new
170header, and h containing the following header, or NULL. */
171
f3ebb786 172for (p = q = gs.s; *p; p = q)
059ec3d9
PH
173 {
174 for (;;)
175 {
176 q = Ustrchr(q, '\n');
94759fce 177 if (!q) q = p + Ustrlen(p);
059ec3d9
PH
178 if (*(++q) != ' ' && *q != '\t') break;
179 }
180
f3ebb786 181 new = store_get(sizeof(header_line), FALSE);
059ec3d9
PH
182 new->text = string_copyn(p, q - p);
183 new->slen = q - p;
184 new->type = type;
185 new->next = h;
186
187 *hptr = new;
dca6d121 188 hptr = &new->next;
059ec3d9 189
94759fce 190 if (!h) header_last = new;
059ec3d9 191 }
049782c0 192return new;
059ec3d9
PH
193}
194
195
196/*************************************************
197* Add new header anywhere in the chain *
198*************************************************/
199
200/* This is an external interface to header_add_backend().
201
202Arguments:
203 after TRUE for "after", FALSE for "before"
204 name name if adding at a specific header, else NULL
205 topnot TRUE to add at top if no header found
206 type Exim header type character (htype_something)
207 format sprintf format
208 ... format arguments
209
049782c0 210Returns: pointer to header struct added
059ec3d9
PH
211*/
212
049782c0
JH
213header_line *
214header_add_at_position_internal(BOOL after, uschar *name, BOOL topnot, int type,
1ba28e2b 215 const char *format, ...)
059ec3d9 216{
049782c0 217header_line * h;
059ec3d9
PH
218va_list ap;
219va_start(ap, format);
049782c0 220h = header_add_backend(after, name, topnot, type, format, ap);
059ec3d9 221va_end(ap);
049782c0 222return h;
059ec3d9
PH
223}
224
225
049782c0
JH
226/* Documented external i/f for local_scan */
227void
228header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
229 const char *format, ...)
230{
049782c0
JH
231va_list ap;
232va_start(ap, format);
233(void) header_add_backend(after, name, topnot, type, format, ap);
234va_end(ap);
235}
059ec3d9
PH
236
237/*************************************************
238* Add new header on end of chain *
239*************************************************/
240
241/* This is now a convenience interface to header_add_backend().
242
243Arguments:
244 type Exim header type character
245 format sprintf format
246 ... arguments for the format
247
248Returns: nothing
249*/
250
251void
1ba28e2b 252header_add(int type, const char *format, ...)
059ec3d9
PH
253{
254va_list ap;
255va_start(ap, format);
049782c0 256(void) header_add_backend(TRUE, NULL, FALSE, type, format, ap);
059ec3d9
PH
257va_end(ap);
258}
259
260
261
262/*************************************************
263* Remove (mark as old) a header *
264*************************************************/
265
266/* This function is used by the filter code; it is also exported in the
267local_scan() API. If no header is found, the function does nothing.
268
269Arguments:
270 occ the occurrence number for multiply-defined headers
271 <= 0 means "all"; deleted headers are not counted
272 name the header name
273
274Returns: nothing
275*/
276
277void
1ba28e2b 278header_remove(int occ, const uschar *name)
059ec3d9 279{
059ec3d9
PH
280int hcount = 0;
281int len = Ustrlen(name);
d7978c0f 282for (header_line * h = header_list; h; h = h->next)
059ec3d9
PH
283 if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
284 {
285 h->type = htype_old;
286 if (occ > 0) return;
287 }
059ec3d9
PH
288}
289
290
291
292/*************************************************
293* Check the name of a header *
294*************************************************/
295
296/* This function scans a table of header field names that Exim recognizes, and
297returns the identification of a match. If "resent" is true, the header is known
298to start with "resent-". In that case, the function matches only those fields
299that are allowed to appear with resent- in front of them.
300
301Arguments:
302 h points to the header line
303 is_resent TRUE if the name starts "Resent-"
304
305Returns: One of the htype_ enum values, identifying the header
306*/
307
308int
309header_checkname(header_line *h, BOOL is_resent)
310{
311uschar *text = h->text;
312header_name *bot = header_names;
313header_name *top = header_names + header_names_size;
314
315if (is_resent) text += 7;
316
317while (bot < top)
318 {
319 header_name *mid = bot + (top - bot)/2;
320 int c = strncmpic(text, mid->name, mid->len);
321
322 if (c == 0)
323 {
137ae145
JH
324 uschar * s = text + mid->len;
325 if (Uskip_whitespace(&s) == ':')
059ec3d9
PH
326 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
327 c = 1;
328 }
329
330 if (c > 0) bot = mid + 1; else top = mid;
331 }
332
333return htype_other;
334}
335
336
337/*************************************************
338* Scan a header for certain strings *
339*************************************************/
340
341/* This function is used for the "personal" test. It scans a particular header
342line for any one of a number of strings, matched caselessly either as plain
343strings, or as regular expressions. If the header line contains a list of
344addresses, each match is applied only to the operative part of each address in
345the header, and non-regular expressions must be exact matches.
346
347The patterns can be provided either as a chain of string_item structures, or
348inline in the argument list, or both. If there is more than one header of the
349same name, they are all searched.
350
351Arguments:
352 name header name, including the trailing colon
353 has_addresses TRUE if the header contains a list of addresses
354 cond value to return if the header contains any of the strings
355 strings points to a chain of string_item blocks
356 count number of inline strings
357 ... the inline strings
358
359Returns: cond if the header exists and contains one of the strings;
360 otherwise !cond
361*/
362
363
364/* First we have a local subroutine to handle a single pattern */
365
366static BOOL
367one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
368{
369BOOL yield = FALSE;
059ec3d9
PH
370const pcre *re = NULL;
371
372/* If the pattern is a regex, compile it. Bomb out if compiling fails; these
373patterns are all constructed internally and should be valid. */
374
375if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
376
377/* Scan for the required header(s) and scan each one */
378
d7978c0f 379for (header_line * h = header_list; !yield && h; h = h->next)
059ec3d9
PH
380 {
381 if (h->type == htype_old || slen > h->slen ||
382 strncmpic(name, h->text, slen) != 0)
383 continue;
384
385 /* If the header is a list of addresses, extract each one in turn, and scan
386 it. A non-regex scan must be an exact match for the address. */
387
388 if (has_addresses)
389 {
390 uschar *s = h->text + slen;
391
d7978c0f 392 while (!yield && *s)
059ec3d9
PH
393 {
394 uschar *error, *next;
395 uschar *e = parse_find_address_end(s, FALSE);
396 int terminator = *e;
397 int start, end, domain;
398
399 /* Temporarily terminate the string at the address end while extracting
400 the operative address within. */
401
402 *e = 0;
403 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
404 *e = terminator;
405
406 /* Move on, ready for the next address */
407
408 s = e;
409 if (*s == ',') s++;
410
411 /* If there is some kind of syntax error, just give up on this header
412 line. */
413
5fcc791a 414 if (!next) break;
059ec3d9
PH
415
416 /* Otherwise, test for the pattern; a non-regex must be an exact match */
417
5fcc791a
JH
418 yield = !re
419 ? (strcmpic(next, pattern) == 0)
420 : (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
059ec3d9
PH
421 >= 0);
422 }
423 }
424
425 /* For headers that are not lists of addresses, scan the entire header line,
426 and just require "contains" for non-regex patterns. */
427
428 else
429 {
430 yield = (re == NULL)?
431 (strstric(h->text, pattern, FALSE) != NULL)
432 :
433 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
434 }
435 }
436
437return yield;
438}
439
440
441/* The externally visible interface */
442
443BOOL
444header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
445 int count, ...)
446{
447va_list ap;
059ec3d9
PH
448int slen = Ustrlen(name);
449
d7978c0f
JH
450for (string_item * s = strings; s; s = s->next)
451 if (one_pattern_match(name, slen, has_addresses, s->text))
452 return cond;
059ec3d9
PH
453
454va_start(ap, count);
d7978c0f 455for (int i = 0; i < count; i++)
059ec3d9 456 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
cb570b5e
JH
457 {
458 va_end(ap);
059ec3d9 459 return cond;
cb570b5e 460 }
059ec3d9
PH
461va_end(ap);
462
463return !cond;
464}
465
466/* End of header.c */