Expansions: new ${authresults {mch}} for an Authentication-Results header
[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
89Returns: nothing
90*/
91
92static void
93header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
1ba28e2b 94 const char *format, va_list ap)
059ec3d9
PH
95{
96header_line *h, *new;
97header_line **hptr;
98
99uschar *p, *q;
100uschar buffer[HEADER_ADD_BUFFER_SIZE];
101
102if (header_last == NULL) return;
103
104if (!string_vformat(buffer, sizeof(buffer), format, ap))
105 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
106 "%.100s ...", buffer);
107
108/* Find where to insert this header */
109
110if (name == NULL)
111 {
112 if (after)
113 {
114 hptr = &(header_last->next);
115 h = NULL;
116 }
117 else
118 {
119 hptr = &header_list;
5eb690a1
NM
120
121 /* header_list->text can be NULL if we get here between when the new
4c04137d 122 received header is allocated and when it is actually filled in. We want
5eb690a1
NM
123 that header to be first, so skip it for now. */
124
125 if (header_list->text == NULL)
126 hptr = &header_list->next;
127 h = *hptr;
059ec3d9
PH
128 }
129 }
130
131else
132 {
133 int len = Ustrlen(name);
134
4c04137d 135 /* Find the first non-deleted header with the correct name. */
059ec3d9
PH
136
137 for (hptr = &header_list; (h = *hptr) != NULL; hptr = &(h->next))
138 {
139 if (header_testname(h, name, len, TRUE)) break;
140 }
141
142 /* Handle the case where no header is found. To insert at the bottom, nothing
143 needs to be done. */
144
145 if (h == NULL)
146 {
147 if (topnot)
148 {
149 hptr = &header_list;
150 h = header_list;
151 }
152 }
153
154 /* Handle the case where a header is found. Check for more if "after" is
155 true. In this case, we want to include deleted headers in the block. */
156
157 else if (after)
158 {
159 for (;;)
160 {
161 if (h->next == NULL || !header_testname(h, name, len, FALSE)) break;
162 hptr = &(h->next);
163 h = h->next;
164 }
165 }
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
172for (p = q = buffer; *p != 0; )
173 {
174 for (;;)
175 {
176 q = Ustrchr(q, '\n');
177 if (q == NULL) q = p + Ustrlen(p);
178 if (*(++q) != ' ' && *q != '\t') break;
179 }
180
181 new = store_get(sizeof(header_line));
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;
188 hptr = &(new->next);
189
190 if (h == NULL) header_last = new;
191 p = q;
192 }
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
210Returns: nothing
211*/
212
213void
214header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
1ba28e2b 215 const char *format, ...)
059ec3d9
PH
216{
217va_list ap;
218va_start(ap, format);
219header_add_backend(after, name, topnot, type, format, ap);
220va_end(ap);
221}
222
223
224
225/*************************************************
226* Add new header on end of chain *
227*************************************************/
228
229/* This is now a convenience interface to header_add_backend().
230
231Arguments:
232 type Exim header type character
233 format sprintf format
234 ... arguments for the format
235
236Returns: nothing
237*/
238
239void
1ba28e2b 240header_add(int type, const char *format, ...)
059ec3d9
PH
241{
242va_list ap;
243va_start(ap, format);
244header_add_backend(TRUE, NULL, FALSE, type, format, ap);
245va_end(ap);
246}
247
248
249
250/*************************************************
251* Remove (mark as old) a header *
252*************************************************/
253
254/* This function is used by the filter code; it is also exported in the
255local_scan() API. If no header is found, the function does nothing.
256
257Arguments:
258 occ the occurrence number for multiply-defined headers
259 <= 0 means "all"; deleted headers are not counted
260 name the header name
261
262Returns: nothing
263*/
264
265void
1ba28e2b 266header_remove(int occ, const uschar *name)
059ec3d9
PH
267{
268header_line *h;
269int hcount = 0;
270int len = Ustrlen(name);
271for (h = header_list; h != NULL; h = h->next)
272 {
273 if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
274 {
275 h->type = htype_old;
276 if (occ > 0) return;
277 }
278 }
279}
280
281
282
283/*************************************************
284* Check the name of a header *
285*************************************************/
286
287/* This function scans a table of header field names that Exim recognizes, and
288returns the identification of a match. If "resent" is true, the header is known
289to start with "resent-". In that case, the function matches only those fields
290that are allowed to appear with resent- in front of them.
291
292Arguments:
293 h points to the header line
294 is_resent TRUE if the name starts "Resent-"
295
296Returns: One of the htype_ enum values, identifying the header
297*/
298
299int
300header_checkname(header_line *h, BOOL is_resent)
301{
302uschar *text = h->text;
303header_name *bot = header_names;
304header_name *top = header_names + header_names_size;
305
306if (is_resent) text += 7;
307
308while (bot < top)
309 {
310 header_name *mid = bot + (top - bot)/2;
311 int c = strncmpic(text, mid->name, mid->len);
312
313 if (c == 0)
314 {
315 uschar *s = text + mid->len;
316 while (isspace(*s)) s++;
317 if (*s == ':')
318 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
319 c = 1;
320 }
321
322 if (c > 0) bot = mid + 1; else top = mid;
323 }
324
325return htype_other;
326}
327
328
329/*************************************************
330* Scan a header for certain strings *
331*************************************************/
332
333/* This function is used for the "personal" test. It scans a particular header
334line for any one of a number of strings, matched caselessly either as plain
335strings, or as regular expressions. If the header line contains a list of
336addresses, each match is applied only to the operative part of each address in
337the header, and non-regular expressions must be exact matches.
338
339The patterns can be provided either as a chain of string_item structures, or
340inline in the argument list, or both. If there is more than one header of the
341same name, they are all searched.
342
343Arguments:
344 name header name, including the trailing colon
345 has_addresses TRUE if the header contains a list of addresses
346 cond value to return if the header contains any of the strings
347 strings points to a chain of string_item blocks
348 count number of inline strings
349 ... the inline strings
350
351Returns: cond if the header exists and contains one of the strings;
352 otherwise !cond
353*/
354
355
356/* First we have a local subroutine to handle a single pattern */
357
358static BOOL
359one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
360{
361BOOL yield = FALSE;
362header_line *h;
363const pcre *re = NULL;
364
365/* If the pattern is a regex, compile it. Bomb out if compiling fails; these
366patterns are all constructed internally and should be valid. */
367
368if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
369
370/* Scan for the required header(s) and scan each one */
371
372for (h = header_list; !yield && h != NULL; h = h->next)
373 {
374 if (h->type == htype_old || slen > h->slen ||
375 strncmpic(name, h->text, slen) != 0)
376 continue;
377
378 /* If the header is a list of addresses, extract each one in turn, and scan
379 it. A non-regex scan must be an exact match for the address. */
380
381 if (has_addresses)
382 {
383 uschar *s = h->text + slen;
384
385 while (!yield && *s != 0)
386 {
387 uschar *error, *next;
388 uschar *e = parse_find_address_end(s, FALSE);
389 int terminator = *e;
390 int start, end, domain;
391
392 /* Temporarily terminate the string at the address end while extracting
393 the operative address within. */
394
395 *e = 0;
396 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
397 *e = terminator;
398
399 /* Move on, ready for the next address */
400
401 s = e;
402 if (*s == ',') s++;
403
404 /* If there is some kind of syntax error, just give up on this header
405 line. */
406
407 if (next == NULL) break;
408
409 /* Otherwise, test for the pattern; a non-regex must be an exact match */
410
411 yield = (re == NULL)?
412 (strcmpic(next, pattern) == 0)
413 :
414 (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
415 >= 0);
416 }
417 }
418
419 /* For headers that are not lists of addresses, scan the entire header line,
420 and just require "contains" for non-regex patterns. */
421
422 else
423 {
424 yield = (re == NULL)?
425 (strstric(h->text, pattern, FALSE) != NULL)
426 :
427 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
428 }
429 }
430
431return yield;
432}
433
434
435/* The externally visible interface */
436
437BOOL
438header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
439 int count, ...)
440{
441va_list ap;
442string_item *s;
443int i;
444int slen = Ustrlen(name);
445
446for (s = strings; s != NULL; s = s->next)
447 {
448 if (one_pattern_match(name, slen, has_addresses, s->text)) return cond;
449 }
450
451va_start(ap, count);
452for (i = 0; i < count; i++)
059ec3d9 453 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
cb570b5e
JH
454 {
455 va_end(ap);
059ec3d9 456 return cond;
cb570b5e 457 }
059ec3d9
PH
458va_end(ap);
459
460return !cond;
461}
462
463/* End of header.c */