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

Annotation of src/usr.bin/ssh/monitor.c, Revision 1.9.2.1

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.9.2.1 ! jason      28: RCSID("$OpenBSD: monitor.c,v 1.11 2002/05/15 15:47:49 mouring Exp $");
1.1       provos     29:
                     30: #include <openssl/dh.h>
                     31:
                     32: #ifdef SKEY
                     33: #include <skey.h>
                     34: #endif
                     35:
                     36: #include "ssh.h"
                     37: #include "auth.h"
                     38: #include "kex.h"
                     39: #include "dh.h"
                     40: #include "zlib.h"
                     41: #include "packet.h"
                     42: #include "auth-options.h"
                     43: #include "sshpty.h"
                     44: #include "channels.h"
                     45: #include "session.h"
                     46: #include "sshlogin.h"
                     47: #include "canohost.h"
                     48: #include "log.h"
                     49: #include "servconf.h"
                     50: #include "monitor.h"
                     51: #include "monitor_mm.h"
                     52: #include "monitor_wrap.h"
                     53: #include "monitor_fdpass.h"
                     54: #include "xmalloc.h"
                     55: #include "misc.h"
                     56: #include "buffer.h"
                     57: #include "bufaux.h"
                     58: #include "compat.h"
                     59: #include "ssh2.h"
                     60: #include "mpaux.h"
                     61:
                     62: /* Imports */
                     63: extern ServerOptions options;
                     64: extern u_int utmp_len;
                     65: extern Newkeys *current_keys[];
                     66: extern z_stream incoming_stream;
                     67: extern z_stream outgoing_stream;
                     68: extern u_char session_id[];
                     69: extern Buffer input, output;
                     70: extern Buffer auth_debug;
                     71: extern int auth_debug_init;
                     72:
                     73: /* State exported from the child */
                     74:
                     75: struct {
                     76:        z_stream incoming;
                     77:        z_stream outgoing;
                     78:        u_char *keyin;
                     79:        u_int keyinlen;
                     80:        u_char *keyout;
                     81:        u_int keyoutlen;
                     82:        u_char *ivin;
                     83:        u_int ivinlen;
                     84:        u_char *ivout;
                     85:        u_int ivoutlen;
                     86:        int ssh1cipher;
                     87:        int ssh1protoflags;
                     88:        u_char *input;
                     89:        u_int ilen;
                     90:        u_char *output;
                     91:        u_int olen;
                     92: } child_state;
                     93:
                     94: /* Functions on the montior that answer unprivileged requests */
                     95:
                     96: int mm_answer_moduli(int, Buffer *);
                     97: int mm_answer_sign(int, Buffer *);
                     98: int mm_answer_pwnamallow(int, Buffer *);
