[BACK]Return to monitor_wrap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.39

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