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