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

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