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

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