]> andersk Git - openssh.git/blame - nchan.c
- markus@cvs.openbsd.org 2008/05/08 06:59:01
[openssh.git] / nchan.c
CommitLineData
31652869 1/* $OpenBSD: nchan.c,v 1.57 2006/08/03 03:34:42 deraadt Exp $ */
aa3378df 2/*
8ab5f6b2 3 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
aa3378df 4 *
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.
aa3378df 13 *
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
8efc0c15 26#include "includes.h"
8efc0c15 27
5b04a8bf 28#include <sys/types.h>
29#include <sys/socket.h>
30
028094f4 31#include <errno.h>
00146caa 32#include <string.h>
31652869 33#include <stdarg.h>
028094f4 34
42f11eb2 35#include "ssh1.h"
36#include "ssh2.h"
8efc0c15 37#include "buffer.h"
38#include "packet.h"
39#include "channels.h"
7e7327a1 40#include "compat.h"
42f11eb2 41#include "log.h"
8efc0c15 42
f048f3e2 43/*
44 * SSH Protocol 1.5 aka New Channel Protocol
45 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
46 * Written by Markus Friedl in October 1999
47 *
48 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
49 * tear down of channels:
50 *
51 * 1.3: strict request-ack-protocol:
f2107e97 52 * CLOSE ->
53 * <- CLOSE_CONFIRM
f048f3e2 54 *
55 * 1.5: uses variations of:
f2107e97 56 * IEOF ->
57 * <- OCLOSE
58 * <- IEOF
59 * OCLOSE ->
60 * i.e. both sides have to close the channel
f048f3e2 61 *
62 * 2.0: the EOF messages are optional
63 *
64 * See the debugging output from 'ssh -v' and 'sshd -d' of
65 * ssh-1.2.27 as an example.
66 *
67 */
1b73841d 68
7e7327a1 69/* functions manipulating channel states */
8efc0c15 70/*
5260325f 71 * EVENTS update channel input/output states execute ACTIONS
8efc0c15 72 */
7e7327a1 73/*
74 * ACTIONS: should never update the channel states
75 */
396c147e 76static void chan_send_ieof1(Channel *);
77static void chan_send_oclose1(Channel *);
78static void chan_send_close2(Channel *);
79static void chan_send_eof2(Channel *);
7e7327a1 80
7e7327a1 81/* helper */
396c147e 82static void chan_shutdown_write(Channel *);
83static void chan_shutdown_read(Channel *);
7e7327a1 84
368e9dfc 85static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
86static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
87
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);
d7ac5f18 93 debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
368e9dfc 94 istates[next]);
95 c->istate = next;
96}
97static void
98chan_set_ostate(Channel *c, u_int next)
99{
100 if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
101 fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
d7ac5f18 102 debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
368e9dfc 103 ostates[next]);
104 c->ostate = next;
105}
106
7e7327a1 107/*
108 * SSH1 specific implementation of event functions
109 */
110
111static void
112chan_rcvd_oclose1(Channel *c)
5260325f 113{
d7ac5f18 114 debug2("channel %d: rcvd oclose", c->self);
5260325f 115 switch (c->istate) {
8efc0c15 116 case CHAN_INPUT_WAIT_OCLOSE:
368e9dfc 117 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 118 break;
119 case CHAN_INPUT_OPEN:
8efc0c15 120 chan_shutdown_read(c);
7e7327a1 121 chan_send_ieof1(c);
368e9dfc 122 chan_set_istate(c, CHAN_INPUT_CLOSED);
48e671d5 123 break;
124 case CHAN_INPUT_WAIT_DRAIN:
125 /* both local read_failed and remote write_failed */
7e7327a1 126 chan_send_ieof1(c);
368e9dfc 127 chan_set_istate(c, CHAN_INPUT_CLOSED);
8efc0c15 128 break;
129 default:
f048f3e2 130 error("channel %d: protocol error: rcvd_oclose for istate %d",
7e7327a1 131 c->self, c->istate);
48e671d5 132 return;
8efc0c15 133 }
134}
70bef40e 135void
136chan_read_failed(Channel *c)
5260325f 137{
d7ac5f18 138 debug2("channel %d: read failed", c->self);
5260325f 139 switch (c->istate) {
8efc0c15 140 case CHAN_INPUT_OPEN:
8efc0c15 141 chan_shutdown_read(c);
368e9dfc 142 chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
8efc0c15 143 break;
144 default:
f048f3e2 145 error("channel %d: chan_read_failed for istate %d",
7e7327a1 146 c->self, c->istate);
8efc0c15 147 break;
148 }
149}
70bef40e 150void
151chan_ibuf_empty(Channel *c)
5260325f 152{
d7ac5f18 153 debug2("channel %d: ibuf empty", c->self);
5260325f 154 if (buffer_len(&c->input)) {
f048f3e2 155 error("channel %d: chan_ibuf_empty for non empty buffer",
7e7327a1 156 c->self);
8efc0c15 157 return;
158 }
5260325f 159 switch (c->istate) {
8efc0c15 160 case CHAN_INPUT_WAIT_DRAIN:
3057c23b 161 if (compat20) {
162 if (!(c->flags & CHAN_CLOSE_SENT))
163 chan_send_eof2(c);
164 chan_set_istate(c, CHAN_INPUT_CLOSED);
165 } else {
166 chan_send_ieof1(c);
167 chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
168 }
8efc0c15 169 break;
170 default:
f048f3e2 171 error("channel %d: chan_ibuf_empty for istate %d",
7e7327a1 172 c->self, c->istate);
8efc0c15 173 break;
174 }
175}
7e7327a1 176static void
177chan_rcvd_ieof1(Channel *c)
5260325f 178{
d7ac5f18 179 debug2("channel %d: rcvd ieof", c->self);
5260325f 180 switch (c->ostate) {
8efc0c15 181 case CHAN_OUTPUT_OPEN:
368e9dfc 182 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 183 break;
184 case CHAN_OUTPUT_WAIT_IEOF:
368e9dfc 185 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 186 break;
187 default:
f048f3e2 188 error("channel %d: protocol error: rcvd_ieof for ostate %d",
7e7327a1 189 c->self, c->ostate);
8efc0c15 190 break;
191 }
192}
7e7327a1 193static void
194chan_write_failed1(Channel *c)
5260325f 195{
d7ac5f18 196 debug2("channel %d: write failed", c->self);
5260325f 197 switch (c->ostate) {
8efc0c15 198 case CHAN_OUTPUT_OPEN:
1e5e896c 199 chan_shutdown_write(c);
7e7327a1 200 chan_send_oclose1(c);
368e9dfc 201 chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
8efc0c15 202 break;
203 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 204 chan_shutdown_write(c);
7e7327a1 205 chan_send_oclose1(c);
368e9dfc 206 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 207 break;
208 default:
f048f3e2 209 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 210 c->self, c->ostate);
8efc0c15 211 break;
212 }
213}
70bef40e 214void
215chan_obuf_empty(Channel *c)
5260325f 216{
d7ac5f18 217 debug2("channel %d: obuf empty", c->self);
5260325f 218 if (buffer_len(&c->output)) {
f048f3e2 219 error("channel %d: chan_obuf_empty for non empty buffer",
7e7327a1 220 c->self);
8efc0c15 221 return;
222 }
5260325f 223 switch (c->ostate) {
8efc0c15 224 case CHAN_OUTPUT_WAIT_DRAIN:
1e5e896c 225 chan_shutdown_write(c);
3057c23b 226 if (!compat20)
227 chan_send_oclose1(c);
368e9dfc 228 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
8efc0c15 229 break;
230 default:
f048f3e2 231 error("channel %d: internal error: obuf_empty for ostate %d",
7e7327a1 232 c->self, c->ostate);
8efc0c15 233 break;
234 }
235}
8efc0c15 236static void
7e7327a1 237chan_send_ieof1(Channel *c)
5260325f 238{
d7ac5f18 239 debug2("channel %d: send ieof", c->self);
5260325f 240 switch (c->istate) {
8efc0c15 241 case CHAN_INPUT_OPEN:
242 case CHAN_INPUT_WAIT_DRAIN:
243 packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
244 packet_put_int(c->remote_id);
245 packet_send();
246 break;
247 default:
f048f3e2 248 error("channel %d: cannot send ieof for istate %d",
7e7327a1 249 c->self, c->istate);
8efc0c15 250 break;
251 }
252}
253static void
7e7327a1 254chan_send_oclose1(Channel *c)
5260325f 255{
d7ac5f18 256 debug2("channel %d: send oclose", c->self);
5260325f 257 switch (c->ostate) {
8efc0c15 258 case CHAN_OUTPUT_OPEN:
259 case CHAN_OUTPUT_WAIT_DRAIN:
9c50edcf 260 buffer_clear(&c->output);
8efc0c15 261 packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
262 packet_put_int(c->remote_id);
263 packet_send();
264 break;
265 default:
f048f3e2 266 error("channel %d: cannot send oclose for ostate %d",
184eed6a 267 c->self, c->ostate);
8efc0c15 268 break;
269 }
270}
5260325f 271
7e7327a1 272/*
273 * the same for SSH2
274 */
8efc0c15 275static void
668a91b7 276chan_rcvd_close2(Channel *c)
5260325f 277{
d7ac5f18 278 debug2("channel %d: rcvd close", c->self);
7e7327a1 279 if (c->flags & CHAN_CLOSE_RCVD)
280 error("channel %d: protocol error: close rcvd twice", c->self);
281 c->flags |= CHAN_CLOSE_RCVD;
282 if (c->type == SSH_CHANNEL_LARVAL) {
283 /* tear down larval channels immediately */
368e9dfc 284 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
285 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 286 return;
287 }
288 switch (c->ostate) {
289 case CHAN_OUTPUT_OPEN:
f048f3e2 290 /*
291 * wait until a data from the channel is consumed if a CLOSE
292 * is received
293 */
368e9dfc 294 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
7e7327a1 295 break;
296 }
297 switch (c->istate) {
298 case CHAN_INPUT_OPEN:
7e7327a1 299 chan_shutdown_read(c);
668a91b7 300 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 301 break;
302 case CHAN_INPUT_WAIT_DRAIN:
7e7327a1 303 chan_send_eof2(c);
668a91b7 304 chan_set_istate(c, CHAN_INPUT_CLOSED);
7e7327a1 305 break;
306 }
8efc0c15 307}
308static void
668a91b7 309chan_rcvd_eof2(Channel *c)
5260325f 310{
d7ac5f18 311 debug2("channel %d: rcvd eof", c->self);
d2296ed7 312 c->flags |= CHAN_EOF_RCVD;
368e9dfc 313 if (c->ostate == CHAN_OUTPUT_OPEN)
314 chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
8efc0c15 315}
7e7327a1 316static void
317chan_write_failed2(Channel *c)
318{
d7ac5f18 319 debug2("channel %d: write failed", c->self);
7e7327a1 320 switch (c->ostate) {
321 case CHAN_OUTPUT_OPEN:
7e7327a1 322 case CHAN_OUTPUT_WAIT_DRAIN:
7e7327a1 323 chan_shutdown_write(c);
368e9dfc 324 chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
7e7327a1 325 break;
326 default:
f048f3e2 327 error("channel %d: chan_write_failed for ostate %d",
7e7327a1 328 c->self, c->ostate);
329 break;
330 }
331}
332static void
7e7327a1 333chan_send_eof2(Channel *c)
334{
d7ac5f18 335 debug2("channel %d: send eof", c->self);
7e7327a1 336 switch (c->istate) {
337 case CHAN_INPUT_WAIT_DRAIN:
338 packet_start(SSH2_MSG_CHANNEL_EOF);
339 packet_put_int(c->remote_id);
340 packet_send();
d2296ed7 341 c->flags |= CHAN_EOF_SENT;
7e7327a1 342 break;
343 default:
f048f3e2 344 error("channel %d: cannot send eof for istate %d",
7e7327a1 345 c->self, c->istate);
346 break;
347 }
348}
349static void
350chan_send_close2(Channel *c)
351{
d7ac5f18 352 debug2("channel %d: send close", c->self);
7e7327a1 353 if (c->ostate != CHAN_OUTPUT_CLOSED ||
354 c->istate != CHAN_INPUT_CLOSED) {
f048f3e2 355 error("channel %d: cannot send close for istate/ostate %d/%d",
7e7327a1 356 c->self, c->istate, c->ostate);
357 } else if (c->flags & CHAN_CLOSE_SENT) {
f048f3e2 358 error("channel %d: already sent close", c->self);
7e7327a1 359 } else {
360 packet_start(SSH2_MSG_CHANNEL_CLOSE);
361 packet_put_int(c->remote_id);
362 packet_send();
363 c->flags |= CHAN_CLOSE_SENT;
364 }
365}
ee55dacf 366
367/* shared */
368
70bef40e 369void
370chan_rcvd_ieof(Channel *c)
371{
372 if (compat20)
373 chan_rcvd_eof2(c);
374 else
375 chan_rcvd_ieof1(c);
32e7d71f 376 if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
7203d6bb 377 buffer_len(&c->output) == 0 &&
d2296ed7 378 !CHANNEL_EFD_OUTPUT_ACTIVE(c))
32e7d71f 379 chan_obuf_empty(c);
70bef40e 380}
381void
382chan_rcvd_oclose(Channel *c)
383{
384 if (compat20)
385 chan_rcvd_close2(c);
386 else
387 chan_rcvd_oclose1(c);
388}
389void
390chan_write_failed(Channel *c)
391{
392 if (compat20)
393 chan_write_failed2(c);
394 else
395 chan_write_failed1(c);
396}
397
719fc62f 398void
399chan_mark_dead(Channel *c)
400{
6fd8622b 401 c->type = SSH_CHANNEL_ZOMBIE;
719fc62f 402}
403
ee55dacf 404int
ca75d7de 405chan_is_dead(Channel *c, int do_send)
5260325f 406{
6fd8622b 407 if (c->type == SSH_CHANNEL_ZOMBIE) {
d7ac5f18 408 debug2("channel %d: zombie", c->self);
719fc62f 409 return 1;
6fd8622b 410 }
ee55dacf 411 if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
412 return 0;
413 if (!compat20) {
d7ac5f18 414 debug2("channel %d: is dead", c->self);
ee55dacf 415 return 1;
416 }
d2296ed7 417 if ((datafellows & SSH_BUG_EXTEOF) &&
418 c->extended_usage == CHAN_EXTENDED_WRITE &&
419 c->efd != -1 &&
420 buffer_len(&c->extended) > 0) {
875ec275 421 debug2("channel %d: active efd: %d len %d",
422 c->self, c->efd, buffer_len(&c->extended));
d2296ed7 423 return 0;
424 }
425 if (!(c->flags & CHAN_CLOSE_SENT)) {
ca75d7de 426 if (do_send) {
d2296ed7 427 chan_send_close2(c);
428 } else {
429 /* channel would be dead if we sent a close */
430 if (c->flags & CHAN_CLOSE_RCVD) {
d7ac5f18 431 debug2("channel %d: almost dead",
d2296ed7 432 c->self);
433 return 1;
418e724c 434 }
7e7327a1 435 }
d2296ed7 436 }
437 if ((c->flags & CHAN_CLOSE_SENT) &&
438 (c->flags & CHAN_CLOSE_RCVD)) {
d7ac5f18 439 debug2("channel %d: is dead", c->self);
d2296ed7 440 return 1;
8efc0c15 441 }
ee55dacf 442 return 0;
8efc0c15 443}
7e7327a1 444
7e7327a1 445/* helper */
446static void
447chan_shutdown_write(Channel *c)
448{
9c50edcf 449 buffer_clear(&c->output);
7e7327a1 450 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
451 return;
452 /* shutdown failure is allowed if write failed already */
d7ac5f18 453 debug2("channel %d: close_write", c->self);
7e7327a1 454 if (c->sock != -1) {
455 if (shutdown(c->sock, SHUT_WR) < 0)
d7ac5f18 456 debug2("channel %d: chan_shutdown_write: "
f048f3e2 457 "shutdown() failed for fd%d: %.100s",
7e7327a1 458 c->self, c->sock, strerror(errno));
459 } else {
489aa2e9 460 if (channel_close_fd(&c->wfd) < 0)
bbe88b6d 461 logit("channel %d: chan_shutdown_write: "
f048f3e2 462 "close() failed for fd%d: %.100s",
7e7327a1 463 c->self, c->wfd, strerror(errno));
7e7327a1 464 }
465}
466static void
467chan_shutdown_read(Channel *c)
468{
469 if (compat20 && c->type == SSH_CHANNEL_LARVAL)
470 return;
d7ac5f18 471 debug2("channel %d: close_read", c->self);
7e7327a1 472 if (c->sock != -1) {
2b87da3b 473 /*
47670e77 474 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
475 * write side has been closed already. (bug on Linux)
f49df8e9 476 * HP-UX may return ENOTCONN also.
47670e77 477 */
478 if (shutdown(c->sock, SHUT_RD) < 0
f49df8e9 479 && errno != ENOTCONN)
f048f3e2 480 error("channel %d: chan_shutdown_read: "
481 "shutdown() failed for fd%d [i%d o%d]: %.100s",
482 c->self, c->sock, c->istate, c->ostate,
1b73841d 483 strerror(errno));
7e7327a1 484 } else {
489aa2e9 485 if (channel_close_fd(&c->rfd) < 0)
bbe88b6d 486 logit("channel %d: chan_shutdown_read: "
f048f3e2 487 "close() failed for fd%d: %.100s",
7e7327a1 488 c->self, c->rfd, strerror(errno));
7e7327a1 489 }
8efc0c15 490}
This page took 0.523684 seconds and 5 git commands to generate.