1 /* $OpenBSD: canohost.c,v 1.56 2006/07/11 20:07:25 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>
30 static void check_ip_options(int, char *);
33 * Return the canonical name of the host at the other end of the socket. The
34 * caller should free the returned string with xfree.
38 get_remote_hostname(int sock, int use_dns)
40 struct sockaddr_storage from;
43 struct addrinfo hints, *ai, *aitop;
44 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
46 /* Get IP address of client. */
47 fromlen = sizeof(from);
48 memset(&from, 0, sizeof(from));
49 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
50 debug("getpeername failed: %.100s", strerror(errno));
54 if (from.ss_family == AF_INET)
55 check_ip_options(sock, ntop);
57 ipv64_normalise_mapped(&from, &fromlen);
59 if (from.ss_family == AF_INET6)
60 fromlen = sizeof(struct sockaddr_in6);
62 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
63 NULL, 0, NI_NUMERICHOST) != 0)
64 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
69 debug3("Trying to reverse map address %.100s.", ntop);
70 /* Map the IP address to a host name. */
71 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
72 NULL, 0, NI_NAMEREQD) != 0) {
73 /* Host name not found. Use ip address. */
78 * if reverse lookup result looks like a numeric hostname,
79 * someone is trying to trick us by PTR record like following:
80 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
84 hints.ai_flags = AI_NUMERICHOST;
85 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
86 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
93 * Convert it to all lowercase (which is expected by the rest
96 for (i = 0; name[i]; i++)
98 name[i] = (char)tolower(name[i]);
100 * Map it back to an IP address and check that the given
101 * address actually is an address of this host. This is
102 * necessary because anyone with access to a name server can
103 * define arbitrary names for an IP address. Mapping from
104 * name to IP address can be trusted better (but can still be
105 * fooled if the intruder has access to the name server of
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = from.ss_family;
110 hints.ai_socktype = SOCK_STREAM;
111 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
112 logit("reverse mapping checking getaddrinfo for %.700s "
113 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
114 return xstrdup(ntop);
116 /* Look for the address from the list of addresses. */
117 for (ai = aitop; ai; ai = ai->ai_next) {
118 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
119 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
120 (strcmp(ntop, ntop2) == 0))
124 /* If we reached the end of the list, the address was not there. */
126 /* Address not found for the host name. */
127 logit("Address %.100s maps to %.600s, but this does not "
128 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
130 return xstrdup(ntop);
132 return xstrdup(name);
136 * If IP options are supported, make sure there are none (log and
137 * disconnect them if any are found). Basically we are worried about
138 * source routing; it can be used to pretend you are somebody
139 * (ip-address) you are not. That itself may be "almost acceptable"
140 * under certain circumstances, but rhosts autentication is useless
141 * if source routing is accepted. Notice also that if we just dropped
142 * source routing here, the other side could use IP spoofing to do
143 * rest of the interaction and could still bypass security. So we
144 * exit here if we detect any IP options.
148 check_ip_options(int sock, char *ipaddr)
152 char text[sizeof(options) * 3 + 1];
153 socklen_t option_size;
158 if ((ip = getprotobyname("ip")) != NULL)
159 ipproto = ip->p_proto;
161 ipproto = IPPROTO_IP;
162 option_size = sizeof(options);
163 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
164 &option_size) >= 0 && option_size != 0) {
166 for (i = 0; i < option_size; i++)
167 snprintf(text + i*3, sizeof(text) - i*3,
168 " %2.2x", options[i]);
169 fatal("Connection from %.100s with IP options:%.800s",
172 #endif /* IP_OPTIONS */
176 ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
178 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
179 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
180 struct in_addr inaddr;
183 if (addr->ss_family != AF_INET6 ||
184 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
187 debug3("Normalising mapped IPv4 in IPv6 address");
189 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
190 port = a6->sin6_port;
192 memset(addr, 0, sizeof(*a4));
194 a4->sin_family = AF_INET;
196 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
201 * Return the canonical name of the host in the other side of the current
202 * connection. The host name is cached, so it is efficient to call this
207 get_canonical_hostname(int use_dns)
210 static char *canonical_host_name = NULL;
211 static char *remote_ip = NULL;
213 /* Check if we have previously retrieved name with same option. */
214 if (use_dns && canonical_host_name != NULL)
215 return canonical_host_name;
216 if (!use_dns && remote_ip != NULL)
219 /* Get the real hostname if socket; otherwise return UNKNOWN. */
220 if (packet_connection_is_on_socket())
221 host = get_remote_hostname(packet_get_connection_in(), use_dns);
226 canonical_host_name = host;
233 * Returns the local/remote IP-address/hostname of socket as a string.
234 * The returned string must be freed.
237 get_socket_address(int sock, int remote, int flags)
239 struct sockaddr_storage addr;
241 char ntop[NI_MAXHOST];
244 /* Get IP address of client. */
245 addrlen = sizeof(addr);
246 memset(&addr, 0, sizeof(addr));
249 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
253 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
258 /* Work around Linux IPv6 weirdness */
259 if (addr.ss_family == AF_INET6)
260 addrlen = sizeof(struct sockaddr_in6);
262 ipv64_normalise_mapped(&addr, &addrlen);
264 /* Get the address in ascii. */
265 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
266 sizeof(ntop), NULL, 0, flags)) != 0) {
267 error("get_socket_address: getnameinfo %d failed: %s", flags,
268 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
271 return xstrdup(ntop);
275 get_peer_ipaddr(int sock)
279 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
281 return xstrdup("UNKNOWN");
285 get_local_ipaddr(int sock)
289 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
291 return xstrdup("UNKNOWN");
295 get_local_name(int sock)
297 return get_socket_address(sock, 0, NI_NAMEREQD);
301 * Returns the IP-address of the remote host as a string. The returned
302 * string must not be freed.
306 get_remote_ipaddr(void)
308 static char *canonical_host_ip = NULL;
310 /* Check whether we have cached the ipaddr. */
311 if (canonical_host_ip == NULL) {
312 if (packet_connection_is_on_socket()) {
314 get_peer_ipaddr(packet_get_connection_in());
315 if (canonical_host_ip == NULL)
318 /* If not on socket, return UNKNOWN. */
319 canonical_host_ip = xstrdup("UNKNOWN");
322 return canonical_host_ip;
326 get_remote_name_or_ip(u_int utmp_len, int use_dns)
328 static const char *remote = "";
330 remote = get_canonical_hostname(use_dns);
331 if (utmp_len == 0 || strlen(remote) > utmp_len)
332 remote = get_remote_ipaddr();
336 /* Returns the local/remote port for the socket. */
339 get_sock_port(int sock, int local)
341 struct sockaddr_storage from;
343 char strport[NI_MAXSERV];
346 /* Get IP address of client. */
347 fromlen = sizeof(from);
348 memset(&from, 0, sizeof(from));
350 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
351 error("getsockname failed: %.100s", strerror(errno));
355 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
356 debug("getpeername failed: %.100s", strerror(errno));
361 /* Work around Linux IPv6 weirdness */
362 if (from.ss_family == AF_INET6)
363 fromlen = sizeof(struct sockaddr_in6);
365 /* Return port number. */
366 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
367 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
368 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
369 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
370 return atoi(strport);
373 /* Returns remote/local port number for the current connection. */
379 * If the connection is not a socket, return 65535. This is
380 * intentionally chosen to be an unprivileged port number.
382 if (!packet_connection_is_on_socket())
385 /* Get socket and return the port number. */
386 return get_sock_port(packet_get_connection_in(), local);
390 get_peer_port(int sock)
392 return get_sock_port(sock, 0);
396 get_remote_port(void)
398 static int port = -1;
400 /* Cache to avoid getpeername() on a dead connection */