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

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.23    ! millert    28: RCSID("$OpenBSD: monitor.c,v 1.22 2002/07/22 17:32:56 stevesk 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)
1.21      deraadt   543:                xfree(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;
1.22      stevesk   571:        int authenticated;
                    572:        u_int plen;
1.1       provos    573:
                    574:        passwd = buffer_get_string(m, &plen);
                    575:        /* Only authenticate if the context is valid */
1.12      markus    576:        authenticated = options.password_authentication &&
                    577:            authctxt->valid && auth_password(authctxt, passwd);
1.1       provos    578:        memset(passwd, 0, strlen(passwd));
                    579:        xfree(passwd);
                    580:
                    581:        buffer_clear(m);
                    582:        buffer_put_int(m, authenticated);
                    583:
1.14      markus    584:        debug3("%s: sending result %d", __func__, authenticated);
1.1       provos    585:        mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
                    586:
                    587:        call_count++;
                    588:        if (plen == 0 && call_count == 1)
                    589:                auth_method = "none";
                    590:        else
                    591:                auth_method = "password";
                    592:
                    593:        /* Causes monitor loop to terminate if authenticated */
                    594:        return (authenticated);
                    595: }
                    596:
                    597: #ifdef BSD_AUTH
                    598: int
                    599: mm_answer_bsdauthquery(int socket, Buffer *m)
                    600: {
                    601:        char *name, *infotxt;
                    602:        u_int numprompts;
                    603:        u_int *echo_on;
                    604:        char **prompts;
                    605:        int res;
                    606:
                    607:        res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    608:            &prompts, &echo_on);
                    609:
                    610:        buffer_clear(m);
                    611:        buffer_put_int(m, res);
                    612:        if (res != -1)
                    613:                buffer_put_cstring(m, prompts[0]);
                    614:
1.14      markus    615:        debug3("%s: sending challenge res: %d", __func__, res);
1.1       provos    616:        mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
                    617:
                    618:        if (res != -1) {
                    619:                xfree(name);
                    620:                xfree(infotxt);
                    621:                xfree(prompts);
                    622:                xfree(echo_on);
                    623:        }
                    624:
                    625:        return (0);
                    626: }
                    627:
                    628: int
                    629: mm_answer_bsdauthrespond(int socket, Buffer *m)
                    630: {
                    631:        char *response;
                    632:        int authok;
                    633:
                    634:        if (authctxt->as == 0)
1.14      markus    635:                fatal("%s: no bsd auth session", __func__);
1.1       provos    636:
                    637:        response = buffer_get_string(m, NULL);
1.12      markus    638:        authok = options.challenge_response_authentication &&
                    639:            auth_userresponse(authctxt->as, response, 0);
1.1       provos    640:        authctxt->as = NULL;
1.14      markus    641:        debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1       provos    642:        xfree(response);
                    643:
                    644:        buffer_clear(m);
                    645:        buffer_put_int(m, authok);
                    646:
1.14      markus    647:        debug3("%s: sending authenticated: %d", __func__, authok);
1.1       provos    648:        mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
                    649:
                    650:        auth_method = "bsdauth";
                    651:
                    652:        return (authok != 0);
                    653: }
                    654: #endif
                    655:
                    656: #ifdef SKEY
                    657: int
                    658: mm_answer_skeyquery(int socket, Buffer *m)
                    659: {
                    660:        struct skey skey;
                    661:        char challenge[1024];
                    662:        int res;
                    663:
                    664:        res = skeychallenge(&skey, authctxt->user, challenge);
                    665:
                    666:        buffer_clear(m);
                    667:        buffer_put_int(m, res);
                    668:        if (res != -1)
                    669:                buffer_put_cstring(m, challenge);
                    670:
1.14      markus    671:        debug3("%s: sending challenge res: %d", __func__, res);
1.1       provos    672:        mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
                    673:
                    674:        return (0);
                    675: }
                    676:
                    677: int
                    678: mm_answer_skeyrespond(int socket, Buffer *m)
                    679: {
                    680:        char *response;
                    681:        int authok;
                    682:
                    683:        response = buffer_get_string(m, NULL);
                    684:
1.12      markus    685:        authok = (options.challenge_response_authentication &&
                    686:            authctxt->valid &&
1.1       provos    687:            skey_haskey(authctxt->pw->pw_name) == 0 &&
                    688:            skey_passcheck(authctxt->pw->pw_name, response) != -1);
                    689:
                    690:        xfree(response);
                    691:
                    692:        buffer_clear(m);
                    693:        buffer_put_int(m, authok);
                    694:
1.14      markus    695:        debug3("%s: sending authenticated: %d", __func__, authok);
1.1       provos    696:        mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
                    697:
                    698:        auth_method = "skey";
                    699:
                    700:        return (authok != 0);
                    701: }
                    702: #endif
                    703:
1.2       markus    704: static void
1.1       provos    705: mm_append_debug(Buffer *m)
                    706: {
                    707:        if (auth_debug_init && buffer_len(&auth_debug)) {
1.14      markus    708:                debug3("%s: Appending debug messages for child", __func__);
1.1       provos    709:                buffer_append(m, buffer_ptr(&auth_debug),
                    710:                    buffer_len(&auth_debug));
                    711:                buffer_clear(&auth_debug);
                    712:        }
                    713: }
                    714:
                    715: int
                    716: mm_answer_keyallowed(int socket, Buffer *m)
                    717: {
                    718:        Key *key;
                    719:        u_char *cuser, *chost, *blob;
                    720:        u_int bloblen;
                    721:        enum mm_keytype type = 0;
                    722:        int allowed = 0;
                    723:
1.14      markus    724:        debug3("%s entering", __func__);
1.3       markus    725:
1.1       provos    726:        type = buffer_get_int(m);
                    727:        cuser = buffer_get_string(m, NULL);
                    728:        chost = buffer_get_string(m, NULL);
                    729:        blob = buffer_get_string(m, &bloblen);
                    730:
                    731:        key = key_from_blob(blob, bloblen);
                    732:
                    733:        if ((compat20 && type == MM_RSAHOSTKEY) ||
                    734:            (!compat20 && type != MM_RSAHOSTKEY))
1.14      markus    735:                fatal("%s: key type and protocol mismatch", __func__);
1.1       provos    736:
1.14      markus    737:        debug3("%s: key_from_blob: %p", __func__, key);
1.1       provos    738:
                    739:        if (key != NULL && authctxt->pw != NULL) {
                    740:                switch(type) {
                    741:                case MM_USERKEY:
1.12      markus    742:                        allowed = options.pubkey_authentication &&
                    743:                            user_key_allowed(authctxt->pw, key);
1.1       provos    744:                        break;
                    745:                case MM_HOSTKEY:
1.12      markus    746:                        allowed = options.hostbased_authentication &&
                    747:                            hostbased_key_allowed(authctxt->pw,
1.1       provos    748:                            cuser, chost, key);
                    749:                        break;
                    750:                case MM_RSAHOSTKEY:
                    751:                        key->type = KEY_RSA1; /* XXX */
1.12      markus    752:                        allowed = options.rhosts_rsa_authentication &&
                    753:                            auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1       provos    754:                            cuser, chost, key);
                    755:                        break;
                    756:                default:
1.14      markus    757:                        fatal("%s: unknown key type %d", __func__, type);
1.1       provos    758:                        break;
                    759:                }
                    760:                key_free(key);
                    761:        }
                    762:
                    763:        /* clear temporarily storage (used by verify) */
                    764:        monitor_reset_key_state();
                    765:
                    766:        if (allowed) {
                    767:                /* Save temporarily for comparison in verify */
                    768:                key_blob = blob;
                    769:                key_bloblen = bloblen;
                    770:                key_blobtype = type;
                    771:                hostbased_cuser = cuser;
                    772:                hostbased_chost = chost;
                    773:        }
                    774:
                    775:        debug3("%s: key %p is %s",
1.14      markus    776:            __func__, key, allowed ? "allowed" : "disallowed");
1.1       provos    777:
                    778:        buffer_clear(m);
                    779:        buffer_put_int(m, allowed);
                    780:
                    781:        mm_append_debug(m);
                    782:
                    783:        mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
                    784:
                    785:        if (type == MM_RSAHOSTKEY)
                    786:                monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                    787:
                    788:        return (0);
                    789: }
                    790:
                    791: static int
                    792: monitor_valid_userblob(u_char *data, u_int datalen)
                    793: {
                    794:        Buffer b;
                    795:        u_char *p;
                    796:        u_int len;
                    797:        int fail = 0;
                    798:
                    799:        buffer_init(&b);
                    800:        buffer_append(&b, data, datalen);
1.3       markus    801:
1.1       provos    802:        if (datafellows & SSH_OLD_SESSIONID) {
1.13      markus    803:                p = buffer_ptr(&b);
                    804:                len = buffer_len(&b);
                    805:                if ((session_id2 == NULL) ||
                    806:                    (len < session_id2_len) ||
                    807:                    (memcmp(p, session_id2, session_id2_len) != 0))
                    808:                        fail++;
1.1       provos    809:                buffer_consume(&b, session_id2_len);
                    810:        } else {
1.13      markus    811:                p = buffer_get_string(&b, &len);
                    812:                if ((session_id2 == NULL) ||
                    813:                    (len != session_id2_len) ||
                    814:                    (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    815:                        fail++;
1.13      markus    816:                xfree(p);
1.1       provos    817:        }
                    818:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    819:                fail++;
                    820:        p = buffer_get_string(&b, NULL);
                    821:        if (strcmp(authctxt->user, p) != 0) {
                    822:                log("wrong user name passed to monitor: expected %s != %.100s",
                    823:                    authctxt->user, p);
                    824:                fail++;
                    825:        }
                    826:        xfree(p);
                    827:        buffer_skip_string(&b);
                    828:        if (datafellows & SSH_BUG_PKAUTH) {
                    829:                if (!buffer_get_char(&b))
                    830:                        fail++;
                    831:        } else {
                    832:                p = buffer_get_string(&b, NULL);
                    833:                if (strcmp("publickey", p) != 0)
                    834:                        fail++;
                    835:                xfree(p);
                    836:                if (!buffer_get_char(&b))
                    837:                        fail++;
                    838:                buffer_skip_string(&b);
                    839:        }
                    840:        buffer_skip_string(&b);
                    841:        if (buffer_len(&b) != 0)
                    842:                fail++;
                    843:        buffer_free(&b);
                    844:        return (fail == 0);
                    845: }
                    846:
                    847: static int
                    848: monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
                    849:     u_char *chost)
                    850: {
                    851:        Buffer b;
                    852:        u_char *p;
                    853:        u_int len;
                    854:        int fail = 0;
                    855:
                    856:        buffer_init(&b);
                    857:        buffer_append(&b, data, datalen);
1.3       markus    858:
1.13      markus    859:        p = buffer_get_string(&b, &len);
                    860:        if ((session_id2 == NULL) ||
                    861:            (len != session_id2_len) ||
                    862:            (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    863:                fail++;
1.13      markus    864:        xfree(p);
                    865:
1.1       provos    866:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    867:                fail++;
                    868:        p = buffer_get_string(&b, NULL);
                    869:        if (strcmp(authctxt->user, p) != 0) {
                    870:                log("wrong user name passed to monitor: expected %s != %.100s",
                    871:                    authctxt->user, p);
                    872:                fail++;
                    873:        }
                    874:        xfree(p);
                    875:        buffer_skip_string(&b); /* service */
                    876:        p = buffer_get_string(&b, NULL);
                    877:        if (strcmp(p, "hostbased") != 0)
                    878:                fail++;
                    879:        xfree(p);
                    880:        buffer_skip_string(&b); /* pkalg */
                    881:        buffer_skip_string(&b); /* pkblob */
                    882:
                    883:        /* verify client host, strip trailing dot if necessary */
                    884:        p = buffer_get_string(&b, NULL);
                    885:        if (((len = strlen(p)) > 0) && p[len - 1] == '.')
                    886:                p[len - 1] = '\0';
                    887:        if (strcmp(p, chost) != 0)
                    888:                fail++;
                    889:        xfree(p);
                    890:
                    891:        /* verify client user */
                    892:        p = buffer_get_string(&b, NULL);
                    893:        if (strcmp(p, cuser) != 0)
                    894:                fail++;
                    895:        xfree(p);
                    896:
                    897:        if (buffer_len(&b) != 0)
                    898:                fail++;
                    899:        buffer_free(&b);
                    900:        return (fail == 0);
                    901: }
                    902:
                    903: int
                    904: mm_answer_keyverify(int socket, Buffer *m)
                    905: {
                    906:        Key *key;
                    907:        u_char *signature, *data, *blob;
                    908:        u_int signaturelen, datalen, bloblen;
                    909:        int verified = 0;
                    910:        int valid_data = 0;
                    911:
                    912:        blob = buffer_get_string(m, &bloblen);
                    913:        signature = buffer_get_string(m, &signaturelen);
                    914:        data = buffer_get_string(m, &datalen);
                    915:
                    916:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring   917:          !monitor_allowed_key(blob, bloblen))
1.14      markus    918:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos    919:
                    920:        key = key_from_blob(blob, bloblen);
                    921:        if (key == NULL)
1.14      markus    922:                fatal("%s: bad public key blob", __func__);
1.1       provos    923:
                    924:        switch (key_blobtype) {
                    925:        case MM_USERKEY:
                    926:                valid_data = monitor_valid_userblob(data, datalen);
                    927:                break;
                    928:        case MM_HOSTKEY:
                    929:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                    930:                    hostbased_cuser, hostbased_chost);
                    931:                break;
                    932:        default:
                    933:                valid_data = 0;
                    934:                break;
                    935:        }
                    936:        if (!valid_data)
1.14      markus    937:                fatal("%s: bad signature data blob", __func__);
1.1       provos    938:
                    939:        verified = key_verify(key, signature, signaturelen, data, datalen);
                    940:        debug3("%s: key %p signature %s",
1.14      markus    941:            __func__, key, verified ? "verified" : "unverified");
1.1       provos    942:
                    943:        key_free(key);
                    944:        xfree(blob);
                    945:        xfree(signature);
                    946:        xfree(data);
                    947:
1.17      stevesk   948:        auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
                    949:
1.1       provos    950:        monitor_reset_key_state();
1.3       markus    951:
1.1       provos    952:        buffer_clear(m);
                    953:        buffer_put_int(m, verified);
                    954:        mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
                    955:
                    956:        return (verified);
                    957: }
                    958:
1.2       markus    959: static void
1.1       provos    960: mm_record_login(Session *s, struct passwd *pw)
                    961: {
                    962:        socklen_t fromlen;
                    963:        struct sockaddr_storage from;
                    964:
                    965:        /*
                    966:         * Get IP address of client. If the connection is not a socket, let
                    967:         * the address be 0.0.0.0.
                    968:         */
                    969:        memset(&from, 0, sizeof(from));
                    970:        if (packet_connection_is_on_socket()) {
                    971:                fromlen = sizeof(from);
                    972:                if (getpeername(packet_get_connection_in(),
                    973:                        (struct sockaddr *) & from, &fromlen) < 0) {
                    974:                        debug("getpeername: %.100s", strerror(errno));
                    975:                        fatal_cleanup();
                    976:                }
                    977:        }
                    978:        /* Record that there was a login on that tty from the remote host. */
                    979:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
                    980:            get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
                    981:            (struct sockaddr *)&from);
                    982: }
                    983:
                    984: static void
                    985: mm_session_close(Session *s)
                    986: {
1.14      markus    987:        debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1.1       provos    988:        if (s->ttyfd != -1) {
1.14      markus    989:                debug3("%s: tty %s ptyfd %d",  __func__, s->tty, s->ptyfd);
1.1       provos    990:                fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
                    991:                session_pty_cleanup2(s);
                    992:        }
                    993:        s->used = 0;
                    994: }
                    995:
                    996: int
                    997: mm_answer_pty(int socket, Buffer *m)
                    998: {
1.11      mouring   999:        extern struct monitor *pmonitor;
1.1       provos   1000:        Session *s;
                   1001:        int res, fd0;
                   1002:
1.14      markus   1003:        debug3("%s entering", __func__);
1.1       provos   1004:
                   1005:        buffer_clear(m);
                   1006:        s = session_new();
                   1007:        if (s == NULL)
                   1008:                goto error;
                   1009:        s->authctxt = authctxt;
                   1010:        s->pw = authctxt->pw;
1.11      mouring  1011:        s->pid = pmonitor->m_pid;
1.1       provos   1012:        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
                   1013:        if (res == 0)
                   1014:                goto error;
                   1015:        fatal_add_cleanup(session_pty_cleanup2, (void *)s);
                   1016:        pty_setowner(authctxt->pw, s->tty);
                   1017:
                   1018:        buffer_put_int(m, 1);
                   1019:        buffer_put_cstring(m, s->tty);
                   1020:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                   1021:
                   1022:        mm_send_fd(socket, s->ptyfd);
                   1023:        mm_send_fd(socket, s->ttyfd);
                   1024:
                   1025:        /* We need to trick ttyslot */
                   1026:        if (dup2(s->ttyfd, 0) == -1)
1.14      markus   1027:                fatal("%s: dup2", __func__);
1.1       provos   1028:
                   1029:        mm_record_login(s, authctxt->pw);
                   1030:
                   1031:        /* Now we can close the file descriptor again */
                   1032:        close(0);
                   1033:
                   1034:        /* make sure nothing uses fd 0 */
                   1035:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14      markus   1036:                fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1       provos   1037:        if (fd0 != 0)
1.14      markus   1038:                error("%s: fd0 %d != 0", __func__, fd0);
1.1       provos   1039:
                   1040:        /* slave is not needed */
                   1041:        close(s->ttyfd);
                   1042:        s->ttyfd = s->ptyfd;
                   1043:        /* no need to dup() because nobody closes ptyfd */
                   1044:        s->ptymaster = s->ptyfd;
                   1045:
1.14      markus   1046:        debug3("%s: tty %s ptyfd %d",  __func__, s->tty, s->ttyfd);
1.1       provos   1047:
                   1048:        return (0);
                   1049:
                   1050:  error:
                   1051:        if (s != NULL)
                   1052:                mm_session_close(s);
                   1053:        buffer_put_int(m, 0);
                   1054:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                   1055:        return (0);
                   1056: }
                   1057:
                   1058: int
                   1059: mm_answer_pty_cleanup(int socket, Buffer *m)
                   1060: {
                   1061:        Session *s;
                   1062:        char *tty;
                   1063:
1.14      markus   1064:        debug3("%s entering", __func__);
1.1       provos   1065:
                   1066:        tty = buffer_get_string(m, NULL);
                   1067:        if ((s = session_by_tty(tty)) != NULL)
                   1068:                mm_session_close(s);
                   1069:        buffer_clear(m);
                   1070:        xfree(tty);
                   1071:        return (0);
                   1072: }
                   1073:
                   1074: int
                   1075: mm_answer_sesskey(int socket, Buffer *m)
                   1076: {
                   1077:        BIGNUM *p;
                   1078:        int rsafail;
                   1079:
                   1080:        /* Turn off permissions */
                   1081:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                   1082:
                   1083:        if ((p = BN_new()) == NULL)
1.14      markus   1084:                fatal("%s: BN_new", __func__);
1.1       provos   1085:
                   1086:        buffer_get_bignum2(m, p);
                   1087:
                   1088:        rsafail = ssh1_session_key(p);
                   1089:
                   1090:        buffer_clear(m);
                   1091:        buffer_put_int(m, rsafail);
                   1092:        buffer_put_bignum2(m, p);
                   1093:
                   1094:        BN_clear_free(p);
                   1095:
                   1096:        mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
                   1097:
                   1098:        /* Turn on permissions for sessid passing */
                   1099:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
                   1100:
                   1101:        return (0);
                   1102: }
                   1103:
                   1104: int
                   1105: mm_answer_sessid(int socket, Buffer *m)
                   1106: {
                   1107:        int i;
                   1108:
1.14      markus   1109:        debug3("%s entering", __func__);
1.1       provos   1110:
                   1111:        if (buffer_len(m) != 16)
1.14      markus   1112:                fatal("%s: bad ssh1 session id", __func__);
1.1       provos   1113:        for (i = 0; i < 16; i++)
                   1114:                session_id[i] = buffer_get_char(m);
                   1115:
                   1116:        /* Turn on permissions for getpwnam */
                   1117:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                   1118:
                   1119:        return (0);
                   1120: }
                   1121:
                   1122: int
                   1123: mm_answer_rsa_keyallowed(int socket, Buffer *m)
                   1124: {
                   1125:        BIGNUM *client_n;
                   1126:        Key *key = NULL;
1.3       markus   1127:        u_char *blob = NULL;
                   1128:        u_int blen = 0;
1.1       provos   1129:        int allowed = 0;
                   1130:
1.14      markus   1131:        debug3("%s entering", __func__);
1.1       provos   1132:
1.12      markus   1133:        if (options.rsa_authentication && authctxt->valid) {
1.1       provos   1134:                if ((client_n = BN_new()) == NULL)
1.14      markus   1135:                        fatal("%s: BN_new", __func__);
1.1       provos   1136:                buffer_get_bignum2(m, client_n);
                   1137:                allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
                   1138:                BN_clear_free(client_n);
                   1139:        }
                   1140:        buffer_clear(m);
                   1141:        buffer_put_int(m, allowed);
                   1142:
                   1143:        /* clear temporarily storage (used by generate challenge) */
                   1144:        monitor_reset_key_state();
                   1145:
                   1146:        if (allowed && key != NULL) {
                   1147:                key->type = KEY_RSA;    /* cheat for key_to_blob */
                   1148:                if (key_to_blob(key, &blob, &blen) == 0)
1.14      markus   1149:                        fatal("%s: key_to_blob failed", __func__);
1.1       provos   1150:                buffer_put_string(m, blob, blen);
                   1151:
                   1152:                /* Save temporarily for comparison in verify */
                   1153:                key_blob = blob;
                   1154:                key_bloblen = blen;
                   1155:                key_blobtype = MM_RSAUSERKEY;
                   1156:                key_free(key);
                   1157:        }
                   1158:
                   1159:        mm_append_debug(m);
                   1160:
                   1161:        mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
                   1162:
                   1163:        monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                   1164:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
                   1165:        return (0);
                   1166: }
                   1167:
                   1168: int
                   1169: mm_answer_rsa_challenge(int socket, Buffer *m)
                   1170: {
                   1171:        Key *key = NULL;
1.3       markus   1172:        u_char *blob;
                   1173:        u_int blen;
1.1       provos   1174:
1.14      markus   1175:        debug3("%s entering", __func__);
1.1       provos   1176:
                   1177:        if (!authctxt->valid)
1.14      markus   1178:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1179:        blob = buffer_get_string(m, &blen);
                   1180:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1181:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1182:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1183:                fatal("%s: key type mismatch", __func__);
1.1       provos   1184:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1185:                fatal("%s: received bad key", __func__);
1.1       provos   1186:
                   1187:        if (ssh1_challenge)
                   1188:                BN_clear_free(ssh1_challenge);
                   1189:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1190:
                   1191:        buffer_clear(m);
                   1192:        buffer_put_bignum2(m, ssh1_challenge);
                   1193:
1.14      markus   1194:        debug3("%s sending reply", __func__);
1.1       provos   1195:        mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
                   1196:
                   1197:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
                   1198:        return (0);
                   1199: }
                   1200:
                   1201: int
                   1202: mm_answer_rsa_response(int socket, Buffer *m)
                   1203: {
                   1204:        Key *key = NULL;
1.3       markus   1205:        u_char *blob, *response;
                   1206:        u_int blen, len;
                   1207:        int success;
1.1       provos   1208:
1.14      markus   1209:        debug3("%s entering", __func__);
1.1       provos   1210:
                   1211:        if (!authctxt->valid)
1.14      markus   1212:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1213:        if (ssh1_challenge == NULL)
1.14      markus   1214:                fatal("%s: no ssh1_challenge", __func__);
1.1       provos   1215:
                   1216:        blob = buffer_get_string(m, &blen);
                   1217:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1218:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1219:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1220:                fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1       provos   1221:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1222:                fatal("%s: received bad key", __func__);
1.1       provos   1223:        response = buffer_get_string(m, &len);
                   1224:        if (len != 16)
1.14      markus   1225:                fatal("%s: received bad response to challenge", __func__);
1.1       provos   1226:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1227:
                   1228:        key_free(key);
                   1229:        xfree(response);
                   1230:
                   1231:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1232:
                   1233:        /* reset state */
                   1234:        BN_clear_free(ssh1_challenge);
                   1235:        ssh1_challenge = NULL;
                   1236:        monitor_reset_key_state();
                   1237:
                   1238:        buffer_clear(m);
                   1239:        buffer_put_int(m, success);
                   1240:        mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
                   1241:
                   1242:        return (success);
                   1243: }
                   1244:
                   1245: int
                   1246: mm_answer_term(int socket, Buffer *req)
                   1247: {
1.11      mouring  1248:        extern struct monitor *pmonitor;
1.1       provos   1249:        int res, status;
                   1250:
1.14      markus   1251:        debug3("%s: tearing down sessions", __func__);
1.1       provos   1252:
                   1253:        /* The child is terminating */
                   1254:        session_destroy_all(&mm_session_close);
                   1255:
1.11      mouring  1256:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1257:                if (errno != EINTR)
                   1258:                        exit(1);
1.1       provos   1259:
                   1260:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1261:
                   1262:        /* Terminate process */
                   1263:        exit (res);
                   1264: }
                   1265:
                   1266: void
