xref: /openbsd/usr.bin/ssh/canohost.c (revision ced49c3f)
1 /* $OpenBSD: canohost.c,v 1.70 2014/01/19 04:17:29 dtucker Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Functions for returning the canonical host name of the remote site.
7  *
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".
13  */
14 
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 
18 #include <netinet/in.h>
19 
20 #include <errno.h>
21 #include <netdb.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 
28 #include "xmalloc.h"
29 #include "packet.h"
30 #include "log.h"
31 #include "canohost.h"
32 #include "misc.h"
33 
34 static void check_ip_options(int, char *);
35 static char *canonical_host_ip = NULL;
36 static int cached_port = -1;
37 
38 /*
39  * Return the canonical name of the host at the other end of the socket. The
40  * caller should free the returned string.
41  */
42 
43 static char *
44 get_remote_hostname(int sock, int use_dns)
45 {
46 	struct sockaddr_storage from;
47 	socklen_t fromlen;
48 	struct addrinfo hints, *ai, *aitop;
49 	char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
50 
51 	/* Get IP address of client. */
52 	fromlen = sizeof(from);
53 	memset(&from, 0, sizeof(from));
54 	if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
55 		debug("getpeername failed: %.100s", strerror(errno));
56 		cleanup_exit(255);
57 	}
58 
59 	if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
60 	    NULL, 0, NI_NUMERICHOST) != 0)
61 		fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
62 
63 	if (from.ss_family == AF_INET)
64 		check_ip_options(sock, ntop);
65 
66 	if (!use_dns)
67 		return xstrdup(ntop);
68 
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. */
74 		return xstrdup(ntop);
75 	}
76 
77 	/*
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
81 	 */
82 	memset(&hints, 0, sizeof(hints));
83 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
84 	hints.ai_flags = AI_NUMERICHOST;
85 	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
86 		logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
87 		    name, ntop);
88 		freeaddrinfo(ai);
89 		return xstrdup(ntop);
90 	}
91 
92 	/* Names are stores in lowercase. */
93 	lowercase(name);
94 
95 	/*
96 	 * Map it back to an IP address and check that the given
97 	 * address actually is an address of this host.  This is
98 	 * necessary because anyone with access to a name server can
99 	 * define arbitrary names for an IP address. Mapping from
100 	 * name to IP address can be trusted better (but can still be
101 	 * fooled if the intruder has access to the name server of
102 	 * the domain).
103 	 */
104 	memset(&hints, 0, sizeof(hints));
105 	hints.ai_family = from.ss_family;
106 	hints.ai_socktype = SOCK_STREAM;
107 	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
108 		logit("reverse mapping checking getaddrinfo for %.700s "
109 		    "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
110 		return xstrdup(ntop);
111 	}
112 	/* Look for the address from the list of addresses. */
113 	for (ai = aitop; ai; ai = ai->ai_next) {
114 		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
115 		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
116 		    (strcmp(ntop, ntop2) == 0))
117 				break;
118 	}
119 	freeaddrinfo(aitop);
120 	/* If we reached the end of the list, the address was not there. */
121 	if (!ai) {
122 		/* Address not found for the host name. */
123 		logit("Address %.100s maps to %.600s, but this does not "
124 		    "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
125 		    ntop, name);
126 		return xstrdup(ntop);
127 	}
128 	return xstrdup(name);
129 }
130 
131 /*
132  * If IP options are supported, make sure there are none (log and
133  * disconnect them if any are found).  Basically we are worried about
134  * source routing; it can be used to pretend you are somebody
135  * (ip-address) you are not. That itself may be "almost acceptable"
136  * under certain circumstances, but rhosts autentication is useless
137  * if source routing is accepted. Notice also that if we just dropped
138  * source routing here, the other side could use IP spoofing to do
139  * rest of the interaction and could still bypass security.  So we
140  * exit here if we detect any IP options.
141  */
142 /* IPv4 only */
143 static void
144 check_ip_options(int sock, char *ipaddr)
145 {
146 	u_char options[200];
147 	char text[sizeof(options) * 3 + 1];
148 	socklen_t option_size, i;
149 	int ipproto;
150 	struct protoent *ip;
151 
152 	if ((ip = getprotobyname("ip")) != NULL)
153 		ipproto = ip->p_proto;
154 	else
155 		ipproto = IPPROTO_IP;
156 	option_size = sizeof(options);
157 	if (getsockopt(sock, ipproto, IP_OPTIONS, options,
158 	    &option_size) >= 0 && option_size != 0) {
159 		text[0] = '\0';
160 		for (i = 0; i < option_size; i++)
161 			snprintf(text + i*3, sizeof(text) - i*3,
162 			    " %2.2x", options[i]);
163 		fatal("Connection from %.100s with IP options:%.800s",
164 		    ipaddr, text);
165 	}
166 }
167 
168 /*
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  */
173 
174 const char *
175 get_canonical_hostname(int use_dns)
176 {
177 	char *host;
178 	static char *canonical_host_name = NULL;
179 	static char *remote_ip = NULL;
180 
181 	/* Check if we have previously retrieved name with same option. */
182 	if (use_dns && canonical_host_name != NULL)
183 		return canonical_host_name;
184 	if (!use_dns && remote_ip != NULL)
185 		return remote_ip;
186 
187 	/* Get the real hostname if socket; otherwise return UNKNOWN. */
188 	if (packet_connection_is_on_socket())
189 		host = get_remote_hostname(packet_get_connection_in(), use_dns);
190 	else
191 		host = "UNKNOWN";
192 
193 	if (use_dns)
194 		canonical_host_name = host;
195 	else
196 		remote_ip = host;
197 	return host;
198 }
199 
200 /*
201  * Returns the local/remote IP-address/hostname of socket as a string.
202  * The returned string must be freed.
203  */
204 static char *
205 get_socket_address(int sock, int remote, int flags)
206 {
207 	struct sockaddr_storage addr;
208 	socklen_t addrlen;
209 	char ntop[NI_MAXHOST];
210 	int r;
211 
212 	/* Get IP address of client. */
213 	addrlen = sizeof(addr);
214 	memset(&addr, 0, sizeof(addr));
215 
216 	if (remote) {
217 		if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
218 		    < 0)
219 			return NULL;
220 	} else {
221 		if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
222 		    < 0)
223 			return NULL;
224 	}
225 	/* Get the address in ascii. */
226 	if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
227 	    sizeof(ntop), NULL, 0, flags)) != 0) {
228 		error("get_socket_address: getnameinfo %d failed: %s", flags,
229 		    ssh_gai_strerror(r));
230 		return NULL;
231 	}
232 	return xstrdup(ntop);
233 }
234 
235 char *
236 get_peer_ipaddr(int sock)
237 {
238 	char *p;
239 
240 	if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
241 		return p;
242 	return xstrdup("UNKNOWN");
243 }
244 
245 char *
246 get_local_ipaddr(int sock)
247 {
248 	char *p;
249 
250 	if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
251 		return p;
252 	return xstrdup("UNKNOWN");
253 }
254 
255 char *
256 get_local_name(int fd)
257 {
258 	char *host, myname[NI_MAXHOST];
259 
260 	/* Assume we were passed a socket */
261 	if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
262 		return host;
263 
264 	/* Handle the case where we were passed a pipe */
265 	if (gethostname(myname, sizeof(myname)) == -1) {
266 		verbose("get_local_name: gethostname: %s", strerror(errno));
267 	} else {
268 		host = xstrdup(myname);
269 	}
270 
271 	return host;
272 }
273 
274 void
275 clear_cached_addr(void)
276 {
277 	free(canonical_host_ip);
278 	canonical_host_ip = NULL;
279 	cached_port = -1;
280 }
281 
282 /*
283  * Returns the IP-address of the remote host as a string.  The returned
284  * string must not be freed.
285  */
286 
287 const char *
288 get_remote_ipaddr(void)
289 {
290 	/* Check whether we have cached the ipaddr. */
291 	if (canonical_host_ip == NULL) {
292 		if (packet_connection_is_on_socket()) {
293 			canonical_host_ip =
294 			    get_peer_ipaddr(packet_get_connection_in());
295 			if (canonical_host_ip == NULL)
296 				cleanup_exit(255);
297 		} else {
298 			/* If not on socket, return UNKNOWN. */
299 			canonical_host_ip = xstrdup("UNKNOWN");
300 		}
301 	}
302 	return canonical_host_ip;
303 }
304 
305 const char *
306 get_remote_name_or_ip(u_int utmp_len, int use_dns)
307 {
308 	static const char *remote = "";
309 	if (utmp_len > 0)
310 		remote = get_canonical_hostname(use_dns);
311 	if (utmp_len == 0 || strlen(remote) > utmp_len)
312 		remote = get_remote_ipaddr();
313 	return remote;
314 }
315 
316 /* Returns the local/remote port for the socket. */
317 
318 int
319 get_sock_port(int sock, int local)
320 {
321 	struct sockaddr_storage from;
322 	socklen_t fromlen;
323 	char strport[NI_MAXSERV];
324 	int r;
325 
326 	/* Get IP address of client. */
327 	fromlen = sizeof(from);
328 	memset(&from, 0, sizeof(from));
329 	if (local) {
330 		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
331 			error("getsockname failed: %.100s", strerror(errno));
332 			return 0;
333 		}
334 	} else {
335 		if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
336 			debug("getpeername failed: %.100s", strerror(errno));
337 			return -1;
338 		}
339 	}
340 	/* Return port number. */
341 	if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
342 	    strport, sizeof(strport), NI_NUMERICSERV)) != 0)
343 		fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
344 		    ssh_gai_strerror(r));
345 	return atoi(strport);
346 }
347 
348 /* Returns remote/local port number for the current connection. */
349 
350 static int
351 get_port(int local)
352 {
353 	/*
354 	 * If the connection is not a socket, return 65535.  This is
355 	 * intentionally chosen to be an unprivileged port number.
356 	 */
357 	if (!packet_connection_is_on_socket())
358 		return 65535;
359 
360 	/* Get socket and return the port number. */
361 	return get_sock_port(packet_get_connection_in(), local);
362 }
363 
364 int
365 get_peer_port(int sock)
366 {
367 	return get_sock_port(sock, 0);
368 }
369 
370 int
371 get_remote_port(void)
372 {
373 	/* Cache to avoid getpeername() on a dead connection */
374 	if (cached_port == -1)
375 		cached_port = get_port(0);
376 
377 	return cached_port;
378 }
379 
380 int
381 get_local_port(void)
382 {
383 	return get_port(1);
384 }
385