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