blob: 1b5790929c844d472ac26099b7abcd22c5498ead [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * canohost.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Sun Jul 2 17:52:22 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Functions for returning the canonical host name of the remote site.
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller4af51302000-04-16 11:18:38 +100017RCSID("$Id: canohost.c,v 1.9 2000/04/16 01:18:40 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "packet.h"
20#include "xmalloc.h"
21#include "ssh.h"
22
Damien Miller5428f641999-11-25 11:54:57 +110023/*
24 * Return the canonical name of the host at the other end of the socket. The
25 * caller should free the returned string with xfree.
26 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller95def091999-11-25 00:26:21 +110028char *
29get_remote_hostname(int socket)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030{
Damien Miller34132e52000-01-14 15:45:46 +110031 struct sockaddr_storage from;
32 int i;
33 socklen_t fromlen;
34 struct addrinfo hints, *ai, *aitop;
Damien Miller95def091999-11-25 00:26:21 +110035 char name[MAXHOSTNAMELEN];
Damien Miller34132e52000-01-14 15:45:46 +110036 char ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller95def091999-11-25 00:26:21 +110038 /* Get IP address of client. */
39 fromlen = sizeof(from);
40 memset(&from, 0, sizeof(from));
41 if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) {
42 debug("getpeername failed: %.100s", strerror(errno));
43 fatal_cleanup();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044 }
Damien Miller7bcb0892000-03-11 20:45:40 +110045
46#ifdef IPV4_IN_IPV6
47 if (from.ss_family == AF_INET6) {
48 struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
49
50 /* Detect IPv4 in IPv6 mapped address and convert it to */
51 /* plain (AF_INET) IPv4 address */
52 if (IN6_IS_ADDR_V4MAPPED(&from6->sin6_addr)) {
53 struct sockaddr_in *from4 = (struct sockaddr_in *)&from;
54 struct in_addr addr;
55 u_int16_t port;
56
57 memcpy(&addr, ((char *)&from6->sin6_addr) + 12, sizeof(addr));
58 port = from6->sin6_port;
59
60 memset(&from, 0, sizeof(from));
61
62 from4->sin_family = AF_INET;
63 memcpy(&from4->sin_addr, &addr, sizeof(addr));
64 from4->sin_port = port;
65 }
66 }
67#endif
68
Damien Miller34132e52000-01-14 15:45:46 +110069 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
70 NULL, 0, NI_NUMERICHOST) != 0)
71 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
Damien Miller95def091999-11-25 00:26:21 +110072
Damien Miller34132e52000-01-14 15:45:46 +110073 /* Map the IP address to a host name. */
74 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
75 NULL, 0, NI_NAMEREQD) == 0) {
76 /* Got host name. */
77 name[sizeof(name) - 1] = '\0';
Damien Miller5428f641999-11-25 11:54:57 +110078 /*
79 * Convert it to all lowercase (which is expected by the rest
80 * of this software).
81 */
Damien Miller95def091999-11-25 00:26:21 +110082 for (i = 0; name[i]; i++)
83 if (isupper(name[i]))
84 name[i] = tolower(name[i]);
85
Damien Miller5428f641999-11-25 11:54:57 +110086 /*
87 * Map it back to an IP address and check that the given
88 * address actually is an address of this host. This is
89 * necessary because anyone with access to a name server can
90 * define arbitrary names for an IP address. Mapping from
91 * name to IP address can be trusted better (but can still be
92 * fooled if the intruder has access to the name server of
93 * the domain).
94 */
Damien Miller34132e52000-01-14 15:45:46 +110095 memset(&hints, 0, sizeof(hints));
96 hints.ai_family = from.ss_family;
97 hints.ai_socktype = SOCK_STREAM;
98 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
99 log("reverse mapping checking getaddrinfo for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name);
100 strlcpy(name, ntop, sizeof name);
Damien Miller95def091999-11-25 00:26:21 +1100101 goto check_ip_options;
102 }
103 /* Look for the address from the list of addresses. */
Damien Miller34132e52000-01-14 15:45:46 +1100104 for (ai = aitop; ai; ai = ai->ai_next) {
105 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
106 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
107 (strcmp(ntop, ntop2) == 0))
108 break;
109 }
110 freeaddrinfo(aitop);
111 /* If we reached the end of the list, the address was not there. */
112 if (!ai) {
Damien Miller95def091999-11-25 00:26:21 +1100113 /* Address not found for the host name. */
114 log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!",
Damien Miller34132e52000-01-14 15:45:46 +1100115 ntop, name);
116 strlcpy(name, ntop, sizeof name);
Damien Miller95def091999-11-25 00:26:21 +1100117 goto check_ip_options;
118 }
119 /* Address was found for the host name. We accept the host name. */
120 } else {
121 /* Host name not found. Use ascii representation of the address. */
Damien Miller34132e52000-01-14 15:45:46 +1100122 strlcpy(name, ntop, sizeof name);
Damien Miller95def091999-11-25 00:26:21 +1100123 log("Could not reverse map address %.100s.", name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller95def091999-11-25 00:26:21 +1100126check_ip_options:
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128 /*
129 * If IP options are supported, make sure there are none (log and
130 * disconnect them if any are found). Basically we are worried about
131 * source routing; it can be used to pretend you are somebody
132 * (ip-address) you are not. That itself may be "almost acceptable"
133 * under certain circumstances, but rhosts autentication is useless
134 * if source routing is accepted. Notice also that if we just dropped
135 * source routing here, the other side could use IP spoofing to do
136 * rest of the interaction and could still bypass security. So we
137 * exit here if we detect any IP options.
138 */
Damien Miller34132e52000-01-14 15:45:46 +1100139 /* IP options -- IPv4 only */
140 if (from.ss_family == AF_INET) {
Damien Miller95def091999-11-25 00:26:21 +1100141 unsigned char options[200], *ucp;
142 char text[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +1100143 socklen_t option_size;
144 int ipproto;
Damien Miller95def091999-11-25 00:26:21 +1100145 struct protoent *ip;
146
147 if ((ip = getprotobyname("ip")) != NULL)
148 ipproto = ip->p_proto;
149 else
150 ipproto = IPPROTO_IP;
151 option_size = sizeof(options);
152 if (getsockopt(0, ipproto, IP_OPTIONS, (char *) options,
Damien Miller34132e52000-01-14 15:45:46 +1100153 &option_size) >= 0 && option_size != 0) {
Damien Miller95def091999-11-25 00:26:21 +1100154 cp = text;
155 /* Note: "text" buffer must be at least 3x as big as options. */
156 for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3)
157 sprintf(cp, " %2.2x", *ucp);
158 log("Connection from %.100s with IP options:%.800s",
Damien Miller34132e52000-01-14 15:45:46 +1100159 ntop, text);
Damien Miller95def091999-11-25 00:26:21 +1100160 packet_disconnect("Connection from %.100s with IP options:%.800s",
Damien Miller34132e52000-01-14 15:45:46 +1100161 ntop, text);
Damien Miller95def091999-11-25 00:26:21 +1100162 }
163 }
164
165 return xstrdup(name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Return the canonical name of the host in the other side of the current
170 * connection. The host name is cached, so it is efficient to call this
171 * several times.
172 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Damien Miller95def091999-11-25 00:26:21 +1100174const char *
175get_canonical_hostname()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176{
Damien Miller34132e52000-01-14 15:45:46 +1100177 static char *canonical_host_name = NULL;
178
Damien Miller95def091999-11-25 00:26:21 +1100179 /* Check if we have previously retrieved this same name. */
180 if (canonical_host_name != NULL)
181 return canonical_host_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Damien Miller95def091999-11-25 00:26:21 +1100183 /* Get the real hostname if socket; otherwise return UNKNOWN. */
Damien Miller34132e52000-01-14 15:45:46 +1100184 if (packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +1100185 canonical_host_name = get_remote_hostname(packet_get_connection_in());
186 else
187 canonical_host_name = xstrdup("UNKNOWN");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 return canonical_host_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller5428f641999-11-25 11:54:57 +1100192/*
193 * Returns the IP-address of the remote host as a string. The returned
Damien Miller34132e52000-01-14 15:45:46 +1100194 * string must not be freed.
Damien Miller5428f641999-11-25 11:54:57 +1100195 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Miller95def091999-11-25 00:26:21 +1100197const char *
198get_remote_ipaddr()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller34132e52000-01-14 15:45:46 +1100200 static char *canonical_host_ip = NULL;
201 struct sockaddr_storage from;
202 socklen_t fromlen;
203 int socket;
204 char ntop[NI_MAXHOST];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller5428f641999-11-25 11:54:57 +1100206 /* Check whether we have chached the name. */
Damien Miller95def091999-11-25 00:26:21 +1100207 if (canonical_host_ip != NULL)
208 return canonical_host_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller95def091999-11-25 00:26:21 +1100210 /* If not a socket, return UNKNOWN. */
Damien Miller34132e52000-01-14 15:45:46 +1100211 if (!packet_connection_is_on_socket()) {
Damien Miller95def091999-11-25 00:26:21 +1100212 canonical_host_ip = xstrdup("UNKNOWN");
213 return canonical_host_ip;
214 }
215 /* Get client socket. */
216 socket = packet_get_connection_in();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Get IP address of client. */
219 fromlen = sizeof(from);
220 memset(&from, 0, sizeof(from));
221 if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) {
222 debug("getpeername failed: %.100s", strerror(errno));
223 fatal_cleanup();
224 }
225 /* Get the IP address in ascii. */
Damien Miller34132e52000-01-14 15:45:46 +1100226 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
227 NULL, 0, NI_NUMERICHOST) != 0)
228 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
229
230 canonical_host_ip = xstrdup(ntop);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller95def091999-11-25 00:26:21 +1100232 /* Return ip address string. */
233 return canonical_host_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234}
235
Damien Miller34132e52000-01-14 15:45:46 +1100236/* Returns the local/remote port for the socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller34132e52000-01-14 15:45:46 +1100238int
239get_sock_port(int sock, int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Damien Miller34132e52000-01-14 15:45:46 +1100241 struct sockaddr_storage from;
242 socklen_t fromlen;
243 char strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller95def091999-11-25 00:26:21 +1100245 /* Get IP address of client. */
246 fromlen = sizeof(from);
247 memset(&from, 0, sizeof(from));
Damien Miller34132e52000-01-14 15:45:46 +1100248 if (local) {
249 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
250 error("getsockname failed: %.100s", strerror(errno));
251 return 0;
252 }
253 } else {
254 if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
255 debug("getpeername failed: %.100s", strerror(errno));
256 fatal_cleanup();
257 }
Damien Miller95def091999-11-25 00:26:21 +1100258 }
259 /* Return port number. */
Damien Miller34132e52000-01-14 15:45:46 +1100260 if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
261 strport, sizeof(strport), NI_NUMERICSERV) != 0)
262 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
263 return atoi(strport);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264}
265
Damien Miller34132e52000-01-14 15:45:46 +1100266/* Returns remote/local port number for the current connection. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller4af51302000-04-16 11:18:38 +1000268int
Damien Miller34132e52000-01-14 15:45:46 +1100269get_port(int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270{
Damien Miller5428f641999-11-25 11:54:57 +1100271 /*
272 * If the connection is not a socket, return 65535. This is
273 * intentionally chosen to be an unprivileged port number.
274 */
Damien Miller34132e52000-01-14 15:45:46 +1100275 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +1100276 return 65535;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277
Damien Miller34132e52000-01-14 15:45:46 +1100278 /* Get socket and return the port number. */
279 return get_sock_port(packet_get_connection_in(), local);
280}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller4af51302000-04-16 11:18:38 +1000282int
Damien Miller34132e52000-01-14 15:45:46 +1100283get_peer_port(int sock)
284{
285 return get_sock_port(sock, 0);
286}
287
Damien Miller4af51302000-04-16 11:18:38 +1000288int
Damien Miller34132e52000-01-14 15:45:46 +1100289get_remote_port()
290{
291 return get_port(0);
292}
293
294int
295get_local_port()
296{
297 return get_port(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298}