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

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