2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Functions for returning the canonical host name of the remote site.
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
14 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
15 * Use is subject to license terms.
17 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
21 RCSID("$OpenBSD: canohost.c,v 1.34 2002/09/23 20:46:27 stevesk Exp $");
28 static const char *inet_ntop_native(int af
, const void *src
,
29 char *dst
, size_t size
);
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.
38 get_remote_hostname(int socket
, int verify_reverse_mapping
)
40 struct sockaddr_storage from
;
43 struct addrinfo hints
, *ai
, *aitop
;
44 char name
[NI_MAXHOST
], ntop
[NI_MAXHOST
], ntop2
[NI_MAXHOST
];
46 /* Get IP address of client. */
47 fromlen
= sizeof(from
);
48 memset(&from
, 0, sizeof(from
));
49 if (getpeername(socket
, (struct sockaddr
*) &from
, &fromlen
) < 0) {
50 debug("getpeername failed: %.100s", strerror(errno
));
54 if ((res
= getnameinfo((struct sockaddr
*)&from
, fromlen
, ntop
, sizeof(ntop
),
55 NULL
, 0, NI_NUMERICHOST
)) != 0)
56 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed: %d", res
);
59 if (from
.ss_family
== AF_INET6
) {
60 struct sockaddr_in6
*from6
= (struct sockaddr_in6
*)&from
;
62 (void) inet_ntop_native(from
.ss_family
,
63 from6
->sin6_addr
.s6_addr
,
66 #endif /* IPV4_IN_IPV6 */
68 if (!verify_reverse_mapping
)
71 debug3("Trying to reverse map address %.100s.", ntop
);
72 /* Map the IP address to a host name. */
73 if (getnameinfo((struct sockaddr
*)&from
, fromlen
, name
, sizeof(name
),
74 NULL
, 0, NI_NAMEREQD
) != 0) {
75 /* Host name not found. Use ip address. */
77 log("Could not reverse map address %.100s.", ntop
);
83 name
[sizeof(name
) - 1] = '\0';
85 * Convert it to all lowercase (which is expected by the rest
88 for (i
= 0; name
[i
]; i
++)
90 name
[i
] = tolower(name
[i
]);
93 * Map it back to an IP address and check that the given
94 * address actually is an address of this host. This is
95 * necessary because anyone with access to a name server can
96 * define arbitrary names for an IP address. Mapping from
97 * name to IP address can be trusted better (but can still be
98 * fooled if the intruder has access to the name server of
101 memset(&hints
, 0, sizeof(hints
));
102 hints
.ai_family
= from
.ss_family
;
103 hints
.ai_socktype
= SOCK_STREAM
;
104 if (getaddrinfo(name
, NULL
, &hints
, &aitop
) != 0) {
105 log("reverse mapping checking getaddrinfo for %.700s "
106 "failed - POSSIBLE BREAKIN ATTEMPT!", name
);
107 return xstrdup(ntop
);
109 /* Look for the address from the list of addresses. */
110 for (ai
= aitop
; ai
; ai
= ai
->ai_next
) {
111 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, ntop2
,
112 sizeof(ntop2
), NULL
, 0, NI_NUMERICHOST
) == 0 &&
113 (strcmp(ntop
, ntop2
) == 0))
117 /* If we reached the end of the list, the address was not there. */
119 /* Address not found for the host name. */
120 log("Address %.100s maps to %.600s, but this does not "
121 "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
123 return xstrdup(ntop
);
125 return xstrdup(name
);
129 * Return the canonical name of the host in the other side of the current
130 * connection. The host name is cached, so it is efficient to call this
135 get_canonical_hostname(int verify_reverse_mapping
)
137 static char *canonical_host_name
= NULL
;
138 static int verify_reverse_mapping_done
= 0;
140 /* Check if we have previously retrieved name with same option. */
141 if (canonical_host_name
!= NULL
) {
142 if (verify_reverse_mapping_done
!= verify_reverse_mapping
)
143 xfree(canonical_host_name
);
145 return canonical_host_name
;
148 /* Get the real hostname if socket; otherwise return UNKNOWN. */
149 if (packet_connection_is_on_socket())
150 canonical_host_name
= get_remote_hostname(
151 packet_get_connection_in(), verify_reverse_mapping
);
153 canonical_host_name
= xstrdup("UNKNOWN");
155 verify_reverse_mapping_done
= verify_reverse_mapping
;
156 return canonical_host_name
;
160 * Returns the remote IP-address of socket as a string. The returned
161 * string must be freed.
164 get_socket_address(int socket
, int remote
, int flags
)
166 struct sockaddr_storage addr
;
167 struct sockaddr_in6
*addr6
= (struct sockaddr_in6
*)&addr
;
169 char ntop
[NI_MAXHOST
];
171 char abuf
[INET6_ADDRSTRLEN
];
173 /* Get IP address of client. */
174 addrlen
= sizeof (addr
);
175 memset(&addr
, 0, sizeof (addr
));
178 if (getpeername(socket
, (struct sockaddr
*)&addr
, &addrlen
)
180 debug("get_socket_ipaddr: getpeername failed: %.100s",
185 if (getsockname(socket
, (struct sockaddr
*)&addr
, &addrlen
)
187 debug("get_socket_ipaddr: getsockname failed: %.100s",
193 /* Get the address in ascii. */
194 if (getnameinfo((struct sockaddr
*)&addr
, addrlen
, ntop
, sizeof (ntop
),
195 NULL
, 0, flags
) != 0) {
196 error("get_socket_ipaddr: getnameinfo %d failed", flags
);
200 if (addr
.ss_family
== AF_INET
) {
201 return (xstrdup(ntop
));
204 result
= inet_ntop_native(addr
.ss_family
,
205 addr6
->sin6_addr
.s6_addr
, abuf
, sizeof (abuf
));
207 return (xstrdup(result
));
211 get_socket_address(int socket
, int remote
, int flags
)
213 struct sockaddr_storage addr
;
215 char ntop
[NI_MAXHOST
];
217 /* Get IP address of client. */
218 addrlen
= sizeof(addr
);
219 memset(&addr
, 0, sizeof(addr
));
222 if (getpeername(socket
, (struct sockaddr
*)&addr
, &addrlen
)
226 if (getsockname(socket
, (struct sockaddr
*)&addr
, &addrlen
)
230 /* Get the address in ascii. */
231 if (getnameinfo((struct sockaddr
*)&addr
, addrlen
, ntop
, sizeof(ntop
),
232 NULL
, 0, flags
) != 0) {
233 error("get_socket_ipaddr: getnameinfo %d failed", flags
);
236 return xstrdup(ntop
);
241 get_peer_ipaddr(int socket
)
245 if ((p
= get_socket_address(socket
, 1, NI_NUMERICHOST
)) != NULL
)
247 return xstrdup("UNKNOWN");
251 get_local_ipaddr(int socket
)
255 if ((p
= get_socket_address(socket
, 0, NI_NUMERICHOST
)) != NULL
)
257 return xstrdup("UNKNOWN");
261 get_local_name(int socket
)
263 return get_socket_address(socket
, 0, NI_NAMEREQD
);
267 * Returns the IP-address of the remote host as a string. The returned
268 * string must not be freed.
272 get_remote_ipaddr(void)
274 static char *canonical_host_ip
= NULL
;
276 /* Check whether we have cached the ipaddr. */
277 if (canonical_host_ip
== NULL
) {
278 if (packet_connection_is_on_socket()) {
280 get_peer_ipaddr(packet_get_connection_in());
281 if (canonical_host_ip
== NULL
)
284 /* If not on socket, return UNKNOWN. */
285 canonical_host_ip
= xstrdup("UNKNOWN");
288 return canonical_host_ip
;
292 get_remote_name_or_ip(u_int utmp_len
, int verify_reverse_mapping
)
294 static const char *remote
= "";
296 remote
= get_canonical_hostname(verify_reverse_mapping
);
297 if (utmp_len
== 0 || strlen(remote
) > utmp_len
)
298 remote
= get_remote_ipaddr();
302 /* Returns the local/remote port for the socket. */
305 get_sock_port(int sock
, int local
)
307 struct sockaddr_storage from
;
309 char strport
[NI_MAXSERV
];
311 /* Get IP address of client. */
312 fromlen
= sizeof(from
);
313 memset(&from
, 0, sizeof(from
));
315 if (getsockname(sock
, (struct sockaddr
*)&from
, &fromlen
) < 0) {
316 error("getsockname failed: %.100s", strerror(errno
));
320 if (getpeername(sock
, (struct sockaddr
*) & from
, &fromlen
) < 0) {
321 debug("getpeername failed: %.100s", strerror(errno
));
325 /* Return port number. */
326 if (getnameinfo((struct sockaddr
*)&from
, fromlen
, NULL
, 0,
327 strport
, sizeof(strport
), NI_NUMERICSERV
) != 0)
328 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
329 return atoi(strport
);
332 /* Returns remote/local port number for the current connection. */
338 * If the connection is not a socket, return 65535. This is
339 * intentionally chosen to be an unprivileged port number.
341 if (!packet_connection_is_on_socket())
344 /* Get socket and return the port number. */
345 return get_sock_port(packet_get_connection_in(), local
);
349 get_peer_port(int sock
)
351 return get_sock_port(sock
, 0);
355 get_remote_port(void)
368 * This is a wrapper function for inet_ntop(). In case the af is AF_INET6
369 * and the address pointed by src is a IPv4-mapped IPv6 address, it
370 * returns printable IPv4 address, not IPv4-mapped IPv6 address. In other cases
371 * it behaves just like inet_ntop().
374 inet_ntop_native(int af
, const void *src
, char *dst
, size_t size
)
379 if (af
== AF_INET6
) {
380 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr
*)src
)) {
381 IN6_V4MAPPED_TO_INADDR((struct in6_addr
*)src
, &src4
);
382 result
= inet_ntop(AF_INET
, &src4
, dst
, size
);
384 result
= inet_ntop(AF_INET6
, src
, dst
, size
);
387 result
= inet_ntop(af
, src
, dst
, size
);