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