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