Allow port setting on lists of hosts in manualroute, queryprogram,
[exim.git] / src / src / host.c
1 /* $Cambridge: exim/src/src/host.c,v 1.12 2005/08/09 13:31:52 ph10 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2005 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10 /* Functions for finding hosts, either by gethostbyname(), gethostbyaddr(), or
11 directly via the DNS. When IPv6 is supported, getipnodebyname() and
12 getipnodebyaddr() may be used instead of gethostbyname() and gethostbyaddr(),
13 if the newer functions are available. This module also contains various other
14 functions concerned with hosts and addresses, and a random number function,
15 used for randomizing hosts with equal MXs but available for use in other parts
16 of Exim. */
17
18
19 #include "exim.h"
20
21
22 /* Static variable for preserving the list of interface addresses in case it is
23 used more than once. */
24
25 static ip_address_item *local_interface_data = NULL;
26
27
28 #ifdef USE_INET_NTOA_FIX
29 /*************************************************
30 *         Replacement for broken inet_ntoa()     *
31 *************************************************/
32
33 /* On IRIX systems, gcc uses a different structure passing convention to the
34 native libraries. This causes inet_ntoa() to always yield 0.0.0.0 or
35 255.255.255.255. To get round this, we provide a private version of the
36 function here. It is used only if USE_INET_NTOA_FIX is set, which should happen
37 only when gcc is in use on an IRIX system. Code send to me by J.T. Breitner,
38 with these comments:
39
40   code by Stuart Levy
41   as seen in comp.sys.sgi.admin
42
43 August 2005: Apparently this is also needed for AIX systems; USE_INET_NTOA_FIX
44 should now be set for them as well.
45
46 Arguments:  sa  an in_addr structure
47 Returns:        pointer to static text string
48 */
49
50 char *
51 inet_ntoa(struct in_addr sa)
52 {
53 static uschar addr[20];
54 sprintf(addr, "%d.%d.%d.%d",
55         (US &sa.s_addr)[0],
56         (US &sa.s_addr)[1],
57         (US &sa.s_addr)[2],
58         (US &sa.s_addr)[3]);
59   return addr;
60 }
61 #endif
62
63
64
65 /*************************************************
66 *              Random number generator           *
67 *************************************************/
68
69 /* This is a simple pseudo-random number generator. It does not have to be
70 very good for the uses to which it is put. When running the regression tests,
71 start with a fixed seed.
72
73 Arguments:
74   limit:    one more than the largest number required
75
76 Returns:    a pseudo-random number in the range 0 to limit-1
77 */
78
79 int
80 random_number(int limit)
81 {
82 if (random_seed == 0)
83   {
84   if (running_in_test_harness) random_seed = 42; else
85     {
86     int p = (int)getpid();
87     random_seed = (int)time(NULL) ^ ((p << 16) | p);
88     }
89   }
90 random_seed = 1103515245 * random_seed + 12345;
91 return (unsigned int)(random_seed >> 16) % limit;
92 }
93
94
95
96 /*************************************************
97 *         Sort addresses when testing            *
98 *************************************************/
99
100 /* This function is called only when running in the test harness. It sorts a
101 number of multihomed host IP addresses into the order, so as to get
102 repeatability. This doesn't have to be efficient. But don't interchange IPv4
103 and IPv6 addresses!
104
105 Arguments:
106   host        -> the first host item
107   last        -> the last host item
108
109 Returns:      nothing
110 */
111
112 static void
113 sort_addresses(host_item *host, host_item *last)
114 {
115 BOOL done = FALSE;
116 while (!done)
117   {
118   host_item *h;
119   done = TRUE;
120   for (h = host; h != last; h = h->next)
121     {
122     if ((Ustrchr(h->address, ':') == NULL) !=
123         (Ustrchr(h->next->address, ':') == NULL))
124       continue;
125     if (Ustrcmp(h->address, h->next->address) > 0)
126       {
127       uschar *temp = h->address;
128       h->address = h->next->address;
129       h->next->address = temp;
130       done = FALSE;
131       }
132     }
133   }
134 }
135
136
137
138 /*************************************************
139 *       Build chain of host items from list      *
140 *************************************************/
141
142 /* This function builds a chain of host items from a textual list of host
143 names. It does not do any lookups. If randomize is true, the chain is build in
144 a randomized order. There may be multiple groups of independently randomized
145 hosts; they are delimited by a host name consisting of just "+".
146
147 Arguments:
148   anchor      anchor for the chain
149   list        text list
150   randomize   TRUE for randomizing
151
152 Returns:      nothing
153 */
154
155 void
156 host_build_hostlist(host_item **anchor, uschar *list, BOOL randomize)
157 {
158 int sep = 0;
159 int fake_mx = MX_NONE;          /* This value is actually -1 */
160 uschar *name;
161 uschar buffer[1024];
162
163 if (list == NULL) return;
164 if (randomize) fake_mx--;       /* Start at -2 for randomizing */
165
166 *anchor = NULL;
167
168 while ((name = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
169   {
170   host_item *h;
171
172   if (name[0] == '+' && name[1] == 0)   /* "+" delimits a randomized group */
173     {                                   /* ignore if not randomizing */
174     if (randomize) fake_mx--;
175     continue;
176     }
177
178   h = store_get(sizeof(host_item));
179   h->name = string_copy(name);
180   h->address = NULL;
181   h->port = PORT_NONE;
182   h->mx = fake_mx;
183   h->sort_key = randomize? (-fake_mx)*1000 + random_number(1000) : 0;
184   h->status = hstatus_unknown;
185   h->why = hwhy_unknown;
186   h->last_try = 0;
187
188   if (*anchor == NULL)
189     {
190     h->next = NULL;
191     *anchor = h;
192     }
193   else
194     {
195     host_item *hh = *anchor;
196     if (h->sort_key < hh->sort_key)
197       {
198       h->next = hh;
199       *anchor = h;
200       }
201     else
202       {
203       while (hh->next != NULL && h->sort_key >= (hh->next)->sort_key)
204         hh = hh->next;
205       h->next = hh->next;
206       hh->next = h;
207       }
208     }
209   }
210 }
211
212
213
214
215
216 /*************************************************
217 *        Extract port from address string        *
218 *************************************************/
219
220 /* In the spool file, and in the -oMa and -oMi options, a host plus port is
221 given as an IP address followed by a dot and a port number. This function
222 decodes this.
223
224 An alternative format for the -oMa and -oMi options is [ip address]:port which
225 is what Exim 4 uses for output, because it seems to becoming commonly used,
226 whereas the dot form confuses some programs/people. So we recognize that form
227 too.
228
229 Argument:
230   address    points to the string; if there is a port, the '.' in the string
231              is overwritten with zero to terminate the address; if the string
232              is in the [xxx]:ppp format, the address is shifted left and the
233              brackets are removed
234
235 Returns:     0 if there is no port, else the port number. If there's a syntax
236              error, leave the incoming address alone, and return 0.
237 */
238
239 int
240 host_address_extract_port(uschar *address)
241 {
242 int port = 0;
243 uschar *endptr;
244
245 /* Handle the "bracketed with colon on the end" format */
246
247 if (*address == '[')
248   {
249   uschar *rb = address + 1;
250   while (*rb != 0 && *rb != ']') rb++;
251   if (*rb++ == 0) return 0;        /* Missing ]; leave invalid address */
252   if (*rb == ':')
253     {
254     port = Ustrtol(rb + 1, &endptr, 10);
255     if (*endptr != 0) return 0;    /* Invalid port; leave invalid address */
256     }
257   else if (*rb != 0) return 0;     /* Bad syntax; leave invalid address */
258   memmove(address, address + 1, rb - address - 2);
259   rb[-2] = 0;
260   }
261
262 /* Handle the "dot on the end" format */
263
264 else
265   {
266   int skip = -3;                   /* Skip 3 dots in IPv4 addresses */
267   address--;
268   while (*(++address) != 0)
269     {
270     int ch = *address;
271     if (ch == ':') skip = 0;       /* Skip 0 dots in IPv6 addresses */
272       else if (ch == '.' && skip++ >= 0) break;
273     }
274   if (*address == 0) return 0;
275   port = Ustrtol(address + 1, &endptr, 10);
276   if (*endptr != 0) return 0;      /* Invalid port; leave invalid address */
277   *address = 0;
278   }
279
280 return port;
281 }
282
283
284 /*************************************************
285 *         Get port from a host item's name       *
286 *************************************************/
287
288 /* This function is called when finding the IP address for a host that is in a
289 list of hosts explicitly configured, such as in the manualroute router, or in a
290 fallback hosts list. We see if there is a port specification at the end of the
291 host name, and if so, remove it. A minimum length of 3 is required for the
292 original name; nothing shorter is recognized as having a port.
293
294 We test for a name ending with a sequence of digits; if preceded by colon we
295 have a port if the character before the colon is ] and the name starts with [
296 or if there are no other colons in the name (i.e. it's not an IPv6 address).
297
298 Arguments:  pointer to the host item
299 Returns:    a port number or PORT_NONE
300 */
301
302 int
303 host_item_get_port(host_item *h)
304 {
305 uschar *p;
306 int port, x;
307 int len = Ustrlen(h->name);
308
309 if (len < 3 || (p = h->name + len - 1, !isdigit(*p))) return PORT_NONE;
310
311 /* Extract potential port number */
312
313 port = *p-- - '0';
314 x = 10;
315
316 while (p > h->name + 1 && isdigit(*p))
317   {
318   port += (*p-- - '0') * x;
319   x *= 10;
320   }
321
322 /* The smallest value of p at this point is h->name + 1. */
323
324 if (*p != ':') return PORT_NONE;
325
326 if (p[-1] == ']' && h->name[0] == '[')
327   h->name = string_copyn(h->name + 1, p - h->name - 2);
328 else if (Ustrchr(h->name, ':') == p)
329   h->name = string_copyn(h->name, p - h->name);
330 else return PORT_NONE;
331
332 DEBUG(D_route|D_host_lookup) debug_printf("host=%s port=%d\n", h->name, port);
333 return port;
334 }
335
336
337
338 #ifndef STAND_ALONE    /* Omit when standalone testing */
339
340 /*************************************************
341 *     Build sender_fullhost and sender_rcvhost   *
342 *************************************************/
343
344 /* This function is called when sender_host_name and/or sender_helo_name
345 have been set. Or might have been set - for a local message read off the spool
346 they won't be. In that case, do nothing. Otherwise, set up the fullhost string
347 as follows:
348
349 (a) No sender_host_name or sender_helo_name: "[ip address]"
350 (b) Just sender_host_name: "host_name [ip address]"
351 (c) Just sender_helo_name: "(helo_name) [ip address]"
352 (d) The two are identical: "host_name [ip address]"
353 (e) The two are different: "host_name (helo_name) [ip address]"
354
355 If log_incoming_port is set, the sending host's port number is added to the IP
356 address.
357
358 This function also builds sender_rcvhost for use in Received: lines, whose
359 syntax is a bit different. This value also includes the RFC 1413 identity.
360 There wouldn't be two different variables if I had got all this right in the
361 first place.
362
363 Because this data may survive over more than one incoming SMTP message, it has
364 to be in permanent store.
365
366 Arguments:  none
367 Returns:    nothing
368 */
369
370 void
371 host_build_sender_fullhost(void)
372 {
373 uschar *address;
374 int old_pool = store_pool;
375
376 if (sender_host_address == NULL) return;
377
378 store_pool = POOL_PERM;
379
380 /* Set up address, with or without the port. After discussion, it seems that
381 the only format that doesn't cause trouble is [aaaa]:pppp. However, we can't
382 use this directly as the first item for Received: because it ain't an RFC 2822
383 domain. Sigh. */
384
385 address = string_sprintf("[%s]:%d", sender_host_address, sender_host_port);
386 if ((log_extra_selector & LX_incoming_port) == 0 || sender_host_port <= 0)
387   *(Ustrrchr(address, ':')) = 0;
388
389 /* Host name is not verified */
390
391 if (sender_host_name == NULL)
392   {
393   uschar *portptr = Ustrstr(address, "]:");
394   int size = 0;
395   int ptr = 0;
396   int adlen;    /* Sun compiler doesn't like ++ in initializers */
397
398   adlen = (portptr == NULL)? Ustrlen(address) : (++portptr - address);
399   sender_fullhost = (sender_helo_name == NULL)? address :
400     string_sprintf("(%s) %s", sender_helo_name, address);
401
402   sender_rcvhost = string_cat(NULL, &size, &ptr, address, adlen);
403
404   if (sender_ident != NULL || sender_helo_name != NULL || portptr != NULL)
405     {
406     int firstptr;
407     sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US" (", 2);
408     firstptr = ptr;
409
410     if (portptr != NULL)
411       sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2, US"port=",
412         portptr + 1);
413
414     if (sender_helo_name != NULL)
415       sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2,
416         (firstptr == ptr)? US"helo=" : US" helo=", sender_helo_name);
417
418     if (sender_ident != NULL)
419       sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2,
420         (firstptr == ptr)? US"ident=" : US" ident=", sender_ident);
421
422     sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US")", 1);
423     }
424
425   sender_rcvhost[ptr] = 0;   /* string_cat() always leaves room */
426
427   /* Release store, because string_cat allocated a minimum of 100 bytes that
428   are rarely completely used. */
429
430   store_reset(sender_rcvhost + ptr + 1);
431   }
432
433 /* Host name is known and verified. */
434
435 else
436   {
437   int len;
438   if (sender_helo_name == NULL ||
439       strcmpic(sender_host_name, sender_helo_name) == 0 ||
440         (sender_helo_name[0] == '[' &&
441          sender_helo_name[(len=Ustrlen(sender_helo_name))-1] == ']' &&
442          strncmpic(sender_helo_name+1, sender_host_address, len - 2) == 0))
443     {
444     sender_fullhost = string_sprintf("%s %s", sender_host_name, address);
445     sender_rcvhost = (sender_ident == NULL)?
446       string_sprintf("%s (%s)", sender_host_name, address) :
447       string_sprintf("%s (%s ident=%s)", sender_host_name, address,
448         sender_ident);
449     }
450   else
451     {
452     sender_fullhost = string_sprintf("%s (%s) %s", sender_host_name,
453       sender_helo_name, address);
454     sender_rcvhost = (sender_ident == NULL)?
455       string_sprintf("%s (%s helo=%s)", sender_host_name,
456         address, sender_helo_name) :
457       string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name,
458         address, sender_helo_name, sender_ident);
459     }
460   }
461
462 store_pool = old_pool;
463
464 DEBUG(D_host_lookup) debug_printf("sender_fullhost = %s\n", sender_fullhost);
465 DEBUG(D_host_lookup) debug_printf("sender_rcvhost = %s\n", sender_rcvhost);
466 }
467
468
469
470 /*************************************************
471 *          Build host+ident message              *
472 *************************************************/
473
474 /* Used when logging rejections and various ACL and SMTP incidents. The text
475 return depends on whether sender_fullhost and sender_ident are set or not:
476
477   no ident, no host   => U=unknown
478   no ident, host set  => H=sender_fullhost
479   ident set, no host  => U=ident
480   ident set, host set => H=sender_fullhost U=ident
481
482 Arguments:
483   useflag   TRUE if first item to be flagged (H= or U=); if there are two
484               items, the second is always flagged
485
486 Returns:    pointer to a string in big_buffer
487 */
488
489 uschar *
490 host_and_ident(BOOL useflag)
491 {
492 if (sender_fullhost == NULL)
493   {
494   (void)string_format(big_buffer, big_buffer_size, "%s%s", useflag? "U=" : "",
495      (sender_ident == NULL)? US"unknown" : sender_ident);
496   }
497 else
498   {
499   uschar *flag = useflag? US"H=" : US"";
500   uschar *iface = US"";
501   if ((log_extra_selector & LX_incoming_interface) != 0 &&
502        interface_address != NULL)
503     iface = string_sprintf(" I=[%s]:%d", interface_address, interface_port);
504   if (sender_ident == NULL)
505     (void)string_format(big_buffer, big_buffer_size, "%s%s%s",
506       flag, sender_fullhost, iface);
507   else
508     (void)string_format(big_buffer, big_buffer_size, "%s%s%s U=%s",
509       flag, sender_fullhost, iface, sender_ident);
510   }
511 return big_buffer;
512 }
513
514 #endif   /* STAND_ALONE */
515
516
517
518
519 /*************************************************
520 *         Build list of local interfaces         *
521 *************************************************/
522
523 /* This function interprets the contents of the local_interfaces or
524 extra_local_interfaces options, and creates an ip_address_item block for each
525 item on the list. There is no special interpretation of any IP addresses; in
526 particular, 0.0.0.0 and ::0 are returned without modification. If any address
527 includes a port, it is set in the block. Otherwise the port value is set to
528 zero.
529
530 Arguments:
531   list        the list
532   name        the name of the option being expanded
533
534 Returns:      a chain of ip_address_items, each containing to a textual
535               version of an IP address, and a port number (host order) or
536               zero if no port was given with the address
537 */
538
539 ip_address_item *
540 host_build_ifacelist(uschar *list, uschar *name)
541 {
542 int sep = 0;
543 uschar *s;
544 uschar buffer[64];
545 ip_address_item *yield = NULL;
546 ip_address_item *last = NULL;
547 ip_address_item *next;
548
549 while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
550   {
551   int port = host_address_extract_port(s);            /* Leaves just the IP address */
552   if (string_is_ip_address(s, NULL) == 0)
553     log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Malformed IP address \"%s\" in %s",
554       s, name);
555
556   /* This use of strcpy() is OK because we have checked that s is a valid IP
557   address above. The field in the ip_address_item is large enough to hold an
558   IPv6 address. */
559
560   next = store_get(sizeof(ip_address_item));
561   next->next = NULL;
562   Ustrcpy(next->address, s);
563   next->port = port;
564   next->v6_include_v4 = FALSE;
565
566   if (yield == NULL) yield = last = next; else
567     {
568     last->next = next;
569     last = next;
570     }
571   }
572
573 return yield;
574 }
575
576
577
578
579
580 /*************************************************
581 *         Find addresses on local interfaces     *
582 *************************************************/
583
584 /* This function finds the addresses of local IP interfaces. These are used
585 when testing for routing to the local host. As the function may be called more
586 than once, the list is preserved in permanent store, pointed to by a static
587 variable, to save doing the work more than once per process.
588
589 The generic list of interfaces is obtained by calling host_build_ifacelist()
590 for local_interfaces and extra_local_interfaces. This list scanned to remove
591 duplicates (which may exist with different ports - not relevant here). If
592 either of the wildcard IP addresses (0.0.0.0 and ::0) are encountered, they are
593 replaced by the appropriate (IPv4 or IPv6) list of actual local interfaces,
594 obtained from os_find_running_interfaces().
595
596 Arguments:    none
597 Returns:      a chain of ip_address_items, each containing to a textual
598               version of an IP address; the port numbers are not relevant
599 */
600
601
602 /* First, a local subfunction to add an interface to a list in permanent store,
603 but only if there isn't a previous copy of that address on the list. */
604
605 static ip_address_item *
606 add_unique_interface(ip_address_item *list, ip_address_item *ipa)
607 {
608 ip_address_item *ipa2;
609 for (ipa2 = list; ipa2 != NULL; ipa2 = ipa2->next)
610   if (Ustrcmp(ipa2->address, ipa->address) == 0) return list;
611 ipa2 = store_get_perm(sizeof(ip_address_item));
612 *ipa2 = *ipa;
613 ipa2->next = list;
614 return ipa2;
615 }
616
617
618 /* This is the globally visible function */
619
620 ip_address_item *
621 host_find_interfaces(void)
622 {
623 ip_address_item *running_interfaces = NULL;
624
625 if (local_interface_data == NULL)
626   {
627   void *reset_item = store_get(0);
628   ip_address_item *dlist = host_build_ifacelist(local_interfaces,
629     US"local_interfaces");
630   ip_address_item *xlist = host_build_ifacelist(extra_local_interfaces,
631     US"extra_local_interfaces");
632   ip_address_item *ipa;
633
634   if (dlist == NULL) dlist = xlist; else
635     {
636     for (ipa = dlist; ipa->next != NULL; ipa = ipa->next);
637     ipa->next = xlist;
638     }
639
640   for (ipa = dlist; ipa != NULL; ipa = ipa->next)
641     {
642     if (Ustrcmp(ipa->address, "0.0.0.0") == 0 ||
643         Ustrcmp(ipa->address, "::0") == 0)
644       {
645       ip_address_item *ipa2;
646       BOOL ipv6 = ipa->address[0] == ':';
647       if (running_interfaces == NULL)
648         running_interfaces = os_find_running_interfaces();
649       for (ipa2 = running_interfaces; ipa2 != NULL; ipa2 = ipa2->next)
650         {
651         if ((Ustrchr(ipa2->address, ':') != NULL) == ipv6)
652           local_interface_data = add_unique_interface(local_interface_data,
653           ipa2);
654         }
655       }
656     else
657       {
658       local_interface_data = add_unique_interface(local_interface_data, ipa);
659       DEBUG(D_interface)
660         {
661         debug_printf("Configured local interface: address=%s", ipa->address);
662         if (ipa->port != 0) debug_printf(" port=%d", ipa->port);
663         debug_printf("\n");
664         }
665       }
666     }
667   store_reset(reset_item);
668   }
669
670 return local_interface_data;
671 }
672
673
674
675
676
677 /*************************************************
678 *        Convert network IP address to text      *
679 *************************************************/
680
681 /* Given an IPv4 or IPv6 address in binary, convert it to a text
682 string and return the result in a piece of new store. The address can
683 either be given directly, or passed over in a sockaddr structure. Note
684 that this isn't the converse of host_aton() because of byte ordering
685 differences. See host_nmtoa() below.
686
687 Arguments:
688   type       if < 0 then arg points to a sockaddr, else
689              either AF_INET or AF_INET6
690   arg        points to a sockaddr if type is < 0, or
691              points to an IPv4 address (32 bits), or
692              points to an IPv6 address (128 bits),
693              in both cases, in network byte order
694   buffer     if NULL, the result is returned in gotten store;
695              else points to a buffer to hold the answer
696   portptr    points to where to put the port number, if non NULL; only
697              used when type < 0
698
699 Returns:     pointer to character string
700 */
701
702 uschar *
703 host_ntoa(int type, const void *arg, uschar *buffer, int *portptr)
704 {
705 uschar *yield;
706
707 /* The new world. It is annoying that we have to fish out the address from
708 different places in the block, depending on what kind of address it is. It
709 is also a pain that inet_ntop() returns a const uschar *, whereas the IPv4
710 function inet_ntoa() returns just uschar *, and some picky compilers insist
711 on warning if one assigns a const uschar * to a uschar *. Hence the casts. */
712
713 #if HAVE_IPV6
714 uschar addr_buffer[46];
715 if (type < 0)
716   {
717   int family = ((struct sockaddr *)arg)->sa_family;
718   if (family == AF_INET6)
719     {
720     struct sockaddr_in6 *sk = (struct sockaddr_in6 *)arg;
721     yield = (uschar *)inet_ntop(family, &(sk->sin6_addr), CS addr_buffer,
722       sizeof(addr_buffer));
723     if (portptr != NULL) *portptr = ntohs(sk->sin6_port);
724     }
725   else
726     {
727     struct sockaddr_in *sk = (struct sockaddr_in *)arg;
728     yield = (uschar *)inet_ntop(family, &(sk->sin_addr), CS addr_buffer,
729       sizeof(addr_buffer));
730     if (portptr != NULL) *portptr = ntohs(sk->sin_port);
731     }
732   }
733 else
734   {
735   yield = (uschar *)inet_ntop(type, arg, CS addr_buffer, sizeof(addr_buffer));
736   }
737
738 /* If the result is a mapped IPv4 address, show it in V4 format. */
739
740 if (Ustrncmp(yield, "::ffff:", 7) == 0) yield += 7;
741
742 #else  /* HAVE_IPV6 */
743
744 /* The old world */
745
746 if (type < 0)
747   {
748   yield = US inet_ntoa(((struct sockaddr_in *)arg)->sin_addr);
749   if (portptr != NULL) *portptr = ntohs(((struct sockaddr_in *)arg)->sin_port);
750   }
751 else
752   yield = US inet_ntoa(*((struct in_addr *)arg));
753 #endif
754
755 /* If there is no buffer, put the string into some new store. */
756
757 if (buffer == NULL) return string_copy(yield);
758
759 /* Callers of this function with a non-NULL buffer must ensure that it is
760 large enough to hold an IPv6 address, namely, at least 46 bytes. That's what
761 makes this use of strcpy() OK. */
762
763 Ustrcpy(buffer, yield);
764 return buffer;
765 }
766
767
768
769
770 /*************************************************
771 *         Convert address text to binary         *
772 *************************************************/
773
774 /* Given the textual form of an IP address, convert it to binary in an
775 array of ints. IPv4 addresses occupy one int; IPv6 addresses occupy 4 ints.
776 The result has the first byte in the most significant byte of the first int. In
777 other words, the result is not in network byte order, but in host byte order.
778 As a result, this is not the converse of host_ntoa(), which expects network
779 byte order. See host_nmtoa() below.
780
781 Arguments:
782   address    points to the textual address, checked for syntax
783   bin        points to an array of 4 ints
784
785 Returns:     the number of ints used
786 */
787
788 int
789 host_aton(uschar *address, int *bin)
790 {
791 int x[4];
792 int v4offset = 0;
793
794 /* Handle IPv6 address, which may end with an IPv4 address. It may also end
795 with a "scope", introduced by a percent sign. This code is NOT enclosed in #if
796 HAVE_IPV6 in order that IPv6 addresses are recognized even if IPv6 is not
797 supported. */
798
799 if (Ustrchr(address, ':') != NULL)
800   {
801   uschar *p = address;
802   uschar *component[8];
803   BOOL ipv4_ends = FALSE;
804   int ci = 0;
805   int nulloffset = 0;
806   int v6count = 8;
807   int i;
808
809   /* If the address starts with a colon, it will start with two colons.
810   Just lose the first one, which will leave a null first component. */
811
812   if (*p == ':') p++;
813
814   /* Split the address into components separated by colons. The input address
815   is supposed to be checked for syntax. There was a case where this was
816   overlooked; to guard against that happening again, check here and crash if
817   there are too many components. */
818
819   while (*p != 0 && *p != '%')
820     {
821     int len = Ustrcspn(p, ":%");
822     if (len == 0) nulloffset = ci;
823     if (ci > 7) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
824       "Internal error: invalid IPv6 address \"%s\" passed to host_aton()",
825       address);
826     component[ci++] = p;
827     p += len;
828     if (*p == ':') p++;
829     }
830
831   /* If the final component contains a dot, it is a trailing v4 address.
832   As the syntax is known to be checked, just set up for a trailing
833   v4 address and restrict the v6 part to 6 components. */
834
835   if (Ustrchr(component[ci-1], '.') != NULL)
836     {
837     address = component[--ci];
838     ipv4_ends = TRUE;
839     v4offset = 3;
840     v6count = 6;
841     }
842
843   /* If there are fewer than 6 or 8 components, we have to insert some
844   more empty ones in the middle. */
845
846   if (ci < v6count)
847     {
848     int insert_count = v6count - ci;
849     for (i = v6count-1; i > nulloffset + insert_count; i--)
850       component[i] = component[i - insert_count];
851     while (i > nulloffset) component[i--] = US"";
852     }
853
854   /* Now turn the components into binary in pairs and bung them
855   into the vector of ints. */
856
857   for (i = 0; i < v6count; i += 2)
858     bin[i/2] = (Ustrtol(component[i], NULL, 16) << 16) +
859       Ustrtol(component[i+1], NULL, 16);
860
861   /* If there was no terminating v4 component, we are done. */
862
863   if (!ipv4_ends) return 4;
864   }
865
866 /* Handle IPv4 address */
867
868 (void)sscanf(CS address, "%d.%d.%d.%d", x, x+1, x+2, x+3);
869 bin[v4offset] = (x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3];
870 return v4offset+1;
871 }
872
873
874 /*************************************************
875 *           Apply mask to an IP address          *
876 *************************************************/
877
878 /* Mask an address held in 1 or 4 ints, with the ms bit in the ms bit of the
879 first int, etc.
880
881 Arguments:
882   count        the number of ints
883   binary       points to the ints to be masked
884   mask         the count of ms bits to leave, or -1 if no masking
885
886 Returns:       nothing
887 */
888
889 void
890 host_mask(int count, int *binary, int mask)
891 {
892 int i;
893 if (mask < 0) mask = 99999;
894 for (i = 0; i < count; i++)
895   {
896   int wordmask;
897   if (mask == 0) wordmask = 0;
898   else if (mask < 32)
899     {
900     wordmask = (-1) << (32 - mask);
901     mask = 0;
902     }
903   else
904     {
905     wordmask = -1;
906     mask -= 32;
907     }
908   binary[i] &= wordmask;
909   }
910 }
911
912
913
914
915 /*************************************************
916 *     Convert masked IP address in ints to text  *
917 *************************************************/
918
919 /* We can't use host_ntoa() because it assumes the binary values are in network
920 byte order, and these are the result of host_aton(), which puts them in ints in
921 host byte order. Also, we really want IPv6 addresses to be in a canonical
922 format, so we output them with no abbreviation. In a number of cases we can't
923 use the normal colon separator in them because it terminates keys in lsearch
924 files, so we want to use dot instead. There's an argument that specifies what
925 to use for IPv6 addresses.
926
927 Arguments:
928   count       1 or 4 (number of ints)
929   binary      points to the ints
930   mask        mask value; if < 0 don't add to result
931   buffer      big enough to hold the result
932   sep         component separator character for IPv6 addresses
933
934 Returns:      the number of characters placed in buffer, not counting
935               the final nul.
936 */
937
938 int
939 host_nmtoa(int count, int *binary, int mask, uschar *buffer, int sep)
940 {
941 int i, j;
942 uschar *tt = buffer;
943
944 if (count == 1)
945   {
946   j = binary[0];
947   for (i = 24; i >= 0; i -= 8)
948     {
949     sprintf(CS tt, "%d.", (j >> i) & 255);
950     while (*tt) tt++;
951     }
952   }
953 else
954   {
955   for (i = 0; i < 4; i++)
956     {
957     j = binary[i];
958     sprintf(CS tt, "%04x%c%04x%c", (j >> 16) & 0xffff, sep, j & 0xffff, sep);
959     while (*tt) tt++;
960     }
961   }
962
963 tt--;   /* lose final separator */
964
965 if (mask < 0)
966   *tt = 0;
967 else
968   {
969   sprintf(CS tt, "/%d", mask);
970   while (*tt) tt++;
971   }
972
973 return tt - buffer;
974 }
975
976
977
978 /*************************************************
979 *        Check port for tls_on_connect           *
980 *************************************************/
981
982 /* This function checks whether a given incoming port is configured for tls-
983 on-connect. It is called from the daemon and from inetd handling. If the global
984 option tls_on_connect is already set, all ports operate this way. Otherwise, we
985 check the tls_on_connect_ports option for a list of ports.
986
987 Argument:  a port number
988 Returns:   TRUE or FALSE
989 */
990
991 BOOL
992 host_is_tls_on_connect_port(int port)
993 {
994 int sep = 0;
995 uschar buffer[32];
996 uschar *list = tls_on_connect_ports;
997 uschar *s;
998
999 if (tls_on_connect) return TRUE;
1000
1001 while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
1002   {
1003   uschar *end;
1004   int lport = Ustrtol(s, &end, 10);
1005   if (*end != 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "tls_on_connect_ports "
1006     "contains \"%s\", which is not a port number: exim abandoned", s);
1007   if (lport == port) return TRUE;
1008   }
1009
1010 return FALSE;
1011 }
1012
1013
1014
1015 /*************************************************
1016 *        Check whether host is in a network      *
1017 *************************************************/
1018
1019 /* This function checks whether a given IP address matches a pattern that
1020 represents either a single host, or a network (using CIDR notation). The caller
1021 of this function must check the syntax of the arguments before calling it.
1022
1023 Arguments:
1024   host        string representation of the ip-address to check
1025   net         string representation of the network, with optional CIDR mask
1026   maskoffset  offset to the / that introduces the mask in the key
1027               zero if there is no mask
1028
1029 Returns:
1030   TRUE   the host is inside the network
1031   FALSE  the host is NOT inside the network
1032 */
1033
1034 BOOL
1035 host_is_in_net(uschar *host, uschar *net, int maskoffset)
1036 {
1037 int i;
1038 int address[4];
1039 int incoming[4];
1040 int mlen;
1041 int size = host_aton(net, address);
1042 int insize;
1043
1044 /* No mask => all bits to be checked */
1045
1046 if (maskoffset == 0) mlen = 99999;    /* Big number */
1047   else mlen = Uatoi(net + maskoffset + 1);
1048
1049 /* Convert the incoming address to binary. */
1050
1051 insize = host_aton(host, incoming);
1052
1053 /* Convert IPv4 addresses given in IPv6 compatible mode, which represent
1054    connections from IPv4 hosts to IPv6 hosts, that is, addresses of the form
1055    ::ffff:<v4address>, to IPv4 format. */
1056
1057 if (insize == 4 && incoming[0] == 0 && incoming[1] == 0 &&
1058     incoming[2] == 0xffff)
1059   {
1060   insize = 1;
1061   incoming[0] = incoming[3];
1062   }
1063
1064 /* No match if the sizes don't agree. */
1065
1066 if (insize != size) return FALSE;
1067
1068 /* Else do the masked comparison. */
1069
1070 for (i = 0; i < size; i++)
1071   {
1072   int mask;
1073   if (mlen == 0) mask = 0;
1074   else if (mlen < 32)
1075     {
1076     mask = (-1) << (32 - mlen);
1077     mlen = 0;
1078     }
1079   else
1080     {
1081     mask = -1;
1082     mlen -= 32;
1083     }
1084   if ((incoming[i] & mask) != (address[i] & mask)) return FALSE;
1085   }
1086
1087 return TRUE;
1088 }
1089
1090
1091
1092 /*************************************************
1093 *       Scan host list for local hosts           *
1094 *************************************************/
1095
1096 /* Scan through a chain of addresses and check whether any of them is the
1097 address of an interface on the local machine. If so, remove that address and
1098 any previous ones with the same MX value, and all subsequent ones (which will
1099 have greater or equal MX values) from the chain. Note: marking them as unusable
1100 is NOT the right thing to do because it causes the hosts not to be used for
1101 other domains, for which they may well be correct.
1102
1103 The hosts may be part of a longer chain; we only process those between the
1104 initial pointer and the "last" pointer.
1105
1106 There is also a list of "pseudo-local" host names which are checked against the
1107 host names. Any match causes that host item to be treated the same as one which
1108 matches a local IP address.
1109
1110 If the very first host is a local host, then all MX records had a precedence
1111 greater than or equal to that of the local host. Either there's a problem in
1112 the DNS, or an apparently remote name turned out to be an abbreviation for the
1113 local host. Give a specific return code, and let the caller decide what to do.
1114 Otherwise, give a success code if at least one host address has been found.
1115
1116 Arguments:
1117   host        pointer to the first host in the chain
1118   lastptr     pointer to pointer to the last host in the chain (may be updated)
1119   removed     if not NULL, set TRUE if some local addresses were removed
1120                 from the list
1121
1122 Returns:
1123   HOST_FOUND       if there is at least one host with an IP address on the chain
1124                      and an MX value less than any MX value associated with the
1125                      local host
1126   HOST_FOUND_LOCAL if a local host is among the lowest-numbered MX hosts; when
1127                      the host addresses were obtained from A records or
1128                      gethostbyname(), the MX values are set to -1.
1129   HOST_FIND_FAILED if no valid hosts with set IP addresses were found
1130 */
1131
1132 int
1133 host_scan_for_local_hosts(host_item *host, host_item **lastptr, BOOL *removed)
1134 {
1135 int yield = HOST_FIND_FAILED;
1136 host_item *last = *lastptr;
1137 host_item *prev = NULL;
1138 host_item *h;
1139
1140 if (removed != NULL) *removed = FALSE;
1141
1142 if (local_interface_data == NULL) local_interface_data = host_find_interfaces();
1143
1144 for (h = host; h != last->next; h = h->next)
1145   {
1146   #ifndef STAND_ALONE
1147   if (hosts_treat_as_local != NULL)
1148     {
1149     int rc;
1150     uschar *save = deliver_domain;
1151     deliver_domain = h->name;   /* set $domain */
1152     rc = match_isinlist(string_copylc(h->name), &hosts_treat_as_local, 0,
1153       &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL);
1154     deliver_domain = save;
1155     if (rc == OK) goto FOUND_LOCAL;
1156     }
1157   #endif
1158
1159   /* It seems that on many operating systems, 0.0.0.0 is treated as a synonym
1160   for 127.0.0.1 and refers to the local host. We therefore force it always to
1161   be treated as local. */
1162
1163   if (h->address != NULL)
1164     {
1165     ip_address_item *ip;
1166     if (Ustrcmp(h->address, "0.0.0.0") == 0) goto FOUND_LOCAL;
1167     for (ip = local_interface_data; ip != NULL; ip = ip->next)
1168       if (Ustrcmp(h->address, ip->address) == 0) goto FOUND_LOCAL;
1169     yield = HOST_FOUND;  /* At least one remote address has been found */
1170     }
1171
1172   /* Update prev to point to the last host item before any that have
1173   the same MX value as the one we have just considered. */
1174
1175   if (h->next == NULL || h->next->mx != h->mx) prev = h;
1176   }
1177
1178 return yield;  /* No local hosts found: return HOST_FOUND or HOST_FIND_FAILED */
1179
1180 /* A host whose IP address matches a local IP address, or whose name matches
1181 something in hosts_treat_as_local has been found. */
1182
1183 FOUND_LOCAL:
1184
1185 if (prev == NULL)
1186   {
1187   HDEBUG(D_host_lookup) debug_printf((h->mx >= 0)?
1188     "local host has lowest MX\n" :
1189     "local host found for non-MX address\n");
1190   return HOST_FOUND_LOCAL;
1191   }
1192
1193 HDEBUG(D_host_lookup)
1194   {
1195   debug_printf("local host in host list - removed hosts:\n");
1196   for (h = prev->next; h != last->next; h = h->next)
1197     debug_printf("  %s %s %d\n", h->name, h->address, h->mx);
1198   }
1199
1200 if (removed != NULL) *removed = TRUE;
1201 prev->next = last->next;
1202 *lastptr = prev;
1203 return yield;
1204 }
1205
1206
1207
1208
1209 /*************************************************
1210 *        Remove duplicate IPs in host list       *
1211 *************************************************/
1212
1213 /* You would think that administrators could set up their DNS records so that
1214 one ended up with a list of unique IP addresses after looking up A or MX
1215 records, but apparently duplication is common. So we scan such lists and
1216 remove the later duplicates. Note that we may get lists in which some host
1217 addresses are not set.
1218
1219 Arguments:
1220   host        pointer to the first host in the chain
1221   lastptr     pointer to pointer to the last host in the chain (may be updated)
1222
1223 Returns:      nothing
1224 */
1225
1226 static void
1227 host_remove_duplicates(host_item *host, host_item **lastptr)
1228 {
1229 while (host != *lastptr)
1230   {
1231   if (host->address != NULL)
1232     {
1233     host_item *h = host;
1234     while (h != *lastptr)
1235       {
1236       if (h->next->address != NULL &&
1237           Ustrcmp(h->next->address, host->address) == 0)
1238         {
1239         DEBUG(D_host_lookup) debug_printf("duplicate IP address %s (MX=%d) "
1240           "removed\n", host->address, h->next->mx);
1241         if (h->next == *lastptr) *lastptr = h;
1242         h->next = h->next->next;
1243         }
1244       else h = h->next;
1245       }
1246     }
1247   /* If the last item was removed, host may have become == *lastptr */
1248   if (host != *lastptr) host = host->next;
1249   }
1250 }
1251
1252
1253
1254
1255 /*************************************************
1256 *    Find sender host name by gethostbyaddr()    *
1257 *************************************************/
1258
1259 /* This used to be the only way it was done, but it turns out that not all
1260 systems give aliases for calls to gethostbyaddr() - or one of the modern
1261 equivalents like getipnodebyaddr(). Fortunately, multiple PTR records are rare,
1262 but they can still exist. This function is now used only when a DNS lookup of
1263 the IP address fails, in order to give access to /etc/hosts.
1264
1265 Arguments:   none
1266 Returns:     OK, DEFER, FAIL
1267 */
1268
1269 static int
1270 host_name_lookup_byaddr(void)
1271 {
1272 int len;
1273 uschar *s, *t;
1274 struct hostent *hosts;
1275 struct in_addr addr;
1276
1277 /* Lookup on IPv6 system */
1278
1279 #if HAVE_IPV6
1280 if (Ustrchr(sender_host_address, ':') != NULL)
1281   {
1282   struct in6_addr addr6;
1283   if (inet_pton(AF_INET6, CS sender_host_address, &addr6) != 1)
1284     log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
1285       "IPv6 address", sender_host_address);
1286   #if HAVE_GETIPNODEBYADDR
1287   hosts = getipnodebyaddr(CS &addr6, sizeof(addr6), AF_INET6, &h_errno);
1288   #else
1289   hosts = gethostbyaddr(CS &addr6, sizeof(addr6), AF_INET6);
1290   #endif
1291   }
1292 else
1293   {
1294   if (inet_pton(AF_INET, CS sender_host_address, &addr) != 1)
1295     log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an "
1296       "IPv4 address", sender_host_address);
1297   #if HAVE_GETIPNODEBYADDR
1298   hosts = getipnodebyaddr(CS &addr, sizeof(addr), AF_INET, &h_errno);
1299   #else
1300   hosts = gethostbyaddr(CS &addr, sizeof(addr), AF_INET);
1301   #endif
1302   }
1303
1304 /* Do lookup on IPv4 system */
1305
1306 #else
1307 addr.s_addr = (S_ADDR_TYPE)inet_addr(CS sender_host_address);
1308 hosts = gethostbyaddr(CS(&addr), sizeof(addr), AF_INET);
1309 #endif
1310
1311 /* Failed to look up the host. */
1312
1313 if (hosts == NULL)
1314   {
1315   HDEBUG(D_host_lookup) debug_printf("IP address lookup failed: h_errno=%d\n",
1316     h_errno);
1317   return (h_errno == TRY_AGAIN || h_errno == NO_RECOVERY) ? DEFER : FAIL;
1318   }
1319
1320 /* It seems there are some records in the DNS that yield an empty name. We
1321 treat this as non-existent. In some operating systems, this is returned as an
1322 empty string; in others as a single dot. */
1323
1324 if (hosts->h_name[0] == 0 || hosts->h_name[0] == '.')
1325   {
1326   HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an empty name: "
1327     "treated as non-existent host name\n");
1328   return FAIL;
1329   }
1330
1331 /* Copy and lowercase the name, which is in static storage in many systems.
1332 Put it in permanent memory. */
1333
1334 s = (uschar *)hosts->h_name;
1335 len = Ustrlen(s) + 1;
1336 t = sender_host_name = store_get_perm(len);
1337 while (*s != 0) *t++ = tolower(*s++);
1338 *t = 0;
1339
1340 /* If the host has aliases, build a copy of the alias list */
1341
1342 if (hosts->h_aliases != NULL)
1343   {
1344   int count = 1;
1345   uschar **aliases, **ptr;
1346   for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++) count++;
1347   ptr = sender_host_aliases = store_get_perm(count * sizeof(uschar *));
1348   for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++)
1349     {
1350     uschar *s = *aliases;
1351     int len = Ustrlen(s) + 1;
1352     uschar *t = *ptr++ = store_get_perm(len);
1353     while (*s != 0) *t++ = tolower(*s++);
1354     *t = 0;
1355     }
1356   *ptr = NULL;
1357   }
1358
1359 return OK;
1360 }
1361
1362
1363
1364 /*************************************************
1365 *        Find host name for incoming call        *
1366 *************************************************/
1367
1368 /* Put the name in permanent store, pointed to by sender_host_name. We also set
1369 up a list of alias names, pointed to by sender_host_alias. The list is
1370 NULL-terminated. The incoming address is in sender_host_address, either in
1371 dotted-quad form for IPv4 or in colon-separated form for IPv6.
1372
1373 This function does a thorough check that the names it finds point back to the
1374 incoming IP address. Any that do not are discarded. Note that this is relied on
1375 by the ACL reverse_host_lookup check.
1376
1377 On some systems, get{host,ipnode}byaddr() appears to do this internally, but
1378 this it not universally true. Also, for release 4.30, this function was changed
1379 to do a direct DNS lookup first, by default[1], because it turns out that that
1380 is the only guaranteed way to find all the aliases on some systems. My
1381 experiments indicate that Solaris gethostbyaddr() gives the aliases for but
1382 Linux does not.
1383
1384 [1] The actual order is controlled by the host_lookup_order option.
1385
1386 Arguments:    none
1387 Returns:      OK on success, the answer being placed in the global variable
1388                 sender_host_name, with any aliases in a list hung off
1389                 sender_host_aliases
1390               FAIL if no host name can be found
1391               DEFER if a temporary error was encountered
1392
1393 The variable host_lookup_msg is set to an empty string on sucess, or to a
1394 reason for the failure otherwise, in a form suitable for tagging onto an error
1395 message, and also host_lookup_failed is set TRUE if the lookup failed. If there
1396 was a defer, host_lookup_deferred is set TRUE.
1397
1398 Any dynamically constructed string for host_lookup_msg must be in permanent
1399 store, because it might be used for several incoming messages on the same SMTP
1400 connection. */
1401
1402 int
1403 host_name_lookup(void)
1404 {
1405 int old_pool, rc;
1406 int sep = 0;
1407 uschar *hname, *save_hostname;
1408 uschar **aliases;
1409 uschar buffer[256];
1410 uschar *ordername;
1411 uschar *list = host_lookup_order;
1412 dns_record *rr;
1413 dns_answer dnsa;
1414 dns_scan dnss;
1415
1416 host_lookup_deferred = host_lookup_failed = FALSE;
1417
1418 HDEBUG(D_host_lookup)
1419   debug_printf("looking up host name for %s\n", sender_host_address);
1420
1421 /* For testing the case when a lookup does not complete, we have a special
1422 reserved IP address. */
1423
1424 if (running_in_test_harness &&
1425     Ustrcmp(sender_host_address, "99.99.99.99") == 0)
1426   {
1427   HDEBUG(D_host_lookup)
1428     debug_printf("Test harness: host name lookup returns DEFER\n");
1429   host_lookup_deferred = TRUE;
1430   return DEFER;
1431   }
1432
1433 /* Do lookups directly in the DNS or via gethostbyaddr() (or equivalent), in
1434 the order specified by the host_lookup_order option. */
1435
1436 while ((ordername = string_nextinlist(&list, &sep, buffer, sizeof(buffer)))
1437         != NULL)
1438   {
1439   if (strcmpic(ordername, US"bydns") == 0)
1440     {
1441     dns_init(FALSE, FALSE);
1442     dns_build_reverse(sender_host_address, buffer);
1443     rc = dns_lookup(&dnsa, buffer, T_PTR, NULL);
1444
1445     /* The first record we come across is used for the name; others are
1446     considered to be aliases. We have to scan twice, in order to find out the
1447     number of aliases. However, if all the names are empty, we will behave as
1448     if failure. (PTR records that yield empty names have been encountered in
1449     the DNS.) */
1450
1451     if (rc == DNS_SUCCEED)
1452       {
1453       uschar **aptr = NULL;
1454       int ssize = 264;
1455       int count = 0;
1456       int old_pool = store_pool;
1457
1458       store_pool = POOL_PERM;        /* Save names in permanent storage */
1459
1460       for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
1461            rr != NULL;
1462            rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
1463         {
1464         if (rr->type == T_PTR) count++;
1465         }
1466
1467       /* Get store for the list of aliases. For compatibility with
1468       gethostbyaddr, we make an empty list if there are none. */
1469
1470       aptr = sender_host_aliases = store_get(count * sizeof(uschar *));
1471
1472       /* Re-scan and extract the names */
1473
1474       for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
1475            rr != NULL;
1476            rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
1477         {
1478         uschar *s = NULL;
1479         if (rr->type != T_PTR) continue;
1480         s = store_get(ssize);
1481
1482         /* If an overlong response was received, the data will have been
1483         truncated and dn_expand may fail. */
1484
1485         if (dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen,
1486              (uschar *)(rr->data), (DN_EXPAND_ARG4_TYPE)(s), ssize) < 0)
1487           {
1488           log_write(0, LOG_MAIN, "host name alias list truncated for %s",
1489             sender_host_address);
1490           break;
1491           }
1492
1493         store_reset(s + Ustrlen(s) + 1);
1494         if (s[0] == 0)
1495           {
1496           HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an "
1497             "empty name: treated as non-existent host name\n");
1498           continue;
1499           }
1500         if (sender_host_name == NULL) sender_host_name = s;
1501           else *aptr++ = s;
1502         while (*s != 0) { *s = tolower(*s); s++; }
1503         }
1504
1505       *aptr = NULL;            /* End of alias list */
1506       store_pool = old_pool;   /* Reset store pool */
1507
1508       /* If we've found a names, break out of the "order" loop */
1509
1510       if (sender_host_name != NULL) break;
1511       }
1512
1513     /* If the DNS lookup deferred, we must also defer. */
1514
1515     if (rc == DNS_AGAIN)
1516       {
1517       HDEBUG(D_host_lookup)
1518         debug_printf("IP address PTR lookup gave temporary error\n");
1519       host_lookup_deferred = TRUE;
1520       return DEFER;
1521       }
1522     }
1523
1524   /* Do a lookup using gethostbyaddr() - or equivalent */
1525
1526   else if (strcmpic(ordername, US"byaddr") == 0)
1527     {
1528     HDEBUG(D_host_lookup)
1529       debug_printf("IP address lookup using gethostbyaddr()\n");
1530     rc = host_name_lookup_byaddr();
1531     if (rc == DEFER)
1532       {
1533       host_lookup_deferred = TRUE;
1534       return rc;                       /* Can't carry on */
1535       }
1536     if (rc == OK) break;               /* Found a name */
1537     }
1538   }      /* Loop for bydns/byaddr scanning */
1539
1540 /* If we have failed to find a name, return FAIL and log when required.
1541 NB host_lookup_msg must be in permanent store.  */
1542
1543 if (sender_host_name == NULL)
1544   {
1545   if (host_checking || !log_testing_mode)
1546     log_write(L_host_lookup_failed, LOG_MAIN, "no host name found for IP "
1547       "address %s", sender_host_address);
1548   host_lookup_msg = US" (failed to find host name from IP address)";
1549   host_lookup_failed = TRUE;
1550   return FAIL;
1551   }
1552
1553 /* We have a host name. If we are running in the test harness, we want the host
1554 name and its alias to appear always the same way round. There are only ever two
1555 names in these tests. If one of them contains "alias", make sure it is second;
1556 otherwise put them in alphabetical order. */
1557
1558 if (running_in_test_harness && *sender_host_aliases != NULL &&
1559     (
1560     Ustrstr(sender_host_name, "alias") != NULL ||
1561       (
1562       Ustrstr(*sender_host_aliases, "alias") == NULL &&
1563       Ustrcmp(sender_host_name, *sender_host_aliases) > 0
1564       )
1565     ))
1566   {
1567   uschar *temp = sender_host_name;
1568   sender_host_name = *sender_host_aliases;
1569   *sender_host_aliases = temp;
1570   }
1571
1572 /* Debug output what was found, after test harness swapping, for consistency */
1573
1574 HDEBUG(D_host_lookup)
1575   {
1576   uschar **aliases = sender_host_aliases;
1577   debug_printf("IP address lookup yielded %s\n", sender_host_name);
1578   while (*aliases != NULL) debug_printf("  alias %s\n", *aliases++);
1579   }
1580
1581 /* We need to verify that a forward lookup on the name we found does indeed
1582 correspond to the address. This is for security: in principle a malefactor who
1583 happened to own a reverse zone could set it to point to any names at all.
1584
1585 This code was present in versions of Exim before 3.20. At that point I took it
1586 out because I thought that gethostbyaddr() did the check anyway. It turns out
1587 that this isn't always the case, so it's coming back in at 4.01. This version
1588 is actually better, because it also checks aliases.
1589
1590 The code was made more robust at release 4.21. Prior to that, it accepted all
1591 the names if any of them had the correct IP address. Now the code checks all
1592 the names, and accepts only those that have the correct IP address. */
1593
1594 save_hostname = sender_host_name;   /* Save for error messages */
1595 aliases = sender_host_aliases;
1596 for (hname = sender_host_name; hname != NULL; hname = *aliases++)
1597   {
1598   int rc;
1599   BOOL ok = FALSE;
1600   host_item h;
1601   h.next = NULL;
1602   h.name = hname;
1603   h.mx = MX_NONE;
1604   h.address = NULL;
1605
1606   /* When called with the 5th argument FALSE, host_find_byname() won't return
1607   HOST_FOUND_LOCAL. If the incoming address is an IPv4 address expressed in
1608   IPv6 format, we must compare the IPv4 part to any IPv4 addresses. */
1609
1610   if ((rc = host_find_byname(&h, NULL, NULL, FALSE)) == HOST_FOUND)
1611     {
1612     host_item *hh;
1613     uschar *address_ipv4 = (Ustrncmp(sender_host_address, "::ffff:", 7) == 0)?
1614       sender_host_address + 7 : sender_host_address;
1615     HDEBUG(D_host_lookup) debug_printf("checking addresses for %s\n", hname);
1616     for (hh = &h; hh != NULL; hh = hh->next)
1617       {
1618       if ((Ustrcmp(hh->address, (Ustrchr(hh->address, ':') == NULL)?
1619           address_ipv4 : sender_host_address)) == 0)
1620         {
1621         HDEBUG(D_host_lookup) debug_printf("  %s OK\n", hh->address);
1622         ok = TRUE;
1623         break;
1624         }
1625       else
1626         {
1627         HDEBUG(D_host_lookup) debug_printf("  %s\n", hh->address);
1628         }
1629       }
1630     if (!ok) HDEBUG(D_host_lookup)
1631       debug_printf("no IP address for %s matched %s\n", hname,
1632         sender_host_address);
1633     }
1634   else if (rc == HOST_FIND_AGAIN)
1635     {
1636     HDEBUG(D_host_lookup) debug_printf("temporary error for host name lookup\n");
1637     host_lookup_deferred = TRUE;
1638     return DEFER;
1639     }
1640   else
1641     {
1642     HDEBUG(D_host_lookup) debug_printf("no IP addresses found for %s\n", hname);
1643     }
1644
1645   /* If this name is no good, and it's the sender name, set it null pro tem;
1646   if it's an alias, just remove it from the list. */
1647
1648   if (!ok)
1649     {
1650     if (hname == sender_host_name) sender_host_name = NULL; else
1651       {
1652       uschar **a;                              /* Don't amalgamate - some */
1653       a = --aliases;                           /* compilers grumble */
1654       while (*a != NULL) { *a = a[1]; a++; }
1655       }
1656     }
1657   }
1658
1659 /* If sender_host_name == NULL, it means we didn't like the name. Replace
1660 it with the first alias, if there is one. */
1661
1662 if (sender_host_name == NULL && *sender_host_aliases != NULL)
1663   sender_host_name = *sender_host_aliases++;
1664
1665 /* If we now have a main name, all is well. */
1666
1667 if (sender_host_name != NULL) return OK;
1668
1669 /* We have failed to find an address that matches. */
1670
1671 HDEBUG(D_host_lookup)
1672   debug_printf("%s does not match any IP address for %s\n",
1673     sender_host_address, save_hostname);
1674
1675 /* This message must be in permanent store */
1676
1677 old_pool = store_pool;
1678 store_pool = POOL_PERM;
1679 host_lookup_msg = string_sprintf(" (%s does not match any IP address for %s)",
1680   sender_host_address, save_hostname);
1681 store_pool = old_pool;
1682 host_lookup_failed = TRUE;
1683 return FAIL;
1684 }
1685
1686
1687
1688
1689 /*************************************************
1690 *    Find IP address(es) for host by name        *
1691 *************************************************/
1692
1693 /* The input is a host_item structure with the name filled in and the address
1694 field set to NULL. We use gethostbyname(). Of course, gethostbyname() may use
1695 the DNS, but it doesn't do MX processing. If more than one address is given,
1696 chain on additional host items, with other relevant fields copied.
1697
1698 The second argument provides a host list (usually an IP list) of hosts to
1699 ignore. This makes it possible to ignore IPv6 link-local addresses or loopback
1700 addresses in unreasonable places.
1701
1702 The lookup may result in a change of name. For compatibility with the dns
1703 lookup, return this via fully_qualified_name as well as updating the host item.
1704 The lookup may also yield more than one IP address, in which case chain on
1705 subsequent host_item structures.
1706
1707 Arguments:
1708   host                   a host item with the name and MX filled in;
1709                            the address is to be filled in;
1710                            multiple IP addresses cause other host items to be
1711                              chained on.
1712   ignore_target_hosts    a list of hosts to ignore
1713   fully_qualified_name   if not NULL, set to point to host name for
1714                          compatibility with host_find_bydns
1715   local_host_check       TRUE if a check for the local host is wanted
1716
1717 Returns:                 HOST_FIND_FAILED  Failed to find the host or domain
1718                          HOST_FIND_AGAIN   Try again later
1719                          HOST_FOUND        Host found - data filled in
1720                          HOST_FOUND_LOCAL  Host found and is the local host
1721 */
1722
1723 int
1724 host_find_byname(host_item *host, uschar *ignore_target_hosts,
1725   uschar **fully_qualified_name, BOOL local_host_check)
1726 {
1727 int i, yield, times;
1728 uschar **addrlist;
1729 host_item *last = NULL;
1730 BOOL temp_error = FALSE;
1731 #if HAVE_IPV6
1732 int af;
1733 #endif
1734
1735 /* If we are in the test harness, a name ending in .test.again.dns always
1736 forces a temporary error response. */
1737
1738 if (running_in_test_harness)
1739   {
1740   uschar *endname = host->name + Ustrlen(host->name);
1741   if (Ustrcmp(endname - 14, "test.again.dns") == 0)
1742     return HOST_FIND_AGAIN;
1743   }
1744
1745 /* In an IPv6 world, we need to scan for both kinds of address, so go round the
1746 loop twice. Note that we have ensured that AF_INET6 is defined even in an IPv4
1747 world, which makes for slightly tidier code. However, if dns_ipv4_lookup
1748 matches the domain, we also just do IPv4 lookups here (except when testing
1749 standalone). */
1750
1751 #if HAVE_IPV6
1752   #ifndef STAND_ALONE
1753   if (dns_ipv4_lookup != NULL &&
1754         match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN,
1755           TRUE, NULL) == OK)
1756     { af = AF_INET; times = 1; }
1757   else
1758   #endif  /* STAND_ALONE */
1759
1760     { af = AF_INET6; times = 2; }
1761
1762 /* No IPv6 support */
1763
1764 #else   /* HAVE_IPV6 */
1765   times = 1;
1766 #endif  /* HAVE_IPV6 */
1767
1768 /* Initialize the flag that gets set for DNS syntax check errors, so that the
1769 interface to this function can be similar to host_find_bydns. */
1770
1771 host_find_failed_syntax = FALSE;
1772
1773 /* Loop to look up both kinds of address in an IPv6 world */
1774
1775 for (i = 1; i <= times;
1776      #if HAVE_IPV6
1777        af = AF_INET,     /* If 2 passes, IPv4 on the second */
1778      #endif
1779      i++)
1780   {
1781   BOOL ipv4_addr;
1782   int error_num;
1783   struct hostent *hostdata;
1784
1785   #if HAVE_IPV6
1786     #if HAVE_GETIPNODEBYNAME
1787     hostdata = getipnodebyname(CS host->name, af, 0, &error_num);
1788     #else
1789     hostdata = gethostbyname2(CS host->name, af);
1790     error_num = h_errno;
1791     #endif
1792   #else
1793   hostdata = gethostbyname(CS host->name);
1794   error_num = h_errno;
1795   #endif
1796
1797   if (hostdata == NULL)
1798     {
1799     uschar *error;
1800     switch (error_num)
1801       {
1802       case HOST_NOT_FOUND: error = US"HOST_NOT_FOUND"; break;
1803       case TRY_AGAIN: error = US"TRY_AGAIN"; break;
1804       case NO_RECOVERY: error = US"NO_RECOVERY"; break;
1805       case NO_DATA: error = US"NO_DATA"; break;
1806       #if NO_DATA != NO_ADDRESS
1807       case NO_ADDRESS: error = US"NO_ADDRESS"; break;
1808       #endif
1809       default: error = US"?"; break;
1810       }
1811
1812     DEBUG(D_host_lookup) debug_printf("%s returned %d (%s)\n",
1813       #if HAVE_IPV6
1814         #if HAVE_GETIPNODEBYNAME
1815         (af == AF_INET6)? "getipnodebyname(af=inet6)" : "getipnodebyname(af=inet)",
1816         #else
1817         (af == AF_INET6)? "gethostbyname2(af=inet6)" : "gethostbyname2(af=inet)",
1818         #endif
1819       #else
1820       "gethostbyname",
1821       #endif
1822       error_num, error);
1823
1824     if (error_num == TRY_AGAIN || error_num == NO_RECOVERY) temp_error = TRUE;
1825     continue;
1826     }
1827   if ((hostdata->h_addr_list)[0] == NULL) continue;
1828
1829   /* Replace the name with the fully qualified one if necessary, and fill in
1830   the fully_qualified_name pointer. */
1831
1832   if (hostdata->h_name[0] != 0 &&
1833       Ustrcmp(host->name, hostdata->h_name) != 0)
1834     host->name = string_copy_dnsdomain((uschar *)hostdata->h_name);
1835   if (fully_qualified_name != NULL) *fully_qualified_name = host->name;
1836
1837   /* Get the list of addresses. IPv4 and IPv6 addresses can be distinguished
1838   by their different lengths. Scan the list, ignoring any that are to be
1839   ignored, and build a chain from the rest. */
1840
1841   ipv4_addr = hostdata->h_length == sizeof(struct in_addr);
1842
1843   for (addrlist = USS hostdata->h_addr_list; *addrlist != NULL; addrlist++)
1844     {
1845     uschar *text_address =
1846       host_ntoa(ipv4_addr? AF_INET:AF_INET6, *addrlist, NULL, NULL);
1847
1848     #ifndef STAND_ALONE
1849     if (ignore_target_hosts != NULL &&
1850         verify_check_this_host(&ignore_target_hosts, NULL, host->name,
1851           text_address, NULL) == OK)
1852       {
1853       DEBUG(D_host_lookup)
1854         debug_printf("ignored host %s [%s]\n", host->name, text_address);
1855       continue;
1856       }
1857     #endif
1858
1859     /* If this is the first address, last == NULL and we put the data in the
1860     original block. */
1861
1862     if (last == NULL)
1863       {
1864       host->address = text_address;
1865       host->port = PORT_NONE;
1866       host->status = hstatus_unknown;
1867       host->why = hwhy_unknown;
1868       last = host;
1869       }
1870
1871     /* Else add further host item blocks for any other addresses, keeping
1872     the order. */
1873
1874     else
1875       {
1876       host_item *next = store_get(sizeof(host_item));
1877       next->name = host->name;
1878       next->mx = host->mx;
1879       next->address = text_address;
1880       next->port = PORT_NONE;
1881       next->status = hstatus_unknown;
1882       next->why = hwhy_unknown;
1883       next->last_try = 0;
1884       next->next = last->next;
1885       last->next = next;
1886       last = next;
1887       }
1888     }
1889   }
1890
1891 /* If no hosts were found, the address field in the original host block will be
1892 NULL. If temp_error is set, at least one of the lookups gave a temporary error,
1893 so we pass that back. */
1894
1895 if (host->address == NULL)
1896   {
1897   uschar *msg =
1898     #ifndef STAND_ALONE
1899     (message_id[0] == 0 && smtp_in != NULL)?
1900       string_sprintf("no IP address found for host %s (during %s)", host->name,
1901           smtp_get_connection_info()) :
1902     #endif
1903     string_sprintf("no IP address found for host %s", host->name);
1904
1905   HDEBUG(D_host_lookup) debug_printf("%s\n", msg);
1906   if (temp_error) return HOST_FIND_AGAIN;
1907   if (host_checking || !log_testing_mode)
1908     log_write(L_host_lookup_failed, LOG_MAIN, "%s", msg);
1909   return HOST_FIND_FAILED;
1910   }
1911
1912 /* Remove any duplicate IP addresses, then check to see if this is the local
1913 host if required. */
1914
1915 host_remove_duplicates(host, &last);
1916 yield = local_host_check?
1917   host_scan_for_local_hosts(host, &last, NULL) : HOST_FOUND;
1918
1919 /* When running in the test harness, sort into the order of addresses so as to
1920 get repeatability. */
1921
1922 if (running_in_test_harness) sort_addresses(host, last);
1923
1924 HDEBUG(D_host_lookup)
1925   {
1926   host_item *h;
1927   if (fully_qualified_name != NULL)
1928     debug_printf("fully qualified name = %s\n", *fully_qualified_name);
1929   debug_printf("%s looked up these IP addresses:\n",
1930     #if HAVE_IPV6
1931       #if HAVE_GETIPNODEBYNAME
1932       "getipnodebyname"
1933       #else
1934       "gethostbyname2"
1935       #endif
1936     #else
1937     "gethostbyname"
1938     #endif
1939     );
1940   for (h = host; h != last->next; h = h->next)
1941     debug_printf("  name=%s address=%s\n", h->name,
1942       (h->address == NULL)? US"<null>" : h->address);
1943   }
1944
1945 /* Return the found status. */
1946
1947 return yield;
1948 }
1949
1950
1951
1952 /*************************************************
1953 *        Fill in a host address from the DNS     *
1954 *************************************************/
1955
1956 /* Given a host item, with its name and mx fields set, and its address field
1957 set to NULL, fill in its IP address from the DNS. If it is multi-homed, create
1958 additional host items for the additional addresses, copying all the other
1959 fields, and randomizing the order.
1960
1961 On IPv6 systems, A6 records are sought first (but only if support for A6 is
1962 configured - they may never become mainstream), then AAAA records are sought,
1963 and finally A records are sought as well.
1964
1965 The host name may be changed if the DNS returns a different name - e.g. fully
1966 qualified or changed via CNAME. If fully_qualified_name is not NULL, dns_lookup
1967 ensures that it points to the fully qualified name. However, this is the fully
1968 qualified version of the original name; if a CNAME is involved, the actual
1969 canonical host name may be different again, and so we get it directly from the
1970 relevant RR. Note that we do NOT change the mx field of the host item in this
1971 function as it may be called to set the addresses of hosts taken from MX
1972 records.
1973
1974 Arguments:
1975   host                  points to the host item we're filling in
1976   lastptr               points to pointer to last host item in a chain of
1977                           host items (may be updated if host is last and gets
1978                           extended because multihomed)
1979   ignore_target_hosts   list of hosts to ignore
1980   allow_ip              if TRUE, recognize an IP address and return it
1981   fully_qualified_name  if not NULL, return fully qualified name here if
1982                           the contents are different (i.e. it must be preset
1983                           to something)
1984
1985 Returns:       HOST_FIND_FAILED     couldn't find A record
1986                HOST_FIND_AGAIN      try again later
1987                HOST_FOUND           found AAAA and/or A record(s)
1988                HOST_IGNORED         found, but all IPs ignored
1989 */
1990
1991 static int
1992 set_address_from_dns(host_item *host, host_item **lastptr,
1993   uschar *ignore_target_hosts, BOOL allow_ip, uschar **fully_qualified_name)
1994 {
1995 dns_record *rr;
1996 host_item *thishostlast = NULL;    /* Indicates not yet filled in anything */
1997 BOOL v6_find_again = FALSE;
1998 int i;
1999
2000 /* If allow_ip is set, a name which is an IP address returns that value
2001 as its address. This is used for MX records when allow_mx_to_ip is set, for
2002 those sites that feel they have to flaunt the RFC rules. */
2003
2004 if (allow_ip && string_is_ip_address(host->name, NULL) != 0)
2005   {
2006   #ifndef STAND_ALONE
2007   if (ignore_target_hosts != NULL &&
2008         verify_check_this_host(&ignore_target_hosts, NULL, host->name,
2009         host->name, NULL) == OK)
2010     return HOST_IGNORED;
2011   #endif
2012
2013   host->address = host->name;
2014   host->port = PORT_NONE;
2015   return HOST_FOUND;
2016   }
2017
2018 /* On an IPv6 system, go round the loop up to three times, looking for A6 and
2019 AAAA records the first two times. However, unless doing standalone testing, we
2020 force an IPv4 lookup if the domain matches dns_ipv4_lookup is set. Since A6
2021 records look like being abandoned, support them only if explicitly configured
2022 to do so. On an IPv4 system, go round the loop once only, looking only for A
2023 records. */
2024
2025 #if HAVE_IPV6
2026
2027   #ifndef STAND_ALONE
2028     if (dns_ipv4_lookup != NULL &&
2029         match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN,
2030         TRUE, NULL) == OK)
2031       i = 0;    /* look up A records only */
2032     else
2033   #endif        /* STAND_ALONE */
2034
2035   #ifdef SUPPORT_A6
2036   i = 2;        /* look up A6 and AAAA and A records */
2037   #else
2038   i = 1;        /* look up AAAA and A records */
2039   #endif        /* SUPPORT_A6 */
2040
2041 /* The IPv4 world */
2042
2043 #else           /* HAVE_IPV6 */
2044   i = 0;        /* look up A records only */
2045 #endif          /* HAVE_IPV6 */
2046
2047 for (; i >= 0; i--)
2048   {
2049   static int types[] = { T_A, T_AAAA, T_A6 };
2050   int type = types[i];
2051   int randoffset = (i == 0)? 500 : 0;  /* Ensures v6 sorts before v4 */
2052   dns_answer dnsa;
2053   dns_scan dnss;
2054
2055   int rc = dns_lookup(&dnsa, host->name, type, fully_qualified_name);
2056
2057   /* We want to return HOST_FIND_AGAIN if one of the A, A6, or AAAA lookups
2058   fails or times out, but not if another one succeeds. (In the early
2059   IPv6 days there are name servers that always fail on AAAA, but are happy
2060   to give out an A record. We want to proceed with that A record.) */
2061
2062   if (rc != DNS_SUCCEED)
2063     {
2064     if (i == 0)  /* Just tried for an A record, i.e. end of loop */
2065       {
2066       if (host->address != NULL) return HOST_FOUND;  /* A6 or AAAA was found */
2067       if (rc == DNS_AGAIN || rc == DNS_FAIL || v6_find_again)
2068         return HOST_FIND_AGAIN;
2069       return HOST_FIND_FAILED;    /* DNS_NOMATCH or DNS_NODATA */
2070       }
2071
2072     /* Tried for an A6 or AAAA record: remember if this was a temporary
2073     error, and look for the next record type. */
2074
2075     if (rc != DNS_NOMATCH && rc != DNS_NODATA) v6_find_again = TRUE;
2076     continue;
2077     }
2078
2079   /* Lookup succeeded: fill in the given host item with the first non-ignored
2080   address found; create additional items for any others. A single A6 record
2081   may generate more than one address. */
2082
2083   for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
2084        rr != NULL;
2085        rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
2086     {
2087     if (rr->type == type)
2088       {
2089       /* dns_address *da = dns_address_from_rr(&dnsa, rr); */
2090
2091       dns_address *da;
2092       da = dns_address_from_rr(&dnsa, rr);
2093
2094       DEBUG(D_host_lookup)
2095         {
2096         if (da == NULL)
2097           debug_printf("no addresses extracted from A6 RR for %s\n",
2098             host->name);
2099         }
2100
2101       /* This loop runs only once for A and AAAA records, but may run
2102       several times for an A6 record that generated multiple addresses. */
2103
2104       for (; da != NULL; da = da->next)
2105         {
2106         #ifndef STAND_ALONE
2107         if (ignore_target_hosts != NULL &&
2108               verify_check_this_host(&ignore_target_hosts, NULL,
2109                 host->name, da->address, NULL) == OK)
2110           {
2111           DEBUG(D_host_lookup)
2112             debug_printf("ignored host %s [%s]\n", host->name, da->address);
2113           continue;
2114           }
2115         #endif
2116
2117         /* If this is the first address, stick it in the given host block,
2118         and change the name if the returned RR has a different name. */
2119
2120         if (thishostlast == NULL)
2121           {
2122           if (strcmpic(host->name, rr->name) != 0)
2123             host->name = string_copy_dnsdomain(rr->name);
2124           host->address = da->address;
2125           host->port = PORT_NONE;
2126           host->sort_key = host->mx * 1000 + random_number(500) + randoffset;
2127           host->status = hstatus_unknown;
2128           host->why = hwhy_unknown;
2129           thishostlast = host;
2130           }
2131
2132         /* Not the first address. Check for, and ignore, duplicates. Then
2133         insert in the chain at a random point. */
2134
2135         else
2136           {
2137           int new_sort_key;
2138           host_item *next;
2139
2140           /* End of our local chain is specified by "thishostlast". */
2141
2142           for (next = host;; next = next->next)
2143             {
2144             if (Ustrcmp(CS da->address, next->address) == 0) break;
2145             if (next == thishostlast) { next = NULL; break; }
2146             }
2147           if (next != NULL) continue;  /* With loop for next address */
2148
2149           /* Not a duplicate */
2150
2151           new_sort_key = host->mx * 1000 + random_number(500) + randoffset;
2152           next = store_get(sizeof(host_item));
2153
2154           /* New address goes first: insert the new block after the first one
2155           (so as not to disturb the original pointer) but put the new address
2156           in the original block. */
2157
2158           if (new_sort_key < host->sort_key)
2159             {
2160             *next = *host;
2161             host->next = next;
2162             host->address = da->address;
2163             host->port = PORT_NONE;
2164             host->sort_key = new_sort_key;
2165             if (thishostlast == host) thishostlast = next;  /* Local last */
2166             if (*lastptr == host) *lastptr = next;          /* Global last */
2167             }
2168
2169           /* Otherwise scan down the addresses for this host to find the
2170           one to insert after. */
2171
2172           else
2173             {
2174             host_item *h = host;
2175             while (h != thishostlast)
2176               {
2177               if (new_sort_key < h->next->sort_key) break;
2178               h = h->next;
2179               }
2180             *next = *h;
2181             h->next = next;
2182             next->address = da->address;
2183             next->port = PORT_NONE;
2184             next->sort_key = new_sort_key;
2185             if (h == thishostlast) thishostlast = next; /* Local last */
2186             if (h == *lastptr) *lastptr = next;         /* Global last */
2187             }
2188           }
2189         }
2190       }
2191     }
2192   }
2193
2194 /* Control gets here only if the third lookup (the A record) succeeded.
2195 However, the address may not be filled in if it was ignored. */
2196
2197 return (host->address == NULL)? HOST_IGNORED : HOST_FOUND;
2198 }
2199
2200
2201
2202
2203 /*************************************************
2204 *  Find IP addresses and names for host via DNS  *
2205 *************************************************/
2206
2207 /* The input is a host_item structure with the name filled in and the address
2208 field set to NULL. This may be in a chain of other host items. The lookup may
2209 result in more than one IP address, in which case we must created new host
2210 blocks for the additional addresses, and insert them into the chain. The
2211 original name may not be fully qualified. Use the fully_qualified_name argument
2212 to return the official name, as returned by the resolver.
2213
2214 Arguments:
2215   host                  point to initial host item
2216   ignore_target_hosts   a list of hosts to ignore
2217   whichrrs              flags indicating which RRs to look for:
2218                           HOST_FIND_BY_SRV  => look for SRV
2219                           HOST_FIND_BY_MX   => look for MX
2220                           HOST_FIND_BY_A    => look for A or AAAA
2221                         also flags indicating how the lookup is done
2222                           HOST_FIND_QUALIFY_SINGLE   ) passed to the
2223                           HOST_FIND_SEARCH_PARENTS   )   resolver
2224   srv_service           when SRV used, the service name
2225   srv_fail_domains      DNS errors for these domains => assume nonexist
2226   mx_fail_domains       DNS errors for these domains => assume nonexist
2227   fully_qualified_name  if not NULL, return fully-qualified name
2228   removed               set TRUE if local host was removed from the list
2229
2230 Returns:                HOST_FIND_FAILED  Failed to find the host or domain;
2231                                           if there was a syntax error,
2232                                           host_find_failed_syntax is set.
2233                         HOST_FIND_AGAIN   Could not resolve at this time
2234                         HOST_FOUND        Host found
2235                         HOST_FOUND_LOCAL  The lowest MX record points to this
2236                                           machine, if MX records were found, or
2237                                           an A record that was found contains
2238                                           an address of the local host
2239 */
2240
2241 int
2242 host_find_bydns(host_item *host, uschar *ignore_target_hosts, int whichrrs,
2243   uschar *srv_service, uschar *srv_fail_domains, uschar *mx_fail_domains,
2244   uschar **fully_qualified_name, BOOL *removed)
2245 {
2246 host_item *h, *last;
2247 dns_record *rr;
2248 int rc = DNS_FAIL;
2249 int ind_type = 0;
2250 int yield;
2251 dns_answer dnsa;
2252 dns_scan dnss;
2253
2254 /* Set the default fully qualified name to the incoming name, initialize the
2255 resolver if necessary, set up the relevant options, and initialize the flag
2256 that gets set for DNS syntax check errors. */
2257
2258 if (fully_qualified_name != NULL) *fully_qualified_name = host->name;
2259 dns_init((whichrrs & HOST_FIND_QUALIFY_SINGLE) != 0,
2260          (whichrrs & HOST_FIND_SEARCH_PARENTS) != 0);
2261 host_find_failed_syntax = FALSE;
2262
2263 /* First, if requested, look for SRV records. The service name is given; we
2264 assume TCP progocol. DNS domain names are constrained to a maximum of 256
2265 characters, so the code below should be safe. */
2266
2267 if ((whichrrs & HOST_FIND_BY_SRV) != 0)
2268   {
2269   uschar buffer[300];
2270   uschar *temp_fully_qualified_name = buffer;
2271   int prefix_length;
2272
2273   (void)sprintf(CS buffer, "_%s._tcp.%n%.256s", srv_service, &prefix_length,
2274     host->name);
2275   ind_type = T_SRV;
2276
2277   /* Search for SRV records. If the fully qualified name is different to
2278   the input name, pass back the new original domain, without the prepended
2279   magic. */
2280
2281   rc = dns_lookup(&dnsa, buffer, ind_type, &temp_fully_qualified_name);
2282   if (temp_fully_qualified_name != buffer && fully_qualified_name != NULL)
2283     *fully_qualified_name = temp_fully_qualified_name + prefix_length;
2284
2285   /* On DNS failures, we give the "try again" error unless the domain is
2286   listed as one for which we continue. */
2287
2288   if (rc == DNS_FAIL || rc == DNS_AGAIN)
2289     {
2290     if (match_isinlist(host->name, &srv_fail_domains, 0, NULL, NULL, MCL_DOMAIN,
2291         TRUE, NULL) != OK)
2292       return HOST_FIND_AGAIN;
2293     DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA "
2294       "(domain in srv_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN");
2295     }
2296   }
2297
2298 /* If we did not find any SRV records, search the DNS for MX records, if
2299 requested to do so. If the result is DNS_NOMATCH, it means there is no such
2300 domain, and there's no point in going on to look for address records with the
2301 same domain. The result will be DNS_NODATA if the domain exists but has no MX
2302 records. On DNS failures, we give the "try again" error unless the domain is
2303 listed as one for which we continue. */
2304
2305 if (rc != DNS_SUCCEED && (whichrrs & HOST_FIND_BY_MX) != 0)
2306   {
2307   ind_type = T_MX;
2308   rc = dns_lookup(&dnsa, host->name, ind_type, fully_qualified_name);
2309   if (rc == DNS_NOMATCH) return HOST_FIND_FAILED;
2310   if (rc == DNS_FAIL || rc == DNS_AGAIN)
2311     {
2312     if (match_isinlist(host->name, &mx_fail_domains, 0, NULL, NULL, MCL_DOMAIN,
2313         TRUE, NULL) != OK)
2314       return HOST_FIND_AGAIN;
2315     DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA "
2316       "(domain in mx_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN");
2317     }
2318   }
2319
2320 /* If we haven't found anything yet, and we are requested to do so, try for an
2321 A or AAAA record. If we find it (or them) check to see that it isn't the local
2322 host. */
2323
2324 if (rc != DNS_SUCCEED)
2325   {
2326   if ((whichrrs & HOST_FIND_BY_A) == 0)
2327     {
2328     DEBUG(D_host_lookup) debug_printf("Address records are not being sought\n");
2329     return HOST_FIND_FAILED;
2330     }
2331
2332   last = host;        /* End of local chainlet */
2333   host->mx = MX_NONE;
2334   host->port = PORT_NONE;
2335   rc = set_address_from_dns(host, &last, ignore_target_hosts, FALSE,
2336     fully_qualified_name);
2337
2338   /* If one or more address records have been found, check that none of them
2339   are local. Since we know the host items all have their IP addresses
2340   inserted, host_scan_for_local_hosts() can only return HOST_FOUND or
2341   HOST_FOUND_LOCAL. We do not need to scan for duplicate IP addresses here,
2342   because set_address_from_dns() removes them. */
2343
2344   if (rc == HOST_FOUND)
2345     rc = host_scan_for_local_hosts(host, &last, removed);
2346   else
2347     if (rc == HOST_IGNORED) rc = HOST_FIND_FAILED;  /* No special action */
2348
2349   /* When running in the test harness, sort into the order of addresses so as
2350   to get repeatability. */
2351
2352   if (running_in_test_harness) sort_addresses(host, last);
2353
2354   DEBUG(D_host_lookup)
2355     {
2356     host_item *h;
2357     if (host->address != NULL)
2358       {
2359       if (fully_qualified_name != NULL)
2360         debug_printf("fully qualified name = %s\n", *fully_qualified_name);
2361       for (h = host; h != last->next; h = h->next)
2362         debug_printf("%s %s mx=%d sort=%d %s\n", h->name,
2363           (h->address == NULL)? US"<null>" : h->address, h->mx, h->sort_key,
2364           (h->status >= hstatus_unusable)? US"*" : US"");
2365       }
2366     }
2367
2368   return rc;
2369   }
2370
2371 /* We have found one or more MX or SRV records. Sort them according to
2372 precedence. Put the data for the first one into the existing host block, and
2373 insert new host_item blocks into the chain for the remainder. For equal
2374 precedences one is supposed to randomize the order. To make this happen, the
2375 sorting is actually done on the MX value * 1000 + a random number. This is put
2376 into a host field called sort_key.
2377
2378 In the case of hosts with both IPv6 and IPv4 addresses, we want to choose the
2379 IPv6 address in preference. At this stage, we don't know what kind of address
2380 the host has. We choose a random number < 500; if later we find an A record
2381 first, we add 500 to the random number. Then for any other address records, we
2382 use random numbers in the range 0-499 for AAAA records and 500-999 for A
2383 records.
2384
2385 At this point we remove any duplicates that point to the same host, retaining
2386 only the one with the lowest precedence. We cannot yet check for precedence
2387 greater than that of the local host, because that test cannot be properly done
2388 until the addresses have been found - an MX record may point to a name for this
2389 host which is not the primary hostname. */
2390
2391 last = NULL;    /* Indicates that not even the first item is filled yet */
2392
2393 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
2394      rr != NULL;
2395      rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
2396   {
2397   int precedence;
2398   int weight = 0;        /* For SRV records */
2399   int port = PORT_NONE;  /* For SRV records */
2400   uschar *s;             /* MUST be unsigned for GETSHORT */
2401   uschar data[256];
2402
2403   if (rr->type != ind_type) continue;
2404   s = rr->data;
2405   GETSHORT(precedence, s);      /* Pointer s is advanced */
2406
2407   /* For MX records, we use a random "weight" which causes multiple records of
2408   the same precedence to sort randomly. */
2409
2410   if (ind_type == T_MX)
2411     {
2412     weight = random_number(500);
2413     }
2414
2415   /* SRV records are specified with a port and a weight. The weight is used
2416   in a special algorithm. However, to start with, we just use it to order the
2417   records of equal priority (precedence). */
2418
2419   else
2420     {
2421     GETSHORT(weight, s);
2422     GETSHORT(port, s);
2423     }
2424
2425   /* Get the name of the host pointed to. */
2426
2427   (void)dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, s,
2428     (DN_EXPAND_ARG4_TYPE)data, sizeof(data));
2429
2430   /* Check that we haven't already got this host on the chain; if we have,
2431   keep only the lower precedence. This situation shouldn't occur, but you
2432   never know what junk might get into the DNS (and this case has been seen on
2433   more than one occasion). */
2434
2435   if (last != NULL)       /* This is not the first record */
2436     {
2437     host_item *prev = NULL;
2438
2439     for (h = host; h != last->next; prev = h, h = h->next)
2440       {
2441       if (strcmpic(h->name, data) == 0)
2442         {
2443         DEBUG(D_host_lookup)
2444           debug_printf("discarded duplicate host %s (MX=%d)\n", data,
2445             (precedence > h->mx)? precedence : h->mx);
2446         if (precedence >= h->mx) goto NEXT_MX_RR; /* Skip greater precedence */
2447         if (h == host)                            /* Override first item */
2448           {
2449           h->mx = precedence;
2450           host->sort_key = precedence * 1000 + weight;
2451           goto NEXT_MX_RR;
2452           }
2453
2454         /* Unwanted host item is not the first in the chain, so we can get
2455         get rid of it by cutting it out. */
2456
2457         prev->next = h->next;
2458         if (h == last) last = prev;
2459         break;
2460         }
2461       }
2462     }
2463
2464   /* If this is the first MX or SRV record, put the data into the existing host
2465   block. Otherwise, add a new block in the correct place; if it has to be
2466   before the first block, copy the first block's data to a new second block. */
2467
2468   if (last == NULL)
2469     {
2470     host->name = string_copy_dnsdomain(data);
2471     host->address = NULL;
2472     host->port = port;
2473     host->mx = precedence;
2474     host->sort_key = precedence * 1000 + weight;
2475     host->status = hstatus_unknown;
2476     host->why = hwhy_unknown;
2477     last = host;
2478     }
2479
2480   /* Make a new host item and seek the correct insertion place */
2481
2482   else
2483     {
2484     int sort_key = precedence * 1000 + weight;
2485     host_item *next = store_get(sizeof(host_item));
2486     next->name = string_copy_dnsdomain(data);
2487     next->address = NULL;
2488     next->port = port;
2489     next->mx = precedence;
2490     next->sort_key = sort_key;
2491     next->status = hstatus_unknown;
2492     next->why = hwhy_unknown;
2493     next->last_try = 0;
2494
2495     /* Handle the case when we have to insert before the first item. */
2496
2497     if (sort_key < host->sort_key)
2498       {
2499       host_item htemp;
2500       htemp = *host;
2501       *host = *next;
2502       *next = htemp;
2503       host->next = next;
2504       if (last == host) last = next;
2505       }
2506
2507     /* Else scan down the items we have inserted as part of this exercise;
2508     don't go further. */
2509
2510     else
2511       {
2512       for (h = host; h != last; h = h->next)
2513         {
2514         if (sort_key < h->next->sort_key)
2515           {
2516           next->next = h->next;
2517           h->next = next;
2518           break;
2519           }
2520         }
2521
2522       /* Join on after the last host item that's part of this
2523       processing if we haven't stopped sooner. */
2524
2525       if (h == last)
2526         {
2527         next->next = last->next;
2528         last->next = next;
2529         last = next;
2530         }
2531       }
2532     }
2533
2534   NEXT_MX_RR: continue;
2535   }
2536
2537 /* If the list of hosts was obtained from SRV records, there are two things to
2538 do. First, if there is only one host, and it's name is ".", it means there is
2539 no SMTP service at this domain. Otherwise, we have to sort the hosts of equal
2540 priority according to their weights, using an algorithm that is defined in RFC
2541 2782. The hosts are currently sorted by priority and weight. For each priority
2542 group we have to pick off one host and put it first, and then repeat for any
2543 remaining in the same priority group. */
2544
2545 if (ind_type == T_SRV)
2546   {
2547   host_item **pptr;
2548
2549   if (host == last && host->name[0] == 0)
2550     {
2551     DEBUG(D_host_lookup) debug_printf("the single SRV record is \".\"\n");
2552     return HOST_FIND_FAILED;
2553     }
2554
2555   DEBUG(D_host_lookup)
2556     {
2557     debug_printf("original ordering of hosts from SRV records:\n");
2558     for (h = host; h != last->next; h = h->next)
2559       debug_printf("  %s P=%d W=%d\n", h->name, h->mx, h->sort_key % 1000);
2560     }
2561
2562   for (pptr = &host, h = host; h != last; pptr = &(h->next), h = h->next)
2563     {
2564     int sum = 0;
2565     host_item *hh;
2566
2567     /* Find the last following host that has the same precedence. At the same
2568     time, compute the sum of the weights and the running totals. These can be
2569     stored in the sort_key field. */
2570
2571     for (hh = h; hh != last; hh = hh->next)
2572       {
2573       int weight = hh->sort_key % 1000;   /* was precedence * 1000 + weight */
2574       sum += weight;
2575       hh->sort_key = sum;
2576       if (hh->mx != hh->next->mx) break;
2577       }
2578
2579     /* If there's more than one host at this precedence (priority), we need to
2580     pick one to go first. */
2581
2582     if (hh != h)
2583       {
2584       host_item *hhh;
2585       host_item **ppptr;
2586       int randomizer = random_number(sum + 1);
2587
2588       for (ppptr = pptr, hhh = h;
2589            hhh != hh;
2590            ppptr = &(hhh->next), hhh = hhh->next)
2591         {
2592         if (hhh->sort_key >= randomizer) break;
2593         }
2594
2595       /* hhh now points to the host that should go first; ppptr points to the
2596       place that points to it. Unfortunately, if the start of the minilist is
2597       the start of the entire list, we can't just swap the items over, because
2598       we must not change the value of host, since it is passed in from outside.
2599       One day, this could perhaps be changed.
2600
2601       The special case is fudged by putting the new item *second* in the chain,
2602       and then transferring the data between the first and second items. We
2603       can't just swap the first and the chosen item, because that would mean
2604       that an item with zero weight might no longer be first. */
2605
2606       if (hhh != h)
2607         {
2608         *ppptr = hhh->next;          /* Cuts it out of the chain */
2609
2610         if (h == host)
2611           {
2612           host_item temp = *h;
2613           *h = *hhh;
2614           *hhh = temp;
2615           hhh->next = temp.next;
2616           h->next = hhh;
2617           }
2618
2619         else
2620           {
2621           hhh->next = h;               /* The rest of the chain follows it */
2622           *pptr = hhh;                 /* It takes the place of h */
2623           h = hhh;                     /* It's now the start of this minilist */
2624           }
2625         }
2626       }
2627
2628     /* A host has been chosen to be first at this priority and h now points
2629     to this host. There may be others at the same priority, or others at a
2630     different priority. Before we leave this host, we need to put back a sort
2631     key of the traditional MX kind, in case this host is multihomed, because
2632     the sort key is used for ordering the multiple IP addresses. We do not need
2633     to ensure that these new sort keys actually reflect the order of the hosts,
2634     however. */
2635
2636     h->sort_key = h->mx * 1000 + random_number(500);
2637     }   /* Move on to the next host */
2638   }
2639
2640 /* Now we have to ensure addresses exist for all the hosts. We have ensured
2641 above that the names in the host items are all unique. The addresses may have
2642 been returned in the additional data section of the DNS query. Because it is
2643 more expensive to scan the returned DNS records (because you have to expand the
2644 names) we do a single scan over them, and multiple scans of the chain of host
2645 items (which is typically only 3 or 4 long anyway.) Add extra host items for
2646 multi-homed hosts. */
2647
2648 for (rr = dns_next_rr(&dnsa, &dnss, RESET_ADDITIONAL);
2649      rr != NULL;
2650      rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
2651   {
2652   dns_address *da;
2653   int status = hstatus_unknown;
2654   int why = hwhy_unknown;
2655   int randoffset;
2656
2657   if (rr->type != T_A
2658   #if HAVE_IPV6
2659     && rr->type != T_AAAA
2660     #ifdef SUPPORT_A6
2661     && rr->type != T_A6
2662     #endif
2663   #endif
2664     ) continue;
2665
2666   /* Find the first host that matches this record's name. If there isn't
2667   one, move on to the next RR. */
2668
2669   for (h = host; h != last->next; h = h->next)
2670     { if (strcmpic(h->name, rr->name) == 0) break; }
2671   if (h == last->next) continue;
2672
2673   /* For IPv4 addresses, add 500 to the random part of the sort key, to ensure
2674   they sort after IPv6 addresses. */
2675
2676   randoffset = (rr->type == T_A)? 500 : 0;
2677
2678   /* Get the list of textual addresses for this RR. There may be more than one
2679   if it is an A6 RR. Then loop to handle multiple addresses from an A6 record.
2680   If there are none, nothing will get done - the record is ignored. */
2681
2682   for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next)
2683     {
2684     /* Set status for an ignorable host. */
2685
2686     #ifndef STAND_ALONE
2687     if (ignore_target_hosts != NULL &&
2688           verify_check_this_host(&ignore_target_hosts, NULL, h->name,
2689             da->address, NULL) == OK)
2690       {
2691       DEBUG(D_host_lookup)
2692         debug_printf("ignored host %s [%s]\n", h->name, da->address);
2693       status = hstatus_unusable;
2694       why = hwhy_ignored;
2695       }
2696     #endif
2697
2698     /* If the address is already set for this host, it may be that
2699     we just have a duplicate DNS record. Alternatively, this may be
2700     a multi-homed host. Search all items with the same host name
2701     (they will all be together) and if this address is found, skip
2702     to the next RR. */
2703
2704     if (h->address != NULL)
2705       {
2706       int new_sort_key;
2707       host_item *thishostlast;
2708       host_item *hh = h;
2709
2710       do
2711         {
2712         if (hh->address != NULL && Ustrcmp(CS da->address, hh->address) == 0)
2713           goto DNS_NEXT_RR;         /* Need goto to escape from inner loop */
2714         thishostlast = hh;
2715         hh = hh->next;
2716         }
2717       while (hh != last->next && strcmpic(hh->name, rr->name) == 0);
2718
2719       /* We have a multi-homed host, since we have a new address for
2720       an existing name. Create a copy of the current item, and give it
2721       the new address. RRs can be in arbitrary order, but one is supposed
2722       to randomize the addresses of multi-homed hosts, so compute a new
2723       sorting key and do that. [Latest SMTP RFC says not to randomize multi-
2724       homed hosts, but to rely on the resolver. I'm not happy about that -
2725       caching in the resolver will not rotate as often as the name server
2726       does.] */
2727
2728       new_sort_key = h->mx * 1000 + random_number(500) + randoffset;
2729       hh = store_get(sizeof(host_item));
2730
2731       /* New address goes first: insert the new block after the first one
2732       (so as not to disturb the original pointer) but put the new address
2733       in the original block. */
2734
2735       if (new_sort_key < h->sort_key)
2736         {
2737         *hh = *h;                       /* Note: copies the port */
2738         h->next = hh;
2739         h->address = da->address;
2740         h->sort_key = new_sort_key;
2741         h->status = status;
2742         h->why = why;
2743         }
2744
2745       /* Otherwise scan down the addresses for this host to find the
2746       one to insert after. */
2747
2748       else
2749         {
2750         while (h != thishostlast)
2751           {
2752           if (new_sort_key < h->next->sort_key) break;
2753           h = h->next;
2754           }
2755         *hh = *h;                       /* Note: copies the port */
2756         h->next = hh;
2757         hh->address = da->address;
2758         hh->sort_key = new_sort_key;
2759         hh->status = status;
2760         hh->why = why;
2761         }
2762
2763       if (h == last) last = hh;         /* Inserted after last */
2764       }
2765
2766     /* The existing item doesn't have its address set yet, so just set it.
2767     Ensure that an IPv4 address gets its sort key incremented in case an IPv6
2768     address is found later. */
2769
2770     else
2771       {
2772       h->address = da->address;         /* Port should be set already */
2773       h->status = status;
2774       h->why = why;
2775       h->sort_key += randoffset;
2776       }
2777     }    /* Loop for addresses extracted from one RR */
2778
2779   /* Carry on to the next RR. It would be nice to be able to be able to stop
2780   when every host on the list has an address, but we can't be sure there won't
2781   be an additional address for a multi-homed host further down the list, so
2782   we have to continue to the end. */
2783
2784   DNS_NEXT_RR: continue;
2785   }
2786
2787 /* Set the default yield to failure */
2788
2789 yield = HOST_FIND_FAILED;
2790
2791 /* If we haven't found all the addresses in the additional section, we
2792 need to search for A or AAAA records explicitly. The names shouldn't point to
2793 CNAMES, but we use the general lookup function that handles them, just
2794 in case. If any lookup gives a soft error, change the default yield.
2795
2796 For these DNS lookups, we must disable qualify_single and search_parents;
2797 otherwise invalid host names obtained from MX or SRV records can cause trouble
2798 if they happen to match something local. */
2799
2800 dns_init(FALSE, FALSE);
2801
2802 for (h = host; h != last->next; h = h->next)
2803   {
2804   if (h->address != NULL || h->status == hstatus_unusable) continue;
2805   rc = set_address_from_dns(h, &last, ignore_target_hosts, allow_mx_to_ip, NULL);
2806   if (rc != HOST_FOUND)
2807     {
2808     h->status = hstatus_unusable;
2809     if (rc == HOST_FIND_AGAIN)
2810       {
2811       yield = rc;
2812       h->why = hwhy_deferred;
2813       }
2814     else
2815       h->why = (rc == HOST_IGNORED)? hwhy_ignored : hwhy_failed;
2816     }
2817   }
2818
2819 /* Scan the list for any hosts that are marked unusable because they have
2820 been explicitly ignored, and remove them from the list, as if they did not
2821 exist. If we end up with just a single, ignored host, flatten its fields as if
2822 nothing was found. */
2823
2824 if (ignore_target_hosts != NULL)
2825   {
2826   host_item *prev = NULL;
2827   for (h = host; h != last->next; h = h->next)
2828     {
2829     REDO:
2830     if (h->why != hwhy_ignored)        /* Non ignored host, just continue */
2831       prev = h;
2832     else if (prev == NULL)             /* First host is ignored */
2833       {
2834       if (h != last)                   /* First is not last */
2835         {
2836         if (h->next == last) last = h; /* Overwrite it with next */
2837         *h = *(h->next);               /* and reprocess it. */
2838         goto REDO;                     /* C should have redo, like Perl */
2839         }
2840       }
2841     else                               /* Ignored host is not first - */
2842       {                                /*   cut it out */
2843       prev->next = h->next;
2844       if (h == last) last = prev;
2845       }
2846     }
2847
2848   if (host->why == hwhy_ignored) host->address = NULL;
2849   }
2850
2851 /* There is still one complication in the case of IPv6. Although the code above
2852 arranges that IPv6 addresses take precedence over IPv4 addresses for multihomed
2853 hosts, it doesn't do this for addresses that apply to different hosts with the
2854 same MX precedence, because the sorting on MX precedence happens first. So we
2855 have to make another pass to check for this case. We ensure that, within a
2856 single MX preference value, IPv6 addresses come first. This can separate the
2857 addresses of a multihomed host, but that should not matter. */
2858
2859 #if HAVE_IPV6
2860 if (h != last)
2861   {
2862   for (h = host; h != last; h = h->next)
2863     {
2864     host_item temp;
2865     host_item *next = h->next;
2866     if (h->mx != next->mx ||                /* If next is different MX value */
2867         (h->sort_key % 1000) < 500 ||       /* OR this one is IPv6 */
2868         (next->sort_key % 1000) >= 500)     /* OR next is IPv4 */
2869       continue;                             /* move on to next */
2870     temp = *h;
2871     temp.next = next->next;
2872     *h = *next;
2873     h->next = next;
2874     *next = temp;
2875     }
2876   }
2877 #endif
2878
2879 /* When running in the test harness, we want the hosts always to be in the same
2880 order so that the debugging output is the same and can be compared. Having a
2881 fixed set of "random" numbers doesn't actually achieve this, because the RRs
2882 come back from the resolver in a random order, so the non-random random numbers
2883 get used in a different order. We therefore have to sort the hosts that have
2884 the same MX values. We chose do to this by their name and then by IP address.
2885 The fact that the sort is slow matters not - this is testing only! */
2886
2887 if (running_in_test_harness)
2888   {
2889   BOOL done;
2890   do
2891     {
2892     done = TRUE;
2893     for (h = host; h != last; h = h->next)
2894       {
2895       int c = Ustrcmp(h->name, h->next->name);
2896       if (c == 0) c = Ustrcmp(h->address, h->next->address);
2897       if (h->mx == h->next->mx && c > 0)
2898         {
2899         host_item *next = h->next;
2900         host_item temp = *h;
2901         temp.next = next->next;
2902         *h = *next;
2903         h->next = next;
2904         *next = temp;
2905         done = FALSE;
2906         }
2907       }
2908     }
2909   while (!done);
2910   }
2911
2912 /* Remove any duplicate IP addresses and then scan the list of hosts for any
2913 whose IP addresses are on the local host. If any are found, all hosts with the
2914 same or higher MX values are removed. However, if the local host has the lowest
2915 numbered MX, then HOST_FOUND_LOCAL is returned. Otherwise, if at least one host
2916 with an IP address is on the list, HOST_FOUND is returned. Otherwise,
2917 HOST_FIND_FAILED is returned, but in this case do not update the yield, as it
2918 might have been set to HOST_FIND_AGAIN just above here. If not, it will already
2919 be HOST_FIND_FAILED. */
2920
2921 host_remove_duplicates(host, &last);
2922 rc = host_scan_for_local_hosts(host, &last, removed);
2923 if (rc != HOST_FIND_FAILED) yield = rc;
2924
2925 DEBUG(D_host_lookup)
2926   {
2927   if (fully_qualified_name != NULL)
2928     debug_printf("fully qualified name = %s\n", *fully_qualified_name);
2929   debug_printf("host_find_bydns yield = %s (%d); returned hosts:\n",
2930     (yield == HOST_FOUND)? "HOST_FOUND" :
2931     (yield == HOST_FOUND_LOCAL)? "HOST_FOUND_LOCAL" :
2932     (yield == HOST_FIND_AGAIN)? "HOST_FIND_AGAIN" :
2933     (yield == HOST_FIND_FAILED)? "HOST_FIND_FAILED" : "?",
2934     yield);
2935   for (h = host; h != last->next; h = h->next)
2936     {
2937     debug_printf("  %s %s MX=%d ", h->name,
2938       (h->address == NULL)? US"<null>" : h->address, h->mx);
2939     if (h->port != PORT_NONE) debug_printf("port=%d ", h->port);
2940     if (h->status >= hstatus_unusable) debug_printf("*");
2941     debug_printf("\n");
2942     }
2943   }
2944
2945 return yield;
2946 }
2947
2948
2949
2950
2951 /*************************************************
2952 **************************************************
2953 *             Stand-alone test program           *
2954 **************************************************
2955 *************************************************/
2956
2957 #ifdef STAND_ALONE
2958
2959 BOOL alldigits(uschar *buffer)
2960 {
2961 if (!isdigit(*buffer)) return FALSE;
2962 if (*buffer == '0' && buffer[1] == 'x')
2963   {
2964   buffer++;
2965   while (isxdigit(*(++buffer)));
2966   }
2967 else while (isdigit(*(++buffer)));
2968 return (*buffer == 0);
2969 }
2970
2971 int main(int argc, char **cargv)
2972 {
2973 host_item h;
2974 int whichrrs = HOST_FIND_BY_MX | HOST_FIND_BY_A;
2975 BOOL byname = FALSE;
2976 BOOL qualify_single = TRUE;
2977 BOOL search_parents = FALSE;
2978 uschar **argv = USS cargv;
2979 uschar buffer[256];
2980
2981 primary_hostname = US"";
2982 store_pool = POOL_MAIN;
2983 debug_selector = D_host_lookup|D_interface;
2984 debug_file = stdout;
2985 debug_fd = fileno(debug_file);
2986
2987 printf("Exim stand-alone host functions test\n");
2988
2989 host_find_interfaces();
2990 debug_selector = D_host_lookup | D_dns;
2991
2992 if (argc > 1) primary_hostname = argv[1];
2993
2994 /* So that debug level changes can be done first */
2995
2996 dns_init(qualify_single, search_parents);
2997
2998 printf("Testing host lookup\n");
2999 printf("> ");
3000 while (Ufgets(buffer, 256, stdin) != NULL)
3001   {
3002   int rc;
3003   int len = Ustrlen(buffer);
3004   uschar *fully_qualified_name;
3005
3006   while (len > 0 && isspace(buffer[len-1])) len--;
3007   buffer[len] = 0;
3008
3009   if (Ustrcmp(buffer, "q") == 0) break;
3010
3011   if (Ustrcmp(buffer, "byname") == 0) byname = TRUE;
3012   else if (Ustrcmp(buffer, "no_byname") == 0) byname = FALSE;
3013   else if (Ustrcmp(buffer, "a_only") == 0) whichrrs = HOST_FIND_BY_A;
3014   else if (Ustrcmp(buffer, "mx_only") == 0) whichrrs = HOST_FIND_BY_MX;
3015   else if (Ustrcmp(buffer, "srv_only") == 0) whichrrs = HOST_FIND_BY_SRV;
3016   else if (Ustrcmp(buffer, "srv+a") == 0)
3017     whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_A;
3018   else if (Ustrcmp(buffer, "srv+mx") == 0)
3019     whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX;
3020   else if (Ustrcmp(buffer, "srv+mx+a") == 0)
3021     whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX | HOST_FIND_BY_A;
3022   else if (Ustrcmp(buffer, "qualify_single") == 0) qualify_single = TRUE;
3023   else if (Ustrcmp(buffer, "no_qualify_single") == 0) qualify_single = FALSE;
3024   else if (Ustrcmp(buffer, "search_parents") == 0) search_parents = TRUE;
3025   else if (Ustrcmp(buffer, "no_search_parents") == 0) search_parents = FALSE;
3026   else if (Ustrncmp(buffer, "retrans", 7) == 0)
3027     {
3028     (void)sscanf(CS(buffer+8), "%d", &dns_retrans);
3029     _res.retrans = dns_retrans;
3030     }
3031   else if (Ustrncmp(buffer, "retry", 5) == 0)
3032     {
3033     (void)sscanf(CS(buffer+6), "%d", &dns_retry);
3034     _res.retry = dns_retry;
3035     }
3036   else if (alldigits(buffer))
3037     {
3038     debug_selector = Ustrtol(buffer, NULL, 0);
3039     _res.options &= ~RES_DEBUG;
3040     DEBUG(D_resolver) _res.options |= RES_DEBUG;
3041     }
3042   else
3043     {
3044     int flags = whichrrs;
3045
3046     h.name = buffer;
3047     h.next = NULL;
3048     h.mx = MX_NONE;
3049     h.port = PORT_NONE;
3050     h.status = hstatus_unknown;
3051     h.why = hwhy_unknown;
3052     h.address = NULL;
3053
3054     if (qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE;
3055     if (search_parents) flags |= HOST_FIND_SEARCH_PARENTS;
3056
3057     rc = byname?
3058       host_find_byname(&h, NULL, &fully_qualified_name, TRUE)
3059       :
3060       host_find_bydns(&h, NULL, flags, US"smtp", NULL, NULL,
3061         &fully_qualified_name, NULL);
3062
3063     if (rc == HOST_FIND_FAILED) printf("Failed\n");
3064       else if (rc == HOST_FIND_AGAIN) printf("Again\n");
3065         else if (rc == HOST_FOUND_LOCAL) printf("Local\n");
3066     }
3067
3068   printf("\n> ");
3069   }
3070
3071 printf("Testing host_aton\n");
3072 printf("> ");
3073 while (Ufgets(buffer, 256, stdin) != NULL)
3074   {
3075   int i;
3076   int x[4];
3077   int len = Ustrlen(buffer);
3078
3079   while (len > 0 && isspace(buffer[len-1])) len--;
3080   buffer[len] = 0;
3081
3082   if (Ustrcmp(buffer, "q") == 0) break;
3083
3084   len = host_aton(buffer, x);
3085   printf("length = %d ", len);
3086   for (i = 0; i < len; i++)
3087     {
3088     printf("%04x ", (x[i] >> 16) & 0xffff);
3089     printf("%04x ", x[i] & 0xffff);
3090     }
3091   printf("\n> ");
3092   }
3093
3094 printf("\n");
3095
3096 printf("Testing host_name_lookup\n");
3097 printf("> ");
3098 while (Ufgets(buffer, 256, stdin) != NULL)
3099   {
3100   int len = Ustrlen(buffer);
3101   while (len > 0 && isspace(buffer[len-1])) len--;
3102   buffer[len] = 0;
3103   if (Ustrcmp(buffer, "q") == 0) break;
3104   sender_host_address = buffer;
3105   sender_host_name = NULL;
3106   sender_host_aliases = NULL;
3107   host_lookup_msg = US"";
3108   host_lookup_failed = FALSE;
3109   if (host_name_lookup() == FAIL)  /* Debug causes printing */
3110     printf("Lookup failed:%s\n", host_lookup_msg);
3111   printf("\n> ");
3112   }
3113
3114 printf("\n");
3115
3116 return 0;
3117 }
3118 #endif  /* STAND_ALONE */
3119
3120 /* End of host.c */