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