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