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

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