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