Fix missing return value from exim_chown_failure
[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
PH
95{
96header_line *h, *new;
97header_line **hptr;
98
99uschar *p, *q;
100uschar buffer[HEADER_ADD_BUFFER_SIZE];
d12746bc 101gstring gs = { .size = HEADER_ADD_BUFFER_SIZE, .ptr = 0, .s = buffer };
059ec3d9 102
049782c0 103if (!header_last) return NULL;
059ec3d9 104
d12746bc 105if (!string_vformat(&gs, FALSE, format, ap))
059ec3d9 106 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
d12746bc
JH
107 "%.100s ...", string_from_gstring(&gs));
108string_from_gstring(&gs);
059ec3d9
PH
109
110/* Find where to insert this header */
111
94759fce 112if (!name)
059ec3d9
PH
113 {
114 if (after)
115 {
116 hptr = &(header_last->next);
117 h = NULL;
118 }
119 else
120 {
121 hptr = &header_list;
5eb690a1
NM
122
123 /* header_list->text can be NULL if we get here between when the new
4c04137d 124 received header is allocated and when it is actually filled in. We want
5eb690a1
NM
125 that header to be first, so skip it for now. */
126
94759fce 127 if (!header_list->text)
5eb690a1
NM
128 hptr = &header_list->next;
129 h = *hptr;
059ec3d9
PH
130 }
131 }
132
133else
134 {
135 int len = Ustrlen(name);
136
4c04137d 137 /* Find the first non-deleted header with the correct name. */
059ec3d9 138
94759fce
JH
139 for (hptr = &header_list; (h = *hptr); hptr = &h->next)
140 if (header_testname(h, name, len, TRUE))
141 break;
059ec3d9
PH
142
143 /* Handle the case where no header is found. To insert at the bottom, nothing
144 needs to be done. */
145
94759fce 146 if (!h)
059ec3d9
PH
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)
059ec3d9
PH
159 for (;;)
160 {
94759fce 161 if (!h->next || !header_testname(h, name, len, FALSE)) break;
059ec3d9
PH
162 hptr = &(h->next);
163 h = h->next;
164 }
059ec3d9
PH
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');
94759fce 176 if (!q) q = p + Ustrlen(p);
059ec3d9
PH
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
94759fce 189 if (!h) header_last = new;
059ec3d9
PH
190 p = q;
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{
231header_line * h;
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 {
325 uschar *s = text + mid->len;
326 while (isspace(*s)) s++;
327 if (*s == ':')
328 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
329 c = 1;
330 }
331
332 if (c > 0) bot = mid + 1; else top = mid;
333 }
334
335return htype_other;
336}
337
338
339/*************************************************
340* Scan a header for certain strings *
341*************************************************/
342
343/* This function is used for the "personal" test. It scans a particular header
344line for any one of a number of strings, matched caselessly either as plain
345strings, or as regular expressions. If the header line contains a list of
346addresses, each match is applied only to the operative part of each address in
347the header, and non-regular expressions must be exact matches.
348
349The patterns can be provided either as a chain of string_item structures, or
350inline in the argument list, or both. If there is more than one header of the
351same name, they are all searched.
352
353Arguments:
354 name header name, including the trailing colon
355 has_addresses TRUE if the header contains a list of addresses
356 cond value to return if the header contains any of the strings
357 strings points to a chain of string_item blocks
358 count number of inline strings
359 ... the inline strings
360
361Returns: cond if the header exists and contains one of the strings;
362 otherwise !cond
363*/
364
365
366/* First we have a local subroutine to handle a single pattern */
367
368static BOOL
369one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
370{
371BOOL yield = FALSE;
059ec3d9
PH
372const pcre *re = NULL;
373
374/* If the pattern is a regex, compile it. Bomb out if compiling fails; these
375patterns are all constructed internally and should be valid. */
376
377if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
378
379/* Scan for the required header(s) and scan each one */
380
d7978c0f 381for (header_line * h = header_list; !yield && h; h = h->next)
059ec3d9
PH
382 {
383 if (h->type == htype_old || slen > h->slen ||
384 strncmpic(name, h->text, slen) != 0)
385 continue;
386
387 /* If the header is a list of addresses, extract each one in turn, and scan
388 it. A non-regex scan must be an exact match for the address. */
389
390 if (has_addresses)
391 {
392 uschar *s = h->text + slen;
393
d7978c0f 394 while (!yield && *s)
059ec3d9
PH
395 {
396 uschar *error, *next;
397 uschar *e = parse_find_address_end(s, FALSE);
398 int terminator = *e;
399 int start, end, domain;
400
401 /* Temporarily terminate the string at the address end while extracting
402 the operative address within. */
403
404 *e = 0;
405 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
406 *e = terminator;
407
408 /* Move on, ready for the next address */
409
410 s = e;
411 if (*s == ',') s++;
412
413 /* If there is some kind of syntax error, just give up on this header
414 line. */
415
416 if (next == NULL) break;
417
418 /* Otherwise, test for the pattern; a non-regex must be an exact match */
419
420 yield = (re == NULL)?
421 (strcmpic(next, pattern) == 0)
422 :
423 (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
424 >= 0);
425 }
426 }
427
428 /* For headers that are not lists of addresses, scan the entire header line,
429 and just require "contains" for non-regex patterns. */
430
431 else
432 {
433 yield = (re == NULL)?
434 (strstric(h->text, pattern, FALSE) != NULL)
435 :
436 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
437 }
438 }
439
440return yield;
441}
442
443
444/* The externally visible interface */
445
446BOOL
447header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
448 int count, ...)
449{
450va_list ap;
059ec3d9
PH
451int slen = Ustrlen(name);
452
d7978c0f
JH
453for (string_item * s = strings; s; s = s->next)
454 if (one_pattern_match(name, slen, has_addresses, s->text))
455 return cond;
059ec3d9
PH
456
457va_start(ap, count);
d7978c0f 458for (int i = 0; i < count; i++)
059ec3d9 459 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
cb570b5e
JH
460 {
461 va_end(ap);
059ec3d9 462 return cond;
cb570b5e 463 }
059ec3d9
PH
464va_end(ap);
465
466return !cond;
467}
468
469/* End of header.c */