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