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

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