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