Build without WITH_CONTENT_SCAN.
[exim.git] / src / src / header.c
CommitLineData
0a49a7a4 1/* $Cambridge: exim/src/src/header.c,v 1.8 2009/11/16 19:50:37 nm4 Exp $ */
059ec3d9
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
0a49a7a4 7/* Copyright (c) University of Cambridge 1995 - 2009 */
059ec3d9
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10
11#include "exim.h"
12
13
14/*************************************************
15* Test a header for matching name *
16*************************************************/
17
18/* This function tests the name of a header. It is made into a function because
19it isn't just a string comparison: spaces and tabs are permitted between the
20name and the colon. The h->text field should nowadays never be NULL, but check
21it just in case.
22
23Arguments:
24 h points to the header
25 name the name to test
26 len the length of the name
27 notdel if TRUE, force FALSE for deleted headers
28
29Returns: TRUE or FALSE
30*/
31
32BOOL
33header_testname(header_line *h, uschar *name, int len, BOOL notdel)
34{
35uschar *tt;
36if (h->type == '*' && notdel) return FALSE;
37if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
38tt = h->text + len;
39while (*tt == ' ' || *tt == '\t') tt++;
40return *tt == ':';
41}
42
8523533c
TK
43/* This is a copy of the function above, only that it is possible to pass
44 only the beginning of a header name. It simply does a front-anchored
45 substring match. Arguments and Return codes are the same as for
46 header_testname() above. */
47
48BOOL
49header_testname_incomplete(header_line *h, uschar *name, int len, BOOL notdel)
50{
8523533c
TK
51if (h->type == '*' && notdel) return FALSE;
52if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
53return TRUE;
54}
059ec3d9
PH
55
56
57/*************************************************
58* Add new header backend function *
59*************************************************/
60
61/* The header_last variable points to the last header during message reception
62and delivery; otherwise it is NULL. We add new headers only when header_last is
63not NULL. The function may get called sometimes when it is NULL (e.g. during
64address verification where rewriting options exist). When called from a filter,
65there may be multiple header lines in a single string.
66
67This is an internal static function that is the common back end to the external
68functions defined below. The general interface allows the header to be inserted
69before or after a given occurrence of a given header.
70
71(a) if "name" is NULL, the header is added at the end of all the existing
72 headers if "after" is true, or at the start if it is false. The "topnot"
73 flag is not used.
74
75(b) If "name" is not NULL, the first existing header with that name is sought.
76 If "after" is false, the new header is added before it. If "after" is true,
77 a check is made for adjacent headers with the same name, and the new header
78 is added after the last of them. If a header of the given name is not
79 found, the new header is added first if "topnot" is true, and at the bottom
80 otherwise.
81
82Arguments:
83 after TRUE for "after", FALSE for "before"
84 name name if adding at a specific header, else NULL
85 topnot TRUE to add at top if no header found
86 type Exim header type character (htype_something)
87 format sprintf format
88 ap va_list value for format arguments
89
90Returns: nothing
91*/
92
93static void
94header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
95 char *format, va_list ap)
96{
97header_line *h, *new;
98header_line **hptr;
99
100uschar *p, *q;
101uschar buffer[HEADER_ADD_BUFFER_SIZE];
102
103if (header_last == NULL) return;
104
105if (!string_vformat(buffer, sizeof(buffer), format, ap))
106 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: "
107 "%.100s ...", buffer);
108
109/* Find where to insert this header */
110
111if (name == NULL)
112 {
113 if (after)
114 {
115 hptr = &(header_last->next);
116 h = NULL;
117 }
118 else
119 {
120 hptr = &header_list;
5eb690a1
NM
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 acutally filled in. We want
124 that header to be first, so skip it for now. */
125
126 if (header_list->text == NULL)
127 hptr = &header_list->next;
128 h = *hptr;
059ec3d9
PH
129 }
130 }
131
132else
133 {
134 int len = Ustrlen(name);
135
136 /* Find the first non-deleted header witht the correct name. */
137
138 for (hptr = &header_list; (h = *hptr) != NULL; hptr = &(h->next))
139 {
140 if (header_testname(h, name, len, TRUE)) break;
141 }
142
143 /* Handle the case where no header is found. To insert at the bottom, nothing
144 needs to be done. */
145
146 if (h == NULL)
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)
159 {
160 for (;;)
161 {
162 if (h->next == NULL || !header_testname(h, name, len, FALSE)) break;
163 hptr = &(h->next);
164 h = h->next;
165 }
166 }
167 }
168
169/* Loop for multiple header lines, taking care about continuations. At this
170point, we have hptr pointing to the link field that will point to the new
171header, and h containing the following header, or NULL. */
172
173for (p = q = buffer; *p != 0; )
174 {
175 for (;;)
176 {
177 q = Ustrchr(q, '\n');
178 if (q == NULL) q = p + Ustrlen(p);
179 if (*(++q) != ' ' && *q != '\t') break;
180 }
181
182 new = store_get(sizeof(header_line));
183 new->text = string_copyn(p, q - p);
184 new->slen = q - p;
185 new->type = type;
186 new->next = h;
187
188 *hptr = new;
189 hptr = &(new->next);
190
191 if (h == NULL) header_last = new;
192 p = q;
193 }
194}
195
196
197/*************************************************
198* Add new header anywhere in the chain *
199*************************************************/
200
201/* This is an external interface to header_add_backend().
202
203Arguments:
204 after TRUE for "after", FALSE for "before"
205 name name if adding at a specific header, else NULL
206 topnot TRUE to add at top if no header found
207 type Exim header type character (htype_something)
208 format sprintf format
209 ... format arguments
210
211Returns: nothing
212*/
213
214void
215header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
216 char *format, ...)
217{
218va_list ap;
219va_start(ap, format);
220header_add_backend(after, name, topnot, type, format, ap);
221va_end(ap);
222}
223
224
225
226/*************************************************
227* Add new header on end of chain *
228*************************************************/
229
230/* This is now a convenience interface to header_add_backend().
231
232Arguments:
233 type Exim header type character
234 format sprintf format
235 ... arguments for the format
236
237Returns: nothing
238*/
239
240void
241header_add(int type, char *format, ...)
242{
243va_list ap;
244va_start(ap, format);
245header_add_backend(TRUE, NULL, FALSE, type, format, ap);
246va_end(ap);
247}
248
249
250
251/*************************************************
252* Remove (mark as old) a header *
253*************************************************/
254
255/* This function is used by the filter code; it is also exported in the
256local_scan() API. If no header is found, the function does nothing.
257
258Arguments:
259 occ the occurrence number for multiply-defined headers
260 <= 0 means "all"; deleted headers are not counted
261 name the header name
262
263Returns: nothing
264*/
265
266void
267header_remove(int occ, uschar *name)
268{
269header_line *h;
270int hcount = 0;
271int len = Ustrlen(name);
272for (h = header_list; h != NULL; h = h->next)
273 {
274 if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
275 {
276 h->type = htype_old;
277 if (occ > 0) return;
278 }
279 }
280}
281
282
283
284/*************************************************
285* Check the name of a header *
286*************************************************/
287
288/* This function scans a table of header field names that Exim recognizes, and
289returns the identification of a match. If "resent" is true, the header is known
290to start with "resent-". In that case, the function matches only those fields
291that are allowed to appear with resent- in front of them.
292
293Arguments:
294 h points to the header line
295 is_resent TRUE if the name starts "Resent-"
296
297Returns: One of the htype_ enum values, identifying the header
298*/
299
300int
301header_checkname(header_line *h, BOOL is_resent)
302{
303uschar *text = h->text;
304header_name *bot = header_names;
305header_name *top = header_names + header_names_size;
306
307if (is_resent) text += 7;
308
309while (bot < top)
310 {
311 header_name *mid = bot + (top - bot)/2;
312 int c = strncmpic(text, mid->name, mid->len);
313
314 if (c == 0)
315 {
316 uschar *s = text + mid->len;
317 while (isspace(*s)) s++;
318 if (*s == ':')
319 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
320 c = 1;
321 }
322
323 if (c > 0) bot = mid + 1; else top = mid;
324 }
325
326return htype_other;
327}
328
329
330/*************************************************
331* Scan a header for certain strings *
332*************************************************/
333
334/* This function is used for the "personal" test. It scans a particular header
335line for any one of a number of strings, matched caselessly either as plain
336strings, or as regular expressions. If the header line contains a list of
337addresses, each match is applied only to the operative part of each address in
338the header, and non-regular expressions must be exact matches.
339
340The patterns can be provided either as a chain of string_item structures, or
341inline in the argument list, or both. If there is more than one header of the
342same name, they are all searched.
343
344Arguments:
345 name header name, including the trailing colon
346 has_addresses TRUE if the header contains a list of addresses
347 cond value to return if the header contains any of the strings
348 strings points to a chain of string_item blocks
349 count number of inline strings
350 ... the inline strings
351
352Returns: cond if the header exists and contains one of the strings;
353 otherwise !cond
354*/
355
356
357/* First we have a local subroutine to handle a single pattern */
358
359static BOOL
360one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
361{
362BOOL yield = FALSE;
363header_line *h;
364const pcre *re = NULL;
365
366/* If the pattern is a regex, compile it. Bomb out if compiling fails; these
367patterns are all constructed internally and should be valid. */
368
369if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
370
371/* Scan for the required header(s) and scan each one */
372
373for (h = header_list; !yield && h != NULL; h = h->next)
374 {
375 if (h->type == htype_old || slen > h->slen ||
376 strncmpic(name, h->text, slen) != 0)
377 continue;
378
379 /* If the header is a list of addresses, extract each one in turn, and scan
380 it. A non-regex scan must be an exact match for the address. */
381
382 if (has_addresses)
383 {
384 uschar *s = h->text + slen;
385
386 while (!yield && *s != 0)
387 {
388 uschar *error, *next;
389 uschar *e = parse_find_address_end(s, FALSE);
390 int terminator = *e;
391 int start, end, domain;
392
393 /* Temporarily terminate the string at the address end while extracting
394 the operative address within. */
395
396 *e = 0;
397 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
398 *e = terminator;
399
400 /* Move on, ready for the next address */
401
402 s = e;
403 if (*s == ',') s++;
404
405 /* If there is some kind of syntax error, just give up on this header
406 line. */
407
408 if (next == NULL) break;
409
410 /* Otherwise, test for the pattern; a non-regex must be an exact match */
411
412 yield = (re == NULL)?
413 (strcmpic(next, pattern) == 0)
414 :
415 (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
416 >= 0);
417 }
418 }
419
420 /* For headers that are not lists of addresses, scan the entire header line,
421 and just require "contains" for non-regex patterns. */
422
423 else
424 {
425 yield = (re == NULL)?
426 (strstric(h->text, pattern, FALSE) != NULL)
427 :
428 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
429 }
430 }
431
432return yield;
433}
434
435
436/* The externally visible interface */
437
438BOOL
439header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
440 int count, ...)
441{
442va_list ap;
443string_item *s;
444int i;
445int slen = Ustrlen(name);
446
447for (s = strings; s != NULL; s = s->next)
448 {
449 if (one_pattern_match(name, slen, has_addresses, s->text)) return cond;
450 }
451
452va_start(ap, count);
453for (i = 0; i < count; i++)
454 {
455 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
456 return cond;
457 }
458va_end(ap);
459
460return !cond;
461}
462
463/* End of header.c */