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

1.224   ! djm         1: /* $OpenBSD: monitor.c,v 1.223 2021/01/27 10:05:28 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.203     djm        83: #include "sk-api.h"
1.189     djm        84:
1.46      markus     85: #ifdef GSSAPI
                     86: static Gssctxt *gsscontext = NULL;
                     87: #endif
                     88:
1.1       provos     89: /* Imports */
                     90: extern ServerOptions options;
                     91: extern u_int utmp_len;
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.224   ! djm       133: static u_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.217     djm       263:                                debug3_f("method %s: partial", auth_method);
1.118     djm       264:                                authenticated = 0;
1.119     djm       265:                                partial = 1;
1.118     djm       266:                        }
                    267:                }
                    268:
1.1       provos    269:                if (authenticated) {
                    270:                        if (!(ent->flags & MON_AUTHDECIDE))
1.217     djm       271:                                fatal_f("unexpected authentication from %d",
                    272:                                    ent->type);
1.1       provos    273:                        if (authctxt->pw->pw_uid == 0 &&
1.180     djm       274:                            !auth_root_allowed(ssh, auth_method))
1.1       provos    275:                                authenticated = 0;
                    276:                }
1.77      dtucker   277:                if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1.190     djm       278:                        auth_log(ssh, authenticated, partial,
1.125     djm       279:                            auth_method, auth_submethod);
1.150     djm       280:                        if (!partial && !authenticated)
1.1       provos    281:                                authctxt->failures++;
1.172     djm       282:                        if (authenticated || partial) {
                    283:                                auth2_update_session_info(authctxt,
                    284:                                    auth_method, auth_submethod);
                    285:                        }
1.1       provos    286:                }
                    287:        }
                    288:
                    289:        if (!authctxt->valid)
1.217     djm       290:                fatal_f("authenticated invalid user");
1.77      dtucker   291:        if (strcmp(auth_method, "unknown") == 0)
1.217     djm       292:                fatal_f("authentication method name unknown");
1.1       provos    293:
1.217     djm       294:        debug_f("user %s authenticated by privileged process", authctxt->user);
1.180     djm       295:        ssh->authctxt = NULL;
1.167     djm       296:        ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.1       provos    297:
1.192     djm       298:        mm_get_keystate(ssh, pmonitor);
1.120     markus    299:
                    300:        /* Drain any buffered messages from the child */
                    301:        while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
                    302:                ;
1.114     djm       303:
1.179     tb        304:        if (pmonitor->m_recvfd >= 0)
                    305:                close(pmonitor->m_recvfd);
                    306:        if (pmonitor->m_log_sendfd >= 0)
                    307:                close(pmonitor->m_log_sendfd);
1.114     djm       308:        pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
1.1       provos    309: }
                    310:
1.40      markus    311: static void
                    312: monitor_set_child_handler(pid_t pid)
                    313: {
                    314:        monitor_child_pid = pid;
                    315: }
                    316:
                    317: static void
1.59      avsm      318: monitor_child_handler(int sig)
1.40      markus    319: {
1.59      avsm      320:        kill(monitor_child_pid, sig);
1.40      markus    321: }
                    322:
1.1       provos    323: void
1.191     djm       324: monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
1.1       provos    325: {
1.114     djm       326:        close(pmonitor->m_recvfd);
                    327:        pmonitor->m_recvfd = -1;
                    328:
1.40      markus    329:        monitor_set_child_handler(pmonitor->m_pid);
1.207     dtucker   330:        ssh_signal(SIGHUP, &monitor_child_handler);
                    331:        ssh_signal(SIGTERM, &monitor_child_handler);
                    332:        ssh_signal(SIGINT, &monitor_child_handler);
1.40      markus    333:
1.162     markus    334:        mon_dispatch = mon_dispatch_postauth20;
                    335:
                    336:        /* Permit requests for moduli and signatures */
                    337:        monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    338:        monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    339:        monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
1.1       provos    340:
1.180     djm       341:        if (auth_opts->permit_pty_flag) {
1.1       provos    342:                monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
                    343:                monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
                    344:        }
                    345:
                    346:        for (;;)
1.191     djm       347:                monitor_read(ssh, pmonitor, mon_dispatch, NULL);
1.1       provos    348: }
                    349:
1.114     djm       350: static int
                    351: monitor_read_log(struct monitor *pmonitor)
                    352: {
1.183     markus    353:        struct sshbuf *logmsg;
1.215     djm       354:        u_int len, level, line;
                    355:        char *msg, *file, *func;
1.183     markus    356:        u_char *p;
                    357:        int r;
1.114     djm       358:
1.183     markus    359:        if ((logmsg = sshbuf_new()) == NULL)
1.217     djm       360:                fatal_f("sshbuf_new");
1.114     djm       361:
                    362:        /* Read length */
1.183     markus    363:        if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
1.217     djm       364:                fatal_fr(r, "reserve len");
1.183     markus    365:        if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
1.114     djm       366:                if (errno == EPIPE) {
1.183     markus    367:                        sshbuf_free(logmsg);
1.217     djm       368:                        debug_f("child log fd closed");
1.114     djm       369:                        close(pmonitor->m_log_recvfd);
                    370:                        pmonitor->m_log_recvfd = -1;
                    371:                        return -1;
                    372:                }
1.217     djm       373:                fatal_f("log fd read: %s", strerror(errno));
1.114     djm       374:        }
1.183     markus    375:        if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
1.217     djm       376:                fatal_fr(r, "parse len");
1.114     djm       377:        if (len <= 4 || len > 8192)
1.217     djm       378:                fatal_f("invalid log message length %u", len);
1.114     djm       379:
                    380:        /* Read severity, message */
1.183     markus    381:        sshbuf_reset(logmsg);
                    382:        if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
1.217     djm       383:                fatal_fr(r, "reserve msg");
1.183     markus    384:        if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
1.217     djm       385:                fatal_f("log fd read: %s", strerror(errno));
1.215     djm       386:        if ((r = sshbuf_get_cstring(logmsg, &file, NULL)) != 0 ||
                    387:            (r = sshbuf_get_cstring(logmsg, &func, NULL)) != 0 ||
                    388:            (r = sshbuf_get_u32(logmsg, &line)) != 0 ||
                    389:            (r = sshbuf_get_u32(logmsg, &level)) != 0 ||
1.183     markus    390:            (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
1.217     djm       391:                fatal_fr(r, "parse");
1.114     djm       392:
                    393:        /* Log it */
                    394:        if (log_level_name(level) == NULL)
1.216     djm       395:                fatal_f("invalid log level %u (corrupted message?)", level);
                    396:        sshlog(file, func, line, 0, level, NULL, "%s [preauth]", msg);
1.114     djm       397:
1.183     markus    398:        sshbuf_free(logmsg);
1.215     djm       399:        free(file);
                    400:        free(func);
1.124     djm       401:        free(msg);
1.114     djm       402:
                    403:        return 0;
                    404: }
                    405:
1.191     djm       406: static int
                    407: monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
1.1       provos    408:     struct mon_table **pent)
                    409: {
1.183     markus    410:        struct sshbuf *m;
                    411:        int r, ret;
1.1       provos    412:        u_char type;
1.114     djm       413:        struct pollfd pfd[2];
                    414:
                    415:        for (;;) {
1.130     tedu      416:                memset(&pfd, 0, sizeof(pfd));
1.114     djm       417:                pfd[0].fd = pmonitor->m_sendfd;
                    418:                pfd[0].events = POLLIN;
                    419:                pfd[1].fd = pmonitor->m_log_recvfd;
                    420:                pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
1.115     djm       421:                if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
                    422:                        if (errno == EINTR || errno == EAGAIN)
                    423:                                continue;
1.217     djm       424:                        fatal_f("poll: %s", strerror(errno));
1.115     djm       425:                }
1.114     djm       426:                if (pfd[1].revents) {
                    427:                        /*
                    428:                         * Drain all log messages before processing next
                    429:                         * monitor request.
                    430:                         */
                    431:                        monitor_read_log(pmonitor);
                    432:                        continue;
                    433:                }
                    434:                if (pfd[0].revents)
                    435:                        break;  /* Continues below */
                    436:        }
