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