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

1.199   ! deraadt     1: /* $OpenBSD: sshconnect.c,v 1.198 2006/08/01 23:22:47 stevesk 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:
                     22: #include <netinet/in.h>
1.172     stevesk    23:
1.176     stevesk    24: #include <ctype.h>
1.190     stevesk    25: #include <errno.h>
1.191     stevesk    26: #include <netdb.h>
1.172     stevesk    27: #include <paths.h>
1.199   ! deraadt    28: #include <signal.h>
1.188     stevesk    29: #include <pwd.h>
1.198     stevesk    30: #include <stdio.h>
1.196     stevesk    31: #include <stdlib.h>
1.193     stevesk    32: #include <string.h>
1.192     stevesk    33: #include <unistd.h>
1.71      markus     34:
1.199   ! deraadt    35: #include "xmalloc.h"
1.91      markus     36: #include "ssh.h"
1.1       deraadt    37: #include "rsa.h"
1.59      markus     38: #include "buffer.h"
1.1       deraadt    39: #include "packet.h"
                     40: #include "uidswap.h"
1.21      markus     41: #include "compat.h"
1.58      markus     42: #include "key.h"
1.71      markus     43: #include "sshconnect.h"
1.58      markus     44: #include "hostfile.h"
1.91      markus     45: #include "log.h"
                     46: #include "readconf.h"
                     47: #include "atomicio.h"
                     48: #include "misc.h"
1.140     jakob      49: #include "dns.h"
1.186     stevesk    50: #include "version.h"
1.140     jakob      51:
1.70      markus     52: char *client_version_string = NULL;
                     53: char *server_version_string = NULL;
1.59      markus     54:
1.169     stevesk    55: static int matching_host_key_dns = 0;
1.145     jakob      56:
1.124     markus     57: /* import */
1.43      markus     58: extern Options options;
1.50      markus     59: extern char *__progname;
1.124     markus     60: extern uid_t original_real_uid;
                     61: extern uid_t original_effective_uid;
1.135     djm        62: extern pid_t proxy_command_pid;
1.91      markus     63:
1.132     markus     64: static int show_other_keys(const char *, Key *);
1.150     jakob      65: static void warn_changed_key(Key *);
1.132     markus     66:
1.39      deraadt    67: /*
                     68:  * Connect to the given ssh server using a proxy command.
                     69:  */
1.109     itojun     70: static int
1.124     markus     71: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1       deraadt    72: {
1.164     djm        73:        char *command_string, *tmp;
1.38      markus     74:        int pin[2], pout[2];
1.69      deraadt    75:        pid_t pid;
1.49      markus     76:        char strport[NI_MAXSERV];
1.38      markus     77:
                     78:        /* Convert the port number into a string. */
1.49      markus     79:        snprintf(strport, sizeof strport, "%hu", port);
1.38      markus     80:
1.135     djm        81:        /*
                     82:         * Build the final command string in the buffer by making the
                     83:         * appropriate substitutions to the given proxy command.
                     84:         *
1.154     djm        85:         * Use "exec" to avoid "sh -c" processes on some platforms
1.135     djm        86:         * (e.g. Solaris)
                     87:         */
1.179     djm        88:        xasprintf(&tmp, "exec %s", proxy_command);
1.164     djm        89:        command_string = percent_expand(tmp, "h", host,
                     90:            "p", strport, (char *)NULL);
                     91:        xfree(tmp);
1.38      markus     92:
                     93:        /* Create pipes for communicating with the proxy. */
                     94:        if (pipe(pin) < 0 || pipe(pout) < 0)
                     95:                fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118     deraadt    96:                    strerror(errno));
1.38      markus     97:
                     98:        debug("Executing proxy command: %.500s", command_string);
                     99:
                    100:        /* Fork and execute the proxy command. */
                    101:        if ((pid = fork()) == 0) {
                    102:                char *argv[10];
                    103:
                    104:                /* Child.  Permanently give up superuser privileges. */
1.184     markus    105:                permanently_drop_suid(original_real_uid);
1.38      markus    106:
                    107:                /* Redirect stdin and stdout. */
                    108:                close(pin[1]);
                    109:                if (pin[0] != 0) {
                    110:                        if (dup2(pin[0], 0) < 0)
                    111:                                perror("dup2 stdin");
                    112:                        close(pin[0]);
                    113:                }
                    114:                close(pout[0]);
                    115:                if (dup2(pout[1], 1) < 0)
                    116:                        perror("dup2 stdout");
                    117:                /* Cannot be 1 because pin allocated two descriptors. */
                    118:                close(pout[1]);
                    119:
                    120:                /* Stderr is left as it is so that error messages get
                    121:                   printed on the user's terminal. */
1.89      markus    122:                argv[0] = _PATH_BSHELL;
1.38      markus    123:                argv[1] = "-c";
                    124:                argv[2] = command_string;
                    125:                argv[3] = NULL;
                    126:
                    127:                /* Execute the proxy command.  Note that we gave up any
                    128:                   extra privileges above. */
1.89      markus    129:                execv(argv[0], argv);
                    130:                perror(argv[0]);
1.38      markus    131:                exit(1);
                    132:        }
                    133:        /* Parent. */
                    134:        if (pid < 0)
                    135:                fatal("fork failed: %.100s", strerror(errno));
1.135     djm       136:        else
                    137:                proxy_command_pid = pid; /* save pid to clean up later */
1.38      markus    138:
                    139:        /* Close child side of the descriptors. */
                    140:        close(pin[0]);
                    141:        close(pout[1]);
                    142:
                    143:        /* Free the command name. */
1.164     djm       144:        xfree(command_string);
1.38      markus    145:
                    146:        /* Set the connection file descriptors. */
                    147:        packet_set_connection(pout[0], pin[1]);
1.1       deraadt   148:
1.110     markus    149:        /* Indicate OK return */
                    150:        return 0;
1.1       deraadt   151: }
                    152:
1.39      deraadt   153: /*
                    154:  * Creates a (possibly privileged) socket for use as the ssh connection.
                    155:  */
