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