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

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:  * Created: Sat Mar 18 22:15:47 1995 ylo
                      6:  * Code to connect to a remote host, and to perform the client side of the
                      7:  * login (authentication) dialog.
                      8:  */
1.1       deraadt     9:
                     10: #include "includes.h"
1.58    ! markus     11: RCSID("$OpenBSD: sshconnect.c,v 1.57 2000/03/16 20:56:14 markus Exp $");
1.1       deraadt    12:
1.3       provos     13: #include <ssl/bn.h>
1.1       deraadt    14: #include "xmalloc.h"
                     15: #include "rsa.h"
                     16: #include "ssh.h"
                     17: #include "packet.h"
                     18: #include "authfd.h"
                     19: #include "cipher.h"
                     20: #include "mpaux.h"
                     21: #include "uidswap.h"
1.21      markus     22: #include "compat.h"
1.27      markus     23: #include "readconf.h"
1.1       deraadt    24:
1.58    ! markus     25: #include <ssl/rsa.h>
        !            26: #include <ssl/dsa.h>
1.24      deraadt    27: #include <ssl/md5.h>
1.58    ! markus     28: #include "key.h"
        !            29: #include "hostfile.h"
1.10      deraadt    30:
1.1       deraadt    31: /* Session id for the current session. */
                     32: unsigned char session_id[16];
                     33:
1.51      markus     34: /* authentications supported by server */
                     35: unsigned int supported_authentications;
                     36:
1.43      markus     37: extern Options options;
1.50      markus     38: extern char *__progname;
1.43      markus     39:
1.39      deraadt    40: /*
                     41:  * Connect to the given ssh server using a proxy command.
                     42:  */
1.3       provos     43: int
1.41      markus     44: ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid,
1.3       provos     45:                  const char *proxy_command)
1.1       deraadt    46: {
1.38      markus     47:        Buffer command;
                     48:        const char *cp;
                     49:        char *command_string;
                     50:        int pin[2], pout[2];
                     51:        int pid;
1.49      markus     52:        char strport[NI_MAXSERV];
1.38      markus     53:
                     54:        /* Convert the port number into a string. */
1.49      markus     55:        snprintf(strport, sizeof strport, "%hu", port);
1.38      markus     56:
                     57:        /* Build the final command string in the buffer by making the
                     58:           appropriate substitutions to the given proxy command. */
                     59:        buffer_init(&command);
                     60:        for (cp = proxy_command; *cp; cp++) {
                     61:                if (cp[0] == '%' && cp[1] == '%') {
                     62:                        buffer_append(&command, "%", 1);
                     63:                        cp++;
                     64:                        continue;
                     65:                }
                     66:                if (cp[0] == '%' && cp[1] == 'h') {
                     67:                        buffer_append(&command, host, strlen(host));
                     68:                        cp++;
                     69:                        continue;
                     70:                }
                     71:                if (cp[0] == '%' && cp[1] == 'p') {
1.49      markus     72:                        buffer_append(&command, strport, strlen(strport));
1.38      markus     73:                        cp++;
                     74:                        continue;
                     75:                }
                     76:                buffer_append(&command, cp, 1);
                     77:        }
                     78:        buffer_append(&command, "\0", 1);
                     79:
                     80:        /* Get the final command string. */
                     81:        command_string = buffer_ptr(&command);
                     82:
                     83:        /* Create pipes for communicating with the proxy. */
                     84:        if (pipe(pin) < 0 || pipe(pout) < 0)
                     85:                fatal("Could not create pipes to communicate with the proxy: %.100s",
                     86:                      strerror(errno));
                     87:
                     88:        debug("Executing proxy command: %.500s", command_string);
                     89:
                     90:        /* Fork and execute the proxy command. */
                     91:        if ((pid = fork()) == 0) {
                     92:                char *argv[10];
                     93:
                     94:                /* Child.  Permanently give up superuser privileges. */
                     95:                permanently_set_uid(original_real_uid);
                     96:
                     97:                /* Redirect stdin and stdout. */
                     98:                close(pin[1]);
                     99:                if (pin[0] != 0) {
                    100:                        if (dup2(pin[0], 0) < 0)
                    101:                                perror("dup2 stdin");
                    102:                        close(pin[0]);
                    103:                }
                    104:                close(pout[0]);
                    105:                if (dup2(pout[1], 1) < 0)
                    106:                        perror("dup2 stdout");
                    107:                /* Cannot be 1 because pin allocated two descriptors. */
                    108:                close(pout[1]);
                    109:
                    110:                /* Stderr is left as it is so that error messages get
                    111:                   printed on the user's terminal. */
                    112:                argv[0] = "/bin/sh";
                    113:                argv[1] = "-c";
                    114:                argv[2] = command_string;
                    115:                argv[3] = NULL;
                    116:
                    117:                /* Execute the proxy command.  Note that we gave up any
                    118:                   extra privileges above. */
                    119:                execv("/bin/sh", argv);
                    120:                perror("/bin/sh");
                    121:                exit(1);
                    122:        }
                    123:        /* Parent. */
                    124:        if (pid < 0)
                    125:                fatal("fork failed: %.100s", strerror(errno));
                    126:
                    127:        /* Close child side of the descriptors. */
                    128:        close(pin[0]);
                    129:        close(pout[1]);
                    130:
                    131:        /* Free the command name. */
                    132:        buffer_free(&command);
                    133:
                    134:        /* Set the connection file descriptors. */
                    135:        packet_set_connection(pout[0], pin[1]);
1.1       deraadt   136:
1.38      markus    137:        return 1;
1.1       deraadt   138: }
                    139:
1.39      deraadt   140: /*
                    141:  * Creates a (possibly privileged) socket for use as the ssh connection.
                    142:  */
1.38      markus    143: int
1.49      markus    144: ssh_create_socket(uid_t original_real_uid, int privileged, int family)
1.1       deraadt   145: {
1.38      markus    146:        int sock;
1.1       deraadt   147:
1.40      markus    148:        /*
                    149:         * If we are running as root and want to connect to a privileged
                    150:         * port, bind our own socket to a privileged port.
                    151:         */
1.38      markus    152:        if (privileged) {
                    153:                int p = IPPORT_RESERVED - 1;
1.49      markus    154:                sock = rresvport_af(&p, family);
1.38      markus    155:                if (sock < 0)
1.55      markus    156:                        error("rresvport: af=%d %.100s", family, strerror(errno));
                    157:                else
                    158:                        debug("Allocated local port %d.", p);
1.38      markus    159:        } else {
1.46      markus    160:                /*
                    161:                 * Just create an ordinary socket on arbitrary port.  We use
                    162:                 * the user's uid to create the socket.
                    163:                 */
1.38      markus    164:                temporarily_use_uid(original_real_uid);
1.49      markus    165:                sock = socket(family, SOCK_STREAM, 0);
1.38      markus    166:                if (sock < 0)
1.49      markus    167:                        error("socket: %.100s", strerror(errno));
1.38      markus    168:                restore_uid();
                    169:        }
                    170:        return sock;
1.1       deraadt   171: }
                    172:
1.39      deraadt   173: /*
1.49      markus    174:  * Opens a TCP/IP connection to the remote server on the given host.
                    175:  * The address of the remote host will be returned in hostaddr.
                    176:  * If port is 0, the default port will be used.  If anonymous is zero,
1.39      deraadt   177:  * a privileged port will be allocated to make the connection.
                    178:  * This requires super-user privileges if anonymous is false.
                    179:  * Connection_attempts specifies the maximum number of tries (one per
                    180:  * second).  If proxy_command is non-NULL, it specifies the command (with %h
                    181:  * and %p substituted for host and port, respectively) to use to contact
                    182:  * the daemon.
                    183:  */
1.38      markus    184: int
1.49      markus    185: ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
1.41      markus    186:            u_short port, int connection_attempts,
1.38      markus    187:            int anonymous, uid_t original_real_uid,
                    188:            const char *proxy_command)
1.1       deraadt   189: {
1.49      markus    190:        int sock = -1, attempt;
1.38      markus    191:        struct servent *sp;
1.49      markus    192:        struct addrinfo hints, *ai, *aitop;
                    193:        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
                    194:        int gaierr;
1.38      markus    195:        struct linger linger;
                    196:
                    197:        debug("ssh_connect: getuid %d geteuid %d anon %d",
                    198:              (int) getuid(), (int) geteuid(), anonymous);
                    199:
                    200:        /* Get default port if port has not been set. */
                    201:        if (port == 0) {
                    202:                sp = getservbyname(SSH_SERVICE_NAME, "tcp");
                    203:                if (sp)
                    204:                        port = ntohs(sp->s_port);
                    205:                else
                    206:                        port = SSH_DEFAULT_PORT;
                    207:        }
                    208:        /* If a proxy command is given, connect using it. */
                    209:        if (proxy_command != NULL)
                    210:                return ssh_proxy_connect(host, port, original_real_uid, proxy_command);
                    211:
                    212:        /* No proxy command. */
                    213:
1.49      markus    214:        memset(&hints, 0, sizeof(hints));
                    215:        hints.ai_family = IPv4or6;
                    216:        hints.ai_socktype = SOCK_STREAM;
                    217:        snprintf(strport, sizeof strport, "%d", port);
                    218:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
1.50      markus    219:                fatal("%s: %.100s: %s", __progname, host,
                    220:                    gai_strerror(gaierr));
1.38      markus    221:
1.46      markus    222:        /*
                    223:         * Try to connect several times.  On some machines, the first time
                    224:         * will sometimes fail.  In general socket code appears to behave
                    225:         * quite magically on many machines.
                    226:         */
1.38      markus    227:        for (attempt = 0; attempt < connection_attempts; attempt++) {
                    228:                if (attempt > 0)
                    229:                        debug("Trying again...");
                    230:
1.49      markus    231:                /* Loop through addresses for this host, and try each one in
                    232:                   sequence until the connection succeeds. */
                    233:                for (ai = aitop; ai; ai = ai->ai_next) {
                    234:                        if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
                    235:                                continue;
                    236:                        if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
                    237:                            ntop, sizeof(ntop), strport, sizeof(strport),
                    238:                            NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                    239:                                error("ssh_connect: getnameinfo failed");
                    240:                                continue;
                    241:                        }
                    242:                        debug("Connecting to %.200s [%.100s] port %s.",
                    243:                                host, ntop, strport);
                    244:
                    245:                        /* Create a socket for connecting. */
                    246:                        sock = ssh_create_socket(original_real_uid,
                    247:                            !anonymous && geteuid() == 0 && port < IPPORT_RESERVED,
                    248:                            ai->ai_family);
                    249:                        if (sock < 0)
                    250:                                continue;
                    251:
                    252:                        /* Connect to the host.  We use the user's uid in the
                    253:                         * hope that it will help with tcp_wrappers showing
                    254:                         * the remote uid as root.
1.40      markus    255:                         */
1.38      markus    256:                        temporarily_use_uid(original_real_uid);
1.49      markus    257:                        if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
                    258:                                /* Successful connection. */
                    259:                                memcpy(hostaddr, ai->ai_addr, sizeof(*hostaddr));
1.38      markus    260:                                restore_uid();
                    261:                                break;
1.49      markus    262:                        } else {
1.38      markus    263:                                debug("connect: %.100s", strerror(errno));
                    264:                                restore_uid();
1.40      markus    265:                                /*
                    266:                                 * Close the failed socket; there appear to
                    267:                                 * be some problems when reusing a socket for
                    268:                                 * which connect() has already returned an
                    269:                                 * error.
                    270:                                 */
1.38      markus    271:                                shutdown(sock, SHUT_RDWR);
                    272:                                close(sock);
                    273:                        }
                    274:                }
1.49      markus    275:                if (ai)
                    276:                        break;  /* Successful connection. */
1.1       deraadt   277:
1.38      markus    278:                /* Sleep a moment before retrying. */
                    279:                sleep(1);
                    280:        }
1.49      markus    281:
                    282:        freeaddrinfo(aitop);
                    283:
1.38      markus    284:        /* Return failure if we didn't get a successful connection. */
                    285:        if (attempt >= connection_attempts)
                    286:                return 0;
                    287:
                    288:        debug("Connection established.");
                    289:
1.40      markus    290:        /*
                    291:         * Set socket options.  We would like the socket to disappear as soon
                    292:         * as it has been closed for whatever reason.
                    293:         */
                    294:        /* setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
1.38      markus    295:        linger.l_onoff = 1;
                    296:        linger.l_linger = 5;
                    297:        setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
                    298:
                    299:        /* Set the connection. */
                    300:        packet_set_connection(sock, sock);
1.1       deraadt   301:
1.38      markus    302:        return 1;
1.1       deraadt   303: }
                    304:
1.39      deraadt   305: /*
                    306:  * Checks if the user has an authentication agent, and if so, tries to
                    307:  * authenticate using the agent.
                    308:  */
1.3       provos    309: int
                    310: try_agent_authentication()
1.1       deraadt   311: {
1.38      markus    312:        int status, type;
                    313:        char *comment;
                    314:        AuthenticationConnection *auth;
                    315:        unsigned char response[16];
                    316:        unsigned int i;
                    317:        BIGNUM *e, *n, *challenge;
                    318:
                    319:        /* Get connection to the agent. */
                    320:        auth = ssh_get_authentication_connection();
                    321:        if (!auth)
                    322:                return 0;
                    323:
                    324:        e = BN_new();
                    325:        n = BN_new();
                    326:        challenge = BN_new();
                    327:
                    328:        /* Loop through identities served by the agent. */
                    329:        for (status = ssh_get_first_identity(auth, e, n, &comment);
                    330:             status;
                    331:             status = ssh_get_next_identity(auth, e, n, &comment)) {
                    332:                int plen, clen;
                    333:
                    334:                /* Try this identity. */
                    335:                debug("Trying RSA authentication via agent with '%.100s'", comment);
                    336:                xfree(comment);
                    337:
                    338:                /* Tell the server that we are willing to authenticate using this key. */
                    339:                packet_start(SSH_CMSG_AUTH_RSA);
                    340:                packet_put_bignum(n);
                    341:                packet_send();
                    342:                packet_write_wait();
                    343:
                    344:                /* Wait for server's response. */
                    345:                type = packet_read(&plen);
                    346:
                    347:                /* The server sends failure if it doesn\'t like our key or
                    348:                   does not support RSA authentication. */
                    349:                if (type == SSH_SMSG_FAILURE) {
                    350:                        debug("Server refused our key.");
                    351:                        continue;
                    352:                }
                    353:                /* Otherwise it should have sent a challenge. */
                    354:                if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    355:                        packet_disconnect("Protocol error during RSA authentication: %d",
                    356:                                          type);
                    357:
                    358:                packet_get_bignum(challenge, &clen);
                    359:
                    360:                packet_integrity_check(plen, clen, type);
                    361:
                    362:                debug("Received RSA challenge from server.");
                    363:
                    364:                /* Ask the agent to decrypt the challenge. */
                    365:                if (!ssh_decrypt_challenge(auth, e, n, challenge,
                    366:                                           session_id, 1, response)) {
                    367:                        /* The agent failed to authenticate this identifier although it
                    368:                           advertised it supports this.  Just return a wrong value. */
                    369:                        log("Authentication agent failed to decrypt challenge.");
                    370:                        memset(response, 0, sizeof(response));
                    371:                }
                    372:                debug("Sending response to RSA challenge.");
1.1       deraadt   373:
1.38      markus    374:                /* Send the decrypted challenge back to the server. */
                    375:                packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    376:                for (i = 0; i < 16; i++)
                    377:                        packet_put_char(response[i]);
                    378:                packet_send();
                    379:                packet_write_wait();
                    380:
                    381:                /* Wait for response from the server. */
                    382:                type = packet_read(&plen);
                    383:
                    384:                /* The server returns success if it accepted the authentication. */
                    385:                if (type == SSH_SMSG_SUCCESS) {
                    386:                        debug("RSA authentication accepted by server.");
                    387:                        BN_clear_free(e);
                    388:                        BN_clear_free(n);
                    389:                        BN_clear_free(challenge);
                    390:                        return 1;
                    391:                }
                    392:                /* Otherwise it should return failure. */
                    393:                if (type != SSH_SMSG_FAILURE)
                    394:                        packet_disconnect("Protocol error waiting RSA auth response: %d",
                    395:                                          type);
                    396:        }
                    397:
                    398:        BN_clear_free(e);
                    399:        BN_clear_free(n);
                    400:        BN_clear_free(challenge);
                    401:
                    402:        debug("RSA authentication using agent refused.");
                    403:        return 0;
1.1       deraadt   404: }
                    405:
1.39      deraadt   406: /*
                    407:  * Computes the proper response to a RSA challenge, and sends the response to
                    408:  * the server.
                    409:  */
1.3       provos    410: void
1.38      markus    411: respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
1.1       deraadt   412: {
1.38      markus    413:        unsigned char buf[32], response[16];
                    414:        MD5_CTX md;
                    415:        int i, len;
                    416:
                    417:        /* Decrypt the challenge using the private key. */
                    418:        rsa_private_decrypt(challenge, challenge, prv);
                    419:
                    420:        /* Compute the response. */
                    421:        /* The response is MD5 of decrypted challenge plus session id. */
                    422:        len = BN_num_bytes(challenge);
                    423:        if (len <= 0 || len > sizeof(buf))
                    424:                packet_disconnect("respond_to_rsa_challenge: bad challenge length %d",
                    425:                                  len);
                    426:
                    427:        memset(buf, 0, sizeof(buf));
                    428:        BN_bn2bin(challenge, buf + sizeof(buf) - len);
                    429:        MD5_Init(&md);
                    430:        MD5_Update(&md, buf, 32);
                    431:        MD5_Update(&md, session_id, 16);
                    432:        MD5_Final(response, &md);
                    433:
                    434:        debug("Sending response to host key RSA challenge.");
                    435:
                    436:        /* Send the response back to the server. */
                    437:        packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    438:        for (i = 0; i < 16; i++)
                    439:                packet_put_char(response[i]);
                    440:        packet_send();
                    441:        packet_write_wait();
                    442:
                    443:        memset(buf, 0, sizeof(buf));
                    444:        memset(response, 0, sizeof(response));
                    445:        memset(&md, 0, sizeof(md));
1.1       deraadt   446: }
                    447:
