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