1/* $OpenBSD: canohost.c,v 1.77 2023/03/31 04:42:29 dtucker 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 <sys/types.h>
16#include <sys/socket.h>
17#include <sys/un.h>
18
19#include <netinet/in.h>
20
21#include <errno.h>
22#include <netdb.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27#include <unistd.h>
28
29#include "xmalloc.h"
30#include "packet.h"
31#include "log.h"
32#include "canohost.h"
33#include "misc.h"
34
35/*
36 * Returns the local/remote IP-address/hostname of socket as a string.
37 * The returned string must be freed.
38 */
39static char *
40get_socket_address(int sock, int remote, int flags)
41{
42	struct sockaddr_storage addr;
43	socklen_t addrlen;
44	char ntop[NI_MAXHOST];
45	int r;
46
47	if (sock < 0)
48		return NULL;
49
50	/* Get IP address of client. */
51	addrlen = sizeof(addr);
52	memset(&addr, 0, sizeof(addr));
53
54	if (remote) {
55		if (getpeername(sock, (struct sockaddr *)&addr, &addrlen) != 0)
56			return NULL;
57	} else {
58		if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) != 0)
59			return NULL;
60	}
61
62	switch (addr.ss_family) {
63	case AF_INET:
64	case AF_INET6:
65		/* Get the address in ascii. */
66		if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
67		    sizeof(ntop), NULL, 0, flags)) != 0) {
68			error_f("getnameinfo %d failed: %s",
69			    flags, ssh_gai_strerror(r));
70			return NULL;
71		}
72		return xstrdup(ntop);
73	case AF_UNIX:
74		/* Get the Unix domain socket path. */
75		return xstrdup(((struct sockaddr_un *)&addr)->sun_path);
76	default:
77		/* We can't look up remote Unix domain sockets. */
78		return NULL;
79	}
80}
81
82char *
83get_peer_ipaddr(int sock)
84{
85	char *p;
86
87	if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
88		return p;
89	return xstrdup("UNKNOWN");
90}
91
92char *
93get_local_ipaddr(int sock)
94{
95	char *p;
96
97	if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
98		return p;
99	return xstrdup("UNKNOWN");
100}
101
102char *
103get_local_name(int fd)
104{
105	char *host, myname[NI_MAXHOST];
106
107	/* Assume we were passed a socket */
108	if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
109		return host;
110
111	/* Handle the case where we were passed a pipe */
112	if (gethostname(myname, sizeof(myname)) == -1) {
113		verbose_f("gethostname: %s", strerror(errno));
114		host = xstrdup("UNKNOWN");
115	} else {
116		host = xstrdup(myname);
117	}
118
119	return host;
120}
121
122/* Returns the local/remote port for the socket. */
123
124static int
125get_sock_port(int sock, int local)
126{
127	struct sockaddr_storage from;
128	socklen_t fromlen;
129	char strport[NI_MAXSERV];
130	int r;
131
132	if (sock < 0)
133		return -1;
134	/* Get IP address of client. */
135	fromlen = sizeof(from);
136	memset(&from, 0, sizeof(from));
137	if (local) {
138		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) == -1) {
139			error("getsockname failed: %.100s", strerror(errno));
140			return 0;
141		}
142	} else {
143		if (getpeername(sock, (struct sockaddr *)&from, &fromlen) == -1) {
144			debug("getpeername failed: %.100s", strerror(errno));
145			return -1;
146		}
147	}
148
149	/* Non-inet sockets don't have a port number. */
150	if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
151		return 0;
152
153	/* Return port number. */
154	if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
155	    strport, sizeof(strport), NI_NUMERICSERV)) != 0)
156		fatal_f("getnameinfo NI_NUMERICSERV failed: %s",
157		    ssh_gai_strerror(r));
158	return atoi(strport);
159}
160
161int
162get_peer_port(int sock)
163{
164	return get_sock_port(sock, 0);
165}
166
167int
168get_local_port(int sock)
169{
170	return get_sock_port(sock, 1);
171}
172