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

1.83    ! djm         1: /* $OpenBSD: mux.c,v 1.82 2020/04/30 17:12:20 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 */
1.2       djm        19:
1.1       djm        20: #include <sys/types.h>
                     21: #include <sys/queue.h>
                     22: #include <sys/stat.h>
                     23: #include <sys/socket.h>
                     24: #include <sys/un.h>
                     25:
                     26: #include <errno.h>
                     27: #include <fcntl.h>
1.10      djm        28: #include <poll.h>
1.1       djm        29: #include <signal.h>
                     30: #include <stdarg.h>
                     31: #include <stddef.h>
                     32: #include <stdlib.h>
                     33: #include <stdio.h>
                     34: #include <string.h>
                     35: #include <unistd.h>
                     36: #include <util.h>
                     37: #include <paths.h>
                     38:
1.10      djm        39: #include "atomicio.h"
1.1       djm        40: #include "xmalloc.h"
                     41: #include "log.h"
                     42: #include "ssh.h"
1.18      markus     43: #include "ssh2.h"
1.1       djm        44: #include "pathnames.h"
                     45: #include "misc.h"
                     46: #include "match.h"
1.73      markus     47: #include "sshbuf.h"
1.1       djm        48: #include "channels.h"
                     49: #include "msg.h"
                     50: #include "packet.h"
                     51: #include "monitor_fdpass.h"
                     52: #include "sshpty.h"
1.74      markus     53: #include "sshkey.h"
1.1       djm        54: #include "readconf.h"
                     55: #include "clientloop.h"
1.62      markus     56: #include "ssherr.h"
1.78      djm        57:
1.1       djm        58: /* from ssh.c */
                     59: extern int tty_flag;
                     60: extern Options options;
                     61: extern int stdin_null_flag;
                     62: extern char *host;
1.8       dtucker    63: extern int subsystem_flag;
1.72      markus     64: extern struct sshbuf *command;
1.10      djm        65: extern volatile sig_atomic_t quit_pending;
1.1       djm        66:
1.2       djm        67: /* Context for session open confirmation callback */
                     68: struct mux_session_confirm_ctx {
1.10      djm        69:        u_int want_tty;
                     70:        u_int want_subsys;
                     71:        u_int want_x_fwd;
                     72:        u_int want_agent_fwd;
1.73      markus     73:        struct sshbuf *cmd;
1.2       djm        74:        char *term;
                     75:        struct termios tio;
                     76:        char **env;
1.17      djm        77:        u_int rid;
1.2       djm        78: };
                     79:
1.48      djm        80: /* Context for stdio fwd open confirmation callback */
                     81: struct mux_stdio_confirm_ctx {
                     82:        u_int rid;
                     83: };
                     84:
1.18      markus     85: /* Context for global channel callback */
                     86: struct mux_channel_confirm_ctx {
                     87:        u_int cid;      /* channel id */
                     88:        u_int rid;      /* request id */
                     89:        int fid;        /* forward id */
                     90: };
                     91:
1.1       djm        92: /* fd to control socket */
                     93: int muxserver_sock = -1;
                     94:
1.10      djm        95: /* client request id */
                     96: u_int muxclient_request_id = 0;
                     97:
1.1       djm        98: /* Multiplexing control command */
                     99: u_int muxclient_command = 0;
                    100:
                    101: /* Set when signalled. */
                    102: static volatile sig_atomic_t muxclient_terminate = 0;
                    103:
                    104: /* PID of multiplex server */
                    105: static u_int muxserver_pid = 0;
                    106:
1.10      djm       107: static Channel *mux_listener_channel = NULL;
                    108:
                    109: struct mux_master_state {
                    110:        int hello_rcvd;
                    111: };
1.1       djm       112:
1.10      djm       113: /* mux protocol messages */
                    114: #define MUX_MSG_HELLO          0x00000001
                    115: #define MUX_C_NEW_SESSION      0x10000002
                    116: #define MUX_C_ALIVE_CHECK      0x10000004
                    117: #define MUX_C_TERMINATE                0x10000005
                    118: #define MUX_C_OPEN_FWD         0x10000006
                    119: #define MUX_C_CLOSE_FWD                0x10000007
                    120: #define MUX_C_NEW_STDIO_FWD    0x10000008
1.25      djm       121: #define MUX_C_STOP_LISTENING   0x10000009
1.62      markus    122: #define MUX_C_PROXY            0x1000000f
1.10      djm       123: #define MUX_S_OK               0x80000001
                    124: #define MUX_S_PERMISSION_DENIED        0x80000002
                    125: #define MUX_S_FAILURE          0x80000003
                    126: #define MUX_S_EXIT_MESSAGE     0x80000004
                    127: #define MUX_S_ALIVE            0x80000005
                    128: #define MUX_S_SESSION_OPENED   0x80000006
1.18      markus    129: #define MUX_S_REMOTE_PORT      0x80000007
1.28      djm       130: #define MUX_S_TTY_ALLOC_FAIL   0x80000008
1.62      markus    131: #define MUX_S_PROXY            0x8000000f
1.10      djm       132:
                    133: /* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
                    134: #define MUX_FWD_LOCAL   1
                    135: #define MUX_FWD_REMOTE  2
                    136: #define MUX_FWD_DYNAMIC 3
                    137:
1.66      djm       138: static void mux_session_confirm(struct ssh *, int, int, void *);
                    139: static void mux_stdio_confirm(struct ssh *, int, int, void *);
1.10      djm       140:
1.76      djm       141: static int mux_master_process_hello(struct ssh *, u_int,
1.66      djm       142:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       143: static int mux_master_process_new_session(struct ssh *, u_int,
1.66      djm       144:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       145: static int mux_master_process_alive_check(struct ssh *, u_int,
1.66      djm       146:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       147: static int mux_master_process_terminate(struct ssh *, u_int,
1.66      djm       148:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       149: static int mux_master_process_open_fwd(struct ssh *, u_int,
1.66      djm       150:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       151: static int mux_master_process_close_fwd(struct ssh *, u_int,
1.66      djm       152:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       153: static int mux_master_process_stdio_fwd(struct ssh *, u_int,
1.66      djm       154:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       155: static int mux_master_process_stop_listening(struct ssh *, u_int,
1.66      djm       156:            Channel *, struct sshbuf *, struct sshbuf *);
1.76      djm       157: static int mux_master_process_proxy(struct ssh *, u_int,
1.66      djm       158:            Channel *, struct sshbuf *, struct sshbuf *);
1.10      djm       159:
                    160: static const struct {
                    161:        u_int type;
1.66      djm       162:        int (*handler)(struct ssh *, u_int, Channel *,
                    163:            struct sshbuf *, struct sshbuf *);
1.10      djm       164: } mux_master_handlers[] = {
1.76      djm       165:        { MUX_MSG_HELLO, mux_master_process_hello },
                    166:        { MUX_C_NEW_SESSION, mux_master_process_new_session },
                    167:        { MUX_C_ALIVE_CHECK, mux_master_process_alive_check },
                    168:        { MUX_C_TERMINATE, mux_master_process_terminate },
                    169:        { MUX_C_OPEN_FWD, mux_master_process_open_fwd },
                    170:        { MUX_C_CLOSE_FWD, mux_master_process_close_fwd },
                    171:        { MUX_C_NEW_STDIO_FWD, mux_master_process_stdio_fwd },
                    172:        { MUX_C_STOP_LISTENING, mux_master_process_stop_listening },
                    173:        { MUX_C_PROXY, mux_master_process_proxy },
1.10      djm       174:        { 0, NULL }
                    175: };
1.1       djm       176:
1.83    ! djm       177: /* Cleanup callback fired on closure of mux client _session_ channel */
1.10      djm       178: /* ARGSUSED */
1.42      dtucker   179: static void
1.66      djm       180: mux_master_session_cleanup_cb(struct ssh *ssh, int cid, void *unused)
1.1       djm       181: {
1.66      djm       182:        Channel *cc, *c = channel_by_id(ssh, cid);
1.1       djm       183:
1.10      djm       184:        debug3("%s: entering for channel %d", __func__, cid);
                    185:        if (c == NULL)
                    186:                fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
                    187:        if (c->ctl_chan != -1) {
1.66      djm       188:                if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.10      djm       189:                        fatal("%s: channel %d missing control channel %d",
                    190:                            __func__, c->self, c->ctl_chan);
                    191:                c->ctl_chan = -1;
1.67      djm       192:                cc->remote_id = 0;
                    193:                cc->have_remote_id = 0;
1.66      djm       194:                chan_rcvd_oclose(ssh, cc);
1.1       djm       195:        }
1.66      djm       196:        channel_cancel_cleanup(ssh, c->self);
1.1       djm       197: }
                    198:
1.83    ! djm       199: /* Cleanup callback fired on closure of mux client _control_ channel */
1.10      djm       200: /* ARGSUSED */
1.1       djm       201: static void
1.66      djm       202: mux_master_control_cleanup_cb(struct ssh *ssh, int cid, void *unused)
1.1       djm       203: {
1.66      djm       204:        Channel *sc, *c = channel_by_id(ssh, cid);
1.1       djm       205:
1.10      djm       206:        debug3("%s: entering for channel %d", __func__, cid);
                    207:        if (c == NULL)
                    208:                fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
1.67      djm       209:        if (c->have_remote_id) {
1.66      djm       210:                if ((sc = channel_by_id(ssh, c->remote_id)) == NULL)
1.67      djm       211:                        fatal("%s: channel %d missing session channel %u",
1.10      djm       212:                            __func__, c->self, c->remote_id);
1.67      djm       213:                c->remote_id = 0;
                    214:                c->have_remote_id = 0;
1.10      djm       215:                sc->ctl_chan = -1;
1.39      djm       216:                if (sc->type != SSH_CHANNEL_OPEN &&
                    217:                    sc->type != SSH_CHANNEL_OPENING) {
1.12      djm       218:                        debug2("%s: channel %d: not open", __func__, sc->self);
1.66      djm       219:                        chan_mark_dead(ssh, sc);
1.12      djm       220:                } else {
1.14      djm       221:                        if (sc->istate == CHAN_INPUT_OPEN)
1.66      djm       222:                                chan_read_failed(ssh, sc);
1.14      djm       223:                        if (sc->ostate == CHAN_OUTPUT_OPEN)
1.66      djm       224:                                chan_write_failed(ssh, sc);
1.12      djm       225:                }
1.1       djm       226:        }
1.66      djm       227:        channel_cancel_cleanup(ssh, c->self);
1.1       djm       228: }
                    229:
1.10      djm       230: /* Check mux client environment variables before passing them to mux master. */
                    231: static int
                    232: env_permitted(char *env)
1.1       djm       233: {
1.10      djm       234:        int i, ret;
                    235:        char name[1024], *cp;
1.1       djm       236:
1.10      djm       237:        if ((cp = strchr(env, '=')) == NULL || cp == env)
1.1       djm       238:                return 0;
1.10      djm       239:        ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
                    240:        if (ret <= 0 || (size_t)ret >= sizeof(name)) {
1.76      djm       241:                error("%s: name '%.100s...' too long", __func__, env);
1.1       djm       242:                return 0;
                    243:        }
                    244:
1.10      djm       245:        for (i = 0; i < options.num_send_env; i++)
                    246:                if (match_pattern(name, options.send_env[i]))
                    247:                        return 1;
1.1       djm       248:
1.10      djm       249:        return 0;
                    250: }
