blob: 1e96eb64167461727c49028c6bc4175fbba71fec [file] [log] [blame]
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001/* $OpenBSD: nchan.c,v 1.70 2019/06/28 13:35:04 deraadt Exp $ */
Damien Miller5428f641999-11-25 11:54:57 +11002/*
Damien Millerb51ed392002-01-22 23:29:03 +11003 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Miller5428f641999-11-25 11:54:57 +11004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Miller5428f641999-11-25 11:54:57 +110013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Millere3b60b52006-07-10 21:08:03 +100028#include <sys/types.h>
29#include <sys/socket.h>
30
Darren Tucker39972492006-07-12 22:22:46 +100031#include <errno.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <stdarg.h>
Darren Tucker39972492006-07-12 22:22:46 +100034
Damien Millerb84886b2008-05-19 15:05:07 +100035#include "openbsd-compat/sys-queue.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "ssh2.h"
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000037#include "sshbuf.h"
38#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "packet.h"
40#include "channels.h"
Damien Miller33b13562000-04-04 14:38:59 +100041#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Ben Lindstrom3b670d02001-06-09 00:57:39 +000044/*
45 * SSH Protocol 1.5 aka New Channel Protocol
46 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
47 * Written by Markus Friedl in October 1999
48 *
49 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
50 * tear down of channels:
51 *
52 * 1.3: strict request-ack-protocol:
Darren Tuckerfc959702004-07-17 16:12:08 +100053 * CLOSE ->
54 * <- CLOSE_CONFIRM
Ben Lindstrom3b670d02001-06-09 00:57:39 +000055 *
56 * 1.5: uses variations of:
Darren Tuckerfc959702004-07-17 16:12:08 +100057 * IEOF ->
58 * <- OCLOSE
59 * <- IEOF
60 * OCLOSE ->
61 * i.e. both sides have to close the channel
Ben Lindstrom3b670d02001-06-09 00:57:39 +000062 *
63 * 2.0: the EOF messages are optional
64 *
65 * See the debugging output from 'ssh -v' and 'sshd -d' of
66 * ssh-1.2.27 as an example.
67 *
68 */
Kevin Stevesc3422eb2001-09-20 19:33:33 +000069
Damien Miller33b13562000-04-04 14:38:59 +100070/* functions manipulating channel states */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071/*
Damien Miller95def091999-11-25 00:26:21 +110072 * EVENTS update channel input/output states execute ACTIONS
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073 */
Damien Miller33b13562000-04-04 14:38:59 +100074/*
75 * ACTIONS: should never update the channel states
76 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000077static void chan_send_eof2(struct ssh *, Channel *);
78static void chan_send_eow2(struct ssh *, Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100079
Damien Miller33b13562000-04-04 14:38:59 +100080/* helper */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000081static void chan_shutdown_write(struct ssh *, Channel *);
82static void chan_shutdown_read(struct ssh *, Channel *);
djm@openbsd.orge0d65012018-10-04 07:47:35 +000083static void chan_shutdown_extended_read(struct ssh *, Channel *);
Damien Miller33b13562000-04-04 14:38:59 +100084
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000085static const char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
86static const char *istates[] = { "open", "drain", "wait_oclose", "closed" };
Damien Millerabea8ee2002-01-22 23:27:11 +110087
88static void
89chan_set_istate(Channel *c, u_int next)
90{
91 if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
92 fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +100093 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
Damien Millerabea8ee2002-01-22 23:27:11 +110094 istates[next]);
95 c->istate = next;
96}
djm@openbsd.org97f4d302017-04-30 23:13:25 +000097
Damien Millerabea8ee2002-01-22 23:27:11 +110098static void
99chan_set_ostate(Channel *c, u_int next)
100{
101 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
102 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
Damien Millerfbdeece2003-09-02 22:52:31 +1000103 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
Damien Millerabea8ee2002-01-22 23:27:11 +1100104 ostates[next]);
105 c->ostate = next;
106}
107
Damien Miller5144df92002-01-22 23:28:45 +1100108void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000109chan_read_failed(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100110{
Damien Millerfbdeece2003-09-02 22:52:31 +1000111 debug2("channel %d: read failed", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100112 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113 case CHAN_INPUT_OPEN:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000114 chan_shutdown_read(ssh, c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100115 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116 break;
117 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000118 error("channel %d: chan_read_failed for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000119 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120 break;
121 }
122}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000123
Damien Miller5144df92002-01-22 23:28:45 +1100124void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000125chan_ibuf_empty(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100126{
Damien Millerfbdeece2003-09-02 22:52:31 +1000127 debug2("channel %d: ibuf empty", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000128 if (sshbuf_len(c->input)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000129 error("channel %d: chan_ibuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000130 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131 return;
132 }
Damien Miller95def091999-11-25 00:26:21 +1100133 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 case CHAN_INPUT_WAIT_DRAIN:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000135 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000136 chan_send_eof2(ssh, c);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000137 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138 break;
139 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000140 error("channel %d: chan_ibuf_empty for istate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000141 c->self, c->istate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142 break;
143 }
144}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000145
Damien Miller5144df92002-01-22 23:28:45 +1100146void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000147chan_obuf_empty(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100148{
Damien Millerfbdeece2003-09-02 22:52:31 +1000149 debug2("channel %d: obuf empty", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000150 if (sshbuf_len(c->output)) {
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000151 error("channel %d: chan_obuf_empty for non empty buffer",
Damien Miller33b13562000-04-04 14:38:59 +1000152 c->self);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153 return;
154 }
Damien Miller95def091999-11-25 00:26:21 +1100155 switch (c->ostate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156 case CHAN_OUTPUT_WAIT_DRAIN:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000157 chan_shutdown_write(ssh, c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100158 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159 break;
160 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000161 error("channel %d: internal error: obuf_empty for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000162 c->self, c->ostate);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163 break;
164 }
165}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000166
167void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000168chan_rcvd_eow(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100169{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000170 debug2("channel %d: rcvd eow", c->self);
Damien Miller95def091999-11-25 00:26:21 +1100171 switch (c->istate) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 case CHAN_INPUT_OPEN:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000173 chan_shutdown_read(ssh, c);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000174 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175 break;
176 }
177}
Damien Miller33b13562000-04-04 14:38:59 +1000178
Damien Miller33b13562000-04-04 14:38:59 +1000179static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180chan_send_eof2(struct ssh *ssh, Channel *c)
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000181{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000182 int r;
183
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000184 debug2("channel %d: send eof", c->self);
185 switch (c->istate) {
186 case CHAN_INPUT_WAIT_DRAIN:
djm@openbsd.org9f532292017-09-12 06:35:31 +0000187 if (!c->have_remote_id)
188 fatal("%s: channel %d: no remote_id",
189 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000190 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
191 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
192 (r = sshpkt_send(ssh)) != 0)
193 fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000194 c->flags |= CHAN_EOF_SENT;
195 break;
196 default:
197 error("channel %d: cannot send eof for istate %d",
198 c->self, c->istate);
199 break;
200 }
201}
202
203static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000204chan_send_close2(struct ssh *ssh, Channel *c)
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000205{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000206 int r;
207
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000208 debug2("channel %d: send close", c->self);
209 if (c->ostate != CHAN_OUTPUT_CLOSED ||
210 c->istate != CHAN_INPUT_CLOSED) {
211 error("channel %d: cannot send close for istate/ostate %d/%d",
212 c->self, c->istate, c->ostate);
213 } else if (c->flags & CHAN_CLOSE_SENT) {
214 error("channel %d: already sent close", c->self);
215 } else {
djm@openbsd.org9f532292017-09-12 06:35:31 +0000216 if (!c->have_remote_id)
217 fatal("%s: channel %d: no remote_id",
218 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000219 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
220 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
221 (r = sshpkt_send(ssh)) != 0)
222 fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000223 c->flags |= CHAN_CLOSE_SENT;
224 }
225}
226
227static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000228chan_send_eow2(struct ssh *ssh, Channel *c)
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000229{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000230 int r;
231
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000232 debug2("channel %d: send eow", c->self);
233 if (c->ostate == CHAN_OUTPUT_CLOSED) {
234 error("channel %d: must not sent eow on closed output",
235 c->self);
236 return;
237 }
238 if (!(datafellows & SSH_NEW_OPENSSH))
239 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +0000240 if (!c->have_remote_id)
241 fatal("%s: channel %d: no remote_id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000242 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
243 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
244 (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
245 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
246 (r = sshpkt_send(ssh)) != 0)
247 fatal("%s: send CHANNEL_EOF: %s", __func__, ssh_err(r));
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000248}
249
250/* shared */
251
252void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000253chan_rcvd_ieof(struct ssh *ssh, Channel *c)
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000254{
255 debug2("channel %d: rcvd eof", c->self);
256 c->flags |= CHAN_EOF_RCVD;
257 if (c->ostate == CHAN_OUTPUT_OPEN)
258 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
259 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000260 sshbuf_len(c->output) == 0 &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000261 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000262 chan_obuf_empty(ssh, c);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000263}
264
265void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000266chan_rcvd_oclose(struct ssh *ssh, Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000267{
Damien Millerfbdeece2003-09-02 22:52:31 +1000268 debug2("channel %d: rcvd close", c->self);
Damien Millere1537f92010-01-26 13:26:22 +1100269 if (!(c->flags & CHAN_LOCAL)) {
270 if (c->flags & CHAN_CLOSE_RCVD)
271 error("channel %d: protocol error: close rcvd twice",
272 c->self);
273 c->flags |= CHAN_CLOSE_RCVD;
274 }
Damien Miller33b13562000-04-04 14:38:59 +1000275 if (c->type == SSH_CHANNEL_LARVAL) {
276 /* tear down larval channels immediately */
Damien Millerabea8ee2002-01-22 23:27:11 +1100277 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
278 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000279 return;
280 }
281 switch (c->ostate) {
282 case CHAN_OUTPUT_OPEN:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000283 /*
284 * wait until a data from the channel is consumed if a CLOSE
285 * is received
286 */
Damien Millerabea8ee2002-01-22 23:27:11 +1100287 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
Damien Miller33b13562000-04-04 14:38:59 +1000288 break;
289 }
290 switch (c->istate) {
291 case CHAN_INPUT_OPEN:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000292 chan_shutdown_read(ssh, c);
djm@openbsd.orge0d65012018-10-04 07:47:35 +0000293 chan_shutdown_extended_read(ssh, c);
Damien Millerebc11d32002-01-22 23:28:13 +1100294 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000295 break;
296 case CHAN_INPUT_WAIT_DRAIN:
Damien Millere1537f92010-01-26 13:26:22 +1100297 if (!(c->flags & CHAN_LOCAL))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000298 chan_send_eof2(ssh, c);
djm@openbsd.orge0d65012018-10-04 07:47:35 +0000299 chan_shutdown_extended_read(ssh, c);
Damien Millerebc11d32002-01-22 23:28:13 +1100300 chan_set_istate(c, CHAN_INPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000301 break;
302 }
Damien Miller33b13562000-04-04 14:38:59 +1000303}
Damien Millere1537f92010-01-26 13:26:22 +1100304
Damien Millerbab9bd42008-05-19 16:06:47 +1000305void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000306chan_write_failed(struct ssh *ssh, Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000307{
Damien Millerfbdeece2003-09-02 22:52:31 +1000308 debug2("channel %d: write failed", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000309 switch (c->ostate) {
310 case CHAN_OUTPUT_OPEN:
Damien Miller33b13562000-04-04 14:38:59 +1000311 case CHAN_OUTPUT_WAIT_DRAIN:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000312 chan_shutdown_write(ssh, c);
Darren Tucker8748b962008-07-02 22:32:43 +1000313 if (strcmp(c->ctype, "session") == 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000314 chan_send_eow2(ssh, c);
Damien Millerabea8ee2002-01-22 23:27:11 +1100315 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
Damien Miller33b13562000-04-04 14:38:59 +1000316 break;
317 default:
Ben Lindstrom3b670d02001-06-09 00:57:39 +0000318 error("channel %d: chan_write_failed for ostate %d",
Damien Miller33b13562000-04-04 14:38:59 +1000319 c->self, c->ostate);
320 break;
321 }
322}
Damien Miller5144df92002-01-22 23:28:45 +1100323
324void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000325chan_mark_dead(struct ssh *ssh, Channel *c)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000326{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000327 c->type = SSH_CHANNEL_ZOMBIE;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000328}
329
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000330int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000331chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
Damien Miller33b13562000-04-04 14:38:59 +1000332{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000333 if (c->type == SSH_CHANNEL_ZOMBIE) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000334 debug2("channel %d: zombie", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000335 return 1;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000336 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000337 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
338 return 0;
Ben Lindstromcf159442002-03-26 03:26:24 +0000339 if ((datafellows & SSH_BUG_EXTEOF) &&
340 c->extended_usage == CHAN_EXTENDED_WRITE &&
341 c->efd != -1 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000342 sshbuf_len(c->extended) > 0) {
343 debug2("channel %d: active efd: %d len %zu",
344 c->self, c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000345 return 0;
346 }
Damien Millere1537f92010-01-26 13:26:22 +1100347 if (c->flags & CHAN_LOCAL) {
348 debug2("channel %d: is dead (local)", c->self);
349 return 1;
350 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000351 if (!(c->flags & CHAN_CLOSE_SENT)) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000352 if (do_send) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000353 chan_send_close2(ssh, c);
Ben Lindstromcf159442002-03-26 03:26:24 +0000354 } else {
355 /* channel would be dead if we sent a close */
356 if (c->flags & CHAN_CLOSE_RCVD) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000357 debug2("channel %d: almost dead",
Ben Lindstromcf159442002-03-26 03:26:24 +0000358 c->self);
359 return 1;
Damien Miller3ec27592001-10-12 11:35:04 +1000360 }
Damien Miller33b13562000-04-04 14:38:59 +1000361 }
Ben Lindstromcf159442002-03-26 03:26:24 +0000362 }
363 if ((c->flags & CHAN_CLOSE_SENT) &&
364 (c->flags & CHAN_CLOSE_RCVD)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000365 debug2("channel %d: is dead", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +0000366 return 1;
Damien Miller33b13562000-04-04 14:38:59 +1000367 }
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000368 return 0;
Damien Miller33b13562000-04-04 14:38:59 +1000369}
370
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371/* helper */
372static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000373chan_shutdown_write(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100374{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000375 sshbuf_reset(c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000376 if (c->type == SSH_CHANNEL_LARVAL)
Damien Miller33b13562000-04-04 14:38:59 +1000377 return;
Damien Miller5428f641999-11-25 11:54:57 +1100378 /* shutdown failure is allowed if write failed already */
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000379 debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
380 c->self, __func__, c->istate, c->ostate, c->sock, c->wfd, c->efd,
381 channel_format_extended_usage(c));
Damien Miller33b13562000-04-04 14:38:59 +1000382 if (c->sock != -1) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000383 if (shutdown(c->sock, SHUT_WR) == -1) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000384 debug2("channel %d: %s: shutdown() failed for "
385 "fd %d [i%d o%d]: %.100s", c->self, __func__,
386 c->sock, c->istate, c->ostate,
387 strerror(errno));
388 }
Damien Miller33b13562000-04-04 14:38:59 +1000389 } else {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000390 if (channel_close_fd(ssh, &c->wfd) < 0) {
391 logit("channel %d: %s: close() failed for "
392 "fd %d [i%d o%d]: %.100s",
393 c->self, __func__, c->wfd, c->istate, c->ostate,
394 strerror(errno));
395 }
Damien Miller33b13562000-04-04 14:38:59 +1000396 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397}
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000398
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000400chan_shutdown_read(struct ssh *ssh, Channel *c)
Damien Miller95def091999-11-25 00:26:21 +1100401{
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000402 if (c->type == SSH_CHANNEL_LARVAL)
Damien Miller33b13562000-04-04 14:38:59 +1000403 return;
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000404 debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
405 c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
406 channel_format_extended_usage(c));
Damien Miller33b13562000-04-04 14:38:59 +1000407 if (c->sock != -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000408 /*
Damien Miller0f091bd2000-08-07 15:47:48 +1000409 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
410 * write side has been closed already. (bug on Linux)
Kevin Steves871f6622001-09-18 16:08:24 +0000411 * HP-UX may return ENOTCONN also.
Damien Miller0f091bd2000-08-07 15:47:48 +1000412 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000413 if (shutdown(c->sock, SHUT_RD) == -1 && errno != ENOTCONN) {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000414 error("channel %d: %s: shutdown() failed for "
415 "fd %d [i%d o%d]: %.100s",
416 c->self, __func__, c->sock, c->istate, c->ostate,
417 strerror(errno));
418 }
Damien Miller33b13562000-04-04 14:38:59 +1000419 } else {
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000420 if (channel_close_fd(ssh, &c->rfd) < 0) {
421 logit("channel %d: %s: close() failed for "
422 "fd %d [i%d o%d]: %.100s",
423 c->self, __func__, c->rfd, c->istate, c->ostate,
424 strerror(errno));
425 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426 }
427}
djm@openbsd.orge0d65012018-10-04 07:47:35 +0000428
429static void
430chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
431{
432 if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
433 return;
434 if (c->extended_usage != CHAN_EXTENDED_READ &&
435 c->extended_usage != CHAN_EXTENDED_IGNORE)
436 return;
437 debug2("channel %d: %s (i%d o%d sock %d wfd %d efd %d [%s])",
438 c->self, __func__, c->istate, c->ostate, c->sock, c->rfd, c->efd,
439 channel_format_extended_usage(c));
440 if (channel_close_fd(ssh, &c->efd) < 0) {
441 logit("channel %d: %s: close() failed for "
442 "extended fd %d [i%d o%d]: %.100s",
443 c->self, __func__, c->efd, c->istate, c->ostate,
444 strerror(errno));
445 }
446}