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

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