1.39      deraadt   448: /*
                    449:  * Checks if the user has authentication file, and if so, tries to authenticate
                    450:  * the user using it.
                    451:  */
1.3       provos    452: int
1.43      markus    453: try_rsa_authentication(const char *authfile)
1.1       deraadt   454: {
1.38      markus    455:        BIGNUM *challenge;
                    456:        RSA *private_key;
                    457:        RSA *public_key;
                    458:        char *passphrase, *comment;
                    459:        int type, i;
                    460:        int plen, clen;
                    461:
                    462:        /* Try to load identification for the authentication key. */
                    463:        public_key = RSA_new();
                    464:        if (!load_public_key(authfile, public_key, &comment)) {
                    465:                RSA_free(public_key);
1.43      markus    466:                /* Could not load it.  Fail. */
                    467:                return 0;
1.38      markus    468:        }
                    469:        debug("Trying RSA authentication with key '%.100s'", comment);
                    470:
                    471:        /* Tell the server that we are willing to authenticate using this key. */
                    472:        packet_start(SSH_CMSG_AUTH_RSA);
                    473:        packet_put_bignum(public_key->n);
                    474:        packet_send();
                    475:        packet_write_wait();
                    476:
                    477:        /* We no longer need the public key. */
                    478:        RSA_free(public_key);
                    479:
                    480:        /* Wait for server's response. */
                    481:        type = packet_read(&plen);
                    482:
1.40      markus    483:        /*
                    484:         * The server responds with failure if it doesn\'t like our key or
                    485:         * doesn\'t support RSA authentication.
                    486:         */
1.38      markus    487:        if (type == SSH_SMSG_FAILURE) {
                    488:                debug("Server refused our key.");
                    489:                xfree(comment);
1.43      markus    490:                return 0;
1.38      markus    491:        }
                    492:        /* Otherwise, the server should respond with a challenge. */
                    493:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    494:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    495:
                    496:        /* Get the challenge from the packet. */
                    497:        challenge = BN_new();
                    498:        packet_get_bignum(challenge, &clen);
                    499:
                    500:        packet_integrity_check(plen, clen, type);
                    501:
                    502:        debug("Received RSA challenge from server.");
                    503:
                    504:        private_key = RSA_new();
1.40      markus    505:        /*
                    506:         * Load the private key.  Try first with empty passphrase; if it
                    507:         * fails, ask for a passphrase.
                    508:         */
1.38      markus    509:        if (!load_private_key(authfile, "", private_key, NULL)) {
                    510:                char buf[300];
                    511:                snprintf(buf, sizeof buf, "Enter passphrase for RSA key '%.100s': ",
1.45      deraadt   512:                    comment);
1.38      markus    513:                if (!options.batch_mode)
                    514:                        passphrase = read_passphrase(buf, 0);
                    515:                else {
                    516:                        debug("Will not query passphrase for %.100s in batch mode.",
                    517:                              comment);
                    518:                        passphrase = xstrdup("");
                    519:                }
                    520:
                    521:                /* Load the authentication file using the pasphrase. */
                    522:                if (!load_private_key(authfile, passphrase, private_key, NULL)) {
                    523:                        memset(passphrase, 0, strlen(passphrase));
                    524:                        xfree(passphrase);
                    525:                        error("Bad passphrase.");
                    526:
                    527:                        /* Send a dummy response packet to avoid protocol error. */
                    528:                        packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    529:                        for (i = 0; i < 16; i++)
                    530:                                packet_put_char(0);
                    531:                        packet_send();
                    532:                        packet_write_wait();
                    533:
                    534:                        /* Expect the server to reject it... */
                    535:                        packet_read_expect(&plen, SSH_SMSG_FAILURE);
                    536:                        xfree(comment);
                    537:                        return 0;
                    538:                }
                    539:                /* Destroy the passphrase. */
                    540:                memset(passphrase, 0, strlen(passphrase));
                    541:                xfree(passphrase);
                    542:        }
                    543:        /* We no longer need the comment. */
                    544:        xfree(comment);
                    545:
                    546:        /* Compute and send a response to the challenge. */
                    547:        respond_to_rsa_challenge(challenge, private_key);
                    548:
                    549:        /* Destroy the private key. */
                    550:        RSA_free(private_key);
                    551:
                    552:        /* We no longer need the challenge. */
                    553:        BN_clear_free(challenge);
                    554:
                    555:        /* Wait for response from the server. */
                    556:        type = packet_read(&plen);
                    557:        if (type == SSH_SMSG_SUCCESS) {
                    558:                debug("RSA authentication accepted by server.");
                    559:                return 1;
                    560:        }
                    561:        if (type != SSH_SMSG_FAILURE)
                    562:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    563:        debug("RSA authentication refused.");
                    564:        return 0;
1.1       deraadt   565: }
                    566:
1.39      deraadt   567: /*
                    568:  * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
                    569:  * authentication and RSA host authentication.
                    570:  */
1.3       provos    571: int
1.38      markus    572: try_rhosts_rsa_authentication(const char *local_user, RSA * host_key)
1.1       deraadt   573: {
1.38      markus    574:        int type;
                    575:        BIGNUM *challenge;
                    576:        int plen, clen;
                    577:
                    578:        debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
                    579:
                    580:        /* Tell the server that we are willing to authenticate using this key. */
                    581:        packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
                    582:        packet_put_string(local_user, strlen(local_user));
                    583:        packet_put_int(BN_num_bits(host_key->n));
                    584:        packet_put_bignum(host_key->e);
                    585:        packet_put_bignum(host_key->n);
                    586:        packet_send();
                    587:        packet_write_wait();
                    588:
                    589:        /* Wait for server's response. */
                    590:        type = packet_read(&plen);
                    591:
                    592:        /* The server responds with failure if it doesn't admit our
                    593:           .rhosts authentication or doesn't know our host key. */
                    594:        if (type == SSH_SMSG_FAILURE) {
                    595:                debug("Server refused our rhosts authentication or host key.");
                    596:                return 0;
                    597:        }
                    598:        /* Otherwise, the server should respond with a challenge. */
                    599:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    600:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    601:
                    602:        /* Get the challenge from the packet. */
                    603:        challenge = BN_new();
                    604:        packet_get_bignum(challenge, &clen);
                    605:
                    606:        packet_integrity_check(plen, clen, type);
                    607:
                    608:        debug("Received RSA challenge for host key from server.");
                    609:
                    610:        /* Compute a response to the challenge. */
                    611:        respond_to_rsa_challenge(challenge, host_key);
                    612:
                    613:        /* We no longer need the challenge. */
                    614:        BN_clear_free(challenge);
                    615:
                    616:        /* Wait for response from the server. */
                    617:        type = packet_read(&plen);
                    618:        if (type == SSH_SMSG_SUCCESS) {
                    619:                debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
                    620:                return 1;
                    621:        }
                    622:        if (type != SSH_SMSG_FAILURE)
                    623:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    624:        debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
                    625:        return 0;
1.1       deraadt   626: }
                    627:
                    628: #ifdef KRB4
1.38      markus    629: int
                    630: try_kerberos_authentication()
