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

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