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

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