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