source: trunk/third/openssh/nchan.c @ 18763

Revision 18763, 11.8 KB checked in by zacheiss, 22 years ago (diff)
Merge openssh 3.5p1.
Line 
1/*
2 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26RCSID("$OpenBSD: nchan.c,v 1.47 2002/06/19 00:27:55 deraadt Exp $");
27
28#include "ssh1.h"
29#include "ssh2.h"
30#include "buffer.h"
31#include "packet.h"
32#include "channels.h"
33#include "compat.h"
34#include "log.h"
35
36/*
37 * SSH Protocol 1.5 aka New Channel Protocol
38 * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
39 * Written by Markus Friedl in October 1999
40 *
41 * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
42 * tear down of channels:
43 *
44 * 1.3: strict request-ack-protocol:
45 *      CLOSE   ->
46 *              <-  CLOSE_CONFIRM
47 *
48 * 1.5: uses variations of:
49 *      IEOF    ->
50 *              <-  OCLOSE
51 *              <-  IEOF
52 *      OCLOSE  ->
53 *      i.e. both sides have to close the channel
54 *
55 * 2.0: the EOF messages are optional
56 *
57 * See the debugging output from 'ssh -v' and 'sshd -d' of
58 * ssh-1.2.27 as an example.
59 *
60 */
61
62/* functions manipulating channel states */
63/*
64 * EVENTS update channel input/output states execute ACTIONS
65 */
66/*
67 * ACTIONS: should never update the channel states
68 */
69static void     chan_send_ieof1(Channel *);
70static void     chan_send_oclose1(Channel *);
71static void     chan_send_close2(Channel *);
72static void     chan_send_eof2(Channel *);
73
74/* helper */
75static void     chan_shutdown_write(Channel *);
76static void     chan_shutdown_read(Channel *);
77
78static char *ostates[] = { "open", "drain", "wait_ieof", "closed" };
79static char *istates[] = { "open", "drain", "wait_oclose", "closed" };
80
81static void
82chan_set_istate(Channel *c, u_int next)
83{
84        if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
85                fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
86        debug("channel %d: input %s -> %s", c->self, istates[c->istate],
87            istates[next]);
88        c->istate = next;
89}
90static void
91chan_set_ostate(Channel *c, u_int next)
92{
93        if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
94                fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
95        debug("channel %d: output %s -> %s", c->self, ostates[c->ostate],
96            ostates[next]);
97        c->ostate = next;
98}
99
100/*
101 * SSH1 specific implementation of event functions
102 */
103
104static void
105chan_rcvd_oclose1(Channel *c)
106{
107        debug("channel %d: rcvd oclose", c->self);
108        switch (c->istate) {
109        case CHAN_INPUT_WAIT_OCLOSE:
110                chan_set_istate(c, CHAN_INPUT_CLOSED);
111                break;
112        case CHAN_INPUT_OPEN:
113                chan_shutdown_read(c);
114                chan_send_ieof1(c);
115                chan_set_istate(c, CHAN_INPUT_CLOSED);
116                break;
117        case CHAN_INPUT_WAIT_DRAIN:
118                /* both local read_failed and remote write_failed  */
119                  if (!datafellows)
120                    chan_send_ieof1(c);
121                chan_set_istate(c, CHAN_INPUT_CLOSED);
122                break;
123        default:
124                error("channel %d: protocol error: rcvd_oclose for istate %d",
125                    c->self, c->istate);
126                return;
127        }
128}
129void
130chan_read_failed(Channel *c)
131{
132        debug("channel %d: read failed", c->self);
133        switch (c->istate) {
134        case CHAN_INPUT_OPEN:
135                chan_shutdown_read(c);
136                chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
137                break;
138        default:
139                error("channel %d: chan_read_failed for istate %d",
140                    c->self, c->istate);
141                break;
142        }
143}
144void
145chan_ibuf_empty(Channel *c)
146{
147        debug("channel %d: ibuf empty", c->self);
148        if (buffer_len(&c->input)) {
149                error("channel %d: chan_ibuf_empty for non empty buffer",
150                    c->self);
151                return;
152        }
153        switch (c->istate) {
154        case CHAN_INPUT_WAIT_DRAIN:
155                if (compat20) {
156                        if (!(c->flags & CHAN_CLOSE_SENT))
157                                chan_send_eof2(c);
158                        chan_set_istate(c, CHAN_INPUT_CLOSED);
159                } else {
160                        chan_send_ieof1(c);
161                        chan_set_istate(c, CHAN_INPUT_WAIT_OCLOSE);
162                }
163                break;
164        default:
165                error("channel %d: chan_ibuf_empty for istate %d",
166                    c->self, c->istate);
167                break;
168        }
169}
170static void
171chan_rcvd_ieof1(Channel *c)
172{
173        debug("channel %d: rcvd ieof", c->self);
174        switch (c->ostate) {
175        case CHAN_OUTPUT_OPEN:
176                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
177                break;
178        case CHAN_OUTPUT_WAIT_IEOF:
179                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
180                break;
181        default:
182                error("channel %d: protocol error: rcvd_ieof for ostate %d",
183                    c->self, c->ostate);
184                break;
185        }
186}
187static void
188chan_write_failed1(Channel *c)
189{
190        debug("channel %d: write failed", c->self);
191        switch (c->ostate) {
192        case CHAN_OUTPUT_OPEN:
193                chan_shutdown_write(c);
194                chan_send_oclose1(c);
195                chan_set_ostate(c, CHAN_OUTPUT_WAIT_IEOF);
196                break;
197        case CHAN_OUTPUT_WAIT_DRAIN:
198                chan_shutdown_write(c);
199                chan_send_oclose1(c);
200                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
201                break;
202        default:
203                error("channel %d: chan_write_failed for ostate %d",
204                    c->self, c->ostate);
205                break;
206        }
207}
208void
209chan_obuf_empty(Channel *c)
210{
211        debug("channel %d: obuf empty", c->self);
212        if (buffer_len(&c->output)) {
213                error("channel %d: chan_obuf_empty for non empty buffer",
214                    c->self);
215                return;
216        }
217        switch (c->ostate) {
218        case CHAN_OUTPUT_WAIT_DRAIN:
219                chan_shutdown_write(c);
220                if (!compat20)
221                        chan_send_oclose1(c);
222                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
223                break;
224        default:
225                error("channel %d: internal error: obuf_empty for ostate %d",
226                    c->self, c->ostate);
227                break;
228        }
229}
230static void
231chan_send_ieof1(Channel *c)
232{
233        debug("channel %d: send ieof", c->self);
234        switch (c->istate) {
235        case CHAN_INPUT_OPEN:
236        case CHAN_INPUT_WAIT_DRAIN:
237                packet_start(SSH_MSG_CHANNEL_INPUT_EOF);
238                packet_put_int(c->remote_id);
239                packet_send();
240                break;
241        default:
242                error("channel %d: cannot send ieof for istate %d",
243                    c->self, c->istate);
244                break;
245        }
246}
247static void
248chan_send_oclose1(Channel *c)
249{
250        debug("channel %d: send oclose", c->self);
251        switch (c->ostate) {
252        case CHAN_OUTPUT_OPEN:
253        case CHAN_OUTPUT_WAIT_DRAIN:
254                buffer_clear(&c->output);
255                packet_start(SSH_MSG_CHANNEL_OUTPUT_CLOSE);
256                packet_put_int(c->remote_id);
257                packet_send();
258                break;
259        default:
260                error("channel %d: cannot send oclose for ostate %d",
261                    c->self, c->ostate);
262                break;
263        }
264}
265
266/*
267 * the same for SSH2
268 */
269static void
270chan_rcvd_close2(Channel *c)
271{
272        debug("channel %d: rcvd close", c->self);
273        if (c->flags & CHAN_CLOSE_RCVD)
274                error("channel %d: protocol error: close rcvd twice", c->self);
275        c->flags |= CHAN_CLOSE_RCVD;
276        if (c->type == SSH_CHANNEL_LARVAL) {
277                /* tear down larval channels immediately */
278                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
279                chan_set_istate(c, CHAN_INPUT_CLOSED);
280                return;
281        }
282        switch (c->ostate) {
283        case CHAN_OUTPUT_OPEN:
284                /*
285                 * wait until a data from the channel is consumed if a CLOSE
286                 * is received
287                 */
288                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
289                break;
290        }
291        switch (c->istate) {
292        case CHAN_INPUT_OPEN:
293                chan_shutdown_read(c);
294                chan_set_istate(c, CHAN_INPUT_CLOSED);
295                break;
296        case CHAN_INPUT_WAIT_DRAIN:
297                chan_send_eof2(c);
298                chan_set_istate(c, CHAN_INPUT_CLOSED);
299                break;
300        }
301}
302static void
303chan_rcvd_eof2(Channel *c)
304{
305        debug("channel %d: rcvd eof", c->self);
306        c->flags |= CHAN_EOF_RCVD;
307        if (c->ostate == CHAN_OUTPUT_OPEN)
308                chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
309}
310static void
311chan_write_failed2(Channel *c)
312{
313        debug("channel %d: write failed", c->self);
314        switch (c->ostate) {
315        case CHAN_OUTPUT_OPEN:
316        case CHAN_OUTPUT_WAIT_DRAIN:
317                chan_shutdown_write(c);
318                chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
319                break;
320        default:
321                error("channel %d: chan_write_failed for ostate %d",
322                    c->self, c->ostate);
323                break;
324        }
325}
326static void
327chan_send_eof2(Channel *c)
328{
329        debug("channel %d: send eof", c->self);
330        switch (c->istate) {
331        case CHAN_INPUT_WAIT_DRAIN:
332                packet_start(SSH2_MSG_CHANNEL_EOF);
333                packet_put_int(c->remote_id);
334                packet_send();
335                c->flags |= CHAN_EOF_SENT;
336                break;
337        default:
338                error("channel %d: cannot send eof for istate %d",
339                    c->self, c->istate);
340                break;
341        }
342}
343static void
344chan_send_close2(Channel *c)
345{
346        debug("channel %d: send close", c->self);
347        if (c->ostate != CHAN_OUTPUT_CLOSED ||
348            c->istate != CHAN_INPUT_CLOSED) {
349                error("channel %d: cannot send close for istate/ostate %d/%d",
350                    c->self, c->istate, c->ostate);
351        } else if (c->flags & CHAN_CLOSE_SENT) {
352                error("channel %d: already sent close", c->self);
353        } else {
354                packet_start(SSH2_MSG_CHANNEL_CLOSE);
355                packet_put_int(c->remote_id);
356                packet_send();
357                c->flags |= CHAN_CLOSE_SENT;
358        }
359}
360
361/* shared */
362
363void
364chan_rcvd_ieof(Channel *c)
365{
366        if (compat20)
367                chan_rcvd_eof2(c);
368        else
369                chan_rcvd_ieof1(c);
370        if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
371            buffer_len(&c->output) == 0 &&
372            !CHANNEL_EFD_OUTPUT_ACTIVE(c))
373                chan_obuf_empty(c);
374}
375void
376chan_rcvd_oclose(Channel *c)
377{
378        if (compat20)
379                chan_rcvd_close2(c);
380        else
381                chan_rcvd_oclose1(c);
382}
383void
384chan_write_failed(Channel *c)
385{
386        if (compat20)
387                chan_write_failed2(c);
388        else
389                chan_write_failed1(c);
390}
391
392void
393chan_mark_dead(Channel *c)
394{
395        c->type = SSH_CHANNEL_ZOMBIE;
396}
397
398int
399chan_is_dead(Channel *c, int send)
400{
401        if (c->type == SSH_CHANNEL_ZOMBIE) {
402                debug("channel %d: zombie", c->self);
403                return 1;
404        }
405        if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
406                return 0;
407        if (!compat20) {
408                debug("channel %d: is dead", c->self);
409                return 1;
410        }
411        if ((datafellows & SSH_BUG_EXTEOF) &&
412            c->extended_usage == CHAN_EXTENDED_WRITE &&
413            c->efd != -1 &&
414            buffer_len(&c->extended) > 0) {
415                debug2("channel %d: active efd: %d len %d",
416                    c->self, c->efd, buffer_len(&c->extended));
417                return 0;
418        }
419        if (!(c->flags & CHAN_CLOSE_SENT)) {
420                if (send) {
421                        chan_send_close2(c);
422                } else {
423                        /* channel would be dead if we sent a close */
424                        if (c->flags & CHAN_CLOSE_RCVD) {
425                                debug("channel %d: almost dead",
426                                    c->self);
427                                return 1;
428                        }
429                }
430        }
431        if ((c->flags & CHAN_CLOSE_SENT) &&
432            (c->flags & CHAN_CLOSE_RCVD)) {
433                debug("channel %d: is dead", c->self);
434                return 1;
435        }
436        return 0;
437}
438
439/* helper */
440static void
441chan_shutdown_write(Channel *c)
442{
443        buffer_clear(&c->output);
444        if (compat20 && c->type == SSH_CHANNEL_LARVAL)
445                return;
446        /* shutdown failure is allowed if write failed already */
447        debug("channel %d: close_write", c->self);
448        if (c->sock != -1) {
449                if (shutdown(c->sock, SHUT_WR) < 0)
450                        debug("channel %d: chan_shutdown_write: "
451                            "shutdown() failed for fd%d: %.100s",
452                            c->self, c->sock, strerror(errno));
453        } else {
454                if (channel_close_fd(&c->wfd) < 0)
455                        log("channel %d: chan_shutdown_write: "
456                            "close() failed for fd%d: %.100s",
457                            c->self, c->wfd, strerror(errno));
458        }
459}
460static void
461chan_shutdown_read(Channel *c)
462{
463        if (compat20 && c->type == SSH_CHANNEL_LARVAL)
464                return;
465        debug("channel %d: close_read", c->self);
466        if (c->sock != -1) {
467                /*
468                 * shutdown(sock, SHUT_READ) may return ENOTCONN if the
469                 * write side has been closed already. (bug on Linux)
470                 * HP-UX may return ENOTCONN also.
471                 */
472                if (shutdown(c->sock, SHUT_RD) < 0
473                    && errno != ENOTCONN)
474                        error("channel %d: chan_shutdown_read: "
475                            "shutdown() failed for fd%d [i%d o%d]: %.100s",
476                            c->self, c->sock, c->istate, c->ostate,
477                            strerror(errno));
478        } else {
479                if (channel_close_fd(&c->rfd) < 0)
480                        log("channel %d: chan_shutdown_read: "
481                            "close() failed for fd%d: %.100s",
482                            c->self, c->rfd, strerror(errno));
483        }
484}
Note: See TracBrowser for help on using the repository browser.