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