1 /* $OpenBSD: canohost.c,v 1.61 2006/08/03 03:34:41 deraadt Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for returning the canonical host name of the remote site.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/param.h> /* for MAXHOSTNAMELEN */
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
38 static void check_ip_options(int, char *);
41 * Return the canonical name of the host at the other end of the socket. The
42 * caller should free the returned string with xfree.
46 get_remote_hostname(int sock, int use_dns)
48 struct sockaddr_storage from;
51 struct addrinfo hints, *ai, *aitop;
52 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
54 /* Get IP address of client. */
55 fromlen = sizeof(from);
56 memset(&from, 0, sizeof(from));
57 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
58 debug("getpeername failed: %.100s", strerror(errno));
62 if (from.ss_family == AF_INET)
63 check_ip_options(sock, ntop);
65 ipv64_normalise_mapped(&from, &fromlen);
67 if (from.ss_family == AF_INET6)
68 fromlen = sizeof(struct sockaddr_in6);
70 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
71 NULL, 0, NI_NUMERICHOST) != 0)
72 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
77 debug3("Trying to reverse map address %.100s.", ntop);
78 /* Map the IP address to a host name. */
79 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
80 NULL, 0, NI_NAMEREQD) != 0) {
81 /* Host name not found. Use ip address. */
86 * if reverse lookup result looks like a numeric hostname,
87 * someone is trying to trick us by PTR record like following:
88 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
90 memset(&hints, 0, sizeof(hints));
91 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
92 hints.ai_flags = AI_NUMERICHOST;
93 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
94 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
101 * Convert it to all lowercase (which is expected by the rest
104 for (i = 0; name[i]; i++)
105 if (isupper(name[i]))
106 name[i] = (char)tolower(name[i]);
108 * Map it back to an IP address and check that the given
109 * address actually is an address of this host. This is
110 * necessary because anyone with access to a name server can
111 * define arbitrary names for an IP address. Mapping from
112 * name to IP address can be trusted better (but can still be
113 * fooled if the intruder has access to the name server of
116 memset(&hints, 0, sizeof(hints));
117 hints.ai_family = from.ss_family;
118 hints.ai_socktype = SOCK_STREAM;
119 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
120 logit("reverse mapping checking getaddrinfo for %.700s "
121 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
122 return xstrdup(ntop);
124 /* Look for the address from the list of addresses. */
125 for (ai = aitop; ai; ai = ai->ai_next) {
126 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
127 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
128 (strcmp(ntop, ntop2) == 0))
132 /* If we reached the end of the list, the address was not there. */
134 /* Address not found for the host name. */
135 logit("Address %.100s maps to %.600s, but this does not "
136 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
138 return xstrdup(ntop);
140 return xstrdup(name);
144 * If IP options are supported, make sure there are none (log and
145 * disconnect them if any are found). Basically we are worried about
146 * source routing; it can be used to pretend you are somebody
147 * (ip-address) you are not. That itself may be "almost acceptable"
148 * under certain circumstances, but rhosts autentication is useless
149 * if source routing is accepted. Notice also that if we just dropped
150 * source routing here, the other side could use IP spoofing to do
151 * rest of the interaction and could still bypass security. So we
152 * exit here if we detect any IP options.
156 check_ip_options(int sock, char *ipaddr)
160 char text[sizeof(options) * 3 + 1];
161 socklen_t option_size;
166 if ((ip = getprotobyname("ip")) != NULL)
167 ipproto = ip->p_proto;
169 ipproto = IPPROTO_IP;
170 option_size = sizeof(options);
171 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
172 &option_size) >= 0 && option_size != 0) {
174 for (i = 0; i < option_size; i++)
175 snprintf(text + i*3, sizeof(text) - i*3,
176 " %2.2x", options[i]);
177 fatal("Connection from %.100s with IP options:%.800s",
180 #endif /* IP_OPTIONS */
184 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
186 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
187 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
188 struct in_addr inaddr;
191 if (addr->ss_family != AF_INET6 ||
192 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
195 debug3("Normalising mapped IPv4 in IPv6 address");
197 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
198 port = a6->sin6_port;
200 memset(addr, 0, sizeof(*a4));
202 a4->sin_family = AF_INET;
204 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
209 * Return the canonical name of the host in the other side of the current
210 * connection. The host name is cached, so it is efficient to call this
215 get_canonical_hostname(int use_dns)
218 static char *canonical_host_name = NULL;
219 static char *remote_ip = NULL;
221 /* Check if we have previously retrieved name with same option. */
222 if (use_dns && canonical_host_name != NULL)
223 return canonical_host_name;
224 if (!use_dns && remote_ip != NULL)
227 /* Get the real hostname if socket; otherwise return UNKNOWN. */
228 if (packet_connection_is_on_socket())
229 host = get_remote_hostname(packet_get_connection_in(), use_dns);
234 canonical_host_name = host;
241 * Returns the local/remote IP-address/hostname of socket as a string.
242 * The returned string must be freed.
245 get_socket_address(int sock, int remote, int flags)
247 struct sockaddr_storage addr;
249 char ntop[NI_MAXHOST];
252 /* Get IP address of client. */
253 addrlen = sizeof(addr);
254 memset(&addr, 0, sizeof(addr));
257 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
261 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
266 /* Work around Linux IPv6 weirdness */
267 if (addr.ss_family == AF_INET6)
268 addrlen = sizeof(struct sockaddr_in6);
270 ipv64_normalise_mapped(&addr, &addrlen);
272 /* Get the address in ascii. */
273 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
274 sizeof(ntop), NULL, 0, flags)) != 0) {
275 error("get_socket_address: getnameinfo %d failed: %s", flags,
276 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
279 return xstrdup(ntop);
283 get_peer_ipaddr(int sock)
287 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
289 return xstrdup("UNKNOWN");
293 get_local_ipaddr(int sock)
297 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
299 return xstrdup("UNKNOWN");
303 get_local_name(int sock)
305 return get_socket_address(sock, 0, NI_NAMEREQD);
309 * Returns the IP-address of the remote host as a string. The returned
310 * string must not be freed.
314 get_remote_ipaddr(void)
316 static char *canonical_host_ip = NULL;
318 /* Check whether we have cached the ipaddr. */
319 if (canonical_host_ip == NULL) {
320 if (packet_connection_is_on_socket()) {
322 get_peer_ipaddr(packet_get_connection_in());
323 if (canonical_host_ip == NULL)
326 /* If not on socket, return UNKNOWN. */
327 canonical_host_ip = xstrdup("UNKNOWN");
330 return canonical_host_ip;
334 get_remote_name_or_ip(u_int utmp_len, int use_dns)
336 static const char *remote = "";
338 remote = get_canonical_hostname(use_dns);
339 if (utmp_len == 0 || strlen(remote) > utmp_len)
340 remote = get_remote_ipaddr();
344 /* Returns the local/remote port for the socket. */
347 get_sock_port(int sock, int local)
349 struct sockaddr_storage from;
351 char strport[NI_MAXSERV];
354 /* Get IP address of client. */
355 fromlen = sizeof(from);
356 memset(&from, 0, sizeof(from));
358 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
359 error("getsockname failed: %.100s", strerror(errno));
363 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
364 debug("getpeername failed: %.100s", strerror(errno));
369 /* Work around Linux IPv6 weirdness */
370 if (from.ss_family == AF_INET6)
371 fromlen = sizeof(struct sockaddr_in6);
373 /* Return port number. */
374 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
375 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
376 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
377 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
378 return atoi(strport);
381 /* Returns remote/local port number for the current connection. */
387 * If the connection is not a socket, return 65535. This is
388 * intentionally chosen to be an unprivileged port number.
390 if (!packet_connection_is_on_socket())
393 /* Get socket and return the port number. */
394 return get_sock_port(packet_get_connection_in(), local);
398 get_peer_port(int sock)
400 return get_sock_port(sock, 0);
404 get_remote_port(void)
406 static int port = -1;
408 /* Cache to avoid getpeername() on a dead connection */
422 resolve_localhost(char **host)
424 struct hostent *hostinfo;
426 hostinfo = gethostbyname(*host);
427 if (hostinfo == NULL || hostinfo->h_name == NULL) {
428 debug("gethostbyname(%s) failed", *host);
431 if (hostinfo->h_addrtype == AF_INET) {
433 addr = *(struct in_addr *)(hostinfo->h_addr);
434 if (ntohl(addr.s_addr) == INADDR_LOOPBACK) {
435 char buf[MAXHOSTNAMELEN];
436 if (gethostname(buf, sizeof(buf)) < 0) {
437 debug("gethostname() failed");
440 hostinfo = gethostbyname(buf);
442 if (hostinfo == NULL || hostinfo->h_name == NULL) {
443 *host = xstrdup(buf);
445 *host = xstrdup(hostinfo->h_name);