1.1       provos    437:
1.183     markus    438:        if ((m = sshbuf_new()) == NULL)
1.217     djm       439:                fatal_f("sshbuf_new");
1.1       provos    440:
1.183     markus    441:        mm_request_receive(pmonitor->m_sendfd, m);
                    442:        if ((r = sshbuf_get_u8(m, &type)) != 0)
1.217     djm       443:                fatal_fr(r, "parse type");
1.1       provos    444:
1.217     djm       445:        debug3_f("checking request %d", type);
1.1       provos    446:
                    447:        while (ent->f != NULL) {
                    448:                if (ent->type == type)
                    449:                        break;
                    450:                ent++;
                    451:        }
                    452:
                    453:        if (ent->f != NULL) {
                    454:                if (!(ent->flags & MON_PERMIT))
1.217     djm       455:                        fatal_f("unpermitted request %d", type);
1.191     djm       456:                ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
1.183     markus    457:                sshbuf_free(m);
1.1       provos    458:
                    459:                /* The child may use this request only once, disable it */
                    460:                if (ent->flags & MON_ONCE) {
1.217     djm       461:                        debug2_f("%d used once, disabling now", type);
1.1       provos    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.217     djm       471:        fatal_f("unsupported request: %d", type);
1.1       provos    472:
                    473:        /* NOTREACHED */
                    474:        return (-1);
                    475: }
                    476:
                    477: /* allowed key state */
                    478: static int
1.203     djm       479: monitor_allowed_key(const u_char *blob, u_int bloblen)
1.1       provos    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)
1.217     djm       516:                fatal_fr(r, "parse");
1.1       provos    517:
1.217     djm       518:        debug3_f("got parameters: %d %d %d", min, want, max);
1.1       provos    519:        /* We need to check here, too, in case the child got corrupted */
                    520:        if (max < min || want < min || max < want)
1.217     djm       521:                fatal_f("bad parameters: %d %d %d", min, want, max);
1.1       provos    522:
1.183     markus    523:        sshbuf_reset(m);
1.1       provos    524:
                    525:        dh = choose_dh(min, want, max);
                    526:        if (dh == NULL) {
1.183     markus    527:                if ((r = sshbuf_put_u8(m, 0)) != 0)
1.217     djm       528:                        fatal_fr(r, "assemble empty");
1.1       provos    529:                return (0);
                    530:        } else {
                    531:                /* Send first bignum */
1.187     djm       532:                DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
1.183     markus    533:                if ((r = sshbuf_put_u8(m, 1)) != 0 ||
1.187     djm       534:                    (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
                    535:                    (r = sshbuf_put_bignum2(m, dh_g)) != 0)
1.217     djm       536:                        fatal_fr(r, "assemble");
1.3       markus    537:
1.1       provos    538:                DH_free(dh);
                    539:        }
1.59      avsm      540:        mm_request_send(sock, MONITOR_ANS_MODULI, m);
1.1       provos    541:        return (0);
                    542: }
1.132     markus    543: #endif
1.1       provos    544:
                    545: int
1.191     djm       546: mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos    547: {
1.138     djm       548:        extern int auth_sock;                   /* XXX move to state struct? */
                    549:        struct sshkey *key;
1.157     djm       550:        struct sshbuf *sigbuf = NULL;
                    551:        u_char *p = NULL, *signature = NULL;
                    552:        char *alg = NULL;
1.155     markus    553:        size_t datlen, siglen, alglen;
1.159     djm       554:        int r, is_proof = 0;
1.185     markus    555:        u_int keyid, compat;
1.145     djm       556:        const char proof_req[] = "hostkeys-prove-00@openssh.com";
1.3       markus    557:
1.217     djm       558:        debug3_f("entering");
1.1       provos    559:
1.138     djm       560:        if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
1.155     markus    561:            (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
1.185     markus    562:            (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
                    563:            (r = sshbuf_get_u32(m, &compat)) != 0)
1.217     djm       564:                fatal_fr(r, "parse");
1.159     djm       565:        if (keyid > INT_MAX)
1.217     djm       566:                fatal_f("invalid key ID");
1.1       provos    567:
1.69      djm       568:        /*
1.110     djm       569:         * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
                    570:         * SHA384 (48 bytes) and SHA512 (64 bytes).
1.144     djm       571:         *
                    572:         * Otherwise, verify the signature request is for a hostkey
                    573:         * proof.
                    574:         *
                    575:         * XXX perform similar check for KEX signature requests too?
                    576:         * it's not trivial, since what is signed is the hash, rather
                    577:         * than the full kex structure...
1.69      djm       578:         */
1.144     djm       579:        if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
                    580:                /*
                    581:                 * Construct expected hostkey proof and compare it to what
                    582:                 * the client sent us.
                    583:                 */
                    584:                if (session_id2_len == 0) /* hostkeys is never first */
1.217     djm       585:                        fatal_f("bad data length: %zu", datlen);
1.144     djm       586:                if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
1.217     djm       587:                        fatal_f("no hostkey for index %d", keyid);
1.144     djm       588:                if ((sigbuf = sshbuf_new()) == NULL)
1.217     djm       589:                        fatal_f("sshbuf_new");
1.145     djm       590:                if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
                    591:                    (r = sshbuf_put_string(sigbuf, session_id2,
1.152     jsg       592:                    session_id2_len)) != 0 ||
1.144     djm       593:                    (r = sshkey_puts(key, sigbuf)) != 0)
1.217     djm       594:                        fatal_fr(r, "assemble private key proof");
1.144     djm       595:                if (datlen != sshbuf_len(sigbuf) ||
                    596:                    memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
1.217     djm       597:                        fatal_f("bad data length: %zu, hostkey proof len %zu",
                    598:                            datlen, sshbuf_len(sigbuf));
1.144     djm       599:                sshbuf_free(sigbuf);
                    600:                is_proof = 1;
                    601:        }
1.1       provos    602:
1.13      markus    603:        /* save session id, it will be passed on the first call */
                    604:        if (session_id2_len == 0) {
                    605:                session_id2_len = datlen;
                    606:                session_id2 = xmalloc(session_id2_len);
                    607:                memcpy(session_id2, p, session_id2_len);
                    608:        }
                    609:
1.127     markus    610:        if ((key = get_hostkey_by_index(keyid)) != NULL) {
1.155     markus    611:                if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
1.213     djm       612:                    options.sk_provider, NULL, compat)) != 0)
1.217     djm       613:                        fatal_fr(r, "sign");
1.144     djm       614:        } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
1.138     djm       615:            auth_sock > 0) {
                    616:                if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
1.217     djm       617:                    p, datlen, alg, compat)) != 0)
                    618:                        fatal_fr(r, "agent sign");
