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