Deleted Added
full compact
mux.c (204917) mux.c (215116)
1/* $OpenBSD: mux.c,v 1.14 2010/01/30 02:54:53 djm Exp $ */
1/* $OpenBSD: mux.c,v 1.21 2010/06/25 23:15:36 djm Exp $ */
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

--- 56 unchanged lines hidden (view full) ---

66#ifdef HAVE_LIBUTIL_H
67# include <libutil.h>
68#endif
69
70#include "openbsd-compat/sys-queue.h"
71#include "xmalloc.h"
72#include "log.h"
73#include "ssh.h"
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

--- 56 unchanged lines hidden (view full) ---

66#ifdef HAVE_LIBUTIL_H
67# include <libutil.h>
68#endif
69
70#include "openbsd-compat/sys-queue.h"
71#include "xmalloc.h"
72#include "log.h"
73#include "ssh.h"
74#include "ssh2.h"
74#include "pathnames.h"
75#include "misc.h"
76#include "match.h"
77#include "buffer.h"
78#include "channels.h"
79#include "msg.h"
80#include "packet.h"
81#include "monitor_fdpass.h"

--- 19 unchanged lines hidden (view full) ---

101 u_int want_tty;
102 u_int want_subsys;
103 u_int want_x_fwd;
104 u_int want_agent_fwd;
105 Buffer cmd;
106 char *term;
107 struct termios tio;
108 char **env;
75#include "pathnames.h"
76#include "misc.h"
77#include "match.h"
78#include "buffer.h"
79#include "channels.h"
80#include "msg.h"
81#include "packet.h"
82#include "monitor_fdpass.h"

--- 19 unchanged lines hidden (view full) ---

102 u_int want_tty;
103 u_int want_subsys;
104 u_int want_x_fwd;
105 u_int want_agent_fwd;
106 Buffer cmd;
107 char *term;
108 struct termios tio;
109 char **env;
110 u_int rid;
109};
110
111};
112
113/* Context for global channel callback */
114struct mux_channel_confirm_ctx {
115 u_int cid; /* channel id */
116 u_int rid; /* request id */
117 int fid; /* forward id */
118};
119
111/* fd to control socket */
112int muxserver_sock = -1;
113
114/* client request id */
115u_int muxclient_request_id = 0;
116
117/* Multiplexing control command */
118u_int muxclient_command = 0;

--- 19 unchanged lines hidden (view full) ---

138#define MUX_C_CLOSE_FWD 0x10000007
139#define MUX_C_NEW_STDIO_FWD 0x10000008
140#define MUX_S_OK 0x80000001
141#define MUX_S_PERMISSION_DENIED 0x80000002
142#define MUX_S_FAILURE 0x80000003
143#define MUX_S_EXIT_MESSAGE 0x80000004
144#define MUX_S_ALIVE 0x80000005
145#define MUX_S_SESSION_OPENED 0x80000006
120/* fd to control socket */
121int muxserver_sock = -1;
122
123/* client request id */
124u_int muxclient_request_id = 0;
125
126/* Multiplexing control command */
127u_int muxclient_command = 0;

--- 19 unchanged lines hidden (view full) ---

147#define MUX_C_CLOSE_FWD 0x10000007
148#define MUX_C_NEW_STDIO_FWD 0x10000008
149#define MUX_S_OK 0x80000001
150#define MUX_S_PERMISSION_DENIED 0x80000002
151#define MUX_S_FAILURE 0x80000003
152#define MUX_S_EXIT_MESSAGE 0x80000004
153#define MUX_S_ALIVE 0x80000005
154#define MUX_S_SESSION_OPENED 0x80000006
155#define MUX_S_REMOTE_PORT 0x80000007
146
147/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
148#define MUX_FWD_LOCAL 1
149#define MUX_FWD_REMOTE 2
150#define MUX_FWD_DYNAMIC 3
151
156
157/* type codes for MUX_C_OPEN_FWD and MUX_C_CLOSE_FWD */
158#define MUX_FWD_LOCAL 1
159#define MUX_FWD_REMOTE 2
160#define MUX_FWD_DYNAMIC 3
161
152static void mux_session_confirm(int, void *);
162static void mux_session_confirm(int, int, void *);
153
154static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
155static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
156static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
157static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
158static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
159static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
160static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);

