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