1.109     itojun    156: static int
1.139     markus    157: ssh_create_socket(int privileged, struct addrinfo *ai)
1.1       deraadt   158: {
1.105     markus    159:        int sock, gaierr;
                    160:        struct addrinfo hints, *res;
1.1       deraadt   161:
1.40      markus    162:        /*
                    163:         * If we are running as root and want to connect to a privileged
                    164:         * port, bind our own socket to a privileged port.
                    165:         */
1.38      markus    166:        if (privileged) {
                    167:                int p = IPPORT_RESERVED - 1;
1.124     markus    168:                PRIV_START;
1.139     markus    169:                sock = rresvport_af(&p, ai->ai_family);
1.124     markus    170:                PRIV_END;
1.38      markus    171:                if (sock < 0)
1.139     markus    172:                        error("rresvport: af=%d %.100s", ai->ai_family,
                    173:                            strerror(errno));
1.55      markus    174:                else
                    175:                        debug("Allocated local port %d.", p);
1.105     markus    176:                return sock;
                    177:        }
1.139     markus    178:        sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.105     markus    179:        if (sock < 0)
                    180:                error("socket: %.100s", strerror(errno));
                    181:
                    182:        /* Bind the socket to an alternative local IP address */
                    183:        if (options.bind_address == NULL)
                    184:                return sock;
                    185:
                    186:        memset(&hints, 0, sizeof(hints));
1.139     markus    187:        hints.ai_family = ai->ai_family;
                    188:        hints.ai_socktype = ai->ai_socktype;
                    189:        hints.ai_protocol = ai->ai_protocol;
1.105     markus    190:        hints.ai_flags = AI_PASSIVE;
                    191:        gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
                    192:        if (gaierr) {
                    193:                error("getaddrinfo: %s: %s", options.bind_address,
                    194:                    gai_strerror(gaierr));
                    195:                close(sock);
                    196:                return -1;
                    197:        }
                    198:        if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
                    199:                error("bind: %s: %s", options.bind_address, strerror(errno));
                    200:                close(sock);
                    201:                freeaddrinfo(res);
                    202:                return -1;
1.38      markus    203:        }
1.105     markus    204:        freeaddrinfo(res);
1.38      markus    205:        return sock;
1.1       deraadt   206: }
                    207:
1.141     djm       208: static int
                    209: timeout_connect(int sockfd, const struct sockaddr *serv_addr,
                    210:     socklen_t addrlen, int timeout)
                    211: {
                    212:        fd_set *fdset;
                    213:        struct timeval tv;
                    214:        socklen_t optlen;
1.179     djm       215:        int optval, rc, result = -1;
1.141     djm       216:
                    217:        if (timeout <= 0)
                    218:                return (connect(sockfd, serv_addr, addrlen));
                    219:
1.156     djm       220:        set_nonblock(sockfd);
1.141     djm       221:        rc = connect(sockfd, serv_addr, addrlen);
1.156     djm       222:        if (rc == 0) {
                    223:                unset_nonblock(sockfd);
1.141     djm       224:                return (0);
1.156     djm       225:        }
1.141     djm       226:        if (errno != EINPROGRESS)
                    227:                return (-1);
                    228:
1.179     djm       229:        fdset = (fd_set *)xcalloc(howmany(sockfd + 1, NFDBITS),
                    230:            sizeof(fd_mask));
1.141     djm       231:        FD_SET(sockfd, fdset);
                    232:        tv.tv_sec = timeout;
                    233:        tv.tv_usec = 0;
                    234:
1.162     deraadt   235:        for (;;) {
1.141     djm       236:                rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
                    237:                if (rc != -1 || errno != EINTR)
                    238:                        break;
                    239:        }
                    240:
1.162     deraadt   241:        switch (rc) {
1.141     djm       242:        case 0:
                    243:                /* Timed out */
                    244:                errno = ETIMEDOUT;
1.142     djm       245:                break;
1.141     djm       246:        case -1:
                    247:                /* Select error */
1.154     djm       248:                debug("select: %s", strerror(errno));
1.142     djm       249:                break;
1.141     djm       250:        case 1:
                    251:                /* Completed or failed */
                    252:                optval = 0;
                    253:                optlen = sizeof(optval);
1.154     djm       254:                if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval,
1.148     markus    255:                    &optlen) == -1) {
1.154     djm       256:                        debug("getsockopt: %s", strerror(errno));
1.142     djm       257:                        break;
1.148     markus    258:                }
1.141     djm       259:                if (optval != 0) {
                    260:                        errno = optval;
1.142     djm       261:                        break;
1.141     djm       262:                }
1.142     djm       263:                result = 0;
1.156     djm       264:                unset_nonblock(sockfd);
1.141     djm       265:                break;
                    266:        default:
                    267:                /* Should not occur */
                    268:                fatal("Bogus return (%d) from select()", rc);
                    269:        }
                    270:
1.142     djm       271:        xfree(fdset);
                    272:        return (result);
1.141     djm       273: }
                    274:
1.39      deraadt   275: /*
1.49      markus    276:  * Opens a TCP/IP connection to the remote server on the given host.
                    277:  * The address of the remote host will be returned in hostaddr.
1.124     markus    278:  * If port is 0, the default port will be used.  If needpriv is true,
1.39      deraadt   279:  * a privileged port will be allocated to make the connection.
1.124     markus    280:  * This requires super-user privileges if needpriv is true.
1.39      deraadt   281:  * Connection_attempts specifies the maximum number of tries (one per
                    282:  * second).  If proxy_command is non-NULL, it specifies the command (with %h
                    283:  * and %p substituted for host and port, respectively) to use to contact
                    284:  * the daemon.
                    285:  */
