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