Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * canohost.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Sun Jul 2 17:52:22 1995 ylo |
| 11 | * |
| 12 | * Functions for returning the canonical host name of the remote site. |
| 13 | * |
| 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 17 | RCSID("$Id: canohost.c,v 1.8 2000/03/11 09:45:41 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
| 19 | #include "packet.h" |
| 20 | #include "xmalloc.h" |
| 21 | #include "ssh.h" |
| 22 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 23 | /* |
| 24 | * Return the canonical name of the host at the other end of the socket. The |
| 25 | * caller should free the returned string with xfree. |
| 26 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 28 | char * |
| 29 | get_remote_hostname(int socket) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 31 | struct sockaddr_storage from; |
| 32 | int i; |
| 33 | socklen_t fromlen; |
| 34 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | char name[MAXHOSTNAMELEN]; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 36 | char ntop[NI_MAXHOST], ntop2[NI_MAXHOST]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | /* Get IP address of client. */ |
| 39 | fromlen = sizeof(from); |
| 40 | memset(&from, 0, sizeof(from)); |
| 41 | if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) { |
| 42 | debug("getpeername failed: %.100s", strerror(errno)); |
| 43 | fatal_cleanup(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | } |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 45 | |
| 46 | #ifdef IPV4_IN_IPV6 |
| 47 | if (from.ss_family == AF_INET6) { |
| 48 | struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from; |
| 49 | |
| 50 | /* Detect IPv4 in IPv6 mapped address and convert it to */ |
| 51 | /* plain (AF_INET) IPv4 address */ |
| 52 | if (IN6_IS_ADDR_V4MAPPED(&from6->sin6_addr)) { |
| 53 | struct sockaddr_in *from4 = (struct sockaddr_in *)&from; |
| 54 | struct in_addr addr; |
| 55 | u_int16_t port; |
| 56 | |
| 57 | memcpy(&addr, ((char *)&from6->sin6_addr) + 12, sizeof(addr)); |
| 58 | port = from6->sin6_port; |
| 59 | |
| 60 | memset(&from, 0, sizeof(from)); |
| 61 | |
| 62 | from4->sin_family = AF_INET; |
| 63 | memcpy(&from4->sin_addr, &addr, sizeof(addr)); |
| 64 | from4->sin_port = port; |
| 65 | } |
| 66 | } |
| 67 | #endif |
| 68 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 69 | if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
| 70 | NULL, 0, NI_NUMERICHOST) != 0) |
| 71 | fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 73 | /* Map the IP address to a host name. */ |
| 74 | if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), |
| 75 | NULL, 0, NI_NAMEREQD) == 0) { |
| 76 | /* Got host name. */ |
| 77 | name[sizeof(name) - 1] = '\0'; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 78 | /* |
| 79 | * Convert it to all lowercase (which is expected by the rest |
| 80 | * of this software). |
| 81 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | for (i = 0; name[i]; i++) |
| 83 | if (isupper(name[i])) |
| 84 | name[i] = tolower(name[i]); |
| 85 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 86 | /* |
| 87 | * Map it back to an IP address and check that the given |
| 88 | * address actually is an address of this host. This is |
| 89 | * necessary because anyone with access to a name server can |
| 90 | * define arbitrary names for an IP address. Mapping from |
| 91 | * name to IP address can be trusted better (but can still be |
| 92 | * fooled if the intruder has access to the name server of |
| 93 | * the domain). |
| 94 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 95 | memset(&hints, 0, sizeof(hints)); |
| 96 | hints.ai_family = from.ss_family; |
| 97 | hints.ai_socktype = SOCK_STREAM; |
| 98 | if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { |
| 99 | log("reverse mapping checking getaddrinfo for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name); |
| 100 | strlcpy(name, ntop, sizeof name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | goto check_ip_options; |
| 102 | } |
| 103 | /* Look for the address from the list of addresses. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 104 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 105 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2, |
| 106 | sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 && |
| 107 | (strcmp(ntop, ntop2) == 0)) |
| 108 | break; |
| 109 | } |
| 110 | freeaddrinfo(aitop); |
| 111 | /* If we reached the end of the list, the address was not there. */ |
| 112 | if (!ai) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | /* Address not found for the host name. */ |
| 114 | log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 115 | ntop, name); |
| 116 | strlcpy(name, ntop, sizeof name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | goto check_ip_options; |
| 118 | } |
| 119 | /* Address was found for the host name. We accept the host name. */ |
| 120 | } else { |
| 121 | /* Host name not found. Use ascii representation of the address. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 122 | strlcpy(name, ntop, sizeof name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | log("Could not reverse map address %.100s.", name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | check_ip_options: |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 128 | /* |
| 129 | * If IP options are supported, make sure there are none (log and |
| 130 | * disconnect them if any are found). Basically we are worried about |
| 131 | * source routing; it can be used to pretend you are somebody |
| 132 | * (ip-address) you are not. That itself may be "almost acceptable" |
| 133 | * under certain circumstances, but rhosts autentication is useless |
| 134 | * if source routing is accepted. Notice also that if we just dropped |
| 135 | * source routing here, the other side could use IP spoofing to do |
| 136 | * rest of the interaction and could still bypass security. So we |
| 137 | * exit here if we detect any IP options. |
| 138 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 139 | /* IP options -- IPv4 only */ |
| 140 | if (from.ss_family == AF_INET) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | unsigned char options[200], *ucp; |
| 142 | char text[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 143 | socklen_t option_size; |
| 144 | int ipproto; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | struct protoent *ip; |
| 146 | |
| 147 | if ((ip = getprotobyname("ip")) != NULL) |
| 148 | ipproto = ip->p_proto; |
| 149 | else |
| 150 | ipproto = IPPROTO_IP; |
| 151 | option_size = sizeof(options); |
| 152 | if (getsockopt(0, ipproto, IP_OPTIONS, (char *) options, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 153 | &option_size) >= 0 && option_size != 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | cp = text; |
| 155 | /* Note: "text" buffer must be at least 3x as big as options. */ |
| 156 | for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3) |
| 157 | sprintf(cp, " %2.2x", *ucp); |
| 158 | log("Connection from %.100s with IP options:%.800s", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 159 | ntop, text); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | packet_disconnect("Connection from %.100s with IP options:%.800s", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 161 | ntop, text); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | return xstrdup(name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | } |
| 167 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 168 | /* |
| 169 | * Return the canonical name of the host in the other side of the current |
| 170 | * connection. The host name is cached, so it is efficient to call this |
| 171 | * several times. |
| 172 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 173 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | const char * |
| 175 | get_canonical_hostname() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 176 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 177 | static char *canonical_host_name = NULL; |
| 178 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | /* Check if we have previously retrieved this same name. */ |
| 180 | if (canonical_host_name != NULL) |
| 181 | return canonical_host_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 182 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 183 | /* Get the real hostname if socket; otherwise return UNKNOWN. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 184 | if (packet_connection_is_on_socket()) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | canonical_host_name = get_remote_hostname(packet_get_connection_in()); |
| 186 | else |
| 187 | canonical_host_name = xstrdup("UNKNOWN"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 188 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | return canonical_host_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | } |
| 191 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 192 | /* |
| 193 | * Returns the IP-address of the remote host as a string. The returned |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 194 | * string must not be freed. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 195 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 196 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 197 | const char * |
| 198 | get_remote_ipaddr() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 200 | static char *canonical_host_ip = NULL; |
| 201 | struct sockaddr_storage from; |
| 202 | socklen_t fromlen; |
| 203 | int socket; |
| 204 | char ntop[NI_MAXHOST]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 205 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 206 | /* Check whether we have chached the name. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 207 | if (canonical_host_ip != NULL) |
| 208 | return canonical_host_ip; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 209 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | /* If not a socket, return UNKNOWN. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 211 | if (!packet_connection_is_on_socket()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 212 | canonical_host_ip = xstrdup("UNKNOWN"); |
| 213 | return canonical_host_ip; |
| 214 | } |
| 215 | /* Get client socket. */ |
| 216 | socket = packet_get_connection_in(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 217 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | /* Get IP address of client. */ |
| 219 | fromlen = sizeof(from); |
| 220 | memset(&from, 0, sizeof(from)); |
| 221 | if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) { |
| 222 | debug("getpeername failed: %.100s", strerror(errno)); |
| 223 | fatal_cleanup(); |
| 224 | } |
| 225 | /* Get the IP address in ascii. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 226 | if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
| 227 | NULL, 0, NI_NUMERICHOST) != 0) |
| 228 | fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); |
| 229 | |
| 230 | canonical_host_ip = xstrdup(ntop); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 231 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 232 | /* Return ip address string. */ |
| 233 | return canonical_host_ip; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 234 | } |
| 235 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 236 | /* Returns the local/remote port for the socket. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 237 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 238 | int |
| 239 | get_sock_port(int sock, int local) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 240 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 241 | struct sockaddr_storage from; |
| 242 | socklen_t fromlen; |
| 243 | char strport[NI_MAXSERV]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 245 | /* Get IP address of client. */ |
| 246 | fromlen = sizeof(from); |
| 247 | memset(&from, 0, sizeof(from)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 248 | if (local) { |
| 249 | if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) { |
| 250 | error("getsockname failed: %.100s", strerror(errno)); |
| 251 | return 0; |
| 252 | } |
| 253 | } else { |
| 254 | if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) { |
| 255 | debug("getpeername failed: %.100s", strerror(errno)); |
| 256 | fatal_cleanup(); |
| 257 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 258 | } |
| 259 | /* Return port number. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 260 | if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0, |
| 261 | strport, sizeof(strport), NI_NUMERICSERV) != 0) |
| 262 | fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed"); |
| 263 | return atoi(strport); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 264 | } |
| 265 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 266 | /* Returns remote/local port number for the current connection. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 268 | int |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 269 | get_port(int local) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 270 | { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 271 | /* |
| 272 | * If the connection is not a socket, return 65535. This is |
| 273 | * intentionally chosen to be an unprivileged port number. |
| 274 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 275 | if (!packet_connection_is_on_socket()) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 276 | return 65535; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 277 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 278 | /* Get socket and return the port number. */ |
| 279 | return get_sock_port(packet_get_connection_in(), local); |
| 280 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 281 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 282 | int |
| 283 | get_peer_port(int sock) |
| 284 | { |
| 285 | return get_sock_port(sock, 0); |
| 286 | } |
| 287 | |
| 288 | int |
| 289 | get_remote_port() |
| 290 | { |
| 291 | return get_port(0); |
| 292 | } |
| 293 | |
| 294 | int |
| 295 | get_local_port() |
| 296 | { |
| 297 | return get_port(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 298 | } |