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

1.101   ! djm         1: /* $OpenBSD: monitor.c,v 1.100 2008/11/04 08:22:13 djm Exp $ */
1.1       provos      2: /*
                      3:  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
                      4:  * Copyright 2002 Markus Friedl <markus@openbsd.org>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
1.67      stevesk    28: #include <sys/types.h>
                     29: #include <sys/wait.h>
1.79      stevesk    30: #include <sys/socket.h>
1.85      deraadt    31: #include <sys/tree.h>
1.83      stevesk    32: #include <sys/param.h>
1.95      djm        33: #include <sys/queue.h>
1.1       provos     34:
1.78      stevesk    35: #include <openssl/dh.h>
                     36:
1.81      stevesk    37: #include <errno.h>
1.80      stevesk    38: #include <fcntl.h>
1.65      stevesk    39: #include <paths.h>
1.78      stevesk    40: #include <pwd.h>
1.68      stevesk    41: #include <signal.h>
1.84      stevesk    42: #include <stdlib.h>
1.82      stevesk    43: #include <string.h>
1.1       provos     44:
                     45:
1.85      deraadt    46: #include "xmalloc.h"
1.1       provos     47: #include "ssh.h"
1.85      deraadt    48: #include "key.h"
                     49: #include "buffer.h"
                     50: #include "hostfile.h"
1.1       provos     51: #include "auth.h"
1.85      deraadt    52: #include "cipher.h"
1.1       provos     53: #include "kex.h"
                     54: #include "dh.h"
1.88      miod       55: #include <zlib.h>
1.1       provos     56: #include "packet.h"
                     57: #include "auth-options.h"
                     58: #include "sshpty.h"
                     59: #include "channels.h"
                     60: #include "session.h"
                     61: #include "sshlogin.h"
                     62: #include "canohost.h"
                     63: #include "log.h"
                     64: #include "servconf.h"
                     65: #include "monitor.h"
                     66: #include "monitor_mm.h"
1.85      deraadt    67: #ifdef GSSAPI
                     68: #include "ssh-gss.h"
                     69: #endif
1.1       provos     70: #include "monitor_wrap.h"
                     71: #include "monitor_fdpass.h"
                     72: #include "misc.h"
                     73: #include "compat.h"
                     74: #include "ssh2.h"
1.100     djm        75: #include "jpake.h"
1.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.101   ! djm      1230:        if (key->type != KEY_RSA)
        !          1231:                fatal("%s: received bad key type %d", __func__, key->type);
        !          1232:        key->type = KEY_RSA1;
1.1       provos   1233:        if (ssh1_challenge)
                   1234:                BN_clear_free(ssh1_challenge);
                   1235:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1236:
                   1237:        buffer_clear(m);
                   1238:        buffer_put_bignum2(m, ssh1_challenge);
                   1239:
1.14      markus   1240:        debug3("%s sending reply", __func__);
1.59      avsm     1241:        mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1.1       provos   1242:
                   1243:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33      markus   1244:
                   1245:        xfree(blob);
                   1246:        key_free(key);
1.1       provos   1247:        return (0);
                   1248: }
                   1249:
                   1250: int
1.59      avsm     1251: mm_answer_rsa_response(int sock, Buffer *m)
1.1       provos   1252: {
                   1253:        Key *key = NULL;
1.3       markus   1254:        u_char *blob, *response;
                   1255:        u_int blen, len;
                   1256:        int success;
1.1       provos   1257:
1.14      markus   1258:        debug3("%s entering", __func__);
1.1       provos   1259:
                   1260:        if (!authctxt->valid)
1.14      markus   1261:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1262:        if (ssh1_challenge == NULL)
1.14      markus   1263:                fatal("%s: no ssh1_challenge", __func__);
1.1       provos   1264:
                   1265:        blob = buffer_get_string(m, &blen);
                   1266:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1267:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1268:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1269:                fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1       provos   1270:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1271:                fatal("%s: received bad key", __func__);
1.1       provos   1272:        response = buffer_get_string(m, &len);
                   1273:        if (len != 16)
1.14      markus   1274:                fatal("%s: received bad response to challenge", __func__);
1.1       provos   1275:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1276:
1.33      markus   1277:        xfree(blob);
1.1       provos   1278:        key_free(key);
                   1279:        xfree(response);
                   1280:
                   1281:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1282:
                   1283:        /* reset state */
                   1284:        BN_clear_free(ssh1_challenge);
                   1285:        ssh1_challenge = NULL;
                   1286:        monitor_reset_key_state();
                   1287:
                   1288:        buffer_clear(m);
                   1289:        buffer_put_int(m, success);
