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

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