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

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