Annotation of src/usr.bin/ssh/canohost.c, Revision 1.66
1.66 ! dtucker 1: /* $OpenBSD: canohost.c,v 1.65 2009/05/27 06:31:25 andreas Exp $ */
1.1 deraadt 2: /*
1.7 deraadt 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.
1.12 markus 7: *
1.14 deraadt 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".
1.7 deraadt 13: */
1.1 deraadt 14:
1.55 stevesk 15: #include <sys/types.h>
1.54 stevesk 16: #include <sys/socket.h>
17:
18: #include <netinet/in.h>
1.49 stevesk 19:
20: #include <ctype.h>
1.56 stevesk 21: #include <errno.h>
1.57 stevesk 22: #include <netdb.h>
1.60 stevesk 23: #include <stdio.h>
1.59 stevesk 24: #include <stdlib.h>
1.58 stevesk 25: #include <string.h>
1.61 deraadt 26: #include <stdarg.h>
1.66 ! dtucker 27: #include <unistd.h>
1.1 deraadt 28:
1.61 deraadt 29: #include "xmalloc.h"
1.1 deraadt 30: #include "packet.h"
1.18 markus 31: #include "log.h"
1.21 itojun 32: #include "canohost.h"
1.62 dtucker 33: #include "misc.h"
1.1 deraadt 34:
1.27 itojun 35: static void check_ip_options(int, char *);
1.65 andreas 36: static char *canonical_host_ip = NULL;
37: static int cached_port = -1;
1.20 markus 38:
1.8 markus 39: /*
40: * Return the canonical name of the host at the other end of the socket. The
41: * caller should free the returned string with xfree.
42: */
1.1 deraadt 43:
1.27 itojun 44: static char *
1.40 avsm 45: get_remote_hostname(int sock, int use_dns)
1.1 deraadt 46: {
1.10 markus 47: struct sockaddr_storage from;
48: int i;
49: socklen_t fromlen;
50: struct addrinfo hints, *ai, *aitop;
1.20 markus 51: char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
1.6 markus 52:
53: /* Get IP address of client. */
54: fromlen = sizeof(from);
55: memset(&from, 0, sizeof(from));
1.40 avsm 56: if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.6 markus 57: debug("getpeername failed: %.100s", strerror(errno));
1.38 markus 58: cleanup_exit(255);
1.6 markus 59: }
1.20 markus 60:
1.10 markus 61: if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
1.29 deraadt 62: NULL, 0, NI_NUMERICHOST) != 0)
1.10 markus 63: fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
1.45 dtucker 64:
65: if (from.ss_family == AF_INET)
66: check_ip_options(sock, ntop);
1.32 itojun 67:
1.37 markus 68: if (!use_dns)
69: return xstrdup(ntop);
1.10 markus 70:
1.26 markus 71: debug3("Trying to reverse map address %.100s.", ntop);
1.6 markus 72: /* Map the IP address to a host name. */
1.10 markus 73: if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
1.29 deraadt 74: NULL, 0, NI_NAMEREQD) != 0) {
1.20 markus 75: /* Host name not found. Use ip address. */
76: return xstrdup(ntop);
1.6 markus 77: }
78:
1.37 markus 79: /*
80: * if reverse lookup result looks like a numeric hostname,
81: * someone is trying to trick us by PTR record like following:
82: * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
83: */
84: memset(&hints, 0, sizeof(hints));
85: hints.ai_socktype = SOCK_DGRAM; /*dummy*/
86: hints.ai_flags = AI_NUMERICHOST;
1.63 dtucker 87: if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
1.37 markus 88: logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
89: name, ntop);
90: freeaddrinfo(ai);
91: return xstrdup(ntop);
92: }
93:
1.20 markus 94: /*
95: * Convert it to all lowercase (which is expected by the rest
96: * of this software).
97: */
98: for (i = 0; name[i]; i++)
99: if (isupper(name[i]))
1.52 deraadt 100: name[i] = (char)tolower(name[i]);
1.8 markus 101: /*
1.20 markus 102: * Map it back to an IP address and check that the given
103: * address actually is an address of this host. This is
104: * necessary because anyone with access to a name server can
105: * define arbitrary names for an IP address. Mapping from
106: * name to IP address can be trusted better (but can still be
107: * fooled if the intruder has access to the name server of
108: * the domain).
1.8 markus 109: */
1.20 markus 110: memset(&hints, 0, sizeof(hints));
111: hints.ai_family = from.ss_family;
112: hints.ai_socktype = SOCK_STREAM;
113: if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
1.36 itojun 114: logit("reverse mapping checking getaddrinfo for %.700s "
1.50 djm 115: "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
1.20 markus 116: return xstrdup(ntop);
117: }
118: /* Look for the address from the list of addresses. */
119: for (ai = aitop; ai; ai = ai->ai_next) {
120: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
121: sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
122: (strcmp(ntop, ntop2) == 0))
123: break;
124: }
125: freeaddrinfo(aitop);
126: /* If we reached the end of the list, the address was not there. */
127: if (!ai) {
128: /* Address not found for the host name. */
1.36 itojun 129: logit("Address %.100s maps to %.600s, but this does not "
1.48 stevesk 130: "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
1.20 markus 131: ntop, name);
132: return xstrdup(ntop);
133: }
134: return xstrdup(name);
135: }
1.6 markus 136:
1.20 markus 137: /*
138: * If IP options are supported, make sure there are none (log and
139: * disconnect them if any are found). Basically we are worried about
140: * source routing; it can be used to pretend you are somebody
141: * (ip-address) you are not. That itself may be "almost acceptable"
142: * under certain circumstances, but rhosts autentication is useless
143: * if source routing is accepted. Notice also that if we just dropped
144: * source routing here, the other side could use IP spoofing to do
145: * rest of the interaction and could still bypass security. So we
146: * exit here if we detect any IP options.
147: */
148: /* IPv4 only */
1.27 itojun 149: static void
1.40 avsm 150: check_ip_options(int sock, char *ipaddr)
1.20 markus 151: {
1.22 markus 152: u_char options[200];
153: char text[sizeof(options) * 3 + 1];
1.20 markus 154: socklen_t option_size;
1.44 djm 155: u_int i;
156: int ipproto;
1.20 markus 157: struct protoent *ip;
158:
159: if ((ip = getprotobyname("ip")) != NULL)
160: ipproto = ip->p_proto;
161: else
162: ipproto = IPPROTO_IP;
163: option_size = sizeof(options);
1.40 avsm 164: if (getsockopt(sock, ipproto, IP_OPTIONS, options,
1.20 markus 165: &option_size) >= 0 && option_size != 0) {
1.22 markus 166: text[0] = '\0';
167: for (i = 0; i < option_size; i++)
168: snprintf(text + i*3, sizeof(text) - i*3,
169: " %2.2x", options[i]);
1.46 dtucker 170: fatal("Connection from %.100s with IP options:%.800s",
1.20 markus 171: ipaddr, text);
1.6 markus 172: }
1.1 deraadt 173: }
174:
1.8 markus 175: /*
176: * Return the canonical name of the host in the other side of the current
177: * connection. The host name is cached, so it is efficient to call this
178: * several times.
179: */
1.1 deraadt 180:
1.6 markus 181: const char *
1.37 markus 182: get_canonical_hostname(int use_dns)
1.1 deraadt 183: {
1.47 dtucker 184: char *host;
1.10 markus 185: static char *canonical_host_name = NULL;
1.47 dtucker 186: static char *remote_ip = NULL;
1.10 markus 187:
1.20 markus 188: /* Check if we have previously retrieved name with same option. */
1.47 dtucker 189: if (use_dns && canonical_host_name != NULL)
190: return canonical_host_name;
191: if (!use_dns && remote_ip != NULL)
192: return remote_ip;
1.6 markus 193:
194: /* Get the real hostname if socket; otherwise return UNKNOWN. */
1.10 markus 195: if (packet_connection_is_on_socket())
1.47 dtucker 196: host = get_remote_hostname(packet_get_connection_in(), use_dns);
1.6 markus 197: else
1.47 dtucker 198: host = "UNKNOWN";
1.1 deraadt 199:
1.47 dtucker 200: if (use_dns)
201: canonical_host_name = host;
202: else
203: remote_ip = host;
204: return host;
1.1 deraadt 205: }
206:
1.8 markus 207: /*
1.35 stevesk 208: * Returns the local/remote IP-address/hostname of socket as a string.
209: * The returned string must be freed.
1.8 markus 210: */
1.27 itojun 211: static char *
1.40 avsm 212: get_socket_address(int sock, int remote, int flags)
1.1 deraadt 213: {
1.25 markus 214: struct sockaddr_storage addr;
215: socklen_t addrlen;
1.10 markus 216: char ntop[NI_MAXHOST];
1.42 djm 217: int r;
1.1 deraadt 218:
1.6 markus 219: /* Get IP address of client. */
1.25 markus 220: addrlen = sizeof(addr);
221: memset(&addr, 0, sizeof(addr));
222:
223: if (remote) {
1.40 avsm 224: if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
1.34 stevesk 225: < 0)
1.25 markus 226: return NULL;
227: } else {
1.40 avsm 228: if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
1.34 stevesk 229: < 0)
1.25 markus 230: return NULL;
231: }
232: /* Get the address in ascii. */
1.42 djm 233: if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
234: sizeof(ntop), NULL, 0, flags)) != 0) {
235: error("get_socket_address: getnameinfo %d failed: %s", flags,
1.62 dtucker 236: ssh_gai_strerror(r));
1.19 markus 237: return NULL;
238: }
239: return xstrdup(ntop);
1.25 markus 240: }
241:
242: char *
1.40 avsm 243: get_peer_ipaddr(int sock)
1.25 markus 244: {
1.34 stevesk 245: char *p;
246:
1.40 avsm 247: if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
1.34 stevesk 248: return p;
249: return xstrdup("UNKNOWN");
1.25 markus 250: }
251:
252: char *
1.40 avsm 253: get_local_ipaddr(int sock)
1.25 markus 254: {
1.34 stevesk 255: char *p;
256:
1.40 avsm 257: if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
1.34 stevesk 258: return p;
259: return xstrdup("UNKNOWN");
1.25 markus 260: }
261:
262: char *
1.66 ! dtucker 263: get_local_name(int fd)
1.25 markus 264: {
1.66 ! dtucker 265: char *host, myname[NI_MAXHOST];
! 266:
! 267: /* Assume we were passed a socket */
! 268: if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
! 269: return host;
! 270:
! 271: /* Handle the case where we were passed a pipe */
! 272: if (gethostname(myname, sizeof(myname)) == -1) {
! 273: verbose("get_local_name: gethostname: %s", strerror(errno));
! 274: } else {
! 275: host = xstrdup(myname);
! 276: }
! 277:
! 278: return host;
1.19 markus 279: }
280:
1.65 andreas 281: void
282: clear_cached_addr(void)
283: {
284: if (canonical_host_ip != NULL) {
285: xfree(canonical_host_ip);
286: canonical_host_ip = NULL;
287: }
288: cached_port = -1;
289: }
290:
1.19 markus 291: /*
292: * Returns the IP-address of the remote host as a string. The returned
293: * string must not be freed.
294: */
1.10 markus 295:
1.19 markus 296: const char *
1.28 itojun 297: get_remote_ipaddr(void)
1.19 markus 298: {
299: /* Check whether we have cached the ipaddr. */
300: if (canonical_host_ip == NULL) {
301: if (packet_connection_is_on_socket()) {
302: canonical_host_ip =
303: get_peer_ipaddr(packet_get_connection_in());
304: if (canonical_host_ip == NULL)
1.38 markus 305: cleanup_exit(255);
1.19 markus 306: } else {
307: /* If not on socket, return UNKNOWN. */
308: canonical_host_ip = xstrdup("UNKNOWN");
309: }
310: }
1.6 markus 311: return canonical_host_ip;
1.24 stevesk 312: }
313:
314: const char *
1.37 markus 315: get_remote_name_or_ip(u_int utmp_len, int use_dns)
1.24 stevesk 316: {
317: static const char *remote = "";
318: if (utmp_len > 0)
1.37 markus 319: remote = get_canonical_hostname(use_dns);
1.24 stevesk 320: if (utmp_len == 0 || strlen(remote) > utmp_len)
321: remote = get_remote_ipaddr();
322: return remote;
1.1 deraadt 323: }
324:
1.10 markus 325: /* Returns the local/remote port for the socket. */
1.1 deraadt 326:
1.64 djm 327: int
1.10 markus 328: get_sock_port(int sock, int local)
1.1 deraadt 329: {
1.10 markus 330: struct sockaddr_storage from;
331: socklen_t fromlen;
332: char strport[NI_MAXSERV];
1.42 djm 333: int r;
1.1 deraadt 334:
1.6 markus 335: /* Get IP address of client. */
336: fromlen = sizeof(from);
337: memset(&from, 0, sizeof(from));
1.10 markus 338: if (local) {
339: if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
340: error("getsockname failed: %.100s", strerror(errno));
341: return 0;
342: }
343: } else {
1.35 stevesk 344: if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.10 markus 345: debug("getpeername failed: %.100s", strerror(errno));
1.43 markus 346: return -1;
1.10 markus 347: }
1.6 markus 348: }
349: /* Return port number. */
1.42 djm 350: if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
351: strport, sizeof(strport), NI_NUMERICSERV)) != 0)
352: fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
1.62 dtucker 353: ssh_gai_strerror(r));
1.10 markus 354: return atoi(strport);
1.1 deraadt 355: }
356:
1.10 markus 357: /* Returns remote/local port number for the current connection. */
1.1 deraadt 358:
1.27 itojun 359: static int
1.10 markus 360: get_port(int local)
1.1 deraadt 361: {
1.8 markus 362: /*
363: * If the connection is not a socket, return 65535. This is
364: * intentionally chosen to be an unprivileged port number.
365: */
1.10 markus 366: if (!packet_connection_is_on_socket())
1.6 markus 367: return 65535;
1.1 deraadt 368:
1.10 markus 369: /* Get socket and return the port number. */
370: return get_sock_port(packet_get_connection_in(), local);
371: }
372:
1.12 markus 373: int
1.10 markus 374: get_peer_port(int sock)
375: {
376: return get_sock_port(sock, 0);
377: }
378:
1.12 markus 379: int
1.28 itojun 380: get_remote_port(void)
1.10 markus 381: {
1.41 djm 382: /* Cache to avoid getpeername() on a dead connection */
1.65 andreas 383: if (cached_port == -1)
384: cached_port = get_port(0);
1.41 djm 385:
1.65 andreas 386: return cached_port;
1.10 markus 387: }
1.1 deraadt 388:
1.10 markus 389: int
1.28 itojun 390: get_local_port(void)
1.10 markus 391: {
392: return get_port(1);
1.1 deraadt 393: }