[BACK]Return to monitor_wrap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.102

1.102   ! markus      1: /* $OpenBSD: monitor_wrap.c,v 1.101 2018/07/09 13:37:10 sf 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.46      stevesk    28: #include <sys/types.h>
1.51      dtucker    29: #include <sys/uio.h>
1.61      djm        30: #include <sys/queue.h>
1.46      stevesk    31:
1.47      stevesk    32: #include <errno.h>
1.46      stevesk    33: #include <pwd.h>
1.50      deraadt    34: #include <signal.h>
1.49      stevesk    35: #include <stdio.h>
1.48      stevesk    36: #include <string.h>
1.51      dtucker    37: #include <unistd.h>
1.68      dtucker    38:
1.80      markus     39: #ifdef WITH_OPENSSL
1.68      dtucker    40: #include <openssl/bn.h>
                     41: #include <openssl/dh.h>
1.80      markus     42: #endif
1.1       provos     43:
1.50      deraadt    44: #include "xmalloc.h"
1.1       provos     45: #include "ssh.h"
1.80      markus     46: #ifdef WITH_OPENSSL
1.1       provos     47: #include "dh.h"
1.80      markus     48: #endif
1.50      deraadt    49: #include "buffer.h"
                     50: #include "key.h"
                     51: #include "cipher.h"
1.1       provos     52: #include "kex.h"
1.50      deraadt    53: #include "hostfile.h"
1.1       provos     54: #include "auth.h"
1.22      markus     55: #include "auth-options.h"
1.1       provos     56: #include "packet.h"
                     57: #include "mac.h"
                     58: #include "log.h"
1.54      miod       59: #include <zlib.h>
1.1       provos     60: #include "monitor.h"
1.50      deraadt    61: #ifdef GSSAPI
                     62: #include "ssh-gss.h"
                     63: #endif
1.1       provos     64: #include "monitor_wrap.h"
                     65: #include "atomicio.h"
                     66: #include "monitor_fdpass.h"
1.45      djm        67: #include "misc.h"
1.1       provos     68:
                     69: #include "channels.h"
                     70: #include "session.h"
1.55      dtucker    71: #include "servconf.h"
1.29      markus     72:
1.82      markus     73: #include "ssherr.h"
                     74:
1.1       provos     75: /* Imports */
1.101     sf         76: extern z_stream incoming_stream;
                     77: extern z_stream outgoing_stream;
1.7       mouring    78: extern struct monitor *pmonitor;
1.102   ! markus     79: extern struct sshbuf *loginmsg;
1.55      dtucker    80: extern ServerOptions options;
1.73      djm        81:
                     82: void
                     83: mm_log_handler(LogLevel level, const char *msg, void *ctx)
                     84: {
                     85:        Buffer log_msg;
                     86:        struct monitor *mon = (struct monitor *)ctx;
                     87:
                     88:        if (mon->m_log_sendfd == -1)
                     89:                fatal("%s: no log channel", __func__);
                     90:
                     91:        buffer_init(&log_msg);
                     92:        /*
                     93:         * Placeholder for packet length. Will be filled in with the actual
                     94:         * packet length once the packet has been constucted. This saves
                     95:         * fragile math.
                     96:         */
                     97:        buffer_put_int(&log_msg, 0);
                     98:
                     99:        buffer_put_int(&log_msg, level);
                    100:        buffer_put_cstring(&log_msg, msg);
                    101:        put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
                    102:        if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
                    103:            buffer_len(&log_msg)) != buffer_len(&log_msg))
                    104:                fatal("%s: write: %s", __func__, strerror(errno));
                    105:        buffer_free(&log_msg);
                    106: }
1.1       provos    107:
1.32      markus    108: int
                    109: mm_is_monitor(void)
                    110: {
                    111:        /*
                    112:         * m_pid is only set in the privileged part, and
                    113:         * points to the unprivileged child.
                    114:         */
1.34      markus    115:        return (pmonitor && pmonitor->m_pid > 0);
1.32      markus    116: }
                    117:
1.1       provos    118: void
1.36      avsm      119: mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos    120: {
1.13      deraadt   121:        u_int mlen = buffer_len(m);
1.1       provos    122:        u_char buf[5];
                    123:
1.8       markus    124:        debug3("%s entering: type %d", __func__, type);
1.1       provos    125:
1.45      djm       126:        put_u32(buf, mlen + 1);
1.10      deraadt   127:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
1.36      avsm      128:        if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
1.40      avsm      129:                fatal("%s: write: %s", __func__, strerror(errno));
1.36      avsm      130:        if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
1.40      avsm      131:                fatal("%s: write: %s", __func__, strerror(errno));
1.1       provos    132: }
                    133:
                    134: void
