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