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

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