xref: /openbsd/usr.bin/ssh/canohost.c (revision 1667b834)
1 /* $OpenBSD: canohost.c,v 1.73 2016/03/07 19:02:43 djm 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  */
39 static char *
40 get_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 	/* Get IP address of client. */
48 	addrlen = sizeof(addr);
49 	memset(&addr, 0, sizeof(addr));
50 
51 	if (remote) {
52 		if (getpeername(sock, (struct sockaddr *)&addr, &addrlen) != 0)
53 			return NULL;
54 	} else {
55 		if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) != 0)
56 			return NULL;
57 	}
58 
59 	switch (addr.ss_family) {
60 	case AF_INET:
61 	case AF_INET6:
62 		/* Get the address in ascii. */
63 		if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
64 		    sizeof(ntop), NULL, 0, flags)) != 0) {
65 			error("%s: getnameinfo %d failed: %s", __func__,
66 			    flags, ssh_gai_strerror(r));
67 			return NULL;
68 		}
69 		return xstrdup(ntop);
70 	case AF_UNIX:
71 		/* Get the Unix domain socket path. */
72 		return xstrdup(((struct sockaddr_un *)&addr)->sun_path);
73 	default:
74 		/* We can't look up remote Unix domain sockets. */
75 		return NULL;
76 	}
77 }
78 
79 char *
80 get_peer_ipaddr(int sock)
81 {
82 	char *p;
83 
84 	if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
85 		return p;
86 	return xstrdup("UNKNOWN");
87 }
88 
89 char *
90 get_local_ipaddr(int sock)
91 {
92 	char *p;
93 
94 	if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
95 		return p;
96 	return xstrdup("UNKNOWN");
97 }
98 
99 char *
100 get_local_name(int fd)
101 {
102 	char *host, myname[NI_MAXHOST];
103 
104 	/* Assume we were passed a socket */
105 	if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
106 		return host;
107 
108 	/* Handle the case where we were passed a pipe */
109 	if (gethostname(myname, sizeof(myname)) == -1) {
110 		verbose("%s: gethostname: %s", __func__, strerror(errno));
111 		host = xstrdup("UNKNOWN");
112 	} else {
113 		host = xstrdup(myname);
114 	}
115 
116 	return host;
117 }
118 
119 /* Returns the local/remote port for the socket. */
120 
121 static int
122 get_sock_port(int sock, int local)
123 {
124 	struct sockaddr_storage from;
125 	socklen_t fromlen;
126 	char strport[NI_MAXSERV];
127 	int r;
128 
129 	/* Get IP address of client. */
130 	fromlen = sizeof(from);
131 	memset(&from, 0, sizeof(from));
132 	if (local) {
133 		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
134 			error("getsockname failed: %.100s", strerror(errno));
135 			return 0;
136 		}
137 	} else {
138 		if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
139 			debug("getpeername failed: %.100s", strerror(errno));
140 			return -1;
141 		}
142 	}
143 
144 	/* Non-inet sockets don't have a port number. */
145 	if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
146 		return 0;
147 
148 	/* Return port number. */
149 	if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
150 	    strport, sizeof(strport), NI_NUMERICSERV)) != 0)
151 		fatal("%s: getnameinfo NI_NUMERICSERV failed: %s", __func__,
152 		    ssh_gai_strerror(r));
153 	return atoi(strport);
154 }
155 
156 int
157 get_peer_port(int sock)
158 {
159 	return get_sock_port(sock, 0);
160 }
161 
162 int
163 get_local_port(int sock)
164 {
165 	return get_sock_port(sock, 1);
166 }
167