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

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.139   ! markus     16: RCSID("$OpenBSD: sshconnect.c,v 1.138 2003/04/08 20:21:29 itojun 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.119     markus     34: #include "readpass.h"
1.51      markus     35:
1.70      markus     36: char *client_version_string = NULL;
                     37: char *server_version_string = NULL;
1.59      markus     38:
1.124     markus     39: /* import */
1.43      markus     40: extern Options options;
1.50      markus     41: extern char *__progname;
1.124     markus     42: extern uid_t original_real_uid;
                     43: extern uid_t original_effective_uid;
1.135     djm        44: extern pid_t proxy_command_pid;
1.91      markus     45:
1.132     markus     46: static int show_other_keys(const char *, Key *);
                     47:
1.39      deraadt    48: /*
                     49:  * Connect to the given ssh server using a proxy command.
                     50:  */
1.109     itojun     51: static int
1.124     markus     52: ssh_proxy_connect(const char *host, u_short port, const char *proxy_command)
1.1       deraadt    53: {
1.38      markus     54:        Buffer command;
                     55:        const char *cp;
                     56:        char *command_string;
                     57:        int pin[2], pout[2];
1.69      deraadt    58:        pid_t pid;
1.49      markus     59:        char strport[NI_MAXSERV];
1.38      markus     60:
                     61:        /* Convert the port number into a string. */
1.49      markus     62:        snprintf(strport, sizeof strport, "%hu", port);
1.38      markus     63:
1.135     djm        64:        /*
                     65:         * Build the final command string in the buffer by making the
                     66:         * appropriate substitutions to the given proxy command.
                     67:         *
                     68:         * Use "exec" to avoid "sh -c" processes on some platforms
                     69:         * (e.g. Solaris)
                     70:         */
1.38      markus     71:        buffer_init(&command);
1.135     djm        72:        buffer_append(&command, "exec ", 5);
                     73:
1.38      markus     74:        for (cp = proxy_command; *cp; cp++) {
                     75:                if (cp[0] == '%' && cp[1] == '%') {
                     76:                        buffer_append(&command, "%", 1);
                     77:                        cp++;
                     78:                        continue;
                     79:                }
                     80:                if (cp[0] == '%' && cp[1] == 'h') {
                     81:                        buffer_append(&command, host, strlen(host));
                     82:                        cp++;
                     83:                        continue;
                     84:                }
                     85:                if (cp[0] == '%' && cp[1] == 'p') {
1.49      markus     86:                        buffer_append(&command, strport, strlen(strport));
1.38      markus     87:                        cp++;
                     88:                        continue;
                     89:                }
                     90:                buffer_append(&command, cp, 1);
                     91:        }
                     92:        buffer_append(&command, "\0", 1);
                     93:
                     94:        /* Get the final command string. */
                     95:        command_string = buffer_ptr(&command);
                     96:
                     97:        /* Create pipes for communicating with the proxy. */
                     98:        if (pipe(pin) < 0 || pipe(pout) < 0)
                     99:                fatal("Could not create pipes to communicate with the proxy: %.100s",
1.118     deraadt   100:                    strerror(errno));
1.38      markus    101:
                    102:        debug("Executing proxy command: %.500s", command_string);
                    103:
                    104:        /* Fork and execute the proxy command. */
                    105:        if ((pid = fork()) == 0) {
                    106:                char *argv[10];
                    107:
                    108:                /* Child.  Permanently give up superuser privileges. */
1.124     markus    109:                seteuid(original_real_uid);
                    110:                setuid(original_real_uid);
1.38      markus    111:
                    112:                /* Redirect stdin and stdout. */
                    113:                close(pin[1]);
                    114:                if (pin[0] != 0) {
                    115:                        if (dup2(pin[0], 0) < 0)
                    116:                                perror("dup2 stdin");
                    117:                        close(pin[0]);
                    118:                }
                    119:                close(pout[0]);
                    120:                if (dup2(pout[1], 1) < 0)
                    121:                        perror("dup2 stdout");
                    122:                /* Cannot be 1 because pin allocated two descriptors. */
                    123:                close(pout[1]);
                    124:
                    125:                /* Stderr is left as it is so that error messages get
                    126:                   printed on the user's terminal. */
1.89      markus    127:                argv[0] = _PATH_BSHELL;
1.38      markus    128:                argv[1] = "-c";
                    129:                argv[2] = command_string;
                    130:                argv[3] = NULL;
                    131:
                    132:                /* Execute the proxy command.  Note that we gave up any
                    133:                   extra privileges above. */
1.89      markus    134:                execv(argv[0], argv);
                    135:                perror(argv[0]);
1.38      markus    136:                exit(1);
                    137:        }
                    138:        /* Parent. */
                    139:        if (pid < 0)
                    140:                fatal("fork failed: %.100s", strerror(errno));
1.135     djm       141:        else
                    142:                proxy_command_pid = pid; /* save pid to clean up later */
1.38      markus    143:
                    144:        /* Close child side of the descriptors. */
                    145:        close(pin[0]);
                    146:        close(pout[1]);
                    147:
                    148:        /* Free the command name. */
                    149:        buffer_free(&command);
                    150:
                    151:        /* Set the connection file descriptors. */
                    152:        packet_set_connection(pout[0], pin[1]);
1.1       deraadt   153:
1.110     markus    154:        /* Indicate OK return */
                    155:        return 0;
1.1       deraadt   156: }
                    157:
1.39      deraadt   158: /*
                    159:  * Creates a (possibly privileged) socket for use as the ssh connection.
                    160:  */
1.109     itojun    161: static int
1.139   ! markus    162: ssh_create_socket(int privileged, struct addrinfo *ai)
1.1       deraadt   163: {
1.105     markus    164:        int sock, gaierr;
                    165:        struct addrinfo hints, *res;
1.1       deraadt   166:
1.40      markus    167:        /*
                    168:         * If we are running as root and want to connect to a privileged
                    169:         * port, bind our own socket to a privileged port.
                    170:         */
1.38      markus    171:        if (privileged) {
                    172:                int p = IPPORT_RESERVED - 1;
1.124     markus    173:                PRIV_START;
1.139   ! markus    174:                sock = rresvport_af(&p, ai->ai_family);
1.124     markus    175:                PRIV_END;
1.38      markus    176:                if (sock < 0)
1.139   ! markus    177:                        error("rresvport: af=%d %.100s", ai->ai_family,
        !           178:                            strerror(errno));
1.55      markus    179:                else
                    180:                        debug("Allocated local port %d.", p);
1.105     markus    181:                return sock;
                    182:        }
1.139   ! markus    183:        sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.105     markus    184:        if (sock < 0)
                    185:                error("socket: %.100s", strerror(errno));
                    186:
                    187:        /* Bind the socket to an alternative local IP address */
                    188:        if (options.bind_address == NULL)
                    189:                return sock;
                    190:
                    191:        memset(&hints, 0, sizeof(hints));
1.139   ! markus    192:        hints.ai_family = ai->ai_family;
        !           193:        hints.ai_socktype = ai->ai_socktype;
        !           194:        hints.ai_protocol = ai->ai_protocol;
1.105     markus    195:        hints.ai_flags = AI_PASSIVE;
                    196:        gaierr = getaddrinfo(options.bind_address, "0", &hints, &res);
                    197:        if (gaierr) {
                    198:                error("getaddrinfo: %s: %s", options.bind_address,
                    199:                    gai_strerror(gaierr));
                    200:                close(sock);
                    201:                return -1;
                    202:        }
                    203:        if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
                    204:                error("bind: %s: %s", options.bind_address, strerror(errno));
                    205:                close(sock);
                    206:                freeaddrinfo(res);
                    207:                return -1;
1.38      markus    208:        }
1.105     markus    209:        freeaddrinfo(res);
1.38      markus    210:        return sock;
1.1       deraadt   211: }
                    212:
