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

1.100   ! djm         1: /* $OpenBSD: monitor.c,v 1.99 2008/07/10 18:08:11 markus 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.1       provos     76:
1.46      markus     77: #ifdef GSSAPI
                     78: static Gssctxt *gsscontext = NULL;
                     79: #endif
                     80:
1.1       provos     81: /* Imports */
                     82: extern ServerOptions options;
                     83: extern u_int utmp_len;
                     84: extern Newkeys *current_keys[];
                     85: extern z_stream incoming_stream;
                     86: extern z_stream outgoing_stream;
                     87: extern u_char session_id[];
                     88: extern Buffer input, output;
                     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;
                    114: } child_state;
                    115:
1.43      markus    116: /* Functions on the monitor that answer unprivileged requests */
1.1       provos    117:
                    118: int mm_answer_moduli(int, Buffer *);
                    119: int mm_answer_sign(int, Buffer *);
                    120: int mm_answer_pwnamallow(int, Buffer *);
1.10      djm       121: int mm_answer_auth2_read_banner(int, Buffer *);
1.1       provos    122: int mm_answer_authserv(int, Buffer *);
                    123: int mm_answer_authpassword(int, Buffer *);
                    124: int mm_answer_bsdauthquery(int, Buffer *);
                    125: int mm_answer_bsdauthrespond(int, Buffer *);
                    126: int mm_answer_skeyquery(int, Buffer *);
                    127: int mm_answer_skeyrespond(int, Buffer *);
                    128: int mm_answer_keyallowed(int, Buffer *);
                    129: int mm_answer_keyverify(int, Buffer *);
                    130: int mm_answer_pty(int, Buffer *);
                    131: int mm_answer_pty_cleanup(int, Buffer *);
                    132: int mm_answer_term(int, Buffer *);
                    133: int mm_answer_rsa_keyallowed(int, Buffer *);
                    134: int mm_answer_rsa_challenge(int, Buffer *);
                    135: int mm_answer_rsa_response(int, Buffer *);
                    136: int mm_answer_sesskey(int, Buffer *);
                    137: int mm_answer_sessid(int, Buffer *);
1.100   ! djm       138: int mm_answer_jpake_get_pwdata(int, Buffer *);
        !           139: int mm_answer_jpake_step1(int, Buffer *);
        !           140: int mm_answer_jpake_step2(int, Buffer *);
        !           141: int mm_answer_jpake_key_confirm(int, Buffer *);
        !           142: int mm_answer_jpake_check_confirm(int, Buffer *);
1.1       provos    143:
1.46      markus    144: #ifdef GSSAPI
                    145: int mm_answer_gss_setup_ctx(int, Buffer *);
                    146: int mm_answer_gss_accept_ctx(int, Buffer *);
                    147: int mm_answer_gss_userok(int, Buffer *);
1.52      markus    148: int mm_answer_gss_checkmic(int, Buffer *);
1.46      markus    149: #endif
1.25      itojun    150:
1.1       provos    151: static Authctxt *authctxt;
                    152: static BIGNUM *ssh1_challenge = NULL;  /* used for ssh1 rsa auth */
                    153:
                    154: /* local state for key verify */
                    155: static u_char *key_blob = NULL;
                    156: static u_int key_bloblen = 0;
                    157: static int key_blobtype = MM_NOKEY;
1.26      markus    158: static char *hostbased_cuser = NULL;
                    159: static char *hostbased_chost = NULL;
1.1       provos    160: static char *auth_method = "unknown";
1.44      markus    161: static u_int session_id2_len = 0;
1.13      markus    162: static u_char *session_id2 = NULL;
1.40      markus    163: static pid_t monitor_child_pid;
1.1       provos    164:
                    165: struct mon_table {
                    166:        enum monitor_reqtype type;
                    167:        int flags;
                    168:        int (*f)(int, Buffer *);
                    169: };
                    170:
                    171: #define MON_ISAUTH     0x0004  /* Required for Authentication */
                    172: #define MON_AUTHDECIDE 0x0008  /* Decides Authentication */
                    173: #define MON_ONCE       0x0010  /* Disable after calling */
1.77      dtucker   174: #define MON_ALOG       0x0020  /* Log auth attempt without authenticating */
1.1       provos    175:
                    176: #define MON_AUTH       (MON_ISAUTH|MON_AUTHDECIDE)
                    177:
                    178: #define MON_PERMIT     0x1000  /* Request is permitted */
                    179:
                    180: struct mon_table mon_dispatch_proto20[] = {
                    181:     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
                    182:     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
                    183:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    184:     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10      djm       185:     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1       provos    186:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
                    187:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66      stevesk   188:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1       provos    189:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
                    190:     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.46      markus    191: #ifdef GSSAPI
                    192:     {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
                    193:     {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
                    194:     {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
1.52      markus    195:     {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
1.46      markus    196: #endif
1.100   ! djm       197: #ifdef JPAKE
        !           198:     {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
        !           199:     {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
        !           200:     {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
        !           201:     {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
        !           202:     {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
        !           203: #endif
1.1       provos    204:     {0, 0, NULL}
                    205: };
                    206:
                    207: struct mon_table mon_dispatch_postauth20[] = {
                    208:     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
                    209:     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
                    210:     {MONITOR_REQ_PTY, 0, mm_answer_pty},
                    211:     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
                    212:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    213:     {0, 0, NULL}
                    214: };
                    215:
                    216: struct mon_table mon_dispatch_proto15[] = {
                    217:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    218:     {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
                    219:     {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
                    220:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
1.77      dtucker   221:     {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
                    222:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
1.1       provos    223:     {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
                    224:     {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
                    225:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
1.66      stevesk   226:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
1.1       provos    227:     {0, 0, NULL}
                    228: };
                    229:
                    230: struct mon_table mon_dispatch_postauth15[] = {
                    231:     {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
                    232:     {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
                    233:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    234:     {0, 0, NULL}
                    235: };
                    236:
                    237: struct mon_table *mon_dispatch;
                    238:
                    239: /* Specifies if a certain message is allowed at the moment */
                    240:
                    241: static void
                    242: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
                    243: {
                    244:        while (ent->f != NULL) {
                    245:                if (ent->type == type) {
                    246:                        ent->flags &= ~MON_PERMIT;
                    247:                        ent->flags |= permit ? MON_PERMIT : 0;
                    248:                        return;
                    249:                }
                    250:                ent++;
                    251:        }
                    252: }
                    253:
                    254: static void
                    255: monitor_permit_authentications(int permit)
                    256: {
                    257:        struct mon_table *ent = mon_dispatch;
                    258:
                    259:        while (ent->f != NULL) {
                    260:                if (ent->flags & MON_AUTH) {
                    261:                        ent->flags &= ~MON_PERMIT;
                    262:                        ent->flags |= permit ? MON_PERMIT : 0;
                    263:                }
                    264:                ent++;
                    265:        }
                    266: }
                    267:
1.50      markus    268: void
                    269: monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
1.1       provos    270: {
                    271:        struct mon_table *ent;
                    272:        int authenticated = 0;
                    273:
                    274:        debug3("preauth child monitor started");
                    275:
1.50      markus    276:        authctxt = _authctxt;
                    277:        memset(authctxt, 0, sizeof(*authctxt));
                    278:
1.1       provos    279:        if (compat20) {
                    280:                mon_dispatch = mon_dispatch_proto20;
                    281:
                    282:                /* Permit requests for moduli and signatures */
                    283:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    284:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    285:        } else {
                    286:                mon_dispatch = mon_dispatch_proto15;
                    287:
                    288:                monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                    289:        }
                    290:
                    291:        /* The first few requests do not require asynchronous access */
                    292:        while (!authenticated) {
1.77      dtucker   293:                auth_method = "unknown";
1.89      markus    294:                authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
1.1       provos    295:                if (authenticated) {
                    296:                        if (!(ent->flags & MON_AUTHDECIDE))
                    297:                                fatal("%s: unexpected authentication from %d",
1.14      markus    298:                                    __func__, ent->type);
1.1       provos    299:                        if (authctxt->pw->pw_uid == 0 &&
                    300:                            !auth_root_allowed(auth_method))
                    301:                                authenticated = 0;
                    302:                }
                    303:
1.77      dtucker   304:                if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
1.1       provos    305:                        auth_log(authctxt, authenticated, auth_method,
                    306:                            compat20 ? " ssh2" : "");
                    307:                        if (!authenticated)
                    308:                                authctxt->failures++;
                    309:                }
1.100   ! djm       310: #ifdef JPAKE
        !           311:                /* Cleanup JPAKE context after authentication */
        !           312:                if (ent->flags & MON_AUTHDECIDE) {
        !           313:                        if (authctxt->jpake_ctx != NULL) {
        !           314:                                jpake_free(authctxt->jpake_ctx);
        !           315:                                authctxt->jpake_ctx = NULL;
        !           316:                        }
        !           317:                }
        !           318: #endif
1.1       provos    319:        }
                    320:
                    321:        if (!authctxt->valid)
1.14      markus    322:                fatal("%s: authenticated invalid user", __func__);
1.77      dtucker   323:        if (strcmp(auth_method, "unknown") == 0)
                    324:                fatal("%s: authentication method name unknown", __func__);
1.1       provos    325:
                    326:        debug("%s: %s has been authenticated by privileged process",
1.14      markus    327:            __func__, authctxt->user);
1.1       provos    328:
1.11      mouring   329:        mm_get_keystate(pmonitor);
1.1       provos    330: }
                    331:
1.40      markus    332: static void
                    333: monitor_set_child_handler(pid_t pid)
                    334: {
                    335:        monitor_child_pid = pid;
                    336: }
                    337:
                    338: static void
1.59      avsm      339: monitor_child_handler(int sig)
1.40      markus    340: {
1.59      avsm      341:        kill(monitor_child_pid, sig);
1.40      markus    342: }
                    343:
1.1       provos    344: void
1.11      mouring   345: monitor_child_postauth(struct monitor *pmonitor)
1.1       provos    346: {
1.40      markus    347:        monitor_set_child_handler(pmonitor->m_pid);
                    348:        signal(SIGHUP, &monitor_child_handler);
                    349:        signal(SIGTERM, &monitor_child_handler);
1.91      djm       350:        signal(SIGINT, &monitor_child_handler);
1.40      markus    351:
1.1       provos    352:        if (compat20) {
                    353:                mon_dispatch = mon_dispatch_postauth20;
                    354:
                    355:                /* Permit requests for moduli and signatures */
                    356:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    357:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    358:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    359:        } else {
                    360:                mon_dispatch = mon_dispatch_postauth15;
                    361:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    362:        }
                    363:        if (!no_pty_flag) {
                    364:                monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
                    365:                monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
                    366:        }
                    367:
                    368:        for (;;)
1.11      mouring   369:                monitor_read(pmonitor, mon_dispatch, NULL);
1.1       provos    370: }
                    371:
                    372: void
1.11      mouring   373: monitor_sync(struct monitor *pmonitor)
1.1       provos    374: {
1.16      djm       375:        if (options.compression) {
                    376:                /* The member allocation is not visible, so sync it */
                    377:                mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
                    378:        }
1.1       provos    379: }
                    380:
                    381: int
1.11      mouring   382: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1       provos    383:     struct mon_table **pent)
                    384: {
                    385:        Buffer m;
                    386:        int ret;
                    387:        u_char type;
                    388:
                    389:        buffer_init(&m);
                    390:
1.11      mouring   391:        mm_request_receive(pmonitor->m_sendfd, &m);
1.1       provos    392:        type = buffer_get_char(&m);
                    393:
1.14      markus    394:        debug3("%s: checking request %d", __func__, type);
1.1       provos    395:
                    396:        while (ent->f != NULL) {
                    397:                if (ent->type == type)
                    398:                        break;
                    399:                ent++;
                    400:        }
                    401:
                    402:        if (ent->f != NULL) {
                    403:                if (!(ent->flags & MON_PERMIT))
1.14      markus    404:                        fatal("%s: unpermitted request %d", __func__,
1.1       provos    405:                            type);
1.11      mouring   406:                ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1       provos    407:                buffer_free(&m);
                    408:
                    409:                /* The child may use this request only once, disable it */
                    410:                if (ent->flags & MON_ONCE) {
1.14      markus    411:                        debug2("%s: %d used once, disabling now", __func__,
1.1       provos    412:                            type);
                    413:                        ent->flags &= ~MON_PERMIT;
                    414:                }
                    415:
                    416:                if (pent != NULL)
                    417:                        *pent = ent;
1.3       markus    418:
1.1       provos    419:                return ret;
                    420:        }
                    421:
1.14      markus    422:        fatal("%s: unsupported request: %d", __func__, type);
1.1       provos    423:
                    424:        /* NOTREACHED */
                    425:        return (-1);
                    426: }
                    427:
                    428: /* allowed key state */
                    429: static int
                    430: monitor_allowed_key(u_char *blob, u_int bloblen)
                    431: {
                    432:        /* make sure key is allowed */
                    433:        if (key_blob == NULL || key_bloblen != bloblen ||
                    434:            memcmp(key_blob, blob, key_bloblen))
                    435:                return (0);
                    436:        return (1);
                    437: }
                    438:
                    439: static void
                    440: monitor_reset_key_state(void)
                    441: {
                    442:        /* reset state */
                    443:        if (key_blob != NULL)
                    444:                xfree(key_blob);
                    445:        if (hostbased_cuser != NULL)
                    446:                xfree(hostbased_cuser);
                    447:        if (hostbased_chost != NULL)
                    448:                xfree(hostbased_chost);
                    449:        key_blob = NULL;
                    450:        key_bloblen = 0;
                    451:        key_blobtype = MM_NOKEY;
                    452:        hostbased_cuser = NULL;
                    453:        hostbased_chost = NULL;
                    454: }
                    455:
                    456: int
1.59      avsm      457: mm_answer_moduli(int sock, Buffer *m)
1.1       provos    458: {
                    459:        DH *dh;
                    460:        int min, want, max;
                    461:
                    462:        min = buffer_get_int(m);
                    463:        want = buffer_get_int(m);
                    464:        max = buffer_get_int(m);
                    465:
                    466:        debug3("%s: got parameters: %d %d %d",
1.14      markus    467:            __func__, min, want, max);
1.1       provos    468:        /* We need to check here, too, in case the child got corrupted */
                    469:        if (max < min || want < min || max < want)
                    470:                fatal("%s: bad parameters: %d %d %d",
1.14      markus    471:                    __func__, min, want, max);
1.1       provos    472:
                    473:        buffer_clear(m);
                    474:
                    475:        dh = choose_dh(min, want, max);
                    476:        if (dh == NULL) {
                    477:                buffer_put_char(m, 0);
                    478:                return (0);
                    479:        } else {
                    480:                /* Send first bignum */
                    481:                buffer_put_char(m, 1);
                    482:                buffer_put_bignum2(m, dh->p);
                    483:                buffer_put_bignum2(m, dh->g);
1.3       markus    484:
1.1       provos    485:                DH_free(dh);
                    486:        }
1.59      avsm      487:        mm_request_send(sock, MONITOR_ANS_MODULI, m);
1.1       provos    488:        return (0);
                    489: }
                    490:
                    491: int
1.59      avsm      492: mm_answer_sign(int sock, Buffer *m)
1.1       provos    493: {
                    494:        Key *key;
                    495:        u_char *p;
                    496:        u_char *signature;
                    497:        u_int siglen, datlen;
                    498:        int keyid;
1.3       markus    499:
1.14      markus    500:        debug3("%s", __func__);
1.1       provos    501:
1.3       markus    502:        keyid = buffer_get_int(m);
                    503:        p = buffer_get_string(m, &datlen);
1.1       provos    504:
1.69      djm       505:        /*
1.71      deraadt   506:         * Supported KEX types will only return SHA1 (20 byte) or
1.69      djm       507:         * SHA256 (32 byte) hashes
                    508:         */
                    509:        if (datlen != 20 && datlen != 32)
1.19      deraadt   510:                fatal("%s: data length incorrect: %u", __func__, datlen);
1.1       provos    511:
1.13      markus    512:        /* save session id, it will be passed on the first call */
                    513:        if (session_id2_len == 0) {
                    514:                session_id2_len = datlen;
                    515:                session_id2 = xmalloc(session_id2_len);
                    516:                memcpy(session_id2, p, session_id2_len);
                    517:        }
                    518:
1.1       provos    519:        if ((key = get_hostkey_by_index(keyid)) == NULL)
1.14      markus    520:                fatal("%s: no hostkey from index %d", __func__, keyid);
1.1       provos    521:        if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1.14      markus    522:                fatal("%s: key_sign failed", __func__);
1.1       provos    523:
1.19      deraadt   524:        debug3("%s: signature %p(%u)", __func__, signature, siglen);
1.1       provos    525:
                    526:        buffer_clear(m);
                    527:        buffer_put_string(m, signature, siglen);
                    528:
                    529:        xfree(p);
                    530:        xfree(signature);
                    531:
1.59      avsm      532:        mm_request_send(sock, MONITOR_ANS_SIGN, m);
1.1       provos    533:
                    534:        /* Turn on permissions for getpwnam */
                    535:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                    536:
                    537:        return (0);
                    538: }
                    539:
                    540: /* Retrieves the password entry and also checks if the user is permitted */
                    541:
                    542: int
1.59      avsm      543: mm_answer_pwnamallow(int sock, Buffer *m)
1.1       provos    544: {
1.60      dtucker   545:        char *username;
1.1       provos    546:        struct passwd *pwent;
                    547:        int allowed = 0;
1.3       markus    548:
1.14      markus    549:        debug3("%s", __func__);
1.1       provos    550:
                    551:        if (authctxt->attempt++ != 0)
1.14      markus    552:                fatal("%s: multiple attempts for getpwnam", __func__);
1.1       provos    553:
1.60      dtucker   554:        username = buffer_get_string(m, NULL);
1.1       provos    555:
1.60      dtucker   556:        pwent = getpwnamallow(username);
1.1       provos    557:
1.60      dtucker   558:        authctxt->user = xstrdup(username);
                    559:        setproctitle("%s [priv]", pwent ? username : "unknown");
                    560:        xfree(username);
1.1       provos    561:
                    562:        buffer_clear(m);
                    563:
                    564:        if (pwent == NULL) {
                    565:                buffer_put_char(m, 0);
1.53      djm       566:                authctxt->pw = fakepw();
1.1       provos    567:                goto out;
                    568:        }
                    569:
                    570:        allowed = 1;
1.4       markus    571:        authctxt->pw = pwent;
1.1       provos    572:        authctxt->valid = 1;
                    573:
                    574:        buffer_put_char(m, 1);
                    575:        buffer_put_string(m, pwent, sizeof(struct passwd));
                    576:        buffer_put_cstring(m, pwent->pw_name);
                    577:        buffer_put_cstring(m, "*");
                    578:        buffer_put_cstring(m, pwent->pw_gecos);
                    579:        buffer_put_cstring(m, pwent->pw_class);
                    580:        buffer_put_cstring(m, pwent->pw_dir);
                    581:        buffer_put_cstring(m, pwent->pw_shell);
1.94      dtucker   582:
                    583:  out:
1.90      dtucker   584:        buffer_put_string(m, &options, sizeof(options));
                    585:        if (options.banner != NULL)
                    586:                buffer_put_cstring(m, options.banner);
1.14      markus    587:        debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.59      avsm      588:        mm_request_send(sock, MONITOR_ANS_PWNAM, m);
1.1       provos    589:
                    590:        /* For SSHv1 allow authentication now */
                    591:        if (!compat20)
                    592:                monitor_permit_authentications(1);
1.10      djm       593:        else {
1.1       provos    594:                /* Allow service/style information on the auth context */
                    595:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.10      djm       596:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
                    597:        }
                    598:
                    599:
                    600:        return (0);
                    601: }
                    602:
1.59      avsm      603: int mm_answer_auth2_read_banner(int sock, Buffer *m)
1.10      djm       604: {
                    605:        char *banner;
                    606:
                    607:        buffer_clear(m);
                    608:        banner = auth2_read_banner();
                    609:        buffer_put_cstring(m, banner != NULL ? banner : "");
1.59      avsm      610:        mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1       provos    611:
1.10      djm       612:        if (banner != NULL)
1.21      deraadt   613:                xfree(banner);
1.1       provos    614:
                    615:        return (0);
                    616: }
                    617:
                    618: int
1.59      avsm      619: mm_answer_authserv(int sock, Buffer *m)
1.1       provos    620: {
                    621:        monitor_permit_authentications(1);
                    622:
                    623:        authctxt->service = buffer_get_string(m, NULL);
                    624:        authctxt->style = buffer_get_string(m, NULL);
1.6       stevesk   625:        debug3("%s: service=%s, style=%s",
1.14      markus    626:            __func__, authctxt->service, authctxt->style);
1.6       stevesk   627:
1.1       provos    628:        if (strlen(authctxt->style) == 0) {
                    629:                xfree(authctxt->style);
                    630:                authctxt->style = NULL;
                    631:        }
                    632:
                    633:        return (0);
                    634: }
                    635:
                    636: int
1.59      avsm      637: mm_answer_authpassword(int sock, Buffer *m)
1.1       provos    638: {
                    639:        static int call_count;
                    640:        char *passwd;
1.22      stevesk   641:        int authenticated;
                    642:        u_int plen;
1.1       provos    643:
                    644:        passwd = buffer_get_string(m, &plen);
                    645:        /* Only authenticate if the context is valid */
1.12      markus    646:        authenticated = options.password_authentication &&
1.48      markus    647:            auth_password(authctxt, passwd);
1.1       provos    648:        memset(passwd, 0, strlen(passwd));
                    649:        xfree(passwd);
                    650:
                    651:        buffer_clear(m);
                    652:        buffer_put_int(m, authenticated);
                    653:
1.14      markus    654:        debug3("%s: sending result %d", __func__, authenticated);
1.59      avsm      655:        mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
1.1       provos    656:
                    657:        call_count++;
                    658:        if (plen == 0 && call_count == 1)
                    659:                auth_method = "none";
                    660:        else
                    661:                auth_method = "password";
                    662:
                    663:        /* Causes monitor loop to terminate if authenticated */
                    664:        return (authenticated);
                    665: }
                    666:
                    667: int
1.59      avsm      668: mm_answer_bsdauthquery(int sock, Buffer *m)
1.1       provos    669: {
                    670:        char *name, *infotxt;
                    671:        u_int numprompts;
                    672:        u_int *echo_on;
                    673:        char **prompts;
1.31      markus    674:        u_int success;
1.1       provos    675:
1.31      markus    676:        success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    677:            &prompts, &echo_on) < 0 ? 0 : 1;
1.1       provos    678:
                    679:        buffer_clear(m);
1.31      markus    680:        buffer_put_int(m, success);
                    681:        if (success)
1.1       provos    682:                buffer_put_cstring(m, prompts[0]);
                    683:
1.31      markus    684:        debug3("%s: sending challenge success: %u", __func__, success);
1.59      avsm      685:        mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
1.1       provos    686:
1.31      markus    687:        if (success) {
1.1       provos    688:                xfree(name);
                    689:                xfree(infotxt);
                    690:                xfree(prompts);
                    691:                xfree(echo_on);
                    692:        }
                    693:
                    694:        return (0);
                    695: }
                    696:
                    697: int
1.59      avsm      698: mm_answer_bsdauthrespond(int sock, Buffer *m)
1.1       provos    699: {
                    700:        char *response;
                    701:        int authok;
                    702:
                    703:        if (authctxt->as == 0)
1.14      markus    704:                fatal("%s: no bsd auth session", __func__);
1.1       provos    705:
                    706:        response = buffer_get_string(m, NULL);
1.12      markus    707:        authok = options.challenge_response_authentication &&
                    708:            auth_userresponse(authctxt->as, response, 0);
1.1       provos    709:        authctxt->as = NULL;
1.14      markus    710:        debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1       provos    711:        xfree(response);
                    712:
                    713:        buffer_clear(m);
                    714:        buffer_put_int(m, authok);
                    715:
1.14      markus    716:        debug3("%s: sending authenticated: %d", __func__, authok);
1.59      avsm      717:        mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
1.1       provos    718:
                    719:        auth_method = "bsdauth";
                    720:
                    721:        return (authok != 0);
                    722: }
                    723:
                    724:
1.2       markus    725: static void
1.1       provos    726: mm_append_debug(Buffer *m)
                    727: {
                    728:        if (auth_debug_init && buffer_len(&auth_debug)) {
1.14      markus    729:                debug3("%s: Appending debug messages for child", __func__);
1.1       provos    730:                buffer_append(m, buffer_ptr(&auth_debug),
                    731:                    buffer_len(&auth_debug));
                    732:                buffer_clear(&auth_debug);
                    733:        }
                    734: }
                    735:
                    736: int
1.59      avsm      737: mm_answer_keyallowed(int sock, Buffer *m)
1.1       provos    738: {
                    739:        Key *key;
1.26      markus    740:        char *cuser, *chost;
                    741:        u_char *blob;
1.1       provos    742:        u_int bloblen;
                    743:        enum mm_keytype type = 0;
                    744:        int allowed = 0;
                    745:
1.14      markus    746:        debug3("%s entering", __func__);
1.3       markus    747:
1.1       provos    748:        type = buffer_get_int(m);
                    749:        cuser = buffer_get_string(m, NULL);
                    750:        chost = buffer_get_string(m, NULL);
                    751:        blob = buffer_get_string(m, &bloblen);
                    752:
                    753:        key = key_from_blob(blob, bloblen);
                    754:
                    755:        if ((compat20 && type == MM_RSAHOSTKEY) ||
                    756:            (!compat20 && type != MM_RSAHOSTKEY))
1.14      markus    757:                fatal("%s: key type and protocol mismatch", __func__);
1.1       provos    758:
1.14      markus    759:        debug3("%s: key_from_blob: %p", __func__, key);
1.1       provos    760:
1.51      djm       761:        if (key != NULL && authctxt->valid) {
1.63      deraadt   762:                switch (type) {
1.1       provos    763:                case MM_USERKEY:
1.12      markus    764:                        allowed = options.pubkey_authentication &&
                    765:                            user_key_allowed(authctxt->pw, key);
1.77      dtucker   766:                        auth_method = "publickey";
1.97      dtucker   767:                        if (options.pubkey_authentication && allowed != 1)
                    768:                                auth_clear_options();
1.1       provos    769:                        break;
                    770:                case MM_HOSTKEY:
1.12      markus    771:                        allowed = options.hostbased_authentication &&
                    772:                            hostbased_key_allowed(authctxt->pw,
1.1       provos    773:                            cuser, chost, key);
1.77      dtucker   774:                        auth_method = "hostbased";
1.1       provos    775:                        break;
                    776:                case MM_RSAHOSTKEY:
                    777:                        key->type = KEY_RSA1; /* XXX */
1.12      markus    778:                        allowed = options.rhosts_rsa_authentication &&
                    779:                            auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1       provos    780:                            cuser, chost, key);
1.97      dtucker   781:                        if (options.rhosts_rsa_authentication && allowed != 1)
                    782:                                auth_clear_options();
1.77      dtucker   783:                        auth_method = "rsa";
1.1       provos    784:                        break;
                    785:                default:
1.14      markus    786:                        fatal("%s: unknown key type %d", __func__, type);
1.1       provos    787:                        break;
                    788:                }
1.33      markus    789:        }
                    790:        if (key != NULL)
1.1       provos    791:                key_free(key);
                    792:
                    793:        /* clear temporarily storage (used by verify) */
                    794:        monitor_reset_key_state();
                    795:
                    796:        if (allowed) {
                    797:                /* Save temporarily for comparison in verify */
                    798:                key_blob = blob;
                    799:                key_bloblen = bloblen;
                    800:                key_blobtype = type;
                    801:                hostbased_cuser = cuser;
                    802:                hostbased_chost = chost;
1.72      djm       803:        } else {
1.77      dtucker   804:                /* Log failed attempt */
                    805:                auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1.72      djm       806:                xfree(blob);
                    807:                xfree(cuser);
                    808:                xfree(chost);
1.1       provos    809:        }
                    810:
                    811:        debug3("%s: key %p is %s",
1.98      dtucker   812:            __func__, key, allowed ? "allowed" : "not allowed");
1.1       provos    813:
                    814:        buffer_clear(m);
                    815:        buffer_put_int(m, allowed);
1.32      markus    816:        buffer_put_int(m, forced_command != NULL);
1.1       provos    817:
                    818:        mm_append_debug(m);
                    819:
1.59      avsm      820:        mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1.1       provos    821:
                    822:        if (type == MM_RSAHOSTKEY)
                    823:                monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                    824:
                    825:        return (0);
                    826: }
                    827:
                    828: static int
                    829: monitor_valid_userblob(u_char *data, u_int datalen)
                    830: {
                    831:        Buffer b;
1.26      markus    832:        char *p;
1.1       provos    833:        u_int len;
                    834:        int fail = 0;
                    835:
                    836:        buffer_init(&b);
                    837:        buffer_append(&b, data, datalen);
1.3       markus    838:
1.1       provos    839:        if (datafellows & SSH_OLD_SESSIONID) {
1.13      markus    840:                p = buffer_ptr(&b);
                    841:                len = buffer_len(&b);
                    842:                if ((session_id2 == NULL) ||
                    843:                    (len < session_id2_len) ||
                    844:                    (memcmp(p, session_id2, session_id2_len) != 0))
                    845:                        fail++;
1.1       provos    846:                buffer_consume(&b, session_id2_len);
                    847:        } else {
1.13      markus    848:                p = buffer_get_string(&b, &len);
                    849:                if ((session_id2 == NULL) ||
                    850:                    (len != session_id2_len) ||
                    851:                    (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    852:                        fail++;
1.13      markus    853:                xfree(p);
1.1       provos    854:        }
                    855:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    856:                fail++;
                    857:        p = buffer_get_string(&b, NULL);
                    858:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    859:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    860:                    authctxt->user, p);
                    861:                fail++;
                    862:        }
                    863:        xfree(p);
                    864:        buffer_skip_string(&b);
                    865:        if (datafellows & SSH_BUG_PKAUTH) {
                    866:                if (!buffer_get_char(&b))
                    867:                        fail++;
                    868:        } else {
                    869:                p = buffer_get_string(&b, NULL);
                    870:                if (strcmp("publickey", p) != 0)
                    871:                        fail++;
                    872:                xfree(p);
                    873:                if (!buffer_get_char(&b))
                    874:                        fail++;
                    875:                buffer_skip_string(&b);
                    876:        }
                    877:        buffer_skip_string(&b);
                    878:        if (buffer_len(&b) != 0)
                    879:                fail++;
                    880:        buffer_free(&b);
                    881:        return (fail == 0);
                    882: }
                    883:
                    884: static int
1.26      markus    885: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
                    886:     char *chost)
1.1       provos    887: {
                    888:        Buffer b;
1.26      markus    889:        char *p;
1.1       provos    890:        u_int len;
                    891:        int fail = 0;
                    892:
                    893:        buffer_init(&b);
                    894:        buffer_append(&b, data, datalen);
1.3       markus    895:
1.13      markus    896:        p = buffer_get_string(&b, &len);
                    897:        if ((session_id2 == NULL) ||
                    898:            (len != session_id2_len) ||
                    899:            (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    900:                fail++;
1.13      markus    901:        xfree(p);
                    902:
1.1       provos    903:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    904:                fail++;
                    905:        p = buffer_get_string(&b, NULL);
                    906:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    907:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    908:                    authctxt->user, p);
                    909:                fail++;
                    910:        }
                    911:        xfree(p);
                    912:        buffer_skip_string(&b); /* service */
                    913:        p = buffer_get_string(&b, NULL);
                    914:        if (strcmp(p, "hostbased") != 0)
                    915:                fail++;
                    916:        xfree(p);
                    917:        buffer_skip_string(&b); /* pkalg */
                    918:        buffer_skip_string(&b); /* pkblob */
                    919:
                    920:        /* verify client host, strip trailing dot if necessary */
                    921:        p = buffer_get_string(&b, NULL);
                    922:        if (((len = strlen(p)) > 0) && p[len - 1] == '.')
                    923:                p[len - 1] = '\0';
                    924:        if (strcmp(p, chost) != 0)
                    925:                fail++;
                    926:        xfree(p);
                    927:
                    928:        /* verify client user */
                    929:        p = buffer_get_string(&b, NULL);
                    930:        if (strcmp(p, cuser) != 0)
                    931:                fail++;
                    932:        xfree(p);
                    933:
                    934:        if (buffer_len(&b) != 0)
                    935:                fail++;
                    936:        buffer_free(&b);
                    937:        return (fail == 0);
                    938: }
                    939:
                    940: int
1.59      avsm      941: mm_answer_keyverify(int sock, Buffer *m)
1.1       provos    942: {
                    943:        Key *key;
                    944:        u_char *signature, *data, *blob;
                    945:        u_int signaturelen, datalen, bloblen;
                    946:        int verified = 0;
                    947:        int valid_data = 0;
                    948:
                    949:        blob = buffer_get_string(m, &bloblen);
                    950:        signature = buffer_get_string(m, &signaturelen);
                    951:        data = buffer_get_string(m, &datalen);
                    952:
                    953:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring   954:          !monitor_allowed_key(blob, bloblen))
1.14      markus    955:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos    956:
                    957:        key = key_from_blob(blob, bloblen);
                    958:        if (key == NULL)
1.14      markus    959:                fatal("%s: bad public key blob", __func__);
1.1       provos    960:
                    961:        switch (key_blobtype) {
                    962:        case MM_USERKEY:
                    963:                valid_data = monitor_valid_userblob(data, datalen);
                    964:                break;
                    965:        case MM_HOSTKEY:
                    966:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                    967:                    hostbased_cuser, hostbased_chost);
                    968:                break;
                    969:        default:
                    970:                valid_data = 0;
                    971:                break;
                    972:        }
                    973:        if (!valid_data)
1.14      markus    974:                fatal("%s: bad signature data blob", __func__);
1.1       provos    975:
                    976:        verified = key_verify(key, signature, signaturelen, data, datalen);
                    977:        debug3("%s: key %p signature %s",
1.89      markus    978:            __func__, key, (verified == 1) ? "verified" : "unverified");
1.1       provos    979:
                    980:        key_free(key);
                    981:        xfree(blob);
                    982:        xfree(signature);
                    983:        xfree(data);
                    984:
1.17      stevesk   985:        auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
                    986:
1.1       provos    987:        monitor_reset_key_state();
1.3       markus    988:
1.1       provos    989:        buffer_clear(m);
                    990:        buffer_put_int(m, verified);
1.59      avsm      991:        mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1.1       provos    992:
1.89      markus    993:        return (verified == 1);
1.1       provos    994: }
                    995:
1.2       markus    996: static void
1.1       provos    997: mm_record_login(Session *s, struct passwd *pw)
                    998: {
                    999:        socklen_t fromlen;
                   1000:        struct sockaddr_storage from;
                   1001:
                   1002:        /*
                   1003:         * Get IP address of client. If the connection is not a socket, let
                   1004:         * the address be 0.0.0.0.
                   1005:         */
                   1006:        memset(&from, 0, sizeof(from));
1.24      stevesk  1007:        fromlen = sizeof(from);
1.1       provos   1008:        if (packet_connection_is_on_socket()) {
                   1009:                if (getpeername(packet_get_connection_in(),
1.74      deraadt  1010:                    (struct sockaddr *)&from, &fromlen) < 0) {
1.1       provos   1011:                        debug("getpeername: %.100s", strerror(errno));
1.50      markus   1012:                        cleanup_exit(255);
1.1       provos   1013:                }
                   1014:        }
                   1015:        /* Record that there was a login on that tty from the remote host. */
                   1016:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.42      markus   1017:            get_remote_name_or_ip(utmp_len, options.use_dns),
1.24      stevesk  1018:            (struct sockaddr *)&from, fromlen);
1.1       provos   1019: }
                   1020:
                   1021: static void
                   1022: mm_session_close(Session *s)
                   1023: {
1.41      djm      1024:        debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1       provos   1025:        if (s->ttyfd != -1) {
1.86      stevesk  1026:                debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1.1       provos   1027:                session_pty_cleanup2(s);
                   1028:        }
1.96      djm      1029:        session_unused(s->self);
1.1       provos   1030: }
                   1031:
                   1032: int
1.59      avsm     1033: mm_answer_pty(int sock, Buffer *m)
1.1       provos   1034: {
1.11      mouring  1035:        extern struct monitor *pmonitor;
1.1       provos   1036:        Session *s;
                   1037:        int res, fd0;
                   1038:
1.14      markus   1039:        debug3("%s entering", __func__);
1.1       provos   1040:
                   1041:        buffer_clear(m);
                   1042:        s = session_new();
                   1043:        if (s == NULL)
                   1044:                goto error;
                   1045:        s->authctxt = authctxt;
                   1046:        s->pw = authctxt->pw;
1.11      mouring  1047:        s->pid = pmonitor->m_pid;
1.1       provos   1048:        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
                   1049:        if (res == 0)
                   1050:                goto error;
                   1051:        pty_setowner(authctxt->pw, s->tty);
                   1052:
                   1053:        buffer_put_int(m, 1);
                   1054:        buffer_put_cstring(m, s->tty);
                   1055:
                   1056:        /* We need to trick ttyslot */
                   1057:        if (dup2(s->ttyfd, 0) == -1)
1.14      markus   1058:                fatal("%s: dup2", __func__);
1.1       provos   1059:
                   1060:        mm_record_login(s, authctxt->pw);
                   1061:
                   1062:        /* Now we can close the file descriptor again */
                   1063:        close(0);
1.61      dtucker  1064:
                   1065:        /* send messages generated by record_login */
                   1066:        buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
                   1067:        buffer_clear(&loginmsg);
                   1068:
                   1069:        mm_request_send(sock, MONITOR_ANS_PTY, m);
                   1070:
1.92      djm      1071:        if (mm_send_fd(sock, s->ptyfd) == -1 ||
                   1072:            mm_send_fd(sock, s->ttyfd) == -1)
                   1073:                fatal("%s: send fds failed", __func__);
1.1       provos   1074:
                   1075:        /* make sure nothing uses fd 0 */
                   1076:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14      markus   1077:                fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1       provos   1078:        if (fd0 != 0)
1.14      markus   1079:                error("%s: fd0 %d != 0", __func__, fd0);
1.1       provos   1080:
                   1081:        /* slave is not needed */
                   1082:        close(s->ttyfd);
                   1083:        s->ttyfd = s->ptyfd;
                   1084:        /* no need to dup() because nobody closes ptyfd */
                   1085:        s->ptymaster = s->ptyfd;
                   1086:
1.86      stevesk  1087:        debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1.1       provos   1088:
                   1089:        return (0);
                   1090:
                   1091:  error:
                   1092:        if (s != NULL)
                   1093:                mm_session_close(s);
                   1094:        buffer_put_int(m, 0);
1.59      avsm     1095:        mm_request_send(sock, MONITOR_ANS_PTY, m);
1.1       provos   1096:        return (0);
                   1097: }
                   1098:
                   1099: int
1.59      avsm     1100: mm_answer_pty_cleanup(int sock, Buffer *m)
1.1       provos   1101: {
                   1102:        Session *s;
                   1103:        char *tty;
                   1104:
1.14      markus   1105:        debug3("%s entering", __func__);
1.1       provos   1106:
                   1107:        tty = buffer_get_string(m, NULL);
                   1108:        if ((s = session_by_tty(tty)) != NULL)
                   1109:                mm_session_close(s);
                   1110:        buffer_clear(m);
                   1111:        xfree(tty);
                   1112:        return (0);
                   1113: }
                   1114:
                   1115: int
1.59      avsm     1116: mm_answer_sesskey(int sock, Buffer *m)
1.1       provos   1117: {
                   1118:        BIGNUM *p;
                   1119:        int rsafail;
                   1120:
                   1121:        /* Turn off permissions */
1.62      dtucker  1122:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1.1       provos   1123:
                   1124:        if ((p = BN_new()) == NULL)
1.14      markus   1125:                fatal("%s: BN_new", __func__);
1.1       provos   1126:
                   1127:        buffer_get_bignum2(m, p);
                   1128:
                   1129:        rsafail = ssh1_session_key(p);
                   1130:
                   1131:        buffer_clear(m);
                   1132:        buffer_put_int(m, rsafail);
                   1133:        buffer_put_bignum2(m, p);
                   1134:
                   1135:        BN_clear_free(p);
                   1136:
1.59      avsm     1137:        mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1.1       provos   1138:
                   1139:        /* Turn on permissions for sessid passing */
                   1140:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
                   1141:
                   1142:        return (0);
                   1143: }
                   1144:
                   1145: int
1.59      avsm     1146: mm_answer_sessid(int sock, Buffer *m)
1.1       provos   1147: {
                   1148:        int i;
                   1149:
1.14      markus   1150:        debug3("%s entering", __func__);
1.1       provos   1151:
                   1152:        if (buffer_len(m) != 16)
1.14      markus   1153:                fatal("%s: bad ssh1 session id", __func__);
1.1       provos   1154:        for (i = 0; i < 16; i++)
                   1155:                session_id[i] = buffer_get_char(m);
                   1156:
                   1157:        /* Turn on permissions for getpwnam */
                   1158:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                   1159:
                   1160:        return (0);
                   1161: }
                   1162:
                   1163: int
1.59      avsm     1164: mm_answer_rsa_keyallowed(int sock, Buffer *m)
1.1       provos   1165: {
                   1166:        BIGNUM *client_n;
                   1167:        Key *key = NULL;
1.3       markus   1168:        u_char *blob = NULL;
                   1169:        u_int blen = 0;
1.1       provos   1170:        int allowed = 0;
                   1171:
1.14      markus   1172:        debug3("%s entering", __func__);
1.1       provos   1173:
1.77      dtucker  1174:        auth_method = "rsa";
1.12      markus   1175:        if (options.rsa_authentication && authctxt->valid) {
1.1       provos   1176:                if ((client_n = BN_new()) == NULL)
1.14      markus   1177:                        fatal("%s: BN_new", __func__);
1.1       provos   1178:                buffer_get_bignum2(m, client_n);
                   1179:                allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
                   1180:                BN_clear_free(client_n);
                   1181:        }
                   1182:        buffer_clear(m);
                   1183:        buffer_put_int(m, allowed);
1.32      markus   1184:        buffer_put_int(m, forced_command != NULL);
1.1       provos   1185:
                   1186:        /* clear temporarily storage (used by generate challenge) */
                   1187:        monitor_reset_key_state();
                   1188:
                   1189:        if (allowed && key != NULL) {
                   1190:                key->type = KEY_RSA;    /* cheat for key_to_blob */
                   1191:                if (key_to_blob(key, &blob, &blen) == 0)
1.14      markus   1192:                        fatal("%s: key_to_blob failed", __func__);
1.1       provos   1193:                buffer_put_string(m, blob, blen);
                   1194:
                   1195:                /* Save temporarily for comparison in verify */
                   1196:                key_blob = blob;
                   1197:                key_bloblen = blen;
                   1198:                key_blobtype = MM_RSAUSERKEY;
1.33      markus   1199:        }
                   1200:        if (key != NULL)
1.1       provos   1201:                key_free(key);
                   1202:
                   1203:        mm_append_debug(m);
                   1204:
1.59      avsm     1205:        mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1.1       provos   1206:
                   1207:        monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                   1208:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
                   1209:        return (0);
                   1210: }
                   1211:
                   1212: int
1.59      avsm     1213: mm_answer_rsa_challenge(int sock, Buffer *m)
1.1       provos   1214: {
                   1215:        Key *key = NULL;
1.3       markus   1216:        u_char *blob;
                   1217:        u_int blen;
1.1       provos   1218:
1.14      markus   1219:        debug3("%s entering", __func__);
1.1       provos   1220:
                   1221:        if (!authctxt->valid)
1.14      markus   1222:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1223:        blob = buffer_get_string(m, &blen);
                   1224:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1225:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1226:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1227:                fatal("%s: key type mismatch", __func__);
1.1       provos   1228:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1229:                fatal("%s: received bad key", __func__);
1.1       provos   1230:
                   1231:        if (ssh1_challenge)
                   1232:                BN_clear_free(ssh1_challenge);
                   1233:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1234:
                   1235:        buffer_clear(m);
                   1236:        buffer_put_bignum2(m, ssh1_challenge);
                   1237:
1.14      markus   1238:        debug3("%s sending reply", __func__);
1.59      avsm     1239:        mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1.1       provos   1240:
                   1241:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33      markus   1242:
                   1243:        xfree(blob);
                   1244:        key_free(key);
1.1       provos   1245:        return (0);
                   1246: }
                   1247:
                   1248: int
1.59      avsm     1249: mm_answer_rsa_response(int sock, Buffer *m)
1.1       provos   1250: {
                   1251:        Key *key = NULL;
1.3       markus   1252:        u_char *blob, *response;
                   1253:        u_int blen, len;
                   1254:        int success;
1.1       provos   1255:
1.14      markus   1256:        debug3("%s entering", __func__);
1.1       provos   1257:
                   1258:        if (!authctxt->valid)
1.14      markus   1259:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1260:        if (ssh1_challenge == NULL)
1.14      markus   1261:                fatal("%s: no ssh1_challenge", __func__);
1.1       provos   1262:
                   1263:        blob = buffer_get_string(m, &blen);
                   1264:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1265:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1266:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1267:                fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1       provos   1268:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1269:                fatal("%s: received bad key", __func__);
1.1       provos   1270:        response = buffer_get_string(m, &len);
                   1271:        if (len != 16)
1.14      markus   1272:                fatal("%s: received bad response to challenge", __func__);
1.1       provos   1273:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1274:
1.33      markus   1275:        xfree(blob);
1.1       provos   1276:        key_free(key);
                   1277:        xfree(response);
                   1278:
                   1279:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1280:
                   1281:        /* reset state */
                   1282:        BN_clear_free(ssh1_challenge);
                   1283:        ssh1_challenge = NULL;
                   1284:        monitor_reset_key_state();
                   1285:
                   1286:        buffer_clear(m);
                   1287:        buffer_put_int(m, success);
1.59      avsm     1288:        mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1.1       provos   1289:
                   1290:        return (success);
                   1291: }
                   1292:
                   1293: int
1.59      avsm     1294: mm_answer_term(int sock, Buffer *req)
1.1       provos   1295: {
1.11      mouring  1296:        extern struct monitor *pmonitor;
1.1       provos   1297:        int res, status;
                   1298:
1.14      markus   1299:        debug3("%s: tearing down sessions", __func__);
1.1       provos   1300:
                   1301:        /* The child is terminating */
                   1302:        session_destroy_all(&mm_session_close);
                   1303:
1.11      mouring  1304:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1305:                if (errno != EINTR)
                   1306:                        exit(1);
1.1       provos   1307:
                   1308:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1309:
                   1310:        /* Terminate process */
1.57      deraadt  1311:        exit(res);
1.1       provos   1312: }
                   1313:
                   1314: void
1.11      mouring  1315: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1316: {
                   1317:        if (compat20) {
                   1318:                set_newkeys(MODE_IN);
                   1319:                set_newkeys(MODE_OUT);
                   1320:        } else {
                   1321:                packet_set_protocol_flags(child_state.ssh1protoflags);
1.15      markus   1322:                packet_set_encryption_key(child_state.ssh1key,
                   1323:                    child_state.ssh1keylen, child_state.ssh1cipher);
                   1324:                xfree(child_state.ssh1key);
1.1       provos   1325:        }
                   1326:
1.15      markus   1327:        /* for rc4 and other stateful ciphers */
1.1       provos   1328:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1329:        xfree(child_state.keyout);
                   1330:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1331:        xfree(child_state.keyin);
                   1332:
                   1333:        if (!compat20) {
                   1334:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1335:                xfree(child_state.ivout);
                   1336:                packet_set_iv(MODE_IN, child_state.ivin);
                   1337:                xfree(child_state.ivin);
                   1338:        }
                   1339:
                   1340:        memcpy(&incoming_stream, &child_state.incoming,
                   1341:            sizeof(incoming_stream));
                   1342:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1343:            sizeof(outgoing_stream));
1.3       markus   1344:
1.1       provos   1345:        /* Update with new address */
1.16      djm      1346:        if (options.compression)
                   1347:                mm_init_compression(pmonitor->m_zlib);
1.1       provos   1348:
                   1349:        /* Network I/O buffers */
                   1350:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1351:        buffer_clear(&input);
                   1352:        buffer_append(&input, child_state.input, child_state.ilen);
                   1353:        memset(child_state.input, 0, child_state.ilen);
                   1354:        xfree(child_state.input);
                   1355:
                   1356:        buffer_clear(&output);
                   1357:        buffer_append(&output, child_state.output, child_state.olen);
                   1358:        memset(child_state.output, 0, child_state.olen);
                   1359:        xfree(child_state.output);
                   1360: }
                   1361:
1.2       markus   1362: static Kex *
1.1       provos   1363: mm_get_kex(Buffer *m)
                   1364: {
                   1365:        Kex *kex;
                   1366:        void *blob;
                   1367:        u_int bloblen;
                   1368:
1.75      djm      1369:        kex = xcalloc(1, sizeof(*kex));
1.1       provos   1370:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13      markus   1371:        if ((session_id2 == NULL) ||
                   1372:            (kex->session_id_len != session_id2_len) ||
                   1373:            (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
                   1374:                fatal("mm_get_get: internal error: bad session id");
1.1       provos   1375:        kex->we_need = buffer_get_int(m);
1.34      markus   1376:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.58      djm      1377:        kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.34      markus   1378:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.69      djm      1379:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.1       provos   1380:        kex->server = 1;
                   1381:        kex->hostkey_type = buffer_get_int(m);
                   1382:        kex->kex_type = buffer_get_int(m);
                   1383:        blob = buffer_get_string(m, &bloblen);
                   1384:        buffer_init(&kex->my);
                   1385:        buffer_append(&kex->my, blob, bloblen);
                   1386:        xfree(blob);
                   1387:        blob = buffer_get_string(m, &bloblen);
                   1388:        buffer_init(&kex->peer);
                   1389:        buffer_append(&kex->peer, blob, bloblen);
                   1390:        xfree(blob);
                   1391:        kex->done = 1;
                   1392:        kex->flags = buffer_get_int(m);
                   1393:        kex->client_version_string = buffer_get_string(m, NULL);
                   1394:        kex->server_version_string = buffer_get_string(m, NULL);
                   1395:        kex->load_host_key=&get_hostkey_by_type;
                   1396:        kex->host_key_index=&get_hostkey_index;
                   1397:
                   1398:        return (kex);
                   1399: }
                   1400:
                   1401: /* This function requries careful sanity checking */
                   1402:
                   1403: void
1.11      mouring  1404: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1405: {
                   1406:        Buffer m;
                   1407:        u_char *blob, *p;
                   1408:        u_int bloblen, plen;
1.37      markus   1409:        u_int32_t seqnr, packets;
1.99      markus   1410:        u_int64_t blocks, bytes;
1.1       provos   1411:
1.14      markus   1412:        debug3("%s: Waiting for new keys", __func__);
1.1       provos   1413:
                   1414:        buffer_init(&m);
1.11      mouring  1415:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1416:        if (!compat20) {
                   1417:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1418:                child_state.ssh1cipher = buffer_get_int(&m);
1.15      markus   1419:                child_state.ssh1key = buffer_get_string(&m,
                   1420:                    &child_state.ssh1keylen);
1.1       provos   1421:                child_state.ivout = buffer_get_string(&m,
                   1422:                    &child_state.ivoutlen);
                   1423:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1424:                goto skip;
                   1425:        } else {
                   1426:                /* Get the Kex for rekeying */
1.11      mouring  1427:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1428:        }
                   1429:
                   1430:        blob = buffer_get_string(&m, &bloblen);
                   1431:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1432:        xfree(blob);
                   1433:
1.14      markus   1434:        debug3("%s: Waiting for second key", __func__);
1.1       provos   1435:        blob = buffer_get_string(&m, &bloblen);
                   1436:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1437:        xfree(blob);
1.3       markus   1438:
1.1       provos   1439:        /* Now get sequence numbers for the packets */
1.37      markus   1440:        seqnr = buffer_get_int(&m);
                   1441:        blocks = buffer_get_int64(&m);
                   1442:        packets = buffer_get_int(&m);
1.99      markus   1443:        bytes = buffer_get_int64(&m);
                   1444:        packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1.37      markus   1445:        seqnr = buffer_get_int(&m);
                   1446:        blocks = buffer_get_int64(&m);
                   1447:        packets = buffer_get_int(&m);
1.99      markus   1448:        bytes = buffer_get_int64(&m);
                   1449:        packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1.1       provos   1450:
                   1451:  skip:
                   1452:        /* Get the key context */
                   1453:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1454:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1455:
1.14      markus   1456:        debug3("%s: Getting compression state", __func__);
1.1       provos   1457:        /* Get compression state */
                   1458:        p = buffer_get_string(&m, &plen);
                   1459:        if (plen != sizeof(child_state.outgoing))
1.14      markus   1460:                fatal("%s: bad request size", __func__);
1.1       provos   1461:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1462:        xfree(p);
                   1463:
                   1464:        p = buffer_get_string(&m, &plen);
                   1465:        if (plen != sizeof(child_state.incoming))
1.14      markus   1466:                fatal("%s: bad request size", __func__);
1.1       provos   1467:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1468:        xfree(p);
                   1469:
                   1470:        /* Network I/O buffers */
1.14      markus   1471:        debug3("%s: Getting Network I/O buffers", __func__);
1.1       provos   1472:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1473:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   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 */