socket.c revision 1.11 1 /* $NetBSD: socket.c,v 1.11 2002/06/06 21:28:50 itojun Exp $ */
2
3 /*
4 * This module determines the type of socket (datagram, stream), the client
5 * socket address and port, the server socket address and port. In addition,
6 * it provides methods to map a transport address to a printable host name
7 * or address. Socket address information results are in static memory.
8 *
9 * The result from the hostname lookup method is STRING_PARANOID when a host
10 * pretends to have someone elses name, or when a host name is available but
11 * could not be verified.
12 *
13 * When lookup or conversion fails the result is set to STRING_UNKNOWN.
14 *
15 * Diagnostics are reported through syslog(3).
16 *
17 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
18 */
19
20 #include <sys/cdefs.h>
21 #ifndef lint
22 #if 0
23 static char sccsid[] = "@(#) socket.c 1.15 97/03/21 19:27:24";
24 #else
25 __RCSID("$NetBSD: socket.c,v 1.11 2002/06/06 21:28:50 itojun Exp $");
26 #endif
27 #endif
28
29 /* System libraries. */
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <syslog.h>
38 #include <string.h>
39 #include <arpa/inet.h>
40
41 /* Local stuff. */
42
43 #include "tcpd.h"
44
45 /* Forward declarations. */
46
47 static void sock_sink __P((int));
48
49 /* sock_host - look up endpoint addresses and install conversion methods */
50
51 void sock_host(request)
52 struct request_info *request;
53 {
54 static struct sockaddr_storage client;
55 static struct sockaddr_storage server;
56 int len;
57 char buf[BUFSIZ];
58 int fd = request->fd;
59
60 sock_methods(request);
61
62 /*
63 * Look up the client host address. Hal R. Brand <BRAND (at) addvax.llnl.gov>
64 * suggested how to get the client host info in case of UDP connections:
65 * peek at the first message without actually looking at its contents. We
66 * really should verify that client.sin_family gets the value AF_INET,
67 * but this program has already caused too much grief on systems with
68 * broken library code.
69 *
70 * XXX the last sentence is untrue as we support AF_INET6 as well :-)
71 */
72
73 len = sizeof(client);
74 if (getpeername(fd, (struct sockaddr *) & client, &len) < 0) {
75 request->sink = sock_sink;
76 len = sizeof(client);
77 if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK,
78 (struct sockaddr *) & client, &len) < 0) {
79 tcpd_warn("can't get client address: %m");
80 return; /* give up */
81 }
82 #ifdef really_paranoid
83 memset(buf, 0 sizeof(buf));
84 #endif
85 }
86 request->client->sin = (struct sockaddr *)&client;
87
88 /*
89 * Determine the server binding. This is used for client username
90 * lookups, and for access control rules that trigger on the server
91 * address or name.
92 */
93
94 len = sizeof(server);
95 if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
96 tcpd_warn("getsockname: %m");
97 return;
98 }
99 request->server->sin = (struct sockaddr *)&server;
100 }
101
102 /* sock_hostaddr - map endpoint address to printable form */
103
104 void sock_hostaddr(host)
105 struct host_info *host;
106 {
107 struct sockaddr *sa = host->sin;
108
109 if (!sa)
110 return;
111 host->addr[0] = '\0';
112 getnameinfo(sa, sa->sa_len, host->addr, sizeof(host->addr),
113 NULL, 0, NI_NUMERICHOST);
114 }
115
116 /* sock_hostname - map endpoint address to host name */
117
118 void sock_hostname(host)
119 struct host_info *host;
120 {
121 struct sockaddr *sa = host->sin;
122 char h1[NI_MAXHOST], h2[NI_MAXHOST];
123 struct addrinfo hints, *res, *res0;
124 #ifdef INET6
125 struct sockaddr_in tmp;
126 #endif
127
128 if (!sa)
129 return;
130 #ifdef INET6
131 /* special case on reverse lookup: mapped addr. I hate it */
132 if (sa->sa_family == AF_INET6 &&
133 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
134 memset(&tmp, 0, sizeof(tmp));
135 tmp.sin_family = AF_INET;
136 tmp.sin_len = sizeof(struct sockaddr_in);
137 memcpy(&tmp.sin_addr,
138 &((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[12], 4);
139 sa = (struct sockaddr *)&tmp;
140 }
141 #endif
142 if (getnameinfo(sa, sa->sa_len, h1, sizeof(h1), NULL, 0,
143 NI_NUMERICHOST) != 0) {
144 return;
145 }
146 if (getnameinfo(sa, sa->sa_len, host->name, sizeof(host->name), NULL, 0,
147 NI_NAMEREQD) == 0) {
148 /*
149 * Verify that the address is a member of the address list returned
150 * by getaddrinfo(hostname).
151 *
152 * Verify also that getnameinfo() and getaddrinfo() return the same
153 * hostname, or rshd and rlogind may still end up being spoofed.
154 *
155 * On some sites, getaddrinfo("localhost") returns "localhost.domain".
156 * This is a DNS artefact. We treat it as a special case. When we
157 * can't believe the address list from getaddrinfo("localhost")
158 * we're in big trouble anyway.
159 */
160 memset(&hints, 0, sizeof(hints));
161 hints.ai_family = sa->sa_family;
162 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
163 hints.ai_flags = AI_CANONNAME;
164 if (getaddrinfo(host->name, "0", &hints, &res0) != 0) {
165 /*
166 * Unable to verify that the host name matches the address. This
167 * may be a transient problem or a botched name server setup.
168 */
169
170 tcpd_warn("can't verify hostname: getaddrinfo(%s, %d) failed",
171 host->name, hints.ai_family);
172 } else if (res0->ai_canonname &&
173 STR_NE(host->name, res0->ai_canonname) &&
174 STR_NE(host->name, "localhost")) {
175 /*
176 * The getnameinfo() and getaddrinfo() calls did not return
177 * the same hostname. This could be a nameserver configuration
178 * problem. It could also be that someone is trying to spoof us.
179 */
180
181 tcpd_warn("host name/name mismatch: %s != %s",
182 host->name, res0->ai_canonname);
183 freeaddrinfo(res0);
184 } else {
185 /*
186 * The address should be a member of the address list returned by
187 * getaddrinfo().
188 */
189
190 for (res = res0; res; res = res->ai_next) {
191 if (getnameinfo(res->ai_addr, res->ai_addrlen, h2, sizeof(h2),
192 NULL, 0, NI_NUMERICHOST) != 0) {
193 continue;
194 }
195 if (STR_EQ(h1, h2)) {
196 freeaddrinfo(res0);
197 return;
198 }
199 }
200
201 /*
202 * The host name does not map to the initial address. Perhaps
203 * someone has messed up. Perhaps someone compromised a name
204 * server.
205 */
206
207 tcpd_warn("host name/address mismatch: %s != %s", h1,
208 res0->ai_canonname ? res0->ai_canonname : "?");
209
210 freeaddrinfo(res0);
211 }
212 /* name is bad, clobber it */
213 (void)strncpy(host->name, paranoid, sizeof(host->name) - 1);
214 }
215 }
216
217 /* sock_sink - absorb unreceived IP datagram */
218
219 static void sock_sink(fd)
220 int fd;
221 {
222 char buf[BUFSIZ];
223 struct sockaddr_storage sst;
224 int size = sizeof(sst);
225
226 /*
227 * Eat up the not-yet received datagram. Some systems insist on a
228 * non-zero source address argument in the recvfrom() call below.
229 */
230
231 (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & sst, &size);
232 }
233