1.36      avsm      135: mm_request_receive(int sock, Buffer *m)
1.1       provos    136: {
                    137:        u_char buf[4];
1.13      deraadt   138:        u_int msg_len;
1.1       provos    139:
1.8       markus    140:        debug3("%s entering", __func__);
1.1       provos    141:
1.40      avsm      142:        if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
                    143:                if (errno == EPIPE)
1.32      markus    144:                        cleanup_exit(255);
1.40      avsm      145:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    146:        }
1.45      djm       147:        msg_len = get_u32(buf);
1.1       provos    148:        if (msg_len > 256 * 1024)
1.8       markus    149:                fatal("%s: read: bad msg_len %d", __func__, msg_len);
1.1       provos    150:        buffer_clear(m);
                    151:        buffer_append_space(m, msg_len);
1.40      avsm      152:        if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
                    153:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    154: }
                    155:
                    156: void
1.36      avsm      157: mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos    158: {
                    159:        u_char rtype;
                    160:
1.8       markus    161:        debug3("%s entering: type %d", __func__, type);
1.1       provos    162:
1.36      avsm      163:        mm_request_receive(sock, m);
1.1       provos    164:        rtype = buffer_get_char(m);
                    165:        if (rtype != type)
1.8       markus    166:                fatal("%s: read: rtype %d != type %d", __func__,
1.1       provos    167:                    rtype, type);
                    168: }
                    169:
1.80      markus    170: #ifdef WITH_OPENSSL
1.1       provos    171: DH *
                    172: mm_choose_dh(int min, int nbits, int max)
                    173: {
                    174:        BIGNUM *p, *g;
                    175:        int success = 0;
                    176:        Buffer m;
                    177:
                    178:        buffer_init(&m);
                    179:        buffer_put_int(&m, min);
                    180:        buffer_put_int(&m, nbits);
                    181:        buffer_put_int(&m, max);
                    182:
1.7       mouring   183:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
1.1       provos    184:
1.8       markus    185:        debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
1.7       mouring   186:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
1.1       provos    187:
                    188:        success = buffer_get_char(&m);
                    189:        if (success == 0)
1.8       markus    190:                fatal("%s: MONITOR_ANS_MODULI failed", __func__);
1.1       provos    191:
                    192:        if ((p = BN_new()) == NULL)
1.8       markus    193:                fatal("%s: BN_new failed", __func__);
1.3       markus    194:        if ((g = BN_new()) == NULL)
1.8       markus    195:                fatal("%s: BN_new failed", __func__);
1.1       provos    196:        buffer_get_bignum2(&m, p);
                    197:        buffer_get_bignum2(&m, g);
                    198:
1.8       markus    199:        debug3("%s: remaining %d", __func__, buffer_len(&m));
1.1       provos    200:        buffer_free(&m);
                    201:
                    202:        return (dh_new_group(g, p));
                    203: }
1.80      markus    204: #endif
1.1       provos    205:
                    206: int
1.91      markus    207: mm_key_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
1.86      markus    208:     const u_char *data, u_int datalen, const char *hostkey_alg)
1.1       provos    209: {
1.83      markus    210:        struct kex *kex = *pmonitor->m_pkex;
1.1       provos    211:        Buffer m;
                    212:
1.8       markus    213:        debug3("%s entering", __func__);
1.1       provos    214:
                    215:        buffer_init(&m);
1.84      djm       216:        buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
1.1       provos    217:        buffer_put_string(&m, data, datalen);
1.86      markus    218:        buffer_put_cstring(&m, hostkey_alg);
1.1       provos    219:
1.7       mouring   220:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
1.1       provos    221:
1.8       markus    222:        debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
1.7       mouring   223:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
1.1       provos    224:        *sigp  = buffer_get_string(&m, lenp);
                    225:        buffer_free(&m);
                    226:
                    227:        return (0);
                    228: }
                    229:
                    230: struct passwd *
