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