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