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