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

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