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

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