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