Revert introduction of alloc_insecure_tainted_data
[exim.git] / src / src / routers / rf_get_transport.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* Copyright (c) The Exim Maintainers 2021 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 #include "../exim.h"
10 #include "rf_functions.h"
11
12
13 /*************************************************
14 *       Get transport for a router               *
15 *************************************************/
16
17 /* If transport_name contains $, it must be expanded each time and used as a
18 transport name. Otherwise, look up the transport only if the destination is not
19 already set.
20
21 Some routers (e.g. accept) insist that their transport option is set at
22 initialization time. However, for some (e.g. file_transport in redirect), there
23 is no such check, because the transport may not be required. Calls to this
24 function from the former type of router have require_name = NULL, because it
25 will never be used. NULL is also used in verify_only cases, where a transport
26 is not required.
27
28 Arguments:
29   tpname        the text of the transport name
30   tpptr         where to put the transport
31   addr          the address being processed
32   router_name   for error messages
33   require_name  used in the error message if transport is unset
34
35 Returns:        TRUE if *tpptr is already set and tpname has no '$' in it;
36                 TRUE if a transport has been placed in tpptr;
37                 FALSE if there's a problem, in which case
38                 addr->message contains a message, and addr->basic_errno has
39                 ERRNO_BADTRANSPORT set in it.
40 */
41
42 BOOL
43 rf_get_transport(uschar *tpname, transport_instance **tpptr, address_item *addr,
44   uschar *router_name, uschar *require_name)
45 {
46 uschar *ss;
47 BOOL expandable;
48
49 if (!tpname)
50   {
51   if (!require_name) return TRUE;
52   addr->basic_errno = ERRNO_BADTRANSPORT;
53   addr->message = string_sprintf("%s unset in %s router", require_name,
54     router_name);
55   return FALSE;
56   }
57
58 expandable = Ustrchr(tpname, '$') != NULL;
59 if (*tpptr != NULL && !expandable) return TRUE;
60
61 if (expandable)
62   {
63   if (!(ss = expand_string(tpname)))
64     {
65     addr->basic_errno = ERRNO_BADTRANSPORT;
66     addr->message = string_sprintf("failed to expand transport "
67       "\"%s\" in %s router: %s", tpname, router_name, expand_string_message);
68     return FALSE;
69     }
70   if (is_tainted(ss))
71     {
72     log_write(0, LOG_MAIN|LOG_PANIC,
73       "attempt to use tainted value '%s' from '%s' for transport", ss, tpname);
74     addr->basic_errno = ERRNO_BADTRANSPORT;
75     /* Avoid leaking info to an attacker */
76     addr->message = US"internal configuration error";
77     return FALSE;
78     }
79   }
80 else
81   ss = tpname;
82
83 for (transport_instance * tp = transports; tp; tp = tp->next)
84   if (Ustrcmp(tp->name, ss) == 0)
85     {
86     DEBUG(D_route) debug_printf("set transport %s\n", ss);
87     *tpptr = tp;
88     return TRUE;
89     }
90
91 addr->basic_errno = ERRNO_BADTRANSPORT;
92 addr->message = string_sprintf("transport \"%s\" not found in %s router", ss,
93   router_name);
94 return FALSE;
95 }
96
97 /* End of rf_get_transport.c */