Use C99 initialisations for iterators
[exim.git] / src / src / rewrite.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 /* Functions concerned with rewriting headers */
9
10
11 #include "exim.h"
12
13 /* Names for testing rewriting */
14
15 static const char *rrname[] = {
16   "  sender",
17   "    from",
18   "      to",
19   "      cc",
20   "     bcc",
21   "reply-to",
22   "env-from",
23   "  env-to"
24 };
25
26 /* Structure and table for finding source of address for debug printing */
27
28 typedef struct where_list_block {
29   int bit;
30   const uschar *string;
31 } where_list_block;
32
33 static where_list_block where_list[] = {
34   { rewrite_sender,  CUS"sender:" },
35   { rewrite_from,    CUS"from:" },
36   { rewrite_to,      CUS"to:" },
37   { rewrite_cc,      CUS"cc:" },
38   { rewrite_bcc,     CUS"bcc:" },
39   { rewrite_replyto, CUS"reply-to:" },
40   { rewrite_envfrom, CUS"env-from" },
41   { rewrite_envto,   CUS"env-to" },
42   { rewrite_smtp,    CUS"smtp recipient" },
43   { rewrite_smtp|rewrite_smtp_sender, CUS"smtp sender" }
44 };
45
46 static int where_list_size = sizeof(where_list)/sizeof(where_list_block);
47
48
49
50 /*************************************************
51 *            Ensure an address is qualified      *
52 *************************************************/
53
54 /*
55 Arguments:
56   s              address to check
57   is_recipient   TRUE if a recipient address; FALSE if a sender address
58
59 Returns:         fully-qualified address
60 */
61
62 uschar *
63 rewrite_address_qualify(uschar *s, BOOL is_recipient)
64 {
65 return (parse_find_at(s) != NULL)? s :
66   string_sprintf("%s@%s", s,
67     is_recipient? qualify_domain_recipient : qualify_domain_sender);
68 }
69
70
71
72 /*************************************************
73 *               Rewrite a single address         *
74 *************************************************/
75
76 /* The yield is the input address if there is no rewriting to be done. Assume
77 the input is a valid address, except in the case of SMTP-time rewriting, which
78 is handled specially. When this function is called while processing filter and
79 forward files, the uid may be that of the user. Ensure it is reset while
80 expanding a replacement, in case that involves file lookups.
81
82 Arguments:
83   s              address to rewrite
84   flag           indicates where this address comes from; it must match the
85                    flags in the rewriting rule
86   whole          if not NULL, set TRUE if any rewriting rule contained the
87                    "whole" bit and it is a header that is being rewritten
88   add_header     if TRUE and rewriting occurs, add an "X-rewrote-xxx" header
89                    if headers are in existence; this should be TRUE only when
90                    a message is being received, not during delivery
91   name           name of header, for use when adding X-rewrote-xxxx
92   rewrite_rules  chain of rewriting rules
93
94 Returns:         new address if rewritten; the input address if no change;
95                  for a header rewrite, if the "whole" bit is set, the entire
96                  rewritten address is returned, not just the active bit.
97 */
98
99 uschar *
100 rewrite_one(uschar *s, int flag, BOOL *whole, BOOL add_header, uschar *name,
101   rewrite_rule *rewrite_rules)
102 {
103 uschar *yield = s;
104 uschar *subject = s;
105 uschar *domain = NULL;
106 BOOL done = FALSE;
107 int rule_number = 1;
108 int yield_start = 0, yield_end = 0;
109
110 if (whole != NULL) *whole = FALSE;
111
112 /* Scan the rewriting rules */
113
114 for (rewrite_rule * rule = rewrite_rules; rule; rule_number++, rule = rule->next)
115   {
116   int start, end, pdomain;
117   int count = 0;
118   uschar *save_localpart;
119   const uschar *save_domain;
120   uschar *error, *new, *newparsed;
121
122   /* Ensure that the flag matches the flags in the rule. */
123
124   if ((rule->flags & flag) == 0) continue;
125
126   /* Come back here for a repeat after a successful rewrite. We do this
127   only so many times. */
128
129   REPEAT_RULE:
130
131   /* If this is an SMTP-time rewrite, the pattern must be a regex and
132   the subject may have any structure. No local part or domain variables
133   can be set for the expansion. We expand the pattern in order to be consistent
134   with the other kinds of rewrite, where expansion happens inside
135   match_address_list(). */
136
137   if ((flag & rewrite_smtp) != 0)
138     {
139     uschar *key = expand_string(rule->key);
140     if (key == NULL)
141       {
142       if (!f.expand_string_forcedfail)
143         log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while "
144           "checking for SMTP rewriting: %s", rule->key, expand_string_message);
145       continue;
146       }
147     if (match_check_string(subject, key, 0, TRUE, FALSE, FALSE, NULL) != OK)
148       continue;
149     new = expand_string(rule->replacement);
150     }
151
152   /* All other rewrites expect the input to be a valid address, so local part
153   and domain variables can be set for expansion. For the first rule, to be
154   applied to this address, domain will be NULL and needs to be set. */
155
156   else
157     {
158     if (domain == NULL) domain = Ustrrchr(subject, '@') + 1;
159
160     /* Use the general function for matching an address against a list (here
161     just one item, so use the "impossible value" separator UCHAR_MAX+1). */
162
163     if (match_address_list(subject, FALSE, TRUE, CUSS &(rule->key), NULL, 0,
164         UCHAR_MAX + 1, NULL) != OK)
165       continue;
166
167     /* The source address matches, and numerical variables have been
168     set up. If the replacement string consists of precisely "*" then no
169     rewriting is required for this address - the behaviour is as for "fail"
170     in the replacement expansion, but assuming the quit flag. */
171
172     if (Ustrcmp(rule->replacement, "*") == 0) break;
173
174     /* Otherwise, expand the replacement string. Set $local_part and $domain to
175     the appropriate values, restoring whatever value they previously had
176     afterwards. */
177
178     save_localpart = deliver_localpart;
179     save_domain = deliver_domain;
180
181     /* We have subject pointing to "localpart@domain" and domain pointing to
182     the domain. Temporarily terminate the local part so that it can be
183     set up as an expansion variable */
184
185     domain[-1] = 0;
186     deliver_localpart = subject;
187     deliver_domain = domain;
188
189     new = expand_string(rule->replacement);
190
191     domain[-1] = '@';
192     deliver_localpart = save_localpart;
193     deliver_domain = save_domain;
194     }
195
196   /* If the expansion failed with the "forcedfail" flag, don't generate
197   an error - just give up on this rewriting rule. If the "q" flag is set,
198   give up altogether. For other expansion failures we have a configuration
199   error. */
200
201   if (new == NULL)
202     {
203     if (f.expand_string_forcedfail)
204       { if ((rule->flags & rewrite_quit) != 0) break; else continue; }
205
206     expand_string_message = expand_hide_passwords(expand_string_message);
207
208     log_write(0, LOG_MAIN|LOG_PANIC, "Expansion of %s failed while rewriting: "
209       "%s", rule->replacement, expand_string_message);
210     break;
211     }
212
213   /* Check the what has been generated is a valid RFC 2822 address. Only
214   envelope from or SMTP sender is permitted to be rewritten as <>.*/
215
216   newparsed = parse_extract_address(new, &error, &start, &end, &pdomain,
217     flag == rewrite_envfrom || flag == (rewrite_smtp|rewrite_smtp_sender));
218
219   if (newparsed == NULL)
220     {
221     log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unparseable "
222       "address: %s in address %s", subject, error, new);
223     break;   /* Give up on this address */
224     }
225
226   /* A non-null unqualified address can be qualified if requested. Otherwise,
227   this is an error unless it's the empty address in circumstances where that is
228   permitted. */
229
230   if (pdomain == 0 && (*newparsed != 0 ||
231       (flag != rewrite_envfrom && flag != (rewrite_smtp|rewrite_smtp_sender))))
232     {
233     if ((rule->flags & rewrite_qualify) != 0)
234       {
235       newparsed = rewrite_address_qualify(newparsed, TRUE);
236       new = string_sprintf("%.*s%s%.*s", start, new, newparsed,
237         Ustrlen(new) - end, new + end);
238       end = start + Ustrlen(newparsed);
239       }
240     else
241       {
242       log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unqualified "
243         "address \"%s\"", subject, new);
244       break;   /* Give up on this address */
245       }
246     }
247
248   /* We have a validly rewritten address */
249
250   if (LOGGING(address_rewrite) || (debug_selector & D_rewrite) != 0)
251     {
252     const uschar *where = CUS"?";
253
254     for (int i = 0; i < where_list_size; i++)
255       if (flag == where_list[i].bit)
256         {
257         where = where_list[i].string;
258         break;
259         }
260     log_write(L_address_rewrite,
261            LOG_MAIN, "\"%s\" from %s rewritten as \"%s\" by rule %d",
262            yield, where, new, rule_number);
263     }
264
265   /* A header will only actually be added if header_last is non-NULL,
266   i.e. during message reception or delivery, but add_header should not
267   be set TRUE during delivery, as otherwise multiple instances of the header
268   can fill up the -H file and make it embarrassingly large. We don't need
269   to set header_rewritten because the -H file always gets written at the end
270   of message reception. */
271
272   if (add_header)
273     header_add(htype_old, "X-rewrote-%s: %s\n", name, subject);
274
275   /* Handle the case when replacement of the whole address is possible.
276   This happens only when whole is not NULL and we are rewriting a header.
277   If *whole is already TRUE it means that a previous rule had the w
278   flag set and so we must preserve the non-active portion of the current
279   subject unless the current rule also has the w flag set. */
280
281   if (whole != NULL && (flag & rewrite_all_headers) != 0)
282     {
283     /* Current rule has the w flag set. We must ensure the phrase parts
284     are syntactically valid if they are present. */
285
286     if ((rule->flags & rewrite_whole) != 0)
287       {
288       if (start > 0 && new[start-1] == '<')
289         {
290         uschar *p1 = new + start - 1;
291         uschar *p2 = new + end + 1;
292         const uschar *pf1, *pf2;
293         uschar buff1[256], buff2[256];
294
295         while (p1 > new && p1[-1] == ' ') p1--;
296         pf1 = parse_fix_phrase(new, p1 - new, buff1, sizeof(buff1));
297         while (*p2 == ' ') p2++;
298         pf2 = parse_fix_phrase(p2, Ustrlen(p2), buff2, sizeof(buff2));
299
300         /* Note that pf1 and pf2 are NOT necessarily buff1 and buff2. For
301         a non-RFC 2047 phrase that does not need to be RFC 2822 quoted, they
302         will be buff1+1 and buff2+1. */
303
304         start = Ustrlen(pf1) + start + new - p1;
305         end = start + Ustrlen(newparsed);
306         new = string_sprintf("%s%.*s%s", pf1, (int)(p2 - p1), p1, pf2);
307         }
308
309       /* Now accept the whole thing */
310
311       yield = new;
312       yield_start = start;
313       yield_end = end;
314       subject = newparsed;
315       *whole = TRUE;
316       }
317
318     /* Current rule does not have the w flag set; if not previously
319     done any whole rewriting, behave in non-whole manner. */
320
321     else if (!*whole) goto NEVER_WHOLE;
322
323     /* Current rule does not have the w flag set, but a previous
324     rule did rewrite the whole address. Thus yield and subject will be
325     different. Preserve the previous non-active part of the address. */
326
327     else
328       {
329       subject = newparsed;
330       new = string_sprintf("%.*s%s%n%s",
331          yield_start, yield, subject, &end, yield + yield_end);
332       yield_end = end;
333       yield = new;
334       }
335     }
336
337   /* Rule just rewrites active part, or handling an envelope. This
338   code is obeyed only when all rules so far have not done "whole"
339   replacement. */
340
341   else
342     {
343     NEVER_WHOLE:
344     subject = yield = newparsed;
345     }
346
347   domain = NULL;    /* Reset for next rule */
348
349   /* If no further rewrites are to be done, set the done flag. This allows
350   repeats of the current rule if configured before breaking the loop. */
351
352   if ((rule->flags & rewrite_quit) != 0) done = TRUE;
353
354   /* Allow the current rule to be applied up to 10 times if
355   requested. */
356
357   if ((rule->flags & rewrite_repeat) != 0)
358     {
359     if (count++ < 10) goto REPEAT_RULE;
360     log_write(0, LOG_MAIN|LOG_PANIC, "rewrite rule repeat ignored after 10 "
361       "times");
362     }
363   }
364
365 /* Unset expansion numeric variables, and that's it. */
366
367 expand_nmax = -1;
368 return yield;
369 }
370
371
372
373 /*************************************************
374 *         Ensure qualification and rewrite       *
375 *************************************************/
376
377 /* This function is called for envelope addresses, the boolean specifying
378 whether a recipient or a sender. It must first of all ensure the address is
379 fully qualified, and then apply any relevant re-writing rules. The add-header
380 flag causes a header to be added, recording the old address. This is marked
381 "old", so that it is never transported anywhere; it exists for local checking
382 and debugging purposes.
383
384 Arguments:
385   s              the address to be considered
386   is_recipient   TRUE for recipient addresses; FALSE otherwise
387   add_header     add "X-rewrote-xxx" header when rewriting; this is
388                    set TRUE only for calls from the reception functions
389   rewrite_rules  points to chain of rewrite rules
390   existflags     bits indicating which headers there are rewrites for
391                  (just an optimisation)
392
393 Returns:         possibly rewritten address
394 */
395
396 uschar *
397 rewrite_address(uschar *s, BOOL is_recipient, BOOL add_header,
398   rewrite_rule *rewrite_rules, int existflags)
399 {
400 int flag = is_recipient? rewrite_envto : rewrite_envfrom;
401 s = rewrite_address_qualify(s, is_recipient);
402 if ((existflags & flag) != 0)
403   {
404   uschar *new = rewrite_one(s, flag, NULL, add_header, is_recipient?
405     US"original-recipient" : US"sender", rewrite_rules);
406   if (new != s) s = new;
407   }
408 return s;
409 }
410
411
412
413 /*************************************************
414 *    Qualify and possibly rewrite one header     *
415 *************************************************/
416
417 /* This is called only from rewrite_header() below, either when reading a
418 message. or when routing, in order to rewrite addresses that get changed by a
419 router. This is normally the addition of full qualification to a partial
420 domain. The first rewriting rule in this case is "change routed_old into
421 routed_new", and it applies to all header lines that contain addresses. Then
422 header-specific rewriting rules are applied.
423
424 Before rewriting can be done, addresses without domains have to be qualified.
425 This should only be done for messages from "local" senders. This is a difficult
426 concept to pin down, what with the use of SMTP both as a submission and as a
427 transmission protocol. Exim normally requires incoming SMTP to contain fully-
428 qualified addresses, but there are options to permit unqualified ones from
429 certain hosts. For those hosts only, addresses in headers can also be
430 qualified. For other hosts, unqualified addresses in headers do not get touched
431 in any way. For locally sourced messages, unqualified addresses always get
432 qualified, except when -bnq is used to explicitly suppress this.
433
434 Arguments:
435   h              pointer to header line block
436   flag           indicates which header this is
437   routed_old     if not NULL, this is a rewrite caused by a router, changing
438                    this domain into routed_new
439   routed_new     new routed domain if routed_old is not NULL
440   rewrite_rules  points to chain of rewriting rules
441   existflags     bits indicating which rewrites exist
442   replace        if TRUE, insert the new header in the chain after the old
443                    one, and mark the old one "replaced"
444
445 Returns:         NULL if header unchanged; otherwise the rewritten header
446 */
447
448 static header_line *
449 rewrite_one_header(header_line *h, int flag,
450   const uschar *routed_old, const uschar *routed_new,
451   rewrite_rule *rewrite_rules, int existflags, BOOL replace)
452 {
453 int lastnewline = 0;
454 header_line *newh = NULL;
455 void *function_reset_point = store_get(0);
456 uschar *s = Ustrchr(h->text, ':') + 1;
457 while (isspace(*s)) s++;
458
459 DEBUG(D_rewrite)
460   debug_printf("rewrite_one_header: type=%c:\n  %s", h->type, h->text);
461
462 f.parse_allow_group = TRUE;     /* Allow group syntax */
463
464 /* Loop for multiple addresses in the header. We have to go through them all
465 in case any need qualifying, even if there's no rewriting. Pathological headers
466 may have thousands of addresses in them, so cause the store to be reset for
467 any that don't actually get rewritten. We also play silly games for those that
468 _are_ rewritten so as to avoid runaway store usage for these kinds of header.
469 We want to avoid keeping store for any intermediate versions. */
470
471 while (*s)
472   {
473   uschar *sprev;
474   uschar *ss = parse_find_address_end(s, FALSE);
475   uschar *recipient, *new, *errmess;
476   void *loop_reset_point = store_get(0);
477   BOOL changed = FALSE;
478   int terminator = *ss;
479   int start, end, domain;
480
481   /* Temporarily terminate the string at this point, and extract the
482   operative address within. Then put back the terminator and prepare for
483   the next address, saving the start of the old one. */
484
485   *ss = 0;
486   recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE);
487   *ss = terminator;
488   sprev = s;
489   s = ss + (terminator? 1:0);
490   while (isspace(*s)) s++;
491
492   /* There isn't much we can do for syntactic disasters at this stage.
493   Pro tem (possibly for ever) ignore them. */
494
495   if (!recipient)
496     {
497     store_reset(loop_reset_point);
498     continue;
499     }
500
501   /* If routed_old is not NULL, this is a rewrite caused by a router,
502   consisting of changing routed_old into routed_new, and applying to all
503   headers. If the header address has no domain, it is excluded, since a router
504   rewrite affects domains only. The new value should always be fully qualified,
505   but it may be something that has an explicit re-write rule set, so we need to
506   check the configured rules subsequently as well. (Example: there's an
507   explicit rewrite turning *.foo.com into foo.com, and an address is supplied
508   as abc@xyz, which the DNS lookup turns into abc@xyz.foo.com). However, if no
509   change is made here, don't bother carrying on. */
510
511   if (routed_old != NULL)
512     {
513     if (domain <= 0 || strcmpic(recipient+domain, routed_old) != 0) continue;
514     recipient[domain-1] = 0;
515     new = string_sprintf("%s@%s", recipient, routed_new);
516     DEBUG(D_rewrite)
517       {
518       recipient[domain-1] = '@';
519       debug_printf("%s rewritten by router as %s\n", recipient, new);
520       }
521     recipient = new;
522     changed = TRUE;
523     }
524
525   /* This is not a router-inspired rewrite. Ensure the address is fully
526   qualified if that is permitted. If an unqualified address was received
527   from a host that isn't listed, do not continue rewriting this address.
528   Sender, From or Reply-To headers are treated as senders, the rest as
529   recipients. This matters only when there are different qualify strings. */
530
531   else
532     {
533     BOOL is_recipient =
534       (flag & (rewrite_sender | rewrite_from | rewrite_replyto)) == 0;
535     new = rewrite_address_qualify(recipient, is_recipient);
536     changed = (new != recipient);
537     recipient = new;
538
539     /* Can only qualify if permitted; if not, no rewrite. */
540
541     if (changed && ((is_recipient && !f.allow_unqualified_recipient) ||
542                     (!is_recipient && !f.allow_unqualified_sender)))
543       {
544       store_reset(loop_reset_point);
545       continue;
546       }
547     }
548
549   /* If there are rewrite rules for this type of header, apply
550   them. This test is just for efficiency, to save scanning the rules
551   in cases when nothing is going to change. If any rewrite rule had the
552   "whole" flag set, adjust the pointers so that the whole address gets
553   replaced, except possibly a final \n. */
554
555   if ((existflags & flag) != 0)
556     {
557     BOOL whole;
558     new = rewrite_one(recipient, flag, &whole, FALSE, NULL, rewrite_rules);
559     if (new != recipient)
560       {
561       changed = TRUE;
562       if (whole)
563         {
564         start = 0;
565         end = ss - sprev;
566         if (sprev[end-1] == '\n') end--;
567         }
568       }
569     }
570
571   /* If nothing has changed, lose all dynamic store obtained in this loop, and
572   move on to the next address. We can't reset to the function start store
573   point, because we may have a rewritten line from a previous time round the
574   loop. */
575
576   if (!changed) store_reset(loop_reset_point);
577
578   /* If the address has changed, create a new header containing the
579   rewritten address. We do not need to set the chain pointers at this
580   stage. We want to avoid using more and more memory if the header is very long
581   and contains lots and lots of rewritten addresses. Therefore, we build the
582   new text string in malloc store, then at the end we reset dynamic store
583   before copying the new header to a new block (and then freeing the malloc
584   block). The header must end up in dynamic store so that it's freed at the end
585   of receiving a message. */
586
587   else
588     {
589     int remlen;
590     int newlen = Ustrlen(new);
591     int oldlen = end - start;
592
593     header_line *prev = (newh == NULL)? h : newh;
594     uschar *newt = store_malloc(prev->slen - oldlen + newlen + 4);
595     uschar *newtstart = newt;
596
597     int type = prev->type;
598     int slen = prev->slen - oldlen + newlen;
599
600     /* Build the new header text by copying the old and putting in the
601     replacement. This process may make the header substantially longer
602     than it was before - qualification of a list of bare addresses can
603     often do this - so we stick in a newline after the re-written address
604     if it has increased in length and ends more than 40 characters in. In
605     fact, the code is not perfect, since it does not scan for existing
606     newlines in the header, but it doesn't seem worth going to that
607     amount of trouble. */
608
609     Ustrncpy(newt, prev->text, sprev - prev->text + start);
610     newt += sprev - prev->text + start;
611     *newt = 0;
612     Ustrcat(newt, new);
613     newt += newlen;
614     remlen = s - (sprev + end);
615     if (remlen > 0)
616       {
617       Ustrncpy(newt, sprev + end, remlen);
618       newt += remlen;
619       *newt = 0;
620       }
621
622     /* Must check that there isn't a newline here anyway; in particular, there
623     will be one at the very end of the header, where we DON'T want to insert
624     another one! The pointer s has been skipped over white space, so just
625     look back to see if the last non-space-or-tab was a newline. */
626
627     if (newlen > oldlen && newt - newtstart - lastnewline > 40)
628       {
629       uschar *p = s - 1;
630       while (p >= prev->text && (*p == ' ' || *p == '\t')) p--;
631       if (*p != '\n')
632         {
633         lastnewline = newt - newtstart;
634         Ustrcat(newt, "\n\t");
635         slen += 2;
636         }
637       }
638
639     /* Finally, the remaining unprocessed addresses, if any. */
640
641     Ustrcat(newt, s);
642
643     DEBUG(D_rewrite) debug_printf("newlen=%d newtype=%c newtext:\n%s",
644       slen, type, newtstart);
645
646     /* Compute the length of the rest of the header line before we possibly
647     flatten a previously rewritten copy. */
648
649     remlen = (s - prev->text) - oldlen + newlen;
650
651     /* We have the new text in a malloc block. That enables us to release all
652     the memory that has been used, back to the point at which the function was
653     entered. Then set up a new header in dynamic store. This will override a
654     rewritten copy from a previous time round this loop. */
655
656     store_reset(function_reset_point);
657     newh = store_get(sizeof(header_line));
658     newh->type = type;
659     newh->slen = slen;
660     newh->text = string_copyn(newtstart, slen);
661     store_free(newtstart);
662
663     /* Set up for scanning the rest of the header */
664
665     s = newh->text + remlen;
666     DEBUG(D_rewrite) debug_printf("remainder: %s", (*s == 0)? US"\n" : s);
667     }
668   }
669
670 f.parse_allow_group = FALSE;  /* Reset group flags */
671 f.parse_found_group = FALSE;
672
673 /* If a rewrite happened and "replace" is true, put the new header into the
674 chain following the old one, and mark the old one as replaced. */
675
676 if (newh != NULL && replace)
677   {
678   newh->next = h->next;
679   if (newh->next == NULL) header_last = newh;
680   h->type = htype_old;
681   h->next = newh;
682   }
683
684 return newh;
685 }
686
687
688
689
690 /*************************************************
691 *              Rewrite a header line             *
692 *************************************************/
693
694 /* This function may be passed any old header line. It must detect those which
695 contain addresses, then then apply any rewriting rules that apply. If
696 routed_old is NULL, only the configured rewriting rules are consulted.
697 Otherwise, the rewriting rule is "change routed_old into routed_new", and it
698 applies to all header lines that contain addresses. Then header-specific
699 rewriting rules are applied.
700
701 The old header line is flagged as "old". Old headers are saved on the spool for
702 debugging but are never sent to any recipients.
703
704 Arguments:
705   h              header line to rewrite
706   routed_old     if not NULL, this is a rewrite caused by a router, changing
707                    this domain into routed_new
708   routed_new     new routed domain if routed_old is not NULL
709   rewrite_rules  points to chain of rewrite rules
710   existflags     bits indicating which rewrites exist
711   replace        if TRUE, the new header is inserted into the header chain
712                     after the old one, and the old one is marked replaced
713
714 Returns:         NULL if header unchanged; otherwise the rewritten header
715 */
716
717 header_line *
718 rewrite_header(header_line *h,
719   const uschar *routed_old, const uschar *routed_new,
720   rewrite_rule *rewrite_rules, int existflags, BOOL replace)
721 {
722 switch (h->type)
723   {
724   case htype_sender:
725   return rewrite_one_header(h, rewrite_sender, routed_old, routed_new,
726     rewrite_rules, existflags, replace);
727
728   case htype_from:
729   return rewrite_one_header(h, rewrite_from, routed_old, routed_new,
730     rewrite_rules, existflags, replace);
731
732   case htype_to:
733   return rewrite_one_header(h, rewrite_to, routed_old, routed_new,
734     rewrite_rules, existflags, replace);
735
736   case htype_cc:
737   return rewrite_one_header(h, rewrite_cc, routed_old, routed_new,
738     rewrite_rules, existflags, replace);
739
740   case htype_bcc:
741   return rewrite_one_header(h, rewrite_bcc, routed_old, routed_new,
742     rewrite_rules, existflags, replace);
743
744   case htype_reply_to:
745   return rewrite_one_header(h, rewrite_replyto, routed_old, routed_new,
746     rewrite_rules, existflags, replace);
747   }
748
749 return NULL;
750 }
751
752
753
754 /************************************************
755 *            Test rewriting rules               *
756 ************************************************/
757
758 /* Called from the mainline as a result of the -brw option. Test the
759 address for all possible cases.
760
761 Argument: the address to test
762 Returns:  nothing
763 */
764
765 void rewrite_test(uschar *s)
766 {
767 uschar *recipient, *error;
768 int start, end, domain;
769 BOOL done_smtp = FALSE;
770
771 if (rewrite_existflags == 0)
772   {
773   printf("No rewrite rules are defined\n");
774   return;
775   }
776
777 /* Do SMTP rewrite only if a rule with the S flag exists. Allow <> by
778 pretending it is a sender. */
779
780 if ((rewrite_existflags & rewrite_smtp) != 0)
781   {
782   uschar *new = rewrite_one(s, rewrite_smtp|rewrite_smtp_sender, NULL, FALSE,
783     US"", global_rewrite_rules);
784   if (new != s)
785     {
786     if (*new == 0)
787       printf("    SMTP: <>\n");
788     else
789       printf("    SMTP: %s\n", new);
790     done_smtp = TRUE;
791     }
792   }
793
794 /* Do the other rewrites only if a rule without the S flag exists */
795
796 if ((rewrite_existflags & ~rewrite_smtp) == 0) return;
797
798 /* Qualify if necessary before extracting the address */
799
800 if (parse_find_at(s) == NULL)
801   s = string_sprintf("%s@%s", s, qualify_domain_recipient);
802
803 recipient = parse_extract_address(s, &error, &start, &end, &domain, FALSE);
804
805 if (!recipient)
806   {
807   if (!done_smtp)
808     printf("Syntax error in %s\n%c%s\n", s, toupper(error[0]), error+1);
809   return;
810   }
811
812 for (int i = 0; i < 8; i++)
813   {
814   BOOL whole = FALSE;
815   int flag = 1 << i;
816   uschar *new = rewrite_one(recipient, flag, &whole, FALSE, US"",
817     global_rewrite_rules);
818   printf("%s: ", rrname[i]);
819   if (*new == 0)
820     printf("<>\n");
821   else if (whole || (flag & rewrite_all_headers) == 0)
822     printf("%s\n", CS new);
823   else printf("%.*s%s%s\n", start, s, new, s+end);
824   }
825 }
826
827 /* End of rewrite.c */