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

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