2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Functions for returning the canonical host name of the remote site.
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".
15 RCSID("$OpenBSD: canohost.c,v 1.26 2001/04/18 14:15:00 markus Exp $");
23 void check_ip_options(int socket, char *ipaddr);
26 * Return the canonical name of the host at the other end of the socket. The
27 * caller should free the returned string with xfree.
31 get_remote_hostname(int socket, int reverse_mapping_check)
33 struct sockaddr_storage from;
36 struct addrinfo hints, *ai, *aitop;
37 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
39 /* Get IP address of client. */
40 fromlen = sizeof(from);
41 memset(&from, 0, sizeof(from));
42 if (getpeername(socket, (struct sockaddr *) &from, &fromlen) < 0) {
43 debug("getpeername failed: %.100s", strerror(errno));
46 if (from.ss_family == AF_INET)
47 check_ip_options(socket, ntop);
49 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
50 NULL, 0, NI_NUMERICHOST) != 0)
51 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
53 debug3("Trying to reverse map address %.100s.", ntop);
54 /* Map the IP address to a host name. */
55 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
56 NULL, 0, NI_NAMEREQD) != 0) {
57 /* Host name not found. Use ip address. */
58 log("Could not reverse map address %.100s.", ntop);
63 name[sizeof(name) - 1] = '\0';
65 * Convert it to all lowercase (which is expected by the rest
68 for (i = 0; name[i]; i++)
70 name[i] = tolower(name[i]);
72 if (!reverse_mapping_check)
75 * Map it back to an IP address and check that the given
76 * address actually is an address of this host. This is
77 * necessary because anyone with access to a name server can
78 * define arbitrary names for an IP address. Mapping from
79 * name to IP address can be trusted better (but can still be
80 * fooled if the intruder has access to the name server of
83 memset(&hints, 0, sizeof(hints));
84 hints.ai_family = from.ss_family;
85 hints.ai_socktype = SOCK_STREAM;
86 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
87 log("reverse mapping checking getaddrinfo for %.700s "
88 "failed - POSSIBLE BREAKIN ATTEMPT!", name);
91 /* Look for the address from the list of addresses. */
92 for (ai = aitop; ai; ai = ai->ai_next) {
93 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
94 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
95 (strcmp(ntop, ntop2) == 0))
99 /* If we reached the end of the list, the address was not there. */
101 /* Address not found for the host name. */
102 log("Address %.100s maps to %.600s, but this does not "
103 "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
105 return xstrdup(ntop);
107 return xstrdup(name);
111 * If IP options are supported, make sure there are none (log and
112 * disconnect them if any are found). Basically we are worried about
113 * source routing; it can be used to pretend you are somebody
114 * (ip-address) you are not. That itself may be "almost acceptable"
115 * under certain circumstances, but rhosts autentication is useless
116 * if source routing is accepted. Notice also that if we just dropped
117 * source routing here, the other side could use IP spoofing to do
118 * rest of the interaction and could still bypass security. So we
119 * exit here if we detect any IP options.
123 check_ip_options(int socket, char *ipaddr)
126 char text[sizeof(options) * 3 + 1];
127 socklen_t option_size;
131 if ((ip = getprotobyname("ip")) != NULL)
132 ipproto = ip->p_proto;
134 ipproto = IPPROTO_IP;
135 option_size = sizeof(options);
136 if (getsockopt(socket, ipproto, IP_OPTIONS, (void *)options,
137 &option_size) >= 0 && option_size != 0) {
139 for (i = 0; i < option_size; i++)
140 snprintf(text + i*3, sizeof(text) - i*3,
141 " %2.2x", options[i]);
142 log("Connection from %.100s with IP options:%.800s",
144 packet_disconnect("Connection from %.100s with IP options:%.800s",
150 * Return the canonical name of the host in the other side of the current
151 * connection. The host name is cached, so it is efficient to call this
156 get_canonical_hostname(int reverse_mapping_check)
158 static char *canonical_host_name = NULL;
159 static int reverse_mapping_checked = 0;
161 /* Check if we have previously retrieved name with same option. */
162 if (canonical_host_name != NULL) {
163 if (reverse_mapping_checked != reverse_mapping_check)
164 xfree(canonical_host_name);
166 return canonical_host_name;
169 /* Get the real hostname if socket; otherwise return UNKNOWN. */
170 if (packet_connection_is_on_socket())
171 canonical_host_name = get_remote_hostname(
172 packet_get_connection_in(), reverse_mapping_check);
174 canonical_host_name = xstrdup("UNKNOWN");
176 reverse_mapping_checked = reverse_mapping_check;
177 return canonical_host_name;
181 * Returns the remote IP-address of socket as a string. The returned
182 * string must be freed.
185 get_socket_address(int socket, int remote, int flags)
187 struct sockaddr_storage addr;
189 char ntop[NI_MAXHOST];
191 /* Get IP address of client. */
192 addrlen = sizeof(addr);
193 memset(&addr, 0, sizeof(addr));
196 if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
198 debug("get_socket_ipaddr: getpeername failed: %.100s",
203 if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
205 debug("get_socket_ipaddr: getsockname failed: %.100s",
210 /* Get the address in ascii. */
211 if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop),
212 NULL, 0, flags) != 0) {
213 error("get_socket_ipaddr: getnameinfo %d failed", flags);
216 return xstrdup(ntop);
220 get_peer_ipaddr(int socket)
222 return get_socket_address(socket, 1, NI_NUMERICHOST);
226 get_local_ipaddr(int socket)
228 return get_socket_address(socket, 0, NI_NUMERICHOST);
232 get_local_name(int socket)
234 return get_socket_address(socket, 0, NI_NAMEREQD);
238 * Returns the IP-address of the remote host as a string. The returned
239 * string must not be freed.
245 static char *canonical_host_ip = NULL;
247 /* Check whether we have cached the ipaddr. */
248 if (canonical_host_ip == NULL) {
249 if (packet_connection_is_on_socket()) {
251 get_peer_ipaddr(packet_get_connection_in());
252 if (canonical_host_ip == NULL)
255 /* If not on socket, return UNKNOWN. */
256 canonical_host_ip = xstrdup("UNKNOWN");
259 return canonical_host_ip;
263 get_remote_name_or_ip(u_int utmp_len, int reverse_mapping_check)
265 static const char *remote = "";
267 remote = get_canonical_hostname(reverse_mapping_check);
268 if (utmp_len == 0 || strlen(remote) > utmp_len)
269 remote = get_remote_ipaddr();
273 /* Returns the local/remote port for the socket. */
276 get_sock_port(int sock, int local)
278 struct sockaddr_storage from;
280 char strport[NI_MAXSERV];
282 /* Get IP address of client. */
283 fromlen = sizeof(from);
284 memset(&from, 0, sizeof(from));
286 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
287 error("getsockname failed: %.100s", strerror(errno));
291 if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
292 debug("getpeername failed: %.100s", strerror(errno));
296 /* Return port number. */
297 if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
298 strport, sizeof(strport), NI_NUMERICSERV) != 0)
299 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
300 return atoi(strport);
303 /* Returns remote/local port number for the current connection. */
309 * If the connection is not a socket, return 65535. This is
310 * intentionally chosen to be an unprivileged port number.
312 if (!packet_connection_is_on_socket())
315 /* Get socket and return the port number. */
316 return get_sock_port(packet_get_connection_in(), local);
320 get_peer_port(int sock)
322 return get_sock_port(sock, 0);