]> andersk Git - gssapi-openssh.git/blame - openssh/canohost.c
merged OpenSSH 5.3p1 to trunk
[gssapi-openssh.git] / openssh / canohost.c
CommitLineData
b5afdff5 1/* $OpenBSD: canohost.c,v 1.65 2009/05/27 06:31:25 andreas Exp $ */
3c0ef626 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 "includes.h"
3c0ef626 16
30460aeb 17#include <sys/types.h>
18#include <sys/socket.h>
d7ffeb35 19#include <sys/param.h> /* for MAXHOSTNAMELEN */
30460aeb 20
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
24#include <ctype.h>
25#include <errno.h>
26#include <netdb.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdarg.h>
31#include <unistd.h>
32
3c0ef626 33#include "xmalloc.h"
30460aeb 34#include "packet.h"
3c0ef626 35#include "log.h"
36#include "canohost.h"
e74dc197 37#include "misc.h"
3c0ef626 38
39static void check_ip_options(int, char *);
b5afdff5 40static char *canonical_host_ip = NULL;
41static int cached_port = -1;
3c0ef626 42
43/*
44 * Return the canonical name of the host at the other end of the socket. The
45 * caller should free the returned string with xfree.
46 */
47
48static char *
7e82606e 49get_remote_hostname(int sock, int use_dns)
3c0ef626 50{
51 struct sockaddr_storage from;
52 int i;
53 socklen_t fromlen;
54 struct addrinfo hints, *ai, *aitop;
55 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
56
57 /* Get IP address of client. */
58 fromlen = sizeof(from);
59 memset(&from, 0, sizeof(from));
7e82606e 60 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
3c0ef626 61 debug("getpeername failed: %.100s", strerror(errno));
540d72c3 62 cleanup_exit(255);
3c0ef626 63 }
540d72c3 64
30460aeb 65 if (from.ss_family == AF_INET)
66 check_ip_options(sock, ntop);
67
540d72c3 68 ipv64_normalise_mapped(&from, &fromlen);
69
bfe49944 70 if (from.ss_family == AF_INET6)
71 fromlen = sizeof(struct sockaddr_in6);
3c0ef626 72
73 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
dfe9414c 74 NULL, 0, NI_NUMERICHOST) != 0)
3c0ef626 75 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
76
7cac2b65 77 if (!use_dns)
78 return xstrdup(ntop);
79
3c0ef626 80 debug3("Trying to reverse map address %.100s.", ntop);
81 /* Map the IP address to a host name. */
82 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
dfe9414c 83 NULL, 0, NI_NAMEREQD) != 0) {
3c0ef626 84 /* Host name not found. Use ip address. */
3c0ef626 85 return xstrdup(ntop);
86 }
87
7cac2b65 88 /*
89 * if reverse lookup result looks like a numeric hostname,
90 * someone is trying to trick us by PTR record like following:
91 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
92 */
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
95 hints.ai_flags = AI_NUMERICHOST;
5156b1a1 96 if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
7cac2b65 97 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
98 name, ntop);
99 freeaddrinfo(ai);
100 return xstrdup(ntop);
101 }
102
3c0ef626 103 /*
104 * Convert it to all lowercase (which is expected by the rest
105 * of this software).
106 */
107 for (i = 0; name[i]; i++)
108 if (isupper(name[i]))
30460aeb 109 name[i] = (char)tolower(name[i]);
3c0ef626 110 /*
111 * Map it back to an IP address and check that the given
112 * address actually is an address of this host. This is
113 * necessary because anyone with access to a name server can
114 * define arbitrary names for an IP address. Mapping from
115 * name to IP address can be trusted better (but can still be
116 * fooled if the intruder has access to the name server of
117 * the domain).
118 */
119 memset(&hints, 0, sizeof(hints));
120 hints.ai_family = from.ss_family;
121 hints.ai_socktype = SOCK_STREAM;
122 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
7cac2b65 123 logit("reverse mapping checking getaddrinfo for %.700s "
30460aeb 124 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
3c0ef626 125 return xstrdup(ntop);
126 }
127 /* Look for the address from the list of addresses. */
128 for (ai = aitop; ai; ai = ai->ai_next) {
129 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
130 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
131 (strcmp(ntop, ntop2) == 0))
132 break;
133 }
134 freeaddrinfo(aitop);
135 /* If we reached the end of the list, the address was not there. */
136 if (!ai) {
137 /* Address not found for the host name. */
7cac2b65 138 logit("Address %.100s maps to %.600s, but this does not "
08822d99 139 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
3c0ef626 140 ntop, name);
141 return xstrdup(ntop);
142 }
143 return xstrdup(name);
144}
145
146/*
147 * If IP options are supported, make sure there are none (log and
148 * disconnect them if any are found). Basically we are worried about
149 * source routing; it can be used to pretend you are somebody
150 * (ip-address) you are not. That itself may be "almost acceptable"
151 * under certain circumstances, but rhosts autentication is useless
152 * if source routing is accepted. Notice also that if we just dropped
153 * source routing here, the other side could use IP spoofing to do
154 * rest of the interaction and could still bypass security. So we
155 * exit here if we detect any IP options.
156 */
157/* IPv4 only */
158static void
7e82606e 159check_ip_options(int sock, char *ipaddr)
3c0ef626 160{
7cac2b65 161#ifdef IP_OPTIONS
3c0ef626 162 u_char options[200];
163 char text[sizeof(options) * 3 + 1];
164 socklen_t option_size;
2ce0bfe4 165 u_int i;
166 int ipproto;
3c0ef626 167 struct protoent *ip;
168
169 if ((ip = getprotobyname("ip")) != NULL)
170 ipproto = ip->p_proto;
171 else
172 ipproto = IPPROTO_IP;
173 option_size = sizeof(options);
7e82606e 174 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
3c0ef626 175 &option_size) >= 0 && option_size != 0) {
176 text[0] = '\0';
177 for (i = 0; i < option_size; i++)
178 snprintf(text + i*3, sizeof(text) - i*3,
179 " %2.2x", options[i]);
08822d99 180 fatal("Connection from %.100s with IP options:%.800s",
3c0ef626 181 ipaddr, text);
182 }
7cac2b65 183#endif /* IP_OPTIONS */
3c0ef626 184}
185
dfddba3d 186void
540d72c3 187ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
188{
189 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
190 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
191 struct in_addr inaddr;
192 u_int16_t port;
193
2ce0bfe4 194 if (addr->ss_family != AF_INET6 ||
540d72c3 195 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
196 return;
197
198 debug3("Normalising mapped IPv4 in IPv6 address");
199
200 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
201 port = a6->sin6_port;
202
203 memset(addr, 0, sizeof(*a4));
204
205 a4->sin_family = AF_INET;
206 *len = sizeof(*a4);
207 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
208 a4->sin_port = port;
209}
210
3c0ef626 211/*
212 * Return the canonical name of the host in the other side of the current
213 * connection. The host name is cached, so it is efficient to call this
214 * several times.
215 */
216
217const char *
7cac2b65 218get_canonical_hostname(int use_dns)
3c0ef626 219{
08822d99 220 char *host;
3c0ef626 221 static char *canonical_host_name = NULL;
08822d99 222 static char *remote_ip = NULL;
3c0ef626 223
224 /* Check if we have previously retrieved name with same option. */
08822d99 225 if (use_dns && canonical_host_name != NULL)
226 return canonical_host_name;
227 if (!use_dns && remote_ip != NULL)
228 return remote_ip;
3c0ef626 229
230 /* Get the real hostname if socket; otherwise return UNKNOWN. */
231 if (packet_connection_is_on_socket())
08822d99 232 host = get_remote_hostname(packet_get_connection_in(), use_dns);
3c0ef626 233 else
08822d99 234 host = "UNKNOWN";
3c0ef626 235
08822d99 236 if (use_dns)
237 canonical_host_name = host;
238 else
239 remote_ip = host;
240 return host;
3c0ef626 241}
242
243/*
bfe49944 244 * Returns the local/remote IP-address/hostname of socket as a string.
245 * The returned string must be freed.
3c0ef626 246 */
247static char *
7e82606e 248get_socket_address(int sock, int remote, int flags)
3c0ef626 249{
250 struct sockaddr_storage addr;
251 socklen_t addrlen;
252 char ntop[NI_MAXHOST];
dfddba3d 253 int r;
3c0ef626 254
255 /* Get IP address of client. */
256 addrlen = sizeof(addr);
257 memset(&addr, 0, sizeof(addr));
258
259 if (remote) {
7e82606e 260 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
d03f4262 261 < 0)
3c0ef626 262 return NULL;
3c0ef626 263 } else {
7e82606e 264 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
d03f4262 265 < 0)
3c0ef626 266 return NULL;
3c0ef626 267 }
bfe49944 268
269 /* Work around Linux IPv6 weirdness */
270 if (addr.ss_family == AF_INET6)
271 addrlen = sizeof(struct sockaddr_in6);
272
8b32eddc 273 ipv64_normalise_mapped(&addr, &addrlen);
274
3c0ef626 275 /* Get the address in ascii. */
dfddba3d 276 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
277 sizeof(ntop), NULL, 0, flags)) != 0) {
278 error("get_socket_address: getnameinfo %d failed: %s", flags,
e74dc197 279 ssh_gai_strerror(r));
3c0ef626 280 return NULL;
281 }
282 return xstrdup(ntop);
283}
284
285char *
7e82606e 286get_peer_ipaddr(int sock)
3c0ef626 287{
d03f4262 288 char *p;
289
7e82606e 290 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
d03f4262 291 return p;
292 return xstrdup("UNKNOWN");
3c0ef626 293}
294
295char *
7e82606e 296get_local_ipaddr(int sock)
3c0ef626 297{
d03f4262 298 char *p;
299
7e82606e 300 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
d03f4262 301 return p;
302 return xstrdup("UNKNOWN");
3c0ef626 303}
304
305char *
7e82606e 306get_local_name(int sock)
3c0ef626 307{
7e82606e 308 return get_socket_address(sock, 0, NI_NAMEREQD);
3c0ef626 309}
310
b5afdff5 311void
312clear_cached_addr(void)
313{
314 if (canonical_host_ip != NULL) {
315 xfree(canonical_host_ip);
316 canonical_host_ip = NULL;
317 }
318 cached_port = -1;
319}
320
3c0ef626 321/*
322 * Returns the IP-address of the remote host as a string. The returned
323 * string must not be freed.
324 */
325
326const char *
dfe9414c 327get_remote_ipaddr(void)
3c0ef626 328{
3c0ef626 329 /* Check whether we have cached the ipaddr. */
330 if (canonical_host_ip == NULL) {
331 if (packet_connection_is_on_socket()) {
332 canonical_host_ip =
333 get_peer_ipaddr(packet_get_connection_in());
334 if (canonical_host_ip == NULL)
540d72c3 335 cleanup_exit(255);
3c0ef626 336 } else {
337 /* If not on socket, return UNKNOWN. */
338 canonical_host_ip = xstrdup("UNKNOWN");
339 }
340 }
341 return canonical_host_ip;
342}
343
344const char *
7cac2b65 345get_remote_name_or_ip(u_int utmp_len, int use_dns)
3c0ef626 346{
347 static const char *remote = "";
348 if (utmp_len > 0)
7cac2b65 349 remote = get_canonical_hostname(use_dns);
3c0ef626 350 if (utmp_len == 0 || strlen(remote) > utmp_len)
351 remote = get_remote_ipaddr();
352 return remote;
353}
354
355/* Returns the local/remote port for the socket. */
356
5262cbfb 357int
3c0ef626 358get_sock_port(int sock, int local)
359{
360 struct sockaddr_storage from;
361 socklen_t fromlen;
362 char strport[NI_MAXSERV];
dfddba3d 363 int r;
3c0ef626 364
365 /* Get IP address of client. */
366 fromlen = sizeof(from);
367 memset(&from, 0, sizeof(from));
368 if (local) {
369 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
370 error("getsockname failed: %.100s", strerror(errno));
371 return 0;
372 }
373 } else {
bfe49944 374 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
3c0ef626 375 debug("getpeername failed: %.100s", strerror(errno));
2ce0bfe4 376 return -1;
3c0ef626 377 }
378 }
bfe49944 379
380 /* Work around Linux IPv6 weirdness */
381 if (from.ss_family == AF_INET6)
382 fromlen = sizeof(struct sockaddr_in6);
383
3c0ef626 384 /* Return port number. */
dfddba3d 385 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
386 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
387 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
e74dc197 388 ssh_gai_strerror(r));
3c0ef626 389 return atoi(strport);
390}
391
392/* Returns remote/local port number for the current connection. */
393
394static int
395get_port(int local)
396{
397 /*
398 * If the connection is not a socket, return 65535. This is
399 * intentionally chosen to be an unprivileged port number.
400 */
401 if (!packet_connection_is_on_socket())
402 return 65535;
403
404 /* Get socket and return the port number. */
405 return get_sock_port(packet_get_connection_in(), local);
406}
407
408int
409get_peer_port(int sock)
410{
411 return get_sock_port(sock, 0);
412}
413
414int
dfe9414c 415get_remote_port(void)
3c0ef626 416{
7e82606e 417 /* Cache to avoid getpeername() on a dead connection */
b5afdff5 418 if (cached_port == -1)
419 cached_port = get_port(0);
7e82606e 420
b5afdff5 421 return cached_port;
3c0ef626 422}
423
424int
dfe9414c 425get_local_port(void)
3c0ef626 426{
427 return get_port(1);
428}
dfe9414c 429
dfe9414c 430void
1df48666 431resolve_localhost(char **host)
dfe9414c 432{
433 struct hostent *hostinfo;
434
435 hostinfo = gethostbyname(*host);
890e8b9d 436 if (hostinfo == NULL || hostinfo->h_name == NULL) {
437 debug("gethostbyname(%s) failed", *host);
438 return;
439 }
1df48666 440 if (hostinfo->h_addrtype == AF_INET) {
dfe9414c 441 struct in_addr addr;
442 addr = *(struct in_addr *)(hostinfo->h_addr);
443 if (ntohl(addr.s_addr) == INADDR_LOOPBACK) {
444 char buf[MAXHOSTNAMELEN];
890e8b9d 445 if (gethostname(buf, sizeof(buf)) < 0) {
446 debug("gethostname() failed");
447 return;
dfe9414c 448 }
890e8b9d 449 hostinfo = gethostbyname(buf);
1df48666 450 xfree(*host);
41d8cd01 451 if (hostinfo == NULL || hostinfo->h_name == NULL) {
452 *host = xstrdup(buf);
453 } else {
454 *host = xstrdup(hostinfo->h_name);
455 }
dfe9414c 456 }
457 }
dfe9414c 458}
This page took 0.194069 seconds and 5 git commands to generate.