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