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

Annotation of src/usr.bin/ssh/mux.c, Revision 1.74

1.74    ! markus      1: /* $OpenBSD: mux.c,v 1.73 2018/07/09 21:18:10 markus Exp $ */
1.1       djm         2: /*
                      3:  * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: /* ssh session multiplexing support */
                     19:
1.2       djm        20: /*
                     21:  * TODO:
1.10      djm        22:  *   - Better signalling from master to slave, especially passing of
1.2       djm        23:  *      error messages
1.10      djm        24:  *   - Better fall-back from mux slave error to new connection.
                     25:  *   - ExitOnForwardingFailure
                     26:  *   - Maybe extension mechanisms for multi-X11/multi-agent forwarding
                     27:  *   - Support ~^Z in mux slaves.
                     28:  *   - Inspect or control sessions in master.
                     29:  *   - If we ever support the "signal" channel request, send signals on
                     30:  *     sessions in master.
1.2       djm        31:  */
                     32:
1.1       djm        33: #include <sys/types.h>
                     34: #include <sys/queue.h>
                     35: #include <sys/stat.h>
                     36: #include <sys/socket.h>
                     37: #include <sys/un.h>
                     38:
                     39: #include <errno.h>
                     40: #include <fcntl.h>
1.10      djm        41: #include <poll.h>
1.1       djm        42: #include <signal.h>
                     43: #include <stdarg.h>
                     44: #include <stddef.h>
                     45: #include <stdlib.h>
                     46: #include <stdio.h>
                     47: #include <string.h>
                     48: #include <unistd.h>
                     49: #include <util.h>
                     50: #include <paths.h>
                     51:
1.10      djm        52: #include "atomicio.h"
1.1       djm        53: #include "xmalloc.h"
                     54: #include "log.h"
                     55: #include "ssh.h"
1.18      markus     56: #include "ssh2.h"
1.1       djm        57: #include "pathnames.h"
                     58: #include "misc.h"
                     59: #include "match.h"
1.73      markus     60: #include "sshbuf.h"
1.1       djm        61: #include "channels.h"
                     62: #include "msg.h"
                     63: #include "packet.h"
                     64: #include "monitor_fdpass.h"
                     65: #include "sshpty.h"
1.74    ! markus     66: #include "sshkey.h"
1.1       djm        67: #include "readconf.h"
                     68: #include "clientloop.h"
1.62      markus     69: #include "ssherr.h"
1.1       djm        70:
                     71: /* from ssh.c */
                     72: extern int tty_flag;
                     73: extern Options options;
                     74: extern int stdin_null_flag;
                     75: extern char *host;
1.8       dtucker    76: extern int subsystem_flag;
1.72      markus     77: extern struct sshbuf *command;
1.10      djm        78: extern volatile sig_atomic_t quit_pending;
1.1       djm        79:
1.2       djm        80: /* Context for session open confirmation callback */
                     81: struct mux_session_confirm_ctx {
1.10      djm        82:        u_int want_tty;
                     83:        u_int want_subsys;
                     84:        u_int want_x_fwd;
                     85:        u_int want_agent_fwd;
1.73      markus     86:        struct sshbuf *cmd;
1.2       djm        87:        char *term;
                     88:        struct termios tio;
                     89:        char **env;
1.17      djm        90:        u_int rid;
1.2       djm        91: };
                     92:
1.48      djm        93: /* Context for stdio fwd open confirmation callback */
                     94: struct mux_stdio_confirm_ctx {
                     95:        u_int rid;
                     96: };
                     97:
1.18      markus     98: /* Context for global channel callback */
                     99: struct mux_channel_confirm_ctx {
                    100:        u_int cid;      /* channel id */
                    101:        u_int rid;      /* request id */
                    102:        int fid;        /* forward id */
                    103: };
                    104:
1.1       djm       105: /* fd to control socket */
                    106: int muxserver_sock = -1;
                    107:
1.10      djm       108: /* client request id */
                    109: u_int muxclient_request_id = 0;
                    110:
1.1       djm       111: /* Multiplexing control command */
                    112: u_int muxclient_command = 0;
                    113:
                    114: /* Set when signalled. */
                    115: static volatile sig_atomic_t muxclient_terminate = 0;
                    116:
                    117: /* PID of multiplex server */
                    118: static u_int muxserver_pid = 0;
                    119:
1.10      djm       120: static Channel *mux_listener_channel = NULL;
                    121:
                    122: struct mux_master_state {
                    123:        int hello_rcvd;
                    124: };
1.1       djm       125:
1.10      djm       126: /* mux protocol messages */
                    127: #define MUX_MSG_HELLO          0x00000001
                    128: #define MUX_C_NEW_SESSION      0x10000002
                    129: #define MUX_C_ALIVE_CHECK      0x10000004
                    130: #define MUX_C_TERMINATE                0x10000005
                    131: #define MUX_C_OPEN_FWD         0x10000006
                    132: #define MUX_C_CLOSE_FWD                0x10000007
                    133: #define MUX_C_NEW_STDIO_FWD    0x10000008
1.25      djm       134: #define MUX_C_STOP_LISTENING   0x10000009
1.62      markus    135: #define MUX_C_PROXY            0x1000000f
1.10      djm       136: #define MUX_S_OK               0x80000001
                    137: #define MUX_S_PERMISSION_DENIED        0x80000002
                    138: #define MUX_S_FAILURE          0x80000003
                    139: #define MUX_S_EXIT_MESSAGE     0x80000004
                    140: #define MUX_S_ALIVE            0x80000005
                    141: #define MUX_S_SESSION_OPENED   0x80000006
1.18      markus    142: #define MUX_S_REMOTE_PORT      0x80000007
1.28      djm       143: #define MUX_S_TTY_ALLOC_FAIL   0x80000008
1.62      markus    144: #define MUX_S_PROXY            0x8000000f
1.10      djm       145:
                    146: /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
                    147: #define MUX_FWD_LOCAL   1
                    148: #define MUX_FWD_REMOTE  2
                    149: #define MUX_FWD_DYNAMIC 3
                    150:
1.66      djm       151: static void mux_session_confirm(struct ssh *, int, int, void *);
                    152: static void mux_stdio_confirm(struct ssh *, int, int, void *);
1.10      djm       153:
1.66      djm       154: static int process_mux_master_hello(struct ssh *, u_int,
                    155:            Channel *, struct sshbuf *, struct sshbuf *);
                    156: static int process_mux_new_session(struct ssh *, u_int,
                    157:            Channel *, struct sshbuf *, struct sshbuf *);
                    158: static int process_mux_alive_check(struct ssh *, u_int,
                    159:            Channel *, struct sshbuf *, struct sshbuf *);
                    160: static int process_mux_terminate(struct ssh *, u_int,
                    161:            Channel *, struct sshbuf *, struct sshbuf *);
                    162: static int process_mux_open_fwd(struct ssh *, u_int,
                    163:            Channel *, struct sshbuf *, struct sshbuf *);
                    164: static int process_mux_close_fwd(struct ssh *, u_int,
                    165:            Channel *, struct sshbuf *, struct sshbuf *);
                    166: static int process_mux_stdio_fwd(struct ssh *, u_int,
                    167:            Channel *, struct sshbuf *, struct sshbuf *);
                    168: static int process_mux_stop_listening(struct ssh *, u_int,
                    169:            Channel *, struct sshbuf *, struct sshbuf *);
                    170: static int process_mux_proxy(struct ssh *, u_int,
                    171:            Channel *, struct sshbuf *, struct sshbuf *);
1.10      djm       172:
                    173: static const struct {
                    174:        u_int type;
1.66      djm       175:        int (*handler)(struct ssh *, u_int, Channel *,
                    176:            struct sshbuf *, struct sshbuf *);
1.10      djm       177: } mux_master_handlers[] = {
                    178:        { MUX_MSG_HELLO, process_mux_master_hello },
                    179:        { MUX_C_NEW_SESSION, process_mux_new_session },
                    180:        { MUX_C_ALIVE_CHECK, process_mux_alive_check },
                    181:        { MUX_C_TERMINATE, process_mux_terminate },
                    182:        { MUX_C_OPEN_FWD, process_mux_open_fwd },
                    183:        { MUX_C_CLOSE_FWD, process_mux_close_fwd },
                    184:        { MUX_C_NEW_STDIO_FWD, process_mux_stdio_fwd },
1.25      djm       185:        { MUX_C_STOP_LISTENING, process_mux_stop_listening },
1.62      markus    186:        { MUX_C_PROXY, process_mux_proxy },
1.10      djm       187:        { 0, NULL }
                    188: };
1.1       djm       189:
1.10      djm       190: /* Cleanup callback fired on closure of mux slave _session_ channel */
                    191: /* ARGSUSED */
1.42      dtucker   192: static void
1.66      djm       193: mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
1.1       djm       194: {
1.66      djm       195:        Channel *cc, *c = channel_by_id(ssh, cid);
1.1       djm       196:
1.10      djm       197:        debug3("%s: entering for channel %d", __func__, cid);
                    198:        if (c == NULL)
                    199:                fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
                    200:        if (c->ctl_chan != -1) {
1.66      djm       201:                if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.10      djm       202:                        fatal("%s: channel %d missing control channel %d",
                    203:                            __func__, c->self, c->ctl_chan);
                    204:                c->ctl_chan = -1;
1.67      djm       205:                cc->remote_id = 0;
                    206:                cc->have_remote_id = 0;
1.66      djm       207:                chan_rcvd_oclose(ssh, cc);
1.1       djm       208:        }
1.66      djm       209:        channel_cancel_cleanup(ssh, c->self);
1.1       djm       210: }
                    211:
1.10      djm       212: /* Cleanup callback fired on closure of mux slave _control_ channel */
                    213: /* ARGSUSED */
1.1       djm       214: static void
1.66      djm       215: mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
1.1       djm       216: {
1.66      djm       217:        Channel *sc, *c = channel_by_id(ssh, cid);
1.1       djm       218:
1.10      djm       219:        debug3("%s: entering for channel %d", __func__, cid);
                    220:        if (c == NULL)
                    221:                fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
1.67      djm       222:        if (c->have_remote_id) {
1.66      djm       223:                if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
1.67      djm       224:                        fatal("%s: channel %d missing session channel %u",
1.10      djm       225:                            __func__, c->self, c->remote_id);
1.67      djm       226:                c->remote_id = 0;
                    227:                c->have_remote_id = 0;
1.10      djm       228:                sc->ctl_chan = -1;
1.39      djm       229:                if (sc->type != SSH_CHANNEL_OPEN &&
                    230:                    sc->type != SSH_CHANNEL_OPENING) {
1.12      djm       231:                        debug2("%s: channel %d: not open", __func__, sc->self);
1.66      djm       232:                        chan_mark_dead(ssh, sc);
1.12      djm       233:                } else {
1.14      djm       234:                        if (sc->istate == CHAN_INPUT_OPEN)
1.66      djm       235:                                chan_read_failed(ssh, sc);
1.14      djm       236:                        if (sc->ostate == CHAN_OUTPUT_OPEN)
1.66      djm       237:                                chan_write_failed(ssh, sc);
1.12      djm       238:                }
1.1       djm       239:        }
1.66      djm       240:        channel_cancel_cleanup(ssh, c->self);
1.1       djm       241: }
                    242:
1.10      djm       243: /* Check mux client environment variables before passing them to mux master. */
                    244: static int
                    245: env_permitted(char *env)
1.1       djm       246: {
1.10      djm       247:        int i, ret;
                    248:        char name[1024], *cp;
1.1       djm       249:
1.10      djm       250:        if ((cp = strchr(env, '=')) == NULL || cp == env)
1.1       djm       251:                return 0;
1.10      djm       252:        ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
                    253:        if (ret <= 0 || (size_t)ret >= sizeof(name)) {
                    254:                error("env_permitted: name '%.100s...' too long", env);
1.1       djm       255:                return 0;
                    256:        }
                    257:
1.10      djm       258:        for (i = 0; i < options.num_send_env; i++)
                    259:                if (match_pattern(name, options.send_env[i]))
                    260:                        return 1;
1.1       djm       261:
1.10      djm       262:        return 0;
                    263: }
1.1       djm       264:
1.10      djm       265: /* Mux master protocol message handlers */
1.1       djm       266:
1.10      djm       267: static int
1.66      djm       268: process_mux_master_hello(struct ssh *ssh, u_int rid,
1.73      markus    269:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       270: {
                    271:        u_int ver;
                    272:        struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1.73      markus    273:        int r;
1.1       djm       274:
1.10      djm       275:        if (state == NULL)
                    276:                fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
                    277:        if (state->hello_rcvd) {
                    278:                error("%s: HELLO received twice", __func__);
                    279:                return -1;
                    280:        }
1.73      markus    281:        if ((r = sshbuf_get_u32(m, &ver)) != 0) {
                    282:                error("%s: malformed message: %s", __func__, ssh_err(r));
1.10      djm       283:                return -1;
                    284:        }
                    285:        if (ver != SSHMUX_VER) {
                    286:                error("Unsupported multiplexing protocol version %d "
                    287:                    "(expected %d)", ver, SSHMUX_VER);
                    288:                return -1;
                    289:        }
                    290:        debug2("%s: channel %d slave version %u", __func__, c->self, ver);
                    291:
                    292:        /* No extensions are presently defined */
1.73      markus    293:        while (sshbuf_len(m) > 0) {
                    294:                char *name = NULL;
                    295:
                    296:                if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
                    297:                    (r = sshbuf_skip_string(m)) != 0) { /* value */
                    298:                        error("%s: malformed extension: %s",
                    299:                            __func__, ssh_err(r));
                    300:                        return -1;
1.1       djm       301:                }
1.10      djm       302:                debug2("Unrecognised slave extension \"%s\"", name);
1.41      djm       303:                free(name);
1.1       djm       304:        }
1.10      djm       305:        state->hello_rcvd = 1;
                    306:        return 0;
                    307: }
                    308:
