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