1.37      dtucker   231: mm_getpwnamallow(const char *username)
1.1       provos    232: {
1.93      djm       233:        struct ssh *ssh = active_state;         /* XXX */
1.1       provos    234:        Buffer m;
                    235:        struct passwd *pw;
1.72      djm       236:        u_int len, i;
1.55      dtucker   237:        ServerOptions *newopts;
1.1       provos    238:
1.8       markus    239:        debug3("%s entering", __func__);
1.1       provos    240:
                    241:        buffer_init(&m);
1.37      dtucker   242:        buffer_put_cstring(&m, username);
1.1       provos    243:
1.7       mouring   244:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
1.1       provos    245:
1.8       markus    246:        debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
1.7       mouring   247:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
1.1       provos    248:
                    249:        if (buffer_get_char(&m) == 0) {
1.60      dtucker   250:                pw = NULL;
                    251:                goto out;
1.1       provos    252:        }
1.55      dtucker   253:        pw = buffer_get_string(&m, &len);
                    254:        if (len != sizeof(struct passwd))
1.8       markus    255:                fatal("%s: struct passwd size mismatch", __func__);
1.1       provos    256:        pw->pw_name = buffer_get_string(&m, NULL);
                    257:        pw->pw_passwd = buffer_get_string(&m, NULL);
                    258:        pw->pw_gecos = buffer_get_string(&m, NULL);
                    259:        pw->pw_class = buffer_get_string(&m, NULL);
                    260:        pw->pw_dir = buffer_get_string(&m, NULL);
                    261:        pw->pw_shell = buffer_get_string(&m, NULL);
1.55      dtucker   262:
1.60      dtucker   263: out:
1.55      dtucker   264:        /* copy options block as a Match directive may have changed some */
                    265:        newopts = buffer_get_string(&m, &len);
                    266:        if (len != sizeof(*newopts))
                    267:                fatal("%s: option block size mismatch", __func__);
1.71      djm       268:
                    269: #define M_CP_STROPT(x) do { \
                    270:                if (newopts->x != NULL) \
                    271:                        newopts->x = buffer_get_string(&m, NULL); \
                    272:        } while (0)
1.72      djm       273: #define M_CP_STRARRAYOPT(x, nx) do { \
1.95      djm       274:                newopts->x = newopts->nx == 0 ? \
                    275:                    NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
1.72      djm       276:                for (i = 0; i < newopts->nx; i++) \
                    277:                        newopts->x[i] = buffer_get_string(&m, NULL); \
                    278:        } while (0)
1.71      djm       279:        /* See comment in servconf.h */
                    280:        COPY_MATCH_STRING_OPTS();
                    281: #undef M_CP_STROPT
1.72      djm       282: #undef M_CP_STRARRAYOPT
1.71      djm       283:
1.55      dtucker   284:        copy_set_server_options(&options, newopts, 1);
1.90      djm       285:        log_change_level(options.log_level);
1.93      djm       286:        process_permitopen(ssh, &options);
1.76      djm       287:        free(newopts);
1.55      dtucker   288:
1.1       provos    289:        buffer_free(&m);
                    290:
                    291:        return (pw);
1.6       djm       292: }
                    293:
1.33      markus    294: char *
                    295: mm_auth2_read_banner(void)
1.6       djm       296: {
                    297:        Buffer m;
                    298:        char *banner;
                    299:
1.8       markus    300:        debug3("%s entering", __func__);
1.6       djm       301:
                    302:        buffer_init(&m);
1.7       mouring   303:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       304:        buffer_clear(&m);
                    305:
1.33      markus    306:        mm_request_receive_expect(pmonitor->m_recvfd,
                    307:            MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       308:        banner = buffer_get_string(&m, NULL);
                    309:        buffer_free(&m);
1.10      deraadt   310:
1.33      markus    311:        /* treat empty banner as missing banner */
                    312:        if (strlen(banner) == 0) {
1.76      djm       313:                free(banner);
1.33      markus    314:                banner = NULL;
                    315:        }
1.6       djm       316:        return (banner);
1.1       provos    317: }
                    318:
                    319: /* Inform the privileged process about service and style */
                    320:
                    321: void
                    322: mm_inform_authserv(char *service, char *style)
                    323: {
                    324:        Buffer m;
                    325:
1.8       markus    326:        debug3("%s entering", __func__);
1.1       provos    327:
                    328:        buffer_init(&m);
                    329:        buffer_put_cstring(&m, service);
                    330:        buffer_put_cstring(&m, style ? style : "");
                    331:
1.7       mouring   332:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    333:
                    334:        buffer_free(&m);
                    335: }
                    336:
                    337: /* Do the password authentication */
                    338: int
1.99      djm       339: mm_auth_password(struct ssh *ssh, char *password)
1.1       provos    340: {
                    341:        Buffer m;
                    342:        int authenticated = 0;
                    343:
1.8       markus    344:        debug3("%s entering", __func__);
1.1       provos    345:
                    346:        buffer_init(&m);
                    347:        buffer_put_cstring(&m, password);
1.7       mouring   348:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    349:
1.8       markus    350:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   351:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    352:
                    353:        authenticated = buffer_get_int(&m);
                    354:
                    355:        buffer_free(&m);
                    356:
1.3       markus    357:        debug3("%s: user %sauthenticated",
1.8       markus    358:            __func__, authenticated ? "" : "not ");
1.1       provos    359:        return (authenticated);
                    360: }
                    361:
                    362: int
1.99      djm       363: mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
                    364:     int pubkey_auth_attempt, struct sshauthopt **authoptp)
