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

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