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

1.1       provos      1: /*
                      2:  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
                      3:  * Copyright 2002 Markus Friedl <markus@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include "includes.h"
1.42    ! markus     28: RCSID("$OpenBSD: monitor.c,v 1.41 2003/05/24 09:30:40 djm Exp $");
1.1       provos     29:
                     30: #include <openssl/dh.h>
                     31:
                     32: #ifdef SKEY
                     33: #include <skey.h>
                     34: #endif
                     35:
                     36: #include "ssh.h"
                     37: #include "auth.h"
                     38: #include "kex.h"
                     39: #include "dh.h"
                     40: #include "zlib.h"
                     41: #include "packet.h"
                     42: #include "auth-options.h"
                     43: #include "sshpty.h"
                     44: #include "channels.h"
                     45: #include "session.h"
                     46: #include "sshlogin.h"
                     47: #include "canohost.h"
                     48: #include "log.h"
                     49: #include "servconf.h"
                     50: #include "monitor.h"
                     51: #include "monitor_mm.h"
                     52: #include "monitor_wrap.h"
                     53: #include "monitor_fdpass.h"
                     54: #include "xmalloc.h"
                     55: #include "misc.h"
                     56: #include "buffer.h"
                     57: #include "bufaux.h"
                     58: #include "compat.h"
                     59: #include "ssh2.h"
                     60: #include "mpaux.h"
                     61:
                     62: /* Imports */
                     63: extern ServerOptions options;
                     64: extern u_int utmp_len;
                     65: extern Newkeys *current_keys[];
                     66: extern z_stream incoming_stream;
                     67: extern z_stream outgoing_stream;
                     68: extern u_char session_id[];
                     69: extern Buffer input, output;
                     70: extern Buffer auth_debug;
                     71: extern int auth_debug_init;
                     72:
                     73: /* State exported from the child */
                     74:
                     75: struct {
                     76:        z_stream incoming;
                     77:        z_stream outgoing;
                     78:        u_char *keyin;
                     79:        u_int keyinlen;
                     80:        u_char *keyout;
                     81:        u_int keyoutlen;
                     82:        u_char *ivin;
                     83:        u_int ivinlen;
                     84:        u_char *ivout;
                     85:        u_int ivoutlen;
1.15      markus     86:        u_char *ssh1key;
                     87:        u_int ssh1keylen;
1.1       provos     88:        int ssh1cipher;
                     89:        int ssh1protoflags;
                     90:        u_char *input;
                     91:        u_int ilen;
                     92:        u_char *output;
                     93:        u_int olen;
                     94: } child_state;
                     95:
                     96: /* Functions on the montior that answer unprivileged requests */
                     97:
                     98: int mm_answer_moduli(int, Buffer *);
                     99: int mm_answer_sign(int, Buffer *);
                    100: int mm_answer_pwnamallow(int, Buffer *);
1.10      djm       101: int mm_answer_auth2_read_banner(int, Buffer *);
1.1       provos    102: int mm_answer_authserv(int, Buffer *);
                    103: int mm_answer_authpassword(int, Buffer *);
                    104: int mm_answer_bsdauthquery(int, Buffer *);
                    105: int mm_answer_bsdauthrespond(int, Buffer *);
                    106: int mm_answer_skeyquery(int, Buffer *);
                    107: int mm_answer_skeyrespond(int, Buffer *);
                    108: int mm_answer_keyallowed(int, Buffer *);
                    109: int mm_answer_keyverify(int, Buffer *);
                    110: int mm_answer_pty(int, Buffer *);
                    111: int mm_answer_pty_cleanup(int, Buffer *);
                    112: int mm_answer_term(int, Buffer *);
                    113: int mm_answer_rsa_keyallowed(int, Buffer *);
                    114: int mm_answer_rsa_challenge(int, Buffer *);
                    115: int mm_answer_rsa_response(int, Buffer *);
                    116: int mm_answer_sesskey(int, Buffer *);
                    117: int mm_answer_sessid(int, Buffer *);
                    118:
1.29      markus    119: #ifdef KRB4
                    120: int mm_answer_krb4(int, Buffer *);
                    121: #endif
1.25      itojun    122: #ifdef KRB5
                    123: int mm_answer_krb5(int, Buffer *);
                    124: #endif
                    125:
1.1       provos    126: static Authctxt *authctxt;
                    127: static BIGNUM *ssh1_challenge = NULL;  /* used for ssh1 rsa auth */
                    128:
                    129: /* local state for key verify */
                    130: static u_char *key_blob = NULL;
                    131: static u_int key_bloblen = 0;
                    132: static int key_blobtype = MM_NOKEY;
1.26      markus    133: static char *hostbased_cuser = NULL;
                    134: static char *hostbased_chost = NULL;
1.1       provos    135: static char *auth_method = "unknown";
1.13      markus    136: static int session_id2_len = 0;
                    137: static u_char *session_id2 = NULL;