1.1       provos    365: {
1.85      djm       366:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
1.99      djm       367:            pubkey_auth_attempt, authoptp));
1.1       provos    368: }
                    369:
                    370: int
1.88      djm       371: mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
1.91      markus    372:     struct sshkey *key)
1.1       provos    373: {
1.99      djm       374:        return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
1.1       provos    375: }
                    376:
                    377: int
1.88      djm       378: mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
1.99      djm       379:     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
1.1       provos    380: {
                    381:        Buffer m;
                    382:        u_char *blob;
                    383:        u_int len;
1.99      djm       384:        int r, allowed = 0;
                    385:        struct sshauthopt *opts = NULL;
1.1       provos    386:
1.8       markus    387:        debug3("%s entering", __func__);
1.1       provos    388:
1.99      djm       389:        if (authoptp != NULL)
                    390:                *authoptp = NULL;
                    391:
1.1       provos    392:        /* Convert the key to a blob and the pass it over */
                    393:        if (!key_to_blob(key, &blob, &len))
1.99      djm       394:                return 0;
1.1       provos    395:
                    396:        buffer_init(&m);
                    397:        buffer_put_int(&m, type);
                    398:        buffer_put_cstring(&m, user ? user : "");
                    399:        buffer_put_cstring(&m, host ? host : "");
                    400:        buffer_put_string(&m, blob, len);
1.85      djm       401:        buffer_put_int(&m, pubkey_auth_attempt);
1.76      djm       402:        free(blob);
1.1       provos    403:
1.7       mouring   404:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    405:
1.8       markus    406:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.99      djm       407:        mm_request_receive_expect(pmonitor->m_recvfd,
                    408:            MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    409:
                    410:        allowed = buffer_get_int(&m);
1.99      djm       411:        if (allowed && type == MM_USERKEY) {
                    412:                if ((r = sshauthopt_deserialise(&m, &opts)) != 0)
                    413:                        fatal("%s: sshauthopt_deserialise: %s",
                    414:                            __func__, ssh_err(r));
                    415:        }
                    416:        buffer_free(&m);
1.1       provos    417:
1.99      djm       418:        if (authoptp != NULL) {
                    419:                *authoptp = opts;
                    420:                opts = NULL;
                    421:        }
                    422:        sshauthopt_free(opts);
1.1       provos    423:
1.99      djm       424:        return allowed;
1.1       provos    425: }
                    426:
1.3       markus    427: /*
1.1       provos    428:  * This key verify needs to send the key type along, because the
                    429:  * privileged parent makes the decision if the key is allowed
                    430:  * for authentication.
                    431:  */
                    432:
                    433: int
1.92      markus    434: mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
1.96      djm       435:     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
1.1       provos    436: {
                    437:        Buffer m;
                    438:        u_char *blob;
                    439:        u_int len;
1.92      markus    440:        u_int encoded_ret = 0;
1.1       provos    441:
1.8       markus    442:        debug3("%s entering", __func__);
1.1       provos    443:
                    444:        /* Convert the key to a blob and the pass it over */
                    445:        if (!key_to_blob(key, &blob, &len))
                    446:                return (0);
                    447:
                    448:        buffer_init(&m);
                    449:        buffer_put_string(&m, blob, len);
                    450:        buffer_put_string(&m, sig, siglen);
                    451:        buffer_put_string(&m, data, datalen);
1.97      djm       452:        buffer_put_cstring(&m, sigalg == NULL ? "" : sigalg);
1.76      djm       453:        free(blob);
1.1       provos    454:
1.7       mouring   455:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    456:
1.8       markus    457:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   458:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    459:
1.92      markus    460:        encoded_ret = buffer_get_int(&m);
1.1       provos    461:
                    462:        buffer_free(&m);
                    463:
1.92      markus    464:        if (encoded_ret != 0)
                    465:                return SSH_ERR_SIGNATURE_INVALID;
                    466:        return 0;
1.1       provos    467: }
                    468:
                    469: void
1.36      avsm      470: mm_send_keystate(struct monitor *monitor)
1.1       provos    471: {
1.82      markus    472:        struct ssh *ssh = active_state;         /* XXX */
                    473:        struct sshbuf *m;
                    474:        int r;
                    475:
                    476:        if ((m = sshbuf_new()) == NULL)
                    477:                fatal("%s: sshbuf_new failed", __func__);
                    478:        if ((r = ssh_packet_get_state(ssh, m)) != 0)
                    479:                fatal("%s: get_state failed: %s",
                    480:                    __func__, ssh_err(r));
                    481:        mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
1.8       markus    482:        debug3("%s: Finished sending state", __func__);
1.82      markus    483:        sshbuf_free(m);
1.1       provos    484: }
                    485:
                    486: int
1.42      deraadt   487: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1       provos    488: {
                    489:        Buffer m;
1.39      dtucker   490:        char *p, *msg;
1.102   ! markus    491:        int success = 0, tmp1 = -1, tmp2 = -1, r;
1.62      djm       492:
                    493:        /* Kludge: ensure there are fds free to receive the pty/tty */
                    494:        if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
                    495:            (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
                    496:                error("%s: cannot allocate fds for pty", __func__);
                    497:                if (tmp1 > 0)
                    498:                        close(tmp1);
                    499:                if (tmp2 > 0)
                    500:                        close(tmp2);
                    501:                return 0;
                    502:        }
                    503:        close(tmp1);
                    504:        close(tmp2);
1.1       provos    505:
                    506:        buffer_init(&m);
1.7       mouring   507:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    508:
1.8       markus    509:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   510:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    511:
                    512:        success = buffer_get_int(&m);
                    513:        if (success == 0) {
1.8       markus    514:                debug3("%s: pty alloc failed", __func__);
1.1       provos    515:                buffer_free(&m);
                    516:                return (0);
                    517:        }
                    518:        p = buffer_get_string(&m, NULL);
1.39      dtucker   519:        msg = buffer_get_string(&m, NULL);
1.1       provos    520:        buffer_free(&m);
                    521:
                    522:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
1.76      djm       523:        free(p);
1.39      dtucker   524:
1.102   ! markus    525:        if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
        !           526:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.76      djm       527:        free(msg);
1.1       provos    528:
1.58      djm       529:        if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
                    530:            (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
                    531:                fatal("%s: receive fds failed", __func__);
1.1       provos    532:
                    533:        /* Success */
                    534:        return (1);
                    535: }
                    536:
                    537: void
1.32      markus    538: mm_session_pty_cleanup2(Session *s)
1.1       provos    539: {
                    540:        Buffer m;
                    541:
                    542:        if (s->ttyfd == -1)
                    543:                return;
                    544:        buffer_init(&m);
                    545:        buffer_put_cstring(&m, s->tty);
1.7       mouring   546:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    547:        buffer_free(&m);
1.3       markus    548:
1.1       provos    549:        /* closed dup'ed master */
1.62      djm       550:        if (s->ptymaster != -1 && close(s->ptymaster) < 0)
                    551:                error("close(s->ptymaster/%d): %s",
                    552:                    s->ptymaster, strerror(errno));
1.1       provos    553:
                    554:        /* unlink pty from session */
                    555:        s->ttyfd = -1;
                    556: }
                    557:
                    558: /* Request process termination */
                    559:
                    560: void
                    561: mm_terminate(void)
                    562: {
                    563:        Buffer m;
                    564:
                    565:        buffer_init(&m);
1.7       mouring   566:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    567:        buffer_free(&m);
                    568: }
                    569:
1.2       markus    570: static void
1.1       provos    571: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    572:     char ***prompts, u_int **echo_on)
                    573: {
1.10      deraadt   574:        *name = xstrdup("");
                    575:        *infotxt = xstrdup("");
1.1       provos    576:        *numprompts = 1;
1.43      djm       577:        *prompts = xcalloc(*numprompts, sizeof(char *));
                    578:        *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1       provos    579:        (*echo_on)[0] = 0;
                    580: }
                    581:
                    582: int
                    583: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    584:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    585: {
                    586:        Buffer m;
1.21      markus    587:        u_int success;
1.1       provos    588:        char *challenge;
                    589:
1.8       markus    590:        debug3("%s: entering", __func__);
1.1       provos    591:
                    592:        buffer_init(&m);
1.7       mouring   593:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    594:
1.7       mouring   595:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    596:            &m);
1.21      markus    597:        success = buffer_get_int(&m);
                    598:        if (success == 0) {
1.8       markus    599:                debug3("%s: no challenge", __func__);
1.1       provos    600:                buffer_free(&m);
                    601:                return (-1);
                    602:        }
                    603:
                    604:        /* Get the challenge, and format the response */
                    605:        challenge  = buffer_get_string(&m, NULL);
                    606:        buffer_free(&m);
                    607:
                    608:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    609:        (*prompts)[0] = challenge;
                    610:
1.8       markus    611:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    612:
                    613:        return (0);
                    614: }
                    615:
                    616: int
                    617: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    618: {
                    619:        Buffer m;
                    620:        int authok;
                    621:
1.8       markus    622:        debug3("%s: entering", __func__);
1.1       provos    623:        if (numresponses != 1)
                    624:                return (-1);
                    625:
                    626:        buffer_init(&m);
                    627:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   628:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    629:
1.7       mouring   630:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    631:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    632:
                    633:        authok = buffer_get_int(&m);
                    634:        buffer_free(&m);
                    635:
                    636:        return ((authok == 0) ? -1 : 0);
                    637: }