1.1       deraadt   631: {
1.38      markus    632:        KTEXT_ST auth;          /* Kerberos data */
                    633:        char *reply;
                    634:        char inst[INST_SZ];
                    635:        char *realm;
                    636:        CREDENTIALS cred;
                    637:        int r, type, plen;
1.57      markus    638:        socklen_t slen;
1.38      markus    639:        Key_schedule schedule;
                    640:        u_long checksum, cksum;
                    641:        MSG_DAT msg_data;
                    642:        struct sockaddr_in local, foreign;
                    643:        struct stat st;
                    644:
                    645:        /* Don't do anything if we don't have any tickets. */
                    646:        if (stat(tkt_string(), &st) < 0)
                    647:                return 0;
                    648:
                    649:        strncpy(inst, (char *) krb_get_phost(get_canonical_hostname()), INST_SZ);
                    650:
                    651:        realm = (char *) krb_realmofhost(get_canonical_hostname());
                    652:        if (!realm) {
                    653:                debug("Kerberos V4: no realm for %s", get_canonical_hostname());
                    654:                return 0;
                    655:        }
                    656:        /* This can really be anything. */
                    657:        checksum = (u_long) getpid();
                    658:
                    659:        r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
                    660:        if (r != KSUCCESS) {
                    661:                debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]);
                    662:                return 0;
                    663:        }
                    664:        /* Get session key to decrypt the server's reply with. */
                    665:        r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
                    666:        if (r != KSUCCESS) {
                    667:                debug("get_cred failed: %s", krb_err_txt[r]);
                    668:                return 0;
                    669:        }
                    670:        des_key_sched((des_cblock *) cred.session, schedule);
                    671:
                    672:        /* Send authentication info to server. */
                    673:        packet_start(SSH_CMSG_AUTH_KERBEROS);
                    674:        packet_put_string((char *) auth.dat, auth.length);
                    675:        packet_send();
                    676:        packet_write_wait();
                    677:
                    678:        /* Zero the buffer. */
                    679:        (void) memset(auth.dat, 0, MAX_KTXT_LEN);
                    680:
1.57      markus    681:        slen = sizeof(local);
1.38      markus    682:        memset(&local, 0, sizeof(local));
                    683:        if (getsockname(packet_get_connection_in(),
1.57      markus    684:                        (struct sockaddr *) & local, &slen) < 0)
1.38      markus    685:                debug("getsockname failed: %s", strerror(errno));
                    686:
1.57      markus    687:        slen = sizeof(foreign);
1.38      markus    688:        memset(&foreign, 0, sizeof(foreign));
                    689:        if (getpeername(packet_get_connection_in(),
1.57      markus    690:                        (struct sockaddr *) & foreign, &slen) < 0) {
1.38      markus    691:                debug("getpeername failed: %s", strerror(errno));
                    692:                fatal_cleanup();
                    693:        }
                    694:        /* Get server reply. */
                    695:        type = packet_read(&plen);
                    696:        switch (type) {
                    697:        case SSH_SMSG_FAILURE:
                    698:                /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
                    699:                debug("Kerberos V4 authentication failed.");
                    700:                return 0;
                    701:                break;
                    702:
                    703:        case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
                    704:                /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
                    705:                debug("Kerberos V4 authentication accepted.");
                    706:
                    707:                /* Get server's response. */
                    708:                reply = packet_get_string((unsigned int *) &auth.length);
                    709:                memcpy(auth.dat, reply, auth.length);
                    710:                xfree(reply);
                    711:
                    712:                packet_integrity_check(plen, 4 + auth.length, type);
                    713:
1.40      markus    714:                /*
                    715:                 * If his response isn't properly encrypted with the session
                    716:                 * key, and the decrypted checksum fails to match, he's
                    717:                 * bogus. Bail out.
                    718:                 */
1.38      markus    719:                r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
                    720:                                &foreign, &local, &msg_data);
                    721:                if (r != KSUCCESS) {
                    722:                        debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]);
                    723:                        packet_disconnect("Kerberos V4 challenge failed!");
                    724:                }
                    725:                /* Fetch the (incremented) checksum that we supplied in the request. */
                    726:                (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum));
                    727:                cksum = ntohl(cksum);
                    728:
                    729:                /* If it matches, we're golden. */
                    730:                if (cksum == checksum + 1) {
                    731:                        debug("Kerberos V4 challenge successful.");
                    732:                        return 1;
                    733:                } else
                    734:                        packet_disconnect("Kerberos V4 challenge failed!");
                    735:                break;
                    736:
                    737:        default:
                    738:                packet_disconnect("Protocol error on Kerberos V4 response: %d", type);
                    739:        }
                    740:        return 0;
1.1       deraadt   741: }
1.38      markus    742:
1.1       deraadt   743: #endif /* KRB4 */
                    744:
                    745: #ifdef AFS
1.38      markus    746: int
                    747: send_kerberos_tgt()
1.1       deraadt   748: {
1.38      markus    749:        CREDENTIALS *creds;
                    750:        char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
                    751:        int r, type, plen;
1.57      markus    752:        char buffer[8192];
1.38      markus    753:        struct stat st;
                    754:
                    755:        /* Don't do anything if we don't have any tickets. */
                    756:        if (stat(tkt_string(), &st) < 0)
                    757:                return 0;
                    758:
                    759:        creds = xmalloc(sizeof(*creds));
                    760:
                    761:        if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) {
                    762:                debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]);
                    763:                return 0;
                    764:        }
                    765:        if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) {
                    766:                debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]);
                    767:                return 0;
                    768:        }
                    769:        if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
                    770:                debug("Kerberos V4 ticket expired: %s", TKT_FILE);
                    771:                return 0;
                    772:        }
1.57      markus    773:        creds_to_radix(creds, (unsigned char *)buffer);
1.38      markus    774:        xfree(creds);
                    775:
                    776:        packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
1.57      markus    777:        packet_put_string(buffer, strlen(buffer));
1.38      markus    778:        packet_send();
                    779:        packet_write_wait();
                    780:
                    781:        type = packet_read(&plen);
1.1       deraadt   782:
1.38      markus    783:        if (type == SSH_SMSG_FAILURE)
                    784:                debug("Kerberos TGT for realm %s rejected.", prealm);
                    785:        else if (type != SSH_SMSG_SUCCESS)
                    786:                packet_disconnect("Protocol error on Kerberos TGT response: %d", type);
                    787:
                    788:        return 1;
1.1       deraadt   789: }
                    790:
1.38      markus    791: void
                    792: send_afs_tokens(void)
1.1       deraadt   793: {
1.38      markus    794:        CREDENTIALS creds;
                    795:        struct ViceIoctl parms;
                    796:        struct ClearToken ct;
                    797:        int i, type, len, plen;
                    798:        char buf[2048], *p, *server_cell;
1.57      markus    799:        char buffer[8192];
1.38      markus    800:
                    801:        /* Move over ktc_GetToken, here's something leaner. */
                    802:        for (i = 0; i < 100; i++) {     /* just in case */
                    803:                parms.in = (char *) &i;
                    804:                parms.in_size = sizeof(i);
                    805:                parms.out = buf;
                    806:                parms.out_size = sizeof(buf);
                    807:                if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
                    808:                        break;
                    809:                p = buf;
                    810:
                    811:                /* Get secret token. */
                    812:                memcpy(&creds.ticket_st.length, p, sizeof(unsigned int));
                    813:                if (creds.ticket_st.length > MAX_KTXT_LEN)
                    814:                        break;
                    815:                p += sizeof(unsigned int);
                    816:                memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
                    817:                p += creds.ticket_st.length;
                    818:
                    819:                /* Get clear token. */
                    820:                memcpy(&len, p, sizeof(len));
                    821:                if (len != sizeof(struct ClearToken))
                    822:                        break;
                    823:                p += sizeof(len);
                    824:                memcpy(&ct, p, len);
                    825:                p += len;
                    826:                p += sizeof(len);       /* primary flag */
                    827:                server_cell = p;
                    828:
                    829:                /* Flesh out our credentials. */
                    830:                strlcpy(creds.service, "afs", sizeof creds.service);
                    831:                creds.instance[0] = '\0';
                    832:                strlcpy(creds.realm, server_cell, REALM_SZ);
                    833:                memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
                    834:                creds.issue_date = ct.BeginTimestamp;
                    835:                creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp);
                    836:                creds.kvno = ct.AuthHandle;
                    837:                snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
                    838:                creds.pinst[0] = '\0';
                    839:
                    840:                /* Encode token, ship it off. */
1.57      markus    841:                if (!creds_to_radix(&creds, (unsigned char*) buffer))
1.38      markus    842:                        break;
                    843:                packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
1.57      markus    844:                packet_put_string(buffer, strlen(buffer));
1.38      markus    845:                packet_send();
                    846:                packet_write_wait();
                    847:
                    848:                /* Roger, Roger. Clearance, Clarence. What's your vector,
                    849:                   Victor? */
                    850:                type = packet_read(&plen);
                    851:
                    852:                if (type == SSH_SMSG_FAILURE)
                    853:                        debug("AFS token for cell %s rejected.", server_cell);
                    854:                else if (type != SSH_SMSG_SUCCESS)
                    855:                        packet_disconnect("Protocol error on AFS token response: %d", type);
                    856:        }
1.1       deraadt   857: }
1.38      markus    858:
1.1       deraadt   859: #endif /* AFS */
                    860:
1.39      deraadt   861: /*
1.43      markus    862:  * Tries to authenticate with any string-based challenge/response system.
                    863:  * Note that the client code is not tied to s/key or TIS.
                    864:  */
                    865: int
                    866: try_skey_authentication()
                    867: {
1.57      markus    868:        int type, i;
                    869:        int payload_len;
                    870:        unsigned int clen;
1.43      markus    871:        char *challenge, *response;
                    872:
                    873:        debug("Doing skey authentication.");
                    874:
                    875:        /* request a challenge */
                    876:        packet_start(SSH_CMSG_AUTH_TIS);
                    877:        packet_send();
                    878:        packet_write_wait();
                    879:
                    880:        type = packet_read(&payload_len);
                    881:        if (type != SSH_SMSG_FAILURE &&
                    882:            type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
                    883:                packet_disconnect("Protocol error: got %d in response "
                    884:                                  "to skey-auth", type);
                    885:        }
                    886:        if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
                    887:                debug("No challenge for skey authentication.");
                    888:                return 0;
                    889:        }
1.57      markus    890:        challenge = packet_get_string(&clen);
                    891:        packet_integrity_check(payload_len, (4 + clen), type);
1.43      markus    892:        if (options.cipher == SSH_CIPHER_NONE)
                    893:                log("WARNING: Encryption is disabled! "
                    894:                    "Reponse will be transmitted in clear text.");
                    895:        fprintf(stderr, "%s\n", challenge);
1.54      markus    896:        xfree(challenge);
1.43      markus    897:        fflush(stderr);
                    898:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    899:                if (i != 0)
                    900:                        error("Permission denied, please try again.");
                    901:                response = read_passphrase("Response: ", 0);
                    902:                packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
                    903:                packet_put_string(response, strlen(response));
                    904:                memset(response, 0, strlen(response));
                    905:                xfree(response);
                    906:                packet_send();
                    907:                packet_write_wait();
                    908:                type = packet_read(&payload_len);
                    909:                if (type == SSH_SMSG_SUCCESS)
                    910:                        return 1;
                    911:                if (type != SSH_SMSG_FAILURE)
                    912:                        packet_disconnect("Protocol error: got %d in response "
                    913:                                          "to skey-auth-reponse", type);
                    914:        }
                    915:        /* failure */
                    916:        return 0;
                    917: }
                    918:
                    919: /*
                    920:  * Tries to authenticate with plain passwd authentication.
                    921:  */
                    922: int
                    923: try_password_authentication(char *prompt)
                    924: {
                    925:        int type, i, payload_len;
                    926:        char *password;
                    927:
                    928:        debug("Doing password authentication.");
                    929:        if (options.cipher == SSH_CIPHER_NONE)
                    930:                log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
                    931:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    932:                if (i != 0)
                    933:                        error("Permission denied, please try again.");
                    934:                password = read_passphrase(prompt, 0);
                    935:                packet_start(SSH_CMSG_AUTH_PASSWORD);
                    936:                packet_put_string(password, strlen(password));
                    937:                memset(password, 0, strlen(password));
                    938:                xfree(password);
                    939:                packet_send();
                    940:                packet_write_wait();
                    941:
                    942:                type = packet_read(&payload_len);
                    943:                if (type == SSH_SMSG_SUCCESS)
                    944:                        return 1;
                    945:                if (type != SSH_SMSG_FAILURE)
                    946:                        packet_disconnect("Protocol error: got %d in response to passwd auth", type);
                    947:        }
                    948:        /* failure */
                    949:        return 0;
                    950: }
                    951:
                    952: /*
1.39      deraadt   953:  * Waits for the server identification string, and sends our own
                    954:  * identification string.
                    955:  */
1.38      markus    956: void
                    957: ssh_exchange_identification()
1.1       deraadt   958: {
1.38      markus    959:        char buf[256], remote_version[256];     /* must be same size! */
                    960:        int remote_major, remote_minor, i;
                    961:        int connection_in = packet_get_connection_in();
                    962:        int connection_out = packet_get_connection_out();
                    963:
                    964:        /* Read other side\'s version identification. */
                    965:        for (i = 0; i < sizeof(buf) - 1; i++) {
1.56      markus    966:                int len = read(connection_in, &buf[i], 1);
                    967:                if (len < 0)
1.38      markus    968:                        fatal("ssh_exchange_identification: read: %.100s", strerror(errno));
1.56      markus    969:                if (len != 1)
                    970:                        fatal("ssh_exchange_identification: Connection closed by remote host");
1.38      markus    971:                if (buf[i] == '\r') {
                    972:                        buf[i] = '\n';
                    973:                        buf[i + 1] = 0;
                    974:                        break;
                    975:                }
                    976:                if (buf[i] == '\n') {
                    977:                        buf[i + 1] = 0;
                    978:                        break;
                    979:                }
                    980:        }
                    981:        buf[sizeof(buf) - 1] = 0;
                    982:
1.40      markus    983:        /*
                    984:         * Check that the versions match.  In future this might accept
                    985:         * several versions and set appropriate flags to handle them.
                    986:         */
1.38      markus    987:        if (sscanf(buf, "SSH-%d.%d-%[^\n]\n", &remote_major, &remote_minor,
                    988:                   remote_version) != 3)
                    989:                fatal("Bad remote protocol version identification: '%.100s'", buf);
                    990:        debug("Remote protocol version %d.%d, remote software version %.100s",
                    991:              remote_major, remote_minor, remote_version);
                    992:
                    993:        /* Check if the remote protocol version is too old. */
                    994:        if (remote_major == 1 && remote_minor < 3)
                    995:                fatal("Remote machine has too old SSH software version.");
                    996:
                    997:        /* We speak 1.3, too. */
                    998:        if (remote_major == 1 && remote_minor == 3) {
                    999:                enable_compat13();
1.53      markus   1000:                if (options.forward_agent) {
                   1001:                        log("Agent forwarding disabled for protocol 1.3");
1.38      markus   1002:                        options.forward_agent = 0;
                   1003:                }
                   1004:        }
1.1       deraadt  1005: #if 0
1.40      markus   1006:        /*
                   1007:         * Removed for now, to permit compatibility with latter versions. The
                   1008:         * server will reject our version and disconnect if it doesn't
                   1009:         * support it.
                   1010:         */
1.38      markus   1011:        if (remote_major != PROTOCOL_MAJOR)
                   1012:                fatal("Protocol major versions differ: %d vs. %d",
                   1013:                      PROTOCOL_MAJOR, remote_major);
1.1       deraadt  1014: #endif
                   1015:
1.38      markus   1016:        /* Send our own protocol version identification. */
                   1017:        snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n",
1.45      deraadt  1018:            PROTOCOL_MAJOR, PROTOCOL_MINOR, SSH_VERSION);
                   1019:        if (atomicio(write, connection_out, buf, strlen(buf)) != strlen(buf))
1.38      markus   1020:                fatal("write: %.100s", strerror(errno));
1.1       deraadt  1021: }
                   1022:
                   1023: int ssh_cipher_default = SSH_CIPHER_3DES;
                   1024:
1.38      markus   1025: int
                   1026: read_yes_or_no(const char *prompt, int defval)
1.1       deraadt  1027: {
1.38      markus   1028:        char buf[1024];
                   1029:        FILE *f;
                   1030:        int retval = -1;
                   1031:
                   1032:        if (isatty(0))
                   1033:                f = stdin;
                   1034:        else
                   1035:                f = fopen("/dev/tty", "rw");
                   1036:
                   1037:        if (f == NULL)
                   1038:                return 0;
                   1039:
                   1040:        fflush(stdout);
                   1041:
                   1042:        while (1) {
                   1043:                fprintf(stderr, "%s", prompt);
                   1044:                if (fgets(buf, sizeof(buf), f) == NULL) {
                   1045:                        /* Print a newline (the prompt probably didn\'t have one). */
                   1046:                        fprintf(stderr, "\n");
                   1047:                        strlcpy(buf, "no", sizeof buf);
                   1048:                }
                   1049:                /* Remove newline from response. */
                   1050:                if (strchr(buf, '\n'))
                   1051:                        *strchr(buf, '\n') = 0;
                   1052:
                   1053:                if (buf[0] == 0)
                   1054:                        retval = defval;
                   1055:                if (strcmp(buf, "yes") == 0)
                   1056:                        retval = 1;
                   1057:                if (strcmp(buf, "no") == 0)
                   1058:                        retval = 0;
                   1059:
                   1060:                if (retval != -1) {
                   1061:                        if (f != stdin)
                   1062:                                fclose(f);
                   1063:                        return retval;
                   1064:                }
1.1       deraadt  1065:        }
                   1066: }
                   1067:
1.39      deraadt  1068: /*
1.46      markus   1069:  * check whether the supplied host key is valid, return only if ok.
1.39      deraadt  1070:  */
1.46      markus   1071:
1.38      markus   1072: void
1.58    ! markus   1073: check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key)
1.1       deraadt  1074: {
1.58    ! markus   1075:        Key *file_key;
1.46      markus   1076:        char *ip = NULL;
1.38      markus   1077:        char hostline[1000], *hostp;
                   1078:        HostStatus host_status;
                   1079:        HostStatus ip_status;
1.49      markus   1080:        int local = 0, host_ip_differ = 0;
                   1081:        char ntop[NI_MAXHOST];
                   1082:
                   1083:        /*
                   1084:         * Force accepting of the host key for loopback/localhost. The
                   1085:         * problem is that if the home directory is NFS-mounted to multiple
                   1086:         * machines, localhost will refer to a different machine in each of
                   1087:         * them, and the user will get bogus HOST_CHANGED warnings.  This
                   1088:         * essentially disables host authentication for localhost; however,
                   1089:         * this is probably not a real problem.
                   1090:         */
                   1091:        switch (hostaddr->sa_family) {
                   1092:        case AF_INET:
                   1093:                local = (ntohl(((struct sockaddr_in *)hostaddr)->sin_addr.s_addr) >> 24) == IN_LOOPBACKNET;
                   1094:                break;
                   1095:        case AF_INET6:
                   1096:                local = IN6_IS_ADDR_LOOPBACK(&(((struct sockaddr_in6 *)hostaddr)->sin6_addr));
                   1097:                break;
                   1098:        default:
                   1099:                local = 0;
                   1100:                break;
                   1101:        }
                   1102:        if (local) {
                   1103:                debug("Forcing accepting of host key for loopback/localhost.");
                   1104:                return;
                   1105:        }
1.42      markus   1106:
                   1107:        /*
1.44      markus   1108:         * Turn off check_host_ip for proxy connects, since
1.42      markus   1109:         * we don't have the remote ip-address
                   1110:         */
                   1111:        if (options.proxy_command != NULL && options.check_host_ip)
                   1112:                options.check_host_ip = 0;
1.38      markus   1113:
1.49      markus   1114:        if (options.check_host_ip) {
                   1115:                if (getnameinfo(hostaddr, hostaddr->sa_len, ntop, sizeof(ntop),
                   1116:                    NULL, 0, NI_NUMERICHOST) != 0)
                   1117:                        fatal("check_host_key: getnameinfo failed");
                   1118:                ip = xstrdup(ntop);
                   1119:        }
1.38      markus   1120:
1.46      markus   1121:        /*
                   1122:         * Store the host key from the known host file in here so that we can
                   1123:         * compare it with the key for the IP address.
                   1124:         */
1.58    ! markus   1125:        file_key = key_new(host_key->type);
1.38      markus   1126:
1.40      markus   1127:        /*
                   1128:         * Check if the host key is present in the user\'s list of known
                   1129:         * hosts or in the systemwide list.
                   1130:         */
1.58    ! markus   1131:        host_status = check_host_in_hostfile(options.user_hostfile, host, host_key, file_key);
1.38      markus   1132:        if (host_status == HOST_NEW)
1.58    ! markus   1133:                host_status = check_host_in_hostfile(options.system_hostfile, host, host_key, file_key);
1.40      markus   1134:        /*
                   1135:         * Also perform check for the ip address, skip the check if we are
                   1136:         * localhost or the hostname was an ip address to begin with
                   1137:         */
1.38      markus   1138:        if (options.check_host_ip && !local && strcmp(host, ip)) {
1.58    ! markus   1139:                Key *ip_key = key_new(host_key->type);
        !          1140:                ip_status = check_host_in_hostfile(options.user_hostfile, ip, host_key, ip_key);
1.38      markus   1141:
                   1142:                if (ip_status == HOST_NEW)
1.58    ! markus   1143:                        ip_status = check_host_in_hostfile(options.system_hostfile, ip, host_key, ip_key);
1.38      markus   1144:                if (host_status == HOST_CHANGED &&
1.58    ! markus   1145:                    (ip_status != HOST_CHANGED || !key_equal(ip_key, file_key)))
1.38      markus   1146:                        host_ip_differ = 1;
                   1147:
1.58    ! markus   1148:                key_free(ip_key);
1.38      markus   1149:        } else
                   1150:                ip_status = host_status;
                   1151:
1.58    ! markus   1152:        key_free(file_key);
1.38      markus   1153:
                   1154:        switch (host_status) {
                   1155:        case HOST_OK:
                   1156:                /* The host is known and the key matches. */
                   1157:                debug("Host '%.200s' is known and matches the host key.", host);
                   1158:                if (options.check_host_ip) {
                   1159:                        if (ip_status == HOST_NEW) {
1.58    ! markus   1160:                                if (!add_host_to_hostfile(options.user_hostfile, ip, host_key))
1.38      markus   1161:                                        log("Failed to add the host key for IP address '%.30s' to the list of known hosts (%.30s).",
                   1162:                                            ip, options.user_hostfile);
                   1163:                                else
                   1164:                                        log("Warning: Permanently added host key for IP address '%.30s' to the list of known hosts.",
                   1165:                                            ip);
                   1166:                        } else if (ip_status != HOST_OK)
                   1167:                                log("Warning: the host key for '%.200s' differs from the key for the IP address '%.30s'",
                   1168:                                    host, ip);
                   1169:                }
                   1170:                break;
                   1171:        case HOST_NEW:
                   1172:                /* The host is new. */
                   1173:                if (options.strict_host_key_checking == 1) {
                   1174:                        /* User has requested strict host key checking.  We will not add the host key
                   1175:                           automatically.  The only alternative left is to abort. */
                   1176:                        fatal("No host key is known for %.200s and you have requested strict checking.", host);
                   1177:                } else if (options.strict_host_key_checking == 2) {
                   1178:                        /* The default */
                   1179:                        char prompt[1024];
1.58    ! markus   1180:                        char *fp = key_fingerprint(host_key);
1.38      markus   1181:                        snprintf(prompt, sizeof(prompt),
1.45      deraadt  1182:                            "The authenticity of host '%.200s' can't be established.\n"
1.58    ! markus   1183:                            "Key fingerprint is %s.\n"
1.45      deraadt  1184:                            "Are you sure you want to continue connecting (yes/no)? ",
1.58    ! markus   1185:                            host, fp);
1.38      markus   1186:                        if (!read_yes_or_no(prompt, -1))
                   1187:                                fatal("Aborted by user!\n");
                   1188:                }
                   1189:                if (options.check_host_ip && ip_status == HOST_NEW && strcmp(host, ip)) {
                   1190:                        snprintf(hostline, sizeof(hostline), "%s,%s", host, ip);
                   1191:                        hostp = hostline;
                   1192:                } else
                   1193:                        hostp = host;
                   1194:
                   1195:                /* If not in strict mode, add the key automatically to the local known_hosts file. */
1.58    ! markus   1196:                if (!add_host_to_hostfile(options.user_hostfile, hostp, host_key))
1.38      markus   1197:                        log("Failed to add the host to the list of known hosts (%.500s).",
                   1198:                            options.user_hostfile);
                   1199:                else
                   1200:                        log("Warning: Permanently added '%.200s' to the list of known hosts.",
                   1201:                            hostp);
                   1202:                break;
                   1203:        case HOST_CHANGED:
                   1204:                if (options.check_host_ip && host_ip_differ) {
                   1205:                        char *msg;
                   1206:                        if (ip_status == HOST_NEW)
                   1207:                                msg = "is unknown";
                   1208:                        else if (ip_status == HOST_OK)
                   1209:                                msg = "is unchanged";
                   1210:                        else
                   1211:                                msg = "has a different value";
                   1212:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1213:                        error("@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @");
                   1214:                        error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1215:                        error("The host key for %s has changed,", host);
                   1216:                        error("and the key for the according IP address %s", ip);
                   1217:                        error("%s. This could either mean that", msg);
                   1218:                        error("DNS SPOOFING is happening or the IP address for the host");
                   1219:                        error("and its host key have changed at the same time");
                   1220:                }
                   1221:                /* The host key has changed. */
                   1222:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.47      markus   1223:                error("@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @");
1.38      markus   1224:                error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                   1225:                error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
                   1226:                error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
                   1227:                error("It is also possible that the host key has just been changed.");
                   1228:                error("Please contact your system administrator.");
                   1229:                error("Add correct host key in %.100s to get rid of this message.",
                   1230:                      options.user_hostfile);
                   1231:
1.40      markus   1232:                /*
                   1233:                 * If strict host key checking is in use, the user will have
                   1234:                 * to edit the key manually and we can only abort.
                   1235:                 */
1.38      markus   1236:                if (options.strict_host_key_checking)
                   1237:                        fatal("Host key for %.200s has changed and you have requested strict checking.", host);
                   1238:
1.40      markus   1239:                /*
                   1240:                 * If strict host key checking has not been requested, allow
                   1241:                 * the connection but without password authentication or
                   1242:                 * agent forwarding.
                   1243:                 */
1.38      markus   1244:                if (options.password_authentication) {
                   1245:                        error("Password authentication is disabled to avoid trojan horses.");
                   1246:                        options.password_authentication = 0;
                   1247:                }
                   1248:                if (options.forward_agent) {
                   1249:                        error("Agent forwarding is disabled to avoid trojan horses.");
                   1250:                        options.forward_agent = 0;
                   1251:                }
1.40      markus   1252:                /*
                   1253:                 * XXX Should permit the user to change to use the new id.
                   1254:                 * This could be done by converting the host key to an
                   1255:                 * identifying sentence, tell that the host identifies itself
                   1256:                 * by that sentence, and ask the user if he/she whishes to
                   1257:                 * accept the authentication.
                   1258:                 */
1.38      markus   1259:                break;
                   1260:        }
                   1261:        if (options.check_host_ip)
                   1262:                xfree(ip);
1.46      markus   1263: }
1.58    ! markus   1264: void
        !          1265: check_rsa_host_key(char *host, struct sockaddr *hostaddr, RSA *host_key)
        !          1266: {
        !          1267:        Key k;
        !          1268:        k.type = KEY_RSA;
        !          1269:        k.rsa = host_key;
        !          1270:        check_host_key(host, hostaddr, &k);
        !          1271: }
