1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252 |
- #include "includes.h"
- #include "session.h"
- #include "packet.h"
- #include "ssh.h"
- #include "buffer.h"
- #include "circbuffer.h"
- #include "dbutil.h"
- #include "channel.h"
- #include "ssh.h"
- #include "listener.h"
- #include "runopts.h"
- #include "netio.h"
- static void send_msg_channel_open_failure(unsigned int remotechan, int reason,
- const char *text, const char *lang);
- static void send_msg_channel_open_confirmation(struct Channel* channel,
- unsigned int recvwindow,
- unsigned int recvmaxpacket);
- static int writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
- const unsigned char *moredata, unsigned int *morelen);
- static void send_msg_channel_window_adjust(struct Channel *channel,
- unsigned int incr);
- static void send_msg_channel_data(struct Channel *channel, int isextended);
- static void send_msg_channel_eof(struct Channel *channel);
- static void send_msg_channel_close(struct Channel *channel);
- static void remove_channel(struct Channel *channel);
- static unsigned int write_pending(struct Channel * channel);
- static void check_close(struct Channel *channel);
- static void close_chan_fd(struct Channel *channel, int fd, int how);
- #define FD_UNINIT (-2)
- #define FD_CLOSED (-1)
- #define ERRFD_IS_READ(channel) ((channel)->extrabuf == NULL)
- #define ERRFD_IS_WRITE(channel) (!ERRFD_IS_READ(channel))
- #define RECV_MAX_CHANNEL_DATA_LEN (RECV_MAX_PAYLOAD_LEN-(1+4+4))
- void chaninitialise(const struct ChanType *chantypes[]) {
-
- ses.channels = (struct Channel**)m_malloc(sizeof(struct Channel*));
- ses.chansize = 1;
- ses.channels[0] = NULL;
- ses.chancount = 0;
- ses.chantypes = chantypes;
- #ifdef USING_LISTENERS
- listeners_initialise();
- #endif
- }
- void chancleanup() {
- unsigned int i;
- TRACE(("enter chancleanup"))
- for (i = 0; i < ses.chansize; i++) {
- if (ses.channels[i] != NULL) {
- TRACE(("channel %d closing", i))
- remove_channel(ses.channels[i]);
- }
- }
- m_free(ses.channels);
- TRACE(("leave chancleanup"))
- }
- static struct Channel* newchannel(unsigned int remotechan,
- const struct ChanType *type,
- unsigned int transwindow, unsigned int transmaxpacket) {
- struct Channel * newchan;
- unsigned int i, j;
- TRACE(("enter newchannel"))
-
-
- for (i = 0; i < ses.chansize; i++) {
- if (ses.channels[i] == NULL) {
- break;
- }
- }
-
- if (i == ses.chansize) {
- if (ses.chansize >= MAX_CHANNELS) {
- TRACE(("leave newchannel: max chans reached"))
- return NULL;
- }
-
- ses.channels = (struct Channel**)m_realloc(ses.channels,
- (ses.chansize+CHAN_EXTEND_SIZE)*sizeof(struct Channel*));
- ses.chansize += CHAN_EXTEND_SIZE;
-
- for (j = i; j < ses.chansize; j++) {
- ses.channels[j] = NULL;
- }
- }
-
- newchan = (struct Channel*)m_malloc(sizeof(struct Channel));
- newchan->type = type;
- newchan->index = i;
- newchan->sent_close = newchan->recv_close = 0;
- newchan->sent_eof = newchan->recv_eof = 0;
- newchan->close_handler_done = 0;
- newchan->remotechan = remotechan;
- newchan->transwindow = transwindow;
- newchan->transmaxpacket = transmaxpacket;
-
- newchan->typedata = NULL;
- newchan->writefd = FD_UNINIT;
- newchan->readfd = FD_UNINIT;
- newchan->errfd = FD_CLOSED;
- newchan->await_open = 0;
- newchan->flushing = 0;
- newchan->writebuf = cbuf_new(opts.recv_window);
- newchan->recvwindow = opts.recv_window;
- newchan->extrabuf = NULL;
- newchan->recvdonelen = 0;
- newchan->recvmaxpacket = RECV_MAX_CHANNEL_DATA_LEN;
- newchan->prio = DROPBEAR_CHANNEL_PRIO_EARLY;
- ses.channels[i] = newchan;
- ses.chancount++;
- TRACE(("leave newchannel"))
- return newchan;
- }
- static struct Channel* getchannel_msg(const char* kind) {
- unsigned int chan;
- chan = buf_getint(ses.payload);
- if (chan >= ses.chansize || ses.channels[chan] == NULL) {
- if (kind) {
- dropbear_exit("%s for unknown channel %d", kind, chan);
- } else {
- dropbear_exit("Unknown channel %d", chan);
- }
- }
- return ses.channels[chan];
- }
- struct Channel* getchannel() {
- return getchannel_msg(NULL);
- }
- void channelio(fd_set *readfds, fd_set *writefds) {
-
- struct Channel *channel;
- unsigned int i;
-
- for (i = 0; i < ses.chansize; i++) {
-
- int do_check_close = 0;
- channel = ses.channels[i];
- if (channel == NULL) {
-
- continue;
- }
-
- if (channel->readfd >= 0 && FD_ISSET(channel->readfd, readfds)) {
- TRACE(("send normal readfd"))
- send_msg_channel_data(channel, 0);
- do_check_close = 1;
- }
-
- if (ERRFD_IS_READ(channel) && channel->errfd >= 0
- && FD_ISSET(channel->errfd, readfds)) {
- TRACE(("send normal errfd"))
- send_msg_channel_data(channel, 1);
- do_check_close = 1;
- }
-
- if (channel->writefd >= 0 && FD_ISSET(channel->writefd, writefds)) {
- writechannel(channel, channel->writefd, channel->writebuf, NULL, NULL);
- do_check_close = 1;
- }
-
-
- if (ERRFD_IS_WRITE(channel)
- && channel->errfd >= 0 && FD_ISSET(channel->errfd, writefds)) {
- writechannel(channel, channel->errfd, channel->extrabuf, NULL, NULL);
- do_check_close = 1;
- }
- if (ses.channel_signal_pending) {
-
- do_check_close = 1;
- }
-
-
- if (do_check_close) {
- check_close(channel);
- }
- }
- #ifdef USING_LISTENERS
- handle_listeners(readfds);
- #endif
- }
- static unsigned int write_pending(struct Channel * channel) {
- if (channel->writefd >= 0 && cbuf_getused(channel->writebuf) > 0) {
- return 1;
- } else if (channel->errfd >= 0 && channel->extrabuf &&
- cbuf_getused(channel->extrabuf) > 0) {
- return 1;
- }
- return 0;
- }
- static void check_close(struct Channel *channel) {
- int close_allowed = 0;
- TRACE2(("check_close: writefd %d, readfd %d, errfd %d, sent_close %d, recv_close %d",
- channel->writefd, channel->readfd,
- channel->errfd, channel->sent_close, channel->recv_close))
- TRACE2(("writebuf size %d extrabuf size %d",
- channel->writebuf ? cbuf_getused(channel->writebuf) : 0,
- channel->extrabuf ? cbuf_getused(channel->extrabuf) : 0))
- if (!channel->flushing
- && !channel->close_handler_done
- && channel->type->check_close
- && channel->type->check_close(channel))
- {
- channel->flushing = 1;
- }
-
-
- if (!channel->type->check_close
- || channel->close_handler_done
- || channel->type->check_close(channel)) {
- close_allowed = 1;
- }
- if (channel->recv_close && !write_pending(channel) && close_allowed) {
- if (!channel->sent_close) {
- TRACE(("Sending MSG_CHANNEL_CLOSE in response to same."))
- send_msg_channel_close(channel);
- }
- remove_channel(channel);
- return;
- }
- if ((channel->recv_eof && !write_pending(channel))
-
- || (channel->type->check_close && close_allowed)) {
- close_chan_fd(channel, channel->writefd, SHUT_WR);
- }
-
- if (channel->flushing) {
- TRACE(("might send data, flushing"))
- if (channel->readfd >= 0 && channel->transwindow > 0) {
- TRACE(("send data readfd"))
- send_msg_channel_data(channel, 0);
- }
- if (ERRFD_IS_READ(channel) && channel->errfd >= 0
- && channel->transwindow > 0) {
- TRACE(("send data errfd"))
- send_msg_channel_data(channel, 1);
- }
- }
-
- if (!channel->sent_eof
- && channel->readfd == FD_CLOSED
- && (ERRFD_IS_WRITE(channel) || channel->errfd == FD_CLOSED)) {
- send_msg_channel_eof(channel);
- }
-
- if (channel->readfd == FD_CLOSED
- && channel->writefd == FD_CLOSED
- && (ERRFD_IS_WRITE(channel) || channel->errfd == FD_CLOSED)
- && !channel->sent_close
- && close_allowed
- && !write_pending(channel)) {
- TRACE(("sending close, readfd is closed"))
- send_msg_channel_close(channel);
- }
- }
- void channel_connect_done(int result, int sock, void* user_data, const char* UNUSED(errstring)) {
- struct Channel *channel = user_data;
- TRACE(("enter channel_connect_done"))
- if (result == DROPBEAR_SUCCESS)
- {
- channel->readfd = channel->writefd = sock;
- channel->conn_pending = NULL;
- send_msg_channel_open_confirmation(channel, channel->recvwindow,
- channel->recvmaxpacket);
- TRACE(("leave channel_connect_done: success"))
- }
- else
- {
- send_msg_channel_open_failure(channel->remotechan,
- SSH_OPEN_CONNECT_FAILED, "", "");
- remove_channel(channel);
- TRACE(("leave check_in_progress: fail"))
- }
- }
- static void send_msg_channel_close(struct Channel *channel) {
- TRACE(("enter send_msg_channel_close %p", (void*)channel))
- if (channel->type->closehandler
- && !channel->close_handler_done) {
- channel->type->closehandler(channel);
- channel->close_handler_done = 1;
- }
-
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_CLOSE);
- buf_putint(ses.writepayload, channel->remotechan);
- encrypt_packet();
- channel->sent_eof = 1;
- channel->sent_close = 1;
- close_chan_fd(channel, channel->readfd, SHUT_RD);
- close_chan_fd(channel, channel->errfd, SHUT_RDWR);
- close_chan_fd(channel, channel->writefd, SHUT_WR);
- TRACE(("leave send_msg_channel_close"))
- }
- static void send_msg_channel_eof(struct Channel *channel) {
- TRACE(("enter send_msg_channel_eof"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_EOF);
- buf_putint(ses.writepayload, channel->remotechan);
- encrypt_packet();
- channel->sent_eof = 1;
- TRACE(("leave send_msg_channel_eof"))
- }
- #ifndef HAVE_WRITEV
- static int writechannel_fallback(struct Channel* channel, int fd, circbuffer *cbuf,
- const unsigned char *UNUSED(moredata), unsigned int *morelen) {
- unsigned char *circ_p1, *circ_p2;
- unsigned int circ_len1, circ_len2;
- ssize_t written;
- if (morelen) {
-
- *morelen = 0;
- }
-
- cbuf_readptrs(cbuf, &circ_p1, &circ_len1, &circ_p2, &circ_len2);
- written = write(fd, circ_p1, circ_len1);
- if (written < 0) {
- if (errno != EINTR && errno != EAGAIN) {
- TRACE(("channel IO write error fd %d %s", fd, strerror(errno)))
- close_chan_fd(channel, fd, SHUT_WR);
- return DROPBEAR_FAILURE;
- }
- } else {
- cbuf_incrread(cbuf, written);
- channel->recvdonelen += written;
- }
- return DROPBEAR_SUCCESS;
- }
- #endif
- #ifdef HAVE_WRITEV
- static int writechannel_writev(struct Channel* channel, int fd, circbuffer *cbuf,
- const unsigned char *moredata, unsigned int *morelen) {
- struct iovec iov[3];
- unsigned char *circ_p1, *circ_p2;
- unsigned int circ_len1, circ_len2;
- int io_count = 0;
- ssize_t written;
- cbuf_readptrs(cbuf, &circ_p1, &circ_len1, &circ_p2, &circ_len2);
- if (circ_len1 > 0) {
- TRACE(("circ1 %d", circ_len1))
- iov[io_count].iov_base = circ_p1;
- iov[io_count].iov_len = circ_len1;
- io_count++;
- }
- if (circ_len2 > 0) {
- TRACE(("circ2 %d", circ_len2))
- iov[io_count].iov_base = circ_p2;
- iov[io_count].iov_len = circ_len2;
- io_count++;
- }
- if (morelen) {
- assert(moredata);
- TRACE(("more %d", *morelen))
- iov[io_count].iov_base = (void*)moredata;
- iov[io_count].iov_len = *morelen;
- io_count++;
- }
- if (io_count == 0) {
-
- TRACE(("leave writechannel, no data"))
- return DROPBEAR_SUCCESS;
- }
- if (morelen) {
-
- *morelen = 0;
- }
- written = writev(fd, iov, io_count);
- if (written < 0) {
- if (errno != EINTR && errno != EAGAIN) {
- TRACE(("channel IO write error fd %d %s", fd, strerror(errno)))
- close_chan_fd(channel, fd, SHUT_WR);
- return DROPBEAR_FAILURE;
- }
- } else {
- int cbuf_written = MIN(circ_len1+circ_len2, (unsigned int)written);
- cbuf_incrread(cbuf, cbuf_written);
- if (morelen) {
- *morelen = written - cbuf_written;
- }
- channel->recvdonelen += written;
- }
- return DROPBEAR_SUCCESS;
- }
- #endif
- static int writechannel(struct Channel* channel, int fd, circbuffer *cbuf,
- const unsigned char *moredata, unsigned int *morelen) {
- int ret = DROPBEAR_SUCCESS;
- TRACE(("enter writechannel fd %d", fd))
- #ifdef HAVE_WRITEV
- ret = writechannel_writev(channel, fd, cbuf, moredata, morelen);
- #else
- ret = writechannel_fallback(channel, fd, cbuf, moredata, morelen);
- #endif
-
- if (channel->recvdonelen >= RECV_WINDOWEXTEND) {
- send_msg_channel_window_adjust(channel, channel->recvdonelen);
- channel->recvwindow += channel->recvdonelen;
- channel->recvdonelen = 0;
- }
- dropbear_assert(channel->recvwindow <= opts.recv_window);
- dropbear_assert(channel->recvwindow <= cbuf_getavail(channel->writebuf));
- dropbear_assert(channel->extrabuf == NULL ||
- channel->recvwindow <= cbuf_getavail(channel->extrabuf));
-
- TRACE(("leave writechannel"))
- return ret;
- }
- void setchannelfds(fd_set *readfds, fd_set *writefds, int allow_reads) {
-
- unsigned int i;
- struct Channel * channel;
-
- for (i = 0; i < ses.chansize; i++) {
- channel = ses.channels[i];
- if (channel == NULL) {
- continue;
- }
-
- if (channel->transwindow > 0
- && ((ses.dataallowed && allow_reads) || channel->read_mangler)) {
- if (channel->readfd >= 0) {
- FD_SET(channel->readfd, readfds);
- }
-
- if (ERRFD_IS_READ(channel) && channel->errfd >= 0) {
- FD_SET(channel->errfd, readfds);
- }
- }
-
- if (channel->writefd >= 0 && cbuf_getused(channel->writebuf) > 0) {
- FD_SET(channel->writefd, writefds);
- }
- if (ERRFD_IS_WRITE(channel) && channel->errfd >= 0
- && cbuf_getused(channel->extrabuf) > 0) {
- FD_SET(channel->errfd, writefds);
- }
- }
- #ifdef USING_LISTENERS
- set_listener_fds(readfds);
- #endif
- }
- void recv_msg_channel_eof() {
- struct Channel * channel;
- TRACE(("enter recv_msg_channel_eof"))
- channel = getchannel_msg("EOF");
- channel->recv_eof = 1;
- check_close(channel);
- TRACE(("leave recv_msg_channel_eof"))
- }
- void recv_msg_channel_close() {
- struct Channel * channel;
- TRACE(("enter recv_msg_channel_close"))
- channel = getchannel_msg("Close");
- channel->recv_eof = 1;
- channel->recv_close = 1;
- check_close(channel);
- TRACE(("leave recv_msg_channel_close"))
- }
- static void remove_channel(struct Channel * channel) {
- TRACE(("enter remove_channel"))
- TRACE(("channel index is %d", channel->index))
- cbuf_free(channel->writebuf);
- channel->writebuf = NULL;
- if (channel->extrabuf) {
- cbuf_free(channel->extrabuf);
- channel->extrabuf = NULL;
- }
- if (IS_DROPBEAR_SERVER || (channel->writefd != STDOUT_FILENO)) {
-
- TRACE(("CLOSE writefd %d", channel->writefd))
- m_close(channel->writefd);
- TRACE(("CLOSE readfd %d", channel->readfd))
- m_close(channel->readfd);
- TRACE(("CLOSE errfd %d", channel->errfd))
- m_close(channel->errfd);
- }
- if (!channel->close_handler_done
- && channel->type->closehandler) {
- channel->type->closehandler(channel);
- channel->close_handler_done = 1;
- }
- if (channel->conn_pending) {
- cancel_connect(channel->conn_pending);
- }
- ses.channels[channel->index] = NULL;
- m_free(channel);
- ses.chancount--;
- update_channel_prio();
- TRACE(("leave remove_channel"))
- }
- void recv_msg_channel_request() {
- struct Channel *channel;
- channel = getchannel();
- TRACE(("enter recv_msg_channel_request %p", (void*)channel))
- if (channel->sent_close) {
- TRACE(("leave recv_msg_channel_request: already closed channel"))
- return;
- }
- if (channel->type->reqhandler
- && !channel->close_handler_done) {
- channel->type->reqhandler(channel);
- } else {
- int wantreply;
- buf_eatstring(ses.payload);
- wantreply = buf_getbool(ses.payload);
- if (wantreply) {
- send_msg_channel_failure(channel);
- }
- }
- TRACE(("leave recv_msg_channel_request"))
- }
- static void send_msg_channel_data(struct Channel *channel, int isextended) {
- int len;
- size_t maxlen, size_pos;
- int fd;
- CHECKCLEARTOWRITE();
- TRACE(("enter send_msg_channel_data"))
- dropbear_assert(!channel->sent_close);
- if (isextended) {
- fd = channel->errfd;
- } else {
- fd = channel->readfd;
- }
- TRACE(("enter send_msg_channel_data isextended %d fd %d", isextended, fd))
- dropbear_assert(fd >= 0);
- maxlen = MIN(channel->transwindow, channel->transmaxpacket);
-
- maxlen = MIN(maxlen,
- ses.writepayload->size - 1 - 4 - 4 - (isextended ? 4 : 0));
- TRACE(("maxlen %zd", maxlen))
- if (maxlen == 0) {
- TRACE(("leave send_msg_channel_data: no window"))
- return;
- }
- buf_putbyte(ses.writepayload,
- isextended ? SSH_MSG_CHANNEL_EXTENDED_DATA : SSH_MSG_CHANNEL_DATA);
- buf_putint(ses.writepayload, channel->remotechan);
- if (isextended) {
- buf_putint(ses.writepayload, SSH_EXTENDED_DATA_STDERR);
- }
-
- size_pos = ses.writepayload->pos;
- buf_putint(ses.writepayload, 0);
-
- len = read(fd, buf_getwriteptr(ses.writepayload, maxlen), maxlen);
- if (len <= 0) {
- if (len == 0 || errno != EINTR) {
-
- close_chan_fd(channel, fd, SHUT_RD);
- }
- buf_setpos(ses.writepayload, 0);
- buf_setlen(ses.writepayload, 0);
- TRACE(("leave send_msg_channel_data: len %d read err %d or EOF for fd %d",
- len, errno, fd))
- return;
- }
- if (channel->read_mangler) {
- channel->read_mangler(channel, buf_getwriteptr(ses.writepayload, len), &len);
- if (len == 0) {
- buf_setpos(ses.writepayload, 0);
- buf_setlen(ses.writepayload, 0);
- return;
- }
- }
- TRACE(("send_msg_channel_data: len %d fd %d", len, fd))
- buf_incrwritepos(ses.writepayload, len);
-
- buf_setpos(ses.writepayload, size_pos);
- buf_putint(ses.writepayload, len);
- channel->transwindow -= len;
- encrypt_packet();
-
-
- if (channel->flushing && len < (ssize_t)maxlen)
- {
- TRACE(("closing from channel, flushing out."))
- close_chan_fd(channel, fd, SHUT_RD);
- }
- TRACE(("leave send_msg_channel_data"))
- }
- void recv_msg_channel_data() {
- struct Channel *channel;
- channel = getchannel();
- common_recv_msg_channel_data(channel, channel->writefd, channel->writebuf);
- }
- void common_recv_msg_channel_data(struct Channel *channel, int fd,
- circbuffer * cbuf) {
- unsigned int datalen;
- unsigned int maxdata;
- unsigned int buflen;
- unsigned int len;
- unsigned int consumed;
- int res;
- TRACE(("enter recv_msg_channel_data"))
- if (channel->recv_eof) {
- dropbear_exit("Received data after eof");
- }
- if (fd < 0 || !cbuf) {
-
- return;
- }
- datalen = buf_getint(ses.payload);
- TRACE(("length %d", datalen))
- maxdata = cbuf_getavail(cbuf);
-
- if (datalen > maxdata) {
- dropbear_exit("Oversized packet");
- }
- dropbear_assert(channel->recvwindow >= datalen);
- channel->recvwindow -= datalen;
- dropbear_assert(channel->recvwindow <= opts.recv_window);
-
- consumed = datalen;
- res = writechannel(channel, fd, cbuf, buf_getptr(ses.payload, datalen), &consumed);
- datalen -= consumed;
- buf_incrpos(ses.payload, consumed);
-
- if (res == DROPBEAR_SUCCESS) {
- len = datalen;
- while (len > 0) {
- buflen = cbuf_writelen(cbuf);
- buflen = MIN(buflen, len);
- memcpy(cbuf_writeptr(cbuf, buflen),
- buf_getptr(ses.payload, buflen), buflen);
- cbuf_incrwrite(cbuf, buflen);
- buf_incrpos(ses.payload, buflen);
- len -= buflen;
- }
- }
- TRACE(("leave recv_msg_channel_data"))
- }
- void recv_msg_channel_window_adjust() {
- struct Channel * channel;
- unsigned int incr;
-
- channel = getchannel();
-
- incr = buf_getint(ses.payload);
- TRACE(("received window increment %d", incr))
- incr = MIN(incr, TRANS_MAX_WIN_INCR);
-
- channel->transwindow += incr;
- channel->transwindow = MIN(channel->transwindow, TRANS_MAX_WINDOW);
- }
- static void send_msg_channel_window_adjust(struct Channel* channel,
- unsigned int incr) {
- TRACE(("sending window adjust %d", incr))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_WINDOW_ADJUST);
- buf_putint(ses.writepayload, channel->remotechan);
- buf_putint(ses.writepayload, incr);
- encrypt_packet();
- }
-
- void recv_msg_channel_open() {
- char *type;
- unsigned int typelen;
- unsigned int remotechan, transwindow, transmaxpacket;
- struct Channel *channel;
- const struct ChanType **cp;
- const struct ChanType *chantype;
- unsigned int errtype = SSH_OPEN_UNKNOWN_CHANNEL_TYPE;
- int ret;
- TRACE(("enter recv_msg_channel_open"))
-
- type = buf_getstring(ses.payload, &typelen);
- remotechan = buf_getint(ses.payload);
- transwindow = buf_getint(ses.payload);
- transwindow = MIN(transwindow, TRANS_MAX_WINDOW);
- transmaxpacket = buf_getint(ses.payload);
- transmaxpacket = MIN(transmaxpacket, TRANS_MAX_PAYLOAD_LEN);
-
- if (typelen > MAX_NAME_LEN) {
- goto failure;
- }
-
- for (cp = &ses.chantypes[0], chantype = (*cp);
- chantype != NULL;
- cp++, chantype = (*cp)) {
- if (strcmp(type, chantype->name) == 0) {
- break;
- }
- }
- if (chantype == NULL) {
- TRACE(("No matching type for '%s'", type))
- goto failure;
- }
- TRACE(("matched type '%s'", type))
-
- channel = newchannel(remotechan, chantype, transwindow, transmaxpacket);
- if (channel == NULL) {
- TRACE(("newchannel returned NULL"))
- errtype = SSH_OPEN_RESOURCE_SHORTAGE;
- goto failure;
- }
- if (channel->type->inithandler) {
- ret = channel->type->inithandler(channel);
- if (ret == SSH_OPEN_IN_PROGRESS) {
-
- goto cleanup;
- }
- if (ret > 0) {
- errtype = ret;
- remove_channel(channel);
- TRACE(("inithandler returned failure %d", ret))
- goto failure;
- }
- }
- if (channel->prio == DROPBEAR_CHANNEL_PRIO_EARLY) {
- channel->prio = DROPBEAR_CHANNEL_PRIO_BULK;
- }
-
- send_msg_channel_open_confirmation(channel, channel->recvwindow,
- channel->recvmaxpacket);
- goto cleanup;
- failure:
- TRACE(("recv_msg_channel_open failure"))
- send_msg_channel_open_failure(remotechan, errtype, "", "");
- cleanup:
- m_free(type);
-
- update_channel_prio();
- TRACE(("leave recv_msg_channel_open"))
- }
- void send_msg_channel_failure(struct Channel *channel) {
- TRACE(("enter send_msg_channel_failure"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_FAILURE);
- buf_putint(ses.writepayload, channel->remotechan);
- encrypt_packet();
- TRACE(("leave send_msg_channel_failure"))
- }
- void send_msg_channel_success(struct Channel *channel) {
- TRACE(("enter send_msg_channel_success"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_SUCCESS);
- buf_putint(ses.writepayload, channel->remotechan);
- encrypt_packet();
- TRACE(("leave send_msg_channel_success"))
- }
- static void send_msg_channel_open_failure(unsigned int remotechan,
- int reason, const char *text, const char *lang) {
- TRACE(("enter send_msg_channel_open_failure"))
- CHECKCLEARTOWRITE();
-
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_FAILURE);
- buf_putint(ses.writepayload, remotechan);
- buf_putint(ses.writepayload, reason);
- buf_putstring(ses.writepayload, text, strlen(text));
- buf_putstring(ses.writepayload, lang, strlen(lang));
- encrypt_packet();
- TRACE(("leave send_msg_channel_open_failure"))
- }
- static void send_msg_channel_open_confirmation(struct Channel* channel,
- unsigned int recvwindow,
- unsigned int recvmaxpacket) {
- TRACE(("enter send_msg_channel_open_confirmation"))
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
- buf_putint(ses.writepayload, channel->remotechan);
- buf_putint(ses.writepayload, channel->index);
- buf_putint(ses.writepayload, recvwindow);
- buf_putint(ses.writepayload, recvmaxpacket);
- encrypt_packet();
- TRACE(("leave send_msg_channel_open_confirmation"))
- }
- static void close_chan_fd(struct Channel *channel, int fd, int how) {
- int closein = 0, closeout = 0;
- if (channel->type->sepfds) {
- TRACE(("SHUTDOWN(%d, %d)", fd, how))
- shutdown(fd, how);
- if (how == 0) {
- closeout = 1;
- } else {
- closein = 1;
- }
- } else {
- TRACE(("CLOSE some fd %d", fd))
- m_close(fd);
- closein = closeout = 1;
- }
- if (closeout && (fd == channel->readfd)) {
- channel->readfd = FD_CLOSED;
- }
- if (closeout && ERRFD_IS_READ(channel) && (fd == channel->errfd)) {
- channel->errfd = FD_CLOSED;
- }
- if (closein && fd == channel->writefd) {
- channel->writefd = FD_CLOSED;
- }
- if (closein && ERRFD_IS_WRITE(channel) && (fd == channel->errfd)) {
- channel->errfd = FD_CLOSED;
- }
-
- if (channel->type->sepfds && channel->readfd == FD_CLOSED
- && channel->writefd == FD_CLOSED && channel->errfd == FD_CLOSED) {
- TRACE(("CLOSE (finally) of %d", fd))
- m_close(fd);
- }
- }
- #if defined(USING_LISTENERS) || defined(DROPBEAR_CLIENT)
- int send_msg_channel_open_init(int fd, const struct ChanType *type) {
- struct Channel* chan;
- TRACE(("enter send_msg_channel_open_init()"))
- chan = newchannel(0, type, 0, 0);
- if (!chan) {
- TRACE(("leave send_msg_channel_open_init() - FAILED in newchannel()"))
- return DROPBEAR_FAILURE;
- }
-
-
- setnonblocking(fd);
- chan->writefd = chan->readfd = fd;
- ses.maxfd = MAX(ses.maxfd, fd);
- chan->await_open = 1;
-
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN);
- buf_putstring(ses.writepayload, type->name, strlen(type->name));
- buf_putint(ses.writepayload, chan->index);
- buf_putint(ses.writepayload, opts.recv_window);
- buf_putint(ses.writepayload, RECV_MAX_CHANNEL_DATA_LEN);
- TRACE(("leave send_msg_channel_open_init()"))
- return DROPBEAR_SUCCESS;
- }
- void recv_msg_channel_open_confirmation() {
- struct Channel * channel;
- int ret;
- TRACE(("enter recv_msg_channel_open_confirmation"))
- channel = getchannel();
- if (!channel->await_open) {
- dropbear_exit("Unexpected channel reply");
- }
- channel->await_open = 0;
- channel->remotechan = buf_getint(ses.payload);
- channel->transwindow = buf_getint(ses.payload);
- channel->transmaxpacket = buf_getint(ses.payload);
-
- TRACE(("new chan remote %d local %d",
- channel->remotechan, channel->index))
-
- if (channel->type->inithandler) {
- ret = channel->type->inithandler(channel);
- if (ret > 0) {
- remove_channel(channel);
- TRACE(("inithandler returned failure %d", ret))
- return;
- }
- }
- if (channel->prio == DROPBEAR_CHANNEL_PRIO_EARLY) {
- channel->prio = DROPBEAR_CHANNEL_PRIO_BULK;
- }
- update_channel_prio();
-
- TRACE(("leave recv_msg_channel_open_confirmation"))
- }
- void recv_msg_channel_open_failure() {
- struct Channel * channel;
- channel = getchannel();
- if (!channel->await_open) {
- dropbear_exit("Unexpected channel reply");
- }
- channel->await_open = 0;
- remove_channel(channel);
- }
- #endif
- void send_msg_request_success() {
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS);
- encrypt_packet();
- }
- void send_msg_request_failure() {
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE);
- encrypt_packet();
- }
- struct Channel* get_any_ready_channel() {
- size_t i;
- if (ses.chancount == 0) {
- return NULL;
- }
- for (i = 0; i < ses.chansize; i++) {
- struct Channel *chan = ses.channels[i];
- if (chan
- && !(chan->sent_eof || chan->recv_eof)
- && !(chan->await_open)) {
- return chan;
- }
- }
- return NULL;
- }
- void start_send_channel_request(struct Channel *channel,
- char *type) {
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_REQUEST);
- buf_putint(ses.writepayload, channel->remotechan);
- buf_putstring(ses.writepayload, type, strlen(type));
- }
|