1.39      deraadt   213: /*
1.49      markus    214:  * Opens a TCP/IP connection to the remote server on the given host.
                    215:  * The address of the remote host will be returned in hostaddr.
1.124     markus    216:  * If port is 0, the default port will be used.  If needpriv is true,
1.39      deraadt   217:  * a privileged port will be allocated to make the connection.
1.124     markus    218:  * This requires super-user privileges if needpriv is true.
1.39      deraadt   219:  * Connection_attempts specifies the maximum number of tries (one per
                    220:  * second).  If proxy_command is non-NULL, it specifies the command (with %h
                    221:  * and %p substituted for host and port, respectively) to use to contact
                    222:  * the daemon.
1.110     markus    223:  * Return values:
                    224:  *    0 for OK
                    225:  *    ECONNREFUSED if we got a "Connection Refused" by the peer on any address
                    226:  *    ECONNABORTED if we failed without a "Connection refused"
                    227:  * Suitable error messages for the connection failure will already have been
                    228:  * printed.
1.39      deraadt   229:  */
1.38      markus    230: int
1.49      markus    231: ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
1.115     markus    232:     u_short port, int family, int connection_attempts,
1.124     markus    233:     int needpriv, const char *proxy_command)
1.1       deraadt   234: {
1.90      markus    235:        int gaierr;
                    236:        int on = 1;
1.49      markus    237:        int sock = -1, attempt;
1.90      markus    238:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.49      markus    239:        struct addrinfo hints, *ai, *aitop;
1.90      markus    240:        struct servent *sp;
1.110     markus    241:        /*
                    242:         * Did we get only other errors than "Connection refused" (which
                    243:         * should block fallback to rsh and similar), or did we get at least
                    244:         * one "Connection refused"?
                    245:         */
                    246:        int full_failure = 1;
1.38      markus    247:
1.136     markus    248:        debug2("ssh_connect: needpriv %d", needpriv);
1.38      markus    249:
                    250:        /* Get default port if port has not been set. */
                    251:        if (port == 0) {
                    252:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    253:                if (sp)
                    254:                        port = ntohs(sp->s_port);
                    255:                else
                    256:                        port = SSH_DEFAULT_PORT;
                    257:        }
                    258:        /* If a proxy command is given, connect using it. */
                    259:        if (proxy_command != NULL)
1.124     markus    260:                return ssh_proxy_connect(host, port, proxy_command);
1.38      markus    261:
                    262:        /* No proxy command. */
                    263:
1.49      markus    264:        memset(&hints, 0, sizeof(hints));
1.115     markus    265:        hints.ai_family = family;
1.49      markus    266:        hints.ai_socktype = SOCK_STREAM;
1.126     deraadt   267:        snprintf(strport, sizeof strport, "%u", port);
1.49      markus    268:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.50      markus    269:                fatal("%s: %.100s: %s", __progname, host,
                    270:                    gai_strerror(gaierr));
1.38      markus    271:
1.46      markus    272:        /*
                    273:         * Try to connect several times.  On some machines, the first time
                    274:         * will sometimes fail.  In general socket code appears to behave
                    275:         * quite magically on many machines.
1.110     markus    276:                 */
                    277:        for (attempt = 0; ;) {
1.38      markus    278:                if (attempt > 0)
                    279:                        debug("Trying again...");
                    280:
1.49      markus    281:                /* Loop through addresses for this host, and try each one in
1.68      markus    282:                   sequence until the connection succeeds. */
1.49      markus    283:                for (ai = aitop; ai; ai = ai->ai_next) {
                    284:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                    285:                                continue;
                    286:                        if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
                    287:                            ntop, sizeof(ntop), strport, sizeof(strport),
                    288:                            NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                    289:                                error("ssh_connect: getnameinfo failed");
                    290:                                continue;
                    291:                        }
                    292:                        debug("Connecting to %.200s [%.100s] port %s.",
                    293:                                host, ntop, strport);
                    294:
                    295:                        /* Create a socket for connecting. */
1.139   ! markus    296:                        sock = ssh_create_socket(needpriv, ai);
1.49      markus    297:                        if (sock < 0)
1.110     markus    298:                                /* Any error is already output */
1.49      markus    299:                                continue;
                    300:
                    301:                        if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
                    302:                                /* Successful connection. */
1.102     markus    303:                                memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
1.38      markus    304:                                break;
1.49      markus    305:                        } else {
1.110     markus    306:                                if (errno == ECONNREFUSED)
                    307:                                        full_failure = 0;
1.131     itojun    308:                                debug("connect to address %s port %s: %s",
                    309:                                    ntop, strport, strerror(errno));
1.40      markus    310:                                /*
                    311:                                 * Close the failed socket; there appear to
                    312:                                 * be some problems when reusing a socket for
                    313:                                 * which connect() has already returned an
                    314:                                 * error.
                    315:                                 */
1.38      markus    316:                                close(sock);
                    317:                        }
                    318:                }
