1/*	$NetBSD: canohost.c,v 1.16 2023/10/25 20:19:57 christos Exp $	*/
2/* $OpenBSD: canohost.c,v 1.77 2023/03/31 04:42:29 dtucker Exp $ */
3
4/*
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 *                    All rights reserved
8 * Functions for returning the canonical host name of the remote site.
9 *
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose.  Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 */
16
17#include "includes.h"
18__RCSID("$NetBSD: canohost.c,v 1.16 2023/10/25 20:19:57 christos Exp $");
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <sys/un.h>
22
23#include <netinet/in.h>
24
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 <time.h>
32#include <unistd.h>
33
34#include "xmalloc.h"
35#include "packet.h"
36#include "log.h"
37#include "canohost.h"
38#include "misc.h"
39
40/*
41 * Returns the local/remote IP-address/hostname of socket as a string.
42 * The returned string must be freed.
43 */
44static char *
45get_socket_address(int sock, int remote, int flags)
46{
47	struct sockaddr_storage addr;
48	socklen_t addrlen;
49	char ntop[NI_MAXHOST];
50	int r;
51
52	if (sock < 0)
53		return NULL;
54
55	/* Get IP address of client. */
56	addrlen = sizeof(addr);
57	memset(&addr, 0, sizeof(addr));
58
59	if (remote) {
60		if (getpeername(sock, (struct sockaddr *)&addr, &addrlen) != 0)
61			return NULL;
62	} else {
63		if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) != 0)
64			return NULL;
65	}
66
67	switch (addr.ss_family) {
68	case AF_INET:
69	case AF_INET6:
70		/* Get the address in ascii. */
71		if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
72		    sizeof(ntop), NULL, 0, flags)) != 0) {
73			error_f("getnameinfo %d failed: %s",
74			    flags, ssh_gai_strerror(r));
75			return NULL;
76		}
77		return xstrdup(ntop);
78	case AF_UNIX:
79		/* Get the Unix domain socket path. */
80		return xstrdup(((struct sockaddr_un *)&addr)->sun_path);
81	default:
82		/* We can't look up remote Unix domain sockets. */
83		return NULL;
84	}
85}
86
87char *
88get_peer_ipaddr(int sock)
89{
90	char *p;
91
92	if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
93		return p;
94	return xstrdup("UNKNOWN");
95}
96
97char *
98get_local_ipaddr(int sock)
99{
100	char *p;
101
102	if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
103		return p;
104	return xstrdup("UNKNOWN");
105}
106
107char *
108get_local_name(int fd)
109{
110	char *host, myname[NI_MAXHOST];
111
112	/* Assume we were passed a socket */
113	if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
114		return host;
115
116	/* Handle the case where we were passed a pipe */
117	if (gethostname(myname, sizeof(myname)) == -1) {
118		verbose_f("gethostname: %s", strerror(errno));
119		host = xstrdup("UNKNOWN");
120	} else {
121		host = xstrdup(myname);
122	}
123
124	return host;
125}
126
127/* Returns the local/remote port for the socket. */
128
129static int
130get_sock_port(int sock, int local)
131{
132	struct sockaddr_storage from;
133	socklen_t fromlen;
134	char strport[NI_MAXSERV];
135	int r;
136
137	if (sock < 0)
138		return -1;
139	/* Get IP address of client. */
140	fromlen = sizeof(from);
141	memset(&from, 0, sizeof(from));
142	if (local) {
143		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) == -1) {
144			error("getsockname failed: %.100s", strerror(errno));
145			return 0;
146		}
147	} else {
148		if (getpeername(sock, (struct sockaddr *)&from, &fromlen) == -1) {
149			debug("getpeername failed: %.100s", strerror(errno));
150			return -1;
151		}
152	}
153
154	/* Non-inet sockets don't have a port number. */
155	if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
156		return 0;
157
158	/* Return port number. */
159	if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
160	    strport, sizeof(strport), NI_NUMERICSERV)) != 0)
161		fatal_f("getnameinfo NI_NUMERICSERV failed: %s",
162		    ssh_gai_strerror(r));
163	return atoi(strport);
164}
165
166int
167get_peer_port(int sock)
168{
169	return get_sock_port(sock, 0);
170}
171
172int
173get_local_port(int sock)
174{
175	return get_sock_port(sock, 1);
176}
177