1.38      markus    286: int
1.49      markus    287: ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
1.115     markus    288:     u_short port, int family, int connection_attempts,
1.124     markus    289:     int needpriv, const char *proxy_command)
1.1       deraadt   290: {
1.90      markus    291:        int gaierr;
                    292:        int on = 1;
1.49      markus    293:        int sock = -1, attempt;
1.90      markus    294:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.49      markus    295:        struct addrinfo hints, *ai, *aitop;
1.38      markus    296:
1.136     markus    297:        debug2("ssh_connect: needpriv %d", needpriv);
1.38      markus    298:
                    299:        /* If a proxy command is given, connect using it. */
                    300:        if (proxy_command != NULL)
1.124     markus    301:                return ssh_proxy_connect(host, port, proxy_command);
1.38      markus    302:
                    303:        /* No proxy command. */
                    304:
1.49      markus    305:        memset(&hints, 0, sizeof(hints));
1.115     markus    306:        hints.ai_family = family;
1.49      markus    307:        hints.ai_socktype = SOCK_STREAM;
1.126     deraadt   308:        snprintf(strport, sizeof strport, "%u", port);
1.49      markus    309:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.50      markus    310:                fatal("%s: %.100s: %s", __progname, host,
                    311:                    gai_strerror(gaierr));
1.38      markus    312:
1.181     markus    313:        for (attempt = 0; attempt < connection_attempts; attempt++) {
1.38      markus    314:                if (attempt > 0)
                    315:                        debug("Trying again...");
                    316:
1.181     markus    317:                /*
                    318:                 * Loop through addresses for this host, and try each one in
                    319:                 * sequence until the connection succeeds.
                    320:                 */
1.49      markus    321:                for (ai = aitop; ai; ai = ai->ai_next) {
                    322:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                    323:                                continue;
                    324:                        if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
                    325:                            ntop, sizeof(ntop), strport, sizeof(strport),
                    326:                            NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                    327:                                error("ssh_connect: getnameinfo failed");
                    328:                                continue;
                    329:                        }
                    330:                        debug("Connecting to %.200s [%.100s] port %s.",
                    331:                                host, ntop, strport);
                    332:
                    333:                        /* Create a socket for connecting. */
1.139     markus    334:                        sock = ssh_create_socket(needpriv, ai);
1.49      markus    335:                        if (sock < 0)
1.110     markus    336:                                /* Any error is already output */
1.49      markus    337:                                continue;
                    338:
1.141     djm       339:                        if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
                    340:                            options.connection_timeout) >= 0) {
1.49      markus    341:                                /* Successful connection. */
1.102     markus    342:                                memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
1.38      markus    343:                                break;
1.49      markus    344:                        } else {
1.131     itojun    345:                                debug("connect to address %s port %s: %s",
                    346:                                    ntop, strport, strerror(errno));
1.38      markus    347:                                close(sock);
1.181     markus    348:                                sock = -1;
1.38      markus    349:                        }
                    350:                }
1.181     markus    351:                if (sock != -1)
1.49      markus    352:                        break;  /* Successful connection. */
1.1       deraadt   353:
1.38      markus    354:                /* Sleep a moment before retrying. */
                    355:                sleep(1);
                    356:        }
1.49      markus    357:
                    358:        freeaddrinfo(aitop);
                    359:
1.38      markus    360:        /* Return failure if we didn't get a successful connection. */
1.181     markus    361:        if (sock == -1) {
1.159     markus    362:                error("ssh: connect to host %s port %s: %s",
1.130     itojun    363:                    host, strport, strerror(errno));
1.159     markus    364:                return (-1);
1.130     itojun    365:        }
1.38      markus    366:
                    367:        debug("Connection established.");
1.90      markus    368:
1.155     markus    369:        /* Set SO_KEEPALIVE if requested. */
                    370:        if (options.tcp_keep_alive &&
1.90      markus    371:            setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
                    372:            sizeof(on)) < 0)
                    373:                error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.38      markus    374:
                    375:        /* Set the connection. */
                    376:        packet_set_connection(sock, sock);
1.1       deraadt   377:
1.110     markus    378:        return 0;
1.59      markus    379: }
                    380:
1.43      markus    381: /*
1.39      deraadt   382:  * Waits for the server identification string, and sends our own
                    383:  * identification string.
                    384:  */
1.109     itojun    385: static void
1.95      itojun    386: ssh_exchange_identification(void)
1.1       deraadt   387: {
1.38      markus    388:        char buf[256], remote_version[256];     /* must be same size! */
1.165     djm       389:        int remote_major, remote_minor, mismatch;
1.38      markus    390:        int connection_in = packet_get_connection_in();
                    391:        int connection_out = packet_get_connection_out();
1.93      stevesk   392:        int minor1 = PROTOCOL_MINOR_1;
1.185     djm       393:        u_int i, n;
1.38      markus    394:
1.163     avsm      395:        /* Read other side's version identification. */
1.185     djm       396:        for (n = 0;;) {
1.75      markus    397:                for (i = 0; i < sizeof(buf) - 1; i++) {
1.163     avsm      398:                        size_t len = atomicio(read, connection_in, &buf[i], 1);
                    399:
1.167     djm       400:                        if (len != 1 && errno == EPIPE)
1.163     avsm      401:                                fatal("ssh_exchange_identification: Connection closed by remote host");
                    402:                        else if (len != 1)
1.75      markus    403:                                fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
                    404:                        if (buf[i] == '\r') {
                    405:                                buf[i] = '\n';
                    406:                                buf[i + 1] = 0;
                    407:                                continue;               /**XXX wait for \n */
                    408:                        }
                    409:                        if (buf[i] == '\n') {
                    410:                                buf[i + 1] = 0;
                    411:                                break;
                    412:                        }
1.185     djm       413:                        if (++n > 65536)
                    414:                                fatal("ssh_exchange_identification: No banner received");
1.38      markus    415:                }
1.75      markus    416:                buf[sizeof(buf) - 1] = 0;
1.76      markus    417:                if (strncmp(buf, "SSH-", 4) == 0)
1.38      markus    418:                        break;
1.75      markus    419:                debug("ssh_exchange_identification: %s", buf);
1.38      markus    420:        }
1.59      markus    421:        server_version_string = xstrdup(buf);
1.38      markus    422:
1.40      markus    423:        /*
                    424:         * Check that the versions match.  In future this might accept
                    425:         * several versions and set appropriate flags to handle them.
                    426:         */
1.59      markus    427:        if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
                    428:            &remote_major, &remote_minor, remote_version) != 3)
