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

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