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

1.90    ! dtucker     1: /* $OpenBSD: monitor.c,v 1.89 2006/11/07 10:31:31 markus 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"
1.88      miod       57: #include <zlib.h>
1.1       provos     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.89      markus    295:                authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
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);
1.90    ! dtucker   573:        buffer_put_string(m, &options, sizeof(options));
        !           574:        if (options.banner != NULL)
        !           575:                buffer_put_cstring(m, options.banner);
1.1       provos    576:
                    577:  out:
1.14      markus    578:        debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.59      avsm      579:        mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1       provos    580:
                    581:        /* For SSHv1 allow authentication now */
                    582:        if (!compat20)
                    583:                monitor_permit_authentications(1);
1.10      djm       584:        else {
1.1       provos    585:                /* Allow service/style information on the auth context */
                    586:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.10      djm       587:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
                    588:        }
                    589:
                    590:
                    591:        return (0);
                    592: }
                    593:
1.59      avsm      594: int mm_answer_auth2_read_banner(int sock, Buffer *m)
1.10      djm       595: {
                    596:        char *banner;
                    597:
                    598:        buffer_clear(m);
                    599:        banner = auth2_read_banner();
                    600:        buffer_put_cstring(m, banner != NULL ? banner : "");
1.59      avsm      601:        mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1       provos    602:
1.10      djm       603:        if (banner != NULL)
1.21      deraadt   604:                xfree(banner);
1.1       provos    605:
                    606:        return (0);
                    607: }
                    608:
                    609: int
1.59      avsm      610: mm_answer_authserv(int sock, Buffer *m)
1.1       provos    611: {
                    612:        monitor_permit_authentications(1);
                    613:
                    614:        authctxt->service = buffer_get_string(m, NULL);
                    615:        authctxt->style = buffer_get_string(m, NULL);
1.6       stevesk   616:        debug3("%s: service=%s, style=%s",
1.14      markus    617:            __func__, authctxt->service, authctxt->style);
1.6       stevesk   618:
1.1       provos    619:        if (strlen(authctxt->style) == 0) {
                    620:                xfree(authctxt->style);
                    621:                authctxt->style = NULL;
                    622:        }
                    623:
                    624:        return (0);
                    625: }
                    626:
                    627: int
1.59      avsm      628: mm_answer_authpassword(int sock, Buffer *m)
1.1       provos    629: {
                    630:        static int call_count;
                    631:        char *passwd;
1.22      stevesk   632:        int authenticated;
                    633:        u_int plen;
1.1       provos    634:
                    635:        passwd = buffer_get_string(m, &plen);
                    636:        /* Only authenticate if the context is valid */
1.12      markus    637:        authenticated = options.password_authentication &&
1.48      markus    638:            auth_password(authctxt, passwd);
1.1       provos    639:        memset(passwd, 0, strlen(passwd));
                    640:        xfree(passwd);
                    641:
                    642:        buffer_clear(m);
                    643:        buffer_put_int(m, authenticated);
                    644:
1.14      markus    645:        debug3("%s: sending result %d", __func__, authenticated);
1.59      avsm      646:        mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1       provos    647:
                    648:        call_count++;
                    649:        if (plen == 0 && call_count == 1)
                    650:                auth_method = "none";
                    651:        else
                    652:                auth_method = "password";
                    653:
                    654:        /* Causes monitor loop to terminate if authenticated */
                    655:        return (authenticated);
                    656: }
                    657:
                    658: #ifdef BSD_AUTH
                    659: int
1.59      avsm      660: mm_answer_bsdauthquery(int sock, Buffer *m)
1.1       provos    661: {
                    662:        char *name, *infotxt;
                    663:        u_int numprompts;
                    664:        u_int *echo_on;
                    665:        char **prompts;
1.31      markus    666:        u_int success;
1.1       provos    667:
1.31      markus    668:        success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    669:            &prompts, &echo_on) < 0 ? 0 : 1;
1.1       provos    670:
                    671:        buffer_clear(m);
1.31      markus    672:        buffer_put_int(m, success);
                    673:        if (success)
1.1       provos    674:                buffer_put_cstring(m, prompts[0]);
                    675:
1.31      markus    676:        debug3("%s: sending challenge success: %u", __func__, success);
1.59      avsm      677:        mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1.1       provos    678:
1.31      markus    679:        if (success) {
1.1       provos    680:                xfree(name);
                    681:                xfree(infotxt);
                    682:                xfree(prompts);
                    683:                xfree(echo_on);
                    684:        }
                    685:
                    686:        return (0);
                    687: }
                    688:
                    689: int
1.59      avsm      690: mm_answer_bsdauthrespond(int sock, Buffer *m)
1.1       provos    691: {
                    692:        char *response;
                    693:        int authok;
                    694:
                    695:        if (authctxt->as == 0)
1.14      markus    696:                fatal("%s: no bsd auth session", __func__);
1.1       provos    697:
                    698:        response = buffer_get_string(m, NULL);
1.12      markus    699:        authok = options.challenge_response_authentication &&
                    700:            auth_userresponse(authctxt->as, response, 0);
1.1       provos    701:        authctxt->as = NULL;
1.14      markus    702:        debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1       provos    703:        xfree(response);
                    704:
                    705:        buffer_clear(m);
                    706:        buffer_put_int(m, authok);
                    707:
1.14      markus    708:        debug3("%s: sending authenticated: %d", __func__, authok);
1.59      avsm      709:        mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1       provos    710:
                    711:        auth_method = "bsdauth";
                    712:
                    713:        return (authok != 0);
                    714: }
                    715: #endif
                    716:
                    717: #ifdef SKEY
                    718: int
