Home | History | Annotate | Line # | Download | only in libwrap
socket.c revision 1.14
      1 /*	$NetBSD: socket.c,v 1.14 2002/06/07 01:39:07 itojun 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.15 97/03/21 19:27:24";
     24 #else
     25 __RCSID("$NetBSD: socket.c,v 1.14 2002/06/07 01:39:07 itojun 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 #ifdef APPEND_DOT
     48 static const char *append_dot __P((const char *));
     49 #endif
     50 static void sock_sink __P((int));
     51 
     52 #ifdef APPEND_DOT
     53  /*
     54   * Speed up DNS lookups by terminating the host name with a dot. Should be
     55   * done with care. The speedup can give problems with lookups from sources
     56   * that lack DNS-style trailing dot magic, such as local files or NIS maps.
     57   */
     58 
     59 static const char *
     60 append_dot(name)
     61 const char *name;
     62 {
     63     static char hbuf[MAXHOSTNAMELEN + 1];
     64 
     65     /*
     66      * Don't append dots to unqualified names. Such names are likely to come
     67      * from local hosts files or from NIS.
     68      */
     69 
     70     if (strchr(name, '.') == 0 || strlen(name) + 2 > sizeof(hbuf))
     71 	strlcpy(hbuf, name, sizeof(hbuf));
     72     else
     73 	(void)snprintf(hbuf, sizeof(hbuf), "%s.", name);
     74     return hbuf;
     75 }
     76 #endif
     77 
     78 /* sock_host - look up endpoint addresses and install conversion methods */
     79 
     80 void    sock_host(request)
     81 struct request_info *request;
     82 {
     83     static struct sockaddr_storage client;
     84     static struct sockaddr_storage server;
     85     int     len;
     86     char    buf[BUFSIZ];
     87     int     fd = request->fd;
     88 
     89     sock_methods(request);
     90 
     91     /*
     92      * Look up the client host address. Hal R. Brand <BRAND (at) addvax.llnl.gov>
     93      * suggested how to get the client host info in case of UDP connections:
     94      * peek at the first message without actually looking at its contents. We
     95      * really should verify that client.sin_family gets the value AF_INET,
     96      * but this program has already caused too much grief on systems with
     97      * broken library code.
     98      *
     99      * XXX the last sentence is untrue as we support AF_INET6 as well :-)
    100      */
    101 
    102     len = sizeof(client);
    103     if (getpeername(fd, (struct sockaddr *) & client, &len) < 0) {
    104 	request->sink = sock_sink;
    105 	len = sizeof(client);
    106 	if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK,
    107 		     (struct sockaddr *) & client, &len) < 0) {
    108 	    tcpd_warn("can't get client address: %m");
    109 	    return;				/* give up */
    110 	}
    111 #ifdef really_paranoid
    112 	memset(buf, 0 sizeof(buf));
    113 #endif
    114     }
    115     request->client->sin = (struct sockaddr *)&client;
    116 
    117     /*
    118      * Determine the server binding. This is used for client username
    119      * lookups, and for access control rules that trigger on the server
    120      * address or name.
    121      */
    122 
    123     len = sizeof(server);
    124     if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
    125 	tcpd_warn("getsockname: %m");
    126 	return;
    127     }
    128     request->server->sin = (struct sockaddr *)&server;
    129 }
    130 
    131 /* sock_hostaddr - map endpoint address to printable form */
    132 
    133 void    sock_hostaddr(host)
    134 struct host_info *host;
    135 {
    136     struct sockaddr *sa = host->sin;
    137 
    138     if (!sa)
    139 	return;
    140     host->addr[0] = '\0';
    141     getnameinfo(sa, sa->sa_len, host->addr, sizeof(host->addr),
    142 	NULL, 0, NI_NUMERICHOST);
    143 }
    144 
    145 /* sock_hostname - map endpoint address to host name */
    146 
    147 void    sock_hostname(host)
    148 struct host_info *host;
    149 {
    150     struct sockaddr *sa = host->sin;
    151     char h1[NI_MAXHOST], h2[NI_MAXHOST];
    152     struct addrinfo hints, *res, *res0;
    153 #ifdef INET6
    154     struct sockaddr_in tmp;
    155 #endif
    156 
    157     if (!sa)
    158 	return;
    159 #ifdef INET6
    160     /* special case on reverse lookup: mapped addr.  I hate it */
    161     if (sa->sa_family == AF_INET6 &&
    162         IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
    163 	memset(&tmp, 0, sizeof(tmp));
    164 	tmp.sin_family = AF_INET;
    165 	tmp.sin_len = sizeof(struct sockaddr_in);
    166 	memcpy(&tmp.sin_addr,
    167 	    &((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[12], 4);
    168 	sa = (struct sockaddr *)&tmp;
    169     }
    170 #endif
    171     if (getnameinfo(sa, sa->sa_len, h1, sizeof(h1), NULL, 0,
    172         NI_NUMERICHOST) != 0) {
    173 	return;
    174     }
    175     if (getnameinfo(sa, sa->sa_len, host->name, sizeof(host->name), NULL, 0,
    176         NI_NAMEREQD) == 0) {
    177 	/*
    178 	 * Verify that the address is a member of the address list returned
    179 	 * by getaddrinfo(hostname).
    180 	 *
    181 	 * Verify also that getnameinfo() and getaddrinfo() return the same
    182 	 * hostname, or rshd and rlogind may still end up being spoofed.
    183 	 *
    184 	 * On some sites, getaddrinfo("localhost") returns "localhost.domain".
    185 	 * This is a DNS artefact. We treat it as a special case. When we
    186 	 * can't believe the address list from getaddrinfo("localhost")
    187 	 * we're in big trouble anyway.
    188 	 */
    189 	memset(&hints, 0, sizeof(hints));
    190 	hints.ai_family = sa->sa_family;
    191 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    192 	hints.ai_flags = AI_CANONNAME;
    193 #ifdef APPEND_DOT
    194 	if (getaddrinfo(append_dot(host->name), "0", &hints, &res0) != 0)
    195 #else
    196 	if (getaddrinfo(host->name, "0", &hints, &res0) != 0)
    197 #endif
    198 	{
    199 	    /*
    200 	     * Unable to verify that the host name matches the address. This
    201 	     * may be a transient problem or a botched name server setup.
    202 	     */
    203 
    204 	    tcpd_warn("can't verify hostname: getaddrinfo(%s, %d) failed",
    205 	        host->name, hints.ai_family);
    206 	} else if (res0->ai_canonname &&
    207 	    STR_NE(host->name, res0->ai_canonname) &&
    208 	    STR_NE(host->name, "localhost")) {
    209 	    /*
    210 	     * The getnameinfo() and getaddrinfo() calls did not return
    211 	     * the same hostname. This could be a nameserver configuration
    212 	     * problem. It could also be that someone is trying to spoof us.
    213 	     */
    214 
    215 	    tcpd_warn("host name/name mismatch: %s != %s",
    216 		host->name, res0->ai_canonname);
    217 	    freeaddrinfo(res0);
    218 	} else {
    219 	    /*
    220 	     * The address should be a member of the address list returned by
    221 	     * getaddrinfo().
    222 	     */
    223 
    224 	    for (res = res0; res; res = res->ai_next) {
    225 		if (getnameinfo(res->ai_addr, res->ai_addrlen, h2, sizeof(h2),
    226 		    NULL, 0, NI_NUMERICHOST) != 0) {
    227 		    continue;
    228 		}
    229 		if (STR_EQ(h1, h2)) {
    230 		    freeaddrinfo(res0);
    231 		    return;
    232 		}
    233 	    }
    234 
    235 	    /*
    236 	     * The host name does not map to the initial address. Perhaps
    237 	     * someone has messed up. Perhaps someone compromised a name
    238 	     * server.
    239 	     */
    240 
    241 	    tcpd_warn("host name/address mismatch: %s != %s", h1,
    242 		res0->ai_canonname ? res0->ai_canonname : "?");
    243 
    244 	    freeaddrinfo(res0);
    245 	}
    246 	/* name is bad, clobber it */
    247 	(void)strlcpy(host->name, paranoid, sizeof(host->name));
    248     }
    249 }
    250 
    251 /* sock_sink - absorb unreceived IP datagram */
    252 
    253 static void sock_sink(fd)
    254 int     fd;
    255 {
    256     char    buf[BUFSIZ];
    257     struct sockaddr_storage ss;
    258     int     size = sizeof(ss);
    259 
    260     /*
    261      * Eat up the not-yet received datagram. Some systems insist on a
    262      * non-zero source address argument in the recvfrom() call below.
    263      */
    264 
    265     (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & ss, &size);
    266 }
    267