socket.c revision 1.3 1 /* $NetBSD: socket.c,v 1.3 1997/10/09 21:20:50 christos Exp $ */
2
3 /*
4 * This module determines the type of socket (datagram, stream), the client
5 * socket address and port, the server socket address and port. In addition,
6 * it provides methods to map a transport address to a printable host name
7 * or address. Socket address information results are in static memory.
8 *
9 * The result from the hostname lookup method is STRING_PARANOID when a host
10 * pretends to have someone elses name, or when a host name is available but
11 * could not be verified.
12 *
13 * When lookup or conversion fails the result is set to STRING_UNKNOWN.
14 *
15 * Diagnostics are reported through syslog(3).
16 *
17 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
18 */
19
20 #include <sys/cdefs.h>
21 #ifndef lint
22 #if 0
23 static char sccsid[] = "@(#) socket.c 1.14 95/01/30 19:51:50";
24 #else
25 __RCSID("$NetBSD: socket.c,v 1.3 1997/10/09 21:20:50 christos Exp $");
26 #endif
27 #endif
28
29 /* System libraries. */
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <syslog.h>
38 #include <string.h>
39 #include <arpa/inet.h>
40
41 /* Local stuff. */
42
43 #include "tcpd.h"
44
45 /* Forward declarations. */
46
47 static void sock_sink __P((int));
48
49 #ifdef APPEND_DOT
50 static struct hostent *gethostbyname_dot __P((char *));
51
52 /*
53 * Speed up DNS lookups by terminating the host name with a dot. Should be
54 * done with care. The speedup can give problems with lookups from sources
55 * that lack DNS-style trailing dot magic, such as local files or NIS maps.
56 */
57
58 static struct hostent *gethostbyname_dot(name)
59 char *name;
60 {
61 char dot_name[MAXHOSTNAMELEN + 1];
62
63 /*
64 * Don't append dots to unqualified names. Such names are likely to come
65 * from local hosts files or from NIS.
66 */
67
68 if (strchr(name, '.') == 0 || strlen(name) >= MAXHOSTNAMELEN - 1) {
69 return (gethostbyname(name));
70 } else {
71 (void)snprintf(dot_name, sizeof dot_name, "%s.", name);
72 return (gethostbyname(dot_name));
73 }
74 }
75
76 #define gethostbyname gethostbyname_dot
77 #endif
78
79 /* sock_host - look up endpoint addresses and install conversion methods */
80
81 void sock_host(request)
82 struct request_info *request;
83 {
84 static struct sockaddr_in client;
85 static struct sockaddr_in server;
86 int len;
87 char buf[BUFSIZ];
88 int fd = request->fd;
89
90 sock_methods(request);
91
92 /*
93 * Look up the client host address. Hal R. Brand <BRAND (at) addvax.llnl.gov>
94 * suggested how to get the client host info in case of UDP connections:
95 * peek at the first message without actually looking at its contents. We
96 * really should verify that client.sin_family gets the value AF_INET,
97 * but this program has already caused too much grief on systems with
98 * broken library code.
99 */
100
101 len = sizeof(client);
102 if (getpeername(fd, (struct sockaddr *) & client, &len) < 0) {
103 request->sink = sock_sink;
104 len = sizeof(client);
105 if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK,
106 (struct sockaddr *) & client, &len) < 0) {
107 tcpd_warn("can't get client address: %m");
108 return; /* give up */
109 }
110 #ifdef really_paranoid
111 memset(buf, 0 sizeof(buf));
112 #endif
113 }
114 request->client->sin = &client;
115
116 /*
117 * Determine the server binding. This is used for client username
118 * lookups, and for access control rules that trigger on the server
119 * address or name.
120 */
121
122 len = sizeof(server);
123 if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
124 tcpd_warn("getsockname: %m");
125 return;
126 }
127 request->server->sin = &server;
128 }
129
130 /* sock_hostaddr - map endpoint address to printable form */
131
132 void sock_hostaddr(host)
133 struct host_info *host;
134 {
135 struct sockaddr_in *sin = host->sin;
136
137 if (sin != 0)
138 STRN_CPY(host->addr, inet_ntoa(sin->sin_addr), sizeof(host->addr));
139 }
140
141 /* sock_hostname - map endpoint address to host name */
142
143 void sock_hostname(host)
144 struct host_info *host;
145 {
146 struct sockaddr_in *sin = host->sin;
147 struct hostent *hp;
148 int i;
149
150 /*
151 * On some systems, for example Solaris 2.3, gethostbyaddr(0.0.0.0) does
152 * not fail. Instead it returns "INADDR_ANY". Unfortunately, this does
153 * not work the other way around: gethostbyname("INADDR_ANY") fails. We
154 * have to special-case 0.0.0.0, in order to avoid false alerts from the
155 * host name/address checking code below.
156 */
157 if (sin != 0 && sin->sin_addr.s_addr != 0
158 && (hp = gethostbyaddr((char *) &(sin->sin_addr),
159 sizeof(sin->sin_addr), AF_INET)) != 0) {
160
161 STRN_CPY(host->name, hp->h_name, sizeof(host->name));
162
163 /*
164 * Verify that the address is a member of the address list returned
165 * by gethostbyname(hostname).
166 *
167 * Verify also that gethostbyaddr() and gethostbyname() return the same
168 * hostname, or rshd and rlogind may still end up being spoofed.
169 *
170 * On some sites, gethostbyname("localhost") returns "localhost.domain".
171 * This is a DNS artefact. We treat it as a special case. When we
172 * can't believe the address list from gethostbyname("localhost")
173 * we're in big trouble anyway.
174 */
175
176 if ((hp = gethostbyname(host->name)) == 0) {
177
178 /*
179 * Unable to verify that the host name matches the address. This
180 * may be a transient problem or a botched name server setup.
181 */
182
183 tcpd_warn("can't verify hostname: gethostbyname(%s) failed",
184 host->name);
185
186 } else if (STR_NE(host->name, hp->h_name)
187 && STR_NE(host->name, "localhost")) {
188
189 /*
190 * The gethostbyaddr() and gethostbyname() calls did not return
191 * the same hostname. This could be a nameserver configuration
192 * problem. It could also be that someone is trying to spoof us.
193 */
194
195 tcpd_warn("host name/name mismatch: %s != %s",
196 host->name, hp->h_name);
197
198 } else {
199
200 /*
201 * The address should be a member of the address list returned by
202 * gethostbyname(). We should first verify that the h_addrtype
203 * field is AF_INET, but this program has already caused too much
204 * grief on systems with broken library code.
205 */
206
207 for (i = 0; hp->h_addr_list[i]; i++) {
208 if (memcmp(hp->h_addr_list[i],
209 (char *) &sin->sin_addr,
210 sizeof(sin->sin_addr)) == 0)
211 return; /* name is good, keep it */
212 }
213
214 /*
215 * The host name does not map to the initial address. Perhaps
216 * someone has messed up. Perhaps someone compromised a name
217 * server.
218 */
219
220 tcpd_warn("host name/address mismatch: %s != %s",
221 inet_ntoa(sin->sin_addr), hp->h_name);
222 }
223 /* name is bad, clobber it */
224 (void)strncpy(host->name, paranoid, sizeof(host->name) - 1);
225 }
226 }
227
228 /* sock_sink - absorb unreceived IP datagram */
229
230 static void sock_sink(fd)
231 int fd;
232 {
233 char buf[BUFSIZ];
234 struct sockaddr_in sin;
235 int size = sizeof(sin);
236
237 /*
238 * Eat up the not-yet received datagram. Some systems insist on a
239 * non-zero source address argument in the recvfrom() call below.
240 */
241
242 (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & sin, &size);
243 }
244