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

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