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