1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2016 */
6 /* See the file NOTICE for conditions of use and distribution. */
12 /*************************************************
13 * Test a header for matching name *
14 *************************************************/
16 /* This function tests the name of a header. It is made into a function because
17 it isn't just a string comparison: spaces and tabs are permitted between the
18 name and the colon. The h->text field should nowadays never be NULL, but check
22 h points to the header
24 len the length of the name
25 notdel if TRUE, force FALSE for deleted headers
27 Returns: TRUE or FALSE
31 header_testname(header_line *h, const uschar *name, int len, BOOL notdel)
34 if (h->type == '*' && notdel) return FALSE;
35 if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
37 while (*tt == ' ' || *tt == '\t') tt++;
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. */
47 header_testname_incomplete(header_line *h, const uschar *name,
50 if (h->type == '*' && notdel) return FALSE;
51 if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
56 /*************************************************
57 * Add new header backend function *
58 *************************************************/
60 /* The header_last variable points to the last header during message reception
61 and delivery; otherwise it is NULL. We add new headers only when header_last is
62 not NULL. The function may get called sometimes when it is NULL (e.g. during
63 address verification where rewriting options exist). When called from a filter,
64 there may be multiple header lines in a single string.
66 This is an internal static function that is the common back end to the external
67 functions defined below. The general interface allows the header to be inserted
68 before or after a given occurrence of a given header.
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"
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
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)
87 ap va_list value for format arguments
93 header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
94 const char *format, va_list ap)
100 uschar buffer[HEADER_ADD_BUFFER_SIZE];
101 gstring gs = { .size = HEADER_ADD_BUFFER_SIZE, .ptr = 0, .s = buffer };
103 if (!header_last) return;
105 if (!string_vformat(&gs, FALSE, format, ap))
106 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
107 "%.100s ...", string_from_gstring(&gs));
108 string_from_gstring(&gs);
110 /* Find where to insert this header */
116 hptr = &(header_last->next);
123 /* header_list->text can be NULL if we get here between when the new
124 received header is allocated and when it is actually filled in. We want
125 that header to be first, so skip it for now. */
127 if (!header_list->text)
128 hptr = &header_list->next;
135 int len = Ustrlen(name);
137 /* Find the first non-deleted header with the correct name. */
139 for (hptr = &header_list; (h = *hptr); hptr = &h->next)
140 if (header_testname(h, name, len, TRUE))
143 /* Handle the case where no header is found. To insert at the bottom, nothing
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. */
161 if (!h->next || !header_testname(h, name, len, FALSE)) break;
167 /* Loop for multiple header lines, taking care about continuations. At this
168 point, we have hptr pointing to the link field that will point to the new
169 header, and h containing the following header, or NULL. */
171 for (p = q = buffer; *p != 0; )
175 q = Ustrchr(q, '\n');
176 if (!q) q = p + Ustrlen(p);
177 if (*(++q) != ' ' && *q != '\t') break;
180 new = store_get(sizeof(header_line));
181 new->text = string_copyn(p, q - p);
189 if (!h) header_last = new;
195 /*************************************************
196 * Add new header anywhere in the chain *
197 *************************************************/
199 /* This is an external interface to header_add_backend().
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
213 header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
214 const char *format, ...)
217 va_start(ap, format);
218 header_add_backend(after, name, topnot, type, format, ap);
224 /*************************************************
225 * Add new header on end of chain *
226 *************************************************/
228 /* This is now a convenience interface to header_add_backend().
231 type Exim header type character
232 format sprintf format
233 ... arguments for the format
239 header_add(int type, const char *format, ...)
242 va_start(ap, format);
243 header_add_backend(TRUE, NULL, FALSE, type, format, ap);
249 /*************************************************
250 * Remove (mark as old) a header *
251 *************************************************/
253 /* This function is used by the filter code; it is also exported in the
254 local_scan() API. If no header is found, the function does nothing.
257 occ the occurrence number for multiply-defined headers
258 <= 0 means "all"; deleted headers are not counted
265 header_remove(int occ, const uschar *name)
269 int len = Ustrlen(name);
270 for (h = header_list; h != NULL; h = h->next)
272 if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
282 /*************************************************
283 * Check the name of a header *
284 *************************************************/
286 /* This function scans a table of header field names that Exim recognizes, and
287 returns the identification of a match. If "resent" is true, the header is known
288 to start with "resent-". In that case, the function matches only those fields
289 that are allowed to appear with resent- in front of them.
292 h points to the header line
293 is_resent TRUE if the name starts "Resent-"
295 Returns: One of the htype_ enum values, identifying the header
299 header_checkname(header_line *h, BOOL is_resent)
301 uschar *text = h->text;
302 header_name *bot = header_names;
303 header_name *top = header_names + header_names_size;
305 if (is_resent) text += 7;
309 header_name *mid = bot + (top - bot)/2;
310 int c = strncmpic(text, mid->name, mid->len);
314 uschar *s = text + mid->len;
315 while (isspace(*s)) s++;
317 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
321 if (c > 0) bot = mid + 1; else top = mid;
328 /*************************************************
329 * Scan a header for certain strings *
330 *************************************************/
332 /* This function is used for the "personal" test. It scans a particular header
333 line for any one of a number of strings, matched caselessly either as plain
334 strings, or as regular expressions. If the header line contains a list of
335 addresses, each match is applied only to the operative part of each address in
336 the header, and non-regular expressions must be exact matches.
338 The patterns can be provided either as a chain of string_item structures, or
339 inline in the argument list, or both. If there is more than one header of the
340 same name, they are all searched.
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
350 Returns: cond if the header exists and contains one of the strings;
355 /* First we have a local subroutine to handle a single pattern */
358 one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
362 const pcre *re = NULL;
364 /* If the pattern is a regex, compile it. Bomb out if compiling fails; these
365 patterns are all constructed internally and should be valid. */
367 if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
369 /* Scan for the required header(s) and scan each one */
371 for (h = header_list; !yield && h != NULL; h = h->next)
373 if (h->type == htype_old || slen > h->slen ||
374 strncmpic(name, h->text, slen) != 0)
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. */
382 uschar *s = h->text + slen;
384 while (!yield && *s != 0)
386 uschar *error, *next;
387 uschar *e = parse_find_address_end(s, FALSE);
389 int start, end, domain;
391 /* Temporarily terminate the string at the address end while extracting
392 the operative address within. */
395 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
398 /* Move on, ready for the next address */
403 /* If there is some kind of syntax error, just give up on this header
406 if (next == NULL) break;
408 /* Otherwise, test for the pattern; a non-regex must be an exact match */
410 yield = (re == NULL)?
411 (strcmpic(next, pattern) == 0)
413 (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
418 /* For headers that are not lists of addresses, scan the entire header line,
419 and just require "contains" for non-regex patterns. */
423 yield = (re == NULL)?
424 (strstric(h->text, pattern, FALSE) != NULL)
426 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
434 /* The externally visible interface */
437 header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
443 int slen = Ustrlen(name);
445 for (s = strings; s != NULL; s = s->next)
447 if (one_pattern_match(name, slen, has_addresses, s->text)) return cond;
451 for (i = 0; i < count; i++)
452 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
462 /* End of header.c */