1.127     markus    619:        } else
1.217     djm       620:                fatal_f("no hostkey from index %d", keyid);
1.1       provos    621:
1.217     djm       622:        debug3_f("%s signature %p(%zu)", is_proof ? "hostkey proof" : "KEX",
                    623:            signature, siglen);
1.1       provos    624:
1.138     djm       625:        sshbuf_reset(m);
                    626:        if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
1.217     djm       627:                fatal_fr(r, "assemble");
1.1       provos    628:
1.157     djm       629:        free(alg);
1.124     djm       630:        free(p);
                    631:        free(signature);
1.1       provos    632:
1.59      avsm      633:        mm_request_send(sock, MONITOR_ANS_SIGN, m);
1.1       provos    634:
                    635:        /* Turn on permissions for getpwnam */
                    636:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                    637:
                    638:        return (0);
                    639: }
                    640:
1.218     djm       641: #define PUTPW(b, id) \
                    642:        do { \
                    643:                if ((r = sshbuf_put_string(b, \
                    644:                    &pwent->id, sizeof(pwent->id))) != 0) \
                    645:                        fatal_fr(r, "assemble %s", #id); \
                    646:        } while (0)
                    647:
1.1       provos    648: /* Retrieves the password entry and also checks if the user is permitted */
                    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.217     djm       657:        debug3_f("entering");
1.1       provos    658:
                    659:        if (authctxt->attempt++ != 0)
1.217     djm       660:                fatal_f("multiple attempts for getpwnam");
1.1       provos    661:
1.183     markus    662:        if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
1.217     djm       663:                fatal_fr(r, "parse");
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)
1.217     djm       675:                        fatal_fr(r, "assemble fakepw");
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.218     djm       684:        /* XXX send fake class/dir/shell, etc. */
                    685:        if ((r = sshbuf_put_u8(m, 1)) != 0)
                    686:                fatal_fr(r, "assemble ok");
                    687:        PUTPW(m, pw_uid);
                    688:        PUTPW(m, pw_gid);
                    689:        PUTPW(m, pw_change);
                    690:        PUTPW(m, pw_expire);
                    691:        if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
