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

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