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

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