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

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