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

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