6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libwrap / socket.c
blob0d914f9eadb6561d858767770c9c0279201e3a77
1 /*
2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 /*
9 * This module determines the type of socket (datagram, stream), the client
10 * socket address and port, the server socket address and port. In addition,
11 * it provides methods to map a transport address to a printable host name
12 * or address. Socket address information results are in static memory.
14 * The result from the hostname lookup method is STRING_PARANOID when a host
15 * pretends to have someone elses name, or when a host name is available but
16 * could not be verified.
18 * When lookup or conversion fails the result is set to STRING_UNKNOWN.
20 * Diagnostics are reported through syslog(3).
22 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
25 #ifndef lint
26 static char sccsid[] = "@(#) socket.c 1.15 97/03/21 19:27:24";
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 <arpa/inet.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <syslog.h>
39 #include <string.h>
41 extern char *inet_ntoa();
43 /* Local stuff. */
45 #include "tcpd.h"
47 /* Forward declarations. */
49 static void sock_sink();
51 #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 struct hostent *tcpd_gethostbyname_dot(name, af)
60 char *name;
61 int af;
63 char dot_name[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) >= MAXHOSTNAMELEN - 1) {
71 return (tcpd_gethostbyname(name, af));
72 } else {
73 sprintf(dot_name, "%s.", name);
74 return (tcpd_gethostbyname(dot_name, af));
78 #define tcpd_gethostbyname tcpd_gethostbyname_dot
79 #endif
81 /* sock_host - look up endpoint addresses and install conversion methods */
83 void sock_host(request)
84 struct request_info *request;
86 static struct sockaddr_gen client;
87 static struct sockaddr_gen server;
88 int len;
89 char buf[BUFSIZ];
90 int fd = request->fd;
92 sock_methods(request);
95 * Look up the client host address. Hal R. Brand <BRAND@addvax.llnl.gov>
96 * suggested how to get the client host info in case of UDP connections:
97 * peek at the first message without actually looking at its contents. We
98 * really should verify that client.sin_family gets the value AF_INET,
99 * but this program has already caused too much grief on systems with
100 * broken library code.
103 len = sizeof(client);
104 if (getpeername(fd, (struct sockaddr *) & 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 sockgen_simplify(&client);
117 request->client->sin = &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 len = sizeof(server);
126 if (getsockname(fd, (struct sockaddr *) & server, &len) < 0) {
127 tcpd_warn("getsockname: %m");
128 return;
130 sockgen_simplify(&server);
131 request->server->sin = &server;
134 /* sock_hostaddr - map endpoint address to printable form */
136 void sock_hostaddr(host)
137 struct host_info *host;
139 struct sockaddr_gen *sin = host->sin;
141 if (sin != 0)
142 #ifdef HAVE_IPV6
144 (void) inet_ntop(SGFAM(sin), SGADDRP(sin), host->addr, sizeof(host->addr));
145 #else
146 STRN_CPY(host->addr, inet_ntoa(sin->sg_sin.sin_addr), sizeof(host->addr));
147 #endif
150 /* sock_hostname - map endpoint address to host name */
152 void sock_hostname(host)
153 struct host_info *host;
155 struct sockaddr_gen *sin = host->sin;
156 struct hostent *hp;
157 int i;
158 int herr;
161 * On some systems, for example Solaris 2.3, gethostbyaddr(0.0.0.0) does
162 * not fail. Instead it returns "INADDR_ANY". Unfortunately, this does
163 * not work the other way around: gethostbyname("INADDR_ANY") fails. We
164 * have to special-case 0.0.0.0, in order to avoid false alerts from the
165 * host name/address checking code below.
167 if (sin != 0
168 && !SG_IS_UNSPECIFIED(sin)
169 && (hp = gethostbyaddr(SGADDRP(sin), SGADDRSZ(sin), SGFAM(sin))) != 0) {
171 STRN_CPY(host->name, hp->h_name, sizeof(host->name));
174 * Verify that the address is a member of the address list returned
175 * by gethostbyname(hostname).
177 * Verify also that gethostbyaddr() and gethostbyname() return the same
178 * hostname, or rshd and rlogind may still end up being spoofed.
180 * On some sites, gethostbyname("localhost") returns "localhost.domain".
181 * This is a DNS artefact. We treat it as a special case. When we
182 * can't believe the address list from gethostbyname("localhost")
183 * we're in big trouble anyway.
186 if ((hp = tcpd_gethostbyname(host->name, SGFAM(sin))) == 0) {
189 * Unable to verify that the host name matches the address. This
190 * may be a transient problem or a botched name server setup.
193 tcpd_warn("can't verify hostname: gethostbyname(%s) failed",
194 host->name);
196 } else if (STR_NE(host->name, hp->h_name)
197 && STR_NE(host->name, "localhost")) {
200 * The gethostbyaddr() and gethostbyname() calls did not return
201 * the same hostname. This could be a nameserver configuration
202 * problem. It could also be that someone is trying to spoof us.
205 tcpd_warn("host name/name mismatch: %s != %.*s",
206 host->name, STRING_LENGTH, hp->h_name);
208 } else {
209 #ifdef HAVE_IPV6
210 char buf[INET6_ADDRSTRLEN];
211 #endif
214 * The address should be a member of the address list returned by
215 * gethostbyname(). We should first verify that the h_addrtype
216 * field is AF_INET, but this program has already caused too much
217 * grief on systems with broken library code.
220 for (i = 0; hp->h_addr_list[i]; i++) {
221 if (memcmp(hp->h_addr_list[i],
222 (char *) SGADDRP(sin),
223 SGADDRSZ(sin)) == 0) {
224 return; /* name is good, keep it */
229 * The host name does not map to the initial address. Perhaps
230 * someone has messed up. Perhaps someone compromised a name
231 * server.
233 tcpd_warn("host name/address mismatch: %s != %.*s",
234 #ifdef HAVE_IPV6
235 inet_ntop(SGFAM(sin), SGADDRP(sin), buf, sizeof(buf)),
236 #else
237 inet_ntoa(sin->sg_sin.sin_addr),
238 #endif
239 STRING_LENGTH, hp->h_name);
241 strcpy(host->name, paranoid); /* name is bad, clobber it */
245 /* sock_sink - absorb unreceived IP datagram */
247 static void sock_sink(fd)
248 int fd;
250 char buf[BUFSIZ];
251 struct sockaddr_in sin;
252 int size = sizeof(sin);
255 * Eat up the not-yet received datagram. Some systems insist on a
256 * non-zero source address argument in the recvfrom() call below.
259 (void) recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) & sin, &size);
263 * If we receive a V4 connection on a V6 socket, we pretend we really
264 * got a V4 connection.
266 void sockgen_simplify(sg)
267 sockaddr_gen *sg;
269 #ifdef HAVE_IPV6
270 if (sg->sg_family == AF_INET6 &&
271 IN6_IS_ADDR_V4MAPPED(&sg->sg_sin6.sin6_addr)) {
272 struct sockaddr_in v4_addr;
274 #ifdef IN6_V4MAPPED_TO_INADDR /* Solaris 8 */
275 IN6_V4MAPPED_TO_INADDR(&sg->sg_sin6.sin6_addr, &v4_addr.sin_addr);
276 #elif defined(IN6_MAPPED_TO_V4) /* Solaris 8 Beta only? */
277 IN6_MAPPED_TO_V4(&sg->sg_sin6.sin6_addr, &v4_addr.sin_addr);
278 #else /* Do it the hard way */
279 memcpy(&v4_addr.sin_addr, ((char*) &sg->sg_sin6.sin6_addr) + 12, 4);
280 #endif
281 v4_addr.sin_port = sg->sg_sin6.sin6_port;
282 v4_addr.sin_family = AF_INET;
283 memcpy(&sg->sg_sin, &v4_addr, sizeof(v4_addr));
285 #else
286 return;
287 #endif /* HAVE_IPV6 */