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

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