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

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