]> diplodocus.org Git - nmh/blob - mts/sendmail/hosts.c
Just reworded the bit about '%s' being safe not to quote (it's only safe not to
[nmh] / mts / sendmail / hosts.c
1
2 /*
3 * hosts.c -- find out the official name of a host
4 *
5 * $Id$
6 */
7
8 /*
9 * In the SendMail world, we really don't know what the valid
10 * hosts are. We could poke around in the sendmail.cf file, but
11 * that still isn't a guarantee. As a result, we'll say that
12 * everything is a valid host, and let SendMail worry about it.
13 */
14
15 #include <h/mh.h>
16 #include <zotnet/mts/mts.h>
17 #include <netdb.h>
18
19 static struct host {
20 char *h_name;
21 char **h_aliases;
22 struct host *h_next;
23 } hosts;
24
25
26 /*
27 * static prototypes
28 */
29 static int init_hs(void);
30
31
32 char *
33 OfficialName (char *name)
34 {
35 char *p, *q, site[BUFSIZ];
36 struct hostent *hp;
37
38 static char buffer[BUFSIZ];
39 char **r;
40 struct host *h;
41
42 for (p = name, q = site; *p && (q - site < sizeof(site) - 1); p++, q++)
43 *q = isupper (*p) ? tolower (*p) : *p;
44 *q = '\0';
45 q = site;
46
47 if (!strcasecmp (LocalName(), site))
48 return LocalName();
49
50 #ifdef HAVE_SETHOSTENT
51 sethostent (1);
52 #endif
53
54 if ((hp = gethostbyname (q))) {
55 strncpy (buffer, hp->h_name, sizeof(buffer));
56 return buffer;
57 }
58 if (hosts.h_name || init_hs ()) {
59 for (h = hosts.h_next; h; h = h->h_next)
60 if (!strcasecmp (h->h_name, q))
61 return h->h_name;
62 else
63 for (r = h->h_aliases; *r; r++)
64 if (!strcasecmp (*r, q))
65 return h->h_name;
66 }
67
68 strncpy (buffer, site, sizeof(buffer));
69 return buffer;
70 }
71
72 /*
73 * Use hostable as an exception file for those hosts that aren't
74 * on the Internet (listed in /etc/hosts). These are usually
75 * PhoneNet and UUCP sites.
76 */
77
78 #define NALIASES 50
79
80 static int
81 init_hs (void)
82 {
83 char *cp, *dp, **q, **r;
84 char buffer[BUFSIZ], *aliases[NALIASES];
85 register struct host *h;
86 register FILE *fp;
87
88 if ((fp = fopen (hostable, "r")) == NULL)
89 return 0;
90
91 h = &hosts;
92 while (fgets (buffer, sizeof(buffer), fp) != NULL) {
93 if ((cp = strchr(buffer, '#')))
94 *cp = 0;
95 if ((cp = strchr(buffer, '\n')))
96 *cp = 0;
97 for (cp = buffer; *cp; cp++)
98 if (isspace (*cp))
99 *cp = ' ';
100 for (cp = buffer; isspace (*cp); cp++)
101 continue;
102 if (*cp == 0)
103 continue;
104
105 q = aliases;
106 if ((cp = strchr(dp = cp, ' '))) {
107 *cp = 0;
108 for (cp++; *cp; cp++) {
109 while (isspace (*cp))
110 cp++;
111 if (*cp == 0)
112 break;
113 if ((cp = strchr(*q++ = cp, ' ')))
114 *cp = 0;
115 else
116 break;
117 if (q >= aliases + NALIASES)
118 break;
119 }
120 }
121
122 *q = 0;
123
124 h->h_next = (struct host *) calloc (1, sizeof(*h));
125 h = h->h_next;
126 h->h_name = getcpy (dp);
127 r = h->h_aliases =
128 (char **) calloc ((size_t) (q - aliases + 1), sizeof(*q));
129 for (q = aliases; *q; q++)
130 *r++ = getcpy (*q);
131 *r = 0;
132 }
133
134 fclose (fp);
135 return 1;
136 }