[BACK]Return to monitor.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor.c, Revision 1.193

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