1.38      markus    429:                fatal("Bad remote protocol version identification: '%.100s'", buf);
                    430:        debug("Remote protocol version %d.%d, remote software version %.100s",
1.118     deraadt   431:            remote_major, remote_minor, remote_version);
1.38      markus    432:
1.59      markus    433:        compat_datafellows(remote_version);
1.64      markus    434:        mismatch = 0;
1.59      markus    435:
1.116     deraadt   436:        switch (remote_major) {
1.64      markus    437:        case 1:
                    438:                if (remote_minor == 99 &&
                    439:                    (options.protocol & SSH_PROTO_2) &&
                    440:                    !(options.protocol & SSH_PROTO_1_PREFERRED)) {
                    441:                        enable_compat20();
                    442:                        break;
                    443:                }
                    444:                if (!(options.protocol & SSH_PROTO_1)) {
                    445:                        mismatch = 1;
                    446:                        break;
                    447:                }
                    448:                if (remote_minor < 3) {
                    449:                        fatal("Remote machine has too old SSH software version.");
1.81      markus    450:                } else if (remote_minor == 3 || remote_minor == 4) {
1.64      markus    451:                        /* We speak 1.3, too. */
                    452:                        enable_compat13();
1.81      markus    453:                        minor1 = 3;
1.64      markus    454:                        if (options.forward_agent) {
1.138     itojun    455:                                logit("Agent forwarding disabled for protocol 1.3");
1.64      markus    456:                                options.forward_agent = 0;
                    457:                        }
                    458:                }
                    459:                break;
                    460:        case 2:
                    461:                if (options.protocol & SSH_PROTO_2) {
                    462:                        enable_compat20();
                    463:                        break;
1.38      markus    464:                }
1.64      markus    465:                /* FALLTHROUGH */
1.68      markus    466:        default:
1.64      markus    467:                mismatch = 1;
                    468:                break;
1.38      markus    469:        }
1.64      markus    470:        if (mismatch)
1.38      markus    471:                fatal("Protocol major versions differ: %d vs. %d",
1.64      markus    472:                    (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
                    473:                    remote_major);
1.38      markus    474:        /* Send our own protocol version identification. */
                    475:        snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.64      markus    476:            compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
1.81      markus    477:            compat20 ? PROTOCOL_MINOR_2 : minor1,
1.59      markus    478:            SSH_VERSION);
1.146     deraadt   479:        if (atomicio(vwrite, connection_out, buf, strlen(buf)) != strlen(buf))
1.38      markus    480:                fatal("write: %.100s", strerror(errno));
1.59      markus    481:        client_version_string = xstrdup(buf);
                    482:        chop(client_version_string);
                    483:        chop(server_version_string);
                    484:        debug("Local version string %.100s", client_version_string);
1.1       deraadt   485: }
                    486:
1.96      markus    487: /* defaults to 'no' */
1.109     itojun    488: static int
1.112     markus    489: confirm(const char *prompt)
1.1       deraadt   490: {
1.119     markus    491:        const char *msg, *again = "Please type 'yes' or 'no': ";
                    492:        char *p;
                    493:        int ret = -1;
1.96      markus    494:
                    495:        if (options.batch_mode)
                    496:                return 0;
1.119     markus    497:        for (msg = prompt;;msg = again) {
                    498:                p = read_passphrase(msg, RP_ECHO);
                    499:                if (p == NULL ||
                    500:                    (p[0] == '\0') || (p[0] == '\n') ||
                    501:                    strncasecmp(p, "no", 2) == 0)
                    502:                        ret = 0;
1.127     markus    503:                if (p && strncasecmp(p, "yes", 3) == 0)
1.119     markus    504:                        ret = 1;
                    505:                if (p)
                    506:                        xfree(p);
                    507:                if (ret != -1)
                    508:                        return ret;
1.1       deraadt   509:        }
                    510: }
                    511:
1.39      deraadt   512: /*
1.108     markus    513:  * check whether the supplied host key is valid, return -1 if the key
                    514:  * is not valid. the user_hostfile will not be updated if 'readonly' is true.
1.39      deraadt   515:  */
1.197     dtucker   516: #define RDRW   0
                    517: #define RDONLY 1
                    518: #define ROQUIET        2
1.109     itojun    519: static int
1.197     dtucker   520: check_host_key(char *hostname, struct sockaddr *hostaddr, u_short port,
                    521:     Key *host_key, int readonly, const char *user_hostfile,
                    522:     const char *system_hostfile)
