Remove building with NOCRYPTO option
[minix3.git] / lib / libwrap / socket.c
blob3b5129a3ea71405faa79042b7e953aba38ec5a95
1 /* $NetBSD: socket.c,v 1.19 2012/03/21 10:10:37 matt Exp $ */
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.
13 * When lookup or conversion fails the result is set to STRING_UNKNOWN.
15 * Diagnostics are reported through syslog(3).
17 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
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.19 2012/03/21 10:10:37 matt Exp $");
26 #endif
27 #endif
29 /* System libraries. */
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>
41 /* Local stuff. */
43 #include "tcpd.h"
45 /* Forward declarations. */
47 #ifdef APPEND_DOT
48 static const char *append_dot __P((const char *));
49 #endif
50 static void sock_sink __P((int));
52 #ifdef APPEND_DOT
54 * Speed up DNS lookups by terminating the host name with a dot. Should be
55 * done with care. The speedup can give problems with lookups from sources
56 * that lack DNS-style trailing dot magic, such as local files or NIS maps.
59 static const char *
60 append_dot(name)
61 const char *name;
63 static char hbuf[MAXHOSTNAMELEN + 1];
66 * Don't append dots to unqualified names. Such names are likely to come
67 * from local hosts files or from NIS.
70 if (strchr(name, '.') == 0 || strlen(name) + 2 > sizeof(hbuf))
71 strlcpy(hbuf, name, sizeof(hbuf));
72 else
73 (void)snprintf(hbuf, sizeof(hbuf), "%s.", name);
74 return hbuf;
76 #endif
78 /* sock_host - look up endpoint addresses and install conversion methods */
80 void
81 sock_host(struct request_info *request)
83 static struct sockaddr_storage client;
84 static struct sockaddr_storage server;
85 socklen_t len;
86 char buf[BUFSIZ];
87 int fd = request->fd;
89 sock_methods(request);
92 * Look up the client host address. Hal R. Brand <BRAND@addvax.llnl.gov>
93 * suggested how to get the client host info in case of UDP connections:
94 * peek at the first message without actually looking at its contents. We
95 * really should verify that client.sin_family gets the value AF_INET,
96 * but this program has already caused too much grief on systems with
97 * broken library code.
99 * XXX the last sentence is untrue as we support AF_INET6 as well :-)
102 if (request->client->sin == NULL) {
103 len = sizeof(client);
104 if (getpeername(fd, (struct sockaddr *)(void *)& client, &len) < 0) {
105 request->sink = sock_sink;
106 len = sizeof(client);
107 if (recvfrom(fd, buf, sizeof(buf), MSG_PEEK,
108 (struct sockaddr *) & client, &len) < 0) {
109 tcpd_warn("can't get client address: %m");
110 return; /* give up */
112 #ifdef really_paranoid
113 memset(buf, 0, sizeof(buf));
114 #endif
116 request->client->sin = (struct sockaddr *)&client;
120 * Determine the server binding. This is used for client username
121 * lookups, and for access control rules that trigger on the server
122 * address or name.
125 if (request->server->sin == NULL) {
126 len = sizeof(server);
127 if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
128 tcpd_warn("getsockname: %m");
129 return;
131 request->server->sin = (struct sockaddr *)&server;
135 /* sock_hostaddr - map endpoint address to printable form */
137 void
138 sock_hostaddr(struct host_info *host)
140 struct sockaddr *sa = host->sin;
142 if (!sa)
143 return;
144 host->addr[0] = '\0';
145 getnameinfo(sa, sa->sa_len, host->addr, sizeof(host->addr),
146 NULL, 0, NI_NUMERICHOST);
149 /* sock_hostname - map endpoint address to host name */
151 void
152 sock_hostname(struct host_info *host)
154 struct sockaddr *sa = host->sin;
155 char h1[NI_MAXHOST], h2[NI_MAXHOST];
156 struct addrinfo hints, *res, *res0;
157 #ifdef INET6
158 struct sockaddr_in tmp;
159 #endif
161 if (!sa)
162 return;
163 #ifdef INET6
164 /* special case on reverse lookup: mapped addr. I hate it */
165 if (sa->sa_family == AF_INET6 &&
166 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
167 memset(&tmp, 0, sizeof(tmp));
168 tmp.sin_family = AF_INET;
169 tmp.sin_len = sizeof(struct sockaddr_in);
170 memcpy(&tmp.sin_addr,
171 &((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[12], 4);
172 sa = (struct sockaddr *)&tmp;
174 #endif
175 if (getnameinfo(sa, sa->sa_len, h1, sizeof(h1), NULL, 0,
176 NI_NUMERICHOST) != 0) {
177 return;
179 if (getnameinfo(sa, sa->sa_len, host->name, sizeof(host->name), NULL, 0,
180 NI_NAMEREQD) == 0) {
182 * if reverse lookup result looks like a numeric hostname,
183 * someone is trying to trick us by PTR record like following:
184 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
186 memset(&hints, 0, sizeof(hints));
187 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
188 hints.ai_flags = AI_NUMERICHOST;
189 #ifdef APPEND_DOT
190 if (getaddrinfo(append_dot(host->name), "0", &hints, &res0) == 0)
191 #else
192 if (getaddrinfo(host->name, "0", &hints, &res0) == 0)
193 #endif
195 tcpd_warn("Nasty PTR record is configured");
196 freeaddrinfo(res0);
197 /* name is bad, clobber it */
198 (void)strlcpy(host->name, paranoid, sizeof(host->name));
199 return;
203 * Verify that the address is a member of the address list returned
204 * by getaddrinfo(hostname).
206 * Verify also that getnameinfo() and getaddrinfo() return the same
207 * hostname, or rshd and rlogind may still end up being spoofed.
209 * On some sites, getaddrinfo("localhost") returns "localhost.domain".
210 * This is a DNS artefact. We treat it as a special case. When we
211 * can't believe the address list from getaddrinfo("localhost")
212 * we're in big trouble anyway.
214 memset(&hints, 0, sizeof(hints));
215 hints.ai_family = sa->sa_family;
216 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
217 hints.ai_flags = AI_CANONNAME;
218 #ifdef APPEND_DOT
219 if (getaddrinfo(append_dot(host->name), "0", &hints, &res0) != 0)
220 #else
221 if (getaddrinfo(host->name, "0", &hints, &res0) != 0)
222 #endif
225 * Unable to verify that the host name matches the address. This
226 * may be a transient problem or a botched name server setup.
229 tcpd_warn("can't verify hostname: getaddrinfo(%s, %d) failed",
230 host->name, hints.ai_family);
231 } else if (res0->ai_canonname &&
232 STR_NE(host->name, res0->ai_canonname) &&
233 STR_NE(host->name, "localhost")) {
235 * The getnameinfo() and getaddrinfo() calls did not return
236 * the same hostname. This could be a nameserver configuration
237 * problem. It could also be that someone is trying to spoof us.
240 tcpd_warn("host name/name mismatch: %s != %s",
241 host->name, res0->ai_canonname);
242 freeaddrinfo(res0);
243 } else {
245 * The address should be a member of the address list returned by
246 * getaddrinfo().
249 for (res = res0; res; res = res->ai_next) {
250 if (getnameinfo(res->ai_addr, res->ai_addrlen, h2, sizeof(h2),
251 NULL, 0, NI_NUMERICHOST) != 0) {
252 continue;
254 if (STR_EQ(h1, h2)) {
255 freeaddrinfo(res0);
256 return;
261 * The host name does not map to the initial address. Perhaps
262 * someone has messed up. Perhaps someone compromised a name
263 * server.
266 tcpd_warn("host name/address mismatch: %s != %s", h1,
267 res0->ai_canonname ? res0->ai_canonname : "?");
269 freeaddrinfo(res0);
271 /* name is bad, clobber it */
272 (void)strlcpy(host->name, paranoid, sizeof(host->name));
276 /* sock_sink - absorb unreceived IP datagram */
278 static void
279 sock_sink(int fd)
281 char buf[BUFSIZ];
282 struct sockaddr_storage ss;
283 socklen_t size = sizeof(ss);
286 * Eat up the not-yet received datagram. Some systems insist on a
287 * non-zero source address argument in the recvfrom() call below.
290 (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & ss, &size);