xref: /netbsd-src/external/ibm-public/postfix/dist/src/trivial-rewrite/resolve.c (revision a30b880ed60a24c405edba78187a04247f4d9d33)
1 /*	$NetBSD: resolve.c,v 1.1.1.4 2013/01/02 18:59:10 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	resolve 3
6 /* SUMMARY
7 /*	mail address resolver
8 /* SYNOPSIS
9 /*	#include "trivial-rewrite.h"
10 /*
11 /*	void	resolve_init(void)
12 /*
13 /*	void	resolve_proto(context, stream)
14 /*	RES_CONTEXT *context;
15 /*	VSTREAM	*stream;
16 /* DESCRIPTION
17 /*	This module implements the trivial address resolving engine.
18 /*	It distinguishes between local and remote mail, and optionally
19 /*	consults one or more transport tables that map a destination
20 /*	to a transport, nexthop pair.
21 /*
22 /*	resolve_init() initializes data structures that are private
23 /*	to this module. It should be called once before using the
24 /*	actual resolver routines.
25 /*
26 /*	resolve_proto() implements the client-server protocol:
27 /*	read one address in FQDN form, reply with a (transport,
28 /*	nexthop, internalized recipient) triple.
29 /* STANDARDS
30 /* DIAGNOSTICS
31 /*	Problems and transactions are logged to the syslog daemon.
32 /* BUGS
33 /* SEE ALSO
34 /* LICENSE
35 /* .ad
36 /* .fi
37 /*	The Secure Mailer license must be distributed with this software.
38 /* AUTHOR(S)
39 /*	Wietse Venema
40 /*	IBM T.J. Watson Research
41 /*	P.O. Box 704
42 /*	Yorktown Heights, NY 10598, USA
43 /*--*/
44 
45 /* System library. */
46 
47 #include <sys_defs.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #ifdef STRCASECMP_IN_STRINGS_H
52 #include <strings.h>
53 #endif
54 
55 /* Utility library. */
56 
57 #include <msg.h>
58 #include <vstring.h>
59 #include <vstream.h>
60 #include <vstring_vstream.h>
61 #include <split_at.h>
62 #include <valid_hostname.h>
63 #include <stringops.h>
64 #include <mymalloc.h>
65 
66 /* Global library. */
67 
68 #include <mail_params.h>
69 #include <mail_proto.h>
70 #include <resolve_local.h>
71 #include <mail_conf.h>
72 #include <quote_822_local.h>
73 #include <tok822.h>
74 #include <domain_list.h>
75 #include <string_list.h>
76 #include <match_parent_style.h>
77 #include <maps.h>
78 #include <mail_addr_find.h>
79 #include <valid_mailhost_addr.h>
80 
81 /* Application-specific. */
82 
83 #include "trivial-rewrite.h"
84 #include "transport.h"
85 
86  /*
87   * The job of the address resolver is to map one recipient address to a
88   * triple of (channel, nexthop, recipient). The channel is the name of the
89   * delivery service specified in master.cf, the nexthop is (usually) a
90   * description of the next host to deliver to, and recipient is the final
91   * recipient address. The latter may differ from the input address as the
92   * result of stripping multiple layers of sender-specified routing.
93   *
94   * Addresses are resolved by their domain name. Known domain names are
95   * categorized into classes: local, virtual alias, virtual mailbox, relay,
96   * and everything else. Finding the address domain class is a matter of
97   * table lookups.
98   *
99   * Different address domain classes generally use different delivery channels,
100   * and may use class dependent ways to arrive at the corresponding nexthop
101   * information. With classes that do final delivery, the nexthop is
102   * typically the local machine hostname.
103   *
104   * The transport lookup table provides a means to override the domain class
105   * channel and/or nexhop information for specific recipients or for entire
106   * domain hierarchies.
107   *
108   * This works well in the general case. The only bug in this approach is that
109   * the structure of the nexthop information is transport dependent.
110   * Typically, the nexthop specifies a hostname, hostname + TCP Port, or the
111   * pathname of a UNIX-domain socket. However, with the error transport the
112   * nexthop field contains free text with the reason for non-delivery.
113   *
114   * Therefore, a transport map entry that overrides the channel but not the
115   * nexthop information (or vice versa) may produce surprising results. In
116   * particular, the free text nexthop information for the error transport is
117   * likely to confuse regular delivery agents; and conversely, a hostname or
118   * socket pathname is not an adequate text as reason for non-delivery.
119   *
120   * In the code below, rcpt_domain specifies the domain name that we will use
121   * when the transport table specifies a non-default channel but no nexthop
122   * information (we use a generic text when that non-default channel is the
123   * error transport).
124   */
125 
126 #define STR	vstring_str
127 
128  /*
129   * Some of the lists that define the address domain classes.
130   */
131 static DOMAIN_LIST *relay_domains;
132 static STRING_LIST *virt_alias_doms;
133 static STRING_LIST *virt_mailbox_doms;
134 
135 static MAPS *relocated_maps;
136 
137 /* resolve_addr - resolve address according to rule set */
138 
139 static void resolve_addr(RES_CONTEXT *rp, char *sender, char *addr,
140 			         VSTRING *channel, VSTRING *nexthop,
141 			         VSTRING *nextrcpt, int *flags)
142 {
143     const char *myname = "resolve_addr";
144     VSTRING *addr_buf = vstring_alloc(100);
145     TOK822 *tree = 0;
146     TOK822 *saved_domain = 0;
147     TOK822 *domain = 0;
148     char   *destination;
149     const char *blame = 0;
150     const char *rcpt_domain;
151     ssize_t addr_len;
152     ssize_t loop_count;
153     ssize_t loop_max;
154     char   *local;
155     char   *oper;
156     char   *junk;
157     const char *relay;
158     const char *xport;
159     const char *sender_key;
160     int     rc;
161 
162     *flags = 0;
163     vstring_strcpy(channel, "CHANNEL NOT UPDATED");
164     vstring_strcpy(nexthop, "NEXTHOP NOT UPDATED");
165     vstring_strcpy(nextrcpt, "NEXTRCPT NOT UPDATED");
166 
167     /*
168      * The address is in internalized (unquoted) form.
169      *
170      * In an ideal world we would parse the externalized address form as given
171      * to us by the sender.
172      *
173      * However, in the real world we have to look for routing characters like
174      * %@! in the address local-part, even when that information is quoted
175      * due to the presence of special characters or whitespace. Although
176      * technically incorrect, this is needed to stop user@domain@domain relay
177      * attempts when forwarding mail to a Sendmail MX host.
178      *
179      * This suggests that we parse the address in internalized (unquoted) form.
180      * Unfortunately, if we do that, the unparser generates incorrect white
181      * space between adjacent non-operator tokens. Example: ``first last''
182      * needs white space, but ``stuff[stuff]'' does not. This is is not a
183      * problem when unparsing the result from parsing externalized forms,
184      * because the parser/unparser were designed for valid externalized forms
185      * where ``stuff[stuff]'' does not happen.
186      *
187      * As a workaround we start with the quoted form and then dequote the
188      * local-part only where needed. This will do the right thing in most
189      * (but not all) cases.
190      */
191     addr_len = strlen(addr);
192     quote_822_local(addr_buf, addr);
193     tree = tok822_scan_addr(vstring_str(addr_buf));
194 
195     /*
196      * The optimizer will eliminate tests that always fail, and will replace
197      * multiple expansions of this macro by a GOTO to a single instance.
198      */
199 #define FREE_MEMORY_AND_RETURN { \
200 	if (saved_domain) \
201 	    tok822_free_tree(saved_domain); \
202 	if(tree) \
203 	    tok822_free_tree(tree); \
204 	if (addr_buf) \
205 	    vstring_free(addr_buf); \
206 	return; \
207     }
208 
209     /*
210      * Preliminary resolver: strip off all instances of the local domain.
211      * Terminate when no destination domain is left over, or when the
212      * destination domain is remote.
213      *
214      * XXX To whom it may concern. If you change the resolver loop below, or
215      * quote_822_local.c, or tok822_parse.c, be sure to re-run the tests
216      * under "make resolve_clnt_test" in the global directory.
217      */
218 #define RESOLVE_LOCAL(domain) \
219     resolve_local(STR(tok822_internalize(addr_buf, domain, TOK822_STR_DEFL)))
220 
221     for (loop_count = 0, loop_max = addr_len + 100; /* void */ ; loop_count++) {
222 
223 	/*
224 	 * XXX Should never happen, but if this happens with some
225 	 * pathological address, then that is not sufficient reason to
226 	 * disrupt the operation of an MTA.
227 	 */
228 	if (loop_count > loop_max) {
229 	    msg_warn("resolve_addr: <%s>: giving up after %ld iterations",
230 		     addr, (long) loop_count);
231 	    *flags |= RESOLVE_FLAG_FAIL;
232 	    FREE_MEMORY_AND_RETURN;
233 	    break;
234 	}
235 
236 	/*
237 	 * Strip trailing dot at end of domain, but not dot-dot or at-dot.
238 	 * This merely makes diagnostics more accurate by leaving bogus
239 	 * addresses alone.
240 	 */
241 	if (tree->tail
242 	    && tree->tail->type == '.'
243 	    && tok822_rfind_type(tree->tail, '@') != 0
244 	    && tree->tail->prev->type != '.'
245 	    && tree->tail->prev->type != '@')
246 	    tok822_free_tree(tok822_sub_keep_before(tree, tree->tail));
247 
248 	/*
249 	 * Strip trailing @.
250 	 */
251 	if (var_resolve_nulldom
252 	    && tree->tail
253 	    && tree->tail->type == '@')
254 	    tok822_free_tree(tok822_sub_keep_before(tree, tree->tail));
255 
256 	/*
257 	 * Strip (and save) @domain if local.
258 	 *
259 	 * Grr. resolve_local() table lookups may fail. It may be OK for local
260 	 * file lookup code to abort upon failure, but with network-based
261 	 * tables it is preferable to return an error indication to the
262 	 * requestor.
263 	 */
264 	if ((domain = tok822_rfind_type(tree->tail, '@')) != 0) {
265 	    if (domain->next && (rc = RESOLVE_LOCAL(domain->next)) <= 0) {
266 		if (rc < 0) {
267 		    *flags |= RESOLVE_FLAG_FAIL;
268 		    FREE_MEMORY_AND_RETURN;
269 		}
270 		break;
271 	    }
272 	    tok822_sub_keep_before(tree, domain);
273 	    if (saved_domain)
274 		tok822_free_tree(saved_domain);
275 	    saved_domain = domain;
276 	    domain = 0;				/* safety for future change */
277 	}
278 
279 	/*
280 	 * After stripping the local domain, if any, replace foo%bar by
281 	 * foo@bar, site!user by user@site, rewrite to canonical form, and
282 	 * retry.
283 	 */
284 	if (tok822_rfind_type(tree->tail, '@')
285 	    || (var_swap_bangpath && tok822_rfind_type(tree->tail, '!'))
286 	    || (var_percent_hack && tok822_rfind_type(tree->tail, '%'))) {
287 	    rewrite_tree(&local_context, tree);
288 	    continue;
289 	}
290 
291 	/*
292 	 * If the local-part is a quoted string, crack it open when we're
293 	 * permitted to do so and look for routing operators. This is
294 	 * technically incorrect, but is needed to stop relaying problems.
295 	 *
296 	 * XXX Do another feeble attempt to keep local-part info quoted.
297 	 */
298 	if (var_resolve_dequoted
299 	    && tree->head && tree->head == tree->tail
300 	    && tree->head->type == TOK822_QSTRING
301 	    && ((oper = strrchr(local = STR(tree->head->vstr), '@')) != 0
302 		|| (var_percent_hack && (oper = strrchr(local, '%')) != 0)
303 	     || (var_swap_bangpath && (oper = strrchr(local, '!')) != 0))) {
304 	    if (*oper == '%')
305 		*oper = '@';
306 	    tok822_internalize(addr_buf, tree->head, TOK822_STR_DEFL);
307 	    if (*oper == '@') {
308 		junk = mystrdup(STR(addr_buf));
309 		quote_822_local(addr_buf, junk);
310 		myfree(junk);
311 	    }
312 	    tok822_free(tree->head);
313 	    tree->head = tok822_scan(STR(addr_buf), &tree->tail);
314 	    rewrite_tree(&local_context, tree);
315 	    continue;
316 	}
317 
318 	/*
319 	 * An empty local-part or an empty quoted string local-part becomes
320 	 * the local MAILER-DAEMON, for consistency with our own From:
321 	 * message headers.
322 	 */
323 	if (tree->head && tree->head == tree->tail
324 	    && tree->head->type == TOK822_QSTRING
325 	    && VSTRING_LEN(tree->head->vstr) == 0) {
326 	    tok822_free(tree->head);
327 	    tree->head = 0;
328 	}
329 	/* XXX must be localpart only, not user@domain form. */
330 	if (tree->head == 0)
331 	    tree->head = tok822_scan(var_empty_addr, &tree->tail);
332 
333 	/*
334 	 * We're done. There are no domains left to strip off the address,
335 	 * and all null local-part information is sanitized.
336 	 */
337 	domain = 0;
338 	break;
339     }
340 
341     vstring_free(addr_buf);
342     addr_buf = 0;
343 
344     /*
345      * Make sure the resolved envelope recipient has the user@domain form. If
346      * no domain was specified in the address, assume the local machine. See
347      * above for what happens with an empty address.
348      */
349     if (domain == 0) {
350 	if (saved_domain) {
351 	    tok822_sub_append(tree, saved_domain);
352 	    saved_domain = 0;
353 	} else {
354 	    tok822_sub_append(tree, tok822_alloc('@', (char *) 0));
355 	    tok822_sub_append(tree, tok822_scan(var_myhostname, (TOK822 **) 0));
356 	}
357     }
358 
359     /*
360      * Transform the recipient address back to internal form.
361      *
362      * XXX This may produce incorrect results if we cracked open a quoted
363      * local-part with routing operators; see discussion above at the top of
364      * the big loop.
365      *
366      * XXX We explicitly disallow domain names in bare network address form. A
367      * network address destination should be formatted according to RFC 2821:
368      * it should be enclosed in [], and an IPv6 address should have an IPv6:
369      * prefix.
370      */
371     tok822_internalize(nextrcpt, tree, TOK822_STR_DEFL);
372     rcpt_domain = strrchr(STR(nextrcpt), '@') + 1;
373     if (rcpt_domain == 0)
374 	msg_panic("no @ in address: \"%s\"", STR(nextrcpt));
375     if (*rcpt_domain == '[') {
376 	if (!valid_mailhost_literal(rcpt_domain, DONT_GRIPE))
377 	    *flags |= RESOLVE_FLAG_ERROR;
378     } else if (!valid_hostname(rcpt_domain, DONT_GRIPE)) {
379 	if (var_resolve_num_dom && valid_hostaddr(rcpt_domain, DONT_GRIPE)) {
380 	    vstring_insert(nextrcpt, rcpt_domain - STR(nextrcpt), "[", 1);
381 	    vstring_strcat(nextrcpt, "]");
382 	    rcpt_domain = strrchr(STR(nextrcpt), '@') + 1;
383 	    if ((rc = resolve_local(rcpt_domain)) > 0)	/* XXX */
384 		domain = 0;
385 	    else if (rc < 0) {
386 		*flags |= RESOLVE_FLAG_FAIL;
387 		FREE_MEMORY_AND_RETURN;
388 	    }
389 	} else {
390 	    *flags |= RESOLVE_FLAG_ERROR;
391 	}
392     }
393     tok822_free_tree(tree);
394     tree = 0;
395 
396     /*
397      * XXX Short-cut invalid address forms.
398      */
399     if (*flags & RESOLVE_FLAG_ERROR) {
400 	*flags |= RESOLVE_CLASS_DEFAULT;
401 	FREE_MEMORY_AND_RETURN;
402     }
403 
404     /*
405      * Recognize routing operators in the local-part, even when we do not
406      * recognize ! or % as valid routing operators locally. This is needed to
407      * prevent backup MX hosts from relaying third-party destinations through
408      * primary MX hosts, otherwise the backup host could end up on black
409      * lists. Ignore local swap_bangpath and percent_hack settings because we
410      * can't know how the next MX host is set up.
411      */
412     if (strcmp(STR(nextrcpt) + strcspn(STR(nextrcpt), "@!%") + 1, rcpt_domain))
413 	*flags |= RESOLVE_FLAG_ROUTED;
414 
415     /*
416      * With local, virtual, relay, or other non-local destinations, give the
417      * highest precedence to transport associated nexthop information.
418      *
419      * Otherwise, with relay or other non-local destinations, the relayhost
420      * setting overrides the recipient domain name, and the sender-dependent
421      * relayhost overrides both.
422      *
423      * XXX Nag if the recipient domain is listed in multiple domain lists. The
424      * result is implementation defined, and may break when internals change.
425      *
426      * For now, we distinguish only a fixed number of address classes.
427      * Eventually this may become extensible, so that new classes can be
428      * configured with their own domain list, delivery transport, and
429      * recipient table.
430      */
431 #define STREQ(x,y) (strcmp((x), (y)) == 0)
432 
433     if (domain != 0) {
434 
435 	/*
436 	 * Virtual alias domain.
437 	 */
438 	if (virt_alias_doms
439 	    && string_list_match(virt_alias_doms, rcpt_domain)) {
440 	    if (var_helpful_warnings) {
441 		if (virt_mailbox_doms
442 		    && string_list_match(virt_mailbox_doms, rcpt_domain))
443 		    msg_warn("do not list domain %s in BOTH %s and %s",
444 			     rcpt_domain, VAR_VIRT_ALIAS_DOMS,
445 			     VAR_VIRT_MAILBOX_DOMS);
446 		if (relay_domains
447 		    && domain_list_match(relay_domains, rcpt_domain))
448 		    msg_warn("do not list domain %s in BOTH %s and %s",
449 			     rcpt_domain, VAR_VIRT_ALIAS_DOMS,
450 			     VAR_RELAY_DOMAINS);
451 #if 0
452 		if (strcasecmp(rcpt_domain, var_myorigin) == 0)
453 		    msg_warn("do not list $%s (%s) in %s",
454 			   VAR_MYORIGIN, var_myorigin, VAR_VIRT_ALIAS_DOMS);
455 #endif
456 	    }
457 	    vstring_strcpy(channel, MAIL_SERVICE_ERROR);
458 	    vstring_sprintf(nexthop, "User unknown%s",
459 			    var_show_unk_rcpt_table ?
460 			    " in virtual alias table" : "");
461 	    *flags |= RESOLVE_CLASS_ALIAS;
462 	} else if (virt_alias_doms && virt_alias_doms->error != 0) {
463 	    msg_warn("%s lookup failure", VAR_VIRT_ALIAS_DOMS);
464 	    *flags |= RESOLVE_FLAG_FAIL;
465 	    FREE_MEMORY_AND_RETURN;
466 	}
467 
468 	/*
469 	 * Virtual mailbox domain.
470 	 */
471 	else if (virt_mailbox_doms
472 		 && string_list_match(virt_mailbox_doms, rcpt_domain)) {
473 	    if (var_helpful_warnings) {
474 		if (relay_domains
475 		    && domain_list_match(relay_domains, rcpt_domain))
476 		    msg_warn("do not list domain %s in BOTH %s and %s",
477 			     rcpt_domain, VAR_VIRT_MAILBOX_DOMS,
478 			     VAR_RELAY_DOMAINS);
479 	    }
480 	    vstring_strcpy(channel, RES_PARAM_VALUE(rp->virt_transport));
481 	    vstring_strcpy(nexthop, rcpt_domain);
482 	    blame = rp->virt_transport_name;
483 	    *flags |= RESOLVE_CLASS_VIRTUAL;
484 	} else if (virt_mailbox_doms && virt_mailbox_doms->error != 0) {
485 	    msg_warn("%s lookup failure", VAR_VIRT_MAILBOX_DOMS);
486 	    *flags |= RESOLVE_FLAG_FAIL;
487 	    FREE_MEMORY_AND_RETURN;
488 	} else {
489 
490 	    /*
491 	     * Off-host relay destination.
492 	     */
493 	    if (relay_domains
494 		&& domain_list_match(relay_domains, rcpt_domain)) {
495 		vstring_strcpy(channel, RES_PARAM_VALUE(rp->relay_transport));
496 		blame = rp->relay_transport_name;
497 		*flags |= RESOLVE_CLASS_RELAY;
498 	    } else if (relay_domains && relay_domains->error != 0) {
499 		msg_warn("%s lookup failure", VAR_RELAY_DOMAINS);
500 		*flags |= RESOLVE_FLAG_FAIL;
501 		FREE_MEMORY_AND_RETURN;
502 	    }
503 
504 	    /*
505 	     * Other off-host destination.
506 	     */
507 	    else {
508 		if (rp->snd_def_xp_info
509 		    && (xport = mail_addr_find(rp->snd_def_xp_info,
510 					    sender_key = (*sender ? sender :
511 					       var_null_def_xport_maps_key),
512 					       (char **) 0)) != 0) {
513 		    if (*xport == 0) {
514 			msg_warn("%s: ignoring null lookup result for %s",
515 				 rp->snd_def_xp_maps_name, sender_key);
516 			xport = "DUNNO";
517 		    }
518 		    vstring_strcpy(channel, strcasecmp(xport, "DUNNO") == 0 ?
519 				RES_PARAM_VALUE(rp->def_transport) : xport);
520 		    blame = rp->snd_def_xp_maps_name;
521 		} else if (rp->snd_def_xp_info
522 			   && rp->snd_def_xp_info->error != 0) {
523 		    msg_warn("%s lookup failure", rp->snd_def_xp_maps_name);
524 		    *flags |= RESOLVE_FLAG_FAIL;
525 		    FREE_MEMORY_AND_RETURN;
526 		} else {
527 		    vstring_strcpy(channel, RES_PARAM_VALUE(rp->def_transport));
528 		    blame = rp->def_transport_name;
529 		}
530 		*flags |= RESOLVE_CLASS_DEFAULT;
531 	    }
532 
533 	    /*
534 	     * With off-host delivery, sender-dependent or global relayhost
535 	     * override the recipient domain.
536 	     */
537 	    if (rp->snd_relay_info
538 		&& (relay = mail_addr_find(rp->snd_relay_info,
539 					   sender_key = (*sender ? sender :
540 						   var_null_relay_maps_key),
541 					   (char **) 0)) != 0) {
542 		if (*relay == 0) {
543 		    msg_warn("%s: ignoring null lookup result for %s",
544 			     rp->snd_relay_maps_name, sender_key);
545 		    relay = "DUNNO";
546 		}
547 		vstring_strcpy(nexthop, strcasecmp(relay, "DUNNO") == 0 ?
548 			       rcpt_domain : relay);
549 	    } else if (rp->snd_relay_info
550 		       && rp->snd_relay_info->error != 0) {
551 		msg_warn("%s lookup failure", rp->snd_relay_maps_name);
552 		*flags |= RESOLVE_FLAG_FAIL;
553 		FREE_MEMORY_AND_RETURN;
554 	    } else if (*RES_PARAM_VALUE(rp->relayhost))
555 		vstring_strcpy(nexthop, RES_PARAM_VALUE(rp->relayhost));
556 	    else
557 		vstring_strcpy(nexthop, rcpt_domain);
558 	}
559     }
560 
561     /*
562      * Local delivery.
563      *
564      * XXX Nag if the domain is listed in multiple domain lists. The effect is
565      * implementation defined, and may break when internals change.
566      */
567     else {
568 	if (var_helpful_warnings) {
569 	    if (virt_alias_doms
570 		&& string_list_match(virt_alias_doms, rcpt_domain))
571 		msg_warn("do not list domain %s in BOTH %s and %s",
572 			 rcpt_domain, VAR_MYDEST, VAR_VIRT_ALIAS_DOMS);
573 	    if (virt_mailbox_doms
574 		&& string_list_match(virt_mailbox_doms, rcpt_domain))
575 		msg_warn("do not list domain %s in BOTH %s and %s",
576 			 rcpt_domain, VAR_MYDEST, VAR_VIRT_MAILBOX_DOMS);
577 	}
578 	vstring_strcpy(channel, RES_PARAM_VALUE(rp->local_transport));
579 	vstring_strcpy(nexthop, rcpt_domain);
580 	blame = rp->local_transport_name;
581 	*flags |= RESOLVE_CLASS_LOCAL;
582     }
583 
584     /*
585      * An explicit main.cf transport:nexthop setting overrides the nexthop.
586      *
587      * XXX We depend on this mechanism to enforce per-recipient concurrencies
588      * for local recipients. With "local_transport = local:$myhostname" we
589      * force mail for any domain in $mydestination/${proxy,inet}_interfaces
590      * to share the same queue.
591      */
592     if ((destination = split_at(STR(channel), ':')) != 0 && *destination)
593 	vstring_strcpy(nexthop, destination);
594 
595     /*
596      * Sanity checks.
597      */
598     if (*STR(channel) == 0) {
599 	if (blame == 0)
600 	    msg_panic("%s: null blame", myname);
601 	msg_warn("file %s/%s: parameter %s: null transport is not allowed",
602 		 var_config_dir, MAIN_CONF_FILE, blame);
603 	*flags |= RESOLVE_FLAG_FAIL;
604 	FREE_MEMORY_AND_RETURN;
605     }
606     if (*STR(nexthop) == 0)
607 	msg_panic("%s: null nexthop", myname);
608 
609     /*
610      * The transport map can selectively override any transport and/or
611      * nexthop host info that is set up above. Unfortunately, the syntax for
612      * nexthop information is transport specific. We therefore need sane and
613      * intuitive semantics for transport map entries that specify a channel
614      * but no nexthop.
615      *
616      * With non-error transports, the initial nexthop information is the
617      * recipient domain. However, specific main.cf transport definitions may
618      * specify a transport-specific destination, such as a host + TCP socket,
619      * or the pathname of a UNIX-domain socket. With less precedence than
620      * main.cf transport definitions, a main.cf relayhost definition may also
621      * override nexthop information for off-host deliveries.
622      *
623      * With the error transport, the nexthop information is free text that
624      * specifies the reason for non-delivery.
625      *
626      * Because nexthop syntax is transport specific we reset the nexthop
627      * information to the recipient domain when the transport table specifies
628      * a transport without also specifying the nexthop information.
629      *
630      * Subtle note: reset nexthop even when the transport table does not change
631      * the transport. Otherwise it is hard to get rid of main.cf specified
632      * nexthop information.
633      *
634      * XXX Don't override the virtual alias class (error:User unknown) result.
635      */
636     if (rp->transport_info && !(*flags & RESOLVE_CLASS_ALIAS)) {
637 	if (transport_lookup(rp->transport_info, STR(nextrcpt),
638 			     rcpt_domain, channel, nexthop) == 0
639 	    && rp->transport_info->transport_path->error != 0) {
640 	    msg_warn("%s lookup failure", rp->transport_maps_name);
641 	    *flags |= RESOLVE_FLAG_FAIL;
642 	    FREE_MEMORY_AND_RETURN;
643 	}
644     }
645 
646     /*
647      * Bounce recipients that have moved, regardless of domain address class.
648      * We do this last, in anticipation of transport maps that can override
649      * the recipient address.
650      *
651      * The downside of not doing this in delivery agents is that this table has
652      * no effect on local alias expansion results. Such mail will have to
653      * make almost an entire iteration through the mail system.
654      */
655 #define IGNORE_ADDR_EXTENSION   ((char **) 0)
656 
657     if (relocated_maps != 0) {
658 	const char *newloc;
659 
660 	if ((newloc = mail_addr_find(relocated_maps, STR(nextrcpt),
661 				     IGNORE_ADDR_EXTENSION)) != 0) {
662 	    vstring_strcpy(channel, MAIL_SERVICE_ERROR);
663 	    /* 5.1.6 is the closest match, but not perfect. */
664 	    vstring_sprintf(nexthop, "5.1.6 User has moved to %s", newloc);
665 	} else if (relocated_maps->error != 0) {
666 	    msg_warn("%s lookup failure", VAR_RELOCATED_MAPS);
667 	    *flags |= RESOLVE_FLAG_FAIL;
668 	    FREE_MEMORY_AND_RETURN;
669 	}
670     }
671 
672     /*
673      * Bounce recipient addresses that start with `-'. External commands may
674      * misinterpret such addresses as command-line options.
675      *
676      * In theory I could say people should always carefully set up their
677      * master.cf pipe mailer entries with `--' before the first non-option
678      * argument, but mistakes will happen regardless.
679      *
680      * Therefore the protection is put in place here, where it cannot be
681      * bypassed.
682      */
683     if (var_allow_min_user == 0 && STR(nextrcpt)[0] == '-') {
684 	*flags |= RESOLVE_FLAG_ERROR;
685 	FREE_MEMORY_AND_RETURN;
686     }
687 
688     /*
689      * Clean up.
690      */
691     FREE_MEMORY_AND_RETURN;
692 }
693 
694 /* Static, so they can be used by the network protocol interface only. */
695 
696 static VSTRING *channel;
697 static VSTRING *nexthop;
698 static VSTRING *nextrcpt;
699 static VSTRING *query;
700 static VSTRING *sender;
701 
702 /* resolve_proto - read request and send reply */
703 
704 int     resolve_proto(RES_CONTEXT *context, VSTREAM *stream)
705 {
706     int     flags;
707 
708     if (attr_scan(stream, ATTR_FLAG_STRICT,
709 		  ATTR_TYPE_STR, MAIL_ATTR_SENDER, sender,
710 		  ATTR_TYPE_STR, MAIL_ATTR_ADDR, query,
711 		  ATTR_TYPE_END) != 2)
712 	return (-1);
713 
714     resolve_addr(context, STR(sender), STR(query),
715 		 channel, nexthop, nextrcpt, &flags);
716 
717     if (msg_verbose)
718 	msg_info("`%s' -> `%s' -> (`%s' `%s' `%s' `%d')",
719 		 STR(sender), STR(query), STR(channel),
720 		 STR(nexthop), STR(nextrcpt), flags);
721 
722     attr_print(stream, ATTR_FLAG_NONE,
723 	       ATTR_TYPE_INT, MAIL_ATTR_FLAGS, server_flags,
724 	       ATTR_TYPE_STR, MAIL_ATTR_TRANSPORT, STR(channel),
725 	       ATTR_TYPE_STR, MAIL_ATTR_NEXTHOP, STR(nexthop),
726 	       ATTR_TYPE_STR, MAIL_ATTR_RECIP, STR(nextrcpt),
727 	       ATTR_TYPE_INT, MAIL_ATTR_FLAGS, flags,
728 	       ATTR_TYPE_END);
729 
730     if (vstream_fflush(stream) != 0) {
731 	msg_warn("write resolver reply: %m");
732 	return (-1);
733     }
734     return (0);
735 }
736 
737 /* resolve_init - module initializations */
738 
739 void    resolve_init(void)
740 {
741     sender = vstring_alloc(100);
742     query = vstring_alloc(100);
743     channel = vstring_alloc(100);
744     nexthop = vstring_alloc(100);
745     nextrcpt = vstring_alloc(100);
746 
747     if (*var_virt_alias_doms)
748 	virt_alias_doms =
749 	    string_list_init(MATCH_FLAG_RETURN, var_virt_alias_doms);
750 
751     if (*var_virt_mailbox_doms)
752 	virt_mailbox_doms =
753 	    string_list_init(MATCH_FLAG_RETURN, var_virt_mailbox_doms);
754 
755     if (*var_relay_domains)
756 	relay_domains =
757 	    domain_list_init(MATCH_FLAG_RETURN
758 			     | match_parent_style(VAR_RELAY_DOMAINS),
759 			     var_relay_domains);
760 
761     if (*var_relocated_maps)
762 	relocated_maps =
763 	    maps_create(VAR_RELOCATED_MAPS, var_relocated_maps,
764 			DICT_FLAG_LOCK | DICT_FLAG_FOLD_FIX);
765 }
766