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