xref: /freebsd/crypto/openssh/sshconnect.c (revision 0fdf8fae8b569bf9fff3b5171e669dcd7cf9c79e)
1*0fdf8faeSEd Maste /* $OpenBSD: sshconnect.c,v 1.368 2024/04/30 02:10:49 djm Exp $ */
2511b41d2SMark Murray /*
3511b41d2SMark Murray  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4511b41d2SMark Murray  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5511b41d2SMark Murray  *                    All rights reserved
6511b41d2SMark Murray  * Code to connect to a remote host, and to perform the client side of the
7511b41d2SMark Murray  * login (authentication) dialog.
842f71286SMark Murray  *
9c2d3a559SKris Kennaway  * As far as I am concerned, the code I have written for this software
10c2d3a559SKris Kennaway  * can be used freely for any purpose.  Any derived versions of this
11c2d3a559SKris Kennaway  * software must be clearly marked as such, and if the derived work is
12c2d3a559SKris Kennaway  * incompatible with the protocol description in the RFC file, it must be
13c2d3a559SKris Kennaway  * called by a name other than "ssh" or "Secure Shell".
14511b41d2SMark Murray  */
15511b41d2SMark Murray 
16511b41d2SMark Murray #include "includes.h"
17511b41d2SMark Murray 
18333ee039SDag-Erling Smørgrav #include <sys/types.h>
19333ee039SDag-Erling Smørgrav #include <sys/wait.h>
20333ee039SDag-Erling Smørgrav #include <sys/stat.h>
21333ee039SDag-Erling Smørgrav #include <sys/socket.h>
22333ee039SDag-Erling Smørgrav #ifdef HAVE_SYS_TIME_H
23333ee039SDag-Erling Smørgrav # include <sys/time.h>
24333ee039SDag-Erling Smørgrav #endif
25e8aafc91SKris Kennaway 
2647dd1d1bSDag-Erling Smørgrav #include <net/if.h>
27333ee039SDag-Erling Smørgrav #include <netinet/in.h>
28333ee039SDag-Erling Smørgrav #include <arpa/inet.h>
29333ee039SDag-Erling Smørgrav 
30333ee039SDag-Erling Smørgrav #include <ctype.h>
31333ee039SDag-Erling Smørgrav #include <errno.h>
32b15c8340SDag-Erling Smørgrav #include <fcntl.h>
3319261079SEd Maste #include <limits.h>
34333ee039SDag-Erling Smørgrav #include <netdb.h>
35333ee039SDag-Erling Smørgrav #ifdef HAVE_PATHS_H
36333ee039SDag-Erling Smørgrav #include <paths.h>
37333ee039SDag-Erling Smørgrav #endif
38333ee039SDag-Erling Smørgrav #include <pwd.h>
394f52dfbbSDag-Erling Smørgrav #ifdef HAVE_POLL_H
404f52dfbbSDag-Erling Smørgrav #include <poll.h>
414f52dfbbSDag-Erling Smørgrav #endif
424a421b63SDag-Erling Smørgrav #include <signal.h>
43333ee039SDag-Erling Smørgrav #include <stdio.h>
44333ee039SDag-Erling Smørgrav #include <stdlib.h>
4519261079SEd Maste #include <stdarg.h>
46333ee039SDag-Erling Smørgrav #include <string.h>
47333ee039SDag-Erling Smørgrav #include <unistd.h>
4847dd1d1bSDag-Erling Smørgrav #ifdef HAVE_IFADDRS_H
4947dd1d1bSDag-Erling Smørgrav # include <ifaddrs.h>
5047dd1d1bSDag-Erling Smørgrav #endif
51333ee039SDag-Erling Smørgrav 
52511b41d2SMark Murray #include "xmalloc.h"
53333ee039SDag-Erling Smørgrav #include "hostfile.h"
54333ee039SDag-Erling Smørgrav #include "ssh.h"
55190cef3dSDag-Erling Smørgrav #include "sshbuf.h"
56511b41d2SMark Murray #include "packet.h"
57190cef3dSDag-Erling Smørgrav #include "sshkey.h"
58e8aafc91SKris Kennaway #include "sshconnect.h"
59ca3176e7SBrian Feldman #include "log.h"
60*0fdf8faeSEd Maste #include "match.h"
61a0ee8cc6SDag-Erling Smørgrav #include "misc.h"
62ca3176e7SBrian Feldman #include "readconf.h"
63ca3176e7SBrian Feldman #include "atomicio.h"
64cf2b5f3bSDag-Erling Smørgrav #include "dns.h"
65f7167e0eSDag-Erling Smørgrav #include "monitor_fdpass.h"
66b15c8340SDag-Erling Smørgrav #include "ssh2.h"
67333ee039SDag-Erling Smørgrav #include "version.h"
68bc5531deSDag-Erling Smørgrav #include "authfile.h"
69bc5531deSDag-Erling Smørgrav #include "ssherr.h"
70acc1a9efSDag-Erling Smørgrav #include "authfd.h"
7119261079SEd Maste #include "kex.h"
72cf2b5f3bSDag-Erling Smørgrav 
734f52dfbbSDag-Erling Smørgrav struct sshkey *previous_host_key = NULL;
74511b41d2SMark Murray 
75b74df5b2SDag-Erling Smørgrav static int matching_host_key_dns = 0;
76cf2b5f3bSDag-Erling Smørgrav 
774a421b63SDag-Erling Smørgrav static pid_t proxy_command_pid = 0;
784a421b63SDag-Erling Smørgrav 
7980628bacSDag-Erling Smørgrav /* import */
8019261079SEd Maste extern int debug_flag;
81511b41d2SMark Murray extern Options options;
82511b41d2SMark Murray extern char *__progname;
83511b41d2SMark Murray 
844f52dfbbSDag-Erling Smørgrav static int show_other_keys(struct hostkeys *, struct sshkey *);
854f52dfbbSDag-Erling Smørgrav static void warn_changed_key(struct sshkey *);
86ca3176e7SBrian Feldman 
87f7167e0eSDag-Erling Smørgrav /* Expand a proxy command */
88f7167e0eSDag-Erling Smørgrav static char *
expand_proxy_command(const char * proxy_command,const char * user,const char * host,const char * host_arg,int port)89f7167e0eSDag-Erling Smørgrav expand_proxy_command(const char *proxy_command, const char *user,
9019261079SEd Maste     const char *host, const char *host_arg, int port)
91f7167e0eSDag-Erling Smørgrav {
92f7167e0eSDag-Erling Smørgrav 	char *tmp, *ret, strport[NI_MAXSERV];
9319261079SEd Maste 	const char *keyalias = options.host_key_alias ?
9419261079SEd Maste 	    options.host_key_alias : host_arg;
95f7167e0eSDag-Erling Smørgrav 
96f7167e0eSDag-Erling Smørgrav 	snprintf(strport, sizeof strport, "%d", port);
97f7167e0eSDag-Erling Smørgrav 	xasprintf(&tmp, "exec %s", proxy_command);
9819261079SEd Maste 	ret = percent_expand(tmp,
9919261079SEd Maste 	    "h", host,
10019261079SEd Maste 	    "k", keyalias,
10119261079SEd Maste 	    "n", host_arg,
10219261079SEd Maste 	    "p", strport,
10319261079SEd Maste 	    "r", options.user,
10419261079SEd Maste 	    (char *)NULL);
105f7167e0eSDag-Erling Smørgrav 	free(tmp);
106f7167e0eSDag-Erling Smørgrav 	return ret;
107f7167e0eSDag-Erling Smørgrav }
108f7167e0eSDag-Erling Smørgrav 
109f7167e0eSDag-Erling Smørgrav /*
110f7167e0eSDag-Erling Smørgrav  * Connect to the given ssh server using a proxy command that passes a
111f7167e0eSDag-Erling Smørgrav  * a connected fd back to us.
112f7167e0eSDag-Erling Smørgrav  */
113f7167e0eSDag-Erling Smørgrav static int
ssh_proxy_fdpass_connect(struct ssh * ssh,const char * host,const char * host_arg,u_short port,const char * proxy_command)11419261079SEd Maste ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host,
11519261079SEd Maste     const char *host_arg, u_short port, const char *proxy_command)
116f7167e0eSDag-Erling Smørgrav {
117f7167e0eSDag-Erling Smørgrav 	char *command_string;
118f7167e0eSDag-Erling Smørgrav 	int sp[2], sock;
119f7167e0eSDag-Erling Smørgrav 	pid_t pid;
120f7167e0eSDag-Erling Smørgrav 	char *shell;
121f7167e0eSDag-Erling Smørgrav 
122f7167e0eSDag-Erling Smørgrav 	if ((shell = getenv("SHELL")) == NULL)
123f7167e0eSDag-Erling Smørgrav 		shell = _PATH_BSHELL;
124f7167e0eSDag-Erling Smørgrav 
12519261079SEd Maste 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) == -1)
126f7167e0eSDag-Erling Smørgrav 		fatal("Could not create socketpair to communicate with "
127f7167e0eSDag-Erling Smørgrav 		    "proxy dialer: %.100s", strerror(errno));
128f7167e0eSDag-Erling Smørgrav 
129f7167e0eSDag-Erling Smørgrav 	command_string = expand_proxy_command(proxy_command, options.user,
13019261079SEd Maste 	    host, host_arg, port);
131f7167e0eSDag-Erling Smørgrav 	debug("Executing proxy dialer command: %.500s", command_string);
132f7167e0eSDag-Erling Smørgrav 
133f7167e0eSDag-Erling Smørgrav 	/* Fork and execute the proxy command. */
134f7167e0eSDag-Erling Smørgrav 	if ((pid = fork()) == 0) {
135f7167e0eSDag-Erling Smørgrav 		char *argv[10];
136f7167e0eSDag-Erling Smørgrav 
137f7167e0eSDag-Erling Smørgrav 		close(sp[1]);
138f7167e0eSDag-Erling Smørgrav 		/* Redirect stdin and stdout. */
139f7167e0eSDag-Erling Smørgrav 		if (sp[0] != 0) {
14019261079SEd Maste 			if (dup2(sp[0], 0) == -1)
141f7167e0eSDag-Erling Smørgrav 				perror("dup2 stdin");
142f7167e0eSDag-Erling Smørgrav 		}
143f7167e0eSDag-Erling Smørgrav 		if (sp[0] != 1) {
14419261079SEd Maste 			if (dup2(sp[0], 1) == -1)
145f7167e0eSDag-Erling Smørgrav 				perror("dup2 stdout");
146f7167e0eSDag-Erling Smørgrav 		}
147f7167e0eSDag-Erling Smørgrav 		if (sp[0] >= 2)
148f7167e0eSDag-Erling Smørgrav 			close(sp[0]);
149f7167e0eSDag-Erling Smørgrav 
150f7167e0eSDag-Erling Smørgrav 		/*
15119261079SEd Maste 		 * Stderr is left for non-ControlPersist connections is so
15219261079SEd Maste 		 * error messages may be printed on the user's terminal.
153f7167e0eSDag-Erling Smørgrav 		 */
15419261079SEd Maste 		if (!debug_flag && options.control_path != NULL &&
15519261079SEd Maste 		    options.control_persist && stdfd_devnull(0, 0, 1) == -1)
15619261079SEd Maste 			error_f("stdfd_devnull failed");
15719261079SEd Maste 
158f7167e0eSDag-Erling Smørgrav 		argv[0] = shell;
159f7167e0eSDag-Erling Smørgrav 		argv[1] = "-c";
160f7167e0eSDag-Erling Smørgrav 		argv[2] = command_string;
161f7167e0eSDag-Erling Smørgrav 		argv[3] = NULL;
162f7167e0eSDag-Erling Smørgrav 
163f7167e0eSDag-Erling Smørgrav 		/*
164f7167e0eSDag-Erling Smørgrav 		 * Execute the proxy command.
165f7167e0eSDag-Erling Smørgrav 		 * Note that we gave up any extra privileges above.
166f7167e0eSDag-Erling Smørgrav 		 */
167f7167e0eSDag-Erling Smørgrav 		execv(argv[0], argv);
168f7167e0eSDag-Erling Smørgrav 		perror(argv[0]);
169f7167e0eSDag-Erling Smørgrav 		exit(1);
170f7167e0eSDag-Erling Smørgrav 	}
171f7167e0eSDag-Erling Smørgrav 	/* Parent. */
17219261079SEd Maste 	if (pid == -1)
173f7167e0eSDag-Erling Smørgrav 		fatal("fork failed: %.100s", strerror(errno));
174f7167e0eSDag-Erling Smørgrav 	close(sp[0]);
175f7167e0eSDag-Erling Smørgrav 	free(command_string);
176f7167e0eSDag-Erling Smørgrav 
177f7167e0eSDag-Erling Smørgrav 	if ((sock = mm_receive_fd(sp[1])) == -1)
178f7167e0eSDag-Erling Smørgrav 		fatal("proxy dialer did not pass back a connection");
179acc1a9efSDag-Erling Smørgrav 	close(sp[1]);
180f7167e0eSDag-Erling Smørgrav 
181f7167e0eSDag-Erling Smørgrav 	while (waitpid(pid, NULL, 0) == -1)
182f7167e0eSDag-Erling Smørgrav 		if (errno != EINTR)
183f7167e0eSDag-Erling Smørgrav 			fatal("Couldn't wait for child: %s", strerror(errno));
184f7167e0eSDag-Erling Smørgrav 
185f7167e0eSDag-Erling Smørgrav 	/* Set the connection file descriptors. */
1864f52dfbbSDag-Erling Smørgrav 	if (ssh_packet_set_connection(ssh, sock, sock) == NULL)
1874f52dfbbSDag-Erling Smørgrav 		return -1; /* ssh_packet_set_connection logs error */
188f7167e0eSDag-Erling Smørgrav 
189f7167e0eSDag-Erling Smørgrav 	return 0;
190f7167e0eSDag-Erling Smørgrav }
191f7167e0eSDag-Erling Smørgrav 
192511b41d2SMark Murray /*
193511b41d2SMark Murray  * Connect to the given ssh server using a proxy command.
194511b41d2SMark Murray  */
195af12a3e7SDag-Erling Smørgrav static int
ssh_proxy_connect(struct ssh * ssh,const char * host,const char * host_arg,u_short port,const char * proxy_command)19619261079SEd Maste ssh_proxy_connect(struct ssh *ssh, const char *host, const char *host_arg,
19719261079SEd Maste     u_short port, const char *proxy_command)
198511b41d2SMark Murray {
199f7167e0eSDag-Erling Smørgrav 	char *command_string;
200511b41d2SMark Murray 	int pin[2], pout[2];
201e8aafc91SKris Kennaway 	pid_t pid;
202f7167e0eSDag-Erling Smørgrav 	char *shell;
203420bce64SDag-Erling Smørgrav 
2044a421b63SDag-Erling Smørgrav 	if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
205d4af9e69SDag-Erling Smørgrav 		shell = _PATH_BSHELL;
206511b41d2SMark Murray 
207511b41d2SMark Murray 	/* Create pipes for communicating with the proxy. */
20819261079SEd Maste 	if (pipe(pin) == -1 || pipe(pout) == -1)
209511b41d2SMark Murray 		fatal("Could not create pipes to communicate with the proxy: %.100s",
210511b41d2SMark Murray 		    strerror(errno));
211511b41d2SMark Murray 
212f7167e0eSDag-Erling Smørgrav 	command_string = expand_proxy_command(proxy_command, options.user,
21319261079SEd Maste 	    host, host_arg, port);
214511b41d2SMark Murray 	debug("Executing proxy command: %.500s", command_string);
215511b41d2SMark Murray 
216511b41d2SMark Murray 	/* Fork and execute the proxy command. */
217511b41d2SMark Murray 	if ((pid = fork()) == 0) {
218511b41d2SMark Murray 		char *argv[10];
219511b41d2SMark Murray 
220511b41d2SMark Murray 		/* Redirect stdin and stdout. */
221511b41d2SMark Murray 		close(pin[1]);
222511b41d2SMark Murray 		if (pin[0] != 0) {
22319261079SEd Maste 			if (dup2(pin[0], 0) == -1)
224511b41d2SMark Murray 				perror("dup2 stdin");
225511b41d2SMark Murray 			close(pin[0]);
226511b41d2SMark Murray 		}
227511b41d2SMark Murray 		close(pout[0]);
22819261079SEd Maste 		if (dup2(pout[1], 1) == -1)
229511b41d2SMark Murray 			perror("dup2 stdout");
230511b41d2SMark Murray 		/* Cannot be 1 because pin allocated two descriptors. */
231511b41d2SMark Murray 		close(pout[1]);
232511b41d2SMark Murray 
23319261079SEd Maste 		/*
23419261079SEd Maste 		 * Stderr is left for non-ControlPersist connections is so
23519261079SEd Maste 		 * error messages may be printed on the user's terminal.
23619261079SEd Maste 		 */
23719261079SEd Maste 		if (!debug_flag && options.control_path != NULL &&
23819261079SEd Maste 		    options.control_persist && stdfd_devnull(0, 0, 1) == -1)
23919261079SEd Maste 			error_f("stdfd_devnull failed");
24019261079SEd Maste 
241d4af9e69SDag-Erling Smørgrav 		argv[0] = shell;
242511b41d2SMark Murray 		argv[1] = "-c";
243511b41d2SMark Murray 		argv[2] = command_string;
244511b41d2SMark Murray 		argv[3] = NULL;
245511b41d2SMark Murray 
24619261079SEd Maste 		/*
24719261079SEd Maste 		 * Execute the proxy command.  Note that we gave up any
24819261079SEd Maste 		 * extra privileges above.
24919261079SEd Maste 		 */
25019261079SEd Maste 		ssh_signal(SIGPIPE, SIG_DFL);
251ca3176e7SBrian Feldman 		execv(argv[0], argv);
252ca3176e7SBrian Feldman 		perror(argv[0]);
253511b41d2SMark Murray 		exit(1);
254511b41d2SMark Murray 	}
255511b41d2SMark Murray 	/* Parent. */
25619261079SEd Maste 	if (pid == -1)
257511b41d2SMark Murray 		fatal("fork failed: %.100s", strerror(errno));
258f388f5efSDag-Erling Smørgrav 	else
259f388f5efSDag-Erling Smørgrav 		proxy_command_pid = pid; /* save pid to clean up later */
260511b41d2SMark Murray 
261511b41d2SMark Murray 	/* Close child side of the descriptors. */
262511b41d2SMark Murray 	close(pin[0]);
263511b41d2SMark Murray 	close(pout[1]);
264511b41d2SMark Murray 
265511b41d2SMark Murray 	/* Free the command name. */
266e4a9863fSDag-Erling Smørgrav 	free(command_string);
267511b41d2SMark Murray 
268511b41d2SMark Murray 	/* Set the connection file descriptors. */
2694f52dfbbSDag-Erling Smørgrav 	if (ssh_packet_set_connection(ssh, pout[0], pin[1]) == NULL)
2704f52dfbbSDag-Erling Smørgrav 		return -1; /* ssh_packet_set_connection logs error */
271511b41d2SMark Murray 
272af12a3e7SDag-Erling Smørgrav 	return 0;
273511b41d2SMark Murray }
274511b41d2SMark Murray 
2754a421b63SDag-Erling Smørgrav void
ssh_kill_proxy_command(void)2764a421b63SDag-Erling Smørgrav ssh_kill_proxy_command(void)
2774a421b63SDag-Erling Smørgrav {
2784a421b63SDag-Erling Smørgrav 	/*
2794a421b63SDag-Erling Smørgrav 	 * Send SIGHUP to proxy command if used. We don't wait() in
2804a421b63SDag-Erling Smørgrav 	 * case it hangs and instead rely on init to reap the child
2814a421b63SDag-Erling Smørgrav 	 */
2824a421b63SDag-Erling Smørgrav 	if (proxy_command_pid > 1)
2834a421b63SDag-Erling Smørgrav 		kill(proxy_command_pid, SIGHUP);
2844a421b63SDag-Erling Smørgrav }
2854a421b63SDag-Erling Smørgrav 
28647dd1d1bSDag-Erling Smørgrav #ifdef HAVE_IFADDRS_H
28747dd1d1bSDag-Erling Smørgrav /*
28847dd1d1bSDag-Erling Smørgrav  * Search a interface address list (returned from getifaddrs(3)) for an
289190cef3dSDag-Erling Smørgrav  * address that matches the desired address family on the specified interface.
29047dd1d1bSDag-Erling Smørgrav  * Returns 0 and fills in *resultp and *rlenp on success. Returns -1 on failure.
29147dd1d1bSDag-Erling Smørgrav  */
29247dd1d1bSDag-Erling Smørgrav static int
check_ifaddrs(const char * ifname,int af,const struct ifaddrs * ifaddrs,struct sockaddr_storage * resultp,socklen_t * rlenp)29347dd1d1bSDag-Erling Smørgrav check_ifaddrs(const char *ifname, int af, const struct ifaddrs *ifaddrs,
29447dd1d1bSDag-Erling Smørgrav     struct sockaddr_storage *resultp, socklen_t *rlenp)
29547dd1d1bSDag-Erling Smørgrav {
29647dd1d1bSDag-Erling Smørgrav 	struct sockaddr_in6 *sa6;
29747dd1d1bSDag-Erling Smørgrav 	struct sockaddr_in *sa;
29847dd1d1bSDag-Erling Smørgrav 	struct in6_addr *v6addr;
29947dd1d1bSDag-Erling Smørgrav 	const struct ifaddrs *ifa;
30047dd1d1bSDag-Erling Smørgrav 	int allow_local;
30147dd1d1bSDag-Erling Smørgrav 
30247dd1d1bSDag-Erling Smørgrav 	/*
30347dd1d1bSDag-Erling Smørgrav 	 * Prefer addresses that are not loopback or linklocal, but use them
30447dd1d1bSDag-Erling Smørgrav 	 * if nothing else matches.
30547dd1d1bSDag-Erling Smørgrav 	 */
30647dd1d1bSDag-Erling Smørgrav 	for (allow_local = 0; allow_local < 2; allow_local++) {
30747dd1d1bSDag-Erling Smørgrav 		for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
30847dd1d1bSDag-Erling Smørgrav 			if (ifa->ifa_addr == NULL || ifa->ifa_name == NULL ||
30947dd1d1bSDag-Erling Smørgrav 			    (ifa->ifa_flags & IFF_UP) == 0 ||
31047dd1d1bSDag-Erling Smørgrav 			    ifa->ifa_addr->sa_family != af ||
31147dd1d1bSDag-Erling Smørgrav 			    strcmp(ifa->ifa_name, options.bind_interface) != 0)
31247dd1d1bSDag-Erling Smørgrav 				continue;
31347dd1d1bSDag-Erling Smørgrav 			switch (ifa->ifa_addr->sa_family) {
31447dd1d1bSDag-Erling Smørgrav 			case AF_INET:
31547dd1d1bSDag-Erling Smørgrav 				sa = (struct sockaddr_in *)ifa->ifa_addr;
31647dd1d1bSDag-Erling Smørgrav 				if (!allow_local && sa->sin_addr.s_addr ==
31747dd1d1bSDag-Erling Smørgrav 				    htonl(INADDR_LOOPBACK))
31847dd1d1bSDag-Erling Smørgrav 					continue;
31947dd1d1bSDag-Erling Smørgrav 				if (*rlenp < sizeof(struct sockaddr_in)) {
32019261079SEd Maste 					error_f("v4 addr doesn't fit");
32147dd1d1bSDag-Erling Smørgrav 					return -1;
32247dd1d1bSDag-Erling Smørgrav 				}
32347dd1d1bSDag-Erling Smørgrav 				*rlenp = sizeof(struct sockaddr_in);
32447dd1d1bSDag-Erling Smørgrav 				memcpy(resultp, sa, *rlenp);
32547dd1d1bSDag-Erling Smørgrav 				return 0;
32647dd1d1bSDag-Erling Smørgrav 			case AF_INET6:
32747dd1d1bSDag-Erling Smørgrav 				sa6 = (struct sockaddr_in6 *)ifa->ifa_addr;
32847dd1d1bSDag-Erling Smørgrav 				v6addr = &sa6->sin6_addr;
32947dd1d1bSDag-Erling Smørgrav 				if (!allow_local &&
33047dd1d1bSDag-Erling Smørgrav 				    (IN6_IS_ADDR_LINKLOCAL(v6addr) ||
33147dd1d1bSDag-Erling Smørgrav 				    IN6_IS_ADDR_LOOPBACK(v6addr)))
33247dd1d1bSDag-Erling Smørgrav 					continue;
33347dd1d1bSDag-Erling Smørgrav 				if (*rlenp < sizeof(struct sockaddr_in6)) {
33419261079SEd Maste 					error_f("v6 addr doesn't fit");
33547dd1d1bSDag-Erling Smørgrav 					return -1;
33647dd1d1bSDag-Erling Smørgrav 				}
33747dd1d1bSDag-Erling Smørgrav 				*rlenp = sizeof(struct sockaddr_in6);
33847dd1d1bSDag-Erling Smørgrav 				memcpy(resultp, sa6, *rlenp);
33947dd1d1bSDag-Erling Smørgrav 				return 0;
34047dd1d1bSDag-Erling Smørgrav 			}
34147dd1d1bSDag-Erling Smørgrav 		}
34247dd1d1bSDag-Erling Smørgrav 	}
34347dd1d1bSDag-Erling Smørgrav 	return -1;
34447dd1d1bSDag-Erling Smørgrav }
34547dd1d1bSDag-Erling Smørgrav #endif
34647dd1d1bSDag-Erling Smørgrav 
347511b41d2SMark Murray /*
348190cef3dSDag-Erling Smørgrav  * Creates a socket for use as the ssh connection.
349511b41d2SMark Murray  */
350af12a3e7SDag-Erling Smørgrav static int
ssh_create_socket(struct addrinfo * ai)351190cef3dSDag-Erling Smørgrav ssh_create_socket(struct addrinfo *ai)
352511b41d2SMark Murray {
353190cef3dSDag-Erling Smørgrav 	int sock, r;
35447dd1d1bSDag-Erling Smørgrav 	struct sockaddr_storage bindaddr;
35547dd1d1bSDag-Erling Smørgrav 	socklen_t bindaddrlen = 0;
356b83788ffSDag-Erling Smørgrav 	struct addrinfo hints, *res = NULL;
35747dd1d1bSDag-Erling Smørgrav #ifdef HAVE_IFADDRS_H
35847dd1d1bSDag-Erling Smørgrav 	struct ifaddrs *ifaddrs = NULL;
35947dd1d1bSDag-Erling Smørgrav #endif
36047dd1d1bSDag-Erling Smørgrav 	char ntop[NI_MAXHOST];
361511b41d2SMark Murray 
362cf2b5f3bSDag-Erling Smørgrav 	sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
36319261079SEd Maste 	if (sock == -1) {
364f7167e0eSDag-Erling Smørgrav 		error("socket: %s", strerror(errno));
365b15c8340SDag-Erling Smørgrav 		return -1;
366b15c8340SDag-Erling Smørgrav 	}
3674d3fc8b0SEd Maste 	(void)fcntl(sock, F_SETFD, FD_CLOEXEC);
368af12a3e7SDag-Erling Smørgrav 
36919261079SEd Maste 	/* Use interactive QOS (if specified) until authentication completed */
37019261079SEd Maste 	if (options.ip_qos_interactive != INT_MAX)
37119261079SEd Maste 		set_sock_tos(sock, options.ip_qos_interactive);
37219261079SEd Maste 
373af12a3e7SDag-Erling Smørgrav 	/* Bind the socket to an alternative local IP address */
374190cef3dSDag-Erling Smørgrav 	if (options.bind_address == NULL && options.bind_interface == NULL)
375af12a3e7SDag-Erling Smørgrav 		return sock;
376af12a3e7SDag-Erling Smørgrav 
37747dd1d1bSDag-Erling Smørgrav 	if (options.bind_address != NULL) {
378af12a3e7SDag-Erling Smørgrav 		memset(&hints, 0, sizeof(hints));
379cf2b5f3bSDag-Erling Smørgrav 		hints.ai_family = ai->ai_family;
380cf2b5f3bSDag-Erling Smørgrav 		hints.ai_socktype = ai->ai_socktype;
381cf2b5f3bSDag-Erling Smørgrav 		hints.ai_protocol = ai->ai_protocol;
382af12a3e7SDag-Erling Smørgrav 		hints.ai_flags = AI_PASSIVE;
38347dd1d1bSDag-Erling Smørgrav 		if ((r = getaddrinfo(options.bind_address, NULL,
38447dd1d1bSDag-Erling Smørgrav 		    &hints, &res)) != 0) {
385af12a3e7SDag-Erling Smørgrav 			error("getaddrinfo: %s: %s", options.bind_address,
38647dd1d1bSDag-Erling Smørgrav 			    ssh_gai_strerror(r));
38747dd1d1bSDag-Erling Smørgrav 			goto fail;
388511b41d2SMark Murray 		}
38947dd1d1bSDag-Erling Smørgrav 		if (res == NULL) {
39047dd1d1bSDag-Erling Smørgrav 			error("getaddrinfo: no addrs");
39147dd1d1bSDag-Erling Smørgrav 			goto fail;
39247dd1d1bSDag-Erling Smørgrav 		}
39347dd1d1bSDag-Erling Smørgrav 		memcpy(&bindaddr, res->ai_addr, res->ai_addrlen);
39447dd1d1bSDag-Erling Smørgrav 		bindaddrlen = res->ai_addrlen;
39547dd1d1bSDag-Erling Smørgrav 	} else if (options.bind_interface != NULL) {
39647dd1d1bSDag-Erling Smørgrav #ifdef HAVE_IFADDRS_H
39747dd1d1bSDag-Erling Smørgrav 		if ((r = getifaddrs(&ifaddrs)) != 0) {
39847dd1d1bSDag-Erling Smørgrav 			error("getifaddrs: %s: %s", options.bind_interface,
39947dd1d1bSDag-Erling Smørgrav 			    strerror(errno));
40047dd1d1bSDag-Erling Smørgrav 			goto fail;
40147dd1d1bSDag-Erling Smørgrav 		}
40247dd1d1bSDag-Erling Smørgrav 		bindaddrlen = sizeof(bindaddr);
40347dd1d1bSDag-Erling Smørgrav 		if (check_ifaddrs(options.bind_interface, ai->ai_family,
40447dd1d1bSDag-Erling Smørgrav 		    ifaddrs, &bindaddr, &bindaddrlen) != 0) {
40547dd1d1bSDag-Erling Smørgrav 			logit("getifaddrs: %s: no suitable addresses",
40647dd1d1bSDag-Erling Smørgrav 			    options.bind_interface);
40747dd1d1bSDag-Erling Smørgrav 			goto fail;
40847dd1d1bSDag-Erling Smørgrav 		}
40947dd1d1bSDag-Erling Smørgrav #else
41047dd1d1bSDag-Erling Smørgrav 		error("BindInterface not supported on this platform.");
41147dd1d1bSDag-Erling Smørgrav #endif
41247dd1d1bSDag-Erling Smørgrav 	}
41347dd1d1bSDag-Erling Smørgrav 	if ((r = getnameinfo((struct sockaddr *)&bindaddr, bindaddrlen,
41447dd1d1bSDag-Erling Smørgrav 	    ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST)) != 0) {
41519261079SEd Maste 		error_f("getnameinfo failed: %s", ssh_gai_strerror(r));
41647dd1d1bSDag-Erling Smørgrav 		goto fail;
417b83788ffSDag-Erling Smørgrav 	}
418190cef3dSDag-Erling Smørgrav 	if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) {
41947dd1d1bSDag-Erling Smørgrav 		error("bind %s: %s", ntop, strerror(errno));
42047dd1d1bSDag-Erling Smørgrav 		goto fail;
42147dd1d1bSDag-Erling Smørgrav 	}
42219261079SEd Maste 	debug_f("bound to %s", ntop);
42347dd1d1bSDag-Erling Smørgrav 	/* success */
42447dd1d1bSDag-Erling Smørgrav 	goto out;
425f7167e0eSDag-Erling Smørgrav fail:
426af12a3e7SDag-Erling Smørgrav 	close(sock);
42747dd1d1bSDag-Erling Smørgrav 	sock = -1;
42847dd1d1bSDag-Erling Smørgrav  out:
429b83788ffSDag-Erling Smørgrav 	if (res != NULL)
430af12a3e7SDag-Erling Smørgrav 		freeaddrinfo(res);
43147dd1d1bSDag-Erling Smørgrav #ifdef HAVE_IFADDRS_H
43247dd1d1bSDag-Erling Smørgrav 	if (ifaddrs != NULL)
43347dd1d1bSDag-Erling Smørgrav 		freeifaddrs(ifaddrs);
43447dd1d1bSDag-Erling Smørgrav #endif
435511b41d2SMark Murray 	return sock;
436511b41d2SMark Murray }
437511b41d2SMark Murray 
4384f52dfbbSDag-Erling Smørgrav /*
439511b41d2SMark Murray  * Opens a TCP/IP connection to the remote server on the given host.
440511b41d2SMark Murray  * The address of the remote host will be returned in hostaddr.
441190cef3dSDag-Erling Smørgrav  * If port is 0, the default port will be used.
442511b41d2SMark Murray  * Connection_attempts specifies the maximum number of tries (one per
443511b41d2SMark Murray  * second).  If proxy_command is non-NULL, it specifies the command (with %h
444511b41d2SMark Murray  * and %p substituted for host and port, respectively) to use to contact
445511b41d2SMark Murray  * the daemon.
446511b41d2SMark Murray  */
447f7167e0eSDag-Erling Smørgrav static int
ssh_connect_direct(struct ssh * ssh,const char * host,struct addrinfo * aitop,struct sockaddr_storage * hostaddr,u_short port,int connection_attempts,int * timeout_ms,int want_keepalive)4484f52dfbbSDag-Erling Smørgrav ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
44919261079SEd Maste     struct sockaddr_storage *hostaddr, u_short port, int connection_attempts,
45019261079SEd Maste     int *timeout_ms, int want_keepalive)
451511b41d2SMark Murray {
45219261079SEd Maste 	int on = 1, saved_timeout_ms = *timeout_ms;
45347dd1d1bSDag-Erling Smørgrav 	int oerrno, sock = -1, attempt;
454ca3176e7SBrian Feldman 	char ntop[NI_MAXHOST], strport[NI_MAXSERV];
455f7167e0eSDag-Erling Smørgrav 	struct addrinfo *ai;
456511b41d2SMark Murray 
45719261079SEd Maste 	debug3_f("entering");
458acc1a9efSDag-Erling Smørgrav 	memset(ntop, 0, sizeof(ntop));
459acc1a9efSDag-Erling Smørgrav 	memset(strport, 0, sizeof(strport));
460511b41d2SMark Murray 
461333ee039SDag-Erling Smørgrav 	for (attempt = 0; attempt < connection_attempts; attempt++) {
46262efe23aSDag-Erling Smørgrav 		if (attempt > 0) {
46362efe23aSDag-Erling Smørgrav 			/* Sleep a moment before retrying. */
46462efe23aSDag-Erling Smørgrav 			sleep(1);
465511b41d2SMark Murray 			debug("Trying again...");
46662efe23aSDag-Erling Smørgrav 		}
467333ee039SDag-Erling Smørgrav 		/*
468333ee039SDag-Erling Smørgrav 		 * Loop through addresses for this host, and try each one in
469333ee039SDag-Erling Smørgrav 		 * sequence until the connection succeeds.
470333ee039SDag-Erling Smørgrav 		 */
471511b41d2SMark Murray 		for (ai = aitop; ai; ai = ai->ai_next) {
472f7167e0eSDag-Erling Smørgrav 			if (ai->ai_family != AF_INET &&
47347dd1d1bSDag-Erling Smørgrav 			    ai->ai_family != AF_INET6) {
47447dd1d1bSDag-Erling Smørgrav 				errno = EAFNOSUPPORT;
475511b41d2SMark Murray 				continue;
47647dd1d1bSDag-Erling Smørgrav 			}
477511b41d2SMark Murray 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
478511b41d2SMark Murray 			    ntop, sizeof(ntop), strport, sizeof(strport),
479511b41d2SMark Murray 			    NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
48047dd1d1bSDag-Erling Smørgrav 				oerrno = errno;
48119261079SEd Maste 				error_f("getnameinfo failed");
48247dd1d1bSDag-Erling Smørgrav 				errno = oerrno;
483511b41d2SMark Murray 				continue;
484511b41d2SMark Murray 			}
485069ac184SEd Maste 			if (options.address_family != AF_UNSPEC &&
486069ac184SEd Maste 			    ai->ai_family != options.address_family) {
487069ac184SEd Maste 				debug2_f("skipping address [%s]:%s: "
488069ac184SEd Maste 				    "wrong address family", ntop, strport);
489069ac184SEd Maste 				errno = EAFNOSUPPORT;
490069ac184SEd Maste 				continue;
491069ac184SEd Maste 			}
492069ac184SEd Maste 
493511b41d2SMark Murray 			debug("Connecting to %.200s [%.100s] port %s.",
4941f5ce8f4SBrian Feldman 				host, ntop, strport);
495511b41d2SMark Murray 
496511b41d2SMark Murray 			/* Create a socket for connecting. */
497190cef3dSDag-Erling Smørgrav 			sock = ssh_create_socket(ai);
49847dd1d1bSDag-Erling Smørgrav 			if (sock < 0) {
499af12a3e7SDag-Erling Smørgrav 				/* Any error is already output */
50047dd1d1bSDag-Erling Smørgrav 				errno = 0;
501511b41d2SMark Murray 				continue;
50247dd1d1bSDag-Erling Smørgrav 			}
503511b41d2SMark Murray 
50419261079SEd Maste 			*timeout_ms = saved_timeout_ms;
505cf2b5f3bSDag-Erling Smørgrav 			if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
506d4af9e69SDag-Erling Smørgrav 			    timeout_ms) >= 0) {
507511b41d2SMark Murray 				/* Successful connection. */
508c322fe35SKris Kennaway 				memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
509511b41d2SMark Murray 				break;
510511b41d2SMark Murray 			} else {
51147dd1d1bSDag-Erling Smørgrav 				oerrno = errno;
512f388f5efSDag-Erling Smørgrav 				debug("connect to address %s port %s: %s",
513f388f5efSDag-Erling Smørgrav 				    ntop, strport, strerror(errno));
514511b41d2SMark Murray 				close(sock);
515333ee039SDag-Erling Smørgrav 				sock = -1;
51647dd1d1bSDag-Erling Smørgrav 				errno = oerrno;
517511b41d2SMark Murray 			}
518511b41d2SMark Murray 		}
519333ee039SDag-Erling Smørgrav 		if (sock != -1)
520511b41d2SMark Murray 			break;	/* Successful connection. */
521511b41d2SMark Murray 	}
522511b41d2SMark Murray 
523511b41d2SMark Murray 	/* Return failure if we didn't get a successful connection. */
524333ee039SDag-Erling Smørgrav 	if (sock == -1) {
525aa49c926SDag-Erling Smørgrav 		error("ssh: connect to host %s port %s: %s",
52647dd1d1bSDag-Erling Smørgrav 		    host, strport, errno == 0 ? "failure" : strerror(errno));
52747dd1d1bSDag-Erling Smørgrav 		return -1;
528f388f5efSDag-Erling Smørgrav 	}
529511b41d2SMark Murray 
530511b41d2SMark Murray 	debug("Connection established.");
531511b41d2SMark Murray 
5321ec0d754SDag-Erling Smørgrav 	/* Set SO_KEEPALIVE if requested. */
533d4af9e69SDag-Erling Smørgrav 	if (want_keepalive &&
534ca3176e7SBrian Feldman 	    setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
53519261079SEd Maste 	    sizeof(on)) == -1)
536ca3176e7SBrian Feldman 		error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
537ca3176e7SBrian Feldman 
538511b41d2SMark Murray 	/* Set the connection. */
5394f52dfbbSDag-Erling Smørgrav 	if (ssh_packet_set_connection(ssh, sock, sock) == NULL)
5404f52dfbbSDag-Erling Smørgrav 		return -1; /* ssh_packet_set_connection logs error */
541511b41d2SMark Murray 
542af12a3e7SDag-Erling Smørgrav 	return 0;
543511b41d2SMark Murray }
544511b41d2SMark Murray 
545f7167e0eSDag-Erling Smørgrav int
ssh_connect(struct ssh * ssh,const char * host,const char * host_arg,struct addrinfo * addrs,struct sockaddr_storage * hostaddr,u_short port,int connection_attempts,int * timeout_ms,int want_keepalive)54619261079SEd Maste ssh_connect(struct ssh *ssh, const char *host, const char *host_arg,
54719261079SEd Maste     struct addrinfo *addrs, struct sockaddr_storage *hostaddr, u_short port,
548190cef3dSDag-Erling Smørgrav     int connection_attempts, int *timeout_ms, int want_keepalive)
549f7167e0eSDag-Erling Smørgrav {
55019261079SEd Maste 	int in, out;
55119261079SEd Maste 
552f7167e0eSDag-Erling Smørgrav 	if (options.proxy_command == NULL) {
5534f52dfbbSDag-Erling Smørgrav 		return ssh_connect_direct(ssh, host, addrs, hostaddr, port,
55419261079SEd Maste 		    connection_attempts, timeout_ms, want_keepalive);
555f7167e0eSDag-Erling Smørgrav 	} else if (strcmp(options.proxy_command, "-") == 0) {
55619261079SEd Maste 		if ((in = dup(STDIN_FILENO)) == -1 ||
55719261079SEd Maste 		    (out = dup(STDOUT_FILENO)) == -1) {
55819261079SEd Maste 			if (in >= 0)
55919261079SEd Maste 				close(in);
56019261079SEd Maste 			error_f("dup() in/out failed");
56119261079SEd Maste 			return -1; /* ssh_packet_set_connection logs error */
56219261079SEd Maste 		}
56319261079SEd Maste 		if ((ssh_packet_set_connection(ssh, in, out)) == NULL)
5644f52dfbbSDag-Erling Smørgrav 			return -1; /* ssh_packet_set_connection logs error */
5654f52dfbbSDag-Erling Smørgrav 		return 0;
566f7167e0eSDag-Erling Smørgrav 	} else if (options.proxy_use_fdpass) {
56719261079SEd Maste 		return ssh_proxy_fdpass_connect(ssh, host, host_arg, port,
568f7167e0eSDag-Erling Smørgrav 		    options.proxy_command);
569f7167e0eSDag-Erling Smørgrav 	}
57019261079SEd Maste 	return ssh_proxy_connect(ssh, host, host_arg, port,
57119261079SEd Maste 	    options.proxy_command);
572511b41d2SMark Murray }
573511b41d2SMark Murray 
574ca3176e7SBrian Feldman /* defaults to 'no' */
575af12a3e7SDag-Erling Smørgrav static int
confirm(const char * prompt,const char * fingerprint)57619261079SEd Maste confirm(const char *prompt, const char *fingerprint)
577511b41d2SMark Murray {
578af12a3e7SDag-Erling Smørgrav 	const char *msg, *again = "Please type 'yes' or 'no': ";
57919261079SEd Maste 	const char *again_fp = "Please type 'yes', 'no' or the fingerprint: ";
58019261079SEd Maste 	char *p, *cp;
581af12a3e7SDag-Erling Smørgrav 	int ret = -1;
582511b41d2SMark Murray 
583ca3176e7SBrian Feldman 	if (options.batch_mode)
584ca3176e7SBrian Feldman 		return 0;
58519261079SEd Maste 	for (msg = prompt;;msg = fingerprint ? again_fp : again) {
58619261079SEd Maste 		cp = p = read_passphrase(msg, RP_ECHO);
58747dd1d1bSDag-Erling Smørgrav 		if (p == NULL)
58847dd1d1bSDag-Erling Smørgrav 			return 0;
58919261079SEd Maste 		p += strspn(p, " \t"); /* skip leading whitespace */
59019261079SEd Maste 		p[strcspn(p, " \t\n")] = '\0'; /* remove trailing whitespace */
59147dd1d1bSDag-Erling Smørgrav 		if (p[0] == '\0' || strcasecmp(p, "no") == 0)
592af12a3e7SDag-Erling Smørgrav 			ret = 0;
59319261079SEd Maste 		else if (strcasecmp(p, "yes") == 0 || (fingerprint != NULL &&
59419261079SEd Maste 		    strcmp(p, fingerprint) == 0))
595af12a3e7SDag-Erling Smørgrav 			ret = 1;
59619261079SEd Maste 		free(cp);
597af12a3e7SDag-Erling Smørgrav 		if (ret != -1)
598af12a3e7SDag-Erling Smørgrav 			return ret;
599511b41d2SMark Murray 	}
600511b41d2SMark Murray }
601511b41d2SMark Murray 
602b15c8340SDag-Erling Smørgrav static int
sockaddr_is_local(struct sockaddr * hostaddr)6034a421b63SDag-Erling Smørgrav sockaddr_is_local(struct sockaddr *hostaddr)
6044a421b63SDag-Erling Smørgrav {
6054a421b63SDag-Erling Smørgrav 	switch (hostaddr->sa_family) {
6064a421b63SDag-Erling Smørgrav 	case AF_INET:
6074a421b63SDag-Erling Smørgrav 		return (ntohl(((struct sockaddr_in *)hostaddr)->
6084a421b63SDag-Erling Smørgrav 		    sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
6094a421b63SDag-Erling Smørgrav 	case AF_INET6:
6104a421b63SDag-Erling Smørgrav 		return IN6_IS_ADDR_LOOPBACK(
6114a421b63SDag-Erling Smørgrav 		    &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
6124a421b63SDag-Erling Smørgrav 	default:
6134a421b63SDag-Erling Smørgrav 		return 0;
6144a421b63SDag-Erling Smørgrav 	}
6154a421b63SDag-Erling Smørgrav }
6164a421b63SDag-Erling Smørgrav 
6174a421b63SDag-Erling Smørgrav /*
6184a421b63SDag-Erling Smørgrav  * Prepare the hostname and ip address strings that are used to lookup
6194a421b63SDag-Erling Smørgrav  * host keys in known_hosts files. These may have a port number appended.
6204a421b63SDag-Erling Smørgrav  */
6214a421b63SDag-Erling Smørgrav void
get_hostfile_hostname_ipaddr(char * hostname,struct sockaddr * hostaddr,u_short port,char ** hostfile_hostname,char ** hostfile_ipaddr)6224a421b63SDag-Erling Smørgrav get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr,
6234a421b63SDag-Erling Smørgrav     u_short port, char **hostfile_hostname, char **hostfile_ipaddr)
6244a421b63SDag-Erling Smørgrav {
6254a421b63SDag-Erling Smørgrav 	char ntop[NI_MAXHOST];
6264a421b63SDag-Erling Smørgrav 	socklen_t addrlen;
6274a421b63SDag-Erling Smørgrav 
6284a421b63SDag-Erling Smørgrav 	switch (hostaddr == NULL ? -1 : hostaddr->sa_family) {
6294a421b63SDag-Erling Smørgrav 	case -1:
6304a421b63SDag-Erling Smørgrav 		addrlen = 0;
6314a421b63SDag-Erling Smørgrav 		break;
6324a421b63SDag-Erling Smørgrav 	case AF_INET:
6334a421b63SDag-Erling Smørgrav 		addrlen = sizeof(struct sockaddr_in);
6344a421b63SDag-Erling Smørgrav 		break;
6354a421b63SDag-Erling Smørgrav 	case AF_INET6:
6364a421b63SDag-Erling Smørgrav 		addrlen = sizeof(struct sockaddr_in6);
6374a421b63SDag-Erling Smørgrav 		break;
6384a421b63SDag-Erling Smørgrav 	default:
6394a421b63SDag-Erling Smørgrav 		addrlen = sizeof(struct sockaddr);
6404a421b63SDag-Erling Smørgrav 		break;
6414a421b63SDag-Erling Smørgrav 	}
6424a421b63SDag-Erling Smørgrav 
6434a421b63SDag-Erling Smørgrav 	/*
6444a421b63SDag-Erling Smørgrav 	 * We don't have the remote ip-address for connections
6454a421b63SDag-Erling Smørgrav 	 * using a proxy command
6464a421b63SDag-Erling Smørgrav 	 */
6474a421b63SDag-Erling Smørgrav 	if (hostfile_ipaddr != NULL) {
6484a421b63SDag-Erling Smørgrav 		if (options.proxy_command == NULL) {
6494a421b63SDag-Erling Smørgrav 			if (getnameinfo(hostaddr, addrlen,
6504a421b63SDag-Erling Smørgrav 			    ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0)
65119261079SEd Maste 				fatal_f("getnameinfo failed");
6524a421b63SDag-Erling Smørgrav 			*hostfile_ipaddr = put_host_port(ntop, port);
6534a421b63SDag-Erling Smørgrav 		} else {
6544a421b63SDag-Erling Smørgrav 			*hostfile_ipaddr = xstrdup("<no hostip for proxy "
6554a421b63SDag-Erling Smørgrav 			    "command>");
6564a421b63SDag-Erling Smørgrav 		}
6574a421b63SDag-Erling Smørgrav 	}
6584a421b63SDag-Erling Smørgrav 
6594a421b63SDag-Erling Smørgrav 	/*
6604a421b63SDag-Erling Smørgrav 	 * Allow the user to record the key under a different name or
6614a421b63SDag-Erling Smørgrav 	 * differentiate a non-standard port.  This is useful for ssh
6624a421b63SDag-Erling Smørgrav 	 * tunneling over forwarded connections or if you run multiple
6634a421b63SDag-Erling Smørgrav 	 * sshd's on different ports on the same machine.
6644a421b63SDag-Erling Smørgrav 	 */
6654a421b63SDag-Erling Smørgrav 	if (hostfile_hostname != NULL) {
6664a421b63SDag-Erling Smørgrav 		if (options.host_key_alias != NULL) {
6674a421b63SDag-Erling Smørgrav 			*hostfile_hostname = xstrdup(options.host_key_alias);
6684a421b63SDag-Erling Smørgrav 			debug("using hostkeyalias: %s", *hostfile_hostname);
6694a421b63SDag-Erling Smørgrav 		} else {
6704a421b63SDag-Erling Smørgrav 			*hostfile_hostname = put_host_port(hostname, port);
6714a421b63SDag-Erling Smørgrav 		}
6724a421b63SDag-Erling Smørgrav 	}
6734a421b63SDag-Erling Smørgrav }
6744a421b63SDag-Erling Smørgrav 
67519261079SEd Maste /* returns non-zero if path appears in hostfiles, or 0 if not. */
67619261079SEd Maste static int
path_in_hostfiles(const char * path,char ** hostfiles,u_int num_hostfiles)67719261079SEd Maste path_in_hostfiles(const char *path, char **hostfiles, u_int num_hostfiles)
67819261079SEd Maste {
67919261079SEd Maste 	u_int i;
68019261079SEd Maste 
68119261079SEd Maste 	for (i = 0; i < num_hostfiles; i++) {
68219261079SEd Maste 		if (strcmp(path, hostfiles[i]) == 0)
68319261079SEd Maste 			return 1;
68419261079SEd Maste 	}
68519261079SEd Maste 	return 0;
68619261079SEd Maste }
68719261079SEd Maste 
68819261079SEd Maste struct find_by_key_ctx {
68919261079SEd Maste 	const char *host, *ip;
69019261079SEd Maste 	const struct sshkey *key;
69119261079SEd Maste 	char **names;
69219261079SEd Maste 	u_int nnames;
69319261079SEd Maste };
69419261079SEd Maste 
69519261079SEd Maste /* Try to replace home directory prefix (per $HOME) with a ~/ sequence */
69619261079SEd Maste static char *
try_tilde_unexpand(const char * path)69719261079SEd Maste try_tilde_unexpand(const char *path)
69819261079SEd Maste {
69919261079SEd Maste 	char *home, *ret = NULL;
70019261079SEd Maste 	size_t l;
70119261079SEd Maste 
70219261079SEd Maste 	if (*path != '/')
70319261079SEd Maste 		return xstrdup(path);
70419261079SEd Maste 	if ((home = getenv("HOME")) == NULL || (l = strlen(home)) == 0)
70519261079SEd Maste 		return xstrdup(path);
70619261079SEd Maste 	if (strncmp(path, home, l) != 0)
70719261079SEd Maste 		return xstrdup(path);
70819261079SEd Maste 	/*
70919261079SEd Maste 	 * ensure we have matched on a path boundary: either the $HOME that
71019261079SEd Maste 	 * we just compared ends with a '/' or the next character of the path
71119261079SEd Maste 	 * must be a '/'.
71219261079SEd Maste 	 */
71319261079SEd Maste 	if (home[l - 1] != '/' && path[l] != '/')
71419261079SEd Maste 		return xstrdup(path);
71519261079SEd Maste 	if (path[l] == '/')
71619261079SEd Maste 		l++;
71719261079SEd Maste 	xasprintf(&ret, "~/%s", path + l);
71819261079SEd Maste 	return ret;
71919261079SEd Maste }
72019261079SEd Maste 
721*0fdf8faeSEd Maste /*
722*0fdf8faeSEd Maste  * Returns non-zero if the key is accepted by HostkeyAlgorithms.
723*0fdf8faeSEd Maste  * Made slightly less trivial by the multiple RSA signature algorithm names.
724*0fdf8faeSEd Maste  */
725*0fdf8faeSEd Maste int
hostkey_accepted_by_hostkeyalgs(const struct sshkey * key)726*0fdf8faeSEd Maste hostkey_accepted_by_hostkeyalgs(const struct sshkey *key)
727*0fdf8faeSEd Maste {
728*0fdf8faeSEd Maste 	const char *ktype = sshkey_ssh_name(key);
729*0fdf8faeSEd Maste 	const char *hostkeyalgs = options.hostkeyalgorithms;
730*0fdf8faeSEd Maste 
731*0fdf8faeSEd Maste 	if (key->type == KEY_UNSPEC)
732*0fdf8faeSEd Maste 		return 0;
733*0fdf8faeSEd Maste 	if (key->type == KEY_RSA &&
734*0fdf8faeSEd Maste 	    (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
735*0fdf8faeSEd Maste 	    match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
736*0fdf8faeSEd Maste 		return 1;
737*0fdf8faeSEd Maste 	if (key->type == KEY_RSA_CERT &&
738*0fdf8faeSEd Maste 	    (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com", hostkeyalgs, 0) == 1 ||
739*0fdf8faeSEd Maste 	    match_pattern_list("rsa-sha2-256-cert-v01@openssh.com", hostkeyalgs, 0) == 1))
740*0fdf8faeSEd Maste 		return 1;
741*0fdf8faeSEd Maste 	return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
742*0fdf8faeSEd Maste }
743*0fdf8faeSEd Maste 
74419261079SEd Maste static int
hostkeys_find_by_key_cb(struct hostkey_foreach_line * l,void * _ctx)74519261079SEd Maste hostkeys_find_by_key_cb(struct hostkey_foreach_line *l, void *_ctx)
74619261079SEd Maste {
74719261079SEd Maste 	struct find_by_key_ctx *ctx = (struct find_by_key_ctx *)_ctx;
74819261079SEd Maste 	char *path;
74919261079SEd Maste 
75019261079SEd Maste 	/* we are looking for keys with names that *do not* match */
75119261079SEd Maste 	if ((l->match & HKF_MATCH_HOST) != 0)
75219261079SEd Maste 		return 0;
75319261079SEd Maste 	/* not interested in marker lines */
75419261079SEd Maste 	if (l->marker != MRK_NONE)
75519261079SEd Maste 		return 0;
75619261079SEd Maste 	/* we are only interested in exact key matches */
75719261079SEd Maste 	if (l->key == NULL || !sshkey_equal(ctx->key, l->key))
75819261079SEd Maste 		return 0;
75919261079SEd Maste 	path = try_tilde_unexpand(l->path);
76019261079SEd Maste 	debug_f("found matching key in %s:%lu", path, l->linenum);
76119261079SEd Maste 	ctx->names = xrecallocarray(ctx->names,
76219261079SEd Maste 	    ctx->nnames, ctx->nnames + 1, sizeof(*ctx->names));
76319261079SEd Maste 	xasprintf(&ctx->names[ctx->nnames], "%s:%lu: %s", path, l->linenum,
76419261079SEd Maste 	    strncmp(l->hosts, HASH_MAGIC, strlen(HASH_MAGIC)) == 0 ?
76519261079SEd Maste 	    "[hashed name]" : l->hosts);
76619261079SEd Maste 	ctx->nnames++;
76719261079SEd Maste 	free(path);
76819261079SEd Maste 	return 0;
76919261079SEd Maste }
77019261079SEd Maste 
77119261079SEd Maste static int
hostkeys_find_by_key_hostfile(const char * file,const char * which,struct find_by_key_ctx * ctx)77219261079SEd Maste hostkeys_find_by_key_hostfile(const char *file, const char *which,
77319261079SEd Maste     struct find_by_key_ctx *ctx)
77419261079SEd Maste {
77519261079SEd Maste 	int r;
77619261079SEd Maste 
77719261079SEd Maste 	debug3_f("trying %s hostfile \"%s\"", which, file);
77819261079SEd Maste 	if ((r = hostkeys_foreach(file, hostkeys_find_by_key_cb, ctx,
77919261079SEd Maste 	    ctx->host, ctx->ip, HKF_WANT_PARSE_KEY, 0)) != 0) {
78019261079SEd Maste 		if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) {
78119261079SEd Maste 			debug_f("hostkeys file %s does not exist", file);
78219261079SEd Maste 			return 0;
78319261079SEd Maste 		}
78419261079SEd Maste 		error_fr(r, "hostkeys_foreach failed for %s", file);
78519261079SEd Maste 		return r;
78619261079SEd Maste 	}
78719261079SEd Maste 	return 0;
78819261079SEd Maste }
78919261079SEd Maste 
79019261079SEd Maste /*
79119261079SEd Maste  * Find 'key' in known hosts file(s) that do not match host/ip.
79219261079SEd Maste  * Used to display also-known-as information for previously-unseen hostkeys.
79319261079SEd Maste  */
79419261079SEd Maste static void
hostkeys_find_by_key(const char * host,const char * ip,const struct sshkey * key,char ** user_hostfiles,u_int num_user_hostfiles,char ** system_hostfiles,u_int num_system_hostfiles,char *** names,u_int * nnames)79519261079SEd Maste hostkeys_find_by_key(const char *host, const char *ip, const struct sshkey *key,
79619261079SEd Maste     char **user_hostfiles, u_int num_user_hostfiles,
79719261079SEd Maste     char **system_hostfiles, u_int num_system_hostfiles,
79819261079SEd Maste     char ***names, u_int *nnames)
79919261079SEd Maste {
80019261079SEd Maste 	struct find_by_key_ctx ctx = {0, 0, 0, 0, 0};
80119261079SEd Maste 	u_int i;
80219261079SEd Maste 
80319261079SEd Maste 	*names = NULL;
80419261079SEd Maste 	*nnames = 0;
80519261079SEd Maste 
80619261079SEd Maste 	if (key == NULL || sshkey_is_cert(key))
80719261079SEd Maste 		return;
80819261079SEd Maste 
80919261079SEd Maste 	ctx.host = host;
81019261079SEd Maste 	ctx.ip = ip;
81119261079SEd Maste 	ctx.key = key;
81219261079SEd Maste 
81319261079SEd Maste 	for (i = 0; i < num_user_hostfiles; i++) {
81419261079SEd Maste 		if (hostkeys_find_by_key_hostfile(user_hostfiles[i],
81519261079SEd Maste 		    "user", &ctx) != 0)
81619261079SEd Maste 			goto fail;
81719261079SEd Maste 	}
81819261079SEd Maste 	for (i = 0; i < num_system_hostfiles; i++) {
81919261079SEd Maste 		if (hostkeys_find_by_key_hostfile(system_hostfiles[i],
82019261079SEd Maste 		    "system", &ctx) != 0)
82119261079SEd Maste 			goto fail;
82219261079SEd Maste 	}
82319261079SEd Maste 	/* success */
82419261079SEd Maste 	*names = ctx.names;
82519261079SEd Maste 	*nnames = ctx.nnames;
82619261079SEd Maste 	ctx.names = NULL;
82719261079SEd Maste 	ctx.nnames = 0;
82819261079SEd Maste 	return;
82919261079SEd Maste  fail:
83019261079SEd Maste 	for (i = 0; i < ctx.nnames; i++)
83119261079SEd Maste 		free(ctx.names[i]);
83219261079SEd Maste 	free(ctx.names);
83319261079SEd Maste }
83419261079SEd Maste 
83519261079SEd Maste #define MAX_OTHER_NAMES	8 /* Maximum number of names to list */
83619261079SEd Maste static char *
other_hostkeys_message(const char * host,const char * ip,const struct sshkey * key,char ** user_hostfiles,u_int num_user_hostfiles,char ** system_hostfiles,u_int num_system_hostfiles)83719261079SEd Maste other_hostkeys_message(const char *host, const char *ip,
83819261079SEd Maste     const struct sshkey *key,
83919261079SEd Maste     char **user_hostfiles, u_int num_user_hostfiles,
84019261079SEd Maste     char **system_hostfiles, u_int num_system_hostfiles)
84119261079SEd Maste {
84219261079SEd Maste 	char *ret = NULL, **othernames = NULL;
84319261079SEd Maste 	u_int i, n, num_othernames = 0;
84419261079SEd Maste 
84519261079SEd Maste 	hostkeys_find_by_key(host, ip, key,
84619261079SEd Maste 	    user_hostfiles, num_user_hostfiles,
84719261079SEd Maste 	    system_hostfiles, num_system_hostfiles,
84819261079SEd Maste 	    &othernames, &num_othernames);
84919261079SEd Maste 	if (num_othernames == 0)
85038a52bd3SEd Maste 		return xstrdup("This key is not known by any other names.");
85119261079SEd Maste 
85219261079SEd Maste 	xasprintf(&ret, "This host key is known by the following other "
85319261079SEd Maste 	    "names/addresses:");
85419261079SEd Maste 
85519261079SEd Maste 	n = num_othernames;
85619261079SEd Maste 	if (n > MAX_OTHER_NAMES)
85719261079SEd Maste 		n = MAX_OTHER_NAMES;
85819261079SEd Maste 	for (i = 0; i < n; i++) {
85919261079SEd Maste 		xextendf(&ret, "\n", "    %s", othernames[i]);
86019261079SEd Maste 	}
86119261079SEd Maste 	if (n < num_othernames) {
86219261079SEd Maste 		xextendf(&ret, "\n", "    (%d additional names omitted)",
86319261079SEd Maste 		    num_othernames - n);
86419261079SEd Maste 	}
86519261079SEd Maste 	for (i = 0; i < num_othernames; i++)
86619261079SEd Maste 		free(othernames[i]);
86719261079SEd Maste 	free(othernames);
86819261079SEd Maste 	return ret;
86919261079SEd Maste }
87019261079SEd Maste 
87119261079SEd Maste void
load_hostkeys_command(struct hostkeys * hostkeys,const char * command_template,const char * invocation,const struct ssh_conn_info * cinfo,const struct sshkey * host_key,const char * hostfile_hostname)87219261079SEd Maste load_hostkeys_command(struct hostkeys *hostkeys, const char *command_template,
87319261079SEd Maste     const char *invocation, const struct ssh_conn_info *cinfo,
87419261079SEd Maste     const struct sshkey *host_key, const char *hostfile_hostname)
87519261079SEd Maste {
87619261079SEd Maste 	int r, i, ac = 0;
87719261079SEd Maste 	char *key_fp = NULL, *keytext = NULL, *tmp;
87819261079SEd Maste 	char *command = NULL, *tag = NULL, **av = NULL;
87919261079SEd Maste 	FILE *f = NULL;
88019261079SEd Maste 	pid_t pid;
88119261079SEd Maste 	void (*osigchld)(int);
88219261079SEd Maste 
88319261079SEd Maste 	xasprintf(&tag, "KnownHostsCommand-%s", invocation);
88419261079SEd Maste 
88519261079SEd Maste 	if (host_key != NULL) {
88619261079SEd Maste 		if ((key_fp = sshkey_fingerprint(host_key,
88719261079SEd Maste 		    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
88819261079SEd Maste 			fatal_f("sshkey_fingerprint failed");
88919261079SEd Maste 		if ((r = sshkey_to_base64(host_key, &keytext)) != 0)
89019261079SEd Maste 			fatal_fr(r, "sshkey_to_base64 failed");
89119261079SEd Maste 	}
89219261079SEd Maste 	/*
89319261079SEd Maste 	 * NB. all returns later this function should go via "out" to
89419261079SEd Maste 	 * ensure the original SIGCHLD handler is restored properly.
89519261079SEd Maste 	 */
89619261079SEd Maste 	osigchld = ssh_signal(SIGCHLD, SIG_DFL);
89719261079SEd Maste 
89819261079SEd Maste 	/* Turn the command into an argument vector */
89919261079SEd Maste 	if (argv_split(command_template, &ac, &av, 0) != 0) {
90019261079SEd Maste 		error("%s \"%s\" contains invalid quotes", tag,
90119261079SEd Maste 		    command_template);
90219261079SEd Maste 		goto out;
90319261079SEd Maste 	}
90419261079SEd Maste 	if (ac == 0) {
90519261079SEd Maste 		error("%s \"%s\" yielded no arguments", tag,
90619261079SEd Maste 		    command_template);
90719261079SEd Maste 		goto out;
90819261079SEd Maste 	}
90919261079SEd Maste 	for (i = 1; i < ac; i++) {
91019261079SEd Maste 		tmp = percent_dollar_expand(av[i],
91119261079SEd Maste 		    DEFAULT_CLIENT_PERCENT_EXPAND_ARGS(cinfo),
91219261079SEd Maste 		    "H", hostfile_hostname,
91319261079SEd Maste 		    "I", invocation,
91419261079SEd Maste 		    "t", host_key == NULL ? "NONE" : sshkey_ssh_name(host_key),
91519261079SEd Maste 		    "f", key_fp == NULL ? "NONE" : key_fp,
91619261079SEd Maste 		    "K", keytext == NULL ? "NONE" : keytext,
91719261079SEd Maste 		    (char *)NULL);
91819261079SEd Maste 		if (tmp == NULL)
91919261079SEd Maste 			fatal_f("percent_expand failed");
92019261079SEd Maste 		free(av[i]);
92119261079SEd Maste 		av[i] = tmp;
92219261079SEd Maste 	}
92319261079SEd Maste 	/* Prepare a printable command for logs, etc. */
92419261079SEd Maste 	command = argv_assemble(ac, av);
92519261079SEd Maste 
92619261079SEd Maste 	if ((pid = subprocess(tag, command, ac, av, &f,
92719261079SEd Maste 	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_UNSAFE_PATH|
92819261079SEd Maste 	    SSH_SUBPROCESS_PRESERVE_ENV, NULL, NULL, NULL)) == 0)
92919261079SEd Maste 		goto out;
93019261079SEd Maste 
93119261079SEd Maste 	load_hostkeys_file(hostkeys, hostfile_hostname, tag, f, 1);
93219261079SEd Maste 
93319261079SEd Maste 	if (exited_cleanly(pid, tag, command, 0) != 0)
93419261079SEd Maste 		fatal("KnownHostsCommand failed");
93519261079SEd Maste 
93619261079SEd Maste  out:
93719261079SEd Maste 	if (f != NULL)
93819261079SEd Maste 		fclose(f);
93919261079SEd Maste 	ssh_signal(SIGCHLD, osigchld);
94019261079SEd Maste 	for (i = 0; i < ac; i++)
94119261079SEd Maste 		free(av[i]);
94219261079SEd Maste 	free(av);
94319261079SEd Maste 	free(tag);
94419261079SEd Maste 	free(command);
94519261079SEd Maste 	free(key_fp);
94619261079SEd Maste 	free(keytext);
94719261079SEd Maste }
94819261079SEd Maste 
949e8aafc91SKris Kennaway /*
950af12a3e7SDag-Erling Smørgrav  * check whether the supplied host key is valid, return -1 if the key
951e146993eSDag-Erling Smørgrav  * is not valid. user_hostfile[0] will not be updated if 'readonly' is true.
952e8aafc91SKris Kennaway  */
953333ee039SDag-Erling Smørgrav #define RDRW	0
954333ee039SDag-Erling Smørgrav #define RDONLY	1
955333ee039SDag-Erling Smørgrav #define ROQUIET	2
956af12a3e7SDag-Erling Smørgrav static int
check_host_key(char * hostname,const struct ssh_conn_info * cinfo,struct sockaddr * hostaddr,u_short port,struct sshkey * host_key,int readonly,int clobber_port,char ** user_hostfiles,u_int num_user_hostfiles,char ** system_hostfiles,u_int num_system_hostfiles,const char * hostfile_command)95719261079SEd Maste check_host_key(char *hostname, const struct ssh_conn_info *cinfo,
95819261079SEd Maste     struct sockaddr *hostaddr, u_short port,
95919261079SEd Maste     struct sshkey *host_key, int readonly, int clobber_port,
960e146993eSDag-Erling Smørgrav     char **user_hostfiles, u_int num_user_hostfiles,
96119261079SEd Maste     char **system_hostfiles, u_int num_system_hostfiles,
96219261079SEd Maste     const char *hostfile_command)
963511b41d2SMark Murray {
96419261079SEd Maste 	HostStatus host_status = -1, ip_status = -1;
9654f52dfbbSDag-Erling Smørgrav 	struct sshkey *raw_key = NULL;
966e146993eSDag-Erling Smørgrav 	char *ip = NULL, *host = NULL;
967e146993eSDag-Erling Smørgrav 	char hostline[1000], *hostp, *fp, *ra;
968af12a3e7SDag-Erling Smørgrav 	char msg[1024];
9692e828220SEd Maste 	const char *type, *fail_reason = NULL;
97019261079SEd Maste 	const struct hostkey_entry *host_found = NULL, *ip_found = NULL;
97119261079SEd Maste 	int len, cancelled_forwarding = 0, confirmed;
972e146993eSDag-Erling Smørgrav 	int local = sockaddr_is_local(hostaddr);
9734f52dfbbSDag-Erling Smørgrav 	int r, want_cert = sshkey_is_cert(host_key), host_ip_differ = 0;
974bc5531deSDag-Erling Smørgrav 	int hostkey_trusted = 0; /* Known or explicitly accepted by user */
975e146993eSDag-Erling Smørgrav 	struct hostkeys *host_hostkeys, *ip_hostkeys;
976e146993eSDag-Erling Smørgrav 	u_int i;
977511b41d2SMark Murray 
978e8aafc91SKris Kennaway 	/*
979e8aafc91SKris Kennaway 	 * Force accepting of the host key for loopback/localhost. The
980e8aafc91SKris Kennaway 	 * problem is that if the home directory is NFS-mounted to multiple
981e8aafc91SKris Kennaway 	 * machines, localhost will refer to a different machine in each of
982e8aafc91SKris Kennaway 	 * them, and the user will get bogus HOST_CHANGED warnings.  This
983e8aafc91SKris Kennaway 	 * essentially disables host authentication for localhost; however,
984e8aafc91SKris Kennaway 	 * this is probably not a real problem.
985e8aafc91SKris Kennaway 	 */
986af12a3e7SDag-Erling Smørgrav 	if (options.no_host_authentication_for_localhost == 1 && local &&
987af12a3e7SDag-Erling Smørgrav 	    options.host_key_alias == NULL) {
988ca3176e7SBrian Feldman 		debug("Forcing accepting of host key for "
989ca3176e7SBrian Feldman 		    "loopback/localhost.");
99019261079SEd Maste 		options.update_hostkeys = 0;
991af12a3e7SDag-Erling Smørgrav 		return 0;
992511b41d2SMark Murray 	}
993511b41d2SMark Murray 
994e8aafc91SKris Kennaway 	/*
9952e828220SEd Maste 	 * Don't ever try to write an invalid name to a known hosts file.
9962e828220SEd Maste 	 * Note: do this before get_hostfile_hostname_ipaddr() to catch
9972e828220SEd Maste 	 * '[' or ']' in the name before they are added.
9982e828220SEd Maste 	 */
9992e828220SEd Maste 	if (strcspn(hostname, "@?*#[]|'\'\"\\") != strlen(hostname)) {
10002e828220SEd Maste 		debug_f("invalid hostname \"%s\"; will not record: %s",
10012e828220SEd Maste 		    hostname, fail_reason);
10022e828220SEd Maste 		readonly = RDONLY;
10032e828220SEd Maste 	}
10042e828220SEd Maste 
10052e828220SEd Maste 	/*
10064a421b63SDag-Erling Smørgrav 	 * Prepare the hostname and address strings used for hostkey lookup.
10074a421b63SDag-Erling Smørgrav 	 * In some cases, these will have a port number appended.
1008e8aafc91SKris Kennaway 	 */
100919261079SEd Maste 	get_hostfile_hostname_ipaddr(hostname, hostaddr,
101019261079SEd Maste 	    clobber_port ? 0 : port, &host, &ip);
1011d4af9e69SDag-Erling Smørgrav 
1012ca3176e7SBrian Feldman 	/*
1013ca3176e7SBrian Feldman 	 * Turn off check_host_ip if the connection is to localhost, via proxy
1014ca3176e7SBrian Feldman 	 * command or if we don't have a hostname to compare with
1015ca3176e7SBrian Feldman 	 */
1016333ee039SDag-Erling Smørgrav 	if (options.check_host_ip && (local ||
1017333ee039SDag-Erling Smørgrav 	    strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
1018ca3176e7SBrian Feldman 		options.check_host_ip = 0;
1019ca3176e7SBrian Feldman 
10204a421b63SDag-Erling Smørgrav 	host_hostkeys = init_hostkeys();
1021e146993eSDag-Erling Smørgrav 	for (i = 0; i < num_user_hostfiles; i++)
102219261079SEd Maste 		load_hostkeys(host_hostkeys, host, user_hostfiles[i], 0);
1023e146993eSDag-Erling Smørgrav 	for (i = 0; i < num_system_hostfiles; i++)
102419261079SEd Maste 		load_hostkeys(host_hostkeys, host, system_hostfiles[i], 0);
102519261079SEd Maste 	if (hostfile_command != NULL && !clobber_port) {
102619261079SEd Maste 		load_hostkeys_command(host_hostkeys, hostfile_command,
102719261079SEd Maste 		    "HOSTNAME", cinfo, host_key, host);
102819261079SEd Maste 	}
10294a421b63SDag-Erling Smørgrav 
10304a421b63SDag-Erling Smørgrav 	ip_hostkeys = NULL;
10314a421b63SDag-Erling Smørgrav 	if (!want_cert && options.check_host_ip) {
10324a421b63SDag-Erling Smørgrav 		ip_hostkeys = init_hostkeys();
1033e146993eSDag-Erling Smørgrav 		for (i = 0; i < num_user_hostfiles; i++)
103419261079SEd Maste 			load_hostkeys(ip_hostkeys, ip, user_hostfiles[i], 0);
1035e146993eSDag-Erling Smørgrav 		for (i = 0; i < num_system_hostfiles; i++)
103619261079SEd Maste 			load_hostkeys(ip_hostkeys, ip, system_hostfiles[i], 0);
103719261079SEd Maste 		if (hostfile_command != NULL && !clobber_port) {
103819261079SEd Maste 			load_hostkeys_command(ip_hostkeys, hostfile_command,
103919261079SEd Maste 			    "ADDRESS", cinfo, host_key, ip);
104019261079SEd Maste 		}
1041e8aafc91SKris Kennaway 	}
1042e8aafc91SKris Kennaway 
1043b15c8340SDag-Erling Smørgrav  retry:
1044*0fdf8faeSEd Maste 	if (!hostkey_accepted_by_hostkeyalgs(host_key)) {
1045*0fdf8faeSEd Maste 		error("host key %s not permitted by HostkeyAlgorithms",
1046*0fdf8faeSEd Maste 		    sshkey_ssh_name(host_key));
1047*0fdf8faeSEd Maste 		goto fail;
1048*0fdf8faeSEd Maste 	}
1049*0fdf8faeSEd Maste 
10504a421b63SDag-Erling Smørgrav 	/* Reload these as they may have changed on cert->key downgrade */
10514f52dfbbSDag-Erling Smørgrav 	want_cert = sshkey_is_cert(host_key);
10524f52dfbbSDag-Erling Smørgrav 	type = sshkey_type(host_key);
1053b15c8340SDag-Erling Smørgrav 
1054e8aafc91SKris Kennaway 	/*
1055b74df5b2SDag-Erling Smørgrav 	 * Check if the host key is present in the user's list of known
1056e8aafc91SKris Kennaway 	 * hosts or in the systemwide list.
1057e8aafc91SKris Kennaway 	 */
10584a421b63SDag-Erling Smørgrav 	host_status = check_key_in_hostkeys(host_hostkeys, host_key,
10594a421b63SDag-Erling Smørgrav 	    &host_found);
10604a421b63SDag-Erling Smørgrav 
1061e8aafc91SKris Kennaway 	/*
106219261079SEd Maste 	 * If there are no hostfiles, or if the hostkey was found via
106319261079SEd Maste 	 * KnownHostsCommand, then don't try to touch the disk.
106419261079SEd Maste 	 */
106519261079SEd Maste 	if (!readonly && (num_user_hostfiles == 0 ||
106619261079SEd Maste 	    (host_found != NULL && host_found->note != 0)))
106719261079SEd Maste 		readonly = RDONLY;
106819261079SEd Maste 
106919261079SEd Maste 	/*
1070e8aafc91SKris Kennaway 	 * Also perform check for the ip address, skip the check if we are
1071b15c8340SDag-Erling Smørgrav 	 * localhost, looking for a certificate, or the hostname was an ip
1072b15c8340SDag-Erling Smørgrav 	 * address to begin with.
1073e8aafc91SKris Kennaway 	 */
10744a421b63SDag-Erling Smørgrav 	if (!want_cert && ip_hostkeys != NULL) {
10754a421b63SDag-Erling Smørgrav 		ip_status = check_key_in_hostkeys(ip_hostkeys, host_key,
10764a421b63SDag-Erling Smørgrav 		    &ip_found);
1077e8aafc91SKris Kennaway 		if (host_status == HOST_CHANGED &&
10784a421b63SDag-Erling Smørgrav 		    (ip_status != HOST_CHANGED ||
10794a421b63SDag-Erling Smørgrav 		    (ip_found != NULL &&
10804f52dfbbSDag-Erling Smørgrav 		    !sshkey_equal(ip_found->key, host_found->key))))
1081e8aafc91SKris Kennaway 			host_ip_differ = 1;
1082e8aafc91SKris Kennaway 	} else
1083e8aafc91SKris Kennaway 		ip_status = host_status;
1084e8aafc91SKris Kennaway 
1085e8aafc91SKris Kennaway 	switch (host_status) {
1086e8aafc91SKris Kennaway 	case HOST_OK:
1087e8aafc91SKris Kennaway 		/* The host is known and the key matches. */
1088b15c8340SDag-Erling Smørgrav 		debug("Host '%.200s' is known and matches the %s host %s.",
1089b15c8340SDag-Erling Smørgrav 		    host, type, want_cert ? "certificate" : "key");
10904a421b63SDag-Erling Smørgrav 		debug("Found %s in %s:%lu", want_cert ? "CA key" : "key",
10914a421b63SDag-Erling Smørgrav 		    host_found->file, host_found->line);
109219261079SEd Maste 		if (want_cert) {
109319261079SEd Maste 			if (sshkey_cert_check_host(host_key,
109419261079SEd Maste 			    options.host_key_alias == NULL ?
109519261079SEd Maste 			    hostname : options.host_key_alias, 0,
109619261079SEd Maste 			    options.ca_sign_algorithms, &fail_reason) != 0) {
109719261079SEd Maste 				error("%s", fail_reason);
1098b15c8340SDag-Erling Smørgrav 				goto fail;
109919261079SEd Maste 			}
110019261079SEd Maste 			/*
110119261079SEd Maste 			 * Do not attempt hostkey update if a certificate was
110219261079SEd Maste 			 * successfully matched.
110319261079SEd Maste 			 */
110419261079SEd Maste 			if (options.update_hostkeys != 0) {
110519261079SEd Maste 				options.update_hostkeys = 0;
110619261079SEd Maste 				debug3_f("certificate host key in use; "
110719261079SEd Maste 				    "disabling UpdateHostkeys");
110819261079SEd Maste 			}
110919261079SEd Maste 		}
111019261079SEd Maste 		/* Turn off UpdateHostkeys if key was in system known_hosts */
111119261079SEd Maste 		if (options.update_hostkeys != 0 &&
111219261079SEd Maste 		    (path_in_hostfiles(host_found->file,
111319261079SEd Maste 		    system_hostfiles, num_system_hostfiles) ||
111419261079SEd Maste 		    (ip_status == HOST_OK && ip_found != NULL &&
111519261079SEd Maste 		    path_in_hostfiles(ip_found->file,
111619261079SEd Maste 		    system_hostfiles, num_system_hostfiles)))) {
111719261079SEd Maste 			options.update_hostkeys = 0;
111819261079SEd Maste 			debug3_f("host key found in GlobalKnownHostsFile; "
111919261079SEd Maste 			    "disabling UpdateHostkeys");
112019261079SEd Maste 		}
112119261079SEd Maste 		if (options.update_hostkeys != 0 && host_found->note) {
112219261079SEd Maste 			options.update_hostkeys = 0;
112319261079SEd Maste 			debug3_f("host key found via KnownHostsCommand; "
112419261079SEd Maste 			    "disabling UpdateHostkeys");
112519261079SEd Maste 		}
1126ca3176e7SBrian Feldman 		if (options.check_host_ip && ip_status == HOST_NEW) {
1127b15c8340SDag-Erling Smørgrav 			if (readonly || want_cert)
1128cf2b5f3bSDag-Erling Smørgrav 				logit("%s host key for IP address "
1129af12a3e7SDag-Erling Smørgrav 				    "'%.128s' not in list of known hosts.",
1130e8aafc91SKris Kennaway 				    type, ip);
1131e146993eSDag-Erling Smørgrav 			else if (!add_host_to_hostfile(user_hostfiles[0], ip,
1132aa49c926SDag-Erling Smørgrav 			    host_key, options.hash_known_hosts))
1133cf2b5f3bSDag-Erling Smørgrav 				logit("Failed to add the %s host key for IP "
1134af12a3e7SDag-Erling Smørgrav 				    "address '%.128s' to the list of known "
1135557f75e5SDag-Erling Smørgrav 				    "hosts (%.500s).", type, ip,
1136e146993eSDag-Erling Smørgrav 				    user_hostfiles[0]);
1137af12a3e7SDag-Erling Smørgrav 			else
1138cf2b5f3bSDag-Erling Smørgrav 				logit("Warning: Permanently added the %s host "
1139af12a3e7SDag-Erling Smørgrav 				    "key for IP address '%.128s' to the list "
1140af12a3e7SDag-Erling Smørgrav 				    "of known hosts.", type, ip);
1141d4af9e69SDag-Erling Smørgrav 		} else if (options.visual_host_key) {
1142bc5531deSDag-Erling Smørgrav 			fp = sshkey_fingerprint(host_key,
1143bc5531deSDag-Erling Smørgrav 			    options.fingerprint_hash, SSH_FP_DEFAULT);
1144bc5531deSDag-Erling Smørgrav 			ra = sshkey_fingerprint(host_key,
1145bc5531deSDag-Erling Smørgrav 			    options.fingerprint_hash, SSH_FP_RANDOMART);
1146bc5531deSDag-Erling Smørgrav 			if (fp == NULL || ra == NULL)
114719261079SEd Maste 				fatal_f("sshkey_fingerprint failed");
1148acc1a9efSDag-Erling Smørgrav 			logit("Host key fingerprint is %s\n%s", fp, ra);
1149e4a9863fSDag-Erling Smørgrav 			free(ra);
1150e4a9863fSDag-Erling Smørgrav 			free(fp);
1151e8aafc91SKris Kennaway 		}
1152bc5531deSDag-Erling Smørgrav 		hostkey_trusted = 1;
1153e8aafc91SKris Kennaway 		break;
1154e8aafc91SKris Kennaway 	case HOST_NEW:
1155333ee039SDag-Erling Smørgrav 		if (options.host_key_alias == NULL && port != 0 &&
115619261079SEd Maste 		    port != SSH_DEFAULT_PORT && !clobber_port) {
1157333ee039SDag-Erling Smørgrav 			debug("checking without port identifier");
115819261079SEd Maste 			if (check_host_key(hostname, cinfo, hostaddr, 0,
115919261079SEd Maste 			    host_key, ROQUIET, 1,
116019261079SEd Maste 			    user_hostfiles, num_user_hostfiles,
116119261079SEd Maste 			    system_hostfiles, num_system_hostfiles,
116219261079SEd Maste 			    hostfile_command) == 0) {
1163333ee039SDag-Erling Smørgrav 				debug("found matching key w/out port");
1164333ee039SDag-Erling Smørgrav 				break;
1165333ee039SDag-Erling Smørgrav 			}
1166333ee039SDag-Erling Smørgrav 		}
1167b15c8340SDag-Erling Smørgrav 		if (readonly || want_cert)
1168af12a3e7SDag-Erling Smørgrav 			goto fail;
1169e8aafc91SKris Kennaway 		/* The host is new. */
11704f52dfbbSDag-Erling Smørgrav 		if (options.strict_host_key_checking ==
11714f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_YES) {
1172af12a3e7SDag-Erling Smørgrav 			/*
1173af12a3e7SDag-Erling Smørgrav 			 * User has requested strict host key checking.  We
1174af12a3e7SDag-Erling Smørgrav 			 * will not add the host key automatically.  The only
1175af12a3e7SDag-Erling Smørgrav 			 * alternative left is to abort.
1176af12a3e7SDag-Erling Smørgrav 			 */
1177af12a3e7SDag-Erling Smørgrav 			error("No %s host key is known for %.200s and you "
1178af12a3e7SDag-Erling Smørgrav 			    "have requested strict checking.", type, host);
1179af12a3e7SDag-Erling Smørgrav 			goto fail;
11804f52dfbbSDag-Erling Smørgrav 		} else if (options.strict_host_key_checking ==
11814f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_ASK) {
118219261079SEd Maste 			char *msg1 = NULL, *msg2 = NULL;
1183cf2b5f3bSDag-Erling Smørgrav 
118419261079SEd Maste 			xasprintf(&msg1, "The authenticity of host "
118519261079SEd Maste 			    "'%.200s (%s)' can't be established", host, ip);
118619261079SEd Maste 
118719261079SEd Maste 			if (show_other_keys(host_hostkeys, host_key)) {
118819261079SEd Maste 				xextendf(&msg1, "\n", "but keys of different "
118919261079SEd Maste 				    "type are already known for this host.");
119019261079SEd Maste 			} else
119119261079SEd Maste 				xextendf(&msg1, "", ".");
119219261079SEd Maste 
1193bc5531deSDag-Erling Smørgrav 			fp = sshkey_fingerprint(host_key,
1194bc5531deSDag-Erling Smørgrav 			    options.fingerprint_hash, SSH_FP_DEFAULT);
1195bc5531deSDag-Erling Smørgrav 			ra = sshkey_fingerprint(host_key,
1196bc5531deSDag-Erling Smørgrav 			    options.fingerprint_hash, SSH_FP_RANDOMART);
1197bc5531deSDag-Erling Smørgrav 			if (fp == NULL || ra == NULL)
119819261079SEd Maste 				fatal_f("sshkey_fingerprint failed");
119919261079SEd Maste 			xextendf(&msg1, "\n", "%s key fingerprint is %s.",
120019261079SEd Maste 			    type, fp);
120119261079SEd Maste 			if (options.visual_host_key)
120219261079SEd Maste 				xextendf(&msg1, "\n", "%s", ra);
1203cf2b5f3bSDag-Erling Smørgrav 			if (options.verify_host_key_dns) {
120419261079SEd Maste 				xextendf(&msg1, "\n",
120519261079SEd Maste 				    "%s host key fingerprint found in DNS.",
120619261079SEd Maste 				    matching_host_key_dns ?
120719261079SEd Maste 				    "Matching" : "No matching");
1208cf2b5f3bSDag-Erling Smørgrav 			}
120919261079SEd Maste 			/* msg2 informs for other names matching this key */
121019261079SEd Maste 			if ((msg2 = other_hostkeys_message(host, ip, host_key,
121119261079SEd Maste 			    user_hostfiles, num_user_hostfiles,
121219261079SEd Maste 			    system_hostfiles, num_system_hostfiles)) != NULL)
121319261079SEd Maste 				xextendf(&msg1, "\n", "%s", msg2);
121419261079SEd Maste 
121519261079SEd Maste 			xextendf(&msg1, "\n",
1216af12a3e7SDag-Erling Smørgrav 			    "Are you sure you want to continue connecting "
121719261079SEd Maste 			    "(yes/no/[fingerprint])? ");
121819261079SEd Maste 
121919261079SEd Maste 			confirmed = confirm(msg1, fp);
1220e4a9863fSDag-Erling Smørgrav 			free(ra);
1221e4a9863fSDag-Erling Smørgrav 			free(fp);
122219261079SEd Maste 			free(msg1);
122319261079SEd Maste 			free(msg2);
122419261079SEd Maste 			if (!confirmed)
1225af12a3e7SDag-Erling Smørgrav 				goto fail;
1226bc5531deSDag-Erling Smørgrav 			hostkey_trusted = 1; /* user explicitly confirmed */
1227e8aafc91SKris Kennaway 		}
1228af12a3e7SDag-Erling Smørgrav 		/*
12294f52dfbbSDag-Erling Smørgrav 		 * If in "new" or "off" strict mode, add the key automatically
12304f52dfbbSDag-Erling Smørgrav 		 * to the local known_hosts file.
1231af12a3e7SDag-Erling Smørgrav 		 */
1232aa49c926SDag-Erling Smørgrav 		if (options.check_host_ip && ip_status == HOST_NEW) {
12334a421b63SDag-Erling Smørgrav 			snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
1234aa49c926SDag-Erling Smørgrav 			hostp = hostline;
1235aa49c926SDag-Erling Smørgrav 			if (options.hash_known_hosts) {
1236aa49c926SDag-Erling Smørgrav 				/* Add hash of host and IP separately */
1237e146993eSDag-Erling Smørgrav 				r = add_host_to_hostfile(user_hostfiles[0],
1238e146993eSDag-Erling Smørgrav 				    host, host_key, options.hash_known_hosts) &&
1239e146993eSDag-Erling Smørgrav 				    add_host_to_hostfile(user_hostfiles[0], ip,
1240aa49c926SDag-Erling Smørgrav 				    host_key, options.hash_known_hosts);
1241aa49c926SDag-Erling Smørgrav 			} else {
1242aa49c926SDag-Erling Smørgrav 				/* Add unhashed "host,ip" */
1243e146993eSDag-Erling Smørgrav 				r = add_host_to_hostfile(user_hostfiles[0],
1244aa49c926SDag-Erling Smørgrav 				    hostline, host_key,
1245aa49c926SDag-Erling Smørgrav 				    options.hash_known_hosts);
1246aa49c926SDag-Erling Smørgrav 			}
1247aa49c926SDag-Erling Smørgrav 		} else {
1248e146993eSDag-Erling Smørgrav 			r = add_host_to_hostfile(user_hostfiles[0], host,
1249e146993eSDag-Erling Smørgrav 			    host_key, options.hash_known_hosts);
1250aa49c926SDag-Erling Smørgrav 			hostp = host;
1251aa49c926SDag-Erling Smørgrav 		}
1252aa49c926SDag-Erling Smørgrav 
1253aa49c926SDag-Erling Smørgrav 		if (!r)
1254cf2b5f3bSDag-Erling Smørgrav 			logit("Failed to add the host to the list of known "
1255e146993eSDag-Erling Smørgrav 			    "hosts (%.500s).", user_hostfiles[0]);
1256e8aafc91SKris Kennaway 		else
1257cf2b5f3bSDag-Erling Smørgrav 			logit("Warning: Permanently added '%.200s' (%s) to the "
1258af12a3e7SDag-Erling Smørgrav 			    "list of known hosts.", hostp, type);
1259e8aafc91SKris Kennaway 		break;
1260b15c8340SDag-Erling Smørgrav 	case HOST_REVOKED:
1261b15c8340SDag-Erling Smørgrav 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1262b15c8340SDag-Erling Smørgrav 		error("@       WARNING: REVOKED HOST KEY DETECTED!               @");
1263b15c8340SDag-Erling Smørgrav 		error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1264b15c8340SDag-Erling Smørgrav 		error("The %s host key for %s is marked as revoked.", type, host);
1265b15c8340SDag-Erling Smørgrav 		error("This could mean that a stolen key is being used to");
1266b15c8340SDag-Erling Smørgrav 		error("impersonate this host.");
1267b15c8340SDag-Erling Smørgrav 
1268b15c8340SDag-Erling Smørgrav 		/*
1269b15c8340SDag-Erling Smørgrav 		 * If strict host key checking is in use, the user will have
1270b15c8340SDag-Erling Smørgrav 		 * to edit the key manually and we can only abort.
1271b15c8340SDag-Erling Smørgrav 		 */
12724f52dfbbSDag-Erling Smørgrav 		if (options.strict_host_key_checking !=
12734f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_OFF) {
1274b15c8340SDag-Erling Smørgrav 			error("%s host key for %.200s was revoked and you have "
1275b15c8340SDag-Erling Smørgrav 			    "requested strict checking.", type, host);
1276b15c8340SDag-Erling Smørgrav 			goto fail;
1277b15c8340SDag-Erling Smørgrav 		}
1278b15c8340SDag-Erling Smørgrav 		goto continue_unsafe;
1279b15c8340SDag-Erling Smørgrav 
1280e8aafc91SKris Kennaway 	case HOST_CHANGED:
1281b15c8340SDag-Erling Smørgrav 		if (want_cert) {
1282b15c8340SDag-Erling Smørgrav 			/*
1283b15c8340SDag-Erling Smørgrav 			 * This is only a debug() since it is valid to have
1284b15c8340SDag-Erling Smørgrav 			 * CAs with wildcard DNS matches that don't match
1285b15c8340SDag-Erling Smørgrav 			 * all hosts that one might visit.
1286b15c8340SDag-Erling Smørgrav 			 */
1287b15c8340SDag-Erling Smørgrav 			debug("Host certificate authority does not "
12884a421b63SDag-Erling Smørgrav 			    "match %s in %s:%lu", CA_MARKER,
12894a421b63SDag-Erling Smørgrav 			    host_found->file, host_found->line);
1290b15c8340SDag-Erling Smørgrav 			goto fail;
1291b15c8340SDag-Erling Smørgrav 		}
1292333ee039SDag-Erling Smørgrav 		if (readonly == ROQUIET)
1293333ee039SDag-Erling Smørgrav 			goto fail;
1294e8aafc91SKris Kennaway 		if (options.check_host_ip && host_ip_differ) {
129521e764dfSDag-Erling Smørgrav 			char *key_msg;
1296e8aafc91SKris Kennaway 			if (ip_status == HOST_NEW)
129721e764dfSDag-Erling Smørgrav 				key_msg = "is unknown";
1298e8aafc91SKris Kennaway 			else if (ip_status == HOST_OK)
129921e764dfSDag-Erling Smørgrav 				key_msg = "is unchanged";
1300e8aafc91SKris Kennaway 			else
130121e764dfSDag-Erling Smørgrav 				key_msg = "has a different value";
1302e8aafc91SKris Kennaway 			error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1303e8aafc91SKris Kennaway 			error("@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @");
1304e8aafc91SKris Kennaway 			error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1305e8aafc91SKris Kennaway 			error("The %s host key for %s has changed,", type, host);
1306d4af9e69SDag-Erling Smørgrav 			error("and the key for the corresponding IP address %s", ip);
130721e764dfSDag-Erling Smørgrav 			error("%s. This could either mean that", key_msg);
1308e8aafc91SKris Kennaway 			error("DNS SPOOFING is happening or the IP address for the host");
1309ca3176e7SBrian Feldman 			error("and its host key have changed at the same time.");
1310ca3176e7SBrian Feldman 			if (ip_status != HOST_NEW)
13114a421b63SDag-Erling Smørgrav 				error("Offending key for IP in %s:%lu",
13124a421b63SDag-Erling Smørgrav 				    ip_found->file, ip_found->line);
1313e8aafc91SKris Kennaway 		}
1314e8aafc91SKris Kennaway 		/* The host key has changed. */
13151ec0d754SDag-Erling Smørgrav 		warn_changed_key(host_key);
1316f374ba41SEd Maste 		if (num_user_hostfiles > 0 || num_system_hostfiles > 0) {
1317f374ba41SEd Maste 			error("Add correct host key in %.100s to get rid "
1318f374ba41SEd Maste 			    "of this message.", num_user_hostfiles > 0 ?
1319f374ba41SEd Maste 			    user_hostfiles[0] : system_hostfiles[0]);
1320f374ba41SEd Maste 		}
13214f52dfbbSDag-Erling Smørgrav 		error("Offending %s key in %s:%lu",
13224f52dfbbSDag-Erling Smørgrav 		    sshkey_type(host_found->key),
13234a421b63SDag-Erling Smørgrav 		    host_found->file, host_found->line);
1324e8aafc91SKris Kennaway 
1325e8aafc91SKris Kennaway 		/*
1326e8aafc91SKris Kennaway 		 * If strict host key checking is in use, the user will have
1327e8aafc91SKris Kennaway 		 * to edit the key manually and we can only abort.
1328e8aafc91SKris Kennaway 		 */
13294f52dfbbSDag-Erling Smørgrav 		if (options.strict_host_key_checking !=
13304f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_OFF) {
133119261079SEd Maste 			error("Host key for %.200s has changed and you have "
133219261079SEd Maste 			    "requested strict checking.", host);
1333af12a3e7SDag-Erling Smørgrav 			goto fail;
1334af12a3e7SDag-Erling Smørgrav 		}
1335e8aafc91SKris Kennaway 
1336b15c8340SDag-Erling Smørgrav  continue_unsafe:
1337e8aafc91SKris Kennaway 		/*
1338e8aafc91SKris Kennaway 		 * If strict host key checking has not been requested, allow
1339cf2b5f3bSDag-Erling Smørgrav 		 * the connection but without MITM-able authentication or
1340333ee039SDag-Erling Smørgrav 		 * forwarding.
1341e8aafc91SKris Kennaway 		 */
1342e8aafc91SKris Kennaway 		if (options.password_authentication) {
1343af12a3e7SDag-Erling Smørgrav 			error("Password authentication is disabled to avoid "
1344af12a3e7SDag-Erling Smørgrav 			    "man-in-the-middle attacks.");
1345e8aafc91SKris Kennaway 			options.password_authentication = 0;
1346d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1347e8aafc91SKris Kennaway 		}
1348cf2b5f3bSDag-Erling Smørgrav 		if (options.kbd_interactive_authentication) {
1349cf2b5f3bSDag-Erling Smørgrav 			error("Keyboard-interactive authentication is disabled"
1350cf2b5f3bSDag-Erling Smørgrav 			    " to avoid man-in-the-middle attacks.");
1351cf2b5f3bSDag-Erling Smørgrav 			options.kbd_interactive_authentication = 0;
1352d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1353cf2b5f3bSDag-Erling Smørgrav 		}
1354e8aafc91SKris Kennaway 		if (options.forward_agent) {
1355af12a3e7SDag-Erling Smørgrav 			error("Agent forwarding is disabled to avoid "
1356af12a3e7SDag-Erling Smørgrav 			    "man-in-the-middle attacks.");
1357e8aafc91SKris Kennaway 			options.forward_agent = 0;
1358d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1359e8aafc91SKris Kennaway 		}
1360ca3176e7SBrian Feldman 		if (options.forward_x11) {
1361af12a3e7SDag-Erling Smørgrav 			error("X11 forwarding is disabled to avoid "
1362af12a3e7SDag-Erling Smørgrav 			    "man-in-the-middle attacks.");
1363ca3176e7SBrian Feldman 			options.forward_x11 = 0;
1364d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1365ca3176e7SBrian Feldman 		}
1366af12a3e7SDag-Erling Smørgrav 		if (options.num_local_forwards > 0 ||
1367af12a3e7SDag-Erling Smørgrav 		    options.num_remote_forwards > 0) {
1368af12a3e7SDag-Erling Smørgrav 			error("Port forwarding is disabled to avoid "
1369af12a3e7SDag-Erling Smørgrav 			    "man-in-the-middle attacks.");
1370af12a3e7SDag-Erling Smørgrav 			options.num_local_forwards =
1371af12a3e7SDag-Erling Smørgrav 			    options.num_remote_forwards = 0;
1372d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1373ca3176e7SBrian Feldman 		}
1374333ee039SDag-Erling Smørgrav 		if (options.tun_open != SSH_TUNMODE_NO) {
1375333ee039SDag-Erling Smørgrav 			error("Tunnel forwarding is disabled to avoid "
1376333ee039SDag-Erling Smørgrav 			    "man-in-the-middle attacks.");
1377333ee039SDag-Erling Smørgrav 			options.tun_open = SSH_TUNMODE_NO;
1378d4af9e69SDag-Erling Smørgrav 			cancelled_forwarding = 1;
1379333ee039SDag-Erling Smørgrav 		}
138019261079SEd Maste 		if (options.update_hostkeys != 0) {
138119261079SEd Maste 			error("UpdateHostkeys is disabled because the host "
138219261079SEd Maste 			    "key is not trusted.");
138319261079SEd Maste 			options.update_hostkeys = 0;
138419261079SEd Maste 		}
1385d4af9e69SDag-Erling Smørgrav 		if (options.exit_on_forward_failure && cancelled_forwarding)
1386d4af9e69SDag-Erling Smørgrav 			fatal("Error: forwarding disabled due to host key "
1387d4af9e69SDag-Erling Smørgrav 			    "check failure");
1388d4af9e69SDag-Erling Smørgrav 
1389e8aafc91SKris Kennaway 		/*
1390e8aafc91SKris Kennaway 		 * XXX Should permit the user to change to use the new id.
1391e8aafc91SKris Kennaway 		 * This could be done by converting the host key to an
1392e8aafc91SKris Kennaway 		 * identifying sentence, tell that the host identifies itself
139319261079SEd Maste 		 * by that sentence, and ask the user if they wish to
1394e8aafc91SKris Kennaway 		 * accept the authentication.
1395e8aafc91SKris Kennaway 		 */
1396e8aafc91SKris Kennaway 		break;
1397f388f5efSDag-Erling Smørgrav 	case HOST_FOUND:
1398f388f5efSDag-Erling Smørgrav 		fatal("internal error");
1399f388f5efSDag-Erling Smørgrav 		break;
1400e8aafc91SKris Kennaway 	}
1401ca3176e7SBrian Feldman 
1402ca3176e7SBrian Feldman 	if (options.check_host_ip && host_status != HOST_CHANGED &&
1403ca3176e7SBrian Feldman 	    ip_status == HOST_CHANGED) {
1404af12a3e7SDag-Erling Smørgrav 		snprintf(msg, sizeof(msg),
1405af12a3e7SDag-Erling Smørgrav 		    "Warning: the %s host key for '%.200s' "
1406af12a3e7SDag-Erling Smørgrav 		    "differs from the key for the IP address '%.128s'"
14074a421b63SDag-Erling Smørgrav 		    "\nOffending key for IP in %s:%lu",
14084a421b63SDag-Erling Smørgrav 		    type, host, ip, ip_found->file, ip_found->line);
1409af12a3e7SDag-Erling Smørgrav 		if (host_status == HOST_OK) {
1410af12a3e7SDag-Erling Smørgrav 			len = strlen(msg);
1411af12a3e7SDag-Erling Smørgrav 			snprintf(msg + len, sizeof(msg) - len,
14124a421b63SDag-Erling Smørgrav 			    "\nMatching host key in %s:%lu",
14134a421b63SDag-Erling Smørgrav 			    host_found->file, host_found->line);
1414af12a3e7SDag-Erling Smørgrav 		}
14154f52dfbbSDag-Erling Smørgrav 		if (options.strict_host_key_checking ==
14164f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_ASK) {
1417af12a3e7SDag-Erling Smørgrav 			strlcat(msg, "\nAre you sure you want "
1418af12a3e7SDag-Erling Smørgrav 			    "to continue connecting (yes/no)? ", sizeof(msg));
141919261079SEd Maste 			if (!confirm(msg, NULL))
1420af12a3e7SDag-Erling Smørgrav 				goto fail;
14214f52dfbbSDag-Erling Smørgrav 		} else if (options.strict_host_key_checking !=
14224f52dfbbSDag-Erling Smørgrav 		    SSH_STRICT_HOSTKEY_OFF) {
14234f52dfbbSDag-Erling Smørgrav 			logit("%s", msg);
14244f52dfbbSDag-Erling Smørgrav 			error("Exiting, you have requested strict checking.");
14254f52dfbbSDag-Erling Smørgrav 			goto fail;
1426af12a3e7SDag-Erling Smørgrav 		} else {
1427cf2b5f3bSDag-Erling Smørgrav 			logit("%s", msg);
1428ca3176e7SBrian Feldman 		}
1429ca3176e7SBrian Feldman 	}
1430ca3176e7SBrian Feldman 
1431bc5531deSDag-Erling Smørgrav 	if (!hostkey_trusted && options.update_hostkeys) {
143219261079SEd Maste 		debug_f("hostkey not known or explicitly trusted: "
143319261079SEd Maste 		    "disabling UpdateHostkeys");
1434bc5531deSDag-Erling Smørgrav 		options.update_hostkeys = 0;
1435bc5531deSDag-Erling Smørgrav 	}
1436bc5531deSDag-Erling Smørgrav 
1437e4a9863fSDag-Erling Smørgrav 	free(ip);
1438e4a9863fSDag-Erling Smørgrav 	free(host);
14394a421b63SDag-Erling Smørgrav 	if (host_hostkeys != NULL)
14404a421b63SDag-Erling Smørgrav 		free_hostkeys(host_hostkeys);
14414a421b63SDag-Erling Smørgrav 	if (ip_hostkeys != NULL)
14424a421b63SDag-Erling Smørgrav 		free_hostkeys(ip_hostkeys);
1443af12a3e7SDag-Erling Smørgrav 	return 0;
1444af12a3e7SDag-Erling Smørgrav 
1445af12a3e7SDag-Erling Smørgrav fail:
1446b15c8340SDag-Erling Smørgrav 	if (want_cert && host_status != HOST_REVOKED) {
1447b15c8340SDag-Erling Smørgrav 		/*
1448b15c8340SDag-Erling Smørgrav 		 * No matching certificate. Downgrade cert to raw key and
1449b15c8340SDag-Erling Smørgrav 		 * search normally.
1450b15c8340SDag-Erling Smørgrav 		 */
1451b15c8340SDag-Erling Smørgrav 		debug("No matching CA found. Retry with plain key");
14524f52dfbbSDag-Erling Smørgrav 		if ((r = sshkey_from_private(host_key, &raw_key)) != 0)
145319261079SEd Maste 			fatal_fr(r, "decode key");
14544f52dfbbSDag-Erling Smørgrav 		if ((r = sshkey_drop_cert(raw_key)) != 0)
145519261079SEd Maste 			fatal_r(r, "Couldn't drop certificate");
1456b15c8340SDag-Erling Smørgrav 		host_key = raw_key;
1457b15c8340SDag-Erling Smørgrav 		goto retry;
1458b15c8340SDag-Erling Smørgrav 	}
14594f52dfbbSDag-Erling Smørgrav 	sshkey_free(raw_key);
1460e4a9863fSDag-Erling Smørgrav 	free(ip);
1461e4a9863fSDag-Erling Smørgrav 	free(host);
14624a421b63SDag-Erling Smørgrav 	if (host_hostkeys != NULL)
14634a421b63SDag-Erling Smørgrav 		free_hostkeys(host_hostkeys);
14644a421b63SDag-Erling Smørgrav 	if (ip_hostkeys != NULL)
14654a421b63SDag-Erling Smørgrav 		free_hostkeys(ip_hostkeys);
1466af12a3e7SDag-Erling Smørgrav 	return -1;
1467e8aafc91SKris Kennaway }
1468511b41d2SMark Murray 
1469cf2b5f3bSDag-Erling Smørgrav /* returns 0 if key verifies or -1 if key does NOT verify */
1470fe5fd017SMark Murray int
verify_host_key(char * host,struct sockaddr * hostaddr,struct sshkey * host_key,const struct ssh_conn_info * cinfo)147119261079SEd Maste verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key,
147219261079SEd Maste     const struct ssh_conn_info *cinfo)
1473fe5fd017SMark Murray {
1474acc1a9efSDag-Erling Smørgrav 	u_int i;
1475a0ee8cc6SDag-Erling Smørgrav 	int r = -1, flags = 0;
1476acc1a9efSDag-Erling Smørgrav 	char valid[64], *fp = NULL, *cafp = NULL;
1477bc5531deSDag-Erling Smørgrav 	struct sshkey *plain = NULL;
14784a421b63SDag-Erling Smørgrav 
1479bc5531deSDag-Erling Smørgrav 	if ((fp = sshkey_fingerprint(host_key,
1480bc5531deSDag-Erling Smørgrav 	    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
148119261079SEd Maste 		error_fr(r, "fingerprint host key");
1482bc5531deSDag-Erling Smørgrav 		r = -1;
1483bc5531deSDag-Erling Smørgrav 		goto out;
1484bc5531deSDag-Erling Smørgrav 	}
1485fe5fd017SMark Murray 
1486acc1a9efSDag-Erling Smørgrav 	if (sshkey_is_cert(host_key)) {
1487acc1a9efSDag-Erling Smørgrav 		if ((cafp = sshkey_fingerprint(host_key->cert->signature_key,
1488acc1a9efSDag-Erling Smørgrav 		    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
148919261079SEd Maste 			error_fr(r, "fingerprint CA key");
1490acc1a9efSDag-Erling Smørgrav 			r = -1;
1491acc1a9efSDag-Erling Smørgrav 			goto out;
1492acc1a9efSDag-Erling Smørgrav 		}
1493acc1a9efSDag-Erling Smørgrav 		sshkey_format_cert_validity(host_key->cert,
1494acc1a9efSDag-Erling Smørgrav 		    valid, sizeof(valid));
1495acc1a9efSDag-Erling Smørgrav 		debug("Server host certificate: %s %s, serial %llu "
1496acc1a9efSDag-Erling Smørgrav 		    "ID \"%s\" CA %s %s valid %s",
1497acc1a9efSDag-Erling Smørgrav 		    sshkey_ssh_name(host_key), fp,
1498acc1a9efSDag-Erling Smørgrav 		    (unsigned long long)host_key->cert->serial,
1499acc1a9efSDag-Erling Smørgrav 		    host_key->cert->key_id,
1500acc1a9efSDag-Erling Smørgrav 		    sshkey_ssh_name(host_key->cert->signature_key), cafp,
1501acc1a9efSDag-Erling Smørgrav 		    valid);
1502acc1a9efSDag-Erling Smørgrav 		for (i = 0; i < host_key->cert->nprincipals; i++) {
1503acc1a9efSDag-Erling Smørgrav 			debug2("Server host certificate hostname: %s",
1504acc1a9efSDag-Erling Smørgrav 			    host_key->cert->principals[i]);
1505acc1a9efSDag-Erling Smørgrav 		}
1506acc1a9efSDag-Erling Smørgrav 	} else {
15074f52dfbbSDag-Erling Smørgrav 		debug("Server host key: %s %s", sshkey_ssh_name(host_key), fp);
1508acc1a9efSDag-Erling Smørgrav 	}
1509bc5531deSDag-Erling Smørgrav 
1510bc5531deSDag-Erling Smørgrav 	if (sshkey_equal(previous_host_key, host_key)) {
151119261079SEd Maste 		debug2_f("server host key %s %s matches cached key",
151219261079SEd Maste 		    sshkey_type(host_key), fp);
1513bc5531deSDag-Erling Smørgrav 		r = 0;
1514bc5531deSDag-Erling Smørgrav 		goto out;
1515bc5531deSDag-Erling Smørgrav 	}
1516bc5531deSDag-Erling Smørgrav 
1517bc5531deSDag-Erling Smørgrav 	/* Check in RevokedHostKeys file if specified */
1518bc5531deSDag-Erling Smørgrav 	if (options.revoked_host_keys != NULL) {
1519bc5531deSDag-Erling Smørgrav 		r = sshkey_check_revoked(host_key, options.revoked_host_keys);
1520bc5531deSDag-Erling Smørgrav 		switch (r) {
1521bc5531deSDag-Erling Smørgrav 		case 0:
1522bc5531deSDag-Erling Smørgrav 			break; /* not revoked */
1523bc5531deSDag-Erling Smørgrav 		case SSH_ERR_KEY_REVOKED:
1524bc5531deSDag-Erling Smørgrav 			error("Host key %s %s revoked by file %s",
1525bc5531deSDag-Erling Smørgrav 			    sshkey_type(host_key), fp,
1526bc5531deSDag-Erling Smørgrav 			    options.revoked_host_keys);
1527bc5531deSDag-Erling Smørgrav 			r = -1;
1528bc5531deSDag-Erling Smørgrav 			goto out;
1529bc5531deSDag-Erling Smørgrav 		default:
153019261079SEd Maste 			error_r(r, "Error checking host key %s %s in "
153119261079SEd Maste 			    "revoked keys file %s", sshkey_type(host_key),
153219261079SEd Maste 			    fp, options.revoked_host_keys);
1533bc5531deSDag-Erling Smørgrav 			r = -1;
1534bc5531deSDag-Erling Smørgrav 			goto out;
1535bc5531deSDag-Erling Smørgrav 		}
1536a0ee8cc6SDag-Erling Smørgrav 	}
1537a0ee8cc6SDag-Erling Smørgrav 
15383a0b9b77SXin LI 	if (options.verify_host_key_dns) {
15393a0b9b77SXin LI 		/*
15403a0b9b77SXin LI 		 * XXX certs are not yet supported for DNS, so downgrade
15413a0b9b77SXin LI 		 * them and try the plain key.
15423a0b9b77SXin LI 		 */
1543bc5531deSDag-Erling Smørgrav 		if ((r = sshkey_from_private(host_key, &plain)) != 0)
1544bc5531deSDag-Erling Smørgrav 			goto out;
1545bc5531deSDag-Erling Smørgrav 		if (sshkey_is_cert(plain))
1546bc5531deSDag-Erling Smørgrav 			sshkey_drop_cert(plain);
15473a0b9b77SXin LI 		if (verify_host_key_dns(host, hostaddr, plain, &flags) == 0) {
15481ec0d754SDag-Erling Smørgrav 			if (flags & DNS_VERIFY_FOUND) {
15491ec0d754SDag-Erling Smørgrav 				if (options.verify_host_key_dns == 1 &&
15501ec0d754SDag-Erling Smørgrav 				    flags & DNS_VERIFY_MATCH &&
15513a0b9b77SXin LI 				    flags & DNS_VERIFY_SECURE) {
1552a0ee8cc6SDag-Erling Smørgrav 					r = 0;
1553bc5531deSDag-Erling Smørgrav 					goto out;
15543a0b9b77SXin LI 				}
15551ec0d754SDag-Erling Smørgrav 				if (flags & DNS_VERIFY_MATCH) {
15561ec0d754SDag-Erling Smørgrav 					matching_host_key_dns = 1;
15571ec0d754SDag-Erling Smørgrav 				} else {
15583a0b9b77SXin LI 					warn_changed_key(plain);
15593a0b9b77SXin LI 					error("Update the SSHFP RR in DNS "
15603a0b9b77SXin LI 					    "with the new host key to get rid "
15613a0b9b77SXin LI 					    "of this message.");
1562cf2b5f3bSDag-Erling Smørgrav 				}
1563cf2b5f3bSDag-Erling Smørgrav 			}
15641ec0d754SDag-Erling Smørgrav 		}
15653a0b9b77SXin LI 	}
156619261079SEd Maste 	r = check_host_key(host, cinfo, hostaddr, options.port, host_key,
156719261079SEd Maste 	    RDRW, 0, options.user_hostfiles, options.num_user_hostfiles,
156819261079SEd Maste 	    options.system_hostfiles, options.num_system_hostfiles,
156919261079SEd Maste 	    options.known_hosts_command);
1570a0ee8cc6SDag-Erling Smørgrav 
1571bc5531deSDag-Erling Smørgrav out:
1572bc5531deSDag-Erling Smørgrav 	sshkey_free(plain);
1573bc5531deSDag-Erling Smørgrav 	free(fp);
1574acc1a9efSDag-Erling Smørgrav 	free(cafp);
1575a0ee8cc6SDag-Erling Smørgrav 	if (r == 0 && host_key != NULL) {
15764f52dfbbSDag-Erling Smørgrav 		sshkey_free(previous_host_key);
15774f52dfbbSDag-Erling Smørgrav 		r = sshkey_from_private(host_key, &previous_host_key);
1578a0ee8cc6SDag-Erling Smørgrav 	}
1579a0ee8cc6SDag-Erling Smørgrav 
1580a0ee8cc6SDag-Erling Smørgrav 	return r;
1581fe5fd017SMark Murray }
1582fe5fd017SMark Murray 
1583511b41d2SMark Murray /*
1584511b41d2SMark Murray  * Starts a dialog with the server, and authenticates the current user on the
1585511b41d2SMark Murray  * server.  This does not need any extra privileges.  The basic connection
1586511b41d2SMark Murray  * to the server must already have been established before this is called.
1587511b41d2SMark Murray  * If login fails, this function prints an error and never returns.
1588511b41d2SMark Murray  * This function does not require super-user privileges.
1589511b41d2SMark Murray  */
1590511b41d2SMark Murray void
ssh_login(struct ssh * ssh,Sensitive * sensitive,const char * orighost,struct sockaddr * hostaddr,u_short port,struct passwd * pw,int timeout_ms,const struct ssh_conn_info * cinfo)159119261079SEd Maste ssh_login(struct ssh *ssh, Sensitive *sensitive, const char *orighost,
159219261079SEd Maste     struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms,
159319261079SEd Maste     const struct ssh_conn_info *cinfo)
1594511b41d2SMark Murray {
1595f7167e0eSDag-Erling Smørgrav 	char *host;
1596e8aafc91SKris Kennaway 	char *server_user, *local_user;
159719261079SEd Maste 	int r;
1598e8aafc91SKris Kennaway 
1599e8aafc91SKris Kennaway 	local_user = xstrdup(pw->pw_name);
1600e8aafc91SKris Kennaway 	server_user = options.user ? options.user : local_user;
1601511b41d2SMark Murray 
1602511b41d2SMark Murray 	/* Convert the user-supplied hostname into all lowercase. */
1603511b41d2SMark Murray 	host = xstrdup(orighost);
1604f7167e0eSDag-Erling Smørgrav 	lowercase(host);
1605511b41d2SMark Murray 
1606511b41d2SMark Murray 	/* Exchange protocol version identification strings with the server. */
1607bffe60eaSEd Maste 	if ((r = kex_exchange_identification(ssh, timeout_ms, NULL)) != 0)
160819261079SEd Maste 		sshpkt_fatal(ssh, r, "banner exchange");
1609511b41d2SMark Murray 
1610511b41d2SMark Murray 	/* Put the connection into non-blocking mode. */
161119261079SEd Maste 	ssh_packet_set_nonblocking(ssh);
1612511b41d2SMark Murray 
1613511b41d2SMark Murray 	/* key exchange */
1614511b41d2SMark Murray 	/* authenticate user */
1615557f75e5SDag-Erling Smørgrav 	debug("Authenticating to %s:%d as '%s'", host, port, server_user);
161619261079SEd Maste 	ssh_kex2(ssh, host, hostaddr, port, cinfo);
161719261079SEd Maste 	ssh_userauth2(ssh, local_user, server_user, host, sensitive);
1618e4a9863fSDag-Erling Smørgrav 	free(local_user);
161919261079SEd Maste 	free(host);
1620e0fbb1d2SBrian Feldman }
1621f388f5efSDag-Erling Smørgrav 
1622f388f5efSDag-Erling Smørgrav /* print all known host keys for a given host, but skip keys of given type */
1623f388f5efSDag-Erling Smørgrav static int
show_other_keys(struct hostkeys * hostkeys,struct sshkey * key)16244f52dfbbSDag-Erling Smørgrav show_other_keys(struct hostkeys *hostkeys, struct sshkey *key)
1625f388f5efSDag-Erling Smørgrav {
1626f7167e0eSDag-Erling Smørgrav 	int type[] = {
1627f7167e0eSDag-Erling Smørgrav 		KEY_RSA,
1628a91a2465SEd Maste #ifdef WITH_DSA
1629f7167e0eSDag-Erling Smørgrav 		KEY_DSA,
1630a91a2465SEd Maste #endif
1631f7167e0eSDag-Erling Smørgrav 		KEY_ECDSA,
1632f7167e0eSDag-Erling Smørgrav 		KEY_ED25519,
163347dd1d1bSDag-Erling Smørgrav 		KEY_XMSS,
1634f7167e0eSDag-Erling Smørgrav 		-1
1635f7167e0eSDag-Erling Smørgrav 	};
16364a421b63SDag-Erling Smørgrav 	int i, ret = 0;
16374a421b63SDag-Erling Smørgrav 	char *fp, *ra;
16384a421b63SDag-Erling Smørgrav 	const struct hostkey_entry *found;
1639f388f5efSDag-Erling Smørgrav 
1640f388f5efSDag-Erling Smørgrav 	for (i = 0; type[i] != -1; i++) {
1641f388f5efSDag-Erling Smørgrav 		if (type[i] == key->type)
1642f388f5efSDag-Erling Smørgrav 			continue;
164319261079SEd Maste 		if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i],
164419261079SEd Maste 		    -1, &found))
1645f388f5efSDag-Erling Smørgrav 			continue;
1646bc5531deSDag-Erling Smørgrav 		fp = sshkey_fingerprint(found->key,
1647bc5531deSDag-Erling Smørgrav 		    options.fingerprint_hash, SSH_FP_DEFAULT);
1648bc5531deSDag-Erling Smørgrav 		ra = sshkey_fingerprint(found->key,
1649bc5531deSDag-Erling Smørgrav 		    options.fingerprint_hash, SSH_FP_RANDOMART);
1650bc5531deSDag-Erling Smørgrav 		if (fp == NULL || ra == NULL)
165119261079SEd Maste 			fatal_f("sshkey_fingerprint fail");
16524a421b63SDag-Erling Smørgrav 		logit("WARNING: %s key found for host %s\n"
16534a421b63SDag-Erling Smørgrav 		    "in %s:%lu\n"
16544a421b63SDag-Erling Smørgrav 		    "%s key fingerprint %s.",
1655190cef3dSDag-Erling Smørgrav 		    sshkey_type(found->key),
16564a421b63SDag-Erling Smørgrav 		    found->host, found->file, found->line,
1657190cef3dSDag-Erling Smørgrav 		    sshkey_type(found->key), fp);
16584a421b63SDag-Erling Smørgrav 		if (options.visual_host_key)
16594a421b63SDag-Erling Smørgrav 			logit("%s", ra);
1660e4a9863fSDag-Erling Smørgrav 		free(ra);
1661e4a9863fSDag-Erling Smørgrav 		free(fp);
16624a421b63SDag-Erling Smørgrav 		ret = 1;
1663f388f5efSDag-Erling Smørgrav 	}
16644a421b63SDag-Erling Smørgrav 	return ret;
1665f388f5efSDag-Erling Smørgrav }
16661ec0d754SDag-Erling Smørgrav 
16671ec0d754SDag-Erling Smørgrav static void
warn_changed_key(struct sshkey * host_key)16684f52dfbbSDag-Erling Smørgrav warn_changed_key(struct sshkey *host_key)
16691ec0d754SDag-Erling Smørgrav {
16701ec0d754SDag-Erling Smørgrav 	char *fp;
16711ec0d754SDag-Erling Smørgrav 
1672bc5531deSDag-Erling Smørgrav 	fp = sshkey_fingerprint(host_key, options.fingerprint_hash,
1673bc5531deSDag-Erling Smørgrav 	    SSH_FP_DEFAULT);
1674bc5531deSDag-Erling Smørgrav 	if (fp == NULL)
167519261079SEd Maste 		fatal_f("sshkey_fingerprint fail");
16761ec0d754SDag-Erling Smørgrav 
16771ec0d754SDag-Erling Smørgrav 	error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
16781ec0d754SDag-Erling Smørgrav 	error("@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @");
16791ec0d754SDag-Erling Smørgrav 	error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
16801ec0d754SDag-Erling Smørgrav 	error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
16811ec0d754SDag-Erling Smørgrav 	error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
16824a421b63SDag-Erling Smørgrav 	error("It is also possible that a host key has just been changed.");
16831ec0d754SDag-Erling Smørgrav 	error("The fingerprint for the %s key sent by the remote host is\n%s.",
1684190cef3dSDag-Erling Smørgrav 	    sshkey_type(host_key), fp);
16851ec0d754SDag-Erling Smørgrav 	error("Please contact your system administrator.");
16861ec0d754SDag-Erling Smørgrav 
1687e4a9863fSDag-Erling Smørgrav 	free(fp);
16881ec0d754SDag-Erling Smørgrav }
1689b74df5b2SDag-Erling Smørgrav 
1690b74df5b2SDag-Erling Smørgrav /*
1691b74df5b2SDag-Erling Smørgrav  * Execute a local command
1692b74df5b2SDag-Erling Smørgrav  */
1693b74df5b2SDag-Erling Smørgrav int
ssh_local_cmd(const char * args)1694b74df5b2SDag-Erling Smørgrav ssh_local_cmd(const char *args)
1695b74df5b2SDag-Erling Smørgrav {
1696b74df5b2SDag-Erling Smørgrav 	char *shell;
1697b74df5b2SDag-Erling Smørgrav 	pid_t pid;
1698b74df5b2SDag-Erling Smørgrav 	int status;
16994a421b63SDag-Erling Smørgrav 	void (*osighand)(int);
1700b74df5b2SDag-Erling Smørgrav 
1701b74df5b2SDag-Erling Smørgrav 	if (!options.permit_local_command ||
1702b74df5b2SDag-Erling Smørgrav 	    args == NULL || !*args)
1703b74df5b2SDag-Erling Smørgrav 		return (1);
1704b74df5b2SDag-Erling Smørgrav 
17054a421b63SDag-Erling Smørgrav 	if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1706b74df5b2SDag-Erling Smørgrav 		shell = _PATH_BSHELL;
1707b74df5b2SDag-Erling Smørgrav 
170819261079SEd Maste 	osighand = ssh_signal(SIGCHLD, SIG_DFL);
1709b74df5b2SDag-Erling Smørgrav 	pid = fork();
1710b74df5b2SDag-Erling Smørgrav 	if (pid == 0) {
171119261079SEd Maste 		ssh_signal(SIGPIPE, SIG_DFL);
1712b74df5b2SDag-Erling Smørgrav 		debug3("Executing %s -c \"%s\"", shell, args);
1713b74df5b2SDag-Erling Smørgrav 		execl(shell, shell, "-c", args, (char *)NULL);
1714b74df5b2SDag-Erling Smørgrav 		error("Couldn't execute %s -c \"%s\": %s",
1715b74df5b2SDag-Erling Smørgrav 		    shell, args, strerror(errno));
1716b74df5b2SDag-Erling Smørgrav 		_exit(1);
1717b74df5b2SDag-Erling Smørgrav 	} else if (pid == -1)
1718b74df5b2SDag-Erling Smørgrav 		fatal("fork failed: %.100s", strerror(errno));
1719b74df5b2SDag-Erling Smørgrav 	while (waitpid(pid, &status, 0) == -1)
1720b74df5b2SDag-Erling Smørgrav 		if (errno != EINTR)
1721b74df5b2SDag-Erling Smørgrav 			fatal("Couldn't wait for child: %s", strerror(errno));
172219261079SEd Maste 	ssh_signal(SIGCHLD, osighand);
1723b74df5b2SDag-Erling Smørgrav 
1724b74df5b2SDag-Erling Smørgrav 	if (!WIFEXITED(status))
1725b74df5b2SDag-Erling Smørgrav 		return (1);
1726b74df5b2SDag-Erling Smørgrav 
1727b74df5b2SDag-Erling Smørgrav 	return (WEXITSTATUS(status));
1728b74df5b2SDag-Erling Smørgrav }
1729acc1a9efSDag-Erling Smørgrav 
1730acc1a9efSDag-Erling Smørgrav void
maybe_add_key_to_agent(const char * authfile,struct sshkey * private,const char * comment,const char * passphrase)173119261079SEd Maste maybe_add_key_to_agent(const char *authfile, struct sshkey *private,
173219261079SEd Maste     const char *comment, const char *passphrase)
1733acc1a9efSDag-Erling Smørgrav {
1734acc1a9efSDag-Erling Smørgrav 	int auth_sock = -1, r;
173519261079SEd Maste 	const char *skprovider = NULL;
1736acc1a9efSDag-Erling Smørgrav 
1737acc1a9efSDag-Erling Smørgrav 	if (options.add_keys_to_agent == 0)
1738acc1a9efSDag-Erling Smørgrav 		return;
1739acc1a9efSDag-Erling Smørgrav 
1740acc1a9efSDag-Erling Smørgrav 	if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
1741acc1a9efSDag-Erling Smørgrav 		debug3("no authentication agent, not adding key");
1742acc1a9efSDag-Erling Smørgrav 		return;
1743acc1a9efSDag-Erling Smørgrav 	}
1744acc1a9efSDag-Erling Smørgrav 
1745acc1a9efSDag-Erling Smørgrav 	if (options.add_keys_to_agent == 2 &&
1746acc1a9efSDag-Erling Smørgrav 	    !ask_permission("Add key %s (%s) to agent?", authfile, comment)) {
1747acc1a9efSDag-Erling Smørgrav 		debug3("user denied adding this key");
1748d93a896eSDag-Erling Smørgrav 		close(auth_sock);
1749acc1a9efSDag-Erling Smørgrav 		return;
1750acc1a9efSDag-Erling Smørgrav 	}
175119261079SEd Maste 	if (sshkey_is_sk(private))
175219261079SEd Maste 		skprovider = options.sk_provider;
175319261079SEd Maste 	if ((r = ssh_add_identity_constrained(auth_sock, private,
175419261079SEd Maste 	    comment == NULL ? authfile : comment,
175519261079SEd Maste 	    options.add_keys_to_agent_lifespan,
17561323ec57SEd Maste 	    (options.add_keys_to_agent == 3), 0, skprovider, NULL, 0)) == 0)
1757acc1a9efSDag-Erling Smørgrav 		debug("identity added to agent: %s", authfile);
1758acc1a9efSDag-Erling Smørgrav 	else
1759acc1a9efSDag-Erling Smørgrav 		debug("could not add identity to agent: %s (%d)", authfile, r);
1760d93a896eSDag-Erling Smørgrav 	close(auth_sock);
1761acc1a9efSDag-Erling Smørgrav }
1762