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