1.73      markus    309: /* Enqueue a "ok" response to the reply buffer */
                    310: static void
                    311: reply_ok(struct sshbuf *reply, u_int rid)
                    312: {
                    313:        int r;
                    314:
                    315:        if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
                    316:            (r = sshbuf_put_u32(reply, rid)) != 0)
                    317:                fatal("%s: reply: %s", __func__, ssh_err(r));
                    318: }
                    319:
                    320: /* Enqueue an error response to the reply buffer */
                    321: static void
                    322: reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
                    323: {
                    324:        int r;
                    325:
                    326:        if ((r = sshbuf_put_u32(reply, type)) != 0 ||
                    327:            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    328:            (r = sshbuf_put_cstring(reply, msg)) != 0)
                    329:                fatal("%s: reply: %s", __func__, ssh_err(r));
                    330: }
                    331:
1.10      djm       332: static int
1.66      djm       333: process_mux_new_session(struct ssh *ssh, u_int rid,
1.73      markus    334:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       335: {
                    336:        Channel *nc;
                    337:        struct mux_session_confirm_ctx *cctx;
1.73      markus    338:        char *cmd, *cp;
                    339:        u_int i, j, env_len, escape_char, window, packetmax;
                    340:        int r, new_fd[3];
1.1       djm       341:
                    342:        /* Reply for SSHMUX_COMMAND_OPEN */
1.10      djm       343:        cctx = xcalloc(1, sizeof(*cctx));
                    344:        cctx->term = NULL;
1.17      djm       345:        cctx->rid = rid;
1.73      markus    346:        cmd = NULL;
1.36      djm       347:        cctx->env = NULL;
                    348:        env_len = 0;
1.73      markus    349:        if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
                    350:            (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
                    351:            (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
                    352:            (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
                    353:            (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
                    354:            (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
                    355:            (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
                    356:            (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
1.10      djm       357:  malf:
1.41      djm       358:                free(cmd);
1.36      djm       359:                for (j = 0; j < env_len; j++)
1.41      djm       360:                        free(cctx->env[j]);
                    361:                free(cctx->env);
                    362:                free(cctx->term);
                    363:                free(cctx);
1.10      djm       364:                error("%s: malformed message", __func__);
                    365:                return -1;
1.1       djm       366:        }
                    367:
1.10      djm       368: #define MUX_MAX_ENV_VARS       4096
1.73      markus    369:        while (sshbuf_len(m) > 0) {
                    370:                if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
1.10      djm       371:                        goto malf;
                    372:                if (!env_permitted(cp)) {
1.41      djm       373:                        free(cp);
1.10      djm       374:                        continue;
                    375:                }
1.51      deraadt   376:                cctx->env = xreallocarray(cctx->env, env_len + 2,
1.10      djm       377:                    sizeof(*cctx->env));
                    378:                cctx->env[env_len++] = cp;
                    379:                cctx->env[env_len] = NULL;
                    380:                if (env_len > MUX_MAX_ENV_VARS) {
                    381:                        error(">%d environment variables received, ignoring "
                    382:                            "additional", MUX_MAX_ENV_VARS);
                    383:                        break;
                    384:                }
1.1       djm       385:        }
                    386:
1.10      djm       387:        debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
                    388:            "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
                    389:            cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
                    390:            cctx->want_subsys, cctx->term, cmd, env_len);
1.1       djm       391:
1.73      markus    392:        if ((cctx->cmd = sshbuf_new()) == NULL)
                    393:                fatal("%s: sshbuf_new", __func__);
                    394:        if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
                    395:                fatal("%s: sshbuf_put: %s", __func__, ssh_err(r));
1.41      djm       396:        free(cmd);
1.11      djm       397:        cmd = NULL;
1.1       djm       398:
                    399:        /* Gather fds from client */
                    400:        for(i = 0; i < 3; i++) {
1.10      djm       401:                if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
1.1       djm       402:                        error("%s: failed to receive fd %d from slave",
                    403:                            __func__, i);
                    404:                        for (j = 0; j < i; j++)
                    405:                                close(new_fd[j]);
                    406:                        for (j = 0; j < env_len; j++)
1.41      djm       407:                                free(cctx->env[j]);
                    408:                        free(cctx->env);
                    409:                        free(cctx->term);
1.73      markus    410:                        sshbuf_free(cctx->cmd);
1.41      djm       411:                        free(cctx);
1.73      markus    412:                        reply_error(reply, MUX_S_FAILURE, rid,
1.10      djm       413:                            "did not receive file descriptors");
                    414:                        return -1;
1.1       djm       415:                }
                    416:        }
                    417:
1.10      djm       418:        debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
1.1       djm       419:            new_fd[0], new_fd[1], new_fd[2]);
                    420:
1.10      djm       421:        /* XXX support multiple child sessions in future */
1.67      djm       422:        if (c->have_remote_id) {
1.10      djm       423:                debug2("%s: session already open", __func__);
1.73      markus    424:                reply_error(reply, MUX_S_FAILURE, rid,
                    425:                    "Multiple sessions not supported");
1.10      djm       426:  cleanup:
1.1       djm       427:                close(new_fd[0]);
                    428:                close(new_fd[1]);
                    429:                close(new_fd[2]);
1.41      djm       430:                free(cctx->term);
1.1       djm       431:                if (env_len != 0) {
                    432:                        for (i = 0; i < env_len; i++)
1.41      djm       433:                                free(cctx->env[i]);
                    434:                        free(cctx->env);
1.1       djm       435:                }
1.73      markus    436:                sshbuf_free(cctx->cmd);
1.41      djm       437:                free(cctx);
1.1       djm       438:                return 0;
                    439:        }
1.10      djm       440:
                    441:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    442:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    443:                if (!ask_permission("Allow shared connection to %s? ", host)) {
                    444:                        debug2("%s: session refused by user", __func__);
1.73      markus    445:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    446:                            "Permission denied");
1.10      djm       447:                        goto cleanup;
                    448:                }
                    449:        }
                    450:
                    451:        /* Try to pick up ttymodes from client before it goes raw */
                    452:        if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
                    453:                error("%s: tcgetattr: %s", __func__, strerror(errno));
1.1       djm       454:
                    455:        /* enable nonblocking unless tty */
                    456:        if (!isatty(new_fd[0]))
                    457:                set_nonblock(new_fd[0]);
                    458:        if (!isatty(new_fd[1]))
                    459:                set_nonblock(new_fd[1]);
                    460:        if (!isatty(new_fd[2]))
                    461:                set_nonblock(new_fd[2]);
                    462:
                    463:        window = CHAN_SES_WINDOW_DEFAULT;
                    464:        packetmax = CHAN_SES_PACKET_DEFAULT;
                    465:        if (cctx->want_tty) {
                    466:                window >>= 1;
                    467:                packetmax >>= 1;
                    468:        }
1.10      djm       469:
1.66      djm       470:        nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
1.1       djm       471:            new_fd[0], new_fd[1], new_fd[2], window, packetmax,
                    472:            CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
                    473:
1.10      djm       474:        nc->ctl_chan = c->self;         /* link session -> control channel */
                    475:        c->remote_id = nc->self;        /* link control -> session channel */
1.67      djm       476:        c->have_remote_id = 1;
1.10      djm       477:
1.2       djm       478:        if (cctx->want_tty && escape_char != 0xffffffff) {
1.66      djm       479:                channel_register_filter(ssh, nc->self,
1.2       djm       480:                    client_simple_escape_filter, NULL,
1.4       djm       481:                    client_filter_cleanup,
1.2       djm       482:                    client_new_escape_filter_ctx((int)escape_char));
                    483:        }
1.1       djm       484:
1.10      djm       485:        debug2("%s: channel_new: %d linked to control channel %d",
                    486:            __func__, nc->self, nc->ctl_chan);
                    487:
1.66      djm       488:        channel_send_open(ssh, nc->self);
                    489:        channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
1.17      djm       490:        c->mux_pause = 1; /* stop handling messages until open_confirm done */
1.66      djm       491:        channel_register_cleanup(ssh, nc->self,
                    492:            mux_master_session_cleanup_cb, 1);
1.10      djm       493:
1.17      djm       494:        /* reply is deferred, sent by mux_session_confirm */
1.1       djm       495:        return 0;
                    496: }
                    497:
1.10      djm       498: static int
1.66      djm       499: process_mux_alive_check(struct ssh *ssh, u_int rid,
1.73      markus    500:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.1       djm       501: {
1.73      markus    502:        int r;
                    503:
1.10      djm       504:        debug2("%s: channel %d: alive check", __func__, c->self);
1.1       djm       505:
1.10      djm       506:        /* prepare reply */
1.73      markus    507:        if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
                    508:            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    509:            (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
                    510:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.1       djm       511:
1.10      djm       512:        return 0;
1.1       djm       513: }
                    514:
                    515: static int
1.66      djm       516: process_mux_terminate(struct ssh *ssh, u_int rid,
1.73      markus    517:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.1       djm       518: {
1.10      djm       519:        debug2("%s: channel %d: terminate request", __func__, c->self);
1.1       djm       520:
1.10      djm       521:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    522:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    523:                if (!ask_permission("Terminate shared connection to %s? ",
                    524:                    host)) {
                    525:                        debug2("%s: termination refused by user", __func__);
1.73      markus    526:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    527:                            "Permission denied");
1.10      djm       528:                        return 0;
                    529:                }
                    530:        }
1.1       djm       531:
1.10      djm       532:        quit_pending = 1;
1.73      markus    533:        reply_ok(reply, rid);
1.10      djm       534:        /* XXX exit happens too soon - message never makes it to client */
                    535:        return 0;
1.1       djm       536: }
                    537:
1.10      djm       538: static char *
1.46      millert   539: format_forward(u_int ftype, struct Forward *fwd)
1.1       djm       540: {
1.10      djm       541:        char *ret;
1.1       djm       542:
1.10      djm       543:        switch (ftype) {
                    544:        case MUX_FWD_LOCAL:
                    545:                xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
1.46      millert   546:                    (fwd->listen_path != NULL) ? fwd->listen_path :
1.10      djm       547:                    (fwd->listen_host == NULL) ?
1.46      millert   548:                    (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.10      djm       549:                    fwd->listen_host, fwd->listen_port,
1.46      millert   550:                    (fwd->connect_path != NULL) ? fwd->connect_path :
1.10      djm       551:                    fwd->connect_host, fwd->connect_port);
                    552:                break;
                    553:        case MUX_FWD_DYNAMIC:
                    554:                xasprintf(&ret, "dynamic forward %.200s:%d -> *",
                    555:                    (fwd->listen_host == NULL) ?
1.46      millert   556:                    (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.10      djm       557:                     fwd->listen_host, fwd->listen_port);
                    558:                break;
                    559:        case MUX_FWD_REMOTE:
                    560:                xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
1.46      millert   561:                    (fwd->listen_path != NULL) ? fwd->listen_path :
1.10      djm       562:                    (fwd->listen_host == NULL) ?
                    563:                    "LOCALHOST" : fwd->listen_host,
                    564:                    fwd->listen_port,
1.46      millert   565:                    (fwd->connect_path != NULL) ? fwd->connect_path :
1.10      djm       566:                    fwd->connect_host, fwd->connect_port);
1.1       djm       567:                break;
                    568:        default:
1.10      djm       569:                fatal("%s: unknown forward type %u", __func__, ftype);
1.1       djm       570:        }
1.10      djm       571:        return ret;
                    572: }
1.1       djm       573:
1.10      djm       574: static int
                    575: compare_host(const char *a, const char *b)
                    576: {
                    577:        if (a == NULL && b == NULL)
                    578:                return 1;
                    579:        if (a == NULL || b == NULL)
                    580:                return 0;
                    581:        return strcmp(a, b) == 0;
                    582: }
1.1       djm       583:
1.10      djm       584: static int
1.46      millert   585: compare_forward(struct Forward *a, struct Forward *b)
1.10      djm       586: {
                    587:        if (!compare_host(a->listen_host, b->listen_host))
                    588:                return 0;
1.46      millert   589:        if (!compare_host(a->listen_path, b->listen_path))
                    590:                return 0;
1.10      djm       591:        if (a->listen_port != b->listen_port)
                    592:                return 0;
                    593:        if (!compare_host(a->connect_host, b->connect_host))
                    594:                return 0;
1.46      millert   595:        if (!compare_host(a->connect_path, b->connect_path))
                    596:                return 0;
1.10      djm       597:        if (a->connect_port != b->connect_port)
                    598:                return 0;
1.1       djm       599:
1.10      djm       600:        return 1;
                    601: }
1.1       djm       602:
1.18      markus    603: static void
1.66      djm       604: mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
1.18      markus    605: {
                    606:        struct mux_channel_confirm_ctx *fctx = ctxt;
                    607:        char *failmsg = NULL;
1.46      millert   608:        struct Forward *rfwd;
1.18      markus    609:        Channel *c;
1.73      markus    610:        struct sshbuf *out;
                    611:        int r;
1.18      markus    612:
1.66      djm       613:        if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
1.18      markus    614:                /* no channel for reply */
                    615:                error("%s: unknown channel", __func__);
                    616:                return;
                    617:        }
1.73      markus    618:        if ((out = sshbuf_new()) == NULL)
                    619:                fatal("%s: sshbuf_new", __func__);
1.53      djm       620:        if (fctx->fid >= options.num_remote_forwards ||
                    621:            (options.remote_forwards[fctx->fid].connect_path == NULL &&
                    622:            options.remote_forwards[fctx->fid].connect_host == NULL)) {
1.18      markus    623:                xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
                    624:                goto fail;
                    625:        }
                    626:        rfwd = &options.remote_forwards[fctx->fid];
                    627:        debug("%s: %s for: listen %d, connect %s:%d", __func__,
                    628:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.46      millert   629:            rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
                    630:            rfwd->connect_host, rfwd->connect_port);
1.18      markus    631:        if (type == SSH2_MSG_REQUEST_SUCCESS) {
                    632:                if (rfwd->listen_port == 0) {
                    633:                        rfwd->allocated_port = packet_get_int();
1.52      djm       634:                        debug("Allocated port %u for mux remote forward"
1.18      markus    635:                            " to %s:%d", rfwd->allocated_port,
                    636:                            rfwd->connect_host, rfwd->connect_port);
1.73      markus    637:                        if ((r = sshbuf_put_u32(out,
                    638:                            MUX_S_REMOTE_PORT)) != 0 ||
                    639:                            (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
                    640:                            (r = sshbuf_put_u32(out,
                    641:                            rfwd->allocated_port)) != 0)
                    642:                                fatal("%s: reply: %s", __func__, ssh_err(r));
1.70      djm       643:                        channel_update_permission(ssh, rfwd->handle,
1.31      markus    644:                           rfwd->allocated_port);
1.18      markus    645:                } else {
1.73      markus    646:                        reply_ok(out, fctx->rid);
1.18      markus    647:                }
                    648:                goto out;
                    649:        } else {
1.31      markus    650:                if (rfwd->listen_port == 0)
1.70      djm       651:                        channel_update_permission(ssh, rfwd->handle, -1);
1.46      millert   652:                if (rfwd->listen_path != NULL)
                    653:                        xasprintf(&failmsg, "remote port forwarding failed for "
                    654:                            "listen path %s", rfwd->listen_path);
                    655:                else
                    656:                        xasprintf(&failmsg, "remote port forwarding failed for "
                    657:                            "listen port %d", rfwd->listen_port);
1.53      djm       658:
                    659:                 debug2("%s: clearing registered forwarding for listen %d, "
                    660:                    "connect %s:%d", __func__, rfwd->listen_port,
                    661:                    rfwd->connect_path ? rfwd->connect_path :
                    662:                    rfwd->connect_host, rfwd->connect_port);
                    663:
                    664:                free(rfwd->listen_host);
                    665:                free(rfwd->listen_path);
                    666:                free(rfwd->connect_host);
                    667:                free(rfwd->connect_path);
                    668:                memset(rfwd, 0, sizeof(*rfwd));
1.18      markus    669:        }
                    670:  fail:
                    671:        error("%s: %s", __func__, failmsg);
1.73      markus    672:        reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
1.41      djm       673:        free(failmsg);
1.18      markus    674:  out:
1.73      markus    675:        if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                    676:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
                    677:        sshbuf_free(out);
1.18      markus    678:        if (c->mux_pause <= 0)
                    679:                fatal("%s: mux_pause %d", __func__, c->mux_pause);
                    680:        c->mux_pause = 0; /* start processing messages again */
                    681: }
                    682:
1.10      djm       683: static int
1.66      djm       684: process_mux_open_fwd(struct ssh *ssh, u_int rid,
1.73      markus    685:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       686: {
1.46      millert   687:        struct Forward fwd;
1.10      djm       688:        char *fwd_desc = NULL;
1.46      millert   689:        char *listen_addr, *connect_addr;
1.10      djm       690:        u_int ftype;
1.44      djm       691:        u_int lport, cport;
1.73      markus    692:        int r, i, ret = 0, freefwd = 1;
1.10      djm       693:
1.54      djm       694:        memset(&fwd, 0, sizeof(fwd));
                    695:
1.46      millert   696:        /* XXX - lport/cport check redundant */
1.73      markus    697:        if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
                    698:            (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
                    699:            (r = sshbuf_get_u32(m, &lport)) != 0 ||
                    700:            (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
                    701:            (r = sshbuf_get_u32(m, &cport)) != 0 ||
1.46      millert   702:            (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
                    703:            (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
1.10      djm       704:                error("%s: malformed message", __func__);
                    705:                ret = -1;
                    706:                goto out;
                    707:        }
1.46      millert   708:        if (*listen_addr == '\0') {
                    709:                free(listen_addr);
                    710:                listen_addr = NULL;
                    711:        }
                    712:        if (*connect_addr == '\0') {
                    713:                free(connect_addr);
                    714:                connect_addr = NULL;
                    715:        }
                    716:
                    717:        memset(&fwd, 0, sizeof(fwd));
1.44      djm       718:        fwd.listen_port = lport;
1.46      millert   719:        if (fwd.listen_port == PORT_STREAMLOCAL)
                    720:                fwd.listen_path = listen_addr;
                    721:        else
                    722:                fwd.listen_host = listen_addr;
1.44      djm       723:        fwd.connect_port = cport;
1.46      millert   724:        if (fwd.connect_port == PORT_STREAMLOCAL)
                    725:                fwd.connect_path = connect_addr;
                    726:        else
                    727:                fwd.connect_host = connect_addr;
1.10      djm       728:
                    729:        debug2("%s: channel %d: request %s", __func__, c->self,
                    730:            (fwd_desc = format_forward(ftype, &fwd)));
                    731:
                    732:        if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
                    733:            ftype != MUX_FWD_DYNAMIC) {
                    734:                logit("%s: invalid forwarding type %u", __func__, ftype);
                    735:  invalid:
1.46      millert   736:                free(listen_addr);
                    737:                free(connect_addr);
1.73      markus    738:                reply_error(reply, MUX_S_FAILURE, rid,
                    739:                    "Invalid forwarding request");
1.10      djm       740:                return 0;
                    741:        }
1.46      millert   742:        if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
                    743:                logit("%s: streamlocal and dynamic forwards "
                    744:                    "are mutually exclusive", __func__);
                    745:                goto invalid;
                    746:        }
                    747:        if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
1.10      djm       748:                logit("%s: invalid listen port %u", __func__,
                    749:                    fwd.listen_port);
                    750:                goto invalid;
                    751:        }
1.66      djm       752:        if ((fwd.connect_port != PORT_STREAMLOCAL &&
                    753:            fwd.connect_port >= 65536) ||
                    754:            (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
                    755:            fwd.connect_port == 0)) {
1.10      djm       756:                logit("%s: invalid connect port %u", __func__,
                    757:                    fwd.connect_port);
                    758:                goto invalid;
                    759:        }
1.66      djm       760:        if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
                    761:            fwd.connect_path == NULL) {
1.10      djm       762:                logit("%s: missing connect host", __func__);
                    763:                goto invalid;
                    764:        }
                    765:
                    766:        /* Skip forwards that have already been requested */
                    767:        switch (ftype) {
                    768:        case MUX_FWD_LOCAL:
                    769:        case MUX_FWD_DYNAMIC:
                    770:                for (i = 0; i < options.num_local_forwards; i++) {
                    771:                        if (compare_forward(&fwd,
                    772:                            options.local_forwards + i)) {
                    773:  exists:
                    774:                                debug2("%s: found existing forwarding",
                    775:                                    __func__);
1.73      markus    776:                                reply_ok(reply, rid);
1.10      djm       777:                                goto out;
                    778:                        }
                    779:                }
                    780:                break;
                    781:        case MUX_FWD_REMOTE:
                    782:                for (i = 0; i < options.num_remote_forwards; i++) {
1.73      markus    783:                        if (!compare_forward(&fwd, options.remote_forwards + i))
                    784:                                continue;
                    785:                        if (fwd.listen_port != 0)
                    786:                                goto exists;
                    787:                        debug2("%s: found allocated port", __func__);
                    788:                        if ((r = sshbuf_put_u32(reply,
                    789:                            MUX_S_REMOTE_PORT)) != 0 ||
                    790:                            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    791:                            (r = sshbuf_put_u32(reply,
                    792:                            options.remote_forwards[i].allocated_port)) != 0)
                    793:                                fatal("%s: reply: %s", __func__, ssh_err(r));
                    794:                        goto out;
1.10      djm       795:                }
                    796:                break;
                    797:        }
                    798:
                    799:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    800:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    801:                if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
                    802:                        debug2("%s: forwarding refused by user", __func__);
1.73      markus    803:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    804:                            "Permission denied");
1.10      djm       805:                        goto out;
                    806:                }
                    807:        }
                    808:
                    809:        if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
1.66      djm       810:                if (!channel_setup_local_fwd_listener(ssh, &fwd,
1.46      millert   811:                    &options.fwd_opts)) {
1.10      djm       812:  fail:
                    813:                        logit("slave-requested %s failed", fwd_desc);
1.73      markus    814:                        reply_error(reply, MUX_S_FAILURE, rid,
                    815:                            "Port forwarding failed");
1.10      djm       816:                        goto out;
                    817:                }
                    818:                add_local_forward(&options, &fwd);
                    819:                freefwd = 0;
                    820:        } else {
1.18      markus    821:                struct mux_channel_confirm_ctx *fctx;
                    822:
1.66      djm       823:                fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
1.31      markus    824:                if (fwd.handle < 0)
1.10      djm       825:                        goto fail;
                    826:                add_remote_forward(&options, &fwd);
1.18      markus    827:                fctx = xcalloc(1, sizeof(*fctx));
                    828:                fctx->cid = c->self;
                    829:                fctx->rid = rid;
1.20      djm       830:                fctx->fid = options.num_remote_forwards - 1;
1.18      markus    831:                client_register_global_confirm(mux_confirm_remote_forward,
                    832:                    fctx);
1.10      djm       833:                freefwd = 0;
1.18      markus    834:                c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
                    835:                /* delayed reply in mux_confirm_remote_forward */
                    836:                goto out;
1.10      djm       837:        }
1.73      markus    838:        reply_ok(reply, rid);
1.10      djm       839:  out:
1.41      djm       840:        free(fwd_desc);
1.10      djm       841:        if (freefwd) {
1.41      djm       842:                free(fwd.listen_host);
1.46      millert   843:                free(fwd.listen_path);
1.41      djm       844:                free(fwd.connect_host);
1.46      millert   845:                free(fwd.connect_path);
1.10      djm       846:        }
                    847:        return ret;
                    848: }
                    849:
                    850: static int
