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