blob: 1507934d878d79fe2a3c59d69f001d34fbb74dd3 [file] [log] [blame]
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001/* $OpenBSD: sshconnect.c,v 1.267 2015/11/19 01:09:38 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
Damien Millere4340be2000-09-16 13:29:08 +11008 *
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 Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110017
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000018#include <sys/param.h> /* roundup */
Damien Miller9cf6d072006-03-15 11:29:24 +110019#include <sys/types.h>
20#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110021#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100022#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100026
27#include <netinet/in.h>
Darren Tucker46aa3e02006-09-02 15:32:40 +100028#include <arpa/inet.h>
Damien Miller03e20032006-03-15 11:16:59 +110029
Damien Millerc7b06362006-03-15 11:53:45 +110030#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100031#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110032#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100033#include <netdb.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110034#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110035#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110036#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100037#include <pwd.h>
Damien Miller9c0c31d2010-10-12 13:30:44 +110038#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100041#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100043#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100046#include "key.h"
47#include "hostfile.h"
48#include "ssh.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "rsa.h"
Damien Miller1383bd82000-04-06 12:32:37 +100050#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "uidswap.h"
53#include "compat.h"
Damien Miller450a7a12000-03-26 13:04:51 +100054#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "sshconnect.h"
Damien Miller450a7a12000-03-26 13:04:51 +100056#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100058#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "readconf.h"
60#include "atomicio.h"
Damien Miller37876e92003-05-15 10:19:46 +100061#include "dns.h"
Darren Tuckerc5564e12009-06-21 18:53:53 +100062#include "roaming.h"
Damien Miller1262b662013-08-21 02:44:24 +100063#include "monitor_fdpass.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110064#include "ssh2.h"
Damien Millerb7576772006-07-10 20:23:39 +100065#include "version.h"
djm@openbsd.org5e39a492014-12-04 02:24:32 +000066#include "authfile.h"
67#include "ssherr.h"
jcs@openbsd.orgf361df42015-11-15 22:26:49 +000068#include "authfd.h"
Damien Miller37876e92003-05-15 10:19:46 +100069
Damien Millereba71ba2000-04-29 23:57:08 +100070char *client_version_string = NULL;
71char *server_version_string = NULL;
Damien Miller6b37fbb2014-07-04 08:59:24 +100072Key *previous_host_key = NULL;
Damien Miller1383bd82000-04-06 12:32:37 +100073
Damien Millerc1af1d52005-11-05 15:08:57 +110074static int matching_host_key_dns = 0;
Damien Miller7392ae62003-06-11 22:05:25 +100075
Damien Millera41ccca2010-10-07 22:07:32 +110076static pid_t proxy_command_pid = 0;
77
Ben Lindstromf9c48842002-06-11 16:37:51 +000078/* import */
Damien Milleraae6c611999-12-06 11:47:28 +110079extern Options options;
Damien Miller34132e52000-01-14 15:45:46 +110080extern char *__progname;
Ben Lindstromf9c48842002-06-11 16:37:51 +000081extern uid_t original_real_uid;
82extern uid_t original_effective_uid;
Damien Milleraae6c611999-12-06 11:47:28 +110083
Damien Millerd925dcd2010-12-01 12:21:51 +110084static int show_other_keys(struct hostkeys *, Key *);
Damien Miller5a388972003-11-17 21:10:47 +110085static void warn_changed_key(Key *);
Ben Lindstrom3ed66402002-08-01 01:21:56 +000086
Damien Miller1262b662013-08-21 02:44:24 +100087/* Expand a proxy command */
88static char *
89expand_proxy_command(const char *proxy_command, const char *user,
90 const char *host, int port)
91{
92 char *tmp, *ret, strport[NI_MAXSERV];
93
Damien Miller0faf7472013-10-17 11:47:23 +110094 snprintf(strport, sizeof strport, "%d", port);
Damien Miller1262b662013-08-21 02:44:24 +100095 xasprintf(&tmp, "exec %s", proxy_command);
96 ret = percent_expand(tmp, "h", host, "p", strport,
97 "r", options.user, (char *)NULL);
98 free(tmp);
99 return ret;
100}
101
102/*
103 * Connect to the given ssh server using a proxy command that passes a
104 * a connected fd back to us.
105 */
106static int
107ssh_proxy_fdpass_connect(const char *host, u_short port,
108 const char *proxy_command)
109{
110 char *command_string;
111 int sp[2], sock;
112 pid_t pid;
113 char *shell;
114
115 if ((shell = getenv("SHELL")) == NULL)
116 shell = _PATH_BSHELL;
117
118 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0)
119 fatal("Could not create socketpair to communicate with "
120 "proxy dialer: %.100s", strerror(errno));
121
122 command_string = expand_proxy_command(proxy_command, options.user,
123 host, port);
124 debug("Executing proxy dialer command: %.500s", command_string);
125
126 /* Fork and execute the proxy command. */
127 if ((pid = fork()) == 0) {
128 char *argv[10];
129
130 /* Child. Permanently give up superuser privileges. */
131 permanently_drop_suid(original_real_uid);
132
133 close(sp[1]);
134 /* Redirect stdin and stdout. */
135 if (sp[0] != 0) {
136 if (dup2(sp[0], 0) < 0)
137 perror("dup2 stdin");
138 }
139 if (sp[0] != 1) {
140 if (dup2(sp[0], 1) < 0)
141 perror("dup2 stdout");
142 }
143 if (sp[0] >= 2)
144 close(sp[0]);
145
146 /*
147 * Stderr is left as it is so that error messages get
148 * printed on the user's terminal.
149 */
150 argv[0] = shell;
151 argv[1] = "-c";
152 argv[2] = command_string;
153 argv[3] = NULL;
154
155 /*
156 * Execute the proxy command.
157 * Note that we gave up any extra privileges above.
158 */
159 execv(argv[0], argv);
160 perror(argv[0]);
161 exit(1);
162 }
163 /* Parent. */
164 if (pid < 0)
165 fatal("fork failed: %.100s", strerror(errno));
166 close(sp[0]);
167 free(command_string);
168
169 if ((sock = mm_receive_fd(sp[1])) == -1)
170 fatal("proxy dialer did not pass back a connection");
171
172 while (waitpid(pid, NULL, 0) == -1)
173 if (errno != EINTR)
174 fatal("Couldn't wait for child: %s", strerror(errno));
175
176 /* Set the connection file descriptors. */
177 packet_set_connection(sock, sock);
Damien Miller1262b662013-08-21 02:44:24 +1000178
179 return 0;
180}
181
Damien Miller95def091999-11-25 00:26:21 +1100182/*
183 * Connect to the given ssh server using a proxy command.
184 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int
Ben Lindstromf9c48842002-06-11 16:37:51 +0000186ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187{
Damien Miller1262b662013-08-21 02:44:24 +1000188 char *command_string;
Damien Miller95def091999-11-25 00:26:21 +1100189 int pin[2], pout[2];
Damien Miller166fca82000-04-20 07:42:21 +1000190 pid_t pid;
Damien Miller1262b662013-08-21 02:44:24 +1000191 char *shell;
Damien Miller1d824ab2007-09-17 11:58:04 +1000192
Damien Miller38d9a962010-10-07 22:07:11 +1100193 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
Damien Miller1d824ab2007-09-17 11:58:04 +1000194 shell = _PATH_BSHELL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Miller95def091999-11-25 00:26:21 +1100196 /* Create pipes for communicating with the proxy. */
197 if (pipe(pin) < 0 || pipe(pout) < 0)
198 fatal("Could not create pipes to communicate with the proxy: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100199 strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Miller1262b662013-08-21 02:44:24 +1000201 command_string = expand_proxy_command(proxy_command, options.user,
202 host, port);
Damien Miller95def091999-11-25 00:26:21 +1100203 debug("Executing proxy command: %.500s", command_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller95def091999-11-25 00:26:21 +1100205 /* Fork and execute the proxy command. */
206 if ((pid = fork()) == 0) {
207 char *argv[10];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Miller95def091999-11-25 00:26:21 +1100209 /* Child. Permanently give up superuser privileges. */
Damien Miller2e5fe882006-06-13 13:10:00 +1000210 permanently_drop_suid(original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100211
212 /* Redirect stdin and stdout. */
213 close(pin[1]);
214 if (pin[0] != 0) {
215 if (dup2(pin[0], 0) < 0)
216 perror("dup2 stdin");
217 close(pin[0]);
218 }
219 close(pout[0]);
220 if (dup2(pout[1], 1) < 0)
221 perror("dup2 stdout");
222 /* Cannot be 1 because pin allocated two descriptors. */
223 close(pout[1]);
224
225 /* Stderr is left as it is so that error messages get
226 printed on the user's terminal. */
Damien Miller1d824ab2007-09-17 11:58:04 +1000227 argv[0] = shell;
Damien Miller95def091999-11-25 00:26:21 +1100228 argv[1] = "-c";
229 argv[2] = command_string;
230 argv[3] = NULL;
231
232 /* Execute the proxy command. Note that we gave up any
233 extra privileges above. */
Damien Miller6fb6fd52011-01-16 23:17:45 +1100234 signal(SIGPIPE, SIG_DFL);
Kevin Stevesfcd5d602001-01-07 11:45:22 +0000235 execv(argv[0], argv);
236 perror(argv[0]);
Damien Miller95def091999-11-25 00:26:21 +1100237 exit(1);
238 }
239 /* Parent. */
240 if (pid < 0)
241 fatal("fork failed: %.100s", strerror(errno));
Damien Miller8c4e18a2002-09-19 12:05:02 +1000242 else
243 proxy_command_pid = pid; /* save pid to clean up later */
Damien Miller95def091999-11-25 00:26:21 +1100244
245 /* Close child side of the descriptors. */
246 close(pin[0]);
247 close(pout[1]);
248
249 /* Free the command name. */
Darren Tuckera627d422013-06-02 07:31:17 +1000250 free(command_string);
Damien Miller95def091999-11-25 00:26:21 +1100251
252 /* Set the connection file descriptors. */
253 packet_set_connection(pout[0], pin[1]);
254
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000255 /* Indicate OK return */
256 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257}
258
Damien Millera41ccca2010-10-07 22:07:32 +1100259void
260ssh_kill_proxy_command(void)
261{
262 /*
263 * Send SIGHUP to proxy command if used. We don't wait() in
264 * case it hangs and instead rely on init to reap the child
265 */
266 if (proxy_command_pid > 1)
Damien Miller45fcdaa2010-10-07 22:07:58 +1100267 kill(proxy_command_pid, SIGHUP);
Damien Millera41ccca2010-10-07 22:07:32 +1100268}
269
Damien Miller95def091999-11-25 00:26:21 +1100270/*
271 * Creates a (possibly privileged) socket for use as the ssh connection.
272 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000273static int
Damien Miller2372ace2003-05-14 13:42:23 +1000274ssh_create_socket(int privileged, struct addrinfo *ai)
Damien Miller95def091999-11-25 00:26:21 +1100275{
Darren Tuckere6e52f82013-10-10 10:28:07 +1100276 int sock, r, gaierr;
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100277 struct addrinfo hints, *res = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Darren Tucker7bd98e72010-01-10 10:31:12 +1100279 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100280 if (sock < 0) {
Darren Tuckere6e52f82013-10-10 10:28:07 +1100281 error("socket: %s", strerror(errno));
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100282 return -1;
283 }
284 fcntl(sock, F_SETFD, FD_CLOEXEC);
Ben Lindstrome0f88042001-04-30 13:06:24 +0000285
286 /* Bind the socket to an alternative local IP address */
Darren Tuckere6e52f82013-10-10 10:28:07 +1100287 if (options.bind_address == NULL && !privileged)
Ben Lindstrome0f88042001-04-30 13:06:24 +0000288 return sock;
289
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100290 if (options.bind_address) {
291 memset(&hints, 0, sizeof(hints));
292 hints.ai_family = ai->ai_family;
293 hints.ai_socktype = ai->ai_socktype;
294 hints.ai_protocol = ai->ai_protocol;
295 hints.ai_flags = AI_PASSIVE;
296 gaierr = getaddrinfo(options.bind_address, NULL, &hints, &res);
297 if (gaierr) {
298 error("getaddrinfo: %s: %s", options.bind_address,
299 ssh_gai_strerror(gaierr));
300 close(sock);
301 return -1;
302 }
Ben Lindstrome0f88042001-04-30 13:06:24 +0000303 }
Darren Tuckere6e52f82013-10-10 10:28:07 +1100304 /*
305 * If we are running as root and want to connect to a privileged
306 * port, bind our own socket to a privileged port.
307 */
308 if (privileged) {
309 PRIV_START;
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100310 r = bindresvport_sa(sock, res ? res->ai_addr : NULL);
Darren Tuckere6e52f82013-10-10 10:28:07 +1100311 PRIV_END;
312 if (r < 0) {
313 error("bindresvport_sa: af=%d %s", ai->ai_family,
314 strerror(errno));
315 goto fail;
316 }
317 } else {
318 if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
319 error("bind: %s: %s", options.bind_address,
320 strerror(errno));
321 fail:
322 close(sock);
323 freeaddrinfo(res);
324 return -1;
325 }
Ben Lindstrome0f88042001-04-30 13:06:24 +0000326 }
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100327 if (res != NULL)
328 freeaddrinfo(res);
Damien Miller95def091999-11-25 00:26:21 +1100329 return sock;
330}
331
Damien Millerb78d5eb2003-05-16 11:39:04 +1000332static int
333timeout_connect(int sockfd, const struct sockaddr *serv_addr,
Damien Miller67bd0622007-09-17 12:06:57 +1000334 socklen_t addrlen, int *timeoutp)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000335{
336 fd_set *fdset;
Damien Miller67bd0622007-09-17 12:06:57 +1000337 struct timeval tv, t_start;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000338 socklen_t optlen;
Damien Miller07d86be2006-03-26 14:19:21 +1100339 int optval, rc, result = -1;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000340
Damien Miller67bd0622007-09-17 12:06:57 +1000341 gettimeofday(&t_start, NULL);
342
343 if (*timeoutp <= 0) {
344 result = connect(sockfd, serv_addr, addrlen);
345 goto done;
346 }
Damien Millerb78d5eb2003-05-16 11:39:04 +1000347
Damien Millerf6723f02004-01-27 21:21:27 +1100348 set_nonblock(sockfd);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000349 rc = connect(sockfd, serv_addr, addrlen);
Damien Millerf6723f02004-01-27 21:21:27 +1100350 if (rc == 0) {
351 unset_nonblock(sockfd);
Damien Miller67bd0622007-09-17 12:06:57 +1000352 result = 0;
353 goto done;
Damien Millerf6723f02004-01-27 21:21:27 +1100354 }
Damien Miller67bd0622007-09-17 12:06:57 +1000355 if (errno != EINPROGRESS) {
356 result = -1;
357 goto done;
358 }
Damien Millerb78d5eb2003-05-16 11:39:04 +1000359
deraadt@openbsd.orgce445b02015-08-20 22:32:42 +0000360 fdset = xcalloc(howmany(sockfd + 1, NFDBITS),
Damien Miller07d86be2006-03-26 14:19:21 +1100361 sizeof(fd_mask));
Damien Millerb78d5eb2003-05-16 11:39:04 +1000362 FD_SET(sockfd, fdset);
Damien Miller67bd0622007-09-17 12:06:57 +1000363 ms_to_timeval(&tv, *timeoutp);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000364
Darren Tucker47eede72005-03-14 23:08:12 +1100365 for (;;) {
Damien Millerb78d5eb2003-05-16 11:39:04 +1000366 rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
367 if (rc != -1 || errno != EINTR)
368 break;
369 }
370
Darren Tucker47eede72005-03-14 23:08:12 +1100371 switch (rc) {
Damien Millerb78d5eb2003-05-16 11:39:04 +1000372 case 0:
373 /* Timed out */
374 errno = ETIMEDOUT;
Damien Miller08293fa2003-05-23 18:44:41 +1000375 break;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000376 case -1:
377 /* Select error */
Damien Millera8e06ce2003-11-21 23:48:55 +1100378 debug("select: %s", strerror(errno));
Damien Miller08293fa2003-05-23 18:44:41 +1000379 break;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000380 case 1:
381 /* Completed or failed */
382 optval = 0;
383 optlen = sizeof(optval);
Damien Millera8e06ce2003-11-21 23:48:55 +1100384 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
Darren Tuckerbd5361b2003-09-22 20:59:16 +1000385 &optlen) == -1) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100386 debug("getsockopt: %s", strerror(errno));
Damien Miller08293fa2003-05-23 18:44:41 +1000387 break;
Darren Tuckerbd5361b2003-09-22 20:59:16 +1000388 }
Damien Millerb78d5eb2003-05-16 11:39:04 +1000389 if (optval != 0) {
390 errno = optval;
Damien Miller08293fa2003-05-23 18:44:41 +1000391 break;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000392 }
Damien Miller08293fa2003-05-23 18:44:41 +1000393 result = 0;
Damien Millerf6723f02004-01-27 21:21:27 +1100394 unset_nonblock(sockfd);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000395 break;
396 default:
397 /* Should not occur */
398 fatal("Bogus return (%d) from select()", rc);
399 }
400
Darren Tuckera627d422013-06-02 07:31:17 +1000401 free(fdset);
Damien Miller67bd0622007-09-17 12:06:57 +1000402
403 done:
404 if (result == 0 && *timeoutp > 0) {
405 ms_subtract_diff(&t_start, timeoutp);
406 if (*timeoutp <= 0) {
407 errno = ETIMEDOUT;
408 result = -1;
409 }
410 }
411
Damien Miller08293fa2003-05-23 18:44:41 +1000412 return (result);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000413}
414
Damien Miller95def091999-11-25 00:26:21 +1100415/*
Damien Miller34132e52000-01-14 15:45:46 +1100416 * Opens a TCP/IP connection to the remote server on the given host.
417 * The address of the remote host will be returned in hostaddr.
Ben Lindstromf9c48842002-06-11 16:37:51 +0000418 * If port is 0, the default port will be used. If needpriv is true,
Damien Miller95def091999-11-25 00:26:21 +1100419 * a privileged port will be allocated to make the connection.
Ben Lindstromf9c48842002-06-11 16:37:51 +0000420 * This requires super-user privileges if needpriv is true.
Damien Miller95def091999-11-25 00:26:21 +1100421 * Connection_attempts specifies the maximum number of tries (one per
422 * second). If proxy_command is non-NULL, it specifies the command (with %h
423 * and %p substituted for host and port, respectively) to use to contact
424 * the daemon.
425 */
Damien Miller0faf7472013-10-17 11:47:23 +1100426static int
427ssh_connect_direct(const char *host, struct addrinfo *aitop,
428 struct sockaddr_storage *hostaddr, u_short port, int family,
429 int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
Damien Miller95def091999-11-25 00:26:21 +1100430{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000431 int on = 1;
432 int sock = -1, attempt;
433 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0faf7472013-10-17 11:47:23 +1100434 struct addrinfo *ai;
Damien Miller95def091999-11-25 00:26:21 +1100435
djm@openbsd.org46152af2015-09-04 04:55:24 +0000436 debug2("%s: needpriv %d", __func__, needpriv);
Damien Miller95def091999-11-25 00:26:21 +1100437
Damien Miller56e5e6a2006-04-23 12:08:59 +1000438 for (attempt = 0; attempt < connection_attempts; attempt++) {
Damien Millerf4bcd102006-10-24 03:02:23 +1000439 if (attempt > 0) {
440 /* Sleep a moment before retrying. */
441 sleep(1);
Damien Miller95def091999-11-25 00:26:21 +1100442 debug("Trying again...");
Damien Millerf4bcd102006-10-24 03:02:23 +1000443 }
Damien Miller56e5e6a2006-04-23 12:08:59 +1000444 /*
445 * Loop through addresses for this host, and try each one in
446 * sequence until the connection succeeds.
447 */
Damien Miller34132e52000-01-14 15:45:46 +1100448 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller0faf7472013-10-17 11:47:23 +1100449 if (ai->ai_family != AF_INET &&
450 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +1100451 continue;
452 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
453 ntop, sizeof(ntop), strport, sizeof(strport),
454 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
djm@openbsd.org46152af2015-09-04 04:55:24 +0000455 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +1100456 continue;
457 }
458 debug("Connecting to %.200s [%.100s] port %s.",
459 host, ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +1100460
Damien Miller34132e52000-01-14 15:45:46 +1100461 /* Create a socket for connecting. */
Damien Miller2372ace2003-05-14 13:42:23 +1000462 sock = ssh_create_socket(needpriv, ai);
Damien Miller34132e52000-01-14 15:45:46 +1100463 if (sock < 0)
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000464 /* Any error is already output */
Damien Miller34132e52000-01-14 15:45:46 +1100465 continue;
Damien Miller95def091999-11-25 00:26:21 +1100466
Damien Millerb78d5eb2003-05-16 11:39:04 +1000467 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
Damien Miller67bd0622007-09-17 12:06:57 +1000468 timeout_ms) >= 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100469 /* Successful connection. */
Ben Lindstroma3700052001-04-05 23:26:32 +0000470 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
Damien Miller95def091999-11-25 00:26:21 +1100471 break;
Damien Miller34132e52000-01-14 15:45:46 +1100472 } else {
Ben Lindstrom728aa7e2002-07-15 17:48:11 +0000473 debug("connect to address %s port %s: %s",
474 ntop, strport, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100475 close(sock);
Damien Miller56e5e6a2006-04-23 12:08:59 +1000476 sock = -1;
Damien Miller95def091999-11-25 00:26:21 +1100477 }
Damien Miller95def091999-11-25 00:26:21 +1100478 }
Damien Miller56e5e6a2006-04-23 12:08:59 +1000479 if (sock != -1)
Damien Miller34132e52000-01-14 15:45:46 +1100480 break; /* Successful connection. */
Damien Miller95def091999-11-25 00:26:21 +1100481 }
Damien Miller34132e52000-01-14 15:45:46 +1100482
Damien Miller95def091999-11-25 00:26:21 +1100483 /* Return failure if we didn't get a successful connection. */
Damien Miller56e5e6a2006-04-23 12:08:59 +1000484 if (sock == -1) {
Darren Tuckerb2161e32005-01-20 11:00:46 +1100485 error("ssh: connect to host %s port %s: %s",
Ben Lindstroma6cd75c2002-07-11 04:00:19 +0000486 host, strport, strerror(errno));
Darren Tuckerb2161e32005-01-20 11:00:46 +1100487 return (-1);
Ben Lindstroma6cd75c2002-07-11 04:00:19 +0000488 }
Damien Miller95def091999-11-25 00:26:21 +1100489
490 debug("Connection established.");
491
Damien Miller12c150e2003-12-17 16:31:10 +1100492 /* Set SO_KEEPALIVE if requested. */
Damien Miller67bd0622007-09-17 12:06:57 +1000493 if (want_keepalive &&
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000494 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
495 sizeof(on)) < 0)
496 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100497
498 /* Set the connection. */
499 packet_set_connection(sock, sock);
500
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000501 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100502}
503
Damien Miller0faf7472013-10-17 11:47:23 +1100504int
505ssh_connect(const char *host, struct addrinfo *addrs,
506 struct sockaddr_storage *hostaddr, u_short port, int family,
507 int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
508{
509 if (options.proxy_command == NULL) {
510 return ssh_connect_direct(host, addrs, hostaddr, port, family,
511 connection_attempts, timeout_ms, want_keepalive, needpriv);
512 } else if (strcmp(options.proxy_command, "-") == 0) {
513 packet_set_connection(STDIN_FILENO, STDOUT_FILENO);
514 return 0; /* Always succeeds */
515 } else if (options.proxy_use_fdpass) {
516 return ssh_proxy_fdpass_connect(host, port,
517 options.proxy_command);
518 }
519 return ssh_proxy_connect(host, port, options.proxy_command);
520}
521
Darren Tucker00c15182012-09-06 21:21:56 +1000522static void
523send_client_banner(int connection_out, int minor1)
524{
Darren Tucker00c15182012-09-06 21:21:56 +1000525 /* Send our own protocol version identification. */
526 if (compat20) {
527 xasprintf(&client_version_string, "SSH-%d.%d-%.100s\r\n",
528 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION);
529 } else {
530 xasprintf(&client_version_string, "SSH-%d.%d-%.100s\n",
531 PROTOCOL_MAJOR_1, minor1, SSH_VERSION);
532 }
533 if (roaming_atomicio(vwrite, connection_out, client_version_string,
534 strlen(client_version_string)) != strlen(client_version_string))
535 fatal("write: %.100s", strerror(errno));
536 chop(client_version_string);
537 debug("Local version string %.100s", client_version_string);
538}
539
Damien Milleraae6c611999-12-06 11:47:28 +1100540/*
Damien Miller95def091999-11-25 00:26:21 +1100541 * Waits for the server identification string, and sends our own
542 * identification string.
543 */
Darren Tucker1cc55d72009-06-21 18:17:19 +1000544void
Damien Miller67bd0622007-09-17 12:06:57 +1000545ssh_exchange_identification(int timeout_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546{
Damien Miller95def091999-11-25 00:26:21 +1100547 char buf[256], remote_version[256]; /* must be same size! */
Damien Millereccb9de2005-06-17 12:59:34 +1000548 int remote_major, remote_minor, mismatch;
Damien Miller95def091999-11-25 00:26:21 +1100549 int connection_in = packet_get_connection_in();
550 int connection_out = packet_get_connection_out();
Darren Tucker00c15182012-09-06 21:21:56 +1000551 int minor1 = PROTOCOL_MINOR_1, client_banner_sent = 0;
Damien Miller1e88ea62006-07-10 20:15:56 +1000552 u_int i, n;
Damien Miller67bd0622007-09-17 12:06:57 +1000553 size_t len;
554 int fdsetsz, remaining, rc;
555 struct timeval t_start, t_remaining;
556 fd_set *fdset;
557
558 fdsetsz = howmany(connection_in + 1, NFDBITS) * sizeof(fd_mask);
559 fdset = xcalloc(1, fdsetsz);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560
Darren Tucker00c15182012-09-06 21:21:56 +1000561 /*
562 * If we are SSH2-only then we can send the banner immediately and
563 * save a round-trip.
564 */
565 if (options.protocol == SSH_PROTO_2) {
566 enable_compat20();
567 send_client_banner(connection_out, 0);
568 client_banner_sent = 1;
569 }
570
Damien Millerb253cc42005-05-26 12:23:44 +1000571 /* Read other side's version identification. */
Damien Miller67bd0622007-09-17 12:06:57 +1000572 remaining = timeout_ms;
Damien Miller1e88ea62006-07-10 20:15:56 +1000573 for (n = 0;;) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000574 for (i = 0; i < sizeof(buf) - 1; i++) {
Damien Miller67bd0622007-09-17 12:06:57 +1000575 if (timeout_ms > 0) {
576 gettimeofday(&t_start, NULL);
577 ms_to_timeval(&t_remaining, remaining);
578 FD_SET(connection_in, fdset);
579 rc = select(connection_in + 1, fdset, NULL,
580 fdset, &t_remaining);
581 ms_subtract_diff(&t_start, &remaining);
582 if (rc == 0 || remaining <= 0)
583 fatal("Connection timed out during "
584 "banner exchange");
585 if (rc == -1) {
586 if (errno == EINTR)
587 continue;
588 fatal("ssh_exchange_identification: "
589 "select: %s", strerror(errno));
590 }
591 }
592
Darren Tuckerc5564e12009-06-21 18:53:53 +1000593 len = roaming_atomicio(read, connection_in, &buf[i], 1);
Damien Millerb253cc42005-05-26 12:23:44 +1000594
Damien Miller46d38de2005-07-17 17:02:09 +1000595 if (len != 1 && errno == EPIPE)
Damien Miller67bd0622007-09-17 12:06:57 +1000596 fatal("ssh_exchange_identification: "
597 "Connection closed by remote host");
Damien Millerb253cc42005-05-26 12:23:44 +1000598 else if (len != 1)
Damien Miller67bd0622007-09-17 12:06:57 +1000599 fatal("ssh_exchange_identification: "
600 "read: %.100s", strerror(errno));
Damien Millerf6d9e222000-06-18 14:50:44 +1000601 if (buf[i] == '\r') {
602 buf[i] = '\n';
603 buf[i + 1] = 0;
604 continue; /**XXX wait for \n */
605 }
606 if (buf[i] == '\n') {
607 buf[i + 1] = 0;
608 break;
609 }
Damien Miller1e88ea62006-07-10 20:15:56 +1000610 if (++n > 65536)
Damien Miller67bd0622007-09-17 12:06:57 +1000611 fatal("ssh_exchange_identification: "
612 "No banner received");
Damien Miller95def091999-11-25 00:26:21 +1100613 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000614 buf[sizeof(buf) - 1] = 0;
615 if (strncmp(buf, "SSH-", 4) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100616 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000617 debug("ssh_exchange_identification: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618 }
Damien Miller1383bd82000-04-06 12:32:37 +1000619 server_version_string = xstrdup(buf);
Darren Tuckera627d422013-06-02 07:31:17 +1000620 free(fdset);
Damien Miller95def091999-11-25 00:26:21 +1100621
Damien Miller5428f641999-11-25 11:54:57 +1100622 /*
623 * Check that the versions match. In future this might accept
624 * several versions and set appropriate flags to handle them.
625 */
Damien Miller1383bd82000-04-06 12:32:37 +1000626 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
627 &remote_major, &remote_minor, remote_version) != 3)
Damien Miller95def091999-11-25 00:26:21 +1100628 fatal("Bad remote protocol version identification: '%.100s'", buf);
629 debug("Remote protocol version %d.%d, remote software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100630 remote_major, remote_minor, remote_version);
Damien Miller95def091999-11-25 00:26:21 +1100631
markus@openbsd.org48b3b2b2015-01-19 20:20:20 +0000632 active_state->compat = compat_datafellows(remote_version);
Damien Miller78928792000-04-12 20:17:38 +1000633 mismatch = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000634
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000635 switch (remote_major) {
Damien Miller78928792000-04-12 20:17:38 +1000636 case 1:
637 if (remote_minor == 99 &&
638 (options.protocol & SSH_PROTO_2) &&
639 !(options.protocol & SSH_PROTO_1_PREFERRED)) {
640 enable_compat20();
641 break;
Damien Miller95def091999-11-25 00:26:21 +1100642 }
Damien Miller78928792000-04-12 20:17:38 +1000643 if (!(options.protocol & SSH_PROTO_1)) {
644 mismatch = 1;
645 break;
646 }
647 if (remote_minor < 3) {
648 fatal("Remote machine has too old SSH software version.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100649 } else if (remote_minor == 3 || remote_minor == 4) {
Damien Miller78928792000-04-12 20:17:38 +1000650 /* We speak 1.3, too. */
651 enable_compat13();
Damien Miller0bc1bd82000-11-13 22:57:25 +1100652 minor1 = 3;
Damien Miller78928792000-04-12 20:17:38 +1000653 if (options.forward_agent) {
Damien Miller996acd22003-04-09 20:59:48 +1000654 logit("Agent forwarding disabled for protocol 1.3");
Damien Miller78928792000-04-12 20:17:38 +1000655 options.forward_agent = 0;
656 }
657 }
658 break;
659 case 2:
660 if (options.protocol & SSH_PROTO_2) {
661 enable_compat20();
662 break;
663 }
664 /* FALLTHROUGH */
Damien Miller4af51302000-04-16 11:18:38 +1000665 default:
Damien Miller78928792000-04-12 20:17:38 +1000666 mismatch = 1;
667 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668 }
Damien Miller78928792000-04-12 20:17:38 +1000669 if (mismatch)
Damien Miller95def091999-11-25 00:26:21 +1100670 fatal("Protocol major versions differ: %d vs. %d",
Damien Miller78928792000-04-12 20:17:38 +1000671 (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
672 remote_major);
Damien Miller58cd63b2014-01-10 10:59:24 +1100673 if ((datafellows & SSH_BUG_DERIVEKEY) != 0)
674 fatal("Server version \"%.100s\" uses unsafe key agreement; "
675 "refusing connection", remote_version);
Damien Miller324541e2013-12-31 12:25:40 +1100676 if ((datafellows & SSH_BUG_RSASIGMD5) != 0)
677 logit("Server version \"%.100s\" uses unsafe RSA signature "
678 "scheme; disabling use of RSA keys", remote_version);
Darren Tucker00c15182012-09-06 21:21:56 +1000679 if (!client_banner_sent)
680 send_client_banner(connection_out, minor1);
Damien Miller1383bd82000-04-06 12:32:37 +1000681 chop(server_version_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682}
683
Ben Lindstromb6c06d92001-02-10 23:21:09 +0000684/* defaults to 'no' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static int
Damien Millere3980042001-10-10 15:02:03 +1000686confirm(const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687{
Damien Miller49d795c2002-01-22 23:34:12 +1100688 const char *msg, *again = "Please type 'yes' or 'no': ";
689 char *p;
690 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691
Ben Lindstromb6c06d92001-02-10 23:21:09 +0000692 if (options.batch_mode)
693 return 0;
Damien Miller49d795c2002-01-22 23:34:12 +1100694 for (msg = prompt;;msg = again) {
695 p = read_passphrase(msg, RP_ECHO);
696 if (p == NULL ||
697 (p[0] == '\0') || (p[0] == '\n') ||
698 strncasecmp(p, "no", 2) == 0)
699 ret = 0;
Ben Lindstrom04f9af72002-07-04 00:03:56 +0000700 if (p && strncasecmp(p, "yes", 3) == 0)
Damien Miller49d795c2002-01-22 23:34:12 +1100701 ret = 1;
Darren Tuckera627d422013-06-02 07:31:17 +1000702 free(p);
Damien Miller49d795c2002-01-22 23:34:12 +1100703 if (ret != -1)
704 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706}
707
Damien Miller0a80ca12010-02-27 07:55:05 +1100708static int
709check_host_cert(const char *host, const Key *host_key)
710{
711 const char *reason;
712
713 if (key_cert_check_authority(host_key, 1, 0, host, &reason) != 0) {
714 error("%s", reason);
715 return 0;
716 }
Damien Miller86687062014-07-02 15:28:02 +1000717 if (buffer_len(host_key->cert->critical) != 0) {
Damien Miller4e270b02010-04-16 15:56:21 +1000718 error("Certificate for %s contains unsupported "
719 "critical options(s)", host);
Damien Miller0a80ca12010-02-27 07:55:05 +1100720 return 0;
721 }
722 return 1;
723}
724
Damien Millerd925dcd2010-12-01 12:21:51 +1100725static int
726sockaddr_is_local(struct sockaddr *hostaddr)
727{
728 switch (hostaddr->sa_family) {
729 case AF_INET:
730 return (ntohl(((struct sockaddr_in *)hostaddr)->
731 sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
732 case AF_INET6:
733 return IN6_IS_ADDR_LOOPBACK(
734 &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
735 default:
736 return 0;
737 }
738}
739
740/*
741 * Prepare the hostname and ip address strings that are used to lookup
742 * host keys in known_hosts files. These may have a port number appended.
743 */
744void
745get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr,
746 u_short port, char **hostfile_hostname, char **hostfile_ipaddr)
747{
748 char ntop[NI_MAXHOST];
749 socklen_t addrlen;
750
751 switch (hostaddr == NULL ? -1 : hostaddr->sa_family) {
752 case -1:
753 addrlen = 0;
754 break;
755 case AF_INET:
756 addrlen = sizeof(struct sockaddr_in);
757 break;
758 case AF_INET6:
759 addrlen = sizeof(struct sockaddr_in6);
760 break;
761 default:
762 addrlen = sizeof(struct sockaddr);
763 break;
764 }
765
766 /*
767 * We don't have the remote ip-address for connections
768 * using a proxy command
769 */
770 if (hostfile_ipaddr != NULL) {
771 if (options.proxy_command == NULL) {
772 if (getnameinfo(hostaddr, addrlen,
773 ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0)
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000774 fatal("%s: getnameinfo failed", __func__);
Damien Millerd925dcd2010-12-01 12:21:51 +1100775 *hostfile_ipaddr = put_host_port(ntop, port);
776 } else {
777 *hostfile_ipaddr = xstrdup("<no hostip for proxy "
778 "command>");
779 }
780 }
781
782 /*
783 * Allow the user to record the key under a different name or
784 * differentiate a non-standard port. This is useful for ssh
785 * tunneling over forwarded connections or if you run multiple
786 * sshd's on different ports on the same machine.
787 */
788 if (hostfile_hostname != NULL) {
789 if (options.host_key_alias != NULL) {
790 *hostfile_hostname = xstrdup(options.host_key_alias);
791 debug("using hostkeyalias: %s", *hostfile_hostname);
792 } else {
793 *hostfile_hostname = put_host_port(hostname, port);
794 }
795 }
796}
797
Damien Miller95def091999-11-25 00:26:21 +1100798/*
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000799 * check whether the supplied host key is valid, return -1 if the key
Damien Miller295ee632011-05-29 21:42:31 +1000800 * is not valid. user_hostfile[0] will not be updated if 'readonly' is true.
Damien Miller95def091999-11-25 00:26:21 +1100801 */
Damien Millerda828392006-08-05 11:35:45 +1000802#define RDRW 0
803#define RDONLY 1
804#define ROQUIET 2
Ben Lindstrombba81212001-06-25 05:01:22 +0000805static int
Damien Millerda828392006-08-05 11:35:45 +1000806check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
Damien Miller295ee632011-05-29 21:42:31 +1000807 Key *host_key, int readonly,
808 char **user_hostfiles, u_int num_user_hostfiles,
809 char **system_hostfiles, u_int num_system_hostfiles)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810{
Damien Miller95def091999-11-25 00:26:21 +1100811 HostStatus host_status;
812 HostStatus ip_status;
Damien Miller295ee632011-05-29 21:42:31 +1000813 Key *raw_key = NULL;
814 char *ip = NULL, *host = NULL;
815 char hostline[1000], *hostp, *fp, *ra;
Damien Miller49d795c2002-01-22 23:34:12 +1100816 char msg[1024];
Damien Miller295ee632011-05-29 21:42:31 +1000817 const char *type;
Damien Millerd925dcd2010-12-01 12:21:51 +1100818 const struct hostkey_entry *host_found, *ip_found;
Damien Miller295ee632011-05-29 21:42:31 +1000819 int len, cancelled_forwarding = 0;
820 int local = sockaddr_is_local(hostaddr);
821 int r, want_cert = key_is_cert(host_key), host_ip_differ = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000822 int hostkey_trusted = 0; /* Known or explicitly accepted by user */
Damien Miller295ee632011-05-29 21:42:31 +1000823 struct hostkeys *host_hostkeys, *ip_hostkeys;
824 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100825
826 /*
827 * Force accepting of the host key for loopback/localhost. The
828 * problem is that if the home directory is NFS-mounted to multiple
829 * machines, localhost will refer to a different machine in each of
830 * them, and the user will get bogus HOST_CHANGED warnings. This
831 * essentially disables host authentication for localhost; however,
832 * this is probably not a real problem.
833 */
Ben Lindstrom3cecc9a2001-10-03 17:39:38 +0000834 if (options.no_host_authentication_for_localhost == 1 && local &&
835 options.host_key_alias == NULL) {
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000836 debug("Forcing accepting of host key for "
837 "loopback/localhost.");
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000838 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100839 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840
Damien Milleraae6c611999-12-06 11:47:28 +1100841 /*
Damien Millerd925dcd2010-12-01 12:21:51 +1100842 * Prepare the hostname and address strings used for hostkey lookup.
843 * In some cases, these will have a port number appended.
Damien Milleraae6c611999-12-06 11:47:28 +1100844 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100845 get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip);
Darren Tucker78913e02008-06-13 04:47:34 +1000846
847 /*
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000848 * Turn off check_host_ip if the connection is to localhost, via proxy
849 * command or if we don't have a hostname to compare with
850 */
Darren Tuckerda345532006-07-10 23:04:19 +1000851 if (options.check_host_ip && (local ||
852 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
Damien Milleraae6c611999-12-06 11:47:28 +1100853 options.check_host_ip = 0;
854
Damien Millerd925dcd2010-12-01 12:21:51 +1100855 host_hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000856 for (i = 0; i < num_user_hostfiles; i++)
857 load_hostkeys(host_hostkeys, host, user_hostfiles[i]);
858 for (i = 0; i < num_system_hostfiles; i++)
859 load_hostkeys(host_hostkeys, host, system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100860
861 ip_hostkeys = NULL;
862 if (!want_cert && options.check_host_ip) {
863 ip_hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000864 for (i = 0; i < num_user_hostfiles; i++)
865 load_hostkeys(ip_hostkeys, ip, user_hostfiles[i]);
866 for (i = 0; i < num_system_hostfiles; i++)
867 load_hostkeys(ip_hostkeys, ip, system_hostfiles[i]);
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000868 }
869
Damien Miller0a80ca12010-02-27 07:55:05 +1100870 retry:
Damien Millerd925dcd2010-12-01 12:21:51 +1100871 /* Reload these as they may have changed on cert->key downgrade */
Damien Miller0a80ca12010-02-27 07:55:05 +1100872 want_cert = key_is_cert(host_key);
873 type = key_type(host_key);
874
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000875 /*
Damien Miller788f2122005-11-05 15:14:59 +1100876 * Check if the host key is present in the user's list of known
Damien Miller5428f641999-11-25 11:54:57 +1100877 * hosts or in the systemwide list.
878 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100879 host_status = check_key_in_hostkeys(host_hostkeys, host_key,
880 &host_found);
881
Damien Miller5428f641999-11-25 11:54:57 +1100882 /*
Damien Miller5428f641999-11-25 11:54:57 +1100883 * Also perform check for the ip address, skip the check if we are
Damien Miller0a80ca12010-02-27 07:55:05 +1100884 * localhost, looking for a certificate, or the hostname was an ip
885 * address to begin with.
Damien Miller5428f641999-11-25 11:54:57 +1100886 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100887 if (!want_cert && ip_hostkeys != NULL) {
888 ip_status = check_key_in_hostkeys(ip_hostkeys, host_key,
889 &ip_found);
Damien Miller95def091999-11-25 00:26:21 +1100890 if (host_status == HOST_CHANGED &&
Damien Millerd925dcd2010-12-01 12:21:51 +1100891 (ip_status != HOST_CHANGED ||
892 (ip_found != NULL &&
893 !key_equal(ip_found->key, host_found->key))))
Damien Miller95def091999-11-25 00:26:21 +1100894 host_ip_differ = 1;
Damien Miller95def091999-11-25 00:26:21 +1100895 } else
896 ip_status = host_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Miller95def091999-11-25 00:26:21 +1100898 switch (host_status) {
899 case HOST_OK:
900 /* The host is known and the key matches. */
Damien Miller0a80ca12010-02-27 07:55:05 +1100901 debug("Host '%.200s' is known and matches the %s host %s.",
902 host, type, want_cert ? "certificate" : "key");
Damien Millerd925dcd2010-12-01 12:21:51 +1100903 debug("Found %s in %s:%lu", want_cert ? "CA key" : "key",
904 host_found->file, host_found->line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100905 if (want_cert && !check_host_cert(hostname, host_key))
906 goto fail;
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000907 if (options.check_host_ip && ip_status == HOST_NEW) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100908 if (readonly || want_cert)
Damien Miller996acd22003-04-09 20:59:48 +1000909 logit("%s host key for IP address "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000910 "'%.128s' not in list of known hosts.",
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000911 type, ip);
Damien Miller295ee632011-05-29 21:42:31 +1000912 else if (!add_host_to_hostfile(user_hostfiles[0], ip,
Damien Millere1776152005-03-01 21:47:37 +1100913 host_key, options.hash_known_hosts))
Damien Miller996acd22003-04-09 20:59:48 +1000914 logit("Failed to add the %s host key for IP "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000915 "address '%.128s' to the list of known "
dtucker@openbsd.org496aeb22015-05-28 05:41:29 +0000916 "hosts (%.500s).", type, ip,
Damien Miller295ee632011-05-29 21:42:31 +1000917 user_hostfiles[0]);
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000918 else
Damien Miller996acd22003-04-09 20:59:48 +1000919 logit("Warning: Permanently added the %s host "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000920 "key for IP address '%.128s' to the list "
921 "of known hosts.", type, ip);
Damien Miller10288242008-06-30 00:04:03 +1000922 } else if (options.visual_host_key) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000923 fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000924 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000925 ra = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000926 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000927 if (fp == NULL || ra == NULL)
928 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.orgac3451d2015-09-04 03:57:38 +0000929 logit("Host key fingerprint is %s\n%s", fp, ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000930 free(ra);
931 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100932 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000933 hostkey_trusted = 1;
Damien Miller95def091999-11-25 00:26:21 +1100934 break;
935 case HOST_NEW:
Damien Millerda828392006-08-05 11:35:45 +1000936 if (options.host_key_alias == NULL && port != 0 &&
937 port != SSH_DEFAULT_PORT) {
938 debug("checking without port identifier");
Damien Millerece92c82008-11-03 19:25:03 +1100939 if (check_host_key(hostname, hostaddr, 0, host_key,
Damien Miller295ee632011-05-29 21:42:31 +1000940 ROQUIET, user_hostfiles, num_user_hostfiles,
941 system_hostfiles, num_system_hostfiles) == 0) {
Damien Millerda828392006-08-05 11:35:45 +1000942 debug("found matching key w/out port");
943 break;
944 }
945 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100946 if (readonly || want_cert)
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000947 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100948 /* The host is new. */
949 if (options.strict_host_key_checking == 1) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000950 /*
951 * User has requested strict host key checking. We
952 * will not add the host key automatically. The only
953 * alternative left is to abort.
954 */
955 error("No %s host key is known for %.200s and you "
956 "have requested strict checking.", type, host);
957 goto fail;
Damien Miller95def091999-11-25 00:26:21 +1100958 } else if (options.strict_host_key_checking == 2) {
Damien Miller7392ae62003-06-11 22:05:25 +1000959 char msg1[1024], msg2[1024];
960
Damien Millerd925dcd2010-12-01 12:21:51 +1100961 if (show_other_keys(host_hostkeys, host_key))
Damien Miller7392ae62003-06-11 22:05:25 +1000962 snprintf(msg1, sizeof(msg1),
Damien Miller0dc1bef2005-07-17 17:22:45 +1000963 "\nbut keys of different type are already"
964 " known for this host.");
Damien Miller7392ae62003-06-11 22:05:25 +1000965 else
966 snprintf(msg1, sizeof(msg1), ".");
Damien Miller95def091999-11-25 00:26:21 +1100967 /* The default */
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000968 fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000969 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000970 ra = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000971 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000972 if (fp == NULL || ra == NULL)
973 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Miller7392ae62003-06-11 22:05:25 +1000974 msg2[0] = '\0';
Damien Miller7392ae62003-06-11 22:05:25 +1000975 if (options.verify_host_key_dns) {
Damien Miller150b5572003-11-17 21:19:29 +1100976 if (matching_host_key_dns)
Damien Miller7392ae62003-06-11 22:05:25 +1000977 snprintf(msg2, sizeof(msg2),
978 "Matching host key fingerprint"
979 " found in DNS.\n");
980 else
981 snprintf(msg2, sizeof(msg2),
982 "No matching host key fingerprint"
983 " found in DNS.\n");
984 }
Damien Miller49d795c2002-01-22 23:34:12 +1100985 snprintf(msg, sizeof(msg),
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000986 "The authenticity of host '%.200s (%s)' can't be "
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000987 "established%s\n"
Damien Miller10288242008-06-30 00:04:03 +1000988 "%s key fingerprint is %s.%s%s\n%s"
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000989 "Are you sure you want to continue connecting "
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000990 "(yes/no)? ",
Damien Miller10288242008-06-30 00:04:03 +1000991 host, ip, msg1, type, fp,
992 options.visual_host_key ? "\n" : "",
993 options.visual_host_key ? ra : "",
994 msg2);
Darren Tuckera627d422013-06-02 07:31:17 +1000995 free(ra);
996 free(fp);
Damien Miller49d795c2002-01-22 23:34:12 +1100997 if (!confirm(msg))
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000998 goto fail;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000999 hostkey_trusted = 1; /* user explicitly confirmed */
Damien Miller95def091999-11-25 00:26:21 +11001000 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001001 /*
1002 * If not in strict mode, add the key automatically to the
1003 * local known_hosts file.
1004 */
Damien Miller1227d4c2005-03-02 12:06:51 +11001005 if (options.check_host_ip && ip_status == HOST_NEW) {
Damien Millerd925dcd2010-12-01 12:21:51 +11001006 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
Damien Miller1227d4c2005-03-02 12:06:51 +11001007 hostp = hostline;
1008 if (options.hash_known_hosts) {
1009 /* Add hash of host and IP separately */
Damien Miller295ee632011-05-29 21:42:31 +10001010 r = add_host_to_hostfile(user_hostfiles[0],
1011 host, host_key, options.hash_known_hosts) &&
1012 add_host_to_hostfile(user_hostfiles[0], ip,
Damien Miller1227d4c2005-03-02 12:06:51 +11001013 host_key, options.hash_known_hosts);
1014 } else {
1015 /* Add unhashed "host,ip" */
Damien Miller295ee632011-05-29 21:42:31 +10001016 r = add_host_to_hostfile(user_hostfiles[0],
Damien Miller1227d4c2005-03-02 12:06:51 +11001017 hostline, host_key,
1018 options.hash_known_hosts);
1019 }
1020 } else {
Damien Miller295ee632011-05-29 21:42:31 +10001021 r = add_host_to_hostfile(user_hostfiles[0], host,
1022 host_key, options.hash_known_hosts);
Damien Miller1227d4c2005-03-02 12:06:51 +11001023 hostp = host;
1024 }
1025
1026 if (!r)
Damien Miller996acd22003-04-09 20:59:48 +10001027 logit("Failed to add the host to the list of known "
Damien Miller295ee632011-05-29 21:42:31 +10001028 "hosts (%.500s).", user_hostfiles[0]);
Damien Miller95def091999-11-25 00:26:21 +11001029 else
Damien Miller996acd22003-04-09 20:59:48 +10001030 logit("Warning: Permanently added '%.200s' (%s) to the "
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001031 "list of known hosts.", hostp, type);
Damien Miller95def091999-11-25 00:26:21 +11001032 break;
Damien Miller1aed65e2010-03-04 21:53:35 +11001033 case HOST_REVOKED:
1034 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1035 error("@ WARNING: REVOKED HOST KEY DETECTED! @");
1036 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1037 error("The %s host key for %s is marked as revoked.", type, host);
1038 error("This could mean that a stolen key is being used to");
1039 error("impersonate this host.");
1040
1041 /*
1042 * If strict host key checking is in use, the user will have
1043 * to edit the key manually and we can only abort.
1044 */
1045 if (options.strict_host_key_checking) {
1046 error("%s host key for %.200s was revoked and you have "
1047 "requested strict checking.", type, host);
1048 goto fail;
1049 }
1050 goto continue_unsafe;
1051
Damien Miller95def091999-11-25 00:26:21 +11001052 case HOST_CHANGED:
Damien Miller0a80ca12010-02-27 07:55:05 +11001053 if (want_cert) {
1054 /*
1055 * This is only a debug() since it is valid to have
1056 * CAs with wildcard DNS matches that don't match
1057 * all hosts that one might visit.
1058 */
1059 debug("Host certificate authority does not "
Damien Millerd925dcd2010-12-01 12:21:51 +11001060 "match %s in %s:%lu", CA_MARKER,
1061 host_found->file, host_found->line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001062 goto fail;
1063 }
Damien Millerda828392006-08-05 11:35:45 +10001064 if (readonly == ROQUIET)
1065 goto fail;
Damien Miller95def091999-11-25 00:26:21 +11001066 if (options.check_host_ip && host_ip_differ) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001067 char *key_msg;
Damien Miller95def091999-11-25 00:26:21 +11001068 if (ip_status == HOST_NEW)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001069 key_msg = "is unknown";
Damien Miller95def091999-11-25 00:26:21 +11001070 else if (ip_status == HOST_OK)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001071 key_msg = "is unchanged";
Damien Miller95def091999-11-25 00:26:21 +11001072 else
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001073 key_msg = "has a different value";
Damien Miller95def091999-11-25 00:26:21 +11001074 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1075 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1076 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Damien Millere247cc42000-05-07 12:03:14 +10001077 error("The %s host key for %s has changed,", type, host);
Darren Tuckerff4454d2008-06-13 10:21:51 +10001078 error("and the key for the corresponding IP address %s", ip);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001079 error("%s. This could either mean that", key_msg);
Damien Miller95def091999-11-25 00:26:21 +11001080 error("DNS SPOOFING is happening or the IP address for the host");
Ben Lindstrom46c16222000-12-22 01:43:59 +00001081 error("and its host key have changed at the same time.");
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001082 if (ip_status != HOST_NEW)
Damien Millerd925dcd2010-12-01 12:21:51 +11001083 error("Offending key for IP in %s:%lu",
1084 ip_found->file, ip_found->line);
Damien Miller95def091999-11-25 00:26:21 +11001085 }
1086 /* The host key has changed. */
Damien Miller5a388972003-11-17 21:10:47 +11001087 warn_changed_key(host_key);
Damien Miller95def091999-11-25 00:26:21 +11001088 error("Add correct host key in %.100s to get rid of this message.",
Damien Miller295ee632011-05-29 21:42:31 +10001089 user_hostfiles[0]);
Damien Millerd925dcd2010-12-01 12:21:51 +11001090 error("Offending %s key in %s:%lu", key_type(host_found->key),
1091 host_found->file, host_found->line);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092
Damien Miller5428f641999-11-25 11:54:57 +11001093 /*
1094 * If strict host key checking is in use, the user will have
1095 * to edit the key manually and we can only abort.
1096 */
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001097 if (options.strict_host_key_checking) {
1098 error("%s host key for %.200s has changed and you have "
1099 "requested strict checking.", type, host);
1100 goto fail;
1101 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
Damien Miller1aed65e2010-03-04 21:53:35 +11001103 continue_unsafe:
Damien Miller5428f641999-11-25 11:54:57 +11001104 /*
1105 * If strict host key checking has not been requested, allow
Damien Miller941ac452003-06-04 20:31:53 +10001106 * the connection but without MITM-able authentication or
Damien Miller437edb92006-08-05 09:11:13 +10001107 * forwarding.
Damien Miller5428f641999-11-25 11:54:57 +11001108 */
Damien Miller95def091999-11-25 00:26:21 +11001109 if (options.password_authentication) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001110 error("Password authentication is disabled to avoid "
1111 "man-in-the-middle attacks.");
Damien Miller95def091999-11-25 00:26:21 +11001112 options.password_authentication = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001113 cancelled_forwarding = 1;
Damien Miller95def091999-11-25 00:26:21 +11001114 }
Damien Miller941ac452003-06-04 20:31:53 +10001115 if (options.kbd_interactive_authentication) {
1116 error("Keyboard-interactive authentication is disabled"
1117 " to avoid man-in-the-middle attacks.");
1118 options.kbd_interactive_authentication = 0;
1119 options.challenge_response_authentication = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001120 cancelled_forwarding = 1;
Damien Miller941ac452003-06-04 20:31:53 +10001121 }
1122 if (options.challenge_response_authentication) {
1123 error("Challenge/response authentication is disabled"
1124 " to avoid man-in-the-middle attacks.");
1125 options.challenge_response_authentication = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001126 cancelled_forwarding = 1;
Damien Miller941ac452003-06-04 20:31:53 +10001127 }
Damien Miller95def091999-11-25 00:26:21 +11001128 if (options.forward_agent) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001129 error("Agent forwarding is disabled to avoid "
1130 "man-in-the-middle attacks.");
Damien Miller95def091999-11-25 00:26:21 +11001131 options.forward_agent = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001132 cancelled_forwarding = 1;
Damien Miller95def091999-11-25 00:26:21 +11001133 }
Ben Lindstromc72745a2000-12-02 19:03:54 +00001134 if (options.forward_x11) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001135 error("X11 forwarding is disabled to avoid "
1136 "man-in-the-middle attacks.");
Ben Lindstromc72745a2000-12-02 19:03:54 +00001137 options.forward_x11 = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001138 cancelled_forwarding = 1;
Ben Lindstromc72745a2000-12-02 19:03:54 +00001139 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001140 if (options.num_local_forwards > 0 ||
1141 options.num_remote_forwards > 0) {
1142 error("Port forwarding is disabled to avoid "
1143 "man-in-the-middle attacks.");
1144 options.num_local_forwards =
Damien Miller9f0f5c62001-12-21 14:45:46 +11001145 options.num_remote_forwards = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001146 cancelled_forwarding = 1;
Ben Lindstromc72745a2000-12-02 19:03:54 +00001147 }
Damien Miller437edb92006-08-05 09:11:13 +10001148 if (options.tun_open != SSH_TUNMODE_NO) {
1149 error("Tunnel forwarding is disabled to avoid "
1150 "man-in-the-middle attacks.");
1151 options.tun_open = SSH_TUNMODE_NO;
Darren Tucker068e01f2008-07-02 22:33:55 +10001152 cancelled_forwarding = 1;
Damien Miller437edb92006-08-05 09:11:13 +10001153 }
Darren Tucker068e01f2008-07-02 22:33:55 +10001154 if (options.exit_on_forward_failure && cancelled_forwarding)
1155 fatal("Error: forwarding disabled due to host key "
1156 "check failure");
1157
Damien Miller5428f641999-11-25 11:54:57 +11001158 /*
1159 * XXX Should permit the user to change to use the new id.
1160 * This could be done by converting the host key to an
1161 * identifying sentence, tell that the host identifies itself
Darren Tuckere2b36742010-01-13 22:42:34 +11001162 * by that sentence, and ask the user if he/she wishes to
Damien Miller5428f641999-11-25 11:54:57 +11001163 * accept the authentication.
1164 */
Damien Miller95def091999-11-25 00:26:21 +11001165 break;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001166 case HOST_FOUND:
1167 fatal("internal error");
1168 break;
Damien Miller95def091999-11-25 00:26:21 +11001169 }
Ben Lindstromc72745a2000-12-02 19:03:54 +00001170
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001171 if (options.check_host_ip && host_status != HOST_CHANGED &&
1172 ip_status == HOST_CHANGED) {
Damien Miller49d795c2002-01-22 23:34:12 +11001173 snprintf(msg, sizeof(msg),
1174 "Warning: the %s host key for '%.200s' "
1175 "differs from the key for the IP address '%.128s'"
Damien Millerd925dcd2010-12-01 12:21:51 +11001176 "\nOffending key for IP in %s:%lu",
1177 type, host, ip, ip_found->file, ip_found->line);
Damien Miller49d795c2002-01-22 23:34:12 +11001178 if (host_status == HOST_OK) {
1179 len = strlen(msg);
1180 snprintf(msg + len, sizeof(msg) - len,
Damien Millerd925dcd2010-12-01 12:21:51 +11001181 "\nMatching host key in %s:%lu",
1182 host_found->file, host_found->line);
Damien Miller49d795c2002-01-22 23:34:12 +11001183 }
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001184 if (options.strict_host_key_checking == 1) {
Damien Millerab2db412003-06-02 19:09:13 +10001185 logit("%s", msg);
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001186 error("Exiting, you have requested strict checking.");
1187 goto fail;
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001188 } else if (options.strict_host_key_checking == 2) {
Damien Miller49d795c2002-01-22 23:34:12 +11001189 strlcat(msg, "\nAre you sure you want "
1190 "to continue connecting (yes/no)? ", sizeof(msg));
1191 if (!confirm(msg))
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001192 goto fail;
Damien Miller49d795c2002-01-22 23:34:12 +11001193 } else {
Damien Millerab2db412003-06-02 19:09:13 +10001194 logit("%s", msg);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001195 }
1196 }
1197
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001198 if (!hostkey_trusted && options.update_hostkeys) {
1199 debug("%s: hostkey not known or explicitly trusted: "
1200 "disabling UpdateHostkeys", __func__);
1201 options.update_hostkeys = 0;
1202 }
1203
Darren Tuckera627d422013-06-02 07:31:17 +10001204 free(ip);
1205 free(host);
Damien Millerd925dcd2010-12-01 12:21:51 +11001206 if (host_hostkeys != NULL)
1207 free_hostkeys(host_hostkeys);
1208 if (ip_hostkeys != NULL)
1209 free_hostkeys(ip_hostkeys);
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001210 return 0;
1211
1212fail:
Damien Miller1aed65e2010-03-04 21:53:35 +11001213 if (want_cert && host_status != HOST_REVOKED) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001214 /*
1215 * No matching certificate. Downgrade cert to raw key and
1216 * search normally.
1217 */
1218 debug("No matching CA found. Retry with plain key");
1219 raw_key = key_from_private(host_key);
1220 if (key_drop_cert(raw_key) != 0)
1221 fatal("Couldn't drop certificate");
1222 host_key = raw_key;
1223 goto retry;
1224 }
1225 if (raw_key != NULL)
1226 key_free(raw_key);
Darren Tuckera627d422013-06-02 07:31:17 +10001227 free(ip);
1228 free(host);
Damien Millerd925dcd2010-12-01 12:21:51 +11001229 if (host_hostkeys != NULL)
1230 free_hostkeys(host_hostkeys);
1231 if (ip_hostkeys != NULL)
1232 free_hostkeys(ip_hostkeys);
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001233 return -1;
1234}
1235
Damien Miller37876e92003-05-15 10:19:46 +10001236/* returns 0 if key verifies or -1 if key does NOT verify */
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001237int
1238verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1239{
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001240 u_int i;
Damien Miller6b37fbb2014-07-04 08:59:24 +10001241 int r = -1, flags = 0;
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001242 char valid[64], *fp = NULL, *cafp = NULL;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001243 struct sshkey *plain = NULL;
Damien Millerd925dcd2010-12-01 12:21:51 +11001244
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001245 if ((fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001246 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001247 error("%s: fingerprint host key: %s", __func__, ssh_err(r));
1248 r = -1;
1249 goto out;
1250 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001251
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001252 if (sshkey_is_cert(host_key)) {
1253 if ((cafp = sshkey_fingerprint(host_key->cert->signature_key,
1254 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
1255 error("%s: fingerprint CA key: %s",
1256 __func__, ssh_err(r));
1257 r = -1;
1258 goto out;
1259 }
1260 sshkey_format_cert_validity(host_key->cert,
1261 valid, sizeof(valid));
1262 debug("Server host certificate: %s %s, serial %llu "
1263 "ID \"%s\" CA %s %s valid %s",
1264 sshkey_ssh_name(host_key), fp,
1265 host_key->cert->serial, host_key->cert->key_id,
1266 sshkey_ssh_name(host_key->cert->signature_key), cafp,
1267 valid);
1268 for (i = 0; i < host_key->cert->nprincipals; i++) {
1269 debug2("Server host certificate hostname: %s",
1270 host_key->cert->principals[i]);
1271 }
1272 } else {
1273 debug("Server host key: %s %s", compat20 ?
1274 sshkey_ssh_name(host_key) : sshkey_type(host_key), fp);
1275 }
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001276
1277 if (sshkey_equal(previous_host_key, host_key)) {
1278 debug2("%s: server host key %s %s matches cached key",
1279 __func__, sshkey_type(host_key), fp);
1280 r = 0;
1281 goto out;
1282 }
1283
1284 /* Check in RevokedHostKeys file if specified */
1285 if (options.revoked_host_keys != NULL) {
1286 r = sshkey_check_revoked(host_key, options.revoked_host_keys);
1287 switch (r) {
1288 case 0:
1289 break; /* not revoked */
1290 case SSH_ERR_KEY_REVOKED:
1291 error("Host key %s %s revoked by file %s",
1292 sshkey_type(host_key), fp,
1293 options.revoked_host_keys);
1294 r = -1;
1295 goto out;
1296 default:
1297 error("Error checking host key %s %s in "
1298 "revoked keys file %s: %s", sshkey_type(host_key),
1299 fp, options.revoked_host_keys, ssh_err(r));
1300 r = -1;
1301 goto out;
1302 }
Damien Miller6b37fbb2014-07-04 08:59:24 +10001303 }
1304
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001305 if (options.verify_host_key_dns) {
1306 /*
1307 * XXX certs are not yet supported for DNS, so downgrade
1308 * them and try the plain key.
1309 */
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001310 if ((r = sshkey_from_private(host_key, &plain)) != 0)
1311 goto out;
1312 if (sshkey_is_cert(plain))
1313 sshkey_drop_cert(plain);
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001314 if (verify_host_key_dns(host, hostaddr, plain, &flags) == 0) {
1315 if (flags & DNS_VERIFY_FOUND) {
1316 if (options.verify_host_key_dns == 1 &&
1317 flags & DNS_VERIFY_MATCH &&
1318 flags & DNS_VERIFY_SECURE) {
Damien Miller6b37fbb2014-07-04 08:59:24 +10001319 r = 0;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001320 goto out;
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001321 }
1322 if (flags & DNS_VERIFY_MATCH) {
1323 matching_host_key_dns = 1;
1324 } else {
1325 warn_changed_key(plain);
1326 error("Update the SSHFP RR in DNS "
1327 "with the new host key to get rid "
1328 "of this message.");
1329 }
Damien Miller150b5572003-11-17 21:19:29 +11001330 }
Damien Miller37876e92003-05-15 10:19:46 +10001331 }
1332 }
Damien Miller6b37fbb2014-07-04 08:59:24 +10001333 r = check_host_key(host, hostaddr, options.port, host_key, RDRW,
Damien Miller295ee632011-05-29 21:42:31 +10001334 options.user_hostfiles, options.num_user_hostfiles,
1335 options.system_hostfiles, options.num_system_hostfiles);
Damien Miller6b37fbb2014-07-04 08:59:24 +10001336
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001337out:
1338 sshkey_free(plain);
1339 free(fp);
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001340 free(cafp);
Damien Miller6b37fbb2014-07-04 08:59:24 +10001341 if (r == 0 && host_key != NULL) {
1342 key_free(previous_host_key);
1343 previous_host_key = key_from_private(host_key);
1344 }
1345
1346 return r;
Damien Millera34a28b1999-12-14 10:47:15 +11001347}
Damien Miller037a0dc1999-12-07 15:38:31 +11001348
Damien Miller396691a2000-01-20 22:44:08 +11001349/*
1350 * Starts a dialog with the server, and authenticates the current user on the
1351 * server. This does not need any extra privileges. The basic connection
1352 * to the server must already have been established before this is called.
1353 * If login fails, this function prints an error and never returns.
1354 * This function does not require super-user privileges.
1355 */
1356void
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001357ssh_login(Sensitive *sensitive, const char *orighost,
Damien Millerd925dcd2010-12-01 12:21:51 +11001358 struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms)
Damien Miller396691a2000-01-20 22:44:08 +11001359{
Damien Miller0faf7472013-10-17 11:47:23 +11001360 char *host;
Damien Millereba71ba2000-04-29 23:57:08 +10001361 char *server_user, *local_user;
1362
Damien Millereba71ba2000-04-29 23:57:08 +10001363 local_user = xstrdup(pw->pw_name);
1364 server_user = options.user ? options.user : local_user;
Damien Miller396691a2000-01-20 22:44:08 +11001365
1366 /* Convert the user-supplied hostname into all lowercase. */
1367 host = xstrdup(orighost);
Damien Miller0faf7472013-10-17 11:47:23 +11001368 lowercase(host);
Damien Miller396691a2000-01-20 22:44:08 +11001369
1370 /* Exchange protocol version identification strings with the server. */
Damien Miller67bd0622007-09-17 12:06:57 +10001371 ssh_exchange_identification(timeout_ms);
Damien Miller396691a2000-01-20 22:44:08 +11001372
1373 /* Put the connection into non-blocking mode. */
1374 packet_set_nonblocking();
1375
Damien Miller396691a2000-01-20 22:44:08 +11001376 /* key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001377 /* authenticate user */
dtucker@openbsd.orgbb2289e2015-04-14 04:17:03 +00001378 debug("Authenticating to %s:%d as '%s'", host, port, server_user);
Damien Miller1383bd82000-04-06 12:32:37 +10001379 if (compat20) {
Damien Millerd925dcd2010-12-01 12:21:51 +11001380 ssh_kex2(host, hostaddr, port);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001381 ssh_userauth2(local_user, server_user, host, sensitive);
Damien Miller1383bd82000-04-06 12:32:37 +10001382 } else {
Damien Miller1f0311c2014-05-15 14:24:09 +10001383#ifdef WITH_SSH1
Damien Miller1383bd82000-04-06 12:32:37 +10001384 ssh_kex(host, hostaddr);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001385 ssh_userauth1(local_user, server_user, host, sensitive);
Damien Miller1f0311c2014-05-15 14:24:09 +10001386#else
djm@openbsd.org53097b22015-03-24 01:11:12 +00001387 fatal("ssh1 is not supported");
Damien Miller1f0311c2014-05-15 14:24:09 +10001388#endif
Damien Miller1383bd82000-04-06 12:32:37 +10001389 }
Darren Tuckera627d422013-06-02 07:31:17 +10001390 free(local_user);
Damien Miller396691a2000-01-20 22:44:08 +11001391}
Damien Miller79438cc2001-02-16 12:34:57 +11001392
1393void
1394ssh_put_password(char *password)
1395{
1396 int size;
1397 char *padded;
1398
Ben Lindstromd20d0f32001-03-10 17:22:20 +00001399 if (datafellows & SSH_BUG_PASSWORDPAD) {
Ben Lindstrom664408d2001-06-09 01:42:01 +00001400 packet_put_cstring(password);
Ben Lindstromd20d0f32001-03-10 17:22:20 +00001401 return;
1402 }
Damien Miller79438cc2001-02-16 12:34:57 +11001403 size = roundup(strlen(password) + 1, 32);
Damien Miller07d86be2006-03-26 14:19:21 +11001404 padded = xcalloc(1, size);
Damien Miller79438cc2001-02-16 12:34:57 +11001405 strlcpy(padded, password, size);
1406 packet_put_string(padded, size);
Damien Millera5103f42014-02-04 11:20:14 +11001407 explicit_bzero(padded, size);
Darren Tuckera627d422013-06-02 07:31:17 +10001408 free(padded);
Damien Miller79438cc2001-02-16 12:34:57 +11001409}
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001410
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001411/* print all known host keys for a given host, but skip keys of given type */
1412static int
Damien Millerd925dcd2010-12-01 12:21:51 +11001413show_other_keys(struct hostkeys *hostkeys, Key *key)
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001414{
Damien Miller106bf1c2013-12-29 17:54:03 +11001415 int type[] = {
1416 KEY_RSA1,
1417 KEY_RSA,
1418 KEY_DSA,
1419 KEY_ECDSA,
1420 KEY_ED25519,
1421 -1
1422 };
Damien Millerd925dcd2010-12-01 12:21:51 +11001423 int i, ret = 0;
1424 char *fp, *ra;
1425 const struct hostkey_entry *found;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001426
1427 for (i = 0; type[i] != -1; i++) {
1428 if (type[i] == key->type)
1429 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +11001430 if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found))
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001431 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001432 fp = sshkey_fingerprint(found->key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001433 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001434 ra = sshkey_fingerprint(found->key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001435 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001436 if (fp == NULL || ra == NULL)
1437 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerd925dcd2010-12-01 12:21:51 +11001438 logit("WARNING: %s key found for host %s\n"
1439 "in %s:%lu\n"
1440 "%s key fingerprint %s.",
1441 key_type(found->key),
1442 found->host, found->file, found->line,
1443 key_type(found->key), fp);
1444 if (options.visual_host_key)
1445 logit("%s", ra);
Darren Tuckera627d422013-06-02 07:31:17 +10001446 free(ra);
1447 free(fp);
Damien Millerd925dcd2010-12-01 12:21:51 +11001448 ret = 1;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001449 }
Damien Millerd925dcd2010-12-01 12:21:51 +11001450 return ret;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001451}
Damien Miller5a388972003-11-17 21:10:47 +11001452
1453static void
1454warn_changed_key(Key *host_key)
1455{
1456 char *fp;
Damien Miller5a388972003-11-17 21:10:47 +11001457
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001458 fp = sshkey_fingerprint(host_key, options.fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001459 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001460 if (fp == NULL)
1461 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Miller5a388972003-11-17 21:10:47 +11001462
1463 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1464 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
1465 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1466 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
1467 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
Damien Miller05c89972011-01-06 22:42:04 +11001468 error("It is also possible that a host key has just been changed.");
Damien Miller5a388972003-11-17 21:10:47 +11001469 error("The fingerprint for the %s key sent by the remote host is\n%s.",
Damien Miller05c89972011-01-06 22:42:04 +11001470 key_type(host_key), fp);
Damien Miller5a388972003-11-17 21:10:47 +11001471 error("Please contact your system administrator.");
1472
Darren Tuckera627d422013-06-02 07:31:17 +10001473 free(fp);
Damien Miller5a388972003-11-17 21:10:47 +11001474}
Damien Millerd27b9472005-12-13 19:29:02 +11001475
1476/*
1477 * Execute a local command
1478 */
1479int
1480ssh_local_cmd(const char *args)
1481{
1482 char *shell;
1483 pid_t pid;
1484 int status;
Damien Millered3a8eb2011-01-07 10:02:52 +11001485 void (*osighand)(int);
Damien Millerd27b9472005-12-13 19:29:02 +11001486
1487 if (!options.permit_local_command ||
1488 args == NULL || !*args)
1489 return (1);
1490
Damien Miller38d9a962010-10-07 22:07:11 +11001491 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
Damien Millerd27b9472005-12-13 19:29:02 +11001492 shell = _PATH_BSHELL;
1493
Damien Millered3a8eb2011-01-07 10:02:52 +11001494 osighand = signal(SIGCHLD, SIG_DFL);
Damien Millerd27b9472005-12-13 19:29:02 +11001495 pid = fork();
1496 if (pid == 0) {
Damien Miller6fb6fd52011-01-16 23:17:45 +11001497 signal(SIGPIPE, SIG_DFL);
Damien Millerd27b9472005-12-13 19:29:02 +11001498 debug3("Executing %s -c \"%s\"", shell, args);
1499 execl(shell, shell, "-c", args, (char *)NULL);
1500 error("Couldn't execute %s -c \"%s\": %s",
1501 shell, args, strerror(errno));
1502 _exit(1);
1503 } else if (pid == -1)
1504 fatal("fork failed: %.100s", strerror(errno));
1505 while (waitpid(pid, &status, 0) == -1)
1506 if (errno != EINTR)
1507 fatal("Couldn't wait for child: %s", strerror(errno));
Damien Millered3a8eb2011-01-07 10:02:52 +11001508 signal(SIGCHLD, osighand);
Damien Millerd27b9472005-12-13 19:29:02 +11001509
1510 if (!WIFEXITED(status))
1511 return (1);
1512
1513 return (WEXITSTATUS(status));
1514}
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001515
1516void
1517maybe_add_key_to_agent(char *authfile, Key *private, char *comment,
1518 char *passphrase)
1519{
1520 int auth_sock = -1, r;
1521
1522 if (options.add_keys_to_agent == 0)
1523 return;
1524
1525 if ((r = ssh_get_authentication_socket(&auth_sock)) != 0) {
1526 debug3("no authentication agent, not adding key");
1527 return;
1528 }
1529
1530 if (options.add_keys_to_agent == 2 &&
1531 !ask_permission("Add key %s (%s) to agent?", authfile, comment)) {
1532 debug3("user denied adding this key");
1533 return;
1534 }
1535
1536 if ((r = ssh_add_identity_constrained(auth_sock, private, comment, 0,
1537 (options.add_keys_to_agent == 3))) == 0)
1538 debug("identity added to agent: %s", authfile);
1539 else
1540 debug("could not add identity to agent: %s (%d)", authfile, r);
1541}