[BACK]Return to sshconnect.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshconnect.c, Revision 1.308

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