1.49      markus    319:                if (ai)
                    320:                        break;  /* Successful connection. */
1.1       deraadt   321:
1.110     markus    322:                attempt++;
                    323:                if (attempt >= connection_attempts)
                    324:                        break;
1.38      markus    325:                /* Sleep a moment before retrying. */
                    326:                sleep(1);
                    327:        }
1.49      markus    328:
                    329:        freeaddrinfo(aitop);
                    330:
1.38      markus    331:        /* Return failure if we didn't get a successful connection. */
1.130     itojun    332:        if (attempt >= connection_attempts) {
1.138     itojun    333:                logit("ssh: connect to host %s port %s: %s",
1.130     itojun    334:                    host, strport, strerror(errno));
1.110     markus    335:                return full_failure ? ECONNABORTED : ECONNREFUSED;
1.130     itojun    336:        }
1.38      markus    337:
                    338:        debug("Connection established.");
1.90      markus    339:
                    340:        /* Set keepalives if requested. */
                    341:        if (options.keepalives &&
                    342:            setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
                    343:            sizeof(on)) < 0)
                    344:                error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.38      markus    345:
                    346:        /* Set the connection. */
                    347:        packet_set_connection(sock, sock);
1.1       deraadt   348:
1.110     markus    349:        return 0;
1.59      markus    350: }
                    351:
1.43      markus    352: /*
1.39      deraadt   353:  * Waits for the server identification string, and sends our own
                    354:  * identification string.
                    355:  */
