]> andersk Git - openssh.git/blame - canohost.c
- stevesk@cvs.openbsd.org 2006/07/11 20:07:25
[openssh.git] / canohost.c
CommitLineData
028094f4 1/* $OpenBSD: canohost.c,v 1.56 2006/07/11 20:07:25 stevesk Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * Functions for returning the canonical host name of the remote site.
6ae2364d 7 *
bcbf86ec 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".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
b6438382 16
5b04a8bf 17#include <sys/types.h>
9794d008 18#include <sys/socket.h>
19
20#include <netinet/in.h>
21
b6438382 22#include <ctype.h>
028094f4 23#include <errno.h>
8efc0c15 24
25#include "packet.h"
26#include "xmalloc.h"
42f11eb2 27#include "log.h"
5ca51e19 28#include "canohost.h"
8efc0c15 29
396c147e 30static void check_ip_options(int, char *);
61e96248 31
aa3378df 32/*
33 * Return the canonical name of the host at the other end of the socket. The
34 * caller should free the returned string with xfree.
35 */
8efc0c15 36
396c147e 37static char *
ca75d7de 38get_remote_hostname(int sock, int use_dns)
8efc0c15 39{
48e671d5 40 struct sockaddr_storage from;
41 int i;
42 socklen_t fromlen;
43 struct addrinfo hints, *ai, *aitop;
61e96248 44 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
5260325f 45
46 /* Get IP address of client. */
47 fromlen = sizeof(from);
48 memset(&from, 0, sizeof(from));
ca75d7de 49 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
5260325f 50 debug("getpeername failed: %.100s", strerror(errno));
2362db19 51 cleanup_exit(255);
5260325f 52 }
1639bb8f 53
88680c8b 54 if (from.ss_family == AF_INET)
55 check_ip_options(sock, ntop);
56
1639bb8f 57 ipv64_normalise_mapped(&from, &fromlen);
58
e7ccd20d 59 if (from.ss_family == AF_INET6)
60 fromlen = sizeof(struct sockaddr_in6);
80faa19f 61
48e671d5 62 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
184eed6a 63 NULL, 0, NI_NUMERICHOST) != 0)
48e671d5 64 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
5260325f 65
c5a7d788 66 if (!use_dns)
67 return xstrdup(ntop);
68
3a83b819 69 debug3("Trying to reverse map address %.100s.", ntop);
48e671d5 70 /* Map the IP address to a host name. */
71 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
184eed6a 72 NULL, 0, NI_NAMEREQD) != 0) {
61e96248 73 /* Host name not found. Use ip address. */
61e96248 74 return xstrdup(ntop);
8efc0c15 75 }
5260325f 76
c5a7d788 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*/
c44d7846 84 hints.ai_flags = AI_NUMERICHOST;
c5a7d788 85 if (getaddrinfo(name, "0", &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
61e96248 92 /*
93 * Convert it to all lowercase (which is expected by the rest
94 * of this software).
95 */
96 for (i = 0; name[i]; i++)
97 if (isupper(name[i]))
9555d258 98 name[i] = (char)tolower(name[i]);
aa3378df 99 /*
61e96248 100 * Map it back to an IP address and check that the given
101 * address actually is an address of this host. This is
102 * necessary because anyone with access to a name server can
103 * define arbitrary names for an IP address. Mapping from
104 * name to IP address can be trusted better (but can still be
105 * fooled if the intruder has access to the name server of
106 * the domain).
aa3378df 107 */
61e96248 108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = from.ss_family;
110 hints.ai_socktype = SOCK_STREAM;
111 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
bbe88b6d 112 logit("reverse mapping checking getaddrinfo for %.700s "
22aa23f8 113 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
61e96248 114 return xstrdup(ntop);
115 }
116 /* Look for the address from the list of addresses. */
117 for (ai = aitop; ai; ai = ai->ai_next) {
118 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
119 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
120 (strcmp(ntop, ntop2) == 0))
121 break;
122 }
123 freeaddrinfo(aitop);
124 /* If we reached the end of the list, the address was not there. */
125 if (!ai) {
126 /* Address not found for the host name. */
bbe88b6d 127 logit("Address %.100s maps to %.600s, but this does not "
1908529f 128 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
61e96248 129 ntop, name);
130 return xstrdup(ntop);
8efc0c15 131 }
5260325f 132 return xstrdup(name);
8efc0c15 133}
134
61e96248 135/*
136 * If IP options are supported, make sure there are none (log and
137 * disconnect them if any are found). Basically we are worried about
138 * source routing; it can be used to pretend you are somebody
139 * (ip-address) you are not. That itself may be "almost acceptable"
140 * under certain circumstances, but rhosts autentication is useless
141 * if source routing is accepted. Notice also that if we just dropped
142 * source routing here, the other side could use IP spoofing to do
143 * rest of the interaction and could still bypass security. So we
144 * exit here if we detect any IP options.
145 */
146/* IPv4 only */
396c147e 147static void
ca75d7de 148check_ip_options(int sock, char *ipaddr)
61e96248 149{
97722976 150#ifdef IP_OPTIONS
e6fa162e 151 u_char options[200];
152 char text[sizeof(options) * 3 + 1];
61e96248 153 socklen_t option_size;
2ceb8101 154 u_int i;
155 int ipproto;
61e96248 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);
ca75d7de 163 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
61e96248 164 &option_size) >= 0 && option_size != 0) {
e6fa162e 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]);
3a85986d 169 fatal("Connection from %.100s with IP options:%.800s",
61e96248 170 ipaddr, text);
171 }
97722976 172#endif /* IP_OPTIONS */
61e96248 173}
174
b6610e8f 175void
1639bb8f 176ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
177{
178 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
179 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
180 struct in_addr inaddr;
181 u_int16_t port;
182
d1cf9a87 183 if (addr->ss_family != AF_INET6 ||
1639bb8f 184 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
185 return;
186
187 debug3("Normalising mapped IPv4 in IPv6 address");
188
189 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
190 port = a6->sin6_port;
191
192 memset(addr, 0, sizeof(*a4));
193
194 a4->sin_family = AF_INET;
195 *len = sizeof(*a4);
196 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
197 a4->sin_port = port;
198}
199
aa3378df 200/*
201 * Return the canonical name of the host in the other side of the current
202 * connection. The host name is cached, so it is efficient to call this
203 * several times.
204 */
8efc0c15 205
5260325f 206const char *
c5a7d788 207get_canonical_hostname(int use_dns)
8efc0c15 208{
0bbbf2a4 209 char *host;
48e671d5 210 static char *canonical_host_name = NULL;
0bbbf2a4 211 static char *remote_ip = NULL;
48e671d5 212
61e96248 213 /* Check if we have previously retrieved name with same option. */
0bbbf2a4 214 if (use_dns && canonical_host_name != NULL)
215 return canonical_host_name;
216 if (!use_dns && remote_ip != NULL)
217 return remote_ip;
8efc0c15 218
5260325f 219 /* Get the real hostname if socket; otherwise return UNKNOWN. */
48e671d5 220 if (packet_connection_is_on_socket())
0bbbf2a4 221 host = get_remote_hostname(packet_get_connection_in(), use_dns);
5260325f 222 else
0bbbf2a4 223 host = "UNKNOWN";
8efc0c15 224
0bbbf2a4 225 if (use_dns)
226 canonical_host_name = host;
227 else
228 remote_ip = host;
229 return host;
8efc0c15 230}
231
aa3378df 232/*
649bb60b 233 * Returns the local/remote IP-address/hostname of socket as a string.
234 * The returned string must be freed.
aa3378df 235 */
396c147e 236static char *
ca75d7de 237get_socket_address(int sock, int remote, int flags)
8efc0c15 238{
8002af61 239 struct sockaddr_storage addr;
240 socklen_t addrlen;
48e671d5 241 char ntop[NI_MAXHOST];
adc75586 242 int r;
5260325f 243
5260325f 244 /* Get IP address of client. */
8002af61 245 addrlen = sizeof(addr);
246 memset(&addr, 0, sizeof(addr));
247
248 if (remote) {
ca75d7de 249 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
99443922 250 < 0)
8002af61 251 return NULL;
8002af61 252 } else {
ca75d7de 253 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
99443922 254 < 0)
8002af61 255 return NULL;
5260325f 256 }
e7ccd20d 257
258 /* Work around Linux IPv6 weirdness */
259 if (addr.ss_family == AF_INET6)
260 addrlen = sizeof(struct sockaddr_in6);
261
a56cebd3 262 ipv64_normalise_mapped(&addr, &addrlen);
263
8002af61 264 /* Get the address in ascii. */
adc75586 265 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
266 sizeof(ntop), NULL, 0, flags)) != 0) {
267 error("get_socket_address: getnameinfo %d failed: %s", flags,
268 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
865ac82e 269 return NULL;
270 }
271 return xstrdup(ntop);
272}
48e671d5 273
8002af61 274char *
ca75d7de 275get_peer_ipaddr(int sock)
8002af61 276{
99443922 277 char *p;
278
ca75d7de 279 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
99443922 280 return p;
281 return xstrdup("UNKNOWN");
8002af61 282}
283
284char *
ca75d7de 285get_local_ipaddr(int sock)
8002af61 286{
99443922 287 char *p;
288
ca75d7de 289 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
99443922 290 return p;
291 return xstrdup("UNKNOWN");
8002af61 292}
293
294char *
ca75d7de 295get_local_name(int sock)
8002af61 296{
ca75d7de 297 return get_socket_address(sock, 0, NI_NAMEREQD);
8002af61 298}
299
865ac82e 300/*
301 * Returns the IP-address of the remote host as a string. The returned
302 * string must not be freed.
303 */
5260325f 304
865ac82e 305const char *
d5bb9418 306get_remote_ipaddr(void)
865ac82e 307{
308 static char *canonical_host_ip = NULL;
309
310 /* Check whether we have cached the ipaddr. */
311 if (canonical_host_ip == NULL) {
312 if (packet_connection_is_on_socket()) {
313 canonical_host_ip =
314 get_peer_ipaddr(packet_get_connection_in());
315 if (canonical_host_ip == NULL)
2362db19 316 cleanup_exit(255);
865ac82e 317 } else {
318 /* If not on socket, return UNKNOWN. */
319 canonical_host_ip = xstrdup("UNKNOWN");
320 }
321 }
5260325f 322 return canonical_host_ip;
8efc0c15 323}
324
46e3af7f 325const char *
c5a7d788 326get_remote_name_or_ip(u_int utmp_len, int use_dns)
46e3af7f 327{
328 static const char *remote = "";
329 if (utmp_len > 0)
c5a7d788 330 remote = get_canonical_hostname(use_dns);
46e3af7f 331 if (utmp_len == 0 || strlen(remote) > utmp_len)
332 remote = get_remote_ipaddr();
333 return remote;
334}
335
48e671d5 336/* Returns the local/remote port for the socket. */
8efc0c15 337
396c147e 338static int
48e671d5 339get_sock_port(int sock, int local)
8efc0c15 340{
48e671d5 341 struct sockaddr_storage from;
342 socklen_t fromlen;
343 char strport[NI_MAXSERV];
adc75586 344 int r;
5260325f 345
346 /* Get IP address of client. */
347 fromlen = sizeof(from);
348 memset(&from, 0, sizeof(from));
48e671d5 349 if (local) {
350 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
351 error("getsockname failed: %.100s", strerror(errno));
352 return 0;
353 }
354 } else {
649bb60b 355 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
48e671d5 356 debug("getpeername failed: %.100s", strerror(errno));
29798ed0 357 return -1;
48e671d5 358 }
5260325f 359 }
e7ccd20d 360
361 /* Work around Linux IPv6 weirdness */
362 if (from.ss_family == AF_INET6)
363 fromlen = sizeof(struct sockaddr_in6);
364
5260325f 365 /* Return port number. */
adc75586 366 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
367 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
368 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
369 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
48e671d5 370 return atoi(strport);
8efc0c15 371}
372
48e671d5 373/* Returns remote/local port number for the current connection. */
8efc0c15 374
396c147e 375static int
48e671d5 376get_port(int local)
8efc0c15 377{
aa3378df 378 /*
379 * If the connection is not a socket, return 65535. This is
380 * intentionally chosen to be an unprivileged port number.
381 */
48e671d5 382 if (!packet_connection_is_on_socket())
5260325f 383 return 65535;
8efc0c15 384
48e671d5 385 /* Get socket and return the port number. */
386 return get_sock_port(packet_get_connection_in(), local);
387}
388
6ae2364d 389int
48e671d5 390get_peer_port(int sock)
391{
392 return get_sock_port(sock, 0);
393}
8efc0c15 394
6ae2364d 395int
d5bb9418 396get_remote_port(void)
48e671d5 397{
13f2a382 398 static int port = -1;
399
400 /* Cache to avoid getpeername() on a dead connection */
401 if (port == -1)
402 port = get_port(0);
403
404 return port;
48e671d5 405}
406
407int
d5bb9418 408get_local_port(void)
48e671d5 409{
410 return get_port(1);
8efc0c15 411}
This page took 0.299 seconds and 5 git commands to generate.