--- 40 unchanged lines hidden (view full) ---

201{
202 Channel *sc, *c = channel_by_id(cid);
203
204 debug3("%s: entering for channel %d", __func__, cid);
205 if (c == NULL)
206 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
207 if (c->remote_id != -1) {
208 if ((sc = channel_by_id(c->remote_id)) == NULL)
163
164static int process_mux_master_hello(u_int, Channel *, Buffer *, Buffer *);
165static int process_mux_new_session(u_int, Channel *, Buffer *, Buffer *);
166static int process_mux_alive_check(u_int, Channel *, Buffer *, Buffer *);
167static int process_mux_terminate(u_int, Channel *, Buffer *, Buffer *);
168static int process_mux_open_fwd(u_int, Channel *, Buffer *, Buffer *);
169static int process_mux_close_fwd(u_int, Channel *, Buffer *, Buffer *);
170static int process_mux_stdio_fwd(u_int, Channel *, Buffer *, Buffer *);

--- 40 unchanged lines hidden (view full) ---

211{
212 Channel *sc, *c = channel_by_id(cid);
213
214 debug3("%s: entering for channel %d", __func__, cid);
215 if (c == NULL)
216 fatal("%s: channel_by_id(%i) == NULL", __func__, cid);
217 if (c->remote_id != -1) {
218 if ((sc = channel_by_id(c->remote_id)) == NULL)
209 debug2("%s: channel %d n session channel %d",
219 fatal("%s: channel %d missing session channel %d",
210 __func__, c->self, c->remote_id);
211 c->remote_id = -1;
212 sc->ctl_chan = -1;
213 if (sc->type != SSH_CHANNEL_OPEN) {
214 debug2("%s: channel %d: not open", __func__, sc->self);
215 chan_mark_dead(sc);
216 } else {
217 if (sc->istate == CHAN_INPUT_OPEN)

--- 78 unchanged lines hidden (view full) ---

296 struct mux_session_confirm_ctx *cctx;
297 char *reserved, *cmd, *cp;
298 u_int i, j, len, env_len, escape_char, window, packetmax;
299 int new_fd[3];
300
301 /* Reply for SSHMUX_COMMAND_OPEN */
302 cctx = xcalloc(1, sizeof(*cctx));
303 cctx->term = NULL;
220 __func__, c->self, c->remote_id);
221 c->remote_id = -1;
222 sc->ctl_chan = -1;
223 if (sc->type != SSH_CHANNEL_OPEN) {
224 debug2("%s: channel %d: not open", __func__, sc->self);
225 chan_mark_dead(sc);
226 } else {
227 if (sc->istate == CHAN_INPUT_OPEN)

--- 78 unchanged lines hidden (view full) ---

306 struct mux_session_confirm_ctx *cctx;
307 char *reserved, *cmd, *cp;
308 u_int i, j, len, env_len, escape_char, window, packetmax;
309 int new_fd[3];
310
311 /* Reply for SSHMUX_COMMAND_OPEN */
312 cctx = xcalloc(1, sizeof(*cctx));
313 cctx->term = NULL;
314 cctx->rid = rid;
304 cmd = reserved = NULL;
305 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
306 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
307 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
308 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
309 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
310 buffer_get_int_ret(&escape_char, m) != 0 ||
311 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||

--- 137 unchanged lines hidden (view full) ---

449 client_new_escape_filter_ctx((int)escape_char));
450 }
451
452 debug2("%s: channel_new: %d linked to control channel %d",
453 __func__, nc->self, nc->ctl_chan);
454
455 channel_send_open(nc->self);
456 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
315 cmd = reserved = NULL;
316 if ((reserved = buffer_get_string_ret(m, NULL)) == NULL ||
317 buffer_get_int_ret(&cctx->want_tty, m) != 0 ||
318 buffer_get_int_ret(&cctx->want_x_fwd, m) != 0 ||
319 buffer_get_int_ret(&cctx->want_agent_fwd, m) != 0 ||
320 buffer_get_int_ret(&cctx->want_subsys, m) != 0 ||
321 buffer_get_int_ret(&escape_char, m) != 0 ||
322 (cctx->term = buffer_get_string_ret(m, &len)) == NULL ||

--- 137 unchanged lines hidden (view full) ---

460 client_new_escape_filter_ctx((int)escape_char));
461 }
462
463 debug2("%s: channel_new: %d linked to control channel %d",
464 __func__, nc->self, nc->ctl_chan);
465
466 channel_send_open(nc->self);
467 channel_register_open_confirm(nc->self, mux_session_confirm, cctx);
457 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 0);
468 c->mux_pause = 1; /* stop handling messages until open_confirm done */
469 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
458
470
459 /* prepare reply */
460 /* XXX defer until mux_session_confirm() fires */
461 buffer_put_int(r, MUX_S_SESSION_OPENED);
462 buffer_put_int(r, rid);
463 buffer_put_int(r, nc->self);
464
471 /* reply is deferred, sent by mux_session_confirm */
465 return 0;
466}
467
468static int
469process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
470{
471 debug2("%s: channel %d: alive check", __func__, c->self);
472

--- 81 unchanged lines hidden (view full) ---

554 if (!compare_host(a->connect_host, b->connect_host))
555 return 0;
556 if (a->connect_port != b->connect_port)
557 return 0;
558
559 return 1;
560}
561
472 return 0;
473}
474
475static int
476process_mux_alive_check(u_int rid, Channel *c, Buffer *m, Buffer *r)
477{
478 debug2("%s: channel %d: alive check", __func__, c->self);
479

--- 81 unchanged lines hidden (view full) ---

561 if (!compare_host(a->connect_host, b->connect_host))
562 return 0;
563 if (a->connect_port != b->connect_port)
564 return 0;
565
566 return 1;
567}
568
569static void
570mux_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
571{
572 struct mux_channel_confirm_ctx *fctx = ctxt;
573 char *failmsg = NULL;
574 Forward *rfwd;
575 Channel *c;
576 Buffer out;
577
578 if ((c = channel_by_id(fctx->cid)) == NULL) {
579 /* no channel for reply */
580 error("%s: unknown channel", __func__);
581 return;
582 }
583 buffer_init(&out);
584 if (fctx->fid >= options.num_remote_forwards) {
585 xasprintf(&failmsg, "unknown forwarding id %d", fctx->fid);
586 goto fail;
587 }
588 rfwd = &options.remote_forwards[fctx->fid];
589 debug("%s: %s for: listen %d, connect %s:%d", __func__,
590 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
591 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
592 if (type == SSH2_MSG_REQUEST_SUCCESS) {
593 if (rfwd->listen_port == 0) {
594 rfwd->allocated_port = packet_get_int();
595 logit("Allocated port %u for mux remote forward"
596 " to %s:%d", rfwd->allocated_port,
597 rfwd->connect_host, rfwd->connect_port);
598 buffer_put_int(&out, MUX_S_REMOTE_PORT);
599 buffer_put_int(&out, fctx->rid);
600 buffer_put_int(&out, rfwd->allocated_port);
601 } else {
602 buffer_put_int(&out, MUX_S_OK);
603 buffer_put_int(&out, fctx->rid);
604 }
605 goto out;
606 } else {
607 xasprintf(&failmsg, "remote port forwarding failed for "
608 "listen port %d", rfwd->listen_port);
609 }
610 fail:
611 error("%s: %s", __func__, failmsg);
612 buffer_put_int(&out, MUX_S_FAILURE);
613 buffer_put_int(&out, fctx->rid);
614 buffer_put_cstring(&out, failmsg);
615 xfree(failmsg);
616 out:
617 buffer_put_string(&c->output, buffer_ptr(&out), buffer_len(&out));
618 buffer_free(&out);
619 if (c->mux_pause <= 0)
620 fatal("%s: mux_pause %d", __func__, c->mux_pause);
621 c->mux_pause = 0; /* start processing messages again */
622}
623
562static int
563process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
564{
565 Forward fwd;
566 char *fwd_desc = NULL;
567 u_int ftype;
568 int i, ret = 0, freefwd = 1;
569

--- 19 unchanged lines hidden (view full) ---

589
590 debug2("%s: channel %d: request %s", __func__, c->self,
591 (fwd_desc = format_forward(ftype, &fwd)));
592
593 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
594 ftype != MUX_FWD_DYNAMIC) {
595 logit("%s: invalid forwarding type %u", __func__, ftype);
596 invalid:
624static int
625process_mux_open_fwd(u_int rid, Channel *c, Buffer *m, Buffer *r)
626{
627 Forward fwd;
628 char *fwd_desc = NULL;
629 u_int ftype;
630 int i, ret = 0, freefwd = 1;
631

--- 19 unchanged lines hidden (view full) ---

651
652 debug2("%s: channel %d: request %s", __func__, c->self,
653 (fwd_desc = format_forward(ftype, &fwd)));
654
655 if (ftype != MUX_FWD_LOCAL && ftype != MUX_FWD_REMOTE &&
656 ftype != MUX_FWD_DYNAMIC) {
657 logit("%s: invalid forwarding type %u", __func__, ftype);
658 invalid:
597 xfree(fwd.listen_host);
598 xfree(fwd.connect_host);
659 if (fwd.listen_host)
660 xfree(fwd.listen_host);
661 if (fwd.connect_host)
662 xfree(fwd.connect_host);
599 buffer_put_int(r, MUX_S_FAILURE);
600 buffer_put_int(r, rid);
601 buffer_put_cstring(r, "Invalid forwarding request");
602 return 0;
603 }
663 buffer_put_int(r, MUX_S_FAILURE);
664 buffer_put_int(r, rid);
665 buffer_put_cstring(r, "Invalid forwarding request");
666 return 0;
667 }
604 /* XXX support rport0 forwarding with reply of port assigned */
605 if (fwd.listen_port == 0 || fwd.listen_port >= 65536) {
668 if (fwd.listen_port >= 65536) {
606 logit("%s: invalid listen port %u", __func__,
607 fwd.listen_port);
608 goto invalid;
609 }
610 if (fwd.connect_port >= 65536 || (ftype != MUX_FWD_DYNAMIC &&
611 ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
612 logit("%s: invalid connect port %u", __func__,
613 fwd.connect_port);

--- 18 unchanged lines hidden (view full) ---

632 buffer_put_int(r, rid);
633 goto out;
634 }
635 }
636 break;
637 case MUX_FWD_REMOTE:
638 for (i = 0; i < options.num_remote_forwards; i++) {
639 if (compare_forward(&fwd,
669 logit("%s: invalid listen port %u", __func__,
670 fwd.listen_port);
671 goto invalid;
672 }
673 if (fwd.connect_port >= 65536 || (ftype != MUX_FWD_DYNAMIC &&
674 ftype != MUX_FWD_REMOTE && fwd.connect_port == 0)) {
675 logit("%s: invalid connect port %u", __func__,
676 fwd.connect_port);

--- 18 unchanged lines hidden (view full) ---

695 buffer_put_int(r, rid);
696 goto out;
697 }
698 }
699 break;
700 case MUX_FWD_REMOTE:
701 for (i = 0; i < options.num_remote_forwards; i++) {
702 if (compare_forward(&fwd,
640 options.remote_forwards + i))
641 goto exists;
703 options.remote_forwards + i)) {
704 if (fwd.listen_port != 0)
705 goto exists;
706 debug2("%s: found allocated port",
707 __func__);
708 buffer_put_int(r, MUX_S_REMOTE_PORT);
709 buffer_put_int(r, rid);
710 buffer_put_int(r,
711 options.remote_forwards[i].allocated_port);
712 goto out;
713 }
642 }
643 break;
644 }
645
646 if (options.control_master == SSHCTL_MASTER_ASK ||
647 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
648 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
649 debug2("%s: forwarding refused by user", __func__);
650 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
651 buffer_put_int(r, rid);
652 buffer_put_cstring(r, "Permission denied");
653 goto out;
654 }
655 }
656
657 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
714 }
715 break;
716 }
717
718 if (options.control_master == SSHCTL_MASTER_ASK ||
719 options.control_master == SSHCTL_MASTER_AUTO_ASK) {
720 if (!ask_permission("Open %s on %s?", fwd_desc, host)) {
721 debug2("%s: forwarding refused by user", __func__);
722 buffer_put_int(r, MUX_S_PERMISSION_DENIED);
723 buffer_put_int(r, rid);
724 buffer_put_cstring(r, "Permission denied");
725 goto out;
726 }
727 }
728
729 if (ftype == MUX_FWD_LOCAL || ftype == MUX_FWD_DYNAMIC) {
658 if (options.num_local_forwards + 1 >=
659 SSH_MAX_FORWARDS_PER_DIRECTION ||
660 channel_setup_local_fwd_listener(fwd.listen_host,
730 if (channel_setup_local_fwd_listener(fwd.listen_host,
661 fwd.listen_port, fwd.connect_host, fwd.connect_port,
662 options.gateway_ports) < 0) {
663 fail:
664 logit("slave-requested %s failed", fwd_desc);
665 buffer_put_int(r, MUX_S_FAILURE);
666 buffer_put_int(r, rid);
667 buffer_put_cstring(r, "Port forwarding failed");
668 goto out;
669 }
670 add_local_forward(&options, &fwd);
671 freefwd = 0;
672 } else {
731 fwd.listen_port, fwd.connect_host, fwd.connect_port,
732 options.gateway_ports) < 0) {
733 fail:
734 logit("slave-requested %s failed", fwd_desc);
735 buffer_put_int(r, MUX_S_FAILURE);
736 buffer_put_int(r, rid);
737 buffer_put_cstring(r, "Port forwarding failed");
738 goto out;
739 }
740 add_local_forward(&options, &fwd);
741 freefwd = 0;
742 } else {
673 /* XXX wait for remote to confirm */
674 if (options.num_remote_forwards + 1 >=
675 SSH_MAX_FORWARDS_PER_DIRECTION ||
676 channel_request_remote_forwarding(fwd.listen_host,
743 struct mux_channel_confirm_ctx *fctx;
744
745 if (channel_request_remote_forwarding(fwd.listen_host,
677 fwd.listen_port, fwd.connect_host, fwd.connect_port) < 0)
678 goto fail;
679 add_remote_forward(&options, &fwd);
746 fwd.listen_port, fwd.connect_host, fwd.connect_port) < 0)
747 goto fail;
748 add_remote_forward(&options, &fwd);
749 fctx = xcalloc(1, sizeof(*fctx));
750 fctx->cid = c->self;
751 fctx->rid = rid;
752 fctx->fid = options.num_remote_forwards - 1;
753 client_register_global_confirm(mux_confirm_remote_forward,
754 fctx);
680 freefwd = 0;
755 freefwd = 0;
756 c->mux_pause = 1; /* wait for mux_confirm_remote_forward */
757 /* delayed reply in mux_confirm_remote_forward */
758 goto out;
681 }
682 buffer_put_int(r, MUX_S_OK);
683 buffer_put_int(r, rid);
684 out:
685 if (fwd_desc != NULL)
686 xfree(fwd_desc);
687 if (freefwd) {
688 if (fwd.listen_host != NULL)

--- 132 unchanged lines hidden (view full) ---

821 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
822
823 nc->ctl_chan = c->self; /* link session -> control channel */
824 c->remote_id = nc->self; /* link control -> session channel */
825
826 debug2("%s: channel_new: %d linked to control channel %d",
827 __func__, nc->self, nc->ctl_chan);
828
759 }
760 buffer_put_int(r, MUX_S_OK);
761 buffer_put_int(r, rid);
762 out:
763 if (fwd_desc != NULL)
764 xfree(fwd_desc);
765 if (freefwd) {
766 if (fwd.listen_host != NULL)

--- 132 unchanged lines hidden (view full) ---

899 nc = channel_connect_stdio_fwd(chost, cport, new_fd[0], new_fd[1]);
900
901 nc->ctl_chan = c->self; /* link session -> control channel */
902 c->remote_id = nc->self; /* link control -> session channel */
903
904 debug2("%s: channel_new: %d linked to control channel %d",
905 __func__, nc->self, nc->ctl_chan);
906
829 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 0);
907 channel_register_cleanup(nc->self, mux_master_session_cleanup_cb, 1);
830
831 /* prepare reply */
832 /* XXX defer until channel confirmed */
833 buffer_put_int(r, MUX_S_SESSION_OPENED);
834 buffer_put_int(r, rid);
835 buffer_put_int(r, nc->self);
836
837 return 0;

