3 * hosts.c -- find out the official name of a host
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * In the SendMail world, we really don't know what the valid
12 * hosts are. We could poke around in the sendmail.cf file, but
13 * that still isn't a guarantee. As a result, we'll say that
14 * everything is a valid host, and let SendMail worry about it.
19 #include <sys/socket.h>
32 static int init_hs(void);
36 OfficialName (char *name)
39 char *q, site[BUFSIZ];
40 struct addrinfo hints, *res;
42 static char buffer[BUFSIZ];
46 for (p = name, q = site; *p && (q - site < (int)sizeof(site) - 1); p++, q++)
47 *q = isupper (*p) ? tolower (*p) : *p;
51 if (!mh_strcasecmp (LocalName(1), site))
54 memset(&hints, 0, sizeof(hints));
55 hints.ai_flags = AI_CANONNAME;
56 hints.ai_family = PF_UNSPEC;
58 if (getaddrinfo(q, NULL, &hints, &res) == 0) {
59 strncpy (buffer, res->ai_canonname, sizeof(buffer));
60 buffer[sizeof(buffer) - 1] = '\0';
64 if (hosts.h_name || init_hs ()) {
65 for (h = hosts.h_next; h; h = h->h_next)
66 if (!mh_strcasecmp (h->h_name, q)) {
69 for (r = h->h_aliases; *r; r++)
70 if (!mh_strcasecmp (*r, q))
75 strncpy (buffer, site, sizeof(buffer));
80 * Use hostable as an exception file for those hosts that aren't
81 * on the Internet (listed in /etc/hosts). These are usually
82 * PhoneNet and UUCP sites.
92 char buffer[BUFSIZ], *aliases[NALIASES];
93 register struct host *h;
96 if ((fp = fopen (hostable, "r")) == NULL)
100 while (fgets (buffer, sizeof(buffer), fp) != NULL) {
101 if ((cp = strchr(buffer, '#')))
103 if ((cp = strchr(buffer, '\n')))
105 for (cp = buffer; *cp; cp++)
108 for (cp = buffer; isspace (*cp); cp++)
114 if ((cp = strchr(dp = cp, ' '))) {
116 for (cp++; *cp; cp++) {
117 while (isspace (*cp))
121 if ((cp = strchr(*q++ = cp, ' ')))
125 if (q >= aliases + NALIASES)
132 h->h_next = (struct host *) calloc (1, sizeof(*h));
134 h->h_name = getcpy (dp);
136 (char **) calloc ((size_t) (q - aliases + 1), sizeof(*q));
137 for (q = aliases; *q; q++)