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

1.196   ! stevesk     1: /* $OpenBSD: sshconnect.c,v 1.195 2006/07/25 02:59:21 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:
                     16: #include "includes.h"
1.174     stevesk    17:
                     18: #include <sys/types.h>
                     19: #include <sys/wait.h>
1.175     stevesk    20: #include <sys/stat.h>
1.187     stevesk    21: #include <sys/socket.h>
1.195     stevesk    22: #include <sys/time.h>
1.187     stevesk    23:
                     24: #include <netinet/in.h>
1.172     stevesk    25:
1.176     stevesk    26: #include <ctype.h>
1.190     stevesk    27: #include <errno.h>
1.191     stevesk    28: #include <netdb.h>
1.172     stevesk    29: #include <paths.h>
1.188     stevesk    30: #include <pwd.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.91      markus     35: #include "ssh.h"
1.1       deraadt    36: #include "xmalloc.h"
                     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.109     itojun    516: static int
1.189     dtucker   517: check_host_key(char *hostname, struct sockaddr *hostaddr, Key *host_key,
1.108     markus    518:     int readonly, const char *user_hostfile, const char *system_hostfile)
1.1       deraadt   519: {
1.58      markus    520:        Key *file_key;
1.152     jakob     521:        const char *type = key_type(host_key);
1.189     dtucker   522:        char *ip = NULL, *host = NULL;
1.100     markus    523:        char hostline[1000], *hostp, *fp;
1.38      markus    524:        HostStatus host_status;
                    525:        HostStatus ip_status;
1.161     djm       526:        int r, local = 0, host_ip_differ = 0;
1.49      markus    527:        char ntop[NI_MAXHOST];
1.119     markus    528:        char msg[1024];
1.145     jakob     529:        int len, host_line, ip_line;
1.85      markus    530:        const char *host_file = NULL, *ip_file = NULL;
1.49      markus    531:
                    532:        /*
                    533:         * Force accepting of the host key for loopback/localhost. The
                    534:         * problem is that if the home directory is NFS-mounted to multiple
                    535:         * machines, localhost will refer to a different machine in each of
                    536:         * them, and the user will get bogus HOST_CHANGED warnings.  This
                    537:         * essentially disables host authentication for localhost; however,
                    538:         * this is probably not a real problem.
                    539:         */
