markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sshconnect.c,v 1.299 2018/07/09 21:03:30 markus Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Code to connect to a remote host, and to perform the client side of the |
| 7 | * login (authentication) dialog. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * |
| 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/wait.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 20 | #include <sys/stat.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 21 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_TIME_H |
| 23 | # include <sys/time.h> |
| 24 | #endif |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 25 | |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 26 | #include <net/if.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 27 | #include <netinet/in.h> |
Darren Tucker | 46aa3e0 | 2006-09-02 15:32:40 +1000 | [diff] [blame] | 28 | #include <arpa/inet.h> |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 29 | |
Damien Miller | c7b0636 | 2006-03-15 11:53:45 +1100 | [diff] [blame] | 30 | #include <ctype.h> |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 31 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 32 | #include <fcntl.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 33 | #include <netdb.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 34 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 35 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 36 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 38 | #ifdef HAVE_POLL_H |
| 39 | #include <poll.h> |
| 40 | #endif |
Damien Miller | 9c0c31d | 2010-10-12 13:30:44 +1100 | [diff] [blame] | 41 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 42 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 43 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 44 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 46 | #include <unistd.h> |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 47 | #ifdef HAVE_IFADDRS_H |
| 48 | # include <ifaddrs.h> |
| 49 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 52 | #include "key.h" |
| 53 | #include "hostfile.h" |
| 54 | #include "ssh.h" |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 55 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | #include "packet.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | #include "uidswap.h" |
| 58 | #include "compat.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 59 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 60 | #include "sshconnect.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 61 | #include "hostfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 62 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 63 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 64 | #include "readconf.h" |
| 65 | #include "atomicio.h" |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 66 | #include "dns.h" |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 67 | #include "monitor_fdpass.h" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 68 | #include "ssh2.h" |
Damien Miller | b757677 | 2006-07-10 20:23:39 +1000 | [diff] [blame] | 69 | #include "version.h" |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 70 | #include "authfile.h" |
| 71 | #include "ssherr.h" |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 72 | #include "authfd.h" |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 73 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 74 | char *client_version_string = NULL; |
| 75 | char *server_version_string = NULL; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 76 | struct sshkey *previous_host_key = NULL; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 77 | |
Damien Miller | c1af1d5 | 2005-11-05 15:08:57 +1100 | [diff] [blame] | 78 | static int matching_host_key_dns = 0; |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 79 | |
Damien Miller | a41ccca | 2010-10-07 22:07:32 +1100 | [diff] [blame] | 80 | static pid_t proxy_command_pid = 0; |
| 81 | |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 82 | /* import */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 83 | extern Options options; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 84 | extern char *__progname; |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 85 | extern uid_t original_real_uid; |
| 86 | extern uid_t original_effective_uid; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 87 | |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 88 | static int show_other_keys(struct hostkeys *, struct sshkey *); |
| 89 | static void warn_changed_key(struct sshkey *); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 90 | |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 91 | /* Expand a proxy command */ |
| 92 | static char * |
| 93 | expand_proxy_command(const char *proxy_command, const char *user, |
| 94 | const char *host, int port) |
| 95 | { |
| 96 | char *tmp, *ret, strport[NI_MAXSERV]; |
| 97 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 98 | snprintf(strport, sizeof strport, "%d", port); |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 99 | xasprintf(&tmp, "exec %s", proxy_command); |
| 100 | ret = percent_expand(tmp, "h", host, "p", strport, |
| 101 | "r", options.user, (char *)NULL); |
| 102 | free(tmp); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Connect to the given ssh server using a proxy command that passes a |
| 108 | * a connected fd back to us. |
| 109 | */ |
| 110 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 111 | ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 112 | const char *proxy_command) |
| 113 | { |
| 114 | char *command_string; |
| 115 | int sp[2], sock; |
| 116 | pid_t pid; |
| 117 | char *shell; |
| 118 | |
| 119 | if ((shell = getenv("SHELL")) == NULL) |
| 120 | shell = _PATH_BSHELL; |
| 121 | |
| 122 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) |
| 123 | fatal("Could not create socketpair to communicate with " |
| 124 | "proxy dialer: %.100s", strerror(errno)); |
| 125 | |
| 126 | command_string = expand_proxy_command(proxy_command, options.user, |
| 127 | host, port); |
| 128 | debug("Executing proxy dialer command: %.500s", command_string); |
| 129 | |
| 130 | /* Fork and execute the proxy command. */ |
| 131 | if ((pid = fork()) == 0) { |
| 132 | char *argv[10]; |
| 133 | |
| 134 | /* Child. Permanently give up superuser privileges. */ |
| 135 | permanently_drop_suid(original_real_uid); |
| 136 | |
| 137 | close(sp[1]); |
| 138 | /* Redirect stdin and stdout. */ |
| 139 | if (sp[0] != 0) { |
| 140 | if (dup2(sp[0], 0) < 0) |
| 141 | perror("dup2 stdin"); |
| 142 | } |
| 143 | if (sp[0] != 1) { |
| 144 | if (dup2(sp[0], 1) < 0) |
| 145 | perror("dup2 stdout"); |
| 146 | } |
| 147 | if (sp[0] >= 2) |
| 148 | close(sp[0]); |
| 149 | |
| 150 | /* |
| 151 | * Stderr is left as it is so that error messages get |
| 152 | * printed on the user's terminal. |
| 153 | */ |
| 154 | argv[0] = shell; |
| 155 | argv[1] = "-c"; |
| 156 | argv[2] = command_string; |
| 157 | argv[3] = NULL; |
| 158 | |
| 159 | /* |
| 160 | * Execute the proxy command. |
| 161 | * Note that we gave up any extra privileges above. |
| 162 | */ |
| 163 | execv(argv[0], argv); |
| 164 | perror(argv[0]); |
| 165 | exit(1); |
| 166 | } |
| 167 | /* Parent. */ |
| 168 | if (pid < 0) |
| 169 | fatal("fork failed: %.100s", strerror(errno)); |
| 170 | close(sp[0]); |
| 171 | free(command_string); |
| 172 | |
| 173 | if ((sock = mm_receive_fd(sp[1])) == -1) |
| 174 | fatal("proxy dialer did not pass back a connection"); |
markus@openbsd.org | fc77ccd | 2016-01-14 22:56:56 +0000 | [diff] [blame] | 175 | close(sp[1]); |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 176 | |
| 177 | while (waitpid(pid, NULL, 0) == -1) |
| 178 | if (errno != EINTR) |
| 179 | fatal("Couldn't wait for child: %s", strerror(errno)); |
| 180 | |
| 181 | /* Set the connection file descriptors. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 182 | if (ssh_packet_set_connection(ssh, sock, sock) == NULL) |
| 183 | return -1; /* ssh_packet_set_connection logs error */ |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | /* |
| 189 | * Connect to the given ssh server using a proxy command. |
| 190 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 191 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 192 | ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port, |
| 193 | const char *proxy_command) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | { |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 195 | char *command_string; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | int pin[2], pout[2]; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 197 | pid_t pid; |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 198 | char *shell; |
Damien Miller | 1d824ab | 2007-09-17 11:58:04 +1000 | [diff] [blame] | 199 | |
Damien Miller | 38d9a96 | 2010-10-07 22:07:11 +1100 | [diff] [blame] | 200 | if ((shell = getenv("SHELL")) == NULL || *shell == '\0') |
Damien Miller | 1d824ab | 2007-09-17 11:58:04 +1000 | [diff] [blame] | 201 | shell = _PATH_BSHELL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | /* Create pipes for communicating with the proxy. */ |
| 204 | if (pipe(pin) < 0 || pipe(pout) < 0) |
| 205 | fatal("Could not create pipes to communicate with the proxy: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 206 | strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 207 | |
Damien Miller | 1262b66 | 2013-08-21 02:44:24 +1000 | [diff] [blame] | 208 | command_string = expand_proxy_command(proxy_command, options.user, |
| 209 | host, port); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | debug("Executing proxy command: %.500s", command_string); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 212 | /* Fork and execute the proxy command. */ |
| 213 | if ((pid = fork()) == 0) { |
| 214 | char *argv[10]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 215 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | /* Child. Permanently give up superuser privileges. */ |
Damien Miller | 2e5fe88 | 2006-06-13 13:10:00 +1000 | [diff] [blame] | 217 | permanently_drop_suid(original_real_uid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | |
| 219 | /* Redirect stdin and stdout. */ |
| 220 | close(pin[1]); |
| 221 | if (pin[0] != 0) { |
| 222 | if (dup2(pin[0], 0) < 0) |
| 223 | perror("dup2 stdin"); |
| 224 | close(pin[0]); |
| 225 | } |
| 226 | close(pout[0]); |
| 227 | if (dup2(pout[1], 1) < 0) |
| 228 | perror("dup2 stdout"); |
| 229 | /* Cannot be 1 because pin allocated two descriptors. */ |
| 230 | close(pout[1]); |
| 231 | |
| 232 | /* Stderr is left as it is so that error messages get |
| 233 | printed on the user's terminal. */ |
Damien Miller | 1d824ab | 2007-09-17 11:58:04 +1000 | [diff] [blame] | 234 | argv[0] = shell; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | argv[1] = "-c"; |
| 236 | argv[2] = command_string; |
| 237 | argv[3] = NULL; |
| 238 | |
| 239 | /* Execute the proxy command. Note that we gave up any |
| 240 | extra privileges above. */ |
Damien Miller | 6fb6fd5 | 2011-01-16 23:17:45 +1100 | [diff] [blame] | 241 | signal(SIGPIPE, SIG_DFL); |
Kevin Steves | fcd5d60 | 2001-01-07 11:45:22 +0000 | [diff] [blame] | 242 | execv(argv[0], argv); |
| 243 | perror(argv[0]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 244 | exit(1); |
| 245 | } |
| 246 | /* Parent. */ |
| 247 | if (pid < 0) |
| 248 | fatal("fork failed: %.100s", strerror(errno)); |
Damien Miller | 8c4e18a | 2002-09-19 12:05:02 +1000 | [diff] [blame] | 249 | else |
| 250 | proxy_command_pid = pid; /* save pid to clean up later */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | |
| 252 | /* Close child side of the descriptors. */ |
| 253 | close(pin[0]); |
| 254 | close(pout[1]); |
| 255 | |
| 256 | /* Free the command name. */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 257 | free(command_string); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 258 | |
| 259 | /* Set the connection file descriptors. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 260 | if (ssh_packet_set_connection(ssh, pout[0], pin[1]) == NULL) |
| 261 | return -1; /* ssh_packet_set_connection logs error */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 262 | |
Ben Lindstrom | f9cedb9 | 2001-08-06 21:07:11 +0000 | [diff] [blame] | 263 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 264 | } |
| 265 | |
Damien Miller | a41ccca | 2010-10-07 22:07:32 +1100 | [diff] [blame] | 266 | void |
| 267 | ssh_kill_proxy_command(void) |
| 268 | { |
| 269 | /* |
| 270 | * Send SIGHUP to proxy command if used. We don't wait() in |
| 271 | * case it hangs and instead rely on init to reap the child |
| 272 | */ |
| 273 | if (proxy_command_pid > 1) |
Damien Miller | 45fcdaa | 2010-10-07 22:07:58 +1100 | [diff] [blame] | 274 | kill(proxy_command_pid, SIGHUP); |
Damien Miller | a41ccca | 2010-10-07 22:07:32 +1100 | [diff] [blame] | 275 | } |
| 276 | |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 277 | #ifdef HAVE_IFADDRS_H |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 278 | /* |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 279 | * Search a interface address list (returned from getifaddrs(3)) for an |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 280 | * address that matches the desired address family on the specified interface. |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 281 | * Returns 0 and fills in *resultp and *rlenp on success. Returns -1 on failure. |
| 282 | */ |
| 283 | static int |
| 284 | check_ifaddrs(const char *ifname, int af, const struct ifaddrs *ifaddrs, |
| 285 | struct sockaddr_storage *resultp, socklen_t *rlenp) |
| 286 | { |
| 287 | struct sockaddr_in6 *sa6; |
| 288 | struct sockaddr_in *sa; |
| 289 | struct in6_addr *v6addr; |
| 290 | const struct ifaddrs *ifa; |
| 291 | int allow_local; |
| 292 | |
| 293 | /* |
| 294 | * Prefer addresses that are not loopback or linklocal, but use them |
| 295 | * if nothing else matches. |
| 296 | */ |
| 297 | for (allow_local = 0; allow_local < 2; allow_local++) { |
| 298 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { |
| 299 | if (ifa->ifa_addr == NULL || ifa->ifa_name == NULL || |
| 300 | (ifa->ifa_flags & IFF_UP) == 0 || |
| 301 | ifa->ifa_addr->sa_family != af || |
| 302 | strcmp(ifa->ifa_name, options.bind_interface) != 0) |
| 303 | continue; |
| 304 | switch (ifa->ifa_addr->sa_family) { |
| 305 | case AF_INET: |
| 306 | sa = (struct sockaddr_in *)ifa->ifa_addr; |
| 307 | if (!allow_local && sa->sin_addr.s_addr == |
| 308 | htonl(INADDR_LOOPBACK)) |
| 309 | continue; |
| 310 | if (*rlenp < sizeof(struct sockaddr_in)) { |
| 311 | error("%s: v4 addr doesn't fit", |
| 312 | __func__); |
| 313 | return -1; |
| 314 | } |
| 315 | *rlenp = sizeof(struct sockaddr_in); |
| 316 | memcpy(resultp, sa, *rlenp); |
| 317 | return 0; |
| 318 | case AF_INET6: |
| 319 | sa6 = (struct sockaddr_in6 *)ifa->ifa_addr; |
| 320 | v6addr = &sa6->sin6_addr; |
| 321 | if (!allow_local && |
| 322 | (IN6_IS_ADDR_LINKLOCAL(v6addr) || |
| 323 | IN6_IS_ADDR_LOOPBACK(v6addr))) |
| 324 | continue; |
| 325 | if (*rlenp < sizeof(struct sockaddr_in6)) { |
| 326 | error("%s: v6 addr doesn't fit", |
| 327 | __func__); |
| 328 | return -1; |
| 329 | } |
| 330 | *rlenp = sizeof(struct sockaddr_in6); |
| 331 | memcpy(resultp, sa6, *rlenp); |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | return -1; |
| 337 | } |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 338 | #endif |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 339 | |
| 340 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 341 | * Creates a (possibly privileged) socket for use as the ssh connection. |
| 342 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 343 | static int |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 344 | ssh_create_socket(int privileged, struct addrinfo *ai) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | { |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 346 | int sock, r, oerrno; |
| 347 | struct sockaddr_storage bindaddr; |
| 348 | socklen_t bindaddrlen = 0; |
Damien Miller | d1a7a9c | 2014-02-07 09:24:33 +1100 | [diff] [blame] | 349 | struct addrinfo hints, *res = NULL; |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 350 | #ifdef HAVE_IFADDRS_H |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 351 | struct ifaddrs *ifaddrs = NULL; |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 352 | #endif |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 353 | char ntop[NI_MAXHOST]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 354 | |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 355 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 356 | if (sock < 0) { |
Darren Tucker | e6e52f8 | 2013-10-10 10:28:07 +1100 | [diff] [blame] | 357 | error("socket: %s", strerror(errno)); |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | fcntl(sock, F_SETFD, FD_CLOEXEC); |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 361 | |
| 362 | /* Bind the socket to an alternative local IP address */ |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 363 | if (options.bind_address == NULL && options.bind_interface == NULL && |
| 364 | !privileged) |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 365 | return sock; |
| 366 | |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 367 | if (options.bind_address != NULL) { |
Damien Miller | d1a7a9c | 2014-02-07 09:24:33 +1100 | [diff] [blame] | 368 | memset(&hints, 0, sizeof(hints)); |
| 369 | hints.ai_family = ai->ai_family; |
| 370 | hints.ai_socktype = ai->ai_socktype; |
| 371 | hints.ai_protocol = ai->ai_protocol; |
| 372 | hints.ai_flags = AI_PASSIVE; |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 373 | if ((r = getaddrinfo(options.bind_address, NULL, |
| 374 | &hints, &res)) != 0) { |
Damien Miller | d1a7a9c | 2014-02-07 09:24:33 +1100 | [diff] [blame] | 375 | error("getaddrinfo: %s: %s", options.bind_address, |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 376 | ssh_gai_strerror(r)); |
| 377 | goto fail; |
Damien Miller | d1a7a9c | 2014-02-07 09:24:33 +1100 | [diff] [blame] | 378 | } |
dtucker@openbsd.org | 3e19fb9 | 2018-02-23 04:18:46 +0000 | [diff] [blame] | 379 | if (res == NULL) { |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 380 | error("getaddrinfo: no addrs"); |
| 381 | goto fail; |
dtucker@openbsd.org | 3e19fb9 | 2018-02-23 04:18:46 +0000 | [diff] [blame] | 382 | } |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 383 | if (res->ai_addrlen > sizeof(bindaddr)) { |
| 384 | error("%s: addr doesn't fit", __func__); |
| 385 | goto fail; |
| 386 | } |
| 387 | memcpy(&bindaddr, res->ai_addr, res->ai_addrlen); |
| 388 | bindaddrlen = res->ai_addrlen; |
| 389 | } else if (options.bind_interface != NULL) { |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 390 | #ifdef HAVE_IFADDRS_H |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 391 | if ((r = getifaddrs(&ifaddrs)) != 0) { |
| 392 | error("getifaddrs: %s: %s", options.bind_interface, |
| 393 | strerror(errno)); |
| 394 | goto fail; |
| 395 | } |
| 396 | bindaddrlen = sizeof(bindaddr); |
| 397 | if (check_ifaddrs(options.bind_interface, ai->ai_family, |
| 398 | ifaddrs, &bindaddr, &bindaddrlen) != 0) { |
| 399 | logit("getifaddrs: %s: no suitable addresses", |
| 400 | options.bind_interface); |
| 401 | goto fail; |
| 402 | } |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 403 | #else |
| 404 | error("BindInterface not supported on this platform."); |
| 405 | #endif |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 406 | } |
| 407 | if ((r = getnameinfo((struct sockaddr *)&bindaddr, bindaddrlen, |
| 408 | ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST)) != 0) { |
| 409 | error("%s: getnameinfo failed: %s", __func__, |
| 410 | ssh_gai_strerror(r)); |
| 411 | goto fail; |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 412 | } |
Darren Tucker | e6e52f8 | 2013-10-10 10:28:07 +1100 | [diff] [blame] | 413 | /* |
| 414 | * If we are running as root and want to connect to a privileged |
| 415 | * port, bind our own socket to a privileged port. |
| 416 | */ |
| 417 | if (privileged) { |
| 418 | PRIV_START; |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 419 | r = bindresvport_sa(sock, |
| 420 | bindaddrlen == 0 ? NULL : (struct sockaddr *)&bindaddr); |
| 421 | oerrno = errno; |
Darren Tucker | e6e52f8 | 2013-10-10 10:28:07 +1100 | [diff] [blame] | 422 | PRIV_END; |
| 423 | if (r < 0) { |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 424 | error("bindresvport_sa %s: %s", ntop, |
| 425 | strerror(oerrno)); |
Darren Tucker | e6e52f8 | 2013-10-10 10:28:07 +1100 | [diff] [blame] | 426 | goto fail; |
| 427 | } |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 428 | } else if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) { |
| 429 | error("bind %s: %s", ntop, strerror(errno)); |
| 430 | goto fail; |
Ben Lindstrom | e0f8804 | 2001-04-30 13:06:24 +0000 | [diff] [blame] | 431 | } |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 432 | debug("%s: bound to %s", __func__, ntop); |
| 433 | /* success */ |
| 434 | goto out; |
| 435 | fail: |
| 436 | close(sock); |
| 437 | sock = -1; |
| 438 | out: |
Damien Miller | d1a7a9c | 2014-02-07 09:24:33 +1100 | [diff] [blame] | 439 | if (res != NULL) |
| 440 | freeaddrinfo(res); |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 441 | #ifdef HAVE_IFADDRS_H |
djm@openbsd.org | ac2e302 | 2018-02-23 02:34:33 +0000 | [diff] [blame] | 442 | if (ifaddrs != NULL) |
| 443 | freeifaddrs(ifaddrs); |
Darren Tucker | b59162d | 2018-02-23 15:20:42 +1100 | [diff] [blame] | 444 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | return sock; |
| 446 | } |
| 447 | |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 448 | /* |
| 449 | * Wait up to *timeoutp milliseconds for fd to be readable. Updates |
| 450 | * *timeoutp with time remaining. |
| 451 | * Returns 0 if fd ready or -1 on timeout or error (see errno). |
| 452 | */ |
| 453 | static int |
| 454 | waitrfd(int fd, int *timeoutp) |
| 455 | { |
| 456 | struct pollfd pfd; |
| 457 | struct timeval t_start; |
| 458 | int oerrno, r; |
| 459 | |
dtucker@openbsd.org@openbsd.org | 5db6fbf | 2017-11-25 06:46:22 +0000 | [diff] [blame] | 460 | monotime_tv(&t_start); |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 461 | pfd.fd = fd; |
| 462 | pfd.events = POLLIN; |
| 463 | for (; *timeoutp >= 0;) { |
| 464 | r = poll(&pfd, 1, *timeoutp); |
| 465 | oerrno = errno; |
| 466 | ms_subtract_diff(&t_start, timeoutp); |
| 467 | errno = oerrno; |
| 468 | if (r > 0) |
| 469 | return 0; |
| 470 | else if (r == -1 && errno != EAGAIN) |
| 471 | return -1; |
| 472 | else if (r == 0) |
| 473 | break; |
| 474 | } |
| 475 | /* timeout */ |
| 476 | errno = ETIMEDOUT; |
| 477 | return -1; |
| 478 | } |
| 479 | |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 480 | static int |
| 481 | timeout_connect(int sockfd, const struct sockaddr *serv_addr, |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 482 | socklen_t addrlen, int *timeoutp) |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 483 | { |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 484 | int optval = 0; |
| 485 | socklen_t optlen = sizeof(optval); |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 486 | |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 487 | /* No timeout: just do a blocking connect() */ |
| 488 | if (*timeoutp <= 0) |
| 489 | return connect(sockfd, serv_addr, addrlen); |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 490 | |
Damien Miller | f6723f0 | 2004-01-27 21:21:27 +1100 | [diff] [blame] | 491 | set_nonblock(sockfd); |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 492 | if (connect(sockfd, serv_addr, addrlen) == 0) { |
| 493 | /* Succeeded already? */ |
Damien Miller | f6723f0 | 2004-01-27 21:21:27 +1100 | [diff] [blame] | 494 | unset_nonblock(sockfd); |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 495 | return 0; |
| 496 | } else if (errno != EINPROGRESS) |
| 497 | return -1; |
| 498 | |
| 499 | if (waitrfd(sockfd, timeoutp) == -1) |
| 500 | return -1; |
| 501 | |
| 502 | /* Completed or failed */ |
| 503 | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1) { |
| 504 | debug("getsockopt: %s", strerror(errno)); |
| 505 | return -1; |
Damien Miller | f6723f0 | 2004-01-27 21:21:27 +1100 | [diff] [blame] | 506 | } |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 507 | if (optval != 0) { |
| 508 | errno = optval; |
| 509 | return -1; |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 510 | } |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 511 | unset_nonblock(sockfd); |
| 512 | return 0; |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 513 | } |
| 514 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 515 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 516 | * Opens a TCP/IP connection to the remote server on the given host. |
| 517 | * The address of the remote host will be returned in hostaddr. |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 518 | * If port is 0, the default port will be used. If needpriv is true, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 519 | * a privileged port will be allocated to make the connection. |
Ben Lindstrom | f9c4884 | 2002-06-11 16:37:51 +0000 | [diff] [blame] | 520 | * This requires super-user privileges if needpriv is true. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 521 | * Connection_attempts specifies the maximum number of tries (one per |
| 522 | * second). If proxy_command is non-NULL, it specifies the command (with %h |
| 523 | * and %p substituted for host and port, respectively) to use to contact |
| 524 | * the daemon. |
| 525 | */ |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 526 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 527 | ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 528 | struct sockaddr_storage *hostaddr, u_short port, int family, |
| 529 | int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 530 | { |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 531 | int on = 1; |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 532 | int oerrno, sock = -1, attempt; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 533 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 534 | struct addrinfo *ai; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 535 | |
djm@openbsd.org | 46152af | 2015-09-04 04:55:24 +0000 | [diff] [blame] | 536 | debug2("%s: needpriv %d", __func__, needpriv); |
djm@openbsd.org | 88b6fcd | 2015-11-19 08:23:27 +0000 | [diff] [blame] | 537 | memset(ntop, 0, sizeof(ntop)); |
| 538 | memset(strport, 0, sizeof(strport)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | |
Damien Miller | 56e5e6a | 2006-04-23 12:08:59 +1000 | [diff] [blame] | 540 | for (attempt = 0; attempt < connection_attempts; attempt++) { |
Damien Miller | f4bcd10 | 2006-10-24 03:02:23 +1000 | [diff] [blame] | 541 | if (attempt > 0) { |
| 542 | /* Sleep a moment before retrying. */ |
| 543 | sleep(1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 544 | debug("Trying again..."); |
Damien Miller | f4bcd10 | 2006-10-24 03:02:23 +1000 | [diff] [blame] | 545 | } |
Damien Miller | 56e5e6a | 2006-04-23 12:08:59 +1000 | [diff] [blame] | 546 | /* |
| 547 | * Loop through addresses for this host, and try each one in |
| 548 | * sequence until the connection succeeds. |
| 549 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 550 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 551 | if (ai->ai_family != AF_INET && |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 552 | ai->ai_family != AF_INET6) { |
| 553 | errno = EAFNOSUPPORT; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 554 | continue; |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 555 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 556 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, |
| 557 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 558 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 559 | oerrno = errno; |
djm@openbsd.org | 46152af | 2015-09-04 04:55:24 +0000 | [diff] [blame] | 560 | error("%s: getnameinfo failed", __func__); |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 561 | errno = oerrno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 562 | continue; |
| 563 | } |
| 564 | debug("Connecting to %.200s [%.100s] port %s.", |
| 565 | host, ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 566 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 567 | /* Create a socket for connecting. */ |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 568 | sock = ssh_create_socket(needpriv, ai); |
stsp@openbsd.org | a60c5dc | 2018-01-23 18:33:49 +0000 | [diff] [blame] | 569 | if (sock < 0) { |
Ben Lindstrom | f9cedb9 | 2001-08-06 21:07:11 +0000 | [diff] [blame] | 570 | /* Any error is already output */ |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 571 | errno = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 572 | continue; |
stsp@openbsd.org | a60c5dc | 2018-01-23 18:33:49 +0000 | [diff] [blame] | 573 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 574 | |
Damien Miller | b78d5eb | 2003-05-16 11:39:04 +1000 | [diff] [blame] | 575 | if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen, |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 576 | timeout_ms) >= 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 577 | /* Successful connection. */ |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 578 | memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 579 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 580 | } else { |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 581 | oerrno = errno; |
Ben Lindstrom | 728aa7e | 2002-07-15 17:48:11 +0000 | [diff] [blame] | 582 | debug("connect to address %s port %s: %s", |
| 583 | ntop, strport, strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 584 | close(sock); |
Damien Miller | 56e5e6a | 2006-04-23 12:08:59 +1000 | [diff] [blame] | 585 | sock = -1; |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 586 | errno = oerrno; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 587 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 588 | } |
Damien Miller | 56e5e6a | 2006-04-23 12:08:59 +1000 | [diff] [blame] | 589 | if (sock != -1) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 590 | break; /* Successful connection. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 591 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 592 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 593 | /* Return failure if we didn't get a successful connection. */ |
Damien Miller | 56e5e6a | 2006-04-23 12:08:59 +1000 | [diff] [blame] | 594 | if (sock == -1) { |
Darren Tucker | b2161e3 | 2005-01-20 11:00:46 +1100 | [diff] [blame] | 595 | error("ssh: connect to host %s port %s: %s", |
djm@openbsd.org | 7c77991 | 2018-01-23 05:17:04 +0000 | [diff] [blame] | 596 | host, strport, errno == 0 ? "failure" : strerror(errno)); |
| 597 | return -1; |
Ben Lindstrom | a6cd75c | 2002-07-11 04:00:19 +0000 | [diff] [blame] | 598 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 599 | |
| 600 | debug("Connection established."); |
| 601 | |
Damien Miller | 12c150e | 2003-12-17 16:31:10 +1100 | [diff] [blame] | 602 | /* Set SO_KEEPALIVE if requested. */ |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 603 | if (want_keepalive && |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 604 | setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, |
| 605 | sizeof(on)) < 0) |
| 606 | error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 607 | |
| 608 | /* Set the connection. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 609 | if (ssh_packet_set_connection(ssh, sock, sock) == NULL) |
| 610 | return -1; /* ssh_packet_set_connection logs error */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 611 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 612 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 613 | } |
| 614 | |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 615 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 616 | ssh_connect(struct ssh *ssh, const char *host, struct addrinfo *addrs, |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 617 | struct sockaddr_storage *hostaddr, u_short port, int family, |
| 618 | int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv) |
| 619 | { |
| 620 | if (options.proxy_command == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 621 | return ssh_connect_direct(ssh, host, addrs, hostaddr, port, |
| 622 | family, connection_attempts, timeout_ms, want_keepalive, |
| 623 | needpriv); |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 624 | } else if (strcmp(options.proxy_command, "-") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 625 | if ((ssh_packet_set_connection(ssh, |
| 626 | STDIN_FILENO, STDOUT_FILENO)) == NULL) |
| 627 | return -1; /* ssh_packet_set_connection logs error */ |
| 628 | return 0; |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 629 | } else if (options.proxy_use_fdpass) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 630 | return ssh_proxy_fdpass_connect(ssh, host, port, |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 631 | options.proxy_command); |
| 632 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 633 | return ssh_proxy_connect(ssh, host, port, options.proxy_command); |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 634 | } |
| 635 | |
Darren Tucker | 00c1518 | 2012-09-06 21:21:56 +1000 | [diff] [blame] | 636 | static void |
| 637 | send_client_banner(int connection_out, int minor1) |
| 638 | { |
Darren Tucker | 00c1518 | 2012-09-06 21:21:56 +1000 | [diff] [blame] | 639 | /* Send our own protocol version identification. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 640 | xasprintf(&client_version_string, "SSH-%d.%d-%.100s\r\n", |
| 641 | PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION); |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 642 | if (atomicio(vwrite, connection_out, client_version_string, |
Darren Tucker | 00c1518 | 2012-09-06 21:21:56 +1000 | [diff] [blame] | 643 | strlen(client_version_string)) != strlen(client_version_string)) |
| 644 | fatal("write: %.100s", strerror(errno)); |
| 645 | chop(client_version_string); |
| 646 | debug("Local version string %.100s", client_version_string); |
| 647 | } |
| 648 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 649 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 650 | * Waits for the server identification string, and sends our own |
| 651 | * identification string. |
| 652 | */ |
Darren Tucker | 1cc55d7 | 2009-06-21 18:17:19 +1000 | [diff] [blame] | 653 | void |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 654 | ssh_exchange_identification(int timeout_ms) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 655 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 656 | char buf[256], remote_version[256]; /* must be same size! */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 657 | int remote_major, remote_minor, mismatch; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 658 | int connection_in = packet_get_connection_in(); |
| 659 | int connection_out = packet_get_connection_out(); |
Damien Miller | 1e88ea6 | 2006-07-10 20:15:56 +1000 | [diff] [blame] | 660 | u_int i, n; |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 661 | size_t len; |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 662 | int rc; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 663 | |
djm@openbsd.org | 99f95ba | 2017-04-30 23:11:45 +0000 | [diff] [blame] | 664 | send_client_banner(connection_out, 0); |
Darren Tucker | 00c1518 | 2012-09-06 21:21:56 +1000 | [diff] [blame] | 665 | |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 666 | /* Read other side's version identification. */ |
Damien Miller | 1e88ea6 | 2006-07-10 20:15:56 +1000 | [diff] [blame] | 667 | for (n = 0;;) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 668 | for (i = 0; i < sizeof(buf) - 1; i++) { |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 669 | if (timeout_ms > 0) { |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 670 | rc = waitrfd(connection_in, &timeout_ms); |
| 671 | if (rc == -1 && errno == ETIMEDOUT) { |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 672 | fatal("Connection timed out during " |
| 673 | "banner exchange"); |
djm@openbsd.org | 4540428 | 2017-06-24 05:37:44 +0000 | [diff] [blame] | 674 | } else if (rc == -1) { |
| 675 | fatal("%s: %s", |
| 676 | __func__, strerror(errno)); |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 680 | len = atomicio(read, connection_in, &buf[i], 1); |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 681 | if (len != 1 && errno == EPIPE) |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 682 | fatal("ssh_exchange_identification: " |
| 683 | "Connection closed by remote host"); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 684 | else if (len != 1) |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 685 | fatal("ssh_exchange_identification: " |
| 686 | "read: %.100s", strerror(errno)); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 687 | if (buf[i] == '\r') { |
| 688 | buf[i] = '\n'; |
| 689 | buf[i + 1] = 0; |
| 690 | continue; /**XXX wait for \n */ |
| 691 | } |
| 692 | if (buf[i] == '\n') { |
| 693 | buf[i + 1] = 0; |
| 694 | break; |
| 695 | } |
Damien Miller | 1e88ea6 | 2006-07-10 20:15:56 +1000 | [diff] [blame] | 696 | if (++n > 65536) |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 697 | fatal("ssh_exchange_identification: " |
| 698 | "No banner received"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 699 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 700 | buf[sizeof(buf) - 1] = 0; |
| 701 | if (strncmp(buf, "SSH-", 4) == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 702 | break; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 703 | debug("ssh_exchange_identification: %s", buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 704 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 705 | server_version_string = xstrdup(buf); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 706 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 707 | /* |
| 708 | * Check that the versions match. In future this might accept |
| 709 | * several versions and set appropriate flags to handle them. |
| 710 | */ |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 711 | if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", |
| 712 | &remote_major, &remote_minor, remote_version) != 3) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 713 | fatal("Bad remote protocol version identification: '%.100s'", buf); |
| 714 | debug("Remote protocol version %d.%d, remote software version %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 715 | remote_major, remote_minor, remote_version); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 716 | |
markus@openbsd.org | 48b3b2b | 2015-01-19 20:20:20 +0000 | [diff] [blame] | 717 | active_state->compat = compat_datafellows(remote_version); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 718 | mismatch = 0; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 719 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 720 | switch (remote_major) { |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 721 | case 2: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 722 | break; |
| 723 | case 1: |
| 724 | if (remote_minor != 99) |
| 725 | mismatch = 1; |
djm@openbsd.org | 99f95ba | 2017-04-30 23:11:45 +0000 | [diff] [blame] | 726 | break; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 727 | default: |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 728 | mismatch = 1; |
| 729 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 730 | } |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 731 | if (mismatch) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 732 | fatal("Protocol major versions differ: %d vs. %d", |
djm@openbsd.org | 99f95ba | 2017-04-30 23:11:45 +0000 | [diff] [blame] | 733 | PROTOCOL_MAJOR_2, remote_major); |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 734 | if ((datafellows & SSH_BUG_RSASIGMD5) != 0) |
| 735 | logit("Server version \"%.100s\" uses unsafe RSA signature " |
| 736 | "scheme; disabling use of RSA keys", remote_version); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 737 | chop(server_version_string); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 738 | } |
| 739 | |
Ben Lindstrom | b6c06d9 | 2001-02-10 23:21:09 +0000 | [diff] [blame] | 740 | /* defaults to 'no' */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 741 | static int |
Damien Miller | e398004 | 2001-10-10 15:02:03 +1000 | [diff] [blame] | 742 | confirm(const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 743 | { |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 744 | const char *msg, *again = "Please type 'yes' or 'no': "; |
| 745 | char *p; |
| 746 | int ret = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 747 | |
Ben Lindstrom | b6c06d9 | 2001-02-10 23:21:09 +0000 | [diff] [blame] | 748 | if (options.batch_mode) |
| 749 | return 0; |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 750 | for (msg = prompt;;msg = again) { |
| 751 | p = read_passphrase(msg, RP_ECHO); |
djm@openbsd.org | e0ce54c | 2017-12-06 05:06:21 +0000 | [diff] [blame] | 752 | if (p == NULL) |
| 753 | return 0; |
| 754 | p[strcspn(p, "\n")] = '\0'; |
| 755 | if (p[0] == '\0' || strcasecmp(p, "no") == 0) |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 756 | ret = 0; |
djm@openbsd.org | e0ce54c | 2017-12-06 05:06:21 +0000 | [diff] [blame] | 757 | else if (strcasecmp(p, "yes") == 0) |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 758 | ret = 1; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 759 | free(p); |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 760 | if (ret != -1) |
| 761 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 762 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 763 | } |
| 764 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 765 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 766 | check_host_cert(const char *host, const struct sshkey *host_key) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 767 | { |
| 768 | const char *reason; |
| 769 | |
| 770 | if (key_cert_check_authority(host_key, 1, 0, host, &reason) != 0) { |
| 771 | error("%s", reason); |
| 772 | return 0; |
| 773 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 774 | if (sshbuf_len(host_key->cert->critical) != 0) { |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 775 | error("Certificate for %s contains unsupported " |
| 776 | "critical options(s)", host); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | return 1; |
| 780 | } |
| 781 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 782 | static int |
| 783 | sockaddr_is_local(struct sockaddr *hostaddr) |
| 784 | { |
| 785 | switch (hostaddr->sa_family) { |
| 786 | case AF_INET: |
| 787 | return (ntohl(((struct sockaddr_in *)hostaddr)-> |
| 788 | sin_addr.s_addr) >> 24) == IN_LOOPBACKNET; |
| 789 | case AF_INET6: |
| 790 | return IN6_IS_ADDR_LOOPBACK( |
| 791 | &(((struct sockaddr_in6 *)hostaddr)->sin6_addr)); |
| 792 | default: |
| 793 | return 0; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Prepare the hostname and ip address strings that are used to lookup |
| 799 | * host keys in known_hosts files. These may have a port number appended. |
| 800 | */ |
| 801 | void |
| 802 | get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr, |
| 803 | u_short port, char **hostfile_hostname, char **hostfile_ipaddr) |
| 804 | { |
| 805 | char ntop[NI_MAXHOST]; |
| 806 | socklen_t addrlen; |
| 807 | |
| 808 | switch (hostaddr == NULL ? -1 : hostaddr->sa_family) { |
| 809 | case -1: |
| 810 | addrlen = 0; |
| 811 | break; |
| 812 | case AF_INET: |
| 813 | addrlen = sizeof(struct sockaddr_in); |
| 814 | break; |
| 815 | case AF_INET6: |
| 816 | addrlen = sizeof(struct sockaddr_in6); |
| 817 | break; |
| 818 | default: |
| 819 | addrlen = sizeof(struct sockaddr); |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * We don't have the remote ip-address for connections |
| 825 | * using a proxy command |
| 826 | */ |
| 827 | if (hostfile_ipaddr != NULL) { |
| 828 | if (options.proxy_command == NULL) { |
| 829 | if (getnameinfo(hostaddr, addrlen, |
| 830 | ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0) |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 831 | fatal("%s: getnameinfo failed", __func__); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 832 | *hostfile_ipaddr = put_host_port(ntop, port); |
| 833 | } else { |
| 834 | *hostfile_ipaddr = xstrdup("<no hostip for proxy " |
| 835 | "command>"); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Allow the user to record the key under a different name or |
| 841 | * differentiate a non-standard port. This is useful for ssh |
| 842 | * tunneling over forwarded connections or if you run multiple |
| 843 | * sshd's on different ports on the same machine. |
| 844 | */ |
| 845 | if (hostfile_hostname != NULL) { |
| 846 | if (options.host_key_alias != NULL) { |
| 847 | *hostfile_hostname = xstrdup(options.host_key_alias); |
| 848 | debug("using hostkeyalias: %s", *hostfile_hostname); |
| 849 | } else { |
| 850 | *hostfile_hostname = put_host_port(hostname, port); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 855 | /* |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 856 | * check whether the supplied host key is valid, return -1 if the key |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 857 | * is not valid. user_hostfile[0] will not be updated if 'readonly' is true. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 858 | */ |
Damien Miller | da82839 | 2006-08-05 11:35:45 +1000 | [diff] [blame] | 859 | #define RDRW 0 |
| 860 | #define RDONLY 1 |
| 861 | #define ROQUIET 2 |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 862 | static int |
Damien Miller | da82839 | 2006-08-05 11:35:45 +1000 | [diff] [blame] | 863 | check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 864 | struct sshkey *host_key, int readonly, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 865 | char **user_hostfiles, u_int num_user_hostfiles, |
| 866 | char **system_hostfiles, u_int num_system_hostfiles) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 867 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 868 | HostStatus host_status; |
| 869 | HostStatus ip_status; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 870 | struct sshkey *raw_key = NULL; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 871 | char *ip = NULL, *host = NULL; |
| 872 | char hostline[1000], *hostp, *fp, *ra; |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 873 | char msg[1024]; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 874 | const char *type; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 875 | const struct hostkey_entry *host_found, *ip_found; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 876 | int len, cancelled_forwarding = 0; |
| 877 | int local = sockaddr_is_local(hostaddr); |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 878 | int r, want_cert = sshkey_is_cert(host_key), host_ip_differ = 0; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 879 | int hostkey_trusted = 0; /* Known or explicitly accepted by user */ |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 880 | struct hostkeys *host_hostkeys, *ip_hostkeys; |
| 881 | u_int i; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 882 | |
| 883 | /* |
| 884 | * Force accepting of the host key for loopback/localhost. The |
| 885 | * problem is that if the home directory is NFS-mounted to multiple |
| 886 | * machines, localhost will refer to a different machine in each of |
| 887 | * them, and the user will get bogus HOST_CHANGED warnings. This |
| 888 | * essentially disables host authentication for localhost; however, |
| 889 | * this is probably not a real problem. |
| 890 | */ |
Ben Lindstrom | 3cecc9a | 2001-10-03 17:39:38 +0000 | [diff] [blame] | 891 | if (options.no_host_authentication_for_localhost == 1 && local && |
| 892 | options.host_key_alias == NULL) { |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 893 | debug("Forcing accepting of host key for " |
| 894 | "loopback/localhost."); |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 895 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 896 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 897 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 898 | /* |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 899 | * Prepare the hostname and address strings used for hostkey lookup. |
| 900 | * In some cases, these will have a port number appended. |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 901 | */ |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 902 | get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip); |
Darren Tucker | 78913e0 | 2008-06-13 04:47:34 +1000 | [diff] [blame] | 903 | |
| 904 | /* |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 905 | * Turn off check_host_ip if the connection is to localhost, via proxy |
| 906 | * command or if we don't have a hostname to compare with |
| 907 | */ |
Darren Tucker | da34553 | 2006-07-10 23:04:19 +1000 | [diff] [blame] | 908 | if (options.check_host_ip && (local || |
| 909 | strcmp(hostname, ip) == 0 || options.proxy_command != NULL)) |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 910 | options.check_host_ip = 0; |
| 911 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 912 | host_hostkeys = init_hostkeys(); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 913 | for (i = 0; i < num_user_hostfiles; i++) |
| 914 | load_hostkeys(host_hostkeys, host, user_hostfiles[i]); |
| 915 | for (i = 0; i < num_system_hostfiles; i++) |
| 916 | load_hostkeys(host_hostkeys, host, system_hostfiles[i]); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 917 | |
| 918 | ip_hostkeys = NULL; |
| 919 | if (!want_cert && options.check_host_ip) { |
| 920 | ip_hostkeys = init_hostkeys(); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 921 | for (i = 0; i < num_user_hostfiles; i++) |
| 922 | load_hostkeys(ip_hostkeys, ip, user_hostfiles[i]); |
| 923 | for (i = 0; i < num_system_hostfiles; i++) |
| 924 | load_hostkeys(ip_hostkeys, ip, system_hostfiles[i]); |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 927 | retry: |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 928 | /* Reload these as they may have changed on cert->key downgrade */ |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 929 | want_cert = sshkey_is_cert(host_key); |
| 930 | type = sshkey_type(host_key); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 931 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 932 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 933 | * Check if the host key is present in the user's list of known |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 934 | * hosts or in the systemwide list. |
| 935 | */ |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 936 | host_status = check_key_in_hostkeys(host_hostkeys, host_key, |
| 937 | &host_found); |
| 938 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 939 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 940 | * Also perform check for the ip address, skip the check if we are |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 941 | * localhost, looking for a certificate, or the hostname was an ip |
| 942 | * address to begin with. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 943 | */ |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 944 | if (!want_cert && ip_hostkeys != NULL) { |
| 945 | ip_status = check_key_in_hostkeys(ip_hostkeys, host_key, |
| 946 | &ip_found); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 947 | if (host_status == HOST_CHANGED && |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 948 | (ip_status != HOST_CHANGED || |
| 949 | (ip_found != NULL && |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 950 | !sshkey_equal(ip_found->key, host_found->key)))) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 951 | host_ip_differ = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 952 | } else |
| 953 | ip_status = host_status; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 954 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 955 | switch (host_status) { |
| 956 | case HOST_OK: |
| 957 | /* The host is known and the key matches. */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 958 | debug("Host '%.200s' is known and matches the %s host %s.", |
| 959 | host, type, want_cert ? "certificate" : "key"); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 960 | debug("Found %s in %s:%lu", want_cert ? "CA key" : "key", |
| 961 | host_found->file, host_found->line); |
djm@openbsd.org | 6f8ca3b | 2017-06-24 05:35:05 +0000 | [diff] [blame] | 962 | if (want_cert && |
| 963 | !check_host_cert(options.host_key_alias == NULL ? |
| 964 | hostname : options.host_key_alias, host_key)) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 965 | goto fail; |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 966 | if (options.check_host_ip && ip_status == HOST_NEW) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 967 | if (readonly || want_cert) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 968 | logit("%s host key for IP address " |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 969 | "'%.128s' not in list of known hosts.", |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 970 | type, ip); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 971 | else if (!add_host_to_hostfile(user_hostfiles[0], ip, |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 972 | host_key, options.hash_known_hosts)) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 973 | logit("Failed to add the %s host key for IP " |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 974 | "address '%.128s' to the list of known " |
dtucker@openbsd.org | 496aeb2 | 2015-05-28 05:41:29 +0000 | [diff] [blame] | 975 | "hosts (%.500s).", type, ip, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 976 | user_hostfiles[0]); |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 977 | else |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 978 | logit("Warning: Permanently added the %s host " |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 979 | "key for IP address '%.128s' to the list " |
| 980 | "of known hosts.", type, ip); |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 981 | } else if (options.visual_host_key) { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 982 | fp = sshkey_fingerprint(host_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 983 | options.fingerprint_hash, SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 984 | ra = sshkey_fingerprint(host_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 985 | options.fingerprint_hash, SSH_FP_RANDOMART); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 986 | if (fp == NULL || ra == NULL) |
| 987 | fatal("%s: sshkey_fingerprint fail", __func__); |
djm@openbsd.org | ac3451d | 2015-09-04 03:57:38 +0000 | [diff] [blame] | 988 | logit("Host key fingerprint is %s\n%s", fp, ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 989 | free(ra); |
| 990 | free(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 991 | } |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 992 | hostkey_trusted = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | break; |
| 994 | case HOST_NEW: |
Damien Miller | da82839 | 2006-08-05 11:35:45 +1000 | [diff] [blame] | 995 | if (options.host_key_alias == NULL && port != 0 && |
| 996 | port != SSH_DEFAULT_PORT) { |
| 997 | debug("checking without port identifier"); |
Damien Miller | ece92c8 | 2008-11-03 19:25:03 +1100 | [diff] [blame] | 998 | if (check_host_key(hostname, hostaddr, 0, host_key, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 999 | ROQUIET, user_hostfiles, num_user_hostfiles, |
| 1000 | system_hostfiles, num_system_hostfiles) == 0) { |
Damien Miller | da82839 | 2006-08-05 11:35:45 +1000 | [diff] [blame] | 1001 | debug("found matching key w/out port"); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1005 | if (readonly || want_cert) |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1006 | goto fail; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1007 | /* The host is new. */ |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1008 | if (options.strict_host_key_checking == |
| 1009 | SSH_STRICT_HOSTKEY_YES) { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | * User has requested strict host key checking. We |
| 1012 | * will not add the host key automatically. The only |
| 1013 | * alternative left is to abort. |
| 1014 | */ |
| 1015 | error("No %s host key is known for %.200s and you " |
| 1016 | "have requested strict checking.", type, host); |
| 1017 | goto fail; |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1018 | } else if (options.strict_host_key_checking == |
| 1019 | SSH_STRICT_HOSTKEY_ASK) { |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1020 | char msg1[1024], msg2[1024]; |
| 1021 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1022 | if (show_other_keys(host_hostkeys, host_key)) |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1023 | snprintf(msg1, sizeof(msg1), |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1024 | "\nbut keys of different type are already" |
| 1025 | " known for this host."); |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1026 | else |
| 1027 | snprintf(msg1, sizeof(msg1), "."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1028 | /* The default */ |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1029 | fp = sshkey_fingerprint(host_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1030 | options.fingerprint_hash, SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1031 | ra = sshkey_fingerprint(host_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1032 | options.fingerprint_hash, SSH_FP_RANDOMART); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1033 | if (fp == NULL || ra == NULL) |
| 1034 | fatal("%s: sshkey_fingerprint fail", __func__); |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1035 | msg2[0] = '\0'; |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1036 | if (options.verify_host_key_dns) { |
Damien Miller | 150b557 | 2003-11-17 21:19:29 +1100 | [diff] [blame] | 1037 | if (matching_host_key_dns) |
Damien Miller | 7392ae6 | 2003-06-11 22:05:25 +1000 | [diff] [blame] | 1038 | snprintf(msg2, sizeof(msg2), |
| 1039 | "Matching host key fingerprint" |
| 1040 | " found in DNS.\n"); |
| 1041 | else |
| 1042 | snprintf(msg2, sizeof(msg2), |
| 1043 | "No matching host key fingerprint" |
| 1044 | " found in DNS.\n"); |
| 1045 | } |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1046 | snprintf(msg, sizeof(msg), |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1047 | "The authenticity of host '%.200s (%s)' can't be " |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1048 | "established%s\n" |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1049 | "%s key fingerprint is %s.%s%s\n%s" |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1050 | "Are you sure you want to continue connecting " |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1051 | "(yes/no)? ", |
Damien Miller | 1028824 | 2008-06-30 00:04:03 +1000 | [diff] [blame] | 1052 | host, ip, msg1, type, fp, |
| 1053 | options.visual_host_key ? "\n" : "", |
| 1054 | options.visual_host_key ? ra : "", |
| 1055 | msg2); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1056 | free(ra); |
| 1057 | free(fp); |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1058 | if (!confirm(msg)) |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1059 | goto fail; |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1060 | hostkey_trusted = 1; /* user explicitly confirmed */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1061 | } |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1062 | /* |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1063 | * If in "new" or "off" strict mode, add the key automatically |
| 1064 | * to the local known_hosts file. |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1065 | */ |
Damien Miller | 1227d4c | 2005-03-02 12:06:51 +1100 | [diff] [blame] | 1066 | if (options.check_host_ip && ip_status == HOST_NEW) { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1067 | snprintf(hostline, sizeof(hostline), "%s,%s", host, ip); |
Damien Miller | 1227d4c | 2005-03-02 12:06:51 +1100 | [diff] [blame] | 1068 | hostp = hostline; |
| 1069 | if (options.hash_known_hosts) { |
| 1070 | /* Add hash of host and IP separately */ |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1071 | r = add_host_to_hostfile(user_hostfiles[0], |
| 1072 | host, host_key, options.hash_known_hosts) && |
| 1073 | add_host_to_hostfile(user_hostfiles[0], ip, |
Damien Miller | 1227d4c | 2005-03-02 12:06:51 +1100 | [diff] [blame] | 1074 | host_key, options.hash_known_hosts); |
| 1075 | } else { |
| 1076 | /* Add unhashed "host,ip" */ |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1077 | r = add_host_to_hostfile(user_hostfiles[0], |
Damien Miller | 1227d4c | 2005-03-02 12:06:51 +1100 | [diff] [blame] | 1078 | hostline, host_key, |
| 1079 | options.hash_known_hosts); |
| 1080 | } |
| 1081 | } else { |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1082 | r = add_host_to_hostfile(user_hostfiles[0], host, |
| 1083 | host_key, options.hash_known_hosts); |
Damien Miller | 1227d4c | 2005-03-02 12:06:51 +1100 | [diff] [blame] | 1084 | hostp = host; |
| 1085 | } |
| 1086 | |
| 1087 | if (!r) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1088 | logit("Failed to add the host to the list of known " |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1089 | "hosts (%.500s).", user_hostfiles[0]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1090 | else |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1091 | logit("Warning: Permanently added '%.200s' (%s) to the " |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1092 | "list of known hosts.", hostp, type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1093 | break; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1094 | case HOST_REVOKED: |
| 1095 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1096 | error("@ WARNING: REVOKED HOST KEY DETECTED! @"); |
| 1097 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1098 | error("The %s host key for %s is marked as revoked.", type, host); |
| 1099 | error("This could mean that a stolen key is being used to"); |
| 1100 | error("impersonate this host."); |
| 1101 | |
| 1102 | /* |
| 1103 | * If strict host key checking is in use, the user will have |
| 1104 | * to edit the key manually and we can only abort. |
| 1105 | */ |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1106 | if (options.strict_host_key_checking != |
| 1107 | SSH_STRICT_HOSTKEY_OFF) { |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1108 | error("%s host key for %.200s was revoked and you have " |
| 1109 | "requested strict checking.", type, host); |
| 1110 | goto fail; |
| 1111 | } |
| 1112 | goto continue_unsafe; |
| 1113 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1114 | case HOST_CHANGED: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1115 | if (want_cert) { |
| 1116 | /* |
| 1117 | * This is only a debug() since it is valid to have |
| 1118 | * CAs with wildcard DNS matches that don't match |
| 1119 | * all hosts that one might visit. |
| 1120 | */ |
| 1121 | debug("Host certificate authority does not " |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1122 | "match %s in %s:%lu", CA_MARKER, |
| 1123 | host_found->file, host_found->line); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1124 | goto fail; |
| 1125 | } |
Damien Miller | da82839 | 2006-08-05 11:35:45 +1000 | [diff] [blame] | 1126 | if (readonly == ROQUIET) |
| 1127 | goto fail; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1128 | if (options.check_host_ip && host_ip_differ) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1129 | char *key_msg; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1130 | if (ip_status == HOST_NEW) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1131 | key_msg = "is unknown"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1132 | else if (ip_status == HOST_OK) |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1133 | key_msg = "is unchanged"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1134 | else |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1135 | key_msg = "has a different value"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1136 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1137 | error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @"); |
| 1138 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1139 | error("The %s host key for %s has changed,", type, host); |
Darren Tucker | ff4454d | 2008-06-13 10:21:51 +1000 | [diff] [blame] | 1140 | error("and the key for the corresponding IP address %s", ip); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1141 | error("%s. This could either mean that", key_msg); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1142 | error("DNS SPOOFING is happening or the IP address for the host"); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1143 | error("and its host key have changed at the same time."); |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 1144 | if (ip_status != HOST_NEW) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1145 | error("Offending key for IP in %s:%lu", |
| 1146 | ip_found->file, ip_found->line); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1147 | } |
| 1148 | /* The host key has changed. */ |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1149 | warn_changed_key(host_key); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1150 | error("Add correct host key in %.100s to get rid of this message.", |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1151 | user_hostfiles[0]); |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 1152 | error("Offending %s key in %s:%lu", |
| 1153 | sshkey_type(host_found->key), |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1154 | host_found->file, host_found->line); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1155 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1156 | /* |
| 1157 | * If strict host key checking is in use, the user will have |
| 1158 | * to edit the key manually and we can only abort. |
| 1159 | */ |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1160 | if (options.strict_host_key_checking != |
| 1161 | SSH_STRICT_HOSTKEY_OFF) { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1162 | error("%s host key for %.200s has changed and you have " |
| 1163 | "requested strict checking.", type, host); |
| 1164 | goto fail; |
| 1165 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1166 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1167 | continue_unsafe: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1168 | /* |
| 1169 | * If strict host key checking has not been requested, allow |
Damien Miller | 941ac45 | 2003-06-04 20:31:53 +1000 | [diff] [blame] | 1170 | * the connection but without MITM-able authentication or |
Damien Miller | 437edb9 | 2006-08-05 09:11:13 +1000 | [diff] [blame] | 1171 | * forwarding. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1172 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1173 | if (options.password_authentication) { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1174 | error("Password authentication is disabled to avoid " |
| 1175 | "man-in-the-middle attacks."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1176 | options.password_authentication = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1177 | cancelled_forwarding = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1178 | } |
Damien Miller | 941ac45 | 2003-06-04 20:31:53 +1000 | [diff] [blame] | 1179 | if (options.kbd_interactive_authentication) { |
| 1180 | error("Keyboard-interactive authentication is disabled" |
| 1181 | " to avoid man-in-the-middle attacks."); |
| 1182 | options.kbd_interactive_authentication = 0; |
| 1183 | options.challenge_response_authentication = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1184 | cancelled_forwarding = 1; |
Damien Miller | 941ac45 | 2003-06-04 20:31:53 +1000 | [diff] [blame] | 1185 | } |
| 1186 | if (options.challenge_response_authentication) { |
| 1187 | error("Challenge/response authentication is disabled" |
| 1188 | " to avoid man-in-the-middle attacks."); |
| 1189 | options.challenge_response_authentication = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1190 | cancelled_forwarding = 1; |
Damien Miller | 941ac45 | 2003-06-04 20:31:53 +1000 | [diff] [blame] | 1191 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1192 | if (options.forward_agent) { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1193 | error("Agent forwarding is disabled to avoid " |
| 1194 | "man-in-the-middle attacks."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1195 | options.forward_agent = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1196 | cancelled_forwarding = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1197 | } |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1198 | if (options.forward_x11) { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1199 | error("X11 forwarding is disabled to avoid " |
| 1200 | "man-in-the-middle attacks."); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1201 | options.forward_x11 = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1202 | cancelled_forwarding = 1; |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1203 | } |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1204 | if (options.num_local_forwards > 0 || |
| 1205 | options.num_remote_forwards > 0) { |
| 1206 | error("Port forwarding is disabled to avoid " |
| 1207 | "man-in-the-middle attacks."); |
| 1208 | options.num_local_forwards = |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1209 | options.num_remote_forwards = 0; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1210 | cancelled_forwarding = 1; |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1211 | } |
Damien Miller | 437edb9 | 2006-08-05 09:11:13 +1000 | [diff] [blame] | 1212 | if (options.tun_open != SSH_TUNMODE_NO) { |
| 1213 | error("Tunnel forwarding is disabled to avoid " |
| 1214 | "man-in-the-middle attacks."); |
| 1215 | options.tun_open = SSH_TUNMODE_NO; |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1216 | cancelled_forwarding = 1; |
Damien Miller | 437edb9 | 2006-08-05 09:11:13 +1000 | [diff] [blame] | 1217 | } |
Darren Tucker | 068e01f | 2008-07-02 22:33:55 +1000 | [diff] [blame] | 1218 | if (options.exit_on_forward_failure && cancelled_forwarding) |
| 1219 | fatal("Error: forwarding disabled due to host key " |
| 1220 | "check failure"); |
| 1221 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1222 | /* |
| 1223 | * XXX Should permit the user to change to use the new id. |
| 1224 | * This could be done by converting the host key to an |
| 1225 | * identifying sentence, tell that the host identifies itself |
Darren Tucker | e2b3674 | 2010-01-13 22:42:34 +1100 | [diff] [blame] | 1226 | * by that sentence, and ask the user if he/she wishes to |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1227 | * accept the authentication. |
| 1228 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1229 | break; |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1230 | case HOST_FOUND: |
| 1231 | fatal("internal error"); |
| 1232 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1233 | } |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1234 | |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 1235 | if (options.check_host_ip && host_status != HOST_CHANGED && |
| 1236 | ip_status == HOST_CHANGED) { |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1237 | snprintf(msg, sizeof(msg), |
| 1238 | "Warning: the %s host key for '%.200s' " |
| 1239 | "differs from the key for the IP address '%.128s'" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1240 | "\nOffending key for IP in %s:%lu", |
| 1241 | type, host, ip, ip_found->file, ip_found->line); |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1242 | if (host_status == HOST_OK) { |
| 1243 | len = strlen(msg); |
| 1244 | snprintf(msg + len, sizeof(msg) - len, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1245 | "\nMatching host key in %s:%lu", |
| 1246 | host_found->file, host_found->line); |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1247 | } |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1248 | if (options.strict_host_key_checking == |
| 1249 | SSH_STRICT_HOSTKEY_ASK) { |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1250 | strlcat(msg, "\nAre you sure you want " |
| 1251 | "to continue connecting (yes/no)? ", sizeof(msg)); |
| 1252 | if (!confirm(msg)) |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1253 | goto fail; |
djm@openbsd.org | 22376d2 | 2017-09-03 23:33:13 +0000 | [diff] [blame] | 1254 | } else if (options.strict_host_key_checking != |
| 1255 | SSH_STRICT_HOSTKEY_OFF) { |
| 1256 | logit("%s", msg); |
| 1257 | error("Exiting, you have requested strict checking."); |
| 1258 | goto fail; |
Damien Miller | 49d795c | 2002-01-22 23:34:12 +1100 | [diff] [blame] | 1259 | } else { |
Damien Miller | ab2db41 | 2003-06-02 19:09:13 +1000 | [diff] [blame] | 1260 | logit("%s", msg); |
Ben Lindstrom | 5c1fbab | 2001-01-03 03:51:15 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
djm@openbsd.org | 8d4f872 | 2015-01-26 03:04:45 +0000 | [diff] [blame] | 1264 | if (!hostkey_trusted && options.update_hostkeys) { |
| 1265 | debug("%s: hostkey not known or explicitly trusted: " |
| 1266 | "disabling UpdateHostkeys", __func__); |
| 1267 | options.update_hostkeys = 0; |
| 1268 | } |
| 1269 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1270 | free(ip); |
| 1271 | free(host); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1272 | if (host_hostkeys != NULL) |
| 1273 | free_hostkeys(host_hostkeys); |
| 1274 | if (ip_hostkeys != NULL) |
| 1275 | free_hostkeys(ip_hostkeys); |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1276 | return 0; |
| 1277 | |
| 1278 | fail: |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1279 | if (want_cert && host_status != HOST_REVOKED) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1280 | /* |
| 1281 | * No matching certificate. Downgrade cert to raw key and |
| 1282 | * search normally. |
| 1283 | */ |
| 1284 | debug("No matching CA found. Retry with plain key"); |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 1285 | if ((r = sshkey_from_private(host_key, &raw_key)) != 0) |
| 1286 | fatal("%s: sshkey_from_private: %s", |
| 1287 | __func__, ssh_err(r)); |
| 1288 | if ((r = sshkey_drop_cert(raw_key)) != 0) |
| 1289 | fatal("Couldn't drop certificate: %s", ssh_err(r)); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1290 | host_key = raw_key; |
| 1291 | goto retry; |
| 1292 | } |
dtucker@openbsd.org | f1f047f | 2018-02-07 22:52:45 +0000 | [diff] [blame] | 1293 | sshkey_free(raw_key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1294 | free(ip); |
| 1295 | free(host); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1296 | if (host_hostkeys != NULL) |
| 1297 | free_hostkeys(host_hostkeys); |
| 1298 | if (ip_hostkeys != NULL) |
| 1299 | free_hostkeys(ip_hostkeys); |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1300 | return -1; |
| 1301 | } |
| 1302 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1303 | /* returns 0 if key verifies or -1 if key does NOT verify */ |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1304 | int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1305 | verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1306 | { |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1307 | u_int i; |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1308 | int r = -1, flags = 0; |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1309 | char valid[64], *fp = NULL, *cafp = NULL; |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1310 | struct sshkey *plain = NULL; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1311 | |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1312 | if ((fp = sshkey_fingerprint(host_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1313 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1314 | error("%s: fingerprint host key: %s", __func__, ssh_err(r)); |
| 1315 | r = -1; |
| 1316 | goto out; |
| 1317 | } |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 1318 | |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1319 | if (sshkey_is_cert(host_key)) { |
| 1320 | if ((cafp = sshkey_fingerprint(host_key->cert->signature_key, |
| 1321 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 1322 | error("%s: fingerprint CA key: %s", |
| 1323 | __func__, ssh_err(r)); |
| 1324 | r = -1; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | sshkey_format_cert_validity(host_key->cert, |
| 1328 | valid, sizeof(valid)); |
| 1329 | debug("Server host certificate: %s %s, serial %llu " |
| 1330 | "ID \"%s\" CA %s %s valid %s", |
| 1331 | sshkey_ssh_name(host_key), fp, |
djm@openbsd.org | 8ca915f | 2015-11-20 01:45:29 +0000 | [diff] [blame] | 1332 | (unsigned long long)host_key->cert->serial, |
| 1333 | host_key->cert->key_id, |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1334 | sshkey_ssh_name(host_key->cert->signature_key), cafp, |
| 1335 | valid); |
| 1336 | for (i = 0; i < host_key->cert->nprincipals; i++) { |
| 1337 | debug2("Server host certificate hostname: %s", |
| 1338 | host_key->cert->principals[i]); |
| 1339 | } |
| 1340 | } else { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1341 | debug("Server host key: %s %s", sshkey_ssh_name(host_key), fp); |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1342 | } |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (sshkey_equal(previous_host_key, host_key)) { |
| 1345 | debug2("%s: server host key %s %s matches cached key", |
| 1346 | __func__, sshkey_type(host_key), fp); |
| 1347 | r = 0; |
| 1348 | goto out; |
| 1349 | } |
| 1350 | |
| 1351 | /* Check in RevokedHostKeys file if specified */ |
| 1352 | if (options.revoked_host_keys != NULL) { |
| 1353 | r = sshkey_check_revoked(host_key, options.revoked_host_keys); |
| 1354 | switch (r) { |
| 1355 | case 0: |
| 1356 | break; /* not revoked */ |
| 1357 | case SSH_ERR_KEY_REVOKED: |
| 1358 | error("Host key %s %s revoked by file %s", |
| 1359 | sshkey_type(host_key), fp, |
| 1360 | options.revoked_host_keys); |
| 1361 | r = -1; |
| 1362 | goto out; |
| 1363 | default: |
| 1364 | error("Error checking host key %s %s in " |
| 1365 | "revoked keys file %s: %s", sshkey_type(host_key), |
| 1366 | fp, options.revoked_host_keys, ssh_err(r)); |
| 1367 | r = -1; |
| 1368 | goto out; |
| 1369 | } |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1370 | } |
| 1371 | |
Damien Miller | 7d6a9fb | 2014-04-20 13:23:43 +1000 | [diff] [blame] | 1372 | if (options.verify_host_key_dns) { |
| 1373 | /* |
| 1374 | * XXX certs are not yet supported for DNS, so downgrade |
| 1375 | * them and try the plain key. |
| 1376 | */ |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1377 | if ((r = sshkey_from_private(host_key, &plain)) != 0) |
| 1378 | goto out; |
| 1379 | if (sshkey_is_cert(plain)) |
| 1380 | sshkey_drop_cert(plain); |
Damien Miller | 7d6a9fb | 2014-04-20 13:23:43 +1000 | [diff] [blame] | 1381 | if (verify_host_key_dns(host, hostaddr, plain, &flags) == 0) { |
| 1382 | if (flags & DNS_VERIFY_FOUND) { |
| 1383 | if (options.verify_host_key_dns == 1 && |
| 1384 | flags & DNS_VERIFY_MATCH && |
| 1385 | flags & DNS_VERIFY_SECURE) { |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1386 | r = 0; |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1387 | goto out; |
Damien Miller | 7d6a9fb | 2014-04-20 13:23:43 +1000 | [diff] [blame] | 1388 | } |
| 1389 | if (flags & DNS_VERIFY_MATCH) { |
| 1390 | matching_host_key_dns = 1; |
| 1391 | } else { |
djm@openbsd.org | aea59a0 | 2017-09-14 04:32:21 +0000 | [diff] [blame] | 1392 | warn_changed_key(plain); |
| 1393 | error("Update the SSHFP RR in DNS " |
| 1394 | "with the new host key to get rid " |
| 1395 | "of this message."); |
Damien Miller | 7d6a9fb | 2014-04-20 13:23:43 +1000 | [diff] [blame] | 1396 | } |
Damien Miller | 150b557 | 2003-11-17 21:19:29 +1100 | [diff] [blame] | 1397 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1398 | } |
| 1399 | } |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1400 | r = check_host_key(host, hostaddr, options.port, host_key, RDRW, |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 1401 | options.user_hostfiles, options.num_user_hostfiles, |
| 1402 | options.system_hostfiles, options.num_system_hostfiles); |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1403 | |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 1404 | out: |
| 1405 | sshkey_free(plain); |
| 1406 | free(fp); |
djm@openbsd.org | f96516d | 2015-11-19 01:09:38 +0000 | [diff] [blame] | 1407 | free(cafp); |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1408 | if (r == 0 && host_key != NULL) { |
markus@openbsd.org | beb965b | 2017-05-30 14:13:40 +0000 | [diff] [blame] | 1409 | sshkey_free(previous_host_key); |
| 1410 | r = sshkey_from_private(host_key, &previous_host_key); |
Damien Miller | 6b37fbb | 2014-07-04 08:59:24 +1000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | return r; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1414 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1415 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1416 | /* |
| 1417 | * Starts a dialog with the server, and authenticates the current user on the |
| 1418 | * server. This does not need any extra privileges. The basic connection |
| 1419 | * to the server must already have been established before this is called. |
| 1420 | * If login fails, this function prints an error and never returns. |
| 1421 | * This function does not require super-user privileges. |
| 1422 | */ |
| 1423 | void |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1424 | ssh_login(Sensitive *sensitive, const char *orighost, |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1425 | struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms) |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1426 | { |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1427 | char *host; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1428 | char *server_user, *local_user; |
| 1429 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1430 | local_user = xstrdup(pw->pw_name); |
| 1431 | server_user = options.user ? options.user : local_user; |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1432 | |
| 1433 | /* Convert the user-supplied hostname into all lowercase. */ |
| 1434 | host = xstrdup(orighost); |
Damien Miller | 0faf747 | 2013-10-17 11:47:23 +1100 | [diff] [blame] | 1435 | lowercase(host); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1436 | |
| 1437 | /* Exchange protocol version identification strings with the server. */ |
Damien Miller | 67bd062 | 2007-09-17 12:06:57 +1000 | [diff] [blame] | 1438 | ssh_exchange_identification(timeout_ms); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1439 | |
| 1440 | /* Put the connection into non-blocking mode. */ |
| 1441 | packet_set_nonblocking(); |
| 1442 | |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1443 | /* key exchange */ |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1444 | /* authenticate user */ |
dtucker@openbsd.org | bb2289e | 2015-04-14 04:17:03 +0000 | [diff] [blame] | 1445 | debug("Authenticating to %s:%d as '%s'", host, port, server_user); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1446 | ssh_kex2(host, hostaddr, port); |
| 1447 | ssh_userauth2(local_user, server_user, host, sensitive); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1448 | free(local_user); |
Damien Miller | 396691a | 2000-01-20 22:44:08 +1100 | [diff] [blame] | 1449 | } |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1450 | |
| 1451 | void |
| 1452 | ssh_put_password(char *password) |
| 1453 | { |
| 1454 | int size; |
| 1455 | char *padded; |
| 1456 | |
Ben Lindstrom | d20d0f3 | 2001-03-10 17:22:20 +0000 | [diff] [blame] | 1457 | if (datafellows & SSH_BUG_PASSWORDPAD) { |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1458 | packet_put_cstring(password); |
Ben Lindstrom | d20d0f3 | 2001-03-10 17:22:20 +0000 | [diff] [blame] | 1459 | return; |
| 1460 | } |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 1461 | size = ROUNDUP(strlen(password) + 1, 32); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1462 | padded = xcalloc(1, size); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1463 | strlcpy(padded, password, size); |
| 1464 | packet_put_string(padded, size); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1465 | explicit_bzero(padded, size); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1466 | free(padded); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1467 | } |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1468 | |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1469 | /* print all known host keys for a given host, but skip keys of given type */ |
| 1470 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1471 | show_other_keys(struct hostkeys *hostkeys, struct sshkey *key) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1472 | { |
Damien Miller | 106bf1c | 2013-12-29 17:54:03 +1100 | [diff] [blame] | 1473 | int type[] = { |
Damien Miller | 106bf1c | 2013-12-29 17:54:03 +1100 | [diff] [blame] | 1474 | KEY_RSA, |
| 1475 | KEY_DSA, |
| 1476 | KEY_ECDSA, |
| 1477 | KEY_ED25519, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1478 | KEY_XMSS, |
Damien Miller | 106bf1c | 2013-12-29 17:54:03 +1100 | [diff] [blame] | 1479 | -1 |
| 1480 | }; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1481 | int i, ret = 0; |
| 1482 | char *fp, *ra; |
| 1483 | const struct hostkey_entry *found; |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1484 | |
| 1485 | for (i = 0; type[i] != -1; i++) { |
| 1486 | if (type[i] == key->type) |
| 1487 | continue; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1488 | if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found)) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1489 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1490 | fp = sshkey_fingerprint(found->key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1491 | options.fingerprint_hash, SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1492 | ra = sshkey_fingerprint(found->key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1493 | options.fingerprint_hash, SSH_FP_RANDOMART); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1494 | if (fp == NULL || ra == NULL) |
| 1495 | fatal("%s: sshkey_fingerprint fail", __func__); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1496 | logit("WARNING: %s key found for host %s\n" |
| 1497 | "in %s:%lu\n" |
| 1498 | "%s key fingerprint %s.", |
| 1499 | key_type(found->key), |
| 1500 | found->host, found->file, found->line, |
| 1501 | key_type(found->key), fp); |
| 1502 | if (options.visual_host_key) |
| 1503 | logit("%s", ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1504 | free(ra); |
| 1505 | free(fp); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1506 | ret = 1; |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1507 | } |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 1508 | return ret; |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 1509 | } |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1510 | |
| 1511 | static void |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1512 | warn_changed_key(struct sshkey *host_key) |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1513 | { |
| 1514 | char *fp; |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1515 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1516 | fp = sshkey_fingerprint(host_key, options.fingerprint_hash, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1517 | SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1518 | if (fp == NULL) |
| 1519 | fatal("%s: sshkey_fingerprint fail", __func__); |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1520 | |
| 1521 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1522 | error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); |
| 1523 | error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
| 1524 | error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!"); |
| 1525 | error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!"); |
Damien Miller | 05c8997 | 2011-01-06 22:42:04 +1100 | [diff] [blame] | 1526 | error("It is also possible that a host key has just been changed."); |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1527 | error("The fingerprint for the %s key sent by the remote host is\n%s.", |
djm@openbsd.org | aea59a0 | 2017-09-14 04:32:21 +0000 | [diff] [blame] | 1528 | key_type(host_key), fp); |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1529 | error("Please contact your system administrator."); |
| 1530 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1531 | free(fp); |
Damien Miller | 5a38897 | 2003-11-17 21:10:47 +1100 | [diff] [blame] | 1532 | } |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1533 | |
| 1534 | /* |
| 1535 | * Execute a local command |
| 1536 | */ |
| 1537 | int |
| 1538 | ssh_local_cmd(const char *args) |
| 1539 | { |
| 1540 | char *shell; |
| 1541 | pid_t pid; |
| 1542 | int status; |
Damien Miller | ed3a8eb | 2011-01-07 10:02:52 +1100 | [diff] [blame] | 1543 | void (*osighand)(int); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1544 | |
| 1545 | if (!options.permit_local_command || |
| 1546 | args == NULL || !*args) |
| 1547 | return (1); |
| 1548 | |
Damien Miller | 38d9a96 | 2010-10-07 22:07:11 +1100 | [diff] [blame] | 1549 | if ((shell = getenv("SHELL")) == NULL || *shell == '\0') |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1550 | shell = _PATH_BSHELL; |
| 1551 | |
Damien Miller | ed3a8eb | 2011-01-07 10:02:52 +1100 | [diff] [blame] | 1552 | osighand = signal(SIGCHLD, SIG_DFL); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1553 | pid = fork(); |
| 1554 | if (pid == 0) { |
Damien Miller | 6fb6fd5 | 2011-01-16 23:17:45 +1100 | [diff] [blame] | 1555 | signal(SIGPIPE, SIG_DFL); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1556 | debug3("Executing %s -c \"%s\"", shell, args); |
| 1557 | execl(shell, shell, "-c", args, (char *)NULL); |
| 1558 | error("Couldn't execute %s -c \"%s\": %s", |
| 1559 | shell, args, strerror(errno)); |
| 1560 | _exit(1); |
| 1561 | } else if (pid == -1) |
| 1562 | fatal("fork failed: %.100s", strerror(errno)); |
| 1563 | while (waitpid(pid, &status, 0) == -1) |
| 1564 | if (errno != EINTR) |
| 1565 | fatal("Couldn't wait for child: %s", strerror(errno)); |
Damien Miller | ed3a8eb | 2011-01-07 10:02:52 +1100 | [diff] [blame] | 1566 | signal(SIGCHLD, osighand); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1567 | |
| 1568 | if (!WIFEXITED(status)) |
| 1569 | return (1); |
| 1570 | |
| 1571 | return (WEXITSTATUS(status)); |
| 1572 | } |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1573 | |
| 1574 | void |
djm@openbsd.org | d9e5cf0 | 2018-02-10 09:25:34 +0000 | [diff] [blame] | 1575 | maybe_add_key_to_agent(char *authfile, const struct sshkey *private, |
| 1576 | char *comment, char *passphrase) |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1577 | { |
| 1578 | int auth_sock = -1, r; |
| 1579 | |
| 1580 | if (options.add_keys_to_agent == 0) |
| 1581 | return; |
| 1582 | |
| 1583 | if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) { |
| 1584 | debug3("no authentication agent, not adding key"); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | if (options.add_keys_to_agent == 2 && |
| 1589 | !ask_permission("Add key %s (%s) to agent?", authfile, comment)) { |
| 1590 | debug3("user denied adding this key"); |
dtucker@openbsd.org | 566b3a4 | 2017-03-10 03:22:40 +0000 | [diff] [blame] | 1591 | close(auth_sock); |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | if ((r = ssh_add_identity_constrained(auth_sock, private, comment, 0, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 1596 | (options.add_keys_to_agent == 3), 0)) == 0) |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1597 | debug("identity added to agent: %s", authfile); |
| 1598 | else |
| 1599 | debug("could not add identity to agent: %s (%d)", authfile, r); |
dtucker@openbsd.org | 566b3a4 | 2017-03-10 03:22:40 +0000 | [diff] [blame] | 1600 | close(auth_sock); |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1601 | } |