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

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