1.1       djm       251:
1.10      djm       252: /* Mux master protocol message handlers */
1.1       djm       253:
1.10      djm       254: static int
1.76      djm       255: mux_master_process_hello(struct ssh *ssh, u_int rid,
1.73      markus    256:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       257: {
                    258:        u_int ver;
                    259:        struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1.73      markus    260:        int r;
1.1       djm       261:
1.10      djm       262:        if (state == NULL)
                    263:                fatal("%s: channel %d: c->mux_ctx == NULL", __func__, c->self);
                    264:        if (state->hello_rcvd) {
                    265:                error("%s: HELLO received twice", __func__);
                    266:                return -1;
                    267:        }
1.73      markus    268:        if ((r = sshbuf_get_u32(m, &ver)) != 0) {
                    269:                error("%s: malformed message: %s", __func__, ssh_err(r));
1.10      djm       270:                return -1;
                    271:        }
                    272:        if (ver != SSHMUX_VER) {
1.76      djm       273:                error("%s: unsupported multiplexing protocol version %u "
                    274:                    "(expected %u)", __func__, ver, SSHMUX_VER);
1.10      djm       275:                return -1;
                    276:        }
1.83    ! djm       277:        debug2("%s: channel %d client version %u", __func__, c->self, ver);
1.10      djm       278:
                    279:        /* No extensions are presently defined */
1.73      markus    280:        while (sshbuf_len(m) > 0) {
                    281:                char *name = NULL;
1.76      djm       282:                size_t value_len = 0;
1.73      markus    283:
                    284:                if ((r = sshbuf_get_cstring(m, &name, NULL)) != 0 ||
1.76      djm       285:                    (r = sshbuf_get_string_direct(m, NULL, &value_len)) != 0) {
1.73      markus    286:                        error("%s: malformed extension: %s",
                    287:                            __func__, ssh_err(r));
                    288:                        return -1;
1.1       djm       289:                }
1.76      djm       290:                debug2("%s: Unrecognised extension \"%s\" length %zu",
                    291:                    __func__, name, value_len);
1.41      djm       292:                free(name);
1.1       djm       293:        }
1.10      djm       294:        state->hello_rcvd = 1;
                    295:        return 0;
                    296: }
                    297:
1.73      markus    298: /* Enqueue a "ok" response to the reply buffer */
                    299: static void
                    300: reply_ok(struct sshbuf *reply, u_int rid)
                    301: {
                    302:        int r;
                    303:
                    304:        if ((r = sshbuf_put_u32(reply, MUX_S_OK)) != 0 ||
                    305:            (r = sshbuf_put_u32(reply, rid)) != 0)
                    306:                fatal("%s: reply: %s", __func__, ssh_err(r));
                    307: }
                    308:
                    309: /* Enqueue an error response to the reply buffer */
                    310: static void
                    311: reply_error(struct sshbuf *reply, u_int type, u_int rid, const char *msg)
                    312: {
                    313:        int r;
                    314:
                    315:        if ((r = sshbuf_put_u32(reply, type)) != 0 ||
                    316:            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    317:            (r = sshbuf_put_cstring(reply, msg)) != 0)
                    318:                fatal("%s: reply: %s", __func__, ssh_err(r));
                    319: }
                    320:
1.10      djm       321: static int
1.76      djm       322: mux_master_process_new_session(struct ssh *ssh, u_int rid,
1.73      markus    323:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       324: {
                    325:        Channel *nc;
                    326:        struct mux_session_confirm_ctx *cctx;
1.73      markus    327:        char *cmd, *cp;
                    328:        u_int i, j, env_len, escape_char, window, packetmax;
                    329:        int r, new_fd[3];
1.1       djm       330:
                    331:        /* Reply for SSHMUX_COMMAND_OPEN */
1.10      djm       332:        cctx = xcalloc(1, sizeof(*cctx));
                    333:        cctx->term = NULL;
1.17      djm       334:        cctx->rid = rid;
1.73      markus    335:        cmd = NULL;
1.36      djm       336:        cctx->env = NULL;
                    337:        env_len = 0;
1.73      markus    338:        if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
                    339:            (r = sshbuf_get_u32(m, &cctx->want_tty)) != 0 ||
                    340:            (r = sshbuf_get_u32(m, &cctx->want_x_fwd)) != 0 ||
                    341:            (r = sshbuf_get_u32(m, &cctx->want_agent_fwd)) != 0 ||
                    342:            (r = sshbuf_get_u32(m, &cctx->want_subsys)) != 0 ||
                    343:            (r = sshbuf_get_u32(m, &escape_char)) != 0 ||
                    344:            (r = sshbuf_get_cstring(m, &cctx->term, NULL)) != 0 ||
                    345:            (r = sshbuf_get_cstring(m, &cmd, NULL)) != 0) {
1.10      djm       346:  malf:
1.41      djm       347:                free(cmd);
1.36      djm       348:                for (j = 0; j < env_len; j++)
1.41      djm       349:                        free(cctx->env[j]);
                    350:                free(cctx->env);
                    351:                free(cctx->term);
                    352:                free(cctx);
1.10      djm       353:                error("%s: malformed message", __func__);
                    354:                return -1;
1.1       djm       355:        }
                    356:
1.10      djm       357: #define MUX_MAX_ENV_VARS       4096
1.73      markus    358:        while (sshbuf_len(m) > 0) {
                    359:                if ((r = sshbuf_get_cstring(m, &cp, NULL)) != 0)
1.10      djm       360:                        goto malf;
                    361:                if (!env_permitted(cp)) {
1.41      djm       362:                        free(cp);
1.10      djm       363:                        continue;
                    364:                }
1.51      deraadt   365:                cctx->env = xreallocarray(cctx->env, env_len + 2,
1.10      djm       366:                    sizeof(*cctx->env));
                    367:                cctx->env[env_len++] = cp;
                    368:                cctx->env[env_len] = NULL;
                    369:                if (env_len > MUX_MAX_ENV_VARS) {
1.76      djm       370:                        error("%s: >%d environment variables received, "
                    371:                            "ignoring additional", __func__, MUX_MAX_ENV_VARS);
1.10      djm       372:                        break;
                    373:                }
1.1       djm       374:        }
                    375:
1.10      djm       376:        debug2("%s: channel %d: request tty %d, X %d, agent %d, subsys %d, "
                    377:            "term \"%s\", cmd \"%s\", env %u", __func__, c->self,
                    378:            cctx->want_tty, cctx->want_x_fwd, cctx->want_agent_fwd,
                    379:            cctx->want_subsys, cctx->term, cmd, env_len);
1.1       djm       380:
1.73      markus    381:        if ((cctx->cmd = sshbuf_new()) == NULL)
                    382:                fatal("%s: sshbuf_new", __func__);
                    383:        if ((r = sshbuf_put(cctx->cmd, cmd, strlen(cmd))) != 0)
                    384:                fatal("%s: sshbuf_put: %s", __func__, ssh_err(r));
1.41      djm       385:        free(cmd);
1.11      djm       386:        cmd = NULL;
1.1       djm       387:
                    388:        /* Gather fds from client */
                    389:        for(i = 0; i < 3; i++) {
1.10      djm       390:                if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
1.83    ! djm       391:                        error("%s: failed to receive fd %d from client",
1.1       djm       392:                            __func__, i);
                    393:                        for (j = 0; j < i; j++)
                    394:                                close(new_fd[j]);
                    395:                        for (j = 0; j < env_len; j++)
1.41      djm       396:                                free(cctx->env[j]);
                    397:                        free(cctx->env);
                    398:                        free(cctx->term);
1.73      markus    399:                        sshbuf_free(cctx->cmd);
1.41      djm       400:                        free(cctx);
1.73      markus    401:                        reply_error(reply, MUX_S_FAILURE, rid,
1.10      djm       402:                            "did not receive file descriptors");
                    403:                        return -1;
1.1       djm       404:                }
                    405:        }
                    406:
1.10      djm       407:        debug3("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
1.1       djm       408:            new_fd[0], new_fd[1], new_fd[2]);
                    409:
1.10      djm       410:        /* XXX support multiple child sessions in future */
1.67      djm       411:        if (c->have_remote_id) {
1.10      djm       412:                debug2("%s: session already open", __func__);
1.73      markus    413:                reply_error(reply, MUX_S_FAILURE, rid,
                    414:                    "Multiple sessions not supported");
1.10      djm       415:  cleanup:
1.1       djm       416:                close(new_fd[0]);
                    417:                close(new_fd[1]);
                    418:                close(new_fd[2]);
1.41      djm       419:                free(cctx->term);
1.1       djm       420:                if (env_len != 0) {
                    421:                        for (i = 0; i < env_len; i++)
1.41      djm       422:                                free(cctx->env[i]);
                    423:                        free(cctx->env);
1.1       djm       424:                }
1.73      markus    425:                sshbuf_free(cctx->cmd);
1.41      djm       426:                free(cctx);
1.1       djm       427:                return 0;
                    428:        }
1.10      djm       429:
                    430:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    431:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    432:                if (!ask_permission("Allow shared connection to %s? ", host)) {
                    433:                        debug2("%s: session refused by user", __func__);
1.73      markus    434:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    435:                            "Permission denied");
1.10      djm       436:                        goto cleanup;
                    437:                }
                    438:        }
                    439:
                    440:        /* Try to pick up ttymodes from client before it goes raw */
                    441:        if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
                    442:                error("%s: tcgetattr: %s", __func__, strerror(errno));
1.1       djm       443:
                    444:        /* enable nonblocking unless tty */
                    445:        if (!isatty(new_fd[0]))
                    446:                set_nonblock(new_fd[0]);
                    447:        if (!isatty(new_fd[1]))
                    448:                set_nonblock(new_fd[1]);
                    449:        if (!isatty(new_fd[2]))
                    450:                set_nonblock(new_fd[2]);
                    451:
                    452:        window = CHAN_SES_WINDOW_DEFAULT;
                    453:        packetmax = CHAN_SES_PACKET_DEFAULT;
                    454:        if (cctx->want_tty) {
                    455:                window >>= 1;
                    456:                packetmax >>= 1;
                    457:        }
1.10      djm       458:
1.66      djm       459:        nc = channel_new(ssh, "session", SSH_CHANNEL_OPENING,
1.1       djm       460:            new_fd[0], new_fd[1], new_fd[2], window, packetmax,
                    461:            CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
                    462:
1.10      djm       463:        nc->ctl_chan = c->self;         /* link session -> control channel */
                    464:        c->remote_id = nc->self;        /* link control -> session channel */
1.67      djm       465:        c->have_remote_id = 1;
1.10      djm       466:
1.2       djm       467:        if (cctx->want_tty && escape_char != 0xffffffff) {
1.66      djm       468:                channel_register_filter(ssh, nc->self,
1.2       djm       469:                    client_simple_escape_filter, NULL,
1.4       djm       470:                    client_filter_cleanup,
1.2       djm       471:                    client_new_escape_filter_ctx((int)escape_char));
                    472:        }
1.1       djm       473:
1.10      djm       474:        debug2("%s: channel_new: %d linked to control channel %d",
                    475:            __func__, nc->self, nc->ctl_chan);
                    476:
1.66      djm       477:        channel_send_open(ssh, nc->self);
                    478:        channel_register_open_confirm(ssh, nc->self, mux_session_confirm, cctx);
1.17      djm       479:        c->mux_pause = 1; /* stop handling messages until open_confirm done */
1.66      djm       480:        channel_register_cleanup(ssh, nc->self,
                    481:            mux_master_session_cleanup_cb, 1);
1.10      djm       482:
1.17      djm       483:        /* reply is deferred, sent by mux_session_confirm */
1.1       djm       484:        return 0;
                    485: }
                    486:
1.10      djm       487: static int
1.76      djm       488: mux_master_process_alive_check(struct ssh *ssh, u_int rid,
1.73      markus    489:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.1       djm       490: {
1.73      markus    491:        int r;
                    492:
1.10      djm       493:        debug2("%s: channel %d: alive check", __func__, c->self);
1.1       djm       494:
1.10      djm       495:        /* prepare reply */
1.73      markus    496:        if ((r = sshbuf_put_u32(reply, MUX_S_ALIVE)) != 0 ||
                    497:            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    498:            (r = sshbuf_put_u32(reply, (u_int)getpid())) != 0)
                    499:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.1       djm       500:
1.10      djm       501:        return 0;
1.1       djm       502: }
                    503:
                    504: static int
1.76      djm       505: mux_master_process_terminate(struct ssh *ssh, u_int rid,
1.73      markus    506:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.1       djm       507: {
1.10      djm       508:        debug2("%s: channel %d: terminate request", __func__, c->self);
1.1       djm       509:
1.10      djm       510:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    511:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    512:                if (!ask_permission("Terminate shared connection to %s? ",
                    513:                    host)) {
                    514:                        debug2("%s: termination refused by user", __func__);
1.73      markus    515:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    516:                            "Permission denied");
1.10      djm       517:                        return 0;
                    518:                }
                    519:        }
1.1       djm       520:
1.10      djm       521:        quit_pending = 1;
1.73      markus    522:        reply_ok(reply, rid);
1.10      djm       523:        /* XXX exit happens too soon - message never makes it to client */
                    524:        return 0;
1.1       djm       525: }
                    526:
1.10      djm       527: static char *
1.46      millert   528: format_forward(u_int ftype, struct Forward *fwd)
1.1       djm       529: {
1.10      djm       530:        char *ret;
1.1       djm       531:
1.10      djm       532:        switch (ftype) {
                    533:        case MUX_FWD_LOCAL:
                    534:                xasprintf(&ret, "local forward %.200s:%d -> %.200s:%d",
1.46      millert   535:                    (fwd->listen_path != NULL) ? fwd->listen_path :
1.10      djm       536:                    (fwd->listen_host == NULL) ?
1.46      millert   537:                    (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.10      djm       538:                    fwd->listen_host, fwd->listen_port,
1.46      millert   539:                    (fwd->connect_path != NULL) ? fwd->connect_path :
1.10      djm       540:                    fwd->connect_host, fwd->connect_port);
                    541:                break;
                    542:        case MUX_FWD_DYNAMIC:
                    543:                xasprintf(&ret, "dynamic forward %.200s:%d -> *",
                    544:                    (fwd->listen_host == NULL) ?
1.46      millert   545:                    (options.fwd_opts.gateway_ports ? "*" : "LOCALHOST") :
1.10      djm       546:                     fwd->listen_host, fwd->listen_port);
                    547:                break;
                    548:        case MUX_FWD_REMOTE:
                    549:                xasprintf(&ret, "remote forward %.200s:%d -> %.200s:%d",
1.46      millert   550:                    (fwd->listen_path != NULL) ? fwd->listen_path :
1.10      djm       551:                    (fwd->listen_host == NULL) ?
                    552:                    "LOCALHOST" : fwd->listen_host,
                    553:                    fwd->listen_port,
1.46      millert   554:                    (fwd->connect_path != NULL) ? fwd->connect_path :
1.10      djm       555:                    fwd->connect_host, fwd->connect_port);
1.1       djm       556:                break;
                    557:        default:
1.10      djm       558:                fatal("%s: unknown forward type %u", __func__, ftype);
1.1       djm       559:        }
1.10      djm       560:        return ret;
                    561: }
1.1       djm       562:
1.10      djm       563: static int
                    564: compare_host(const char *a, const char *b)
                    565: {
                    566:        if (a == NULL && b == NULL)
                    567:                return 1;
                    568:        if (a == NULL || b == NULL)
                    569:                return 0;
                    570:        return strcmp(a, b) == 0;
                    571: }
1.1       djm       572:
1.10      djm       573: static int
1.46      millert   574: compare_forward(struct Forward *a, struct Forward *b)
1.10      djm       575: {
                    576:        if (!compare_host(a->listen_host, b->listen_host))
                    577:                return 0;
1.46      millert   578:        if (!compare_host(a->listen_path, b->listen_path))
                    579:                return 0;
1.10      djm       580:        if (a->listen_port != b->listen_port)
                    581:                return 0;
                    582:        if (!compare_host(a->connect_host, b->connect_host))
                    583:                return 0;
1.46      millert   584:        if (!compare_host(a->connect_path, b->connect_path))
                    585:                return 0;
1.10      djm       586:        if (a->connect_port != b->connect_port)
                    587:                return 0;
1.1       djm       588:
1.10      djm       589:        return 1;
                    590: }
1.1       djm       591:
1.18      markus    592: static void
1.66      djm       593: mux_confirm_remote_forward(struct ssh *ssh, int type, u_int32_t seq, void *ctxt)
1.18      markus    594: {
                    595:        struct mux_channel_confirm_ctx *fctx = ctxt;
                    596:        char *failmsg = NULL;
1.46      millert   597:        struct Forward *rfwd;
1.18      markus    598:        Channel *c;
1.73      markus    599:        struct sshbuf *out;
1.79      djm       600:        u_int port;
1.73      markus    601:        int r;
1.18      markus    602:
1.66      djm       603:        if ((c = channel_by_id(ssh, fctx->cid)) == NULL) {
1.18      markus    604:                /* no channel for reply */
                    605:                error("%s: unknown channel", __func__);
                    606:                return;
                    607:        }
1.73      markus    608:        if ((out = sshbuf_new()) == NULL)
                    609:                fatal("%s: sshbuf_new", __func__);
1.53      djm       610:        if (fctx->fid >= options.num_remote_forwards ||
                    611:            (options.remote_forwards[fctx->fid].connect_path == NULL &&
                    612:            options.remote_forwards[fctx->fid].connect_host == NULL)) {
1.18      markus    613:                xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
                    614:                goto fail;
                    615:        }
                    616:        rfwd = &options.remote_forwards[fctx->fid];
                    617:        debug("%s: %s for: listen %d, connect %s:%d", __func__,
                    618:            type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.46      millert   619:            rfwd->listen_port, rfwd->connect_path ? rfwd->connect_path :
                    620:            rfwd->connect_host, rfwd->connect_port);
1.18      markus    621:        if (type == SSH2_MSG_REQUEST_SUCCESS) {
                    622:                if (rfwd->listen_port == 0) {
1.79      djm       623:                        if ((r = sshpkt_get_u32(ssh, &port)) != 0)
                    624:                                fatal("%s: packet error: %s",
                    625:                                    __func__, ssh_err(r));
                    626:                        if (port > 65535) {
                    627:                                fatal("Invalid allocated port %u for "
                    628:                                    "mux remote forward to %s:%d", port,
                    629:                                    rfwd->connect_host, rfwd->connect_port);
                    630:                        }
                    631:                        rfwd->allocated_port = (int)port;
1.52      djm       632:                        debug("Allocated port %u for mux remote forward"
1.18      markus    633:                            " to %s:%d", rfwd->allocated_port,
                    634:                            rfwd->connect_host, rfwd->connect_port);
1.73      markus    635:                        if ((r = sshbuf_put_u32(out,
                    636:                            MUX_S_REMOTE_PORT)) != 0 ||
                    637:                            (r = sshbuf_put_u32(out, fctx->rid)) != 0 ||
                    638:                            (r = sshbuf_put_u32(out,
                    639:                            rfwd->allocated_port)) != 0)
                    640:                                fatal("%s: reply: %s", __func__, ssh_err(r));
1.70      djm       641:                        channel_update_permission(ssh, rfwd->handle,
1.31      markus    642:                           rfwd->allocated_port);
1.18      markus    643:                } else {
1.73      markus    644:                        reply_ok(out, fctx->rid);
1.18      markus    645:                }
                    646:                goto out;
                    647:        } else {
1.31      markus    648:                if (rfwd->listen_port == 0)
1.70      djm       649:                        channel_update_permission(ssh, rfwd->handle, -1);
1.46      millert   650:                if (rfwd->listen_path != NULL)
                    651:                        xasprintf(&failmsg, "remote port forwarding failed for "
                    652:                            "listen path %s", rfwd->listen_path);
                    653:                else
                    654:                        xasprintf(&failmsg, "remote port forwarding failed for "
                    655:                            "listen port %d", rfwd->listen_port);
1.53      djm       656:
                    657:                 debug2("%s: clearing registered forwarding for listen %d, "
                    658:                    "connect %s:%d", __func__, rfwd->listen_port,
                    659:                    rfwd->connect_path ? rfwd->connect_path :
                    660:                    rfwd->connect_host, rfwd->connect_port);
                    661:
                    662:                free(rfwd->listen_host);
                    663:                free(rfwd->listen_path);
                    664:                free(rfwd->connect_host);
                    665:                free(rfwd->connect_path);
                    666:                memset(rfwd, 0, sizeof(*rfwd));
1.18      markus    667:        }
                    668:  fail:
                    669:        error("%s: %s", __func__, failmsg);
1.73      markus    670:        reply_error(out, MUX_S_FAILURE, fctx->rid, failmsg);
1.41      djm       671:        free(failmsg);
1.18      markus    672:  out:
1.73      markus    673:        if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                    674:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
                    675:        sshbuf_free(out);
1.18      markus    676:        if (c->mux_pause <= 0)
                    677:                fatal("%s: mux_pause %d", __func__, c->mux_pause);
                    678:        c->mux_pause = 0; /* start processing messages again */
                    679: }
                    680:
1.10      djm       681: static int
1.76      djm       682: mux_master_process_open_fwd(struct ssh *ssh, u_int rid,
1.73      markus    683:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       684: {
1.46      millert   685:        struct Forward fwd;
1.10      djm       686:        char *fwd_desc = NULL;
1.46      millert   687:        char *listen_addr, *connect_addr;
1.10      djm       688:        u_int ftype;
1.44      djm       689:        u_int lport, cport;
1.73      markus    690:        int r, i, ret = 0, freefwd = 1;
1.10      djm       691:
1.54      djm       692:        memset(&fwd, 0, sizeof(fwd));
                    693:
1.46      millert   694:        /* XXX - lport/cport check redundant */
1.73      markus    695:        if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
                    696:            (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
                    697:            (r = sshbuf_get_u32(m, &lport)) != 0 ||
                    698:            (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
                    699:            (r = sshbuf_get_u32(m, &cport)) != 0 ||
1.46      millert   700:            (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
                    701:            (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
1.10      djm       702:                error("%s: malformed message", __func__);
                    703:                ret = -1;
                    704:                goto out;
                    705:        }
1.46      millert   706:        if (*listen_addr == '\0') {
                    707:                free(listen_addr);
                    708:                listen_addr = NULL;
                    709:        }
                    710:        if (*connect_addr == '\0') {
                    711:                free(connect_addr);
                    712:                connect_addr = NULL;
                    713:        }
                    714:
                    715:        memset(&fwd, 0, sizeof(fwd));
1.44      djm       716:        fwd.listen_port = lport;
1.46      millert   717:        if (fwd.listen_port == PORT_STREAMLOCAL)
                    718:                fwd.listen_path = listen_addr;
                    719:        else
                    720:                fwd.listen_host = listen_addr;
1.44      djm       721:        fwd.connect_port = cport;
1.46      millert   722:        if (fwd.connect_port == PORT_STREAMLOCAL)
                    723:                fwd.connect_path = connect_addr;
                    724:        else
                    725:                fwd.connect_host = connect_addr;
1.10      djm       726:
                    727:        debug2("%s: channel %d: request %s", __func__, c->self,
                    728:            (fwd_desc = format_forward(ftype, &fwd)));
                    729:
                    730:        if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
                    731:            ftype != MUX_FWD_DYNAMIC) {
                    732:                logit("%s: invalid forwarding type %u", __func__, ftype);
                    733:  invalid:
1.46      millert   734:                free(listen_addr);
                    735:                free(connect_addr);
1.73      markus    736:                reply_error(reply, MUX_S_FAILURE, rid,
                    737:                    "Invalid forwarding request");
1.10      djm       738:                return 0;
                    739:        }
1.46      millert   740:        if (ftype == MUX_FWD_DYNAMIC && fwd.listen_path) {
                    741:                logit("%s: streamlocal and dynamic forwards "
                    742:                    "are mutually exclusive", __func__);
                    743:                goto invalid;
                    744:        }
                    745:        if (fwd.listen_port != PORT_STREAMLOCAL && fwd.listen_port >= 65536) {
1.10      djm       746:                logit("%s: invalid listen port %u", __func__,
                    747:                    fwd.listen_port);
                    748:                goto invalid;
                    749:        }
1.66      djm       750:        if ((fwd.connect_port != PORT_STREAMLOCAL &&
                    751:            fwd.connect_port >= 65536) ||
                    752:            (ftype != MUX_FWD_DYNAMIC && ftype != MUX_FWD_REMOTE &&
                    753:            fwd.connect_port == 0)) {
1.10      djm       754:                logit("%s: invalid connect port %u", __func__,
                    755:                    fwd.connect_port);
                    756:                goto invalid;
                    757:        }
1.66      djm       758:        if (ftype != MUX_FWD_DYNAMIC && fwd.connect_host == NULL &&
                    759:            fwd.connect_path == NULL) {
1.10      djm       760:                logit("%s: missing connect host", __func__);
                    761:                goto invalid;
                    762:        }
                    763:
                    764:        /* Skip forwards that have already been requested */
                    765:        switch (ftype) {
                    766:        case MUX_FWD_LOCAL:
                    767:        case MUX_FWD_DYNAMIC:
                    768:                for (i = 0; i < options.num_local_forwards; i++) {
                    769:                        if (compare_forward(&fwd,
                    770:                            options.local_forwards + i)) {
                    771:  exists:
                    772:                                debug2("%s: found existing forwarding",
                    773:                                    __func__);
1.73      markus    774:                                reply_ok(reply, rid);
1.10      djm       775:                                goto out;
                    776:                        }
                    777:                }
                    778:                break;
                    779:        case MUX_FWD_REMOTE:
                    780:                for (i = 0; i < options.num_remote_forwards; i++) {
1.73      markus    781:                        if (!compare_forward(&fwd, options.remote_forwards + i))
                    782:                                continue;
                    783:                        if (fwd.listen_port != 0)
                    784:                                goto exists;
                    785:                        debug2("%s: found allocated port", __func__);
                    786:                        if ((r = sshbuf_put_u32(reply,
                    787:                            MUX_S_REMOTE_PORT)) != 0 ||
                    788:                            (r = sshbuf_put_u32(reply, rid)) != 0 ||
                    789:                            (r = sshbuf_put_u32(reply,
                    790:                            options.remote_forwards[i].allocated_port)) != 0)
                    791:                                fatal("%s: reply: %s", __func__, ssh_err(r));
                    792:                        goto out;
1.10      djm       793:                }
                    794:                break;
                    795:        }
                    796:
                    797:        if (options.control_master == SSHCTL_MASTER_ASK ||
                    798:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                    799:                if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
                    800:                        debug2("%s: forwarding refused by user", __func__);
1.73      markus    801:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                    802:                            "Permission denied");
1.10      djm       803:                        goto out;
                    804:                }
                    805:        }
                    806:
                    807:        if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
1.66      djm       808:                if (!channel_setup_local_fwd_listener(ssh, &fwd,
1.46      millert   809:                    &options.fwd_opts)) {
1.10      djm       810:  fail:
1.76      djm       811:                        logit("%s: requested %s failed", __func__, fwd_desc);
1.73      markus    812:                        reply_error(reply, MUX_S_FAILURE, rid,
                    813:                            "Port forwarding failed");
1.10      djm       814:                        goto out;
                    815:                }
                    816:                add_local_forward(&options, &fwd);
                    817:                freefwd = 0;
                    818:        } else {
1.18      markus    819:                struct mux_channel_confirm_ctx *fctx;
                    820:
1.66      djm       821:                fwd.handle = channel_request_remote_forwarding(ssh, &fwd);
1.31      markus    822:                if (fwd.handle < 0)
1.10      djm       823:                        goto fail;
                    824:                add_remote_forward(&options, &fwd);
1.18      markus    825:                fctx = xcalloc(1, sizeof(*fctx));
                    826:                fctx->cid = c->self;
                    827:                fctx->rid = rid;
1.20      djm       828:                fctx->fid = options.num_remote_forwards - 1;
1.18      markus    829:                client_register_global_confirm(mux_confirm_remote_forward,
                    830:                    fctx);
1.10      djm       831:                freefwd = 0;
1.18      markus    832:                c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
                    833:                /* delayed reply in mux_confirm_remote_forward */
                    834:                goto out;
1.10      djm       835:        }
1.73      markus    836:        reply_ok(reply, rid);
1.10      djm       837:  out:
1.41      djm       838:        free(fwd_desc);
1.10      djm       839:        if (freefwd) {
1.41      djm       840:                free(fwd.listen_host);
1.46      millert   841:                free(fwd.listen_path);
1.41      djm       842:                free(fwd.connect_host);
1.46      millert   843:                free(fwd.connect_path);
1.10      djm       844:        }
                    845:        return ret;
                    846: }
                    847:
                    848: static int
1.76      djm       849: mux_master_process_close_fwd(struct ssh *ssh, u_int rid,
1.73      markus    850:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       851: {
1.46      millert   852:        struct Forward fwd, *found_fwd;
1.10      djm       853:        char *fwd_desc = NULL;
1.30      djm       854:        const char *error_reason = NULL;
1.46      millert   855:        char *listen_addr = NULL, *connect_addr = NULL;
1.10      djm       856:        u_int ftype;
1.73      markus    857:        int r, i, ret = 0;
1.44      djm       858:        u_int lport, cport;
1.54      djm       859:
                    860:        memset(&fwd, 0, sizeof(fwd));
1.10      djm       861:
1.73      markus    862:        if ((r = sshbuf_get_u32(m, &ftype)) != 0 ||
                    863:            (r = sshbuf_get_cstring(m, &listen_addr, NULL)) != 0 ||
                    864:            (r = sshbuf_get_u32(m, &lport)) != 0 ||
                    865:            (r = sshbuf_get_cstring(m, &connect_addr, NULL)) != 0 ||
                    866:            (r = sshbuf_get_u32(m, &cport)) != 0 ||
1.46      millert   867:            (lport != (u_int)PORT_STREAMLOCAL && lport > 65535) ||
                    868:            (cport != (u_int)PORT_STREAMLOCAL && cport > 65535)) {
1.10      djm       869:                error("%s: malformed message", __func__);
                    870:                ret = -1;
                    871:                goto out;
                    872:        }
                    873:
1.46      millert   874:        if (*listen_addr == '\0') {
                    875:                free(listen_addr);
                    876:                listen_addr = NULL;
1.10      djm       877:        }
1.46      millert   878:        if (*connect_addr == '\0') {
                    879:                free(connect_addr);
                    880:                connect_addr = NULL;
1.10      djm       881:        }
                    882:
1.46      millert   883:        memset(&fwd, 0, sizeof(fwd));
                    884:        fwd.listen_port = lport;
                    885:        if (fwd.listen_port == PORT_STREAMLOCAL)
                    886:                fwd.listen_path = listen_addr;
                    887:        else
                    888:                fwd.listen_host = listen_addr;
                    889:        fwd.connect_port = cport;
                    890:        if (fwd.connect_port == PORT_STREAMLOCAL)
                    891:                fwd.connect_path = connect_addr;
                    892:        else
                    893:                fwd.connect_host = connect_addr;
                    894:
1.30      djm       895:        debug2("%s: channel %d: request cancel %s", __func__, c->self,
1.10      djm       896:            (fwd_desc = format_forward(ftype, &fwd)));
                    897:
1.30      djm       898:        /* make sure this has been requested */
                    899:        found_fwd = NULL;
                    900:        switch (ftype) {
                    901:        case MUX_FWD_LOCAL:
                    902:        case MUX_FWD_DYNAMIC:
                    903:                for (i = 0; i < options.num_local_forwards; i++) {
                    904:                        if (compare_forward(&fwd,
                    905:                            options.local_forwards + i)) {
                    906:                                found_fwd = options.local_forwards + i;
                    907:                                break;
                    908:                        }
                    909:                }
                    910:                break;
                    911:        case MUX_FWD_REMOTE:
                    912:                for (i = 0; i < options.num_remote_forwards; i++) {
                    913:                        if (compare_forward(&fwd,
                    914:                            options.remote_forwards + i)) {
                    915:                                found_fwd = options.remote_forwards + i;
                    916:                                break;
                    917:                        }
                    918:                }
                    919:                break;
                    920:        }
                    921:
                    922:        if (found_fwd == NULL)
                    923:                error_reason = "port not forwarded";
                    924:        else if (ftype == MUX_FWD_REMOTE) {
                    925:                /*
                    926:                 * This shouldn't fail unless we confused the host/port
                    927:                 * between options.remote_forwards and permitted_opens.
1.31      markus    928:                 * However, for dynamic allocated listen ports we need
1.46      millert   929:                 * to use the actual listen port.
1.30      djm       930:                 */
1.66      djm       931:                if (channel_request_rforward_cancel(ssh, found_fwd) == -1)
1.30      djm       932:                        error_reason = "port not in permitted opens";
                    933:        } else {        /* local and dynamic forwards */
                    934:                /* Ditto */
1.66      djm       935:                if (channel_cancel_lport_listener(ssh, &fwd, fwd.connect_port,
1.46      millert   936:                    &options.fwd_opts) == -1)
1.30      djm       937:                        error_reason = "port not found";
                    938:        }
                    939:
1.73      markus    940:        if (error_reason != NULL)
                    941:                reply_error(reply, MUX_S_FAILURE, rid, error_reason);
                    942:        else {
                    943:                reply_ok(reply, rid);
1.41      djm       944:                free(found_fwd->listen_host);
1.46      millert   945:                free(found_fwd->listen_path);
1.41      djm       946:                free(found_fwd->connect_host);
1.46      millert   947:                free(found_fwd->connect_path);
1.30      djm       948:                found_fwd->listen_host = found_fwd->connect_host = NULL;
1.46      millert   949:                found_fwd->listen_path = found_fwd->connect_path = NULL;
1.30      djm       950:                found_fwd->listen_port = found_fwd->connect_port = 0;
                    951:        }
1.10      djm       952:  out:
1.41      djm       953:        free(fwd_desc);
1.46      millert   954:        free(listen_addr);
                    955:        free(connect_addr);
1.10      djm       956:
                    957:        return ret;
                    958: }
                    959:
                    960: static int
1.76      djm       961: mux_master_process_stdio_fwd(struct ssh *ssh, u_int rid,
1.73      markus    962:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.10      djm       963: {
                    964:        Channel *nc;
1.73      markus    965:        char *chost = NULL;
1.10      djm       966:        u_int cport, i, j;
1.73      markus    967:        int r, new_fd[2];
1.48      djm       968:        struct mux_stdio_confirm_ctx *cctx;
1.10      djm       969:
1.73      markus    970:        if ((r = sshbuf_skip_string(m)) != 0 || /* reserved */
                    971:            (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
                    972:            (r = sshbuf_get_u32(m, &cport)) != 0) {
1.41      djm       973:                free(chost);
1.10      djm       974:                error("%s: malformed message", __func__);
                    975:                return -1;
                    976:        }
                    977:
                    978:        debug2("%s: channel %d: request stdio fwd to %s:%u",
                    979:            __func__, c->self, chost, cport);
                    980:
                    981:        /* Gather fds from client */
                    982:        for(i = 0; i < 2; i++) {
                    983:                if ((new_fd[i] = mm_receive_fd(c->sock)) == -1) {
1.83    ! djm       984:                        error("%s: failed to receive fd %d from client",
1.10      djm       985:                            __func__, i);
                    986:                        for (j = 0; j < i; j++)
                    987:                                close(new_fd[j]);
1.41      djm       988:                        free(chost);
1.10      djm       989:
                    990:                        /* prepare reply */
1.73      markus    991:                        reply_error(reply, MUX_S_FAILURE, rid,
1.10      djm       992:                            "did not receive file descriptors");
                    993:                        return -1;
                    994:                }
                    995:        }
                    996:
                    997:        debug3("%s: got fds stdin %d, stdout %d", __func__,
                    998:            new_fd[0], new_fd[1]);
                    999:
                   1000:        /* XXX support multiple child sessions in future */
1.67      djm      1001:        if (c->have_remote_id) {
1.10      djm      1002:                debug2("%s: session already open", __func__);
1.73      markus   1003:                reply_error(reply, MUX_S_FAILURE, rid,
                   1004:                    "Multiple sessions not supported");
1.10      djm      1005:  cleanup:
                   1006:                close(new_fd[0]);
                   1007:                close(new_fd[1]);
1.41      djm      1008:                free(chost);
1.10      djm      1009:                return 0;
                   1010:        }
                   1011:
                   1012:        if (options.control_master == SSHCTL_MASTER_ASK ||
                   1013:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
1.23      dtucker  1014:                if (!ask_permission("Allow forward to %s:%u? ",
1.10      djm      1015:                    chost, cport)) {
                   1016:                        debug2("%s: stdio fwd refused by user", __func__);
1.73      markus   1017:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                   1018:                            "Permission denied");
1.10      djm      1019:                        goto cleanup;
                   1020:                }
                   1021:        }
                   1022:
                   1023:        /* enable nonblocking unless tty */
                   1024:        if (!isatty(new_fd[0]))
                   1025:                set_nonblock(new_fd[0]);
                   1026:        if (!isatty(new_fd[1]))
                   1027:                set_nonblock(new_fd[1]);
                   1028:
1.66      djm      1029:        nc = channel_connect_stdio_fwd(ssh, chost, cport, new_fd[0], new_fd[1]);
1.75      djm      1030:        free(chost);
1.10      djm      1031:
                   1032:        nc->ctl_chan = c->self;         /* link session -> control channel */
                   1033:        c->remote_id = nc->self;        /* link control -> session channel */
1.67      djm      1034:        c->have_remote_id = 1;
1.10      djm      1035:
                   1036:        debug2("%s: channel_new: %d linked to control channel %d",
                   1037:            __func__, nc->self, nc->ctl_chan);
                   1038:
1.66      djm      1039:        channel_register_cleanup(ssh, nc->self,
                   1040:            mux_master_session_cleanup_cb, 1);
1.10      djm      1041:
1.48      djm      1042:        cctx = xcalloc(1, sizeof(*cctx));
                   1043:        cctx->rid = rid;
1.66      djm      1044:        channel_register_open_confirm(ssh, nc->self, mux_stdio_confirm, cctx);
1.48      djm      1045:        c->mux_pause = 1; /* stop handling messages until open_confirm done */
                   1046:
                   1047:        /* reply is deferred, sent by mux_session_confirm */
                   1048:        return 0;
                   1049: }
                   1050:
                   1051: /* Callback on open confirmation in mux master for a mux stdio fwd session. */
                   1052: static void
1.66      djm      1053: mux_stdio_confirm(struct ssh *ssh, int id, int success, void *arg)
1.48      djm      1054: {
                   1055:        struct mux_stdio_confirm_ctx *cctx = arg;
                   1056:        Channel *c, *cc;
1.73      markus   1057:        struct sshbuf *reply;
                   1058:        int r;
1.48      djm      1059:
                   1060:        if (cctx == NULL)
                   1061:                fatal("%s: cctx == NULL", __func__);
1.66      djm      1062:        if ((c = channel_by_id(ssh, id)) == NULL)
1.48      djm      1063:                fatal("%s: no channel for id %d", __func__, id);
1.66      djm      1064:        if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.48      djm      1065:                fatal("%s: channel %d lacks control channel %d", __func__,
                   1066:                    id, c->ctl_chan);
1.73      markus   1067:        if ((reply = sshbuf_new()) == NULL)
                   1068:                fatal("%s: sshbuf_new", __func__);
1.48      djm      1069:
                   1070:        if (!success) {
                   1071:                debug3("%s: sending failure reply", __func__);
1.73      markus   1072:                reply_error(reply, MUX_S_FAILURE, cctx->rid,
                   1073:                    "Session open refused by peer");
1.48      djm      1074:                /* prepare reply */
                   1075:                goto done;
                   1076:        }
                   1077:
                   1078:        debug3("%s: sending success reply", __func__);
1.10      djm      1079:        /* prepare reply */
1.73      markus   1080:        if ((r = sshbuf_put_u32(reply, MUX_S_SESSION_OPENED)) != 0 ||
                   1081:            (r = sshbuf_put_u32(reply, cctx->rid)) != 0 ||
                   1082:            (r = sshbuf_put_u32(reply, c->self)) != 0)
                   1083:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.48      djm      1084:
                   1085:  done:
                   1086:        /* Send reply */
1.73      markus   1087:        if ((r = sshbuf_put_stringb(cc->output, reply)) != 0)
                   1088:                fatal("%s: sshbuf_put_stringb: %s", __func__, ssh_err(r));
                   1089:        sshbuf_free(reply);
1.10      djm      1090:
1.48      djm      1091:        if (cc->mux_pause <= 0)
                   1092:                fatal("%s: mux_pause %d", __func__, cc->mux_pause);
                   1093:        cc->mux_pause = 0; /* start processing messages again */
                   1094:        c->open_confirm_ctx = NULL;
                   1095:        free(cctx);
1.10      djm      1096: }
                   1097:
1.25      djm      1098: static int
1.76      djm      1099: mux_master_process_stop_listening(struct ssh *ssh, u_int rid,
1.73      markus   1100:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.25      djm      1101: {
                   1102:        debug("%s: channel %d: stop listening", __func__, c->self);
                   1103:
                   1104:        if (options.control_master == SSHCTL_MASTER_ASK ||
                   1105:            options.control_master == SSHCTL_MASTER_AUTO_ASK) {
                   1106:                if (!ask_permission("Disable further multiplexing on shared "
                   1107:                    "connection to %s? ", host)) {
                   1108:                        debug2("%s: stop listen refused by user", __func__);
1.73      markus   1109:                        reply_error(reply, MUX_S_PERMISSION_DENIED, rid,
                   1110:                            "Permission denied");
1.25      djm      1111:                        return 0;
                   1112:                }
                   1113:        }
                   1114:
                   1115:        if (mux_listener_channel != NULL) {
1.66      djm      1116:                channel_free(ssh, mux_listener_channel);
1.25      djm      1117:                client_stop_mux();
1.41      djm      1118:                free(options.control_path);
1.25      djm      1119:                options.control_path = NULL;
                   1120:                mux_listener_channel = NULL;
                   1121:                muxserver_sock = -1;
                   1122:        }
                   1123:
1.73      markus   1124:        reply_ok(reply, rid);
1.25      djm      1125:        return 0;
                   1126: }
                   1127:
1.62      markus   1128: static int
1.76      djm      1129: mux_master_process_proxy(struct ssh *ssh, u_int rid,
1.73      markus   1130:     Channel *c, struct sshbuf *m, struct sshbuf *reply)
1.62      markus   1131: {
1.73      markus   1132:        int r;
                   1133:
1.62      markus   1134:        debug("%s: channel %d: proxy request", __func__, c->self);
                   1135:
                   1136:        c->mux_rcb = channel_proxy_downstream;
1.73      markus   1137:        if ((r = sshbuf_put_u32(reply, MUX_S_PROXY)) != 0 ||
                   1138:            (r = sshbuf_put_u32(reply, rid)) != 0)
                   1139:                fatal("%s: reply: %s", __func__, ssh_err(r));
1.62      markus   1140:
                   1141:        return 0;
                   1142: }
                   1143:
1.83    ! djm      1144: /* Channel callbacks fired on read/write from mux client fd */
1.10      djm      1145: static int
1.66      djm      1146: mux_master_read_cb(struct ssh *ssh, Channel *c)
1.10      djm      1147: {
                   1148:        struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
1.73      markus   1149:        struct sshbuf *in = NULL, *out = NULL;
                   1150:        u_int type, rid, i;
                   1151:        int r, ret = -1;
                   1152:
                   1153:        if ((out = sshbuf_new()) == NULL)
                   1154:                fatal("%s: sshbuf_new", __func__);
1.10      djm      1155:
                   1156:        /* Setup ctx and  */
                   1157:        if (c->mux_ctx == NULL) {
1.19      djm      1158:                state = xcalloc(1, sizeof(*state));
1.10      djm      1159:                c->mux_ctx = state;
1.66      djm      1160:                channel_register_cleanup(ssh, c->self,
1.10      djm      1161:                    mux_master_control_cleanup_cb, 0);
                   1162:
                   1163:                /* Send hello */
1.73      markus   1164:                if ((r = sshbuf_put_u32(out, MUX_MSG_HELLO)) != 0 ||
                   1165:                    (r = sshbuf_put_u32(out, SSHMUX_VER)) != 0)
                   1166:                        fatal("%s: reply: %s", __func__, ssh_err(r));
1.10      djm      1167:                /* no extensions */
1.73      markus   1168:                if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                   1169:                        fatal("%s: sshbuf_put_stringb: %s",
                   1170:                            __func__, ssh_err(r));
1.10      djm      1171:                debug3("%s: channel %d: hello sent", __func__, c->self);
1.73      markus   1172:                ret = 0;
                   1173:                goto out;
1.10      djm      1174:        }
                   1175:
                   1176:        /* Channel code ensures that we receive whole packets */
1.73      markus   1177:        if ((r = sshbuf_froms(c->input, &in)) != 0) {
1.10      djm      1178:  malf:
                   1179:                error("%s: malformed message", __func__);
                   1180:                goto out;
                   1181:        }
                   1182:
1.73      markus   1183:        if ((r = sshbuf_get_u32(in, &type)) != 0)
1.10      djm      1184:                goto malf;
1.73      markus   1185:        debug3("%s: channel %d packet type 0x%08x len %zu",
                   1186:            __func__, c->self, type, sshbuf_len(in));
1.10      djm      1187:
                   1188:        if (type == MUX_MSG_HELLO)
                   1189:                rid = 0;
                   1190:        else {
                   1191:                if (!state->hello_rcvd) {
                   1192:                        error("%s: expected MUX_MSG_HELLO(0x%08x), "
                   1193:                            "received 0x%08x", __func__, MUX_MSG_HELLO, type);
                   1194:                        goto out;
1.1       djm      1195:                }
1.73      markus   1196:                if ((r = sshbuf_get_u32(in, &rid)) != 0)
1.10      djm      1197:                        goto malf;
                   1198:        }
                   1199:
                   1200:        for (i = 0; mux_master_handlers[i].handler != NULL; i++) {
                   1201:                if (type == mux_master_handlers[i].type) {
1.66      djm      1202:                        ret = mux_master_handlers[i].handler(ssh, rid,
1.73      markus   1203:                            c, in, out);
1.10      djm      1204:                        break;
1.1       djm      1205:                }
1.10      djm      1206:        }
                   1207:        if (mux_master_handlers[i].handler == NULL) {
                   1208:                error("%s: unsupported mux message 0x%08x", __func__, type);
1.73      markus   1209:                reply_error(out, MUX_S_FAILURE, rid, "unsupported request");
1.10      djm      1210:                ret = 0;
                   1211:        }
                   1212:        /* Enqueue reply packet */
1.73      markus   1213:        if (sshbuf_len(out) != 0) {
                   1214:                if ((r = sshbuf_put_stringb(c->output, out)) != 0)
                   1215:                        fatal("%s: sshbuf_put_stringb: %s",
                   1216:                            __func__, ssh_err(r));
1.10      djm      1217:        }
                   1218:  out:
1.73      markus   1219:        sshbuf_free(in);
                   1220:        sshbuf_free(out);
1.10      djm      1221:        return ret;
                   1222: }
                   1223:
                   1224: void
1.66      djm      1225: mux_exit_message(struct ssh *ssh, Channel *c, int exitval)
1.10      djm      1226: {
1.73      markus   1227:        struct sshbuf *m;
1.10      djm      1228:        Channel *mux_chan;
1.73      markus   1229:        int r;
1.10      djm      1230:
1.40      dtucker  1231:        debug3("%s: channel %d: exit message, exitval %d", __func__, c->self,
1.10      djm      1232:            exitval);
                   1233:
1.66      djm      1234:        if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.10      djm      1235:                fatal("%s: channel %d missing mux channel %d",
                   1236:                    __func__, c->self, c->ctl_chan);
                   1237:
                   1238:        /* Append exit message packet to control socket output queue */
1.73      markus   1239:        if ((m = sshbuf_new()) == NULL)
                   1240:                fatal("%s: sshbuf_new", __func__);
                   1241:        if ((r = sshbuf_put_u32(m, MUX_S_EXIT_MESSAGE)) != 0 ||
                   1242:            (r = sshbuf_put_u32(m, c->self)) != 0 ||
                   1243:            (r = sshbuf_put_u32(m, exitval)) != 0 ||
                   1244:            (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
                   1245:                fatal("%s: reply: %s", __func__, ssh_err(r));
                   1246:        sshbuf_free(m);
1.10      djm      1247: }
                   1248:
1.28      djm      1249: void
1.66      djm      1250: mux_tty_alloc_failed(struct ssh *ssh, Channel *c)
1.28      djm      1251: {
1.73      markus   1252:        struct sshbuf *m;
1.28      djm      1253:        Channel *mux_chan;
1.73      markus   1254:        int r;
1.28      djm      1255:
                   1256:        debug3("%s: channel %d: TTY alloc failed", __func__, c->self);
                   1257:
1.66      djm      1258:        if ((mux_chan = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.28      djm      1259:                fatal("%s: channel %d missing mux channel %d",
                   1260:                    __func__, c->self, c->ctl_chan);
                   1261:
                   1262:        /* Append exit message packet to control socket output queue */
1.73      markus   1263:        if ((m = sshbuf_new()) == NULL)
                   1264:                fatal("%s: sshbuf_new", __func__);
                   1265:        if ((r = sshbuf_put_u32(m, MUX_S_TTY_ALLOC_FAIL)) != 0 ||
                   1266:            (r = sshbuf_put_u32(m, c->self)) != 0 ||
                   1267:            (r = sshbuf_put_stringb(mux_chan->output, m)) != 0)
                   1268:                fatal("%s: reply: %s", __func__, ssh_err(r));
                   1269:        sshbuf_free(m);
1.28      djm      1270: }
                   1271:
1.10      djm      1272: /* Prepare a mux master to listen on a Unix domain socket. */
                   1273: void
1.66      djm      1274: muxserver_listen(struct ssh *ssh)
1.10      djm      1275: {
                   1276:        mode_t old_umask;
1.22      djm      1277:        char *orig_control_path = options.control_path;
                   1278:        char rbuf[16+1];
                   1279:        u_int i, r;
1.47      djm      1280:        int oerrno;
1.10      djm      1281:
                   1282:        if (options.control_path == NULL ||
                   1283:            options.control_master == SSHCTL_MASTER_NO)
1.1       djm      1284:                return;
1.10      djm      1285:
                   1286:        debug("setting up multiplex master socket");
                   1287:
1.22      djm      1288:        /*
                   1289:         * Use a temporary path before listen so we can pseudo-atomically
                   1290:         * establish the listening socket in its final location to avoid
                   1291:         * other processes racing in between bind() and listen() and hitting
                   1292:         * an unready socket.
                   1293:         */
                   1294:        for (i = 0; i < sizeof(rbuf) - 1; i++) {
                   1295:                r = arc4random_uniform(26+26+10);
                   1296:                rbuf[i] = (r < 26) ? 'a' + r :
                   1297:                    (r < 26*2) ? 'A' + r - 26 :
                   1298:                    '0' + r - 26 - 26;
                   1299:        }
                   1300:        rbuf[sizeof(rbuf) - 1] = '\0';
                   1301:        options.control_path = NULL;
                   1302:        xasprintf(&options.control_path, "%s.%s", orig_control_path, rbuf);
                   1303:        debug3("%s: temporary control path %s", __func__, options.control_path);
                   1304:
1.10      djm      1305:        old_umask = umask(0177);
1.46      millert  1306:        muxserver_sock = unix_listener(options.control_path, 64, 0);
1.47      djm      1307:        oerrno = errno;
1.46      millert  1308:        umask(old_umask);
                   1309:        if (muxserver_sock < 0) {
1.47      djm      1310:                if (oerrno == EINVAL || oerrno == EADDRINUSE) {
1.10      djm      1311:                        error("ControlSocket %s already exists, "
                   1312:                            "disabling multiplexing", options.control_path);
1.22      djm      1313:  disable_mux_master:
1.26      djm      1314:                        if (muxserver_sock != -1) {
                   1315:                                close(muxserver_sock);
                   1316:                                muxserver_sock = -1;
                   1317:                        }
1.41      djm      1318:                        free(orig_control_path);
                   1319:                        free(options.control_path);
1.10      djm      1320:                        options.control_path = NULL;
                   1321:                        options.control_master = SSHCTL_MASTER_NO;
                   1322:                        return;
1.46      millert  1323:                } else {
                   1324:                        /* unix_listener() logs the error */
                   1325:                        cleanup_exit(255);
                   1326:                }
1.10      djm      1327:        }
                   1328:
1.22      djm      1329:        /* Now atomically "move" the mux socket into position */
                   1330:        if (link(options.control_path, orig_control_path) != 0) {
                   1331:                if (errno != EEXIST) {
1.59      djm      1332:                        fatal("%s: link mux listener %s => %s: %s", __func__,
1.22      djm      1333:                            options.control_path, orig_control_path,
                   1334:                            strerror(errno));
                   1335:                }
                   1336:                error("ControlSocket %s already exists, disabling multiplexing",
                   1337:                    orig_control_path);
                   1338:                unlink(options.control_path);
                   1339:                goto disable_mux_master;
                   1340:        }
                   1341:        unlink(options.control_path);
1.41      djm      1342:        free(options.control_path);
1.22      djm      1343:        options.control_path = orig_control_path;
                   1344:
1.10      djm      1345:        set_nonblock(muxserver_sock);
                   1346:
1.66      djm      1347:        mux_listener_channel = channel_new(ssh, "mux listener",
1.10      djm      1348:            SSH_CHANNEL_MUX_LISTENER, muxserver_sock, muxserver_sock, -1,
                   1349:            CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.22      djm      1350:            0, options.control_path, 1);
1.10      djm      1351:        mux_listener_channel->mux_rcb = mux_master_read_cb;
                   1352:        debug3("%s: mux listener channel %d fd %d", __func__,
                   1353:            mux_listener_channel->self, mux_listener_channel->sock);
                   1354: }
                   1355:
                   1356: /* Callback on open confirmation in mux master for a mux client session. */
                   1357: static void
1.66      djm      1358: mux_session_confirm(struct ssh *ssh, int id, int success, void *arg)
1.10      djm      1359: {
                   1360:        struct mux_session_confirm_ctx *cctx = arg;
                   1361:        const char *display;
1.17      djm      1362:        Channel *c, *cc;
1.73      markus   1363:        int i, r;
                   1364:        struct sshbuf *reply;
1.10      djm      1365:
                   1366:        if (cctx == NULL)
                   1367:                fatal("%s: cctx == NULL", __func__);
1.66      djm      1368:        if ((c = channel_by_id(ssh, id)) == NULL)
1.10      djm      1369:                fatal("%s: no channel for id %d", __func__, id);
1.66      djm      1370:        if ((cc = channel_by_id(ssh, c->ctl_chan)) == NULL)
1.17      djm      1371:                fatal("%s: channel %d lacks control channel %d", __func__,
                   1372:                    id, c->ctl_chan);
1.73      markus   1373:        if ((reply = sshbuf_new()) == NULL)
                   1374:                fatal("%s: sshbuf_new", __func__);
1.17      djm      1375:
                   1376:        if (!success) {
                   1377:                debug3("%s: sending failure reply", __func__);
1.73      markus   1378:                reply_error(reply, MUX_S_FAILURE, cctx->rid,
                   1379:                    "Session open refused by peer");
1.17      djm      1380:                goto done;
                   1381:        }
1.10      djm      1382:
                   1383:        display = getenv("DISPLAY");
                   1384:        if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
                   1385:                char *proto, *data;
1.21      djm      1386:
1.10      djm      1387:                /* Get reasonable local authentication information. */
1.66      djm      1388:                if (client_x11_get_proto(ssh, display, options.xauth_location,
1.21      djm      1389:                    options.forward_x11_trusted, options.forward_x11_timeout,
1.58      djm      1390:                    &proto, &data) == 0) {
                   1391:                        /* Request forwarding with authentication spoofing. */
                   1392:                        debug("Requesting X11 forwarding with authentication "
                   1393:                            "spoofing.");
1.66      djm      1394:                        x11_request_forwarding_with_spoofing(ssh, id,
                   1395:                            display, proto, data, 1);
1.58      djm      1396:                        /* XXX exit_on_forward_failure */
1.66      djm      1397:                        client_expect_confirm(ssh, id, "X11 forwarding",
1.58      djm      1398:                            CONFIRM_WARN);
                   1399:                }
1.10      djm      1400:        }
                   1401:
                   1402:        if (cctx->want_agent_fwd && options.forward_agent) {
                   1403:                debug("Requesting authentication agent forwarding.");
1.66      djm      1404:                channel_request_start(ssh, id, "auth-agent-req@openssh.com", 0);
1.79      djm      1405:                if ((r = sshpkt_send(ssh)) != 0)
                   1406:                        fatal("%s: packet error: %s", __func__, ssh_err(r));
1.10      djm      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);
1.80      deraadt  1482:                if (len == -1) {
1.10      djm      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);
1.80      deraadt  1528:                if (len == -1) {
1.10      djm      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.81      dtucker  1895:        ssh_signal(SIGPIPE, SIG_IGN);
1.10      djm      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.82      markus   1971:                debug("%s: master session id: %u", __func__, sid);
1.10      djm      1972:                break;
                   1973:        case MUX_S_PERMISSION_DENIED:
1.73      markus   1974:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1975:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.24      djm      1976:                error("Master refused session request: %s", e);
1.73      markus   1977:                sshbuf_free(m);
1.10      djm      1978:                return -1;
                   1979:        case MUX_S_FAILURE:
1.73      markus   1980:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   1981:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.24      djm      1982:                error("%s: session request failed: %s", __func__, e);
1.73      markus   1983:                sshbuf_free(m);
1.10      djm      1984:                return -1;
                   1985:        default:
1.73      markus   1986:                sshbuf_free(m);
1.10      djm      1987:                error("%s: unexpected response from master 0x%08x",
                   1988:                    __func__, type);
                   1989:                return -1;
                   1990:        }
                   1991:        muxclient_request_id++;
1.1       djm      1992:
1.57      semarie  1993:        if (pledge("stdio proc tty", NULL) == -1)
                   1994:                fatal("%s pledge(): %s", __func__, strerror(errno));
                   1995:
1.81      dtucker  1996:        ssh_signal(SIGHUP, control_client_sighandler);
                   1997:        ssh_signal(SIGINT, control_client_sighandler);
                   1998:        ssh_signal(SIGTERM, control_client_sighandler);
                   1999:        ssh_signal(SIGWINCH, control_client_sigrelay);
1.1       djm      2000:
1.28      djm      2001:        rawmode = tty_flag;
1.1       djm      2002:        if (tty_flag)
1.27      djm      2003:                enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.1       djm      2004:
                   2005:        /*
                   2006:         * Stick around until the controlee closes the client_fd.
1.10      djm      2007:         * Before it does, it is expected to write an exit message.
                   2008:         * This process must read the value and wait for the closure of
                   2009:         * the client_fd; if this one closes early, the multiplex master will
                   2010:         * terminate early too (possibly losing data).
1.1       djm      2011:         */
1.10      djm      2012:        for (exitval = 255, exitval_seen = 0;;) {
1.73      markus   2013:                sshbuf_reset(m);
                   2014:                if (mux_client_read_packet(fd, m) != 0)
1.1       djm      2015:                        break;
1.73      markus   2016:                if ((r = sshbuf_get_u32(m, &type)) != 0)
                   2017:                        fatal("%s: decode type: %s", __func__, ssh_err(r));
1.28      djm      2018:                switch (type) {
                   2019:                case MUX_S_TTY_ALLOC_FAIL:
1.73      markus   2020:                        if ((r = sshbuf_get_u32(m, &esid)) != 0)
                   2021:                                fatal("%s: decode ID: %s",
                   2022:                                    __func__, ssh_err(r));
                   2023:                        if (esid != sid)
1.28      djm      2024:                                fatal("%s: tty alloc fail on unknown session: "
                   2025:                                    "my id %u theirs %u",
                   2026:                                    __func__, sid, esid);
                   2027:                        leave_raw_mode(options.request_tty ==
                   2028:                            REQUEST_TTY_FORCE);
                   2029:                        rawmode = 0;
                   2030:                        continue;
                   2031:                case MUX_S_EXIT_MESSAGE:
1.73      markus   2032:                        if ((r = sshbuf_get_u32(m, &esid)) != 0)
                   2033:                                fatal("%s: decode ID: %s",
                   2034:                                    __func__, ssh_err(r));
                   2035:                        if (esid != sid)
1.28      djm      2036:                                fatal("%s: exit on unknown session: "
                   2037:                                    "my id %u theirs %u",
                   2038:                                    __func__, sid, esid);
                   2039:                        if (exitval_seen)
                   2040:                                fatal("%s: exitval sent twice", __func__);
1.73      markus   2041:                        if ((r = sshbuf_get_u32(m, &exitval)) != 0)
                   2042:                                fatal("%s: decode exit value: %s",
                   2043:                                    __func__, ssh_err(r));
1.28      djm      2044:                        exitval_seen = 1;
                   2045:                        continue;
                   2046:                default:
1.73      markus   2047:                        if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2048:                                fatal("%s: decode error: %s",
                   2049:                                    __func__, ssh_err(r));
1.10      djm      2050:                        fatal("%s: master returned error: %s", __func__, e);
1.1       djm      2051:                }
                   2052:        }
                   2053:
1.10      djm      2054:        close(fd);
1.28      djm      2055:        if (rawmode)
                   2056:                leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.10      djm      2057:
1.1       djm      2058:        if (muxclient_terminate) {
1.69      dtucker  2059:                debug2("Exiting on signal: %s", strsignal(muxclient_terminate));
1.10      djm      2060:                exitval = 255;
                   2061:        } else if (!exitval_seen) {
1.1       djm      2062:                debug2("Control master terminated unexpectedly");
1.10      djm      2063:                exitval = 255;
1.1       djm      2064:        } else
1.10      djm      2065:                debug2("Received exit status from master %d", exitval);
1.1       djm      2066:
                   2067:        if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
                   2068:                fprintf(stderr, "Shared connection to %s closed.\r\n", host);
                   2069:
1.10      djm      2070:        exit(exitval);
                   2071: }
                   2072:
                   2073: static int
1.62      markus   2074: mux_client_proxy(int fd)
                   2075: {
1.73      markus   2076:        struct sshbuf *m;
1.62      markus   2077:        char *e;
                   2078:        u_int type, rid;
1.73      markus   2079:        int r;
1.62      markus   2080:
1.73      markus   2081:        if ((m = sshbuf_new()) == NULL)
                   2082:                fatal("%s: sshbuf_new", __func__);
                   2083:        if ((r = sshbuf_put_u32(m, MUX_C_PROXY)) != 0 ||
                   2084:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   2085:                fatal("%s: request: %s", __func__, ssh_err(r));
                   2086:        if (mux_client_write_packet(fd, m) != 0)
1.62      markus   2087:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2088:
1.73      markus   2089:        sshbuf_reset(m);
1.62      markus   2090:
                   2091:        /* Read their reply */
1.73      markus   2092:        if (mux_client_read_packet(fd, m) != 0) {
                   2093:                sshbuf_free(m);
1.62      markus   2094:                return 0;
                   2095:        }
1.73      markus   2096:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2097:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2098:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2099:        if (rid != muxclient_request_id)
                   2100:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2101:                    __func__, muxclient_request_id, rid);
1.62      markus   2102:        if (type != MUX_S_PROXY) {
1.73      markus   2103:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2104:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.62      markus   2105:                fatal("%s: master returned error: %s", __func__, e);
                   2106:        }
1.73      markus   2107:        sshbuf_free(m);
1.62      markus   2108:
                   2109:        debug3("%s: done", __func__);
                   2110:        muxclient_request_id++;
                   2111:        return 0;
                   2112: }
                   2113:
                   2114: static int
1.10      djm      2115: mux_client_request_stdio_fwd(int fd)
                   2116: {
1.73      markus   2117:        struct sshbuf *m;
1.10      djm      2118:        char *e;
                   2119:        u_int type, rid, sid;
1.73      markus   2120:        int r, devnull;
1.10      djm      2121:
                   2122:        debug3("%s: entering", __func__);
                   2123:
                   2124:        if ((muxserver_pid = mux_client_request_alive(fd)) == 0) {
                   2125:                error("%s: master alive request failed", __func__);
                   2126:                return -1;
                   2127:        }
                   2128:
1.81      dtucker  2129:        ssh_signal(SIGPIPE, SIG_IGN);
1.10      djm      2130:
                   2131:        if (stdin_null_flag) {
                   2132:                if ((devnull = open(_PATH_DEVNULL, O_RDONLY)) == -1)
                   2133:                        fatal("open(/dev/null): %s", strerror(errno));
                   2134:                if (dup2(devnull, STDIN_FILENO) == -1)
                   2135:                        fatal("dup2: %s", strerror(errno));
                   2136:                if (devnull > STDERR_FILENO)
                   2137:                        close(devnull);
                   2138:        }
                   2139:
1.73      markus   2140:        if ((m = sshbuf_new()) == NULL)
                   2141:                fatal("%s: sshbuf_new", __func__);
                   2142:        if ((r = sshbuf_put_u32(m, MUX_C_NEW_STDIO_FWD)) != 0 ||
                   2143:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0 ||
                   2144:            (r = sshbuf_put_string(m, NULL, 0)) != 0 || /* reserved */
                   2145:            (r = sshbuf_put_cstring(m, options.stdio_forward_host)) != 0 ||
                   2146:            (r = sshbuf_put_u32(m, options.stdio_forward_port)) != 0)
                   2147:                fatal("%s: request: %s", __func__, ssh_err(r));
1.10      djm      2148:
1.73      markus   2149:        if (mux_client_write_packet(fd, m) != 0)
1.10      djm      2150:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2151:
                   2152:        /* Send the stdio file descriptors */
                   2153:        if (mm_send_fd(fd, STDIN_FILENO) == -1 ||
                   2154:            mm_send_fd(fd, STDOUT_FILENO) == -1)
                   2155:                fatal("%s: send fds failed", __func__);
                   2156:
1.56      semarie  2157:        if (pledge("stdio proc tty", NULL) == -1)
                   2158:                fatal("%s pledge(): %s", __func__, strerror(errno));
                   2159:
1.10      djm      2160:        debug3("%s: stdio forward request sent", __func__);
                   2161:
                   2162:        /* Read their reply */
1.73      markus   2163:        sshbuf_reset(m);
1.10      djm      2164:
1.73      markus   2165:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      2166:                error("%s: read from master failed: %s",
                   2167:                    __func__, strerror(errno));
1.73      markus   2168:                sshbuf_free(m);
1.10      djm      2169:                return -1;
                   2170:        }
                   2171:
1.73      markus   2172:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2173:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2174:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2175:        if (rid != muxclient_request_id)
1.10      djm      2176:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2177:                    __func__, muxclient_request_id, rid);
                   2178:        switch (type) {
                   2179:        case MUX_S_SESSION_OPENED:
1.73      markus   2180:                if ((r = sshbuf_get_u32(m, &sid)) != 0)
                   2181:                        fatal("%s: decode ID: %s", __func__, ssh_err(r));
1.10      djm      2182:                debug("%s: master session id: %u", __func__, sid);
                   2183:                break;
                   2184:        case MUX_S_PERMISSION_DENIED:
1.73      markus   2185:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2186:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   2187:                sshbuf_free(m);
1.24      djm      2188:                fatal("Master refused stdio forwarding request: %s", e);
1.10      djm      2189:        case MUX_S_FAILURE:
1.73      markus   2190:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2191:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
                   2192:                sshbuf_free(m);
1.48      djm      2193:                fatal("Stdio forwarding request failed: %s", e);
1.10      djm      2194:        default:
1.73      markus   2195:                sshbuf_free(m);
1.10      djm      2196:                error("%s: unexpected response from master 0x%08x",
                   2197:                    __func__, type);
                   2198:                return -1;
                   2199:        }
                   2200:        muxclient_request_id++;
                   2201:
1.81      dtucker  2202:        ssh_signal(SIGHUP, control_client_sighandler);
                   2203:        ssh_signal(SIGINT, control_client_sighandler);
                   2204:        ssh_signal(SIGTERM, control_client_sighandler);
                   2205:        ssh_signal(SIGWINCH, control_client_sigrelay);
1.10      djm      2206:
                   2207:        /*
                   2208:         * Stick around until the controlee closes the client_fd.
                   2209:         */
1.73      markus   2210:        sshbuf_reset(m);
                   2211:        if (mux_client_read_packet(fd, m) != 0) {
1.10      djm      2212:                if (errno == EPIPE ||
                   2213:                    (errno == EINTR && muxclient_terminate != 0))
                   2214:                        return 0;
                   2215:                fatal("%s: mux_client_read_packet: %s",
                   2216:                    __func__, strerror(errno));
                   2217:        }
                   2218:        fatal("%s: master returned unexpected message %u", __func__, type);
                   2219: }
                   2220:
1.25      djm      2221: static void
                   2222: mux_client_request_stop_listening(int fd)
                   2223: {
1.73      markus   2224:        struct sshbuf *m;
1.25      djm      2225:        char *e;
                   2226:        u_int type, rid;
1.73      markus   2227:        int r;
1.25      djm      2228:
                   2229:        debug3("%s: entering", __func__);
                   2230:
1.73      markus   2231:        if ((m = sshbuf_new()) == NULL)
                   2232:                fatal("%s: sshbuf_new", __func__);
                   2233:        if ((r = sshbuf_put_u32(m, MUX_C_STOP_LISTENING)) != 0 ||
                   2234:            (r = sshbuf_put_u32(m, muxclient_request_id)) != 0)
                   2235:                fatal("%s: request: %s", __func__, ssh_err(r));
1.25      djm      2236:
1.73      markus   2237:        if (mux_client_write_packet(fd, m) != 0)
1.25      djm      2238:                fatal("%s: write packet: %s", __func__, strerror(errno));
                   2239:
1.73      markus   2240:        sshbuf_reset(m);
1.25      djm      2241:
                   2242:        /* Read their reply */
1.73      markus   2243:        if (mux_client_read_packet(fd, m) != 0)
1.25      djm      2244:                fatal("%s: read from master failed: %s",
                   2245:                    __func__, strerror(errno));
                   2246:
1.73      markus   2247:        if ((r = sshbuf_get_u32(m, &type)) != 0 ||
                   2248:            (r = sshbuf_get_u32(m, &rid)) != 0)
                   2249:                fatal("%s: decode: %s", __func__, ssh_err(r));
                   2250:        if (rid != muxclient_request_id)
1.25      djm      2251:                fatal("%s: out of sequence reply: my id %u theirs %u",
                   2252:                    __func__, muxclient_request_id, rid);
1.73      markus   2253:
1.25      djm      2254:        switch (type) {
                   2255:        case MUX_S_OK:
                   2256:                break;
                   2257:        case MUX_S_PERMISSION_DENIED:
1.73      markus   2258:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2259:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.25      djm      2260:                fatal("Master refused stop listening request: %s", e);
                   2261:        case MUX_S_FAILURE:
1.73      markus   2262:                if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
                   2263:                        fatal("%s: decode error: %s", __func__, ssh_err(r));
1.25      djm      2264:                fatal("%s: stop listening request failed: %s", __func__, e);
                   2265:        default:
                   2266:                fatal("%s: unexpected response from master 0x%08x",
                   2267:                    __func__, type);
                   2268:        }
1.73      markus   2269:        sshbuf_free(m);
1.25      djm      2270:        muxclient_request_id++;
                   2271: }
                   2272:
1.10      djm      2273: /* Multiplex client main loop. */
1.62      markus   2274: int
1.10      djm      2275: muxclient(const char *path)
                   2276: {
                   2277:        struct sockaddr_un addr;
                   2278:        int sock;
                   2279:        u_int pid;
                   2280:
                   2281:        if (muxclient_command == 0) {
1.60      dtucker  2282:                if (options.stdio_forward_host != NULL)
1.10      djm      2283:                        muxclient_command = SSHMUX_COMMAND_STDIO_FWD;
                   2284:                else
                   2285:                        muxclient_command = SSHMUX_COMMAND_OPEN;
                   2286:        }
                   2287:
                   2288:        switch (options.control_master) {
                   2289:        case SSHCTL_MASTER_AUTO:
                   2290:        case SSHCTL_MASTER_AUTO_ASK:
                   2291:                debug("auto-mux: Trying existing master");
                   2292:                /* FALLTHROUGH */
                   2293:        case SSHCTL_MASTER_NO:
                   2294:                break;
                   2295:        default:
1.62      markus   2296:                return -1;
1.10      djm      2297:        }
                   2298:
                   2299:        memset(&addr, '\0', sizeof(addr));
                   2300:        addr.sun_family = AF_UNIX;
                   2301:
                   2302:        if (strlcpy(addr.sun_path, path,
                   2303:            sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1.61      dtucker  2304:                fatal("ControlPath too long ('%s' >= %u bytes)", path,
                   2305:                     (unsigned int)sizeof(addr.sun_path));
1.10      djm      2306:
1.80      deraadt  2307:        if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
1.10      djm      2308:                fatal("%s socket(): %s", __func__, strerror(errno));
                   2309:
1.64      guenther 2310:        if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
1.10      djm      2311:                switch (muxclient_command) {
                   2312:                case SSHMUX_COMMAND_OPEN:
                   2313:                case SSHMUX_COMMAND_STDIO_FWD:
                   2314:                        break;
                   2315:                default:
                   2316:                        fatal("Control socket connect(%.100s): %s", path,
                   2317:                            strerror(errno));
                   2318:                }
1.22      djm      2319:                if (errno == ECONNREFUSED &&
                   2320:                    options.control_master != SSHCTL_MASTER_NO) {
                   2321:                        debug("Stale control socket %.100s, unlinking", path);
                   2322:                        unlink(path);
                   2323:                } else if (errno == ENOENT) {
1.10      djm      2324:                        debug("Control socket \"%.100s\" does not exist", path);
1.22      djm      2325:                } else {
1.10      djm      2326:                        error("Control socket connect(%.100s): %s", path,
                   2327:                            strerror(errno));
                   2328:                }
                   2329:                close(sock);
1.62      markus   2330:                return -1;
1.10      djm      2331:        }
                   2332:        set_nonblock(sock);
                   2333:
                   2334:        if (mux_client_hello_exchange(sock) != 0) {
                   2335:                error("%s: master hello exchange failed", __func__);
                   2336:                close(sock);
1.62      markus   2337:                return -1;
1.10      djm      2338:        }
                   2339:
                   2340:        switch (muxclient_command) {
                   2341:        case SSHMUX_COMMAND_ALIVE_CHECK:
                   2342:                if ((pid = mux_client_request_alive(sock)) == 0)
                   2343:                        fatal("%s: master alive check failed", __func__);
1.55      djm      2344:                fprintf(stderr, "Master running (pid=%u)\r\n", pid);
1.10      djm      2345:                exit(0);
                   2346:        case SSHMUX_COMMAND_TERMINATE:
                   2347:                mux_client_request_terminate(sock);
1.63      dtucker  2348:                if (options.log_level != SYSLOG_LEVEL_QUIET)
                   2349:                        fprintf(stderr, "Exit request sent.\r\n");
1.18      markus   2350:                exit(0);
                   2351:        case SSHMUX_COMMAND_FORWARD:
1.30      djm      2352:                if (mux_client_forwards(sock, 0) != 0)
1.18      markus   2353:                        fatal("%s: master forward request failed", __func__);
1.10      djm      2354:                exit(0);
                   2355:        case SSHMUX_COMMAND_OPEN:
1.30      djm      2356:                if (mux_client_forwards(sock, 0) != 0) {
1.10      djm      2357:                        error("%s: master forward request failed", __func__);
1.62      markus   2358:                        return -1;
1.10      djm      2359:                }
                   2360:                mux_client_request_session(sock);
1.62      markus   2361:                return -1;
1.10      djm      2362:        case SSHMUX_COMMAND_STDIO_FWD:
                   2363:                mux_client_request_stdio_fwd(sock);
1.25      djm      2364:                exit(0);
                   2365:        case SSHMUX_COMMAND_STOP:
                   2366:                mux_client_request_stop_listening(sock);
1.63      dtucker  2367:                if (options.log_level != SYSLOG_LEVEL_QUIET)
                   2368:                        fprintf(stderr, "Stop listening request sent.\r\n");
1.30      djm      2369:                exit(0);
                   2370:        case SSHMUX_COMMAND_CANCEL_FWD:
                   2371:                if (mux_client_forwards(sock, 1) != 0)
                   2372:                        error("%s: master cancel forward request failed",
                   2373:                            __func__);
1.10      djm      2374:                exit(0);
1.62      markus   2375:        case SSHMUX_COMMAND_PROXY:
                   2376:                mux_client_proxy(sock);
                   2377:                return (sock);
1.10      djm      2378:        default:
                   2379:                fatal("unrecognised muxclient_command %d", muxclient_command);
                   2380:        }
1.1       djm      2381: }