12aef6930SMark Murray /* 22aef6930SMark Murray * This module determines the type of socket (datagram, stream), the client 32aef6930SMark Murray * socket address and port, the server socket address and port. In addition, 42aef6930SMark Murray * it provides methods to map a transport address to a printable host name 52aef6930SMark Murray * or address. Socket address information results are in static memory. 62aef6930SMark Murray * 72aef6930SMark Murray * The result from the hostname lookup method is STRING_PARANOID when a host 82aef6930SMark Murray * pretends to have someone elses name, or when a host name is available but 92aef6930SMark Murray * could not be verified. 102aef6930SMark Murray * 112aef6930SMark Murray * When lookup or conversion fails the result is set to STRING_UNKNOWN. 122aef6930SMark Murray * 132aef6930SMark Murray * Diagnostics are reported through syslog(3). 142aef6930SMark Murray * 152aef6930SMark Murray * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 168053080cSYoshinobu Inoue * 178053080cSYoshinobu Inoue * $FreeBSD$ 182aef6930SMark Murray */ 192aef6930SMark Murray 202aef6930SMark Murray #ifndef lint 212aef6930SMark Murray static char sccsid[] = "@(#) socket.c 1.15 97/03/21 19:27:24"; 222aef6930SMark Murray #endif 232aef6930SMark Murray 242aef6930SMark Murray /* System libraries. */ 252aef6930SMark Murray 262aef6930SMark Murray #include <sys/types.h> 272aef6930SMark Murray #include <sys/param.h> 282aef6930SMark Murray #include <sys/socket.h> 292aef6930SMark Murray #include <netinet/in.h> 302aef6930SMark Murray #include <netdb.h> 312aef6930SMark Murray #include <stdio.h> 322aef6930SMark Murray #include <syslog.h> 332aef6930SMark Murray #include <string.h> 342aef6930SMark Murray 358053080cSYoshinobu Inoue #ifdef INET6 368053080cSYoshinobu Inoue #ifndef USE_GETIPNODEBY 378053080cSYoshinobu Inoue #include <resolv.h> 388053080cSYoshinobu Inoue #endif 398053080cSYoshinobu Inoue #endif 408053080cSYoshinobu Inoue 412aef6930SMark Murray extern char *inet_ntoa(); 422aef6930SMark Murray 432aef6930SMark Murray /* Local stuff. */ 442aef6930SMark Murray 452aef6930SMark Murray #include "tcpd.h" 462aef6930SMark Murray 472aef6930SMark Murray /* Forward declarations. */ 482aef6930SMark Murray 492aef6930SMark Murray static void sock_sink(); 502aef6930SMark Murray 512aef6930SMark Murray #ifdef APPEND_DOT 522aef6930SMark Murray 532aef6930SMark Murray /* 542aef6930SMark Murray * Speed up DNS lookups by terminating the host name with a dot. Should be 552aef6930SMark Murray * done with care. The speedup can give problems with lookups from sources 562aef6930SMark Murray * that lack DNS-style trailing dot magic, such as local files or NIS maps. 572aef6930SMark Murray */ 582aef6930SMark Murray 592aef6930SMark Murray static struct hostent *gethostbyname_dot(name) 602aef6930SMark Murray char *name; 612aef6930SMark Murray { 622aef6930SMark Murray char dot_name[MAXHOSTNAMELEN + 1]; 632aef6930SMark Murray 642aef6930SMark Murray /* 652aef6930SMark Murray * Don't append dots to unqualified names. Such names are likely to come 662aef6930SMark Murray * from local hosts files or from NIS. 672aef6930SMark Murray */ 682aef6930SMark Murray 692aef6930SMark Murray if (strchr(name, '.') == 0 || strlen(name) >= MAXHOSTNAMELEN - 1) { 702aef6930SMark Murray return (gethostbyname(name)); 712aef6930SMark Murray } else { 722aef6930SMark Murray sprintf(dot_name, "%s.", name); 732aef6930SMark Murray return (gethostbyname(dot_name)); 742aef6930SMark Murray } 752aef6930SMark Murray } 762aef6930SMark Murray 772aef6930SMark Murray #define gethostbyname gethostbyname_dot 782aef6930SMark Murray #endif 792aef6930SMark Murray 802aef6930SMark Murray /* sock_host - look up endpoint addresses and install conversion methods */ 812aef6930SMark Murray 822aef6930SMark Murray void sock_host(request) 832aef6930SMark Murray struct request_info *request; 842aef6930SMark Murray { 858053080cSYoshinobu Inoue #ifdef INET6 868053080cSYoshinobu Inoue static struct sockaddr_storage client; 878053080cSYoshinobu Inoue static struct sockaddr_storage server; 888053080cSYoshinobu Inoue #else 892aef6930SMark Murray static struct sockaddr_in client; 902aef6930SMark Murray static struct sockaddr_in server; 918053080cSYoshinobu Inoue #endif 922aef6930SMark Murray int len; 932aef6930SMark Murray char buf[BUFSIZ]; 942aef6930SMark Murray int fd = request->fd; 952aef6930SMark Murray 962aef6930SMark Murray sock_methods(request); 972aef6930SMark Murray 982aef6930SMark Murray /* 992aef6930SMark Murray * Look up the client host address. Hal R. Brand <BRAND@addvax.llnl.gov> 1002aef6930SMark Murray * suggested how to get the client host info in case of UDP connections: 1012aef6930SMark Murray * peek at the first message without actually looking at its contents. We 1022aef6930SMark Murray * really should verify that client.sin_family gets the value AF_INET, 1032aef6930SMark Murray * but this program has already caused too much grief on systems with 1042aef6930SMark Murray * broken library code. 1052aef6930SMark Murray */ 1062aef6930SMark Murray 1072aef6930SMark Murray len = sizeof(client); 1082aef6930SMark Murray if (getpeername(fd, (struct sockaddr *) & client, &len) < 0) { 1092aef6930SMark Murray request->sink = sock_sink; 1102aef6930SMark Murray len = sizeof(client); 1112aef6930SMark Murray if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK, 1122aef6930SMark Murray (struct sockaddr *) & client, &len) < 0) { 1132aef6930SMark Murray tcpd_warn("can't get client address: %m"); 1142aef6930SMark Murray return; /* give up */ 1152aef6930SMark Murray } 1162aef6930SMark Murray #ifdef really_paranoid 1172aef6930SMark Murray memset(buf, 0 sizeof(buf)); 1182aef6930SMark Murray #endif 1192aef6930SMark Murray } 1208053080cSYoshinobu Inoue #ifdef INET6 1218053080cSYoshinobu Inoue request->client->sin = (struct sockaddr *)&client; 1228053080cSYoshinobu Inoue #else 1232aef6930SMark Murray request->client->sin = &client; 1248053080cSYoshinobu Inoue #endif 1252aef6930SMark Murray 1262aef6930SMark Murray /* 1272aef6930SMark Murray * Determine the server binding. This is used for client username 1282aef6930SMark Murray * lookups, and for access control rules that trigger on the server 1292aef6930SMark Murray * address or name. 1302aef6930SMark Murray */ 1312aef6930SMark Murray 1322aef6930SMark Murray len = sizeof(server); 1332aef6930SMark Murray if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) { 1342aef6930SMark Murray tcpd_warn("getsockname: %m"); 1352aef6930SMark Murray return; 1362aef6930SMark Murray } 1378053080cSYoshinobu Inoue #ifdef INET6 1388053080cSYoshinobu Inoue request->server->sin = (struct sockaddr *)&server; 1398053080cSYoshinobu Inoue #else 1402aef6930SMark Murray request->server->sin = &server; 1418053080cSYoshinobu Inoue #endif 1422aef6930SMark Murray } 1432aef6930SMark Murray 1442aef6930SMark Murray /* sock_hostaddr - map endpoint address to printable form */ 1452aef6930SMark Murray 1462aef6930SMark Murray void sock_hostaddr(host) 1472aef6930SMark Murray struct host_info *host; 1482aef6930SMark Murray { 1498053080cSYoshinobu Inoue #ifdef INET6 1508053080cSYoshinobu Inoue struct sockaddr *sin = host->sin; 1518053080cSYoshinobu Inoue char *ap; 1528053080cSYoshinobu Inoue int alen; 1538053080cSYoshinobu Inoue 1548053080cSYoshinobu Inoue if (!sin) 1558053080cSYoshinobu Inoue return; 1568053080cSYoshinobu Inoue switch (sin->sa_family) { 1578053080cSYoshinobu Inoue case AF_INET: 1588053080cSYoshinobu Inoue ap = (char *)&((struct sockaddr_in *)sin)->sin_addr; 1598053080cSYoshinobu Inoue alen = sizeof(struct in_addr); 1608053080cSYoshinobu Inoue break; 1618053080cSYoshinobu Inoue case AF_INET6: 1628053080cSYoshinobu Inoue ap = (char *)&((struct sockaddr_in6 *)sin)->sin6_addr; 1638053080cSYoshinobu Inoue alen = sizeof(struct in6_addr); 1648053080cSYoshinobu Inoue break; 1658053080cSYoshinobu Inoue default: 1668053080cSYoshinobu Inoue return; 1678053080cSYoshinobu Inoue } 1688053080cSYoshinobu Inoue host->addr[0] = '\0'; 1698053080cSYoshinobu Inoue inet_ntop(sin->sa_family, ap, host->addr, sizeof(host->addr)); 1708053080cSYoshinobu Inoue #else 1712aef6930SMark Murray struct sockaddr_in *sin = host->sin; 1722aef6930SMark Murray 1732aef6930SMark Murray if (sin != 0) 1742aef6930SMark Murray STRN_CPY(host->addr, inet_ntoa(sin->sin_addr), sizeof(host->addr)); 1758053080cSYoshinobu Inoue #endif 1762aef6930SMark Murray } 1772aef6930SMark Murray 1782aef6930SMark Murray /* sock_hostname - map endpoint address to host name */ 1792aef6930SMark Murray 1802aef6930SMark Murray void sock_hostname(host) 1812aef6930SMark Murray struct host_info *host; 1822aef6930SMark Murray { 1838053080cSYoshinobu Inoue #ifdef INET6 1848053080cSYoshinobu Inoue struct sockaddr *sin = host->sin; 1858053080cSYoshinobu Inoue char addr[128]; 1868053080cSYoshinobu Inoue #ifdef USE_GETIPNODEBY 1878053080cSYoshinobu Inoue int h_error; 1888053080cSYoshinobu Inoue #else 1898053080cSYoshinobu Inoue u_long res_options; 1908053080cSYoshinobu Inoue #endif 1918053080cSYoshinobu Inoue struct hostent *hp = NULL; 1928053080cSYoshinobu Inoue char *ap; 1938053080cSYoshinobu Inoue int alen; 1948053080cSYoshinobu Inoue #else 1952aef6930SMark Murray struct sockaddr_in *sin = host->sin; 1962aef6930SMark Murray struct hostent *hp; 1978053080cSYoshinobu Inoue #endif 1982aef6930SMark Murray int i; 1992aef6930SMark Murray 2002aef6930SMark Murray /* 2012aef6930SMark Murray * On some systems, for example Solaris 2.3, gethostbyaddr(0.0.0.0) does 2022aef6930SMark Murray * not fail. Instead it returns "INADDR_ANY". Unfortunately, this does 2032aef6930SMark Murray * not work the other way around: gethostbyname("INADDR_ANY") fails. We 2042aef6930SMark Murray * have to special-case 0.0.0.0, in order to avoid false alerts from the 2052aef6930SMark Murray * host name/address checking code below. 2062aef6930SMark Murray */ 2078053080cSYoshinobu Inoue #ifdef INET6 2088053080cSYoshinobu Inoue if (sin != NULL) { 2098053080cSYoshinobu Inoue switch (sin->sa_family) { 2108053080cSYoshinobu Inoue case AF_INET: 2118053080cSYoshinobu Inoue if (((struct sockaddr_in *)sin)->sin_addr.s_addr == 0) { 2128053080cSYoshinobu Inoue strcpy(host->name, paranoid); /* name is bad, clobber it */ 2138053080cSYoshinobu Inoue return; 2148053080cSYoshinobu Inoue } 2158053080cSYoshinobu Inoue ap = (char *) &((struct sockaddr_in *)sin)->sin_addr; 2168053080cSYoshinobu Inoue alen = sizeof(struct in_addr); 2178053080cSYoshinobu Inoue break; 2188053080cSYoshinobu Inoue case AF_INET6: 2198053080cSYoshinobu Inoue ap = (char *) &((struct sockaddr_in6 *)sin)->sin6_addr; 2208053080cSYoshinobu Inoue alen = sizeof(struct in6_addr); 2218053080cSYoshinobu Inoue break; 2228053080cSYoshinobu Inoue defalut: 2238053080cSYoshinobu Inoue strcpy(host->name, paranoid); /* name is bad, clobber it */ 2248053080cSYoshinobu Inoue return; 2258053080cSYoshinobu Inoue } 2268053080cSYoshinobu Inoue #ifdef USE_GETIPNODEBY 2278053080cSYoshinobu Inoue hp = getipnodebyaddr(ap, alen, sin->sa_family, &h_error); 2288053080cSYoshinobu Inoue #else 2298053080cSYoshinobu Inoue hp = gethostbyaddr(ap, alen, sin->sa_family); 2308053080cSYoshinobu Inoue #endif 2318053080cSYoshinobu Inoue } 2328053080cSYoshinobu Inoue if (hp) { 2338053080cSYoshinobu Inoue #else 2342aef6930SMark Murray if (sin != 0 && sin->sin_addr.s_addr != 0 2352aef6930SMark Murray && (hp = gethostbyaddr((char *) &(sin->sin_addr), 2362aef6930SMark Murray sizeof(sin->sin_addr), AF_INET)) != 0) { 2378053080cSYoshinobu Inoue #endif 2382aef6930SMark Murray 2392aef6930SMark Murray STRN_CPY(host->name, hp->h_name, sizeof(host->name)); 2408053080cSYoshinobu Inoue #if defined(INET6) && defined(USE_GETIPNODEBY) 2418053080cSYoshinobu Inoue freehostent(hp); 2428053080cSYoshinobu Inoue #endif 2432aef6930SMark Murray 2442aef6930SMark Murray /* 2452aef6930SMark Murray * Verify that the address is a member of the address list returned 2462aef6930SMark Murray * by gethostbyname(hostname). 2472aef6930SMark Murray * 2482aef6930SMark Murray * Verify also that gethostbyaddr() and gethostbyname() return the same 2492aef6930SMark Murray * hostname, or rshd and rlogind may still end up being spoofed. 2502aef6930SMark Murray * 2512aef6930SMark Murray * On some sites, gethostbyname("localhost") returns "localhost.domain". 2522aef6930SMark Murray * This is a DNS artefact. We treat it as a special case. When we 2532aef6930SMark Murray * can't believe the address list from gethostbyname("localhost") 2542aef6930SMark Murray * we're in big trouble anyway. 2552aef6930SMark Murray */ 2562aef6930SMark Murray 2578053080cSYoshinobu Inoue #ifdef INET6 2588053080cSYoshinobu Inoue #ifdef USE_GETIPNODEBY 2598053080cSYoshinobu Inoue hp = getipnodebyname(host->name, sin->sa_family, 2608053080cSYoshinobu Inoue AI_V4MAPPED | AI_ADDRCONFIG | AI_ALL, &h_error); 2618053080cSYoshinobu Inoue #else 2628053080cSYoshinobu Inoue if ((_res.options & RES_INIT) == 0) { 2638053080cSYoshinobu Inoue if (res_init() < 0) { 2648053080cSYoshinobu Inoue inet_ntop(sin->sa_family, ap, addr, sizeof(addr)); 2658053080cSYoshinobu Inoue tcpd_warn("can't verify hostname: res_init() for %s failed", 2668053080cSYoshinobu Inoue addr); 2678053080cSYoshinobu Inoue strcpy(host->name, paranoid); /* name is bad, clobber it */ 2688053080cSYoshinobu Inoue return; 2698053080cSYoshinobu Inoue } 2708053080cSYoshinobu Inoue } 2718053080cSYoshinobu Inoue res_options = _res.options; 2728053080cSYoshinobu Inoue if (sin->sa_family == AF_INET6) 2738053080cSYoshinobu Inoue _res.options |= RES_USE_INET6; 2748053080cSYoshinobu Inoue else 2758053080cSYoshinobu Inoue _res.options &= ~RES_USE_INET6; 2768053080cSYoshinobu Inoue hp = gethostbyname2(host->name, 2778053080cSYoshinobu Inoue (sin->sa_family == AF_INET6 && 2788053080cSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sin)->sin6_addr)) ? 2798053080cSYoshinobu Inoue AF_INET : sin->sa_family); 2808053080cSYoshinobu Inoue _res.options = res_options; 2818053080cSYoshinobu Inoue #endif 2828053080cSYoshinobu Inoue if (!hp) { 2838053080cSYoshinobu Inoue #else 2842aef6930SMark Murray if ((hp = gethostbyname(host->name)) == 0) { 2858053080cSYoshinobu Inoue #endif 2862aef6930SMark Murray 2872aef6930SMark Murray /* 2882aef6930SMark Murray * Unable to verify that the host name matches the address. This 2892aef6930SMark Murray * may be a transient problem or a botched name server setup. 2902aef6930SMark Murray */ 2912aef6930SMark Murray 2928053080cSYoshinobu Inoue #ifdef INET6 2938053080cSYoshinobu Inoue #ifdef USE_GETIPNODEBY 2948053080cSYoshinobu Inoue tcpd_warn("can't verify hostname: getipnodebyname(%s, %s) failed", 2958053080cSYoshinobu Inoue #else 2968053080cSYoshinobu Inoue tcpd_warn("can't verify hostname: gethostbyname2(%s, %s) failed", 2978053080cSYoshinobu Inoue #endif 2988053080cSYoshinobu Inoue host->name, 2998053080cSYoshinobu Inoue (sin->sa_family == AF_INET) ? "AF_INET" : "AF_INET6"); 3008053080cSYoshinobu Inoue #else 3012aef6930SMark Murray tcpd_warn("can't verify hostname: gethostbyname(%s) failed", 3022aef6930SMark Murray host->name); 3038053080cSYoshinobu Inoue #endif 3042aef6930SMark Murray 3052aef6930SMark Murray } else if (STR_NE(host->name, hp->h_name) 3062aef6930SMark Murray && STR_NE(host->name, "localhost")) { 3072aef6930SMark Murray 3082aef6930SMark Murray /* 3092aef6930SMark Murray * The gethostbyaddr() and gethostbyname() calls did not return 3102aef6930SMark Murray * the same hostname. This could be a nameserver configuration 3112aef6930SMark Murray * problem. It could also be that someone is trying to spoof us. 3122aef6930SMark Murray */ 3132aef6930SMark Murray 3142aef6930SMark Murray tcpd_warn("host name/name mismatch: %s != %.*s", 3152aef6930SMark Murray host->name, STRING_LENGTH, hp->h_name); 3162aef6930SMark Murray 3172aef6930SMark Murray } else { 3182aef6930SMark Murray 3192aef6930SMark Murray /* 3202aef6930SMark Murray * The address should be a member of the address list returned by 3212aef6930SMark Murray * gethostbyname(). We should first verify that the h_addrtype 3222aef6930SMark Murray * field is AF_INET, but this program has already caused too much 3232aef6930SMark Murray * grief on systems with broken library code. 3242aef6930SMark Murray */ 3252aef6930SMark Murray 3262aef6930SMark Murray for (i = 0; hp->h_addr_list[i]; i++) { 3278053080cSYoshinobu Inoue #ifdef INET6 3288053080cSYoshinobu Inoue if (memcmp(hp->h_addr_list[i], ap, alen) == 0) { 3298053080cSYoshinobu Inoue #ifdef USE_GETIPNODEBY 3308053080cSYoshinobu Inoue freehostent(hp); 3318053080cSYoshinobu Inoue #endif 3328053080cSYoshinobu Inoue return; /* name is good, keep it */ 3338053080cSYoshinobu Inoue } 3348053080cSYoshinobu Inoue #else 3352aef6930SMark Murray if (memcmp(hp->h_addr_list[i], 3362aef6930SMark Murray (char *) &sin->sin_addr, 3372aef6930SMark Murray sizeof(sin->sin_addr)) == 0) 3382aef6930SMark Murray return; /* name is good, keep it */ 3398053080cSYoshinobu Inoue #endif 3402aef6930SMark Murray } 3412aef6930SMark Murray 3422aef6930SMark Murray /* 3432aef6930SMark Murray * The host name does not map to the initial address. Perhaps 3442aef6930SMark Murray * someone has messed up. Perhaps someone compromised a name 3452aef6930SMark Murray * server. 3462aef6930SMark Murray */ 3472aef6930SMark Murray 3488053080cSYoshinobu Inoue #ifdef INET6 3498053080cSYoshinobu Inoue inet_ntop(sin->sa_family, ap, addr, sizeof(addr)); 3508053080cSYoshinobu Inoue tcpd_warn("host name/address mismatch: %s != %.*s", 3518053080cSYoshinobu Inoue addr, STRING_LENGTH, hp->h_name); 3528053080cSYoshinobu Inoue #else 3532aef6930SMark Murray tcpd_warn("host name/address mismatch: %s != %.*s", 3542aef6930SMark Murray inet_ntoa(sin->sin_addr), STRING_LENGTH, hp->h_name); 3558053080cSYoshinobu Inoue #endif 3562aef6930SMark Murray } 3572aef6930SMark Murray strcpy(host->name, paranoid); /* name is bad, clobber it */ 3588053080cSYoshinobu Inoue #if defined(INET6) && defined(USE_GETIPNODEBY) 3598053080cSYoshinobu Inoue if (hp) 3608053080cSYoshinobu Inoue freehostent(hp); 3618053080cSYoshinobu Inoue #endif 3622aef6930SMark Murray } 3632aef6930SMark Murray } 3642aef6930SMark Murray 3652aef6930SMark Murray /* sock_sink - absorb unreceived IP datagram */ 3662aef6930SMark Murray 3672aef6930SMark Murray static void sock_sink(fd) 3682aef6930SMark Murray int fd; 3692aef6930SMark Murray { 3702aef6930SMark Murray char buf[BUFSIZ]; 3718053080cSYoshinobu Inoue #ifdef INET6 3728053080cSYoshinobu Inoue struct sockaddr_storage sin; 3738053080cSYoshinobu Inoue #else 3742aef6930SMark Murray struct sockaddr_in sin; 3758053080cSYoshinobu Inoue #endif 3762aef6930SMark Murray int size = sizeof(sin); 3772aef6930SMark Murray 3782aef6930SMark Murray /* 3792aef6930SMark Murray * Eat up the not-yet received datagram. Some systems insist on a 3802aef6930SMark Murray * non-zero source address argument in the recvfrom() call below. 3812aef6930SMark Murray */ 3822aef6930SMark Murray 3832aef6930SMark Murray (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & sin, &size); 3842aef6930SMark Murray } 385