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