blob: 3805d35d984554c7a8e183df518d8b05acdc840f [file] [log] [blame]
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001/* $OpenBSD: sshconnect.c,v 1.297 2018/02/23 15:58:38 markus 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
18#include <sys/types.h>
19#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110020#include <sys/stat.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100021#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100022#ifdef HAVE_SYS_TIME_H
23# include <sys/time.h>
24#endif
Damien Miller8ec8c3e2006-07-10 20:35:38 +100025
djm@openbsd.orgac2e3022018-02-23 02:34:33 +000026#include <net/if.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100027#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>
djm@openbsd.org45404282017-06-24 05:37:44 +000038#ifdef HAVE_POLL_H
39#include <poll.h>
40#endif
Damien Miller9c0c31d2010-10-12 13:30:44 +110041#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100042#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100043#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100046#include <unistd.h>
Darren Tuckerb59162d2018-02-23 15:20:42 +110047#ifdef HAVE_IFADDRS_H
48# include <ifaddrs.h>
49#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#include "key.h"
53#include "hostfile.h"
54#include "ssh.h"
Damien Miller1383bd82000-04-06 12:32:37 +100055#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "packet.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "uidswap.h"
58#include "compat.h"
Damien Miller450a7a12000-03-26 13:04:51 +100059#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100060#include "sshconnect.h"
Damien Miller450a7a12000-03-26 13:04:51 +100061#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100063#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "readconf.h"
65#include "atomicio.h"
Damien Miller37876e92003-05-15 10:19:46 +100066#include "dns.h"
Damien Miller1262b662013-08-21 02:44:24 +100067#include "monitor_fdpass.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110068#include "ssh2.h"
Damien Millerb7576772006-07-10 20:23:39 +100069#include "version.h"
djm@openbsd.org5e39a492014-12-04 02:24:32 +000070#include "authfile.h"
71#include "ssherr.h"
jcs@openbsd.orgf361df42015-11-15 22:26:49 +000072#include "authfd.h"
Damien Miller37876e92003-05-15 10:19:46 +100073
Damien Millereba71ba2000-04-29 23:57:08 +100074char *client_version_string = NULL;
75char *server_version_string = NULL;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000076struct sshkey *previous_host_key = NULL;
Damien Miller1383bd82000-04-06 12:32:37 +100077
Damien Millerc1af1d52005-11-05 15:08:57 +110078static int matching_host_key_dns = 0;
Damien Miller7392ae62003-06-11 22:05:25 +100079
Damien Millera41ccca2010-10-07 22:07:32 +110080static pid_t proxy_command_pid = 0;
81
Ben Lindstromf9c48842002-06-11 16:37:51 +000082/* import */
Damien Milleraae6c611999-12-06 11:47:28 +110083extern Options options;
Damien Miller34132e52000-01-14 15:45:46 +110084extern char *__progname;
Ben Lindstromf9c48842002-06-11 16:37:51 +000085extern uid_t original_real_uid;
86extern uid_t original_effective_uid;
Damien Milleraae6c611999-12-06 11:47:28 +110087
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000088static int show_other_keys(struct hostkeys *, struct sshkey *);
89static void warn_changed_key(struct sshkey *);
Ben Lindstrom3ed66402002-08-01 01:21:56 +000090
Damien Miller1262b662013-08-21 02:44:24 +100091/* Expand a proxy command */
92static char *
93expand_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 Miller0faf7472013-10-17 11:47:23 +110098 snprintf(strport, sizeof strport, "%d", port);
Damien Miller1262b662013-08-21 02:44:24 +100099 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 */
110static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000111ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port,
Damien Miller1262b662013-08-21 02:44:24 +1000112 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.orgfc77ccd2016-01-14 22:56:56 +0000175 close(sp[1]);
Damien Miller1262b662013-08-21 02:44:24 +1000176
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.orgdbee4112017-09-12 06:32:07 +0000182 if (ssh_packet_set_connection(ssh, sock, sock) == NULL)
183 return -1; /* ssh_packet_set_connection logs error */
Damien Miller1262b662013-08-21 02:44:24 +1000184
185 return 0;
186}
187
Damien Miller95def091999-11-25 00:26:21 +1100188/*
189 * Connect to the given ssh server using a proxy command.
190 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000191static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000192ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port,
193 const char *proxy_command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Damien Miller1262b662013-08-21 02:44:24 +1000195 char *command_string;
Damien Miller95def091999-11-25 00:26:21 +1100196 int pin[2], pout[2];
Damien Miller166fca82000-04-20 07:42:21 +1000197 pid_t pid;
Damien Miller1262b662013-08-21 02:44:24 +1000198 char *shell;
Damien Miller1d824ab2007-09-17 11:58:04 +1000199
Damien Miller38d9a962010-10-07 22:07:11 +1100200 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
Damien Miller1d824ab2007-09-17 11:58:04 +1000201 shell = _PATH_BSHELL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Miller95def091999-11-25 00:26:21 +1100203 /* 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 Miller9f0f5c62001-12-21 14:45:46 +1100206 strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207
Damien Miller1262b662013-08-21 02:44:24 +1000208 command_string = expand_proxy_command(proxy_command, options.user,
209 host, port);
Damien Miller95def091999-11-25 00:26:21 +1100210 debug("Executing proxy command: %.500s", command_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller95def091999-11-25 00:26:21 +1100212 /* Fork and execute the proxy command. */
213 if ((pid = fork()) == 0) {
214 char *argv[10];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 /* Child. Permanently give up superuser privileges. */
Damien Miller2e5fe882006-06-13 13:10:00 +1000217 permanently_drop_suid(original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100218
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 Miller1d824ab2007-09-17 11:58:04 +1000234 argv[0] = shell;
Damien Miller95def091999-11-25 00:26:21 +1100235 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 Miller6fb6fd52011-01-16 23:17:45 +1100241 signal(SIGPIPE, SIG_DFL);
Kevin Stevesfcd5d602001-01-07 11:45:22 +0000242 execv(argv[0], argv);
243 perror(argv[0]);
Damien Miller95def091999-11-25 00:26:21 +1100244 exit(1);
245 }
246 /* Parent. */
247 if (pid < 0)
248 fatal("fork failed: %.100s", strerror(errno));
Damien Miller8c4e18a2002-09-19 12:05:02 +1000249 else
250 proxy_command_pid = pid; /* save pid to clean up later */
Damien Miller95def091999-11-25 00:26:21 +1100251
252 /* Close child side of the descriptors. */
253 close(pin[0]);
254 close(pout[1]);
255
256 /* Free the command name. */
Darren Tuckera627d422013-06-02 07:31:17 +1000257 free(command_string);
Damien Miller95def091999-11-25 00:26:21 +1100258
259 /* Set the connection file descriptors. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000260 if (ssh_packet_set_connection(ssh, pout[0], pin[1]) == NULL)
261 return -1; /* ssh_packet_set_connection logs error */
Damien Miller95def091999-11-25 00:26:21 +1100262
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000263 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264}
265
Damien Millera41ccca2010-10-07 22:07:32 +1100266void
267ssh_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 Miller45fcdaa2010-10-07 22:07:58 +1100274 kill(proxy_command_pid, SIGHUP);
Damien Millera41ccca2010-10-07 22:07:32 +1100275}
276
Darren Tuckerb59162d2018-02-23 15:20:42 +1100277#ifdef HAVE_IFADDRS_H
Damien Miller95def091999-11-25 00:26:21 +1100278/*
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000279 * Search a interface address list (returned from getifaddrs(3)) for an
280 * address that matches the desired address family on the specifed interface.
281 * Returns 0 and fills in *resultp and *rlenp on success. Returns -1 on failure.
282 */
283static int
284check_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 Tuckerb59162d2018-02-23 15:20:42 +1100338#endif
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000339
340/*
Damien Miller95def091999-11-25 00:26:21 +1100341 * Creates a (possibly privileged) socket for use as the ssh connection.
342 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static int
Damien Miller2372ace2003-05-14 13:42:23 +1000344ssh_create_socket(int privileged, struct addrinfo *ai)
Damien Miller95def091999-11-25 00:26:21 +1100345{
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000346 int sock, r, oerrno;
347 struct sockaddr_storage bindaddr;
348 socklen_t bindaddrlen = 0;
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100349 struct addrinfo hints, *res = NULL;
Darren Tuckerb59162d2018-02-23 15:20:42 +1100350#ifdef HAVE_IFADDRS_H
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000351 struct ifaddrs *ifaddrs = NULL;
Darren Tuckerb59162d2018-02-23 15:20:42 +1100352#endif
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000353 char ntop[NI_MAXHOST];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Darren Tucker7bd98e72010-01-10 10:31:12 +1100355 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100356 if (sock < 0) {
Darren Tuckere6e52f82013-10-10 10:28:07 +1100357 error("socket: %s", strerror(errno));
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100358 return -1;
359 }
360 fcntl(sock, F_SETFD, FD_CLOEXEC);
Ben Lindstrome0f88042001-04-30 13:06:24 +0000361
362 /* Bind the socket to an alternative local IP address */
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000363 if (options.bind_address == NULL && options.bind_interface == NULL &&
364 !privileged)
Ben Lindstrome0f88042001-04-30 13:06:24 +0000365 return sock;
366
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000367 if (options.bind_address != NULL) {
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100368 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.orgac2e3022018-02-23 02:34:33 +0000373 if ((r = getaddrinfo(options.bind_address, NULL,
374 &hints, &res)) != 0) {
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100375 error("getaddrinfo: %s: %s", options.bind_address,
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000376 ssh_gai_strerror(r));
377 goto fail;
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100378 }
dtucker@openbsd.org3e19fb92018-02-23 04:18:46 +0000379 if (res == NULL) {
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000380 error("getaddrinfo: no addrs");
381 goto fail;
dtucker@openbsd.org3e19fb92018-02-23 04:18:46 +0000382 }
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000383 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 Tuckerb59162d2018-02-23 15:20:42 +1100390#ifdef HAVE_IFADDRS_H
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000391 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 Tuckerb59162d2018-02-23 15:20:42 +1100403#else
404 error("BindInterface not supported on this platform.");
405#endif
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000406 }
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 Lindstrome0f88042001-04-30 13:06:24 +0000412 }
Darren Tuckere6e52f82013-10-10 10:28:07 +1100413 /*
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.orgac2e3022018-02-23 02:34:33 +0000419 r = bindresvport_sa(sock,
420 bindaddrlen == 0 ? NULL : (struct sockaddr *)&bindaddr);
421 oerrno = errno;
Darren Tuckere6e52f82013-10-10 10:28:07 +1100422 PRIV_END;
423 if (r < 0) {
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000424 error("bindresvport_sa %s: %s", ntop,
425 strerror(oerrno));
Darren Tuckere6e52f82013-10-10 10:28:07 +1100426 goto fail;
427 }
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000428 } else if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) {
429 error("bind %s: %s", ntop, strerror(errno));
430 goto fail;
Ben Lindstrome0f88042001-04-30 13:06:24 +0000431 }
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000432 debug("%s: bound to %s", __func__, ntop);
433 /* success */
434 goto out;
435fail:
436 close(sock);
437 sock = -1;
438 out:
Damien Millerd1a7a9c2014-02-07 09:24:33 +1100439 if (res != NULL)
440 freeaddrinfo(res);
Darren Tuckerb59162d2018-02-23 15:20:42 +1100441#ifdef HAVE_IFADDRS_H
djm@openbsd.orgac2e3022018-02-23 02:34:33 +0000442 if (ifaddrs != NULL)
443 freeifaddrs(ifaddrs);
Darren Tuckerb59162d2018-02-23 15:20:42 +1100444#endif
Damien Miller95def091999-11-25 00:26:21 +1100445 return sock;
446}
447
djm@openbsd.org45404282017-06-24 05:37:44 +0000448/*
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 */
453static int
454waitrfd(int fd, int *timeoutp)
455{
456 struct pollfd pfd;
457 struct timeval t_start;
458 int oerrno, r;
459
dtucker@openbsd.org@openbsd.org5db6fbf2017-11-25 06:46:22 +0000460 monotime_tv(&t_start);
djm@openbsd.org45404282017-06-24 05:37:44 +0000461 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 Millerb78d5eb2003-05-16 11:39:04 +1000480static int
481timeout_connect(int sockfd, const struct sockaddr *serv_addr,
Damien Miller67bd0622007-09-17 12:06:57 +1000482 socklen_t addrlen, int *timeoutp)
Damien Millerb78d5eb2003-05-16 11:39:04 +1000483{
djm@openbsd.org45404282017-06-24 05:37:44 +0000484 int optval = 0;
485 socklen_t optlen = sizeof(optval);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000486
djm@openbsd.org45404282017-06-24 05:37:44 +0000487 /* No timeout: just do a blocking connect() */
488 if (*timeoutp <= 0)
489 return connect(sockfd, serv_addr, addrlen);
Damien Millerb78d5eb2003-05-16 11:39:04 +1000490
Damien Millerf6723f02004-01-27 21:21:27 +1100491 set_nonblock(sockfd);
djm@openbsd.org45404282017-06-24 05:37:44 +0000492 if (connect(sockfd, serv_addr, addrlen) == 0) {
493 /* Succeeded already? */
Damien Millerf6723f02004-01-27 21:21:27 +1100494 unset_nonblock(sockfd);
djm@openbsd.org45404282017-06-24 05:37:44 +0000495 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 Millerf6723f02004-01-27 21:21:27 +1100506 }
djm@openbsd.org45404282017-06-24 05:37:44 +0000507 if (optval != 0) {
508 errno = optval;
509 return -1;
Damien Miller67bd0622007-09-17 12:06:57 +1000510 }
djm@openbsd.org45404282017-06-24 05:37:44 +0000511 unset_nonblock(sockfd);
512 return 0;
Damien Millerb78d5eb2003-05-16 11:39:04 +1000513}
514
Damien Miller95def091999-11-25 00:26:21 +1100515/*
Damien Miller34132e52000-01-14 15:45:46 +1100516 * 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 Lindstromf9c48842002-06-11 16:37:51 +0000518 * If port is 0, the default port will be used. If needpriv is true,
Damien Miller95def091999-11-25 00:26:21 +1100519 * a privileged port will be allocated to make the connection.
Ben Lindstromf9c48842002-06-11 16:37:51 +0000520 * This requires super-user privileges if needpriv is true.
Damien Miller95def091999-11-25 00:26:21 +1100521 * 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 Miller0faf7472013-10-17 11:47:23 +1100526static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000527ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
Damien Miller0faf7472013-10-17 11:47:23 +1100528 struct sockaddr_storage *hostaddr, u_short port, int family,
529 int connection_attempts, int *timeout_ms, int want_keepalive, int needpriv)
Damien Miller95def091999-11-25 00:26:21 +1100530{
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000531 int on = 1;
djm@openbsd.org7c779912018-01-23 05:17:04 +0000532 int oerrno, sock = -1, attempt;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000533 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0faf7472013-10-17 11:47:23 +1100534 struct addrinfo *ai;
Damien Miller95def091999-11-25 00:26:21 +1100535
djm@openbsd.org46152af2015-09-04 04:55:24 +0000536 debug2("%s: needpriv %d", __func__, needpriv);
djm@openbsd.org88b6fcd2015-11-19 08:23:27 +0000537 memset(ntop, 0, sizeof(ntop));
538 memset(strport, 0, sizeof(strport));
Damien Miller95def091999-11-25 00:26:21 +1100539
Damien Miller56e5e6a2006-04-23 12:08:59 +1000540 for (attempt = 0; attempt < connection_attempts; attempt++) {
Damien Millerf4bcd102006-10-24 03:02:23 +1000541 if (attempt > 0) {
542 /* Sleep a moment before retrying. */
543 sleep(1);
Damien Miller95def091999-11-25 00:26:21 +1100544 debug("Trying again...");
Damien Millerf4bcd102006-10-24 03:02:23 +1000545 }
Damien Miller56e5e6a2006-04-23 12:08:59 +1000546 /*
547 * Loop through addresses for this host, and try each one in
548 * sequence until the connection succeeds.
549 */
Damien Miller34132e52000-01-14 15:45:46 +1100550 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller0faf7472013-10-17 11:47:23 +1100551 if (ai->ai_family != AF_INET &&
djm@openbsd.org7c779912018-01-23 05:17:04 +0000552 ai->ai_family != AF_INET6) {
553 errno = EAFNOSUPPORT;
Damien Miller34132e52000-01-14 15:45:46 +1100554 continue;
djm@openbsd.org7c779912018-01-23 05:17:04 +0000555 }
Damien Miller34132e52000-01-14 15:45:46 +1100556 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
557 ntop, sizeof(ntop), strport, sizeof(strport),
558 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
djm@openbsd.org7c779912018-01-23 05:17:04 +0000559 oerrno = errno;
djm@openbsd.org46152af2015-09-04 04:55:24 +0000560 error("%s: getnameinfo failed", __func__);
djm@openbsd.org7c779912018-01-23 05:17:04 +0000561 errno = oerrno;
Damien Miller34132e52000-01-14 15:45:46 +1100562 continue;
563 }
564 debug("Connecting to %.200s [%.100s] port %s.",
565 host, ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +1100566
Damien Miller34132e52000-01-14 15:45:46 +1100567 /* Create a socket for connecting. */
Damien Miller2372ace2003-05-14 13:42:23 +1000568 sock = ssh_create_socket(needpriv, ai);
stsp@openbsd.orga60c5dc2018-01-23 18:33:49 +0000569 if (sock < 0) {
Ben Lindstromf9cedb92001-08-06 21:07:11 +0000570 /* Any error is already output */
djm@openbsd.org7c779912018-01-23 05:17:04 +0000571 errno = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100572 continue;
stsp@openbsd.orga60c5dc2018-01-23 18:33:49 +0000573 }
Damien Miller95def091999-11-25 00:26:21 +1100574
Damien Millerb78d5eb2003-05-16 11:39:04 +1000575 if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
Damien Miller67bd0622007-09-17 12:06:57 +1000576 timeout_ms) >= 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100577 /* Successful connection. */
Ben Lindstroma3700052001-04-05 23:26:32 +0000578 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
Damien Miller95def091999-11-25 00:26:21 +1100579 break;
Damien Miller34132e52000-01-14 15:45:46 +1100580 } else {
djm@openbsd.org7c779912018-01-23 05:17:04 +0000581 oerrno = errno;
Ben Lindstrom728aa7e2002-07-15 17:48:11 +0000582 debug("connect to address %s port %s: %s",
583 ntop, strport, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100584 close(sock);
Damien Miller56e5e6a2006-04-23 12:08:59 +1000585 sock = -1;
djm@openbsd.org7c779912018-01-23 05:17:04 +0000586 errno = oerrno;
Damien Miller95def091999-11-25 00:26:21 +1100587 }
Damien Miller95def091999-11-25 00:26:21 +1100588 }
Damien Miller56e5e6a2006-04-23 12:08:59 +1000589 if (sock != -1)
Damien Miller34132e52000-01-14 15:45:46 +1100590 break; /* Successful connection. */
Damien Miller95def091999-11-25 00:26:21 +1100591 }
Damien Miller34132e52000-01-14 15:45:46 +1100592
Damien Miller95def091999-11-25 00:26:21 +1100593 /* Return failure if we didn't get a successful connection. */
Damien Miller56e5e6a2006-04-23 12:08:59 +1000594 if (sock == -1) {
Darren Tuckerb2161e32005-01-20 11:00:46 +1100595 error("ssh: connect to host %s port %s: %s",
djm@openbsd.org7c779912018-01-23 05:17:04 +0000596 host, strport, errno == 0 ? "failure" : strerror(errno));
597 return -1;
Ben Lindstroma6cd75c2002-07-11 04:00:19 +0000598 }
Damien Miller95def091999-11-25 00:26:21 +1100599
600 debug("Connection established.");
601
Damien Miller12c150e2003-12-17 16:31:10 +1100602 /* Set SO_KEEPALIVE if requested. */
Damien Miller67bd0622007-09-17 12:06:57 +1000603 if (want_keepalive &&
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000604 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
605 sizeof(on)) < 0)
606 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100607
608 /* Set the connection. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000609 if (ssh_packet_set_connection(ssh, sock, sock) == NULL)
610 return -1; /* ssh_packet_set_connection logs error */
Damien Miller95def091999-11-25 00:26:21 +1100611
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000612 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100613}
614
Damien Miller0faf7472013-10-17 11:47:23 +1100615int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000616ssh_connect(struct ssh *ssh, const char *host, struct addrinfo *addrs,
Damien Miller0faf7472013-10-17 11:47:23 +1100617 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.orgdbee4112017-09-12 06:32:07 +0000621 return ssh_connect_direct(ssh, host, addrs, hostaddr, port,
622 family, connection_attempts, timeout_ms, want_keepalive,
623 needpriv);
Damien Miller0faf7472013-10-17 11:47:23 +1100624 } else if (strcmp(options.proxy_command, "-") == 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000625 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 Miller0faf7472013-10-17 11:47:23 +1100629 } else if (options.proxy_use_fdpass) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000630 return ssh_proxy_fdpass_connect(ssh, host, port,
Damien Miller0faf7472013-10-17 11:47:23 +1100631 options.proxy_command);
632 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000633 return ssh_proxy_connect(ssh, host, port, options.proxy_command);
Damien Miller0faf7472013-10-17 11:47:23 +1100634}
635
Darren Tucker00c15182012-09-06 21:21:56 +1000636static void
637send_client_banner(int connection_out, int minor1)
638{
Darren Tucker00c15182012-09-06 21:21:56 +1000639 /* Send our own protocol version identification. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000640 xasprintf(&client_version_string, "SSH-%d.%d-%.100s\r\n",
641 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION);
markus@openbsd.orga3068632016-01-14 16:17:39 +0000642 if (atomicio(vwrite, connection_out, client_version_string,
Darren Tucker00c15182012-09-06 21:21:56 +1000643 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 Milleraae6c611999-12-06 11:47:28 +1100649/*
Damien Miller95def091999-11-25 00:26:21 +1100650 * Waits for the server identification string, and sends our own
651 * identification string.
652 */
Darren Tucker1cc55d72009-06-21 18:17:19 +1000653void
Damien Miller67bd0622007-09-17 12:06:57 +1000654ssh_exchange_identification(int timeout_ms)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655{
Damien Miller95def091999-11-25 00:26:21 +1100656 char buf[256], remote_version[256]; /* must be same size! */
Damien Millereccb9de2005-06-17 12:59:34 +1000657 int remote_major, remote_minor, mismatch;
Damien Miller95def091999-11-25 00:26:21 +1100658 int connection_in = packet_get_connection_in();
659 int connection_out = packet_get_connection_out();
Damien Miller1e88ea62006-07-10 20:15:56 +1000660 u_int i, n;
Damien Miller67bd0622007-09-17 12:06:57 +1000661 size_t len;
djm@openbsd.org45404282017-06-24 05:37:44 +0000662 int rc;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000664 send_client_banner(connection_out, 0);
Darren Tucker00c15182012-09-06 21:21:56 +1000665
Damien Millerb253cc42005-05-26 12:23:44 +1000666 /* Read other side's version identification. */
Damien Miller1e88ea62006-07-10 20:15:56 +1000667 for (n = 0;;) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000668 for (i = 0; i < sizeof(buf) - 1; i++) {
Damien Miller67bd0622007-09-17 12:06:57 +1000669 if (timeout_ms > 0) {
djm@openbsd.org45404282017-06-24 05:37:44 +0000670 rc = waitrfd(connection_in, &timeout_ms);
671 if (rc == -1 && errno == ETIMEDOUT) {
Damien Miller67bd0622007-09-17 12:06:57 +1000672 fatal("Connection timed out during "
673 "banner exchange");
djm@openbsd.org45404282017-06-24 05:37:44 +0000674 } else if (rc == -1) {
675 fatal("%s: %s",
676 __func__, strerror(errno));
Damien Miller67bd0622007-09-17 12:06:57 +1000677 }
678 }
679
markus@openbsd.orga3068632016-01-14 16:17:39 +0000680 len = atomicio(read, connection_in, &buf[i], 1);
Damien Miller46d38de2005-07-17 17:02:09 +1000681 if (len != 1 && errno == EPIPE)
Damien Miller67bd0622007-09-17 12:06:57 +1000682 fatal("ssh_exchange_identification: "
683 "Connection closed by remote host");
Damien Millerb253cc42005-05-26 12:23:44 +1000684 else if (len != 1)
Damien Miller67bd0622007-09-17 12:06:57 +1000685 fatal("ssh_exchange_identification: "
686 "read: %.100s", strerror(errno));
Damien Millerf6d9e222000-06-18 14:50:44 +1000687 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 Miller1e88ea62006-07-10 20:15:56 +1000696 if (++n > 65536)
Damien Miller67bd0622007-09-17 12:06:57 +1000697 fatal("ssh_exchange_identification: "
698 "No banner received");
Damien Miller95def091999-11-25 00:26:21 +1100699 }
Damien Millerf6d9e222000-06-18 14:50:44 +1000700 buf[sizeof(buf) - 1] = 0;
701 if (strncmp(buf, "SSH-", 4) == 0)
Damien Miller95def091999-11-25 00:26:21 +1100702 break;
Damien Millerf6d9e222000-06-18 14:50:44 +1000703 debug("ssh_exchange_identification: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704 }
Damien Miller1383bd82000-04-06 12:32:37 +1000705 server_version_string = xstrdup(buf);
Damien Miller95def091999-11-25 00:26:21 +1100706
Damien Miller5428f641999-11-25 11:54:57 +1100707 /*
708 * Check that the versions match. In future this might accept
709 * several versions and set appropriate flags to handle them.
710 */
Damien Miller1383bd82000-04-06 12:32:37 +1000711 if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
712 &remote_major, &remote_minor, remote_version) != 3)
Damien Miller95def091999-11-25 00:26:21 +1100713 fatal("Bad remote protocol version identification: '%.100s'", buf);
714 debug("Remote protocol version %d.%d, remote software version %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100715 remote_major, remote_minor, remote_version);
Damien Miller95def091999-11-25 00:26:21 +1100716
markus@openbsd.org48b3b2b2015-01-19 20:20:20 +0000717 active_state->compat = compat_datafellows(remote_version);
Damien Miller78928792000-04-12 20:17:38 +1000718 mismatch = 0;
Damien Miller1383bd82000-04-06 12:32:37 +1000719
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000720 switch (remote_major) {
Damien Miller78928792000-04-12 20:17:38 +1000721 case 2:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000722 break;
723 case 1:
724 if (remote_minor != 99)
725 mismatch = 1;
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000726 break;
Damien Miller4af51302000-04-16 11:18:38 +1000727 default:
Damien Miller78928792000-04-12 20:17:38 +1000728 mismatch = 1;
729 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730 }
Damien Miller78928792000-04-12 20:17:38 +1000731 if (mismatch)
Damien Miller95def091999-11-25 00:26:21 +1100732 fatal("Protocol major versions differ: %d vs. %d",
djm@openbsd.org99f95ba2017-04-30 23:11:45 +0000733 PROTOCOL_MAJOR_2, remote_major);
Damien Miller324541e2013-12-31 12:25:40 +1100734 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 Miller1383bd82000-04-06 12:32:37 +1000737 chop(server_version_string);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738}
739
Ben Lindstromb6c06d92001-02-10 23:21:09 +0000740/* defaults to 'no' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000741static int
Damien Millere3980042001-10-10 15:02:03 +1000742confirm(const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743{
Damien Miller49d795c2002-01-22 23:34:12 +1100744 const char *msg, *again = "Please type 'yes' or 'no': ";
745 char *p;
746 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747
Ben Lindstromb6c06d92001-02-10 23:21:09 +0000748 if (options.batch_mode)
749 return 0;
Damien Miller49d795c2002-01-22 23:34:12 +1100750 for (msg = prompt;;msg = again) {
751 p = read_passphrase(msg, RP_ECHO);
djm@openbsd.orge0ce54c2017-12-06 05:06:21 +0000752 if (p == NULL)
753 return 0;
754 p[strcspn(p, "\n")] = '\0';
755 if (p[0] == '\0' || strcasecmp(p, "no") == 0)
Damien Miller49d795c2002-01-22 23:34:12 +1100756 ret = 0;
djm@openbsd.orge0ce54c2017-12-06 05:06:21 +0000757 else if (strcasecmp(p, "yes") == 0)
Damien Miller49d795c2002-01-22 23:34:12 +1100758 ret = 1;
Darren Tuckera627d422013-06-02 07:31:17 +1000759 free(p);
Damien Miller49d795c2002-01-22 23:34:12 +1100760 if (ret != -1)
761 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763}
764
Damien Miller0a80ca12010-02-27 07:55:05 +1100765static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000766check_host_cert(const char *host, const struct sshkey *host_key)
Damien Miller0a80ca12010-02-27 07:55:05 +1100767{
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 }
Damien Miller86687062014-07-02 15:28:02 +1000774 if (buffer_len(host_key->cert->critical) != 0) {
Damien Miller4e270b02010-04-16 15:56:21 +1000775 error("Certificate for %s contains unsupported "
776 "critical options(s)", host);
Damien Miller0a80ca12010-02-27 07:55:05 +1100777 return 0;
778 }
779 return 1;
780}
781
Damien Millerd925dcd2010-12-01 12:21:51 +1100782static int
783sockaddr_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 */
801void
802get_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.org9ce86c92015-01-28 22:36:00 +0000831 fatal("%s: getnameinfo failed", __func__);
Damien Millerd925dcd2010-12-01 12:21:51 +1100832 *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 Miller95def091999-11-25 00:26:21 +1100855/*
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000856 * check whether the supplied host key is valid, return -1 if the key
Damien Miller295ee632011-05-29 21:42:31 +1000857 * is not valid. user_hostfile[0] will not be updated if 'readonly' is true.
Damien Miller95def091999-11-25 00:26:21 +1100858 */
Damien Millerda828392006-08-05 11:35:45 +1000859#define RDRW 0
860#define RDONLY 1
861#define ROQUIET 2
Ben Lindstrombba81212001-06-25 05:01:22 +0000862static int
Damien Millerda828392006-08-05 11:35:45 +1000863check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000864 struct sshkey *host_key, int readonly,
Damien Miller295ee632011-05-29 21:42:31 +1000865 char **user_hostfiles, u_int num_user_hostfiles,
866 char **system_hostfiles, u_int num_system_hostfiles)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867{
Damien Miller95def091999-11-25 00:26:21 +1100868 HostStatus host_status;
869 HostStatus ip_status;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000870 struct sshkey *raw_key = NULL;
Damien Miller295ee632011-05-29 21:42:31 +1000871 char *ip = NULL, *host = NULL;
872 char hostline[1000], *hostp, *fp, *ra;
Damien Miller49d795c2002-01-22 23:34:12 +1100873 char msg[1024];
Damien Miller295ee632011-05-29 21:42:31 +1000874 const char *type;
Damien Millerd925dcd2010-12-01 12:21:51 +1100875 const struct hostkey_entry *host_found, *ip_found;
Damien Miller295ee632011-05-29 21:42:31 +1000876 int len, cancelled_forwarding = 0;
877 int local = sockaddr_is_local(hostaddr);
markus@openbsd.orgbeb965b2017-05-30 14:13:40 +0000878 int r, want_cert = sshkey_is_cert(host_key), host_ip_differ = 0;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000879 int hostkey_trusted = 0; /* Known or explicitly accepted by user */
Damien Miller295ee632011-05-29 21:42:31 +1000880 struct hostkeys *host_hostkeys, *ip_hostkeys;
881 u_int i;
Damien Miller34132e52000-01-14 15:45:46 +1100882
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 Lindstrom3cecc9a2001-10-03 17:39:38 +0000891 if (options.no_host_authentication_for_localhost == 1 && local &&
892 options.host_key_alias == NULL) {
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000893 debug("Forcing accepting of host key for "
894 "loopback/localhost.");
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000895 return 0;
Damien Miller34132e52000-01-14 15:45:46 +1100896 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897
Damien Milleraae6c611999-12-06 11:47:28 +1100898 /*
Damien Millerd925dcd2010-12-01 12:21:51 +1100899 * Prepare the hostname and address strings used for hostkey lookup.
900 * In some cases, these will have a port number appended.
Damien Milleraae6c611999-12-06 11:47:28 +1100901 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100902 get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip);
Darren Tucker78913e02008-06-13 04:47:34 +1000903
904 /*
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000905 * 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 Tuckerda345532006-07-10 23:04:19 +1000908 if (options.check_host_ip && (local ||
909 strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
Damien Milleraae6c611999-12-06 11:47:28 +1100910 options.check_host_ip = 0;
911
Damien Millerd925dcd2010-12-01 12:21:51 +1100912 host_hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000913 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 Millerd925dcd2010-12-01 12:21:51 +1100917
918 ip_hostkeys = NULL;
919 if (!want_cert && options.check_host_ip) {
920 ip_hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000921 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 Lindstrom4dccfa52000-12-28 16:40:05 +0000925 }
926
Damien Miller0a80ca12010-02-27 07:55:05 +1100927 retry:
Damien Millerd925dcd2010-12-01 12:21:51 +1100928 /* Reload these as they may have changed on cert->key downgrade */
markus@openbsd.orgbeb965b2017-05-30 14:13:40 +0000929 want_cert = sshkey_is_cert(host_key);
930 type = sshkey_type(host_key);
Damien Miller0a80ca12010-02-27 07:55:05 +1100931
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000932 /*
Damien Miller788f2122005-11-05 15:14:59 +1100933 * Check if the host key is present in the user's list of known
Damien Miller5428f641999-11-25 11:54:57 +1100934 * hosts or in the systemwide list.
935 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100936 host_status = check_key_in_hostkeys(host_hostkeys, host_key,
937 &host_found);
938
Damien Miller5428f641999-11-25 11:54:57 +1100939 /*
Damien Miller5428f641999-11-25 11:54:57 +1100940 * Also perform check for the ip address, skip the check if we are
Damien Miller0a80ca12010-02-27 07:55:05 +1100941 * localhost, looking for a certificate, or the hostname was an ip
942 * address to begin with.
Damien Miller5428f641999-11-25 11:54:57 +1100943 */
Damien Millerd925dcd2010-12-01 12:21:51 +1100944 if (!want_cert && ip_hostkeys != NULL) {
945 ip_status = check_key_in_hostkeys(ip_hostkeys, host_key,
946 &ip_found);
Damien Miller95def091999-11-25 00:26:21 +1100947 if (host_status == HOST_CHANGED &&
Damien Millerd925dcd2010-12-01 12:21:51 +1100948 (ip_status != HOST_CHANGED ||
949 (ip_found != NULL &&
markus@openbsd.orgbeb965b2017-05-30 14:13:40 +0000950 !sshkey_equal(ip_found->key, host_found->key))))
Damien Miller95def091999-11-25 00:26:21 +1100951 host_ip_differ = 1;
Damien Miller95def091999-11-25 00:26:21 +1100952 } else
953 ip_status = host_status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954
Damien Miller95def091999-11-25 00:26:21 +1100955 switch (host_status) {
956 case HOST_OK:
957 /* The host is known and the key matches. */
Damien Miller0a80ca12010-02-27 07:55:05 +1100958 debug("Host '%.200s' is known and matches the %s host %s.",
959 host, type, want_cert ? "certificate" : "key");
Damien Millerd925dcd2010-12-01 12:21:51 +1100960 debug("Found %s in %s:%lu", want_cert ? "CA key" : "key",
961 host_found->file, host_found->line);
djm@openbsd.org6f8ca3b2017-06-24 05:35:05 +0000962 if (want_cert &&
963 !check_host_cert(options.host_key_alias == NULL ?
964 hostname : options.host_key_alias, host_key))
Damien Miller0a80ca12010-02-27 07:55:05 +1100965 goto fail;
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000966 if (options.check_host_ip && ip_status == HOST_NEW) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100967 if (readonly || want_cert)
Damien Miller996acd22003-04-09 20:59:48 +1000968 logit("%s host key for IP address "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000969 "'%.128s' not in list of known hosts.",
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +0000970 type, ip);
Damien Miller295ee632011-05-29 21:42:31 +1000971 else if (!add_host_to_hostfile(user_hostfiles[0], ip,
Damien Millere1776152005-03-01 21:47:37 +1100972 host_key, options.hash_known_hosts))
Damien Miller996acd22003-04-09 20:59:48 +1000973 logit("Failed to add the %s host key for IP "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000974 "address '%.128s' to the list of known "
dtucker@openbsd.org496aeb22015-05-28 05:41:29 +0000975 "hosts (%.500s).", type, ip,
Damien Miller295ee632011-05-29 21:42:31 +1000976 user_hostfiles[0]);
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000977 else
Damien Miller996acd22003-04-09 20:59:48 +1000978 logit("Warning: Permanently added the %s host "
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000979 "key for IP address '%.128s' to the list "
980 "of known hosts.", type, ip);
Damien Miller10288242008-06-30 00:04:03 +1000981 } else if (options.visual_host_key) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000982 fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000983 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000984 ra = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000985 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000986 if (fp == NULL || ra == NULL)
987 fatal("%s: sshkey_fingerprint fail", __func__);
djm@openbsd.orgac3451d2015-09-04 03:57:38 +0000988 logit("Host key fingerprint is %s\n%s", fp, ra);
Darren Tuckera627d422013-06-02 07:31:17 +1000989 free(ra);
990 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100991 }
djm@openbsd.org8d4f8722015-01-26 03:04:45 +0000992 hostkey_trusted = 1;
Damien Miller95def091999-11-25 00:26:21 +1100993 break;
994 case HOST_NEW:
Damien Millerda828392006-08-05 11:35:45 +1000995 if (options.host_key_alias == NULL && port != 0 &&
996 port != SSH_DEFAULT_PORT) {
997 debug("checking without port identifier");
Damien Millerece92c82008-11-03 19:25:03 +1100998 if (check_host_key(hostname, hostaddr, 0, host_key,
Damien Miller295ee632011-05-29 21:42:31 +1000999 ROQUIET, user_hostfiles, num_user_hostfiles,
1000 system_hostfiles, num_system_hostfiles) == 0) {
Damien Millerda828392006-08-05 11:35:45 +10001001 debug("found matching key w/out port");
1002 break;
1003 }
1004 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001005 if (readonly || want_cert)
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001006 goto fail;
Damien Miller95def091999-11-25 00:26:21 +11001007 /* The host is new. */
djm@openbsd.org22376d22017-09-03 23:33:13 +00001008 if (options.strict_host_key_checking ==
1009 SSH_STRICT_HOSTKEY_YES) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001010 /*
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.org22376d22017-09-03 23:33:13 +00001018 } else if (options.strict_host_key_checking ==
1019 SSH_STRICT_HOSTKEY_ASK) {
Damien Miller7392ae62003-06-11 22:05:25 +10001020 char msg1[1024], msg2[1024];
1021
Damien Millerd925dcd2010-12-01 12:21:51 +11001022 if (show_other_keys(host_hostkeys, host_key))
Damien Miller7392ae62003-06-11 22:05:25 +10001023 snprintf(msg1, sizeof(msg1),
Damien Miller0dc1bef2005-07-17 17:22:45 +10001024 "\nbut keys of different type are already"
1025 " known for this host.");
Damien Miller7392ae62003-06-11 22:05:25 +10001026 else
1027 snprintf(msg1, sizeof(msg1), ".");
Damien Miller95def091999-11-25 00:26:21 +11001028 /* The default */
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001029 fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001030 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001031 ra = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001032 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001033 if (fp == NULL || ra == NULL)
1034 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Miller7392ae62003-06-11 22:05:25 +10001035 msg2[0] = '\0';
Damien Miller7392ae62003-06-11 22:05:25 +10001036 if (options.verify_host_key_dns) {
Damien Miller150b5572003-11-17 21:19:29 +11001037 if (matching_host_key_dns)
Damien Miller7392ae62003-06-11 22:05:25 +10001038 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 Miller49d795c2002-01-22 23:34:12 +11001046 snprintf(msg, sizeof(msg),
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001047 "The authenticity of host '%.200s (%s)' can't be "
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001048 "established%s\n"
Damien Miller10288242008-06-30 00:04:03 +10001049 "%s key fingerprint is %s.%s%s\n%s"
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001050 "Are you sure you want to continue connecting "
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001051 "(yes/no)? ",
Damien Miller10288242008-06-30 00:04:03 +10001052 host, ip, msg1, type, fp,
1053 options.visual_host_key ? "\n" : "",
1054 options.visual_host_key ? ra : "",
1055 msg2);
Darren Tuckera627d422013-06-02 07:31:17 +10001056 free(ra);
1057 free(fp);
Damien Miller49d795c2002-01-22 23:34:12 +11001058 if (!confirm(msg))
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001059 goto fail;
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001060 hostkey_trusted = 1; /* user explicitly confirmed */
Damien Miller95def091999-11-25 00:26:21 +11001061 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001062 /*
djm@openbsd.org22376d22017-09-03 23:33:13 +00001063 * If in "new" or "off" strict mode, add the key automatically
1064 * to the local known_hosts file.
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001065 */
Damien Miller1227d4c2005-03-02 12:06:51 +11001066 if (options.check_host_ip && ip_status == HOST_NEW) {
Damien Millerd925dcd2010-12-01 12:21:51 +11001067 snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
Damien Miller1227d4c2005-03-02 12:06:51 +11001068 hostp = hostline;
1069 if (options.hash_known_hosts) {
1070 /* Add hash of host and IP separately */
Damien Miller295ee632011-05-29 21:42:31 +10001071 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 Miller1227d4c2005-03-02 12:06:51 +11001074 host_key, options.hash_known_hosts);
1075 } else {
1076 /* Add unhashed "host,ip" */
Damien Miller295ee632011-05-29 21:42:31 +10001077 r = add_host_to_hostfile(user_hostfiles[0],
Damien Miller1227d4c2005-03-02 12:06:51 +11001078 hostline, host_key,
1079 options.hash_known_hosts);
1080 }
1081 } else {
Damien Miller295ee632011-05-29 21:42:31 +10001082 r = add_host_to_hostfile(user_hostfiles[0], host,
1083 host_key, options.hash_known_hosts);
Damien Miller1227d4c2005-03-02 12:06:51 +11001084 hostp = host;
1085 }
1086
1087 if (!r)
Damien Miller996acd22003-04-09 20:59:48 +10001088 logit("Failed to add the host to the list of known "
Damien Miller295ee632011-05-29 21:42:31 +10001089 "hosts (%.500s).", user_hostfiles[0]);
Damien Miller95def091999-11-25 00:26:21 +11001090 else
Damien Miller996acd22003-04-09 20:59:48 +10001091 logit("Warning: Permanently added '%.200s' (%s) to the "
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001092 "list of known hosts.", hostp, type);
Damien Miller95def091999-11-25 00:26:21 +11001093 break;
Damien Miller1aed65e2010-03-04 21:53:35 +11001094 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.org22376d22017-09-03 23:33:13 +00001106 if (options.strict_host_key_checking !=
1107 SSH_STRICT_HOSTKEY_OFF) {
Damien Miller1aed65e2010-03-04 21:53:35 +11001108 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 Miller95def091999-11-25 00:26:21 +11001114 case HOST_CHANGED:
Damien Miller0a80ca12010-02-27 07:55:05 +11001115 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 Millerd925dcd2010-12-01 12:21:51 +11001122 "match %s in %s:%lu", CA_MARKER,
1123 host_found->file, host_found->line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001124 goto fail;
1125 }
Damien Millerda828392006-08-05 11:35:45 +10001126 if (readonly == ROQUIET)
1127 goto fail;
Damien Miller95def091999-11-25 00:26:21 +11001128 if (options.check_host_ip && host_ip_differ) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001129 char *key_msg;
Damien Miller95def091999-11-25 00:26:21 +11001130 if (ip_status == HOST_NEW)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001131 key_msg = "is unknown";
Damien Miller95def091999-11-25 00:26:21 +11001132 else if (ip_status == HOST_OK)
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001133 key_msg = "is unchanged";
Damien Miller95def091999-11-25 00:26:21 +11001134 else
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001135 key_msg = "has a different value";
Damien Miller95def091999-11-25 00:26:21 +11001136 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1137 error("@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @");
1138 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Damien Millere247cc42000-05-07 12:03:14 +10001139 error("The %s host key for %s has changed,", type, host);
Darren Tuckerff4454d2008-06-13 10:21:51 +10001140 error("and the key for the corresponding IP address %s", ip);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001141 error("%s. This could either mean that", key_msg);
Damien Miller95def091999-11-25 00:26:21 +11001142 error("DNS SPOOFING is happening or the IP address for the host");
Ben Lindstrom46c16222000-12-22 01:43:59 +00001143 error("and its host key have changed at the same time.");
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001144 if (ip_status != HOST_NEW)
Damien Millerd925dcd2010-12-01 12:21:51 +11001145 error("Offending key for IP in %s:%lu",
1146 ip_found->file, ip_found->line);
Damien Miller95def091999-11-25 00:26:21 +11001147 }
1148 /* The host key has changed. */
Damien Miller5a388972003-11-17 21:10:47 +11001149 warn_changed_key(host_key);
Damien Miller95def091999-11-25 00:26:21 +11001150 error("Add correct host key in %.100s to get rid of this message.",
Damien Miller295ee632011-05-29 21:42:31 +10001151 user_hostfiles[0]);
markus@openbsd.orgbeb965b2017-05-30 14:13:40 +00001152 error("Offending %s key in %s:%lu",
1153 sshkey_type(host_found->key),
Damien Millerd925dcd2010-12-01 12:21:51 +11001154 host_found->file, host_found->line);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155
Damien Miller5428f641999-11-25 11:54:57 +11001156 /*
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.org22376d22017-09-03 23:33:13 +00001160 if (options.strict_host_key_checking !=
1161 SSH_STRICT_HOSTKEY_OFF) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001162 error("%s host key for %.200s has changed and you have "
1163 "requested strict checking.", type, host);
1164 goto fail;
1165 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166
Damien Miller1aed65e2010-03-04 21:53:35 +11001167 continue_unsafe:
Damien Miller5428f641999-11-25 11:54:57 +11001168 /*
1169 * If strict host key checking has not been requested, allow
Damien Miller941ac452003-06-04 20:31:53 +10001170 * the connection but without MITM-able authentication or
Damien Miller437edb92006-08-05 09:11:13 +10001171 * forwarding.
Damien Miller5428f641999-11-25 11:54:57 +11001172 */
Damien Miller95def091999-11-25 00:26:21 +11001173 if (options.password_authentication) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001174 error("Password authentication is disabled to avoid "
1175 "man-in-the-middle attacks.");
Damien Miller95def091999-11-25 00:26:21 +11001176 options.password_authentication = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001177 cancelled_forwarding = 1;
Damien Miller95def091999-11-25 00:26:21 +11001178 }
Damien Miller941ac452003-06-04 20:31:53 +10001179 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 Tucker068e01f2008-07-02 22:33:55 +10001184 cancelled_forwarding = 1;
Damien Miller941ac452003-06-04 20:31:53 +10001185 }
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 Tucker068e01f2008-07-02 22:33:55 +10001190 cancelled_forwarding = 1;
Damien Miller941ac452003-06-04 20:31:53 +10001191 }
Damien Miller95def091999-11-25 00:26:21 +11001192 if (options.forward_agent) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001193 error("Agent forwarding is disabled to avoid "
1194 "man-in-the-middle attacks.");
Damien Miller95def091999-11-25 00:26:21 +11001195 options.forward_agent = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001196 cancelled_forwarding = 1;
Damien Miller95def091999-11-25 00:26:21 +11001197 }
Ben Lindstromc72745a2000-12-02 19:03:54 +00001198 if (options.forward_x11) {
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001199 error("X11 forwarding is disabled to avoid "
1200 "man-in-the-middle attacks.");
Ben Lindstromc72745a2000-12-02 19:03:54 +00001201 options.forward_x11 = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001202 cancelled_forwarding = 1;
Ben Lindstromc72745a2000-12-02 19:03:54 +00001203 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001204 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 Miller9f0f5c62001-12-21 14:45:46 +11001209 options.num_remote_forwards = 0;
Darren Tucker068e01f2008-07-02 22:33:55 +10001210 cancelled_forwarding = 1;
Ben Lindstromc72745a2000-12-02 19:03:54 +00001211 }
Damien Miller437edb92006-08-05 09:11:13 +10001212 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 Tucker068e01f2008-07-02 22:33:55 +10001216 cancelled_forwarding = 1;
Damien Miller437edb92006-08-05 09:11:13 +10001217 }
Darren Tucker068e01f2008-07-02 22:33:55 +10001218 if (options.exit_on_forward_failure && cancelled_forwarding)
1219 fatal("Error: forwarding disabled due to host key "
1220 "check failure");
1221
Damien Miller5428f641999-11-25 11:54:57 +11001222 /*
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 Tuckere2b36742010-01-13 22:42:34 +11001226 * by that sentence, and ask the user if he/she wishes to
Damien Miller5428f641999-11-25 11:54:57 +11001227 * accept the authentication.
1228 */
Damien Miller95def091999-11-25 00:26:21 +11001229 break;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001230 case HOST_FOUND:
1231 fatal("internal error");
1232 break;
Damien Miller95def091999-11-25 00:26:21 +11001233 }
Ben Lindstromc72745a2000-12-02 19:03:54 +00001234
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001235 if (options.check_host_ip && host_status != HOST_CHANGED &&
1236 ip_status == HOST_CHANGED) {
Damien Miller49d795c2002-01-22 23:34:12 +11001237 snprintf(msg, sizeof(msg),
1238 "Warning: the %s host key for '%.200s' "
1239 "differs from the key for the IP address '%.128s'"
Damien Millerd925dcd2010-12-01 12:21:51 +11001240 "\nOffending key for IP in %s:%lu",
1241 type, host, ip, ip_found->file, ip_found->line);
Damien Miller49d795c2002-01-22 23:34:12 +11001242 if (host_status == HOST_OK) {
1243 len = strlen(msg);
1244 snprintf(msg + len, sizeof(msg) - len,
Damien Millerd925dcd2010-12-01 12:21:51 +11001245 "\nMatching host key in %s:%lu",
1246 host_found->file, host_found->line);
Damien Miller49d795c2002-01-22 23:34:12 +11001247 }
djm@openbsd.org22376d22017-09-03 23:33:13 +00001248 if (options.strict_host_key_checking ==
1249 SSH_STRICT_HOSTKEY_ASK) {
Damien Miller49d795c2002-01-22 23:34:12 +11001250 strlcat(msg, "\nAre you sure you want "
1251 "to continue connecting (yes/no)? ", sizeof(msg));
1252 if (!confirm(msg))
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001253 goto fail;
djm@openbsd.org22376d22017-09-03 23:33:13 +00001254 } 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 Miller49d795c2002-01-22 23:34:12 +11001259 } else {
Damien Millerab2db412003-06-02 19:09:13 +10001260 logit("%s", msg);
Ben Lindstrom5c1fbab2001-01-03 03:51:15 +00001261 }
1262 }
1263
djm@openbsd.org8d4f8722015-01-26 03:04:45 +00001264 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 Tuckera627d422013-06-02 07:31:17 +10001270 free(ip);
1271 free(host);
Damien Millerd925dcd2010-12-01 12:21:51 +11001272 if (host_hostkeys != NULL)
1273 free_hostkeys(host_hostkeys);
1274 if (ip_hostkeys != NULL)
1275 free_hostkeys(ip_hostkeys);
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001276 return 0;
1277
1278fail:
Damien Miller1aed65e2010-03-04 21:53:35 +11001279 if (want_cert && host_status != HOST_REVOKED) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001280 /*
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.orgbeb965b2017-05-30 14:13:40 +00001285 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 Miller0a80ca12010-02-27 07:55:05 +11001290 host_key = raw_key;
1291 goto retry;
1292 }
dtucker@openbsd.orgf1f047f2018-02-07 22:52:45 +00001293 sshkey_free(raw_key);
Darren Tuckera627d422013-06-02 07:31:17 +10001294 free(ip);
1295 free(host);
Damien Millerd925dcd2010-12-01 12:21:51 +11001296 if (host_hostkeys != NULL)
1297 free_hostkeys(host_hostkeys);
1298 if (ip_hostkeys != NULL)
1299 free_hostkeys(ip_hostkeys);
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001300 return -1;
1301}
1302
Damien Miller37876e92003-05-15 10:19:46 +10001303/* returns 0 if key verifies or -1 if key does NOT verify */
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001304int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001305verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key)
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001306{
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001307 u_int i;
Damien Miller6b37fbb2014-07-04 08:59:24 +10001308 int r = -1, flags = 0;
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001309 char valid[64], *fp = NULL, *cafp = NULL;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001310 struct sshkey *plain = NULL;
Damien Millerd925dcd2010-12-01 12:21:51 +11001311
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001312 if ((fp = sshkey_fingerprint(host_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001313 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001314 error("%s: fingerprint host key: %s", __func__, ssh_err(r));
1315 r = -1;
1316 goto out;
1317 }
Ben Lindstromd6481ea2001-06-25 04:37:41 +00001318
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001319 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.org8ca915f2015-11-20 01:45:29 +00001332 (unsigned long long)host_key->cert->serial,
1333 host_key->cert->key_id,
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001334 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.org97f4d302017-04-30 23:13:25 +00001341 debug("Server host key: %s %s", sshkey_ssh_name(host_key), fp);
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001342 }
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001343
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 Miller6b37fbb2014-07-04 08:59:24 +10001370 }
1371
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001372 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.org5e39a492014-12-04 02:24:32 +00001377 if ((r = sshkey_from_private(host_key, &plain)) != 0)
1378 goto out;
1379 if (sshkey_is_cert(plain))
1380 sshkey_drop_cert(plain);
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001381 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 Miller6b37fbb2014-07-04 08:59:24 +10001386 r = 0;
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001387 goto out;
Damien Miller7d6a9fb2014-04-20 13:23:43 +10001388 }
1389 if (flags & DNS_VERIFY_MATCH) {
1390 matching_host_key_dns = 1;
1391 } else {
djm@openbsd.orgaea59a02017-09-14 04:32:21 +00001392 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 Miller7d6a9fb2014-04-20 13:23:43 +10001396 }
Damien Miller150b5572003-11-17 21:19:29 +11001397 }
Damien Miller37876e92003-05-15 10:19:46 +10001398 }
1399 }
Damien Miller6b37fbb2014-07-04 08:59:24 +10001400 r = check_host_key(host, hostaddr, options.port, host_key, RDRW,
Damien Miller295ee632011-05-29 21:42:31 +10001401 options.user_hostfiles, options.num_user_hostfiles,
1402 options.system_hostfiles, options.num_system_hostfiles);
Damien Miller6b37fbb2014-07-04 08:59:24 +10001403
djm@openbsd.org5e39a492014-12-04 02:24:32 +00001404out:
1405 sshkey_free(plain);
1406 free(fp);
djm@openbsd.orgf96516d2015-11-19 01:09:38 +00001407 free(cafp);
Damien Miller6b37fbb2014-07-04 08:59:24 +10001408 if (r == 0 && host_key != NULL) {
markus@openbsd.orgbeb965b2017-05-30 14:13:40 +00001409 sshkey_free(previous_host_key);
1410 r = sshkey_from_private(host_key, &previous_host_key);
Damien Miller6b37fbb2014-07-04 08:59:24 +10001411 }
1412
1413 return r;
Damien Millera34a28b1999-12-14 10:47:15 +11001414}
Damien Miller037a0dc1999-12-07 15:38:31 +11001415
Damien Miller396691a2000-01-20 22:44:08 +11001416/*
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 */
1423void
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001424ssh_login(Sensitive *sensitive, const char *orighost,
Damien Millerd925dcd2010-12-01 12:21:51 +11001425 struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms)
Damien Miller396691a2000-01-20 22:44:08 +11001426{
Damien Miller0faf7472013-10-17 11:47:23 +11001427 char *host;
Damien Millereba71ba2000-04-29 23:57:08 +10001428 char *server_user, *local_user;
1429
Damien Millereba71ba2000-04-29 23:57:08 +10001430 local_user = xstrdup(pw->pw_name);
1431 server_user = options.user ? options.user : local_user;
Damien Miller396691a2000-01-20 22:44:08 +11001432
1433 /* Convert the user-supplied hostname into all lowercase. */
1434 host = xstrdup(orighost);
Damien Miller0faf7472013-10-17 11:47:23 +11001435 lowercase(host);
Damien Miller396691a2000-01-20 22:44:08 +11001436
1437 /* Exchange protocol version identification strings with the server. */
Damien Miller67bd0622007-09-17 12:06:57 +10001438 ssh_exchange_identification(timeout_ms);
Damien Miller396691a2000-01-20 22:44:08 +11001439
1440 /* Put the connection into non-blocking mode. */
1441 packet_set_nonblocking();
1442
Damien Miller396691a2000-01-20 22:44:08 +11001443 /* key exchange */
Damien Miller396691a2000-01-20 22:44:08 +11001444 /* authenticate user */
dtucker@openbsd.orgbb2289e2015-04-14 04:17:03 +00001445 debug("Authenticating to %s:%d as '%s'", host, port, server_user);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001446 ssh_kex2(host, hostaddr, port);
1447 ssh_userauth2(local_user, server_user, host, sensitive);
Darren Tuckera627d422013-06-02 07:31:17 +10001448 free(local_user);
Damien Miller396691a2000-01-20 22:44:08 +11001449}
Damien Miller79438cc2001-02-16 12:34:57 +11001450
1451void
1452ssh_put_password(char *password)
1453{
1454 int size;
1455 char *padded;
1456
Ben Lindstromd20d0f32001-03-10 17:22:20 +00001457 if (datafellows & SSH_BUG_PASSWORDPAD) {
Ben Lindstrom664408d2001-06-09 01:42:01 +00001458 packet_put_cstring(password);
Ben Lindstromd20d0f32001-03-10 17:22:20 +00001459 return;
1460 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001461 size = ROUNDUP(strlen(password) + 1, 32);
Damien Miller07d86be2006-03-26 14:19:21 +11001462 padded = xcalloc(1, size);
Damien Miller79438cc2001-02-16 12:34:57 +11001463 strlcpy(padded, password, size);
1464 packet_put_string(padded, size);
Damien Millera5103f42014-02-04 11:20:14 +11001465 explicit_bzero(padded, size);
Darren Tuckera627d422013-06-02 07:31:17 +10001466 free(padded);
Damien Miller79438cc2001-02-16 12:34:57 +11001467}
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001468
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001469/* print all known host keys for a given host, but skip keys of given type */
1470static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001471show_other_keys(struct hostkeys *hostkeys, struct sshkey *key)
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001472{
Damien Miller106bf1c2013-12-29 17:54:03 +11001473 int type[] = {
Damien Miller106bf1c2013-12-29 17:54:03 +11001474 KEY_RSA,
1475 KEY_DSA,
1476 KEY_ECDSA,
1477 KEY_ED25519,
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001478 KEY_XMSS,
Damien Miller106bf1c2013-12-29 17:54:03 +11001479 -1
1480 };
Damien Millerd925dcd2010-12-01 12:21:51 +11001481 int i, ret = 0;
1482 char *fp, *ra;
1483 const struct hostkey_entry *found;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001484
1485 for (i = 0; type[i] != -1; i++) {
1486 if (type[i] == key->type)
1487 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +11001488 if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found))
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001489 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001490 fp = sshkey_fingerprint(found->key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001491 options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001492 ra = sshkey_fingerprint(found->key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001493 options.fingerprint_hash, SSH_FP_RANDOMART);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001494 if (fp == NULL || ra == NULL)
1495 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Millerd925dcd2010-12-01 12:21:51 +11001496 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 Tuckera627d422013-06-02 07:31:17 +10001504 free(ra);
1505 free(fp);
Damien Millerd925dcd2010-12-01 12:21:51 +11001506 ret = 1;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001507 }
Damien Millerd925dcd2010-12-01 12:21:51 +11001508 return ret;
Ben Lindstrom3ed66402002-08-01 01:21:56 +00001509}
Damien Miller5a388972003-11-17 21:10:47 +11001510
1511static void
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001512warn_changed_key(struct sshkey *host_key)
Damien Miller5a388972003-11-17 21:10:47 +11001513{
1514 char *fp;
Damien Miller5a388972003-11-17 21:10:47 +11001515
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001516 fp = sshkey_fingerprint(host_key, options.fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001517 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001518 if (fp == NULL)
1519 fatal("%s: sshkey_fingerprint fail", __func__);
Damien Miller5a388972003-11-17 21:10:47 +11001520
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 Miller05c89972011-01-06 22:42:04 +11001526 error("It is also possible that a host key has just been changed.");
Damien Miller5a388972003-11-17 21:10:47 +11001527 error("The fingerprint for the %s key sent by the remote host is\n%s.",
djm@openbsd.orgaea59a02017-09-14 04:32:21 +00001528 key_type(host_key), fp);
Damien Miller5a388972003-11-17 21:10:47 +11001529 error("Please contact your system administrator.");
1530
Darren Tuckera627d422013-06-02 07:31:17 +10001531 free(fp);
Damien Miller5a388972003-11-17 21:10:47 +11001532}
Damien Millerd27b9472005-12-13 19:29:02 +11001533
1534/*
1535 * Execute a local command
1536 */
1537int
1538ssh_local_cmd(const char *args)
1539{
1540 char *shell;
1541 pid_t pid;
1542 int status;
Damien Millered3a8eb2011-01-07 10:02:52 +11001543 void (*osighand)(int);
Damien Millerd27b9472005-12-13 19:29:02 +11001544
1545 if (!options.permit_local_command ||
1546 args == NULL || !*args)
1547 return (1);
1548
Damien Miller38d9a962010-10-07 22:07:11 +11001549 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
Damien Millerd27b9472005-12-13 19:29:02 +11001550 shell = _PATH_BSHELL;
1551
Damien Millered3a8eb2011-01-07 10:02:52 +11001552 osighand = signal(SIGCHLD, SIG_DFL);
Damien Millerd27b9472005-12-13 19:29:02 +11001553 pid = fork();
1554 if (pid == 0) {
Damien Miller6fb6fd52011-01-16 23:17:45 +11001555 signal(SIGPIPE, SIG_DFL);
Damien Millerd27b9472005-12-13 19:29:02 +11001556 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 Millered3a8eb2011-01-07 10:02:52 +11001566 signal(SIGCHLD, osighand);
Damien Millerd27b9472005-12-13 19:29:02 +11001567
1568 if (!WIFEXITED(status))
1569 return (1);
1570
1571 return (WEXITSTATUS(status));
1572}
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001573
1574void
djm@openbsd.orgd9e5cf02018-02-10 09:25:34 +00001575maybe_add_key_to_agent(char *authfile, const struct sshkey *private,
1576 char *comment, char *passphrase)
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001577{
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.org566b3a42017-03-10 03:22:40 +00001591 close(auth_sock);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001592 return;
1593 }
1594
1595 if ((r = ssh_add_identity_constrained(auth_sock, private, comment, 0,
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001596 (options.add_keys_to_agent == 3), 0)) == 0)
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001597 debug("identity added to agent: %s", authfile);
1598 else
1599 debug("could not add identity to agent: %s (%d)", authfile, r);
dtucker@openbsd.org566b3a42017-03-10 03:22:40 +00001600 close(auth_sock);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001601}