--- 6 unchanged lines hidden (view full) ---

844 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
845 Buffer in, out;
846 void *ptr;
847 u_int type, rid, have, i;
848 int ret = -1;
849
850 /* Setup ctx and */
851 if (c->mux_ctx == NULL) {
908
909 /* prepare reply */
910 /* XXX defer until channel confirmed */
911 buffer_put_int(r, MUX_S_SESSION_OPENED);
912 buffer_put_int(r, rid);
913 buffer_put_int(r, nc->self);
914
915 return 0;

--- 6 unchanged lines hidden (view full) ---

922 struct mux_master_state *state = (struct mux_master_state *)c->mux_ctx;
923 Buffer in, out;
924 void *ptr;
925 u_int type, rid, have, i;
926 int ret = -1;
927
928 /* Setup ctx and */
929 if (c->mux_ctx == NULL) {
852 state = xcalloc(1, sizeof(state));
930 state = xcalloc(1, sizeof(*state));
853 c->mux_ctx = state;
854 channel_register_cleanup(c->self,
855 mux_master_control_cleanup_cb, 0);
856
857 /* Send hello */
858 buffer_init(&out);
859 buffer_put_int(&out, MUX_MSG_HELLO);
860 buffer_put_int(&out, SSHMUX_VER);

--- 134 unchanged lines hidden (view full) ---

995 0, addr.sun_path, 1);
996 mux_listener_channel->mux_rcb = mux_master_read_cb;
997 debug3("%s: mux listener channel %d fd %d", __func__,
998 mux_listener_channel->self, mux_listener_channel->sock);
999}
1000
1001/* Callback on open confirmation in mux master for a mux client session. */
1002static void
931 c->mux_ctx = state;
932 channel_register_cleanup(c->self,
933 mux_master_control_cleanup_cb, 0);
934
935 /* Send hello */
936 buffer_init(&out);
937 buffer_put_int(&out, MUX_MSG_HELLO);
938 buffer_put_int(&out, SSHMUX_VER);

