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