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