[BACK]Return to monitor_wrap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.46

1.46    ! stevesk     1: /* $OpenBSD: monitor_wrap.c,v 1.45 2006/03/30 09:58:15 djm Exp $ */
1.1       provos      2: /*
                      3:  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
                      4:  * Copyright 2002 Markus Friedl <markus@openbsd.org>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: #include "includes.h"
                     29:
1.46    ! stevesk    30: #include <sys/types.h>
        !            31:
1.1       provos     32: #include <openssl/bn.h>
                     33: #include <openssl/dh.h>
1.46    ! stevesk    34:
        !            35: #include <pwd.h>
1.1       provos     36:
                     37: #include "ssh.h"
                     38: #include "dh.h"
                     39: #include "kex.h"
                     40: #include "auth.h"
1.22      markus     41: #include "auth-options.h"
1.1       provos     42: #include "buffer.h"
                     43: #include "bufaux.h"
                     44: #include "packet.h"
                     45: #include "mac.h"
                     46: #include "log.h"
                     47: #include "zlib.h"
                     48: #include "monitor.h"
                     49: #include "monitor_wrap.h"
                     50: #include "xmalloc.h"
                     51: #include "atomicio.h"
                     52: #include "monitor_fdpass.h"
1.45      djm        53: #include "misc.h"
1.1       provos     54:
                     55: #include "auth.h"
                     56: #include "channels.h"
                     57: #include "session.h"
                     58:
1.29      markus     59: #ifdef GSSAPI
                     60: #include "ssh-gss.h"
                     61: #endif
                     62:
1.1       provos     63: /* Imports */
                     64: extern int compat20;
                     65: extern Newkeys *newkeys[];
                     66: extern z_stream incoming_stream;
                     67: extern z_stream outgoing_stream;
1.7       mouring    68: extern struct monitor *pmonitor;
1.1       provos     69: extern Buffer input, output;
1.39      dtucker    70: extern Buffer loginmsg;
1.1       provos     71:
1.32      markus     72: int
                     73: mm_is_monitor(void)
                     74: {
                     75:        /*
                     76:         * m_pid is only set in the privileged part, and
                     77:         * points to the unprivileged child.
                     78:         */
1.34      markus     79:        return (pmonitor && pmonitor->m_pid > 0);
1.32      markus     80: }
                     81:
1.1       provos     82: void
1.36      avsm       83: mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos     84: {
1.13      deraadt    85:        u_int mlen = buffer_len(m);
1.1       provos     86:        u_char buf[5];
                     87:
1.8       markus     88:        debug3("%s entering: type %d", __func__, type);
1.1       provos     89:
1.45      djm        90:        put_u32(buf, mlen + 1);
1.10      deraadt    91:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
1.36      avsm       92:        if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
1.40      avsm       93:                fatal("%s: write: %s", __func__, strerror(errno));
1.36      avsm       94:        if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
1.40      avsm       95:                fatal("%s: write: %s", __func__, strerror(errno));
1.1       provos     96: }
                     97:
                     98: void
1.36      avsm       99: mm_request_receive(int sock, Buffer *m)
1.1       provos    100: {
                    101:        u_char buf[4];
1.13      deraadt   102:        u_int msg_len;
1.1       provos    103:
1.8       markus    104:        debug3("%s entering", __func__);
1.1       provos    105:
1.40      avsm      106:        if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
                    107:                if (errno == EPIPE)
1.32      markus    108:                        cleanup_exit(255);
1.40      avsm      109:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    110:        }
1.45      djm       111:        msg_len = get_u32(buf);
1.1       provos    112:        if (msg_len > 256 * 1024)
1.8       markus    113:                fatal("%s: read: bad msg_len %d", __func__, msg_len);
1.1       provos    114:        buffer_clear(m);
                    115:        buffer_append_space(m, msg_len);
1.40      avsm      116:        if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
                    117:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    118: }
                    119:
                    120: void
