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

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.8     ! markus     28: RCSID("$OpenBSD: monitor_wrap.c,v 1.7 2002/05/15 15:47:49 mouring 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: {
                     65:        u_char buf[5];
                     66:        u_int mlen = buffer_len(m);
                     67:
1.8     ! markus     68:        debug3("%s entering: type %d", __func__, type);
1.1       provos     69:
                     70:        PUT_32BIT(buf, mlen + 1);
                     71:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
                     72:        if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
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];
                     82:        ssize_t res;
                     83:        u_int msg_len;
                     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:
                    208: char* mm_auth2_read_banner(void)
                    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);
                    222:
                    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.8     ! markus    412:                fatal("%s: bad ivlen: expected %d != %d", __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.8     ! markus    426:                fatal("%s: bad mac key lenght: %d > %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)
                    437:                error("newkeys_from_blob: remaining bytes in blob %d", len);
                    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;
                    493:        return len;
                    494: }
                    495:
1.2       markus    496: static void
1.1       provos    497: mm_send_kex(Buffer *m, Kex *kex)
                    498: {
                    499:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    500:        buffer_put_int(m, kex->we_need);
                    501:        buffer_put_int(m, kex->hostkey_type);
                    502:        buffer_put_int(m, kex->kex_type);
                    503:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    504:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    505:        buffer_put_int(m, kex->flags);
                    506:        buffer_put_cstring(m, kex->client_version_string);
                    507:        buffer_put_cstring(m, kex->server_version_string);
                    508: }
                    509:
                    510: void
1.7       mouring   511: mm_send_keystate(struct monitor *pmonitor)
1.1       provos    512: {
                    513:        Buffer m;
                    514:        u_char *blob, *p;
                    515:        u_int bloblen, plen;
                    516:
                    517:        buffer_init(&m);
                    518:
                    519:        if (!compat20) {
                    520:                u_char iv[24];
                    521:                int ivlen;
                    522:
                    523:                buffer_put_int(&m, packet_get_protocol_flags());
                    524:
                    525:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    526:
1.8     ! markus    527:                debug3("%s: Sending ssh1 IV", __func__);
1.1       provos    528:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    529:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    530:                buffer_put_string(&m, iv, ivlen);
                    531:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    532:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    533:                buffer_put_string(&m, iv, ivlen);
                    534:                goto skip;
                    535:        } else {
                    536:                /* Kex for rekeying */
1.7       mouring   537:                mm_send_kex(&m, *pmonitor->m_pkex);
1.1       provos    538:        }
                    539:
                    540:        debug3("%s: Sending new keys: %p %p",
1.8     ! markus    541:            __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1       provos    542:
                    543:        /* Keys from Kex */
                    544:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8     ! markus    545:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    546:
                    547:        buffer_put_string(&m, blob, bloblen);
                    548:        xfree(blob);
                    549:
                    550:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8     ! markus    551:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    552:
                    553:        buffer_put_string(&m, blob, bloblen);
                    554:        xfree(blob);
                    555:
                    556:        buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
                    557:        buffer_put_int(&m, packet_get_seqnr(MODE_IN));
                    558:
1.8     ! markus    559:        debug3("%s: New keys have been sent", __func__);
1.1       provos    560:  skip:
                    561:        /* More key context */
                    562:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    563:        p = xmalloc(plen+1);
                    564:        packet_get_keycontext(MODE_OUT, p);
                    565:        buffer_put_string(&m, p, plen);
                    566:        xfree(p);
                    567:
                    568:        plen = packet_get_keycontext(MODE_IN, NULL);
                    569:        p = xmalloc(plen+1);
                    570:        packet_get_keycontext(MODE_IN, p);
                    571:        buffer_put_string(&m, p, plen);
                    572:        xfree(p);
                    573:
                    574:        /* Compression state */
1.8     ! markus    575:        debug3("%s: Sending compression state", __func__);
1.1       provos    576:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    577:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    578:
                    579:        /* Network I/O buffers */
                    580:        buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
                    581:        buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
                    582:
1.7       mouring   583:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8     ! markus    584:        debug3("%s: Finished sending state", __func__);
1.1       provos    585:
                    586:        buffer_free(&m);
                    587: }
                    588:
                    589: int
                    590: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
                    591: {
                    592:        Buffer m;
                    593:        u_char *p;
                    594:        int success = 0;
                    595:
                    596:        buffer_init(&m);
1.7       mouring   597:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    598:
1.8     ! markus    599:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   600:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    601:
                    602:        success = buffer_get_int(&m);
                    603:        if (success == 0) {
1.8     ! markus    604:                debug3("%s: pty alloc failed", __func__);
1.1       provos    605:                buffer_free(&m);
                    606:                return (0);
                    607:        }
                    608:        p = buffer_get_string(&m, NULL);
                    609:        buffer_free(&m);
                    610:
                    611:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    612:        xfree(p);
                    613:
1.7       mouring   614:        *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
                    615:        *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1.1       provos    616:
                    617:        /* Success */
                    618:        return (1);
                    619: }
                    620:
                    621: void
                    622: mm_session_pty_cleanup2(void *session)
                    623: {
                    624:        Session *s = session;
                    625:        Buffer m;
                    626:
                    627:        if (s->ttyfd == -1)
                    628:                return;
                    629:        buffer_init(&m);
                    630:        buffer_put_cstring(&m, s->tty);
1.7       mouring   631:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    632:        buffer_free(&m);
1.3       markus    633:
1.1       provos    634:        /* closed dup'ed master */
                    635:        if (close(s->ptymaster) < 0)
                    636:                error("close(s->ptymaster): %s", strerror(errno));
                    637:
                    638:        /* unlink pty from session */
                    639:        s->ttyfd = -1;
                    640: }
                    641:
                    642: /* Request process termination */
                    643:
                    644: void
                    645: mm_terminate(void)
                    646: {
                    647:        Buffer m;
                    648:
                    649:        buffer_init(&m);
1.7       mouring   650:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    651:        buffer_free(&m);
                    652: }
                    653:
                    654: int
                    655: mm_ssh1_session_key(BIGNUM *num)
                    656: {
                    657:        int rsafail;
                    658:        Buffer m;
                    659:
                    660:        buffer_init(&m);
                    661:        buffer_put_bignum2(&m, num);
1.7       mouring   662:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    663:
1.7       mouring   664:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    665:
                    666:        rsafail = buffer_get_int(&m);
                    667:        buffer_get_bignum2(&m, num);
                    668:
                    669:        buffer_free(&m);
                    670:
                    671:        return (rsafail);
                    672: }
                    673:
1.2       markus    674: static void
1.1       provos    675: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    676:     char ***prompts, u_int **echo_on)
                    677: {
                    678:        *name       = xstrdup("");
                    679:        *infotxt    = xstrdup("");
                    680:        *numprompts = 1;
                    681:        *prompts = xmalloc(*numprompts * sizeof(char*));
                    682:        *echo_on = xmalloc(*numprompts * sizeof(u_int));
                    683:        (*echo_on)[0] = 0;
                    684: }
                    685:
                    686: int
                    687: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    688:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    689: {
                    690:        Buffer m;
                    691:        int res;
                    692:        char *challenge;
                    693:
1.8     ! markus    694:        debug3("%s: entering", __func__);
1.1       provos    695:
                    696:        buffer_init(&m);
1.7       mouring   697:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    698:
1.7       mouring   699:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    700:            &m);
                    701:        res = buffer_get_int(&m);
                    702:        if (res == -1) {
1.8     ! markus    703:                debug3("%s: no challenge", __func__);
1.1       provos    704:                buffer_free(&m);
                    705:                return (-1);
                    706:        }
                    707:
                    708:        /* Get the challenge, and format the response */
                    709:        challenge  = buffer_get_string(&m, NULL);
                    710:        buffer_free(&m);
                    711:
                    712:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    713:        (*prompts)[0] = challenge;
                    714:
1.8     ! markus    715:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    716:
                    717:        return (0);
                    718: }
                    719:
                    720: int
                    721: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    722: {
                    723:        Buffer m;
                    724:        int authok;
                    725:
1.8     ! markus    726:        debug3("%s: entering", __func__);
1.1       provos    727:        if (numresponses != 1)
                    728:                return (-1);
                    729:
                    730:        buffer_init(&m);
                    731:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   732:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    733:
1.7       mouring   734:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    735:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    736:
                    737:        authok = buffer_get_int(&m);
                    738:        buffer_free(&m);
                    739:
                    740:        return ((authok == 0) ? -1 : 0);
                    741: }
                    742:
                    743: int
                    744: mm_skey_query(void *ctx, char **name, char **infotxt,
                    745:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    746: {
                    747:        Buffer m;
                    748:        int len, res;
                    749:        char *p, *challenge;
                    750:
1.8     ! markus    751:        debug3("%s: entering", __func__);
1.1       provos    752:
                    753:        buffer_init(&m);
1.7       mouring   754:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1.1       provos    755:
1.7       mouring   756:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1.1       provos    757:            &m);
                    758:        res = buffer_get_int(&m);
                    759:        if (res == -1) {
1.8     ! markus    760:                debug3("%s: no challenge", __func__);
1.1       provos    761:                buffer_free(&m);
                    762:                return (-1);
                    763:        }
                    764:
                    765:        /* Get the challenge, and format the response */
                    766:        challenge  = buffer_get_string(&m, NULL);
                    767:        buffer_free(&m);
                    768:
1.8     ! markus    769:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    770:
                    771:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    772:
                    773:        len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
                    774:        p = xmalloc(len);
                    775:        strlcpy(p, challenge, len);
                    776:        strlcat(p, SKEY_PROMPT, len);
                    777:        (*prompts)[0] = p;
                    778:        xfree(challenge);
                    779:
                    780:        return (0);
                    781: }
                    782:
                    783: int
                    784: mm_skey_respond(void *ctx, u_int numresponses, char **responses)
                    785: {
                    786:        Buffer m;
                    787:        int authok;
                    788:
1.8     ! markus    789:        debug3("%s: entering", __func__);
1.1       provos    790:        if (numresponses != 1)
                    791:                return (-1);
                    792:
                    793:        buffer_init(&m);
                    794:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   795:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1.1       provos    796:
1.7       mouring   797:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    798:            MONITOR_ANS_SKEYRESPOND, &m);
                    799:
                    800:        authok = buffer_get_int(&m);
                    801:        buffer_free(&m);
                    802:
                    803:        return ((authok == 0) ? -1 : 0);
                    804: }
                    805:
                    806: void
                    807: mm_ssh1_session_id(u_char session_id[16])
                    808: {
                    809:        Buffer m;
                    810:        int i;
                    811:
1.8     ! markus    812:        debug3("%s entering", __func__);
1.1       provos    813:
                    814:        buffer_init(&m);
                    815:        for (i = 0; i < 16; i++)
                    816:                buffer_put_char(&m, session_id[i]);
                    817:
1.7       mouring   818:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    819:        buffer_free(&m);
                    820: }
                    821:
                    822: int
                    823: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    824: {
                    825:        Buffer m;
                    826:        Key *key;
                    827:        u_char *blob;
                    828:        u_int blen;
                    829:        int allowed = 0;
                    830:
1.8     ! markus    831:        debug3("%s entering", __func__);
1.1       provos    832:
                    833:        buffer_init(&m);
                    834:        buffer_put_bignum2(&m, client_n);
                    835:
1.7       mouring   836:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    837:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    838:
                    839:        allowed = buffer_get_int(&m);
                    840:
                    841:        if (allowed && rkey != NULL) {
                    842:                blob = buffer_get_string(&m, &blen);
                    843:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8     ! markus    844:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    845:                *rkey = key;
                    846:                xfree(blob);
                    847:        }
                    848:        mm_send_debug(&m);
                    849:        buffer_free(&m);
                    850:
                    851:        return (allowed);
                    852: }
                    853:
                    854: BIGNUM *
                    855: mm_auth_rsa_generate_challenge(Key *key)
                    856: {
                    857:        Buffer m;
                    858:        BIGNUM *challenge;
                    859:        u_char *blob;
                    860:        u_int blen;
                    861:
1.8     ! markus    862:        debug3("%s entering", __func__);
1.1       provos    863:
                    864:        if ((challenge = BN_new()) == NULL)
1.8     ! markus    865:                fatal("%s: BN_new failed", __func__);
1.1       provos    866:
                    867:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    868:        if (key_to_blob(key, &blob, &blen) == 0)
1.8     ! markus    869:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    870:        key->type = KEY_RSA1;
                    871:
                    872:        buffer_init(&m);
                    873:        buffer_put_string(&m, blob, blen);
                    874:        xfree(blob);
                    875:
1.7       mouring   876:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    877:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    878:
                    879:        buffer_get_bignum2(&m, challenge);
                    880:        buffer_free(&m);
                    881:
                    882:        return (challenge);
                    883: }
                    884:
                    885: int
                    886: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    887: {
                    888:        Buffer m;
                    889:        u_char *blob;
                    890:        u_int blen;
                    891:        int success = 0;
                    892:
1.8     ! markus    893:        debug3("%s entering", __func__);
1.1       provos    894:
                    895:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    896:        if (key_to_blob(key, &blob, &blen) == 0)
1.8     ! markus    897:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    898:        key->type = KEY_RSA1;
                    899:
                    900:        buffer_init(&m);
                    901:        buffer_put_string(&m, blob, blen);
                    902:        buffer_put_string(&m, response, 16);
                    903:        xfree(blob);
                    904:
1.7       mouring   905:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    906:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    907:
                    908:        success = buffer_get_int(&m);
                    909:        buffer_free(&m);
                    910:
                    911:        return (success);
                    912: }