1.66      djm       851: process_mux_close_fwd(struct ssh *ssh, u_int rid,
1.73      markus    852:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       853: {
1.46      millert   854:        struct Forward fwd, *found_fwd;
1.10      djm       855:        char *fwd_desc = NULL;
1.30      djm       856:        const char *error_reason = NULL;
1.46      millert   857:        char *listen_addr = NULL, *connect_addr = NULL;
1.10      djm       858:        u_int ftype;
1.73      markus    859:        int r, i, ret = 0;
1.44      djm       860:        u_int lport, cport;
1.54      djm       861:
                    862:        memset(&fwd, 0, sizeof(fwd));
1.10      djm       863:
1.73      markus    864:        if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
                    865:            (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
                    866:            (r = sshbuf_get_u32(m, &lport)) != 0 ||
                    867:            (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
                    868:            (r = sshbuf_get_u32(m, &cport)) != 0 ||
1.46      millert   869:            (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
                    870:            (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
1.10      djm       871:                error("%s: malformed message", __func__);
                    872:                ret = -1;
                    873:                goto out;
                    874:        }
                    875:
1.46      millert   876:        if (*listen_addr == '\0') {
                    877:                free(listen_addr);
                    878:                listen_addr = NULL;
1.10      djm       879:        }
1.46      millert   880:        if (*connect_addr == '\0') {
                    881:                free(connect_addr);
                    882:                connect_addr = NULL;
1.10      djm       883:        }
                    884:
1.46      millert   885:        memset(&fwd, 0, sizeof(fwd));
                    886:        fwd.listen_port = lport;
                    887:        if (fwd.listen_port == PORT_STREAMLOCAL)
                    888:                fwd.listen_path = listen_addr;
                    889:        else
                    890:                fwd.listen_host = listen_addr;
                    891:        fwd.connect_port = cport;
                    892:        if (fwd.connect_port == PORT_STREAMLOCAL)
                    893:                fwd.connect_path = connect_addr;
                    894:        else
                    895:                fwd.connect_host = connect_addr;
                    896:
1.30      djm       897:        debug2("%s: channel %d: request cancel %s", __func__, c->self,
1.10      djm       898:            (fwd_desc = format_forward(ftype, &fwd)));
                    899:
1.30      djm       900:        /* make sure this has been requested */
                    901:        found_fwd = NULL;
                    902:        switch (ftype) {
                    903:        case MUX_FWD_LOCAL:
                    904:        case MUX_FWD_DYNAMIC:
                    905:                for (i = 0; i < options.num_local_forwards; i++) {
                    906:                        if (compare_forward(&fwd,
                    907:                            options.local_forwards + i)) {
                    908:                                found_fwd = options.local_forwards + i;
                    909:                                break;
                    910:                        }
                    911:                }
                    912:                break;
                    913:        case MUX_FWD_REMOTE:
                    914:                for (i = 0; i < options.num_remote_forwards; i++) {
                    915:                        if (compare_forward(&fwd,
                    916:                            options.remote_forwards + i)) {
                    917:                                found_fwd = options.remote_forwards + i;
                    918:                                break;
                    919:                        }
                    920:                }
                    921:                break;
                    922:        }
                    923:
                    924:        if (found_fwd == NULL)
                    925:                error_reason = "port not forwarded";
                    926:        else if (ftype == MUX_FWD_REMOTE) {
                    927:                /*
                    928:                 * This shouldn't fail unless we confused the host/port
                    929:                 * between options.remote_forwards and permitted_opens.
1.31      markus    930:                 * However, for dynamic allocated listen ports we need
1.46      millert   931:                 * to use the actual listen port.
1.30      djm       932:                 */
1.66      djm       933:                if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
1.30      djm       934:                        error_reason = "port not in permitted opens";
                    935:        } else {        /* local and dynamic forwards */
                    936:                /* Ditto */
1.66      djm       937:                if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
1.46      millert   938:                    &options.fwd_opts) == -1)
1.30      djm       939:                        error_reason = "port not found";
                    940:        }
                    941:
1.73      markus    942:        if (error_reason != NULL)
                    943:                reply_error(reply, MUX_S_FAILURE, rid, error_reason);
                    944:        else {
                    945:                reply_ok(reply, rid);
1.41      djm       946:                free(found_fwd->listen_host);
1.46      millert   947:                free(found_fwd->listen_path);
1.41      djm       948:                free(found_fwd->connect_host);
1.46      millert   949:                free(found_fwd->connect_path);
1.30      djm       950:                found_fwd->listen_host = found_fwd->connect_host = NULL;
1.46      millert   951:                found_fwd->listen_path = found_fwd->connect_path = NULL;
1.30      djm       952:                found_fwd->listen_port = found_fwd->connect_port = 0;
                    953:        }
1.10      djm       954:  out:
1.41      djm       955:        free(fwd_desc);
1.46      millert   956:        free(listen_addr);
                    957:        free(connect_addr);
1.10      djm       958:
                    959:        return ret;
                    960: }
                    961:
                    962: static int
1.66      djm       963: process_mux_stdio_fwd(struct ssh *ssh, u_int rid,
1.73      markus    964:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       965: {
                    966:        Channel *nc;
1.73      markus    967:        char *chost = NULL;
1.10      djm       968:        u_int cport, i, j;
1.73      markus    969:        int r, new_fd[2];
1.48      djm       970:        struct mux_stdio_confirm_ctx *cctx;
1.10      djm       971:
1.73      markus    972:        if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
                    973:            (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
                    974:            (r = sshbuf_get_u32(m, &cport)) != 0) {
1.41      djm       975:                free(chost);
1.10      djm       976:                error("%s: malformed message", __func__);
                    977:                return -1;
                    978:        }
                    979:
                    980:        debug2("%s: channel %d: request stdio fwd to %s:%u",
                    981:            __func__, c->self, chost, cport);
                    982:
                    983:        /* Gather fds from client */
                    984:        for(i = 0; i < 2; i++) {
                    985:                if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
                    986:                        error("%s: failed to receive fd %d from slave",
                    987:                            __func__, i);
                    988:                        for (j = 0; j < i; j++)
                    989:                                close(new_fd[j]);
1.41      djm       990:                        free(chost);
1.10      djm       991:
                    992:                        /* prepare reply */
1.73      markus    993:                        reply_error(reply, MUX_S_FAILURE, rid,
1.10      djm       994:                            "did not receive file descriptors");
                    995:                        return -1;
                    996:                }
                    997:        }
                    998:
                    999:        debug3("%s: got fds stdin %d, stdout %d", __func__,
                   1000:            new_fd[0], new_fd[1]);
                   1001:
                   1002:        /* XXX support multiple child sessions in future */
1.67      djm      1003:        if (c->have_remote_id) {
1.10      djm      1004:                debug2("%s: session already open", __func__);
1.73      markus   1005:                reply_error(reply, MUX_S_FAILURE, rid,
                   1006:                    "Multiple sessions not supported");
1.10      djm      1007:  cleanup:
                   1008:                close(new_fd[0]);
                   1009:                close(new_fd[1]);
1.41      djm      1010:                free(chost);
1.10      djm      1011:                return 0;
                   1012:        }
                   1013:
                   1014:        if (options.control_master == SSHCTL_MASTER_ASK ||
                   1015:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1.23      dtucker  1016:                if (!ask_permission("Allow forward to %s:%u? ",
1.10      djm      1017:                    chost, cport)) {
                   1018:                        debug2("%s: stdio fwd refused by user", __func__);
1.73      markus   1019:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                   1020:                            "Permission denied");
1.10      djm      1021:                        goto cleanup;
                   1022:                }
                   1023:        }
                   1024:
                   1025:        /* enable nonblocking unless tty */
                   1026:        if (!isatty(new_fd[0]))
                   1027:                set_nonblock(new_fd[0]);
                   1028:        if (!isatty(new_fd[1]))
                   1029:                set_nonblock(new_fd[1]);
                   1030:
1.66      djm      1031:        nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
1.10      djm      1032:
                   1033:        nc->ctl_chan = c->self;         /* link session -> control channel */
                   1034:        c->remote_id = nc->self;        /* link control -> session channel */
1.67      djm      1035:        c->have_remote_id = 1;
1.10      djm      1036:
                   1037:        debug2("%s: channel_new: %d linked to control channel %d",
                   1038:            __func__, nc->self, nc->ctl_chan);
                   1039:
1.66      djm      1040:        channel_register_cleanup(ssh, nc->self,
                   1041:            mux_master_session_cleanup_cb, 1);
1.10      djm      1042:
1.48      djm      1043:        cctx = xcalloc(1, sizeof(*cctx));
                   1044:        cctx->rid = rid;
1.66      djm      1045:        channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
1.48      djm      1046:        c->mux_pause = 1; /* stop handling messages until open_confirm done */
                   1047:
                   1048:        /* reply is deferred, sent by mux_session_confirm */
                   1049:        return 0;
                   1050: }
                   1051:
                   1052: /* Callback on open confirmation in mux master for a mux stdio fwd session. */
                   1053: static void
1.66      djm      1054: mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1.48      djm      1055: {
                   1056:        struct mux_stdio_confirm_ctx *cctx = arg;
                   1057:        Channel *c, *cc;
1.73      markus   1058:        struct sshbuf *reply;
                   1059:        int r;
1.48      djm      1060:
                   1061:        if (cctx == NULL)
                   1062:                fatal("%s: cctx == NULL", __func__);
1.66      djm      1063:        if ((c = channel_by_id(ssh, id)) == NULL)
1.48      djm      1064:                fatal("%s: no channel for id %d", __func__, id);
1.66      djm      1065:        if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.48      djm      1066:                fatal("%s: channel %d lacks control channel %d", __func__,
                   1067:                    id, c->ctl_chan);
1.73      markus   1068:        if ((reply = sshbuf_new()) == NULL)
                   1069:                fatal("%s: sshbuf_new", __func__);
1.48      djm      1070:
                   1071:        if (!success) {
                   1072:                debug3("%s: sending failure reply", __func__);
1.73      markus   1073:                reply_error(reply, MUX_S_FAILURE, cctx->rid,
                   1074:                    "Session open refused by peer");
1.48      djm      1075:                /* prepare reply */
                   1076:                goto done;
                   1077:        }
                   1078:
                   1079:        debug3("%s: sending success reply", __func__);
1.10      djm      1080:        /* prepare reply */
1.73      markus   1081:        if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
                   1082:            (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
                   1083:            (r = sshbuf_put_u32(reply, c->self)) != 0)
                   1084:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.48      djm      1085:
                   1086:  done:
                   1087:        /* Send reply */
1.73      markus   1088:        if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
                   1089:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
                   1090:        sshbuf_free(reply);
1.10      djm      1091:
1.48      djm      1092:        if (cc->mux_pause <= 0)
                   1093:                fatal("%s: mux_pause %d", __func__, cc->mux_pause);
                   1094:        cc->mux_pause = 0; /* start processing messages again */
                   1095:        c->open_confirm_ctx = NULL;
                   1096:        free(cctx);
1.10      djm      1097: }
                   1098:
1.25      djm      1099: static int
1.66      djm      1100: process_mux_stop_listening(struct ssh *ssh, u_int rid,
1.73      markus   1101:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.25      djm      1102: {
                   1103:        debug("%s: channel %d: stop listening", __func__, c->self);
                   1104:
                   1105:        if (options.control_master == SSHCTL_MASTER_ASK ||
                   1106:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                   1107:                if (!ask_permission("Disable further multiplexing on shared "
                   1108:                    "connection to %s? ", host)) {
                   1109:                        debug2("%s: stop listen refused by user", __func__);
1.73      markus   1110:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                   1111:                            "Permission denied");
1.25      djm      1112:                        return 0;
                   1113:                }
                   1114:        }
                   1115:
                   1116:        if (mux_listener_channel != NULL) {
1.66      djm      1117:                channel_free(ssh, mux_listener_channel);
1.25      djm      1118:                client_stop_mux();
1.41      djm      1119:                free(options.control_path);
1.25      djm      1120:                options.control_path = NULL;
                   1121:                mux_listener_channel = NULL;
                   1122:                muxserver_sock = -1;
                   1123:        }
                   1124:
1.73      markus   1125:        reply_ok(reply, rid);
1.25      djm      1126:        return 0;
                   1127: }
                   1128:
1.62      markus   1129: static int
1.66      djm      1130: process_mux_proxy(struct ssh *ssh, u_int rid,
1.73      markus   1131:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.62      markus   1132: {
1.73      markus   1133:        int r;
                   1134:
1.62      markus   1135:        debug("%s: channel %d: proxy request", __func__, c->self);
                   1136:
                   1137:        c->mux_rcb = channel_proxy_downstream;
1.73      markus   1138:        if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
                   1139:            (r = sshbuf_put_u32(reply, rid)) != 0)
                   1140:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.62      markus   1141:
                   1142:        return 0;
                   1143: }
                   1144:
1.10      djm      1145: /* Channel callbacks fired on read/write from mux slave fd */
                   1146: static int
1.66      djm      1147: mux_master_read_cb(struct ssh *ssh, Channel *c)
1.10      djm      1148: {
                   1149:        struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1.73      markus   1150:        struct sshbuf *in = NULL, *out = NULL;
                   1151:        u_int type, rid, i;
                   1152:        int r, ret = -1;
                   1153:
                   1154:        if ((out = sshbuf_new()) == NULL)
                   1155:                fatal("%s: sshbuf_new", __func__);
1.10      djm      1156:
                   1157:        /* Setup ctx and  */
                   1158:        if (c->mux_ctx == NULL) {
1.19      djm      1159:                state = xcalloc(1, sizeof(*state));
1.10      djm      1160:                c->mux_ctx = state;
1.66      djm      1161:                channel_register_cleanup(ssh, c->self,
1.10      djm      1162:                    mux_master_control_cleanup_cb, 0);
                   1163:
                   1164:                /* Send hello */
1.73      markus   1165:                if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
                   1166:                    (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
                   1167:                        fatal("%s: reply: %s", __func__, ssh_err(r));
1.10      djm      1168:                /* no extensions */
1.73      markus   1169:                if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                   1170:                        fatal("%s: sshbuf_put_stringb: %s",
                   1171:                            __func__, ssh_err(r));
1.10      djm      1172:                debug3("%s: channel %d: hello sent", __func__, c->self);
1.73      markus   1173:                ret = 0;
                   1174:                goto out;
1.10      djm      1175:        }
                   1176:
                   1177:        /* Channel code ensures that we receive whole packets */
1.73      markus   1178:        if ((r = sshbuf_froms(c->input, &in)) != 0) {
1.10      djm      1179:  malf:
                   1180:                error("%s: malformed message", __func__);
                   1181:                goto out;
                   1182:        }
                   1183:
1.73      markus   1184:        if ((r = sshbuf_get_u32(in, &type)) != 0)
1.10      djm      1185:                goto malf;
1.73      markus   1186:        debug3("%s: channel %d packet type 0x%08x len %zu",
                   1187:            __func__, c->self, type, sshbuf_len(in));
1.10      djm      1188:
                   1189:        if (type == MUX_MSG_HELLO)
                   1190:                rid = 0;
                   1191:        else {
                   1192:                if (!state->hello_rcvd) {
                   1193:                        error("%s: expected MUX_MSG_HELLO(0x%08x), "
                   1194:                            "received 0x%08x", __func__, MUX_MSG_HELLO, type);
                   1195:                        goto out;
1.1       djm      1196:                }
1.73      markus   1197:                if ((r = sshbuf_get_u32(in, &rid)) != 0)
1.10      djm      1198:                        goto malf;
                   1199:        }
                   1200:
                   1201:        for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
                   1202:                if (type == mux_master_handlers[i].type) {
1.66      djm      1203:                        ret = mux_master_handlers[i].handler(ssh, rid,
1.73      markus   1204:                            c, in, out);
1.10      djm      1205:                        break;
1.1       djm      1206:                }
1.10      djm      1207:        }
                   1208:        if (mux_master_handlers[i].handler == NULL) {
                   1209:                error("%s: unsupported mux message 0x%08x", __func__, type);
1.73      markus   1210:                reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
1.10      djm      1211:                ret = 0;
                   1212:        }
                   1213:        /* Enqueue reply packet */
1.73      markus   1214:        if (sshbuf_len(out) != 0) {
                   1215:                if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                   1216:                        fatal("%s: sshbuf_put_stringb: %s",
                   1217:                            __func__, ssh_err(r));
1.10      djm      1218:        }
                   1219:  out:
1.73      markus   1220:        sshbuf_free(in);
                   1221:        sshbuf_free(out);
1.10      djm      1222:        return ret;
                   1223: }
                   1224:
                   1225: void
1.66      djm      1226: mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
1.10      djm      1227: {
1.73      markus   1228:        struct sshbuf *m;
1.10      djm      1229:        Channel *mux_chan;
1.73      markus   1230:        int r;
1.10      djm      1231:
1.40      dtucker  1232:        debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
1.10      djm      1233:            exitval);
                   1234:
1.66      djm      1235:        if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.10      djm      1236:                fatal("%s: channel %d missing mux channel %d",
                   1237:                    __func__, c->self, c->ctl_chan);
                   1238:
                   1239:        /* Append exit message packet to control socket output queue */
1.73      markus   1240:        if ((m = sshbuf_new()) == NULL)
                   1241:                fatal("%s: sshbuf_new", __func__);
                   1242:        if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
                   1243:            (r = sshbuf_put_u32(m, c->self)) != 0 ||
                   1244:            (r = sshbuf_put_u32(m, exitval)) != 0 ||
                   1245:            (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
                   1246:                fatal("%s: reply: %s", __func__, ssh_err(r));
                   1247:        sshbuf_free(m);
1.10      djm      1248: }
                   1249:
1.28      djm      1250: void
1.66      djm      1251: mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
1.28      djm      1252: {
1.73      markus   1253:        struct sshbuf *m;
1.28      djm      1254:        Channel *mux_chan;
1.73      markus   1255:        int r;
1.28      djm      1256:
                   1257:        debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
                   1258:
1.66      djm      1259:        if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.28      djm      1260:                fatal("%s: channel %d missing mux channel %d",
                   1261:                    __func__, c->self, c->ctl_chan);
                   1262:
                   1263:        /* Append exit message packet to control socket output queue */
1.73      markus   1264:        if ((m = sshbuf_new()) == NULL)
                   1265:                fatal("%s: sshbuf_new", __func__);
                   1266:        if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
                   1267:            (r = sshbuf_put_u32(m, c->self)) != 0 ||
                   1268:            (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
                   1269:                fatal("%s: reply: %s", __func__, ssh_err(r));
                   1270:        sshbuf_free(m);
1.28      djm      1271: }
                   1272:
1.10      djm      1273: /* Prepare a mux master to listen on a Unix domain socket. */
                   1274: void
1.66      djm      1275: muxserver_listen(struct ssh *ssh)
1.10      djm      1276: {
                   1277:        mode_t old_umask;
1.22      djm      1278:        char *orig_control_path = options.control_path;
                   1279:        char rbuf[16+1];
                   1280:        u_int i, r;
1.47      djm      1281:        int oerrno;
1.10      djm      1282:
                   1283:        if (options.control_path == NULL ||
                   1284:            options.control_master == SSHCTL_MASTER_NO)
1.1       djm      1285:                return;
1.10      djm      1286:
                   1287:        debug("setting up multiplex master socket");
                   1288:
1.22      djm      1289:        /*
                   1290:         * Use a temporary path before listen so we can pseudo-atomically
                   1291:         * establish the listening socket in its final location to avoid
                   1292:         * other processes racing in between bind() and listen() and hitting
                   1293:         * an unready socket.
                   1294:         */
                   1295:        for (i = 0; i < sizeof(rbuf) - 1; i++) {
                   1296:                r = arc4random_uniform(26+26+10);
                   1297:                rbuf[i] = (r < 26) ? 'a' + r :
                   1298:                    (r < 26*2) ? 'A' + r - 26 :
                   1299:                    '0' + r - 26 - 26;
                   1300:        }
                   1301:        rbuf[sizeof(rbuf) - 1] = '\0';
                   1302:        options.control_path = NULL;
                   1303:        xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
                   1304:        debug3("%s: temporary control path %s", __func__, options.control_path);
                   1305:
1.10      djm      1306:        old_umask = umask(0177);
1.46      millert  1307:        muxserver_sock = unix_listener(options.control_path, 64, 0);
1.47      djm      1308:        oerrno = errno;
1.46      millert  1309:        umask(old_umask);
                   1310:        if (muxserver_sock < 0) {
1.47      djm      1311:                if (oerrno == EINVAL || oerrno == EADDRINUSE) {
1.10      djm      1312:                        error("ControlSocket %s already exists, "
                   1313:                            "disabling multiplexing", options.control_path);
1.22      djm      1314:  disable_mux_master:
1.26      djm      1315:                        if (muxserver_sock != -1) {
                   1316:                                close(muxserver_sock);
                   1317:                                muxserver_sock = -1;
                   1318:                        }
1.41      djm      1319:                        free(orig_control_path);
                   1320:                        free(options.control_path);
1.10      djm      1321:                        options.control_path = NULL;
                   1322:                        options.control_master = SSHCTL_MASTER_NO;
                   1323:                        return;
1.46      millert  1324:                } else {
                   1325:                        /* unix_listener() logs the error */
                   1326:                        cleanup_exit(255);
                   1327:                }
1.10      djm      1328:        }
                   1329:
1.22      djm      1330:        /* Now atomically "move" the mux socket into position */
                   1331:        if (link(options.control_path, orig_control_path) != 0) {
                   1332:                if (errno != EEXIST) {
1.59      djm      1333:                        fatal("%s: link mux listener %s => %s: %s", __func__,
1.22      djm      1334:                            options.control_path, orig_control_path,
                   1335:                            strerror(errno));
                   1336:                }
                   1337:                error("ControlSocket %s already exists, disabling multiplexing",
                   1338:                    orig_control_path);
                   1339:                unlink(options.control_path);
                   1340:                goto disable_mux_master;
                   1341:        }
                   1342:        unlink(options.control_path);
1.41      djm      1343:        free(options.control_path);
1.22      djm      1344:        options.control_path = orig_control_path;
                   1345:
1.10      djm      1346:        set_nonblock(muxserver_sock);
                   1347:
1.66      djm      1348:        mux_listener_channel = channel_new(ssh, "mux listener",
1.10      djm      1349:            SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
                   1350:            CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.22      djm      1351:            0, options.control_path, 1);
1.10      djm      1352:        mux_listener_channel->mux_rcb = mux_master_read_cb;
                   1353:        debug3("%s: mux listener channel %d fd %d", __func__,
                   1354:            mux_listener_channel->self, mux_listener_channel->sock);
                   1355: }
                   1356:
                   1357: /* Callback on open confirmation in mux master for a mux client session. */
                   1358: static void
1.66      djm      1359: mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
1.10      djm      1360: {
                   1361:        struct mux_session_confirm_ctx *cctx = arg;
                   1362:        const char *display;
1.17      djm      1363:        Channel *c, *cc;
1.73      markus   1364:        int i, r;
                   1365:        struct sshbuf *reply;
1.10      djm      1366:
                   1367:        if (cctx == NULL)
                   1368:                fatal("%s: cctx == NULL", __func__);
1.66      djm      1369:        if ((c = channel_by_id(ssh, id)) == NULL)
1.10      djm      1370:                fatal("%s: no channel for id %d", __func__, id);
1.66      djm      1371:        if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.17      djm      1372:                fatal("%s: channel %d lacks control channel %d", __func__,
                   1373:                    id, c->ctl_chan);
1.73      markus   1374:        if ((reply = sshbuf_new()) == NULL)
                   1375:                fatal("%s: sshbuf_new", __func__);
1.17      djm      1376:
                   1377:        if (!success) {
                   1378:                debug3("%s: sending failure reply", __func__);
1.73      markus   1379:                reply_error(reply, MUX_S_FAILURE, cctx->rid,
                   1380:                    "Session open refused by peer");
1.17      djm      1381:                goto done;
                   1382:        }
1.10      djm      1383:
                   1384:        display = getenv("DISPLAY");
                   1385:        if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
                   1386:                char *proto, *data;
1.21      djm      1387:
1.10      djm      1388:                /* Get reasonable local authentication information. */
1.66      djm      1389:                if (client_x11_get_proto(ssh, display, options.xauth_location,
1.21      djm      1390:                    options.forward_x11_trusted, options.forward_x11_timeout,
1.58      djm      1391:                    &proto, &data) == 0) {
                   1392:                        /* Request forwarding with authentication spoofing. */
                   1393:                        debug("Requesting X11 forwarding with authentication "
                   1394:                            "spoofing.");
1.66      djm      1395:                        x11_request_forwarding_with_spoofing(ssh, id,
                   1396:                            display, proto, data, 1);
1.58      djm      1397:                        /* XXX exit_on_forward_failure */
1.66      djm      1398:                        client_expect_confirm(ssh, id, "X11 forwarding",
1.58      djm      1399:                            CONFIRM_WARN);
                   1400:                }
1.10      djm      1401:        }
                   1402:
                   1403:        if (cctx->want_agent_fwd && options.forward_agent) {
                   1404:                debug("Requesting authentication agent forwarding.");
1.66      djm      1405:                channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1.10      djm      1406:                packet_send();
                   1407:        }
                   1408:
1.66      djm      1409:        client_session2_setup(ssh, id, cctx->want_tty, cctx->want_subsys,
1.73      markus   1410:            cctx->term, &cctx->tio, c->rfd, cctx->cmd, cctx->env);
1.10      djm      1411:
1.17      djm      1412:        debug3("%s: sending success reply", __func__);
                   1413:        /* prepare reply */
1.73      markus   1414:        if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
                   1415:            (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
                   1416:            (r = sshbuf_put_u32(reply, c->self)) != 0)
                   1417:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.17      djm      1418:
                   1419:  done:
                   1420:        /* Send reply */
1.73      markus   1421:        if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
                   1422:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
                   1423:        sshbuf_free(reply);
1.17      djm      1424:
                   1425:        if (cc->mux_pause <= 0)
                   1426:                fatal("%s: mux_pause %d", __func__, cc->mux_pause);
                   1427:        cc->mux_pause = 0; /* start processing messages again */
1.10      djm      1428:        c->open_confirm_ctx = NULL;
1.73      markus   1429:        sshbuf_free(cctx->cmd);
1.41      djm      1430:        free(cctx->term);
1.10      djm      1431:        if (cctx->env != NULL) {
                   1432:                for (i = 0; cctx->env[i] != NULL; i++)
1.41      djm      1433:                        free(cctx->env[i]);
                   1434:                free(cctx->env);
1.1       djm      1435:        }
1.41      djm      1436:        free(cctx);
1.10      djm      1437: }
                   1438:
                   1439: /* ** Multiplexing client support */
                   1440:
                   1441: /* Exit signal handler */
                   1442: static void
                   1443: control_client_sighandler(int signo)
                   1444: {
                   1445:        muxclient_terminate = signo;
                   1446: }
                   1447:
                   1448: /*
                   1449:  * Relay signal handler - used to pass some signals from mux client to
                   1450:  * mux master.
                   1451:  */
                   1452: static void
                   1453: control_client_sigrelay(int signo)
                   1454: {
                   1455:        int save_errno = errno;
                   1456:
                   1457:        if (muxserver_pid > 1)
                   1458:                kill(muxserver_pid, signo);
                   1459:
                   1460:        errno = save_errno;
                   1461: }
1.1       djm      1462:
1.10      djm      1463: static int
1.73      markus   1464: mux_client_read(int fd, struct sshbuf *b, size_t need)
1.10      djm      1465: {
1.73      markus   1466:        size_t have;
1.10      djm      1467:        ssize_t len;
                   1468:        u_char *p;
                   1469:        struct pollfd pfd;
1.73      markus   1470:        int r;
1.10      djm      1471:
                   1472:        pfd.fd = fd;
                   1473:        pfd.events = POLLIN;
1.73      markus   1474:        if ((r = sshbuf_reserve(b, need, &p)) != 0)
                   1475:                fatal("%s: reserve: %s", __func__, ssh_err(r));
1.10      djm      1476:        for (have = 0; have < need; ) {
                   1477:                if (muxclient_terminate) {
                   1478:                        errno = EINTR;
                   1479:                        return -1;
                   1480:                }
                   1481:                len = read(fd, p + have, need - have);
                   1482:                if (len < 0) {
                   1483:                        switch (errno) {
                   1484:                        case EAGAIN:
                   1485:                                (void)poll(&pfd, 1, -1);
                   1486:                                /* FALLTHROUGH */
                   1487:                        case EINTR:
                   1488:                                continue;
                   1489:                        default:
                   1490:                                return -1;
                   1491:                        }
                   1492:                }
                   1493:                if (len == 0) {
                   1494:                        errno = EPIPE;
                   1495:                        return -1;
                   1496:                }
1.73      markus   1497:                have += (size_t)len;
1.1       djm      1498:        }
1.10      djm      1499:        return 0;
                   1500: }
1.1       djm      1501:
1.10      djm      1502: static int
1.73      markus   1503: mux_client_write_packet(int fd, struct sshbuf *m)
1.10      djm      1504: {
1.73      markus   1505:        struct sshbuf *queue;
1.10      djm      1506:        u_int have, need;
1.73      markus   1507:        int r, oerrno, len;
                   1508:        const u_char *ptr;
1.10      djm      1509:        struct pollfd pfd;
                   1510:
                   1511:        pfd.fd = fd;
                   1512:        pfd.events = POLLOUT;
1.73      markus   1513:        if ((queue = sshbuf_new()) == NULL)
                   1514:                fatal("%s: sshbuf_new", __func__);
                   1515:        if ((r = sshbuf_put_stringb(queue, m)) != 0)
                   1516:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
1.10      djm      1517:
1.73      markus   1518:        need = sshbuf_len(queue);
                   1519:        ptr = sshbuf_ptr(queue);
1.10      djm      1520:
                   1521:        for (have = 0; have < need; ) {
                   1522:                if (muxclient_terminate) {
1.73      markus   1523:                        sshbuf_free(queue);
1.10      djm      1524:                        errno = EINTR;
                   1525:                        return -1;
                   1526:                }
                   1527:                len = write(fd, ptr + have, need - have);
                   1528:                if (len < 0) {
                   1529:                        switch (errno) {
                   1530:                        case EAGAIN:
                   1531:                                (void)poll(&pfd, 1, -1);
                   1532:                                /* FALLTHROUGH */
                   1533:                        case EINTR:
                   1534:                                continue;
                   1535:                        default:
                   1536:                                oerrno = errno;
1.73      markus   1537:                                sshbuf_free(queue);
1.10      djm      1538:                                errno = oerrno;
                   1539:                                return -1;
                   1540:                        }
                   1541:                }
                   1542:                if (len == 0) {
1.73      markus   1543:                        sshbuf_free(queue);
1.10      djm      1544:                        errno = EPIPE;
                   1545:                        return -1;
                   1546:                }
                   1547:                have += (u_int)len;
                   1548:        }
1.73      markus   1549:        sshbuf_free(queue);
1.10      djm      1550:        return 0;
                   1551: }
1.1       djm      1552:
1.10      djm      1553: static int
1.73      markus   1554: mux_client_read_packet(int fd, struct sshbuf *m)
1.10      djm      1555: {
1.73      markus   1556:        struct sshbuf *queue;
                   1557:        size_t need, have;
1.45      djm      1558:        const u_char *ptr;
1.73      markus   1559:        int r, oerrno;
1.10      djm      1560:
1.73      markus   1561:        if ((queue = sshbuf_new()) == NULL)
                   1562:                fatal("%s: sshbuf_new", __func__);
                   1563:        if (mux_client_read(fd, queue, 4) != 0) {
1.10      djm      1564:                if ((oerrno = errno) == EPIPE)
1.43      dtucker  1565:                        debug3("%s: read header failed: %s", __func__,
                   1566:                            strerror(errno));
1.73      markus   1567:                sshbuf_free(queue);
1.10      djm      1568:                errno = oerrno;
                   1569:                return -1;
                   1570:        }
1.73      markus   1571:        need = PEEK_U32(sshbuf_ptr(queue));
                   1572:        if (mux_client_read(fd, queue, need) != 0) {
1.10      djm      1573:                oerrno = errno;
                   1574:                debug3("%s: read body failed: %s", __func__, strerror(errno));
1.73      markus   1575:                sshbuf_free(queue);
1.10      djm      1576:                errno = oerrno;
                   1577:                return -1;
                   1578:        }
1.73      markus   1579:        if ((r = sshbuf_get_string_direct(queue, &ptr, &have)) != 0 ||
                   1580:            (r = sshbuf_put(m, ptr, have)) != 0)
                   1581:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1582:        sshbuf_free(queue);
1.10      djm      1583:        return 0;
                   1584: }
1.1       djm      1585:
1.10      djm      1586: static int
                   1587: mux_client_hello_exchange(int fd)
                   1588: {
1.73      markus   1589:        struct sshbuf *m;
1.10      djm      1590:        u_int type, ver;
1.73      markus   1591:        int r, ret = -1;
1.1       djm      1592:
1.73      markus   1593:        if ((m = sshbuf_new()) == NULL)
                   1594:                fatal("%s: sshbuf_new", __func__);
                   1595:        if ((r = sshbuf_put_u32(m, MUX_MSG_HELLO)) != 0 ||
                   1596:            (r = sshbuf_put_u32(m, SSHMUX_VER)) != 0)
                   1597:                fatal("%s: hello: %s", __func__, ssh_err(r));
1.10      djm      1598:        /* no extensions */
                   1599:
1.73      markus   1600:        if (mux_client_write_packet(fd, m) != 0) {
1.65      djm      1601:                debug("%s: write packet: %s", __func__, strerror(errno));
                   1602:                goto out;
                   1603:        }
1.1       djm      1604:
1.73      markus   1605:        sshbuf_reset(m);
1.10      djm      1606:
                   1607:        /* Read their HELLO */
1.73      markus   1608:        if (mux_client_read_packet(fd, m) != 0) {
1.65      djm      1609:                debug("%s: read packet failed", __func__);
                   1610:                goto out;
1.10      djm      1611:        }
                   1612:
1.73      markus   1613:        if ((r = sshbuf_get_u32(m, &type)) != 0)
                   1614:                fatal("%s: decode type: %s", __func__, ssh_err(r));
1.65      djm      1615:        if (type != MUX_MSG_HELLO) {
                   1616:                error("%s: expected HELLO (%u) received %u",
1.10      djm      1617:                    __func__, MUX_MSG_HELLO, type);
1.65      djm      1618:                goto out;
                   1619:        }
1.73      markus   1620:        if ((r = sshbuf_get_u32(m, &ver)) != 0)
                   1621:                fatal("%s: decode version: %s", __func__, ssh_err(r));
1.65      djm      1622:        if (ver != SSHMUX_VER) {
                   1623:                error("Unsupported multiplexing protocol version %d "
1.10      djm      1624:                    "(expected %d)", ver, SSHMUX_VER);
1.65      djm      1625:                goto out;
                   1626:        }
1.10      djm      1627:        debug2("%s: master version %u", __func__, ver);
                   1628:        /* No extensions are presently defined */
1.73      markus   1629:        while (sshbuf_len(m) > 0) {
                   1630:                char *name = NULL;
1.10      djm      1631:
1.73      markus   1632:                if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
                   1633:                    (r = sshbuf_skip_string(m)) != 0) { /* value */
                   1634:                        error("%s: malformed extension: %s",
                   1635:                            __func__, ssh_err(r));
                   1636:                        goto out;
                   1637:                }
1.10      djm      1638:                debug2("Unrecognised master extension \"%s\"", name);
1.41      djm      1639:                free(name);
1.5       djm      1640:        }
1.65      djm      1641:        /* success */
                   1642:        ret = 0;
                   1643:  out:
1.73      markus   1644:        sshbuf_free(m);
1.65      djm      1645:        return ret;
1.10      djm      1646: }
                   1647:
                   1648: static u_int
                   1649: mux_client_request_alive(int fd)
                   1650: {
1.73      markus   1651:        struct sshbuf *m;
1.10      djm      1652:        char *e;
                   1653:        u_int pid, type, rid;
1.73      markus   1654:        int r;
1.10      djm      1655:
                   1656:        debug3("%s: entering", __func__);
                   1657:
1.73      markus   1658:        if ((m = sshbuf_new()) == NULL)
                   1659:                fatal("%s: sshbuf_new", __func__);
                   1660:        if ((r = sshbuf_put_u32(m, MUX_C_ALIVE_CHECK)) != 0 ||
                   1661:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   1662:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      1663:
1.73      markus   1664:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      1665:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   1666:
1.73      markus   1667:        sshbuf_reset(m);
1.1       djm      1668:
1.10      djm      1669:        /* Read their reply */
1.73      markus   1670:        if (mux_client_read_packet(fd, m) != 0) {
                   1671:                sshbuf_free(m);
1.10      djm      1672:                return 0;
                   1673:        }
                   1674:
1.73      markus   1675:        if ((r = sshbuf_get_u32(m, &type)) != 0)
                   1676:                fatal("%s: decode type: %s", __func__, ssh_err(r));
1.10      djm      1677:        if (type != MUX_S_ALIVE) {
1.73      markus   1678:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1679:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.10      djm      1680:                fatal("%s: master returned error: %s", __func__, e);
1.5       djm      1681:        }
1.10      djm      1682:
1.73      markus   1683:        if ((r = sshbuf_get_u32(m, &rid)) != 0)
                   1684:                fatal("%s: decode remote ID: %s", __func__, ssh_err(r));
                   1685:        if (rid != muxclient_request_id)
1.10      djm      1686:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   1687:                    __func__, muxclient_request_id, rid);
1.73      markus   1688:        if ((r = sshbuf_get_u32(m, &pid)) != 0)
                   1689:                fatal("%s: decode PID: %s", __func__, ssh_err(r));
                   1690:        sshbuf_free(m);
1.10      djm      1691:
                   1692:        debug3("%s: done pid = %u", __func__, pid);
                   1693:
                   1694:        muxclient_request_id++;
                   1695:
                   1696:        return pid;
                   1697: }
                   1698:
                   1699: static void
                   1700: mux_client_request_terminate(int fd)
                   1701: {
1.73      markus   1702:        struct sshbuf *m;
1.10      djm      1703:        char *e;
                   1704:        u_int type, rid;
1.73      markus   1705:        int r;
1.10      djm      1706:
                   1707:        debug3("%s: entering", __func__);
                   1708:
1.73      markus   1709:        if ((m = sshbuf_new()) == NULL)
                   1710:                fatal("%s: sshbuf_new", __func__);
                   1711:        if ((r = sshbuf_put_u32(m, MUX_C_TERMINATE)) != 0 ||
                   1712:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   1713:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      1714:
1.73      markus   1715:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      1716:                fatal("%s: write packet: %s", __func__, strerror(errno));
1.1       djm      1717:
1.73      markus   1718:        sshbuf_reset(m);
1.1       djm      1719:
1.10      djm      1720:        /* Read their reply */
1.73      markus   1721:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      1722:                /* Remote end exited already */
                   1723:                if (errno == EPIPE) {
1.73      markus   1724:                        sshbuf_free(m);
1.10      djm      1725:                        return;
1.2       djm      1726:                }
1.10      djm      1727:                fatal("%s: read from master failed: %s",
                   1728:                    __func__, strerror(errno));
                   1729:        }
                   1730:
1.73      markus   1731:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   1732:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   1733:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   1734:        if (rid != muxclient_request_id)
1.10      djm      1735:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   1736:                    __func__, muxclient_request_id, rid);
                   1737:        switch (type) {
                   1738:        case MUX_S_OK:
                   1739:                break;
                   1740:        case MUX_S_PERMISSION_DENIED:
1.73      markus   1741:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1742:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.10      djm      1743:                fatal("Master refused termination request: %s", e);
                   1744:        case MUX_S_FAILURE:
1.73      markus   1745:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1746:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.10      djm      1747:                fatal("%s: termination request failed: %s", __func__, e);
                   1748:        default:
                   1749:                fatal("%s: unexpected response from master 0x%08x",
                   1750:                    __func__, type);
                   1751:        }