1.36      avsm      121: mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos    122: {
                    123:        u_char rtype;
                    124:
1.8       markus    125:        debug3("%s entering: type %d", __func__, type);
1.1       provos    126:
1.36      avsm      127:        mm_request_receive(sock, m);
1.1       provos    128:        rtype = buffer_get_char(m);
                    129:        if (rtype != type)
1.8       markus    130:                fatal("%s: read: rtype %d != type %d", __func__,
1.1       provos    131:                    rtype, type);
                    132: }
                    133:
                    134: DH *
                    135: mm_choose_dh(int min, int nbits, int max)
                    136: {
                    137:        BIGNUM *p, *g;
                    138:        int success = 0;
                    139:        Buffer m;
                    140:
                    141:        buffer_init(&m);
                    142:        buffer_put_int(&m, min);
                    143:        buffer_put_int(&m, nbits);
                    144:        buffer_put_int(&m, max);
                    145:
1.7       mouring   146:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
1.1       provos    147:
1.8       markus    148:        debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
1.7       mouring   149:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
1.1       provos    150:
                    151:        success = buffer_get_char(&m);
                    152:        if (success == 0)
1.8       markus    153:                fatal("%s: MONITOR_ANS_MODULI failed", __func__);
1.1       provos    154:
                    155:        if ((p = BN_new()) == NULL)
1.8       markus    156:                fatal("%s: BN_new failed", __func__);
1.3       markus    157:        if ((g = BN_new()) == NULL)
1.8       markus    158:                fatal("%s: BN_new failed", __func__);
1.1       provos    159:        buffer_get_bignum2(&m, p);
                    160:        buffer_get_bignum2(&m, g);
                    161:
1.8       markus    162:        debug3("%s: remaining %d", __func__, buffer_len(&m));
1.1       provos    163:        buffer_free(&m);
                    164:
                    165:        return (dh_new_group(g, p));
                    166: }
                    167:
                    168: int
                    169: mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
                    170: {
1.7       mouring   171:        Kex *kex = *pmonitor->m_pkex;
1.1       provos    172:        Buffer m;
                    173:
1.8       markus    174:        debug3("%s entering", __func__);
1.1       provos    175:
                    176:        buffer_init(&m);
                    177:        buffer_put_int(&m, kex->host_key_index(key));
                    178:        buffer_put_string(&m, data, datalen);
                    179:
1.7       mouring   180:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
1.1       provos    181:
1.8       markus    182:        debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
1.7       mouring   183:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
1.1       provos    184:        *sigp  = buffer_get_string(&m, lenp);
                    185:        buffer_free(&m);
                    186:
                    187:        return (0);
                    188: }
                    189:
                    190: struct passwd *
1.37      dtucker   191: mm_getpwnamallow(const char *username)
1.1       provos    192: {
                    193:        Buffer m;
                    194:        struct passwd *pw;
                    195:        u_int pwlen;
                    196:
1.8       markus    197:        debug3("%s entering", __func__);
1.1       provos    198:
                    199:        buffer_init(&m);
1.37      dtucker   200:        buffer_put_cstring(&m, username);
1.1       provos    201:
1.7       mouring   202:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
1.1       provos    203:
1.8       markus    204:        debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
1.7       mouring   205:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
1.1       provos    206:
                    207:        if (buffer_get_char(&m) == 0) {
                    208:                buffer_free(&m);
                    209:                return (NULL);
                    210:        }
                    211:        pw = buffer_get_string(&m, &pwlen);
                    212:        if (pwlen != sizeof(struct passwd))
1.8       markus    213:                fatal("%s: struct passwd size mismatch", __func__);
1.1       provos    214:        pw->pw_name = buffer_get_string(&m, NULL);
                    215:        pw->pw_passwd = buffer_get_string(&m, NULL);
                    216:        pw->pw_gecos = buffer_get_string(&m, NULL);
                    217:        pw->pw_class = buffer_get_string(&m, NULL);
                    218:        pw->pw_dir = buffer_get_string(&m, NULL);
                    219:        pw->pw_shell = buffer_get_string(&m, NULL);
                    220:        buffer_free(&m);
                    221:
                    222:        return (pw);
1.6       djm       223: }
                    224:
1.33      markus    225: char *
                    226: mm_auth2_read_banner(void)
