Copyright updates:
[exim.git] / src / src / routers / rf_change_domain.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9
10 #include "../exim.h"
11 #include "rf_functions.h"
12
13
14
15 /*************************************************
16 *          Change domain in an address           *
17 *************************************************/
18
19 /* When a router wants to change the address that is being routed, it is like a
20 redirection. We insert a new parent of the current address to hold the original
21 information, and change the data in the original address, which is now the
22 child. The child address is put onto the addr_new chain. Pick up the local part
23 from the "address" field so as to get it in external form - caseful, and with
24 any quoting retained.
25
26 Arguments:
27   addr        the address block
28   domain      the new domain
29   rewrite     TRUE if headers lines are to be rewritten
30   addr_new    the new address chain
31
32 Returns:      nothing
33 */
34
35 void
36 rf_change_domain(address_item *addr, const uschar *domain, BOOL rewrite,
37   address_item **addr_new)
38 {
39 address_item * parent = store_get(sizeof(address_item), GET_UNTAINTED);
40 uschar * at = Ustrrchr(addr->address, '@');
41 uschar * address = string_sprintf("%.*s@%s",
42   (int)(at - addr->address), addr->address, domain);
43
44 DEBUG(D_route) debug_printf("domain changed to %s\n", domain);
45
46 /* The current address item is made into the parent, and a new address is set
47 up in the old space. */
48
49 *parent = *addr;
50
51 /* First copy in initializing values, to wipe out stuff such as the named
52 domain cache. Then copy over the propagating fields from the parent. Then set
53 up the new fields. */
54
55 *addr = address_defaults;
56 addr->prop = parent->prop;
57
58 addr->address = address;
59 addr->unique = string_copy(address);
60 addr->parent = parent;
61 parent->child_count = 1;
62
63 addr->next = *addr_new;
64 *addr_new = addr;
65
66 /* Rewrite header lines if requested */
67
68 if (rewrite)
69   {
70   DEBUG(D_route|D_rewrite) debug_printf("rewriting header lines\n");
71   for (header_line * h = header_list; h != NULL; h = h->next)
72     {
73     header_line *newh =
74       rewrite_header(h, parent->domain, domain,
75         global_rewrite_rules, rewrite_existflags, TRUE);
76     if (newh)
77       {
78       h = newh;
79       f.header_rewritten = TRUE;
80       }
81     }
82   }
83 }
84
85 /* End of rf_change_domain.c */