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

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