xref: /freebsd/crypto/openssh/canohost.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Functions for returning the canonical host name of the remote site.
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13 
14 #include "includes.h"
15 RCSID("$OpenBSD: canohost.c,v 1.32 2002/06/11 08:11:45 itojun Exp $");
16 
17 #include "packet.h"
18 #include "xmalloc.h"
19 #include "log.h"
20 #include "canohost.h"
21 
22 static void check_ip_options(int, char *);
23 
24 /*
25  * Return the canonical name of the host at the other end of the socket. The
26  * caller should free the returned string with xfree.
27  */
28 
29 static char *
30 get_remote_hostname(int socket, int verify_reverse_mapping)
31 {
32 	struct sockaddr_storage from;
33 	int i;
34 	socklen_t fromlen;
35 	struct addrinfo hints, *ai, *aitop;
36 	char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
37 
38 	/* 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();
44 	}
45 #ifdef IPV4_IN_IPV6
46 	if (from.ss_family == AF_INET6) {
47 		struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
48 
49 		/* Detect IPv4 in IPv6 mapped address and convert it to */
50 		/* plain (AF_INET) IPv4 address */
51 		if (IN6_IS_ADDR_V4MAPPED(&from6->sin6_addr)) {
52 			struct sockaddr_in *from4 = (struct sockaddr_in *)&from;
53 			struct in_addr addr;
54 			u_int16_t port;
55 
56 			memcpy(&addr, ((char *)&from6->sin6_addr) + 12, sizeof(addr));
57 			port = from6->sin6_port;
58 
59 			memset(&from, 0, sizeof(from));
60 
61 			from4->sin_family = AF_INET;
62 			memcpy(&from4->sin_addr, &addr, sizeof(addr));
63 			from4->sin_port = port;
64 		}
65 	}
66 #endif
67 
68 	if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
69 	    NULL, 0, NI_NUMERICHOST) != 0)
70 		fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
71 
72 	if (from.ss_family == AF_INET)
73 		check_ip_options(socket, ntop);
74 
75 	debug3("Trying to reverse map address %.100s.", ntop);
76 	/* Map the IP address to a host name. */
77 	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
78 	    NULL, 0, NI_NAMEREQD) != 0) {
79 		/* Host name not found.  Use ip address. */
80 		log("Could not reverse map address %.100s.", ntop);
81 		return xstrdup(ntop);
82 	}
83 
84 	/* Got host name. */
85 	name[sizeof(name) - 1] = '\0';
86 	/*
87 	 * Convert it to all lowercase (which is expected by the rest
88 	 * of this software).
89 	 */
90 	for (i = 0; name[i]; i++)
91 		if (isupper(name[i]))
92 			name[i] = tolower(name[i]);
93 
94 	if (!verify_reverse_mapping)
95 		return xstrdup(name);
96 	/*
97 	 * Map it back to an IP address and check that the given
98 	 * address actually is an address of this host.  This is
99 	 * necessary because anyone with access to a name server can
100 	 * define arbitrary names for an IP address. Mapping from
101 	 * name to IP address can be trusted better (but can still be
102 	 * fooled if the intruder has access to the name server of
103 	 * the domain).
104 	 */
105 	memset(&hints, 0, sizeof(hints));
106 	hints.ai_family = from.ss_family;
107 	hints.ai_socktype = SOCK_STREAM;
108 	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
109 		log("reverse mapping checking getaddrinfo for %.700s "
110 		    "failed - POSSIBLE BREAKIN ATTEMPT!", name);
111 		return xstrdup(ntop);
112 	}
113 	/* Look for the address from the list of addresses. */
114 	for (ai = aitop; ai; ai = ai->ai_next) {
115 		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
116 		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
117 		    (strcmp(ntop, ntop2) == 0))
118 				break;
119 	}
120 	freeaddrinfo(aitop);
121 	/* If we reached the end of the list, the address was not there. */
122 	if (!ai) {
123 		/* Address not found for the host name. */
124 		log("Address %.100s maps to %.600s, but this does not "
125 		    "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
126 		    ntop, name);
127 		return xstrdup(ntop);
128 	}
129 	return xstrdup(name);
130 }
131 
132 /*
133  * If IP options are supported, make sure there are none (log and
134  * disconnect them if any are found).  Basically we are worried about
135  * source routing; it can be used to pretend you are somebody
136  * (ip-address) you are not. That itself may be "almost acceptable"
137  * under certain circumstances, but rhosts autentication is useless
138  * if source routing is accepted. Notice also that if we just dropped
139  * source routing here, the other side could use IP spoofing to do
140  * rest of the interaction and could still bypass security.  So we
141  * exit here if we detect any IP options.
142  */
143 /* IPv4 only */
144 static void
145 check_ip_options(int socket, char *ipaddr)
146 {
147 	u_char options[200];
148 	char text[sizeof(options) * 3 + 1];
149 	socklen_t option_size;
150 	int i, ipproto;
151 	struct protoent *ip;
152 
153 	if ((ip = getprotobyname("ip")) != NULL)
154 		ipproto = ip->p_proto;
155 	else
156 		ipproto = IPPROTO_IP;
157 	option_size = sizeof(options);
158 	if (getsockopt(socket, ipproto, IP_OPTIONS, options,
159 	    &option_size) >= 0 && option_size != 0) {
160 		text[0] = '\0';
161 		for (i = 0; i < option_size; i++)
162 			snprintf(text + i*3, sizeof(text) - i*3,
163 			    " %2.2x", options[i]);
164 		log("Connection from %.100s with IP options:%.800s",
165 		    ipaddr, text);
166 		packet_disconnect("Connection from %.100s with IP options:%.800s",
167 		    ipaddr, text);
168 	}
169 }
170 
171 /*
172  * Return the canonical name of the host in the other side of the current
173  * connection.  The host name is cached, so it is efficient to call this
174  * several times.
175  */
176 
177 const char *
178 get_canonical_hostname(int verify_reverse_mapping)
179 {
180 	static char *canonical_host_name = NULL;
181 	static int verify_reverse_mapping_done = 0;
182 
183 	/* Check if we have previously retrieved name with same option. */
184 	if (canonical_host_name != NULL) {
185 		if (verify_reverse_mapping_done != verify_reverse_mapping)
186 			xfree(canonical_host_name);
187 		else
188 			return canonical_host_name;
189 	}
190 
191 	/* Get the real hostname if socket; otherwise return UNKNOWN. */
192 	if (packet_connection_is_on_socket())
193 		canonical_host_name = get_remote_hostname(
194 		    packet_get_connection_in(), verify_reverse_mapping);
195 	else
196 		canonical_host_name = xstrdup("UNKNOWN");
197 
198 	verify_reverse_mapping_done = verify_reverse_mapping;
199 	return canonical_host_name;
200 }
201 
202 /*
203  * Returns the remote IP-address of socket as a string.  The returned
204  * string must be freed.
205  */
206 static char *
207 get_socket_address(int socket, int remote, int flags)
208 {
209 	struct sockaddr_storage addr;
210 	socklen_t addrlen;
211 	char ntop[NI_MAXHOST];
212 
213 	/* Get IP address of client. */
214 	addrlen = sizeof(addr);
215 	memset(&addr, 0, sizeof(addr));
216 
217 	if (remote) {
218 		if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
219 		    < 0) {
220 			debug("get_socket_ipaddr: getpeername failed: %.100s",
221 			    strerror(errno));
222 			return NULL;
223 		}
224 	} else {
225 		if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
226 		    < 0) {
227 			debug("get_socket_ipaddr: getsockname failed: %.100s",
228 			    strerror(errno));
229 			return NULL;
230 		}
231 	}
232 	/* Get the address in ascii. */
233 	if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop),
234 	    NULL, 0, flags) != 0) {
235 		error("get_socket_ipaddr: getnameinfo %d failed", flags);
236 		return NULL;
237 	}
238 	return xstrdup(ntop);
239 }
240 
241 char *
242 get_peer_ipaddr(int socket)
243 {
244 	return get_socket_address(socket, 1, NI_NUMERICHOST);
245 }
246 
247 char *
248 get_local_ipaddr(int socket)
249 {
250 	return get_socket_address(socket, 0, NI_NUMERICHOST);
251 }
252 
253 char *
254 get_local_name(int socket)
255 {
256 	return get_socket_address(socket, 0, NI_NAMEREQD);
257 }
258 
259 /*
260  * Returns the IP-address of the remote host as a string.  The returned
261  * string must not be freed.
262  */
263 
264 const char *
265 get_remote_ipaddr(void)
266 {
267 	static char *canonical_host_ip = NULL;
268 
269 	/* Check whether we have cached the ipaddr. */
270 	if (canonical_host_ip == NULL) {
271 		if (packet_connection_is_on_socket()) {
272 			canonical_host_ip =
273 			    get_peer_ipaddr(packet_get_connection_in());
274 			if (canonical_host_ip == NULL)
275 				fatal_cleanup();
276 		} else {
277 			/* If not on socket, return UNKNOWN. */
278 			canonical_host_ip = xstrdup("UNKNOWN");
279 		}
280 	}
281 	return canonical_host_ip;
282 }
283 
284 const char *
285 get_remote_name_or_ip(u_int utmp_len, int verify_reverse_mapping)
286 {
287 	static const char *remote = "";
288 	if (utmp_len > 0)
289 		remote = get_canonical_hostname(verify_reverse_mapping);
290 	if (utmp_len == 0 || strlen(remote) > utmp_len)
291 		remote = get_remote_ipaddr();
292 	return remote;
293 }
294 
295 /* Returns the local/remote port for the socket. */
296 
297 static int
298 get_sock_port(int sock, int local)
299 {
300 	struct sockaddr_storage from;
301 	socklen_t fromlen;
302 	char strport[NI_MAXSERV];
303 
304 	/* Get IP address of client. */
305 	fromlen = sizeof(from);
306 	memset(&from, 0, sizeof(from));
307 	if (local) {
308 		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
309 			error("getsockname failed: %.100s", strerror(errno));
310 			return 0;
311 		}
312 	} else {
313 		if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
314 			debug("getpeername failed: %.100s", strerror(errno));
315 			fatal_cleanup();
316 		}
317 	}
318 	/* Return port number. */
319 	if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
320 	    strport, sizeof(strport), NI_NUMERICSERV) != 0)
321 		fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
322 	return atoi(strport);
323 }
324 
325 /* Returns remote/local port number for the current connection. */
326 
327 static int
328 get_port(int local)
329 {
330 	/*
331 	 * If the connection is not a socket, return 65535.  This is
332 	 * intentionally chosen to be an unprivileged port number.
333 	 */
334 	if (!packet_connection_is_on_socket())
335 		return 65535;
336 
337 	/* Get socket and return the port number. */
338 	return get_sock_port(packet_get_connection_in(), local);
339 }
340 
341 int
342 get_peer_port(int sock)
343 {
344 	return get_sock_port(sock, 0);
345 }
346 
347 int
348 get_remote_port(void)
349 {
350 	return get_port(0);
351 }
352 
353 int
354 get_local_port(void)
355 {
356 	return get_port(1);
357 }
358