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

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