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