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

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