1.59      avsm     1290:        mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1.1       provos   1291:
                   1292:        return (success);
                   1293: }
                   1294:
                   1295: int
1.59      avsm     1296: mm_answer_term(int sock, Buffer *req)
1.1       provos   1297: {
1.11      mouring  1298:        extern struct monitor *pmonitor;
1.1       provos   1299:        int res, status;
                   1300:
1.14      markus   1301:        debug3("%s: tearing down sessions", __func__);
1.1       provos   1302:
                   1303:        /* The child is terminating */
                   1304:        session_destroy_all(&mm_session_close);
                   1305:
1.11      mouring  1306:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1307:                if (errno != EINTR)
                   1308:                        exit(1);
1.1       provos   1309:
                   1310:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1311:
                   1312:        /* Terminate process */
1.57      deraadt  1313:        exit(res);
1.1       provos   1314: }
                   1315:
                   1316: void
1.11      mouring  1317: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1318: {
                   1319:        if (compat20) {
                   1320:                set_newkeys(MODE_IN);
                   1321:                set_newkeys(MODE_OUT);
                   1322:        } else {
                   1323:                packet_set_protocol_flags(child_state.ssh1protoflags);
1.15      markus   1324:                packet_set_encryption_key(child_state.ssh1key,
                   1325:                    child_state.ssh1keylen, child_state.ssh1cipher);
                   1326:                xfree(child_state.ssh1key);
1.1       provos   1327:        }
                   1328:
1.15      markus   1329:        /* for rc4 and other stateful ciphers */
1.1       provos   1330:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1331:        xfree(child_state.keyout);
                   1332:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1333:        xfree(child_state.keyin);
                   1334:
                   1335:        if (!compat20) {
                   1336:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1337:                xfree(child_state.ivout);
                   1338:                packet_set_iv(MODE_IN, child_state.ivin);
                   1339:                xfree(child_state.ivin);
                   1340:        }
                   1341:
                   1342:        memcpy(&incoming_stream, &child_state.incoming,
                   1343:            sizeof(incoming_stream));
                   1344:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1345:            sizeof(outgoing_stream));
1.3       markus   1346:
1.1       provos   1347:        /* Update with new address */
1.16      djm      1348:        if (options.compression)
                   1349:                mm_init_compression(pmonitor->m_zlib);
1.1       provos   1350:
                   1351:        /* Network I/O buffers */
                   1352:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1353:        buffer_clear(&input);
                   1354:        buffer_append(&input, child_state.input, child_state.ilen);
                   1355:        memset(child_state.input, 0, child_state.ilen);
                   1356:        xfree(child_state.input);
                   1357:
                   1358:        buffer_clear(&output);
                   1359:        buffer_append(&output, child_state.output, child_state.olen);
                   1360:        memset(child_state.output, 0, child_state.olen);
                   1361:        xfree(child_state.output);
                   1362: }
                   1363:
1.2       markus   1364: static Kex *
1.1       provos   1365: mm_get_kex(Buffer *m)
                   1366: {
                   1367:        Kex *kex;
                   1368:        void *blob;
                   1369:        u_int bloblen;
                   1370:
1.75      djm      1371:        kex = xcalloc(1, sizeof(*kex));
1.1       provos   1372:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13      markus   1373:        if ((session_id2 == NULL) ||
                   1374:            (kex->session_id_len != session_id2_len) ||
                   1375:            (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
                   1376:                fatal("mm_get_get: internal error: bad session id");
1.1       provos   1377:        kex->we_need = buffer_get_int(m);
1.34      markus   1378:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1.58      djm      1379:        kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.34      markus   1380:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.69      djm      1381:        kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.1       provos   1382:        kex->server = 1;
                   1383:        kex->hostkey_type = buffer_get_int(m);
                   1384:        kex->kex_type = buffer_get_int(m);
                   1385:        blob = buffer_get_string(m, &bloblen);
                   1386:        buffer_init(&kex->my);
                   1387:        buffer_append(&kex->my, blob, bloblen);
                   1388:        xfree(blob);
                   1389:        blob = buffer_get_string(m, &bloblen);
                   1390:        buffer_init(&kex->peer);
                   1391:        buffer_append(&kex->peer, blob, bloblen);
                   1392:        xfree(blob);
                   1393:        kex->done = 1;
                   1394:        kex->flags = buffer_get_int(m);
                   1395:        kex->client_version_string = buffer_get_string(m, NULL);
                   1396:        kex->server_version_string = buffer_get_string(m, NULL);
                   1397:        kex->load_host_key=&get_hostkey_by_type;
                   1398:        kex->host_key_index=&get_hostkey_index;
                   1399:
                   1400:        return (kex);
                   1401: }
                   1402:
                   1403: /* This function requries careful sanity checking */
                   1404:
                   1405: void
1.11      mouring  1406: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1407: {
                   1408:        Buffer m;
                   1409:        u_char *blob, *p;
                   1410:        u_int bloblen, plen;
1.37      markus   1411:        u_int32_t seqnr, packets;
1.99      markus   1412:        u_int64_t blocks, bytes;
1.1       provos   1413:
1.14      markus   1414:        debug3("%s: Waiting for new keys", __func__);
1.1       provos   1415:
                   1416:        buffer_init(&m);
1.11      mouring  1417:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1418:        if (!compat20) {
                   1419:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1420:                child_state.ssh1cipher = buffer_get_int(&m);
1.15      markus   1421:                child_state.ssh1key = buffer_get_string(&m,
                   1422:                    &child_state.ssh1keylen);
1.1       provos   1423:                child_state.ivout = buffer_get_string(&m,
                   1424:                    &child_state.ivoutlen);
                   1425:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1426:                goto skip;
                   1427:        } else {
                   1428:                /* Get the Kex for rekeying */
1.11      mouring  1429:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1430:        }
                   1431:
                   1432:        blob = buffer_get_string(&m, &bloblen);
                   1433:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1434:        xfree(blob);
                   1435:
1.14      markus   1436:        debug3("%s: Waiting for second key", __func__);
1.1       provos   1437:        blob = buffer_get_string(&m, &bloblen);
                   1438:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1439:        xfree(blob);
1.3       markus   1440:
1.1       provos   1441:        /* Now get sequence numbers for the packets */
1.37      markus   1442:        seqnr = buffer_get_int(&m);
                   1443:        blocks = buffer_get_int64(&m);
                   1444:        packets = buffer_get_int(&m);
1.99      markus   1445:        bytes = buffer_get_int64(&m);
                   1446:        packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1.37      markus   1447:        seqnr = buffer_get_int(&m);
                   1448:        blocks = buffer_get_int64(&m);
                   1449:        packets = buffer_get_int(&m);
1.99      markus   1450:        bytes = buffer_get_int64(&m);
                   1451:        packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1.1       provos   1452:
                   1453:  skip:
                   1454:        /* Get the key context */
                   1455:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1456:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1457:
1.14      markus   1458:        debug3("%s: Getting compression state", __func__);
1.1       provos   1459:        /* Get compression state */
                   1460:        p = buffer_get_string(&m, &plen);
                   1461:        if (plen != sizeof(child_state.outgoing))
1.14      markus   1462:                fatal("%s: bad request size", __func__);
1.1       provos   1463:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1464:        xfree(p);
                   1465:
                   1466:        p = buffer_get_string(&m, &plen);
                   1467:        if (plen != sizeof(child_state.incoming))
1.14      markus   1468:                fatal("%s: bad request size", __func__);
1.1       provos   1469:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1470:        xfree(p);
                   1471:
                   1472:        /* Network I/O buffers */
1.14      markus   1473:        debug3("%s: Getting Network I/O buffers", __func__);
1.1       provos   1474:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1475:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   1476:
                   1477:        buffer_free(&m);
                   1478: }
                   1479:
                   1480:
                   1481: /* Allocation functions for zlib */
                   1482: void *
                   1483: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
                   1484: {
1.30      markus   1485:        size_t len = (size_t) size * ncount;
1.1       provos   1486:        void *address;
                   1487:
1.23      millert  1488:        if (len == 0 || ncount > SIZE_T_MAX / size)
1.18      deraadt  1489:                fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
                   1490:
                   1491:        address = mm_malloc(mm, len);
1.1       provos   1492:
                   1493:        return (address);
                   1494: }
                   1495:
                   1496: void
                   1497: mm_zfree(struct mm_master *mm, void *address)
                   1498: {
                   1499:        mm_free(mm, address);
                   1500: }
                   1501:
                   1502: void
                   1503: mm_init_compression(struct mm_master *mm)
                   1504: {
                   1505:        outgoing_stream.zalloc = (alloc_func)mm_zalloc;
                   1506:        outgoing_stream.zfree = (free_func)mm_zfree;
                   1507:        outgoing_stream.opaque = mm;
                   1508:
                   1509:        incoming_stream.zalloc = (alloc_func)mm_zalloc;
                   1510:        incoming_stream.zfree = (free_func)mm_zfree;
                   1511:        incoming_stream.opaque = mm;
                   1512: }
                   1513:
                   1514: /* XXX */
                   1515:
                   1516: #define FD_CLOSEONEXEC(x) do { \
                   1517:        if (fcntl(x, F_SETFD, 1) == -1) \
                   1518:                fatal("fcntl(%d, F_SETFD)", x); \
                   1519: } while (0)
                   1520:
1.2       markus   1521: static void
1.1       provos   1522: monitor_socketpair(int *pair)
1.3       markus   1523: {
1.1       provos   1524:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14      markus   1525:                fatal("%s: socketpair", __func__);
1.1       provos   1526:        FD_CLOSEONEXEC(pair[0]);
                   1527:        FD_CLOSEONEXEC(pair[1]);
                   1528: }
                   1529:
                   1530: #define MM_MEMSIZE     65536
                   1531:
                   1532: struct monitor *
                   1533: monitor_init(void)
                   1534: {
                   1535:        struct monitor *mon;
                   1536:        int pair[2];
                   1537:
1.75      djm      1538:        mon = xcalloc(1, sizeof(*mon));
1.1       provos   1539:
                   1540:        monitor_socketpair(pair);
                   1541:
                   1542:        mon->m_recvfd = pair[0];
                   1543:        mon->m_sendfd = pair[1];
                   1544:
                   1545:        /* Used to share zlib space across processes */
1.16      djm      1546:        if (options.compression) {
                   1547:                mon->m_zback = mm_create(NULL, MM_MEMSIZE);
                   1548:                mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1       provos   1549:
1.16      djm      1550:                /* Compression needs to share state across borders */
                   1551:                mm_init_compression(mon->m_zlib);
                   1552:        }
1.1       provos   1553:
                   1554:        return mon;
                   1555: }
                   1556:
                   1557: void
                   1558: monitor_reinit(struct monitor *mon)
                   1559: {
                   1560:        int pair[2];
                   1561:
                   1562:        monitor_socketpair(pair);
                   1563:
                   1564:        mon->m_recvfd = pair[0];
                   1565:        mon->m_sendfd = pair[1];
                   1566: }
1.46      markus   1567:
                   1568: #ifdef GSSAPI
                   1569: int
1.59      avsm     1570: mm_answer_gss_setup_ctx(int sock, Buffer *m)
1.46      markus   1571: {
1.59      avsm     1572:        gss_OID_desc goid;
1.46      markus   1573:        OM_uint32 major;
                   1574:        u_int len;
                   1575:
1.59      avsm     1576:        goid.elements = buffer_get_string(m, &len);
                   1577:        goid.length = len;
1.46      markus   1578:
1.59      avsm     1579:        major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1.46      markus   1580:
1.59      avsm     1581:        xfree(goid.elements);
1.46      markus   1582:
                   1583:        buffer_clear(m);
                   1584:        buffer_put_int(m, major);
                   1585:
1.64      stevesk  1586:        mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1.46      markus   1587:
                   1588:        /* Now we have a context, enable the step */
                   1589:        monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
                   1590:
                   1591:        return (0);
                   1592: }
                   1593:
                   1594: int
1.59      avsm     1595: mm_answer_gss_accept_ctx(int sock, Buffer *m)
1.46      markus   1596: {
                   1597:        gss_buffer_desc in;
                   1598:        gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1.64      stevesk  1599:        OM_uint32 major, minor;
1.46      markus   1600:        OM_uint32 flags = 0; /* GSI needs this */
1.47      deraadt  1601:        u_int len;
1.46      markus   1602:
1.47      deraadt  1603:        in.value = buffer_get_string(m, &len);
                   1604:        in.length = len;
1.46      markus   1605:        major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
                   1606:        xfree(in.value);
                   1607:
                   1608:        buffer_clear(m);
                   1609:        buffer_put_int(m, major);
                   1610:        buffer_put_string(m, out.value, out.length);
                   1611:        buffer_put_int(m, flags);
1.59      avsm     1612:        mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1.46      markus   1613:
                   1614:        gss_release_buffer(&minor, &out);
                   1615:
1.64      stevesk  1616:        if (major == GSS_S_COMPLETE) {
1.46      markus   1617:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
                   1618:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.52      markus   1619:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1.46      markus   1620:        }
                   1621:        return (0);
                   1622: }
                   1623:
                   1624: int
1.59      avsm     1625: mm_answer_gss_checkmic(int sock, Buffer *m)
1.52      markus   1626: {
                   1627:        gss_buffer_desc gssbuf, mic;
                   1628:        OM_uint32 ret;
                   1629:        u_int len;
1.54      djm      1630:
1.52      markus   1631:        gssbuf.value = buffer_get_string(m, &len);
                   1632:        gssbuf.length = len;
                   1633:        mic.value = buffer_get_string(m, &len);
                   1634:        mic.length = len;
1.54      djm      1635:
1.52      markus   1636:        ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1.54      djm      1637:
1.52      markus   1638:        xfree(gssbuf.value);
                   1639:        xfree(mic.value);
1.54      djm      1640:
1.52      markus   1641:        buffer_clear(m);
                   1642:        buffer_put_int(m, ret);
1.54      djm      1643:
1.59      avsm     1644:        mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1.54      djm      1645:
1.52      markus   1646:        if (!GSS_ERROR(ret))
                   1647:                monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1.54      djm      1648:
1.52      markus   1649:        return (0);
                   1650: }
                   1651:
                   1652: int
1.59      avsm     1653: mm_answer_gss_userok(int sock, Buffer *m)
1.46      markus   1654: {
                   1655:        int authenticated;
                   1656:
                   1657:        authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
                   1658:
                   1659:        buffer_clear(m);
                   1660:        buffer_put_int(m, authenticated);
                   1661:
                   1662:        debug3("%s: sending result %d", __func__, authenticated);
1.59      avsm     1663:        mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1.46      markus   1664:
1.64      stevesk  1665:        auth_method = "gssapi-with-mic";
1.46      markus   1666:
                   1667:        /* Monitor loop will terminate if authenticated */
                   1668:        return (authenticated);
                   1669: }
                   1670: #endif /* GSSAPI */
1.100     djm      1671:
                   1672: #ifdef JPAKE
                   1673: int
                   1674: mm_answer_jpake_step1(int sock, Buffer *m)
                   1675: {
                   1676:        struct jpake_ctx *pctx;
                   1677:        u_char *x3_proof, *x4_proof;
                   1678:        u_int x3_proof_len, x4_proof_len;
                   1679:
                   1680:        if (!options.zero_knowledge_password_authentication)
                   1681:                fatal("zero_knowledge_password_authentication disabled");
                   1682:
                   1683:        if (authctxt->jpake_ctx != NULL)
                   1684:                fatal("%s: authctxt->jpake_ctx already set (%p)",
                   1685:                    __func__, authctxt->jpake_ctx);
                   1686:        authctxt->jpake_ctx = pctx = jpake_new();
                   1687:
                   1688:        jpake_step1(pctx->grp,
                   1689:            &pctx->server_id, &pctx->server_id_len,
                   1690:            &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
                   1691:            &x3_proof, &x3_proof_len,
                   1692:            &x4_proof, &x4_proof_len);
                   1693:
                   1694:        JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
                   1695:
                   1696:        buffer_clear(m);
                   1697:
                   1698:        buffer_put_string(m, pctx->server_id, pctx->server_id_len);
                   1699:        buffer_put_bignum2(m, pctx->g_x3);
                   1700:        buffer_put_bignum2(m, pctx->g_x4);
                   1701:        buffer_put_string(m, x3_proof, x3_proof_len);
                   1702:        buffer_put_string(m, x4_proof, x4_proof_len);
                   1703:
                   1704:        debug3("%s: sending step1", __func__);
                   1705:        mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
                   1706:
                   1707:        bzero(x3_proof, x3_proof_len);
                   1708:        bzero(x4_proof, x4_proof_len);
                   1709:        xfree(x3_proof);
                   1710:        xfree(x4_proof);
                   1711:
                   1712:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
                   1713:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
                   1714:
                   1715:        return 0;
                   1716: }
                   1717:
                   1718: int
                   1719: mm_answer_jpake_get_pwdata(int sock, Buffer *m)
                   1720: {
                   1721:        struct jpake_ctx *pctx = authctxt->jpake_ctx;
                   1722:        char *hash_scheme, *salt;
                   1723:
                   1724:        if (pctx == NULL)
                   1725:                fatal("%s: pctx == NULL", __func__);
                   1726:
                   1727:        auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
                   1728:
                   1729:        buffer_clear(m);
                   1730:        /* pctx->s is sensitive, not returned to slave */
                   1731:        buffer_put_cstring(m, hash_scheme);
                   1732:        buffer_put_cstring(m, salt);
                   1733:
                   1734:        debug3("%s: sending pwdata", __func__);
                   1735:        mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
                   1736:
                   1737:        bzero(hash_scheme, strlen(hash_scheme));
                   1738:        bzero(salt, strlen(salt));
                   1739:        xfree(hash_scheme);
                   1740:        xfree(salt);
                   1741:
                   1742:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
                   1743:
                   1744:        return 0;
                   1745: }
                   1746:
                   1747: int
                   1748: mm_answer_jpake_step2(int sock, Buffer *m)
                   1749: {
                   1750:        struct jpake_ctx *pctx = authctxt->jpake_ctx;
                   1751:        u_char *x1_proof, *x2_proof, *x4_s_proof;
                   1752:        u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
                   1753:
                   1754:        if (pctx == NULL)
                   1755:                fatal("%s: pctx == NULL", __func__);
                   1756:
                   1757:        if ((pctx->g_x1 = BN_new()) == NULL ||
                   1758:            (pctx->g_x2 = BN_new()) == NULL)
                   1759:                fatal("%s: BN_new", __func__);
                   1760:        buffer_get_bignum2(m, pctx->g_x1);
                   1761:        buffer_get_bignum2(m, pctx->g_x2);
                   1762:        pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
                   1763:        x1_proof = buffer_get_string(m, &x1_proof_len);
                   1764:        x2_proof = buffer_get_string(m, &x2_proof_len);
                   1765:
                   1766:        jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
                   1767:            pctx->g_x1, pctx->g_x2, pctx->x4,
                   1768:            pctx->client_id, pctx->client_id_len,
                   1769:            pctx->server_id, pctx->server_id_len,
                   1770:            x1_proof, x1_proof_len,
                   1771:            x2_proof, x2_proof_len,
                   1772:            &pctx->b,
                   1773:            &x4_s_proof, &x4_s_proof_len);
                   1774:
                   1775:        JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
                   1776:
                   1777:        bzero(x1_proof, x1_proof_len);
                   1778:        bzero(x2_proof, x2_proof_len);
                   1779:        xfree(x1_proof);
                   1780:        xfree(x2_proof);
                   1781:
                   1782:        buffer_clear(m);
                   1783:
                   1784:        buffer_put_bignum2(m, pctx->b);
                   1785:        buffer_put_string(m, x4_s_proof, x4_s_proof_len);
                   1786:
                   1787:        debug3("%s: sending step2", __func__);
                   1788:        mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
                   1789:
                   1790:        bzero(x4_s_proof, x4_s_proof_len);
                   1791:        xfree(x4_s_proof);
                   1792:
                   1793:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
                   1794:
                   1795:        return 0;
                   1796: }
                   1797:
                   1798: int
                   1799: mm_answer_jpake_key_confirm(int sock, Buffer *m)
                   1800: {
                   1801:        struct jpake_ctx *pctx = authctxt->jpake_ctx;
                   1802:        u_char *x2_s_proof;
                   1803:        u_int x2_s_proof_len;
                   1804:
                   1805:        if (pctx == NULL)
                   1806:                fatal("%s: pctx == NULL", __func__);
                   1807:
                   1808:        if ((pctx->a = BN_new()) == NULL)
                   1809:                fatal("%s: BN_new", __func__);
                   1810:        buffer_get_bignum2(m, pctx->a);
                   1811:        x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
                   1812:
                   1813:        jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
                   1814:            pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
                   1815:            pctx->server_id, pctx->server_id_len,
                   1816:            pctx->client_id, pctx->client_id_len,
                   1817:            session_id2, session_id2_len,
                   1818:            x2_s_proof, x2_s_proof_len,
                   1819:            &pctx->k,
                   1820:            &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
                   1821:
                   1822:        JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
                   1823:
                   1824:        bzero(x2_s_proof, x2_s_proof_len);
                   1825:        buffer_clear(m);
                   1826:
                   1827:        /* pctx->k is sensitive, not sent */
                   1828:        buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
                   1829:
                   1830:        debug3("%s: sending confirmation hash", __func__);
                   1831:        mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
                   1832:
                   1833:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
                   1834:
                   1835:        return 0;
                   1836: }
                   1837:
                   1838: int
                   1839: mm_answer_jpake_check_confirm(int sock, Buffer *m)
                   1840: {
                   1841:        int authenticated = 0;
                   1842:        u_char *peer_confirm_hash;
                   1843:        u_int peer_confirm_hash_len;
                   1844:        struct jpake_ctx *pctx = authctxt->jpake_ctx;
                   1845:
                   1846:        if (pctx == NULL)
                   1847:                fatal("%s: pctx == NULL", __func__);
                   1848:
                   1849:        peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
                   1850:
                   1851:        authenticated = jpake_check_confirm(pctx->k,
                   1852:            pctx->client_id, pctx->client_id_len,
                   1853:            session_id2, session_id2_len,
                   1854:            peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
                   1855:
                   1856:        JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
                   1857:
                   1858:        bzero(peer_confirm_hash, peer_confirm_hash_len);
                   1859:        xfree(peer_confirm_hash);
                   1860:
                   1861:        buffer_clear(m);
                   1862:        buffer_put_int(m, authenticated);
                   1863:
                   1864:        debug3("%s: sending result %d", __func__, authenticated);
                   1865:        mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
                   1866:
                   1867:        monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
                   1868:
                   1869:        auth_method = "jpake-01@openssh.com";
                   1870:        return authenticated;
                   1871: }
                   1872:
                   1873: #endif /* JPAKE */