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

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