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

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