[BACK]Return to monitor.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / crypto / external / bsd / openssh / dist

Annotation of src/crypto/external/bsd/openssh/dist/monitor.c, Revision 1.1.1.22

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

CVSweb <webmaster@jp.NetBSD.org>