[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.304

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