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