1.6       djm       227: {
                    228:        Buffer m;
                    229:        char *banner;
                    230:
1.8       markus    231:        debug3("%s entering", __func__);
1.6       djm       232:
                    233:        buffer_init(&m);
1.7       mouring   234:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       235:        buffer_clear(&m);
                    236:
1.33      markus    237:        mm_request_receive_expect(pmonitor->m_recvfd,
                    238:            MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       239:        banner = buffer_get_string(&m, NULL);
                    240:        buffer_free(&m);
1.10      deraadt   241:
1.33      markus    242:        /* treat empty banner as missing banner */
                    243:        if (strlen(banner) == 0) {
                    244:                xfree(banner);
                    245:                banner = NULL;
                    246:        }
1.6       djm       247:        return (banner);
1.1       provos    248: }
                    249:
                    250: /* Inform the privileged process about service and style */
                    251:
                    252: void
                    253: mm_inform_authserv(char *service, char *style)
                    254: {
                    255:        Buffer m;
                    256:
1.8       markus    257:        debug3("%s entering", __func__);
1.1       provos    258:
                    259:        buffer_init(&m);
                    260:        buffer_put_cstring(&m, service);
                    261:        buffer_put_cstring(&m, style ? style : "");
                    262:
1.7       mouring   263:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    264:
                    265:        buffer_free(&m);
                    266: }
                    267:
                    268: /* Do the password authentication */
                    269: int
                    270: mm_auth_password(Authctxt *authctxt, char *password)
                    271: {
                    272:        Buffer m;
                    273:        int authenticated = 0;
                    274:
1.8       markus    275:        debug3("%s entering", __func__);
1.1       provos    276:
                    277:        buffer_init(&m);
                    278:        buffer_put_cstring(&m, password);
1.7       mouring   279:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    280:
1.8       markus    281:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   282:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    283:
                    284:        authenticated = buffer_get_int(&m);
                    285:
                    286:        buffer_free(&m);
                    287:
1.3       markus    288:        debug3("%s: user %sauthenticated",
1.8       markus    289:            __func__, authenticated ? "" : "not ");
1.1       provos    290:        return (authenticated);
                    291: }
                    292:
                    293: int
                    294: mm_user_key_allowed(struct passwd *pw, Key *key)
                    295: {
                    296:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
                    297: }
                    298:
                    299: int
                    300: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
                    301:     Key *key)
                    302: {
                    303:        return (mm_key_allowed(MM_HOSTKEY, user, host, key));
                    304: }
                    305:
                    306: int
                    307: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
                    308:     char *host, Key *key)
                    309: {
                    310:        int ret;
                    311:
                    312:        key->type = KEY_RSA; /* XXX hack for key_to_blob */
                    313:        ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
                    314:        key->type = KEY_RSA1;
                    315:        return (ret);
                    316: }
                    317:
1.2       markus    318: static void
1.1       provos    319: mm_send_debug(Buffer *m)
                    320: {
                    321:        char *msg;
                    322:
                    323:        while (buffer_len(m)) {
                    324:                msg = buffer_get_string(m, NULL);
1.8       markus    325:                debug3("%s: Sending debug: %s", __func__, msg);
1.1       provos    326:                packet_send_debug("%s", msg);
                    327:                xfree(msg);
                    328:        }
                    329: }
                    330:
                    331: int
                    332: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
                    333: {
                    334:        Buffer m;
                    335:        u_char *blob;
                    336:        u_int len;
1.22      markus    337:        int allowed = 0, have_forced = 0;
1.1       provos    338:
1.8       markus    339:        debug3("%s entering", __func__);
1.1       provos    340:
                    341:        /* Convert the key to a blob and the pass it over */
                    342:        if (!key_to_blob(key, &blob, &len))
                    343:                return (0);
                    344:
                    345:        buffer_init(&m);
                    346:        buffer_put_int(&m, type);
                    347:        buffer_put_cstring(&m, user ? user : "");
                    348:        buffer_put_cstring(&m, host ? host : "");
                    349:        buffer_put_string(&m, blob, len);
                    350:        xfree(blob);
                    351:
1.7       mouring   352:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    353:
1.8       markus    354:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7       mouring   355:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    356:
                    357:        allowed = buffer_get_int(&m);
                    358:
1.22      markus    359:        /* fake forced command */
                    360:        auth_clear_options();
                    361:        have_forced = buffer_get_int(&m);
                    362:        forced_command = have_forced ? xstrdup("true") : NULL;
                    363:
1.1       provos    364:        /* Send potential debug messages */
                    365:        mm_send_debug(&m);
                    366:
                    367:        buffer_free(&m);
                    368:
                    369:        return (allowed);
                    370: }
                    371:
1.3       markus    372: /*
1.1       provos    373:  * This key verify needs to send the key type along, because the
                    374:  * privileged parent makes the decision if the key is allowed
                    375:  * for authentication.
                    376:  */
                    377:
                    378: int
                    379: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
                    380: {
                    381:        Buffer m;
                    382:        u_char *blob;
                    383:        u_int len;
                    384:        int verified = 0;
                    385:
1.8       markus    386:        debug3("%s entering", __func__);
1.1       provos    387:
                    388:        /* Convert the key to a blob and the pass it over */
                    389:        if (!key_to_blob(key, &blob, &len))
                    390:                return (0);
                    391:
                    392:        buffer_init(&m);
                    393:        buffer_put_string(&m, blob, len);
                    394:        buffer_put_string(&m, sig, siglen);
                    395:        buffer_put_string(&m, data, datalen);
                    396:        xfree(blob);
                    397:
1.7       mouring   398:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    399:
1.8       markus    400:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   401:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    402:
                    403:        verified = buffer_get_int(&m);
                    404:
                    405:        buffer_free(&m);
                    406:
                    407:        return (verified);
                    408: }
                    409:
                    410: /* Export key state after authentication */
                    411: Newkeys *
                    412: mm_newkeys_from_blob(u_char *blob, int blen)
                    413: {
                    414:        Buffer b;
                    415:        u_int len;
                    416:        Newkeys *newkey = NULL;
                    417:        Enc *enc;
                    418:        Mac *mac;
                    419:        Comp *comp;
                    420:
1.8       markus    421:        debug3("%s: %p(%d)", __func__, blob, blen);
1.1       provos    422: #ifdef DEBUG_PK
                    423:        dump_base64(stderr, blob, blen);
                    424: #endif
                    425:        buffer_init(&b);
                    426:        buffer_append(&b, blob, blen);
                    427:
                    428:        newkey = xmalloc(sizeof(*newkey));
                    429:        enc = &newkey->enc;
                    430:        mac = &newkey->mac;
                    431:        comp = &newkey->comp;
                    432:
                    433:        /* Enc structure */
                    434:        enc->name = buffer_get_string(&b, NULL);
                    435:        buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
                    436:        enc->enabled = buffer_get_int(&b);
                    437:        enc->block_size = buffer_get_int(&b);
                    438:        enc->key = buffer_get_string(&b, &enc->key_len);
                    439:        enc->iv = buffer_get_string(&b, &len);
                    440:        if (len != enc->block_size)
1.12      deraadt   441:                fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1       provos    442:                    enc->block_size, len);
                    443:
                    444:        if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8       markus    445:                fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1       provos    446:                    enc->name, enc->cipher);
                    447:
                    448:        /* Mac structure */
                    449:        mac->name = buffer_get_string(&b, NULL);
                    450:        if (mac->name == NULL || mac_init(mac, mac->name) == -1)
1.8       markus    451:                fatal("%s: can not init mac %s", __func__, mac->name);
1.1       provos    452:        mac->enabled = buffer_get_int(&b);
                    453:        mac->key = buffer_get_string(&b, &len);
                    454:        if (len > mac->key_len)
1.12      deraadt   455:                fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1       provos    456:                    mac->key_len);
                    457:        mac->key_len = len;
                    458:
                    459:        /* Comp structure */
                    460:        comp->type = buffer_get_int(&b);
                    461:        comp->enabled = buffer_get_int(&b);
                    462:        comp->name = buffer_get_string(&b, NULL);
                    463:
                    464:        len = buffer_len(&b);
                    465:        if (len != 0)
1.12      deraadt   466:                error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1       provos    467:        buffer_free(&b);
                    468:        return (newkey);
                    469: }
                    470:
                    471: int
                    472: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
                    473: {
                    474:        Buffer b;
                    475:        int len;
                    476:        Enc *enc;
                    477:        Mac *mac;
                    478:        Comp *comp;
                    479:        Newkeys *newkey = newkeys[mode];
                    480:
1.8       markus    481:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    482:
                    483:        if (newkey == NULL) {
1.8       markus    484:                error("%s: newkey == NULL", __func__);
1.1       provos    485:                return 0;
                    486:        }
                    487:        enc = &newkey->enc;
                    488:        mac = &newkey->mac;
                    489:        comp = &newkey->comp;
                    490:
                    491:        buffer_init(&b);
                    492:        /* Enc structure */
                    493:        buffer_put_cstring(&b, enc->name);
                    494:        /* The cipher struct is constant and shared, you export pointer */
                    495:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    496:        buffer_put_int(&b, enc->enabled);
                    497:        buffer_put_int(&b, enc->block_size);
                    498:        buffer_put_string(&b, enc->key, enc->key_len);
                    499:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    500:        buffer_put_string(&b, enc->iv, enc->block_size);
                    501:
                    502:        /* Mac structure */
                    503:        buffer_put_cstring(&b, mac->name);
                    504:        buffer_put_int(&b, mac->enabled);
                    505:        buffer_put_string(&b, mac->key, mac->key_len);
                    506:
                    507:        /* Comp structure */
                    508:        buffer_put_int(&b, comp->type);
                    509:        buffer_put_int(&b, comp->enabled);
                    510:        buffer_put_cstring(&b, comp->name);
                    511:
                    512:        len = buffer_len(&b);
1.16      markus    513:        if (lenp != NULL)
                    514:                *lenp = len;
                    515:        if (blobp != NULL) {
                    516:                *blobp = xmalloc(len);
                    517:                memcpy(*blobp, buffer_ptr(&b), len);
                    518:        }
1.1       provos    519:        memset(buffer_ptr(&b), 0, len);
                    520:        buffer_free(&b);
                    521:        return len;
                    522: }
                    523:
1.2       markus    524: static void
1.1       provos    525: mm_send_kex(Buffer *m, Kex *kex)
                    526: {
                    527:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    528:        buffer_put_int(m, kex->we_need);
                    529:        buffer_put_int(m, kex->hostkey_type);
                    530:        buffer_put_int(m, kex->kex_type);
                    531:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    532:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    533:        buffer_put_int(m, kex->flags);
                    534:        buffer_put_cstring(m, kex->client_version_string);
                    535:        buffer_put_cstring(m, kex->server_version_string);
                    536: }
                    537:
                    538: void
1.36      avsm      539: mm_send_keystate(struct monitor *monitor)
1.1       provos    540: {
                    541:        Buffer m;
                    542:        u_char *blob, *p;
                    543:        u_int bloblen, plen;
1.25      markus    544:        u_int32_t seqnr, packets;
                    545:        u_int64_t blocks;
1.1       provos    546:
                    547:        buffer_init(&m);
                    548:
                    549:        if (!compat20) {
                    550:                u_char iv[24];
1.11      markus    551:                u_char *key;
                    552:                u_int ivlen, keylen;
1.1       provos    553:
                    554:                buffer_put_int(&m, packet_get_protocol_flags());
                    555:
                    556:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    557:
1.11      markus    558:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    559:                keylen = packet_get_encryption_key(NULL);
                    560:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    561:                keylen = packet_get_encryption_key(key);
                    562:                buffer_put_string(&m, key, keylen);
                    563:                memset(key, 0, keylen);
                    564:                xfree(key);
                    565:
1.1       provos    566:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    567:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    568:                buffer_put_string(&m, iv, ivlen);
                    569:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    570:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    571:                buffer_put_string(&m, iv, ivlen);
                    572:                goto skip;
                    573:        } else {
                    574:                /* Kex for rekeying */
1.36      avsm      575:                mm_send_kex(&m, *monitor->m_pkex);
1.1       provos    576:        }
                    577:
                    578:        debug3("%s: Sending new keys: %p %p",
1.8       markus    579:            __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1       provos    580:
                    581:        /* Keys from Kex */
                    582:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    583:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    584:
                    585:        buffer_put_string(&m, blob, bloblen);
                    586:        xfree(blob);
                    587:
                    588:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    589:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    590:
                    591:        buffer_put_string(&m, blob, bloblen);
                    592:        xfree(blob);
                    593:
1.25      markus    594:        packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
                    595:        buffer_put_int(&m, seqnr);
                    596:        buffer_put_int64(&m, blocks);
                    597:        buffer_put_int(&m, packets);
1.26      markus    598:        packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
1.25      markus    599:        buffer_put_int(&m, seqnr);
                    600:        buffer_put_int64(&m, blocks);
                    601:        buffer_put_int(&m, packets);
1.1       provos    602:
1.8       markus    603:        debug3("%s: New keys have been sent", __func__);
1.1       provos    604:  skip:
                    605:        /* More key context */
                    606:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    607:        p = xmalloc(plen+1);
                    608:        packet_get_keycontext(MODE_OUT, p);
                    609:        buffer_put_string(&m, p, plen);
                    610:        xfree(p);
                    611:
                    612:        plen = packet_get_keycontext(MODE_IN, NULL);
                    613:        p = xmalloc(plen+1);
                    614:        packet_get_keycontext(MODE_IN, p);
                    615:        buffer_put_string(&m, p, plen);
                    616:        xfree(p);
                    617:
                    618:        /* Compression state */
1.8       markus    619:        debug3("%s: Sending compression state", __func__);
1.1       provos    620:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    621:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    622:
                    623:        /* Network I/O buffers */
                    624:        buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
                    625:        buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
                    626:
1.36      avsm      627:        mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    628:        debug3("%s: Finished sending state", __func__);
1.1       provos    629:
                    630:        buffer_free(&m);
                    631: }
                    632:
                    633: int
1.42      deraadt   634: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1       provos    635: {
                    636:        Buffer m;
1.39      dtucker   637:        char *p, *msg;
1.1       provos    638:        int success = 0;
                    639:
                    640:        buffer_init(&m);
1.7       mouring   641:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    642:
1.8       markus    643:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   644:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    645:
                    646:        success = buffer_get_int(&m);
                    647:        if (success == 0) {
1.8       markus    648:                debug3("%s: pty alloc failed", __func__);
1.1       provos    649:                buffer_free(&m);
                    650:                return (0);
                    651:        }
                    652:        p = buffer_get_string(&m, NULL);
1.39      dtucker   653:        msg = buffer_get_string(&m, NULL);
1.1       provos    654:        buffer_free(&m);
                    655:
                    656:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    657:        xfree(p);
1.39      dtucker   658:
                    659:        buffer_append(&loginmsg, msg, strlen(msg));
                    660:        xfree(msg);
1.1       provos    661:
1.7       mouring   662:        *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
                    663:        *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1.1       provos    664:
                    665:        /* Success */
                    666:        return (1);
                    667: }
                    668:
                    669: void
1.32      markus    670: mm_session_pty_cleanup2(Session *s)
1.1       provos    671: {
                    672:        Buffer m;
                    673:
                    674:        if (s->ttyfd == -1)
                    675:                return;
                    676:        buffer_init(&m);
                    677:        buffer_put_cstring(&m, s->tty);
1.7       mouring   678:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    679:        buffer_free(&m);
1.3       markus    680:
1.1       provos    681:        /* closed dup'ed master */
                    682:        if (close(s->ptymaster) < 0)
                    683:                error("close(s->ptymaster): %s", strerror(errno));
                    684:
                    685:        /* unlink pty from session */
                    686:        s->ttyfd = -1;
                    687: }
                    688:
                    689: /* Request process termination */
                    690:
                    691: void
                    692: mm_terminate(void)
                    693: {
                    694:        Buffer m;
                    695:
                    696:        buffer_init(&m);
1.7       mouring   697:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    698:        buffer_free(&m);
                    699: }
                    700:
                    701: int
                    702: mm_ssh1_session_key(BIGNUM *num)
                    703: {
                    704:        int rsafail;
                    705:        Buffer m;
                    706:
                    707:        buffer_init(&m);
                    708:        buffer_put_bignum2(&m, num);
1.7       mouring   709:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    710:
1.7       mouring   711:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    712:
                    713:        rsafail = buffer_get_int(&m);
                    714:        buffer_get_bignum2(&m, num);
                    715:
                    716:        buffer_free(&m);
                    717:
                    718:        return (rsafail);
                    719: }
                    720:
1.2       markus    721: static void
1.1       provos    722: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    723:     char ***prompts, u_int **echo_on)
                    724: {
1.10      deraadt   725:        *name = xstrdup("");
                    726:        *infotxt = xstrdup("");
1.1       provos    727:        *numprompts = 1;
1.43      djm       728:        *prompts = xcalloc(*numprompts, sizeof(char *));
                    729:        *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1       provos    730:        (*echo_on)[0] = 0;
                    731: }
                    732:
                    733: int
                    734: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    735:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    736: {
                    737:        Buffer m;
1.21      markus    738:        u_int success;
1.1       provos    739:        char *challenge;
                    740:
1.8       markus    741:        debug3("%s: entering", __func__);
1.1       provos    742:
                    743:        buffer_init(&m);
1.7       mouring   744:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    745:
1.7       mouring   746:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    747:            &m);
1.21      markus    748:        success = buffer_get_int(&m);
                    749:        if (success == 0) {
1.8       markus    750:                debug3("%s: no challenge", __func__);
1.1       provos    751:                buffer_free(&m);
                    752:                return (-1);
                    753:        }
                    754:
                    755:        /* Get the challenge, and format the response */
                    756:        challenge  = buffer_get_string(&m, NULL);
                    757:        buffer_free(&m);
                    758:
                    759:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    760:        (*prompts)[0] = challenge;
                    761:
1.8       markus    762:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    763:
                    764:        return (0);
                    765: }
                    766:
                    767: int
                    768: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    769: {
                    770:        Buffer m;
                    771:        int authok;
                    772:
1.8       markus    773:        debug3("%s: entering", __func__);
1.1       provos    774:        if (numresponses != 1)
                    775:                return (-1);
                    776:
                    777:        buffer_init(&m);
                    778:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   779:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    780:
1.7       mouring   781:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    782:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    783:
                    784:        authok = buffer_get_int(&m);
                    785:        buffer_free(&m);
                    786:
                    787:        return ((authok == 0) ? -1 : 0);
                    788: }
                    789:
1.38      dtucker   790: #ifdef SKEY
1.1       provos    791: int
                    792: mm_skey_query(void *ctx, char **name, char **infotxt,
                    793:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    794: {
                    795:        Buffer m;
1.21      markus    796:        int len;
                    797:        u_int success;
1.1       provos    798:        char *p, *challenge;
                    799:
1.8       markus    800:        debug3("%s: entering", __func__);
1.1       provos    801:
                    802:        buffer_init(&m);
1.7       mouring   803:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1.1       provos    804:
1.7       mouring   805:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1.1       provos    806:            &m);
1.21      markus    807:        success = buffer_get_int(&m);
                    808:        if (success == 0) {
1.8       markus    809:                debug3("%s: no challenge", __func__);
1.1       provos    810:                buffer_free(&m);
                    811:                return (-1);
                    812:        }
                    813:
                    814:        /* Get the challenge, and format the response */
                    815:        challenge  = buffer_get_string(&m, NULL);
                    816:        buffer_free(&m);
                    817:
1.8       markus    818:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    819:
                    820:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    821:
1.43      djm       822:        xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1.1       provos    823:        xfree(challenge);
                    824:
                    825:        return (0);
                    826: }
                    827:
                    828: int
                    829: mm_skey_respond(void *ctx, u_int numresponses, char **responses)
                    830: {
                    831:        Buffer m;
                    832:        int authok;
                    833:
1.8       markus    834:        debug3("%s: entering", __func__);
1.1       provos    835:        if (numresponses != 1)
                    836:                return (-1);
                    837:
                    838:        buffer_init(&m);
                    839:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   840:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1.1       provos    841:
1.7       mouring   842:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    843:            MONITOR_ANS_SKEYRESPOND, &m);
                    844:
                    845:        authok = buffer_get_int(&m);
                    846:        buffer_free(&m);
                    847:
                    848:        return ((authok == 0) ? -1 : 0);
                    849: }