1.59      avsm      719: mm_answer_skeyquery(int sock, Buffer *m)
1.1       provos    720: {
                    721:        struct skey skey;
                    722:        char challenge[1024];
1.31      markus    723:        u_int success;
1.1       provos    724:
1.31      markus    725:        success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
1.1       provos    726:
                    727:        buffer_clear(m);
1.31      markus    728:        buffer_put_int(m, success);
                    729:        if (success)
1.1       provos    730:                buffer_put_cstring(m, challenge);
                    731:
1.31      markus    732:        debug3("%s: sending challenge success: %u", __func__, success);
1.59      avsm      733:        mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
1.1       provos    734:
                    735:        return (0);
                    736: }
                    737:
                    738: int
1.59      avsm      739: mm_answer_skeyrespond(int sock, Buffer *m)
1.1       provos    740: {
                    741:        char *response;
                    742:        int authok;
                    743:
                    744:        response = buffer_get_string(m, NULL);
                    745:
1.12      markus    746:        authok = (options.challenge_response_authentication &&
                    747:            authctxt->valid &&
1.1       provos    748:            skey_haskey(authctxt->pw->pw_name) == 0 &&
                    749:            skey_passcheck(authctxt->pw->pw_name, response) != -1);
                    750:
                    751:        xfree(response);
                    752:
                    753:        buffer_clear(m);
                    754:        buffer_put_int(m, authok);
                    755:
1.14      markus    756:        debug3("%s: sending authenticated: %d", __func__, authok);
1.59      avsm      757:        mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
1.1       provos    758:
                    759:        auth_method = "skey";
                    760:
                    761:        return (authok != 0);
                    762: }
                    763: #endif
                    764:
1.2       markus    765: static void
1.1       provos    766: mm_append_debug(Buffer *m)
                    767: {
                    768:        if (auth_debug_init && buffer_len(&auth_debug)) {
1.14      markus    769:                debug3("%s: Appending debug messages for child", __func__);
1.1       provos    770:                buffer_append(m, buffer_ptr(&auth_debug),
                    771:                    buffer_len(&auth_debug));
                    772:                buffer_clear(&auth_debug);
                    773:        }
                    774: }
                    775:
                    776: int
1.59      avsm      777: mm_answer_keyallowed(int sock, Buffer *m)
1.1       provos    778: {
                    779:        Key *key;
1.26      markus    780:        char *cuser, *chost;
                    781:        u_char *blob;
1.1       provos    782:        u_int bloblen;
                    783:        enum mm_keytype type = 0;
                    784:        int allowed = 0;
                    785:
1.14      markus    786:        debug3("%s entering", __func__);
1.3       markus    787:
1.1       provos    788:        type = buffer_get_int(m);
                    789:        cuser = buffer_get_string(m, NULL);
                    790:        chost = buffer_get_string(m, NULL);
                    791:        blob = buffer_get_string(m, &bloblen);
                    792:
                    793:        key = key_from_blob(blob, bloblen);
                    794:
                    795:        if ((compat20 && type == MM_RSAHOSTKEY) ||
                    796:            (!compat20 && type != MM_RSAHOSTKEY))
1.14      markus    797:                fatal("%s: key type and protocol mismatch", __func__);
1.1       provos    798:
1.14      markus    799:        debug3("%s: key_from_blob: %p", __func__, key);
1.1       provos    800:
1.51      djm       801:        if (key != NULL && authctxt->valid) {
1.63      deraadt   802:                switch (type) {
1.1       provos    803:                case MM_USERKEY:
1.12      markus    804:                        allowed = options.pubkey_authentication &&
                    805:                            user_key_allowed(authctxt->pw, key);
1.77      dtucker   806:                        auth_method = "publickey";
1.1       provos    807:                        break;
                    808:                case MM_HOSTKEY:
1.12      markus    809:                        allowed = options.hostbased_authentication &&
                    810:                            hostbased_key_allowed(authctxt->pw,
1.1       provos    811:                            cuser, chost, key);
1.77      dtucker   812:                        auth_method = "hostbased";
1.1       provos    813:                        break;
                    814:                case MM_RSAHOSTKEY:
                    815:                        key->type = KEY_RSA1; /* XXX */
1.12      markus    816:                        allowed = options.rhosts_rsa_authentication &&
                    817:                            auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1       provos    818:                            cuser, chost, key);
1.77      dtucker   819:                        auth_method = "rsa";
1.1       provos    820:                        break;
                    821:                default:
1.14      markus    822:                        fatal("%s: unknown key type %d", __func__, type);
1.1       provos    823:                        break;
                    824:                }
1.33      markus    825:        }
                    826:        if (key != NULL)
1.1       provos    827:                key_free(key);
                    828:
                    829:        /* clear temporarily storage (used by verify) */
                    830:        monitor_reset_key_state();
                    831:
                    832:        if (allowed) {
                    833:                /* Save temporarily for comparison in verify */
                    834:                key_blob = blob;
                    835:                key_bloblen = bloblen;
                    836:                key_blobtype = type;
                    837:                hostbased_cuser = cuser;
                    838:                hostbased_chost = chost;
1.72      djm       839:        } else {
1.77      dtucker   840:                /* Log failed attempt */
                    841:                auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1.72      djm       842:                xfree(blob);
                    843:                xfree(cuser);
                    844:                xfree(chost);
1.1       provos    845:        }
                    846:
                    847:        debug3("%s: key %p is %s",
1.14      markus    848:            __func__, key, allowed ? "allowed" : "disallowed");
1.1       provos    849:
                    850:        buffer_clear(m);
                    851:        buffer_put_int(m, allowed);
