1 | |
---|
2 | /* |
---|
3 | * hosts.c -- find out the official name of a host |
---|
4 | * |
---|
5 | * $Id: hosts.c,v 1.1.1.1 1999-02-07 18:14:12 danw Exp $ |
---|
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 | #ifndef BIND |
---|
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 | strncpy (buffer, site, sizeof(buffer)); |
---|
68 | return buffer; |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * Use hostable as an exception file for those hosts that aren't |
---|
73 | * on the Internet (listed in /etc/hosts). These are usually |
---|
74 | * PhoneNet and UUCP sites. |
---|
75 | */ |
---|
76 | |
---|
77 | #define NALIASES 50 |
---|
78 | |
---|
79 | static int |
---|
80 | init_hs (void) |
---|
81 | { |
---|
82 | char *cp, *dp, **q, **r; |
---|
83 | char buffer[BUFSIZ], *aliases[NALIASES]; |
---|
84 | register struct host *h; |
---|
85 | register FILE *fp; |
---|
86 | |
---|
87 | if ((fp = fopen (hostable, "r")) == NULL) |
---|
88 | return 0; |
---|
89 | |
---|
90 | h = &hosts; |
---|
91 | while (fgets (buffer, sizeof(buffer), fp) != NULL) { |
---|
92 | if ((cp = strchr(buffer, '#'))) |
---|
93 | *cp = 0; |
---|
94 | if ((cp = strchr(buffer, '\n'))) |
---|
95 | *cp = 0; |
---|
96 | for (cp = buffer; *cp; cp++) |
---|
97 | if (isspace (*cp)) |
---|
98 | *cp = ' '; |
---|
99 | for (cp = buffer; isspace (*cp); cp++) |
---|
100 | continue; |
---|
101 | if (*cp == 0) |
---|
102 | continue; |
---|
103 | |
---|
104 | q = aliases; |
---|
105 | if ((cp = strchr(dp = cp, ' '))) { |
---|
106 | *cp = 0; |
---|
107 | for (cp++; *cp; cp++) { |
---|
108 | while (isspace (*cp)) |
---|
109 | cp++; |
---|
110 | if (*cp == 0) |
---|
111 | break; |
---|
112 | if ((cp = strchr(*q++ = cp, ' '))) |
---|
113 | *cp = 0; |
---|
114 | else |
---|
115 | break; |
---|
116 | if (q >= aliases + NALIASES) |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | *q = 0; |
---|
122 | |
---|
123 | h->h_next = (struct host *) calloc (1, sizeof(*h)); |
---|
124 | h = h->h_next; |
---|
125 | h->h_name = getcpy (dp); |
---|
126 | r = h->h_aliases = |
---|
127 | (char **) calloc ((size_t) (q - aliases + 1), sizeof(*q)); |
---|
128 | for (q = aliases; *q; q++) |
---|
129 | *r++ = getcpy (*q); |
---|
130 | *r = 0; |
---|
131 | } |
---|
132 | |
---|
133 | fclose (fp); |
---|
134 | return 1; |
---|
135 | } |
---|