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 $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
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>
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.
48 get_remote_hostname(int sock
, int use_dns
)
50 struct sockaddr_storage from
;
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
));
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
);
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. */
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",
98 * Convert it to all lowercase (which is expected by the rest
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
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))
129 /* If we reached the end of the list, the address was not there. */
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!",
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.
153 check_ip_options(int sock
, char *ipaddr
)
156 char text
[sizeof(options
) * 3 + 1];
157 socklen_t option_size
;
162 if ((ip
= getprotobyname("ip")) != NULL
)
163 ipproto
= ip
->p_proto
;
165 ipproto
= IPPROTO_IP
;
166 option_size
= sizeof(options
);
167 if (getsockopt(sock
, ipproto
, IP_OPTIONS
, options
,
168 &option_size
) >= 0 && option_size
!= 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",
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
185 get_canonical_hostname(int use_dns
)
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
)
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
);
204 canonical_host_name
= host
;
211 * Returns the local/remote IP-address/hostname of socket as a string.
212 * The returned string must be freed.
215 get_socket_address(int sock
, int remote
, int flags
)
217 struct sockaddr_storage addr
;
219 char ntop
[NI_MAXHOST
];
222 /* Get IP address of client. */
223 addrlen
= sizeof(addr
);
224 memset(&addr
, 0, sizeof(addr
));
227 if (getpeername(sock
, (struct sockaddr
*)&addr
, &addrlen
)
231 if (getsockname(sock
, (struct sockaddr
*)&addr
, &addrlen
)
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
));
242 return xstrdup(ntop
);
246 get_peer_ipaddr(int sock
)
250 if ((p
= get_socket_address(sock
, 1, NI_NUMERICHOST
)) != NULL
)
252 return xstrdup("UNKNOWN");
256 get_local_ipaddr(int sock
)
260 if ((p
= get_socket_address(sock
, 0, NI_NUMERICHOST
)) != NULL
)
262 return xstrdup("UNKNOWN");
266 get_local_name(int sock
)
268 return get_socket_address(sock
, 0, NI_NAMEREQD
);
272 clear_cached_addr(void)
274 if (canonical_host_ip
!= NULL
) {
275 xfree(canonical_host_ip
);
276 canonical_host_ip
= NULL
;
282 * Returns the IP-address of the remote host as a string. The returned
283 * string must not be freed.
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()) {
293 get_peer_ipaddr(packet_get_connection_in());
294 if (canonical_host_ip
== NULL
)
297 /* If not on socket, return UNKNOWN. */
298 canonical_host_ip
= xstrdup("UNKNOWN");
301 return canonical_host_ip
;
305 get_remote_name_or_ip(u_int utmp_len
, int use_dns
)
307 static const char *remote
= "";
309 remote
= get_canonical_hostname(use_dns
);
310 if (utmp_len
== 0 || strlen(remote
) > utmp_len
)
311 remote
= get_remote_ipaddr();
315 /* Returns the local/remote port for the socket. */
318 get_sock_port(int sock
, int local
)
320 struct sockaddr_storage from
;
322 char strport
[NI_MAXSERV
];
325 /* Get IP address of client. */
326 fromlen
= sizeof(from
);
327 memset(&from
, 0, sizeof(from
));
329 if (getsockname(sock
, (struct sockaddr
*)&from
, &fromlen
) < 0) {
330 error("getsockname failed: %.100s", strerror(errno
));
334 if (getpeername(sock
, (struct sockaddr
*)&from
, &fromlen
) < 0) {
335 debug("getpeername failed: %.100s", strerror(errno
));
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. */
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())
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);