1.73      markus   1752:        sshbuf_free(m);
1.10      djm      1753:        muxclient_request_id++;
                   1754: }
                   1755:
                   1756: static int
1.46      millert  1757: mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
1.10      djm      1758: {
1.73      markus   1759:        struct sshbuf *m;
1.10      djm      1760:        char *e, *fwd_desc;
1.73      markus   1761:        const char *lhost, *chost;
1.10      djm      1762:        u_int type, rid;
1.73      markus   1763:        int r;
1.10      djm      1764:
                   1765:        fwd_desc = format_forward(ftype, fwd);
1.30      djm      1766:        debug("Requesting %s %s",
                   1767:            cancel_flag ? "cancellation of" : "forwarding of", fwd_desc);
1.41      djm      1768:        free(fwd_desc);
1.10      djm      1769:
1.73      markus   1770:        type = cancel_flag ? MUX_C_CLOSE_FWD : MUX_C_OPEN_FWD;
                   1771:        if (fwd->listen_path != NULL)
                   1772:                lhost = fwd->listen_path;
                   1773:        else if (fwd->listen_host == NULL)
                   1774:                lhost = "";
                   1775:        else if (*fwd->listen_host == '\0')
                   1776:                lhost = "*";
                   1777:        else
                   1778:                lhost = fwd->listen_host;
                   1779:
                   1780:        if (fwd->connect_path != NULL)
                   1781:                chost = fwd->connect_path;
                   1782:        else if (fwd->connect_host == NULL)
                   1783:                chost = "";
                   1784:        else
                   1785:                chost = fwd->connect_host;
                   1786:
                   1787:        if ((m = sshbuf_new()) == NULL)
                   1788:                fatal("%s: sshbuf_new", __func__);
                   1789:        if ((r = sshbuf_put_u32(m, type)) != 0 ||
                   1790:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
                   1791:            (r = sshbuf_put_u32(m, ftype)) != 0 ||
                   1792:            (r = sshbuf_put_cstring(m, lhost)) != 0 ||
                   1793:            (r = sshbuf_put_u32(m, fwd->listen_port)) != 0 ||
                   1794:            (r = sshbuf_put_cstring(m, chost)) != 0 ||
                   1795:            (r = sshbuf_put_u32(m, fwd->connect_port)) != 0)
                   1796:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      1797:
1.73      markus   1798:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      1799:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   1800:
1.73      markus   1801:        sshbuf_reset(m);
1.10      djm      1802:
                   1803:        /* Read their reply */
1.73      markus   1804:        if (mux_client_read_packet(fd, m) != 0) {
                   1805:                sshbuf_free(m);
1.10      djm      1806:                return -1;
                   1807:        }
                   1808:
1.73      markus   1809:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   1810:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   1811:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   1812:        if (rid != muxclient_request_id)
1.10      djm      1813:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   1814:                    __func__, muxclient_request_id, rid);
1.73      markus   1815:
1.10      djm      1816:        switch (type) {
                   1817:        case MUX_S_OK:
1.1       djm      1818:                break;
1.18      markus   1819:        case MUX_S_REMOTE_PORT:
1.30      djm      1820:                if (cancel_flag)
                   1821:                        fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
1.73      markus   1822:                if ((r = sshbuf_get_u32(m, &fwd->allocated_port)) != 0)
                   1823:                        fatal("%s: decode port: %s", __func__, ssh_err(r));
1.52      djm      1824:                verbose("Allocated port %u for remote forward to %s:%d",
1.18      markus   1825:                    fwd->allocated_port,
                   1826:                    fwd->connect_host ? fwd->connect_host : "",
                   1827:                    fwd->connect_port);
                   1828:                if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1.55      djm      1829:                        fprintf(stdout, "%i\n", fwd->allocated_port);
1.18      markus   1830:                break;
1.10      djm      1831:        case MUX_S_PERMISSION_DENIED:
1.73      markus   1832:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1833:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   1834:                sshbuf_free(m);
1.10      djm      1835:                error("Master refused forwarding request: %s", e);
                   1836:                return -1;
                   1837:        case MUX_S_FAILURE:
1.73      markus   1838:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1839:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   1840:                sshbuf_free(m);
1.24      djm      1841:                error("%s: forwarding request failed: %s", __func__, e);
1.10      djm      1842:                return -1;
1.1       djm      1843:        default:
1.10      djm      1844:                fatal("%s: unexpected response from master 0x%08x",
                   1845:                    __func__, type);
                   1846:        }
