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