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

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