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

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