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

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