1.73      markus   1847:        sshbuf_free(m);
1.10      djm      1848:
                   1849:        muxclient_request_id++;
                   1850:        return 0;
                   1851: }
                   1852:
                   1853: static int
1.30      djm      1854: mux_client_forwards(int fd, int cancel_flag)
1.10      djm      1855: {
1.30      djm      1856:        int i, ret = 0;
1.10      djm      1857:
1.30      djm      1858:        debug3("%s: %s forwardings: %d local, %d remote", __func__,
                   1859:            cancel_flag ? "cancel" : "request",
1.10      djm      1860:            options.num_local_forwards, options.num_remote_forwards);
                   1861:
                   1862:        /* XXX ExitOnForwardingFailure */
                   1863:        for (i = 0; i < options.num_local_forwards; i++) {
1.30      djm      1864:                if (mux_client_forward(fd, cancel_flag,
1.10      djm      1865:                    options.local_forwards[i].connect_port == 0 ?
                   1866:                    MUX_FWD_DYNAMIC : MUX_FWD_LOCAL,
                   1867:                    options.local_forwards + i) != 0)
1.30      djm      1868:                        ret = -1;
1.10      djm      1869:        }
                   1870:        for (i = 0; i < options.num_remote_forwards; i++) {
1.30      djm      1871:                if (mux_client_forward(fd, cancel_flag, MUX_FWD_REMOTE,
1.10      djm      1872:                    options.remote_forwards + i) != 0)
1.30      djm      1873:                        ret = -1;
1.10      djm      1874:        }
1.30      djm      1875:        return ret;
1.10      djm      1876: }
                   1877:
                   1878: static int
                   1879: mux_client_request_session(int fd)
                   1880: {
1.73      markus   1881:        struct sshbuf *m;
                   1882:        char *e;
                   1883:        const char *term;
                   1884:        u_int echar, rid, sid, esid, exitval, type, exitval_seen;
1.10      djm      1885:        extern char **environ;
1.73      markus   1886:        int r, i, devnull, rawmode;
1.10      djm      1887:
                   1888:        debug3("%s: entering", __func__);
                   1889:
                   1890:        if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
                   1891:                error("%s: master alive request failed", __func__);
                   1892:                return -1;
1.1       djm      1893:        }
                   1894:
