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