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

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