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