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

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