1.109     itojun    356: static void
1.95      itojun    357: ssh_exchange_identification(void)
1.1       deraadt   358: {
1.38      markus    359:        char buf[256], remote_version[256];     /* must be same size! */
1.64      markus    360:        int remote_major, remote_minor, i, mismatch;
1.38      markus    361:        int connection_in = packet_get_connection_in();
                    362:        int connection_out = packet_get_connection_out();
1.93      stevesk   363:        int minor1 = PROTOCOL_MINOR_1;
1.38      markus    364:
                    365:        /* Read other side\'s version identification. */
1.75      markus    366:        for (;;) {
                    367:                for (i = 0; i < sizeof(buf) - 1; i++) {
1.76      markus    368:                        int len = atomicio(read, connection_in, &buf[i], 1);
1.75      markus    369:                        if (len < 0)
                    370:                                fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
                    371:                        if (len != 1)
                    372:                                fatal("ssh_exchange_identification: Connection closed by remote host");
                    373:                        if (buf[i] == '\r') {
                    374:                                buf[i] = '\n';
                    375:                                buf[i + 1] = 0;
                    376:                                continue;               /**XXX wait for \n */
                    377:                        }
                    378:                        if (buf[i] == '\n') {
                    379:                                buf[i + 1] = 0;
                    380:                                break;
                    381:                        }
1.38      markus    382:                }
1.75      markus    383:                buf[sizeof(buf) - 1] = 0;
1.76      markus    384:                if (strncmp(buf, "SSH-", 4) == 0)
1.38      markus    385:                        break;
1.75      markus    386:                debug("ssh_exchange_identification: %s", buf);
1.38      markus    387:        }
1.59      markus    388:        server_version_string = xstrdup(buf);
1.38      markus    389:
1.40      markus    390:        /*
                    391:         * Check that the versions match.  In future this might accept
                    392:         * several versions and set appropriate flags to handle them.
                    393:         */
1.59      markus    394:        if (sscanf(server_version_string, "SSH-%d.%d-%[^\n]\n",
                    395:            &remote_major, &remote_minor, remote_version) != 3)
1.38      markus    396:                fatal("Bad remote protocol version identification: '%.100s'", buf);
                    397:        debug("Remote protocol version %d.%d, remote software version %.100s",
1.118     deraadt   398:            remote_major, remote_minor, remote_version);
1.38      markus    399:
1.59      markus    400:        compat_datafellows(remote_version);
1.64      markus    401:        mismatch = 0;
1.59      markus    402:
1.116     deraadt   403:        switch (remote_major) {
1.64      markus    404:        case 1:
                    405:                if (remote_minor == 99 &&
                    406:                    (options.protocol & SSH_PROTO_2) &&
                    407:                    !(options.protocol & SSH_PROTO_1_PREFERRED)) {
                    408:                        enable_compat20();
                    409:                        break;
                    410:                }
                    411:                if (!(options.protocol & SSH_PROTO_1)) {
                    412:                        mismatch = 1;
                    413:                        break;
                    414:                }
                    415:                if (remote_minor < 3) {
                    416:                        fatal("Remote machine has too old SSH software version.");
1.81      markus    417:                } else if (remote_minor == 3 || remote_minor == 4) {
1.64      markus    418:                        /* We speak 1.3, too. */
                    419:                        enable_compat13();
1.81      markus    420:                        minor1 = 3;
1.64      markus    421:                        if (options.forward_agent) {
1.138     itojun    422:                                logit("Agent forwarding disabled for protocol 1.3");
1.64      markus    423:                                options.forward_agent = 0;
                    424:                        }
                    425:                }
                    426:                break;
                    427:        case 2:
                    428:                if (options.protocol & SSH_PROTO_2) {
                    429:                        enable_compat20();
                    430:                        break;
1.38      markus    431:                }
1.64      markus    432:                /* FALLTHROUGH */
1.68      markus    433:        default:
1.64      markus    434:                mismatch = 1;
                    435:                break;
1.38      markus    436:        }
1.64      markus    437:        if (mismatch)
1.38      markus    438:                fatal("Protocol major versions differ: %d vs. %d",
1.64      markus    439:                    (options.protocol & SSH_PROTO_2) ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
                    440:                    remote_major);
1.38      markus    441:        /* Send our own protocol version identification. */
                    442:        snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.64      markus    443:            compat20 ? PROTOCOL_MAJOR_2 : PROTOCOL_MAJOR_1,
1.81      markus    444:            compat20 ? PROTOCOL_MINOR_2 : minor1,
1.59      markus    445:            SSH_VERSION);
1.45      deraadt   446:        if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf))
1.38      markus    447:                fatal("write: %.100s", strerror(errno));
1.59      markus    448:        client_version_string = xstrdup(buf);
                    449:        chop(client_version_string);
                    450:        chop(server_version_string);
                    451:        debug("Local version string %.100s", client_version_string);
1.1       deraadt   452: }
                    453:
1.96      markus    454: /* defaults to 'no' */
1.109     itojun    455: static int
1.112     markus    456: confirm(const char *prompt)
1.1       deraadt   457: {
1.119     markus    458:        const char *msg, *again = "Please type 'yes' or 'no': ";
                    459:        char *p;
                    460:        int ret = -1;
1.96      markus    461:
                    462:        if (options.batch_mode)
                    463:                return 0;
1.119     markus    464:        for (msg = prompt;;msg = again) {
                    465:                p = read_passphrase(msg, RP_ECHO);
                    466:                if (p == NULL ||
                    467:                    (p[0] == '\0') || (p[0] == '\n') ||
                    468:                    strncasecmp(p, "no", 2) == 0)
                    469:                        ret = 0;
1.127     markus    470:                if (p && strncasecmp(p, "yes", 3) == 0)
1.119     markus    471:                        ret = 1;
                    472:                if (p)
                    473:                        xfree(p);
                    474:                if (ret != -1)
                    475:                        return ret;
1.1       deraadt   476:        }
                    477: }
                    478:
