4
* Copyright (c) 2002-2004 Matt Johnston
7
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
* of this software and associated documentation files (the "Software"), to deal
9
* in the Software without restriction, including without limitation the rights
10
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
* copies of the Software, and to permit persons to whom the Software is
12
* furnished to do so, subject to the following conditions:
14
* The above copyright notice and this permission notice shall be included in
15
* all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
/* Handle the multiplexed channels, such as sessions, x11, agent connections */
32
#include "circbuffer.h"
38
static void send_msg_channel_open_failure(unsigned int remotechan, int reason,
39
const unsigned char *text, const unsigned char *lang);
40
static void send_msg_channel_open_confirmation(struct Channel* channel,
41
unsigned int recvwindow,
42
unsigned int recvmaxpacket);
43
static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf);
44
static void send_msg_channel_window_adjust(struct Channel *channel,
46
static void send_msg_channel_data(struct Channel *channel, int isextended,
47
unsigned int exttype);
48
static void send_msg_channel_eof(struct Channel *channel);
49
static void send_msg_channel_close(struct Channel *channel);
50
static void removechannel(struct Channel *channel);
51
static void deletechannel(struct Channel *channel);
52
static void checkinitdone(struct Channel *channel);
53
static void checkclose(struct Channel *channel);
55
static void closeinfd(struct Channel * channel);
56
static void closeoutfd(struct Channel * channel, int fd);
57
static void closechanfd(struct Channel *channel, int fd, int how);
59
#define FD_UNINIT (-2)
60
#define FD_CLOSED (-1)
62
/* Initialise all the channels */
63
void chaninitialise(const struct ChanType *chantypes[]) {
65
/* may as well create space for a single channel */
66
ses.channels = (struct Channel**)m_malloc(sizeof(struct Channel*));
68
ses.channels[0] = NULL;
71
ses.chantypes = chantypes;
73
#ifdef USING_LISTENERS
74
listeners_initialise();
79
/* Clean up channels, freeing allocated memory */
84
TRACE(("enter chancleanup"))
85
for (i = 0; i < ses.chansize; i++) {
86
if (ses.channels[i] != NULL) {
87
TRACE(("channel %d closing", i))
88
removechannel(ses.channels[i]);
92
TRACE(("leave chancleanup"))
95
/* Create a new channel entry, send a reply confirm or failure */
96
/* If remotechan, transwindow and transmaxpacket are not know (for a new
97
* outgoing connection, with them to be filled on confirmation), they should
99
struct Channel* newchannel(unsigned int remotechan,
100
const struct ChanType *type,
101
unsigned int transwindow, unsigned int transmaxpacket) {
103
struct Channel * newchan;
106
TRACE(("enter newchannel"))
108
/* first see if we can use existing channels */
109
for (i = 0; i < ses.chansize; i++) {
110
if (ses.channels[i] == NULL) {
115
/* otherwise extend the list */
116
if (i == ses.chansize) {
117
if (ses.chansize >= MAX_CHANNELS) {
118
TRACE(("leave newchannel: max chans reached"))
122
/* extend the channels */
123
ses.channels = (struct Channel**)m_realloc(ses.channels,
124
(ses.chansize+CHAN_EXTEND_SIZE)*sizeof(struct Channel*));
126
ses.chansize += CHAN_EXTEND_SIZE;
128
/* set the new channels to null */
129
for (j = i; j < ses.chansize; j++) {
130
ses.channels[j] = NULL;
135
newchan = (struct Channel*)m_malloc(sizeof(struct Channel));
136
newchan->type = type;
138
newchan->sentclosed = newchan->recvclosed = 0;
139
newchan->senteof = newchan->recveof = 0;
141
newchan->remotechan = remotechan;
142
newchan->transwindow = transwindow;
143
newchan->transmaxpacket = transmaxpacket;
145
newchan->typedata = NULL;
146
newchan->infd = FD_UNINIT;
147
newchan->outfd = FD_UNINIT;
148
newchan->errfd = FD_CLOSED; /* this isn't always set to start with */
149
newchan->initconn = 0;
151
newchan->writebuf = cbuf_new(RECV_MAXWINDOW);
152
newchan->extrabuf = NULL; /* The user code can set it up */
153
newchan->recvwindow = RECV_MAXWINDOW;
154
newchan->recvdonelen = 0;
155
newchan->recvmaxpacket = RECV_MAXPACKET;
157
ses.channels[i] = newchan;
160
TRACE(("leave newchannel"))
165
/* Get the channel structure corresponding to a channel number */
166
struct Channel* getchannel(unsigned int chan) {
167
if (chan >= ses.chansize || ses.channels[chan] == NULL) {
170
return ses.channels[chan];
173
/* Iterate through the channels, performing IO if available */
174
void channelio(fd_set *readfd, fd_set *writefd) {
176
struct Channel *channel;
180
/* iterate through all the possible channels */
181
for (i = 0; i < ses.chansize; i++) {
183
channel = ses.channels[i];
184
if (channel == NULL) {
185
/* only process in-use channels */
189
/* read from program/pipe stdout */
190
if (channel->outfd >= 0 && FD_ISSET(channel->outfd, readfd)) {
191
send_msg_channel_data(channel, 0, 0);
194
/* read from program/pipe stderr */
195
if (channel->extrabuf == NULL &&
196
channel->errfd >= 0 && FD_ISSET(channel->errfd, readfd)) {
197
send_msg_channel_data(channel, 1, SSH_EXTENDED_DATA_STDERR);
200
/* if we can read from the infd, it might be closed, so we try to
201
* see if it has errors */
202
if (channel->infd >= 0 && channel->infd != channel->outfd
203
&& FD_ISSET(channel->infd, readfd)) {
204
if (channel->initconn) {
205
/* Handling for "in progress" connection - this is needed
206
* to avoid spinning 100% CPU when we connect to a server
207
* which doesn't send anything (tcpfwding) */
208
checkinitdone(channel);
209
continue; /* Important not to use the channel after
210
checkinitdone(), as it may be NULL */
212
ret = write(channel->infd, NULL, 0); /* Fake write */
213
if (ret < 0 && errno != EINTR && errno != EAGAIN) {
218
/* write to program/pipe stdin */
219
if (channel->infd >= 0 && FD_ISSET(channel->infd, writefd)) {
220
if (channel->initconn) {
221
checkinitdone(channel);
222
continue; /* Important not to use the channel after
223
checkinitdone(), as it may be NULL */
225
writechannel(channel, channel->infd, channel->writebuf);
228
/* stderr for client mode */
229
if (channel->extrabuf != NULL
230
&& channel->errfd >= 0 && FD_ISSET(channel->errfd, writefd)) {
231
writechannel(channel, channel->errfd, channel->extrabuf);
234
/* now handle any of the channel-closing type stuff */
237
} /* foreach channel */
239
/* Listeners such as TCP, X11, agent-auth */
240
#ifdef USING_LISTENERS
241
handle_listeners(readfd);
246
/* do all the EOF/close type stuff checking for a channel */
247
static void checkclose(struct Channel *channel) {
249
TRACE(("checkclose: infd %d, outfd %d, errfd %d, sentclosed %d, recvclosed %d",
250
channel->infd, channel->outfd,
251
channel->errfd, channel->sentclosed, channel->recvclosed))
252
TRACE(("writebuf %d extrabuf %s extrabuf %d",
253
cbuf_getused(channel->writebuf),
255
channel->writebuf ? 0 : cbuf_getused(channel->extrabuf)))
257
if (!channel->sentclosed) {
259
/* check for exited - currently only used for server sessions,
260
* if the shell has exited etc */
261
if (channel->type->checkclose) {
262
if (channel->type->checkclose(channel)) {
267
if (!channel->senteof
268
&& channel->outfd == FD_CLOSED
269
&& (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) {
270
send_msg_channel_eof(channel);
273
if (channel->infd == FD_CLOSED
274
&& channel->outfd == FD_CLOSED
275
&& (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) {
276
send_msg_channel_close(channel);
280
/* When either party wishes to terminate the channel, it sends
281
* SSH_MSG_CHANNEL_CLOSE. Upon receiving this message, a party MUST
282
* send back a SSH_MSG_CHANNEL_CLOSE unless it has already sent this
283
* message for the channel. The channel is considered closed for a
284
* party when it has both sent and received SSH_MSG_CHANNEL_CLOSE, and
285
* the party may then reuse the channel number. A party MAY send
286
* SSH_MSG_CHANNEL_CLOSE without having sent or received
287
* SSH_MSG_CHANNEL_EOF.
288
* (from draft-ietf-secsh-connect)
290
if (channel->recvclosed) {
291
if (! channel->sentclosed) {
292
TRACE(("Sending MSG_CHANNEL_CLOSE in response to same."))
293
send_msg_channel_close(channel);
295
removechannel(channel);
300
/* Check whether a deferred (EINPROGRESS) connect() was successful, and
301
* if so, set up the channel properly. Otherwise, the channel is cleaned up, so
302
* it is important that the channel reference isn't used after a call to this
304
static void checkinitdone(struct Channel *channel) {
307
socklen_t vallen = sizeof(val);
309
TRACE(("enter checkinitdone"))
311
if (getsockopt(channel->infd, SOL_SOCKET, SO_ERROR, &val, &vallen)
313
send_msg_channel_open_failure(channel->remotechan,
314
SSH_OPEN_CONNECT_FAILED, "", "");
315
close(channel->infd);
316
deletechannel(channel);
317
TRACE(("leave checkinitdone: fail"))
319
send_msg_channel_open_confirmation(channel, channel->recvwindow,
320
channel->recvmaxpacket);
321
channel->outfd = channel->infd;
322
channel->initconn = 0;
323
TRACE(("leave checkinitdone: success"))
329
/* Send the close message and set the channel as closed */
330
static void send_msg_channel_close(struct Channel *channel) {
332
TRACE(("enter send_msg_channel_close"))
334
if (channel->type->closehandler) {
335
channel->type->closehandler(channel);
340
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_CLOSE);
341
buf_putint(ses.writepayload, channel->remotechan);
345
channel->senteof = 1;
346
channel->sentclosed = 1;
347
TRACE(("leave send_msg_channel_close"))
350
/* call this when trans/eof channels are closed */
351
static void send_msg_channel_eof(struct Channel *channel) {
353
TRACE(("enter send_msg_channel_eof"))
356
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_EOF);
357
buf_putint(ses.writepayload, channel->remotechan);
361
channel->senteof = 1;
363
TRACE(("leave send_msg_channel_eof"))
366
/* Called to write data out to the local side of the channel.
367
* Only called when we know we can write to a channel, writes as much as
369
static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) {
373
TRACE(("enter writechannel"))
375
maxlen = cbuf_readlen(cbuf);
377
/* Write the data out */
378
len = write(fd, cbuf_readptr(cbuf, maxlen), maxlen);
380
if (len < 0 && errno != EINTR) {
381
/* no more to write - we close it even if the fd was stderr, since
382
* that's a nasty failure too */
385
TRACE(("leave writechannel: len <= 0"))
389
cbuf_incrread(cbuf, len);
390
channel->recvdonelen += len;
392
if (fd == channel->infd && len == maxlen && channel->recveof) {
393
/* Check if we're closing up */
395
TRACE(("leave writechannel: recveof set"))
399
/* Window adjust handling */
400
if (channel->recvdonelen >= RECV_WINDOWEXTEND) {
401
/* Set it back to max window */
402
send_msg_channel_window_adjust(channel, channel->recvdonelen);
403
channel->recvwindow += channel->recvdonelen;
404
channel->recvdonelen = 0;
407
assert(channel->recvwindow <= RECV_MAXWINDOW);
408
assert(channel->recvwindow <= cbuf_getavail(channel->writebuf));
409
assert(channel->extrabuf == NULL ||
410
channel->recvwindow <= cbuf_getavail(channel->extrabuf));
413
TRACE(("leave writechannel"))
416
/* Set the file descriptors for the main select in session.c
417
* This avoid channels which don't have any window available, are closed, etc*/
418
void setchannelfds(fd_set *readfd, fd_set *writefd) {
421
struct Channel * channel;
423
for (i = 0; i < ses.chansize; i++) {
425
channel = ses.channels[i];
426
if (channel == NULL) {
430
/* Stuff to put over the wire */
431
if (channel->transwindow > 0) {
433
if (channel->outfd >= 0) {
434
FD_SET(channel->outfd, readfd);
437
if (channel->extrabuf == NULL && channel->errfd >= 0) {
438
FD_SET(channel->errfd, readfd);
442
/* For checking FD status (ie closure etc) - we don't actually
443
* read data from infd */
444
TRACE(("infd = %d, outfd %d, errfd %d, bufused %d",
445
channel->infd, channel->outfd,
447
cbuf_getused(channel->writebuf) ))
448
if (channel->infd >= 0 && channel->infd != channel->outfd) {
449
FD_SET(channel->infd, readfd);
452
/* Stuff from the wire, to local program/shell/user etc */
453
if ((channel->infd >= 0 && cbuf_getused(channel->writebuf) > 0 )
454
|| channel->initconn) {
456
FD_SET(channel->infd, writefd);
459
if (channel->extrabuf != NULL && channel->errfd >= 0
460
&& cbuf_getused(channel->extrabuf) > 0 ) {
461
FD_SET(channel->errfd, writefd);
464
} /* foreach channel */
466
#ifdef USING_LISTENERS
467
set_listener_fds(readfd);
472
/* handle the channel EOF event, by closing the channel filedescriptor. The
473
* channel isn't closed yet, it is left until the incoming (from the program
474
* etc) FD is also EOF */
475
void recv_msg_channel_eof() {
478
struct Channel * channel;
480
TRACE(("enter recv_msg_channel_eof"))
482
chan = buf_getint(ses.payload);
483
channel = getchannel(chan);
485
if (channel == NULL) {
486
dropbear_exit("EOF for unknown channel");
489
channel->recveof = 1;
490
if (cbuf_getused(channel->writebuf) == 0
491
&& (channel->extrabuf == NULL
492
|| cbuf_getused(channel->extrabuf) == 0)) {
496
TRACE(("leave recv_msg_channel_eof"))
500
/* Handle channel closure(), respond in kind and close the channels */
501
void recv_msg_channel_close() {
504
struct Channel * channel;
506
TRACE(("enter recv_msg_channel_close"))
508
chan = buf_getint(ses.payload);
509
TRACE(("close channel = %d", chan))
510
channel = getchannel(chan);
512
if (channel == NULL) {
514
dropbear_exit("Close for unknown channel");
517
channel->recveof = 1;
518
channel->recvclosed = 1;
520
if (channel->sentclosed) {
521
removechannel(channel);
524
TRACE(("leave recv_msg_channel_close"))
527
/* Remove a channel entry, this is only executed after both sides have sent
529
static void removechannel(struct Channel * channel) {
531
TRACE(("enter removechannel"))
532
TRACE(("channel index is %d", channel->index))
534
cbuf_free(channel->writebuf);
535
channel->writebuf = NULL;
537
if (channel->extrabuf) {
538
cbuf_free(channel->extrabuf);
539
channel->extrabuf = NULL;
543
/* close the FDs in case they haven't been done
544
* yet (ie they were shutdown etc */
545
close(channel->infd);
546
close(channel->outfd);
547
close(channel->errfd);
549
channel->typedata = NULL;
551
deletechannel(channel);
553
TRACE(("leave removechannel"))
556
/* Remove a channel entry */
557
static void deletechannel(struct Channel *channel) {
559
ses.channels[channel->index] = NULL;
566
/* Handle channel specific requests, passing off to corresponding handlers
567
* such as chansession or x11fwd */
568
void recv_msg_channel_request() {
571
struct Channel *channel;
573
TRACE(("enter recv_msg_channel_request"))
575
chan = buf_getint(ses.payload);
576
channel = getchannel(chan);
578
if (channel == NULL) {
580
dropbear_exit("Unknown channel");
583
if (channel->type->reqhandler) {
584
channel->type->reqhandler(channel);
586
send_msg_channel_failure(channel);
589
TRACE(("leave recv_msg_channel_request"))
593
/* Reads data from the server's program/shell/etc, and puts it in a
594
* channel_data packet to send.
595
* chan is the remote channel, isextended is 0 if it is normal data, 1
596
* if it is extended data. if it is extended, then the type is in
598
static void send_msg_channel_data(struct Channel *channel, int isextended,
599
unsigned int exttype) {
606
/* TRACE(("enter send_msg_channel_data"))
607
TRACE(("extended = %d type = %d", isextended, exttype))*/
611
assert(!channel->sentclosed);
620
maxlen = MIN(channel->transwindow, channel->transmaxpacket);
621
/* -(1+4+4) is SSH_MSG_CHANNEL_DATA, channel number, string length, and
622
* exttype if is extended */
624
ses.writepayload->size - 1 - 4 - 4 - (isextended ? 4 : 0));
626
TRACE(("leave send_msg_channel_data: no window"))
627
return; /* the data will get written later */
631
TRACE(("maxlen %d", maxlen))
632
buf = buf_new(maxlen);
633
TRACE(("buf pos %d data %x", buf->pos, buf->data))
634
len = read(fd, buf_getwriteptr(buf, maxlen), maxlen);
636
/* on error/eof, send eof */
637
if (len == 0 || errno != EINTR) {
638
closeoutfd(channel, fd);
642
TRACE(("leave send_msg_channel_data: read err or EOF for fd %d",
646
buf_incrlen(buf, len);
648
buf_putbyte(ses.writepayload,
649
isextended ? SSH_MSG_CHANNEL_EXTENDED_DATA : SSH_MSG_CHANNEL_DATA);
650
buf_putint(ses.writepayload, channel->remotechan);
653
buf_putint(ses.writepayload, exttype);
656
buf_putstring(ses.writepayload, buf_getptr(buf, len), len);
660
channel->transwindow -= len;
663
TRACE(("leave send_msg_channel_data"))
666
/* We receive channel data */
667
void recv_msg_channel_data() {
670
struct Channel *channel;
672
chan = buf_getint(ses.payload);
673
channel = getchannel(chan);
675
if (channel == NULL) {
676
dropbear_exit("Unknown channel");
679
common_recv_msg_channel_data(channel, channel->infd, channel->writebuf);
682
/* Shared for data and stderr data - when we receive data, put it in a buffer
683
* for writing to the local file descriptor */
684
void common_recv_msg_channel_data(struct Channel *channel, int fd,
687
unsigned int datalen;
688
unsigned int maxdata;
692
TRACE(("enter recv_msg_channel_data"))
694
if (channel->recveof) {
695
dropbear_exit("received data after eof");
699
dropbear_exit("received data with bad infd");
702
datalen = buf_getint(ses.payload);
705
maxdata = cbuf_getavail(cbuf);
707
/* Whilst the spec says we "MAY ignore data past the end" this could
708
* lead to corrupted file transfers etc (chunks missed etc). It's better to
709
* just die horribly */
710
if (datalen > maxdata) {
711
dropbear_exit("Oversized packet");
714
/* We may have to run throught twice, if the buffer wraps around. Can't
715
* just "leave it for next time" like with writechannel, since this
719
buflen = cbuf_writelen(cbuf);
720
buflen = MIN(buflen, len);
722
memcpy(cbuf_writeptr(cbuf, buflen),
723
buf_getptr(ses.payload, buflen), buflen);
724
cbuf_incrwrite(cbuf, buflen);
725
buf_incrpos(ses.payload, buflen);
729
assert(channel->recvwindow >= datalen);
730
channel->recvwindow -= datalen;
731
assert(channel->recvwindow <= RECV_MAXWINDOW);
733
TRACE(("leave recv_msg_channel_data"))
736
/* Increment the outgoing data window for a channel - the remote end limits
737
* the amount of data which may be transmitted, this window is decremented
738
* as data is sent, and incremented upon receiving window-adjust messages */
739
void recv_msg_channel_window_adjust() {
742
struct Channel * channel;
745
chan = buf_getint(ses.payload);
746
channel = getchannel(chan);
748
if (channel == NULL) {
749
dropbear_exit("Unknown channel");
752
incr = buf_getint(ses.payload);
753
TRACE(("received window increment %d", incr))
754
incr = MIN(incr, MAX_TRANS_WIN_INCR);
756
channel->transwindow += incr;
757
channel->transwindow = MIN(channel->transwindow, MAX_TRANS_WINDOW);
761
/* Increment the incoming data window for a channel, and let the remote
763
static void send_msg_channel_window_adjust(struct Channel* channel,
766
TRACE(("sending window adjust %d", incr))
769
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_WINDOW_ADJUST);
770
buf_putint(ses.writepayload, channel->remotechan);
771
buf_putint(ses.writepayload, incr);
776
/* Handle a new channel request, performing any channel-type-specific setup */
778
void recv_msg_channel_open() {
781
unsigned int typelen;
782
unsigned int remotechan, transwindow, transmaxpacket;
783
struct Channel *channel;
784
const struct ChanType **cp;
785
const struct ChanType *chantype;
786
unsigned int errtype = SSH_OPEN_UNKNOWN_CHANNEL_TYPE;
790
TRACE(("enter recv_msg_channel_open"))
792
/* get the packet contents */
793
type = buf_getstring(ses.payload, &typelen);
795
remotechan = buf_getint(ses.payload);
796
transwindow = buf_getint(ses.payload);
797
transwindow = MIN(transwindow, MAX_TRANS_WINDOW);
798
transmaxpacket = buf_getint(ses.payload);
799
transmaxpacket = MIN(transmaxpacket, MAX_TRANS_PAYLOAD_LEN);
801
/* figure what type of packet it is */
802
if (typelen > MAX_NAME_LEN) {
806
/* Get the channel type. Client and server style invokation will set up a
807
* different list for ses.chantypes at startup. We just iterate through
808
* this list and find the matching name */
809
for (cp = &ses.chantypes[0], chantype = (*cp);
811
cp++, chantype = (*cp)) {
812
if (strcmp(type, chantype->name) == 0) {
817
if (chantype == NULL) {
818
TRACE(("No matching type for '%s'", type))
822
TRACE(("matched type '%s'", type))
824
/* create the channel */
825
channel = newchannel(remotechan, chantype, transwindow, transmaxpacket);
827
if (channel == NULL) {
828
TRACE(("newchannel returned NULL"))
832
if (channel->type->inithandler) {
833
ret = channel->type->inithandler(channel);
835
if (ret == SSH_OPEN_IN_PROGRESS) {
836
/* We'll send the confirmation later */
840
deletechannel(channel);
841
TRACE(("inithandler returned failure %d", ret))
847
send_msg_channel_open_confirmation(channel, channel->recvwindow,
848
channel->recvmaxpacket);
852
TRACE(("recv_msg_channel_open failure"))
853
send_msg_channel_open_failure(remotechan, errtype, "", "");
858
TRACE(("leave recv_msg_channel_open"))
861
/* Send a failure message */
862
void send_msg_channel_failure(struct Channel *channel) {
864
TRACE(("enter send_msg_channel_failure"))
867
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_FAILURE);
868
buf_putint(ses.writepayload, channel->remotechan);
871
TRACE(("leave send_msg_channel_failure"))
874
/* Send a success message */
875
void send_msg_channel_success(struct Channel *channel) {
877
TRACE(("enter send_msg_channel_success"))
880
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_SUCCESS);
881
buf_putint(ses.writepayload, channel->remotechan);
884
TRACE(("leave send_msg_channel_success"))
887
/* Send a channel open failure message, with a corresponding reason
888
* code (usually resource shortage or unknown chan type) */
889
static void send_msg_channel_open_failure(unsigned int remotechan,
890
int reason, const unsigned char *text, const unsigned char *lang) {
892
TRACE(("enter send_msg_channel_open_failure"))
895
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_FAILURE);
896
buf_putint(ses.writepayload, remotechan);
897
buf_putint(ses.writepayload, reason);
898
buf_putstring(ses.writepayload, text, strlen((char*)text));
899
buf_putstring(ses.writepayload, lang, strlen((char*)lang));
902
TRACE(("leave send_msg_channel_open_failure"))
905
/* Confirm a channel open, and let the remote end know what number we've
906
* allocated and the receive parameters */
907
static void send_msg_channel_open_confirmation(struct Channel* channel,
908
unsigned int recvwindow,
909
unsigned int recvmaxpacket) {
911
TRACE(("enter send_msg_channel_open_confirmation"))
914
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
915
buf_putint(ses.writepayload, channel->remotechan);
916
buf_putint(ses.writepayload, channel->index);
917
buf_putint(ses.writepayload, recvwindow);
918
buf_putint(ses.writepayload, recvmaxpacket);
921
TRACE(("leave send_msg_channel_open_confirmation"))
924
#if defined(USING_LISTENERS) || defined(DROPBEAR_CLIENT)
925
/* Create a new channel, and start the open request. This is intended
926
* for X11, agent, tcp forwarding, and should be filled with channel-specific
927
* options, with the calling function calling encrypt_packet() after
928
* completion. It is mandatory for the caller to encrypt_packet() if
929
* DROPBEAR_SUCCESS is returned */
930
int send_msg_channel_open_init(int fd, const struct ChanType *type) {
932
struct Channel* chan;
934
TRACE(("enter send_msg_channel_open_init()"))
935
chan = newchannel(0, type, 0, 0);
937
TRACE(("leave send_msg_channel_open_init() - FAILED in newchannel()"))
938
return DROPBEAR_FAILURE;
941
/* set fd non-blocking */
944
chan->infd = chan->outfd = fd;
945
ses.maxfd = MAX(ses.maxfd, fd);
947
/* now open the channel connection */
950
buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN);
951
buf_putstring(ses.writepayload, type->name, strlen(type->name));
952
buf_putint(ses.writepayload, chan->index);
953
buf_putint(ses.writepayload, RECV_MAXWINDOW);
954
buf_putint(ses.writepayload, RECV_MAXPACKET);
956
TRACE(("leave send_msg_channel_open_init()"))
957
return DROPBEAR_SUCCESS;
960
/* Confirmation that our channel open request (for forwardings) was
962
void recv_msg_channel_open_confirmation() {
965
struct Channel * channel;
968
TRACE(("enter recv_msg_channel_open_confirmation"))
969
chan = buf_getint(ses.payload);
971
channel = getchannel(chan);
972
if (channel == NULL) {
973
dropbear_exit("Unknown channel");
976
channel->remotechan = buf_getint(ses.payload);
977
channel->transwindow = buf_getint(ses.payload);
978
channel->transmaxpacket = buf_getint(ses.payload);
980
TRACE(("new chan remote %d localho %d", channel->remotechan, chan))
982
/* Run the inithandler callback */
983
if (channel->type->inithandler) {
984
ret = channel->type->inithandler(channel);
986
removechannel(channel);
987
TRACE(("inithandler returned failure %d", ret))
992
TRACE(("leave recv_msg_channel_open_confirmation"))
995
/* Notification that our channel open request failed */
996
void recv_msg_channel_open_failure() {
999
struct Channel * channel;
1000
chan = buf_getbyte(ses.payload);
1002
channel = getchannel(chan);
1003
if (channel == NULL) {
1004
dropbear_exit("Unknown channel");
1007
removechannel(channel);
1009
#endif /* USING_LISTENERS */
1011
/* close a stdout/stderr fd */
1012
static void closeoutfd(struct Channel * channel, int fd) {
1014
/* don't close it if it is the same as infd,
1015
* unless infd is already set -1 */
1016
TRACE(("enter closeoutfd"))
1017
closechanfd(channel, fd, 0);
1018
TRACE(("leave closeoutfd"))
1021
/* close a stdin fd */
1022
static void closeinfd(struct Channel * channel) {
1024
TRACE(("enter closeinfd"))
1025
closechanfd(channel, channel->infd, 1);
1026
TRACE(("leave closeinfd"))
1029
/* close a fd, how is 0 for stdout/stderr, 1 for stdin */
1030
static void closechanfd(struct Channel *channel, int fd, int how) {
1032
int closein = 0, closeout = 0;
1035
if (channel->type->sepfds) {
1044
closein = closeout = 1;
1047
if (closeout && fd == channel->outfd) {
1048
channel->outfd = FD_CLOSED;
1050
if (closeout && (channel->extrabuf == NULL) && (fd == channel->errfd)) {
1051
channel->errfd = FD_CLOSED;
1054
if (closein && fd == channel->infd) {
1055
channel->infd = FD_CLOSED;
1057
if (closein && (channel->extrabuf != NULL) && (fd == channel->errfd)) {
1058
channel->errfd = FD_CLOSED;