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

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.15    ! deraadt    28: RCSID("$OpenBSD: monitor_wrap.c,v 1.14 2002/06/30 21:59:45 deraadt 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;
1.7       mouring    59: extern struct monitor *pmonitor;
1.1       provos     60: extern Buffer input, output;
                     61:
                     62: void
                     63: mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
                     64: {
1.13      deraadt    65:        u_int mlen = buffer_len(m);
1.1       provos     66:        u_char buf[5];
                     67:
1.8       markus     68:        debug3("%s entering: type %d", __func__, type);
1.1       provos     69:
                     70:        PUT_32BIT(buf, mlen + 1);
1.10      deraadt    71:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
1.1       provos     72:        if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
1.8       markus     73:                fatal("%s: write", __func__);
1.1       provos     74:        if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen)
1.8       markus     75:                fatal("%s: write", __func__);
1.1       provos     76: }
                     77:
                     78: void
                     79: mm_request_receive(int socket, Buffer *m)
                     80: {
                     81:        u_char buf[4];
1.13      deraadt    82:        u_int msg_len;
1.1       provos     83:        ssize_t res;
                     84:
1.8       markus     85:        debug3("%s entering", __func__);
1.1       provos     86:
                     87:        res = atomicio(read, socket, buf, sizeof(buf));
                     88:        if (res != sizeof(buf)) {
                     89:                if (res == 0)
                     90:                        fatal_cleanup();
1.8       markus     91:                fatal("%s: read: %ld", __func__, (long)res);
1.1       provos     92:        }
                     93:        msg_len = GET_32BIT(buf);
                     94:        if (msg_len > 256 * 1024)
1.8       markus     95:                fatal("%s: read: bad msg_len %d", __func__, msg_len);
1.1       provos     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)
1.8       markus    100:                fatal("%s: read: %ld != msg_len", __func__, (long)res);
1.1       provos    101: }
                    102:
                    103: void
                    104: mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
                    105: {
                    106:        u_char rtype;
                    107:
1.8       markus    108:        debug3("%s entering: type %d", __func__, type);
1.1       provos    109:
                    110:        mm_request_receive(socket, m);
                    111:        rtype = buffer_get_char(m);
                    112:        if (rtype != type)
1.8       markus    113:                fatal("%s: read: rtype %d != type %d", __func__,
1.1       provos    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:
1.7       mouring   129:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
1.1       provos    130:
1.8       markus    131:        debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
1.7       mouring   132:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
1.1       provos    133:
                    134:        success = buffer_get_char(&m);
                    135:        if (success == 0)
1.8       markus    136:                fatal("%s: MONITOR_ANS_MODULI failed", __func__);
1.1       provos    137:
                    138:        if ((p = BN_new()) == NULL)
1.8       markus    139:                fatal("%s: BN_new failed", __func__);
1.3       markus    140:        if ((g = BN_new()) == NULL)
1.8       markus    141:                fatal("%s: BN_new failed", __func__);
1.1       provos    142:        buffer_get_bignum2(&m, p);
                    143:        buffer_get_bignum2(&m, g);
                    144:
1.8       markus    145:        debug3("%s: remaining %d", __func__, buffer_len(&m));
1.1       provos    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: {
1.7       mouring   154:        Kex *kex = *pmonitor->m_pkex;
1.1       provos    155:        Buffer m;
                    156:
1.8       markus    157:        debug3("%s entering", __func__);
1.1       provos    158:
                    159:        buffer_init(&m);
                    160:        buffer_put_int(&m, kex->host_key_index(key));
                    161:        buffer_put_string(&m, data, datalen);
                    162:
1.7       mouring   163:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
1.1       provos    164:
1.8       markus    165:        debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
1.7       mouring   166:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
1.1       provos    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:
1.8       markus    180:        debug3("%s entering", __func__);
1.1       provos    181:
                    182:        buffer_init(&m);
                    183:        buffer_put_cstring(&m, login);
                    184:
1.7       mouring   185:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
1.1       provos    186:
1.8       markus    187:        debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
1.7       mouring   188:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
1.1       provos    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))
1.8       markus    196:                fatal("%s: struct passwd size mismatch", __func__);
1.1       provos    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);
1.6       djm       206: }
                    207:
1.14      deraadt   208: char *mm_auth2_read_banner(void)
1.6       djm       209: {
                    210:        Buffer m;
                    211:        char *banner;
                    212:
1.8       markus    213:        debug3("%s entering", __func__);
1.6       djm       214:
                    215:        buffer_init(&m);
1.7       mouring   216:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       217:        buffer_clear(&m);
                    218:
1.7       mouring   219:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       220:        banner = buffer_get_string(&m, NULL);
                    221:        buffer_free(&m);
1.10      deraadt   222:
1.6       djm       223:        return (banner);
1.1       provos    224: }
                    225:
                    226: /* Inform the privileged process about service and style */
                    227:
                    228: void
                    229: mm_inform_authserv(char *service, char *style)
                    230: {
                    231:        Buffer m;
                    232:
1.8       markus    233:        debug3("%s entering", __func__);
1.1       provos    234:
                    235:        buffer_init(&m);
                    236:        buffer_put_cstring(&m, service);
                    237:        buffer_put_cstring(&m, style ? style : "");
                    238:
1.7       mouring   239:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    240:
                    241:        buffer_free(&m);
                    242: }
                    243:
                    244: /* Do the password authentication */
                    245: int
                    246: mm_auth_password(Authctxt *authctxt, char *password)
                    247: {
                    248:        Buffer m;
                    249:        int authenticated = 0;
                    250:
1.8       markus    251:        debug3("%s entering", __func__);
1.1       provos    252:
                    253:        buffer_init(&m);
                    254:        buffer_put_cstring(&m, password);
1.7       mouring   255:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    256:
1.8       markus    257:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   258:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    259:
                    260:        authenticated = buffer_get_int(&m);
                    261:
                    262:        buffer_free(&m);
                    263:
1.3       markus    264:        debug3("%s: user %sauthenticated",
1.8       markus    265:            __func__, authenticated ? "" : "not ");
1.1       provos    266:        return (authenticated);
                    267: }
                    268:
                    269: int
                    270: mm_user_key_allowed(struct passwd *pw, Key *key)
                    271: {
                    272:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
                    273: }
                    274:
                    275: int
                    276: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
                    277:     Key *key)
                    278: {
                    279:        return (mm_key_allowed(MM_HOSTKEY, user, host, key));
                    280: }
                    281:
                    282: int
                    283: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
                    284:     char *host, Key *key)
                    285: {
                    286:        int ret;
                    287:
                    288:        key->type = KEY_RSA; /* XXX hack for key_to_blob */
                    289:        ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
                    290:        key->type = KEY_RSA1;
                    291:        return (ret);
                    292: }
                    293:
1.2       markus    294: static void
1.1       provos    295: mm_send_debug(Buffer *m)
                    296: {
                    297:        char *msg;
                    298:
                    299:        while (buffer_len(m)) {
                    300:                msg = buffer_get_string(m, NULL);
1.8       markus    301:                debug3("%s: Sending debug: %s", __func__, msg);
1.1       provos    302:                packet_send_debug("%s", msg);
                    303:                xfree(msg);
                    304:        }
                    305: }
                    306:
                    307: int
                    308: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
                    309: {
                    310:        Buffer m;
                    311:        u_char *blob;
                    312:        u_int len;
                    313:        int allowed = 0;
                    314:
1.8       markus    315:        debug3("%s entering", __func__);
1.1       provos    316:
                    317:        /* Convert the key to a blob and the pass it over */
                    318:        if (!key_to_blob(key, &blob, &len))
                    319:                return (0);
                    320:
                    321:        buffer_init(&m);
                    322:        buffer_put_int(&m, type);
                    323:        buffer_put_cstring(&m, user ? user : "");
                    324:        buffer_put_cstring(&m, host ? host : "");
                    325:        buffer_put_string(&m, blob, len);
                    326:        xfree(blob);
                    327:
1.7       mouring   328:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    329:
1.8       markus    330:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7       mouring   331:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    332:
                    333:        allowed = buffer_get_int(&m);
                    334:
                    335:        /* Send potential debug messages */
                    336:        mm_send_debug(&m);
                    337:
                    338:        buffer_free(&m);
                    339:
                    340:        return (allowed);
                    341: }
                    342:
1.3       markus    343: /*
1.1       provos    344:  * This key verify needs to send the key type along, because the
                    345:  * privileged parent makes the decision if the key is allowed
                    346:  * for authentication.
                    347:  */
                    348:
                    349: int
                    350: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
                    351: {
                    352:        Buffer m;
                    353:        u_char *blob;
                    354:        u_int len;
                    355:        int verified = 0;
                    356:
1.8       markus    357:        debug3("%s entering", __func__);
1.1       provos    358:
                    359:        /* Convert the key to a blob and the pass it over */
                    360:        if (!key_to_blob(key, &blob, &len))
                    361:                return (0);
                    362:
                    363:        buffer_init(&m);
                    364:        buffer_put_string(&m, blob, len);
                    365:        buffer_put_string(&m, sig, siglen);
                    366:        buffer_put_string(&m, data, datalen);
                    367:        xfree(blob);
                    368:
1.7       mouring   369:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    370:
1.8       markus    371:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   372:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    373:
                    374:        verified = buffer_get_int(&m);
                    375:
                    376:        buffer_free(&m);
                    377:
                    378:        return (verified);
                    379: }
                    380:
                    381: /* Export key state after authentication */
                    382: Newkeys *
                    383: mm_newkeys_from_blob(u_char *blob, int blen)
                    384: {
                    385:        Buffer b;
                    386:        u_int len;
                    387:        Newkeys *newkey = NULL;
                    388:        Enc *enc;
                    389:        Mac *mac;
                    390:        Comp *comp;
                    391:
1.8       markus    392:        debug3("%s: %p(%d)", __func__, blob, blen);
1.1       provos    393: #ifdef DEBUG_PK
                    394:        dump_base64(stderr, blob, blen);
                    395: #endif
                    396:        buffer_init(&b);
                    397:        buffer_append(&b, blob, blen);
                    398:
                    399:        newkey = xmalloc(sizeof(*newkey));
                    400:        enc = &newkey->enc;
                    401:        mac = &newkey->mac;
                    402:        comp = &newkey->comp;
                    403:
                    404:        /* Enc structure */
                    405:        enc->name = buffer_get_string(&b, NULL);
                    406:        buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
                    407:        enc->enabled = buffer_get_int(&b);
                    408:        enc->block_size = buffer_get_int(&b);
                    409:        enc->key = buffer_get_string(&b, &enc->key_len);
                    410:        enc->iv = buffer_get_string(&b, &len);
                    411:        if (len != enc->block_size)
1.12      deraadt   412:                fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1       provos    413:                    enc->block_size, len);
                    414:
                    415:        if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8       markus    416:                fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1       provos    417:                    enc->name, enc->cipher);
                    418:
                    419:        /* Mac structure */
                    420:        mac->name = buffer_get_string(&b, NULL);
                    421:        if (mac->name == NULL || mac_init(mac, mac->name) == -1)
1.8       markus    422:                fatal("%s: can not init mac %s", __func__, mac->name);
1.1       provos    423:        mac->enabled = buffer_get_int(&b);
                    424:        mac->key = buffer_get_string(&b, &len);
                    425:        if (len > mac->key_len)
1.12      deraadt   426:                fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1       provos    427:                    mac->key_len);
                    428:        mac->key_len = len;
                    429:
                    430:        /* Comp structure */
                    431:        comp->type = buffer_get_int(&b);
                    432:        comp->enabled = buffer_get_int(&b);
                    433:        comp->name = buffer_get_string(&b, NULL);
                    434:
                    435:        len = buffer_len(&b);
                    436:        if (len != 0)
1.12      deraadt   437:                error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1       provos    438:        buffer_free(&b);
                    439:        return (newkey);
                    440: }
                    441:
                    442: int
                    443: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
                    444: {
                    445:        Buffer b;
                    446:        int len;
                    447:        u_char *buf;
                    448:        Enc *enc;
                    449:        Mac *mac;
                    450:        Comp *comp;
                    451:        Newkeys *newkey = newkeys[mode];
                    452:
1.8       markus    453:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    454:
                    455:        if (newkey == NULL) {
1.8       markus    456:                error("%s: newkey == NULL", __func__);
1.1       provos    457:                return 0;
                    458:        }
                    459:        enc = &newkey->enc;
                    460:        mac = &newkey->mac;
                    461:        comp = &newkey->comp;
                    462:
                    463:        buffer_init(&b);
                    464:        /* Enc structure */
                    465:        buffer_put_cstring(&b, enc->name);
                    466:        /* The cipher struct is constant and shared, you export pointer */
                    467:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    468:        buffer_put_int(&b, enc->enabled);
                    469:        buffer_put_int(&b, enc->block_size);
                    470:        buffer_put_string(&b, enc->key, enc->key_len);
                    471:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    472:        buffer_put_string(&b, enc->iv, enc->block_size);
                    473:
                    474:        /* Mac structure */
                    475:        buffer_put_cstring(&b, mac->name);
                    476:        buffer_put_int(&b, mac->enabled);
                    477:        buffer_put_string(&b, mac->key, mac->key_len);
                    478:
                    479:        /* Comp structure */
                    480:        buffer_put_int(&b, comp->type);
                    481:        buffer_put_int(&b, comp->enabled);
                    482:        buffer_put_cstring(&b, comp->name);
                    483:
                    484:        len = buffer_len(&b);
                    485:        buf = xmalloc(len);
                    486:        memcpy(buf, buffer_ptr(&b), len);
                    487:        memset(buffer_ptr(&b), 0, len);
                    488:        buffer_free(&b);
                    489:        if (lenp != NULL)
                    490:                *lenp = len;
                    491:        if (blobp != NULL)
                    492:                *blobp = buf;
1.15    ! deraadt   493:        else
        !           494:                xfree(blobp);
1.1       provos    495:        return len;
                    496: }
                    497:
1.2       markus    498: static void
1.1       provos    499: mm_send_kex(Buffer *m, Kex *kex)
                    500: {
                    501:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    502:        buffer_put_int(m, kex->we_need);
                    503:        buffer_put_int(m, kex->hostkey_type);
                    504:        buffer_put_int(m, kex->kex_type);
                    505:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    506:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    507:        buffer_put_int(m, kex->flags);
                    508:        buffer_put_cstring(m, kex->client_version_string);
                    509:        buffer_put_cstring(m, kex->server_version_string);
                    510: }
                    511:
                    512: void
1.7       mouring   513: mm_send_keystate(struct monitor *pmonitor)
1.1       provos    514: {
                    515:        Buffer m;
                    516:        u_char *blob, *p;
                    517:        u_int bloblen, plen;
                    518:
                    519:        buffer_init(&m);
                    520:
                    521:        if (!compat20) {
                    522:                u_char iv[24];
1.11      markus    523:                u_char *key;
                    524:                u_int ivlen, keylen;
1.1       provos    525:
                    526:                buffer_put_int(&m, packet_get_protocol_flags());
                    527:
                    528:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    529:
1.11      markus    530:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    531:                keylen = packet_get_encryption_key(NULL);
                    532:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    533:                keylen = packet_get_encryption_key(key);
                    534:                buffer_put_string(&m, key, keylen);
                    535:                memset(key, 0, keylen);
                    536:                xfree(key);
                    537:
1.1       provos    538:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    539:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    540:                buffer_put_string(&m, iv, ivlen);
                    541:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    542:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    543:                buffer_put_string(&m, iv, ivlen);
                    544:                goto skip;
                    545:        } else {
                    546:                /* Kex for rekeying */
1.7       mouring   547:                mm_send_kex(&m, *pmonitor->m_pkex);
1.1       provos    548:        }
                    549:
                    550:        debug3("%s: Sending new keys: %p %p",
1.8       markus    551:            __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1       provos    552:
                    553:        /* Keys from Kex */
                    554:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    555:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    556:
                    557:        buffer_put_string(&m, blob, bloblen);
                    558:        xfree(blob);
                    559:
                    560:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    561:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    562:
                    563:        buffer_put_string(&m, blob, bloblen);
                    564:        xfree(blob);
                    565:
                    566:        buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
                    567:        buffer_put_int(&m, packet_get_seqnr(MODE_IN));
                    568:
1.8       markus    569:        debug3("%s: New keys have been sent", __func__);
1.1       provos    570:  skip:
                    571:        /* More key context */
                    572:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    573:        p = xmalloc(plen+1);
                    574:        packet_get_keycontext(MODE_OUT, p);
                    575:        buffer_put_string(&m, p, plen);
                    576:        xfree(p);
                    577:
                    578:        plen = packet_get_keycontext(MODE_IN, NULL);
                    579:        p = xmalloc(plen+1);
                    580:        packet_get_keycontext(MODE_IN, p);
                    581:        buffer_put_string(&m, p, plen);
                    582:        xfree(p);
                    583:
                    584:        /* Compression state */
1.8       markus    585:        debug3("%s: Sending compression state", __func__);
1.1       provos    586:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    587:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    588:
                    589:        /* Network I/O buffers */
                    590:        buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
                    591:        buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
                    592:
1.7       mouring   593:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    594:        debug3("%s: Finished sending state", __func__);
1.1       provos    595:
                    596:        buffer_free(&m);
                    597: }
                    598:
                    599: int
                    600: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
                    601: {
                    602:        Buffer m;
                    603:        u_char *p;
                    604:        int success = 0;
                    605:
                    606:        buffer_init(&m);
1.7       mouring   607:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    608:
1.8       markus    609:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   610:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    611:
                    612:        success = buffer_get_int(&m);
                    613:        if (success == 0) {
1.8       markus    614:                debug3("%s: pty alloc failed", __func__);
1.1       provos    615:                buffer_free(&m);
                    616:                return (0);
                    617:        }
                    618:        p = buffer_get_string(&m, NULL);
                    619:        buffer_free(&m);
                    620:
                    621:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    622:        xfree(p);
                    623:
1.7       mouring   624:        *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
                    625:        *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1.1       provos    626:
                    627:        /* Success */
                    628:        return (1);
                    629: }
                    630:
                    631: void
                    632: mm_session_pty_cleanup2(void *session)
                    633: {
                    634:        Session *s = session;
                    635:        Buffer m;
                    636:
                    637:        if (s->ttyfd == -1)
                    638:                return;
                    639:        buffer_init(&m);
                    640:        buffer_put_cstring(&m, s->tty);
1.7       mouring   641:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    642:        buffer_free(&m);
1.3       markus    643:
1.1       provos    644:        /* closed dup'ed master */
                    645:        if (close(s->ptymaster) < 0)
                    646:                error("close(s->ptymaster): %s", strerror(errno));
                    647:
                    648:        /* unlink pty from session */
                    649:        s->ttyfd = -1;
                    650: }
                    651:
                    652: /* Request process termination */
                    653:
                    654: void
                    655: mm_terminate(void)
                    656: {
                    657:        Buffer m;
                    658:
                    659:        buffer_init(&m);
1.7       mouring   660:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    661:        buffer_free(&m);
                    662: }
                    663:
                    664: int
                    665: mm_ssh1_session_key(BIGNUM *num)
                    666: {
                    667:        int rsafail;
                    668:        Buffer m;
                    669:
                    670:        buffer_init(&m);
                    671:        buffer_put_bignum2(&m, num);
1.7       mouring   672:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    673:
1.7       mouring   674:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    675:
                    676:        rsafail = buffer_get_int(&m);
                    677:        buffer_get_bignum2(&m, num);
                    678:
                    679:        buffer_free(&m);
                    680:
                    681:        return (rsafail);
                    682: }
                    683:
1.2       markus    684: static void
1.1       provos    685: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    686:     char ***prompts, u_int **echo_on)
                    687: {
1.10      deraadt   688:        *name = xstrdup("");
                    689:        *infotxt = xstrdup("");
1.1       provos    690:        *numprompts = 1;
1.14      deraadt   691:        *prompts = xmalloc(*numprompts * sizeof(char *));
1.1       provos    692:        *echo_on = xmalloc(*numprompts * sizeof(u_int));
                    693:        (*echo_on)[0] = 0;
                    694: }
                    695:
                    696: int
                    697: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    698:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    699: {
                    700:        Buffer m;
                    701:        int res;
                    702:        char *challenge;
                    703:
1.8       markus    704:        debug3("%s: entering", __func__);
1.1       provos    705:
                    706:        buffer_init(&m);
1.7       mouring   707:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    708:
1.7       mouring   709:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    710:            &m);
                    711:        res = buffer_get_int(&m);
                    712:        if (res == -1) {
1.8       markus    713:                debug3("%s: no challenge", __func__);
1.1       provos    714:                buffer_free(&m);
                    715:                return (-1);
                    716:        }
                    717:
                    718:        /* Get the challenge, and format the response */
                    719:        challenge  = buffer_get_string(&m, NULL);
                    720:        buffer_free(&m);
                    721:
                    722:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    723:        (*prompts)[0] = challenge;
                    724:
1.8       markus    725:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    726:
                    727:        return (0);
                    728: }
                    729:
                    730: int
                    731: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    732: {
                    733:        Buffer m;
                    734:        int authok;
                    735:
1.8       markus    736:        debug3("%s: entering", __func__);
1.1       provos    737:        if (numresponses != 1)
                    738:                return (-1);
                    739:
                    740:        buffer_init(&m);
                    741:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   742:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    743:
1.7       mouring   744:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    745:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    746:
                    747:        authok = buffer_get_int(&m);
                    748:        buffer_free(&m);
                    749:
                    750:        return ((authok == 0) ? -1 : 0);
                    751: }
                    752:
                    753: int
                    754: mm_skey_query(void *ctx, char **name, char **infotxt,
                    755:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    756: {
                    757:        Buffer m;
                    758:        int len, res;
                    759:        char *p, *challenge;
                    760:
1.8       markus    761:        debug3("%s: entering", __func__);
1.1       provos    762:
                    763:        buffer_init(&m);
1.7       mouring   764:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1.1       provos    765:
1.7       mouring   766:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1.1       provos    767:            &m);
                    768:        res = buffer_get_int(&m);
                    769:        if (res == -1) {
1.8       markus    770:                debug3("%s: no challenge", __func__);
1.1       provos    771:                buffer_free(&m);
                    772:                return (-1);
                    773:        }
                    774:
                    775:        /* Get the challenge, and format the response */
                    776:        challenge  = buffer_get_string(&m, NULL);
                    777:        buffer_free(&m);
                    778:
1.8       markus    779:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    780:
                    781:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    782:
                    783:        len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
                    784:        p = xmalloc(len);
                    785:        strlcpy(p, challenge, len);
                    786:        strlcat(p, SKEY_PROMPT, len);
                    787:        (*prompts)[0] = p;
                    788:        xfree(challenge);
                    789:
                    790:        return (0);
                    791: }
                    792:
                    793: int
                    794: mm_skey_respond(void *ctx, u_int numresponses, char **responses)
                    795: {
                    796:        Buffer m;
                    797:        int authok;
                    798:
1.8       markus    799:        debug3("%s: entering", __func__);
1.1       provos    800:        if (numresponses != 1)
                    801:                return (-1);
                    802:
                    803:        buffer_init(&m);
                    804:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   805:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1.1       provos    806:
1.7       mouring   807:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    808:            MONITOR_ANS_SKEYRESPOND, &m);
                    809:
                    810:        authok = buffer_get_int(&m);
                    811:        buffer_free(&m);
                    812:
                    813:        return ((authok == 0) ? -1 : 0);
                    814: }
                    815:
                    816: void
                    817: mm_ssh1_session_id(u_char session_id[16])
                    818: {
                    819:        Buffer m;
                    820:        int i;
                    821:
1.8       markus    822:        debug3("%s entering", __func__);
1.1       provos    823:
                    824:        buffer_init(&m);
                    825:        for (i = 0; i < 16; i++)
                    826:                buffer_put_char(&m, session_id[i]);
                    827:
1.7       mouring   828:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    829:        buffer_free(&m);
                    830: }
                    831:
                    832: int
                    833: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    834: {
                    835:        Buffer m;
                    836:        Key *key;
                    837:        u_char *blob;
                    838:        u_int blen;
                    839:        int allowed = 0;
                    840:
1.8       markus    841:        debug3("%s entering", __func__);
1.1       provos    842:
                    843:        buffer_init(&m);
                    844:        buffer_put_bignum2(&m, client_n);
                    845:
1.7       mouring   846:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    847:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    848:
                    849:        allowed = buffer_get_int(&m);
                    850:
                    851:        if (allowed && rkey != NULL) {
                    852:                blob = buffer_get_string(&m, &blen);
                    853:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8       markus    854:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    855:                *rkey = key;
                    856:                xfree(blob);
                    857:        }
                    858:        mm_send_debug(&m);
                    859:        buffer_free(&m);
                    860:
                    861:        return (allowed);
                    862: }
                    863:
                    864: BIGNUM *
                    865: mm_auth_rsa_generate_challenge(Key *key)
                    866: {
                    867:        Buffer m;
                    868:        BIGNUM *challenge;
                    869:        u_char *blob;
                    870:        u_int blen;
                    871:
1.8       markus    872:        debug3("%s entering", __func__);
1.1       provos    873:
                    874:        if ((challenge = BN_new()) == NULL)
1.8       markus    875:                fatal("%s: BN_new failed", __func__);
1.1       provos    876:
                    877:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    878:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    879:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    880:        key->type = KEY_RSA1;
                    881:
                    882:        buffer_init(&m);
                    883:        buffer_put_string(&m, blob, blen);
                    884:        xfree(blob);
                    885:
1.7       mouring   886:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    887:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    888:
                    889:        buffer_get_bignum2(&m, challenge);
                    890:        buffer_free(&m);
                    891:
                    892:        return (challenge);
                    893: }
                    894:
                    895: int
                    896: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    897: {
                    898:        Buffer m;
                    899:        u_char *blob;
                    900:        u_int blen;
                    901:        int success = 0;
                    902:
1.8       markus    903:        debug3("%s entering", __func__);
1.1       provos    904:
                    905:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    906:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    907:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    908:        key->type = KEY_RSA1;
                    909:
                    910:        buffer_init(&m);
                    911:        buffer_put_string(&m, blob, blen);
                    912:        buffer_put_string(&m, response, 16);
                    913:        xfree(blob);
                    914:
1.7       mouring   915:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    916:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    917:
                    918:        success = buffer_get_int(&m);
                    919:        buffer_free(&m);
                    920:
                    921:        return (success);
                    922: }