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