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

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