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