5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Sun Jul 2 17:52:22 1995 ylo
12 * Functions for returning the canonical host name of the remote site.
17 RCSID("$OpenBSD: canohost.c,v 1.13 2000/06/20 01:39:39 markus Exp $");
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.
29 get_remote_hostname(int socket)
31 struct sockaddr_storage from;
34 struct addrinfo hints, *ai, *aitop;
35 char name[MAXHOSTNAMELEN];
36 char ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
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));
47 if (from.ss_family == AF_INET6) {
48 struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
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;
57 memcpy(&addr, ((char *)&from6->sin6_addr) + 12, sizeof(addr));
58 port = from6->sin6_port;
60 memset(&from, 0, sizeof(from));
62 from4->sin_family = AF_INET;
63 memcpy(&from4->sin_addr, &addr, sizeof(addr));
64 from4->sin_port = port;
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");
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) {
77 name[sizeof(name) - 1] = '\0';
79 * Convert it to all lowercase (which is expected by the rest
82 for (i = 0; name[i]; i++)
84 name[i] = tolower(name[i]);
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
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);
101 goto check_ip_options;
103 /* Look for the address from the list of addresses. */
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))
111 /* If we reached the end of the list, the address was not there. */
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!",
116 strlcpy(name, ntop, sizeof name);
117 goto check_ip_options;
119 /* Address was found for the host name. We accept the host name. */
121 /* Host name not found. Use ascii representation of the address. */
122 strlcpy(name, ntop, sizeof name);
123 log("Could not reverse map address %.100s.", name);
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.
139 /* IP options -- IPv4 only */
140 if (from.ss_family == AF_INET) {
141 unsigned char options[200], *ucp;
142 char text[1024], *cp;
143 socklen_t option_size;
147 if ((ip = getprotobyname("ip")) != NULL)
148 ipproto = ip->p_proto;
150 ipproto = IPPROTO_IP;
151 option_size = sizeof(options);
152 if (getsockopt(0, ipproto, IP_OPTIONS, (char *) options,
153 &option_size) >= 0 && option_size != 0) {
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",
160 packet_disconnect("Connection from %.100s with IP options:%.800s",
165 return xstrdup(name);
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
175 get_canonical_hostname()
177 static char *canonical_host_name = NULL;
179 /* Check if we have previously retrieved this same name. */
180 if (canonical_host_name != NULL)
181 return canonical_host_name;
183 /* Get the real hostname if socket; otherwise return UNKNOWN. */
184 if (packet_connection_is_on_socket())
185 canonical_host_name = get_remote_hostname(packet_get_connection_in());
187 canonical_host_name = xstrdup("UNKNOWN");
189 return canonical_host_name;
193 * Returns the IP-address of the remote host as a string. The returned
194 * string must not be freed.
200 static char *canonical_host_ip = NULL;
201 struct sockaddr_storage from;
204 char ntop[NI_MAXHOST];
206 /* Check whether we have chached the name. */
207 if (canonical_host_ip != NULL)
208 return canonical_host_ip;
210 /* If not a socket, return UNKNOWN. */
211 if (!packet_connection_is_on_socket()) {
212 canonical_host_ip = xstrdup("UNKNOWN");
213 return canonical_host_ip;
215 /* Get client socket. */
216 socket = packet_get_connection_in();
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));
225 /* Get the IP address in ascii. */
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");
230 canonical_host_ip = xstrdup(ntop);
232 /* Return ip address string. */
233 return canonical_host_ip;
236 /* Returns the local/remote port for the socket. */
239 get_sock_port(int sock, int local)
241 struct sockaddr_storage from;
243 char strport[NI_MAXSERV];
245 /* Get IP address of client. */
246 fromlen = sizeof(from);
247 memset(&from, 0, sizeof(from));
249 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
250 error("getsockname failed: %.100s", strerror(errno));
254 if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
255 debug("getpeername failed: %.100s", strerror(errno));
259 /* Return port number. */
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);
266 /* Returns remote/local port number for the current connection. */
272 * If the connection is not a socket, return 65535. This is
273 * intentionally chosen to be an unprivileged port number.
275 if (!packet_connection_is_on_socket())
278 /* Get socket and return the port number. */
279 return get_sock_port(packet_get_connection_in(), local);
283 get_peer_port(int sock)
285 return get_sock_port(sock, 0);