diff options
Diffstat (limited to 'crypto/openssh/sshconnect.c')
-rw-r--r-- | crypto/openssh/sshconnect.c | 882 |
1 files changed, 504 insertions, 378 deletions
diff --git a/crypto/openssh/sshconnect.c b/crypto/openssh/sshconnect.c index b50029de71b8..8f7541942ac1 100644 --- a/crypto/openssh/sshconnect.c +++ b/crypto/openssh/sshconnect.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sshconnect.c,v 1.305 2018/09/20 03:30:44 djm Exp $ */ +/* $OpenBSD: sshconnect.c,v 1.355 2021/07/02 05:11:21 dtucker Exp $ */ /* * Author: Tatu Ylonen <ylo@cs.hut.fi> * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland @@ -31,6 +31,7 @@ __RCSID("$FreeBSD$"); #include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <limits.h> #include <netdb.h> #ifdef HAVE_PATHS_H #include <paths.h> @@ -40,9 +41,9 @@ __RCSID("$FreeBSD$"); #include <poll.h> #endif #include <signal.h> -#include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <unistd.h> #ifdef HAVE_IFADDRS_H @@ -57,7 +58,6 @@ __RCSID("$FreeBSD$"); #include "compat.h" #include "sshkey.h" #include "sshconnect.h" -#include "hostfile.h" #include "log.h" #include "misc.h" #include "readconf.h" @@ -69,9 +69,8 @@ __RCSID("$FreeBSD$"); #include "authfile.h" #include "ssherr.h" #include "authfd.h" +#include "kex.h" -char *client_version_string = NULL; -char *server_version_string = NULL; struct sshkey *previous_host_key = NULL; static int matching_host_key_dns = 0; @@ -79,6 +78,7 @@ static int matching_host_key_dns = 0; static pid_t proxy_command_pid = 0; /* import */ +extern int debug_flag; extern Options options; extern char *__progname; @@ -88,14 +88,21 @@ static void warn_changed_key(struct sshkey *); /* Expand a proxy command */ static char * expand_proxy_command(const char *proxy_command, const char *user, - const char *host, int port) + const char *host, const char *host_arg, int port) { char *tmp, *ret, strport[NI_MAXSERV]; + const char *keyalias = options.host_key_alias ? + options.host_key_alias : host_arg; snprintf(strport, sizeof strport, "%d", port); xasprintf(&tmp, "exec %s", proxy_command); - ret = percent_expand(tmp, "h", host, "p", strport, - "r", options.user, (char *)NULL); + ret = percent_expand(tmp, + "h", host, + "k", keyalias, + "n", host_arg, + "p", strport, + "r", options.user, + (char *)NULL); free(tmp); return ret; } @@ -105,8 +112,8 @@ expand_proxy_command(const char *proxy_command, const char *user, * a connected fd back to us. */ static int -ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, - const char *proxy_command) +ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, + const char *host_arg, u_short port, const char *proxy_command) { char *command_string; int sp[2], sock; @@ -116,12 +123,12 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, if ((shell = getenv("SHELL")) == NULL) shell = _PATH_BSHELL; - if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) == -1) fatal("Could not create socketpair to communicate with " "proxy dialer: %.100s", strerror(errno)); command_string = expand_proxy_command(proxy_command, options.user, - host, port); + host, host_arg, port); debug("Executing proxy dialer command: %.500s", command_string); /* Fork and execute the proxy command. */ @@ -131,20 +138,24 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, close(sp[1]); /* Redirect stdin and stdout. */ if (sp[0] != 0) { - if (dup2(sp[0], 0) < 0) + if (dup2(sp[0], 0) == -1) perror("dup2 stdin"); } if (sp[0] != 1) { - if (dup2(sp[0], 1) < 0) + if (dup2(sp[0], 1) == -1) perror("dup2 stdout"); } if (sp[0] >= 2) close(sp[0]); /* - * Stderr is left as it is so that error messages get - * printed on the user's terminal. + * Stderr is left for non-ControlPersist connections is so + * error messages may be printed on the user's terminal. */ + if (!debug_flag && options.control_path != NULL && + options.control_persist && stdfd_devnull(0, 0, 1) == -1) + error_f("stdfd_devnull failed"); + argv[0] = shell; argv[1] = "-c"; argv[2] = command_string; @@ -159,7 +170,7 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, exit(1); } /* Parent. */ - if (pid < 0) + if (pid == -1) fatal("fork failed: %.100s", strerror(errno)); close(sp[0]); free(command_string); @@ -183,8 +194,8 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, * Connect to the given ssh server using a proxy command. */ static int -ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port, - const char *proxy_command) +ssh_proxy_connect(struct ssh *ssh, const char *host, const char *host_arg, + u_short port, const char *proxy_command) { char *command_string; int pin[2], pout[2]; @@ -195,12 +206,12 @@ ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port, shell = _PATH_BSHELL; /* Create pipes for communicating with the proxy. */ - if (pipe(pin) < 0 || pipe(pout) < 0) + if (pipe(pin) == -1 || pipe(pout) == -1) fatal("Could not create pipes to communicate with the proxy: %.100s", strerror(errno)); command_string = expand_proxy_command(proxy_command, options.user, - host, port); + host, host_arg, port); debug("Executing proxy command: %.500s", command_string); /* Fork and execute the proxy command. */ @@ -210,32 +221,40 @@ ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port, /* Redirect stdin and stdout. */ close(pin[1]); if (pin[0] != 0) { - if (dup2(pin[0], 0) < 0) + if (dup2(pin[0], 0) == -1) perror("dup2 stdin"); close(pin[0]); } close(pout[0]); - if (dup2(pout[1], 1) < 0) + if (dup2(pout[1], 1) == -1) perror("dup2 stdout"); /* Cannot be 1 because pin allocated two descriptors. */ close(pout[1]); - /* Stderr is left as it is so that error messages get - printed on the user's terminal. */ + /* + * Stderr is left for non-ControlPersist connections is so + * error messages may be printed on the user's terminal. + */ + if (!debug_flag && options.control_path != NULL && + options.control_persist && stdfd_devnull(0, 0, 1) == -1) + error_f("stdfd_devnull failed"); + argv[0] = shell; argv[1] = "-c"; argv[2] = command_string; argv[3] = NULL; - /* Execute the proxy command. Note that we gave up any - extra privileges above. */ - signal(SIGPIPE, SIG_DFL); + /* + * Execute the proxy command. Note that we gave up any + * extra privileges above. + */ + ssh_signal(SIGPIPE, SIG_DFL); execv(argv[0], argv); perror(argv[0]); exit(1); } /* Parent. */ - if (pid < 0) + if (pid == -1) fatal("fork failed: %.100s", strerror(errno)); else proxy_command_pid = pid; /* save pid to clean up later */ @@ -299,8 +318,7 @@ check_ifaddrs(const char *ifname, int af, const struct ifaddrs *ifaddrs, htonl(INADDR_LOOPBACK)) continue; if (*rlenp < sizeof(struct sockaddr_in)) { - error("%s: v4 addr doesn't fit", - __func__); + error_f("v4 addr doesn't fit"); return -1; } *rlenp = sizeof(struct sockaddr_in); @@ -314,8 +332,7 @@ check_ifaddrs(const char *ifname, int af, const struct ifaddrs *ifaddrs, IN6_IS_ADDR_LOOPBACK(v6addr))) continue; if (*rlenp < sizeof(struct sockaddr_in6)) { - error("%s: v6 addr doesn't fit", - __func__); + error_f("v6 addr doesn't fit"); return -1; } *rlenp = sizeof(struct sockaddr_in6); @@ -344,12 +361,16 @@ ssh_create_socket(struct addrinfo *ai) char ntop[NI_MAXHOST]; sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (sock < 0) { + if (sock == -1) { error("socket: %s", strerror(errno)); return -1; } fcntl(sock, F_SETFD, FD_CLOEXEC); + /* Use interactive QOS (if specified) until authentication completed */ + if (options.ip_qos_interactive != INT_MAX) + set_sock_tos(sock, options.ip_qos_interactive); + /* Bind the socket to an alternative local IP address */ if (options.bind_address == NULL && options.bind_interface == NULL) return sock; @@ -370,24 +391,20 @@ ssh_create_socket(struct addrinfo *ai) error("getaddrinfo: no addrs"); goto fail; } - if (res->ai_addrlen > sizeof(bindaddr)) { - error("%s: addr doesn't fit", __func__); - goto fail; - } memcpy(&bindaddr, res->ai_addr, res->ai_addrlen); bindaddrlen = res->ai_addrlen; } else if (options.bind_interface != NULL) { #ifdef HAVE_IFADDRS_H if ((r = getifaddrs(&ifaddrs)) != 0) { error("getifaddrs: %s: %s", options.bind_interface, - strerror(errno)); + strerror(errno)); goto fail; } bindaddrlen = sizeof(bindaddr); if (check_ifaddrs(options.bind_interface, ai->ai_family, ifaddrs, &bindaddr, &bindaddrlen) != 0) { logit("getifaddrs: %s: no suitable addresses", - options.bind_interface); + options.bind_interface); goto fail; } #else @@ -396,15 +413,14 @@ ssh_create_socket(struct addrinfo *ai) } if ((r = getnameinfo((struct sockaddr *)&bindaddr, bindaddrlen, ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST)) != 0) { - error("%s: getnameinfo failed: %s", __func__, - ssh_gai_strerror(r)); + error_f("getnameinfo failed: %s", ssh_gai_strerror(r)); goto fail; } if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) { error("bind %s: %s", ntop, strerror(errno)); goto fail; } - debug("%s: bound to %s", __func__, ntop); + debug_f("bound to %s", ntop); /* success */ goto out; fail: @@ -421,73 +437,6 @@ fail: } /* - * Wait up to *timeoutp milliseconds for fd to be readable. Updates - * *timeoutp with time remaining. - * Returns 0 if fd ready or -1 on timeout or error (see errno). - */ -static int -waitrfd(int fd, int *timeoutp) -{ - struct pollfd pfd; - struct timeval t_start; - int oerrno, r; - - monotime_tv(&t_start); - pfd.fd = fd; - pfd.events = POLLIN; - for (; *timeoutp >= 0;) { - r = poll(&pfd, 1, *timeoutp); - oerrno = errno; - ms_subtract_diff(&t_start, timeoutp); - errno = oerrno; - if (r > 0) - return 0; - else if (r == -1 && errno != EAGAIN) - return -1; - else if (r == 0) - break; - } - /* timeout */ - errno = ETIMEDOUT; - return -1; -} - -static int -timeout_connect(int sockfd, const struct sockaddr *serv_addr, - socklen_t addrlen, int *timeoutp) -{ - int optval = 0; - socklen_t optlen = sizeof(optval); - - /* No timeout: just do a blocking connect() */ - if (*timeoutp <= 0) - return connect(sockfd, serv_addr, addrlen); - - set_nonblock(sockfd); - if (connect(sockfd, serv_addr, addrlen) == 0) { - /* Succeeded already? */ - unset_nonblock(sockfd); - return 0; - } else if (errno != EINPROGRESS) - return -1; - - if (waitrfd(sockfd, timeoutp) == -1) - return -1; - - /* Completed or failed */ - if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1) { - debug("getsockopt: %s", strerror(errno)); - return -1; - } - if (optval != 0) { - errno = optval; - return -1; - } - unset_nonblock(sockfd); - return 0; -} - -/* * Opens a TCP/IP connection to the remote server on the given host. * The address of the remote host will be returned in hostaddr. * If port is 0, the default port will be used. @@ -498,15 +447,15 @@ timeout_connect(int sockfd, const struct sockaddr *serv_addr, */ static int ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, - struct sockaddr_storage *hostaddr, u_short port, int family, - int connection_attempts, int *timeout_ms, int want_keepalive) + struct sockaddr_storage *hostaddr, u_short port, int connection_attempts, + int *timeout_ms, int want_keepalive) { - int on = 1; + int on = 1, saved_timeout_ms = *timeout_ms; int oerrno, sock = -1, attempt; char ntop[NI_MAXHOST], strport[NI_MAXSERV]; struct addrinfo *ai; - debug2("%s", __func__); + debug3_f("entering"); memset(ntop, 0, sizeof(ntop)); memset(strport, 0, sizeof(strport)); @@ -530,7 +479,7 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, ntop, sizeof(ntop), strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { oerrno = errno; - error("%s: getnameinfo failed", __func__); + error_f("getnameinfo failed"); errno = oerrno; continue; } @@ -545,6 +494,7 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, continue; } + *timeout_ms = saved_timeout_ms; if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen, timeout_ms) >= 0) { /* Successful connection. */ @@ -575,194 +525,74 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, /* Set SO_KEEPALIVE if requested. */ if (want_keepalive && setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, - sizeof(on)) < 0) + sizeof(on)) == -1) error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno)); /* Set the connection. */ if (ssh_packet_set_connection(ssh, sock, sock) == NULL) return -1; /* ssh_packet_set_connection logs error */ - return 0; + return 0; } int -ssh_connect(struct ssh *ssh, const char *host, struct addrinfo *addrs, - struct sockaddr_storage *hostaddr, u_short port, int family, +ssh_connect(struct ssh *ssh, const char *host, const char *host_arg, + struct addrinfo *addrs, struct sockaddr_storage *hostaddr, u_short port, int connection_attempts, int *timeout_ms, int want_keepalive) { + int in, out; + if (options.proxy_command == NULL) { return ssh_connect_direct(ssh, host, addrs, hostaddr, port, - family, connection_attempts, timeout_ms, want_keepalive); + connection_attempts, timeout_ms, want_keepalive); } else if (strcmp(options.proxy_command, "-") == 0) { - if ((ssh_packet_set_connection(ssh, - STDIN_FILENO, STDOUT_FILENO)) == NULL) + if ((in = dup(STDIN_FILENO)) == -1 || + (out = dup(STDOUT_FILENO)) == -1) { + if (in >= 0) + close(in); + error_f("dup() in/out failed"); + return -1; /* ssh_packet_set_connection logs error */ + } + if ((ssh_packet_set_connection(ssh, in, out)) == NULL) return -1; /* ssh_packet_set_connection logs error */ return 0; } else if (options.proxy_use_fdpass) { - return ssh_proxy_fdpass_connect(ssh, host, port, + return ssh_proxy_fdpass_connect(ssh, host, host_arg, port, options.proxy_command); } - return ssh_proxy_connect(ssh, host, port, options.proxy_command); -} - -static void -send_client_banner(int connection_out, int minor1) -{ - /* Send our own protocol version identification. */ - xasprintf(&client_version_string, "SSH-%d.%d-%.100s%s%s\n", - PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION, - *options.version_addendum == '\0' ? "" : " ", - options.version_addendum); - if (atomicio(vwrite, connection_out, client_version_string, - strlen(client_version_string)) != strlen(client_version_string)) - fatal("write: %.100s", strerror(errno)); - chop(client_version_string); - debug("Local version string %.100s", client_version_string); -} - -/* - * Waits for the server identification string, and sends our own - * identification string. - */ -void -ssh_exchange_identification(int timeout_ms) -{ - char buf[256], remote_version[256]; /* must be same size! */ - int remote_major, remote_minor, mismatch; - int connection_in = packet_get_connection_in(); - int connection_out = packet_get_connection_out(); - u_int i, n; - size_t len; - int rc; - - send_client_banner(connection_out, 0); - - /* Read other side's version identification. */ - for (n = 0;;) { - for (i = 0; i < sizeof(buf) - 1; i++) { - if (timeout_ms > 0) { - rc = waitrfd(connection_in, &timeout_ms); - if (rc == -1 && errno == ETIMEDOUT) { - fatal("Connection timed out during " - "banner exchange"); - } else if (rc == -1) { - fatal("%s: %s", - __func__, strerror(errno)); - } - } - - len = atomicio(read, connection_in, &buf[i], 1); - if (len != 1 && errno == EPIPE) - fatal("ssh_exchange_identification: " - "Connection closed by remote host"); - else if (len != 1) - fatal("ssh_exchange_identification: " - "read: %.100s", strerror(errno)); - if (buf[i] == '\r') { - buf[i] = '\n'; - buf[i + 1] = 0; - continue; /**XXX wait for \n */ - } - if (buf[i] == '\n') { - buf[i + 1] = 0; - break; - } - if (++n > 65536) - fatal("ssh_exchange_identification: " - "No banner received"); - } - buf[sizeof(buf) - 1] = 0; - if (strncmp(buf, "SSH-", 4) == 0) - break; - debug("ssh_exchange_identification: %s", buf); - } - server_version_string = xstrdup(buf); - - /* - * Check that the versions match. In future this might accept - * several versions and set appropriate flags to handle them. - */ - if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n", - &remote_major, &remote_minor, remote_version) != 3) - fatal("Bad remote protocol version identification: '%.100s'", buf); - debug("Remote protocol version %d.%d, remote software version %.100s", - remote_major, remote_minor, remote_version); - - active_state->compat = compat_datafellows(remote_version); - mismatch = 0; - - switch (remote_major) { - case 2: - break; - case 1: - if (remote_minor != 99) - mismatch = 1; - break; - default: - mismatch = 1; - break; - } - if (mismatch) - fatal("Protocol major versions differ: %d vs. %d", - PROTOCOL_MAJOR_2, remote_major); - if ((datafellows & SSH_BUG_RSASIGMD5) != 0) - logit("Server version \"%.100s\" uses unsafe RSA signature " - "scheme; disabling use of RSA keys", remote_version); - chop(server_version_string); + return ssh_proxy_connect(ssh, host, host_arg, port, + options.proxy_command); } /* defaults to 'no' */ static int -confirm(const char *prompt) +confirm(const char *prompt, const char *fingerprint) { const char *msg, *again = "Please type 'yes' or 'no': "; - char *p; + const char *again_fp = "Please type 'yes', 'no' or the fingerprint: "; + char *p, *cp; int ret = -1; if (options.batch_mode) return 0; - for (msg = prompt;;msg = again) { - p = read_passphrase(msg, RP_ECHO); + for (msg = prompt;;msg = fingerprint ? again_fp : again) { + cp = p = read_passphrase(msg, RP_ECHO); if (p == NULL) return 0; - p[strcspn(p, "\n")] = '\0'; + p += strspn(p, " \t"); /* skip leading whitespace */ + p[strcspn(p, " \t\n")] = '\0'; /* remove trailing whitespace */ if (p[0] == '\0' || strcasecmp(p, "no") == 0) ret = 0; - else if (strcasecmp(p, "yes") == 0) + else if (strcasecmp(p, "yes") == 0 || (fingerprint != NULL && + strcmp(p, fingerprint) == 0)) ret = 1; - free(p); + free(cp); if (ret != -1) return ret; } } static int -check_host_cert(const char *host, const struct sshkey *key) -{ - const char *reason; - int r; - - if (sshkey_cert_check_authority(key, 1, 0, host, &reason) != 0) { - error("%s", reason); - return 0; - } - if (sshbuf_len(key->cert->critical) != 0) { - error("Certificate for %s contains unsupported " - "critical options(s)", host); - return 0; - } - if ((r = sshkey_check_cert_sigtype(key, - options.ca_sign_algorithms)) != 0) { - logit("%s: certificate signature algorithm %s: %s", __func__, - (key->cert == NULL || key->cert->signature_type == NULL) ? - "(null)" : key->cert->signature_type, ssh_err(r)); - return 0; - } - - return 1; -} - -static int sockaddr_is_local(struct sockaddr *hostaddr) { switch (hostaddr->sa_family) { @@ -811,7 +641,7 @@ get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr, if (options.proxy_command == NULL) { if (getnameinfo(hostaddr, addrlen, ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST) != 0) - fatal("%s: getnameinfo failed", __func__); + fatal_f("getnameinfo failed"); *hostfile_ipaddr = put_host_port(ntop, port); } else { *hostfile_ipaddr = xstrdup("<no hostip for proxy " @@ -835,6 +665,257 @@ get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr, } } +/* returns non-zero if path appears in hostfiles, or 0 if not. */ +static int +path_in_hostfiles(const char *path, char **hostfiles, u_int num_hostfiles) +{ + u_int i; + + for (i = 0; i < num_hostfiles; i++) { + if (strcmp(path, hostfiles[i]) == 0) + return 1; + } + return 0; +} + +struct find_by_key_ctx { + const char *host, *ip; + const struct sshkey *key; + char **names; + u_int nnames; +}; + +/* Try to replace home directory prefix (per $HOME) with a ~/ sequence */ +static char * +try_tilde_unexpand(const char *path) +{ + char *home, *ret = NULL; + size_t l; + + if (*path != '/') + return xstrdup(path); + if ((home = getenv("HOME")) == NULL || (l = strlen(home)) == 0) + return xstrdup(path); + if (strncmp(path, home, l) != 0) + return xstrdup(path); + /* + * ensure we have matched on a path boundary: either the $HOME that + * we just compared ends with a '/' or the next character of the path + * must be a '/'. + */ + if (home[l - 1] != '/' && path[l] != '/') + return xstrdup(path); + if (path[l] == '/') + l++; + xasprintf(&ret, "~/%s", path + l); + return ret; +} + +static int +hostkeys_find_by_key_cb(struct hostkey_foreach_line *l, void *_ctx) +{ + struct find_by_key_ctx *ctx = (struct find_by_key_ctx *)_ctx; + char *path; + + /* we are looking for keys with names that *do not* match */ + if ((l->match & HKF_MATCH_HOST) != 0) + return 0; + /* not interested in marker lines */ + if (l->marker != MRK_NONE) + return 0; + /* we are only interested in exact key matches */ + if (l->key == NULL || !sshkey_equal(ctx->key, l->key)) + return 0; + path = try_tilde_unexpand(l->path); + debug_f("found matching key in %s:%lu", path, l->linenum); + ctx->names = xrecallocarray(ctx->names, + ctx->nnames, ctx->nnames + 1, sizeof(*ctx->names)); + xasprintf(&ctx->names[ctx->nnames], "%s:%lu: %s", path, l->linenum, + strncmp(l->hosts, HASH_MAGIC, strlen(HASH_MAGIC)) == 0 ? + "[hashed name]" : l->hosts); + ctx->nnames++; + free(path); + return 0; +} + +static int +hostkeys_find_by_key_hostfile(const char *file, const char *which, + struct find_by_key_ctx *ctx) +{ + int r; + + debug3_f("trying %s hostfile \"%s\"", which, file); + if ((r = hostkeys_foreach(file, hostkeys_find_by_key_cb, ctx, + ctx->host, ctx->ip, HKF_WANT_PARSE_KEY, 0)) != 0) { + if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) { + debug_f("hostkeys file %s does not exist", file); + return 0; + } + error_fr(r, "hostkeys_foreach failed for %s", file); + return r; + } + return 0; +} + +/* + * Find 'key' in known hosts file(s) that do not match host/ip. + * Used to display also-known-as information for previously-unseen hostkeys. + */ +static void +hostkeys_find_by_key(const char *host, const char *ip, const struct sshkey *key, + char **user_hostfiles, u_int num_user_hostfiles, + char **system_hostfiles, u_int num_system_hostfiles, + char ***names, u_int *nnames) +{ + struct find_by_key_ctx ctx = {0, 0, 0, 0, 0}; + u_int i; + + *names = NULL; + *nnames = 0; + + if (key == NULL || sshkey_is_cert(key)) + return; + + ctx.host = host; + ctx.ip = ip; + ctx.key = key; + + for (i = 0; i < num_user_hostfiles; i++) { + if (hostkeys_find_by_key_hostfile(user_hostfiles[i], + "user", &ctx) != 0) + goto fail; + } + for (i = 0; i < num_system_hostfiles; i++) { + if (hostkeys_find_by_key_hostfile(system_hostfiles[i], + "system", &ctx) != 0) + goto fail; + } + /* success */ + *names = ctx.names; + *nnames = ctx.nnames; + ctx.names = NULL; + ctx.nnames = 0; + return; + fail: + for (i = 0; i < ctx.nnames; i++) + free(ctx.names[i]); + free(ctx.names); +} + +#define MAX_OTHER_NAMES 8 /* Maximum number of names to list */ +static char * +other_hostkeys_message(const char *host, const char *ip, + const struct sshkey *key, + char **user_hostfiles, u_int num_user_hostfiles, + char **system_hostfiles, u_int num_system_hostfiles) +{ + char *ret = NULL, **othernames = NULL; + u_int i, n, num_othernames = 0; + + hostkeys_find_by_key(host, ip, key, + user_hostfiles, num_user_hostfiles, + system_hostfiles, num_system_hostfiles, + &othernames, &num_othernames); + if (num_othernames == 0) + return xstrdup("This key is not known by any other names"); + + xasprintf(&ret, "This host key is known by the following other " + "names/addresses:"); + + n = num_othernames; + if (n > MAX_OTHER_NAMES) + n = MAX_OTHER_NAMES; + for (i = 0; i < n; i++) { + xextendf(&ret, "\n", " %s", othernames[i]); + } + if (n < num_othernames) { + xextendf(&ret, "\n", " (%d additional names omitted)", + num_othernames - n); + } + for (i = 0; i < num_othernames; i++) + free(othernames[i]); + free(othernames); + return ret; +} + +void +load_hostkeys_command(struct hostkeys *hostkeys, const char *command_template, + const char *invocation, const struct ssh_conn_info *cinfo, + const struct sshkey *host_key, const char *hostfile_hostname) +{ + int r, i, ac = 0; + char *key_fp = NULL, *keytext = NULL, *tmp; + char *command = NULL, *tag = NULL, **av = NULL; + FILE *f = NULL; + pid_t pid; + void (*osigchld)(int); + + xasprintf(&tag, "KnownHostsCommand-%s", invocation); + + if (host_key != NULL) { + if ((key_fp = sshkey_fingerprint(host_key, + options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) + fatal_f("sshkey_fingerprint failed"); + if ((r = sshkey_to_base64(host_key, &keytext)) != 0) + fatal_fr(r, "sshkey_to_base64 failed"); + } + /* + * NB. all returns later this function should go via "out" to + * ensure the original SIGCHLD handler is restored properly. + */ + osigchld = ssh_signal(SIGCHLD, SIG_DFL); + + /* Turn the command into an argument vector */ + if (argv_split(command_template, &ac, &av, 0) != 0) { + error("%s \"%s\" contains invalid quotes", tag, + command_template); + goto out; + } + if (ac == 0) { + error("%s \"%s\" yielded no arguments", tag, + command_template); + goto out; + } + for (i = 1; i < ac; i++) { + tmp = percent_dollar_expand(av[i], + DEFAULT_CLIENT_PERCENT_EXPAND_ARGS(cinfo), + "H", hostfile_hostname, + "I", invocation, + "t", host_key == NULL ? "NONE" : sshkey_ssh_name(host_key), + "f", key_fp == NULL ? "NONE" : key_fp, + "K", keytext == NULL ? "NONE" : keytext, + (char *)NULL); + if (tmp == NULL) + fatal_f("percent_expand failed"); + free(av[i]); + av[i] = tmp; + } + /* Prepare a printable command for logs, etc. */ + command = argv_assemble(ac, av); + + if ((pid = subprocess(tag, command, ac, av, &f, + SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_UNSAFE_PATH| + SSH_SUBPROCESS_PRESERVE_ENV, NULL, NULL, NULL)) == 0) + goto out; + + load_hostkeys_file(hostkeys, hostfile_hostname, tag, f, 1); + + if (exited_cleanly(pid, tag, command, 0) != 0) + fatal("KnownHostsCommand failed"); + + out: + if (f != NULL) + fclose(f); + ssh_signal(SIGCHLD, osigchld); + for (i = 0; i < ac; i++) + free(av[i]); + free(av); + free(tag); + free(command); + free(key_fp); + free(keytext); +} + /* * check whether the supplied host key is valid, return -1 if the key * is not valid. user_hostfile[0] will not be updated if 'readonly' is true. @@ -843,20 +924,21 @@ get_hostfile_hostname_ipaddr(char *hostname, struct sockaddr *hostaddr, #define RDONLY 1 #define ROQUIET 2 static int -check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, - struct sshkey *host_key, int readonly, +check_host_key(char *hostname, const struct ssh_conn_info *cinfo, + struct sockaddr *hostaddr, u_short port, + struct sshkey *host_key, int readonly, int clobber_port, char **user_hostfiles, u_int num_user_hostfiles, - char **system_hostfiles, u_int num_system_hostfiles) + char **system_hostfiles, u_int num_system_hostfiles, + const char *hostfile_command) { - HostStatus host_status; - HostStatus ip_status; + HostStatus host_status = -1, ip_status = -1; struct sshkey *raw_key = NULL; char *ip = NULL, *host = NULL; char hostline[1000], *hostp, *fp, *ra; char msg[1024]; - const char *type; - const struct hostkey_entry *host_found, *ip_found; - int len, cancelled_forwarding = 0; + const char *type, *fail_reason; + const struct hostkey_entry *host_found = NULL, *ip_found = NULL; + int len, cancelled_forwarding = 0, confirmed; int local = sockaddr_is_local(hostaddr); int r, want_cert = sshkey_is_cert(host_key), host_ip_differ = 0; int hostkey_trusted = 0; /* Known or explicitly accepted by user */ @@ -875,6 +957,7 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, options.host_key_alias == NULL) { debug("Forcing accepting of host key for " "loopback/localhost."); + options.update_hostkeys = 0; return 0; } @@ -882,7 +965,8 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, * Prepare the hostname and address strings used for hostkey lookup. * In some cases, these will have a port number appended. */ - get_hostfile_hostname_ipaddr(hostname, hostaddr, port, &host, &ip); + get_hostfile_hostname_ipaddr(hostname, hostaddr, + clobber_port ? 0 : port, &host, &ip); /* * Turn off check_host_ip if the connection is to localhost, via proxy @@ -894,17 +978,25 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, host_hostkeys = init_hostkeys(); for (i = 0; i < num_user_hostfiles; i++) - load_hostkeys(host_hostkeys, host, user_hostfiles[i]); + load_hostkeys(host_hostkeys, host, user_hostfiles[i], 0); for (i = 0; i < num_system_hostfiles; i++) - load_hostkeys(host_hostkeys, host, system_hostfiles[i]); + load_hostkeys(host_hostkeys, host, system_hostfiles[i], 0); + if (hostfile_command != NULL && !clobber_port) { + load_hostkeys_command(host_hostkeys, hostfile_command, + "HOSTNAME", cinfo, host_key, host); + } ip_hostkeys = NULL; if (!want_cert && options.check_host_ip) { ip_hostkeys = init_hostkeys(); for (i = 0; i < num_user_hostfiles; i++) - load_hostkeys(ip_hostkeys, ip, user_hostfiles[i]); + load_hostkeys(ip_hostkeys, ip, user_hostfiles[i], 0); for (i = 0; i < num_system_hostfiles; i++) - load_hostkeys(ip_hostkeys, ip, system_hostfiles[i]); + load_hostkeys(ip_hostkeys, ip, system_hostfiles[i], 0); + if (hostfile_command != NULL && !clobber_port) { + load_hostkeys_command(ip_hostkeys, hostfile_command, + "ADDRESS", cinfo, host_key, ip); + } } retry: @@ -920,6 +1012,14 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, &host_found); /* + * If there are no hostfiles, or if the hostkey was found via + * KnownHostsCommand, then don't try to touch the disk. + */ + if (!readonly && (num_user_hostfiles == 0 || + (host_found != NULL && host_found->note != 0))) + readonly = RDONLY; + + /* * Also perform check for the ip address, skip the check if we are * localhost, looking for a certificate, or the hostname was an ip * address to begin with. @@ -928,7 +1028,7 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, ip_status = check_key_in_hostkeys(ip_hostkeys, host_key, &ip_found); if (host_status == HOST_CHANGED && - (ip_status != HOST_CHANGED || + (ip_status != HOST_CHANGED || (ip_found != NULL && !sshkey_equal(ip_found->key, host_found->key)))) host_ip_differ = 1; @@ -942,10 +1042,40 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, host, type, want_cert ? "certificate" : "key"); debug("Found %s in %s:%lu", want_cert ? "CA key" : "key", host_found->file, host_found->line); - if (want_cert && - !check_host_cert(options.host_key_alias == NULL ? - hostname : options.host_key_alias, host_key)) - goto fail; + if (want_cert) { + if (sshkey_cert_check_host(host_key, + options.host_key_alias == NULL ? + hostname : options.host_key_alias, 0, + options.ca_sign_algorithms, &fail_reason) != 0) { + error("%s", fail_reason); + goto fail; + } + /* + * Do not attempt hostkey update if a certificate was + * successfully matched. + */ + if (options.update_hostkeys != 0) { + options.update_hostkeys = 0; + debug3_f("certificate host key in use; " + "disabling UpdateHostkeys"); + } + } + /* Turn off UpdateHostkeys if key was in system known_hosts */ + if (options.update_hostkeys != 0 && + (path_in_hostfiles(host_found->file, + system_hostfiles, num_system_hostfiles) || + (ip_status == HOST_OK && ip_found != NULL && + path_in_hostfiles(ip_found->file, + system_hostfiles, num_system_hostfiles)))) { + options.update_hostkeys = 0; + debug3_f("host key found in GlobalKnownHostsFile; " + "disabling UpdateHostkeys"); + } + if (options.update_hostkeys != 0 && host_found->note) { + options.update_hostkeys = 0; + debug3_f("host key found via KnownHostsCommand; " + "disabling UpdateHostkeys"); + } if (options.check_host_ip && ip_status == HOST_NEW) { if (readonly || want_cert) logit("%s host key for IP address " @@ -967,7 +1097,7 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, ra = sshkey_fingerprint(host_key, options.fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); + fatal_f("sshkey_fingerprint failed"); logit("Host key fingerprint is %s\n%s", fp, ra); free(ra); free(fp); @@ -976,11 +1106,13 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, break; case HOST_NEW: if (options.host_key_alias == NULL && port != 0 && - port != SSH_DEFAULT_PORT) { + port != SSH_DEFAULT_PORT && !clobber_port) { debug("checking without port identifier"); - if (check_host_key(hostname, hostaddr, 0, host_key, - ROQUIET, user_hostfiles, num_user_hostfiles, - system_hostfiles, num_system_hostfiles) == 0) { + if (check_host_key(hostname, cinfo, hostaddr, 0, + host_key, ROQUIET, 1, + user_hostfiles, num_user_hostfiles, + system_hostfiles, num_system_hostfiles, + hostfile_command) == 0) { debug("found matching key w/out port"); break; } @@ -1000,45 +1132,49 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, goto fail; } else if (options.strict_host_key_checking == SSH_STRICT_HOSTKEY_ASK) { - char msg1[1024], msg2[1024]; + char *msg1 = NULL, *msg2 = NULL; + + xasprintf(&msg1, "The authenticity of host " + "'%.200s (%s)' can't be established", host, ip); + + if (show_other_keys(host_hostkeys, host_key)) { + xextendf(&msg1, "\n", "but keys of different " + "type are already known for this host."); + } else + xextendf(&msg1, "", "."); - if (show_other_keys(host_hostkeys, host_key)) - snprintf(msg1, sizeof(msg1), - "\nbut keys of different type are already" - " known for this host."); - else - snprintf(msg1, sizeof(msg1), "."); - /* The default */ fp = sshkey_fingerprint(host_key, options.fingerprint_hash, SSH_FP_DEFAULT); ra = sshkey_fingerprint(host_key, options.fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); - msg2[0] = '\0'; + fatal_f("sshkey_fingerprint failed"); + xextendf(&msg1, "\n", "%s key fingerprint is %s.", + type, fp); + if (options.visual_host_key) + xextendf(&msg1, "\n", "%s", ra); if (options.verify_host_key_dns) { - if (matching_host_key_dns) - snprintf(msg2, sizeof(msg2), - "Matching host key fingerprint" - " found in DNS.\n"); - else - snprintf(msg2, sizeof(msg2), - "No matching host key fingerprint" - " found in DNS.\n"); + xextendf(&msg1, "\n", + "%s host key fingerprint found in DNS.", + matching_host_key_dns ? + "Matching" : "No matching"); } - snprintf(msg, sizeof(msg), - "The authenticity of host '%.200s (%s)' can't be " - "established%s\n" - "%s key fingerprint is %s.%s%s\n%s" + /* msg2 informs for other names matching this key */ + if ((msg2 = other_hostkeys_message(host, ip, host_key, + user_hostfiles, num_user_hostfiles, + system_hostfiles, num_system_hostfiles)) != NULL) + xextendf(&msg1, "\n", "%s", msg2); + + xextendf(&msg1, "\n", "Are you sure you want to continue connecting " - "(yes/no)? ", - host, ip, msg1, type, fp, - options.visual_host_key ? "\n" : "", - options.visual_host_key ? ra : "", - msg2); + "(yes/no/[fingerprint])? "); + + confirmed = confirm(msg1, fp); free(ra); free(fp); - if (!confirm(msg)) + free(msg1); + free(msg2); + if (!confirmed) goto fail; hostkey_trusted = 1; /* user explicitly confirmed */ } @@ -1142,8 +1278,8 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, */ if (options.strict_host_key_checking != SSH_STRICT_HOSTKEY_OFF) { - error("%s host key for %.200s has changed and you have " - "requested strict checking.", type, host); + error("Host key for %.200s has changed and you have " + "requested strict checking.", host); goto fail; } @@ -1163,13 +1299,6 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, error("Keyboard-interactive authentication is disabled" " to avoid man-in-the-middle attacks."); options.kbd_interactive_authentication = 0; - options.challenge_response_authentication = 0; - cancelled_forwarding = 1; - } - if (options.challenge_response_authentication) { - error("Challenge/response authentication is disabled" - " to avoid man-in-the-middle attacks."); - options.challenge_response_authentication = 0; cancelled_forwarding = 1; } if (options.forward_agent) { @@ -1198,6 +1327,11 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, options.tun_open = SSH_TUNMODE_NO; cancelled_forwarding = 1; } + if (options.update_hostkeys != 0) { + error("UpdateHostkeys is disabled because the host " + "key is not trusted."); + options.update_hostkeys = 0; + } if (options.exit_on_forward_failure && cancelled_forwarding) fatal("Error: forwarding disabled due to host key " "check failure"); @@ -1206,7 +1340,7 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, * XXX Should permit the user to change to use the new id. * This could be done by converting the host key to an * identifying sentence, tell that the host identifies itself - * by that sentence, and ask the user if he/she wishes to + * by that sentence, and ask the user if they wish to * accept the authentication. */ break; @@ -1232,7 +1366,7 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, SSH_STRICT_HOSTKEY_ASK) { strlcat(msg, "\nAre you sure you want " "to continue connecting (yes/no)? ", sizeof(msg)); - if (!confirm(msg)) + if (!confirm(msg, NULL)) goto fail; } else if (options.strict_host_key_checking != SSH_STRICT_HOSTKEY_OFF) { @@ -1245,8 +1379,8 @@ check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port, } if (!hostkey_trusted && options.update_hostkeys) { - debug("%s: hostkey not known or explicitly trusted: " - "disabling UpdateHostkeys", __func__); + debug_f("hostkey not known or explicitly trusted: " + "disabling UpdateHostkeys"); options.update_hostkeys = 0; } @@ -1266,10 +1400,9 @@ fail: */ debug("No matching CA found. Retry with plain key"); if ((r = sshkey_from_private(host_key, &raw_key)) != 0) - fatal("%s: sshkey_from_private: %s", - __func__, ssh_err(r)); + fatal_fr(r, "decode key"); if ((r = sshkey_drop_cert(raw_key)) != 0) - fatal("Couldn't drop certificate: %s", ssh_err(r)); + fatal_r(r, "Couldn't drop certificate"); host_key = raw_key; goto retry; } @@ -1285,7 +1418,8 @@ fail: /* returns 0 if key verifies or -1 if key does NOT verify */ int -verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) +verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key, + const struct ssh_conn_info *cinfo) { u_int i; int r = -1, flags = 0; @@ -1294,7 +1428,7 @@ verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) if ((fp = sshkey_fingerprint(host_key, options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { - error("%s: fingerprint host key: %s", __func__, ssh_err(r)); + error_fr(r, "fingerprint host key"); r = -1; goto out; } @@ -1302,8 +1436,7 @@ verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) if (sshkey_is_cert(host_key)) { if ((cafp = sshkey_fingerprint(host_key->cert->signature_key, options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { - error("%s: fingerprint CA key: %s", - __func__, ssh_err(r)); + error_fr(r, "fingerprint CA key"); r = -1; goto out; } @@ -1325,8 +1458,8 @@ verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) } if (sshkey_equal(previous_host_key, host_key)) { - debug2("%s: server host key %s %s matches cached key", - __func__, sshkey_type(host_key), fp); + debug2_f("server host key %s %s matches cached key", + sshkey_type(host_key), fp); r = 0; goto out; } @@ -1344,9 +1477,9 @@ verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) r = -1; goto out; default: - error("Error checking host key %s %s in " - "revoked keys file %s: %s", sshkey_type(host_key), - fp, options.revoked_host_keys, ssh_err(r)); + error_r(r, "Error checking host key %s %s in " + "revoked keys file %s", sshkey_type(host_key), + fp, options.revoked_host_keys); r = -1; goto out; } @@ -1380,9 +1513,10 @@ verify_host_key(char *host, struct sockaddr *hostaddr, struct sshkey *host_key) } } } - r = check_host_key(host, hostaddr, options.port, host_key, RDRW, - options.user_hostfiles, options.num_user_hostfiles, - options.system_hostfiles, options.num_system_hostfiles); + r = check_host_key(host, cinfo, hostaddr, options.port, host_key, + RDRW, 0, options.user_hostfiles, options.num_user_hostfiles, + options.system_hostfiles, options.num_system_hostfiles, + options.known_hosts_command); out: sshkey_free(plain); @@ -1404,11 +1538,13 @@ out: * This function does not require super-user privileges. */ void -ssh_login(Sensitive *sensitive, const char *orighost, - struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms) +ssh_login(struct ssh *ssh, Sensitive *sensitive, const char *orighost, + struct sockaddr *hostaddr, u_short port, struct passwd *pw, int timeout_ms, + const struct ssh_conn_info *cinfo) { char *host; char *server_user, *local_user; + int r; local_user = xstrdup(pw->pw_name); server_user = options.user ? options.user : local_user; @@ -1418,35 +1554,20 @@ ssh_login(Sensitive *sensitive, const char *orighost, lowercase(host); /* Exchange protocol version identification strings with the server. */ - ssh_exchange_identification(timeout_ms); + if ((r = kex_exchange_identification(ssh, timeout_ms, + options.version_addendum)) != 0) + sshpkt_fatal(ssh, r, "banner exchange"); /* Put the connection into non-blocking mode. */ - packet_set_nonblocking(); + ssh_packet_set_nonblocking(ssh); /* key exchange */ /* authenticate user */ debug("Authenticating to %s:%d as '%s'", host, port, server_user); - ssh_kex2(host, hostaddr, port); - ssh_userauth2(local_user, server_user, host, sensitive); + ssh_kex2(ssh, host, hostaddr, port, cinfo); + ssh_userauth2(ssh, local_user, server_user, host, sensitive); free(local_user); -} - -void -ssh_put_password(char *password) -{ - int size; - char *padded; - - if (datafellows & SSH_BUG_PASSWORDPAD) { - packet_put_cstring(password); - return; - } - size = ROUNDUP(strlen(password) + 1, 32); - padded = xcalloc(1, size); - strlcpy(padded, password, size); - packet_put_string(padded, size); - explicit_bzero(padded, size); - free(padded); + free(host); } /* print all known host keys for a given host, but skip keys of given type */ @@ -1468,14 +1589,15 @@ show_other_keys(struct hostkeys *hostkeys, struct sshkey *key) for (i = 0; type[i] != -1; i++) { if (type[i] == key->type) continue; - if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], &found)) + if (!lookup_key_in_hostkeys_by_type(hostkeys, type[i], + -1, &found)) continue; fp = sshkey_fingerprint(found->key, options.fingerprint_hash, SSH_FP_DEFAULT); ra = sshkey_fingerprint(found->key, options.fingerprint_hash, SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); + fatal_f("sshkey_fingerprint fail"); logit("WARNING: %s key found for host %s\n" "in %s:%lu\n" "%s key fingerprint %s.", @@ -1499,7 +1621,7 @@ warn_changed_key(struct sshkey *host_key) fp = sshkey_fingerprint(host_key, options.fingerprint_hash, SSH_FP_DEFAULT); if (fp == NULL) - fatal("%s: sshkey_fingerprint fail", __func__); + fatal_f("sshkey_fingerprint fail"); error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @"); @@ -1532,10 +1654,10 @@ ssh_local_cmd(const char *args) if ((shell = getenv("SHELL")) == NULL || *shell == '\0') shell = _PATH_BSHELL; - osighand = signal(SIGCHLD, SIG_DFL); + osighand = ssh_signal(SIGCHLD, SIG_DFL); pid = fork(); if (pid == 0) { - signal(SIGPIPE, SIG_DFL); + ssh_signal(SIGPIPE, SIG_DFL); debug3("Executing %s -c \"%s\"", shell, args); execl(shell, shell, "-c", args, (char *)NULL); error("Couldn't execute %s -c \"%s\": %s", @@ -1546,7 +1668,7 @@ ssh_local_cmd(const char *args) while (waitpid(pid, &status, 0) == -1) if (errno != EINTR) fatal("Couldn't wait for child: %s", strerror(errno)); - signal(SIGCHLD, osighand); + ssh_signal(SIGCHLD, osighand); if (!WIFEXITED(status)) return (1); @@ -1555,10 +1677,11 @@ ssh_local_cmd(const char *args) } void -maybe_add_key_to_agent(char *authfile, const struct sshkey *private, - char *comment, char *passphrase) +maybe_add_key_to_agent(const char *authfile, struct sshkey *private, + const char *comment, const char *passphrase) { int auth_sock = -1, r; + const char *skprovider = NULL; if (options.add_keys_to_agent == 0) return; @@ -1574,9 +1697,12 @@ maybe_add_key_to_agent(char *authfile, const struct sshkey *private, close(auth_sock); return; } - - if ((r = ssh_add_identity_constrained(auth_sock, private, comment, 0, - (options.add_keys_to_agent == 3), 0)) == 0) + if (sshkey_is_sk(private)) + skprovider = options.sk_provider; + if ((r = ssh_add_identity_constrained(auth_sock, private, + comment == NULL ? authfile : comment, + options.add_keys_to_agent_lifespan, + (options.add_keys_to_agent == 3), 0, skprovider)) == 0) debug("identity added to agent: %s", authfile); else debug("could not add identity to agent: %s (%d)", authfile, r); |