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

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