1.1       deraadt   523: {
1.58      markus    524:        Key *file_key;
1.152     jakob     525:        const char *type = key_type(host_key);
1.189     dtucker   526:        char *ip = NULL, *host = NULL;
1.100     markus    527:        char hostline[1000], *hostp, *fp;
1.38      markus    528:        HostStatus host_status;
                    529:        HostStatus ip_status;
1.161     djm       530:        int r, local = 0, host_ip_differ = 0;
1.49      markus    531:        char ntop[NI_MAXHOST];
1.119     markus    532:        char msg[1024];
1.145     jakob     533:        int len, host_line, ip_line;
1.85      markus    534:        const char *host_file = NULL, *ip_file = NULL;
1.49      markus    535:
                    536:        /*
                    537:         * Force accepting of the host key for loopback/localhost. The
                    538:         * problem is that if the home directory is NFS-mounted to multiple
                    539:         * machines, localhost will refer to a different machine in each of
                    540:         * them, and the user will get bogus HOST_CHANGED warnings.  This
                    541:         * essentially disables host authentication for localhost; however,
                    542:         * this is probably not a real problem.
                    543:         */
1.70      markus    544:        /**  hostaddr == 0! */
1.49      markus    545:        switch (hostaddr->sa_family) {
                    546:        case AF_INET:
1.108     markus    547:                local = (ntohl(((struct sockaddr_in *)hostaddr)->
1.168     djm       548:                    sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49      markus    549:                break;
                    550:        case AF_INET6:
1.108     markus    551:                local = IN6_IS_ADDR_LOOPBACK(
                    552:                    &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49      markus    553:                break;
                    554:        default:
                    555:                local = 0;
                    556:                break;
                    557:        }
1.111     markus    558:        if (options.no_host_authentication_for_localhost == 1 && local &&
                    559:            options.host_key_alias == NULL) {
1.88      markus    560:                debug("Forcing accepting of host key for "
                    561:                    "loopback/localhost.");
1.108     markus    562:                return 0;
1.49      markus    563:        }
1.42      markus    564:
                    565:        /*
1.88      markus    566:         * We don't have the remote ip-address for connections
                    567:         * using a proxy command
1.42      markus    568:         */
1.84      markus    569:        if (options.proxy_command == NULL) {
                    570:                if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86      markus    571:                    NULL, 0, NI_NUMERICHOST) != 0)
1.84      markus    572:                        fatal("check_host_key: getnameinfo failed");
1.197     dtucker   573:                ip = put_host_port(ntop, port);
1.84      markus    574:        } else {
                    575:                ip = xstrdup("<no hostip for proxy command>");
1.86      markus    576:        }
1.88      markus    577:        /*
                    578:         * Turn off check_host_ip if the connection is to localhost, via proxy
                    579:         * command or if we don't have a hostname to compare with
                    580:         */
1.189     dtucker   581:        if (options.check_host_ip && (local ||
                    582:            strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
1.88      markus    583:                options.check_host_ip = 0;
1.86      markus    584:
                    585:        /*
1.189     dtucker   586:         * Allow the user to record the key under a different name or
                    587:         * differentiate a non-standard port.  This is useful for ssh
                    588:         * tunneling over forwarded connections or if you run multiple
                    589:         * sshd's on different ports on the same machine.
1.86      markus    590:         */
                    591:        if (options.host_key_alias != NULL) {
1.189     dtucker   592:                host = xstrdup(options.host_key_alias);
1.86      markus    593:                debug("using hostkeyalias: %s", host);
1.189     dtucker   594:        } else {
1.197     dtucker   595:                host = put_host_port(hostname, port);
1.84      markus    596:        }
1.38      markus    597:
1.46      markus    598:        /*
                    599:         * Store the host key from the known host file in here so that we can
                    600:         * compare it with the key for the IP address.
                    601:         */
1.58      markus    602:        file_key = key_new(host_key->type);
1.38      markus    603:
1.40      markus    604:        /*
1.170     djm       605:         * Check if the host key is present in the user's list of known
1.40      markus    606:         * hosts or in the systemwide list.
                    607:         */
1.85      markus    608:        host_file = user_hostfile;
1.108     markus    609:        host_status = check_host_in_hostfile(host_file, host, host_key,
1.118     deraadt   610:            file_key, &host_line);
1.85      markus    611:        if (host_status == HOST_NEW) {
                    612:                host_file = system_hostfile;
1.108     markus    613:                host_status = check_host_in_hostfile(host_file, host, host_key,
                    614:                    file_key, &host_line);
1.85      markus    615:        }
1.40      markus    616:        /*
                    617:         * Also perform check for the ip address, skip the check if we are
                    618:         * localhost or the hostname was an ip address to begin with
                    619:         */
1.88      markus    620:        if (options.check_host_ip) {
1.58      markus    621:                Key *ip_key = key_new(host_key->type);
1.38      markus    622:
1.85      markus    623:                ip_file = user_hostfile;
1.108     markus    624:                ip_status = check_host_in_hostfile(ip_file, ip, host_key,
                    625:                    ip_key, &ip_line);
1.85      markus    626:                if (ip_status == HOST_NEW) {
                    627:                        ip_file = system_hostfile;
1.108     markus    628:                        ip_status = check_host_in_hostfile(ip_file, ip,
                    629:                            host_key, ip_key, &ip_line);
1.85      markus    630:                }
1.38      markus    631:                if (host_status == HOST_CHANGED &&
1.58      markus    632:                    (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38      markus    633:                        host_ip_differ = 1;
                    634:
1.58      markus    635:                key_free(ip_key);
1.38      markus    636:        } else
                    637:                ip_status = host_status;
                    638:
1.58      markus    639:        key_free(file_key);
1.38      markus    640:
                    641:        switch (host_status) {
                    642:        case HOST_OK:
                    643:                /* The host is known and the key matches. */
1.72      markus    644:                debug("Host '%.200s' is known and matches the %s host key.",
                    645:                    host, type);
1.85      markus    646:                debug("Found key in %s:%d", host_file, host_line);
1.88      markus    647:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.108     markus    648:                        if (readonly)
1.138     itojun    649:                                logit("%s host key for IP address "
1.108     markus    650:                                    "'%.128s' not in list of known hosts.",
                    651:                                    type, ip);
                    652:                        else if (!add_host_to_hostfile(user_hostfile, ip,
1.160     djm       653:                            host_key, options.hash_known_hosts))
1.138     itojun    654:                                logit("Failed to add the %s host key for IP "
1.108     markus    655:                                    "address '%.128s' to the list of known "
                    656:                                    "hosts (%.30s).", type, ip, user_hostfile);
1.88      markus    657:                        else
1.138     itojun    658:                                logit("Warning: Permanently added the %s host "
1.108     markus    659:                                    "key for IP address '%.128s' to the list "
                    660:                                    "of known hosts.", type, ip);
1.38      markus    661:                }
                    662:                break;
                    663:        case HOST_NEW:
1.197     dtucker   664:                if (options.host_key_alias == NULL && port != 0 &&
                    665:                    port != SSH_DEFAULT_PORT) {
                    666:                        debug("checking without port identifier");
                    667:                        if (check_host_key(hostname, hostaddr, 0, host_key, 2,
                    668:                            user_hostfile, system_hostfile) == 0) {
                    669:                                debug("found matching key w/out port");
                    670:                                break;
                    671:                        }
                    672:                }
1.108     markus    673:                if (readonly)
                    674:                        goto fail;
1.38      markus    675:                /* The host is new. */
                    676:                if (options.strict_host_key_checking == 1) {
1.108     markus    677:                        /*
                    678:                         * User has requested strict host key checking.  We
                    679:                         * will not add the host key automatically.  The only
                    680:                         * alternative left is to abort.
                    681:                         */
                    682:                        error("No %s host key is known for %.200s and you "
                    683:                            "have requested strict checking.", type, host);
                    684:                        goto fail;
1.38      markus    685:                } else if (options.strict_host_key_checking == 2) {
1.145     jakob     686:                        char msg1[1024], msg2[1024];
                    687:
                    688:                        if (show_other_keys(host, host_key))
                    689:                                snprintf(msg1, sizeof(msg1),
1.168     djm       690:                                    "\nbut keys of different type are already"
                    691:                                    " known for this host.");
1.145     jakob     692:                        else
                    693:                                snprintf(msg1, sizeof(msg1), ".");
1.38      markus    694:                        /* The default */
1.100     markus    695:                        fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.145     jakob     696:                        msg2[0] = '\0';
                    697:                        if (options.verify_host_key_dns) {
1.153     jakob     698:                                if (matching_host_key_dns)
1.145     jakob     699:                                        snprintf(msg2, sizeof(msg2),
                    700:                                            "Matching host key fingerprint"
                    701:                                            " found in DNS.\n");
                    702:                                else
                    703:                                        snprintf(msg2, sizeof(msg2),
                    704:                                            "No matching host key fingerprint"
                    705:                                            " found in DNS.\n");
                    706:                        }
1.119     markus    707:                        snprintf(msg, sizeof(msg),
1.108     markus    708:                            "The authenticity of host '%.200s (%s)' can't be "
1.132     markus    709:                            "established%s\n"
1.145     jakob     710:                            "%s key fingerprint is %s.\n%s"
1.108     markus    711:                            "Are you sure you want to continue connecting "
1.132     markus    712:                            "(yes/no)? ",
1.145     jakob     713:                            host, ip, msg1, type, fp, msg2);
1.100     markus    714:                        xfree(fp);
1.119     markus    715:                        if (!confirm(msg))
1.108     markus    716:                                goto fail;
1.38      markus    717:                }
1.161     djm       718:                /*
                    719:                 * If not in strict mode, add the key automatically to the
                    720:                 * local known_hosts file.
                    721:                 */
1.88      markus    722:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.161     djm       723:                        snprintf(hostline, sizeof(hostline), "%s,%s",
                    724:                            host, ip);
1.38      markus    725:                        hostp = hostline;
1.161     djm       726:                        if (options.hash_known_hosts) {
                    727:                                /* Add hash of host and IP separately */
                    728:                                r = add_host_to_hostfile(user_hostfile, host,
                    729:                                    host_key, options.hash_known_hosts) &&
                    730:                                    add_host_to_hostfile(user_hostfile, ip,
                    731:                                    host_key, options.hash_known_hosts);
                    732:                        } else {
                    733:                                /* Add unhashed "host,ip" */
                    734:                                r = add_host_to_hostfile(user_hostfile,
                    735:                                    hostline, host_key,
                    736:                                    options.hash_known_hosts);
                    737:                        }
                    738:                } else {
                    739:                        r = add_host_to_hostfile(user_hostfile, host, host_key,
                    740:                            options.hash_known_hosts);
1.38      markus    741:                        hostp = host;
1.161     djm       742:                }
1.38      markus    743:
1.161     djm       744:                if (!r)
1.138     itojun    745:                        logit("Failed to add the host to the list of known "
1.108     markus    746:                            "hosts (%.500s).", user_hostfile);
1.38      markus    747:                else
1.138     itojun    748:                        logit("Warning: Permanently added '%.200s' (%s) to the "
1.108     markus    749:                            "list of known hosts.", hostp, type);
1.38      markus    750:                break;
                    751:        case HOST_CHANGED:
1.197     dtucker   752:                if (readonly == ROQUIET)
                    753:                        goto fail;
1.38      markus    754:                if (options.check_host_ip && host_ip_differ) {
1.158     avsm      755:                        char *key_msg;
1.38      markus    756:                        if (ip_status == HOST_NEW)
1.158     avsm      757:                                key_msg = "is unknown";
1.38      markus    758:                        else if (ip_status == HOST_OK)
1.158     avsm      759:                                key_msg = "is unchanged";
1.38      markus    760:                        else
1.158     avsm      761:                                key_msg = "has a different value";
1.38      markus    762:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    763:                        error("@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @");
                    764:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72      markus    765:                        error("The %s host key for %s has changed,", type, host);
1.38      markus    766:                        error("and the key for the according IP address %s", ip);
1.158     avsm      767:                        error("%s. This could either mean that", key_msg);
1.38      markus    768:                        error("DNS SPOOFING is happening or the IP address for the host");
1.85      markus    769:                        error("and its host key have changed at the same time.");
1.88      markus    770:                        if (ip_status != HOST_NEW)
1.85      markus    771:                                error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38      markus    772:                }
                    773:                /* The host key has changed. */
1.150     jakob     774:                warn_changed_key(host_key);
1.38      markus    775:                error("Add correct host key in %.100s to get rid of this message.",
1.87      markus    776:                    user_hostfile);
1.85      markus    777:                error("Offending key in %s:%d", host_file, host_line);
1.38      markus    778:
1.40      markus    779:                /*
                    780:                 * If strict host key checking is in use, the user will have
                    781:                 * to edit the key manually and we can only abort.
                    782:                 */
1.108     markus    783:                if (options.strict_host_key_checking) {
                    784:                        error("%s host key for %.200s has changed and you have "
                    785:                            "requested strict checking.", type, host);
                    786:                        goto fail;
                    787:                }
1.38      markus    788:
1.40      markus    789:                /*
                    790:                 * If strict host key checking has not been requested, allow
1.144     djm       791:                 * the connection but without MITM-able authentication or
1.194     stevesk   792:                 * forwarding.
1.40      markus    793:                 */
1.38      markus    794:                if (options.password_authentication) {
1.108     markus    795:                        error("Password authentication is disabled to avoid "
                    796:                            "man-in-the-middle attacks.");
1.38      markus    797:                        options.password_authentication = 0;
1.144     djm       798:                }
                    799:                if (options.kbd_interactive_authentication) {
                    800:                        error("Keyboard-interactive authentication is disabled"
                    801:                            " to avoid man-in-the-middle attacks.");
                    802:                        options.kbd_interactive_authentication = 0;
                    803:                        options.challenge_response_authentication = 0;
                    804:                }
                    805:                if (options.challenge_response_authentication) {
                    806:                        error("Challenge/response authentication is disabled"
                    807:                            " to avoid man-in-the-middle attacks.");
                    808:                        options.challenge_response_authentication = 0;
1.38      markus    809:                }
                    810:                if (options.forward_agent) {
1.108     markus    811:                        error("Agent forwarding is disabled to avoid "
                    812:                            "man-in-the-middle attacks.");
1.38      markus    813:                        options.forward_agent = 0;
1.83      markus    814:                }
                    815:                if (options.forward_x11) {
1.108     markus    816:                        error("X11 forwarding is disabled to avoid "
                    817:                            "man-in-the-middle attacks.");
1.83      markus    818:                        options.forward_x11 = 0;
                    819:                }
1.108     markus    820:                if (options.num_local_forwards > 0 ||
                    821:                    options.num_remote_forwards > 0) {
                    822:                        error("Port forwarding is disabled to avoid "
                    823:                            "man-in-the-middle attacks.");
                    824:                        options.num_local_forwards =
1.118     deraadt   825:                            options.num_remote_forwards = 0;
1.194     stevesk   826:                }
                    827:                if (options.tun_open != SSH_TUNMODE_NO) {
                    828:                        error("Tunnel forwarding is disabled to avoid "
                    829:                            "man-in-the-middle attacks.");
                    830:                        options.tun_open = SSH_TUNMODE_NO;
1.38      markus    831:                }
1.40      markus    832:                /*
                    833:                 * XXX Should permit the user to change to use the new id.
                    834:                 * This could be done by converting the host key to an
                    835:                 * identifying sentence, tell that the host identifies itself
                    836:                 * by that sentence, and ask the user if he/she whishes to
                    837:                 * accept the authentication.
                    838:                 */
1.38      markus    839:                break;
1.132     markus    840:        case HOST_FOUND:
                    841:                fatal("internal error");
                    842:                break;
1.88      markus    843:        }
                    844:
                    845:        if (options.check_host_ip && host_status != HOST_CHANGED &&
                    846:            ip_status == HOST_CHANGED) {
1.119     markus    847:                snprintf(msg, sizeof(msg),
                    848:                    "Warning: the %s host key for '%.200s' "
                    849:                    "differs from the key for the IP address '%.128s'"
                    850:                    "\nOffending key for IP in %s:%d",
                    851:                    type, host, ip, ip_file, ip_line);
                    852:                if (host_status == HOST_OK) {
                    853:                        len = strlen(msg);
                    854:                        snprintf(msg + len, sizeof(msg) - len,
                    855:                            "\nMatching host key in %s:%d",
1.125     deraadt   856:                            host_file, host_line);
1.119     markus    857:                }
1.88      markus    858:                if (options.strict_host_key_checking == 1) {
1.143     djm       859:                        logit("%s", msg);
1.108     markus    860:                        error("Exiting, you have requested strict checking.");
                    861:                        goto fail;
1.88      markus    862:                } else if (options.strict_host_key_checking == 2) {
1.119     markus    863:                        strlcat(msg, "\nAre you sure you want "
                    864:                            "to continue connecting (yes/no)? ", sizeof(msg));
                    865:                        if (!confirm(msg))
1.108     markus    866:                                goto fail;
1.119     markus    867:                } else {
1.143     djm       868:                        logit("%s", msg);
1.88      markus    869:                }
1.38      markus    870:        }
1.82      provos    871:
                    872:        xfree(ip);
1.189     dtucker   873:        xfree(host);
1.108     markus    874:        return 0;
                    875:
                    876: fail:
                    877:        xfree(ip);
1.189     dtucker   878:        xfree(host);
1.108     markus    879:        return -1;
                    880: }
                    881:
1.140     jakob     882: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108     markus    883: int
                    884: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
                    885: {
                    886:        struct stat st;
1.153     jakob     887:        int flags = 0;
1.140     jakob     888:
1.153     jakob     889:        if (options.verify_host_key_dns &&
                    890:            verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
                    891:
                    892:                if (flags & DNS_VERIFY_FOUND) {
                    893:
                    894:                        if (options.verify_host_key_dns == 1 &&
                    895:                            flags & DNS_VERIFY_MATCH &&
                    896:                            flags & DNS_VERIFY_SECURE)
                    897:                                return 0;
                    898:
                    899:                        if (flags & DNS_VERIFY_MATCH) {
                    900:                                matching_host_key_dns = 1;
                    901:                        } else {
                    902:                                warn_changed_key(host_key);
                    903:                                error("Update the SSHFP RR in DNS with the new "
                    904:                                    "host key to get rid of this message.");
                    905:                        }
1.140     jakob     906:                }
                    907:        }
1.108     markus    908:
                    909:        /* return ok if the key can be found in an old keyfile */
                    910:        if (stat(options.system_hostfile2, &st) == 0 ||
                    911:            stat(options.user_hostfile2, &st) == 0) {
1.197     dtucker   912:                if (check_host_key(host, hostaddr, options.port, host_key,
                    913:                    RDONLY, options.user_hostfile2,
                    914:                    options.system_hostfile2) == 0)
1.108     markus    915:                        return 0;
                    916:        }
1.197     dtucker   917:        return check_host_key(host, hostaddr, options.port, host_key,
                    918:            RDRW, options.user_hostfile, options.system_hostfile);
1.51      markus    919: }
1.70      markus    920:
1.51      markus    921: /*
                    922:  * Starts a dialog with the server, and authenticates the current user on the
                    923:  * server.  This does not need any extra privileges.  The basic connection
                    924:  * to the server must already have been established before this is called.
                    925:  * If login fails, this function prints an error and never returns.
                    926:  * This function does not require super-user privileges.
                    927:  */
                    928: void
1.120     markus    929: ssh_login(Sensitive *sensitive, const char *orighost,
1.103     markus    930:     struct sockaddr *hostaddr, struct passwd *pw)
1.51      markus    931: {
                    932:        char *host, *cp;
1.70      markus    933:        char *server_user, *local_user;
                    934:
                    935:        local_user = xstrdup(pw->pw_name);
                    936:        server_user = options.user ? options.user : local_user;
1.51      markus    937:
                    938:        /* Convert the user-supplied hostname into all lowercase. */
                    939:        host = xstrdup(orighost);
                    940:        for (cp = host; *cp; cp++)
                    941:                if (isupper(*cp))
1.178     deraadt   942:                        *cp = (char)tolower(*cp);
1.51      markus    943:
                    944:        /* Exchange protocol version identification strings with the server. */
                    945:        ssh_exchange_identification();
                    946:
                    947:        /* Put the connection into non-blocking mode. */
                    948:        packet_set_nonblocking();
                    949:
                    950:        /* key exchange */
                    951:        /* authenticate user */
1.59      markus    952:        if (compat20) {
                    953:                ssh_kex2(host, hostaddr);
1.120     markus    954:                ssh_userauth2(local_user, server_user, host, sensitive);
1.59      markus    955:        } else {
                    956:                ssh_kex(host, hostaddr);
1.120     markus    957:                ssh_userauth1(local_user, server_user, host, sensitive);
1.59      markus    958:        }
1.182     markus    959:        xfree(local_user);
1.97      markus    960: }
                    961:
                    962: void
                    963: ssh_put_password(char *password)
                    964: {
                    965:        int size;
                    966:        char *padded;
                    967:
1.99      deraadt   968:        if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107     markus    969:                packet_put_cstring(password);
1.99      deraadt   970:                return;
                    971:        }
1.97      markus    972:        size = roundup(strlen(password) + 1, 32);
1.179     djm       973:        padded = xcalloc(1, size);
1.97      markus    974:        strlcpy(padded, password, size);
                    975:        packet_put_string(padded, size);
                    976:        memset(padded, 0, size);
                    977:        xfree(padded);
1.132     markus    978: }
                    979:
                    980: static int
                    981: show_key_from_file(const char *file, const char *host, int keytype)
                    982: {
                    983:        Key *found;
                    984:        char *fp;
                    985:        int line, ret;
                    986:
                    987:        found = key_new(keytype);
                    988:        if ((ret = lookup_key_in_hostfile_by_type(file, host,
                    989:            keytype, found, &line))) {
                    990:                fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.138     itojun    991:                logit("WARNING: %s key found for host %s\n"
1.133     markus    992:                    "in %s:%d\n"
1.132     markus    993:                    "%s key fingerprint %s.",
                    994:                    key_type(found), host, file, line,
                    995:                    key_type(found), fp);
                    996:                xfree(fp);
                    997:        }
                    998:        key_free(found);
                    999:        return (ret);
                   1000: }
                   1001:
                   1002: /* print all known host keys for a given host, but skip keys of given type */
                   1003: static int
                   1004: show_other_keys(const char *host, Key *key)
                   1005: {
                   1006:        int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
                   1007:        int i, found = 0;
                   1008:
                   1009:        for (i = 0; type[i] != -1; i++) {
                   1010:                if (type[i] == key->type)
                   1011:                        continue;
                   1012:                if (type[i] != KEY_RSA1 &&
                   1013:                    show_key_from_file(options.user_hostfile2, host, type[i])) {
                   1014:                        found = 1;
                   1015:                        continue;
                   1016:                }
                   1017:                if (type[i] != KEY_RSA1 &&
                   1018:                    show_key_from_file(options.system_hostfile2, host, type[i])) {
                   1019:                        found = 1;
                   1020:                        continue;
                   1021:                }
                   1022:                if (show_key_from_file(options.user_hostfile, host, type[i])) {
                   1023:                        found = 1;
                   1024:                        continue;
                   1025:                }
                   1026:                if (show_key_from_file(options.system_hostfile, host, type[i])) {
                   1027:                        found = 1;
                   1028:                        continue;
                   1029:                }
                   1030:                debug2("no key of type %d for host %s", type[i], host);
                   1031:        }
                   1032:        return (found);
1.150     jakob    1033: }
                   1034:
                   1035: static void
                   1036: warn_changed_key(Key *host_key)
                   1037: {
                   1038:        char *fp;
1.151     jakob    1039:        const char *type = key_type(host_key);
1.150     jakob    1040:
                   1041:        fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
                   1042:
                   1043:        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1044:        error("@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @");
                   1045:        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1046:        error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
                   1047:        error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
                   1048:        error("It is also possible that the %s host key has just been changed.", type);
                   1049:        error("The fingerprint for the %s key sent by the remote host is\n%s.",
                   1050:            type, fp);
                   1051:        error("Please contact your system administrator.");
                   1052:
                   1053:        xfree(fp);
1.171     reyk     1054: }
                   1055:
                   1056: /*
                   1057:  * Execute a local command
                   1058:  */
                   1059: int
                   1060: ssh_local_cmd(const char *args)
                   1061: {
                   1062:        char *shell;
                   1063:        pid_t pid;
                   1064:        int status;
                   1065:
                   1066:        if (!options.permit_local_command ||
                   1067:            args == NULL || !*args)
                   1068:                return (1);
                   1069:
                   1070:        if ((shell = getenv("SHELL")) == NULL)
                   1071:                shell = _PATH_BSHELL;
                   1072:
                   1073:        pid = fork();
                   1074:        if (pid == 0) {
                   1075:                debug3("Executing %s -c \"%s\"", shell, args);
                   1076:                execl(shell, shell, "-c", args, (char *)NULL);
                   1077:                error("Couldn't execute %s -c \"%s\": %s",
                   1078:                    shell, args, strerror(errno));
                   1079:                _exit(1);
                   1080:        } else if (pid == -1)
                   1081:                fatal("fork failed: %.100s", strerror(errno));
                   1082:        while (waitpid(pid, &status, 0) == -1)
                   1083:                if (errno != EINTR)
                   1084:                        fatal("Couldn't wait for child: %s", strerror(errno));
                   1085:
                   1086:        if (!WIFEXITED(status))
                   1087:                return (1);
                   1088:
                   1089:        return (WEXITSTATUS(status));
1.1       deraadt  1090: }