1.183     markus    692:            (r = sshbuf_put_cstring(m, "*")) != 0 ||
                    693:            (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
                    694:            (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
                    695:            (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
                    696:            (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
1.217     djm       697:                fatal_fr(r, "assemble pw");
1.94      dtucker   698:
                    699:  out:
1.167     djm       700:        ssh_packet_set_log_preamble(ssh, "%suser %s",
                    701:            authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
1.183     markus    702:        if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
1.217     djm       703:                fatal_fr(r, "assemble options");
1.112     djm       704:
                    705: #define M_CP_STROPT(x) do { \
1.217     djm       706:                if (options.x != NULL && \
                    707:                    (r = sshbuf_put_cstring(m, options.x)) != 0) \
                    708:                        fatal_fr(r, "assemble %s", #x); \
1.112     djm       709:        } while (0)
1.113     djm       710: #define M_CP_STRARRAYOPT(x, nx) do { \
1.183     markus    711:                for (i = 0; i < options.nx; i++) { \
                    712:                        if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
1.217     djm       713:                                fatal_fr(r, "assemble %s", #x); \
1.183     markus    714:                } \
1.113     djm       715:        } while (0)
1.112     djm       716:        /* See comment in servconf.h */
                    717:        COPY_MATCH_STRING_OPTS();
                    718: #undef M_CP_STROPT
1.113     djm       719: #undef M_CP_STRARRAYOPT
1.118     djm       720:
                    721:        /* Create valid auth method lists */
1.162     markus    722:        if (auth2_setup_methods_lists(authctxt) != 0) {
1.118     djm       723:                /*
                    724:                 * The monitor will continue long enough to let the child
                    725:                 * run to it's packet_disconnect(), but it must not allow any
                    726:                 * authentication to succeed.
                    727:                 */
1.217     djm       728:                debug_f("no valid authentication method lists");
1.118     djm       729:        }
                    730:
1.217     djm       731:        debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
1.59      avsm      732:        mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1       provos    733:
1.162     markus    734:        /* Allow service/style information on the auth context */
                    735:        monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
                    736:        monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
1.10      djm       737:
                    738:        return (0);
                    739: }
                    740:
1.191     djm       741: int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
1.10      djm       742: {
                    743:        char *banner;
1.183     markus    744:        int r;
1.10      djm       745:
1.183     markus    746:        sshbuf_reset(m);
1.10      djm       747:        banner = auth2_read_banner();
1.183     markus    748:        if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
1.217     djm       749:                fatal_fr(r, "assemble");
1.59      avsm      750:        mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.124     djm       751:        free(banner);
1.1       provos    752:
                    753:        return (0);
                    754: }
                    755:
                    756: int
1.191     djm       757: mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos    758: {
1.183     markus    759:        int r;
                    760:
1.1       provos    761:        monitor_permit_authentications(1);
                    762:
1.183     markus    763:        if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
                    764:            (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
1.217     djm       765:                fatal_fr(r, "parse");
                    766:        debug3_f("service=%s, style=%s", 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)
1.217     djm       785:                fatal_f("password authentication not enabled");
1.183     markus    786:        if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
1.217     djm       787:                fatal_fr(r, "parse");
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.209     jsg       791:        freezero(passwd, plen);
1.1       provos    792:
1.183     markus    793:        sshbuf_reset(m);
                    794:        if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1.217     djm       795:                fatal_fr(r, "assemble");
1.1       provos    796:
1.217     djm       797:        debug3_f("sending result %d", authenticated);
1.59      avsm      798:        mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1       provos    799:
                    800:        call_count++;
                    801:        if (plen == 0 && call_count == 1)
                    802:                auth_method = "none";
                    803:        else
                    804:                auth_method = "password";
                    805:
                    806:        /* Causes monitor loop to terminate if authenticated */
                    807:        return (authenticated);
                    808: }
                    809:
                    810: int
1.191     djm       811: mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos    812: {
                    813:        char *name, *infotxt;
1.183     markus    814:        u_int numprompts, *echo_on, success;
1.1       provos    815:        char **prompts;
1.183     markus    816:        int r;
1.1       provos    817:
1.164     djm       818:        if (!options.kbd_interactive_authentication)
1.217     djm       819:                fatal_f("kbd-int authentication not enabled");
1.31      markus    820:        success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    821:            &prompts, &echo_on) < 0 ? 0 : 1;
1.1       provos    822:
1.183     markus    823:        sshbuf_reset(m);
                    824:        if ((r = sshbuf_put_u32(m, success)) != 0)
1.217     djm       825:                fatal_fr(r, "assemble");
1.183     markus    826:        if (success) {
                    827:                if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
1.217     djm       828:                        fatal_fr(r, "assemble prompt");
1.183     markus    829:        }
1.1       provos    830:
1.217     djm       831:        debug3_f("sending challenge success: %u", success);
1.59      avsm      832:        mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1.1       provos    833:
1.31      markus    834:        if (success) {
1.124     djm       835:                free(name);
                    836:                free(infotxt);
                    837:                free(prompts);
                    838:                free(echo_on);
1.1       provos    839:        }
                    840:
                    841:        return (0);
                    842: }
                    843:
                    844: int
1.191     djm       845: mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos    846: {
                    847:        char *response;
1.183     markus    848:        int r, authok;
1.1       provos    849:
1.164     djm       850:        if (!options.kbd_interactive_authentication)
1.217     djm       851:                fatal_f("kbd-int authentication not enabled");
1.154     mmcc      852:        if (authctxt->as == NULL)
1.217     djm       853:                fatal_f("no bsd auth session");
1.1       provos    854:
1.183     markus    855:        if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
1.217     djm       856:                fatal_fr(r, "parse");
1.12      markus    857:        authok = options.challenge_response_authentication &&
                    858:            auth_userresponse(authctxt->as, response, 0);
1.1       provos    859:        authctxt->as = NULL;
1.217     djm       860:        debug3_f("<%s> = <%d>", response, authok);
1.124     djm       861:        free(response);
1.1       provos    862:
1.183     markus    863:        sshbuf_reset(m);
                    864:        if ((r = sshbuf_put_u32(m, authok)) != 0)
1.217     djm       865:                fatal_fr(r, "assemble");
1.1       provos    866:
1.217     djm       867:        debug3_f("sending authenticated: %d", authok);
1.59      avsm      868:        mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1       provos    869:
1.162     markus    870:        auth_method = "keyboard-interactive";
                    871:        auth_submethod = "bsdauth";
1.1       provos    872:
                    873:        return (authok != 0);
                    874: }
                    875:
1.188     djm       876: /*
                    877:  * Check that the key type appears in the supplied pattern list, ignoring
                    878:  * mismatches in the signature algorithm. (Signature algorithm checks are
                    879:  * performed in the unprivileged authentication code).
                    880:  * Returns 1 on success, 0 otherwise.
                    881:  */
                    882: static int
                    883: key_base_type_match(const char *method, const struct sshkey *key,
                    884:     const char *list)
                    885: {
                    886:        char *s, *l, *ol = xstrdup(list);
                    887:        int found = 0;
                    888:
                    889:        l = ol;
                    890:        for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
                    891:                if (sshkey_type_from_name(s) == key->type) {
                    892:                        found = 1;
                    893:                        break;
                    894:                }
                    895:        }
                    896:        if (!found) {
                    897:                error("%s key type %s is not in permitted list %s", method,
                    898:                    sshkey_ssh_name(key), list);
                    899:        }
                    900:
                    901:        free(ol);
                    902:        return found;
                    903: }
                    904:
1.1       provos    905: int
1.191     djm       906: mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos    907: {
1.183     markus    908:        struct sshkey *key = NULL;
1.26      markus    909:        char *cuser, *chost;
1.183     markus    910:        u_int pubkey_auth_attempt;
1.224   ! djm       911:        u_int type = 0;
1.180     djm       912:        int r, allowed = 0;
                    913:        struct sshauthopt *opts = NULL;
1.1       provos    914:
1.217     djm       915:        debug3_f("entering");
1.183     markus    916:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                    917:            (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
                    918:            (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
                    919:            (r = sshkey_froms(m, &key)) != 0 ||
                    920:            (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1.217     djm       921:                fatal_fr(r, "parse");
1.1       provos    922:
1.217     djm       923:        debug3_f("key_from_blob: %p", key);
1.1       provos    924:
1.51      djm       925:        if (key != NULL && authctxt->valid) {
1.137     djm       926:                /* These should not make it past the privsep child */
1.185     markus    927:                if (sshkey_type_plain(key->type) == KEY_RSA &&
1.222     djm       928:                    (ssh->compat & SSH_BUG_RSASIGMD5) != 0)
1.217     djm       929:                        fatal_f("passed a SSH_BUG_RSASIGMD5 key");
1.137     djm       930:
1.63      deraadt   931:                switch (type) {
1.1       provos    932:                case MM_USERKEY:
1.77      dtucker   933:                        auth_method = "publickey";
1.180     djm       934:                        if (!options.pubkey_authentication)
                    935:                                break;
                    936:                        if (auth2_key_already_used(authctxt, key))
                    937:                                break;
1.188     djm       938:                        if (!key_base_type_match(auth_method, key,
1.220     dtucker   939:                            options.pubkey_accepted_algos))
1.180     djm       940:                                break;
                    941:                        allowed = user_key_allowed(ssh, authctxt->pw, key,
                    942:                            pubkey_auth_attempt, &opts);
1.1       provos    943:                        break;
                    944:                case MM_HOSTKEY:
1.180     djm       945:                        auth_method = "hostbased";
                    946:                        if (!options.hostbased_authentication)
                    947:                                break;
                    948:                        if (auth2_key_already_used(authctxt, key))
                    949:                                break;
1.188     djm       950:                        if (!key_base_type_match(auth_method, key,
1.221     dtucker   951:                            options.hostbased_accepted_algos))
1.180     djm       952:                                break;
1.192     djm       953:                        allowed = hostbased_key_allowed(ssh, authctxt->pw,
1.1       provos    954:                            cuser, chost, key);
1.172     djm       955:                        auth2_record_info(authctxt,
1.126     djm       956:                            "client user \"%.100s\", client host \"%.100s\"",
                    957:                            cuser, chost);
1.1       provos    958:                        break;
                    959:                default:
1.224   ! djm       960:                        fatal_f("unknown key type %u", type);
1.1       provos    961:                        break;
                    962:                }
1.33      markus    963:        }
1.161     djm       964:
1.217     djm       965:        debug3_f("%s authentication%s: %s key is %s", auth_method,
                    966:            pubkey_auth_attempt ? "" : " test",
1.180     djm       967:            (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
                    968:            allowed ? "allowed" : "not allowed");
1.161     djm       969:
1.172     djm       970:        auth2_record_key(authctxt, 0, key);
1.1       provos    971:
                    972:        /* clear temporarily storage (used by verify) */
                    973:        monitor_reset_key_state();
                    974:
                    975:        if (allowed) {
                    976:                /* Save temporarily for comparison in verify */
1.183     markus    977:                if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1.217     djm       978:                        fatal_fr(r, "sshkey_to_blob");
1.1       provos    979:                key_blobtype = type;
1.180     djm       980:                key_opts = opts;
1.1       provos    981:                hostbased_cuser = cuser;
                    982:                hostbased_chost = chost;
1.72      djm       983:        } else {
1.77      dtucker   984:                /* Log failed attempt */
1.190     djm       985:                auth_log(ssh, 0, 0, auth_method, NULL);
1.124     djm       986:                free(cuser);
                    987:                free(chost);
1.1       provos    988:        }
1.183     markus    989:        sshkey_free(key);
1.1       provos    990:
1.183     markus    991:        sshbuf_reset(m);
                    992:        if ((r = sshbuf_put_u32(m, allowed)) != 0)
1.217     djm       993:                fatal_fr(r, "assemble");
1.180     djm       994:        if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1.217     djm       995:                fatal_fr(r, "sshauthopt_serialise");
1.180     djm       996:        mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1       provos    997:
1.180     djm       998:        if (!allowed)
                    999:                sshauthopt_free(opts);
1.1       provos   1000:
                   1001:        return (0);
                   1002: }
                   1003:
                   1004: static int
1.222     djm      1005: monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
1.1       provos   1006: {
1.183     markus   1007:        struct sshbuf *b;
                   1008:        const u_char *p;
1.159     djm      1009:        char *userstyle, *cp;
1.183     markus   1010:        size_t len;
                   1011:        u_char type;
                   1012:        int r, fail = 0;
1.1       provos   1013:
1.203     djm      1014:        if ((b = sshbuf_from(data, datalen)) == NULL)
1.217     djm      1015:                fatal_f("sshbuf_from");
1.3       markus   1016:
1.222     djm      1017:        if (ssh->compat & SSH_OLD_SESSIONID) {
1.183     markus   1018:                p = sshbuf_ptr(b);
                   1019:                len = sshbuf_len(b);
1.13      markus   1020:                if ((session_id2 == NULL) ||
                   1021:                    (len < session_id2_len) ||
1.108     djm      1022:                    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.13      markus   1023:                        fail++;
1.183     markus   1024:                if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1.217     djm      1025:                        fatal_fr(r, "consume");
1.1       provos   1026:        } else {
1.183     markus   1027:                if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1.217     djm      1028:                        fatal_fr(r, "parse sessionid");
1.13      markus   1029:                if ((session_id2 == NULL) ||
                   1030:                    (len != session_id2_len) ||
1.108     djm      1031:                    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1       provos   1032:                        fail++;
                   1033:        }
1.183     markus   1034:        if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217     djm      1035:                fatal_fr(r, "parse type");
1.183     markus   1036:        if (type != SSH2_MSG_USERAUTH_REQUEST)
1.1       provos   1037:                fail++;
1.183     markus   1038:        if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1039:                fatal_fr(r, "parse userstyle");
1.121     djm      1040:        xasprintf(&userstyle, "%s%s%s", authctxt->user,
                   1041:            authctxt->style ? ":" : "",
                   1042:            authctxt->style ? authctxt->style : "");
1.159     djm      1043:        if (strcmp(userstyle, cp) != 0) {
                   1044:                logit("wrong user name passed to monitor: "
                   1045:                    "expected %s != %.100s", userstyle, cp);
1.1       provos   1046:                fail++;
                   1047:        }
1.124     djm      1048:        free(userstyle);
1.159     djm      1049:        free(cp);
1.183     markus   1050:        if ((r = sshbuf_skip_string(b)) != 0 || /* service */
                   1051:            (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1052:                fatal_fr(r, "parse method");
1.178     djm      1053:        if (strcmp("publickey", cp) != 0)
                   1054:                fail++;
                   1055:        free(cp);
1.183     markus   1056:        if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217     djm      1057:                fatal_fr(r, "parse pktype");
1.183     markus   1058:        if (type == 0)
1.178     djm      1059:                fail++;
1.183     markus   1060:        if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
                   1061:            (r = sshbuf_skip_string(b)) != 0)   /* pkblob */
1.217     djm      1062:                fatal_fr(r, "parse pk");
1.183     markus   1063:        if (sshbuf_len(b) != 0)
1.1       provos   1064:                fail++;
1.183     markus   1065:        sshbuf_free(b);
1.1       provos   1066:        return (fail == 0);
                   1067: }
                   1068:
                   1069: static int
1.203     djm      1070: monitor_valid_hostbasedblob(const u_char *data, u_int datalen,
                   1071:     const char *cuser, const char *chost)
1.1       provos   1072: {
1.183     markus   1073:        struct sshbuf *b;
                   1074:        const u_char *p;
                   1075:        char *cp, *userstyle;
                   1076:        size_t len;
                   1077:        int r, fail = 0;
                   1078:        u_char type;
1.1       provos   1079:
1.203     djm      1080:        if ((b = sshbuf_from(data, datalen)) == NULL)
1.217     djm      1081:                fatal_f("sshbuf_new");
1.203     djm      1082:        if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1.217     djm      1083:                fatal_fr(r, "parse sessionid");
1.3       markus   1084:
1.13      markus   1085:        if ((session_id2 == NULL) ||
                   1086:            (len != session_id2_len) ||
1.108     djm      1087:            (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1.1       provos   1088:                fail++;
1.13      markus   1089:
1.183     markus   1090:        if ((r = sshbuf_get_u8(b, &type)) != 0)
1.217     djm      1091:                fatal_fr(r, "parse type");
1.183     markus   1092:        if (type != SSH2_MSG_USERAUTH_REQUEST)
1.1       provos   1093:                fail++;
1.183     markus   1094:        if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1095:                fatal_fr(r, "parse userstyle");
1.121     djm      1096:        xasprintf(&userstyle, "%s%s%s", authctxt->user,
                   1097:            authctxt->style ? ":" : "",
                   1098:            authctxt->style ? authctxt->style : "");
1.183     markus   1099:        if (strcmp(userstyle, cp) != 0) {
                   1100:                logit("wrong user name passed to monitor: "
                   1101:                    "expected %s != %.100s", userstyle, cp);
1.1       provos   1102:                fail++;
                   1103:        }
1.121     djm      1104:        free(userstyle);
1.183     markus   1105:        free(cp);
                   1106:        if ((r = sshbuf_skip_string(b)) != 0 || /* service */
                   1107:            (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1108:                fatal_fr(r, "parse method");
1.183     markus   1109:        if (strcmp(cp, "hostbased") != 0)
1.1       provos   1110:                fail++;
1.183     markus   1111:        free(cp);
                   1112:        if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
                   1113:            (r = sshbuf_skip_string(b)) != 0)   /* pkblob */
1.217     djm      1114:                fatal_fr(r, "parse pk");
1.1       provos   1115:
                   1116:        /* verify client host, strip trailing dot if necessary */
1.183     markus   1117:        if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1118:                fatal_fr(r, "parse host");
1.183     markus   1119:        if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
                   1120:                cp[len - 1] = '\0';
                   1121:        if (strcmp(cp, chost) != 0)
1.1       provos   1122:                fail++;
1.183     markus   1123:        free(cp);
1.1       provos   1124:
                   1125:        /* verify client user */
1.183     markus   1126:        if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1.217     djm      1127:                fatal_fr(r, "parse ruser");
1.183     markus   1128:        if (strcmp(cp, cuser) != 0)
1.1       provos   1129:                fail++;
1.183     markus   1130:        free(cp);
1.1       provos   1131:
1.183     markus   1132:        if (sshbuf_len(b) != 0)
1.1       provos   1133:                fail++;
1.183     markus   1134:        sshbuf_free(b);
1.1       provos   1135:        return (fail == 0);
                   1136: }
                   1137:
                   1138: int
1.191     djm      1139: mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos   1140: {
1.168     markus   1141:        struct sshkey *key;
1.203     djm      1142:        const u_char *signature, *data, *blob;
                   1143:        char *sigalg = NULL, *fp = NULL;
1.169     markus   1144:        size_t signaturelen, datalen, bloblen;
1.214     djm      1145:        int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
                   1146:        int encoded_ret;
1.202     djm      1147:        struct sshkey_sig_details *sig_details = NULL;
1.1       provos   1148:
1.203     djm      1149:        if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
                   1150:            (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
                   1151:            (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
1.176     djm      1152:            (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1.217     djm      1153:                fatal_fr(r, "parse");
1.1       provos   1154:
                   1155:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring  1156:          !monitor_allowed_key(blob, bloblen))
1.217     djm      1157:                fatal_f("bad key, not previously allowed");
1.1       provos   1158:
1.177     djm      1159:        /* Empty signature algorithm means NULL. */
                   1160:        if (*sigalg == '\0') {
                   1161:                free(sigalg);
                   1162:                sigalg = NULL;
                   1163:        }
                   1164:
1.169     markus   1165:        /* XXX use sshkey_froms here; need to change key_blob, etc. */
                   1166:        if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1.217     djm      1167:                fatal_fr(r, "parse key");
1.1       provos   1168:
                   1169:        switch (key_blobtype) {
                   1170:        case MM_USERKEY:
1.222     djm      1171:                valid_data = monitor_valid_userblob(ssh, data, datalen);
1.172     djm      1172:                auth_method = "publickey";
1.1       provos   1173:                break;
                   1174:        case MM_HOSTKEY:
                   1175:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                   1176:                    hostbased_cuser, hostbased_chost);
1.172     djm      1177:                auth_method = "hostbased";
1.1       provos   1178:                break;
                   1179:        default:
                   1180:                valid_data = 0;
                   1181:                break;
                   1182:        }
                   1183:        if (!valid_data)
1.223     djm      1184:                fatal_f("bad %s signature data blob",
                   1185:                    key_blobtype == MM_USERKEY ? "userkey" :
                   1186:                    (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
1.1       provos   1187:
1.203     djm      1188:        if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
                   1189:            SSH_FP_DEFAULT)) == NULL)
1.217     djm      1190:                fatal_f("sshkey_fingerprint failed");
1.203     djm      1191:
1.169     markus   1192:        ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1.202     djm      1193:            sigalg, ssh->compat, &sig_details);
1.217     djm      1194:        debug3_f("%s %p signature %s%s%s", auth_method, key,
1.201     djm      1195:            (ret == 0) ? "verified" : "unverified",
                   1196:            (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
1.203     djm      1197:
                   1198:        if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
                   1199:                req_presence = (options.pubkey_auth_options &
1.204     djm      1200:                    PUBKEYAUTH_TOUCH_REQUIRED) ||
                   1201:                    !key_opts->no_require_user_presence;
1.203     djm      1202:                if (req_presence &&
                   1203:                    (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
                   1204:                        error("public key %s %s signature for %s%s from %.128s "
1.208     naddy    1205:                            "port %d rejected: user presence "
                   1206:                            "(authenticator touch) requirement not met ",
                   1207:                            sshkey_type(key), fp,
1.214     djm      1208:                            authctxt->valid ? "" : "invalid user ",
                   1209:                            authctxt->user, ssh_remote_ipaddr(ssh),
                   1210:                            ssh_remote_port(ssh));
                   1211:                        ret = SSH_ERR_SIGNATURE_INVALID;
                   1212:                }
                   1213:                req_verify = (options.pubkey_auth_options &
                   1214:                    PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
                   1215:                if (req_verify &&
                   1216:                    (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
                   1217:                        error("public key %s %s signature for %s%s from %.128s "
                   1218:                            "port %d rejected: user verification requirement "
                   1219:                            "not met ", sshkey_type(key), fp,
1.203     djm      1220:                            authctxt->valid ? "" : "invalid user ",
                   1221:                            authctxt->user, ssh_remote_ipaddr(ssh),
                   1222:                            ssh_remote_port(ssh));
                   1223:                        ret = SSH_ERR_SIGNATURE_INVALID;
                   1224:                }
                   1225:        }
1.172     djm      1226:        auth2_record_key(authctxt, ret == 0, key);
1.136     djm      1227:
1.180     djm      1228:        if (key_blobtype == MM_USERKEY)
                   1229:                auth_activate_options(ssh, key_opts);
1.1       provos   1230:        monitor_reset_key_state();
1.3       markus   1231:
1.169     markus   1232:        sshbuf_reset(m);
                   1233:
                   1234:        /* encode ret != 0 as positive integer, since we're sending u32 */
                   1235:        encoded_ret = (ret != 0);
1.202     djm      1236:        if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
1.205     djm      1237:            (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
1.217     djm      1238:                fatal_fr(r, "assemble");
1.202     djm      1239:        if (sig_details != NULL) {
                   1240:                if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
                   1241:                    (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
1.217     djm      1242:                        fatal_fr(r, "assemble sk");
1.202     djm      1243:        }
                   1244:        sshkey_sig_details_free(sig_details);
1.59      avsm     1245:        mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.203     djm      1246:
                   1247:        free(sigalg);
                   1248:        free(fp);
                   1249:        sshkey_free(key);
1.1       provos   1250:
1.169     markus   1251:        return ret == 0;
1.1       provos   1252: }
                   1253:
1.2       markus   1254: static void
1.191     djm      1255: mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1.1       provos   1256: {
                   1257:        socklen_t fromlen;
                   1258:        struct sockaddr_storage from;
                   1259:
                   1260:        /*
                   1261:         * Get IP address of client. If the connection is not a socket, let
                   1262:         * the address be 0.0.0.0.
                   1263:         */
                   1264:        memset(&from, 0, sizeof(from));
1.24      stevesk  1265:        fromlen = sizeof(from);
1.191     djm      1266:        if (ssh_packet_connection_is_on_socket(ssh)) {
                   1267:                if (getpeername(ssh_packet_get_connection_in(ssh),
1.198     deraadt  1268:                    (struct sockaddr *)&from, &fromlen) == -1) {
1.1       provos   1269:                        debug("getpeername: %.100s", strerror(errno));
1.50      markus   1270:                        cleanup_exit(255);
1.1       provos   1271:                }
                   1272:        }
                   1273:        /* Record that there was a login on that tty from the remote host. */
                   1274:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.158     djm      1275:            session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1.24      stevesk  1276:            (struct sockaddr *)&from, fromlen);
1.1       provos   1277: }
                   1278:
                   1279: static void
                   1280: mm_session_close(Session *s)
                   1281: {
1.217     djm      1282:        debug3_f("session %d pid %ld", s->self, (long)s->pid);
1.1       provos   1283:        if (s->ttyfd != -1) {
1.217     djm      1284:                debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
1.1       provos   1285:                session_pty_cleanup2(s);
                   1286:        }
1.96      djm      1287:        session_unused(s->self);
1.1       provos   1288: }
                   1289:
                   1290: int
1.191     djm      1291: mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos   1292: {
1.11      mouring  1293:        extern struct monitor *pmonitor;
1.1       provos   1294:        Session *s;
1.183     markus   1295:        int r, res, fd0;
1.1       provos   1296:
1.217     djm      1297:        debug3_f("entering");
1.1       provos   1298:
1.183     markus   1299:        sshbuf_reset(m);
1.1       provos   1300:        s = session_new();
                   1301:        if (s == NULL)
                   1302:                goto error;
                   1303:        s->authctxt = authctxt;
                   1304:        s->pw = authctxt->pw;
1.11      mouring  1305:        s->pid = pmonitor->m_pid;
1.1       provos   1306:        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
                   1307:        if (res == 0)
                   1308:                goto error;
                   1309:        pty_setowner(authctxt->pw, s->tty);
                   1310:
1.183     markus   1311:        if ((r = sshbuf_put_u32(m, 1)) != 0 ||
                   1312:            (r = sshbuf_put_cstring(m, s->tty)) != 0)
1.217     djm      1313:                fatal_fr(r, "assemble");
1.1       provos   1314:
                   1315:        /* We need to trick ttyslot */
                   1316:        if (dup2(s->ttyfd, 0) == -1)
1.217     djm      1317:                fatal_f("dup2");
1.1       provos   1318:
1.191     djm      1319:        mm_record_login(ssh, s, authctxt->pw);
1.1       provos   1320:
                   1321:        /* Now we can close the file descriptor again */
                   1322:        close(0);
1.61      dtucker  1323:
                   1324:        /* send messages generated by record_login */
1.183     markus   1325:        if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1.217     djm      1326:                fatal_fr(r, "assemble loginmsg");
1.183     markus   1327:        sshbuf_reset(loginmsg);
1.61      dtucker  1328:
                   1329:        mm_request_send(sock, MONITOR_ANS_PTY, m);
                   1330:
1.92      djm      1331:        if (mm_send_fd(sock, s->ptyfd) == -1 ||
                   1332:            mm_send_fd(sock, s->ttyfd) == -1)
1.217     djm      1333:                fatal_f("send fds failed");
1.1       provos   1334:
                   1335:        /* make sure nothing uses fd 0 */
1.198     deraadt  1336:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1.217     djm      1337:                fatal_f("open(/dev/null): %s", strerror(errno));
1.1       provos   1338:        if (fd0 != 0)
1.217     djm      1339:                error_f("fd0 %d != 0", fd0);
1.1       provos   1340:
1.212     deraadt  1341:        /* slave side of pty is not needed */
1.1       provos   1342:        close(s->ttyfd);
                   1343:        s->ttyfd = s->ptyfd;
                   1344:        /* no need to dup() because nobody closes ptyfd */
                   1345:        s->ptymaster = s->ptyfd;
                   1346:
1.217     djm      1347:        debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
1.1       provos   1348:
                   1349:        return (0);
                   1350:
                   1351:  error:
                   1352:        if (s != NULL)
                   1353:                mm_session_close(s);
1.183     markus   1354:        if ((r = sshbuf_put_u32(m, 0)) != 0)
1.217     djm      1355:                fatal_fr(r, "assemble 0");
1.59      avsm     1356:        mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1       provos   1357:        return (0);
                   1358: }
                   1359:
                   1360: int
1.191     djm      1361: mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1.1       provos   1362: {
                   1363:        Session *s;
                   1364:        char *tty;
1.183     markus   1365:        int r;
1.1       provos   1366:
1.217     djm      1367:        debug3_f("entering");
1.1       provos   1368:
1.183     markus   1369:        if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1.217     djm      1370:                fatal_fr(r, "parse tty");
1.1       provos   1371:        if ((s = session_by_tty(tty)) != NULL)
                   1372:                mm_session_close(s);
1.183     markus   1373:        sshbuf_reset(m);
1.124     djm      1374:        free(tty);
1.1       provos   1375:        return (0);
                   1376: }
                   1377:
                   1378: int
1.191     djm      1379: mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1.1       provos   1380: {
1.11      mouring  1381:        extern struct monitor *pmonitor;
1.1       provos   1382:        int res, status;
                   1383:
1.217     djm      1384:        debug3_f("tearing down sessions");
1.1       provos   1385:
                   1386:        /* The child is terminating */
1.173     djm      1387:        session_destroy_all(ssh, &mm_session_close);
1.1       provos   1388:
1.11      mouring  1389:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1390:                if (errno != EINTR)
                   1391:                        exit(1);
1.1       provos   1392:
                   1393:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1394:
                   1395:        /* Terminate process */
1.57      deraadt  1396:        exit(res);
1.170     markus   1397: }
                   1398:
                   1399: void
1.191     djm      1400: monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.170     markus   1401: {
                   1402:        ssh_clear_newkeys(ssh, MODE_IN);
                   1403:        ssh_clear_newkeys(ssh, MODE_OUT);
                   1404:        sshbuf_free(child_state);
                   1405:        child_state = NULL;
1.1       provos   1406: }
                   1407:
                   1408: void
1.191     djm      1409: monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.1       provos   1410: {
1.139     markus   1411:        struct kex *kex;
                   1412:        int r;
                   1413:
1.217     djm      1414:        debug3_f("packet_set_state");
1.139     markus   1415:        if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1.217     djm      1416:                fatal_fr(r, "packet_set_state");
1.139     markus   1417:        sshbuf_free(child_state);
                   1418:        child_state = NULL;
1.223     djm      1419:        if ((kex = ssh->kex) == NULL)
                   1420:                fatal_f("internal error: ssh->kex == NULL");
                   1421:        if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
                   1422:                fatal_f("incorrect session id length %zu (expected %u)",
                   1423:                    sshbuf_len(ssh->kex->session_id), session_id2_len);
                   1424:        }
                   1425:        if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
                   1426:            session_id2_len) != 0)
                   1427:                fatal_f("session ID mismatch");
                   1428:        /* XXX set callbacks */
1.147     djm      1429: #ifdef WITH_OPENSSL
1.223     djm      1430:        kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
                   1431:        kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
                   1432:        kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
                   1433:        kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
                   1434:        kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
                   1435:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
                   1436:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
                   1437:        kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1.147     djm      1438: #endif
1.223     djm      1439:        kex->kex[KEX_C25519_SHA256] = kex_gen_server;
                   1440:        kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
                   1441:        kex->load_host_public_key=&get_hostkey_public_by_type;
                   1442:        kex->load_host_private_key=&get_hostkey_private_by_type;
                   1443:        kex->host_key_index=&get_hostkey_index;
                   1444:        kex->sign = sshd_hostkey_sign;
1.1       provos   1445: }
                   1446:
1.210     djm      1447: /* This function requires careful sanity checking */
1.1       provos   1448:
                   1449: void
1.192     djm      1450: mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1.1       provos   1451: {
1.217     djm      1452:        debug3_f("Waiting for new keys");
1.1       provos   1453:
1.139     markus   1454:        if ((child_state = sshbuf_new()) == NULL)
1.217     djm      1455:                fatal_f("sshbuf_new failed");
1.139     markus   1456:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
                   1457:            child_state);
1.217     djm      1458:        debug3_f("GOT new keys");
1.1       provos   1459: }
                   1460:
                   1461:
                   1462: /* XXX */
                   1463:
                   1464: #define FD_CLOSEONEXEC(x) do { \
1.111     djm      1465:        if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1.1       provos   1466:                fatal("fcntl(%d, F_SETFD)", x); \
                   1467: } while (0)
                   1468:
1.2       markus   1469: static void
1.114     djm      1470: monitor_openfds(struct monitor *mon, int do_logfds)
1.3       markus   1471: {
1.114     djm      1472:        int pair[2];
1.171     markus   1473: #ifdef SO_ZEROIZE
                   1474:        int on = 1;
                   1475: #endif
1.114     djm      1476:
1.1       provos   1477:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.217     djm      1478:                fatal_f("socketpair: %s", strerror(errno));
1.171     markus   1479: #ifdef SO_ZEROIZE
1.198     deraadt  1480:        if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1.171     markus   1481:                error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1.198     deraadt  1482:        if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1.171     markus   1483:                error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
                   1484: #endif
1.1       provos   1485:        FD_CLOSEONEXEC(pair[0]);
                   1486:        FD_CLOSEONEXEC(pair[1]);
1.114     djm      1487:        mon->m_recvfd = pair[0];
                   1488:        mon->m_sendfd = pair[1];
                   1489:
                   1490:        if (do_logfds) {
                   1491:                if (pipe(pair) == -1)
1.217     djm      1492:                        fatal_f("pipe: %s", strerror(errno));
1.114     djm      1493:                FD_CLOSEONEXEC(pair[0]);
                   1494:                FD_CLOSEONEXEC(pair[1]);
                   1495:                mon->m_log_recvfd = pair[0];
                   1496:                mon->m_log_sendfd = pair[1];
                   1497:        } else
                   1498:                mon->m_log_recvfd = mon->m_log_sendfd = -1;
1.1       provos   1499: }
                   1500:
                   1501: #define MM_MEMSIZE     65536
                   1502:
                   1503: struct monitor *
                   1504: monitor_init(void)
                   1505: {
                   1506:        struct monitor *mon;
                   1507:
1.75      djm      1508:        mon = xcalloc(1, sizeof(*mon));
1.114     djm      1509:        monitor_openfds(mon, 1);
1.1       provos   1510:
                   1511:        return mon;
                   1512: }
                   1513:
                   1514: void
                   1515: monitor_reinit(struct monitor *mon)
                   1516: {
1.114     djm      1517:        monitor_openfds(mon, 0);
1.1       provos   1518: }
1.46      markus   1519:
                   1520: #ifdef GSSAPI
                   1521: int
1.191     djm      1522: mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1.46      markus   1523: {
1.59      avsm     1524:        gss_OID_desc goid;
1.46      markus   1525:        OM_uint32 major;
1.183     markus   1526:        size_t len;
1.184     djm      1527:        u_char *p;
1.183     markus   1528:        int r;
1.46      markus   1529:
1.164     djm      1530:        if (!options.gss_authentication)
1.217     djm      1531:                fatal_f("GSSAPI authentication not enabled");
1.164     djm      1532:
1.184     djm      1533:        if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1.217     djm      1534:                fatal_fr(r, "parse");
1.184     djm      1535:        goid.elements = p;
1.59      avsm     1536:        goid.length = len;
1.46      markus   1537:
1.59      avsm     1538:        major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46      markus   1539:
1.124     djm      1540:        free(goid.elements);
1.46      markus   1541:
1.183     markus   1542:        sshbuf_reset(m);
                   1543:        if ((r = sshbuf_put_u32(m, major)) != 0)
1.217     djm      1544:                fatal_fr(r, "assemble");
1.46      markus   1545:
1.64      stevesk  1546:        mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46      markus   1547:
                   1548:        /* Now we have a context, enable the step */
                   1549:        monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
                   1550:
                   1551:        return (0);
                   1552: }
                   1553:
                   1554: int
1.191     djm      1555: mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1.46      markus   1556: {
                   1557:        gss_buffer_desc in;
                   1558:        gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64      stevesk  1559:        OM_uint32 major, minor;
1.46      markus   1560:        OM_uint32 flags = 0; /* GSI needs this */
1.183     markus   1561:        int r;
1.46      markus   1562:
1.164     djm      1563:        if (!options.gss_authentication)
1.217     djm      1564:                fatal_f("GSSAPI authentication not enabled");
1.164     djm      1565:
1.184     djm      1566:        if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1.217     djm      1567:                fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1.46      markus   1568:        major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1.124     djm      1569:        free(in.value);
1.46      markus   1570:
1.183     markus   1571:        sshbuf_reset(m);
                   1572:        if ((r = sshbuf_put_u32(m, major)) != 0 ||
                   1573:            (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
                   1574:            (r = sshbuf_put_u32(m, flags)) != 0)
1.217     djm      1575:                fatal_fr(r, "assemble");
1.59      avsm     1576:        mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46      markus   1577:
                   1578:        gss_release_buffer(&minor, &out);
                   1579:
1.64      stevesk  1580:        if (major == GSS_S_COMPLETE) {
1.46      markus   1581:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
                   1582:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52      markus   1583:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46      markus   1584:        }
                   1585:        return (0);
                   1586: }
                   1587:
                   1588: int
1.191     djm      1589: mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
1.52      markus   1590: {
                   1591:        gss_buffer_desc gssbuf, mic;
                   1592:        OM_uint32 ret;
1.184     djm      1593:        int r;
1.54      djm      1594:
1.164     djm      1595:        if (!options.gss_authentication)
1.217     djm      1596:                fatal_f("GSSAPI authentication not enabled");
1.164     djm      1597:
1.184     djm      1598:        if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
                   1599:            (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1.217     djm      1600:                fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1.54      djm      1601:
1.52      markus   1602:        ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54      djm      1603:
1.124     djm      1604:        free(gssbuf.value);
                   1605:        free(mic.value);
1.54      djm      1606:
1.183     markus   1607:        sshbuf_reset(m);
                   1608:        if ((r = sshbuf_put_u32(m, ret)) != 0)
1.217     djm      1609:                fatal_fr(r, "assemble");
1.54      djm      1610:
1.59      avsm     1611:        mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54      djm      1612:
1.52      markus   1613:        if (!GSS_ERROR(ret))
                   1614:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54      djm      1615:
1.52      markus   1616:        return (0);
                   1617: }
                   1618:
                   1619: int
1.191     djm      1620: mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
1.46      markus   1621: {
1.184     djm      1622:        int r, authenticated;
1.172     djm      1623:        const char *displayname;
1.164     djm      1624:
                   1625:        if (!options.gss_authentication)
1.217     djm      1626:                fatal_f("GSSAPI authentication not enabled");
1.46      markus   1627:
                   1628:        authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
                   1629:
1.183     markus   1630:        sshbuf_reset(m);
                   1631:        if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1.217     djm      1632:                fatal_fr(r, "assemble");
1.46      markus   1633:
1.217     djm      1634:        debug3_f("sending result %d", authenticated);
1.59      avsm     1635:        mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46      markus   1636:
1.64      stevesk  1637:        auth_method = "gssapi-with-mic";
1.172     djm      1638:
                   1639:        if ((displayname = ssh_gssapi_displayname()) != NULL)
                   1640:                auth2_record_info(authctxt, "%s", displayname);
1.46      markus   1641:
                   1642:        /* Monitor loop will terminate if authenticated */
                   1643:        return (authenticated);
                   1644: }
                   1645: #endif /* GSSAPI */
1.100     djm      1646: