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

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