1.32      markus    852:        buffer_put_int(m, forced_command != NULL);
1.1       provos    853:
                    854:        mm_append_debug(m);
                    855:
1.59      avsm      856:        mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1       provos    857:
                    858:        if (type == MM_RSAHOSTKEY)
                    859:                monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                    860:
                    861:        return (0);
                    862: }
                    863:
                    864: static int
                    865: monitor_valid_userblob(u_char *data, u_int datalen)
                    866: {
                    867:        Buffer b;
1.26      markus    868:        char *p;
1.1       provos    869:        u_int len;
                    870:        int fail = 0;
                    871:
                    872:        buffer_init(&b);
                    873:        buffer_append(&b, data, datalen);
1.3       markus    874:
1.1       provos    875:        if (datafellows & SSH_OLD_SESSIONID) {
1.13      markus    876:                p = buffer_ptr(&b);
                    877:                len = buffer_len(&b);
                    878:                if ((session_id2 == NULL) ||
                    879:                    (len < session_id2_len) ||
                    880:                    (memcmp(p, session_id2, session_id2_len) != 0))
                    881:                        fail++;
1.1       provos    882:                buffer_consume(&b, session_id2_len);
                    883:        } else {
1.13      markus    884:                p = buffer_get_string(&b, &len);
                    885:                if ((session_id2 == NULL) ||
                    886:                    (len != session_id2_len) ||
                    887:                    (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    888:                        fail++;
1.13      markus    889:                xfree(p);
1.1       provos    890:        }
                    891:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    892:                fail++;
                    893:        p = buffer_get_string(&b, NULL);
                    894:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    895:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    896:                    authctxt->user, p);
                    897:                fail++;
                    898:        }
                    899:        xfree(p);
                    900:        buffer_skip_string(&b);
                    901:        if (datafellows & SSH_BUG_PKAUTH) {
                    902:                if (!buffer_get_char(&b))
                    903:                        fail++;
                    904:        } else {
                    905:                p = buffer_get_string(&b, NULL);
                    906:                if (strcmp("publickey", p) != 0)
                    907:                        fail++;
                    908:                xfree(p);
                    909:                if (!buffer_get_char(&b))
                    910:                        fail++;
                    911:                buffer_skip_string(&b);
                    912:        }
                    913:        buffer_skip_string(&b);
                    914:        if (buffer_len(&b) != 0)
                    915:                fail++;
                    916:        buffer_free(&b);
                    917:        return (fail == 0);
                    918: }
                    919:
                    920: static int
1.26      markus    921: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
                    922:     char *chost)
1.1       provos    923: {
                    924:        Buffer b;
1.26      markus    925:        char *p;
1.1       provos    926:        u_int len;
                    927:        int fail = 0;
                    928:
                    929:        buffer_init(&b);
                    930:        buffer_append(&b, data, datalen);
1.3       markus    931:
1.13      markus    932:        p = buffer_get_string(&b, &len);
                    933:        if ((session_id2 == NULL) ||
                    934:            (len != session_id2_len) ||
                    935:            (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    936:                fail++;
1.13      markus    937:        xfree(p);
                    938:
1.1       provos    939:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    940:                fail++;
                    941:        p = buffer_get_string(&b, NULL);
                    942:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    943:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    944:                    authctxt->user, p);
                    945:                fail++;
                    946:        }
                    947:        xfree(p);
                    948:        buffer_skip_string(&b); /* service */
                    949:        p = buffer_get_string(&b, NULL);
                    950:        if (strcmp(p, "hostbased") != 0)
                    951:                fail++;
                    952:        xfree(p);
                    953:        buffer_skip_string(&b); /* pkalg */
                    954:        buffer_skip_string(&b); /* pkblob */
                    955:
                    956:        /* verify client host, strip trailing dot if necessary */
                    957:        p = buffer_get_string(&b, NULL);
                    958:        if (((len = strlen(p)) > 0) && p[len - 1] == '.')
                    959:                p[len - 1] = '\0';
                    960:        if (strcmp(p, chost) != 0)
                    961:                fail++;
                    962:        xfree(p);
                    963:
                    964:        /* verify client user */
                    965:        p = buffer_get_string(&b, NULL);
                    966:        if (strcmp(p, cuser) != 0)
                    967:                fail++;
                    968:        xfree(p);
                    969:
                    970:        if (buffer_len(&b) != 0)
                    971:                fail++;
                    972:        buffer_free(&b);
                    973:        return (fail == 0);
                    974: }
                    975:
                    976: int
1.59      avsm      977: mm_answer_keyverify(int sock, Buffer *m)
1.1       provos    978: {
                    979:        Key *key;
                    980:        u_char *signature, *data, *blob;
                    981:        u_int signaturelen, datalen, bloblen;
                    982:        int verified = 0;
                    983:        int valid_data = 0;
                    984:
                    985:        blob = buffer_get_string(m, &bloblen);
                    986:        signature = buffer_get_string(m, &signaturelen);
                    987:        data = buffer_get_string(m, &datalen);
                    988:
                    989:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring   990:          !monitor_allowed_key(blob, bloblen))
1.14      markus    991:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos    992:
                    993:        key = key_from_blob(blob, bloblen);
                    994:        if (key == NULL)
1.14      markus    995:                fatal("%s: bad public key blob", __func__);
1.1       provos    996:
                    997:        switch (key_blobtype) {
                    998:        case MM_USERKEY:
                    999:                valid_data = monitor_valid_userblob(data, datalen);
                   1000:                break;
                   1001:        case MM_HOSTKEY:
                   1002:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                   1003:                    hostbased_cuser, hostbased_chost);
                   1004:                break;
                   1005:        default:
                   1006:                valid_data = 0;
                   1007:                break;
                   1008:        }
                   1009:        if (!valid_data)
1.14      markus   1010:                fatal("%s: bad signature data blob", __func__);
1.1       provos   1011:
                   1012:        verified = key_verify(key, signature, signaturelen, data, datalen);
                   1013:        debug3("%s: key %p signature %s",
1.89      markus   1014:            __func__, key, (verified == 1) ? "verified" : "unverified");
1.1       provos   1015:
                   1016:        key_free(key);
                   1017:        xfree(blob);
                   1018:        xfree(signature);
                   1019:        xfree(data);
                   1020:
1.17      stevesk  1021:        auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
                   1022:
1.1       provos   1023:        monitor_reset_key_state();
1.3       markus   1024:
1.1       provos   1025:        buffer_clear(m);
                   1026:        buffer_put_int(m, verified);
1.59      avsm     1027:        mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.1       provos   1028:
1.89      markus   1029:        return (verified == 1);
1.1       provos   1030: }
                   1031:
1.2       markus   1032: static void
1.1       provos   1033: mm_record_login(Session *s, struct passwd *pw)
                   1034: {
                   1035:        socklen_t fromlen;
                   1036:        struct sockaddr_storage from;
                   1037:
                   1038:        /*
                   1039:         * Get IP address of client. If the connection is not a socket, let
                   1040:         * the address be 0.0.0.0.
                   1041:         */
                   1042:        memset(&from, 0, sizeof(from));
1.24      stevesk  1043:        fromlen = sizeof(from);
1.1       provos   1044:        if (packet_connection_is_on_socket()) {
                   1045:                if (getpeername(packet_get_connection_in(),
1.74      deraadt  1046:                    (struct sockaddr *)&from, &fromlen) < 0) {
1.1       provos   1047:                        debug("getpeername: %.100s", strerror(errno));
1.50      markus   1048:                        cleanup_exit(255);
1.1       provos   1049:                }
                   1050:        }
                   1051:        /* Record that there was a login on that tty from the remote host. */
                   1052:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.42      markus   1053:            get_remote_name_or_ip(utmp_len, options.use_dns),
1.24      stevesk  1054:            (struct sockaddr *)&from, fromlen);
1.1       provos   1055: }
                   1056:
                   1057: static void
                   1058: mm_session_close(Session *s)
                   1059: {
1.41      djm      1060:        debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1       provos   1061:        if (s->ttyfd != -1) {
1.86      stevesk  1062:                debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1.1       provos   1063:                session_pty_cleanup2(s);
                   1064:        }
                   1065:        s->used = 0;
                   1066: }
                   1067:
                   1068: int
1.59      avsm     1069: mm_answer_pty(int sock, Buffer *m)
1.1       provos   1070: {
1.11      mouring  1071:        extern struct monitor *pmonitor;
1.1       provos   1072:        Session *s;
                   1073:        int res, fd0;
                   1074:
1.14      markus   1075:        debug3("%s entering", __func__);
1.1       provos   1076:
                   1077:        buffer_clear(m);
                   1078:        s = session_new();
                   1079:        if (s == NULL)
                   1080:                goto error;
                   1081:        s->authctxt = authctxt;
                   1082:        s->pw = authctxt->pw;
1.11      mouring  1083:        s->pid = pmonitor->m_pid;
1.1       provos   1084:        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
                   1085:        if (res == 0)
                   1086:                goto error;
                   1087:        pty_setowner(authctxt->pw, s->tty);
                   1088:
                   1089:        buffer_put_int(m, 1);
                   1090:        buffer_put_cstring(m, s->tty);
                   1091:
                   1092:        /* We need to trick ttyslot */
                   1093:        if (dup2(s->ttyfd, 0) == -1)
1.14      markus   1094:                fatal("%s: dup2", __func__);
1.1       provos   1095:
                   1096:        mm_record_login(s, authctxt->pw);
                   1097:
                   1098:        /* Now we can close the file descriptor again */
                   1099:        close(0);
1.61      dtucker  1100:
                   1101:        /* send messages generated by record_login */
                   1102:        buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
                   1103:        buffer_clear(&loginmsg);
                   1104:
                   1105:        mm_request_send(sock, MONITOR_ANS_PTY, m);
                   1106:
                   1107:        mm_send_fd(sock, s->ptyfd);
                   1108:        mm_send_fd(sock, s->ttyfd);
1.1       provos   1109:
                   1110:        /* make sure nothing uses fd 0 */
                   1111:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14      markus   1112:                fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1       provos   1113:        if (fd0 != 0)
1.14      markus   1114:                error("%s: fd0 %d != 0", __func__, fd0);
1.1       provos   1115:
                   1116:        /* slave is not needed */
                   1117:        close(s->ttyfd);
                   1118:        s->ttyfd = s->ptyfd;
                   1119:        /* no need to dup() because nobody closes ptyfd */
                   1120:        s->ptymaster = s->ptyfd;
                   1121:
1.86      stevesk  1122:        debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1       provos   1123:
                   1124:        return (0);
                   1125:
                   1126:  error:
                   1127:        if (s != NULL)
                   1128:                mm_session_close(s);
                   1129:        buffer_put_int(m, 0);
1.59      avsm     1130:        mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1       provos   1131:        return (0);
                   1132: }
                   1133:
                   1134: int
1.59      avsm     1135: mm_answer_pty_cleanup(int sock, Buffer *m)
1.1       provos   1136: {
                   1137:        Session *s;
                   1138:        char *tty;
                   1139:
1.14      markus   1140:        debug3("%s entering", __func__);
1.1       provos   1141:
                   1142:        tty = buffer_get_string(m, NULL);
                   1143:        if ((s = session_by_tty(tty)) != NULL)
                   1144:                mm_session_close(s);
                   1145:        buffer_clear(m);
                   1146:        xfree(tty);
                   1147:        return (0);
                   1148: }
                   1149:
                   1150: int
1.59      avsm     1151: mm_answer_sesskey(int sock, Buffer *m)
1.1       provos   1152: {
                   1153:        BIGNUM *p;
                   1154:        int rsafail;
                   1155:
                   1156:        /* Turn off permissions */
1.62      dtucker  1157:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1.1       provos   1158:
                   1159:        if ((p = BN_new()) == NULL)
1.14      markus   1160:                fatal("%s: BN_new", __func__);
1.1       provos   1161:
                   1162:        buffer_get_bignum2(m, p);
                   1163:
                   1164:        rsafail = ssh1_session_key(p);
                   1165:
                   1166:        buffer_clear(m);
                   1167:        buffer_put_int(m, rsafail);
                   1168:        buffer_put_bignum2(m, p);
                   1169:
                   1170:        BN_clear_free(p);
                   1171:
1.59      avsm     1172:        mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1.1       provos   1173:
                   1174:        /* Turn on permissions for sessid passing */
                   1175:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
                   1176:
                   1177:        return (0);
                   1178: }
                   1179:
                   1180: int
1.59      avsm     1181: mm_answer_sessid(int sock, Buffer *m)
1.1       provos   1182: {
                   1183:        int i;
                   1184:
1.14      markus   1185:        debug3("%s entering", __func__);
1.1       provos   1186:
                   1187:        if (buffer_len(m) != 16)
1.14      markus   1188:                fatal("%s: bad ssh1 session id", __func__);
1.1       provos   1189:        for (i = 0; i < 16; i++)
                   1190:                session_id[i] = buffer_get_char(m);
                   1191:
                   1192:        /* Turn on permissions for getpwnam */
                   1193:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                   1194:
                   1195:        return (0);
                   1196: }
                   1197:
                   1198: int
1.59      avsm     1199: mm_answer_rsa_keyallowed(int sock, Buffer *m)
1.1       provos   1200: {
                   1201:        BIGNUM *client_n;
                   1202:        Key *key = NULL;
1.3       markus   1203:        u_char *blob = NULL;
                   1204:        u_int blen = 0;
1.1       provos   1205:        int allowed = 0;
                   1206:
1.14      markus   1207:        debug3("%s entering", __func__);
1.1       provos   1208:
1.77      dtucker  1209:        auth_method = "rsa";
1.12      markus   1210:        if (options.rsa_authentication && authctxt->valid) {
1.1       provos   1211:                if ((client_n = BN_new()) == NULL)
1.14      markus   1212:                        fatal("%s: BN_new", __func__);
1.1       provos   1213:                buffer_get_bignum2(m, client_n);
                   1214:                allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
                   1215:                BN_clear_free(client_n);
                   1216:        }
                   1217:        buffer_clear(m);
                   1218:        buffer_put_int(m, allowed);
1.32      markus   1219:        buffer_put_int(m, forced_command != NULL);
1.1       provos   1220:
                   1221:        /* clear temporarily storage (used by generate challenge) */
                   1222:        monitor_reset_key_state();
                   1223:
                   1224:        if (allowed && key != NULL) {
                   1225:                key->type = KEY_RSA;    /* cheat for key_to_blob */
                   1226:                if (key_to_blob(key, &blob, &blen) == 0)
1.14      markus   1227:                        fatal("%s: key_to_blob failed", __func__);
1.1       provos   1228:                buffer_put_string(m, blob, blen);
                   1229:
                   1230:                /* Save temporarily for comparison in verify */
                   1231:                key_blob = blob;
                   1232:                key_bloblen = blen;
                   1233:                key_blobtype = MM_RSAUSERKEY;
1.33      markus   1234:        }
                   1235:        if (key != NULL)
1.1       provos   1236:                key_free(key);
                   1237:
                   1238:        mm_append_debug(m);
                   1239:
1.59      avsm     1240:        mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1.1       provos   1241:
                   1242:        monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                   1243:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
                   1244:        return (0);
                   1245: }
                   1246:
                   1247: int
1.59      avsm     1248: mm_answer_rsa_challenge(int sock, Buffer *m)
1.1       provos   1249: {
                   1250:        Key *key = NULL;
1.3       markus   1251:        u_char *blob;
                   1252:        u_int blen;
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:        blob = buffer_get_string(m, &blen);
                   1259:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1260:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1261:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1262:                fatal("%s: key type mismatch", __func__);
1.1       provos   1263:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1264:                fatal("%s: received bad key", __func__);
1.1       provos   1265:
                   1266:        if (ssh1_challenge)
                   1267:                BN_clear_free(ssh1_challenge);
                   1268:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1269:
                   1270:        buffer_clear(m);
                   1271:        buffer_put_bignum2(m, ssh1_challenge);
                   1272:
1.14      markus   1273:        debug3("%s sending reply", __func__);
1.59      avsm     1274:        mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1.1       provos   1275:
                   1276:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33      markus   1277:
                   1278:        xfree(blob);
                   1279:        key_free(key);
1.1       provos   1280:        return (0);
                   1281: }
                   1282:
                   1283: int
1.59      avsm     1284: mm_answer_rsa_response(int sock, Buffer *m)
1.1       provos   1285: {
                   1286:        Key *key = NULL;
1.3       markus   1287:        u_char *blob, *response;
                   1288:        u_int blen, len;
                   1289:        int success;
1.1       provos   1290:
1.14      markus   1291:        debug3("%s entering", __func__);
1.1       provos   1292:
                   1293:        if (!authctxt->valid)
1.14      markus   1294:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1295:        if (ssh1_challenge == NULL)
1.14      markus   1296:                fatal("%s: no ssh1_challenge", __func__);
1.1       provos   1297:
                   1298:        blob = buffer_get_string(m, &blen);
                   1299:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1300:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1301:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1302:                fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1       provos   1303:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1304:                fatal("%s: received bad key", __func__);
1.1       provos   1305:        response = buffer_get_string(m, &len);
                   1306:        if (len != 16)
1.14      markus   1307:                fatal("%s: received bad response to challenge", __func__);
1.1       provos   1308:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1309:
1.33      markus   1310:        xfree(blob);
1.1       provos   1311:        key_free(key);
                   1312:        xfree(response);
                   1313:
                   1314:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1315:
                   1316:        /* reset state */
                   1317:        BN_clear_free(ssh1_challenge);
                   1318:        ssh1_challenge = NULL;
                   1319:        monitor_reset_key_state();
                   1320:
                   1321:        buffer_clear(m);
                   1322:        buffer_put_int(m, success);
1.59      avsm     1323:        mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1.1       provos   1324:
                   1325:        return (success);
                   1326: }
                   1327:
                   1328: int
1.59      avsm     1329: mm_answer_term(int sock, Buffer *req)
1.1       provos   1330: {
1.11      mouring  1331:        extern struct monitor *pmonitor;
1.1       provos   1332:        int res, status;
                   1333:
1.14      markus   1334:        debug3("%s: tearing down sessions", __func__);
1.1       provos   1335:
                   1336:        /* The child is terminating */
                   1337:        session_destroy_all(&mm_session_close);
                   1338:
1.11      mouring  1339:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1340:                if (errno != EINTR)
                   1341:                        exit(1);
1.1       provos   1342:
                   1343:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1344:
                   1345:        /* Terminate process */
1.57      deraadt  1346:        exit(res);
1.1       provos   1347: }
                   1348:
                   1349: void
1.11      mouring  1350: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1351: {
                   1352:        if (compat20) {
                   1353:                set_newkeys(MODE_IN);
                   1354:                set_newkeys(MODE_OUT);
                   1355:        } else {
                   1356:                packet_set_protocol_flags(child_state.ssh1protoflags);
1.15      markus   1357:                packet_set_encryption_key(child_state.ssh1key,
                   1358:                    child_state.ssh1keylen, child_state.ssh1cipher);
                   1359:                xfree(child_state.ssh1key);
1.1       provos   1360:        }
                   1361:
1.15      markus   1362:        /* for rc4 and other stateful ciphers */
1.1       provos   1363:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1364:        xfree(child_state.keyout);
                   1365:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1366:        xfree(child_state.keyin);
                   1367:
                   1368:        if (!compat20) {
                   1369:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1370:                xfree(child_state.ivout);
                   1371:                packet_set_iv(MODE_IN, child_state.ivin);
                   1372:                xfree(child_state.ivin);
                   1373:        }
                   1374:
                   1375:        memcpy(&incoming_stream, &child_state.incoming,
                   1376:            sizeof(incoming_stream));
                   1377:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1378:            sizeof(outgoing_stream));
1.3       markus   1379:
1.1       provos   1380:        /* Update with new address */
1.16      djm      1381:        if (options.compression)
                   1382:                mm_init_compression(pmonitor->m_zlib);
1.1       provos   1383:
                   1384:        /* Network I/O buffers */
                   1385:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1386:        buffer_clear(&input);
                   1387:        buffer_append(&input, child_state.input, child_state.ilen);
                   1388:        memset(child_state.input, 0, child_state.ilen);
                   1389:        xfree(child_state.input);
                   1390:
                   1391:        buffer_clear(&output);
                   1392:        buffer_append(&output, child_state.output, child_state.olen);
                   1393:        memset(child_state.output, 0, child_state.olen);
                   1394:        xfree(child_state.output);
                   1395: }
                   1396:
1.2       markus   1397: static Kex *
1.1       provos   1398: mm_get_kex(Buffer *m)
                   1399: {
                   1400:        Kex *kex;
                   1401:        void *blob;
                   1402:        u_int bloblen;
                   1403:
1.75      djm      1404:        kex = xcalloc(1, sizeof(*kex));
1.1       provos   1405:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13      markus   1406:        if ((session_id2 == NULL) ||
                   1407:            (kex->session_id_len != session_id2_len) ||
                   1408:            (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
                   1409:                fatal("mm_get_get: internal error: bad session id");
1.1       provos   1410:        kex->we_need = buffer_get_int(m);
1.34      markus   1411:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.58      djm      1412:        kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.34      markus   1413:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.69      djm      1414:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.1       provos   1415:        kex->server = 1;
                   1416:        kex->hostkey_type = buffer_get_int(m);
                   1417:        kex->kex_type = buffer_get_int(m);
                   1418:        blob = buffer_get_string(m, &bloblen);
                   1419:        buffer_init(&kex->my);
                   1420:        buffer_append(&kex->my, blob, bloblen);
                   1421:        xfree(blob);
                   1422:        blob = buffer_get_string(m, &bloblen);
                   1423:        buffer_init(&kex->peer);
                   1424:        buffer_append(&kex->peer, blob, bloblen);
                   1425:        xfree(blob);
                   1426:        kex->done = 1;
                   1427:        kex->flags = buffer_get_int(m);
                   1428:        kex->client_version_string = buffer_get_string(m, NULL);
                   1429:        kex->server_version_string = buffer_get_string(m, NULL);
                   1430:        kex->load_host_key=&get_hostkey_by_type;
                   1431:        kex->host_key_index=&get_hostkey_index;
                   1432:
                   1433:        return (kex);
                   1434: }
                   1435:
                   1436: /* This function requries careful sanity checking */
                   1437:
                   1438: void
1.11      mouring  1439: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1440: {
                   1441:        Buffer m;
                   1442:        u_char *blob, *p;
                   1443:        u_int bloblen, plen;
1.37      markus   1444:        u_int32_t seqnr, packets;
                   1445:        u_int64_t blocks;
1.1       provos   1446:
1.14      markus   1447:        debug3("%s: Waiting for new keys", __func__);
1.1       provos   1448:
                   1449:        buffer_init(&m);
1.11      mouring  1450:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1451:        if (!compat20) {
                   1452:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1453:                child_state.ssh1cipher = buffer_get_int(&m);
1.15      markus   1454:                child_state.ssh1key = buffer_get_string(&m,
                   1455:                    &child_state.ssh1keylen);
1.1       provos   1456:                child_state.ivout = buffer_get_string(&m,
                   1457:                    &child_state.ivoutlen);
                   1458:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1459:                goto skip;
                   1460:        } else {
                   1461:                /* Get the Kex for rekeying */
1.11      mouring  1462:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1463:        }
                   1464:
                   1465:        blob = buffer_get_string(&m, &bloblen);
                   1466:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1467:        xfree(blob);
                   1468:
1.14      markus   1469:        debug3("%s: Waiting for second key", __func__);
1.1       provos   1470:        blob = buffer_get_string(&m, &bloblen);
                   1471:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1472:        xfree(blob);
1.3       markus   1473:
1.1       provos   1474:        /* Now get sequence numbers for the packets */
1.37      markus   1475:        seqnr = buffer_get_int(&m);
                   1476:        blocks = buffer_get_int64(&m);
                   1477:        packets = buffer_get_int(&m);
                   1478:        packet_set_state(MODE_OUT, seqnr, blocks, packets);
                   1479:        seqnr = buffer_get_int(&m);
                   1480:        blocks = buffer_get_int64(&m);
                   1481:        packets = buffer_get_int(&m);
                   1482:        packet_set_state(MODE_IN, seqnr, blocks, packets);
1.1       provos   1483:
                   1484:  skip:
                   1485:        /* Get the key context */
                   1486:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1487:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1488:
1.14      markus   1489:        debug3("%s: Getting compression state", __func__);
1.1       provos   1490:        /* Get compression state */
                   1491:        p = buffer_get_string(&m, &plen);
                   1492:        if (plen != sizeof(child_state.outgoing))
1.14      markus   1493:                fatal("%s: bad request size", __func__);
1.1       provos   1494:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1495:        xfree(p);
                   1496:
                   1497:        p = buffer_get_string(&m, &plen);
                   1498:        if (plen != sizeof(child_state.incoming))
1.14      markus   1499:                fatal("%s: bad request size", __func__);
1.1       provos   1500:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1501:        xfree(p);
                   1502:
                   1503:        /* Network I/O buffers */
1.14      markus   1504:        debug3("%s: Getting Network I/O buffers", __func__);
1.1       provos   1505:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1506:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   1507:
                   1508:        buffer_free(&m);
                   1509: }
                   1510:
                   1511:
                   1512: /* Allocation functions for zlib */
                   1513: void *
                   1514: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
                   1515: {
1.30      markus   1516:        size_t len = (size_t) size * ncount;
1.1       provos   1517:        void *address;
                   1518:
1.23      millert  1519:        if (len == 0 || ncount > SIZE_T_MAX / size)
1.18      deraadt  1520:                fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
                   1521:
                   1522:        address = mm_malloc(mm, len);
1.1       provos   1523:
                   1524:        return (address);
                   1525: }
                   1526:
                   1527: void
                   1528: mm_zfree(struct mm_master *mm, void *address)
                   1529: {
                   1530:        mm_free(mm, address);
                   1531: }
                   1532:
                   1533: void
                   1534: mm_init_compression(struct mm_master *mm)
                   1535: {
                   1536:        outgoing_stream.zalloc = (alloc_func)mm_zalloc;
                   1537:        outgoing_stream.zfree = (free_func)mm_zfree;
                   1538:        outgoing_stream.opaque = mm;
                   1539:
                   1540:        incoming_stream.zalloc = (alloc_func)mm_zalloc;
                   1541:        incoming_stream.zfree = (free_func)mm_zfree;
                   1542:        incoming_stream.opaque = mm;
                   1543: }
                   1544:
                   1545: /* XXX */
                   1546:
                   1547: #define FD_CLOSEONEXEC(x) do { \
                   1548:        if (fcntl(x, F_SETFD, 1) == -1) \
                   1549:                fatal("fcntl(%d, F_SETFD)", x); \
                   1550: } while (0)
                   1551:
1.2       markus   1552: static void
1.1       provos   1553: monitor_socketpair(int *pair)
1.3       markus   1554: {
1.1       provos   1555:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14      markus   1556:                fatal("%s: socketpair", __func__);
1.1       provos   1557:        FD_CLOSEONEXEC(pair[0]);
                   1558:        FD_CLOSEONEXEC(pair[1]);
                   1559: }
                   1560:
                   1561: #define MM_MEMSIZE     65536
                   1562:
                   1563: struct monitor *
                   1564: monitor_init(void)
                   1565: {
                   1566:        struct monitor *mon;
                   1567:        int pair[2];
                   1568:
1.75      djm      1569:        mon = xcalloc(1, sizeof(*mon));
1.1       provos   1570:
                   1571:        monitor_socketpair(pair);
                   1572:
                   1573:        mon->m_recvfd = pair[0];
                   1574:        mon->m_sendfd = pair[1];
                   1575:
                   1576:        /* Used to share zlib space across processes */
1.16      djm      1577:        if (options.compression) {
                   1578:                mon->m_zback = mm_create(NULL, MM_MEMSIZE);
                   1579:                mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1       provos   1580:
1.16      djm      1581:                /* Compression needs to share state across borders */
                   1582:                mm_init_compression(mon->m_zlib);
                   1583:        }
1.1       provos   1584:
                   1585:        return mon;
                   1586: }
                   1587:
                   1588: void
                   1589: monitor_reinit(struct monitor *mon)
                   1590: {
                   1591:        int pair[2];
                   1592:
                   1593:        monitor_socketpair(pair);
                   1594:
                   1595:        mon->m_recvfd = pair[0];
                   1596:        mon->m_sendfd = pair[1];
                   1597: }
1.46      markus   1598:
                   1599: #ifdef GSSAPI
                   1600: int
1.59      avsm     1601: mm_answer_gss_setup_ctx(int sock, Buffer *m)
1.46      markus   1602: {
1.59      avsm     1603:        gss_OID_desc goid;
1.46      markus   1604:        OM_uint32 major;
                   1605:        u_int len;
                   1606:
1.59      avsm     1607:        goid.elements = buffer_get_string(m, &len);
                   1608:        goid.length = len;
1.46      markus   1609:
1.59      avsm     1610:        major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46      markus   1611:
1.59      avsm     1612:        xfree(goid.elements);
1.46      markus   1613:
                   1614:        buffer_clear(m);
                   1615:        buffer_put_int(m, major);
                   1616:
1.64      stevesk  1617:        mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46      markus   1618:
                   1619:        /* Now we have a context, enable the step */
                   1620:        monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
                   1621:
                   1622:        return (0);
                   1623: }
                   1624:
                   1625: int
1.59      avsm     1626: mm_answer_gss_accept_ctx(int sock, Buffer *m)
1.46      markus   1627: {
                   1628:        gss_buffer_desc in;
                   1629:        gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64      stevesk  1630:        OM_uint32 major, minor;
1.46      markus   1631:        OM_uint32 flags = 0; /* GSI needs this */
1.47      deraadt  1632:        u_int len;
1.46      markus   1633:
1.47      deraadt  1634:        in.value = buffer_get_string(m, &len);
                   1635:        in.length = len;
1.46      markus   1636:        major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
                   1637:        xfree(in.value);
                   1638:
                   1639:        buffer_clear(m);
                   1640:        buffer_put_int(m, major);
                   1641:        buffer_put_string(m, out.value, out.length);
                   1642:        buffer_put_int(m, flags);
1.59      avsm     1643:        mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46      markus   1644:
                   1645:        gss_release_buffer(&minor, &out);
                   1646:
1.64      stevesk  1647:        if (major == GSS_S_COMPLETE) {
1.46      markus   1648:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
                   1649:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52      markus   1650:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46      markus   1651:        }
                   1652:        return (0);
                   1653: }
                   1654:
                   1655: int
1.59      avsm     1656: mm_answer_gss_checkmic(int sock, Buffer *m)
1.52      markus   1657: {
                   1658:        gss_buffer_desc gssbuf, mic;
                   1659:        OM_uint32 ret;
                   1660:        u_int len;
1.54      djm      1661:
1.52      markus   1662:        gssbuf.value = buffer_get_string(m, &len);
                   1663:        gssbuf.length = len;
                   1664:        mic.value = buffer_get_string(m, &len);
                   1665:        mic.length = len;
1.54      djm      1666:
1.52      markus   1667:        ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54      djm      1668:
1.52      markus   1669:        xfree(gssbuf.value);
                   1670:        xfree(mic.value);
1.54      djm      1671:
1.52      markus   1672:        buffer_clear(m);
                   1673:        buffer_put_int(m, ret);
1.54      djm      1674:
1.59      avsm     1675:        mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54      djm      1676:
1.52      markus   1677:        if (!GSS_ERROR(ret))
                   1678:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54      djm      1679:
1.52      markus   1680:        return (0);
                   1681: }
                   1682:
                   1683: int
1.59      avsm     1684: mm_answer_gss_userok(int sock, Buffer *m)
1.46      markus   1685: {
                   1686:        int authenticated;
                   1687:
                   1688:        authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
                   1689:
                   1690:        buffer_clear(m);
                   1691:        buffer_put_int(m, authenticated);
                   1692:
                   1693:        debug3("%s: sending result %d", __func__, authenticated);
1.59      avsm     1694:        mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46      markus   1695:
1.64      stevesk  1696:        auth_method = "gssapi-with-mic";
1.46      markus   1697:
                   1698:        /* Monitor loop will terminate if authenticated */
                   1699:        return (authenticated);
                   1700: }
                   1701: #endif /* GSSAPI */