Annotation of src/usr.bin/ssh/canohost.c, Revision 1.57
1.57 ! stevesk 1: /* $OpenBSD: canohost.c,v 1.56 2006/07/11 20:07:25 stevesk 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:
15: #include "includes.h"
1.54 stevesk 16:
1.55 stevesk 17: #include <sys/types.h>
1.54 stevesk 18: #include <sys/socket.h>
19:
20: #include <netinet/in.h>
1.49 stevesk 21:
22: #include <ctype.h>
1.56 stevesk 23: #include <errno.h>
1.57 ! stevesk 24: #include <netdb.h>
1.1 deraadt 25:
26: #include "packet.h"
27: #include "xmalloc.h"
1.18 markus 28: #include "log.h"
1.21 itojun 29: #include "canohost.h"
1.1 deraadt 30:
1.27 itojun 31: static void check_ip_options(int, char *);
1.20 markus 32:
1.8 markus 33: /*
34: * Return the canonical name of the host at the other end of the socket. The
35: * caller should free the returned string with xfree.
36: */
1.1 deraadt 37:
1.27 itojun 38: static char *
1.40 avsm 39: get_remote_hostname(int sock, int use_dns)
1.1 deraadt 40: {
1.10 markus 41: struct sockaddr_storage from;
42: int i;
43: socklen_t fromlen;
44: struct addrinfo hints, *ai, *aitop;
1.20 markus 45: char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
1.6 markus 46:
47: /* Get IP address of client. */
48: fromlen = sizeof(from);
49: memset(&from, 0, sizeof(from));
1.40 avsm 50: if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.6 markus 51: debug("getpeername failed: %.100s", strerror(errno));
1.38 markus 52: cleanup_exit(255);
1.6 markus 53: }
1.20 markus 54:
1.10 markus 55: if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
1.29 deraadt 56: NULL, 0, NI_NUMERICHOST) != 0)
1.10 markus 57: fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
1.45 dtucker 58:
59: if (from.ss_family == AF_INET)
60: check_ip_options(sock, ntop);
1.32 itojun 61:
1.37 markus 62: if (!use_dns)
63: return xstrdup(ntop);
1.10 markus 64:
1.26 markus 65: debug3("Trying to reverse map address %.100s.", ntop);
1.6 markus 66: /* Map the IP address to a host name. */
1.10 markus 67: if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
1.29 deraadt 68: NULL, 0, NI_NAMEREQD) != 0) {
1.20 markus 69: /* Host name not found. Use ip address. */
70: return xstrdup(ntop);
1.6 markus 71: }
72:
1.37 markus 73: /*
74: * if reverse lookup result looks like a numeric hostname,
75: * someone is trying to trick us by PTR record like following:
76: * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
77: */
78: memset(&hints, 0, sizeof(hints));
79: hints.ai_socktype = SOCK_DGRAM; /*dummy*/
80: hints.ai_flags = AI_NUMERICHOST;
81: if (getaddrinfo(name, "0", &hints, &ai) == 0) {
82: logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
83: name, ntop);
84: freeaddrinfo(ai);
85: return xstrdup(ntop);
86: }
87:
1.20 markus 88: /*
89: * Convert it to all lowercase (which is expected by the rest
90: * of this software).
91: */
92: for (i = 0; name[i]; i++)
93: if (isupper(name[i]))
1.52 deraadt 94: name[i] = (char)tolower(name[i]);
1.8 markus 95: /*
1.20 markus 96: * Map it back to an IP address and check that the given
97: * address actually is an address of this host. This is
98: * necessary because anyone with access to a name server can
99: * define arbitrary names for an IP address. Mapping from
100: * name to IP address can be trusted better (but can still be
101: * fooled if the intruder has access to the name server of
102: * the domain).
1.8 markus 103: */
1.20 markus 104: memset(&hints, 0, sizeof(hints));
105: hints.ai_family = from.ss_family;
106: hints.ai_socktype = SOCK_STREAM;
107: if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
1.36 itojun 108: logit("reverse mapping checking getaddrinfo for %.700s "
1.50 djm 109: "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
1.20 markus 110: return xstrdup(ntop);
111: }
112: /* Look for the address from the list of addresses. */
113: for (ai = aitop; ai; ai = ai->ai_next) {
114: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
115: sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
116: (strcmp(ntop, ntop2) == 0))
117: break;
118: }
119: freeaddrinfo(aitop);
120: /* If we reached the end of the list, the address was not there. */
121: if (!ai) {
122: /* Address not found for the host name. */
1.36 itojun 123: logit("Address %.100s maps to %.600s, but this does not "
1.48 stevesk 124: "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
1.20 markus 125: ntop, name);
126: return xstrdup(ntop);
127: }
128: return xstrdup(name);
129: }
1.6 markus 130:
1.20 markus 131: /*
132: * If IP options are supported, make sure there are none (log and
133: * disconnect them if any are found). Basically we are worried about
134: * source routing; it can be used to pretend you are somebody
135: * (ip-address) you are not. That itself may be "almost acceptable"
136: * under certain circumstances, but rhosts autentication is useless
137: * if source routing is accepted. Notice also that if we just dropped
138: * source routing here, the other side could use IP spoofing to do
139: * rest of the interaction and could still bypass security. So we
140: * exit here if we detect any IP options.
141: */
142: /* IPv4 only */
1.27 itojun 143: static void
1.40 avsm 144: check_ip_options(int sock, char *ipaddr)
1.20 markus 145: {
1.22 markus 146: u_char options[200];
147: char text[sizeof(options) * 3 + 1];
1.20 markus 148: socklen_t option_size;
1.44 djm 149: u_int i;
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: }
226: /* Get the address in ascii. */
1.42 djm 227: if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
228: sizeof(ntop), NULL, 0, flags)) != 0) {
229: error("get_socket_address: getnameinfo %d failed: %s", flags,
230: r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
1.19 markus 231: return NULL;
232: }
233: return xstrdup(ntop);
1.25 markus 234: }
235:
236: char *
1.40 avsm 237: get_peer_ipaddr(int sock)
1.25 markus 238: {
1.34 stevesk 239: char *p;
240:
1.40 avsm 241: if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
1.34 stevesk 242: return p;
243: return xstrdup("UNKNOWN");
1.25 markus 244: }
245:
246: char *
1.40 avsm 247: get_local_ipaddr(int sock)
1.25 markus 248: {
1.34 stevesk 249: char *p;
250:
1.40 avsm 251: if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
1.34 stevesk 252: return p;
253: return xstrdup("UNKNOWN");
1.25 markus 254: }
255:
256: char *
1.40 avsm 257: get_local_name(int sock)
1.25 markus 258: {
1.40 avsm 259: return get_socket_address(sock, 0, NI_NAMEREQD);
1.19 markus 260: }
261:
262: /*
263: * Returns the IP-address of the remote host as a string. The returned
264: * string must not be freed.
265: */
1.10 markus 266:
1.19 markus 267: const char *
1.28 itojun 268: get_remote_ipaddr(void)
1.19 markus 269: {
270: static char *canonical_host_ip = NULL;
1.1 deraadt 271:
1.19 markus 272: /* Check whether we have cached the ipaddr. */
273: if (canonical_host_ip == NULL) {
274: if (packet_connection_is_on_socket()) {
275: canonical_host_ip =
276: get_peer_ipaddr(packet_get_connection_in());
277: if (canonical_host_ip == NULL)
1.38 markus 278: cleanup_exit(255);
1.19 markus 279: } else {
280: /* If not on socket, return UNKNOWN. */
281: canonical_host_ip = xstrdup("UNKNOWN");
282: }
283: }
1.6 markus 284: return canonical_host_ip;
1.24 stevesk 285: }
286:
287: const char *
1.37 markus 288: get_remote_name_or_ip(u_int utmp_len, int use_dns)
1.24 stevesk 289: {
290: static const char *remote = "";
291: if (utmp_len > 0)
1.37 markus 292: remote = get_canonical_hostname(use_dns);
1.24 stevesk 293: if (utmp_len == 0 || strlen(remote) > utmp_len)
294: remote = get_remote_ipaddr();
295: return remote;
1.1 deraadt 296: }
297:
1.10 markus 298: /* Returns the local/remote port for the socket. */
1.1 deraadt 299:
1.27 itojun 300: static int
1.10 markus 301: get_sock_port(int sock, int local)
1.1 deraadt 302: {
1.10 markus 303: struct sockaddr_storage from;
304: socklen_t fromlen;
305: char strport[NI_MAXSERV];
1.42 djm 306: int r;
1.1 deraadt 307:
1.6 markus 308: /* Get IP address of client. */
309: fromlen = sizeof(from);
310: memset(&from, 0, sizeof(from));
1.10 markus 311: if (local) {
312: if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
313: error("getsockname failed: %.100s", strerror(errno));
314: return 0;
315: }
316: } else {
1.35 stevesk 317: if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.10 markus 318: debug("getpeername failed: %.100s", strerror(errno));
1.43 markus 319: return -1;
1.10 markus 320: }
1.6 markus 321: }
322: /* Return port number. */
1.42 djm 323: if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
324: strport, sizeof(strport), NI_NUMERICSERV)) != 0)
325: fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
326: r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
1.10 markus 327: return atoi(strport);
1.1 deraadt 328: }
329:
1.10 markus 330: /* Returns remote/local port number for the current connection. */
1.1 deraadt 331:
1.27 itojun 332: static int
1.10 markus 333: get_port(int local)
1.1 deraadt 334: {
1.8 markus 335: /*
336: * If the connection is not a socket, return 65535. This is
337: * intentionally chosen to be an unprivileged port number.
338: */
1.10 markus 339: if (!packet_connection_is_on_socket())
1.6 markus 340: return 65535;
1.1 deraadt 341:
1.10 markus 342: /* Get socket and return the port number. */
343: return get_sock_port(packet_get_connection_in(), local);
344: }
345:
1.12 markus 346: int
1.10 markus 347: get_peer_port(int sock)
348: {
349: return get_sock_port(sock, 0);
350: }
351:
1.12 markus 352: int
1.28 itojun 353: get_remote_port(void)
1.10 markus 354: {
1.41 djm 355: static int port = -1;
356:
357: /* Cache to avoid getpeername() on a dead connection */
358: if (port == -1)
359: port = get_port(0);
360:
361: return port;
1.10 markus 362: }
1.1 deraadt 363:
1.10 markus 364: int
1.28 itojun 365: get_local_port(void)
1.10 markus 366: {
367: return get_port(1);
1.1 deraadt 368: }