No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / canohost.c
blob6c274013f59a59fe9788719f4c1f7170f6d5aa80
1 /* $NetBSD: canohost.c,v 1.1.1.2 2009/12/27 01:06:50 christos Exp $ */
2 /* $OpenBSD: canohost.c,v 1.65 2009/05/27 06:31:25 andreas Exp $ */
3 /*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 * Functions for returning the canonical host name of the remote site.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
16 #include "includes.h"
17 __RCSID("$NetBSD: canohost.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <netinet/in.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <time.h>
32 #include "xmalloc.h"
33 #include "packet.h"
34 #include "log.h"
35 #include "canohost.h"
36 #include "misc.h"
38 static void check_ip_options(int, char *);
39 static char *canonical_host_ip = NULL;
40 static int cached_port = -1;
43 * Return the canonical name of the host at the other end of the socket. The
44 * caller should free the returned string with xfree.
47 static char *
48 get_remote_hostname(int sock, int use_dns)
50 struct sockaddr_storage from;
51 int i;
52 socklen_t fromlen;
53 struct addrinfo hints, *ai, *aitop;
54 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
56 /* Get IP address of client. */
57 fromlen = sizeof(from);
58 memset(&from, 0, sizeof(from));
59 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
60 debug("getpeername failed: %.100s", strerror(errno));
61 cleanup_exit(255);
64 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
65 NULL, 0, NI_NUMERICHOST) != 0)
66 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
68 if (from.ss_family == AF_INET)
69 check_ip_options(sock, ntop);
71 if (!use_dns)
72 return xstrdup(ntop);
74 debug3("Trying to reverse map address %.100s.", ntop);
75 /* Map the IP address to a host name. */
76 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
77 NULL, 0, NI_NAMEREQD) != 0) {
78 /* Host name not found. Use ip address. */
79 return xstrdup(ntop);
83 * if reverse lookup result looks like a numeric hostname,
84 * someone is trying to trick us by PTR record like following:
85 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
87 memset(&hints, 0, sizeof(hints));
88 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
89 hints.ai_flags = AI_NUMERICHOST;
90 if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
91 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
92 name, ntop);
93 freeaddrinfo(ai);
94 return xstrdup(ntop);
98 * Convert it to all lowercase (which is expected by the rest
99 * of this software).
101 for (i = 0; name[i]; i++)
102 if (isupper((unsigned char)name[i]))
103 name[i] = (char)tolower((unsigned char)name[i]);
105 * Map it back to an IP address and check that the given
106 * address actually is an address of this host. This is
107 * necessary because anyone with access to a name server can
108 * define arbitrary names for an IP address. Mapping from
109 * name to IP address can be trusted better (but can still be
110 * fooled if the intruder has access to the name server of
111 * the domain).
113 memset(&hints, 0, sizeof(hints));
114 hints.ai_family = from.ss_family;
115 hints.ai_socktype = SOCK_STREAM;
116 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
117 logit("reverse mapping checking getaddrinfo for %.700s "
118 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
119 return xstrdup(ntop);
121 /* Look for the address from the list of addresses. */
122 for (ai = aitop; ai; ai = ai->ai_next) {
123 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
124 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
125 (strcmp(ntop, ntop2) == 0))
126 break;
128 freeaddrinfo(aitop);
129 /* If we reached the end of the list, the address was not there. */
130 if (!ai) {
131 /* Address not found for the host name. */
132 logit("Address %.100s maps to %.600s, but this does not "
133 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
134 ntop, name);
135 return xstrdup(ntop);
137 return xstrdup(name);
141 * If IP options are supported, make sure there are none (log and
142 * disconnect them if any are found). Basically we are worried about
143 * source routing; it can be used to pretend you are somebody
144 * (ip-address) you are not. That itself may be "almost acceptable"
145 * under certain circumstances, but rhosts autentication is useless
146 * if source routing is accepted. Notice also that if we just dropped
147 * source routing here, the other side could use IP spoofing to do
148 * rest of the interaction and could still bypass security. So we
149 * exit here if we detect any IP options.
151 /* IPv4 only */
152 static void
153 check_ip_options(int sock, char *ipaddr)
155 u_char options[200];
156 char text[sizeof(options) * 3 + 1];
157 socklen_t option_size;
158 u_int i;
159 int ipproto;
160 struct protoent *ip;
162 if ((ip = getprotobyname("ip")) != NULL)
163 ipproto = ip->p_proto;
164 else
165 ipproto = IPPROTO_IP;
166 option_size = sizeof(options);
167 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
168 &option_size) >= 0 && option_size != 0) {
169 text[0] = '\0';
170 for (i = 0; i < option_size; i++)
171 snprintf(text + i*3, sizeof(text) - i*3,
172 " %2.2x", options[i]);
173 fatal("Connection from %.100s with IP options:%.800s",
174 ipaddr, text);
179 * Return the canonical name of the host in the other side of the current
180 * connection. The host name is cached, so it is efficient to call this
181 * several times.
184 const char *
185 get_canonical_hostname(int use_dns)
187 char *host;
188 static char *canonical_host_name = NULL;
189 static char *remote_ip = NULL;
191 /* Check if we have previously retrieved name with same option. */
192 if (use_dns && canonical_host_name != NULL)
193 return canonical_host_name;
194 if (!use_dns && remote_ip != NULL)
195 return remote_ip;
197 /* Get the real hostname if socket; otherwise return UNKNOWN. */
198 if (packet_connection_is_on_socket())
199 host = get_remote_hostname(packet_get_connection_in(), use_dns);
200 else
201 host = "UNKNOWN";
203 if (use_dns)
204 canonical_host_name = host;
205 else
206 remote_ip = host;
207 return host;
211 * Returns the local/remote IP-address/hostname of socket as a string.
212 * The returned string must be freed.
214 static char *
215 get_socket_address(int sock, int remote, int flags)
217 struct sockaddr_storage addr;
218 socklen_t addrlen;
219 char ntop[NI_MAXHOST];
220 int r;
222 /* Get IP address of client. */
223 addrlen = sizeof(addr);
224 memset(&addr, 0, sizeof(addr));
226 if (remote) {
227 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
228 < 0)
229 return NULL;
230 } else {
231 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
232 < 0)
233 return NULL;
235 /* Get the address in ascii. */
236 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
237 sizeof(ntop), NULL, 0, flags)) != 0) {
238 error("get_socket_address: getnameinfo %d failed: %s", flags,
239 ssh_gai_strerror(r));
240 return NULL;
242 return xstrdup(ntop);
245 char *
246 get_peer_ipaddr(int sock)
248 char *p;
250 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
251 return p;
252 return xstrdup("UNKNOWN");
255 char *
256 get_local_ipaddr(int sock)
258 char *p;
260 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
261 return p;
262 return xstrdup("UNKNOWN");
265 char *
266 get_local_name(int sock)
268 return get_socket_address(sock, 0, NI_NAMEREQD);
271 void
272 clear_cached_addr(void)
274 if (canonical_host_ip != NULL) {
275 xfree(canonical_host_ip);
276 canonical_host_ip = NULL;
278 cached_port = -1;
282 * Returns the IP-address of the remote host as a string. The returned
283 * string must not be freed.
286 const char *
287 get_remote_ipaddr(void)
289 /* Check whether we have cached the ipaddr. */
290 if (canonical_host_ip == NULL) {
291 if (packet_connection_is_on_socket()) {
292 canonical_host_ip =
293 get_peer_ipaddr(packet_get_connection_in());
294 if (canonical_host_ip == NULL)
295 cleanup_exit(255);
296 } else {
297 /* If not on socket, return UNKNOWN. */
298 canonical_host_ip = xstrdup("UNKNOWN");
301 return canonical_host_ip;
304 const char *
305 get_remote_name_or_ip(u_int utmp_len, int use_dns)
307 static const char *remote = "";
308 if (utmp_len > 0)
309 remote = get_canonical_hostname(use_dns);
310 if (utmp_len == 0 || strlen(remote) > utmp_len)
311 remote = get_remote_ipaddr();
312 return remote;
315 /* Returns the local/remote port for the socket. */
318 get_sock_port(int sock, int local)
320 struct sockaddr_storage from;
321 socklen_t fromlen;
322 char strport[NI_MAXSERV];
323 int r;
325 /* Get IP address of client. */
326 fromlen = sizeof(from);
327 memset(&from, 0, sizeof(from));
328 if (local) {
329 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
330 error("getsockname failed: %.100s", strerror(errno));
331 return 0;
333 } else {
334 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
335 debug("getpeername failed: %.100s", strerror(errno));
336 return -1;
339 /* Return port number. */
340 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
341 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
342 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
343 ssh_gai_strerror(r));
344 return atoi(strport);
347 /* Returns remote/local port number for the current connection. */
349 static int
350 get_port(int local)
353 * If the connection is not a socket, return 65535. This is
354 * intentionally chosen to be an unprivileged port number.
356 if (!packet_connection_is_on_socket())
357 return 65535;
359 /* Get socket and return the port number. */
360 return get_sock_port(packet_get_connection_in(), local);
364 get_peer_port(int sock)
366 return get_sock_port(sock, 0);
370 get_remote_port(void)
372 /* Cache to avoid getpeername() on a dead connection */
373 if (cached_port == -1)
374 cached_port = get_port(0);
376 return cached_port;
380 get_local_port(void)
382 return get_port(1);