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

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