--- 134 unchanged lines hidden (view full) ---

1073 0, addr.sun_path, 1);
1074 mux_listener_channel->mux_rcb = mux_master_read_cb;
1075 debug3("%s: mux listener channel %d fd %d", __func__,
1076 mux_listener_channel->self, mux_listener_channel->sock);
1077}
1078
1079/* Callback on open confirmation in mux master for a mux client session. */
1080static void
1003mux_session_confirm(int id, void *arg)
1081mux_session_confirm(int id, int success, void *arg)
1004{
1005 struct mux_session_confirm_ctx *cctx = arg;
1006 const char *display;
1082{
1083 struct mux_session_confirm_ctx *cctx = arg;
1084 const char *display;
1007 Channel *c;
1085 Channel *c, *cc;
1008 int i;
1086 int i;
1087 Buffer reply;
1009
1010 if (cctx == NULL)
1011 fatal("%s: cctx == NULL", __func__);
1012 if ((c = channel_by_id(id)) == NULL)
1013 fatal("%s: no channel for id %d", __func__, id);
1088
1089 if (cctx == NULL)
1090 fatal("%s: cctx == NULL", __func__);
1091 if ((c = channel_by_id(id)) == NULL)
1092 fatal("%s: no channel for id %d", __func__, id);
1093 if ((cc = channel_by_id(c->ctl_chan)) == NULL)
1094 fatal("%s: channel %d lacks control channel %d", __func__,
1095 id, c->ctl_chan);
1014
1096
1097 if (!success) {
1098 debug3("%s: sending failure reply", __func__);
1099 /* prepare reply */
1100 buffer_init(&reply);
1101 buffer_put_int(&reply, MUX_S_FAILURE);
1102 buffer_put_int(&reply, cctx->rid);
1103 buffer_put_cstring(&reply, "Session open refused by peer");
1104 goto done;
1105 }
1106
1015 display = getenv("DISPLAY");
1016 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1017 char *proto, *data;
1107 display = getenv("DISPLAY");
1108 if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
1109 char *proto, *data;
1110
1018 /* Get reasonable local authentication information. */
1019 client_x11_get_proto(display, options.xauth_location,
1111 /* Get reasonable local authentication information. */
1112 client_x11_get_proto(display, options.xauth_location,
1020 options.forward_x11_trusted, &proto, &data);
1113 options.forward_x11_trusted, options.forward_x11_timeout,
1114 &proto, &data);
1021 /* Request forwarding with authentication spoofing. */
1115 /* Request forwarding with authentication spoofing. */
1022 debug("Requesting X11 forwarding with authentication spoofing.");
1116 debug("Requesting X11 forwarding with authentication "
1117 "spoofing.");
1023 x11_request_forwarding_with_spoofing(id, display, proto, data);
1024 /* XXX wait for reply */
1025 }
1026
1027 if (cctx->want_agent_fwd && options.forward_agent) {
1028 debug("Requesting authentication agent forwarding.");
1029 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1030 packet_send();
1031 }
1032
1033 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1034 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1035
1118 x11_request_forwarding_with_spoofing(id, display, proto, data);
1119 /* XXX wait for reply */
1120 }
1121
1122 if (cctx->want_agent_fwd && options.forward_agent) {
1123 debug("Requesting authentication agent forwarding.");
1124 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1125 packet_send();
1126 }
1127
1128 client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
1129 cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
1130
1131 debug3("%s: sending success reply", __func__);
1132 /* prepare reply */
1133 buffer_init(&reply);
1134 buffer_put_int(&reply, MUX_S_SESSION_OPENED);
1135 buffer_put_int(&reply, cctx->rid);
1136 buffer_put_int(&reply, c->self);
1137
1138 done:
1139 /* Send reply */
1140 buffer_put_string(&cc->output, buffer_ptr(&reply), buffer_len(&reply));
1141 buffer_free(&reply);
1142
1143 if (cc->mux_pause <= 0)
1144 fatal("%s: mux_pause %d", __func__, cc->mux_pause);
1145 cc->mux_pause = 0; /* start processing messages again */
1036 c->open_confirm_ctx = NULL;
1037 buffer_free(&cctx->cmd);
1038 xfree(cctx->term);
1039 if (cctx->env != NULL) {
1040 for (i = 0; cctx->env[i] != NULL; i++)
1041 xfree(cctx->env[i]);
1042 xfree(cctx->env);
1043 }