1.29      markus    638:
                    639: #ifdef GSSAPI
                    640: OM_uint32
1.36      avsm      641: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29      markus    642: {
                    643:        Buffer m;
                    644:        OM_uint32 major;
                    645:
                    646:        /* Client doesn't get to see the context */
                    647:        *ctx = NULL;
                    648:
                    649:        buffer_init(&m);
1.36      avsm      650:        buffer_put_string(&m, goid->elements, goid->length);
1.29      markus    651:
                    652:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
                    653:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
                    654:
                    655:        major = buffer_get_int(&m);
                    656:
                    657:        buffer_free(&m);
                    658:        return (major);
                    659: }
                    660:
                    661: OM_uint32
                    662: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
                    663:     gss_buffer_desc *out, OM_uint32 *flags)
                    664: {
                    665:        Buffer m;
                    666:        OM_uint32 major;
1.30      deraadt   667:        u_int len;
1.29      markus    668:
                    669:        buffer_init(&m);
                    670:        buffer_put_string(&m, in->value, in->length);
                    671:
                    672:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
                    673:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
                    674:
                    675:        major = buffer_get_int(&m);
1.30      deraadt   676:        out->value = buffer_get_string(&m, &len);
                    677:        out->length = len;
1.29      markus    678:        if (flags)
                    679:                *flags = buffer_get_int(&m);
                    680:
                    681:        buffer_free(&m);
                    682:
                    683:        return (major);
1.35      markus    684: }
                    685:
                    686: OM_uint32
                    687: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
                    688: {
                    689:        Buffer m;
                    690:        OM_uint32 major;
                    691:
                    692:        buffer_init(&m);
                    693:        buffer_put_string(&m, gssbuf->value, gssbuf->length);
                    694:        buffer_put_string(&m, gssmic->value, gssmic->length);
                    695:
                    696:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
                    697:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
                    698:            &m);
                    699:
                    700:        major = buffer_get_int(&m);
                    701:        buffer_free(&m);
                    702:        return(major);
1.29      markus    703: }
                    704:
                    705: int
                    706: mm_ssh_gssapi_userok(char *user)
                    707: {
                    708:        Buffer m;
                    709:        int authenticated = 0;
                    710:
                    711:        buffer_init(&m);
                    712:
                    713:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
                    714:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
                    715:                                  &m);
                    716:
                    717:        authenticated = buffer_get_int(&m);
                    718:
                    719:        buffer_free(&m);
                    720:        debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
                    721:        return (authenticated);
                    722: }
                    723: #endif /* GSSAPI */
1.64      djm       724: