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

1.49    ! stevesk     1: /* $OpenBSD: monitor_wrap.c,v 1.48 2006/07/22 20:48:23 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.49    ! stevesk    37: #include <stdio.h>
1.48      stevesk    38: #include <string.h>
1.1       provos     39:
                     40: #include "ssh.h"
                     41: #include "dh.h"
                     42: #include "kex.h"
                     43: #include "auth.h"
1.22      markus     44: #include "auth-options.h"
1.1       provos     45: #include "buffer.h"
                     46: #include "bufaux.h"
                     47: #include "packet.h"
                     48: #include "mac.h"
                     49: #include "log.h"
                     50: #include "zlib.h"
                     51: #include "monitor.h"
                     52: #include "monitor_wrap.h"
                     53: #include "xmalloc.h"
                     54: #include "atomicio.h"
                     55: #include "monitor_fdpass.h"
1.45      djm        56: #include "misc.h"
1.1       provos     57:
                     58: #include "auth.h"
                     59: #include "channels.h"
                     60: #include "session.h"
                     61:
1.29      markus     62: #ifdef GSSAPI
                     63: #include "ssh-gss.h"
                     64: #endif
                     65:
1.1       provos     66: /* Imports */
                     67: extern int compat20;
                     68: extern Newkeys *newkeys[];
                     69: extern z_stream incoming_stream;
                     70: extern z_stream outgoing_stream;
1.7       mouring    71: extern struct monitor *pmonitor;
1.1       provos     72: extern Buffer input, output;
1.39      dtucker    73: extern Buffer loginmsg;
1.1       provos     74:
1.32      markus     75: int
                     76: mm_is_monitor(void)
                     77: {
                     78:        /*
                     79:         * m_pid is only set in the privileged part, and
                     80:         * points to the unprivileged child.
                     81:         */
1.34      markus     82:        return (pmonitor && pmonitor->m_pid > 0);
1.32      markus     83: }
                     84:
1.1       provos     85: void
1.36      avsm       86: mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos     87: {
1.13      deraadt    88:        u_int mlen = buffer_len(m);
1.1       provos     89:        u_char buf[5];
                     90:
1.8       markus     91:        debug3("%s entering: type %d", __func__, type);
1.1       provos     92:
1.45      djm        93:        put_u32(buf, mlen + 1);
1.10      deraadt    94:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
1.36      avsm       95:        if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
1.40      avsm       96:                fatal("%s: write: %s", __func__, strerror(errno));
1.36      avsm       97:        if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
1.40      avsm       98:                fatal("%s: write: %s", __func__, strerror(errno));
1.1       provos     99: }
                    100:
                    101: void
1.36      avsm      102: mm_request_receive(int sock, Buffer *m)
1.1       provos    103: {
                    104:        u_char buf[4];
1.13      deraadt   105:        u_int msg_len;
1.1       provos    106:
1.8       markus    107:        debug3("%s entering", __func__);
1.1       provos    108:
1.40      avsm      109:        if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
                    110:                if (errno == EPIPE)
1.32      markus    111:                        cleanup_exit(255);
1.40      avsm      112:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    113:        }
1.45      djm       114:        msg_len = get_u32(buf);
1.1       provos    115:        if (msg_len > 256 * 1024)
1.8       markus    116:                fatal("%s: read: bad msg_len %d", __func__, msg_len);
1.1       provos    117:        buffer_clear(m);
                    118:        buffer_append_space(m, msg_len);
1.40      avsm      119:        if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
                    120:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    121: }
                    122:
                    123: void
1.36      avsm      124: mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos    125: {
                    126:        u_char rtype;
                    127:
1.8       markus    128:        debug3("%s entering: type %d", __func__, type);
1.1       provos    129:
1.36      avsm      130:        mm_request_receive(sock, m);
1.1       provos    131:        rtype = buffer_get_char(m);
                    132:        if (rtype != type)
1.8       markus    133:                fatal("%s: read: rtype %d != type %d", __func__,
1.1       provos    134:                    rtype, type);
                    135: }
                    136:
                    137: DH *
                    138: mm_choose_dh(int min, int nbits, int max)
                    139: {
                    140:        BIGNUM *p, *g;
                    141:        int success = 0;
                    142:        Buffer m;
                    143:
                    144:        buffer_init(&m);
                    145:        buffer_put_int(&m, min);
                    146:        buffer_put_int(&m, nbits);
                    147:        buffer_put_int(&m, max);
                    148:
1.7       mouring   149:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
1.1       provos    150:
1.8       markus    151:        debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
1.7       mouring   152:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
1.1       provos    153:
                    154:        success = buffer_get_char(&m);
                    155:        if (success == 0)
1.8       markus    156:                fatal("%s: MONITOR_ANS_MODULI failed", __func__);
1.1       provos    157:
                    158:        if ((p = BN_new()) == NULL)
1.8       markus    159:                fatal("%s: BN_new failed", __func__);
1.3       markus    160:        if ((g = BN_new()) == NULL)
1.8       markus    161:                fatal("%s: BN_new failed", __func__);
1.1       provos    162:        buffer_get_bignum2(&m, p);
                    163:        buffer_get_bignum2(&m, g);
                    164:
1.8       markus    165:        debug3("%s: remaining %d", __func__, buffer_len(&m));
1.1       provos    166:        buffer_free(&m);
                    167:
                    168:        return (dh_new_group(g, p));
                    169: }
                    170:
                    171: int
                    172: mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
                    173: {
1.7       mouring   174:        Kex *kex = *pmonitor->m_pkex;
1.1       provos    175:        Buffer m;
                    176:
1.8       markus    177:        debug3("%s entering", __func__);
1.1       provos    178:
                    179:        buffer_init(&m);
                    180:        buffer_put_int(&m, kex->host_key_index(key));
                    181:        buffer_put_string(&m, data, datalen);
                    182:
1.7       mouring   183:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
1.1       provos    184:
1.8       markus    185:        debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
1.7       mouring   186:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
1.1       provos    187:        *sigp  = buffer_get_string(&m, lenp);
                    188:        buffer_free(&m);
                    189:
                    190:        return (0);
                    191: }
                    192:
                    193: struct passwd *
1.37      dtucker   194: mm_getpwnamallow(const char *username)
1.1       provos    195: {
                    196:        Buffer m;
                    197:        struct passwd *pw;
                    198:        u_int pwlen;
                    199:
1.8       markus    200:        debug3("%s entering", __func__);
1.1       provos    201:
                    202:        buffer_init(&m);
1.37      dtucker   203:        buffer_put_cstring(&m, username);
1.1       provos    204:
1.7       mouring   205:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
1.1       provos    206:
1.8       markus    207:        debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
1.7       mouring   208:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
1.1       provos    209:
                    210:        if (buffer_get_char(&m) == 0) {
                    211:                buffer_free(&m);
                    212:                return (NULL);
                    213:        }
                    214:        pw = buffer_get_string(&m, &pwlen);
                    215:        if (pwlen != sizeof(struct passwd))
1.8       markus    216:                fatal("%s: struct passwd size mismatch", __func__);
1.1       provos    217:        pw->pw_name = buffer_get_string(&m, NULL);
                    218:        pw->pw_passwd = buffer_get_string(&m, NULL);
                    219:        pw->pw_gecos = buffer_get_string(&m, NULL);
                    220:        pw->pw_class = buffer_get_string(&m, NULL);
                    221:        pw->pw_dir = buffer_get_string(&m, NULL);
                    222:        pw->pw_shell = buffer_get_string(&m, NULL);
                    223:        buffer_free(&m);
                    224:
                    225:        return (pw);
1.6       djm       226: }
                    227:
1.33      markus    228: char *
                    229: mm_auth2_read_banner(void)
1.6       djm       230: {
                    231:        Buffer m;
                    232:        char *banner;
                    233:
1.8       markus    234:        debug3("%s entering", __func__);
1.6       djm       235:
                    236:        buffer_init(&m);
1.7       mouring   237:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       238:        buffer_clear(&m);
                    239:
1.33      markus    240:        mm_request_receive_expect(pmonitor->m_recvfd,
                    241:            MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       242:        banner = buffer_get_string(&m, NULL);
                    243:        buffer_free(&m);
1.10      deraadt   244:
1.33      markus    245:        /* treat empty banner as missing banner */
                    246:        if (strlen(banner) == 0) {
                    247:                xfree(banner);
                    248:                banner = NULL;
                    249:        }
1.6       djm       250:        return (banner);
1.1       provos    251: }
                    252:
                    253: /* Inform the privileged process about service and style */
                    254:
                    255: void
                    256: mm_inform_authserv(char *service, char *style)
                    257: {
                    258:        Buffer m;
                    259:
1.8       markus    260:        debug3("%s entering", __func__);
1.1       provos    261:
                    262:        buffer_init(&m);
                    263:        buffer_put_cstring(&m, service);
                    264:        buffer_put_cstring(&m, style ? style : "");
                    265:
1.7       mouring   266:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    267:
                    268:        buffer_free(&m);
                    269: }
                    270:
                    271: /* Do the password authentication */
                    272: int
                    273: mm_auth_password(Authctxt *authctxt, char *password)
                    274: {
                    275:        Buffer m;
                    276:        int authenticated = 0;
                    277:
1.8       markus    278:        debug3("%s entering", __func__);
1.1       provos    279:
                    280:        buffer_init(&m);
                    281:        buffer_put_cstring(&m, password);
1.7       mouring   282:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    283:
1.8       markus    284:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   285:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    286:
                    287:        authenticated = buffer_get_int(&m);
                    288:
                    289:        buffer_free(&m);
                    290:
1.3       markus    291:        debug3("%s: user %sauthenticated",
1.8       markus    292:            __func__, authenticated ? "" : "not ");
1.1       provos    293:        return (authenticated);
                    294: }
                    295:
                    296: int
                    297: mm_user_key_allowed(struct passwd *pw, Key *key)
                    298: {
                    299:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
                    300: }
                    301:
                    302: int
                    303: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
                    304:     Key *key)
                    305: {
                    306:        return (mm_key_allowed(MM_HOSTKEY, user, host, key));
                    307: }
                    308:
                    309: int
                    310: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
                    311:     char *host, Key *key)
                    312: {
                    313:        int ret;
                    314:
                    315:        key->type = KEY_RSA; /* XXX hack for key_to_blob */
                    316:        ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
                    317:        key->type = KEY_RSA1;
                    318:        return (ret);
                    319: }
                    320:
1.2       markus    321: static void
1.1       provos    322: mm_send_debug(Buffer *m)
                    323: {
                    324:        char *msg;
                    325:
                    326:        while (buffer_len(m)) {
                    327:                msg = buffer_get_string(m, NULL);
1.8       markus    328:                debug3("%s: Sending debug: %s", __func__, msg);
1.1       provos    329:                packet_send_debug("%s", msg);
                    330:                xfree(msg);
                    331:        }
                    332: }
                    333:
                    334: int
                    335: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
                    336: {
                    337:        Buffer m;
                    338:        u_char *blob;
                    339:        u_int len;
1.22      markus    340:        int allowed = 0, have_forced = 0;
1.1       provos    341:
1.8       markus    342:        debug3("%s entering", __func__);
1.1       provos    343:
                    344:        /* Convert the key to a blob and the pass it over */
                    345:        if (!key_to_blob(key, &blob, &len))
                    346:                return (0);
                    347:
                    348:        buffer_init(&m);
                    349:        buffer_put_int(&m, type);
                    350:        buffer_put_cstring(&m, user ? user : "");
                    351:        buffer_put_cstring(&m, host ? host : "");
                    352:        buffer_put_string(&m, blob, len);
                    353:        xfree(blob);
                    354:
1.7       mouring   355:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    356:
1.8       markus    357:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7       mouring   358:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    359:
                    360:        allowed = buffer_get_int(&m);
                    361:
1.22      markus    362:        /* fake forced command */
                    363:        auth_clear_options();
                    364:        have_forced = buffer_get_int(&m);
                    365:        forced_command = have_forced ? xstrdup("true") : NULL;
                    366:
1.1       provos    367:        /* Send potential debug messages */
                    368:        mm_send_debug(&m);
                    369:
                    370:        buffer_free(&m);
                    371:
                    372:        return (allowed);
                    373: }
                    374:
1.3       markus    375: /*
1.1       provos    376:  * This key verify needs to send the key type along, because the
                    377:  * privileged parent makes the decision if the key is allowed
                    378:  * for authentication.
                    379:  */
                    380:
                    381: int
                    382: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
                    383: {
                    384:        Buffer m;
                    385:        u_char *blob;
                    386:        u_int len;
                    387:        int verified = 0;
                    388:
1.8       markus    389:        debug3("%s entering", __func__);
1.1       provos    390:
                    391:        /* Convert the key to a blob and the pass it over */
                    392:        if (!key_to_blob(key, &blob, &len))
                    393:                return (0);
                    394:
                    395:        buffer_init(&m);
                    396:        buffer_put_string(&m, blob, len);
                    397:        buffer_put_string(&m, sig, siglen);
                    398:        buffer_put_string(&m, data, datalen);
                    399:        xfree(blob);
                    400:
1.7       mouring   401:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    402:
1.8       markus    403:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   404:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    405:
                    406:        verified = buffer_get_int(&m);
                    407:
                    408:        buffer_free(&m);
                    409:
                    410:        return (verified);
                    411: }
                    412:
                    413: /* Export key state after authentication */
                    414: Newkeys *
                    415: mm_newkeys_from_blob(u_char *blob, int blen)
                    416: {
                    417:        Buffer b;
                    418:        u_int len;
                    419:        Newkeys *newkey = NULL;
                    420:        Enc *enc;
                    421:        Mac *mac;
                    422:        Comp *comp;
                    423:
1.8       markus    424:        debug3("%s: %p(%d)", __func__, blob, blen);
1.1       provos    425: #ifdef DEBUG_PK
                    426:        dump_base64(stderr, blob, blen);
                    427: #endif
                    428:        buffer_init(&b);
                    429:        buffer_append(&b, blob, blen);
                    430:
                    431:        newkey = xmalloc(sizeof(*newkey));
                    432:        enc = &newkey->enc;
                    433:        mac = &newkey->mac;
                    434:        comp = &newkey->comp;
                    435:
                    436:        /* Enc structure */
                    437:        enc->name = buffer_get_string(&b, NULL);
                    438:        buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
                    439:        enc->enabled = buffer_get_int(&b);
                    440:        enc->block_size = buffer_get_int(&b);
                    441:        enc->key = buffer_get_string(&b, &enc->key_len);
                    442:        enc->iv = buffer_get_string(&b, &len);
                    443:        if (len != enc->block_size)
1.12      deraadt   444:                fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1       provos    445:                    enc->block_size, len);
                    446:
                    447:        if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8       markus    448:                fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1       provos    449:                    enc->name, enc->cipher);
                    450:
                    451:        /* Mac structure */
                    452:        mac->name = buffer_get_string(&b, NULL);
                    453:        if (mac->name == NULL || mac_init(mac, mac->name) == -1)
1.8       markus    454:                fatal("%s: can not init mac %s", __func__, mac->name);
1.1       provos    455:        mac->enabled = buffer_get_int(&b);
                    456:        mac->key = buffer_get_string(&b, &len);
                    457:        if (len > mac->key_len)
1.12      deraadt   458:                fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1       provos    459:                    mac->key_len);
                    460:        mac->key_len = len;
                    461:
                    462:        /* Comp structure */
                    463:        comp->type = buffer_get_int(&b);
                    464:        comp->enabled = buffer_get_int(&b);
                    465:        comp->name = buffer_get_string(&b, NULL);
                    466:
                    467:        len = buffer_len(&b);
                    468:        if (len != 0)
1.12      deraadt   469:                error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1       provos    470:        buffer_free(&b);
                    471:        return (newkey);
                    472: }
                    473:
                    474: int
                    475: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
                    476: {
                    477:        Buffer b;
                    478:        int len;
                    479:        Enc *enc;
                    480:        Mac *mac;
                    481:        Comp *comp;
                    482:        Newkeys *newkey = newkeys[mode];
                    483:
1.8       markus    484:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    485:
                    486:        if (newkey == NULL) {
1.8       markus    487:                error("%s: newkey == NULL", __func__);
1.1       provos    488:                return 0;
                    489:        }
                    490:        enc = &newkey->enc;
                    491:        mac = &newkey->mac;
                    492:        comp = &newkey->comp;
                    493:
                    494:        buffer_init(&b);
                    495:        /* Enc structure */
                    496:        buffer_put_cstring(&b, enc->name);
                    497:        /* The cipher struct is constant and shared, you export pointer */
                    498:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    499:        buffer_put_int(&b, enc->enabled);
                    500:        buffer_put_int(&b, enc->block_size);
                    501:        buffer_put_string(&b, enc->key, enc->key_len);
                    502:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    503:        buffer_put_string(&b, enc->iv, enc->block_size);
                    504:
                    505:        /* Mac structure */
                    506:        buffer_put_cstring(&b, mac->name);
                    507:        buffer_put_int(&b, mac->enabled);
                    508:        buffer_put_string(&b, mac->key, mac->key_len);
                    509:
                    510:        /* Comp structure */
                    511:        buffer_put_int(&b, comp->type);
                    512:        buffer_put_int(&b, comp->enabled);
                    513:        buffer_put_cstring(&b, comp->name);
                    514:
                    515:        len = buffer_len(&b);
1.16      markus    516:        if (lenp != NULL)
                    517:                *lenp = len;
                    518:        if (blobp != NULL) {
                    519:                *blobp = xmalloc(len);
                    520:                memcpy(*blobp, buffer_ptr(&b), len);
                    521:        }
1.1       provos    522:        memset(buffer_ptr(&b), 0, len);
                    523:        buffer_free(&b);
                    524:        return len;
                    525: }
                    526:
1.2       markus    527: static void
1.1       provos    528: mm_send_kex(Buffer *m, Kex *kex)
                    529: {
                    530:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    531:        buffer_put_int(m, kex->we_need);
                    532:        buffer_put_int(m, kex->hostkey_type);
                    533:        buffer_put_int(m, kex->kex_type);
                    534:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    535:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    536:        buffer_put_int(m, kex->flags);
                    537:        buffer_put_cstring(m, kex->client_version_string);
                    538:        buffer_put_cstring(m, kex->server_version_string);
                    539: }
                    540:
                    541: void
1.36      avsm      542: mm_send_keystate(struct monitor *monitor)
1.1       provos    543: {
                    544:        Buffer m;
                    545:        u_char *blob, *p;
                    546:        u_int bloblen, plen;
1.25      markus    547:        u_int32_t seqnr, packets;
                    548:        u_int64_t blocks;
1.1       provos    549:
                    550:        buffer_init(&m);
                    551:
                    552:        if (!compat20) {
                    553:                u_char iv[24];
1.11      markus    554:                u_char *key;
                    555:                u_int ivlen, keylen;
1.1       provos    556:
                    557:                buffer_put_int(&m, packet_get_protocol_flags());
                    558:
                    559:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    560:
1.11      markus    561:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    562:                keylen = packet_get_encryption_key(NULL);
                    563:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    564:                keylen = packet_get_encryption_key(key);
                    565:                buffer_put_string(&m, key, keylen);
                    566:                memset(key, 0, keylen);
                    567:                xfree(key);
                    568:
1.1       provos    569:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    570:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    571:                buffer_put_string(&m, iv, ivlen);
                    572:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    573:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    574:                buffer_put_string(&m, iv, ivlen);
                    575:                goto skip;
                    576:        } else {
                    577:                /* Kex for rekeying */
1.36      avsm      578:                mm_send_kex(&m, *monitor->m_pkex);
1.1       provos    579:        }
                    580:
                    581:        debug3("%s: Sending new keys: %p %p",
1.8       markus    582:            __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
1.1       provos    583:
                    584:        /* Keys from Kex */
                    585:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    586:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    587:
                    588:        buffer_put_string(&m, blob, bloblen);
                    589:        xfree(blob);
                    590:
                    591:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    592:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    593:
                    594:        buffer_put_string(&m, blob, bloblen);
                    595:        xfree(blob);
                    596:
1.25      markus    597:        packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
                    598:        buffer_put_int(&m, seqnr);
                    599:        buffer_put_int64(&m, blocks);
                    600:        buffer_put_int(&m, packets);
1.26      markus    601:        packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
1.25      markus    602:        buffer_put_int(&m, seqnr);
                    603:        buffer_put_int64(&m, blocks);
                    604:        buffer_put_int(&m, packets);
1.1       provos    605:
1.8       markus    606:        debug3("%s: New keys have been sent", __func__);
1.1       provos    607:  skip:
                    608:        /* More key context */
                    609:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    610:        p = xmalloc(plen+1);
                    611:        packet_get_keycontext(MODE_OUT, p);
                    612:        buffer_put_string(&m, p, plen);
                    613:        xfree(p);
                    614:
                    615:        plen = packet_get_keycontext(MODE_IN, NULL);
                    616:        p = xmalloc(plen+1);
                    617:        packet_get_keycontext(MODE_IN, p);
                    618:        buffer_put_string(&m, p, plen);
                    619:        xfree(p);
                    620:
                    621:        /* Compression state */
1.8       markus    622:        debug3("%s: Sending compression state", __func__);
1.1       provos    623:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    624:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    625:
                    626:        /* Network I/O buffers */
                    627:        buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
                    628:        buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
                    629:
1.36      avsm      630:        mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    631:        debug3("%s: Finished sending state", __func__);
1.1       provos    632:
                    633:        buffer_free(&m);
                    634: }
                    635:
                    636: int
1.42      deraadt   637: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1       provos    638: {
                    639:        Buffer m;
1.39      dtucker   640:        char *p, *msg;
1.1       provos    641:        int success = 0;
                    642:
                    643:        buffer_init(&m);
1.7       mouring   644:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    645:
1.8       markus    646:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   647:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    648:
                    649:        success = buffer_get_int(&m);
                    650:        if (success == 0) {
1.8       markus    651:                debug3("%s: pty alloc failed", __func__);
1.1       provos    652:                buffer_free(&m);
                    653:                return (0);
                    654:        }
                    655:        p = buffer_get_string(&m, NULL);
1.39      dtucker   656:        msg = buffer_get_string(&m, NULL);
1.1       provos    657:        buffer_free(&m);
                    658:
                    659:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    660:        xfree(p);
1.39      dtucker   661:
                    662:        buffer_append(&loginmsg, msg, strlen(msg));
                    663:        xfree(msg);
1.1       provos    664:
1.7       mouring   665:        *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
                    666:        *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1.1       provos    667:
                    668:        /* Success */
                    669:        return (1);
                    670: }
                    671:
                    672: void
1.32      markus    673: mm_session_pty_cleanup2(Session *s)
1.1       provos    674: {
                    675:        Buffer m;
                    676:
                    677:        if (s->ttyfd == -1)
                    678:                return;
                    679:        buffer_init(&m);
                    680:        buffer_put_cstring(&m, s->tty);
1.7       mouring   681:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    682:        buffer_free(&m);
1.3       markus    683:
1.1       provos    684:        /* closed dup'ed master */
                    685:        if (close(s->ptymaster) < 0)
                    686:                error("close(s->ptymaster): %s", strerror(errno));
                    687:
                    688:        /* unlink pty from session */
                    689:        s->ttyfd = -1;
                    690: }
                    691:
                    692: /* Request process termination */
                    693:
                    694: void
                    695: mm_terminate(void)
                    696: {
                    697:        Buffer m;
                    698:
                    699:        buffer_init(&m);
1.7       mouring   700:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    701:        buffer_free(&m);
                    702: }
                    703:
                    704: int
                    705: mm_ssh1_session_key(BIGNUM *num)
                    706: {
                    707:        int rsafail;
                    708:        Buffer m;
                    709:
                    710:        buffer_init(&m);
                    711:        buffer_put_bignum2(&m, num);
1.7       mouring   712:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    713:
1.7       mouring   714:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    715:
                    716:        rsafail = buffer_get_int(&m);
                    717:        buffer_get_bignum2(&m, num);
                    718:
                    719:        buffer_free(&m);
                    720:
                    721:        return (rsafail);
                    722: }
                    723:
1.2       markus    724: static void
1.1       provos    725: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    726:     char ***prompts, u_int **echo_on)
                    727: {
1.10      deraadt   728:        *name = xstrdup("");
                    729:        *infotxt = xstrdup("");
1.1       provos    730:        *numprompts = 1;
1.43      djm       731:        *prompts = xcalloc(*numprompts, sizeof(char *));
                    732:        *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1       provos    733:        (*echo_on)[0] = 0;
                    734: }
                    735:
                    736: int
                    737: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    738:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    739: {
                    740:        Buffer m;
1.21      markus    741:        u_int success;
1.1       provos    742:        char *challenge;
                    743:
1.8       markus    744:        debug3("%s: entering", __func__);
1.1       provos    745:
                    746:        buffer_init(&m);
1.7       mouring   747:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    748:
1.7       mouring   749:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    750:            &m);
1.21      markus    751:        success = buffer_get_int(&m);
                    752:        if (success == 0) {
1.8       markus    753:                debug3("%s: no challenge", __func__);
1.1       provos    754:                buffer_free(&m);
                    755:                return (-1);
                    756:        }
                    757:
                    758:        /* Get the challenge, and format the response */
                    759:        challenge  = buffer_get_string(&m, NULL);
                    760:        buffer_free(&m);
                    761:
                    762:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    763:        (*prompts)[0] = challenge;
                    764:
1.8       markus    765:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    766:
                    767:        return (0);
                    768: }
                    769:
                    770: int
                    771: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    772: {
                    773:        Buffer m;
                    774:        int authok;
                    775:
1.8       markus    776:        debug3("%s: entering", __func__);
1.1       provos    777:        if (numresponses != 1)
                    778:                return (-1);
                    779:
                    780:        buffer_init(&m);
                    781:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   782:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    783:
1.7       mouring   784:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    785:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    786:
                    787:        authok = buffer_get_int(&m);
                    788:        buffer_free(&m);
                    789:
                    790:        return ((authok == 0) ? -1 : 0);
                    791: }
                    792:
1.38      dtucker   793: #ifdef SKEY
1.1       provos    794: int
                    795: mm_skey_query(void *ctx, char **name, char **infotxt,
                    796:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    797: {
                    798:        Buffer m;
1.21      markus    799:        int len;
                    800:        u_int success;
1.1       provos    801:        char *p, *challenge;
                    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.43      djm       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 */