1.10      djm      1895:        signal(SIGPIPE, SIG_IGN);
                   1896:
                   1897:        if (stdin_null_flag) {
                   1898:                if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
                   1899:                        fatal("open(/dev/null): %s", strerror(errno));
                   1900:                if (dup2(devnull, STDIN_FILENO) == -1)
                   1901:                        fatal("dup2: %s", strerror(errno));
                   1902:                if (devnull > STDERR_FILENO)
                   1903:                        close(devnull);
1.5       djm      1904:        }
1.1       djm      1905:
1.73      markus   1906:        if ((term = getenv("TERM")) == NULL)
                   1907:                term = "";
                   1908:        echar = 0xffffffff;
                   1909:        if (options.escape_char != SSH_ESCAPECHAR_NONE)
                   1910:            echar = (u_int)options.escape_char;
                   1911:
                   1912:        if ((m = sshbuf_new()) == NULL)
                   1913:                fatal("%s: sshbuf_new", __func__);
                   1914:        if ((r = sshbuf_put_u32(m, MUX_C_NEW_SESSION)) != 0 ||
                   1915:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
                   1916:            (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
                   1917:            (r = sshbuf_put_u32(m, tty_flag)) != 0 ||
                   1918:            (r = sshbuf_put_u32(m, options.forward_x11)) != 0 ||
                   1919:            (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
                   1920:            (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
                   1921:            (r = sshbuf_put_u32(m, echar)) != 0 ||
                   1922:            (r = sshbuf_put_cstring(m, term)) != 0 ||
                   1923:            (r = sshbuf_put_stringb(m, command)) != 0)
                   1924:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      1925:
1.71      djm      1926:        /* Pass environment */
1.10      djm      1927:        if (options.num_send_env > 0 && environ != NULL) {
                   1928:                for (i = 0; environ[i] != NULL; i++) {
1.73      markus   1929:                        if (!env_permitted(environ[i]))
                   1930:                                continue;
                   1931:                        if ((r = sshbuf_put_cstring(m, environ[i])) != 0)
                   1932:                                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      1933:                }
1.5       djm      1934:        }
1.73      markus   1935:        for (i = 0; i < options.num_setenv; i++) {
                   1936:                if ((r = sshbuf_put_cstring(m, options.setenv[i])) != 0)
                   1937:                        fatal("%s: request: %s", __func__, ssh_err(r));
                   1938:        }
1.5       djm      1939:
1.73      markus   1940:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      1941:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   1942:
                   1943:        /* Send the stdio file descriptors */
                   1944:        if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
                   1945:            mm_send_fd(fd, STDOUT_FILENO) == -1 ||
                   1946:            mm_send_fd(fd, STDERR_FILENO) == -1)
                   1947:                fatal("%s: send fds failed", __func__);
                   1948:
                   1949:        debug3("%s: session request sent", __func__);
1.1       djm      1950:
1.10      djm      1951:        /* Read their reply */
1.73      markus   1952:        sshbuf_reset(m);
                   1953:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      1954:                error("%s: read from master failed: %s",
                   1955:                    __func__, strerror(errno));
1.73      markus   1956:                sshbuf_free(m);
1.10      djm      1957:                return -1;
                   1958:        }
                   1959:
1.73      markus   1960:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   1961:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   1962:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   1963:        if (rid != muxclient_request_id)
1.10      djm      1964:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   1965:                    __func__, muxclient_request_id, rid);
1.73      markus   1966:
1.10      djm      1967:        switch (type) {
                   1968:        case MUX_S_SESSION_OPENED:
1.73      markus   1969:                if ((r = sshbuf_get_u32(m, &sid)) != 0)
                   1970:                        fatal("%s: decode ID: %s", __func__, ssh_err(r));
1.10      djm      1971:                break;
                   1972:        case MUX_S_PERMISSION_DENIED:
1.73      markus   1973:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1974:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.24      djm      1975:                error("Master refused session request: %s", e);
1.73      markus   1976:                sshbuf_free(m);
1.10      djm      1977:                return -1;
                   1978:        case MUX_S_FAILURE:
1.73      markus   1979:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1980:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.24      djm      1981:                error("%s: session request failed: %s", __func__, e);
1.73      markus   1982:                sshbuf_free(m);
1.10      djm      1983:                return -1;
                   1984:        default:
1.73      markus   1985:                sshbuf_free(m);
1.10      djm      1986:                error("%s: unexpected response from master 0x%08x",
                   1987:                    __func__, type);
                   1988:                return -1;
                   1989:        }
                   1990:        muxclient_request_id++;
