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

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