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

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.20    ! deraadt    28: RCSID("$OpenBSD: monitor_wrap.c,v 1.19 2002/09/26 11:38:43 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;
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:        Enc *enc;
                    448:        Mac *mac;
                    449:        Comp *comp;
                    450:        Newkeys *newkey = newkeys[mode];
                    451:
1.8       markus    452:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    453:
                    454:        if (newkey == NULL) {
1.8       markus    455:                error("%s: newkey == NULL", __func__);
1.1       provos    456:                return 0;
                    457:        }
                    458:        enc = &newkey->enc;
                    459:        mac = &newkey->mac;
                    460:        comp = &newkey->comp;
                    461:
                    462:        buffer_init(&b);
                    463:        /* Enc structure */
                    464:        buffer_put_cstring(&b, enc->name);
                    465:        /* The cipher struct is constant and shared, you export pointer */
                    466:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    467:        buffer_put_int(&b, enc->enabled);
                    468:        buffer_put_int(&b, enc->block_size);
                    469:        buffer_put_string(&b, enc->key, enc->key_len);
                    470:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    471:        buffer_put_string(&b, enc->iv, enc->block_size);
                    472:
                    473:        /* Mac structure */
                    474:        buffer_put_cstring(&b, mac->name);
                    475:        buffer_put_int(&b, mac->enabled);
                    476:        buffer_put_string(&b, mac->key, mac->key_len);
                    477:
                    478:        /* Comp structure */
                    479:        buffer_put_int(&b, comp->type);
                    480:        buffer_put_int(&b, comp->enabled);
                    481:        buffer_put_cstring(&b, comp->name);
                    482:
                    483:        len = buffer_len(&b);
1.16      markus    484:        if (lenp != NULL)
                    485:                *lenp = len;
                    486:        if (blobp != NULL) {
                    487:                *blobp = xmalloc(len);
                    488:                memcpy(*blobp, buffer_ptr(&b), len);
                    489:        }
1.1       provos    490:        memset(buffer_ptr(&b), 0, len);
                    491:        buffer_free(&b);
                    492:        return len;
                    493: }
                    494:
1.2       markus    495: static void
1.1       provos    496: mm_send_kex(Buffer *m, Kex *kex)
                    497: {
                    498:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    499:        buffer_put_int(m, kex->we_need);
                    500:        buffer_put_int(m, kex->hostkey_type);
                    501:        buffer_put_int(m, kex->kex_type);
                    502:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    503:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    504:        buffer_put_int(m, kex->flags);
                    505:        buffer_put_cstring(m, kex->client_version_string);
                    506:        buffer_put_cstring(m, kex->server_version_string);
                    507: }
                    508:
                    509: void
1.7       mouring   510: mm_send_keystate(struct monitor *pmonitor)
1.1       provos    511: {
                    512:        Buffer m;
                    513:        u_char *blob, *p;
                    514:        u_int bloblen, plen;
                    515:
                    516:        buffer_init(&m);
                    517:
                    518:        if (!compat20) {
                    519:                u_char iv[24];
1.11      markus    520:                u_char *key;
                    521:                u_int ivlen, keylen;
1.1       provos    522:
                    523:                buffer_put_int(&m, packet_get_protocol_flags());
                    524:
                    525:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    526:
1.11      markus    527:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    528:                keylen = packet_get_encryption_key(NULL);
                    529:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    530:                keylen = packet_get_encryption_key(key);
                    531:                buffer_put_string(&m, key, keylen);
                    532:                memset(key, 0, keylen);
                    533:                xfree(key);
                    534:
1.1       provos    535:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    536:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    537:                buffer_put_string(&m, iv, ivlen);
                    538:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    539:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    540:                buffer_put_string(&m, iv, ivlen);
                    541:                goto skip;
                    542:        } else {
                    543:                /* Kex for rekeying */
1.7       mouring   544:                mm_send_kex(&m, *pmonitor->m_pkex);
1.1       provos    545:        }
                    546:
                    547:        debug3("%s: Sending new keys: %p %p",
1.8       markus    548:            __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1       provos    549:
                    550:        /* Keys from Kex */
                    551:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    552:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    553:
                    554:        buffer_put_string(&m, blob, bloblen);
                    555:        xfree(blob);
                    556:
                    557:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    558:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    559:
                    560:        buffer_put_string(&m, blob, bloblen);
                    561:        xfree(blob);
                    562:
                    563:        buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
                    564:        buffer_put_int(&m, packet_get_seqnr(MODE_IN));
                    565:
1.8       markus    566:        debug3("%s: New keys have been sent", __func__);
1.1       provos    567:  skip:
                    568:        /* More key context */
                    569:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    570:        p = xmalloc(plen+1);
                    571:        packet_get_keycontext(MODE_OUT, p);
                    572:        buffer_put_string(&m, p, plen);
                    573:        xfree(p);
                    574:
                    575:        plen = packet_get_keycontext(MODE_IN, NULL);
                    576:        p = xmalloc(plen+1);
                    577:        packet_get_keycontext(MODE_IN, p);
                    578:        buffer_put_string(&m, p, plen);
                    579:        xfree(p);
                    580:
                    581:        /* Compression state */
1.8       markus    582:        debug3("%s: Sending compression state", __func__);
1.1       provos    583:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    584:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    585:
                    586:        /* Network I/O buffers */
                    587:        buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
                    588:        buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
                    589:
1.7       mouring   590:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    591:        debug3("%s: Finished sending state", __func__);
1.1       provos    592:
                    593:        buffer_free(&m);
                    594: }
                    595:
                    596: int
                    597: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
                    598: {
                    599:        Buffer m;
1.18      markus    600:        char *p;
1.1       provos    601:        int success = 0;
                    602:
                    603:        buffer_init(&m);
1.7       mouring   604:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    605:
1.8       markus    606:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   607:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    608:
                    609:        success = buffer_get_int(&m);
                    610:        if (success == 0) {
1.8       markus    611:                debug3("%s: pty alloc failed", __func__);
1.1       provos    612:                buffer_free(&m);
                    613:                return (0);
                    614:        }
                    615:        p = buffer_get_string(&m, NULL);
                    616:        buffer_free(&m);
                    617:
                    618:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    619:        xfree(p);
                    620:
1.7       mouring   621:        *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
                    622:        *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1.1       provos    623:
                    624:        /* Success */
                    625:        return (1);
                    626: }
                    627:
                    628: void
                    629: mm_session_pty_cleanup2(void *session)
                    630: {
                    631:        Session *s = session;
                    632:        Buffer m;
                    633:
                    634:        if (s->ttyfd == -1)
                    635:                return;
                    636:        buffer_init(&m);
                    637:        buffer_put_cstring(&m, s->tty);
1.7       mouring   638:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    639:        buffer_free(&m);
1.3       markus    640:
1.1       provos    641:        /* closed dup'ed master */
                    642:        if (close(s->ptymaster) < 0)
                    643:                error("close(s->ptymaster): %s", strerror(errno));
                    644:
                    645:        /* unlink pty from session */
                    646:        s->ttyfd = -1;
                    647: }
                    648:
                    649: /* Request process termination */
                    650:
                    651: void
                    652: mm_terminate(void)
                    653: {
                    654:        Buffer m;
                    655:
                    656:        buffer_init(&m);
1.7       mouring   657:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    658:        buffer_free(&m);
                    659: }
                    660:
                    661: int
                    662: mm_ssh1_session_key(BIGNUM *num)
                    663: {
                    664:        int rsafail;
                    665:        Buffer m;
                    666:
                    667:        buffer_init(&m);
                    668:        buffer_put_bignum2(&m, num);
1.7       mouring   669:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    670:
1.7       mouring   671:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    672:
                    673:        rsafail = buffer_get_int(&m);
                    674:        buffer_get_bignum2(&m, num);
                    675:
                    676:        buffer_free(&m);
                    677:
                    678:        return (rsafail);
                    679: }
                    680:
1.2       markus    681: static void
1.1       provos    682: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    683:     char ***prompts, u_int **echo_on)
                    684: {
1.10      deraadt   685:        *name = xstrdup("");
                    686:        *infotxt = xstrdup("");
1.1       provos    687:        *numprompts = 1;
1.14      deraadt   688:        *prompts = xmalloc(*numprompts * sizeof(char *));
1.1       provos    689:        *echo_on = xmalloc(*numprompts * sizeof(u_int));
                    690:        (*echo_on)[0] = 0;
                    691: }
                    692:
                    693: int
                    694: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    695:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    696: {
                    697:        Buffer m;
                    698:        int res;
                    699:        char *challenge;
                    700:
1.8       markus    701:        debug3("%s: entering", __func__);
1.1       provos    702:
                    703:        buffer_init(&m);
1.7       mouring   704:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    705:
1.7       mouring   706:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    707:            &m);
                    708:        res = buffer_get_int(&m);
                    709:        if (res == -1) {
1.8       markus    710:                debug3("%s: no challenge", __func__);
1.1       provos    711:                buffer_free(&m);
                    712:                return (-1);
                    713:        }
                    714:
                    715:        /* Get the challenge, and format the response */
                    716:        challenge  = buffer_get_string(&m, NULL);
                    717:        buffer_free(&m);
                    718:
                    719:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    720:        (*prompts)[0] = challenge;
                    721:
1.8       markus    722:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    723:
                    724:        return (0);
                    725: }
                    726:
                    727: int
                    728: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    729: {
                    730:        Buffer m;
                    731:        int authok;
                    732:
1.8       markus    733:        debug3("%s: entering", __func__);
1.1       provos    734:        if (numresponses != 1)
                    735:                return (-1);
                    736:
                    737:        buffer_init(&m);
                    738:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   739:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    740:
1.7       mouring   741:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    742:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    743:
                    744:        authok = buffer_get_int(&m);
                    745:        buffer_free(&m);
                    746:
                    747:        return ((authok == 0) ? -1 : 0);
                    748: }
                    749:
                    750: int
                    751: mm_skey_query(void *ctx, char **name, char **infotxt,
                    752:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    753: {
                    754:        Buffer m;
                    755:        int len, res;
                    756:        char *p, *challenge;
                    757:
1.8       markus    758:        debug3("%s: entering", __func__);
1.1       provos    759:
                    760:        buffer_init(&m);
1.7       mouring   761:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1.1       provos    762:
1.7       mouring   763:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1.1       provos    764:            &m);
                    765:        res = buffer_get_int(&m);
                    766:        if (res == -1) {
1.8       markus    767:                debug3("%s: no challenge", __func__);
1.1       provos    768:                buffer_free(&m);
                    769:                return (-1);
                    770:        }
                    771:
                    772:        /* Get the challenge, and format the response */
                    773:        challenge  = buffer_get_string(&m, NULL);
                    774:        buffer_free(&m);
                    775:
1.8       markus    776:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    777:
                    778:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    779:
                    780:        len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
                    781:        p = xmalloc(len);
                    782:        strlcpy(p, challenge, len);
                    783:        strlcat(p, SKEY_PROMPT, len);
                    784:        (*prompts)[0] = p;
                    785:        xfree(challenge);
                    786:
                    787:        return (0);
                    788: }
                    789:
                    790: int
                    791: mm_skey_respond(void *ctx, u_int numresponses, char **responses)
                    792: {
                    793:        Buffer m;
                    794:        int authok;
                    795:
1.8       markus    796:        debug3("%s: entering", __func__);
1.1       provos    797:        if (numresponses != 1)
                    798:                return (-1);
                    799:
                    800:        buffer_init(&m);
                    801:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   802:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1.1       provos    803:
1.7       mouring   804:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    805:            MONITOR_ANS_SKEYRESPOND, &m);
                    806:
                    807:        authok = buffer_get_int(&m);
                    808:        buffer_free(&m);
                    809:
                    810:        return ((authok == 0) ? -1 : 0);
                    811: }
                    812:
                    813: void
                    814: mm_ssh1_session_id(u_char session_id[16])
                    815: {
                    816:        Buffer m;
                    817:        int i;
                    818:
1.8       markus    819:        debug3("%s entering", __func__);
1.1       provos    820:
                    821:        buffer_init(&m);
                    822:        for (i = 0; i < 16; i++)
                    823:                buffer_put_char(&m, session_id[i]);
                    824:
1.7       mouring   825:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    826:        buffer_free(&m);
                    827: }
                    828:
                    829: int
                    830: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    831: {
                    832:        Buffer m;
                    833:        Key *key;
                    834:        u_char *blob;
                    835:        u_int blen;
                    836:        int allowed = 0;
                    837:
1.8       markus    838:        debug3("%s entering", __func__);
1.1       provos    839:
                    840:        buffer_init(&m);
                    841:        buffer_put_bignum2(&m, client_n);
                    842:
1.7       mouring   843:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    844:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    845:
                    846:        allowed = buffer_get_int(&m);
                    847:
                    848:        if (allowed && rkey != NULL) {
                    849:                blob = buffer_get_string(&m, &blen);
                    850:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8       markus    851:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    852:                *rkey = key;
                    853:                xfree(blob);
                    854:        }
                    855:        mm_send_debug(&m);
                    856:        buffer_free(&m);
                    857:
                    858:        return (allowed);
                    859: }
                    860:
                    861: BIGNUM *
                    862: mm_auth_rsa_generate_challenge(Key *key)
                    863: {
                    864:        Buffer m;
                    865:        BIGNUM *challenge;
                    866:        u_char *blob;
                    867:        u_int blen;
                    868:
1.8       markus    869:        debug3("%s entering", __func__);
1.1       provos    870:
                    871:        if ((challenge = BN_new()) == NULL)
1.8       markus    872:                fatal("%s: BN_new failed", __func__);
1.1       provos    873:
                    874:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    875:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    876:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    877:        key->type = KEY_RSA1;
                    878:
                    879:        buffer_init(&m);
                    880:        buffer_put_string(&m, blob, blen);
                    881:        xfree(blob);
                    882:
1.7       mouring   883:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    884:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    885:
                    886:        buffer_get_bignum2(&m, challenge);
                    887:        buffer_free(&m);
                    888:
                    889:        return (challenge);
                    890: }
                    891:
                    892: int
                    893: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    894: {
                    895:        Buffer m;
                    896:        u_char *blob;
                    897:        u_int blen;
                    898:        int success = 0;
                    899:
1.8       markus    900:        debug3("%s entering", __func__);
1.1       provos    901:
                    902:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    903:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    904:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    905:        key->type = KEY_RSA1;
                    906:
                    907:        buffer_init(&m);
                    908:        buffer_put_string(&m, blob, blen);
                    909:        buffer_put_string(&m, response, 16);
                    910:        xfree(blob);
                    911:
1.7       mouring   912:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    913:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    914:
                    915:        success = buffer_get_int(&m);
                    916:        buffer_free(&m);
                    917:
                    918:        return (success);
                    919: }
1.19      markus    920:
                    921: #ifdef KRB4
                    922: int
                    923: mm_auth_krb4(Authctxt *authctxt, void *_auth, char **client, void *_reply)
                    924: {
                    925:        KTEXT auth, reply;
                    926:        Buffer m;
                    927:        u_int rlen;
                    928:        int success = 0;
                    929:        char *p;
                    930:
                    931:        debug3("%s entering", __func__);
                    932:        auth = _auth;
                    933:        reply = _reply;
                    934:
                    935:        buffer_init(&m);
                    936:        buffer_put_string(&m, auth->dat, auth->length);
                    937:
                    938:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB4, &m);
                    939:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB4, &m);
                    940:
                    941:        success = buffer_get_int(&m);
                    942:        if (success) {
                    943:                *client = buffer_get_string(&m, NULL);
                    944:                p = buffer_get_string(&m, &rlen);
                    945:                if (rlen >= MAX_KTXT_LEN)
                    946:                        fatal("%s: reply from monitor too large", __func__);
                    947:                reply->length = rlen;
                    948:                memcpy(reply->dat, p, rlen);
                    949:                memset(p, 0, rlen);
                    950:                xfree(p);
                    951:        }
                    952:        buffer_free(&m);
1.20    ! deraadt   953:        return (success);
1.19      markus    954: }
                    955: #endif
1.17      itojun    956:
                    957: #ifdef KRB5
                    958: int
                    959: mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp)
                    960: {
                    961:        krb5_data *tkt, *reply;
                    962:        Buffer m;
                    963:        int success;
                    964:
                    965:        debug3("%s entering", __func__);
                    966:        tkt = (krb5_data *) argp;
                    967:        reply = (krb5_data *) resp;
                    968:
                    969:        buffer_init(&m);
                    970:        buffer_put_string(&m, tkt->data, tkt->length);
                    971:
                    972:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m);
                    973:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m);
                    974:
                    975:        success = buffer_get_int(&m);
                    976:        if (success) {
                    977:                u_int len;
                    978:
                    979:                *userp = buffer_get_string(&m, NULL);
                    980:                reply->data = buffer_get_string(&m, &len);
                    981:                reply->length = len;
                    982:        } else {
                    983:                memset(reply, 0, sizeof(*reply));
                    984:                *userp = NULL;
                    985:        }
                    986:
                    987:        buffer_free(&m);
                    988:        return (success);
                    989: }
                    990: #endif