1.1       djm      1991:
1.57      semarie  1992:        if (pledge("stdio proc tty", NULL) == -1)
                   1993:                fatal("%s pledge(): %s", __func__, strerror(errno));
                   1994:
1.1       djm      1995:        signal(SIGHUP, control_client_sighandler);
                   1996:        signal(SIGINT, control_client_sighandler);
                   1997:        signal(SIGTERM, control_client_sighandler);
                   1998:        signal(SIGWINCH, control_client_sigrelay);
                   1999:
1.28      djm      2000:        rawmode = tty_flag;
1.1       djm      2001:        if (tty_flag)
1.27      djm      2002:                enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.1       djm      2003:
                   2004:        /*
                   2005:         * Stick around until the controlee closes the client_fd.
1.10      djm      2006:         * Before it does, it is expected to write an exit message.
                   2007:         * This process must read the value and wait for the closure of
                   2008:         * the client_fd; if this one closes early, the multiplex master will
                   2009:         * terminate early too (possibly losing data).
1.1       djm      2010:         */
1.10      djm      2011:        for (exitval = 255, exitval_seen = 0;;) {
1.73      markus   2012:                sshbuf_reset(m);
                   2013:                if (mux_client_read_packet(fd, m) != 0)
1.1       djm      2014:                        break;
1.73      markus   2015:                if ((r = sshbuf_get_u32(m, &type)) != 0)
                   2016:                        fatal("%s: decode type: %s", __func__, ssh_err(r));
1.28      djm      2017:                switch (type) {
                   2018:                case MUX_S_TTY_ALLOC_FAIL:
1.73      markus   2019:                        if ((r = sshbuf_get_u32(m, &esid)) != 0)
                   2020:                                fatal("%s: decode ID: %s",
                   2021:                                    __func__, ssh_err(r));
                   2022:                        if (esid != sid)
1.28      djm      2023:                                fatal("%s: tty alloc fail on unknown session: "
                   2024:                                    "my id %u theirs %u",
                   2025:                                    __func__, sid, esid);
                   2026:                        leave_raw_mode(options.request_tty ==
                   2027:                            REQUEST_TTY_FORCE);
                   2028:                        rawmode = 0;
                   2029:                        continue;
                   2030:                case MUX_S_EXIT_MESSAGE:
1.73      markus   2031:                        if ((r = sshbuf_get_u32(m, &esid)) != 0)
                   2032:                                fatal("%s: decode ID: %s",
                   2033:                                    __func__, ssh_err(r));
                   2034:                        if (esid != sid)
1.28      djm      2035:                                fatal("%s: exit on unknown session: "
                   2036:                                    "my id %u theirs %u",
                   2037:                                    __func__, sid, esid);
                   2038:                        if (exitval_seen)
                   2039:                                fatal("%s: exitval sent twice", __func__);
1.73      markus   2040:                        if ((r = sshbuf_get_u32(m, &exitval)) != 0)
                   2041:                                fatal("%s: decode exit value: %s",
                   2042:                                    __func__, ssh_err(r));
1.28      djm      2043:                        exitval_seen = 1;
                   2044:                        continue;
                   2045:                default:
1.73      markus   2046:                        if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2047:                                fatal("%s: decode error: %s",
                   2048:                                    __func__, ssh_err(r));
1.10      djm      2049:                        fatal("%s: master returned error: %s", __func__, e);
1.1       djm      2050:                }
                   2051:        }
                   2052:
1.10      djm      2053:        close(fd);
1.28      djm      2054:        if (rawmode)
                   2055:                leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.10      djm      2056:
1.1       djm      2057:        if (muxclient_terminate) {
1.69      dtucker  2058:                debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
1.10      djm      2059:                exitval = 255;
                   2060:        } else if (!exitval_seen) {
1.1       djm      2061:                debug2("Control master terminated unexpectedly");
1.10      djm      2062:                exitval = 255;
1.1       djm      2063:        } else
1.10      djm      2064:                debug2("Received exit status from master %d", exitval);
1.1       djm      2065:
                   2066:        if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
                   2067:                fprintf(stderr, "Shared connection to %s closed.\r\n", host);
                   2068:
1.10      djm      2069:        exit(exitval);
                   2070: }
                   2071:
                   2072: static int