1.38      dtucker   850: #endif /* SKEY */
1.1       provos    851:
                    852: void
                    853: mm_ssh1_session_id(u_char session_id[16])
                    854: {
                    855:        Buffer m;
                    856:        int i;
                    857:
1.8       markus    858:        debug3("%s entering", __func__);
1.1       provos    859:
                    860:        buffer_init(&m);
                    861:        for (i = 0; i < 16; i++)
                    862:                buffer_put_char(&m, session_id[i]);
                    863:
1.7       mouring   864:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    865:        buffer_free(&m);
                    866: }
                    867:
                    868: int
                    869: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    870: {
                    871:        Buffer m;
                    872:        Key *key;
                    873:        u_char *blob;
                    874:        u_int blen;
1.22      markus    875:        int allowed = 0, have_forced = 0;
1.1       provos    876:
1.8       markus    877:        debug3("%s entering", __func__);
1.1       provos    878:
                    879:        buffer_init(&m);
                    880:        buffer_put_bignum2(&m, client_n);
                    881:
1.7       mouring   882:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    883:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    884:
                    885:        allowed = buffer_get_int(&m);
1.22      markus    886:
                    887:        /* fake forced command */
                    888:        auth_clear_options();
                    889:        have_forced = buffer_get_int(&m);
                    890:        forced_command = have_forced ? xstrdup("true") : NULL;
1.1       provos    891:
                    892:        if (allowed && rkey != NULL) {
                    893:                blob = buffer_get_string(&m, &blen);
                    894:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8       markus    895:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    896:                *rkey = key;
                    897:                xfree(blob);
                    898:        }
                    899:        mm_send_debug(&m);
                    900:        buffer_free(&m);
                    901:
                    902:        return (allowed);
                    903: }
                    904:
                    905: BIGNUM *
                    906: mm_auth_rsa_generate_challenge(Key *key)
                    907: {
                    908:        Buffer m;
                    909:        BIGNUM *challenge;
                    910:        u_char *blob;
                    911:        u_int blen;
                    912:
1.8       markus    913:        debug3("%s entering", __func__);
1.1       provos    914:
                    915:        if ((challenge = BN_new()) == NULL)
1.8       markus    916:                fatal("%s: BN_new failed", __func__);
1.1       provos    917:
                    918:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    919:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    920:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    921:        key->type = KEY_RSA1;
                    922:
                    923:        buffer_init(&m);
                    924:        buffer_put_string(&m, blob, blen);
                    925:        xfree(blob);
                    926:
1.7       mouring   927:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    928:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    929:
                    930:        buffer_get_bignum2(&m, challenge);
                    931:        buffer_free(&m);
                    932:
                    933:        return (challenge);
                    934: }
                    935:
                    936: int
                    937: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    938: {
                    939:        Buffer m;
                    940:        u_char *blob;
                    941:        u_int blen;
                    942:        int success = 0;
                    943:
1.8       markus    944:        debug3("%s entering", __func__);
1.1       provos    945:
                    946:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    947:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    948:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    949:        key->type = KEY_RSA1;
                    950:
                    951:        buffer_init(&m);
                    952:        buffer_put_string(&m, blob, blen);
                    953:        buffer_put_string(&m, response, 16);
                    954:        xfree(blob);
                    955:
1.7       mouring   956:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    957:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    958:
                    959:        success = buffer_get_int(&m);
                    960:        buffer_free(&m);
                    961:
                    962:        return (success);
                    963: }