1.46      markus   1272:
                   1273: /*
1.51      markus   1274:  * SSH1 key exchange
1.46      markus   1275:  */
                   1276: void
1.51      markus   1277: ssh_kex(char *host, struct sockaddr *hostaddr)
1.46      markus   1278: {
1.51      markus   1279:        int i;
1.46      markus   1280:        BIGNUM *key;
                   1281:        RSA *host_key;
                   1282:        RSA *public_key;
                   1283:        int bits, rbits;
                   1284:        unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1.51      markus   1285:        unsigned char cookie[8];
                   1286:        unsigned int supported_ciphers;
1.46      markus   1287:        unsigned int server_flags, client_flags;
                   1288:        int payload_len, clen, sum_len = 0;
                   1289:        u_int32_t rand = 0;
                   1290:
                   1291:        debug("Waiting for server public key.");
                   1292:
                   1293:        /* Wait for a public key packet from the server. */
                   1294:        packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
                   1295:
1.51      markus   1296:        /* Get cookie from the packet. */
1.46      markus   1297:        for (i = 0; i < 8; i++)
1.51      markus   1298:                cookie[i] = packet_get_char();
1.46      markus   1299:
                   1300:        /* Get the public key. */
                   1301:        public_key = RSA_new();
                   1302:        bits = packet_get_int();/* bits */
                   1303:        public_key->e = BN_new();
                   1304:        packet_get_bignum(public_key->e, &clen);
                   1305:        sum_len += clen;
                   1306:        public_key->n = BN_new();
                   1307:        packet_get_bignum(public_key->n, &clen);
                   1308:        sum_len += clen;
                   1309:
                   1310:        rbits = BN_num_bits(public_key->n);
                   1311:        if (bits != rbits) {
                   1312:                log("Warning: Server lies about size of server public key: "
                   1313:                    "actual size is %d bits vs. announced %d.", rbits, bits);
                   1314:                log("Warning: This may be due to an old implementation of ssh.");
                   1315:        }
                   1316:        /* Get the host key. */
                   1317:        host_key = RSA_new();
                   1318:        bits = packet_get_int();/* bits */
                   1319:        host_key->e = BN_new();
                   1320:        packet_get_bignum(host_key->e, &clen);
                   1321:        sum_len += clen;
                   1322:        host_key->n = BN_new();
                   1323:        packet_get_bignum(host_key->n, &clen);
                   1324:        sum_len += clen;
                   1325:
                   1326:        rbits = BN_num_bits(host_key->n);
                   1327:        if (bits != rbits) {
                   1328:                log("Warning: Server lies about size of server host key: "
                   1329:                    "actual size is %d bits vs. announced %d.", rbits, bits);
                   1330:                log("Warning: This may be due to an old implementation of ssh.");
                   1331:        }
                   1332:
                   1333:        /* Get protocol flags. */
                   1334:        server_flags = packet_get_int();
                   1335:        packet_set_protocol_flags(server_flags);
                   1336:
                   1337:        supported_ciphers = packet_get_int();
                   1338:        supported_authentications = packet_get_int();
                   1339:
                   1340:        debug("Received server public key (%d bits) and host key (%d bits).",
                   1341:              BN_num_bits(public_key->n), BN_num_bits(host_key->n));
                   1342:
                   1343:        packet_integrity_check(payload_len,
                   1344:                               8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
                   1345:                               SSH_SMSG_PUBLIC_KEY);
                   1346:
1.58    ! markus   1347:        check_rsa_host_key(host, hostaddr, host_key);
1.46      markus   1348:
                   1349:        client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
                   1350:
1.51      markus   1351:        compute_session_id(session_id, cookie, host_key->n, public_key->n);
1.38      markus   1352:
                   1353:        /* Generate a session key. */
                   1354:        arc4random_stir();
                   1355:
1.40      markus   1356:        /*
                   1357:         * Generate an encryption key for the session.   The key is a 256 bit
                   1358:         * random number, interpreted as a 32-byte key, with the least
                   1359:         * significant 8 bits being the first byte of the key.
                   1360:         */
1.38      markus   1361:        for (i = 0; i < 32; i++) {
                   1362:                if (i % 4 == 0)
                   1363:                        rand = arc4random();
                   1364:                session_key[i] = rand & 0xff;
                   1365:                rand >>= 8;
                   1366:        }
                   1367:
1.40      markus   1368:        /*
                   1369:         * According to the protocol spec, the first byte of the session key
                   1370:         * is the highest byte of the integer.  The session key is xored with
                   1371:         * the first 16 bytes of the session id.
                   1372:         */
1.38      markus   1373:        key = BN_new();
                   1374:        BN_set_word(key, 0);
                   1375:        for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
                   1376:                BN_lshift(key, key, 8);
                   1377:                if (i < 16)
                   1378:                        BN_add_word(key, session_key[i] ^ session_id[i]);
                   1379:                else
                   1380:                        BN_add_word(key, session_key[i]);
                   1381:        }
                   1382:
1.40      markus   1383:        /*
                   1384:         * Encrypt the integer using the public key and host key of the
                   1385:         * server (key with smaller modulus first).
                   1386:         */
1.38      markus   1387:        if (BN_cmp(public_key->n, host_key->n) < 0) {
                   1388:                /* Public key has smaller modulus. */
                   1389:                if (BN_num_bits(host_key->n) <
                   1390:                    BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) {
                   1391:                        fatal("respond_to_rsa_challenge: host_key %d < public_key %d + "
                   1392:                              "SSH_KEY_BITS_RESERVED %d",
                   1393:                              BN_num_bits(host_key->n),
                   1394:                              BN_num_bits(public_key->n),
                   1395:                              SSH_KEY_BITS_RESERVED);
                   1396:                }
                   1397:                rsa_public_encrypt(key, key, public_key);
                   1398:                rsa_public_encrypt(key, key, host_key);
                   1399:        } else {
                   1400:                /* Host key has smaller modulus (or they are equal). */
                   1401:                if (BN_num_bits(public_key->n) <
                   1402:                    BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) {
                   1403:                        fatal("respond_to_rsa_challenge: public_key %d < host_key %d + "
                   1404:                              "SSH_KEY_BITS_RESERVED %d",
                   1405:                              BN_num_bits(public_key->n),
                   1406:                              BN_num_bits(host_key->n),
                   1407:                              SSH_KEY_BITS_RESERVED);
                   1408:                }
                   1409:                rsa_public_encrypt(key, key, host_key);
                   1410:                rsa_public_encrypt(key, key, public_key);
                   1411:        }
                   1412:
1.52      markus   1413:        /* Destroy the public keys since we no longer need them. */
                   1414:        RSA_free(public_key);
                   1415:        RSA_free(host_key);
                   1416:
1.38      markus   1417:        if (options.cipher == SSH_CIPHER_NOT_SET) {
                   1418:                if (cipher_mask() & supported_ciphers & (1 << ssh_cipher_default))
                   1419:                        options.cipher = ssh_cipher_default;
                   1420:                else {
                   1421:                        debug("Cipher %s not supported, using %.100s instead.",
                   1422:                              cipher_name(ssh_cipher_default),
                   1423:                              cipher_name(SSH_FALLBACK_CIPHER));
                   1424:                        options.cipher = SSH_FALLBACK_CIPHER;
                   1425:                }
                   1426:        }
                   1427:        /* Check that the selected cipher is supported. */
                   1428:        if (!(supported_ciphers & (1 << options.cipher)))
                   1429:                fatal("Selected cipher type %.100s not supported by server.",
                   1430:                      cipher_name(options.cipher));
                   1431:
                   1432:        debug("Encryption type: %.100s", cipher_name(options.cipher));
                   1433:
                   1434:        /* Send the encrypted session key to the server. */
                   1435:        packet_start(SSH_CMSG_SESSION_KEY);
                   1436:        packet_put_char(options.cipher);
                   1437:
