1 /* $Cambridge: exim/src/src/header.c,v 1.2 2004/12/16 15:11:47 tom Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
14 /*************************************************
15 * Test a header for matching name *
16 *************************************************/
18 /* This function tests the name of a header. It is made into a function because
19 it isn't just a string comparison: spaces and tabs are permitted between the
20 name and the colon. The h->text field should nowadays never be NULL, but check
24 h points to the header
26 len the length of the name
27 notdel if TRUE, force FALSE for deleted headers
29 Returns: TRUE or FALSE
33 header_testname(header_line *h, uschar *name, int len, BOOL notdel)
36 if (h->type == '*' && notdel) return FALSE;
37 if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
39 while (*tt == ' ' || *tt == '\t') tt++;
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. */
49 header_testname_incomplete(header_line *h, uschar *name, int len, BOOL notdel)
52 if (h->type == '*' && notdel) return FALSE;
53 if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE;
58 /*************************************************
59 * Add new header backend function *
60 *************************************************/
62 /* The header_last variable points to the last header during message reception
63 and delivery; otherwise it is NULL. We add new headers only when header_last is
64 not NULL. The function may get called sometimes when it is NULL (e.g. during
65 address verification where rewriting options exist). When called from a filter,
66 there may be multiple header lines in a single string.
68 This is an internal static function that is the common back end to the external
69 functions defined below. The general interface allows the header to be inserted
70 before or after a given occurrence of a given header.
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"
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
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)
89 ap va_list value for format arguments
95 header_add_backend(BOOL after, uschar *name, BOOL topnot, int type,
96 char *format, va_list ap)
102 uschar buffer[HEADER_ADD_BUFFER_SIZE];
104 if (header_last == NULL) return;
106 if (!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);
110 /* Find where to insert this header */
116 hptr = &(header_last->next);
128 int len = Ustrlen(name);
130 /* Find the first non-deleted header witht the correct name. */
132 for (hptr = &header_list; (h = *hptr) != NULL; hptr = &(h->next))
134 if (header_testname(h, name, len, TRUE)) break;
137 /* Handle the case where no header is found. To insert at the bottom, nothing
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. */
156 if (h->next == NULL || !header_testname(h, name, len, FALSE)) break;
163 /* Loop for multiple header lines, taking care about continuations. At this
164 point, we have hptr pointing to the link field that will point to the new
165 header, and h containing the following header, or NULL. */
167 for (p = q = buffer; *p != 0; )
171 q = Ustrchr(q, '\n');
172 if (q == NULL) q = p + Ustrlen(p);
173 if (*(++q) != ' ' && *q != '\t') break;
176 new = store_get(sizeof(header_line));
177 new->text = string_copyn(p, q - p);
185 if (h == NULL) header_last = new;
191 /*************************************************
192 * Add new header anywhere in the chain *
193 *************************************************/
195 /* This is an external interface to header_add_backend().
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
209 header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type,
213 va_start(ap, format);
214 header_add_backend(after, name, topnot, type, format, ap);
220 /*************************************************
221 * Add new header on end of chain *
222 *************************************************/
224 /* This is now a convenience interface to header_add_backend().
227 type Exim header type character
228 format sprintf format
229 ... arguments for the format
235 header_add(int type, char *format, ...)
238 va_start(ap, format);
239 header_add_backend(TRUE, NULL, FALSE, type, format, ap);
245 /*************************************************
246 * Remove (mark as old) a header *
247 *************************************************/
249 /* This function is used by the filter code; it is also exported in the
250 local_scan() API. If no header is found, the function does nothing.
253 occ the occurrence number for multiply-defined headers
254 <= 0 means "all"; deleted headers are not counted
261 header_remove(int occ, uschar *name)
265 int len = Ustrlen(name);
266 for (h = header_list; h != NULL; h = h->next)
268 if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ))
278 /*************************************************
279 * Check the name of a header *
280 *************************************************/
282 /* This function scans a table of header field names that Exim recognizes, and
283 returns the identification of a match. If "resent" is true, the header is known
284 to start with "resent-". In that case, the function matches only those fields
285 that are allowed to appear with resent- in front of them.
288 h points to the header line
289 is_resent TRUE if the name starts "Resent-"
291 Returns: One of the htype_ enum values, identifying the header
295 header_checkname(header_line *h, BOOL is_resent)
297 uschar *text = h->text;
298 header_name *bot = header_names;
299 header_name *top = header_names + header_names_size;
301 if (is_resent) text += 7;
305 header_name *mid = bot + (top - bot)/2;
306 int c = strncmpic(text, mid->name, mid->len);
310 uschar *s = text + mid->len;
311 while (isspace(*s)) s++;
313 return (!is_resent || mid->allow_resent)? mid->htype : htype_other;
317 if (c > 0) bot = mid + 1; else top = mid;
324 /*************************************************
325 * Scan a header for certain strings *
326 *************************************************/
328 /* This function is used for the "personal" test. It scans a particular header
329 line for any one of a number of strings, matched caselessly either as plain
330 strings, or as regular expressions. If the header line contains a list of
331 addresses, each match is applied only to the operative part of each address in
332 the header, and non-regular expressions must be exact matches.
334 The patterns can be provided either as a chain of string_item structures, or
335 inline in the argument list, or both. If there is more than one header of the
336 same name, they are all searched.
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
346 Returns: cond if the header exists and contains one of the strings;
351 /* First we have a local subroutine to handle a single pattern */
354 one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern)
358 const pcre *re = NULL;
360 /* If the pattern is a regex, compile it. Bomb out if compiling fails; these
361 patterns are all constructed internally and should be valid. */
363 if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE);
365 /* Scan for the required header(s) and scan each one */
367 for (h = header_list; !yield && h != NULL; h = h->next)
369 if (h->type == htype_old || slen > h->slen ||
370 strncmpic(name, h->text, slen) != 0)
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. */
378 uschar *s = h->text + slen;
380 while (!yield && *s != 0)
382 uschar *error, *next;
383 uschar *e = parse_find_address_end(s, FALSE);
385 int start, end, domain;
387 /* Temporarily terminate the string at the address end while extracting
388 the operative address within. */
391 next = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
394 /* Move on, ready for the next address */
399 /* If there is some kind of syntax error, just give up on this header
402 if (next == NULL) break;
404 /* Otherwise, test for the pattern; a non-regex must be an exact match */
406 yield = (re == NULL)?
407 (strcmpic(next, pattern) == 0)
409 (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0)
414 /* For headers that are not lists of addresses, scan the entire header line,
415 and just require "contains" for non-regex patterns. */
419 yield = (re == NULL)?
420 (strstric(h->text, pattern, FALSE) != NULL)
422 (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0);
430 /* The externally visible interface */
433 header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings,
439 int slen = Ustrlen(name);
441 for (s = strings; s != NULL; s = s->next)
443 if (one_pattern_match(name, slen, has_addresses, s->text)) return cond;
447 for (i = 0; i < count; i++)
449 if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *)))
457 /* End of header.c */