2 * This module determines the type of socket (datagram, stream), the client
3 * socket address and port, the server socket address and port. In addition,
4 * it provides methods to map a transport address to a printable host name
5 * or address. Socket address information results are in static memory.
7 * The result from the hostname lookup method is STRING_PARANOID when a host
8 * pretends to have someone elses name, or when a host name is available but
9 * could not be verified.
11 * When lookup or conversion fails the result is set to STRING_UNKNOWN.
13 * Diagnostics are reported through syslog(3).
15 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
21 static char sccsid[] = "@(#) socket.c 1.15 97/03/21 19:27:24";
24 /* System libraries. */
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
36 extern char *inet_ntoa();
43 /* Forward declarations. */
45 static void sock_sink(int);
50 * Speed up DNS lookups by terminating the host name with a dot. Should be
51 * done with care. The speedup can give problems with lookups from sources
52 * that lack DNS-style trailing dot magic, such as local files or NIS maps.
55 static struct hostent *gethostbyname_dot(char *name)
57 char dot_name[MAXHOSTNAMELEN + 1];
60 * Don't append dots to unqualified names. Such names are likely to come
61 * from local hosts files or from NIS.
64 if (strchr(name, '.') == 0 || strlen(name) >= MAXHOSTNAMELEN - 1) {
65 return (gethostbyname(name));
67 sprintf(dot_name, "%s.", name);
68 return (gethostbyname(dot_name));
72 #define gethostbyname gethostbyname_dot
75 /* sock_host - look up endpoint addresses and install conversion methods */
77 void sock_host(struct request_info *request)
80 static struct sockaddr_storage client;
81 static struct sockaddr_storage server;
83 static struct sockaddr_in client;
84 static struct sockaddr_in server;
90 sock_methods(request);
93 * Look up the client host address. Hal R. Brand <BRAND@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.
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 */
110 #ifdef really_paranoid
111 memset(buf, 0, sizeof(buf));
115 request->client->sin = (struct sockaddr *)&client;
117 request->client->sin = &client;
121 * Determine the server binding. This is used for client username
122 * lookups, and for access control rules that trigger on the server
126 len = sizeof(server);
127 if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
128 tcpd_warn("getsockname: %m");
132 request->server->sin = (struct sockaddr *)&server;
134 request->server->sin = &server;
138 /* sock_hostaddr - map endpoint address to printable form */
140 void sock_hostaddr(struct host_info *host)
143 struct sockaddr *sin = host->sin;
151 salen = (sin->sa_family == AF_INET) ? sizeof(struct sockaddr_in)
152 : sizeof(struct sockaddr_in6);
154 getnameinfo(sin, salen, host->addr, sizeof(host->addr),
155 NULL, 0, NI_NUMERICHOST);
157 struct sockaddr_in *sin = host->sin;
160 STRN_CPY(host->addr, inet_ntoa(sin->sin_addr), sizeof(host->addr));
164 /* sock_hostname - map endpoint address to host name */
166 void sock_hostname(struct host_info *host)
169 struct sockaddr *sin = host->sin;
170 struct sockaddr_in sin4;
171 struct addrinfo hints, *res, *res0 = NULL;
172 int salen, alen, err = 1;
173 char *ap = NULL, *rap, hname[NI_MAXHOST];
176 if (sin->sa_family == AF_INET6) {
177 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
179 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
180 memset(&sin4, 0, sizeof(sin4));
182 sin4.sin_len = sizeof(sin4);
184 sin4.sin_family = AF_INET;
185 sin4.sin_port = sin6->sin6_port;
186 sin4.sin_addr.s_addr = *(u_int32_t *)&sin6->sin6_addr.s6_addr[12];
187 sin = (struct sockaddr *)&sin4;
190 switch (sin->sa_family) {
192 ap = (char *)&((struct sockaddr_in *)sin)->sin_addr;
193 alen = sizeof(struct in_addr);
194 salen = sizeof(struct sockaddr_in);
197 ap = (char *)&((struct sockaddr_in6 *)sin)->sin6_addr;
198 alen = sizeof(struct in6_addr);
199 salen = sizeof(struct sockaddr_in6);
205 err = getnameinfo(sin, salen, hname, sizeof(hname),
206 NULL, 0, NI_NAMEREQD);
210 STRN_CPY(host->name, hname, sizeof(host->name));
212 /* reject numeric addresses */
213 memset(&hints, 0, sizeof(hints));
214 hints.ai_family = sin->sa_family;
215 hints.ai_socktype = SOCK_STREAM;
216 hints.ai_flags = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST;
217 if ((err = getaddrinfo(host->name, NULL, &hints, &res0)) == 0) {
219 tcpd_warn("host name/name mismatch: "
220 "reverse lookup results in non-FQDN %s",
222 strcpy(host->name, paranoid); /* name is bad, clobber it */
227 /* we are now sure that this is non-numeric */
230 * Verify that the address is a member of the address list returned
231 * by gethostbyname(hostname).
233 * Verify also that gethostbyaddr() and gethostbyname() return the same
234 * hostname, or rshd and rlogind may still end up being spoofed.
236 * On some sites, gethostbyname("localhost") returns "localhost.domain".
237 * This is a DNS artefact. We treat it as a special case. When we
238 * can't believe the address list from gethostbyname("localhost")
239 * we're in big trouble anyway.
242 memset(&hints, 0, sizeof(hints));
243 hints.ai_family = sin->sa_family;
244 hints.ai_socktype = SOCK_STREAM;
245 hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
246 if (getaddrinfo(host->name, NULL, &hints, &res0) != 0) {
249 * Unable to verify that the host name matches the address. This
250 * may be a transient problem or a botched name server setup.
253 tcpd_warn("can't verify hostname: getaddrinfo(%s, %s) failed",
255 (sin->sa_family == AF_INET) ? "AF_INET" : "AF_INET6");
257 } else if ((res0->ai_canonname == NULL
258 || STR_NE(host->name, res0->ai_canonname))
259 && STR_NE(host->name, "localhost")) {
262 * The gethostbyaddr() and gethostbyname() calls did not return
263 * the same hostname. This could be a nameserver configuration
264 * problem. It could also be that someone is trying to spoof us.
267 tcpd_warn("host name/name mismatch: %s != %.*s",
268 host->name, STRING_LENGTH,
269 (res0->ai_canonname == NULL) ? "" : res0->ai_canonname);
274 * The address should be a member of the address list returned by
275 * gethostbyname(). We should first verify that the h_addrtype
276 * field is AF_INET, but this program has already caused too much
277 * grief on systems with broken library code.
280 for (res = res0; res; res = res->ai_next) {
281 if (res->ai_family != sin->sa_family)
283 switch (res->ai_family) {
285 rap = (char *)&((struct sockaddr_in *)res->ai_addr)->sin_addr;
288 /* need to check scope_id */
289 if (((struct sockaddr_in6 *)sin)->sin6_scope_id !=
290 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id) {
293 rap = (char *)&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
298 if (memcmp(rap, ap, alen) == 0) {
300 return; /* name is good, keep it */
305 * The host name does not map to the initial address. Perhaps
306 * someone has messed up. Perhaps someone compromised a name
310 getnameinfo(sin, salen, hname, sizeof(hname),
311 NULL, 0, NI_NUMERICHOST);
312 tcpd_warn("host name/address mismatch: %s != %.*s",
313 hname, STRING_LENGTH,
314 (res0->ai_canonname == NULL) ? "" : res0->ai_canonname);
316 strcpy(host->name, paranoid); /* name is bad, clobber it */
321 struct sockaddr_in *sin = host->sin;
326 * On some systems, for example Solaris 2.3, gethostbyaddr(0.0.0.0) does
327 * not fail. Instead it returns "INADDR_ANY". Unfortunately, this does
328 * not work the other way around: gethostbyname("INADDR_ANY") fails. We
329 * have to special-case 0.0.0.0, in order to avoid false alerts from the
330 * host name/address checking code below.
332 if (sin != 0 && sin->sin_addr.s_addr != 0
333 && (hp = gethostbyaddr((char *) &(sin->sin_addr),
334 sizeof(sin->sin_addr), AF_INET)) != 0) {
336 STRN_CPY(host->name, hp->h_name, sizeof(host->name));
339 * Verify that the address is a member of the address list returned
340 * by gethostbyname(hostname).
342 * Verify also that gethostbyaddr() and gethostbyname() return the same
343 * hostname, or rshd and rlogind may still end up being spoofed.
345 * On some sites, gethostbyname("localhost") returns "localhost.domain".
346 * This is a DNS artefact. We treat it as a special case. When we
347 * can't believe the address list from gethostbyname("localhost")
348 * we're in big trouble anyway.
351 if ((hp = gethostbyname(host->name)) == 0) {
354 * Unable to verify that the host name matches the address. This
355 * may be a transient problem or a botched name server setup.
358 tcpd_warn("can't verify hostname: gethostbyname(%s) failed",
361 } else if (STR_NE(host->name, hp->h_name)
362 && STR_NE(host->name, "localhost")) {
365 * The gethostbyaddr() and gethostbyname() calls did not return
366 * the same hostname. This could be a nameserver configuration
367 * problem. It could also be that someone is trying to spoof us.
370 tcpd_warn("host name/name mismatch: %s != %.*s",
371 host->name, STRING_LENGTH, hp->h_name);
376 * The address should be a member of the address list returned by
377 * gethostbyname(). We should first verify that the h_addrtype
378 * field is AF_INET, but this program has already caused too much
379 * grief on systems with broken library code.
382 for (i = 0; hp->h_addr_list[i]; i++) {
383 if (memcmp(hp->h_addr_list[i],
384 (char *) &sin->sin_addr,
385 sizeof(sin->sin_addr)) == 0)
386 return; /* name is good, keep it */
390 * The host name does not map to the initial address. Perhaps
391 * someone has messed up. Perhaps someone compromised a name
395 tcpd_warn("host name/address mismatch: %s != %.*s",
396 inet_ntoa(sin->sin_addr), STRING_LENGTH, hp->h_name);
398 strcpy(host->name, paranoid); /* name is bad, clobber it */
403 /* sock_sink - absorb unreceived IP datagram */
405 static void sock_sink(int fd)
409 struct sockaddr_storage sin;
411 struct sockaddr_in sin;
413 int size = sizeof(sin);
416 * Eat up the not-yet received datagram. Some systems insist on a
417 * non-zero source address argument in the recvfrom() call below.
420 (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & sin, &size);