1.39      deraadt   479: /*
1.108     markus    480:  * check whether the supplied host key is valid, return -1 if the key
                    481:  * is not valid. the user_hostfile will not be updated if 'readonly' is true.
1.39      deraadt   482:  */
1.109     itojun    483: static int
1.70      markus    484: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
1.108     markus    485:     int readonly, const char *user_hostfile, const char *system_hostfile)
1.1       deraadt   486: {
1.58      markus    487:        Key *file_key;
1.72      markus    488:        char *type = key_type(host_key);
1.46      markus    489:        char *ip = NULL;
1.100     markus    490:        char hostline[1000], *hostp, *fp;
1.38      markus    491:        HostStatus host_status;
                    492:        HostStatus ip_status;
1.49      markus    493:        int local = 0, host_ip_differ = 0;
                    494:        char ntop[NI_MAXHOST];
1.119     markus    495:        char msg[1024];
1.132     markus    496:        int len, host_line, ip_line, has_keys;
1.85      markus    497:        const char *host_file = NULL, *ip_file = NULL;
1.49      markus    498:
                    499:        /*
                    500:         * Force accepting of the host key for loopback/localhost. The
                    501:         * problem is that if the home directory is NFS-mounted to multiple
                    502:         * machines, localhost will refer to a different machine in each of
                    503:         * them, and the user will get bogus HOST_CHANGED warnings.  This
                    504:         * essentially disables host authentication for localhost; however,
                    505:         * this is probably not a real problem.
                    506:         */
1.70      markus    507:        /**  hostaddr == 0! */
1.49      markus    508:        switch (hostaddr->sa_family) {
                    509:        case AF_INET:
1.108     markus    510:                local = (ntohl(((struct sockaddr_in *)hostaddr)->
                    511:                   sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
1.49      markus    512:                break;
                    513:        case AF_INET6:
1.108     markus    514:                local = IN6_IS_ADDR_LOOPBACK(
                    515:                    &(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
1.49      markus    516:                break;
                    517:        default:
                    518:                local = 0;
                    519:                break;
                    520:        }
1.111     markus    521:        if (options.no_host_authentication_for_localhost == 1 && local &&
                    522:            options.host_key_alias == NULL) {
1.88      markus    523:                debug("Forcing accepting of host key for "
                    524:                    "loopback/localhost.");
1.108     markus    525:                return 0;
1.49      markus    526:        }
1.42      markus    527:
                    528:        /*
1.88      markus    529:         * We don't have the remote ip-address for connections
                    530:         * using a proxy command
1.42      markus    531:         */
1.84      markus    532:        if (options.proxy_command == NULL) {
                    533:                if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
1.86      markus    534:                    NULL, 0, NI_NUMERICHOST) != 0)
1.84      markus    535:                        fatal("check_host_key: getnameinfo failed");
                    536:                ip = xstrdup(ntop);
                    537:        } else {
                    538:                ip = xstrdup("<no hostip for proxy command>");
1.86      markus    539:        }
1.88      markus    540:        /*
                    541:         * Turn off check_host_ip if the connection is to localhost, via proxy
                    542:         * command or if we don't have a hostname to compare with
                    543:         */
                    544:        if (options.check_host_ip &&
                    545:            (local || strcmp(host, ip) == 0 || options.proxy_command != NULL))
                    546:                options.check_host_ip = 0;
1.86      markus    547:
                    548:        /*
                    549:         * Allow the user to record the key under a different name. This is
                    550:         * useful for ssh tunneling over forwarded connections or if you run
                    551:         * multiple sshd's on different ports on the same machine.
                    552:         */
                    553:        if (options.host_key_alias != NULL) {
                    554:                host = options.host_key_alias;
                    555:                debug("using hostkeyalias: %s", host);
1.84      markus    556:        }
1.38      markus    557:
1.46      markus    558:        /*
                    559:         * Store the host key from the known host file in here so that we can
                    560:         * compare it with the key for the IP address.
                    561:         */
1.58      markus    562:        file_key = key_new(host_key->type);
1.38      markus    563:
1.40      markus    564:        /*
                    565:         * Check if the host key is present in the user\'s list of known
                    566:         * hosts or in the systemwide list.
                    567:         */
1.85      markus    568:        host_file = user_hostfile;
1.108     markus    569:        host_status = check_host_in_hostfile(host_file, host, host_key,
1.118     deraadt   570:            file_key, &host_line);
1.85      markus    571:        if (host_status == HOST_NEW) {
                    572:                host_file = system_hostfile;
1.108     markus    573:                host_status = check_host_in_hostfile(host_file, host, host_key,
                    574:                    file_key, &host_line);
1.85      markus    575:        }
1.40      markus    576:        /*
                    577:         * Also perform check for the ip address, skip the check if we are
                    578:         * localhost or the hostname was an ip address to begin with
                    579:         */
1.88      markus    580:        if (options.check_host_ip) {
1.58      markus    581:                Key *ip_key = key_new(host_key->type);
1.38      markus    582:
1.85      markus    583:                ip_file = user_hostfile;
1.108     markus    584:                ip_status = check_host_in_hostfile(ip_file, ip, host_key,
                    585:                    ip_key, &ip_line);
1.85      markus    586:                if (ip_status == HOST_NEW) {
                    587:                        ip_file = system_hostfile;
1.108     markus    588:                        ip_status = check_host_in_hostfile(ip_file, ip,
                    589:                            host_key, ip_key, &ip_line);
1.85      markus    590:                }
1.38      markus    591:                if (host_status == HOST_CHANGED &&
1.58      markus    592:                    (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38      markus    593:                        host_ip_differ = 1;
                    594:
1.58      markus    595:                key_free(ip_key);
1.38      markus    596:        } else
                    597:                ip_status = host_status;
                    598:
1.58      markus    599:        key_free(file_key);
1.38      markus    600:
                    601:        switch (host_status) {
                    602:        case HOST_OK:
                    603:                /* The host is known and the key matches. */
1.72      markus    604:                debug("Host '%.200s' is known and matches the %s host key.",
                    605:                    host, type);
1.85      markus    606:                debug("Found key in %s:%d", host_file, host_line);
1.88      markus    607:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.108     markus    608:                        if (readonly)
1.138     itojun    609:                                logit("%s host key for IP address "
1.108     markus    610:                                    "'%.128s' not in list of known hosts.",
                    611:                                    type, ip);
                    612:                        else if (!add_host_to_hostfile(user_hostfile, ip,
1.118     deraadt   613:                            host_key))
1.138     itojun    614:                                logit("Failed to add the %s host key for IP "
1.108     markus    615:                                    "address '%.128s' to the list of known "
                    616:                                    "hosts (%.30s).", type, ip, user_hostfile);
1.88      markus    617:                        else
1.138     itojun    618:                                logit("Warning: Permanently added the %s host "
1.108     markus    619:                                    "key for IP address '%.128s' to the list "
                    620:                                    "of known hosts.", type, ip);
1.38      markus    621:                }
                    622:                break;
                    623:        case HOST_NEW:
1.108     markus    624:                if (readonly)
                    625:                        goto fail;
1.38      markus    626:                /* The host is new. */
                    627:                if (options.strict_host_key_checking == 1) {
1.108     markus    628:                        /*
                    629:                         * User has requested strict host key checking.  We
                    630:                         * will not add the host key automatically.  The only
                    631:                         * alternative left is to abort.
                    632:                         */
                    633:                        error("No %s host key is known for %.200s and you "
                    634:                            "have requested strict checking.", type, host);
                    635:                        goto fail;
1.38      markus    636:                } else if (options.strict_host_key_checking == 2) {
1.132     markus    637:                        has_keys = show_other_keys(host, host_key);
1.38      markus    638:                        /* The default */
1.100     markus    639:                        fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.119     markus    640:                        snprintf(msg, sizeof(msg),
1.108     markus    641:                            "The authenticity of host '%.200s (%s)' can't be "
1.132     markus    642:                            "established%s\n"
1.72      markus    643:                            "%s key fingerprint is %s.\n"
1.108     markus    644:                            "Are you sure you want to continue connecting "
1.132     markus    645:                            "(yes/no)? ",
1.137     deraadt   646:                            host, ip,
                    647:                            has_keys ? ",\nbut keys of different type are already "
                    648:                            "known for this host." : ".",
                    649:                            type, fp);
1.100     markus    650:                        xfree(fp);
1.119     markus    651:                        if (!confirm(msg))
1.108     markus    652:                                goto fail;
1.38      markus    653:                }
1.88      markus    654:                if (options.check_host_ip && ip_status == HOST_NEW) {
1.38      markus    655:                        snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
                    656:                        hostp = hostline;
                    657:                } else
                    658:                        hostp = host;
                    659:
1.108     markus    660:                /*
                    661:                 * If not in strict mode, add the key automatically to the
                    662:                 * local known_hosts file.
                    663:                 */
1.70      markus    664:                if (!add_host_to_hostfile(user_hostfile, hostp, host_key))
1.138     itojun    665:                        logit("Failed to add the host to the list of known "
1.108     markus    666:                            "hosts (%.500s).", user_hostfile);
1.38      markus    667:                else
1.138     itojun    668:                        logit("Warning: Permanently added '%.200s' (%s) to the "
1.108     markus    669:                            "list of known hosts.", hostp, type);
1.38      markus    670:                break;
                    671:        case HOST_CHANGED:
                    672:                if (options.check_host_ip && host_ip_differ) {
                    673:                        char *msg;
                    674:                        if (ip_status == HOST_NEW)
                    675:                                msg = "is unknown";
                    676:                        else if (ip_status == HOST_OK)
                    677:                                msg = "is unchanged";
                    678:                        else
                    679:                                msg = "has a different value";
                    680:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    681:                        error("@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @");
                    682:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.72      markus    683:                        error("The %s host key for %s has changed,", type, host);
1.38      markus    684:                        error("and the key for the according IP address %s", ip);
                    685:                        error("%s. This could either mean that", msg);
                    686:                        error("DNS SPOOFING is happening or the IP address for the host");
1.85      markus    687:                        error("and its host key have changed at the same time.");
1.88      markus    688:                        if (ip_status != HOST_NEW)
1.85      markus    689:                                error("Offending key for IP in %s:%d", ip_file, ip_line);
1.38      markus    690:                }
                    691:                /* The host key has changed. */
1.100     markus    692:                fp = key_fingerprint(host_key, SSH_FP_MD5, SSH_FP_HEX);
1.38      markus    693:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.47      markus    694:                error("@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @");
1.38      markus    695:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    696:                error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
                    697:                error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
1.72      markus    698:                error("It is also possible that the %s host key has just been changed.", type);
1.87      markus    699:                error("The fingerprint for the %s key sent by the remote host is\n%s.",
1.100     markus    700:                    type, fp);
1.38      markus    701:                error("Please contact your system administrator.");
                    702:                error("Add correct host key in %.100s to get rid of this message.",
1.87      markus    703:                    user_hostfile);
1.85      markus    704:                error("Offending key in %s:%d", host_file, host_line);
1.100     markus    705:                xfree(fp);
1.38      markus    706:
1.40      markus    707:                /*
                    708:                 * If strict host key checking is in use, the user will have
                    709:                 * to edit the key manually and we can only abort.
                    710:                 */
1.108     markus    711:                if (options.strict_host_key_checking) {
                    712:                        error("%s host key for %.200s has changed and you have "
                    713:                            "requested strict checking.", type, host);
                    714:                        goto fail;
                    715:                }
1.38      markus    716:
1.40      markus    717:                /*
                    718:                 * If strict host key checking has not been requested, allow
                    719:                 * the connection but without password authentication or
                    720:                 * agent forwarding.
                    721:                 */
1.38      markus    722:                if (options.password_authentication) {
1.108     markus    723:                        error("Password authentication is disabled to avoid "
                    724:                            "man-in-the-middle attacks.");
1.38      markus    725:                        options.password_authentication = 0;
                    726:                }
                    727:                if (options.forward_agent) {
1.108     markus    728:                        error("Agent forwarding is disabled to avoid "
                    729:                            "man-in-the-middle attacks.");
1.38      markus    730:                        options.forward_agent = 0;
1.83      markus    731:                }
                    732:                if (options.forward_x11) {
1.108     markus    733:                        error("X11 forwarding is disabled to avoid "
                    734:                            "man-in-the-middle attacks.");
1.83      markus    735:                        options.forward_x11 = 0;
                    736:                }
1.108     markus    737:                if (options.num_local_forwards > 0 ||
                    738:                    options.num_remote_forwards > 0) {
                    739:                        error("Port forwarding is disabled to avoid "
                    740:                            "man-in-the-middle attacks.");
                    741:                        options.num_local_forwards =
1.118     deraadt   742:                            options.num_remote_forwards = 0;
1.38      markus    743:                }
1.40      markus    744:                /*
                    745:                 * XXX Should permit the user to change to use the new id.
                    746:                 * This could be done by converting the host key to an
                    747:                 * identifying sentence, tell that the host identifies itself
                    748:                 * by that sentence, and ask the user if he/she whishes to
                    749:                 * accept the authentication.
                    750:                 */
1.38      markus    751:                break;
1.132     markus    752:        case HOST_FOUND:
                    753:                fatal("internal error");
                    754:                break;
1.88      markus    755:        }
                    756:
                    757:        if (options.check_host_ip && host_status != HOST_CHANGED &&
                    758:            ip_status == HOST_CHANGED) {
1.119     markus    759:                snprintf(msg, sizeof(msg),
                    760:                    "Warning: the %s host key for '%.200s' "
                    761:                    "differs from the key for the IP address '%.128s'"
                    762:                    "\nOffending key for IP in %s:%d",
                    763:                    type, host, ip, ip_file, ip_line);
                    764:                if (host_status == HOST_OK) {
                    765:                        len = strlen(msg);
                    766:                        snprintf(msg + len, sizeof(msg) - len,
                    767:                            "\nMatching host key in %s:%d",
1.125     deraadt   768:                            host_file, host_line);
1.119     markus    769:                }
1.88      markus    770:                if (options.strict_host_key_checking == 1) {
1.138     itojun    771:                        logit(msg);
1.108     markus    772:                        error("Exiting, you have requested strict checking.");
                    773:                        goto fail;
1.88      markus    774:                } else if (options.strict_host_key_checking == 2) {
1.119     markus    775:                        strlcat(msg, "\nAre you sure you want "
                    776:                            "to continue connecting (yes/no)? ", sizeof(msg));
                    777:                        if (!confirm(msg))
1.108     markus    778:                                goto fail;
1.119     markus    779:                } else {
1.138     itojun    780:                        logit(msg);
1.88      markus    781:                }
1.38      markus    782:        }
1.82      provos    783:
                    784:        xfree(ip);
1.108     markus    785:        return 0;
                    786:
                    787: fail:
                    788:        xfree(ip);
                    789:        return -1;
                    790: }
                    791:
                    792: int
                    793: verify_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
                    794: {
                    795:        struct stat st;
                    796:
                    797:        /* return ok if the key can be found in an old keyfile */
                    798:        if (stat(options.system_hostfile2, &st) == 0 ||
                    799:            stat(options.user_hostfile2, &st) == 0) {
                    800:                if (check_host_key(host, hostaddr, host_key, /*readonly*/ 1,
                    801:                    options.user_hostfile2, options.system_hostfile2) == 0)
                    802:                        return 0;
                    803:        }
                    804:        return check_host_key(host, hostaddr, host_key, /*readonly*/ 0,
                    805:            options.user_hostfile, options.system_hostfile);
1.51      markus    806: }
1.70      markus    807:
1.51      markus    808: /*
                    809:  * Starts a dialog with the server, and authenticates the current user on the
                    810:  * server.  This does not need any extra privileges.  The basic connection
                    811:  * to the server must already have been established before this is called.
                    812:  * If login fails, this function prints an error and never returns.
                    813:  * This function does not require super-user privileges.
                    814:  */
                    815: void
1.120     markus    816: ssh_login(Sensitive *sensitive, const char *orighost,
1.103     markus    817:     struct sockaddr *hostaddr, struct passwd *pw)
1.51      markus    818: {
                    819:        char *host, *cp;
1.70      markus    820:        char *server_user, *local_user;
                    821:
                    822:        local_user = xstrdup(pw->pw_name);
                    823:        server_user = options.user ? options.user : local_user;
1.51      markus    824:
                    825:        /* Convert the user-supplied hostname into all lowercase. */
                    826:        host = xstrdup(orighost);
                    827:        for (cp = host; *cp; cp++)
                    828:                if (isupper(*cp))
                    829:                        *cp = tolower(*cp);
                    830:
                    831:        /* Exchange protocol version identification strings with the server. */
                    832:        ssh_exchange_identification();
                    833:
                    834:        /* Put the connection into non-blocking mode. */
                    835:        packet_set_nonblocking();
                    836:
                    837:        /* key exchange */
                    838:        /* authenticate user */
1.59      markus    839:        if (compat20) {
                    840:                ssh_kex2(host, hostaddr);
1.120     markus    841:                ssh_userauth2(local_user, server_user, host, sensitive);
1.59      markus    842:        } else {
                    843:                ssh_kex(host, hostaddr);
1.120     markus    844:                ssh_userauth1(local_user, server_user, host, sensitive);
1.59      markus    845:        }
1.97      markus    846: }
                    847:
                    848: void
                    849: ssh_put_password(char *password)
                    850: {
                    851:        int size;
                    852:        char *padded;
                    853:
1.99      deraadt   854:        if (datafellows & SSH_BUG_PASSWORDPAD) {
1.107     markus    855:                packet_put_cstring(password);
1.99      deraadt   856:                return;
                    857:        }
1.97      markus    858:        size = roundup(strlen(password) + 1, 32);
                    859:        padded = xmalloc(size);
                    860:        memset(padded, 0, size);
                    861:        strlcpy(padded, password, size);
                    862:        packet_put_string(padded, size);
                    863:        memset(padded, 0, size);
                    864:        xfree(padded);
1.132     markus    865: }
                    866:
                    867: static int
                    868: show_key_from_file(const char *file, const char *host, int keytype)
                    869: {
                    870:        Key *found;
                    871:        char *fp;
                    872:        int line, ret;
                    873:
                    874:        found = key_new(keytype);
                    875:        if ((ret = lookup_key_in_hostfile_by_type(file, host,
                    876:            keytype, found, &line))) {
                    877:                fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
1.138     itojun    878:                logit("WARNING: %s key found for host %s\n"
1.133     markus    879:                    "in %s:%d\n"
1.132     markus    880:                    "%s key fingerprint %s.",
                    881:                    key_type(found), host, file, line,
                    882:                    key_type(found), fp);
                    883:                xfree(fp);
                    884:        }
                    885:        key_free(found);
                    886:        return (ret);
                    887: }
                    888:
                    889: /* print all known host keys for a given host, but skip keys of given type */
                    890: static int
                    891: show_other_keys(const char *host, Key *key)
                    892: {
                    893:        int type[] = { KEY_RSA1, KEY_RSA, KEY_DSA, -1};
                    894:        int i, found = 0;
                    895:
                    896:        for (i = 0; type[i] != -1; i++) {
                    897:                if (type[i] == key->type)
                    898:                        continue;
                    899:                if (type[i] != KEY_RSA1 &&
                    900:                    show_key_from_file(options.user_hostfile2, host, type[i])) {
                    901:                        found = 1;
                    902:                        continue;
                    903:                }
                    904:                if (type[i] != KEY_RSA1 &&
                    905:                    show_key_from_file(options.system_hostfile2, host, type[i])) {
                    906:                        found = 1;
                    907:                        continue;
                    908:                }
                    909:                if (show_key_from_file(options.user_hostfile, host, type[i])) {
                    910:                        found = 1;
                    911:                        continue;
                    912:                }
                    913:                if (show_key_from_file(options.system_hostfile, host, type[i])) {
                    914:                        found = 1;
                    915:                        continue;
                    916:                }
                    917:                debug2("no key of type %d for host %s", type[i], host);
                    918:        }
                    919:        return (found);
1.1       deraadt   920: }