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

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