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