1.40      markus    138: static pid_t monitor_child_pid;
1.1       provos    139:
                    140: struct mon_table {
                    141:        enum monitor_reqtype type;
                    142:        int flags;
                    143:        int (*f)(int, Buffer *);
                    144: };
                    145:
                    146: #define MON_ISAUTH     0x0004  /* Required for Authentication */
                    147: #define MON_AUTHDECIDE 0x0008  /* Decides Authentication */
                    148: #define MON_ONCE       0x0010  /* Disable after calling */
                    149:
                    150: #define MON_AUTH       (MON_ISAUTH|MON_AUTHDECIDE)
                    151:
                    152: #define MON_PERMIT     0x1000  /* Request is permitted */
                    153:
                    154: struct mon_table mon_dispatch_proto20[] = {
                    155:     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
                    156:     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
                    157:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    158:     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
1.10      djm       159:     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
1.1       provos    160:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
                    161: #ifdef BSD_AUTH
                    162:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
                    163:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
                    164: #endif
                    165: #ifdef SKEY
                    166:     {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
                    167:     {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
                    168: #endif
                    169:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
                    170:     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
1.39      markus    171: #ifdef KRB5
                    172:     {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
                    173: #endif
1.1       provos    174:     {0, 0, NULL}
                    175: };
                    176:
                    177: struct mon_table mon_dispatch_postauth20[] = {
                    178:     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
                    179:     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
                    180:     {MONITOR_REQ_PTY, 0, mm_answer_pty},
                    181:     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
                    182:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    183:     {0, 0, NULL}
                    184: };
                    185:
                    186: struct mon_table mon_dispatch_proto15[] = {
                    187:     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
                    188:     {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
                    189:     {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
                    190:     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
                    191:     {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
                    192:     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
                    193:     {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
                    194:     {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
                    195: #ifdef BSD_AUTH
                    196:     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
                    197:     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
                    198: #endif
                    199: #ifdef SKEY
                    200:     {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
                    201:     {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
                    202: #endif
1.29      markus    203: #ifdef KRB4
                    204:     {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
                    205: #endif
1.25      itojun    206: #ifdef KRB5
                    207:     {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
                    208: #endif
1.1       provos    209:     {0, 0, NULL}
                    210: };
                    211:
                    212: struct mon_table mon_dispatch_postauth15[] = {
                    213:     {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
                    214:     {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
                    215:     {MONITOR_REQ_TERM, 0, mm_answer_term},
                    216:     {0, 0, NULL}
                    217: };
                    218:
                    219: struct mon_table *mon_dispatch;
                    220:
                    221: /* Specifies if a certain message is allowed at the moment */
                    222:
                    223: static void
                    224: monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
                    225: {
                    226:        while (ent->f != NULL) {
                    227:                if (ent->type == type) {
                    228:                        ent->flags &= ~MON_PERMIT;
                    229:                        ent->flags |= permit ? MON_PERMIT : 0;
                    230:                        return;
                    231:                }
                    232:                ent++;
                    233:        }
                    234: }
                    235:
                    236: static void
                    237: monitor_permit_authentications(int permit)
                    238: {
                    239:        struct mon_table *ent = mon_dispatch;
                    240:
                    241:        while (ent->f != NULL) {
                    242:                if (ent->flags & MON_AUTH) {
                    243:                        ent->flags &= ~MON_PERMIT;
                    244:                        ent->flags |= permit ? MON_PERMIT : 0;
                    245:                }
                    246:                ent++;
                    247:        }
                    248: }
                    249:
                    250: Authctxt *
1.11      mouring   251: monitor_child_preauth(struct monitor *pmonitor)
1.1       provos    252: {
                    253:        struct mon_table *ent;
                    254:        int authenticated = 0;
                    255:
                    256:        debug3("preauth child monitor started");
                    257:
                    258:        if (compat20) {
                    259:                mon_dispatch = mon_dispatch_proto20;
                    260:
                    261:                /* Permit requests for moduli and signatures */
                    262:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    263:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    264:        } else {
                    265:                mon_dispatch = mon_dispatch_proto15;
                    266:
                    267:                monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                    268:        }
                    269:
                    270:        authctxt = authctxt_new();
                    271:
                    272:        /* The first few requests do not require asynchronous access */
                    273:        while (!authenticated) {
1.11      mouring   274:                authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
1.1       provos    275:                if (authenticated) {
                    276:                        if (!(ent->flags & MON_AUTHDECIDE))
                    277:                                fatal("%s: unexpected authentication from %d",
1.14      markus    278:                                    __func__, ent->type);
1.1       provos    279:                        if (authctxt->pw->pw_uid == 0 &&
                    280:                            !auth_root_allowed(auth_method))
                    281:                                authenticated = 0;
                    282:                }
                    283:
                    284:                if (ent->flags & MON_AUTHDECIDE) {
                    285:                        auth_log(authctxt, authenticated, auth_method,
                    286:                            compat20 ? " ssh2" : "");
                    287:                        if (!authenticated)
                    288:                                authctxt->failures++;
                    289:                }
                    290:        }
                    291:
                    292:        if (!authctxt->valid)
1.14      markus    293:                fatal("%s: authenticated invalid user", __func__);
1.1       provos    294:
                    295:        debug("%s: %s has been authenticated by privileged process",
1.14      markus    296:            __func__, authctxt->user);
1.1       provos    297:
1.11      mouring   298:        mm_get_keystate(pmonitor);
1.1       provos    299:
                    300:        return (authctxt);
                    301: }
                    302:
1.40      markus    303: static void
                    304: monitor_set_child_handler(pid_t pid)
                    305: {
                    306:        monitor_child_pid = pid;
                    307: }
                    308:
                    309: static void
                    310: monitor_child_handler(int signal)
                    311: {
                    312:        kill(monitor_child_pid, signal);
                    313: }
                    314:
1.1       provos    315: void
1.11      mouring   316: monitor_child_postauth(struct monitor *pmonitor)
1.1       provos    317: {
1.40      markus    318:        monitor_set_child_handler(pmonitor->m_pid);
                    319:        signal(SIGHUP, &monitor_child_handler);
                    320:        signal(SIGTERM, &monitor_child_handler);
                    321:
1.1       provos    322:        if (compat20) {
                    323:                mon_dispatch = mon_dispatch_postauth20;
                    324:
                    325:                /* Permit requests for moduli and signatures */
                    326:                monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
                    327:                monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
                    328:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    329:
                    330:        } else {
                    331:                mon_dispatch = mon_dispatch_postauth15;
                    332:                monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
                    333:        }
                    334:        if (!no_pty_flag) {
                    335:                monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
                    336:                monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
                    337:        }
                    338:
                    339:        for (;;)
1.11      mouring   340:                monitor_read(pmonitor, mon_dispatch, NULL);
1.1       provos    341: }
                    342:
                    343: void
1.11      mouring   344: monitor_sync(struct monitor *pmonitor)
1.1       provos    345: {
1.16      djm       346:        if (options.compression) {
                    347:                /* The member allocation is not visible, so sync it */
                    348:                mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
                    349:        }
1.1       provos    350: }
                    351:
                    352: int
1.11      mouring   353: monitor_read(struct monitor *pmonitor, struct mon_table *ent,
1.1       provos    354:     struct mon_table **pent)
                    355: {
                    356:        Buffer m;
                    357:        int ret;
                    358:        u_char type;
                    359:
                    360:        buffer_init(&m);
                    361:
1.11      mouring   362:        mm_request_receive(pmonitor->m_sendfd, &m);
1.1       provos    363:        type = buffer_get_char(&m);
                    364:
1.14      markus    365:        debug3("%s: checking request %d", __func__, type);
1.1       provos    366:
                    367:        while (ent->f != NULL) {
                    368:                if (ent->type == type)
                    369:                        break;
                    370:                ent++;
                    371:        }
                    372:
                    373:        if (ent->f != NULL) {
                    374:                if (!(ent->flags & MON_PERMIT))
1.14      markus    375:                        fatal("%s: unpermitted request %d", __func__,
1.1       provos    376:                            type);
1.11      mouring   377:                ret = (*ent->f)(pmonitor->m_sendfd, &m);
1.1       provos    378:                buffer_free(&m);
                    379:
                    380:                /* The child may use this request only once, disable it */
                    381:                if (ent->flags & MON_ONCE) {
1.14      markus    382:                        debug2("%s: %d used once, disabling now", __func__,
1.1       provos    383:                            type);
                    384:                        ent->flags &= ~MON_PERMIT;
                    385:                }
                    386:
                    387:                if (pent != NULL)
                    388:                        *pent = ent;
1.3       markus    389:
1.1       provos    390:                return ret;
                    391:        }
                    392:
1.14      markus    393:        fatal("%s: unsupported request: %d", __func__, type);
1.1       provos    394:
                    395:        /* NOTREACHED */
                    396:        return (-1);
                    397: }
                    398:
                    399: /* allowed key state */
                    400: static int
                    401: monitor_allowed_key(u_char *blob, u_int bloblen)
                    402: {
                    403:        /* make sure key is allowed */
                    404:        if (key_blob == NULL || key_bloblen != bloblen ||
                    405:            memcmp(key_blob, blob, key_bloblen))
                    406:                return (0);
                    407:        return (1);
                    408: }
                    409:
                    410: static void
                    411: monitor_reset_key_state(void)
                    412: {
                    413:        /* reset state */
                    414:        if (key_blob != NULL)
                    415:                xfree(key_blob);
                    416:        if (hostbased_cuser != NULL)
                    417:                xfree(hostbased_cuser);
                    418:        if (hostbased_chost != NULL)
                    419:                xfree(hostbased_chost);
                    420:        key_blob = NULL;
                    421:        key_bloblen = 0;
                    422:        key_blobtype = MM_NOKEY;
                    423:        hostbased_cuser = NULL;
                    424:        hostbased_chost = NULL;
                    425: }
                    426:
                    427: int
                    428: mm_answer_moduli(int socket, Buffer *m)
                    429: {
                    430:        DH *dh;
                    431:        int min, want, max;
                    432:
                    433:        min = buffer_get_int(m);
                    434:        want = buffer_get_int(m);
                    435:        max = buffer_get_int(m);
                    436:
                    437:        debug3("%s: got parameters: %d %d %d",
1.14      markus    438:            __func__, min, want, max);
1.1       provos    439:        /* We need to check here, too, in case the child got corrupted */
                    440:        if (max < min || want < min || max < want)
                    441:                fatal("%s: bad parameters: %d %d %d",
1.14      markus    442:                    __func__, min, want, max);
1.1       provos    443:
                    444:        buffer_clear(m);
                    445:
                    446:        dh = choose_dh(min, want, max);
                    447:        if (dh == NULL) {
                    448:                buffer_put_char(m, 0);
                    449:                return (0);
                    450:        } else {
                    451:                /* Send first bignum */
                    452:                buffer_put_char(m, 1);
                    453:                buffer_put_bignum2(m, dh->p);
                    454:                buffer_put_bignum2(m, dh->g);
1.3       markus    455:
1.1       provos    456:                DH_free(dh);
                    457:        }
                    458:        mm_request_send(socket, MONITOR_ANS_MODULI, m);
                    459:        return (0);
                    460: }
                    461:
                    462: int
                    463: mm_answer_sign(int socket, Buffer *m)
                    464: {
                    465:        Key *key;
                    466:        u_char *p;
                    467:        u_char *signature;
                    468:        u_int siglen, datlen;
                    469:        int keyid;
1.3       markus    470:
1.14      markus    471:        debug3("%s", __func__);
1.1       provos    472:
1.3       markus    473:        keyid = buffer_get_int(m);
                    474:        p = buffer_get_string(m, &datlen);
1.1       provos    475:
                    476:        if (datlen != 20)
1.19      deraadt   477:                fatal("%s: data length incorrect: %u", __func__, datlen);
1.1       provos    478:
1.13      markus    479:        /* save session id, it will be passed on the first call */
                    480:        if (session_id2_len == 0) {
                    481:                session_id2_len = datlen;
                    482:                session_id2 = xmalloc(session_id2_len);
                    483:                memcpy(session_id2, p, session_id2_len);
                    484:        }
                    485:
1.1       provos    486:        if ((key = get_hostkey_by_index(keyid)) == NULL)
1.14      markus    487:                fatal("%s: no hostkey from index %d", __func__, keyid);
1.1       provos    488:        if (key_sign(key, &signature, &siglen, p, datlen) < 0)
1.14      markus    489:                fatal("%s: key_sign failed", __func__);
1.1       provos    490:
1.19      deraadt   491:        debug3("%s: signature %p(%u)", __func__, signature, siglen);
1.1       provos    492:
                    493:        buffer_clear(m);
                    494:        buffer_put_string(m, signature, siglen);
                    495:
                    496:        xfree(p);
                    497:        xfree(signature);
                    498:
                    499:        mm_request_send(socket, MONITOR_ANS_SIGN, m);
                    500:
                    501:        /* Turn on permissions for getpwnam */
                    502:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                    503:
                    504:        return (0);
                    505: }
                    506:
                    507: /* Retrieves the password entry and also checks if the user is permitted */
                    508:
                    509: int
                    510: mm_answer_pwnamallow(int socket, Buffer *m)
                    511: {
                    512:        char *login;
                    513:        struct passwd *pwent;
                    514:        int allowed = 0;
1.3       markus    515:
1.14      markus    516:        debug3("%s", __func__);
1.1       provos    517:
                    518:        if (authctxt->attempt++ != 0)
1.14      markus    519:                fatal("%s: multiple attempts for getpwnam", __func__);
1.1       provos    520:
                    521:        login = buffer_get_string(m, NULL);
                    522:
                    523:        pwent = getpwnamallow(login);
                    524:
                    525:        authctxt->user = xstrdup(login);
                    526:        setproctitle("%s [priv]", pwent ? login : "unknown");
                    527:        xfree(login);
                    528:
                    529:        buffer_clear(m);
                    530:
                    531:        if (pwent == NULL) {
                    532:                buffer_put_char(m, 0);
                    533:                goto out;
                    534:        }
                    535:
                    536:        allowed = 1;
1.4       markus    537:        authctxt->pw = pwent;
1.1       provos    538:        authctxt->valid = 1;
                    539:
                    540:        buffer_put_char(m, 1);
                    541:        buffer_put_string(m, pwent, sizeof(struct passwd));
                    542:        buffer_put_cstring(m, pwent->pw_name);
                    543:        buffer_put_cstring(m, "*");
                    544:        buffer_put_cstring(m, pwent->pw_gecos);
                    545:        buffer_put_cstring(m, pwent->pw_class);
                    546:        buffer_put_cstring(m, pwent->pw_dir);
                    547:        buffer_put_cstring(m, pwent->pw_shell);
                    548:
                    549:  out:
1.14      markus    550:        debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
1.1       provos    551:        mm_request_send(socket, MONITOR_ANS_PWNAM, m);
                    552:
                    553:        /* For SSHv1 allow authentication now */
                    554:        if (!compat20)
                    555:                monitor_permit_authentications(1);
1.10      djm       556:        else {
1.1       provos    557:                /* Allow service/style information on the auth context */
                    558:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
1.10      djm       559:                monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
                    560:        }
                    561:
                    562:
                    563:        return (0);
                    564: }
                    565:
                    566: int mm_answer_auth2_read_banner(int socket, Buffer *m)
                    567: {
                    568:        char *banner;
                    569:
                    570:        buffer_clear(m);
                    571:        banner = auth2_read_banner();
                    572:        buffer_put_cstring(m, banner != NULL ? banner : "");
                    573:        mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
1.1       provos    574:
1.10      djm       575:        if (banner != NULL)
1.21      deraadt   576:                xfree(banner);
1.1       provos    577:
                    578:        return (0);
                    579: }
                    580:
                    581: int
                    582: mm_answer_authserv(int socket, Buffer *m)
                    583: {
                    584:        monitor_permit_authentications(1);
                    585:
                    586:        authctxt->service = buffer_get_string(m, NULL);
                    587:        authctxt->style = buffer_get_string(m, NULL);
1.6       stevesk   588:        debug3("%s: service=%s, style=%s",
1.14      markus    589:            __func__, authctxt->service, authctxt->style);
1.6       stevesk   590:
1.1       provos    591:        if (strlen(authctxt->style) == 0) {
                    592:                xfree(authctxt->style);
                    593:                authctxt->style = NULL;
                    594:        }
                    595:
                    596:        return (0);
                    597: }
                    598:
                    599: int
                    600: mm_answer_authpassword(int socket, Buffer *m)
                    601: {
                    602:        static int call_count;
                    603:        char *passwd;
1.22      stevesk   604:        int authenticated;
                    605:        u_int plen;
1.1       provos    606:
                    607:        passwd = buffer_get_string(m, &plen);
                    608:        /* Only authenticate if the context is valid */
1.12      markus    609:        authenticated = options.password_authentication &&
                    610:            authctxt->valid && auth_password(authctxt, passwd);
1.1       provos    611:        memset(passwd, 0, strlen(passwd));
                    612:        xfree(passwd);
                    613:
                    614:        buffer_clear(m);
                    615:        buffer_put_int(m, authenticated);
                    616:
1.14      markus    617:        debug3("%s: sending result %d", __func__, authenticated);
1.1       provos    618:        mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
                    619:
                    620:        call_count++;
                    621:        if (plen == 0 && call_count == 1)
                    622:                auth_method = "none";
                    623:        else
                    624:                auth_method = "password";
                    625:
                    626:        /* Causes monitor loop to terminate if authenticated */
                    627:        return (authenticated);
                    628: }
                    629:
                    630: #ifdef BSD_AUTH
                    631: int
                    632: mm_answer_bsdauthquery(int socket, Buffer *m)
                    633: {
                    634:        char *name, *infotxt;
                    635:        u_int numprompts;
                    636:        u_int *echo_on;
                    637:        char **prompts;
1.31      markus    638:        u_int success;
1.1       provos    639:
1.31      markus    640:        success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
                    641:            &prompts, &echo_on) < 0 ? 0 : 1;
1.1       provos    642:
                    643:        buffer_clear(m);
1.31      markus    644:        buffer_put_int(m, success);
                    645:        if (success)
1.1       provos    646:                buffer_put_cstring(m, prompts[0]);
                    647:
1.31      markus    648:        debug3("%s: sending challenge success: %u", __func__, success);
1.1       provos    649:        mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
                    650:
1.31      markus    651:        if (success) {
1.1       provos    652:                xfree(name);
                    653:                xfree(infotxt);
                    654:                xfree(prompts);
                    655:                xfree(echo_on);
                    656:        }
                    657:
                    658:        return (0);
                    659: }
                    660:
                    661: int
                    662: mm_answer_bsdauthrespond(int socket, Buffer *m)
                    663: {
                    664:        char *response;
                    665:        int authok;
                    666:
                    667:        if (authctxt->as == 0)
1.14      markus    668:                fatal("%s: no bsd auth session", __func__);
1.1       provos    669:
                    670:        response = buffer_get_string(m, NULL);
1.12      markus    671:        authok = options.challenge_response_authentication &&
                    672:            auth_userresponse(authctxt->as, response, 0);
1.1       provos    673:        authctxt->as = NULL;
1.14      markus    674:        debug3("%s: <%s> = <%d>", __func__, response, authok);
1.1       provos    675:        xfree(response);
                    676:
                    677:        buffer_clear(m);
                    678:        buffer_put_int(m, authok);
                    679:
1.14      markus    680:        debug3("%s: sending authenticated: %d", __func__, authok);
1.1       provos    681:        mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
                    682:
                    683:        auth_method = "bsdauth";
                    684:
                    685:        return (authok != 0);
                    686: }
                    687: #endif
                    688:
                    689: #ifdef SKEY
                    690: int
                    691: mm_answer_skeyquery(int socket, Buffer *m)
                    692: {
                    693:        struct skey skey;
                    694:        char challenge[1024];
1.31      markus    695:        u_int success;
1.1       provos    696:
1.31      markus    697:        success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
1.1       provos    698:
                    699:        buffer_clear(m);
1.31      markus    700:        buffer_put_int(m, success);
                    701:        if (success)
1.1       provos    702:                buffer_put_cstring(m, challenge);
                    703:
1.31      markus    704:        debug3("%s: sending challenge success: %u", __func__, success);
1.1       provos    705:        mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
                    706:
                    707:        return (0);
                    708: }
                    709:
                    710: int
                    711: mm_answer_skeyrespond(int socket, Buffer *m)
                    712: {
                    713:        char *response;
                    714:        int authok;
                    715:
                    716:        response = buffer_get_string(m, NULL);
                    717:
1.12      markus    718:        authok = (options.challenge_response_authentication &&
                    719:            authctxt->valid &&
1.1       provos    720:            skey_haskey(authctxt->pw->pw_name) == 0 &&
                    721:            skey_passcheck(authctxt->pw->pw_name, response) != -1);
                    722:
                    723:        xfree(response);
                    724:
                    725:        buffer_clear(m);
                    726:        buffer_put_int(m, authok);
                    727:
1.14      markus    728:        debug3("%s: sending authenticated: %d", __func__, authok);
1.1       provos    729:        mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
                    730:
                    731:        auth_method = "skey";
                    732:
                    733:        return (authok != 0);
                    734: }
                    735: #endif
                    736:
1.2       markus    737: static void
1.1       provos    738: mm_append_debug(Buffer *m)
                    739: {
                    740:        if (auth_debug_init && buffer_len(&auth_debug)) {
1.14      markus    741:                debug3("%s: Appending debug messages for child", __func__);
1.1       provos    742:                buffer_append(m, buffer_ptr(&auth_debug),
                    743:                    buffer_len(&auth_debug));
                    744:                buffer_clear(&auth_debug);
                    745:        }
                    746: }
                    747:
                    748: int
                    749: mm_answer_keyallowed(int socket, Buffer *m)
                    750: {
                    751:        Key *key;
1.26      markus    752:        char *cuser, *chost;
                    753:        u_char *blob;
1.1       provos    754:        u_int bloblen;
                    755:        enum mm_keytype type = 0;
                    756:        int allowed = 0;
                    757:
1.14      markus    758:        debug3("%s entering", __func__);
1.3       markus    759:
1.1       provos    760:        type = buffer_get_int(m);
                    761:        cuser = buffer_get_string(m, NULL);
                    762:        chost = buffer_get_string(m, NULL);
                    763:        blob = buffer_get_string(m, &bloblen);
                    764:
                    765:        key = key_from_blob(blob, bloblen);
                    766:
                    767:        if ((compat20 && type == MM_RSAHOSTKEY) ||
                    768:            (!compat20 && type != MM_RSAHOSTKEY))
1.14      markus    769:                fatal("%s: key type and protocol mismatch", __func__);
1.1       provos    770:
1.14      markus    771:        debug3("%s: key_from_blob: %p", __func__, key);
1.1       provos    772:
                    773:        if (key != NULL && authctxt->pw != NULL) {
                    774:                switch(type) {
                    775:                case MM_USERKEY:
1.12      markus    776:                        allowed = options.pubkey_authentication &&
                    777:                            user_key_allowed(authctxt->pw, key);
1.1       provos    778:                        break;
                    779:                case MM_HOSTKEY:
1.12      markus    780:                        allowed = options.hostbased_authentication &&
                    781:                            hostbased_key_allowed(authctxt->pw,
1.1       provos    782:                            cuser, chost, key);
                    783:                        break;
                    784:                case MM_RSAHOSTKEY:
                    785:                        key->type = KEY_RSA1; /* XXX */
1.12      markus    786:                        allowed = options.rhosts_rsa_authentication &&
                    787:                            auth_rhosts_rsa_key_allowed(authctxt->pw,
1.1       provos    788:                            cuser, chost, key);
                    789:                        break;
                    790:                default:
1.14      markus    791:                        fatal("%s: unknown key type %d", __func__, type);
1.1       provos    792:                        break;
                    793:                }
1.33      markus    794:        }
                    795:        if (key != NULL)
1.1       provos    796:                key_free(key);
                    797:
                    798:        /* clear temporarily storage (used by verify) */
                    799:        monitor_reset_key_state();
                    800:
                    801:        if (allowed) {
                    802:                /* Save temporarily for comparison in verify */
                    803:                key_blob = blob;
                    804:                key_bloblen = bloblen;
                    805:                key_blobtype = type;
                    806:                hostbased_cuser = cuser;
                    807:                hostbased_chost = chost;
                    808:        }
                    809:
                    810:        debug3("%s: key %p is %s",
1.14      markus    811:            __func__, key, allowed ? "allowed" : "disallowed");
1.1       provos    812:
                    813:        buffer_clear(m);
                    814:        buffer_put_int(m, allowed);
1.32      markus    815:        buffer_put_int(m, forced_command != NULL);
1.1       provos    816:
                    817:        mm_append_debug(m);
                    818:
                    819:        mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
                    820:
                    821:        if (type == MM_RSAHOSTKEY)
                    822:                monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                    823:
                    824:        return (0);
                    825: }
                    826:
                    827: static int
                    828: monitor_valid_userblob(u_char *data, u_int datalen)
                    829: {
                    830:        Buffer b;
1.26      markus    831:        char *p;
1.1       provos    832:        u_int len;
                    833:        int fail = 0;
                    834:
                    835:        buffer_init(&b);
                    836:        buffer_append(&b, data, datalen);
1.3       markus    837:
1.1       provos    838:        if (datafellows & SSH_OLD_SESSIONID) {
1.13      markus    839:                p = buffer_ptr(&b);
                    840:                len = buffer_len(&b);
                    841:                if ((session_id2 == NULL) ||
                    842:                    (len < session_id2_len) ||
                    843:                    (memcmp(p, session_id2, session_id2_len) != 0))
                    844:                        fail++;
1.1       provos    845:                buffer_consume(&b, session_id2_len);
                    846:        } else {
1.13      markus    847:                p = buffer_get_string(&b, &len);
                    848:                if ((session_id2 == NULL) ||
                    849:                    (len != session_id2_len) ||
                    850:                    (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    851:                        fail++;
1.13      markus    852:                xfree(p);
1.1       provos    853:        }
                    854:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    855:                fail++;
                    856:        p = buffer_get_string(&b, NULL);
                    857:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    858:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    859:                    authctxt->user, p);
                    860:                fail++;
                    861:        }
                    862:        xfree(p);
                    863:        buffer_skip_string(&b);
                    864:        if (datafellows & SSH_BUG_PKAUTH) {
                    865:                if (!buffer_get_char(&b))
                    866:                        fail++;
                    867:        } else {
                    868:                p = buffer_get_string(&b, NULL);
                    869:                if (strcmp("publickey", p) != 0)
                    870:                        fail++;
                    871:                xfree(p);
                    872:                if (!buffer_get_char(&b))
                    873:                        fail++;
                    874:                buffer_skip_string(&b);
                    875:        }
                    876:        buffer_skip_string(&b);
                    877:        if (buffer_len(&b) != 0)
                    878:                fail++;
                    879:        buffer_free(&b);
                    880:        return (fail == 0);
                    881: }
                    882:
                    883: static int
1.26      markus    884: monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
                    885:     char *chost)
1.1       provos    886: {
                    887:        Buffer b;
1.26      markus    888:        char *p;
1.1       provos    889:        u_int len;
                    890:        int fail = 0;
                    891:
                    892:        buffer_init(&b);
                    893:        buffer_append(&b, data, datalen);
1.3       markus    894:
1.13      markus    895:        p = buffer_get_string(&b, &len);
                    896:        if ((session_id2 == NULL) ||
                    897:            (len != session_id2_len) ||
                    898:            (memcmp(p, session_id2, session_id2_len) != 0))
1.1       provos    899:                fail++;
1.13      markus    900:        xfree(p);
                    901:
1.1       provos    902:        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                    903:                fail++;
                    904:        p = buffer_get_string(&b, NULL);
                    905:        if (strcmp(authctxt->user, p) != 0) {
1.38      itojun    906:                logit("wrong user name passed to monitor: expected %s != %.100s",
1.1       provos    907:                    authctxt->user, p);
                    908:                fail++;
                    909:        }
                    910:        xfree(p);
                    911:        buffer_skip_string(&b); /* service */
                    912:        p = buffer_get_string(&b, NULL);
                    913:        if (strcmp(p, "hostbased") != 0)
                    914:                fail++;
                    915:        xfree(p);
                    916:        buffer_skip_string(&b); /* pkalg */
                    917:        buffer_skip_string(&b); /* pkblob */
                    918:
                    919:        /* verify client host, strip trailing dot if necessary */
                    920:        p = buffer_get_string(&b, NULL);
                    921:        if (((len = strlen(p)) > 0) && p[len - 1] == '.')
                    922:                p[len - 1] = '\0';
                    923:        if (strcmp(p, chost) != 0)
                    924:                fail++;
                    925:        xfree(p);
                    926:
                    927:        /* verify client user */
                    928:        p = buffer_get_string(&b, NULL);
                    929:        if (strcmp(p, cuser) != 0)
                    930:                fail++;
                    931:        xfree(p);
                    932:
                    933:        if (buffer_len(&b) != 0)
                    934:                fail++;
                    935:        buffer_free(&b);
                    936:        return (fail == 0);
                    937: }
                    938:
                    939: int
                    940: mm_answer_keyverify(int socket, Buffer *m)
                    941: {
                    942:        Key *key;
                    943:        u_char *signature, *data, *blob;
                    944:        u_int signaturelen, datalen, bloblen;
                    945:        int verified = 0;
                    946:        int valid_data = 0;
                    947:
                    948:        blob = buffer_get_string(m, &bloblen);
                    949:        signature = buffer_get_string(m, &signaturelen);
                    950:        data = buffer_get_string(m, &datalen);
                    951:
                    952:        if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1.8       mouring   953:          !monitor_allowed_key(blob, bloblen))
1.14      markus    954:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos    955:
                    956:        key = key_from_blob(blob, bloblen);
                    957:        if (key == NULL)
1.14      markus    958:                fatal("%s: bad public key blob", __func__);
1.1       provos    959:
                    960:        switch (key_blobtype) {
                    961:        case MM_USERKEY:
                    962:                valid_data = monitor_valid_userblob(data, datalen);
                    963:                break;
                    964:        case MM_HOSTKEY:
                    965:                valid_data = monitor_valid_hostbasedblob(data, datalen,
                    966:                    hostbased_cuser, hostbased_chost);
                    967:                break;
                    968:        default:
                    969:                valid_data = 0;
                    970:                break;
                    971:        }
                    972:        if (!valid_data)
1.14      markus    973:                fatal("%s: bad signature data blob", __func__);
1.1       provos    974:
                    975:        verified = key_verify(key, signature, signaturelen, data, datalen);
                    976:        debug3("%s: key %p signature %s",
1.14      markus    977:            __func__, key, verified ? "verified" : "unverified");
1.1       provos    978:
                    979:        key_free(key);
                    980:        xfree(blob);
                    981:        xfree(signature);
                    982:        xfree(data);
                    983:
1.17      stevesk   984:        auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
                    985:
1.1       provos    986:        monitor_reset_key_state();
1.3       markus    987:
1.1       provos    988:        buffer_clear(m);
                    989:        buffer_put_int(m, verified);
                    990:        mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
                    991:
                    992:        return (verified);
                    993: }
                    994:
1.2       markus    995: static void
1.1       provos    996: mm_record_login(Session *s, struct passwd *pw)
                    997: {
                    998:        socklen_t fromlen;
                    999:        struct sockaddr_storage from;
                   1000:
                   1001:        /*
                   1002:         * Get IP address of client. If the connection is not a socket, let
                   1003:         * the address be 0.0.0.0.
                   1004:         */
                   1005:        memset(&from, 0, sizeof(from));
1.24      stevesk  1006:        fromlen = sizeof(from);
1.1       provos   1007:        if (packet_connection_is_on_socket()) {
                   1008:                if (getpeername(packet_get_connection_in(),
                   1009:                        (struct sockaddr *) & from, &fromlen) < 0) {
                   1010:                        debug("getpeername: %.100s", strerror(errno));
                   1011:                        fatal_cleanup();
                   1012:                }
                   1013:        }
                   1014:        /* Record that there was a login on that tty from the remote host. */
                   1015:        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1.42    ! markus   1016:            get_remote_name_or_ip(utmp_len, options.use_dns),
1.24      stevesk  1017:            (struct sockaddr *)&from, fromlen);
1.1       provos   1018: }
                   1019:
                   1020: static void
                   1021: mm_session_close(Session *s)
                   1022: {
1.41      djm      1023:        debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1.1       provos   1024:        if (s->ttyfd != -1) {
1.14      markus   1025:                debug3("%s: tty %s ptyfd %d",  __func__, s->tty, s->ptyfd);
1.1       provos   1026:                fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
                   1027:                session_pty_cleanup2(s);
                   1028:        }
                   1029:        s->used = 0;
                   1030: }
                   1031:
                   1032: int
                   1033: mm_answer_pty(int socket, Buffer *m)
                   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:        fatal_add_cleanup(session_pty_cleanup2, (void *)s);
                   1052:        pty_setowner(authctxt->pw, s->tty);
                   1053:
                   1054:        buffer_put_int(m, 1);
                   1055:        buffer_put_cstring(m, s->tty);
                   1056:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                   1057:
                   1058:        mm_send_fd(socket, s->ptyfd);
                   1059:        mm_send_fd(socket, s->ttyfd);
                   1060:
                   1061:        /* We need to trick ttyslot */
                   1062:        if (dup2(s->ttyfd, 0) == -1)
1.14      markus   1063:                fatal("%s: dup2", __func__);
1.1       provos   1064:
                   1065:        mm_record_login(s, authctxt->pw);
                   1066:
                   1067:        /* Now we can close the file descriptor again */
                   1068:        close(0);
                   1069:
                   1070:        /* make sure nothing uses fd 0 */
                   1071:        if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1.14      markus   1072:                fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1.1       provos   1073:        if (fd0 != 0)
1.14      markus   1074:                error("%s: fd0 %d != 0", __func__, fd0);
1.1       provos   1075:
                   1076:        /* slave is not needed */
                   1077:        close(s->ttyfd);
                   1078:        s->ttyfd = s->ptyfd;
                   1079:        /* no need to dup() because nobody closes ptyfd */
                   1080:        s->ptymaster = s->ptyfd;
                   1081:
1.14      markus   1082:        debug3("%s: tty %s ptyfd %d",  __func__, s->tty, s->ttyfd);
1.1       provos   1083:
                   1084:        return (0);
                   1085:
                   1086:  error:
                   1087:        if (s != NULL)
                   1088:                mm_session_close(s);
                   1089:        buffer_put_int(m, 0);
                   1090:        mm_request_send(socket, MONITOR_ANS_PTY, m);
                   1091:        return (0);
                   1092: }
                   1093:
                   1094: int
                   1095: mm_answer_pty_cleanup(int socket, Buffer *m)
                   1096: {
                   1097:        Session *s;
                   1098:        char *tty;
                   1099:
1.14      markus   1100:        debug3("%s entering", __func__);
1.1       provos   1101:
                   1102:        tty = buffer_get_string(m, NULL);
                   1103:        if ((s = session_by_tty(tty)) != NULL)
                   1104:                mm_session_close(s);
                   1105:        buffer_clear(m);
                   1106:        xfree(tty);
                   1107:        return (0);
                   1108: }
                   1109:
                   1110: int
                   1111: mm_answer_sesskey(int socket, Buffer *m)
                   1112: {
                   1113:        BIGNUM *p;
                   1114:        int rsafail;
                   1115:
                   1116:        /* Turn off permissions */
                   1117:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
                   1118:
                   1119:        if ((p = BN_new()) == NULL)
1.14      markus   1120:                fatal("%s: BN_new", __func__);
1.1       provos   1121:
                   1122:        buffer_get_bignum2(m, p);
                   1123:
                   1124:        rsafail = ssh1_session_key(p);
                   1125:
                   1126:        buffer_clear(m);
                   1127:        buffer_put_int(m, rsafail);
                   1128:        buffer_put_bignum2(m, p);
                   1129:
                   1130:        BN_clear_free(p);
                   1131:
                   1132:        mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
                   1133:
                   1134:        /* Turn on permissions for sessid passing */
                   1135:        monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
                   1136:
                   1137:        return (0);
                   1138: }
                   1139:
                   1140: int
                   1141: mm_answer_sessid(int socket, Buffer *m)
                   1142: {
                   1143:        int i;
                   1144:
1.14      markus   1145:        debug3("%s entering", __func__);
1.1       provos   1146:
                   1147:        if (buffer_len(m) != 16)
1.14      markus   1148:                fatal("%s: bad ssh1 session id", __func__);
1.1       provos   1149:        for (i = 0; i < 16; i++)
                   1150:                session_id[i] = buffer_get_char(m);
                   1151:
                   1152:        /* Turn on permissions for getpwnam */
                   1153:        monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
                   1154:
                   1155:        return (0);
                   1156: }
                   1157:
                   1158: int
                   1159: mm_answer_rsa_keyallowed(int socket, Buffer *m)
                   1160: {
                   1161:        BIGNUM *client_n;
                   1162:        Key *key = NULL;
1.3       markus   1163:        u_char *blob = NULL;
                   1164:        u_int blen = 0;
1.1       provos   1165:        int allowed = 0;
                   1166:
1.14      markus   1167:        debug3("%s entering", __func__);
1.1       provos   1168:
1.12      markus   1169:        if (options.rsa_authentication && authctxt->valid) {
1.1       provos   1170:                if ((client_n = BN_new()) == NULL)
1.14      markus   1171:                        fatal("%s: BN_new", __func__);
1.1       provos   1172:                buffer_get_bignum2(m, client_n);
                   1173:                allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
                   1174:                BN_clear_free(client_n);
                   1175:        }
                   1176:        buffer_clear(m);
                   1177:        buffer_put_int(m, allowed);
1.32      markus   1178:        buffer_put_int(m, forced_command != NULL);
1.1       provos   1179:
                   1180:        /* clear temporarily storage (used by generate challenge) */
                   1181:        monitor_reset_key_state();
                   1182:
                   1183:        if (allowed && key != NULL) {
                   1184:                key->type = KEY_RSA;    /* cheat for key_to_blob */
                   1185:                if (key_to_blob(key, &blob, &blen) == 0)
1.14      markus   1186:                        fatal("%s: key_to_blob failed", __func__);
1.1       provos   1187:                buffer_put_string(m, blob, blen);
                   1188:
                   1189:                /* Save temporarily for comparison in verify */
                   1190:                key_blob = blob;
                   1191:                key_bloblen = blen;
                   1192:                key_blobtype = MM_RSAUSERKEY;
1.33      markus   1193:        }
                   1194:        if (key != NULL)
1.1       provos   1195:                key_free(key);
                   1196:
                   1197:        mm_append_debug(m);
                   1198:
                   1199:        mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
                   1200:
                   1201:        monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
                   1202:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
                   1203:        return (0);
                   1204: }
                   1205:
                   1206: int
                   1207: mm_answer_rsa_challenge(int socket, Buffer *m)
                   1208: {
                   1209:        Key *key = NULL;
1.3       markus   1210:        u_char *blob;
                   1211:        u_int blen;
1.1       provos   1212:
1.14      markus   1213:        debug3("%s entering", __func__);
1.1       provos   1214:
                   1215:        if (!authctxt->valid)
1.14      markus   1216:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1217:        blob = buffer_get_string(m, &blen);
                   1218:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1219:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1220:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1221:                fatal("%s: key type mismatch", __func__);
1.1       provos   1222:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1223:                fatal("%s: received bad key", __func__);
1.1       provos   1224:
                   1225:        if (ssh1_challenge)
                   1226:                BN_clear_free(ssh1_challenge);
                   1227:        ssh1_challenge = auth_rsa_generate_challenge(key);
                   1228:
                   1229:        buffer_clear(m);
                   1230:        buffer_put_bignum2(m, ssh1_challenge);
                   1231:
1.14      markus   1232:        debug3("%s sending reply", __func__);
1.1       provos   1233:        mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
                   1234:
                   1235:        monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1.33      markus   1236:
                   1237:        xfree(blob);
                   1238:        key_free(key);
1.1       provos   1239:        return (0);
                   1240: }
                   1241:
                   1242: int
                   1243: mm_answer_rsa_response(int socket, Buffer *m)
                   1244: {
                   1245:        Key *key = NULL;
1.3       markus   1246:        u_char *blob, *response;
                   1247:        u_int blen, len;
                   1248:        int success;
1.1       provos   1249:
1.14      markus   1250:        debug3("%s entering", __func__);
1.1       provos   1251:
                   1252:        if (!authctxt->valid)
1.14      markus   1253:                fatal("%s: authctxt not valid", __func__);
1.1       provos   1254:        if (ssh1_challenge == NULL)
1.14      markus   1255:                fatal("%s: no ssh1_challenge", __func__);
1.1       provos   1256:
                   1257:        blob = buffer_get_string(m, &blen);
                   1258:        if (!monitor_allowed_key(blob, blen))
1.14      markus   1259:                fatal("%s: bad key, not previously allowed", __func__);
1.1       provos   1260:        if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1.14      markus   1261:                fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1.1       provos   1262:        if ((key = key_from_blob(blob, blen)) == NULL)
1.14      markus   1263:                fatal("%s: received bad key", __func__);
1.1       provos   1264:        response = buffer_get_string(m, &len);
                   1265:        if (len != 16)
1.14      markus   1266:                fatal("%s: received bad response to challenge", __func__);
1.1       provos   1267:        success = auth_rsa_verify_response(key, ssh1_challenge, response);
                   1268:
1.33      markus   1269:        xfree(blob);
1.1       provos   1270:        key_free(key);
                   1271:        xfree(response);
                   1272:
                   1273:        auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
                   1274:
                   1275:        /* reset state */
                   1276:        BN_clear_free(ssh1_challenge);
                   1277:        ssh1_challenge = NULL;
                   1278:        monitor_reset_key_state();
                   1279:
                   1280:        buffer_clear(m);
                   1281:        buffer_put_int(m, success);
                   1282:        mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
                   1283:
                   1284:        return (success);
                   1285: }
1.25      itojun   1286:
1.29      markus   1287: #ifdef KRB4
                   1288: int
                   1289: mm_answer_krb4(int socket, Buffer *m)
                   1290: {
                   1291:        KTEXT_ST auth, reply;
                   1292:        char  *client, *p;
                   1293:        int success;
                   1294:        u_int alen;
                   1295:
                   1296:        reply.length = auth.length = 0;
                   1297:
                   1298:        p = buffer_get_string(m, &alen);
                   1299:        if (alen >=  MAX_KTXT_LEN)
                   1300:                 fatal("%s: auth too large", __func__);
                   1301:        memcpy(auth.dat, p, alen);
                   1302:        auth.length = alen;
                   1303:        memset(p, 0, alen);
                   1304:        xfree(p);
                   1305:
                   1306:        success = options.kerberos_authentication &&
                   1307:            authctxt->valid &&
                   1308:            auth_krb4(authctxt, &auth, &client, &reply);
                   1309:
                   1310:        memset(auth.dat, 0, alen);
                   1311:        buffer_clear(m);
                   1312:        buffer_put_int(m, success);
                   1313:
                   1314:        if (success) {
                   1315:                buffer_put_cstring(m, client);
                   1316:                buffer_put_string(m, reply.dat, reply.length);
                   1317:                if (client)
                   1318:                        xfree(client);
                   1319:                if (reply.length)
                   1320:                        memset(reply.dat, 0, reply.length);
                   1321:        }
                   1322:
                   1323:        debug3("%s: sending result %d", __func__, success);
                   1324:        mm_request_send(socket, MONITOR_ANS_KRB4, m);
                   1325:
                   1326:        auth_method = "kerberos";
                   1327:
                   1328:        /* Causes monitor loop to terminate if authenticated */
                   1329:        return (success);
                   1330: }
                   1331: #endif
1.25      itojun   1332:
                   1333: #ifdef KRB5
                   1334: int
                   1335: mm_answer_krb5(int socket, Buffer *m)
                   1336: {
                   1337:        krb5_data tkt, reply;
                   1338:        char *client_user;
                   1339:        u_int len;
                   1340:        int success;
                   1341:
                   1342:        /* use temporary var to avoid size issues on 64bit arch */
                   1343:        tkt.data = buffer_get_string(m, &len);
                   1344:        tkt.length = len;
                   1345:
1.28      markus   1346:        success = options.kerberos_authentication &&
                   1347:            authctxt->valid &&
1.27      markus   1348:            auth_krb5(authctxt, &tkt, &client_user, &reply);
1.25      itojun   1349:
                   1350:        if (tkt.length)
                   1351:                xfree(tkt.data);
                   1352:
                   1353:        buffer_clear(m);
                   1354:        buffer_put_int(m, success);
                   1355:
                   1356:        if (success) {
                   1357:                buffer_put_cstring(m, client_user);
                   1358:                buffer_put_string(m, reply.data, reply.length);
                   1359:                if (client_user)
                   1360:                        xfree(client_user);
                   1361:                if (reply.length)
                   1362:                        xfree(reply.data);
                   1363:        }
                   1364:        mm_request_send(socket, MONITOR_ANS_KRB5, m);
1.39      markus   1365:
                   1366:        auth_method = "kerberos";
1.25      itojun   1367:
                   1368:        return success;
                   1369: }
                   1370: #endif
1.1       provos   1371:
                   1372: int
                   1373: mm_answer_term(int socket, Buffer *req)
                   1374: {
1.11      mouring  1375:        extern struct monitor *pmonitor;
1.1       provos   1376:        int res, status;
                   1377:
1.14      markus   1378:        debug3("%s: tearing down sessions", __func__);
1.1       provos   1379:
                   1380:        /* The child is terminating */
                   1381:        session_destroy_all(&mm_session_close);
                   1382:
1.11      mouring  1383:        while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1.9       markus   1384:                if (errno != EINTR)
                   1385:                        exit(1);
1.1       provos   1386:
                   1387:        res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
                   1388:
                   1389:        /* Terminate process */
                   1390:        exit (res);
                   1391: }
                   1392:
                   1393: void
1.11      mouring  1394: monitor_apply_keystate(struct monitor *pmonitor)
1.1       provos   1395: {
                   1396:        if (compat20) {
                   1397:                set_newkeys(MODE_IN);
                   1398:                set_newkeys(MODE_OUT);
                   1399:        } else {
                   1400:                packet_set_protocol_flags(child_state.ssh1protoflags);
1.15      markus   1401:                packet_set_encryption_key(child_state.ssh1key,
                   1402:                    child_state.ssh1keylen, child_state.ssh1cipher);
                   1403:                xfree(child_state.ssh1key);
1.1       provos   1404:        }
                   1405:
1.15      markus   1406:        /* for rc4 and other stateful ciphers */
1.1       provos   1407:        packet_set_keycontext(MODE_OUT, child_state.keyout);
                   1408:        xfree(child_state.keyout);
                   1409:        packet_set_keycontext(MODE_IN, child_state.keyin);
                   1410:        xfree(child_state.keyin);
                   1411:
                   1412:        if (!compat20) {
                   1413:                packet_set_iv(MODE_OUT, child_state.ivout);
                   1414:                xfree(child_state.ivout);
                   1415:                packet_set_iv(MODE_IN, child_state.ivin);
                   1416:                xfree(child_state.ivin);
                   1417:        }
                   1418:
                   1419:        memcpy(&incoming_stream, &child_state.incoming,
                   1420:            sizeof(incoming_stream));
                   1421:        memcpy(&outgoing_stream, &child_state.outgoing,
                   1422:            sizeof(outgoing_stream));
1.3       markus   1423:
1.1       provos   1424:        /* Update with new address */
1.16      djm      1425:        if (options.compression)
                   1426:                mm_init_compression(pmonitor->m_zlib);
1.1       provos   1427:
                   1428:        /* Network I/O buffers */
                   1429:        /* XXX inefficient for large buffers, need: buffer_init_from_string */
                   1430:        buffer_clear(&input);
                   1431:        buffer_append(&input, child_state.input, child_state.ilen);
                   1432:        memset(child_state.input, 0, child_state.ilen);
                   1433:        xfree(child_state.input);
                   1434:
                   1435:        buffer_clear(&output);
                   1436:        buffer_append(&output, child_state.output, child_state.olen);
                   1437:        memset(child_state.output, 0, child_state.olen);
                   1438:        xfree(child_state.output);
                   1439: }
                   1440:
1.2       markus   1441: static Kex *
1.1       provos   1442: mm_get_kex(Buffer *m)
                   1443: {
                   1444:        Kex *kex;
                   1445:        void *blob;
                   1446:        u_int bloblen;
                   1447:
                   1448:        kex = xmalloc(sizeof(*kex));
                   1449:        memset(kex, 0, sizeof(*kex));
                   1450:        kex->session_id = buffer_get_string(m, &kex->session_id_len);
1.13      markus   1451:        if ((session_id2 == NULL) ||
                   1452:            (kex->session_id_len != session_id2_len) ||
                   1453:            (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
                   1454:                fatal("mm_get_get: internal error: bad session id");
1.1       provos   1455:        kex->we_need = buffer_get_int(m);
1.34      markus   1456:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
                   1457:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1.1       provos   1458:        kex->server = 1;
                   1459:        kex->hostkey_type = buffer_get_int(m);
                   1460:        kex->kex_type = buffer_get_int(m);
                   1461:        blob = buffer_get_string(m, &bloblen);
                   1462:        buffer_init(&kex->my);
                   1463:        buffer_append(&kex->my, blob, bloblen);
                   1464:        xfree(blob);
                   1465:        blob = buffer_get_string(m, &bloblen);
                   1466:        buffer_init(&kex->peer);
                   1467:        buffer_append(&kex->peer, blob, bloblen);
                   1468:        xfree(blob);
                   1469:        kex->done = 1;
                   1470:        kex->flags = buffer_get_int(m);
                   1471:        kex->client_version_string = buffer_get_string(m, NULL);
                   1472:        kex->server_version_string = buffer_get_string(m, NULL);
                   1473:        kex->load_host_key=&get_hostkey_by_type;
                   1474:        kex->host_key_index=&get_hostkey_index;
                   1475:
                   1476:        return (kex);
                   1477: }
                   1478:
                   1479: /* This function requries careful sanity checking */
                   1480:
                   1481: void
1.11      mouring  1482: mm_get_keystate(struct monitor *pmonitor)
1.1       provos   1483: {
                   1484:        Buffer m;
                   1485:        u_char *blob, *p;
                   1486:        u_int bloblen, plen;
1.37      markus   1487:        u_int32_t seqnr, packets;
                   1488:        u_int64_t blocks;
1.1       provos   1489:
1.14      markus   1490:        debug3("%s: Waiting for new keys", __func__);
1.1       provos   1491:
                   1492:        buffer_init(&m);
1.11      mouring  1493:        mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1.1       provos   1494:        if (!compat20) {
                   1495:                child_state.ssh1protoflags = buffer_get_int(&m);
                   1496:                child_state.ssh1cipher = buffer_get_int(&m);
1.15      markus   1497:                child_state.ssh1key = buffer_get_string(&m,
                   1498:                    &child_state.ssh1keylen);
1.1       provos   1499:                child_state.ivout = buffer_get_string(&m,
                   1500:                    &child_state.ivoutlen);
                   1501:                child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
                   1502:                goto skip;
                   1503:        } else {
                   1504:                /* Get the Kex for rekeying */
1.11      mouring  1505:                *pmonitor->m_pkex = mm_get_kex(&m);
1.1       provos   1506:        }
                   1507:
                   1508:        blob = buffer_get_string(&m, &bloblen);
                   1509:        current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
                   1510:        xfree(blob);
                   1511:
1.14      markus   1512:        debug3("%s: Waiting for second key", __func__);
1.1       provos   1513:        blob = buffer_get_string(&m, &bloblen);
                   1514:        current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
                   1515:        xfree(blob);
1.3       markus   1516:
1.1       provos   1517:        /* Now get sequence numbers for the packets */
1.37      markus   1518:        seqnr = buffer_get_int(&m);
                   1519:        blocks = buffer_get_int64(&m);
                   1520:        packets = buffer_get_int(&m);
                   1521:        packet_set_state(MODE_OUT, seqnr, blocks, packets);
                   1522:        seqnr = buffer_get_int(&m);
                   1523:        blocks = buffer_get_int64(&m);
                   1524:        packets = buffer_get_int(&m);
                   1525:        packet_set_state(MODE_IN, seqnr, blocks, packets);
1.1       provos   1526:
                   1527:  skip:
                   1528:        /* Get the key context */
                   1529:        child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
                   1530:        child_state.keyin  = buffer_get_string(&m, &child_state.keyinlen);
                   1531:
1.14      markus   1532:        debug3("%s: Getting compression state", __func__);
1.1       provos   1533:        /* Get compression state */
                   1534:        p = buffer_get_string(&m, &plen);
                   1535:        if (plen != sizeof(child_state.outgoing))
1.14      markus   1536:                fatal("%s: bad request size", __func__);
1.1       provos   1537:        memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
                   1538:        xfree(p);
                   1539:
                   1540:        p = buffer_get_string(&m, &plen);
                   1541:        if (plen != sizeof(child_state.incoming))
1.14      markus   1542:                fatal("%s: bad request size", __func__);
1.1       provos   1543:        memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
                   1544:        xfree(p);
                   1545:
                   1546:        /* Network I/O buffers */
1.14      markus   1547:        debug3("%s: Getting Network I/O buffers", __func__);
1.1       provos   1548:        child_state.input = buffer_get_string(&m, &child_state.ilen);
                   1549:        child_state.output = buffer_get_string(&m, &child_state.olen);
                   1550:
                   1551:        buffer_free(&m);
                   1552: }
                   1553:
                   1554:
                   1555: /* Allocation functions for zlib */
                   1556: void *
                   1557: mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
                   1558: {
1.30      markus   1559:        size_t len = (size_t) size * ncount;
1.1       provos   1560:        void *address;
                   1561:
1.23      millert  1562:        if (len == 0 || ncount > SIZE_T_MAX / size)
1.18      deraadt  1563:                fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
                   1564:
                   1565:        address = mm_malloc(mm, len);
1.1       provos   1566:
                   1567:        return (address);
                   1568: }
                   1569:
                   1570: void
                   1571: mm_zfree(struct mm_master *mm, void *address)
                   1572: {
                   1573:        mm_free(mm, address);
                   1574: }
                   1575:
                   1576: void
                   1577: mm_init_compression(struct mm_master *mm)
                   1578: {
                   1579:        outgoing_stream.zalloc = (alloc_func)mm_zalloc;
                   1580:        outgoing_stream.zfree = (free_func)mm_zfree;
                   1581:        outgoing_stream.opaque = mm;
                   1582:
                   1583:        incoming_stream.zalloc = (alloc_func)mm_zalloc;
                   1584:        incoming_stream.zfree = (free_func)mm_zfree;
                   1585:        incoming_stream.opaque = mm;
                   1586: }
                   1587:
                   1588: /* XXX */
                   1589:
                   1590: #define FD_CLOSEONEXEC(x) do { \
                   1591:        if (fcntl(x, F_SETFD, 1) == -1) \
                   1592:                fatal("fcntl(%d, F_SETFD)", x); \
                   1593: } while (0)
                   1594:
1.2       markus   1595: static void
1.1       provos   1596: monitor_socketpair(int *pair)
1.3       markus   1597: {
1.1       provos   1598:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1.14      markus   1599:                fatal("%s: socketpair", __func__);
1.1       provos   1600:        FD_CLOSEONEXEC(pair[0]);
                   1601:        FD_CLOSEONEXEC(pair[1]);
                   1602: }
                   1603:
                   1604: #define MM_MEMSIZE     65536
                   1605:
                   1606: struct monitor *
                   1607: monitor_init(void)
                   1608: {
                   1609:        struct monitor *mon;
                   1610:        int pair[2];
                   1611:
                   1612:        mon = xmalloc(sizeof(*mon));
                   1613:
                   1614:        monitor_socketpair(pair);
                   1615:
                   1616:        mon->m_recvfd = pair[0];
                   1617:        mon->m_sendfd = pair[1];
                   1618:
                   1619:        /* Used to share zlib space across processes */
1.16      djm      1620:        if (options.compression) {
                   1621:                mon->m_zback = mm_create(NULL, MM_MEMSIZE);
                   1622:                mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1.1       provos   1623:
1.16      djm      1624:                /* Compression needs to share state across borders */
                   1625:                mm_init_compression(mon->m_zlib);
                   1626:        }
1.1       provos   1627:
                   1628:        return mon;
                   1629: }
                   1630:
                   1631: void
                   1632: monitor_reinit(struct monitor *mon)
                   1633: {
                   1634:        int pair[2];
                   1635:
                   1636:        monitor_socketpair(pair);
                   1637:
                   1638:        mon->m_recvfd = pair[0];
                   1639:        mon->m_sendfd = pair[1];
                   1640: }