xref: /titanic_44/usr/src/cmd/ssh/libssh/common/canohost.c (revision 8b1606b5d15e5e037182f32e0306c43206772082)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate  *                    All rights reserved
57c478bd9Sstevel@tonic-gate  * Functions for returning the canonical host name of the remote site.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
87c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
97c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
107c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
117c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate /*
147c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
157c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
16*8b1606b5SVitaliy Gusev  *
17*8b1606b5SVitaliy Gusev  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "includes.h"
217c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: canohost.c,v 1.34 2002/09/23 20:46:27 stevesk Exp $");
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include "packet.h"
247c478bd9Sstevel@tonic-gate #include "xmalloc.h"
257c478bd9Sstevel@tonic-gate #include "log.h"
267c478bd9Sstevel@tonic-gate #include "canohost.h"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate static const char *inet_ntop_native(int af, const void *src,
297c478bd9Sstevel@tonic-gate 	char *dst, size_t size);
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Return the canonical name of the host at the other end of the socket. The
347c478bd9Sstevel@tonic-gate  * caller should free the returned string with xfree.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static char *
get_remote_hostname(int socket,int verify_reverse_mapping)387c478bd9Sstevel@tonic-gate get_remote_hostname(int socket, int verify_reverse_mapping)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
417c478bd9Sstevel@tonic-gate 	int i, res;
427c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
437c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
447c478bd9Sstevel@tonic-gate 	char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	/* Get IP address of client. */
477c478bd9Sstevel@tonic-gate 	fromlen = sizeof(from);
487c478bd9Sstevel@tonic-gate 	memset(&from, 0, sizeof(from));
497c478bd9Sstevel@tonic-gate 	if (getpeername(socket, (struct sockaddr *) &from, &fromlen) < 0) {
507c478bd9Sstevel@tonic-gate 		debug("getpeername failed: %.100s", strerror(errno));
517c478bd9Sstevel@tonic-gate 		fatal_cleanup();
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if ((res = getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
557c478bd9Sstevel@tonic-gate 	    NULL, 0, NI_NUMERICHOST)) != 0)
567c478bd9Sstevel@tonic-gate 		fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed: %d", res);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef IPV4_IN_IPV6
597c478bd9Sstevel@tonic-gate 	if (from.ss_family == AF_INET6) {
607c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 		(void) inet_ntop_native(from.ss_family,
637c478bd9Sstevel@tonic-gate 				from6->sin6_addr.s6_addr,
647c478bd9Sstevel@tonic-gate 				ntop, sizeof(ntop));
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate #endif /* IPV4_IN_IPV6 */
677c478bd9Sstevel@tonic-gate 
68*8b1606b5SVitaliy Gusev 	if (!verify_reverse_mapping)
69*8b1606b5SVitaliy Gusev 		return xstrdup(ntop);
70*8b1606b5SVitaliy Gusev 
717c478bd9Sstevel@tonic-gate 	debug3("Trying to reverse map address %.100s.", ntop);
727c478bd9Sstevel@tonic-gate 	/* Map the IP address to a host name. */
737c478bd9Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
747c478bd9Sstevel@tonic-gate 	    NULL, 0, NI_NAMEREQD) != 0) {
757c478bd9Sstevel@tonic-gate 		/* Host name not found.  Use ip address. */
767c478bd9Sstevel@tonic-gate #if 0
777c478bd9Sstevel@tonic-gate 		log("Could not reverse map address %.100s.", ntop);
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 		return xstrdup(ntop);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* Got host name. */
837c478bd9Sstevel@tonic-gate 	name[sizeof(name) - 1] = '\0';
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * Convert it to all lowercase (which is expected by the rest
867c478bd9Sstevel@tonic-gate 	 * of this software).
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	for (i = 0; name[i]; i++)
897c478bd9Sstevel@tonic-gate 		if (isupper(name[i]))
907c478bd9Sstevel@tonic-gate 			name[i] = tolower(name[i]);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Map it back to an IP address and check that the given
947c478bd9Sstevel@tonic-gate 	 * address actually is an address of this host.  This is
957c478bd9Sstevel@tonic-gate 	 * necessary because anyone with access to a name server can
967c478bd9Sstevel@tonic-gate 	 * define arbitrary names for an IP address. Mapping from
977c478bd9Sstevel@tonic-gate 	 * name to IP address can be trusted better (but can still be
987c478bd9Sstevel@tonic-gate 	 * fooled if the intruder has access to the name server of
997c478bd9Sstevel@tonic-gate 	 * the domain).
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
1027c478bd9Sstevel@tonic-gate 	hints.ai_family = from.ss_family;
1037c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
1047c478bd9Sstevel@tonic-gate 	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
1057c478bd9Sstevel@tonic-gate 		log("reverse mapping checking getaddrinfo for %.700s "
1067c478bd9Sstevel@tonic-gate 		    "failed - POSSIBLE BREAKIN ATTEMPT!", name);
1077c478bd9Sstevel@tonic-gate 		return xstrdup(ntop);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	/* Look for the address from the list of addresses. */
1107c478bd9Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
1117c478bd9Sstevel@tonic-gate 		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
1127c478bd9Sstevel@tonic-gate 		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
1137c478bd9Sstevel@tonic-gate 		    (strcmp(ntop, ntop2) == 0))
1147c478bd9Sstevel@tonic-gate 				break;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	freeaddrinfo(aitop);
1177c478bd9Sstevel@tonic-gate 	/* If we reached the end of the list, the address was not there. */
1187c478bd9Sstevel@tonic-gate 	if (!ai) {
1197c478bd9Sstevel@tonic-gate 		/* Address not found for the host name. */
1207c478bd9Sstevel@tonic-gate 		log("Address %.100s maps to %.600s, but this does not "
1217c478bd9Sstevel@tonic-gate 		    "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
1227c478bd9Sstevel@tonic-gate 		    ntop, name);
1237c478bd9Sstevel@tonic-gate 		return xstrdup(ntop);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return xstrdup(name);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Return the canonical name of the host in the other side of the current
1307c478bd9Sstevel@tonic-gate  * connection.  The host name is cached, so it is efficient to call this
1317c478bd9Sstevel@tonic-gate  * several times.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate const char *
get_canonical_hostname(int verify_reverse_mapping)1357c478bd9Sstevel@tonic-gate get_canonical_hostname(int verify_reverse_mapping)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	static char *canonical_host_name = NULL;
1387c478bd9Sstevel@tonic-gate 	static int verify_reverse_mapping_done = 0;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* Check if we have previously retrieved name with same option. */
1417c478bd9Sstevel@tonic-gate 	if (canonical_host_name != NULL) {
1427c478bd9Sstevel@tonic-gate 		if (verify_reverse_mapping_done != verify_reverse_mapping)
1437c478bd9Sstevel@tonic-gate 			xfree(canonical_host_name);
1447c478bd9Sstevel@tonic-gate 		else
1457c478bd9Sstevel@tonic-gate 			return canonical_host_name;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* Get the real hostname if socket; otherwise return UNKNOWN. */
1497c478bd9Sstevel@tonic-gate 	if (packet_connection_is_on_socket())
1507c478bd9Sstevel@tonic-gate 		canonical_host_name = get_remote_hostname(
1517c478bd9Sstevel@tonic-gate 		    packet_get_connection_in(), verify_reverse_mapping);
1527c478bd9Sstevel@tonic-gate 	else
1537c478bd9Sstevel@tonic-gate 		canonical_host_name = xstrdup("UNKNOWN");
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	verify_reverse_mapping_done = verify_reverse_mapping;
1567c478bd9Sstevel@tonic-gate 	return canonical_host_name;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Returns the remote IP-address of socket as a string.  The returned
1617c478bd9Sstevel@tonic-gate  * string must be freed.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate char *
get_socket_address(int socket,int remote,int flags)1647c478bd9Sstevel@tonic-gate get_socket_address(int socket, int remote, int flags)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	struct sockaddr_storage addr;
1677c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
1687c478bd9Sstevel@tonic-gate 	socklen_t addrlen;
1697c478bd9Sstevel@tonic-gate 	char ntop[NI_MAXHOST];
1707c478bd9Sstevel@tonic-gate 	const char *result;
1717c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* Get IP address of client. */
1747c478bd9Sstevel@tonic-gate 	addrlen = sizeof (addr);
1757c478bd9Sstevel@tonic-gate 	memset(&addr, 0, sizeof (addr));
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (remote) {
1787c478bd9Sstevel@tonic-gate 		if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
1797c478bd9Sstevel@tonic-gate 		    < 0) {
1807c478bd9Sstevel@tonic-gate 			debug("get_socket_ipaddr: getpeername failed: %.100s",
1817c478bd9Sstevel@tonic-gate 			    strerror(errno));
1827c478bd9Sstevel@tonic-gate 			return (NULL);
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	} else {
1857c478bd9Sstevel@tonic-gate 		if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
1867c478bd9Sstevel@tonic-gate 		    < 0) {
1877c478bd9Sstevel@tonic-gate 			debug("get_socket_ipaddr: getsockname failed: %.100s",
1887c478bd9Sstevel@tonic-gate 			    strerror(errno));
1897c478bd9Sstevel@tonic-gate 			return (NULL);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* Get the address in ascii. */
1947c478bd9Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof (ntop),
1957c478bd9Sstevel@tonic-gate 	    NULL, 0, flags) != 0) {
1967c478bd9Sstevel@tonic-gate 		error("get_socket_ipaddr: getnameinfo %d failed", flags);
1977c478bd9Sstevel@tonic-gate 		return (NULL);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (addr.ss_family == AF_INET) {
2017c478bd9Sstevel@tonic-gate 		return (xstrdup(ntop));
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	result = inet_ntop_native(addr.ss_family,
2057c478bd9Sstevel@tonic-gate 	    addr6->sin6_addr.s6_addr, abuf, sizeof (abuf));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (xstrdup(result));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate #if 0
2107c478bd9Sstevel@tonic-gate static char *
2117c478bd9Sstevel@tonic-gate get_socket_address(int socket, int remote, int flags)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	struct sockaddr_storage addr;
2147c478bd9Sstevel@tonic-gate 	socklen_t addrlen;
2157c478bd9Sstevel@tonic-gate 	char ntop[NI_MAXHOST];
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* Get IP address of client. */
2187c478bd9Sstevel@tonic-gate 	addrlen = sizeof(addr);
2197c478bd9Sstevel@tonic-gate 	memset(&addr, 0, sizeof(addr));
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (remote) {
2227c478bd9Sstevel@tonic-gate 		if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
2237c478bd9Sstevel@tonic-gate 		    < 0)
2247c478bd9Sstevel@tonic-gate 			return NULL;
2257c478bd9Sstevel@tonic-gate 	} else {
2267c478bd9Sstevel@tonic-gate 		if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
2277c478bd9Sstevel@tonic-gate 		    < 0)
2287c478bd9Sstevel@tonic-gate 			return NULL;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	/* Get the address in ascii. */
2317c478bd9Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop),
2327c478bd9Sstevel@tonic-gate 	    NULL, 0, flags) != 0) {
2337c478bd9Sstevel@tonic-gate 		error("get_socket_ipaddr: getnameinfo %d failed", flags);
2347c478bd9Sstevel@tonic-gate 		return NULL;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return xstrdup(ntop);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate #endif
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate char *
get_peer_ipaddr(int socket)2417c478bd9Sstevel@tonic-gate get_peer_ipaddr(int socket)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	char *p;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((p = get_socket_address(socket, 1, NI_NUMERICHOST)) != NULL)
2467c478bd9Sstevel@tonic-gate 		return p;
2477c478bd9Sstevel@tonic-gate 	return xstrdup("UNKNOWN");
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate char *
get_local_ipaddr(int socket)2517c478bd9Sstevel@tonic-gate get_local_ipaddr(int socket)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	char *p;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if ((p = get_socket_address(socket, 0, NI_NUMERICHOST)) != NULL)
2567c478bd9Sstevel@tonic-gate 		return p;
2577c478bd9Sstevel@tonic-gate 	return xstrdup("UNKNOWN");
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate char *
get_local_name(int socket)2617c478bd9Sstevel@tonic-gate get_local_name(int socket)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	return get_socket_address(socket, 0, NI_NAMEREQD);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Returns the IP-address of the remote host as a string.  The returned
2687c478bd9Sstevel@tonic-gate  * string must not be freed.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate const char *
get_remote_ipaddr(void)2727c478bd9Sstevel@tonic-gate get_remote_ipaddr(void)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	static char *canonical_host_ip = NULL;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* Check whether we have cached the ipaddr. */
2777c478bd9Sstevel@tonic-gate 	if (canonical_host_ip == NULL) {
2787c478bd9Sstevel@tonic-gate 		if (packet_connection_is_on_socket()) {
2797c478bd9Sstevel@tonic-gate 			canonical_host_ip =
2807c478bd9Sstevel@tonic-gate 			    get_peer_ipaddr(packet_get_connection_in());
2817c478bd9Sstevel@tonic-gate 			if (canonical_host_ip == NULL)
2827c478bd9Sstevel@tonic-gate 				fatal_cleanup();
2837c478bd9Sstevel@tonic-gate 		} else {
2847c478bd9Sstevel@tonic-gate 			/* If not on socket, return UNKNOWN. */
2857c478bd9Sstevel@tonic-gate 			canonical_host_ip = xstrdup("UNKNOWN");
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	return canonical_host_ip;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate const char *
get_remote_name_or_ip(u_int utmp_len,int verify_reverse_mapping)2927c478bd9Sstevel@tonic-gate get_remote_name_or_ip(u_int utmp_len, int verify_reverse_mapping)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	static const char *remote = "";
2957c478bd9Sstevel@tonic-gate 	if (utmp_len > 0)
2967c478bd9Sstevel@tonic-gate 		remote = get_canonical_hostname(verify_reverse_mapping);
2977c478bd9Sstevel@tonic-gate 	if (utmp_len == 0 || strlen(remote) > utmp_len)
2987c478bd9Sstevel@tonic-gate 		remote = get_remote_ipaddr();
2997c478bd9Sstevel@tonic-gate 	return remote;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /* Returns the local/remote port for the socket. */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static int
get_sock_port(int sock,int local)3057c478bd9Sstevel@tonic-gate get_sock_port(int sock, int local)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
3087c478bd9Sstevel@tonic-gate 	socklen_t fromlen;
3097c478bd9Sstevel@tonic-gate 	char strport[NI_MAXSERV];
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* Get IP address of client. */
3127c478bd9Sstevel@tonic-gate 	fromlen = sizeof(from);
3137c478bd9Sstevel@tonic-gate 	memset(&from, 0, sizeof(from));
3147c478bd9Sstevel@tonic-gate 	if (local) {
3157c478bd9Sstevel@tonic-gate 		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
3167c478bd9Sstevel@tonic-gate 			error("getsockname failed: %.100s", strerror(errno));
3177c478bd9Sstevel@tonic-gate 			return 0;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	} else {
3207c478bd9Sstevel@tonic-gate 		if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
3217c478bd9Sstevel@tonic-gate 			debug("getpeername failed: %.100s", strerror(errno));
3227c478bd9Sstevel@tonic-gate 			fatal_cleanup();
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	/* Return port number. */
3267c478bd9Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
3277c478bd9Sstevel@tonic-gate 	    strport, sizeof(strport), NI_NUMERICSERV) != 0)
3287c478bd9Sstevel@tonic-gate 		fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
3297c478bd9Sstevel@tonic-gate 	return atoi(strport);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /* Returns remote/local port number for the current connection. */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate static int
get_port(int local)3357c478bd9Sstevel@tonic-gate get_port(int local)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * If the connection is not a socket, return 65535.  This is
3397c478bd9Sstevel@tonic-gate 	 * intentionally chosen to be an unprivileged port number.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (!packet_connection_is_on_socket())
3427c478bd9Sstevel@tonic-gate 		return 65535;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* Get socket and return the port number. */
3457c478bd9Sstevel@tonic-gate 	return get_sock_port(packet_get_connection_in(), local);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate int
get_peer_port(int sock)3497c478bd9Sstevel@tonic-gate get_peer_port(int sock)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	return get_sock_port(sock, 0);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate int
get_remote_port(void)3557c478bd9Sstevel@tonic-gate get_remote_port(void)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	return get_port(0);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate int
get_local_port(void)3617c478bd9Sstevel@tonic-gate get_local_port(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	return get_port(1);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  * Taken from inetd.c
3687c478bd9Sstevel@tonic-gate  * This is a wrapper function for inet_ntop(). In case the af is AF_INET6
3697c478bd9Sstevel@tonic-gate  * and the address pointed by src is a IPv4-mapped IPv6 address, it
3707c478bd9Sstevel@tonic-gate  * returns printable IPv4 address, not IPv4-mapped IPv6 address. In other cases
3717c478bd9Sstevel@tonic-gate  * it behaves just like inet_ntop().
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate static const char *
inet_ntop_native(int af,const void * src,char * dst,size_t size)3747c478bd9Sstevel@tonic-gate inet_ntop_native(int af, const void *src, char *dst, size_t size)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	struct in_addr src4;
3777c478bd9Sstevel@tonic-gate 	const char *result;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
3807c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)) {
3817c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR((struct in6_addr *)src, &src4);
3827c478bd9Sstevel@tonic-gate 			result = inet_ntop(AF_INET, &src4, dst, size);
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			result = inet_ntop(AF_INET6, src, dst, size);
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	} else {
3877c478bd9Sstevel@tonic-gate 		result = inet_ntop(af, src, dst, size);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (result);
3917c478bd9Sstevel@tonic-gate }
392