2012-03-21 14:10:36 +04:00
|
|
|
/* $NetBSD: socket.c,v 1.19 2012/03/21 10:10:37 matt Exp $ */
|
1997-10-10 01:20:16 +04:00
|
|
|
|
1996-11-26 19:48:08 +03:00
|
|
|
/*
|
|
|
|
* This module determines the type of socket (datagram, stream), the client
|
|
|
|
* socket address and port, the server socket address and port. In addition,
|
|
|
|
* it provides methods to map a transport address to a printable host name
|
|
|
|
* or address. Socket address information results are in static memory.
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
1996-11-26 19:48:08 +03:00
|
|
|
* The result from the hostname lookup method is STRING_PARANOID when a host
|
|
|
|
* pretends to have someone elses name, or when a host name is available but
|
|
|
|
* could not be verified.
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
1996-11-26 19:48:08 +03:00
|
|
|
* When lookup or conversion fails the result is set to STRING_UNKNOWN.
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
1996-11-26 19:48:08 +03:00
|
|
|
* Diagnostics are reported through syslog(3).
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
1996-11-26 19:48:08 +03:00
|
|
|
* Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 01:20:16 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-11-26 19:48:08 +03:00
|
|
|
#ifndef lint
|
1997-10-10 01:20:16 +04:00
|
|
|
#if 0
|
1999-08-27 20:06:17 +04:00
|
|
|
static char sccsid[] = "@(#) socket.c 1.15 97/03/21 19:27:24";
|
1997-10-10 01:20:16 +04:00
|
|
|
#else
|
2012-03-21 14:10:36 +04:00
|
|
|
__RCSID("$NetBSD: socket.c,v 1.19 2012/03/21 10:10:37 matt Exp $");
|
1997-10-10 01:20:16 +04:00
|
|
|
#endif
|
1996-11-26 19:48:08 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* System libraries. */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
1997-10-10 01:20:16 +04:00
|
|
|
#include <arpa/inet.h>
|
1996-11-26 19:48:08 +03:00
|
|
|
|
|
|
|
/* Local stuff. */
|
|
|
|
|
|
|
|
#include "tcpd.h"
|
|
|
|
|
|
|
|
/* Forward declarations. */
|
|
|
|
|
2002-06-07 05:36:09 +04:00
|
|
|
#ifdef APPEND_DOT
|
|
|
|
static const char *append_dot __P((const char *));
|
|
|
|
#endif
|
1997-10-10 01:20:16 +04:00
|
|
|
static void sock_sink __P((int));
|
1996-11-26 19:48:08 +03:00
|
|
|
|
2002-06-07 05:36:09 +04:00
|
|
|
#ifdef APPEND_DOT
|
|
|
|
/*
|
|
|
|
* Speed up DNS lookups by terminating the host name with a dot. Should be
|
|
|
|
* done with care. The speedup can give problems with lookups from sources
|
|
|
|
* that lack DNS-style trailing dot magic, such as local files or NIS maps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
append_dot(name)
|
2002-06-07 05:39:07 +04:00
|
|
|
const char *name;
|
2002-06-07 05:36:09 +04:00
|
|
|
{
|
|
|
|
static char hbuf[MAXHOSTNAMELEN + 1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't append dots to unqualified names. Such names are likely to come
|
|
|
|
* from local hosts files or from NIS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strchr(name, '.') == 0 || strlen(name) + 2 > sizeof(hbuf))
|
|
|
|
strlcpy(hbuf, name, sizeof(hbuf));
|
|
|
|
else
|
|
|
|
(void)snprintf(hbuf, sizeof(hbuf), "%s.", name);
|
|
|
|
return hbuf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1996-11-26 19:48:08 +03:00
|
|
|
/* sock_host - look up endpoint addresses and install conversion methods */
|
|
|
|
|
2012-03-21 14:10:36 +04:00
|
|
|
void
|
|
|
|
sock_host(struct request_info *request)
|
1996-11-26 19:48:08 +03:00
|
|
|
{
|
1999-08-31 17:58:58 +04:00
|
|
|
static struct sockaddr_storage client;
|
|
|
|
static struct sockaddr_storage server;
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t len;
|
1996-11-26 19:48:08 +03:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
int fd = request->fd;
|
|
|
|
|
|
|
|
sock_methods(request);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the client host address. Hal R. Brand <BRAND@addvax.llnl.gov>
|
|
|
|
* suggested how to get the client host info in case of UDP connections:
|
|
|
|
* peek at the first message without actually looking at its contents. We
|
|
|
|
* really should verify that client.sin_family gets the value AF_INET,
|
|
|
|
* but this program has already caused too much grief on systems with
|
|
|
|
* broken library code.
|
1999-08-31 17:58:58 +04:00
|
|
|
*
|
|
|
|
* XXX the last sentence is untrue as we support AF_INET6 as well :-)
|
1996-11-26 19:48:08 +03:00
|
|
|
*/
|
|
|
|
|
2003-03-30 04:32:52 +04:00
|
|
|
if (request->client->sin == NULL) {
|
|
|
|
len = sizeof(client);
|
2006-05-10 00:18:05 +04:00
|
|
|
if (getpeername(fd, (struct sockaddr *)(void *)& client, &len) < 0) {
|
2003-03-30 04:32:52 +04:00
|
|
|
request->sink = sock_sink;
|
|
|
|
len = sizeof(client);
|
|
|
|
if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK,
|
|
|
|
(struct sockaddr *) & client, &len) < 0) {
|
|
|
|
tcpd_warn("can't get client address: %m");
|
|
|
|
return; /* give up */
|
|
|
|
}
|
1996-11-26 19:48:08 +03:00
|
|
|
#ifdef really_paranoid
|
2003-03-30 04:32:52 +04:00
|
|
|
memset(buf, 0, sizeof(buf));
|
1996-11-26 19:48:08 +03:00
|
|
|
#endif
|
2003-03-30 04:32:52 +04:00
|
|
|
}
|
|
|
|
request->client->sin = (struct sockaddr *)&client;
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the server binding. This is used for client username
|
|
|
|
* lookups, and for access control rules that trigger on the server
|
|
|
|
* address or name.
|
|
|
|
*/
|
|
|
|
|
2003-03-30 04:32:52 +04:00
|
|
|
if (request->server->sin == NULL) {
|
|
|
|
len = sizeof(server);
|
|
|
|
if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
|
|
|
|
tcpd_warn("getsockname: %m");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request->server->sin = (struct sockaddr *)&server;
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_hostaddr - map endpoint address to printable form */
|
|
|
|
|
2012-03-21 14:10:36 +04:00
|
|
|
void
|
|
|
|
sock_hostaddr(struct host_info *host)
|
1996-11-26 19:48:08 +03:00
|
|
|
{
|
1999-08-31 17:58:58 +04:00
|
|
|
struct sockaddr *sa = host->sin;
|
1996-11-26 19:48:08 +03:00
|
|
|
|
1999-08-31 17:58:58 +04:00
|
|
|
if (!sa)
|
|
|
|
return;
|
|
|
|
host->addr[0] = '\0';
|
2002-06-07 01:28:49 +04:00
|
|
|
getnameinfo(sa, sa->sa_len, host->addr, sizeof(host->addr),
|
|
|
|
NULL, 0, NI_NUMERICHOST);
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_hostname - map endpoint address to host name */
|
|
|
|
|
2012-03-21 14:10:36 +04:00
|
|
|
void
|
|
|
|
sock_hostname(struct host_info *host)
|
1996-11-26 19:48:08 +03:00
|
|
|
{
|
2002-06-07 01:28:49 +04:00
|
|
|
struct sockaddr *sa = host->sin;
|
|
|
|
char h1[NI_MAXHOST], h2[NI_MAXHOST];
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
#ifdef INET6
|
|
|
|
struct sockaddr_in tmp;
|
|
|
|
#endif
|
1996-11-26 19:48:08 +03:00
|
|
|
|
2002-06-07 01:28:49 +04:00
|
|
|
if (!sa)
|
1999-08-31 17:58:58 +04:00
|
|
|
return;
|
|
|
|
#ifdef INET6
|
2002-06-07 01:28:49 +04:00
|
|
|
/* special case on reverse lookup: mapped addr. I hate it */
|
|
|
|
if (sa->sa_family == AF_INET6 &&
|
|
|
|
IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
|
|
|
|
memset(&tmp, 0, sizeof(tmp));
|
|
|
|
tmp.sin_family = AF_INET;
|
|
|
|
tmp.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
memcpy(&tmp.sin_addr,
|
|
|
|
&((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[12], 4);
|
|
|
|
sa = (struct sockaddr *)&tmp;
|
|
|
|
}
|
1999-08-31 17:58:58 +04:00
|
|
|
#endif
|
2002-06-07 01:28:49 +04:00
|
|
|
if (getnameinfo(sa, sa->sa_len, h1, sizeof(h1), NULL, 0,
|
|
|
|
NI_NUMERICHOST) != 0) {
|
1999-08-31 17:58:58 +04:00
|
|
|
return;
|
|
|
|
}
|
2002-06-07 01:28:49 +04:00
|
|
|
if (getnameinfo(sa, sa->sa_len, host->name, sizeof(host->name), NULL, 0,
|
|
|
|
NI_NAMEREQD) == 0) {
|
2003-05-26 14:05:07 +04:00
|
|
|
/*
|
|
|
|
* if reverse lookup result looks like a numeric hostname,
|
|
|
|
* someone is trying to trick us by PTR record like following:
|
|
|
|
* 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
|
|
|
|
*/
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
|
|
|
#ifdef APPEND_DOT
|
|
|
|
if (getaddrinfo(append_dot(host->name), "0", &hints, &res0) == 0)
|
|
|
|
#else
|
|
|
|
if (getaddrinfo(host->name, "0", &hints, &res0) == 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
tcpd_warn("Nasty PTR record is configured");
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
/* name is bad, clobber it */
|
|
|
|
(void)strlcpy(host->name, paranoid, sizeof(host->name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1996-11-26 19:48:08 +03:00
|
|
|
/*
|
|
|
|
* Verify that the address is a member of the address list returned
|
2002-06-07 01:28:49 +04:00
|
|
|
* by getaddrinfo(hostname).
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
2002-06-07 01:28:49 +04:00
|
|
|
* Verify also that getnameinfo() and getaddrinfo() return the same
|
1996-11-26 19:48:08 +03:00
|
|
|
* hostname, or rshd and rlogind may still end up being spoofed.
|
1999-07-03 16:30:16 +04:00
|
|
|
*
|
2002-06-07 01:28:49 +04:00
|
|
|
* On some sites, getaddrinfo("localhost") returns "localhost.domain".
|
1996-11-26 19:48:08 +03:00
|
|
|
* This is a DNS artefact. We treat it as a special case. When we
|
2002-06-07 01:28:49 +04:00
|
|
|
* can't believe the address list from getaddrinfo("localhost")
|
1996-11-26 19:48:08 +03:00
|
|
|
* we're in big trouble anyway.
|
|
|
|
*/
|
2002-06-07 01:28:49 +04:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = sa->sa_family;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
2002-06-07 05:36:09 +04:00
|
|
|
#ifdef APPEND_DOT
|
|
|
|
if (getaddrinfo(append_dot(host->name), "0", &hints, &res0) != 0)
|
|
|
|
#else
|
|
|
|
if (getaddrinfo(host->name, "0", &hints, &res0) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
1996-11-26 19:48:08 +03:00
|
|
|
/*
|
|
|
|
* Unable to verify that the host name matches the address. This
|
|
|
|
* may be a transient problem or a botched name server setup.
|
|
|
|
*/
|
|
|
|
|
2002-06-07 01:28:49 +04:00
|
|
|
tcpd_warn("can't verify hostname: getaddrinfo(%s, %d) failed",
|
|
|
|
host->name, hints.ai_family);
|
|
|
|
} else if (res0->ai_canonname &&
|
|
|
|
STR_NE(host->name, res0->ai_canonname) &&
|
|
|
|
STR_NE(host->name, "localhost")) {
|
1996-11-26 19:48:08 +03:00
|
|
|
/*
|
2002-06-07 01:28:49 +04:00
|
|
|
* The getnameinfo() and getaddrinfo() calls did not return
|
1996-11-26 19:48:08 +03:00
|
|
|
* the same hostname. This could be a nameserver configuration
|
|
|
|
* problem. It could also be that someone is trying to spoof us.
|
|
|
|
*/
|
|
|
|
|
2002-06-07 01:28:49 +04:00
|
|
|
tcpd_warn("host name/name mismatch: %s != %s",
|
|
|
|
host->name, res0->ai_canonname);
|
|
|
|
freeaddrinfo(res0);
|
1996-11-26 19:48:08 +03:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The address should be a member of the address list returned by
|
2002-06-07 01:28:49 +04:00
|
|
|
* getaddrinfo().
|
1996-11-26 19:48:08 +03:00
|
|
|
*/
|
|
|
|
|
2002-06-07 01:28:49 +04:00
|
|
|
for (res = res0; res; res = res->ai_next) {
|
|
|
|
if (getnameinfo(res->ai_addr, res->ai_addrlen, h2, sizeof(h2),
|
|
|
|
NULL, 0, NI_NUMERICHOST) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (STR_EQ(h1, h2)) {
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
return;
|
|
|
|
}
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The host name does not map to the initial address. Perhaps
|
|
|
|
* someone has messed up. Perhaps someone compromised a name
|
|
|
|
* server.
|
|
|
|
*/
|
|
|
|
|
2002-06-07 01:28:49 +04:00
|
|
|
tcpd_warn("host name/address mismatch: %s != %s", h1,
|
|
|
|
res0->ai_canonname ? res0->ai_canonname : "?");
|
|
|
|
|
|
|
|
freeaddrinfo(res0);
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
1997-01-23 17:01:45 +03:00
|
|
|
/* name is bad, clobber it */
|
2002-06-07 01:42:42 +04:00
|
|
|
(void)strlcpy(host->name, paranoid, sizeof(host->name));
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_sink - absorb unreceived IP datagram */
|
|
|
|
|
2012-03-21 14:10:36 +04:00
|
|
|
static void
|
|
|
|
sock_sink(int fd)
|
1996-11-26 19:48:08 +03:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2002-06-07 01:42:42 +04:00
|
|
|
struct sockaddr_storage ss;
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t size = sizeof(ss);
|
1996-11-26 19:48:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Eat up the not-yet received datagram. Some systems insist on a
|
|
|
|
* non-zero source address argument in the recvfrom() call below.
|
|
|
|
*/
|
|
|
|
|
2002-06-07 01:42:42 +04:00
|
|
|
(void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & ss, &size);
|
1996-11-26 19:48:08 +03:00
|
|
|
}
|