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