1.62      markus   2073: mux_client_proxy(int fd)
                   2074: {
1.73      markus   2075:        struct sshbuf *m;
1.62      markus   2076:        char *e;
                   2077:        u_int type, rid;
1.73      markus   2078:        int r;
1.62      markus   2079:
1.73      markus   2080:        if ((m = sshbuf_new()) == NULL)
                   2081:                fatal("%s: sshbuf_new", __func__);
                   2082:        if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
                   2083:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   2084:                fatal("%s: request: %s", __func__, ssh_err(r));
                   2085:        if (mux_client_write_packet(fd, m) != 0)
1.62      markus   2086:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2087:
1.73      markus   2088:        sshbuf_reset(m);
1.62      markus   2089:
                   2090:        /* Read their reply */
1.73      markus   2091:        if (mux_client_read_packet(fd, m) != 0) {
                   2092:                sshbuf_free(m);
1.62      markus   2093:                return 0;
                   2094:        }
1.73      markus   2095:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2096:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2097:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2098:        if (rid != muxclient_request_id)
                   2099:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2100:                    __func__, muxclient_request_id, rid);
1.62      markus   2101:        if (type != MUX_S_PROXY) {
1.73      markus   2102:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2103:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.62      markus   2104:                fatal("%s: master returned error: %s", __func__, e);
                   2105:        }
1.73      markus   2106:        sshbuf_free(m);
1.62      markus   2107:
                   2108:        debug3("%s: done", __func__);
                   2109:        muxclient_request_id++;
                   2110:        return 0;
                   2111: }
                   2112:
                   2113: static int
1.10      djm      2114: mux_client_request_stdio_fwd(int fd)
                   2115: {
1.73      markus   2116:        struct sshbuf *m;
1.10      djm      2117:        char *e;
                   2118:        u_int type, rid, sid;
1.73      markus   2119:        int r, devnull;
1.10      djm      2120:
                   2121:        debug3("%s: entering", __func__);
                   2122:
                   2123:        if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
                   2124:                error("%s: master alive request failed", __func__);
                   2125:                return -1;
                   2126:        }
                   2127:
                   2128:        signal(SIGPIPE, SIG_IGN);
                   2129:
                   2130:        if (stdin_null_flag) {
                   2131:                if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
                   2132:                        fatal("open(/dev/null): %s", strerror(errno));
                   2133:                if (dup2(devnull, STDIN_FILENO) == -1)
                   2134:                        fatal("dup2: %s", strerror(errno));
                   2135:                if (devnull > STDERR_FILENO)
                   2136:                        close(devnull);
                   2137:        }
                   2138:
1.73      markus   2139:        if ((m = sshbuf_new()) == NULL)
                   2140:                fatal("%s: sshbuf_new", __func__);
                   2141:        if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
                   2142:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
                   2143:            (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
                   2144:            (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
                   2145:            (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
                   2146:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      2147:
1.73      markus   2148:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      2149:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2150:
                   2151:        /* Send the stdio file descriptors */
                   2152:        if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
                   2153:            mm_send_fd(fd, STDOUT_FILENO) == -1)
                   2154:                fatal("%s: send fds failed", __func__);
                   2155:
1.56      semarie  2156:        if (pledge("stdio proc tty", NULL) == -1)
                   2157:                fatal("%s pledge(): %s", __func__, strerror(errno));
                   2158:
1.10      djm      2159:        debug3("%s: stdio forward request sent", __func__);
                   2160:
                   2161:        /* Read their reply */
1.73      markus   2162:        sshbuf_reset(m);
1.10      djm      2163:
1.73      markus   2164:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      2165:                error("%s: read from master failed: %s",
                   2166:                    __func__, strerror(errno));
1.73      markus   2167:                sshbuf_free(m);
1.10      djm      2168:                return -1;
                   2169:        }
                   2170:
1.73      markus   2171:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2172:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2173:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2174:        if (rid != muxclient_request_id)
1.10      djm      2175:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2176:                    __func__, muxclient_request_id, rid);
                   2177:        switch (type) {
                   2178:        case MUX_S_SESSION_OPENED:
1.73      markus   2179:                if ((r = sshbuf_get_u32(m, &sid)) != 0)
                   2180:                        fatal("%s: decode ID: %s", __func__, ssh_err(r));
1.10      djm      2181:                debug("%s: master session id: %u", __func__, sid);
                   2182:                break;
                   2183:        case MUX_S_PERMISSION_DENIED:
1.73      markus   2184:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2185:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   2186:                sshbuf_free(m);
1.24      djm      2187:                fatal("Master refused stdio forwarding request: %s", e);
1.10      djm      2188:        case MUX_S_FAILURE:
1.73      markus   2189:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2190:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   2191:                sshbuf_free(m);
1.48      djm      2192:                fatal("Stdio forwarding request failed: %s", e);
1.10      djm      2193:        default:
1.73      markus   2194:                sshbuf_free(m);
1.10      djm      2195:                error("%s: unexpected response from master 0x%08x",
                   2196:                    __func__, type);
                   2197:                return -1;
                   2198:        }
                   2199:        muxclient_request_id++;
                   2200:
                   2201:        signal(SIGHUP, control_client_sighandler);
                   2202:        signal(SIGINT, control_client_sighandler);
                   2203:        signal(SIGTERM, control_client_sighandler);
                   2204:        signal(SIGWINCH, control_client_sigrelay);
                   2205:
                   2206:        /*
                   2207:         * Stick around until the controlee closes the client_fd.
                   2208:         */
1.73      markus   2209:        sshbuf_reset(m);
                   2210:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      2211:                if (errno == EPIPE ||
                   2212:                    (errno == EINTR && muxclient_terminate != 0))
                   2213:                        return 0;
                   2214:                fatal("%s: mux_client_read_packet: %s",
                   2215:                    __func__, strerror(errno));
                   2216:        }
                   2217:        fatal("%s: master returned unexpected message %u", __func__, type);
                   2218: }
                   2219:
1.25      djm      2220: static void
                   2221: mux_client_request_stop_listening(int fd)
                   2222: {
1.73      markus   2223:        struct sshbuf *m;
1.25      djm      2224:        char *e;
                   2225:        u_int type, rid;
1.73      markus   2226:        int r;
1.25      djm      2227:
                   2228:        debug3("%s: entering", __func__);
                   2229:
1.73      markus   2230:        if ((m = sshbuf_new()) == NULL)
                   2231:                fatal("%s: sshbuf_new", __func__);
                   2232:        if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
                   2233:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   2234:                fatal("%s: request: %s", __func__, ssh_err(r));
1.25      djm      2235:
1.73      markus   2236:        if (mux_client_write_packet(fd, m) != 0)
1.25      djm      2237:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2238:
1.73      markus   2239:        sshbuf_reset(m);
1.25      djm      2240:
                   2241:        /* Read their reply */
1.73      markus   2242:        if (mux_client_read_packet(fd, m) != 0)
1.25      djm      2243:                fatal("%s: read from master failed: %s",
                   2244:                    __func__, strerror(errno));
                   2245:
1.73      markus   2246:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2247:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2248:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2249:        if (rid != muxclient_request_id)
1.25      djm      2250:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2251:                    __func__, muxclient_request_id, rid);
1.73      markus   2252:
1.25      djm      2253:        switch (type) {
                   2254:        case MUX_S_OK:
                   2255:                break;
                   2256:        case MUX_S_PERMISSION_DENIED:
1.73      markus   2257:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2258:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.25      djm      2259:                fatal("Master refused stop listening request: %s", e);
                   2260:        case MUX_S_FAILURE:
1.73      markus   2261:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2262:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.25      djm      2263:                fatal("%s: stop listening request failed: %s", __func__, e);
                   2264:        default:
                   2265:                fatal("%s: unexpected response from master 0x%08x",
                   2266:                    __func__, type);
                   2267:        }
1.73      markus   2268:        sshbuf_free(m);
1.25      djm      2269:        muxclient_request_id++;
                   2270: }
                   2271:
1.10      djm      2272: /* Multiplex client main loop. */
1.62      markus   2273: int
1.10      djm      2274: muxclient(const char *path)
                   2275: {
                   2276:        struct sockaddr_un addr;
                   2277:        int sock;
                   2278:        u_int pid;
                   2279:
                   2280:        if (muxclient_command == 0) {
1.60      dtucker  2281:                if (options.stdio_forward_host != NULL)
1.10      djm      2282:                        muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
                   2283:                else
                   2284:                        muxclient_command = SSHMUX_COMMAND_OPEN;
                   2285:        }
                   2286:
                   2287:        switch (options.control_master) {
                   2288:        case SSHCTL_MASTER_AUTO:
                   2289:        case SSHCTL_MASTER_AUTO_ASK:
                   2290:                debug("auto-mux: Trying existing master");
                   2291:                /* FALLTHROUGH */
                   2292:        case SSHCTL_MASTER_NO:
                   2293:                break;
                   2294:        default:
1.62      markus   2295:                return -1;
1.10      djm      2296:        }
                   2297:
                   2298:        memset(&addr, '\0', sizeof(addr));
                   2299:        addr.sun_family = AF_UNIX;
                   2300:
                   2301:        if (strlcpy(addr.sun_path, path,
                   2302:            sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1.61      dtucker  2303:                fatal("ControlPath too long ('%s' >= %u bytes)", path,
                   2304:                     (unsigned int)sizeof(addr.sun_path));
1.10      djm      2305:
                   2306:        if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
                   2307:                fatal("%s socket(): %s", __func__, strerror(errno));
                   2308:
1.64      guenther 2309:        if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
1.10      djm      2310:                switch (muxclient_command) {
                   2311:                case SSHMUX_COMMAND_OPEN:
                   2312:                case SSHMUX_COMMAND_STDIO_FWD:
                   2313:                        break;
                   2314:                default:
                   2315:                        fatal("Control socket connect(%.100s): %s", path,
                   2316:                            strerror(errno));
                   2317:                }
1.22      djm      2318:                if (errno == ECONNREFUSED &&
                   2319:                    options.control_master != SSHCTL_MASTER_NO) {
                   2320:                        debug("Stale control socket %.100s, unlinking", path);
                   2321:                        unlink(path);
                   2322:                } else if (errno == ENOENT) {
1.10      djm      2323:                        debug("Control socket \"%.100s\" does not exist", path);
1.22      djm      2324:                } else {
1.10      djm      2325:                        error("Control socket connect(%.100s): %s", path,
                   2326:                            strerror(errno));
                   2327:                }
                   2328:                close(sock);
1.62      markus   2329:                return -1;
1.10      djm      2330:        }
                   2331:        set_nonblock(sock);
                   2332:
                   2333:        if (mux_client_hello_exchange(sock) != 0) {
                   2334:                error("%s: master hello exchange failed", __func__);
                   2335:                close(sock);
1.62      markus   2336:                return -1;
1.10      djm      2337:        }
                   2338:
                   2339:        switch (muxclient_command) {
                   2340:        case SSHMUX_COMMAND_ALIVE_CHECK:
                   2341:                if ((pid = mux_client_request_alive(sock)) == 0)
                   2342:                        fatal("%s: master alive check failed", __func__);
1.55      djm      2343:                fprintf(stderr, "Master running (pid=%u)\r\n", pid);
1.10      djm      2344:                exit(0);
                   2345:        case SSHMUX_COMMAND_TERMINATE:
                   2346:                mux_client_request_terminate(sock);
1.63      dtucker  2347:                if (options.log_level != SYSLOG_LEVEL_QUIET)
                   2348:                        fprintf(stderr, "Exit request sent.\r\n");
1.18      markus   2349:                exit(0);
                   2350:        case SSHMUX_COMMAND_FORWARD:
1.30      djm      2351:                if (mux_client_forwards(sock, 0) != 0)
1.18      markus   2352:                        fatal("%s: master forward request failed", __func__);
1.10      djm      2353:                exit(0);
                   2354:        case SSHMUX_COMMAND_OPEN:
1.30      djm      2355:                if (mux_client_forwards(sock, 0) != 0) {
1.10      djm      2356:                        error("%s: master forward request failed", __func__);
1.62      markus   2357:                        return -1;
1.10      djm      2358:                }
                   2359:                mux_client_request_session(sock);
1.62      markus   2360:                return -1;
1.10      djm      2361:        case SSHMUX_COMMAND_STDIO_FWD:
                   2362:                mux_client_request_stdio_fwd(sock);
1.25      djm      2363:                exit(0);
                   2364:        case SSHMUX_COMMAND_STOP:
                   2365:                mux_client_request_stop_listening(sock);
1.63      dtucker  2366:                if (options.log_level != SYSLOG_LEVEL_QUIET)
                   2367:                        fprintf(stderr, "Stop listening request sent.\r\n");
1.30      djm      2368:                exit(0);
                   2369:        case SSHMUX_COMMAND_CANCEL_FWD:
                   2370:                if (mux_client_forwards(sock, 1) != 0)
                   2371:                        error("%s: master cancel forward request failed",
                   2372:                            __func__);
1.10      djm      2373:                exit(0);
1.62      markus   2374:        case SSHMUX_COMMAND_PROXY:
                   2375:                mux_client_proxy(sock);
                   2376:                return (sock);
1.10      djm      2377:        default:
                   2378:                fatal("unrecognised muxclient_command %d", muxclient_command);
                   2379:        }
1.1       djm      2380: }