1.9.2.1 ! jason      99: int mm_answer_auth2_read_banner(int, Buffer *);
1.1       provos    100: int mm_answer_authserv(int, Buffer *);
                    101: int mm_answer_authpassword(int, Buffer *);
                    102: int mm_answer_bsdauthquery(int, Buffer *);
                    103: int mm_answer_bsdauthrespond(int, Buffer *);
                    104: int mm_answer_skeyquery(int, Buffer *);
                    105: int mm_answer_skeyrespond(int, Buffer *);
                    106: int mm_answer_keyallowed(int, Buffer *);
                    107: int mm_answer_keyverify(int, Buffer *);
                    108: int mm_answer_pty(int, Buffer *);
                    109: int mm_answer_pty_cleanup(int, Buffer *);
                    110: int mm_answer_term(int, Buffer *);
                    111: int mm_answer_rsa_keyallowed(int, Buffer *);
                    112: int mm_answer_rsa_challenge(int, Buffer *);
                    113: int mm_answer_rsa_response(int, Buffer *);
                    114: int mm_answer_sesskey(int, Buffer *);
                    115: int mm_answer_sessid(int, Buffer *);
                    116:
                    117: static Authctxt *authctxt;
                    118: static BIGNUM *ssh1_challenge = NULL;  /* used for ssh1 rsa auth */
                    119:
                    120: /* local state for key verify */
                    121: static u_char *key_blob = NULL;
                    122: static u_int key_bloblen = 0;
                    123: static int key_blobtype = MM_NOKEY;
                    124: static u_char *hostbased_cuser = NULL;
                    125: static u_char *hostbased_chost = NULL;
                    126: static char *auth_method = "unknown";
                    127:
                    128: struct mon_table {
                    129:        enum monitor_reqtype type;
                    130:        int flags;
                    131:        int (*f)(int, Buffer *);
                    132: };
                    133:
                    134: #define MON_ISAUTH     0x0004  /* Required for Authentication */
                    135: #define MON_AUTHDECIDE 0x0008  /* Decides Authentication */
                    136: #define MON_ONCE       0x0010  /* Disable after calling */
                    137:
                    138: #define MON_AUTH       (MON_ISAUTH|MON_AUTHDECIDE)
                    139:
                    140: #define MON_PERMIT     0x1000  /* Request is permitted */
                    141:
                    142: struct mon_table mon_dispatch_proto20[] = {
                    143:     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
                    144:     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
                    145:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    146:     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.9.2.1 ! jason     147:     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1       provos    148:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
                    149: #ifdef BSD_AUTH
                    150:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
                    151:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
                    152: #endif
                    153: #ifdef SKEY
                    154:     {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
                    155:     {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
                    156: #endif
                    157:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
                    158:     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
                    159:     {0, 0, NULL}
                    160: };
                    161:
                    162: struct mon_table mon_dispatch_postauth20[] = {
                    163:     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
                    164:     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
                    165:     {MONITOR_REQ_PTY, 0, mm_answer_pty},
                    166:     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
                    167:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    168:     {0, 0, NULL}
                    169: };
                    170:
                    171: struct mon_table mon_dispatch_proto15[] = {
                    172:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    173:     {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
                    174:     {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
                    175:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
                    176:     {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
                    177:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
                    178:     {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
                    179:     {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
                    180: #ifdef BSD_AUTH
                    181:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
                    182:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
                    183: #endif
                    184: #ifdef SKEY
                    185:     {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
                    186:     {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
                    187: #endif
                    188:     {0, 0, NULL}
                    189: };
                    190:
                    191: struct mon_table mon_dispatch_postauth15[] = {
                    192:     {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
                    193:     {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
                    194:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    195:     {0, 0, NULL}
                    196: };
                    197:
                    198: struct mon_table *mon_dispatch;
                    199:
                    200: /* Specifies if a certain message is allowed at the moment */
                    201:
                    202: static void
                    203: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
                    204: {
                    205:        while (ent->f != NULL) {
                    206:                if (ent->type == type) {
                    207:                        ent->flags &= ~MON_PERMIT;
                    208:                        ent->flags |= permit ? MON_PERMIT : 0;
                    209:                        return;
                    210:                }
                    211:                ent++;
                    212:        }
                    213: }
                    214:
                    215: static void
                    216: monitor_permit_authentications(int permit)
                    217: {
                    218:        struct mon_table *ent = mon_dispatch;
                    219:
                    220:        while (ent->f != NULL) {
                    221:                if (ent->flags & MON_AUTH) {
                    222:                        ent->flags &= ~MON_PERMIT;
                    223:                        ent->flags |= permit ? MON_PERMIT : 0;
                    224:                }
                    225:                ent++;
                    226:        }
                    227: }
                    228:
                    229: Authctxt *
1.9.2.1 ! jason     230: monitor_child_preauth(struct monitor *pmonitor)
1.1       provos    231: {
                    232:        struct mon_table *ent;
                    233:        int authenticated = 0;
                    234:
                    235:        debug3("preauth child monitor started");
                    236:
                    237:        if (compat20) {
                    238:                mon_dispatch = mon_dispatch_proto20;
                    239:
                    240:                /* Permit requests for moduli and signatures */
                    241:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    242:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    243:        } else {
                    244:                mon_dispatch = mon_dispatch_proto15;
                    245:
                    246:                monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                    247:        }
                    248:
                    249:        authctxt = authctxt_new();
                    250:
                    251:        /* The first few requests do not require asynchronous access */
                    252:        while (!authenticated) {
1.9.2.1 ! jason     253:                authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
1.1       provos    254:                if (authenticated) {
                    255:                        if (!(ent->flags & MON_AUTHDECIDE))
                    256:                                fatal("%s: unexpected authentication from %d",
                    257:                                    __FUNCTION__, ent->type);
                    258:                        if (authctxt->pw->pw_uid == 0 &&
                    259:                            !auth_root_allowed(auth_method))
                    260:                                authenticated = 0;
                    261:                }
                    262:
                    263:                if (ent->flags & MON_AUTHDECIDE) {
                    264:                        auth_log(authctxt, authenticated, auth_method,
                    265:                            compat20 ? " ssh2" : "");
                    266:                        if (!authenticated)
                    267:                                authctxt->failures++;
                    268:                }
                    269:        }
                    270:
                    271:        if (!authctxt->valid)
                    272:                fatal("%s: authenticated invalid user", __FUNCTION__);
                    273:
                    274:        debug("%s: %s has been authenticated by privileged process",
                    275:            __FUNCTION__, authctxt->user);
                    276:
1.9.2.1 ! jason     277:        mm_get_keystate(pmonitor);
1.1       provos    278:
                    279:        return (authctxt);
                    280: }
                    281:
                    282: void
1.9.2.1 ! jason     283: monitor_child_postauth(struct monitor *pmonitor)
1.1       provos    284: {
                    285:        if (compat20) {
                    286:                mon_dispatch = mon_dispatch_postauth20;
                    287:
                    288:                /* Permit requests for moduli and signatures */
                    289:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    290:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    291:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    292:
                    293:        } else {
                    294:                mon_dispatch = mon_dispatch_postauth15;
                    295:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    296:        }
                    297:        if (!no_pty_flag) {
                    298:                monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
                    299:                monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
                    300:        }
                    301:
                    302:        for (;;)
1.9.2.1 ! jason     303:                monitor_read(pmonitor, mon_dispatch, NULL);
1.1       provos    304: }
                    305:
                    306: void
1.9.2.1 ! jason     307: monitor_sync(struct monitor *pmonitor)
1.1       provos    308: {
                    309:        /* The member allocation is not visible, so sync it */
1.9.2.1 ! jason     310:        mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
1.1       provos    311: }
                    312:
                    313: int
1.9.2.1 ! jason     314: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1       provos    315:     struct mon_table **pent)
                    316: {
                    317:        Buffer m;
                    318:        int ret;
                    319:        u_char type;
                    320:
                    321:        buffer_init(&m);
                    322:
1.9.2.1 ! jason     323:        mm_request_receive(pmonitor->m_sendfd, &m);
1.1       provos    324:        type = buffer_get_char(&m);
                    325:
                    326:        debug3("%s: checking request %d", __FUNCTION__, type);
                    327:
                    328:        while (ent->f != NULL) {
                    329:                if (ent->type == type)
                    330:                        break;
                    331:                ent++;
                    332:        }
                    333:
                    334:        if (ent->f != NULL) {
                    335:                if (!(ent->flags & MON_PERMIT))
                    336:                        fatal("%s: unpermitted request %d", __FUNCTION__,
                    337:                            type);
1.9.2.1 ! jason     338:                ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1       provos    339:                buffer_free(&m);
                    340:
                    341:                /* The child may use this request only once, disable it */
                    342:                if (ent->flags & MON_ONCE) {
                    343:                        debug2("%s: %d used once, disabling now", __FUNCTION__,
                    344:                            type);
                    345:                        ent->flags &= ~MON_PERMIT;
                    346:                }
                    347:
                    348:                if (pent != NULL)
                    349:                        *pent = ent;
1.3       markus    350:
1.1       provos    351:                return ret;
                    352:        }
                    353:
1.7       stevesk   354:        fatal("%s: unsupported request: %d", __FUNCTION__, type);
1.1       provos    355:
                    356:        /* NOTREACHED */
                    357:        return (-1);
                    358: }
                    359:
                    360: /* allowed key state */
                    361: static int
                    362: monitor_allowed_key(u_char *blob, u_int bloblen)
                    363: {
                    364:        /* make sure key is allowed */
                    365:        if (key_blob == NULL || key_bloblen != bloblen ||
                    366:            memcmp(key_blob, blob, key_bloblen))
                    367:                return (0);
                    368:        return (1);
                    369: }
                    370:
                    371: static void
                    372: monitor_reset_key_state(void)
                    373: {
                    374:        /* reset state */
                    375:        if (key_blob != NULL)
                    376:                xfree(key_blob);
                    377:        if (hostbased_cuser != NULL)
                    378:                xfree(hostbased_cuser);
                    379:        if (hostbased_chost != NULL)
                    380:                xfree(hostbased_chost);
                    381:        key_blob = NULL;
                    382:        key_bloblen = 0;
                    383:        key_blobtype = MM_NOKEY;
                    384:        hostbased_cuser = NULL;
                    385:        hostbased_chost = NULL;
                    386: }
                    387:
                    388: int
                    389: mm_answer_moduli(int socket, Buffer *m)
                    390: {
                    391:        DH *dh;
                    392:        int min, want, max;
                    393:
                    394:        min = buffer_get_int(m);
                    395:        want = buffer_get_int(m);
                    396:        max = buffer_get_int(m);
                    397:
                    398:        debug3("%s: got parameters: %d %d %d",
                    399:            __FUNCTION__, min, want, max);
                    400:        /* We need to check here, too, in case the child got corrupted */
                    401:        if (max < min || want < min || max < want)
                    402:                fatal("%s: bad parameters: %d %d %d",
                    403:                    __FUNCTION__, min, want, max);
                    404:
                    405:        buffer_clear(m);
                    406:
                    407:        dh = choose_dh(min, want, max);
                    408:        if (dh == NULL) {
                    409:                buffer_put_char(m, 0);
                    410:                return (0);
                    411:        } else {
                    412:                /* Send first bignum */
                    413:                buffer_put_char(m, 1);
                    414:                buffer_put_bignum2(m, dh->p);
                    415:                buffer_put_bignum2(m, dh->g);
1.3       markus    416:
1.1       provos    417:                DH_free(dh);
                    418:        }
                    419:        mm_request_send(socket, MONITOR_ANS_MODULI, m);
                    420:        return (0);
                    421: }
                    422:
                    423: int
                    424: mm_answer_sign(int socket, Buffer *m)
                    425: {
                    426:        Key *key;
                    427:        u_char *p;
                    428:        u_char *signature;
                    429:        u_int siglen, datlen;
                    430:        int keyid;
1.3       markus    431:
1.1       provos    432:        debug3("%s", __FUNCTION__);
                    433:
1.3       markus    434:        keyid = buffer_get_int(m);
                    435:        p = buffer_get_string(m, &datlen);
1.1       provos    436:
                    437:        if (datlen != 20)
                    438:                fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
                    439:
                    440:        if ((key = get_hostkey_by_index(keyid)) == NULL)
                    441:                fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
                    442:        if (key_sign(key, &signature, &siglen, p, datlen) < 0)
                    443:                fatal("%s: key_sign failed", __FUNCTION__);
                    444:
                    445:        debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
                    446:
                    447:        buffer_clear(m);
                    448:        buffer_put_string(m, signature, siglen);
                    449:
                    450:        xfree(p);
                    451:        xfree(signature);
                    452:
                    453:        mm_request_send(socket, MONITOR_ANS_SIGN, m);
                    454:
                    455:        /* Turn on permissions for getpwnam */
                    456:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                    457:
                    458:        return (0);
                    459: }
                    460:
                    461: /* Retrieves the password entry and also checks if the user is permitted */
                    462:
                    463: int
                    464: mm_answer_pwnamallow(int socket, Buffer *m)
                    465: {
                    466:        char *login;
                    467:        struct passwd *pwent;
                    468:        int allowed = 0;
1.3       markus    469:
1.1       provos    470:        debug3("%s", __FUNCTION__);
                    471:
                    472:        if (authctxt->attempt++ != 0)
                    473:                fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
                    474:
                    475:        login = buffer_get_string(m, NULL);
                    476:
                    477:        pwent = getpwnamallow(login);
                    478:
                    479:        authctxt->user = xstrdup(login);
                    480:        setproctitle("%s [priv]", pwent ? login : "unknown");
                    481:        xfree(login);
                    482:
                    483:        buffer_clear(m);
                    484:
                    485:        if (pwent == NULL) {
                    486:                buffer_put_char(m, 0);
                    487:                goto out;
                    488:        }
                    489:
                    490:        allowed = 1;
1.4       markus    491:        authctxt->pw = pwent;
1.1       provos    492:        authctxt->valid = 1;
                    493:
                    494:        buffer_put_char(m, 1);
                    495:        buffer_put_string(m, pwent, sizeof(struct passwd));
                    496:        buffer_put_cstring(m, pwent->pw_name);
                    497:        buffer_put_cstring(m, "*");
                    498:        buffer_put_cstring(m, pwent->pw_gecos);
                    499:        buffer_put_cstring(m, pwent->pw_class);
                    500:        buffer_put_cstring(m, pwent->pw_dir);
                    501:        buffer_put_cstring(m, pwent->pw_shell);
                    502:
                    503:  out:
                    504:        debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
                    505:        mm_request_send(socket, MONITOR_ANS_PWNAM, m);
                    506:
                    507:        /* For SSHv1 allow authentication now */
                    508:        if (!compat20)
                    509:                monitor_permit_authentications(1);
1.9.2.1 ! jason     510:        else {
1.1       provos    511:                /* Allow service/style information on the auth context */
                    512:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.9.2.1 ! jason     513:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
        !           514:        }
        !           515:
        !           516:
        !           517:        return (0);
        !           518: }
        !           519:
        !           520: int mm_answer_auth2_read_banner(int socket, Buffer *m)
        !           521: {
        !           522:        char *banner;
        !           523:
        !           524:        buffer_clear(m);
        !           525:        banner = auth2_read_banner();
        !           526:        buffer_put_cstring(m, banner != NULL ? banner : "");
        !           527:        mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1       provos    528:
1.9.2.1 ! jason     529:        if (banner != NULL)
        !           530:                free(banner);
1.1       provos    531:
                    532:        return (0);
                    533: }
                    534:
                    535: int
                    536: mm_answer_authserv(int socket, Buffer *m)
                    537: {
                    538:        monitor_permit_authentications(1);
                    539:
                    540:        authctxt->service = buffer_get_string(m, NULL);
                    541:        authctxt->style = buffer_get_string(m, NULL);
1.6       stevesk   542:        debug3("%s: service=%s, style=%s",
                    543:            __FUNCTION__, authctxt->service, authctxt->style);
                    544:
1.1       provos    545:        if (strlen(authctxt->style) == 0) {
                    546:                xfree(authctxt->style);
                    547:                authctxt->style = NULL;
                    548:        }
                    549:
                    550:        return (0);
                    551: }
                    552:
                    553: int
                    554: mm_answer_authpassword(int socket, Buffer *m)
                    555: {
                    556:        static int call_count;
                    557:        char *passwd;
                    558:        int authenticated, plen;
                    559:
                    560:        passwd = buffer_get_string(m, &plen);
                    561:        /* Only authenticate if the context is valid */
                    562:        authenticated = authctxt->valid && auth_password(authctxt, passwd);
                    563:        memset(passwd, 0, strlen(passwd));
                    564:        xfree(passwd);
                    565:
                    566:        buffer_clear(m);
                    567:        buffer_put_int(m, authenticated);
                    568:
                    569:        debug3("%s: sending result %d", __FUNCTION__, authenticated);
                    570:        mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
                    571:
                    572:        call_count++;
                    573:        if (plen == 0 && call_count == 1)
                    574:                auth_method = "none";
                    575:        else
                    576:                auth_method = "password";
                    577:
                    578:        /* Causes monitor loop to terminate if authenticated */
                    579:        return (authenticated);
                    580: }
                    581:
                    582: #ifdef BSD_AUTH
                    583: int
                    584: mm_answer_bsdauthquery(int socket, Buffer *m)
                    585: {
                    586:        char *name, *infotxt;
                    587:        u_int numprompts;
                    588:        u_int *echo_on;
                    589:        char **prompts;
                    590:        int res;
                    591:
                    592:        res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    593:            &prompts, &echo_on);
                    594:
                    595:        buffer_clear(m);
                    596:        buffer_put_int(m, res);
                    597:        if (res != -1)
                    598:                buffer_put_cstring(m, prompts[0]);
                    599:
                    600:        debug3("%s: sending challenge res: %d", __FUNCTION__, res);
                    601:        mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
                    602:
                    603:        if (res != -1) {
                    604:                xfree(name);
                    605:                xfree(infotxt);
                    606:                xfree(prompts);
                    607:                xfree(echo_on);
                    608:        }
                    609:
                    610:        return (0);
                    611: }
                    612:
                    613: int
                    614: mm_answer_bsdauthrespond(int socket, Buffer *m)
                    615: {
                    616:        char *response;
                    617:        int authok;
                    618:
                    619:        if (authctxt->as == 0)
                    620:                fatal("%s: no bsd auth session", __FUNCTION__);
                    621:
                    622:        response = buffer_get_string(m, NULL);
                    623:        authok = auth_userresponse(authctxt->as, response, 0);
                    624:        authctxt->as = NULL;
                    625:        debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
                    626:        xfree(response);
                    627:
                    628:        buffer_clear(m);
                    629:        buffer_put_int(m, authok);
                    630:
                    631:        debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
                    632:        mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
                    633:
                    634:        auth_method = "bsdauth";
                    635:
                    636:        return (authok != 0);
                    637: }
                    638: #endif
                    639:
                    640: #ifdef SKEY
                    641: int
                    642: mm_answer_skeyquery(int socket, Buffer *m)
                    643: {
                    644:        struct skey skey;
                    645:        char challenge[1024];
                    646:        int res;
                    647:
                    648:        res = skeychallenge(&skey, authctxt->user, challenge);
                    649:
                    650:        buffer_clear(m);
                    651:        buffer_put_int(m, res);
                    652:        if (res != -1)
                    653:                buffer_put_cstring(m, challenge);
                    654:
                    655:        debug3("%s: sending challenge res: %d", __FUNCTION__, res);
                    656:        mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
                    657:
                    658:        return (0);
                    659: }
                    660:
                    661: int
                    662: mm_answer_skeyrespond(int socket, Buffer *m)
                    663: {
                    664:        char *response;
                    665:        int authok;
                    666:
                    667:        response = buffer_get_string(m, NULL);
                    668:
                    669:        authok = (authctxt->valid &&
                    670:            skey_haskey(authctxt->pw->pw_name) == 0 &&
                    671:            skey_passcheck(authctxt->pw->pw_name, response) != -1);
                    672:
                    673:        xfree(response);
                    674:
                    675:        buffer_clear(m);
                    676:        buffer_put_int(m, authok);
                    677:
                    678:        debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
                    679:        mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
                    680:
                    681:        auth_method = "skey";
                    682:
                    683:        return (authok != 0);
                    684: }
                    685: #endif
                    686:
1.2       markus    687: static void
1.1       provos    688: mm_append_debug(Buffer *m)
                    689: {
                    690:        if (auth_debug_init && buffer_len(&auth_debug)) {
                    691:                debug3("%s: Appending debug messages for child", __FUNCTION__);
                    692:                buffer_append(m, buffer_ptr(&auth_debug),
                    693:                    buffer_len(&auth_debug));
                    694:                buffer_clear(&auth_debug);
                    695:        }
                    696: }
                    697:
                    698: int
                    699: mm_answer_keyallowed(int socket, Buffer *m)
                    700: {
                    701:        Key *key;
                    702:        u_char *cuser, *chost, *blob;
                    703:        u_int bloblen;
                    704:        enum mm_keytype type = 0;
                    705:        int allowed = 0;
                    706:
                    707:        debug3("%s entering", __FUNCTION__);
1.3       markus    708:
1.1       provos    709:        type = buffer_get_int(m);
                    710:        cuser = buffer_get_string(m, NULL);
                    711:        chost = buffer_get_string(m, NULL);
                    712:        blob = buffer_get_string(m, &bloblen);
                    713:
                    714:        key = key_from_blob(blob, bloblen);
                    715:
                    716:        if ((compat20 && type == MM_RSAHOSTKEY) ||
                    717:            (!compat20 && type != MM_RSAHOSTKEY))
                    718:                fatal("%s: key type and protocol mismatch", __FUNCTION__);
                    719:
                    720:        debug3("%s: key_from_blob: %p", __FUNCTION__, key);
                    721:
                    722:        if (key != NULL && authctxt->pw != NULL) {
                    723:                switch(type) {
                    724:                case MM_USERKEY:
                    725:                        allowed = user_key_allowed(authctxt->pw, key);
                    726:                        break;
                    727:                case MM_HOSTKEY:
                    728:                        allowed = hostbased_key_allowed(authctxt->pw,
                    729:                            cuser, chost, key);
                    730:                        break;
                    731:                case MM_RSAHOSTKEY:
                    732:                        key->type = KEY_RSA1; /* XXX */
                    733:                        allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
                    734:                            cuser, chost, key);
                    735:                        break;
                    736:                default:
                    737:                        fatal("%s: unknown key type %d", __FUNCTION__, type);
                    738:                        break;
                    739:                }
                    740:                key_free(key);
                    741:        }
                    742:
                    743:        /* clear temporarily storage (used by verify) */
                    744:        monitor_reset_key_state();
                    745:
                    746:        if (allowed) {
                    747:                /* Save temporarily for comparison in verify */
                    748:                key_blob = blob;
                    749:                key_bloblen = bloblen;
                    750:                key_blobtype = type;
                    751:                hostbased_cuser = cuser;
                    752:                hostbased_chost = chost;
                    753:        }
                    754:
                    755:        debug3("%s: key %p is %s",
                    756:            __FUNCTION__, key, allowed ? "allowed" : "disallowed");
                    757:
                    758:        buffer_clear(m);
                    759:        buffer_put_int(m, allowed);
                    760:
                    761:        mm_append_debug(m);
                    762:
                    763:        mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
                    764:
                    765:        if (type == MM_RSAHOSTKEY)
                    766:                monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                    767:
                    768:        return (0);
                    769: }
                    770:
                    771: static int
                    772: monitor_valid_userblob(u_char *data, u_int datalen)
                    773: {
                    774:        Buffer b;
                    775:        u_char *p;
                    776:        u_int len;
                    777:        int fail = 0;
                    778:        int session_id2_len = 20 /*XXX should get from [net] */;
                    779:
                    780:        buffer_init(&b);
                    781:        buffer_append(&b, data, datalen);
1.3       markus    782:
1.1       provos    783:        if (datafellows & SSH_OLD_SESSIONID) {
                    784:                buffer_consume(&b, session_id2_len);
                    785:        } else {
                    786:                xfree(buffer_get_string(&b, &len));
                    787:                if (len != session_id2_len)
                    788:                        fail++;
                    789:        }
                    790:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    791:                fail++;
                    792:        p = buffer_get_string(&b, NULL);
                    793:        if (strcmp(authctxt->user, p) != 0) {
                    794:                log("wrong user name passed to monitor: expected %s != %.100s",
                    795:                    authctxt->user, p);
                    796:                fail++;
                    797:        }
                    798:        xfree(p);
                    799:        buffer_skip_string(&b);
                    800:        if (datafellows & SSH_BUG_PKAUTH) {
                    801:                if (!buffer_get_char(&b))
                    802:                        fail++;
                    803:        } else {
                    804:                p = buffer_get_string(&b, NULL);
                    805:                if (strcmp("publickey", p) != 0)
                    806:                        fail++;
                    807:                xfree(p);
                    808:                if (!buffer_get_char(&b))
                    809:                        fail++;
                    810:                buffer_skip_string(&b);
                    811:        }
                    812:        buffer_skip_string(&b);
                    813:        if (buffer_len(&b) != 0)
                    814:                fail++;
                    815:        buffer_free(&b);
                    816:        return (fail == 0);
                    817: }
                    818:
                    819: static int
                    820: monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
                    821:     u_char *chost)
                    822: {
                    823:        Buffer b;
                    824:        u_char *p;
                    825:        u_int len;
                    826:        int fail = 0;
                    827:        int session_id2_len = 20 /*XXX should get from [net] */;
                    828:
                    829:        buffer_init(&b);
                    830:        buffer_append(&b, data, datalen);
1.3       markus    831:
1.1       provos    832:        xfree(buffer_get_string(&b, &len));
                    833:        if (len != session_id2_len)
                    834:                fail++;
                    835:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    836:                fail++;
                    837:        p = buffer_get_string(&b, NULL);
                    838:        if (strcmp(authctxt->user, p) != 0) {
                    839:                log("wrong user name passed to monitor: expected %s != %.100s",
                    840:                    authctxt->user, p);
                    841:                fail++;
                    842:        }
                    843:        xfree(p);
                    844:        buffer_skip_string(&b); /* service */
                    845:        p = buffer_get_string(&b, NULL);
                    846:        if (strcmp(p, "hostbased") != 0)
                    847:                fail++;
                    848:        xfree(p);
                    849:        buffer_skip_string(&b); /* pkalg */
                    850:        buffer_skip_string(&b); /* pkblob */
                    851:
                    852:        /* verify client host, strip trailing dot if necessary */
                    853:        p = buffer_get_string(&b, NULL);
                    854:        if (((len = strlen(p)) > 0) && p[len - 1] == '.')
                    855:                p[len - 1] = '\0';
                    856:        if (strcmp(p, chost) != 0)
                    857:                fail++;
                    858:        xfree(p);
                    859:
                    860:        /* verify client user */
                    861:        p = buffer_get_string(&b, NULL);
                    862:        if (strcmp(p, cuser) != 0)
                    863:                fail++;
                    864:        xfree(p);
                    865:
                    866:        if (buffer_len(&b) != 0)
                    867:                fail++;
                    868:        buffer_free(&b);
                    869:        return (fail == 0);
                    870: }
                    871:
                    872: int
                    873: mm_answer_keyverify(int socket, Buffer *m)
                    874: {
                    875:        Key *key;
                    876:        u_char *signature, *data, *blob;
                    877:        u_int signaturelen, datalen, bloblen;
                    878:        int verified = 0;
                    879:        int valid_data = 0;
                    880:
                    881:        blob = buffer_get_string(m, &bloblen);
                    882:        signature = buffer_get_string(m, &signaturelen);
                    883:        data = buffer_get_string(m, &datalen);
                    884:
                    885:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring   886:          !monitor_allowed_key(blob, bloblen))
1.1       provos    887:                fatal("%s: bad key, not previously allowed", __FUNCTION__);
                    888:
                    889:        key = key_from_blob(blob, bloblen);
                    890:        if (key == NULL)
                    891:                fatal("%s: bad public key blob", __FUNCTION__);
                    892:
                    893:        switch (key_blobtype) {
                    894:        case MM_USERKEY:
                    895:                valid_data = monitor_valid_userblob(data, datalen);
                    896:                break;
                    897:        case MM_HOSTKEY:
                    898:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                    899:                    hostbased_cuser, hostbased_chost);
                    900:                break;
                    901:        default:
                    902:                valid_data = 0;
                    903:                break;
                    904:        }
                    905:        if (!valid_data)
                    906:                fatal("%s: bad signature data blob", __FUNCTION__);
                    907:
                    908:        verified = key_verify(key, signature, signaturelen, data, datalen);
                    909:        debug3("%s: key %p signature %s",
                    910:            __FUNCTION__, key, verified ? "verified" : "unverified");
                    911:
                    912:        key_free(key);
                    913:        xfree(blob);
                    914:        xfree(signature);
                    915:        xfree(data);
                    916:
                    917:        monitor_reset_key_state();
1.3       markus    918:
1.1       provos    919:        buffer_clear(m);
                    920:        buffer_put_int(m, verified);
                    921:        mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
                    922:
                    923:        auth_method = "publickey";
                    924:
                    925:        return (verified);
                    926: }
                    927:
1.2       markus    928: static void
1.1       provos    929: mm_record_login(Session *s, struct passwd *pw)
                    930: {
                    931:        socklen_t fromlen;
                    932:        struct sockaddr_storage from;
                    933:
                    934:        /*
                    935:         * Get IP address of client. If the connection is not a socket, let
                    936:         * the address be 0.0.0.0.
                    937:         */
                    938:        memset(&from, 0, sizeof(from));
                    939:        if (packet_connection_is_on_socket()) {
                    940:                fromlen = sizeof(from);
                    941:                if (getpeername(packet_get_connection_in(),
                    942:                        (struct sockaddr *) & from, &fromlen) < 0) {
                    943:                        debug("getpeername: %.100s", strerror(errno));
                    944:                        fatal_cleanup();
                    945:                }
                    946:        }
                    947:        /* Record that there was a login on that tty from the remote host. */
                    948:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
                    949:            get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
                    950:            (struct sockaddr *)&from);
                    951: }
                    952:
                    953: static void
                    954: mm_session_close(Session *s)
                    955: {
                    956:        debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
                    957:        if (s->ttyfd != -1) {
                    958:                debug3("%s: tty %s ptyfd %d",  __FUNCTION__, s->tty, s->ptyfd);
                    959:                fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
                    960:                session_pty_cleanup2(s);
                    961:        }
                    962:        s->used = 0;
                    963: }
                    964:
                    965: int
                    966: mm_answer_pty(int socket, Buffer *m)
                    967: {
1.9.2.1 ! jason     968:        extern struct monitor *pmonitor;
1.1       provos    969:        Session *s;
                    970:        int res, fd0;
                    971:
                    972:        debug3("%s entering", __FUNCTION__);
                    973:
                    974:        buffer_clear(m);
                    975:        s = session_new();
                    976:        if (s == NULL)
                    977:                goto error;
                    978:        s->authctxt = authctxt;
                    979:        s->pw = authctxt->pw;
1.9.2.1 ! jason     980:        s->pid = pmonitor->m_pid;
1.1       provos    981:        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
                    982:        if (res == 0)
                    983:                goto error;
                    984:        fatal_add_cleanup(session_pty_cleanup2, (void *)s);
                    985:        pty_setowner(authctxt->pw, s->tty);
                    986:
                    987:        buffer_put_int(m, 1);
                    988:        buffer_put_cstring(m, s->tty);
                    989:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                    990:
                    991:        mm_send_fd(socket, s->ptyfd);
                    992:        mm_send_fd(socket, s->ttyfd);
                    993:
                    994:        /* We need to trick ttyslot */
                    995:        if (dup2(s->ttyfd, 0) == -1)
                    996:                fatal("%s: dup2", __FUNCTION__);
                    997:
                    998:        mm_record_login(s, authctxt->pw);
                    999:
                   1000:        /* Now we can close the file descriptor again */
                   1001:        close(0);
                   1002:
                   1003:        /* make sure nothing uses fd 0 */
                   1004:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
                   1005:                fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
                   1006:        if (fd0 != 0)
                   1007:                error("%s: fd0 %d != 0", __FUNCTION__, fd0);
                   1008:
                   1009:        /* slave is not needed */
                   1010:        close(s->ttyfd);
                   1011:        s->ttyfd = s->ptyfd;
                   1012:        /* no need to dup() because nobody closes ptyfd */
                   1013:        s->ptymaster = s->ptyfd;
                   1014:
                   1015:        debug3("%s: tty %s ptyfd %d",  __FUNCTION__, s->tty, s->ttyfd);
                   1016:
                   1017:        return (0);
                   1018:
                   1019:  error:
                   1020:        if (s != NULL)
                   1021:                mm_session_close(s);
                   1022:        buffer_put_int(m, 0);
                   1023:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                   1024:        return (0);
                   1025: }
                   1026:
                   1027: int
                   1028: mm_answer_pty_cleanup(int socket, Buffer *m)
                   1029: {
                   1030:        Session *s;
                   1031:        char *tty;
                   1032:
                   1033:        debug3("%s entering", __FUNCTION__);
                   1034:
                   1035:        tty = buffer_get_string(m, NULL);
                   1036:        if ((s = session_by_tty(tty)) != NULL)
                   1037:                mm_session_close(s);
                   1038:        buffer_clear(m);
                   1039:        xfree(tty);
                   1040:        return (0);
                   1041: }
                   1042:
                   1043: int
                   1044: mm_answer_sesskey(int socket, Buffer *m)
                   1045: {
                   1046:        BIGNUM *p;
                   1047:        int rsafail;
                   1048:
                   1049:        /* Turn off permissions */
                   1050:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                   1051:
                   1052:        if ((p = BN_new()) == NULL)
                   1053:                fatal("%s: BN_new", __FUNCTION__);
                   1054:
                   1055:        buffer_get_bignum2(m, p);
                   1056:
                   1057:        rsafail = ssh1_session_key(p);
                   1058:
                   1059:        buffer_clear(m);
                   1060:        buffer_put_int(m, rsafail);
                   1061:        buffer_put_bignum2(m, p);
                   1062:
                   1063:        BN_clear_free(p);
                   1064:
                   1065:        mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
                   1066:
                   1067:        /* Turn on permissions for sessid passing */
                   1068:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
                   1069:
                   1070:        return (0);
                   1071: }
                   1072:
                   1073: int
                   1074: mm_answer_sessid(int socket, Buffer *m)
                   1075: {
                   1076:        int i;
                   1077:
                   1078:        debug3("%s entering", __FUNCTION__);
                   1079:
                   1080:        if (buffer_len(m) != 16)
                   1081:                fatal("%s: bad ssh1 session id", __FUNCTION__);
                   1082:        for (i = 0; i < 16; i++)
                   1083:                session_id[i] = buffer_get_char(m);
                   1084:
                   1085:        /* Turn on permissions for getpwnam */
                   1086:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                   1087:
                   1088:        return (0);
                   1089: }
                   1090:
                   1091: int
                   1092: mm_answer_rsa_keyallowed(int socket, Buffer *m)
                   1093: {
                   1094:        BIGNUM *client_n;
                   1095:        Key *key = NULL;
1.3       markus   1096:        u_char *blob = NULL;
                   1097:        u_int blen = 0;
1.1       provos   1098:        int allowed = 0;
                   1099:
                   1100:        debug3("%s entering", __FUNCTION__);
                   1101:
                   1102:        if (authctxt->valid) {
                   1103:                if ((client_n = BN_new()) == NULL)
                   1104:                        fatal("%s: BN_new", __FUNCTION__);
                   1105:                buffer_get_bignum2(m, client_n);
                   1106:                allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
                   1107:                BN_clear_free(client_n);
                   1108:        }
                   1109:        buffer_clear(m);
                   1110:        buffer_put_int(m, allowed);
                   1111:
                   1112:        /* clear temporarily storage (used by generate challenge) */
                   1113:        monitor_reset_key_state();
                   1114:
                   1115:        if (allowed && key != NULL) {
                   1116:                key->type = KEY_RSA;    /* cheat for key_to_blob */
                   1117:                if (key_to_blob(key, &blob, &blen) == 0)
                   1118:                        fatal("%s: key_to_blob failed", __FUNCTION__);
                   1119:                buffer_put_string(m, blob, blen);
                   1120:
                   1121:                /* Save temporarily for comparison in verify */
                   1122:                key_blob = blob;
                   1123:                key_bloblen = blen;
                   1124:                key_blobtype = MM_RSAUSERKEY;
                   1125:                key_free(key);
                   1126:        }
                   1127:
                   1128:        mm_append_debug(m);
                   1129:
                   1130:        mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
                   1131:
                   1132:        monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                   1133:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
                   1134:        return (0);
                   1135: }
                   1136:
                   1137: int
                   1138: mm_answer_rsa_challenge(int socket, Buffer *m)
                   1139: {
                   1140:        Key *key = NULL;
1.3       markus   1141:        u_char *blob;
                   1142:        u_int blen;
1.1       provos   1143:
                   1144:        debug3("%s entering", __FUNCTION__);
                   1145:
                   1146:        if (!authctxt->valid)
                   1147:                fatal("%s: authctxt not valid", __FUNCTION__);
                   1148:        blob = buffer_get_string(m, &blen);
                   1149:        if (!monitor_allowed_key(blob, blen))
                   1150:                fatal("%s: bad key, not previously allowed", __FUNCTION__);
                   1151:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
                   1152:                fatal("%s: key type mismatch", __FUNCTION__);
                   1153:        if ((key = key_from_blob(blob, blen)) == NULL)
                   1154:                fatal("%s: received bad key", __FUNCTION__);
                   1155:
                   1156:        if (ssh1_challenge)
                   1157:                BN_clear_free(ssh1_challenge);
                   1158:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1159:
                   1160:        buffer_clear(m);
                   1161:        buffer_put_bignum2(m, ssh1_challenge);
                   1162:
                   1163:        debug3("%s sending reply", __FUNCTION__);
                   1164:        mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
                   1165:
                   1166:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
                   1167:        return (0);
                   1168: }
                   1169:
                   1170: int
                   1171: mm_answer_rsa_response(int socket, Buffer *m)
                   1172: {
                   1173:        Key *key = NULL;
1.3       markus   1174:        u_char *blob, *response;
                   1175:        u_int blen, len;
                   1176:        int success;
1.1       provos   1177:
                   1178:        debug3("%s entering", __FUNCTION__);
                   1179:
                   1180:        if (!authctxt->valid)
                   1181:                fatal("%s: authctxt not valid", __FUNCTION__);
                   1182:        if (ssh1_challenge == NULL)
                   1183:                fatal("%s: no ssh1_challenge", __FUNCTION__);
                   1184:
                   1185:        blob = buffer_get_string(m, &blen);
                   1186:        if (!monitor_allowed_key(blob, blen))
                   1187:                fatal("%s: bad key, not previously allowed", __FUNCTION__);
                   1188:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
                   1189:                fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
                   1190:        if ((key = key_from_blob(blob, blen)) == NULL)
                   1191:                fatal("%s: received bad key", __FUNCTION__);
                   1192:        response = buffer_get_string(m, &len);
                   1193:        if (len != 16)
                   1194:                fatal("%s: received bad response to challenge", __FUNCTION__);
                   1195:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1196:
                   1197:        key_free(key);
                   1198:        xfree(response);
                   1199:
                   1200:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1201:
                   1202:        /* reset state */
                   1203:        BN_clear_free(ssh1_challenge);
                   1204:        ssh1_challenge = NULL;
                   1205:        monitor_reset_key_state();
                   1206:
                   1207:        buffer_clear(m);
                   1208:        buffer_put_int(m, success);
                   1209:        mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
                   1210:
                   1211:        return (success);
                   1212: }
                   1213:
                   1214: int
                   1215: mm_answer_term(int socket, Buffer *req)
                   1216: {
1.9.2.1 ! jason    1217:        extern struct monitor *pmonitor;
1.1       provos   1218:        int res, status;
                   1219:
                   1220:        debug3("%s: tearing down sessions", __FUNCTION__);
                   1221:
                   1222:        /* The child is terminating */
                   1223:        session_destroy_all(&mm_session_close);
                   1224:
1.9.2.1 ! jason    1225:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1226:                if (errno != EINTR)
                   1227:                        exit(1);
1.1       provos   1228:
                   1229:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1230:
                   1231:        /* Terminate process */
                   1232:        exit (res);
                   1233: }
                   1234:
                   1235: void
1.9.2.1 ! jason    1236: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1237: {
                   1238:        if (compat20) {
                   1239:                set_newkeys(MODE_IN);
                   1240:                set_newkeys(MODE_OUT);
                   1241:        } else {
1.5       markus   1242:                u_char key[SSH_SESSION_KEY_LENGTH];
                   1243:
                   1244:                memset(key, 'a', sizeof(key));
1.1       provos   1245:                packet_set_protocol_flags(child_state.ssh1protoflags);
                   1246:                packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
                   1247:                    child_state.ssh1cipher);
                   1248:        }
                   1249:
                   1250:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1251:        xfree(child_state.keyout);
                   1252:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1253:        xfree(child_state.keyin);
                   1254:
                   1255:        if (!compat20) {
                   1256:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1257:                xfree(child_state.ivout);
                   1258:                packet_set_iv(MODE_IN, child_state.ivin);
                   1259:                xfree(child_state.ivin);
                   1260:        }
                   1261:
                   1262:        memcpy(&incoming_stream, &child_state.incoming,
                   1263:            sizeof(incoming_stream));
                   1264:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1265:            sizeof(outgoing_stream));
1.3       markus   1266:
1.1       provos   1267:        /* Update with new address */
1.9.2.1 ! jason    1268:        mm_init_compression(pmonitor->m_zlib);
1.1       provos   1269:
                   1270:        /* Network I/O buffers */
                   1271:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1272:        buffer_clear(&input);
                   1273:        buffer_append(&input, child_state.input, child_state.ilen);
                   1274:        memset(child_state.input, 0, child_state.ilen);
                   1275:        xfree(child_state.input);
                   1276:
                   1277:        buffer_clear(&output);
                   1278:        buffer_append(&output, child_state.output, child_state.olen);
                   1279:        memset(child_state.output, 0, child_state.olen);
                   1280:        xfree(child_state.output);
                   1281: }
                   1282:
1.2       markus   1283: static Kex *
1.1       provos   1284: mm_get_kex(Buffer *m)
                   1285: {
                   1286:        Kex *kex;
                   1287:        void *blob;
                   1288:        u_int bloblen;
                   1289:
                   1290:        kex = xmalloc(sizeof(*kex));
                   1291:        memset(kex, 0, sizeof(*kex));
                   1292:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
                   1293:        kex->we_need = buffer_get_int(m);
                   1294:        kex->server = 1;
                   1295:        kex->hostkey_type = buffer_get_int(m);
                   1296:        kex->kex_type = buffer_get_int(m);
                   1297:        blob = buffer_get_string(m, &bloblen);
                   1298:        buffer_init(&kex->my);
                   1299:        buffer_append(&kex->my, blob, bloblen);
                   1300:        xfree(blob);
                   1301:        blob = buffer_get_string(m, &bloblen);
                   1302:        buffer_init(&kex->peer);
                   1303:        buffer_append(&kex->peer, blob, bloblen);
                   1304:        xfree(blob);
                   1305:        kex->done = 1;
                   1306:        kex->flags = buffer_get_int(m);
                   1307:        kex->client_version_string = buffer_get_string(m, NULL);
                   1308:        kex->server_version_string = buffer_get_string(m, NULL);
                   1309:        kex->load_host_key=&get_hostkey_by_type;
                   1310:        kex->host_key_index=&get_hostkey_index;
                   1311:
                   1312:        return (kex);
                   1313: }
                   1314:
                   1315: /* This function requries careful sanity checking */
                   1316:
                   1317: void
1.9.2.1 ! jason    1318: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1319: {
                   1320:        Buffer m;
                   1321:        u_char *blob, *p;
                   1322:        u_int bloblen, plen;
                   1323:
                   1324:        debug3("%s: Waiting for new keys", __FUNCTION__);
                   1325:
                   1326:        buffer_init(&m);
1.9.2.1 ! jason    1327:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1328:        if (!compat20) {
                   1329:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1330:                child_state.ssh1cipher = buffer_get_int(&m);
                   1331:                child_state.ivout = buffer_get_string(&m,
                   1332:                    &child_state.ivoutlen);
                   1333:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1334:                goto skip;
                   1335:        } else {
                   1336:                /* Get the Kex for rekeying */
1.9.2.1 ! jason    1337:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1338:        }
                   1339:
                   1340:        blob = buffer_get_string(&m, &bloblen);
                   1341:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1342:        xfree(blob);
                   1343:
                   1344:        debug3("%s: Waiting for second key", __FUNCTION__);
                   1345:        blob = buffer_get_string(&m, &bloblen);
                   1346:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1347:        xfree(blob);
1.3       markus   1348:
1.1       provos   1349:        /* Now get sequence numbers for the packets */
                   1350:        packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
                   1351:        packet_set_seqnr(MODE_IN, buffer_get_int(&m));
                   1352:
                   1353:  skip:
                   1354:        /* Get the key context */
                   1355:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1356:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1357:
                   1358:        debug3("%s: Getting compression state", __FUNCTION__);
                   1359:        /* Get compression state */
                   1360:        p = buffer_get_string(&m, &plen);
                   1361:        if (plen != sizeof(child_state.outgoing))
                   1362:                fatal("%s: bad request size", __FUNCTION__);
                   1363:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1364:        xfree(p);
                   1365:
                   1366:        p = buffer_get_string(&m, &plen);
                   1367:        if (plen != sizeof(child_state.incoming))
                   1368:                fatal("%s: bad request size", __FUNCTION__);
                   1369:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1370:        xfree(p);
                   1371:
                   1372:        /* Network I/O buffers */
                   1373:        debug3("%s: Getting Network I/O buffers", __FUNCTION__);
                   1374:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1375:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   1376:
                   1377:        buffer_free(&m);
                   1378: }
                   1379:
                   1380:
                   1381: /* Allocation functions for zlib */
                   1382: void *
                   1383: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
                   1384: {
                   1385:        void *address;
                   1386:
                   1387:        address = mm_malloc(mm, size * ncount);
                   1388:
                   1389:        return (address);
                   1390: }
                   1391:
                   1392: void
                   1393: mm_zfree(struct mm_master *mm, void *address)
                   1394: {
                   1395:        mm_free(mm, address);
                   1396: }
                   1397:
                   1398: void
                   1399: mm_init_compression(struct mm_master *mm)
                   1400: {
                   1401:        outgoing_stream.zalloc = (alloc_func)mm_zalloc;
                   1402:        outgoing_stream.zfree = (free_func)mm_zfree;
                   1403:        outgoing_stream.opaque = mm;
                   1404:
                   1405:        incoming_stream.zalloc = (alloc_func)mm_zalloc;
                   1406:        incoming_stream.zfree = (free_func)mm_zfree;
                   1407:        incoming_stream.opaque = mm;
                   1408: }
                   1409:
                   1410: /* XXX */
                   1411:
                   1412: #define FD_CLOSEONEXEC(x) do { \
                   1413:        if (fcntl(x, F_SETFD, 1) == -1) \
                   1414:                fatal("fcntl(%d, F_SETFD)", x); \
                   1415: } while (0)
                   1416:
1.2       markus   1417: static void
1.1       provos   1418: monitor_socketpair(int *pair)
1.3       markus   1419: {
1.1       provos   1420:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
                   1421:                fatal("%s: socketpair", __FUNCTION__);
                   1422:        FD_CLOSEONEXEC(pair[0]);
                   1423:        FD_CLOSEONEXEC(pair[1]);
                   1424: }
                   1425:
                   1426: #define MM_MEMSIZE     65536
                   1427:
                   1428: struct monitor *
                   1429: monitor_init(void)
                   1430: {
                   1431:        struct monitor *mon;
                   1432:        int pair[2];
                   1433:
                   1434:        mon = xmalloc(sizeof(*mon));
                   1435:
                   1436:        monitor_socketpair(pair);
                   1437:
                   1438:        mon->m_recvfd = pair[0];
                   1439:        mon->m_sendfd = pair[1];
                   1440:
                   1441:        /* Used to share zlib space across processes */
                   1442:        mon->m_zback = mm_create(NULL, MM_MEMSIZE);
                   1443:        mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
                   1444:
                   1445:        /* Compression needs to share state across borders */
                   1446:        mm_init_compression(mon->m_zlib);
                   1447:
                   1448:        return mon;
                   1449: }
                   1450:
                   1451: void
                   1452: monitor_reinit(struct monitor *mon)
                   1453: {
                   1454:        int pair[2];
                   1455:
                   1456:        monitor_socketpair(pair);
                   1457:
                   1458:        mon->m_recvfd = pair[0];
                   1459:        mon->m_sendfd = pair[1];
                   1460: }