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

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