1.70      markus    540:        /**  hostaddr == 0! */
1.49      markus    541:        switch (hostaddr->sa_family) {
                    542:        case AF_INET:
1.108     markus    543:                local = (ntohl(((struct sockaddr_in *)hostaddr)->
1.168     djm       544:                    sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49      markus    545:                break;
                    546:        case AF_INET6:
1.108     markus    547:                local = IN6_IS_ADDR_LOOPBACK(
                    548:                    &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49      markus    549:                break;
                    550:        default:
                    551:                local = 0;
                    552:                break;
                    553:        }
1.111     markus    554:        if (options.no_host_authentication_for_localhost == 1 && local &&
                    555:            options.host_key_alias == NULL) {
1.88      markus    556:                debug("Forcing accepting of host key for "
                    557:                    "loopback/localhost.");
1.108     markus    558:                return 0;
1.49      markus    559:        }
1.42      markus    560:
                    561:        /*
1.88      markus    562:         * We don't have the remote ip-address for connections
                    563:         * using a proxy command
1.42      markus    564:         */
1.84      markus    565:        if (options.proxy_command == NULL) {
                    566:                if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86      markus    567:                    NULL, 0, NI_NUMERICHOST) != 0)
1.84      markus    568:                        fatal("check_host_key: getnameinfo failed");
1.189     dtucker   569:                ip = put_host_port(ntop, options.port);
1.84      markus    570:        } else {
                    571:                ip = xstrdup("<no hostip for proxy command>");
1.86      markus    572:        }
1.88      markus    573:        /*
                    574:         * Turn off check_host_ip if the connection is to localhost, via proxy
                    575:         * command or if we don't have a hostname to compare with
                    576:         */
1.189     dtucker   577:        if (options.check_host_ip && (local ||
                    578:            strcmp(hostname, ip) == 0 || options.proxy_command != NULL))
1.88      markus    579:                options.check_host_ip = 0;
1.86      markus    580:
                    581:        /*
1.189     dtucker   582:         * Allow the user to record the key under a different name or
                    583:         * differentiate a non-standard port.  This is useful for ssh
                    584:         * tunneling over forwarded connections or if you run multiple
                    585:         * sshd's on different ports on the same machine.
1.86      markus    586:         */
                    587:        if (options.host_key_alias != NULL) {
1.189     dtucker   588:                host = xstrdup(options.host_key_alias);
1.86      markus    589:                debug("using hostkeyalias: %s", host);
1.189     dtucker   590:        } else {
                    591:                host = put_host_port(hostname, options.port);
1.84      markus    592:        }
1.38      markus    593:
1.46      markus    594:        /*
                    595:         * Store the host key from the known host file in here so that we can
                    596:         * compare it with the key for the IP address.
                    597:         */
1.58      markus    598:        file_key = key_new(host_key->type);
1.38      markus    599:
1.40      markus    600:        /*
1.170     djm       601:         * Check if the host key is present in the user's list of known
1.40      markus    602:         * hosts or in the systemwide list.
                    603:         */
1.85      markus    604:        host_file = user_hostfile;
1.108     markus    605:        host_status = check_host_in_hostfile(host_file, host, host_key,
1.118     deraadt   606:            file_key, &host_line);
1.85      markus    607:        if (host_status == HOST_NEW) {
                    608:                host_file = system_hostfile;
1.108     markus    609:                host_status = check_host_in_hostfile(host_file, host, host_key,
                    610:                    file_key, &host_line);
1.85      markus    611:        }
1.40      markus    612:        /*
                    613:         * Also perform check for the ip address, skip the check if we are
                    614:         * localhost or the hostname was an ip address to begin with
                    615:         */
1.88      markus    616:        if (options.check_host_ip) {
1.58      markus    617:                Key *ip_key = key_new(host_key->type);
1.38      markus    618:
1.85      markus    619:                ip_file = user_hostfile;
1.108     markus    620:                ip_status = check_host_in_hostfile(ip_file, ip, host_key,
                    621:                    ip_key, &ip_line);
1.85      markus    622:                if (ip_status == HOST_NEW) {
                    623:                        ip_file = system_hostfile;
1.108     markus    624:                        ip_status = check_host_in_hostfile(ip_file, ip,
                    625:                            host_key, ip_key, &ip_line);
1.85      markus    626:                }
1.38      markus    627:                if (host_status == HOST_CHANGED &&
1.58      markus    628:                    (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38      markus    629:                        host_ip_differ = 1;
                    630:
1.58      markus    631:                key_free(ip_key);
1.38      markus    632:        } else
                    633:                ip_status = host_status;
                    634:
1.58      markus    635:        key_free(file_key);
1.38      markus    636:
                    637:        switch (host_status) {
                    638:        case HOST_OK:
                    639:                /* The host is known and the key matches. */
1.72      markus    640:                debug("Host '%.200s' is known and matches the %s host key.",
                    641:                    host, type);
1.85      markus    642:                debug("Found key in %s:%d", host_file, host_line);
1.88      markus    643:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.108     markus    644:                        if (readonly)
1.138     itojun    645:                                logit("%s host key for IP address "
1.108     markus    646:                                    "'%.128s' not in list of known hosts.",
                    647:                                    type, ip);
                    648:                        else if (!add_host_to_hostfile(user_hostfile, ip,
1.160     djm       649:                            host_key, options.hash_known_hosts))
1.138     itojun    650:                                logit("Failed to add the %s host key for IP "
1.108     markus    651:                                    "address '%.128s' to the list of known "
                    652:                                    "hosts (%.30s).", type, ip, user_hostfile);
1.88      markus    653:                        else
1.138     itojun    654:                                logit("Warning: Permanently added the %s host "
1.108     markus    655:                                    "key for IP address '%.128s' to the list "
                    656:                                    "of known hosts.", type, ip);
1.38      markus    657:                }
                    658:                break;
                    659:        case HOST_NEW:
1.108     markus    660:                if (readonly)
                    661:                        goto fail;
1.38      markus    662:                /* The host is new. */
                    663:                if (options.strict_host_key_checking == 1) {
1.108     markus    664:                        /*
                    665:                         * User has requested strict host key checking.  We
                    666:                         * will not add the host key automatically.  The only
                    667:                         * alternative left is to abort.
                    668:                         */
                    669:                        error("No %s host key is known for %.200s and you "
                    670:                            "have requested strict checking.", type, host);
                    671:                        goto fail;
1.38      markus    672:                } else if (options.strict_host_key_checking == 2) {
1.145     jakob     673:                        char msg1[1024], msg2[1024];
                    674:
                    675:                        if (show_other_keys(host, host_key))
                    676:                                snprintf(msg1, sizeof(msg1),
1.168     djm       677:                                    "\nbut keys of different type are already"
                    678:                                    " known for this host.");
1.145     jakob     679:                        else
                    680:                                snprintf(msg1, sizeof(msg1), ".");
1.38      markus    681:                        /* The default */
1.100     markus    682:                        fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.145     jakob     683:                        msg2[0] = '\0';
                    684:                        if (options.verify_host_key_dns) {
1.153     jakob     685:                                if (matching_host_key_dns)
1.145     jakob     686:                                        snprintf(msg2, sizeof(msg2),
                    687:                                            "Matching host key fingerprint"
                    688:                                            " found in DNS.\n");
                    689:                                else
                    690:                                        snprintf(msg2, sizeof(msg2),
                    691:                                            "No matching host key fingerprint"
                    692:                                            " found in DNS.\n");
                    693:                        }
1.119     markus    694:                        snprintf(msg, sizeof(msg),
1.108     markus    695:                            "The authenticity of host '%.200s (%s)' can't be "
1.132     markus    696:                            "established%s\n"
1.145     jakob     697:                            "%s key fingerprint is %s.\n%s"
1.108     markus    698:                            "Are you sure you want to continue connecting "
1.132     markus    699:                            "(yes/no)? ",
1.145     jakob     700:                            host, ip, msg1, type, fp, msg2);
1.100     markus    701:                        xfree(fp);
1.119     markus    702:                        if (!confirm(msg))
1.108     markus    703:                                goto fail;
1.38      markus    704:                }
1.161     djm       705:                /*
                    706:                 * If not in strict mode, add the key automatically to the
                    707:                 * local known_hosts file.
                    708:                 */
1.88      markus    709:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.161     djm       710:                        snprintf(hostline, sizeof(hostline), "%s,%s",
                    711:                            host, ip);
1.38      markus    712:                        hostp = hostline;
1.161     djm       713:                        if (options.hash_known_hosts) {
                    714:                                /* Add hash of host and IP separately */
                    715:                                r = add_host_to_hostfile(user_hostfile, host,
                    716:                                    host_key, options.hash_known_hosts) &&
                    717:                                    add_host_to_hostfile(user_hostfile, ip,
                    718:                                    host_key, options.hash_known_hosts);
                    719:                        } else {
                    720:                                /* Add unhashed "host,ip" */
                    721:                                r = add_host_to_hostfile(user_hostfile,
                    722:                                    hostline, host_key,
                    723:                                    options.hash_known_hosts);
                    724:                        }
                    725:                } else {
                    726:                        r = add_host_to_hostfile(user_hostfile, host, host_key,
                    727:                            options.hash_known_hosts);
1.38      markus    728:                        hostp = host;
1.161     djm       729:                }
1.38      markus    730:
1.161     djm       731:                if (!r)
1.138     itojun    732:                        logit("Failed to add the host to the list of known "
1.108     markus    733:                            "hosts (%.500s).", user_hostfile);
1.38      markus    734:                else
1.138     itojun    735:                        logit("Warning: Permanently added '%.200s' (%s) to the "
1.108     markus    736:                            "list of known hosts.", hostp, type);
1.38      markus    737:                break;
                    738:        case HOST_CHANGED:
                    739:                if (options.check_host_ip && host_ip_differ) {
1.158     avsm      740:                        char *key_msg;
1.38      markus    741:                        if (ip_status == HOST_NEW)
1.158     avsm      742:                                key_msg = "is unknown";
1.38      markus    743:                        else if (ip_status == HOST_OK)
1.158     avsm      744:                                key_msg = "is unchanged";
1.38      markus    745:                        else
1.158     avsm      746:                                key_msg = "has a different value";
1.38      markus    747:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    748:                        error("@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @");
                    749:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72      markus    750:                        error("The %s host key for %s has changed,", type, host);
1.38      markus    751:                        error("and the key for the according IP address %s", ip);
1.158     avsm      752:                        error("%s. This could either mean that", key_msg);
1.38      markus    753:                        error("DNS SPOOFING is happening or the IP address for the host");
1.85      markus    754:                        error("and its host key have changed at the same time.");
1.88      markus    755:                        if (ip_status != HOST_NEW)
1.85      markus    756:                                error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38      markus    757:                }
                    758:                /* The host key has changed. */
1.150     jakob     759:                warn_changed_key(host_key);
1.38      markus    760:                error("Add correct host key in %.100s to get rid of this message.",
1.87      markus    761:                    user_hostfile);
1.85      markus    762:                error("Offending key in %s:%d", host_file, host_line);
1.38      markus    763:
1.40      markus    764:                /*
                    765:                 * If strict host key checking is in use, the user will have
                    766:                 * to edit the key manually and we can only abort.
                    767:                 */
1.108     markus    768:                if (options.strict_host_key_checking) {
                    769:                        error("%s host key for %.200s has changed and you have "
                    770:                            "requested strict checking.", type, host);
                    771:                        goto fail;
                    772:                }
1.38      markus    773:
1.40      markus    774:                /*
                    775:                 * If strict host key checking has not been requested, allow
1.144     djm       776:                 * the connection but without MITM-able authentication or
1.194     stevesk   777:                 * forwarding.
1.40      markus    778:                 */
1.38      markus    779:                if (options.password_authentication) {
1.108     markus    780:                        error("Password authentication is disabled to avoid "
                    781:                            "man-in-the-middle attacks.");
1.38      markus    782:                        options.password_authentication = 0;
1.144     djm       783:                }
                    784:                if (options.kbd_interactive_authentication) {
                    785:                        error("Keyboard-interactive authentication is disabled"
                    786:                            " to avoid man-in-the-middle attacks.");
                    787:                        options.kbd_interactive_authentication = 0;
                    788:                        options.challenge_response_authentication = 0;
                    789:                }
                    790:                if (options.challenge_response_authentication) {
                    791:                        error("Challenge/response authentication is disabled"
                    792:                            " to avoid man-in-the-middle attacks.");
                    793:                        options.challenge_response_authentication = 0;
1.38      markus    794:                }
                    795:                if (options.forward_agent) {
1.108     markus    796:                        error("Agent forwarding is disabled to avoid "
                    797:                            "man-in-the-middle attacks.");
1.38      markus    798:                        options.forward_agent = 0;
1.83      markus    799:                }
                    800:                if (options.forward_x11) {
1.108     markus    801:                        error("X11 forwarding is disabled to avoid "
                    802:                            "man-in-the-middle attacks.");
1.83      markus    803:                        options.forward_x11 = 0;
                    804:                }
1.108     markus    805:                if (options.num_local_forwards > 0 ||
                    806:                    options.num_remote_forwards > 0) {
                    807:                        error("Port forwarding is disabled to avoid "
                    808:                            "man-in-the-middle attacks.");
                    809:                        options.num_local_forwards =
1.118     deraadt   810:                            options.num_remote_forwards = 0;
1.194     stevesk   811:                }
                    812:                if (options.tun_open != SSH_TUNMODE_NO) {
                    813:                        error("Tunnel forwarding is disabled to avoid "
                    814:                            "man-in-the-middle attacks.");
                    815:                        options.tun_open = SSH_TUNMODE_NO;
1.38      markus    816:                }
1.40      markus    817:                /*
                    818:                 * XXX Should permit the user to change to use the new id.
                    819:                 * This could be done by converting the host key to an
                    820:                 * identifying sentence, tell that the host identifies itself
                    821:                 * by that sentence, and ask the user if he/she whishes to
                    822:                 * accept the authentication.
                    823:                 */
1.38      markus    824:                break;
1.132     markus    825:        case HOST_FOUND:
                    826:                fatal("internal error");
                    827:                break;
1.88      markus    828:        }
                    829:
                    830:        if (options.check_host_ip && host_status != HOST_CHANGED &&
                    831:            ip_status == HOST_CHANGED) {
1.119     markus    832:                snprintf(msg, sizeof(msg),
                    833:                    "Warning: the %s host key for '%.200s' "
                    834:                    "differs from the key for the IP address '%.128s'"
                    835:                    "\nOffending key for IP in %s:%d",
                    836:                    type, host, ip, ip_file, ip_line);
                    837:                if (host_status == HOST_OK) {
                    838:                        len = strlen(msg);
                    839:                        snprintf(msg + len, sizeof(msg) - len,
                    840:                            "\nMatching host key in %s:%d",
1.125     deraadt   841:                            host_file, host_line);
1.119     markus    842:                }
1.88      markus    843:                if (options.strict_host_key_checking == 1) {
1.143     djm       844:                        logit("%s", msg);
1.108     markus    845:                        error("Exiting, you have requested strict checking.");
                    846:                        goto fail;
1.88      markus    847:                } else if (options.strict_host_key_checking == 2) {
1.119     markus    848:                        strlcat(msg, "\nAre you sure you want "
                    849:                            "to continue connecting (yes/no)? ", sizeof(msg));
                    850:                        if (!confirm(msg))
1.108     markus    851:                                goto fail;
1.119     markus    852:                } else {
1.143     djm       853:                        logit("%s", msg);
1.88      markus    854:                }
1.38      markus    855:        }
1.82      provos    856:
                    857:        xfree(ip);
1.189     dtucker   858:        xfree(host);
1.108     markus    859:        return 0;
                    860:
                    861: fail:
                    862:        xfree(ip);
1.189     dtucker   863:        xfree(host);
1.108     markus    864:        return -1;
                    865: }
                    866:
1.140     jakob     867: /* returns 0 if key verifies or -1 if key does NOT verify */
1.108     markus    868: int
                    869: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
                    870: {
                    871:        struct stat st;
1.153     jakob     872:        int flags = 0;
1.140     jakob     873:
1.153     jakob     874:        if (options.verify_host_key_dns &&
                    875:            verify_host_key_dns(host, hostaddr, host_key, &flags) == 0) {
                    876:
                    877:                if (flags & DNS_VERIFY_FOUND) {
                    878:
                    879:                        if (options.verify_host_key_dns == 1 &&
                    880:                            flags & DNS_VERIFY_MATCH &&
                    881:                            flags & DNS_VERIFY_SECURE)
                    882:                                return 0;
                    883:
                    884:                        if (flags & DNS_VERIFY_MATCH) {
                    885:                                matching_host_key_dns = 1;
                    886:                        } else {
                    887:                                warn_changed_key(host_key);
                    888:                                error("Update the SSHFP RR in DNS with the new "
                    889:                                    "host key to get rid of this message.");
                    890:                        }
1.140     jakob     891:                }
                    892:        }
1.108     markus    893:
                    894:        /* return ok if the key can be found in an old keyfile */
                    895:        if (stat(options.system_hostfile2, &st) == 0 ||
                    896:            stat(options.user_hostfile2, &st) == 0) {
                    897:                if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
                    898:                    options.user_hostfile2, options.system_hostfile2) == 0)
                    899:                        return 0;
                    900:        }
                    901:        return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
                    902:            options.user_hostfile, options.system_hostfile);
1.51      markus    903: }
1.70      markus    904:
1.51      markus    905: /*
                    906:  * Starts a dialog with the server, and authenticates the current user on the
                    907:  * server.  This does not need any extra privileges.  The basic connection
                    908:  * to the server must already have been established before this is called.
                    909:  * If login fails, this function prints an error and never returns.
                    910:  * This function does not require super-user privileges.
                    911:  */
                    912: void
1.120     markus    913: ssh_login(Sensitive *sensitive, const char *orighost,
1.103     markus    914:     struct sockaddr *hostaddr, struct passwd *pw)
1.51      markus    915: {
                    916:        char *host, *cp;
1.70      markus    917:        char *server_user, *local_user;
                    918:
                    919:        local_user = xstrdup(pw->pw_name);
                    920:        server_user = options.user ? options.user : local_user;
1.51      markus    921:
                    922:        /* Convert the user-supplied hostname into all lowercase. */
                    923:        host = xstrdup(orighost);
                    924:        for (cp = host; *cp; cp++)
                    925:                if (isupper(*cp))
1.178     deraadt   926:                        *cp = (char)tolower(*cp);
1.51      markus    927:
                    928:        /* Exchange protocol version identification strings with the server. */
                    929:        ssh_exchange_identification();
                    930:
                    931:        /* Put the connection into non-blocking mode. */
                    932:        packet_set_nonblocking();
                    933:
                    934:        /* key exchange */
                    935:        /* authenticate user */
1.59      markus    936:        if (compat20) {
                    937:                ssh_kex2(host, hostaddr);
1.120     markus    938:                ssh_userauth2(local_user, server_user, host, sensitive);
1.59      markus    939:        } else {
                    940:                ssh_kex(host, hostaddr);
1.120     markus    941:                ssh_userauth1(local_user, server_user, host, sensitive);
1.59      markus    942:        }
1.182     markus    943:        xfree(local_user);
1.97      markus    944: }
                    945:
                    946: void
                    947: ssh_put_password(char *password)
                    948: {
                    949:        int size;
                    950:        char *padded;
                    951:
1.99      deraadt   952:        if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107     markus    953:                packet_put_cstring(password);
1.99      deraadt   954:                return;
                    955:        }
1.97      markus    956:        size = roundup(strlen(password) + 1, 32);
1.179     djm       957:        padded = xcalloc(1, size);
1.97      markus    958:        strlcpy(padded, password, size);
                    959:        packet_put_string(padded, size);
                    960:        memset(padded, 0, size);
                    961:        xfree(padded);
1.132     markus    962: }
                    963:
                    964: static int
                    965: show_key_from_file(const char *file, const char *host, int keytype)
                    966: {
                    967:        Key *found;
                    968:        char *fp;
                    969:        int line, ret;
                    970:
                    971:        found = key_new(keytype);
                    972:        if ((ret = lookup_key_in_hostfile_by_type(file, host,
                    973:            keytype, found, &line))) {
                    974:                fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.138     itojun    975:                logit("WARNING: %s key found for host %s\n"
1.133     markus    976:                    "in %s:%d\n"
1.132     markus    977:                    "%s key fingerprint %s.",
                    978:                    key_type(found), host, file, line,
                    979:                    key_type(found), fp);
                    980:                xfree(fp);
                    981:        }
                    982:        key_free(found);
                    983:        return (ret);
                    984: }
                    985:
                    986: /* print all known host keys for a given host, but skip keys of given type */
                    987: static int
                    988: show_other_keys(const char *host, Key *key)
                    989: {
                    990:        int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
                    991:        int i, found = 0;
                    992:
                    993:        for (i = 0; type[i] != -1; i++) {
                    994:                if (type[i] == key->type)
                    995:                        continue;
                    996:                if (type[i] != KEY_RSA1 &&
                    997:                    show_key_from_file(options.user_hostfile2, host, type[i])) {
                    998:                        found = 1;
                    999:                        continue;
                   1000:                }
                   1001:                if (type[i] != KEY_RSA1 &&
                   1002:                    show_key_from_file(options.system_hostfile2, host, type[i])) {
                   1003:                        found = 1;
                   1004:                        continue;
                   1005:                }
                   1006:                if (show_key_from_file(options.user_hostfile, host, type[i])) {
                   1007:                        found = 1;
                   1008:                        continue;
                   1009:                }
                   1010:                if (show_key_from_file(options.system_hostfile, host, type[i])) {
                   1011:                        found = 1;
                   1012:                        continue;
                   1013:                }
                   1014:                debug2("no key of type %d for host %s", type[i], host);
                   1015:        }
                   1016:        return (found);
1.150     jakob    1017: }
                   1018:
                   1019: static void
                   1020: warn_changed_key(Key *host_key)
                   1021: {
                   1022:        char *fp;
1.151     jakob    1023:        const char *type = key_type(host_key);
1.150     jakob    1024:
                   1025:        fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
                   1026:
                   1027:        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1028:        error("@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @");
                   1029:        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1030:        error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
                   1031:        error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
                   1032:        error("It is also possible that the %s host key has just been changed.", type);
                   1033:        error("The fingerprint for the %s key sent by the remote host is\n%s.",
                   1034:            type, fp);
                   1035:        error("Please contact your system administrator.");
                   1036:
                   1037:        xfree(fp);
1.171     reyk     1038: }
                   1039:
                   1040: /*
                   1041:  * Execute a local command
                   1042:  */
                   1043: int
                   1044: ssh_local_cmd(const char *args)
                   1045: {
                   1046:        char *shell;
                   1047:        pid_t pid;
                   1048:        int status;
                   1049:
                   1050:        if (!options.permit_local_command ||
                   1051:            args == NULL || !*args)
                   1052:                return (1);
                   1053:
                   1054:        if ((shell = getenv("SHELL")) == NULL)
                   1055:                shell = _PATH_BSHELL;
                   1056:
                   1057:        pid = fork();
                   1058:        if (pid == 0) {
                   1059:                debug3("Executing %s -c \"%s\"", shell, args);
                   1060:                execl(shell, shell, "-c", args, (char *)NULL);
                   1061:                error("Couldn't execute %s -c \"%s\": %s",
                   1062:                    shell, args, strerror(errno));
                   1063:                _exit(1);
                   1064:        } else if (pid == -1)
                   1065:                fatal("fork failed: %.100s", strerror(errno));
                   1066:        while (waitpid(pid, &status, 0) == -1)
                   1067:                if (errno != EINTR)
                   1068:                        fatal("Couldn't wait for child: %s", strerror(errno));
                   1069:
                   1070:        if (!WIFEXITED(status))
                   1071:                return (1);
                   1072:
                   1073:        return (WEXITSTATUS(status));
1.1       deraadt  1074: }