1 /* $OpenBSD: canohost.c,v 1.55 2006/07/08 21:47:12 stevesk 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>
20 #include <netinet/in.h>
29 static void check_ip_options(int, char *);
32 * Return the canonical name of the host at the other end of the socket. The
33 * caller should free the returned string with xfree.
37 get_remote_hostname(int sock, int use_dns)
39 struct sockaddr_storage from;
42 struct addrinfo hints, *ai, *aitop;
43 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
45 /* Get IP address of client. */
46 fromlen = sizeof(from);
47 memset(&from, 0, sizeof(from));
48 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
49 debug("getpeername failed: %.100s", strerror(errno));
53 if (from.ss_family == AF_INET)
54 check_ip_options(sock, ntop);
56 ipv64_normalise_mapped(&from, &fromlen);
58 if (from.ss_family == AF_INET6)
59 fromlen = sizeof(struct sockaddr_in6);
61 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
62 NULL, 0, NI_NUMERICHOST) != 0)
63 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
68 debug3("Trying to reverse map address %.100s.", ntop);
69 /* Map the IP address to a host name. */
70 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
71 NULL, 0, NI_NAMEREQD) != 0) {
72 /* Host name not found. Use ip address. */
77 * if reverse lookup result looks like a numeric hostname,
78 * someone is trying to trick us by PTR record like following:
79 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
81 memset(&hints, 0, sizeof(hints));
82 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
83 hints.ai_flags = AI_NUMERICHOST;
84 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
85 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
92 * Convert it to all lowercase (which is expected by the rest
95 for (i = 0; name[i]; i++)
97 name[i] = (char)tolower(name[i]);
99 * Map it back to an IP address and check that the given
100 * address actually is an address of this host. This is
101 * necessary because anyone with access to a name server can
102 * define arbitrary names for an IP address. Mapping from
103 * name to IP address can be trusted better (but can still be
104 * fooled if the intruder has access to the name server of
107 memset(&hints, 0, sizeof(hints));
108 hints.ai_family = from.ss_family;
109 hints.ai_socktype = SOCK_STREAM;
110 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
111 logit("reverse mapping checking getaddrinfo for %.700s "
112 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
113 return xstrdup(ntop);
115 /* Look for the address from the list of addresses. */
116 for (ai = aitop; ai; ai = ai->ai_next) {
117 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
118 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
119 (strcmp(ntop, ntop2) == 0))
123 /* If we reached the end of the list, the address was not there. */
125 /* Address not found for the host name. */
126 logit("Address %.100s maps to %.600s, but this does not "
127 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
129 return xstrdup(ntop);
131 return xstrdup(name);
135 * If IP options are supported, make sure there are none (log and
136 * disconnect them if any are found). Basically we are worried about
137 * source routing; it can be used to pretend you are somebody
138 * (ip-address) you are not. That itself may be "almost acceptable"
139 * under certain circumstances, but rhosts autentication is useless
140 * if source routing is accepted. Notice also that if we just dropped
141 * source routing here, the other side could use IP spoofing to do
142 * rest of the interaction and could still bypass security. So we
143 * exit here if we detect any IP options.
147 check_ip_options(int sock, char *ipaddr)
151 char text[sizeof(options) * 3 + 1];
152 socklen_t option_size;
157 if ((ip = getprotobyname("ip")) != NULL)
158 ipproto = ip->p_proto;
160 ipproto = IPPROTO_IP;
161 option_size = sizeof(options);
162 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
163 &option_size) >= 0 && option_size != 0) {
165 for (i = 0; i < option_size; i++)
166 snprintf(text + i*3, sizeof(text) - i*3,
167 " %2.2x", options[i]);
168 fatal("Connection from %.100s with IP options:%.800s",
171 #endif /* IP_OPTIONS */
175 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
177 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
178 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
179 struct in_addr inaddr;
182 if (addr->ss_family != AF_INET6 ||
183 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
186 debug3("Normalising mapped IPv4 in IPv6 address");
188 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
189 port = a6->sin6_port;
191 memset(addr, 0, sizeof(*a4));
193 a4->sin_family = AF_INET;
195 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
200 * Return the canonical name of the host in the other side of the current
201 * connection. The host name is cached, so it is efficient to call this
206 get_canonical_hostname(int use_dns)
209 static char *canonical_host_name = NULL;
210 static char *remote_ip = NULL;
212 /* Check if we have previously retrieved name with same option. */
213 if (use_dns && canonical_host_name != NULL)
214 return canonical_host_name;
215 if (!use_dns && remote_ip != NULL)
218 /* Get the real hostname if socket; otherwise return UNKNOWN. */
219 if (packet_connection_is_on_socket())
220 host = get_remote_hostname(packet_get_connection_in(), use_dns);
225 canonical_host_name = host;
232 * Returns the local/remote IP-address/hostname of socket as a string.
233 * The returned string must be freed.
236 get_socket_address(int sock, int remote, int flags)
238 struct sockaddr_storage addr;
240 char ntop[NI_MAXHOST];
243 /* Get IP address of client. */
244 addrlen = sizeof(addr);
245 memset(&addr, 0, sizeof(addr));
248 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
252 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
257 /* Work around Linux IPv6 weirdness */
258 if (addr.ss_family == AF_INET6)
259 addrlen = sizeof(struct sockaddr_in6);
261 ipv64_normalise_mapped(&addr, &addrlen);
263 /* Get the address in ascii. */
264 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
265 sizeof(ntop), NULL, 0, flags)) != 0) {
266 error("get_socket_address: getnameinfo %d failed: %s", flags,
267 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
270 return xstrdup(ntop);
274 get_peer_ipaddr(int sock)
278 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
280 return xstrdup("UNKNOWN");
284 get_local_ipaddr(int sock)
288 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
290 return xstrdup("UNKNOWN");
294 get_local_name(int sock)
296 return get_socket_address(sock, 0, NI_NAMEREQD);
300 * Returns the IP-address of the remote host as a string. The returned
301 * string must not be freed.
305 get_remote_ipaddr(void)
307 static char *canonical_host_ip = NULL;
309 /* Check whether we have cached the ipaddr. */
310 if (canonical_host_ip == NULL) {
311 if (packet_connection_is_on_socket()) {
313 get_peer_ipaddr(packet_get_connection_in());
314 if (canonical_host_ip == NULL)
317 /* If not on socket, return UNKNOWN. */
318 canonical_host_ip = xstrdup("UNKNOWN");
321 return canonical_host_ip;
325 get_remote_name_or_ip(u_int utmp_len, int use_dns)
327 static const char *remote = "";
329 remote = get_canonical_hostname(use_dns);
330 if (utmp_len == 0 || strlen(remote) > utmp_len)
331 remote = get_remote_ipaddr();
335 /* Returns the local/remote port for the socket. */
338 get_sock_port(int sock, int local)
340 struct sockaddr_storage from;
342 char strport[NI_MAXSERV];
345 /* Get IP address of client. */
346 fromlen = sizeof(from);
347 memset(&from, 0, sizeof(from));
349 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
350 error("getsockname failed: %.100s", strerror(errno));
354 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
355 debug("getpeername failed: %.100s", strerror(errno));
360 /* Work around Linux IPv6 weirdness */
361 if (from.ss_family == AF_INET6)
362 fromlen = sizeof(struct sockaddr_in6);
364 /* Return port number. */
365 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
366 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
367 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
368 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
369 return atoi(strport);
372 /* Returns remote/local port number for the current connection. */
378 * If the connection is not a socket, return 65535. This is
379 * intentionally chosen to be an unprivileged port number.
381 if (!packet_connection_is_on_socket())
384 /* Get socket and return the port number. */
385 return get_sock_port(packet_get_connection_in(), local);
389 get_peer_port(int sock)
391 return get_sock_port(sock, 0);
395 get_remote_port(void)
397 static int port = -1;
399 /* Cache to avoid getpeername() on a dead connection */