1.29      markus    964:
                    965: #ifdef GSSAPI
                    966: OM_uint32
1.36      avsm      967: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29      markus    968: {
                    969:        Buffer m;
                    970:        OM_uint32 major;
                    971:
                    972:        /* Client doesn't get to see the context */
                    973:        *ctx = NULL;
                    974:
                    975:        buffer_init(&m);
1.36      avsm      976:        buffer_put_string(&m, goid->elements, goid->length);
1.29      markus    977:
                    978:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
                    979:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
                    980:
                    981:        major = buffer_get_int(&m);
                    982:
                    983:        buffer_free(&m);
                    984:        return (major);
                    985: }
                    986:
                    987: OM_uint32
                    988: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
                    989:     gss_buffer_desc *out, OM_uint32 *flags)
                    990: {
                    991:        Buffer m;
                    992:        OM_uint32 major;
1.30      deraadt   993:        u_int len;
1.29      markus    994:
                    995:        buffer_init(&m);
                    996:        buffer_put_string(&m, in->value, in->length);
                    997:
                    998:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
                    999:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
                   1000:
                   1001:        major = buffer_get_int(&m);
1.30      deraadt  1002:        out->value = buffer_get_string(&m, &len);
                   1003:        out->length = len;
1.29      markus   1004:        if (flags)
                   1005:                *flags = buffer_get_int(&m);
                   1006:
                   1007:        buffer_free(&m);
                   1008:
                   1009:        return (major);
1.35      markus   1010: }
                   1011:
                   1012: OM_uint32
                   1013: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
                   1014: {
                   1015:        Buffer m;
                   1016:        OM_uint32 major;
                   1017:
                   1018:        buffer_init(&m);
                   1019:        buffer_put_string(&m, gssbuf->value, gssbuf->length);
                   1020:        buffer_put_string(&m, gssmic->value, gssmic->length);
                   1021:
                   1022:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
                   1023:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
                   1024:            &m);
                   1025:
                   1026:        major = buffer_get_int(&m);
                   1027:        buffer_free(&m);
                   1028:        return(major);
1.29      markus   1029: }
                   1030:
                   1031: int
                   1032: mm_ssh_gssapi_userok(char *user)
                   1033: {
                   1034:        Buffer m;
                   1035:        int authenticated = 0;
                   1036:
                   1037:        buffer_init(&m);
                   1038:
                   1039:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
                   1040:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
                   1041:                                  &m);
                   1042:
                   1043:        authenticated = buffer_get_int(&m);
                   1044:
                   1045:        buffer_free(&m);
                   1046:        debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
                   1047:        return (authenticated);
                   1048: }
                   1049: #endif /* GSSAPI */