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