--- 316 unchanged lines hidden (view full) ---

1360
1361 type = buffer_get_int(&m);
1362 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1363 fatal("%s: out of sequence reply: my id %u theirs %u",
1364 __func__, muxclient_request_id, rid);
1365 switch (type) {
1366 case MUX_S_OK:
1367 break;
1146 c->open_confirm_ctx = NULL;
1147 buffer_free(&cctx->cmd);
1148 xfree(cctx->term);
1149 if (cctx->env != NULL) {
1150 for (i = 0; cctx->env[i] != NULL; i++)
1151 xfree(cctx->env[i]);
1152 xfree(cctx->env);
1153 }

--- 316 unchanged lines hidden (view full) ---

1470
1471 type = buffer_get_int(&m);
1472 if ((rid = buffer_get_int(&m)) != muxclient_request_id)
1473 fatal("%s: out of sequence reply: my id %u theirs %u",
1474 __func__, muxclient_request_id, rid);
1475 switch (type) {
1476 case MUX_S_OK:
1477 break;
1478 case MUX_S_REMOTE_PORT:
1479 fwd->allocated_port = buffer_get_int(&m);
1480 logit("Allocated port %u for remote forward to %s:%d",
1481 fwd->allocated_port,
1482 fwd->connect_host ? fwd->connect_host : "",
1483 fwd->connect_port);
1484 if (muxclient_command == SSHMUX_COMMAND_FORWARD)
1485 fprintf(stdout, "%u\n", fwd->allocated_port);
1486 break;
1368 case MUX_S_PERMISSION_DENIED:
1369 e = buffer_get_string(&m, NULL);
1370 buffer_free(&m);
1371 error("Master refused forwarding request: %s", e);
1372 return -1;
1373 case MUX_S_FAILURE:
1374 e = buffer_get_string(&m, NULL);
1375 buffer_free(&m);

--- 350 unchanged lines hidden (view full) ---

1726 if ((pid = mux_client_request_alive(sock)) == 0)
1727 fatal("%s: master alive check failed", __func__);
1728 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
1729 exit(0);
1730 case SSHMUX_COMMAND_TERMINATE:
1731 mux_client_request_terminate(sock);
1732 fprintf(stderr, "Exit request sent.\r\n");
1733 exit(0);
1487 case MUX_S_PERMISSION_DENIED:
1488 e = buffer_get_string(&m, NULL);
1489 buffer_free(&m);
1490 error("Master refused forwarding request: %s", e);
1491 return -1;
1492 case MUX_S_FAILURE:
1493 e = buffer_get_string(&m, NULL);
1494 buffer_free(&m);

--- 350 unchanged lines hidden (view full) ---

1845 if ((pid = mux_client_request_alive(sock)) == 0)
1846 fatal("%s: master alive check failed", __func__);
1847 fprintf(stderr, "Master running (pid=%d)\r\n", pid);
1848 exit(0);
1849 case SSHMUX_COMMAND_TERMINATE:
1850 mux_client_request_terminate(sock);
1851 fprintf(stderr, "Exit request sent.\r\n");
1852 exit(0);
1853 case SSHMUX_COMMAND_FORWARD:
1854 if (mux_client_request_forwards(sock) != 0)
1855 fatal("%s: master forward request failed", __func__);
1856 exit(0);
1734 case SSHMUX_COMMAND_OPEN:
1735 if (mux_client_request_forwards(sock) != 0) {
1736 error("%s: master forward request failed", __func__);
1737 return;
1738 }
1739 mux_client_request_session(sock);
1740 return;
1741 case SSHMUX_COMMAND_STDIO_FWD:
1742 mux_client_request_stdio_fwd(sock);
1743 exit(0);
1744 default:
1745 fatal("unrecognised muxclient_command %d", muxclient_command);
1746 }
1747}
1857 case SSHMUX_COMMAND_OPEN:
1858 if (mux_client_request_forwards(sock) != 0) {
1859 error("%s: master forward request failed", __func__);
1860 return;
1861 }
1862 mux_client_request_session(sock);
1863 return;
1864 case SSHMUX_COMMAND_STDIO_FWD:
1865 mux_client_request_stdio_fwd(sock);
1866 exit(0);
1867 default:
1868 fatal("unrecognised muxclient_command %d", muxclient_command);
1869 }
1870}