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