1.11      mouring  1267: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1268: {
                   1269:        if (compat20) {
                   1270:                set_newkeys(MODE_IN);
                   1271:                set_newkeys(MODE_OUT);
                   1272:        } else {
                   1273:                packet_set_protocol_flags(child_state.ssh1protoflags);
1.15      markus   1274:                packet_set_encryption_key(child_state.ssh1key,
                   1275:                    child_state.ssh1keylen, child_state.ssh1cipher);
                   1276:                xfree(child_state.ssh1key);
1.1       provos   1277:        }
                   1278:
1.15      markus   1279:        /* for rc4 and other stateful ciphers */
1.1       provos   1280:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1281:        xfree(child_state.keyout);
                   1282:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1283:        xfree(child_state.keyin);
                   1284:
                   1285:        if (!compat20) {
                   1286:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1287:                xfree(child_state.ivout);
                   1288:                packet_set_iv(MODE_IN, child_state.ivin);
                   1289:                xfree(child_state.ivin);
                   1290:        }
                   1291:
                   1292:        memcpy(&incoming_stream, &child_state.incoming,
                   1293:            sizeof(incoming_stream));
                   1294:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1295:            sizeof(outgoing_stream));
1.3       markus   1296:
1.1       provos   1297:        /* Update with new address */
1.16      djm      1298:        if (options.compression)
                   1299:                mm_init_compression(pmonitor->m_zlib);