1.51      markus   1438:        /* Send the cookie back to the server. */
1.38      markus   1439:        for (i = 0; i < 8; i++)
1.51      markus   1440:                packet_put_char(cookie[i]);
1.38      markus   1441:
1.52      markus   1442:        /* Send and destroy the encrypted encryption key integer. */
1.38      markus   1443:        packet_put_bignum(key);
1.52      markus   1444:        BN_clear_free(key);
1.38      markus   1445:
                   1446:        /* Send protocol flags. */
1.46      markus   1447:        packet_put_int(client_flags);
1.38      markus   1448:
                   1449:        /* Send the packet now. */
                   1450:        packet_send();
                   1451:        packet_write_wait();
                   1452:
                   1453:        debug("Sent encrypted session key.");
                   1454:
                   1455:        /* Set the encryption key. */
                   1456:        packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
                   1457:
                   1458:        /* We will no longer need the session key here.  Destroy any extra copies. */
                   1459:        memset(session_key, 0, sizeof(session_key));
                   1460:
1.40      markus   1461:        /*
                   1462:         * Expect a success message from the server.  Note that this message
                   1463:         * will be received in encrypted form.
                   1464:         */
1.38      markus   1465:        packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
                   1466:
                   1467:        debug("Received encrypted confirmation.");
1.51      markus   1468: }
                   1469:
                   1470: /*
                   1471:  * Authenticate user
                   1472:  */
                   1473: void
                   1474: ssh_userauth(int host_key_valid, RSA *own_host_key,
                   1475:     uid_t original_real_uid, char *host)
                   1476: {
                   1477:        int i, type;
                   1478:        int payload_len;
                   1479:        struct passwd *pw;
                   1480:        const char *server_user, *local_user;
                   1481:
                   1482:        /* Get local user name.  Use it as server user if no user name was given. */
                   1483:        pw = getpwuid(original_real_uid);
                   1484:        if (!pw)
                   1485:                fatal("User id %d not found from user database.", original_real_uid);
                   1486:        local_user = xstrdup(pw->pw_name);
                   1487:        server_user = options.user ? options.user : local_user;
1.38      markus   1488:
                   1489:        /* Send the name of the user to log in as on the server. */
                   1490:        packet_start(SSH_CMSG_USER);
                   1491:        packet_put_string(server_user, strlen(server_user));
1.16      dugsong  1492:        packet_send();
                   1493:        packet_write_wait();
1.38      markus   1494:
1.40      markus   1495:        /*
                   1496:         * The server should respond with success if no authentication is
                   1497:         * needed (the user has no password).  Otherwise the server responds
                   1498:         * with failure.
                   1499:         */
1.16      dugsong  1500:        type = packet_read(&payload_len);
1.38      markus   1501:
                   1502:        /* check whether the connection was accepted without authentication. */
1.16      dugsong  1503:        if (type == SSH_SMSG_SUCCESS)
1.38      markus   1504:                return;
1.16      dugsong  1505:        if (type != SSH_SMSG_FAILURE)
1.38      markus   1506:                packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER",
                   1507:                                  type);
                   1508:
                   1509: #ifdef AFS
                   1510:        /* Try Kerberos tgt passing if the server supports it. */
                   1511:        if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
                   1512:            options.kerberos_tgt_passing) {
                   1513:                if (options.cipher == SSH_CIPHER_NONE)
                   1514:                        log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
                   1515:                (void) send_kerberos_tgt();
                   1516:        }
                   1517:        /* Try AFS token passing if the server supports it. */
                   1518:        if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
                   1519:            options.afs_token_passing && k_hasafs()) {
                   1520:                if (options.cipher == SSH_CIPHER_NONE)
                   1521:                        log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
                   1522:                send_afs_tokens();
                   1523:        }
                   1524: #endif /* AFS */
                   1525:
                   1526: #ifdef KRB4
                   1527:        if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
                   1528:            options.kerberos_authentication) {
                   1529:                debug("Trying Kerberos authentication.");
                   1530:                if (try_kerberos_authentication()) {
                   1531:                        /* The server should respond with success or failure. */
                   1532:                        type = packet_read(&payload_len);
                   1533:                        if (type == SSH_SMSG_SUCCESS)
                   1534:                                return;
                   1535:                        if (type != SSH_SMSG_FAILURE)
                   1536:                                packet_disconnect("Protocol error: got %d in response to Kerberos auth", type);
                   1537:                }
                   1538:        }
                   1539: #endif /* KRB4 */
                   1540:
1.40      markus   1541:        /*
                   1542:         * Use rhosts authentication if running in privileged socket and we
                   1543:         * do not wish to remain anonymous.
                   1544:         */
1.38      markus   1545:        if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
                   1546:            options.rhosts_authentication) {
                   1547:                debug("Trying rhosts authentication.");
                   1548:                packet_start(SSH_CMSG_AUTH_RHOSTS);
                   1549:                packet_put_string(local_user, strlen(local_user));
                   1550:                packet_send();
                   1551:                packet_write_wait();
                   1552:
                   1553:                /* The server should respond with success or failure. */
                   1554:                type = packet_read(&payload_len);
                   1555:                if (type == SSH_SMSG_SUCCESS)
                   1556:                        return;
                   1557:                if (type != SSH_SMSG_FAILURE)
                   1558:                        packet_disconnect("Protocol error: got %d in response to rhosts auth",
                   1559:                                          type);
                   1560:        }
1.40      markus   1561:        /*
                   1562:         * Try .rhosts or /etc/hosts.equiv authentication with RSA host
                   1563:         * authentication.
                   1564:         */
1.38      markus   1565:        if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
                   1566:            options.rhosts_rsa_authentication && host_key_valid) {
                   1567:                if (try_rhosts_rsa_authentication(local_user, own_host_key))
                   1568:                        return;
                   1569:        }
                   1570:        /* Try RSA authentication if the server supports it. */
                   1571:        if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
                   1572:            options.rsa_authentication) {
1.40      markus   1573:                /*
                   1574:                 * Try RSA authentication using the authentication agent. The
                   1575:                 * agent is tried first because no passphrase is needed for
                   1576:                 * it, whereas identity files may require passphrases.
                   1577:                 */
1.38      markus   1578:                if (try_agent_authentication())
                   1579:                        return;
                   1580:
                   1581:                /* Try RSA authentication for each identity. */
                   1582:                for (i = 0; i < options.num_identity_files; i++)
1.43      markus   1583:                        if (try_rsa_authentication(options.identity_files[i]))
1.38      markus   1584:                                return;
                   1585:        }
                   1586:        /* Try skey authentication if the server supports it. */
                   1587:        if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
                   1588:            options.skey_authentication && !options.batch_mode) {
1.43      markus   1589:                if (try_skey_authentication())
                   1590:                        return;
1.38      markus   1591:        }
                   1592:        /* Try password authentication if the server supports it. */
                   1593:        if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
                   1594:            options.password_authentication && !options.batch_mode) {
                   1595:                char prompt[80];
1.45      deraadt  1596:
1.43      markus   1597:                snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
1.45      deraadt  1598:                    server_user, host);
1.43      markus   1599:                if (try_password_authentication(prompt))
                   1600:                        return;
1.38      markus   1601:        }
                   1602:        /* All authentication methods have failed.  Exit with an error message. */
                   1603:        fatal("Permission denied.");
                   1604:        /* NOTREACHED */
1.51      markus   1605: }
                   1606: /*
                   1607:  * Starts a dialog with the server, and authenticates the current user on the
                   1608:  * server.  This does not need any extra privileges.  The basic connection
                   1609:  * to the server must already have been established before this is called.
                   1610:  * If login fails, this function prints an error and never returns.
                   1611:  * This function does not require super-user privileges.
                   1612:  */
                   1613: void
                   1614: ssh_login(int host_key_valid, RSA *own_host_key, const char *orighost,
                   1615:     struct sockaddr *hostaddr, uid_t original_real_uid)
                   1616: {
                   1617:        char *host, *cp;
                   1618:
                   1619:        /* Convert the user-supplied hostname into all lowercase. */
                   1620:        host = xstrdup(orighost);
                   1621:        for (cp = host; *cp; cp++)
                   1622:                if (isupper(*cp))
                   1623:                        *cp = tolower(*cp);
                   1624:
                   1625:        /* Exchange protocol version identification strings with the server. */
                   1626:        ssh_exchange_identification();
                   1627:
                   1628:        /* Put the connection into non-blocking mode. */
                   1629:        packet_set_nonblocking();
                   1630:
                   1631:        supported_authentications = 0;
                   1632:        /* key exchange */
                   1633:        ssh_kex(host, hostaddr);
                   1634:        if (supported_authentications == 0)
                   1635:                fatal("supported_authentications == 0.");
1.58    ! markus   1636:
1.51      markus   1637:        /* authenticate user */
                   1638:        ssh_userauth(host_key_valid, own_host_key, original_real_uid, host);
1.1       deraadt  1639: }