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