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