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