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