|
|
1.1 root 1: /* hosts.c - find out the official name of a host */
2:
3: /* LINTLIBRARY */
4:
5: /* In the SendMail world, we really don't know what the valid hosts are.
6: We could poke around in the sendmail.cf file, but that still isn't a
7: guarantee. As a result, we'll say that everything is a valid host, and
8: let SendMail worry about it. */
9:
10:
11: #include "../h/strings.h"
12: #include <stdio.h>
13: #include "../zotnet/mts.h"
14: #include <ctype.h>
15: #ifdef BSD42
16: #include <netdb.h>
17: #endif BSD42
18:
19:
20: #define NOTOK (-1)
21:
22:
23: static struct host {
24: char *h_name;
25: char **h_aliases;
26: struct host *h_next;
27: } hosts;
28:
29: char *getcpy ();
30: static int init_hs();
31:
32: /* */
33:
34: char *
35: OfficialName(name)
36: register char *name;
37: {
38: register char *p;
39: char *q,
40: site[BUFSIZ];
41: #ifdef BSD42
42: register struct hostent *hp;
43: #endif BSD42
44: static char buffer[BUFSIZ];
45: register char **r;
46: register struct host *h;
47:
48: for (p = name, q = site; *p; p++, q++)
49: *q = isupper (*p) ? tolower (*p) : *p;
50: *q = NULL;
51: q = site;
52:
53: if (uleq (LocalName (), site))
54: return LocalName ();
55:
56: #ifdef BSD41A
57: if (rhost (&q) != NOTOK) {
58: (void) strcpy (buffer, q);
59: free (q);
60: return buffer;
61: }
62: #endif BSD41A
63: #ifdef BSD42
64: sethostent (1);
65: if (hp = gethostbyname (q)) {
66: (void) strcpy (buffer, hp -> h_name);
67: return buffer;
68: }
69: #endif BSD42
70:
71: if (hosts.h_name || init_hs ())
72: for (h = hosts.h_next; h; h = h -> h_next)
73: if (uleq (h -> h_name, q))
74: return h -> h_name;
75: else
76: for (r = h -> h_aliases; *r; r++)
77: if (uleq (*r, q))
78: return h -> h_name;
79:
80: (void) strcpy (buffer, site);
81: return buffer;
82: }
83:
84: /* */
85:
86: /* Use hostable as an exception file for those hosts that aren't on the
87: Internet (listed in /etc/hosts). These are usually PhoneNet and UUCP
88: sites. */
89:
90:
91: #define NALIASES 50
92:
93: static int
94: init_hs()
95: {
96: register char *cp,
97: *dp,
98: **q,
99: **r;
100: char buffer[BUFSIZ],
101: *aliases[NALIASES];
102: register struct host *h;
103: register FILE *fp;
104:
105: if ((fp = fopen (hostable, "r")) == NULL)
106: return 0;
107:
108: h = &hosts;
109: while (fgets (buffer, sizeof buffer, fp) != NULL) {
110: if (cp = index (buffer, '#'))
111: *cp = NULL;
112: if (cp = index (buffer, '\n'))
113: *cp = NULL;
114: for (cp = buffer; *cp; cp++)
115: if (isspace (*cp))
116: *cp = ' ';
117: for (cp = buffer; isspace (*cp); cp++)
118: continue;
119: if (*cp == NULL)
120: continue;
121:
122: q = aliases;
123: if (cp = index (dp = cp, ' ')) {
124: *cp = NULL;
125: for (cp++; *cp; cp++) {
126: while (isspace (*cp))
127: cp++;
128: if (*cp == NULL)
129: break;
130: if (cp = index (*q++ = cp, ' '))
131: *cp = NULL;
132: else
133: break;
134: if (q >= aliases + NALIASES)
135: break;
136: }
137: }
138:
139: *q = NULL;
140:
141: h -> h_next = (struct host *) calloc (1, sizeof *h);
142: h = h -> h_next;
143: h -> h_name = getcpy (dp);
144: r = h -> h_aliases =
145: (char **) calloc ((unsigned) (q - aliases + 1), sizeof *q);
146: for (q = aliases; *q; q++)
147: *r++ = getcpy (*q);
148: *r = NULL;
149: }
150:
151: (void) fclose (fp);
152: return 1;
153: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.