1.1       provos   1300:
                   1301:        /* Network I/O buffers */
                   1302:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1303:        buffer_clear(&input);
                   1304:        buffer_append(&input, child_state.input, child_state.ilen);
                   1305:        memset(child_state.input, 0, child_state.ilen);
                   1306:        xfree(child_state.input);
                   1307:
                   1308:        buffer_clear(&output);
                   1309:        buffer_append(&output, child_state.output, child_state.olen);
                   1310:        memset(child_state.output, 0, child_state.olen);
                   1311:        xfree(child_state.output);
                   1312: }
                   1313:
1.2       markus   1314: static Kex *
1.1       provos   1315: mm_get_kex(Buffer *m)
                   1316: {
                   1317:        Kex *kex;
                   1318:        void *blob;
                   1319:        u_int bloblen;
                   1320:
                   1321:        kex = xmalloc(sizeof(*kex));
                   1322:        memset(kex, 0, sizeof(*kex));
                   1323:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13      markus   1324:        if ((session_id2 == NULL) ||
                   1325:            (kex->session_id_len != session_id2_len) ||
                   1326:            (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
                   1327:                fatal("mm_get_get: internal error: bad session id");
1.1       provos   1328:        kex->we_need = buffer_get_int(m);
                   1329:        kex->server = 1;
                   1330:        kex->hostkey_type = buffer_get_int(m);
                   1331:        kex->kex_type = buffer_get_int(m);
                   1332:        blob = buffer_get_string(m, &bloblen);
                   1333:        buffer_init(&kex->my);
                   1334:        buffer_append(&kex->my, blob, bloblen);
                   1335:        xfree(blob);
                   1336:        blob = buffer_get_string(m, &bloblen);
                   1337:        buffer_init(&kex->peer);
                   1338:        buffer_append(&kex->peer, blob, bloblen);
                   1339:        xfree(blob);
                   1340:        kex->done = 1;
                   1341:        kex->flags = buffer_get_int(m);
                   1342:        kex->client_version_string = buffer_get_string(m, NULL);
                   1343:        kex->server_version_string = buffer_get_string(m, NULL);
                   1344:        kex->load_host_key=&get_hostkey_by_type;
                   1345:        kex->host_key_index=&get_hostkey_index;
                   1346:
                   1347:        return (kex);
                   1348: }
                   1349:
                   1350: /* This function requries careful sanity checking */
                   1351:
                   1352: void
1.11      mouring  1353: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1354: {
                   1355:        Buffer m;
                   1356:        u_char *blob, *p;
                   1357:        u_int bloblen, plen;
                   1358:
1.14      markus   1359:        debug3("%s: Waiting for new keys", __func__);
1.1       provos   1360:
                   1361:        buffer_init(&m);
1.11      mouring  1362:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1363:        if (!compat20) {
                   1364:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1365:                child_state.ssh1cipher = buffer_get_int(&m);
1.15      markus   1366:                child_state.ssh1key = buffer_get_string(&m,
                   1367:                    &child_state.ssh1keylen);
1.1       provos   1368:                child_state.ivout = buffer_get_string(&m,
                   1369:                    &child_state.ivoutlen);
                   1370:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1371:                goto skip;
                   1372:        } else {
                   1373:                /* Get the Kex for rekeying */
1.11      mouring  1374:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1375:        }
                   1376:
                   1377:        blob = buffer_get_string(&m, &bloblen);
                   1378:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1379:        xfree(blob);
                   1380:
1.14      markus   1381:        debug3("%s: Waiting for second key", __func__);
1.1       provos   1382:        blob = buffer_get_string(&m, &bloblen);
                   1383:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1384:        xfree(blob);
1.3       markus   1385:
1.1       provos   1386:        /* Now get sequence numbers for the packets */
                   1387:        packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
                   1388:        packet_set_seqnr(MODE_IN, buffer_get_int(&m));
                   1389:
                   1390:  skip:
                   1391:        /* Get the key context */
                   1392:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1393:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1394:
1.14      markus   1395:        debug3("%s: Getting compression state", __func__);
1.1       provos   1396:        /* Get compression state */
                   1397:        p = buffer_get_string(&m, &plen);
                   1398:        if (plen != sizeof(child_state.outgoing))
1.14      markus   1399:                fatal("%s: bad request size", __func__);
1.1       provos   1400:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1401:        xfree(p);
                   1402:
                   1403:        p = buffer_get_string(&m, &plen);
                   1404:        if (plen != sizeof(child_state.incoming))
1.14      markus   1405:                fatal("%s: bad request size", __func__);
1.1       provos   1406:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1407:        xfree(p);
                   1408:
                   1409:        /* Network I/O buffers */
1.14      markus   1410:        debug3("%s: Getting Network I/O buffers", __func__);
1.1       provos   1411:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1412:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   1413:
                   1414:        buffer_free(&m);
                   1415: }
                   1416:
                   1417:
                   1418: /* Allocation functions for zlib */
                   1419: void *
                   1420: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
                   1421: {
1.23    ! millert  1422:        size_t len = size * ncount;
1.1       provos   1423:        void *address;
                   1424:
1.23    ! millert  1425:        if (len == 0 || ncount > SIZE_T_MAX / size)
1.18      deraadt  1426:                fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
                   1427:
                   1428:        address = mm_malloc(mm, len);
1.1       provos   1429:
                   1430:        return (address);
                   1431: }
                   1432:
                   1433: void
                   1434: mm_zfree(struct mm_master *mm, void *address)
                   1435: {
                   1436:        mm_free(mm, address);
                   1437: }
                   1438:
                   1439: void
                   1440: mm_init_compression(struct mm_master *mm)
                   1441: {
                   1442:        outgoing_stream.zalloc = (alloc_func)mm_zalloc;
                   1443:        outgoing_stream.zfree = (free_func)mm_zfree;
                   1444:        outgoing_stream.opaque = mm;
                   1445:
                   1446:        incoming_stream.zalloc = (alloc_func)mm_zalloc;
                   1447:        incoming_stream.zfree = (free_func)mm_zfree;
                   1448:        incoming_stream.opaque = mm;
                   1449: }
                   1450:
                   1451: /* XXX */
                   1452:
                   1453: #define FD_CLOSEONEXEC(x) do { \
                   1454:        if (fcntl(x, F_SETFD, 1) == -1) \
                   1455:                fatal("fcntl(%d, F_SETFD)", x); \
                   1456: } while (0)
                   1457:
1.2       markus   1458: static void
1.1       provos   1459: monitor_socketpair(int *pair)
1.3       markus   1460: {
1.1       provos   1461:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14      markus   1462:                fatal("%s: socketpair", __func__);
1.1       provos   1463:        FD_CLOSEONEXEC(pair[0]);
                   1464:        FD_CLOSEONEXEC(pair[1]);
                   1465: }
                   1466:
                   1467: #define MM_MEMSIZE     65536
                   1468:
                   1469: struct monitor *
                   1470: monitor_init(void)
                   1471: {
                   1472:        struct monitor *mon;
                   1473:        int pair[2];
                   1474:
                   1475:        mon = xmalloc(sizeof(*mon));
                   1476:
                   1477:        monitor_socketpair(pair);
                   1478:
                   1479:        mon->m_recvfd = pair[0];
                   1480:        mon->m_sendfd = pair[1];
                   1481:
                   1482:        /* Used to share zlib space across processes */
1.16      djm      1483:        if (options.compression) {
                   1484:                mon->m_zback = mm_create(NULL, MM_MEMSIZE);
                   1485:                mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1       provos   1486:
1.16      djm      1487:                /* Compression needs to share state across borders */
                   1488:                mm_init_compression(mon->m_zlib);
                   1489:        }
1.1       provos   1490:
                   1491:        return mon;
                   1492: }
                   1493:
                   1494: void
                   1495: monitor_reinit(struct monitor *mon)
                   1496: {
                   1497:        int pair[2];
                   1498:
                   1499:        monitor_socketpair(pair);
                   1500:
                   1501:        mon->m_recvfd = pair[0];
                   1502:        mon->m_sendfd = pair[1];
                   1503: }