Annotation of src/usr.bin/ssh/channels.c, Revision 1.261
1.261 ! stevesk 1: /* $OpenBSD: channels.c,v 1.260 2006/07/22 20:48:22 stevesk Exp $ */
1.1 deraadt 2: /*
1.26 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * This file contains functions for generic socket connection forwarding.
7: * There is also code for initiating connection forwarding for X11 connections,
8: * arbitrary tcp/ip connections, and the authentication agent connection.
1.49 markus 9: *
1.67 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
1.44 markus 16: * SSH2 support added by Markus Friedl.
1.156 markus 17: * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
1.67 deraadt 18: * Copyright (c) 1999 Dug Song. All rights reserved.
19: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20: *
21: * Redistribution and use in source and binary forms, with or without
22: * modification, are permitted provided that the following conditions
23: * are met:
24: * 1. Redistributions of source code must retain the above copyright
25: * notice, this list of conditions and the following disclaimer.
26: * 2. Redistributions in binary form must reproduce the above copyright
27: * notice, this list of conditions and the following disclaimer in the
28: * documentation and/or other materials provided with the distribution.
29: *
30: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.26 deraadt 40: */
1.1 deraadt 41:
42: #include "includes.h"
1.234 stevesk 43:
44: #include <sys/ioctl.h>
1.235 stevesk 45: #include <sys/types.h>
46: #include <sys/un.h>
1.251 stevesk 47: #include <sys/socket.h>
1.261 ! stevesk 48: #include <sys/time.h>
1.251 stevesk 49:
50: #include <netinet/in.h>
51: #include <arpa/inet.h>
1.233 stevesk 52:
1.254 stevesk 53: #include <errno.h>
1.255 stevesk 54: #include <netdb.h>
1.260 stevesk 55: #include <string.h>
1.233 stevesk 56: #include <termios.h>
1.256 stevesk 57: #include <unistd.h>
1.1 deraadt 58:
59: #include "ssh.h"
1.82 markus 60: #include "ssh1.h"
61: #include "ssh2.h"
1.1 deraadt 62: #include "packet.h"
63: #include "xmalloc.h"
1.82 markus 64: #include "log.h"
65: #include "misc.h"
1.14 markus 66: #include "channels.h"
67: #include "compat.h"
1.82 markus 68: #include "canohost.h"
1.64 markus 69: #include "key.h"
70: #include "authfd.h"
1.147 stevesk 71: #include "pathnames.h"
1.193 markus 72: #include "bufaux.h"
1.1 deraadt 73:
1.121 markus 74: /* -- channel core */
1.12 markus 75:
1.27 markus 76: /*
77: * Pointer to an array containing all allocated channels. The array is
78: * dynamically extended as needed.
79: */
1.113 markus 80: static Channel **channels = NULL;
1.1 deraadt 81:
1.27 markus 82: /*
83: * Size of the channel array. All slots of the array must always be
1.113 markus 84: * initialized (at least the type field); unused slots set to NULL
1.27 markus 85: */
1.209 avsm 86: static u_int channels_alloc = 0;
1.1 deraadt 87:
1.27 markus 88: /*
89: * Maximum file descriptor value used in any of the channels. This is
1.113 markus 90: * updated in channel_new.
1.27 markus 91: */
1.84 markus 92: static int channel_max_fd = 0;
1.1 deraadt 93:
94:
1.121 markus 95: /* -- tcp forwarding */
1.1 deraadt 96:
1.27 markus 97: /*
98: * Data structure for storing which hosts are permitted for forward requests.
99: * The local sides of any remote forwards are stored in this array to prevent
100: * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
101: * network (which might be behind a firewall).
102: */
1.25 markus 103: typedef struct {
1.41 markus 104: char *host_to_connect; /* Connect to 'host'. */
105: u_short port_to_connect; /* Connect to 'port'. */
106: u_short listen_port; /* Remote side should listen port number. */
1.1 deraadt 107: } ForwardPermission;
108:
1.257 dtucker 109: /* List of all permitted host/port pairs to connect by the user. */
1.1 deraadt 110: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
1.121 markus 111:
1.257 dtucker 112: /* List of all permitted host/port pairs to connect by the admin. */
113: static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
114:
115: /* Number of permitted host/port pairs in the array permitted by the user. */
1.1 deraadt 116: static int num_permitted_opens = 0;
1.257 dtucker 117:
118: /* Number of permitted host/port pair in the array permitted by the admin. */
119: static int num_adm_permitted_opens = 0;
120:
1.27 markus 121: /*
122: * If this is true, all opens are permitted. This is the case on the server
123: * on which we have to trust the client anyway, and the user could do
124: * anything after logging in anyway.
125: */
1.1 deraadt 126: static int all_opens_permitted = 0;
127:
1.121 markus 128:
129: /* -- X11 forwarding */
130:
131: /* Maximum number of fake X11 displays to try. */
132: #define MAX_DISPLAYS 1000
133:
1.219 djm 134: /* Saved X11 local (client) display. */
135: static char *x11_saved_display = NULL;
136:
1.121 markus 137: /* Saved X11 authentication protocol name. */
138: static char *x11_saved_proto = NULL;
139:
140: /* Saved X11 authentication data. This is the real data. */
141: static char *x11_saved_data = NULL;
142: static u_int x11_saved_data_len = 0;
143:
144: /*
145: * Fake X11 authentication data. This is what the server will be sending us;
146: * we should replace any occurrences of this by the real data.
147: */
1.240 deraadt 148: static u_char *x11_fake_data = NULL;
1.121 markus 149: static u_int x11_fake_data_len;
150:
151:
152: /* -- agent forwarding */
153:
154: #define NUM_SOCKS 10
155:
1.82 markus 156: /* AF_UNSPEC or AF_INET or AF_INET6 */
1.135 markus 157: static int IPv4or6 = AF_UNSPEC;
1.1 deraadt 158:
1.121 markus 159: /* helper */
1.127 itojun 160: static void port_open_helper(Channel *c, char *rtype);
1.103 markus 161:
1.121 markus 162: /* -- channel core */
1.41 markus 163:
164: Channel *
1.229 markus 165: channel_by_id(int id)
1.41 markus 166: {
167: Channel *c;
1.113 markus 168:
1.209 avsm 169: if (id < 0 || (u_int)id >= channels_alloc) {
1.229 markus 170: logit("channel_by_id: %d: bad id", id);
1.41 markus 171: return NULL;
172: }
1.113 markus 173: c = channels[id];
174: if (c == NULL) {
1.229 markus 175: logit("channel_by_id: %d: bad id: channel free", id);
1.41 markus 176: return NULL;
177: }
178: return c;
1.55 markus 179: }
180:
1.27 markus 181: /*
1.229 markus 182: * Returns the channel if it is allowed to receive protocol messages.
183: * Private channels, like listening sockets, may not receive messages.
184: */
185: Channel *
186: channel_lookup(int id)
187: {
188: Channel *c;
189:
190: if ((c = channel_by_id(id)) == NULL)
191: return (NULL);
192:
1.237 deraadt 193: switch (c->type) {
1.229 markus 194: case SSH_CHANNEL_X11_OPEN:
195: case SSH_CHANNEL_LARVAL:
196: case SSH_CHANNEL_CONNECTING:
197: case SSH_CHANNEL_DYNAMIC:
198: case SSH_CHANNEL_OPENING:
199: case SSH_CHANNEL_OPEN:
200: case SSH_CHANNEL_INPUT_DRAINING:
201: case SSH_CHANNEL_OUTPUT_DRAINING:
202: return (c);
203: }
204: logit("Non-public channel %d, type %d.", id, c->type);
205: return (NULL);
206: }
207:
208: /*
1.55 markus 209: * Register filedescriptors for a channel, used when allocating a channel or
1.52 markus 210: * when the channel consumer/producer is ready, e.g. shell exec'd
1.27 markus 211: */
1.127 itojun 212: static void
1.71 markus 213: channel_register_fds(Channel *c, int rfd, int wfd, int efd,
214: int extusage, int nonblock)
1.1 deraadt 215: {
1.25 markus 216: /* Update the maximum file descriptor value. */
1.84 markus 217: channel_max_fd = MAX(channel_max_fd, rfd);
218: channel_max_fd = MAX(channel_max_fd, wfd);
219: channel_max_fd = MAX(channel_max_fd, efd);
220:
1.27 markus 221: /* XXX set close-on-exec -markus */
1.55 markus 222:
1.52 markus 223: c->rfd = rfd;
224: c->wfd = wfd;
225: c->sock = (rfd == wfd) ? rfd : -1;
1.204 djm 226: c->ctl_fd = -1; /* XXX: set elsewhere */
1.52 markus 227: c->efd = efd;
228: c->extended_usage = extusage;
1.71 markus 229:
1.91 markus 230: /* XXX ugly hack: nonblock is only set by the server */
231: if (nonblock && isatty(c->rfd)) {
1.194 markus 232: debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1.91 markus 233: c->isatty = 1;
234: if (!isatty(c->wfd)) {
1.94 markus 235: error("channel %d: wfd %d is not a tty?",
1.91 markus 236: c->self, c->wfd);
237: }
238: } else {
239: c->isatty = 0;
240: }
241:
1.71 markus 242: /* enable nonblocking mode */
243: if (nonblock) {
244: if (rfd != -1)
245: set_nonblock(rfd);
246: if (wfd != -1)
247: set_nonblock(wfd);
248: if (efd != -1)
249: set_nonblock(efd);
250: }
1.52 markus 251: }
252:
253: /*
254: * Allocate a new channel object and set its type and socket. This will cause
255: * remote_name to be freed.
256: */
1.113 markus 257: Channel *
1.52 markus 258: channel_new(char *ctype, int type, int rfd, int wfd, int efd,
1.178 markus 259: u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
1.52 markus 260: {
1.209 avsm 261: int found;
262: u_int i;
1.52 markus 263: Channel *c;
1.25 markus 264:
265: /* Do initial allocation if this is the first call. */
266: if (channels_alloc == 0) {
267: channels_alloc = 10;
1.242 djm 268: channels = xcalloc(channels_alloc, sizeof(Channel *));
1.25 markus 269: for (i = 0; i < channels_alloc; i++)
1.113 markus 270: channels[i] = NULL;
1.25 markus 271: }
272: /* Try to find a free slot where to put the new channel. */
273: for (found = -1, i = 0; i < channels_alloc; i++)
1.113 markus 274: if (channels[i] == NULL) {
1.25 markus 275: /* Found a free slot. */
1.209 avsm 276: found = (int)i;
1.25 markus 277: break;
278: }
1.209 avsm 279: if (found < 0) {
1.27 markus 280: /* There are no free slots. Take last+1 slot and expand the array. */
1.25 markus 281: found = channels_alloc;
1.179 markus 282: if (channels_alloc > 10000)
283: fatal("channel_new: internal error: channels_alloc %d "
284: "too big.", channels_alloc);
1.243 djm 285: channels = xrealloc(channels, channels_alloc + 10,
286: sizeof(Channel *));
1.195 markus 287: channels_alloc += 10;
1.70 markus 288: debug2("channel: expanding %d", channels_alloc);
1.25 markus 289: for (i = found; i < channels_alloc; i++)
1.113 markus 290: channels[i] = NULL;
1.25 markus 291: }
1.113 markus 292: /* Initialize and return new channel. */
1.242 djm 293: c = channels[found] = xcalloc(1, sizeof(Channel));
1.25 markus 294: buffer_init(&c->input);
295: buffer_init(&c->output);
1.41 markus 296: buffer_init(&c->extended);
1.159 markus 297: c->ostate = CHAN_OUTPUT_OPEN;
298: c->istate = CHAN_INPUT_OPEN;
299: c->flags = 0;
1.71 markus 300: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
1.25 markus 301: c->self = found;
302: c->type = type;
1.41 markus 303: c->ctype = ctype;
1.51 markus 304: c->local_window = window;
305: c->local_window_max = window;
306: c->local_consumed = 0;
307: c->local_maxpacket = maxpack;
1.25 markus 308: c->remote_id = -1;
1.190 markus 309: c->remote_name = xstrdup(remote_name);
1.41 markus 310: c->remote_window = 0;
311: c->remote_maxpacket = 0;
1.133 markus 312: c->force_drain = 0;
1.149 markus 313: c->single_connection = 0;
1.131 markus 314: c->detach_user = NULL;
1.225 djm 315: c->detach_close = 0;
1.165 markus 316: c->confirm = NULL;
1.204 djm 317: c->confirm_ctx = NULL;
1.65 markus 318: c->input_filter = NULL;
1.231 reyk 319: c->output_filter = NULL;
1.25 markus 320: debug("channel %d: new [%s]", found, remote_name);
1.113 markus 321: return c;
1.1 deraadt 322: }
1.52 markus 323:
1.132 markus 324: static int
325: channel_find_maxfd(void)
326: {
1.209 avsm 327: u_int i;
328: int max = 0;
1.132 markus 329: Channel *c;
330:
331: for (i = 0; i < channels_alloc; i++) {
332: c = channels[i];
333: if (c != NULL) {
334: max = MAX(max, c->rfd);
335: max = MAX(max, c->wfd);
336: max = MAX(max, c->efd);
337: }
338: }
339: return max;
340: }
341:
342: int
343: channel_close_fd(int *fdp)
344: {
345: int ret = 0, fd = *fdp;
346:
347: if (fd != -1) {
348: ret = close(fd);
349: *fdp = -1;
350: if (fd == channel_max_fd)
351: channel_max_fd = channel_find_maxfd();
352: }
353: return ret;
354: }
355:
1.52 markus 356: /* Close all channel fd/socket. */
1.127 itojun 357: static void
1.52 markus 358: channel_close_fds(Channel *c)
359: {
1.204 djm 360: debug3("channel %d: close_fds r %d w %d e %d c %d",
361: c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
1.118 markus 362:
1.132 markus 363: channel_close_fd(&c->sock);
1.204 djm 364: channel_close_fd(&c->ctl_fd);
1.132 markus 365: channel_close_fd(&c->rfd);
366: channel_close_fd(&c->wfd);
367: channel_close_fd(&c->efd);
1.121 markus 368: }
369:
370: /* Free the channel and close its fd/socket. */
371: void
372: channel_free(Channel *c)
373: {
374: char *s;
1.209 avsm 375: u_int i, n;
1.121 markus 376:
377: for (n = 0, i = 0; i < channels_alloc; i++)
378: if (channels[i])
379: n++;
1.209 avsm 380: debug("channel %d: free: %s, nchannels %u", c->self,
1.121 markus 381: c->remote_name ? c->remote_name : "???", n);
382:
383: s = channel_open_message();
1.194 markus 384: debug3("channel %d: status: %s", c->self, s);
1.121 markus 385: xfree(s);
386:
387: if (c->sock != -1)
388: shutdown(c->sock, SHUT_RDWR);
1.204 djm 389: if (c->ctl_fd != -1)
390: shutdown(c->ctl_fd, SHUT_RDWR);
1.121 markus 391: channel_close_fds(c);
392: buffer_free(&c->input);
393: buffer_free(&c->output);
394: buffer_free(&c->extended);
395: if (c->remote_name) {
396: xfree(c->remote_name);
397: c->remote_name = NULL;
398: }
399: channels[c->self] = NULL;
400: xfree(c);
401: }
402:
403: void
1.126 markus 404: channel_free_all(void)
1.121 markus 405: {
1.209 avsm 406: u_int i;
1.121 markus 407:
1.126 markus 408: for (i = 0; i < channels_alloc; i++)
409: if (channels[i] != NULL)
410: channel_free(channels[i]);
1.131 markus 411: }
412:
1.121 markus 413: /*
414: * Closes the sockets/fds of all channels. This is used to close extra file
415: * descriptors after a fork.
416: */
417: void
1.142 itojun 418: channel_close_all(void)
1.121 markus 419: {
1.209 avsm 420: u_int i;
1.121 markus 421:
422: for (i = 0; i < channels_alloc; i++)
423: if (channels[i] != NULL)
424: channel_close_fds(channels[i]);
425: }
426:
427: /*
1.131 markus 428: * Stop listening to channels.
429: */
430: void
431: channel_stop_listening(void)
432: {
1.209 avsm 433: u_int i;
1.131 markus 434: Channel *c;
435:
436: for (i = 0; i < channels_alloc; i++) {
437: c = channels[i];
438: if (c != NULL) {
439: switch (c->type) {
440: case SSH_CHANNEL_AUTH_SOCKET:
441: case SSH_CHANNEL_PORT_LISTENER:
442: case SSH_CHANNEL_RPORT_LISTENER:
443: case SSH_CHANNEL_X11_LISTENER:
1.132 markus 444: channel_close_fd(&c->sock);
1.131 markus 445: channel_free(c);
446: break;
447: }
448: }
449: }
450: }
451:
452: /*
1.121 markus 453: * Returns true if no channel has too much buffered data, and false if one or
454: * more channel is overfull.
455: */
456: int
1.142 itojun 457: channel_not_very_much_buffered_data(void)
1.121 markus 458: {
459: u_int i;
460: Channel *c;
461:
462: for (i = 0; i < channels_alloc; i++) {
463: c = channels[i];
464: if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
1.136 markus 465: #if 0
466: if (!compat20 &&
467: buffer_len(&c->input) > packet_get_maxsize()) {
1.185 markus 468: debug2("channel %d: big input buffer %d",
1.121 markus 469: c->self, buffer_len(&c->input));
470: return 0;
471: }
1.136 markus 472: #endif
1.121 markus 473: if (buffer_len(&c->output) > packet_get_maxsize()) {
1.191 markus 474: debug2("channel %d: big output buffer %u > %u",
1.136 markus 475: c->self, buffer_len(&c->output),
476: packet_get_maxsize());
1.121 markus 477: return 0;
478: }
479: }
480: }
481: return 1;
482: }
483:
484: /* Returns true if any channel is still open. */
485: int
1.142 itojun 486: channel_still_open(void)
1.121 markus 487: {
1.209 avsm 488: u_int i;
1.121 markus 489: Channel *c;
490:
491: for (i = 0; i < channels_alloc; i++) {
492: c = channels[i];
493: if (c == NULL)
494: continue;
495: switch (c->type) {
496: case SSH_CHANNEL_X11_LISTENER:
497: case SSH_CHANNEL_PORT_LISTENER:
498: case SSH_CHANNEL_RPORT_LISTENER:
499: case SSH_CHANNEL_CLOSED:
500: case SSH_CHANNEL_AUTH_SOCKET:
501: case SSH_CHANNEL_DYNAMIC:
502: case SSH_CHANNEL_CONNECTING:
503: case SSH_CHANNEL_ZOMBIE:
504: continue;
505: case SSH_CHANNEL_LARVAL:
506: if (!compat20)
507: fatal("cannot happen: SSH_CHANNEL_LARVAL");
508: continue;
509: case SSH_CHANNEL_OPENING:
510: case SSH_CHANNEL_OPEN:
511: case SSH_CHANNEL_X11_OPEN:
512: return 1;
513: case SSH_CHANNEL_INPUT_DRAINING:
514: case SSH_CHANNEL_OUTPUT_DRAINING:
515: if (!compat13)
516: fatal("cannot happen: OUT_DRAIN");
517: return 1;
518: default:
519: fatal("channel_still_open: bad channel type %d", c->type);
520: /* NOTREACHED */
521: }
522: }
523: return 0;
524: }
525:
526: /* Returns the id of an open channel suitable for keepaliving */
527: int
1.142 itojun 528: channel_find_open(void)
1.121 markus 529: {
1.209 avsm 530: u_int i;
1.121 markus 531: Channel *c;
532:
533: for (i = 0; i < channels_alloc; i++) {
534: c = channels[i];
1.206 djm 535: if (c == NULL || c->remote_id < 0)
1.121 markus 536: continue;
537: switch (c->type) {
538: case SSH_CHANNEL_CLOSED:
539: case SSH_CHANNEL_DYNAMIC:
540: case SSH_CHANNEL_X11_LISTENER:
541: case SSH_CHANNEL_PORT_LISTENER:
542: case SSH_CHANNEL_RPORT_LISTENER:
543: case SSH_CHANNEL_OPENING:
544: case SSH_CHANNEL_CONNECTING:
545: case SSH_CHANNEL_ZOMBIE:
546: continue;
547: case SSH_CHANNEL_LARVAL:
548: case SSH_CHANNEL_AUTH_SOCKET:
549: case SSH_CHANNEL_OPEN:
550: case SSH_CHANNEL_X11_OPEN:
551: return i;
552: case SSH_CHANNEL_INPUT_DRAINING:
553: case SSH_CHANNEL_OUTPUT_DRAINING:
554: if (!compat13)
555: fatal("cannot happen: OUT_DRAIN");
556: return i;
557: default:
558: fatal("channel_find_open: bad channel type %d", c->type);
559: /* NOTREACHED */
560: }
561: }
562: return -1;
563: }
564:
565:
566: /*
567: * Returns a message describing the currently open forwarded connections,
568: * suitable for sending to the client. The message contains crlf pairs for
569: * newlines.
570: */
571: char *
1.142 itojun 572: channel_open_message(void)
1.121 markus 573: {
574: Buffer buffer;
575: Channel *c;
576: char buf[1024], *cp;
1.209 avsm 577: u_int i;
1.121 markus 578:
579: buffer_init(&buffer);
580: snprintf(buf, sizeof buf, "The following connections are open:\r\n");
581: buffer_append(&buffer, buf, strlen(buf));
582: for (i = 0; i < channels_alloc; i++) {
583: c = channels[i];
584: if (c == NULL)
585: continue;
586: switch (c->type) {
587: case SSH_CHANNEL_X11_LISTENER:
588: case SSH_CHANNEL_PORT_LISTENER:
589: case SSH_CHANNEL_RPORT_LISTENER:
590: case SSH_CHANNEL_CLOSED:
591: case SSH_CHANNEL_AUTH_SOCKET:
592: case SSH_CHANNEL_ZOMBIE:
593: continue;
594: case SSH_CHANNEL_LARVAL:
595: case SSH_CHANNEL_OPENING:
596: case SSH_CHANNEL_CONNECTING:
597: case SSH_CHANNEL_DYNAMIC:
598: case SSH_CHANNEL_OPEN:
599: case SSH_CHANNEL_X11_OPEN:
600: case SSH_CHANNEL_INPUT_DRAINING:
601: case SSH_CHANNEL_OUTPUT_DRAINING:
1.204 djm 602: snprintf(buf, sizeof buf,
603: " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
1.121 markus 604: c->self, c->remote_name,
605: c->type, c->remote_id,
606: c->istate, buffer_len(&c->input),
607: c->ostate, buffer_len(&c->output),
1.204 djm 608: c->rfd, c->wfd, c->ctl_fd);
1.121 markus 609: buffer_append(&buffer, buf, strlen(buf));
610: continue;
611: default:
612: fatal("channel_open_message: bad channel type %d", c->type);
613: /* NOTREACHED */
614: }
615: }
616: buffer_append(&buffer, "\0", 1);
617: cp = xstrdup(buffer_ptr(&buffer));
618: buffer_free(&buffer);
619: return cp;
620: }
621:
622: void
623: channel_send_open(int id)
624: {
625: Channel *c = channel_lookup(id);
1.180 deraadt 626:
1.121 markus 627: if (c == NULL) {
1.188 itojun 628: logit("channel_send_open: %d: bad id", id);
1.121 markus 629: return;
630: }
1.184 markus 631: debug2("channel %d: send open", id);
1.121 markus 632: packet_start(SSH2_MSG_CHANNEL_OPEN);
633: packet_put_cstring(c->ctype);
634: packet_put_int(c->self);
635: packet_put_int(c->local_window);
636: packet_put_int(c->local_maxpacket);
637: packet_send();
638: }
639:
640: void
1.184 markus 641: channel_request_start(int id, char *service, int wantconfirm)
1.121 markus 642: {
1.184 markus 643: Channel *c = channel_lookup(id);
1.180 deraadt 644:
1.121 markus 645: if (c == NULL) {
1.188 itojun 646: logit("channel_request_start: %d: unknown channel id", id);
1.121 markus 647: return;
648: }
1.204 djm 649: debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
1.121 markus 650: packet_start(SSH2_MSG_CHANNEL_REQUEST);
651: packet_put_int(c->remote_id);
652: packet_put_cstring(service);
653: packet_put_char(wantconfirm);
654: }
1.241 deraadt 655:
1.121 markus 656: void
1.204 djm 657: channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
1.121 markus 658: {
659: Channel *c = channel_lookup(id);
1.180 deraadt 660:
1.121 markus 661: if (c == NULL) {
1.188 itojun 662: logit("channel_register_comfirm: %d: bad id", id);
1.121 markus 663: return;
664: }
1.165 markus 665: c->confirm = fn;
1.204 djm 666: c->confirm_ctx = ctx;
1.121 markus 667: }
1.241 deraadt 668:
1.121 markus 669: void
1.225 djm 670: channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
1.121 markus 671: {
1.229 markus 672: Channel *c = channel_by_id(id);
1.180 deraadt 673:
1.121 markus 674: if (c == NULL) {
1.188 itojun 675: logit("channel_register_cleanup: %d: bad id", id);
1.121 markus 676: return;
677: }
1.131 markus 678: c->detach_user = fn;
1.225 djm 679: c->detach_close = do_close;
1.121 markus 680: }
1.241 deraadt 681:
1.121 markus 682: void
683: channel_cancel_cleanup(int id)
684: {
1.229 markus 685: Channel *c = channel_by_id(id);
1.180 deraadt 686:
1.121 markus 687: if (c == NULL) {
1.188 itojun 688: logit("channel_cancel_cleanup: %d: bad id", id);
1.121 markus 689: return;
1.52 markus 690: }
1.131 markus 691: c->detach_user = NULL;
1.225 djm 692: c->detach_close = 0;
1.121 markus 693: }
1.241 deraadt 694:
1.121 markus 695: void
1.231 reyk 696: channel_register_filter(int id, channel_infilter_fn *ifn,
697: channel_outfilter_fn *ofn)
1.121 markus 698: {
699: Channel *c = channel_lookup(id);
1.180 deraadt 700:
1.121 markus 701: if (c == NULL) {
1.188 itojun 702: logit("channel_register_filter: %d: bad id", id);
1.121 markus 703: return;
1.52 markus 704: }
1.231 reyk 705: c->input_filter = ifn;
706: c->output_filter = ofn;
1.52 markus 707: }
708:
1.49 markus 709: void
1.121 markus 710: channel_set_fds(int id, int rfd, int wfd, int efd,
1.168 markus 711: int extusage, int nonblock, u_int window_max)
1.1 deraadt 712: {
1.121 markus 713: Channel *c = channel_lookup(id);
1.180 deraadt 714:
1.121 markus 715: if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
716: fatal("channel_activate for non-larval channel %d.", id);
717: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
718: c->type = SSH_CHANNEL_OPEN;
1.168 markus 719: c->local_window = c->local_window_max = window_max;
1.121 markus 720: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
721: packet_put_int(c->remote_id);
722: packet_put_int(c->local_window);
723: packet_send();
1.1 deraadt 724: }
725:
1.27 markus 726: /*
1.41 markus 727: * 'channel_pre*' are called just before select() to add any bits relevant to
728: * channels in the select bitmasks.
729: */
730: /*
731: * 'channel_post*': perform any appropriate operations for channels which
732: * have events pending.
1.27 markus 733: */
1.237 deraadt 734: typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
1.41 markus 735: chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
736: chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
737:
1.259 stevesk 738: /* ARGSUSED */
1.127 itojun 739: static void
1.237 deraadt 740: channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 741: {
742: FD_SET(c->sock, readset);
743: }
744:
1.259 stevesk 745: /* ARGSUSED */
1.127 itojun 746: static void
1.237 deraadt 747: channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
1.75 markus 748: {
749: debug3("channel %d: waiting for connection", c->self);
750: FD_SET(c->sock, writeset);
751: }
752:
1.127 itojun 753: static void
1.237 deraadt 754: channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 755: {
756: if (buffer_len(&c->input) < packet_get_maxsize())
757: FD_SET(c->sock, readset);
758: if (buffer_len(&c->output) > 0)
759: FD_SET(c->sock, writeset);
760: }
1.1 deraadt 761:
1.127 itojun 762: static void
1.237 deraadt 763: channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 764: {
1.157 markus 765: u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
1.41 markus 766:
1.44 markus 767: if (c->istate == CHAN_INPUT_OPEN &&
1.157 markus 768: limit > 0 &&
1.250 djm 769: buffer_len(&c->input) < limit &&
770: buffer_check_alloc(&c->input, CHAN_RBUF))
1.44 markus 771: FD_SET(c->rfd, readset);
772: if (c->ostate == CHAN_OUTPUT_OPEN ||
773: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
774: if (buffer_len(&c->output) > 0) {
775: FD_SET(c->wfd, writeset);
776: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.172 markus 777: if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.223 djm 778: debug2("channel %d: obuf_empty delayed efd %d/(%d)",
779: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 780: else
781: chan_obuf_empty(c);
1.44 markus 782: }
783: }
784: /** XXX check close conditions, too */
1.157 markus 785: if (compat20 && c->efd != -1) {
1.44 markus 786: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
787: buffer_len(&c->extended) > 0)
788: FD_SET(c->efd, writeset);
1.172 markus 789: else if (!(c->flags & CHAN_EOF_SENT) &&
790: c->extended_usage == CHAN_EXTENDED_READ &&
1.44 markus 791: buffer_len(&c->extended) < c->remote_window)
792: FD_SET(c->efd, readset);
793: }
1.204 djm 794: /* XXX: What about efd? races? */
1.208 deraadt 795: if (compat20 && c->ctl_fd != -1 &&
1.204 djm 796: c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
797: FD_SET(c->ctl_fd, readset);
1.44 markus 798: }
799:
1.259 stevesk 800: /* ARGSUSED */
1.127 itojun 801: static void
1.237 deraadt 802: channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 803: {
804: if (buffer_len(&c->input) == 0) {
805: packet_start(SSH_MSG_CHANNEL_CLOSE);
806: packet_put_int(c->remote_id);
807: packet_send();
808: c->type = SSH_CHANNEL_CLOSED;
1.194 markus 809: debug2("channel %d: closing after input drain.", c->self);
1.41 markus 810: }
811: }
812:
1.259 stevesk 813: /* ARGSUSED */
1.127 itojun 814: static void
1.237 deraadt 815: channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 816: {
817: if (buffer_len(&c->output) == 0)
1.113 markus 818: chan_mark_dead(c);
1.49 markus 819: else
1.41 markus 820: FD_SET(c->sock, writeset);
821: }
822:
823: /*
824: * This is a special state for X11 authentication spoofing. An opened X11
825: * connection (when authentication spoofing is being done) remains in this
826: * state until the first packet has been completely read. The authentication
827: * data in that packet is then substituted by the real data if it matches the
828: * fake data, and the channel is put into normal mode.
1.51 markus 829: * XXX All this happens at the client side.
1.121 markus 830: * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41 markus 831: */
1.127 itojun 832: static int
1.121 markus 833: x11_open_helper(Buffer *b)
1.1 deraadt 834: {
1.77 markus 835: u_char *ucp;
836: u_int proto_len, data_len;
1.25 markus 837:
1.41 markus 838: /* Check if the fixed size part of the packet is in buffer. */
1.121 markus 839: if (buffer_len(b) < 12)
1.41 markus 840: return 0;
841:
842: /* Parse the lengths of variable-length fields. */
1.155 stevesk 843: ucp = buffer_ptr(b);
1.41 markus 844: if (ucp[0] == 0x42) { /* Byte order MSB first. */
845: proto_len = 256 * ucp[6] + ucp[7];
846: data_len = 256 * ucp[8] + ucp[9];
847: } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
848: proto_len = ucp[6] + 256 * ucp[7];
849: data_len = ucp[8] + 256 * ucp[9];
850: } else {
1.194 markus 851: debug2("Initial X11 packet contains bad byte order byte: 0x%x",
1.148 deraadt 852: ucp[0]);
1.41 markus 853: return -1;
854: }
855:
856: /* Check if the whole packet is in buffer. */
1.121 markus 857: if (buffer_len(b) <
1.41 markus 858: 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
859: return 0;
860:
861: /* Check if authentication protocol matches. */
862: if (proto_len != strlen(x11_saved_proto) ||
863: memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
1.194 markus 864: debug2("X11 connection uses different authentication protocol.");
1.41 markus 865: return -1;
866: }
867: /* Check if authentication data matches our fake data. */
868: if (data_len != x11_fake_data_len ||
869: memcmp(ucp + 12 + ((proto_len + 3) & ~3),
870: x11_fake_data, x11_fake_data_len) != 0) {
1.194 markus 871: debug2("X11 auth data does not match fake data.");
1.41 markus 872: return -1;
873: }
874: /* Check fake data length */
875: if (x11_fake_data_len != x11_saved_data_len) {
876: error("X11 fake_data_len %d != saved_data_len %d",
877: x11_fake_data_len, x11_saved_data_len);
878: return -1;
879: }
880: /*
881: * Received authentication protocol and data match
882: * our fake data. Substitute the fake data with real
883: * data.
884: */
885: memcpy(ucp + 12 + ((proto_len + 3) & ~3),
886: x11_saved_data, x11_saved_data_len);
887: return 1;
888: }
889:
1.127 itojun 890: static void
1.237 deraadt 891: channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 892: {
1.121 markus 893: int ret = x11_open_helper(&c->output);
1.180 deraadt 894:
1.41 markus 895: if (ret == 1) {
896: /* Start normal processing for the channel. */
897: c->type = SSH_CHANNEL_OPEN;
1.47 markus 898: channel_pre_open_13(c, readset, writeset);
1.41 markus 899: } else if (ret == -1) {
900: /*
901: * We have received an X11 connection that has bad
902: * authentication information.
903: */
1.188 itojun 904: logit("X11 connection rejected because of wrong authentication.");
1.41 markus 905: buffer_clear(&c->input);
906: buffer_clear(&c->output);
1.132 markus 907: channel_close_fd(&c->sock);
1.41 markus 908: c->sock = -1;
909: c->type = SSH_CHANNEL_CLOSED;
910: packet_start(SSH_MSG_CHANNEL_CLOSE);
911: packet_put_int(c->remote_id);
912: packet_send();
913: }
914: }
1.25 markus 915:
1.127 itojun 916: static void
1.237 deraadt 917: channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 918: {
1.121 markus 919: int ret = x11_open_helper(&c->output);
1.133 markus 920:
921: /* c->force_drain = 1; */
922:
1.41 markus 923: if (ret == 1) {
924: c->type = SSH_CHANNEL_OPEN;
1.157 markus 925: channel_pre_open(c, readset, writeset);
1.41 markus 926: } else if (ret == -1) {
1.188 itojun 927: logit("X11 connection rejected because of wrong authentication.");
1.194 markus 928: debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
1.156 markus 929: chan_read_failed(c);
930: buffer_clear(&c->input);
931: chan_ibuf_empty(c);
932: buffer_clear(&c->output);
933: /* for proto v1, the peer will send an IEOF */
934: if (compat20)
935: chan_write_failed(c);
936: else
937: c->type = SSH_CHANNEL_OPEN;
1.194 markus 938: debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
1.41 markus 939: }
940: }
1.25 markus 941:
1.104 markus 942: /* try to decode a socks4 header */
1.259 stevesk 943: /* ARGSUSED */
1.127 itojun 944: static int
1.237 deraadt 945: channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
1.103 markus 946: {
1.181 markus 947: char *p, *host;
1.217 djm 948: u_int len, have, i, found;
1.148 deraadt 949: char username[256];
1.103 markus 950: struct {
951: u_int8_t version;
952: u_int8_t command;
953: u_int16_t dest_port;
1.104 markus 954: struct in_addr dest_addr;
1.103 markus 955: } s4_req, s4_rsp;
956:
1.104 markus 957: debug2("channel %d: decode socks4", c->self);
1.109 markus 958:
959: have = buffer_len(&c->input);
960: len = sizeof(s4_req);
961: if (have < len)
962: return 0;
963: p = buffer_ptr(&c->input);
964: for (found = 0, i = len; i < have; i++) {
965: if (p[i] == '\0') {
966: found = 1;
967: break;
968: }
969: if (i > 1024) {
970: /* the peer is probably sending garbage */
971: debug("channel %d: decode socks4: too long",
972: c->self);
973: return -1;
974: }
975: }
976: if (!found)
977: return 0;
1.103 markus 978: buffer_get(&c->input, (char *)&s4_req.version, 1);
979: buffer_get(&c->input, (char *)&s4_req.command, 1);
980: buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
1.104 markus 981: buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
1.109 markus 982: have = buffer_len(&c->input);
1.103 markus 983: p = buffer_ptr(&c->input);
984: len = strlen(p);
1.106 markus 985: debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.103 markus 986: if (len > have)
1.104 markus 987: fatal("channel %d: decode socks4: len %d > have %d",
1.103 markus 988: c->self, len, have);
989: strlcpy(username, p, sizeof(username));
990: buffer_consume(&c->input, len);
991: buffer_consume(&c->input, 1); /* trailing '\0' */
992:
1.104 markus 993: host = inet_ntoa(s4_req.dest_addr);
1.103 markus 994: strlcpy(c->path, host, sizeof(c->path));
995: c->host_port = ntohs(s4_req.dest_port);
1.148 deraadt 996:
1.194 markus 997: debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1.106 markus 998: c->self, host, c->host_port, s4_req.command);
1.103 markus 999:
1.104 markus 1000: if (s4_req.command != 1) {
1001: debug("channel %d: cannot handle: socks4 cn %d",
1002: c->self, s4_req.command);
1003: return -1;
1.103 markus 1004: }
1.104 markus 1005: s4_rsp.version = 0; /* vn: 0 for reply */
1006: s4_rsp.command = 90; /* cd: req granted */
1.103 markus 1007: s4_rsp.dest_port = 0; /* ignored */
1.104 markus 1008: s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1.246 deraadt 1009: buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
1.104 markus 1010: return 1;
1011: }
1012:
1.193 markus 1013: /* try to decode a socks5 header */
1014: #define SSH_SOCKS5_AUTHDONE 0x1000
1015: #define SSH_SOCKS5_NOAUTH 0x00
1016: #define SSH_SOCKS5_IPV4 0x01
1017: #define SSH_SOCKS5_DOMAIN 0x03
1018: #define SSH_SOCKS5_IPV6 0x04
1019: #define SSH_SOCKS5_CONNECT 0x01
1020: #define SSH_SOCKS5_SUCCESS 0x00
1021:
1.259 stevesk 1022: /* ARGSUSED */
1.193 markus 1023: static int
1.237 deraadt 1024: channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
1.193 markus 1025: {
1026: struct {
1027: u_int8_t version;
1028: u_int8_t command;
1029: u_int8_t reserved;
1030: u_int8_t atyp;
1031: } s5_req, s5_rsp;
1032: u_int16_t dest_port;
1033: u_char *p, dest_addr[255+1];
1.252 djm 1034: u_int have, need, i, found, nmethods, addrlen, af;
1.193 markus 1035:
1036: debug2("channel %d: decode socks5", c->self);
1037: p = buffer_ptr(&c->input);
1038: if (p[0] != 0x05)
1039: return -1;
1040: have = buffer_len(&c->input);
1041: if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1042: /* format: ver | nmethods | methods */
1.198 djm 1043: if (have < 2)
1.193 markus 1044: return 0;
1045: nmethods = p[1];
1046: if (have < nmethods + 2)
1047: return 0;
1048: /* look for method: "NO AUTHENTICATION REQUIRED" */
1049: for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1050: if (p[i] == SSH_SOCKS5_NOAUTH ) {
1051: found = 1;
1052: break;
1053: }
1054: }
1055: if (!found) {
1056: debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1057: c->self);
1058: return -1;
1059: }
1060: buffer_consume(&c->input, nmethods + 2);
1061: buffer_put_char(&c->output, 0x05); /* version */
1062: buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1063: FD_SET(c->sock, writeset);
1064: c->flags |= SSH_SOCKS5_AUTHDONE;
1065: debug2("channel %d: socks5 auth done", c->self);
1066: return 0; /* need more */
1067: }
1068: debug2("channel %d: socks5 post auth", c->self);
1069: if (have < sizeof(s5_req)+1)
1070: return 0; /* need more */
1.247 deraadt 1071: memcpy(&s5_req, p, sizeof(s5_req));
1.193 markus 1072: if (s5_req.version != 0x05 ||
1073: s5_req.command != SSH_SOCKS5_CONNECT ||
1074: s5_req.reserved != 0x00) {
1.194 markus 1075: debug2("channel %d: only socks5 connect supported", c->self);
1.193 markus 1076: return -1;
1077: }
1.213 deraadt 1078: switch (s5_req.atyp){
1.193 markus 1079: case SSH_SOCKS5_IPV4:
1080: addrlen = 4;
1081: af = AF_INET;
1082: break;
1083: case SSH_SOCKS5_DOMAIN:
1084: addrlen = p[sizeof(s5_req)];
1085: af = -1;
1086: break;
1087: case SSH_SOCKS5_IPV6:
1088: addrlen = 16;
1089: af = AF_INET6;
1090: break;
1091: default:
1.194 markus 1092: debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1.193 markus 1093: return -1;
1094: }
1.252 djm 1095: need = sizeof(s5_req) + addrlen + 2;
1096: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1097: need++;
1098: if (have < need)
1.193 markus 1099: return 0;
1100: buffer_consume(&c->input, sizeof(s5_req));
1101: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1102: buffer_consume(&c->input, 1); /* host string length */
1103: buffer_get(&c->input, (char *)&dest_addr, addrlen);
1104: buffer_get(&c->input, (char *)&dest_port, 2);
1105: dest_addr[addrlen] = '\0';
1106: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1.201 deraadt 1107: strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
1.193 markus 1108: else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1109: return -1;
1110: c->host_port = ntohs(dest_port);
1.198 djm 1111:
1.194 markus 1112: debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1.193 markus 1113: c->self, c->path, c->host_port, s5_req.command);
1114:
1115: s5_rsp.version = 0x05;
1116: s5_rsp.command = SSH_SOCKS5_SUCCESS;
1117: s5_rsp.reserved = 0; /* ignored */
1118: s5_rsp.atyp = SSH_SOCKS5_IPV4;
1119: ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1120: dest_port = 0; /* ignored */
1121:
1.246 deraadt 1122: buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1123: buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1124: buffer_append(&c->output, &dest_port, sizeof(dest_port));
1.193 markus 1125: return 1;
1126: }
1127:
1.104 markus 1128: /* dynamic port forwarding */
1.127 itojun 1129: static void
1.237 deraadt 1130: channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
1.104 markus 1131: {
1132: u_char *p;
1.217 djm 1133: u_int have;
1134: int ret;
1.103 markus 1135:
1.104 markus 1136: have = buffer_len(&c->input);
1.137 markus 1137: c->delayed = 0;
1.104 markus 1138: debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.105 markus 1139: /* buffer_dump(&c->input); */
1.104 markus 1140: /* check if the fixed size part of the packet is in buffer. */
1.193 markus 1141: if (have < 3) {
1.104 markus 1142: /* need more */
1143: FD_SET(c->sock, readset);
1144: return;
1145: }
1146: /* try to guess the protocol */
1147: p = buffer_ptr(&c->input);
1148: switch (p[0]) {
1149: case 0x04:
1150: ret = channel_decode_socks4(c, readset, writeset);
1.193 markus 1151: break;
1152: case 0x05:
1153: ret = channel_decode_socks5(c, readset, writeset);
1.104 markus 1154: break;
1155: default:
1156: ret = -1;
1157: break;
1158: }
1159: if (ret < 0) {
1.113 markus 1160: chan_mark_dead(c);
1.104 markus 1161: } else if (ret == 0) {
1162: debug2("channel %d: pre_dynamic: need more", c->self);
1163: /* need more */
1164: FD_SET(c->sock, readset);
1165: } else {
1166: /* switch to the next state */
1167: c->type = SSH_CHANNEL_OPENING;
1168: port_open_helper(c, "direct-tcpip");
1169: }
1.103 markus 1170: }
1171:
1.41 markus 1172: /* This is our fake X11 server socket. */
1.259 stevesk 1173: /* ARGSUSED */
1.127 itojun 1174: static void
1.237 deraadt 1175: channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1176: {
1.113 markus 1177: Channel *nc;
1.41 markus 1178: struct sockaddr addr;
1.162 stevesk 1179: int newsock;
1.41 markus 1180: socklen_t addrlen;
1.85 markus 1181: char buf[16384], *remote_ipaddr;
1.51 markus 1182: int remote_port;
1.25 markus 1183:
1.41 markus 1184: if (FD_ISSET(c->sock, readset)) {
1185: debug("X11 connection requested.");
1186: addrlen = sizeof(addr);
1187: newsock = accept(c->sock, &addr, &addrlen);
1.149 markus 1188: if (c->single_connection) {
1.194 markus 1189: debug2("single_connection: closing X11 listener.");
1.149 markus 1190: channel_close_fd(&c->sock);
1191: chan_mark_dead(c);
1192: }
1.41 markus 1193: if (newsock < 0) {
1194: error("accept: %.100s", strerror(errno));
1195: return;
1196: }
1.162 stevesk 1197: set_nodelay(newsock);
1.85 markus 1198: remote_ipaddr = get_peer_ipaddr(newsock);
1.51 markus 1199: remote_port = get_peer_port(newsock);
1.41 markus 1200: snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1.85 markus 1201: remote_ipaddr, remote_port);
1.51 markus 1202:
1.113 markus 1203: nc = channel_new("accepted x11 socket",
1.51 markus 1204: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1.190 markus 1205: c->local_window_max, c->local_maxpacket, 0, buf, 1);
1.51 markus 1206: if (compat20) {
1207: packet_start(SSH2_MSG_CHANNEL_OPEN);
1208: packet_put_cstring("x11");
1.113 markus 1209: packet_put_int(nc->self);
1.149 markus 1210: packet_put_int(nc->local_window_max);
1211: packet_put_int(nc->local_maxpacket);
1.85 markus 1212: /* originator ipaddr and port */
1213: packet_put_cstring(remote_ipaddr);
1.57 markus 1214: if (datafellows & SSH_BUG_X11FWD) {
1.194 markus 1215: debug2("ssh2 x11 bug compat mode");
1.57 markus 1216: } else {
1217: packet_put_int(remote_port);
1218: }
1.51 markus 1219: packet_send();
1220: } else {
1221: packet_start(SSH_SMSG_X11_OPEN);
1.113 markus 1222: packet_put_int(nc->self);
1.121 markus 1223: if (packet_get_protocol_flags() &
1224: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.125 markus 1225: packet_put_cstring(buf);
1.51 markus 1226: packet_send();
1227: }
1.85 markus 1228: xfree(remote_ipaddr);
1.41 markus 1229: }
1230: }
1.25 markus 1231:
1.127 itojun 1232: static void
1.103 markus 1233: port_open_helper(Channel *c, char *rtype)
1234: {
1235: int direct;
1236: char buf[1024];
1237: char *remote_ipaddr = get_peer_ipaddr(c->sock);
1.216 markus 1238: int remote_port = get_peer_port(c->sock);
1.103 markus 1239:
1240: direct = (strcmp(rtype, "direct-tcpip") == 0);
1241:
1242: snprintf(buf, sizeof buf,
1243: "%s: listening port %d for %.100s port %d, "
1244: "connect from %.200s port %d",
1245: rtype, c->listening_port, c->path, c->host_port,
1246: remote_ipaddr, remote_port);
1247:
1248: xfree(c->remote_name);
1249: c->remote_name = xstrdup(buf);
1250:
1251: if (compat20) {
1252: packet_start(SSH2_MSG_CHANNEL_OPEN);
1253: packet_put_cstring(rtype);
1254: packet_put_int(c->self);
1255: packet_put_int(c->local_window_max);
1256: packet_put_int(c->local_maxpacket);
1257: if (direct) {
1258: /* target host, port */
1259: packet_put_cstring(c->path);
1260: packet_put_int(c->host_port);
1261: } else {
1262: /* listen address, port */
1263: packet_put_cstring(c->path);
1264: packet_put_int(c->listening_port);
1265: }
1266: /* originator host and port */
1267: packet_put_cstring(remote_ipaddr);
1.216 markus 1268: packet_put_int((u_int)remote_port);
1.103 markus 1269: packet_send();
1270: } else {
1271: packet_start(SSH_MSG_PORT_OPEN);
1272: packet_put_int(c->self);
1273: packet_put_cstring(c->path);
1274: packet_put_int(c->host_port);
1.121 markus 1275: if (packet_get_protocol_flags() &
1276: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.103 markus 1277: packet_put_cstring(c->remote_name);
1278: packet_send();
1279: }
1280: xfree(remote_ipaddr);
1281: }
1282:
1.226 djm 1283: static void
1284: channel_set_reuseaddr(int fd)
1285: {
1286: int on = 1;
1287:
1288: /*
1289: * Set socket options.
1290: * Allow local port reuse in TIME_WAIT.
1291: */
1292: if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1293: error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1294: }
1295:
1.41 markus 1296: /*
1297: * This socket is listening for connections to a forwarded TCP/IP port.
1298: */
1.259 stevesk 1299: /* ARGSUSED */
1.127 itojun 1300: static void
1.237 deraadt 1301: channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1302: {
1.103 markus 1303: Channel *nc;
1.41 markus 1304: struct sockaddr addr;
1.113 markus 1305: int newsock, nextstate;
1.41 markus 1306: socklen_t addrlen;
1.103 markus 1307: char *rtype;
1.73 markus 1308:
1.41 markus 1309: if (FD_ISSET(c->sock, readset)) {
1310: debug("Connection to port %d forwarding "
1311: "to %.100s port %d requested.",
1312: c->listening_port, c->path, c->host_port);
1.103 markus 1313:
1.137 markus 1314: if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1315: nextstate = SSH_CHANNEL_OPENING;
1316: rtype = "forwarded-tcpip";
1317: } else {
1318: if (c->host_port == 0) {
1319: nextstate = SSH_CHANNEL_DYNAMIC;
1.138 markus 1320: rtype = "dynamic-tcpip";
1.137 markus 1321: } else {
1322: nextstate = SSH_CHANNEL_OPENING;
1323: rtype = "direct-tcpip";
1324: }
1325: }
1.103 markus 1326:
1.41 markus 1327: addrlen = sizeof(addr);
1328: newsock = accept(c->sock, &addr, &addrlen);
1329: if (newsock < 0) {
1330: error("accept: %.100s", strerror(errno));
1331: return;
1332: }
1.169 stevesk 1333: set_nodelay(newsock);
1.190 markus 1334: nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1335: c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1.103 markus 1336: nc->listening_port = c->listening_port;
1337: nc->host_port = c->host_port;
1338: strlcpy(nc->path, c->path, sizeof(nc->path));
1339:
1.137 markus 1340: if (nextstate == SSH_CHANNEL_DYNAMIC) {
1341: /*
1342: * do not call the channel_post handler until
1343: * this flag has been reset by a pre-handler.
1344: * otherwise the FD_ISSET calls might overflow
1345: */
1346: nc->delayed = 1;
1347: } else {
1.103 markus 1348: port_open_helper(nc, rtype);
1.137 markus 1349: }
1.41 markus 1350: }
1351: }
1.25 markus 1352:
1.41 markus 1353: /*
1354: * This is the authentication agent socket listening for connections from
1355: * clients.
1356: */
1.259 stevesk 1357: /* ARGSUSED */
1.127 itojun 1358: static void
1.237 deraadt 1359: channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1360: {
1.113 markus 1361: Channel *nc;
1362: int newsock;
1.41 markus 1363: struct sockaddr addr;
1364: socklen_t addrlen;
1.25 markus 1365:
1.41 markus 1366: if (FD_ISSET(c->sock, readset)) {
1367: addrlen = sizeof(addr);
1368: newsock = accept(c->sock, &addr, &addrlen);
1369: if (newsock < 0) {
1370: error("accept from auth socket: %.100s", strerror(errno));
1371: return;
1372: }
1.113 markus 1373: nc = channel_new("accepted auth socket",
1.73 markus 1374: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1375: c->local_window_max, c->local_maxpacket,
1.190 markus 1376: 0, "accepted auth socket", 1);
1.73 markus 1377: if (compat20) {
1378: packet_start(SSH2_MSG_CHANNEL_OPEN);
1379: packet_put_cstring("auth-agent@openssh.com");
1.113 markus 1380: packet_put_int(nc->self);
1.73 markus 1381: packet_put_int(c->local_window_max);
1382: packet_put_int(c->local_maxpacket);
1383: } else {
1384: packet_start(SSH_SMSG_AGENT_OPEN);
1.113 markus 1385: packet_put_int(nc->self);
1.73 markus 1386: }
1.41 markus 1387: packet_send();
1388: }
1389: }
1.25 markus 1390:
1.259 stevesk 1391: /* ARGSUSED */
1.127 itojun 1392: static void
1.237 deraadt 1393: channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
1.75 markus 1394: {
1.114 markus 1395: int err = 0;
1.129 stevesk 1396: socklen_t sz = sizeof(err);
1.114 markus 1397:
1.75 markus 1398: if (FD_ISSET(c->sock, writeset)) {
1.170 stevesk 1399: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1.114 markus 1400: err = errno;
1401: error("getsockopt SO_ERROR failed");
1402: }
1403: if (err == 0) {
1404: debug("channel %d: connected", c->self);
1405: c->type = SSH_CHANNEL_OPEN;
1406: if (compat20) {
1407: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1408: packet_put_int(c->remote_id);
1409: packet_put_int(c->self);
1410: packet_put_int(c->local_window);
1411: packet_put_int(c->local_maxpacket);
1412: } else {
1413: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1414: packet_put_int(c->remote_id);
1415: packet_put_int(c->self);
1416: }
1.75 markus 1417: } else {
1.114 markus 1418: debug("channel %d: not connected: %s",
1419: c->self, strerror(err));
1420: if (compat20) {
1421: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1422: packet_put_int(c->remote_id);
1423: packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1424: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1425: packet_put_cstring(strerror(err));
1426: packet_put_cstring("");
1427: }
1.75 markus 1428: } else {
1.114 markus 1429: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1430: packet_put_int(c->remote_id);
1.75 markus 1431: }
1.114 markus 1432: chan_mark_dead(c);
1.75 markus 1433: }
1.114 markus 1434: packet_send();
1.75 markus 1435: }
1436: }
1437:
1.259 stevesk 1438: /* ARGSUSED */
1.127 itojun 1439: static int
1.237 deraadt 1440: channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1441: {
1.214 markus 1442: char buf[CHAN_RBUF];
1.41 markus 1443: int len;
1.25 markus 1444:
1.118 markus 1445: if (c->rfd != -1 &&
1.41 markus 1446: FD_ISSET(c->rfd, readset)) {
1447: len = read(c->rfd, buf, sizeof(buf));
1.53 markus 1448: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1449: return 1;
1.41 markus 1450: if (len <= 0) {
1.194 markus 1451: debug2("channel %d: read<=0 rfd %d len %d",
1.41 markus 1452: c->self, c->rfd, len);
1.104 markus 1453: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1454: debug2("channel %d: not open", c->self);
1.113 markus 1455: chan_mark_dead(c);
1.103 markus 1456: return -1;
1.104 markus 1457: } else if (compat13) {
1.158 markus 1458: buffer_clear(&c->output);
1.41 markus 1459: c->type = SSH_CHANNEL_INPUT_DRAINING;
1.194 markus 1460: debug2("channel %d: input draining.", c->self);
1.25 markus 1461: } else {
1.41 markus 1462: chan_read_failed(c);
1.25 markus 1463: }
1.41 markus 1464: return -1;
1465: }
1.143 deraadt 1466: if (c->input_filter != NULL) {
1.66 markus 1467: if (c->input_filter(c, buf, len) == -1) {
1.194 markus 1468: debug2("channel %d: filter stops", c->self);
1.65 markus 1469: chan_read_failed(c);
1470: }
1.228 reyk 1471: } else if (c->datagram) {
1472: buffer_put_string(&c->input, buf, len);
1.65 markus 1473: } else {
1474: buffer_append(&c->input, buf, len);
1475: }
1.41 markus 1476: }
1477: return 1;
1478: }
1.241 deraadt 1479:
1.259 stevesk 1480: /* ARGSUSED */
1.127 itojun 1481: static int
1.237 deraadt 1482: channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1483: {
1.95 markus 1484: struct termios tio;
1.231 reyk 1485: u_char *data = NULL, *buf;
1.134 markus 1486: u_int dlen;
1.41 markus 1487: int len;
1.25 markus 1488:
1.41 markus 1489: /* Send buffered output data to the socket. */
1.118 markus 1490: if (c->wfd != -1 &&
1.41 markus 1491: FD_ISSET(c->wfd, writeset) &&
1492: buffer_len(&c->output) > 0) {
1.231 reyk 1493: if (c->output_filter != NULL) {
1494: if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1495: debug2("channel %d: filter stops", c->self);
1.232 reyk 1496: if (c->type != SSH_CHANNEL_OPEN)
1497: chan_mark_dead(c);
1498: else
1499: chan_write_failed(c);
1500: return -1;
1.231 reyk 1501: }
1502: } else if (c->datagram) {
1503: buf = data = buffer_get_string(&c->output, &dlen);
1504: } else {
1505: buf = data = buffer_ptr(&c->output);
1506: dlen = buffer_len(&c->output);
1507: }
1508:
1.228 reyk 1509: if (c->datagram) {
1510: /* ignore truncated writes, datagrams might get lost */
1511: c->local_consumed += dlen + 4;
1.231 reyk 1512: len = write(c->wfd, buf, dlen);
1.228 reyk 1513: xfree(data);
1514: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1515: return 1;
1516: if (len <= 0) {
1517: if (c->type != SSH_CHANNEL_OPEN)
1518: chan_mark_dead(c);
1519: else
1520: chan_write_failed(c);
1521: return -1;
1522: }
1523: return 1;
1524: }
1.231 reyk 1525:
1526: len = write(c->wfd, buf, dlen);
1.53 markus 1527: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1528: return 1;
1.41 markus 1529: if (len <= 0) {
1.104 markus 1530: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1531: debug2("channel %d: not open", c->self);
1.113 markus 1532: chan_mark_dead(c);
1.104 markus 1533: return -1;
1534: } else if (compat13) {
1.158 markus 1535: buffer_clear(&c->output);
1.194 markus 1536: debug2("channel %d: input draining.", c->self);
1.41 markus 1537: c->type = SSH_CHANNEL_INPUT_DRAINING;
1538: } else {
1539: chan_write_failed(c);
1540: }
1541: return -1;
1.91 markus 1542: }
1.231 reyk 1543: if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
1.91 markus 1544: if (tcgetattr(c->wfd, &tio) == 0 &&
1545: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1546: /*
1547: * Simulate echo to reduce the impact of
1.96 markus 1548: * traffic analysis. We need to match the
1.95 markus 1549: * size of a SSH2_MSG_CHANNEL_DATA message
1.231 reyk 1550: * (4 byte channel id + buf)
1.91 markus 1551: */
1.95 markus 1552: packet_send_ignore(4 + len);
1.91 markus 1553: packet_send();
1554: }
1.25 markus 1555: }
1.41 markus 1556: buffer_consume(&c->output, len);
1.44 markus 1557: if (compat20 && len > 0) {
1558: c->local_consumed += len;
1559: }
1560: }
1561: return 1;
1562: }
1.241 deraadt 1563:
1.127 itojun 1564: static int
1.237 deraadt 1565: channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
1.44 markus 1566: {
1.214 markus 1567: char buf[CHAN_RBUF];
1.44 markus 1568: int len;
1569:
1.45 markus 1570: /** XXX handle drain efd, too */
1.44 markus 1571: if (c->efd != -1) {
1572: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1573: FD_ISSET(c->efd, writeset) &&
1574: buffer_len(&c->extended) > 0) {
1575: len = write(c->efd, buffer_ptr(&c->extended),
1576: buffer_len(&c->extended));
1.70 markus 1577: debug2("channel %d: written %d to efd %d",
1.44 markus 1578: c->self, len, c->efd);
1.93 markus 1579: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1580: return 1;
1581: if (len <= 0) {
1582: debug2("channel %d: closing write-efd %d",
1583: c->self, c->efd);
1.132 markus 1584: channel_close_fd(&c->efd);
1.93 markus 1585: } else {
1.44 markus 1586: buffer_consume(&c->extended, len);
1587: c->local_consumed += len;
1588: }
1589: } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1590: FD_ISSET(c->efd, readset)) {
1591: len = read(c->efd, buf, sizeof(buf));
1.70 markus 1592: debug2("channel %d: read %d from efd %d",
1.148 deraadt 1593: c->self, len, c->efd);
1.93 markus 1594: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1595: return 1;
1596: if (len <= 0) {
1597: debug2("channel %d: closing read-efd %d",
1.45 markus 1598: c->self, c->efd);
1.132 markus 1599: channel_close_fd(&c->efd);
1.93 markus 1600: } else {
1.44 markus 1601: buffer_append(&c->extended, buf, len);
1.93 markus 1602: }
1.44 markus 1603: }
1604: }
1605: return 1;
1606: }
1.241 deraadt 1607:
1.259 stevesk 1608: /* ARGSUSED */
1.127 itojun 1609: static int
1.237 deraadt 1610: channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
1.204 djm 1611: {
1612: char buf[16];
1613: int len;
1614:
1615: /* Monitor control fd to detect if the slave client exits */
1616: if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1617: len = read(c->ctl_fd, buf, sizeof(buf));
1618: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1619: return 1;
1620: if (len <= 0) {
1621: debug2("channel %d: ctl read<=0", c->self);
1622: if (c->type != SSH_CHANNEL_OPEN) {
1623: debug2("channel %d: not open", c->self);
1624: chan_mark_dead(c);
1625: return -1;
1626: } else {
1627: chan_read_failed(c);
1628: chan_write_failed(c);
1629: }
1630: return -1;
1631: } else
1632: fatal("%s: unexpected data on ctl fd", __func__);
1633: }
1634: return 1;
1635: }
1.241 deraadt 1636:
1.204 djm 1637: static int
1.93 markus 1638: channel_check_window(Channel *c)
1.44 markus 1639: {
1.104 markus 1640: if (c->type == SSH_CHANNEL_OPEN &&
1641: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.44 markus 1642: c->local_window < c->local_window_max/2 &&
1643: c->local_consumed > 0) {
1644: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1645: packet_put_int(c->remote_id);
1646: packet_put_int(c->local_consumed);
1647: packet_send();
1.70 markus 1648: debug2("channel %d: window %d sent adjust %d",
1.44 markus 1649: c->self, c->local_window,
1650: c->local_consumed);
1651: c->local_window += c->local_consumed;
1652: c->local_consumed = 0;
1.1 deraadt 1653: }
1.41 markus 1654: return 1;
1.1 deraadt 1655: }
1656:
1.127 itojun 1657: static void
1.237 deraadt 1658: channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1659: {
1.137 markus 1660: if (c->delayed)
1661: return;
1.41 markus 1662: channel_handle_rfd(c, readset, writeset);
1663: channel_handle_wfd(c, readset, writeset);
1.157 markus 1664: if (!compat20)
1.137 markus 1665: return;
1.44 markus 1666: channel_handle_efd(c, readset, writeset);
1.204 djm 1667: channel_handle_ctl(c, readset, writeset);
1.93 markus 1668: channel_check_window(c);
1.44 markus 1669: }
1670:
1.259 stevesk 1671: /* ARGSUSED */
1.127 itojun 1672: static void
1.237 deraadt 1673: channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
1.1 deraadt 1674: {
1.41 markus 1675: int len;
1.180 deraadt 1676:
1.41 markus 1677: /* Send buffered output data to the socket. */
1678: if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1679: len = write(c->sock, buffer_ptr(&c->output),
1680: buffer_len(&c->output));
1681: if (len <= 0)
1.158 markus 1682: buffer_clear(&c->output);
1.41 markus 1683: else
1684: buffer_consume(&c->output, len);
1685: }
1686: }
1.25 markus 1687:
1.127 itojun 1688: static void
1.44 markus 1689: channel_handler_init_20(void)
1690: {
1.157 markus 1691: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1692: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.44 markus 1693: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1.73 markus 1694: channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1.51 markus 1695: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1.73 markus 1696: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1697: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1698: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.44 markus 1699:
1.157 markus 1700: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.44 markus 1701: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1.73 markus 1702: channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1.51 markus 1703: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1.73 markus 1704: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.75 markus 1705: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1706: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.44 markus 1707: }
1708:
1.127 itojun 1709: static void
1.41 markus 1710: channel_handler_init_13(void)
1711: {
1712: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1713: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1714: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1715: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1716: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1717: channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1718: channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1.75 markus 1719: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1720: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1721:
1.157 markus 1722: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.41 markus 1723: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1724: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1725: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1726: channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1.75 markus 1727: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1728: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1729: }
1.25 markus 1730:
1.127 itojun 1731: static void
1.41 markus 1732: channel_handler_init_15(void)
1733: {
1.157 markus 1734: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1735: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.41 markus 1736: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1737: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1738: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1739: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1740: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1741:
1.41 markus 1742: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1743: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1744: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.157 markus 1745: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.75 markus 1746: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1747: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1748: }
1.27 markus 1749:
1.127 itojun 1750: static void
1.41 markus 1751: channel_handler_init(void)
1752: {
1753: int i;
1.180 deraadt 1754:
1.148 deraadt 1755: for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1.41 markus 1756: channel_pre[i] = NULL;
1757: channel_post[i] = NULL;
1758: }
1.44 markus 1759: if (compat20)
1760: channel_handler_init_20();
1761: else if (compat13)
1.41 markus 1762: channel_handler_init_13();
1763: else
1764: channel_handler_init_15();
1765: }
1.25 markus 1766:
1.140 markus 1767: /* gc dead channels */
1768: static void
1769: channel_garbage_collect(Channel *c)
1770: {
1771: if (c == NULL)
1772: return;
1773: if (c->detach_user != NULL) {
1.225 djm 1774: if (!chan_is_dead(c, c->detach_close))
1.140 markus 1775: return;
1.194 markus 1776: debug2("channel %d: gc: notify user", c->self);
1.140 markus 1777: c->detach_user(c->self, NULL);
1778: /* if we still have a callback */
1779: if (c->detach_user != NULL)
1780: return;
1.194 markus 1781: debug2("channel %d: gc: user detached", c->self);
1.140 markus 1782: }
1783: if (!chan_is_dead(c, 1))
1784: return;
1.194 markus 1785: debug2("channel %d: garbage collecting", c->self);
1.140 markus 1786: channel_free(c);
1787: }
1788:
1.127 itojun 1789: static void
1.237 deraadt 1790: channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
1.41 markus 1791: {
1792: static int did_init = 0;
1.209 avsm 1793: u_int i;
1.41 markus 1794: Channel *c;
1.25 markus 1795:
1.41 markus 1796: if (!did_init) {
1797: channel_handler_init();
1798: did_init = 1;
1799: }
1800: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1801: c = channels[i];
1802: if (c == NULL)
1.41 markus 1803: continue;
1.118 markus 1804: if (ftab[c->type] != NULL)
1805: (*ftab[c->type])(c, readset, writeset);
1.140 markus 1806: channel_garbage_collect(c);
1.1 deraadt 1807: }
1808: }
1809:
1.121 markus 1810: /*
1811: * Allocate/update select bitmasks and add any bits relevant to channels in
1812: * select bitmasks.
1813: */
1.49 markus 1814: void
1.100 markus 1815: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1.209 avsm 1816: u_int *nallocp, int rekeying)
1.41 markus 1817: {
1.243 djm 1818: u_int n, sz, nfdset;
1.84 markus 1819:
1820: n = MAX(*maxfdp, channel_max_fd);
1821:
1.243 djm 1822: nfdset = howmany(n+1, NFDBITS);
1823: /* Explicitly test here, because xrealloc isn't always called */
1824: if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1825: fatal("channel_prepare_select: max_fd (%d) is too large", n);
1826: sz = nfdset * sizeof(fd_mask);
1827:
1.132 markus 1828: /* perhaps check sz < nalloc/2 and shrink? */
1829: if (*readsetp == NULL || sz > *nallocp) {
1.243 djm 1830: *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1831: *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
1.132 markus 1832: *nallocp = sz;
1.84 markus 1833: }
1.132 markus 1834: *maxfdp = n;
1.84 markus 1835: memset(*readsetp, 0, sz);
1836: memset(*writesetp, 0, sz);
1837:
1.100 markus 1838: if (!rekeying)
1839: channel_handler(channel_pre, *readsetp, *writesetp);
1.41 markus 1840: }
1841:
1.121 markus 1842: /*
1843: * After select, perform any appropriate operations for channels which have
1844: * events pending.
1845: */
1.49 markus 1846: void
1.237 deraadt 1847: channel_after_select(fd_set *readset, fd_set *writeset)
1.41 markus 1848: {
1849: channel_handler(channel_post, readset, writeset);
1850: }
1851:
1.121 markus 1852:
1.84 markus 1853: /* If there is data to send to the connection, enqueue some of it now. */
1.49 markus 1854: void
1.142 itojun 1855: channel_output_poll(void)
1.1 deraadt 1856: {
1.41 markus 1857: Channel *c;
1.209 avsm 1858: u_int i, len;
1.1 deraadt 1859:
1.25 markus 1860: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1861: c = channels[i];
1862: if (c == NULL)
1863: continue;
1.37 markus 1864:
1.121 markus 1865: /*
1866: * We are only interested in channels that can have buffered
1867: * incoming data.
1868: */
1.37 markus 1869: if (compat13) {
1.41 markus 1870: if (c->type != SSH_CHANNEL_OPEN &&
1871: c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37 markus 1872: continue;
1873: } else {
1.41 markus 1874: if (c->type != SSH_CHANNEL_OPEN)
1.37 markus 1875: continue;
1876: }
1.46 markus 1877: if (compat20 &&
1878: (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 1879: /* XXX is this true? */
1.140 markus 1880: debug3("channel %d: will not send data after close", c->self);
1.44 markus 1881: continue;
1882: }
1.25 markus 1883:
1884: /* Get the amount of buffered data for this channel. */
1.93 markus 1885: if ((c->istate == CHAN_INPUT_OPEN ||
1886: c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1887: (len = buffer_len(&c->input)) > 0) {
1.228 reyk 1888: if (c->datagram) {
1889: if (len > 0) {
1890: u_char *data;
1891: u_int dlen;
1892:
1893: data = buffer_get_string(&c->input,
1894: &dlen);
1895: packet_start(SSH2_MSG_CHANNEL_DATA);
1896: packet_put_int(c->remote_id);
1897: packet_put_string(data, dlen);
1898: packet_send();
1899: c->remote_window -= dlen + 4;
1900: xfree(data);
1901: }
1902: continue;
1903: }
1.121 markus 1904: /*
1905: * Send some data for the other side over the secure
1906: * connection.
1907: */
1.44 markus 1908: if (compat20) {
1909: if (len > c->remote_window)
1910: len = c->remote_window;
1911: if (len > c->remote_maxpacket)
1912: len = c->remote_maxpacket;
1.25 markus 1913: } else {
1.44 markus 1914: if (packet_is_interactive()) {
1915: if (len > 1024)
1916: len = 512;
1917: } else {
1918: /* Keep the packets at reasonable size. */
1919: if (len > packet_get_maxsize()/2)
1920: len = packet_get_maxsize()/2;
1921: }
1.25 markus 1922: }
1.41 markus 1923: if (len > 0) {
1.44 markus 1924: packet_start(compat20 ?
1925: SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41 markus 1926: packet_put_int(c->remote_id);
1927: packet_put_string(buffer_ptr(&c->input), len);
1928: packet_send();
1929: buffer_consume(&c->input, len);
1930: c->remote_window -= len;
1931: }
1932: } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25 markus 1933: if (compat13)
1934: fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27 markus 1935: /*
1936: * input-buffer is empty and read-socket shutdown:
1.172 markus 1937: * tell peer, that we will not send more data: send IEOF.
1938: * hack for extended data: delay EOF if EFD still in use.
1.27 markus 1939: */
1.172 markus 1940: if (CHANNEL_EFD_INPUT_ACTIVE(c))
1.223 djm 1941: debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1942: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 1943: else
1944: chan_ibuf_empty(c);
1.25 markus 1945: }
1.44 markus 1946: /* Send extended data, i.e. stderr */
1947: if (compat20 &&
1.172 markus 1948: !(c->flags & CHAN_EOF_SENT) &&
1.44 markus 1949: c->remote_window > 0 &&
1950: (len = buffer_len(&c->extended)) > 0 &&
1951: c->extended_usage == CHAN_EXTENDED_READ) {
1.178 markus 1952: debug2("channel %d: rwin %u elen %u euse %d",
1.93 markus 1953: c->self, c->remote_window, buffer_len(&c->extended),
1954: c->extended_usage);
1.44 markus 1955: if (len > c->remote_window)
1956: len = c->remote_window;
1957: if (len > c->remote_maxpacket)
1958: len = c->remote_maxpacket;
1959: packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1960: packet_put_int(c->remote_id);
1961: packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1962: packet_put_string(buffer_ptr(&c->extended), len);
1963: packet_send();
1964: buffer_consume(&c->extended, len);
1965: c->remote_window -= len;
1.93 markus 1966: debug2("channel %d: sent ext data %d", c->self, len);
1.44 markus 1967: }
1.25 markus 1968: }
1.1 deraadt 1969: }
1970:
1.121 markus 1971:
1972: /* -- protocol input */
1.249 djm 1973:
1974: /* ARGSUSED */
1.49 markus 1975: void
1.154 markus 1976: channel_input_data(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 1977: {
1.37 markus 1978: int id;
1.25 markus 1979: char *data;
1.77 markus 1980: u_int data_len;
1.41 markus 1981: Channel *c;
1.25 markus 1982:
1983: /* Get the channel number and verify it. */
1.37 markus 1984: id = packet_get_int();
1.41 markus 1985: c = channel_lookup(id);
1986: if (c == NULL)
1.37 markus 1987: packet_disconnect("Received data for nonexistent channel %d.", id);
1.25 markus 1988:
1989: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 1990: if (c->type != SSH_CHANNEL_OPEN &&
1991: c->type != SSH_CHANNEL_X11_OPEN)
1.37 markus 1992: return;
1993:
1.25 markus 1994: /* Get the data. */
1995: data = packet_get_string(&data_len);
1.200 markus 1996:
1997: /*
1998: * Ignore data for protocol > 1.3 if output end is no longer open.
1999: * For protocol 2 the sending side is reducing its window as it sends
2000: * data, so we must 'fake' consumption of the data in order to ensure
2001: * that window updates are sent back. Otherwise the connection might
2002: * deadlock.
2003: */
2004: if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2005: if (compat20) {
2006: c->local_window -= data_len;
2007: c->local_consumed += data_len;
2008: }
2009: xfree(data);
2010: return;
2011: }
1.41 markus 2012:
1.143 deraadt 2013: if (compat20) {
1.44 markus 2014: if (data_len > c->local_maxpacket) {
1.188 itojun 2015: logit("channel %d: rcvd big packet %d, maxpack %d",
1.44 markus 2016: c->self, data_len, c->local_maxpacket);
2017: }
2018: if (data_len > c->local_window) {
1.188 itojun 2019: logit("channel %d: rcvd too much data %d, win %d",
1.44 markus 2020: c->self, data_len, c->local_window);
2021: xfree(data);
2022: return;
2023: }
2024: c->local_window -= data_len;
2025: }
1.152 markus 2026: packet_check_eom();
1.228 reyk 2027: if (c->datagram)
2028: buffer_put_string(&c->output, data, data_len);
2029: else
2030: buffer_append(&c->output, data, data_len);
1.25 markus 2031: xfree(data);
1.1 deraadt 2032: }
1.121 markus 2033:
1.249 djm 2034: /* ARGSUSED */
1.49 markus 2035: void
1.154 markus 2036: channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1.44 markus 2037: {
2038: int id;
2039: char *data;
1.177 markus 2040: u_int data_len, tcode;
1.44 markus 2041: Channel *c;
2042:
2043: /* Get the channel number and verify it. */
2044: id = packet_get_int();
2045: c = channel_lookup(id);
2046:
2047: if (c == NULL)
2048: packet_disconnect("Received extended_data for bad channel %d.", id);
2049: if (c->type != SSH_CHANNEL_OPEN) {
1.188 itojun 2050: logit("channel %d: ext data for non open", id);
1.44 markus 2051: return;
1.172 markus 2052: }
2053: if (c->flags & CHAN_EOF_RCVD) {
2054: if (datafellows & SSH_BUG_EXTEOF)
2055: debug("channel %d: accepting ext data after eof", id);
2056: else
2057: packet_disconnect("Received extended_data after EOF "
2058: "on channel %d.", id);
1.44 markus 2059: }
2060: tcode = packet_get_int();
2061: if (c->efd == -1 ||
2062: c->extended_usage != CHAN_EXTENDED_WRITE ||
2063: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 2064: logit("channel %d: bad ext data", c->self);
1.44 markus 2065: return;
2066: }
2067: data = packet_get_string(&data_len);
1.152 markus 2068: packet_check_eom();
1.44 markus 2069: if (data_len > c->local_window) {
1.188 itojun 2070: logit("channel %d: rcvd too much extended_data %d, win %d",
1.44 markus 2071: c->self, data_len, c->local_window);
2072: xfree(data);
2073: return;
2074: }
1.70 markus 2075: debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44 markus 2076: c->local_window -= data_len;
2077: buffer_append(&c->extended, data, data_len);
2078: xfree(data);
2079: }
2080:
1.249 djm 2081: /* ARGSUSED */
1.49 markus 2082: void
1.154 markus 2083: channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1.41 markus 2084: {
2085: int id;
2086: Channel *c;
2087:
2088: id = packet_get_int();
1.152 markus 2089: packet_check_eom();
1.41 markus 2090: c = channel_lookup(id);
2091: if (c == NULL)
2092: packet_disconnect("Received ieof for nonexistent channel %d.", id);
2093: chan_rcvd_ieof(c);
1.133 markus 2094:
2095: /* XXX force input close */
1.156 markus 2096: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 2097: debug("channel %d: FORCE input drain", c->self);
2098: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.161 markus 2099: if (buffer_len(&c->input) == 0)
2100: chan_ibuf_empty(c);
1.133 markus 2101: }
2102:
1.41 markus 2103: }
1.1 deraadt 2104:
1.249 djm 2105: /* ARGSUSED */
1.49 markus 2106: void
1.154 markus 2107: channel_input_close(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2108: {
1.41 markus 2109: int id;
2110: Channel *c;
1.1 deraadt 2111:
1.41 markus 2112: id = packet_get_int();
1.152 markus 2113: packet_check_eom();
1.41 markus 2114: c = channel_lookup(id);
2115: if (c == NULL)
2116: packet_disconnect("Received close for nonexistent channel %d.", id);
1.27 markus 2117:
2118: /*
2119: * Send a confirmation that we have closed the channel and no more
2120: * data is coming for it.
2121: */
1.25 markus 2122: packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41 markus 2123: packet_put_int(c->remote_id);
1.25 markus 2124: packet_send();
2125:
1.27 markus 2126: /*
2127: * If the channel is in closed state, we have sent a close request,
2128: * and the other side will eventually respond with a confirmation.
2129: * Thus, we cannot free the channel here, because then there would be
2130: * no-one to receive the confirmation. The channel gets freed when
2131: * the confirmation arrives.
2132: */
1.41 markus 2133: if (c->type != SSH_CHANNEL_CLOSED) {
1.27 markus 2134: /*
2135: * Not a closed channel - mark it as draining, which will
2136: * cause it to be freed later.
2137: */
1.158 markus 2138: buffer_clear(&c->input);
1.41 markus 2139: c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25 markus 2140: }
1.1 deraadt 2141: }
2142:
1.41 markus 2143: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.249 djm 2144: /* ARGSUSED */
1.49 markus 2145: void
1.154 markus 2146: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41 markus 2147: {
2148: int id = packet_get_int();
2149: Channel *c = channel_lookup(id);
1.151 markus 2150:
1.152 markus 2151: packet_check_eom();
1.41 markus 2152: if (c == NULL)
2153: packet_disconnect("Received oclose for nonexistent channel %d.", id);
2154: chan_rcvd_oclose(c);
2155: }
1.1 deraadt 2156:
1.249 djm 2157: /* ARGSUSED */
1.49 markus 2158: void
1.154 markus 2159: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2160: {
1.41 markus 2161: int id = packet_get_int();
2162: Channel *c = channel_lookup(id);
1.1 deraadt 2163:
1.152 markus 2164: packet_check_eom();
1.41 markus 2165: if (c == NULL)
2166: packet_disconnect("Received close confirmation for "
2167: "out-of-range channel %d.", id);
2168: if (c->type != SSH_CHANNEL_CLOSED)
2169: packet_disconnect("Received close confirmation for "
2170: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 2171: channel_free(c);
1.1 deraadt 2172: }
2173:
1.249 djm 2174: /* ARGSUSED */
1.49 markus 2175: void
1.154 markus 2176: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2177: {
1.41 markus 2178: int id, remote_id;
2179: Channel *c;
1.1 deraadt 2180:
1.41 markus 2181: id = packet_get_int();
2182: c = channel_lookup(id);
1.25 markus 2183:
1.41 markus 2184: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2185: packet_disconnect("Received open confirmation for "
2186: "non-opening channel %d.", id);
2187: remote_id = packet_get_int();
1.27 markus 2188: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 2189: c->remote_id = remote_id;
2190: c->type = SSH_CHANNEL_OPEN;
1.44 markus 2191:
2192: if (compat20) {
2193: c->remote_window = packet_get_int();
2194: c->remote_maxpacket = packet_get_int();
1.165 markus 2195: if (c->confirm) {
1.70 markus 2196: debug2("callback start");
1.204 djm 2197: c->confirm(c->self, c->confirm_ctx);
1.70 markus 2198: debug2("callback done");
1.44 markus 2199: }
1.194 markus 2200: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
1.44 markus 2201: c->remote_window, c->remote_maxpacket);
2202: }
1.152 markus 2203: packet_check_eom();
1.1 deraadt 2204: }
2205:
1.127 itojun 2206: static char *
1.114 markus 2207: reason2txt(int reason)
2208: {
1.143 deraadt 2209: switch (reason) {
1.114 markus 2210: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2211: return "administratively prohibited";
2212: case SSH2_OPEN_CONNECT_FAILED:
2213: return "connect failed";
2214: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2215: return "unknown channel type";
2216: case SSH2_OPEN_RESOURCE_SHORTAGE:
2217: return "resource shortage";
2218: }
1.117 stevesk 2219: return "unknown reason";
1.114 markus 2220: }
2221:
1.249 djm 2222: /* ARGSUSED */
1.49 markus 2223: void
1.154 markus 2224: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2225: {
1.86 markus 2226: int id, reason;
2227: char *msg = NULL, *lang = NULL;
1.41 markus 2228: Channel *c;
2229:
2230: id = packet_get_int();
2231: c = channel_lookup(id);
1.25 markus 2232:
1.41 markus 2233: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2234: packet_disconnect("Received open failure for "
2235: "non-opening channel %d.", id);
1.44 markus 2236: if (compat20) {
1.86 markus 2237: reason = packet_get_int();
1.110 markus 2238: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86 markus 2239: msg = packet_get_string(NULL);
2240: lang = packet_get_string(NULL);
2241: }
1.188 itojun 2242: logit("channel %d: open failed: %s%s%s", id,
1.114 markus 2243: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86 markus 2244: if (msg != NULL)
2245: xfree(msg);
2246: if (lang != NULL)
2247: xfree(lang);
1.44 markus 2248: }
1.152 markus 2249: packet_check_eom();
1.25 markus 2250: /* Free the channel. This will also close the socket. */
1.113 markus 2251: channel_free(c);
1.44 markus 2252: }
2253:
1.249 djm 2254: /* ARGSUSED */
1.121 markus 2255: void
1.154 markus 2256: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121 markus 2257: {
2258: Channel *c;
1.178 markus 2259: int id;
2260: u_int adjust;
1.121 markus 2261:
2262: if (!compat20)
2263: return;
2264:
2265: /* Get the channel number and verify it. */
2266: id = packet_get_int();
2267: c = channel_lookup(id);
1.1 deraadt 2268:
1.229 markus 2269: if (c == NULL) {
2270: logit("Received window adjust for non-open channel %d.", id);
1.121 markus 2271: return;
2272: }
2273: adjust = packet_get_int();
1.152 markus 2274: packet_check_eom();
1.178 markus 2275: debug2("channel %d: rcvd adjust %u", id, adjust);
1.121 markus 2276: c->remote_window += adjust;
2277: }
1.1 deraadt 2278:
1.249 djm 2279: /* ARGSUSED */
1.121 markus 2280: void
1.154 markus 2281: channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2282: {
1.121 markus 2283: Channel *c = NULL;
2284: u_short host_port;
2285: char *host, *originator_string;
2286: int remote_id, sock = -1;
2287:
2288: remote_id = packet_get_int();
2289: host = packet_get_string(NULL);
2290: host_port = packet_get_int();
1.25 markus 2291:
1.121 markus 2292: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2293: originator_string = packet_get_string(NULL);
2294: } else {
2295: originator_string = xstrdup("unknown (remote did not supply name)");
2296: }
1.152 markus 2297: packet_check_eom();
1.121 markus 2298: sock = channel_connect_to(host, host_port);
2299: if (sock != -1) {
2300: c = channel_new("connected socket",
2301: SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2302: originator_string, 1);
1.167 markus 2303: c->remote_id = remote_id;
1.25 markus 2304: }
1.190 markus 2305: xfree(originator_string);
1.121 markus 2306: if (c == NULL) {
2307: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2308: packet_put_int(remote_id);
2309: packet_send();
2310: }
2311: xfree(host);
1.1 deraadt 2312: }
2313:
1.121 markus 2314:
2315: /* -- tcp forwarding */
1.135 markus 2316:
2317: void
2318: channel_set_af(int af)
2319: {
2320: IPv4or6 = af;
2321: }
1.121 markus 2322:
1.160 markus 2323: static int
2324: channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2325: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
1.25 markus 2326: {
1.113 markus 2327: Channel *c;
1.226 djm 2328: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 2329: struct addrinfo hints, *ai, *aitop;
1.212 djm 2330: const char *host, *addr;
1.35 markus 2331: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.25 markus 2332:
1.160 markus 2333: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2334: listen_addr : host_to_connect;
1.212 djm 2335: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 2336:
1.160 markus 2337: if (host == NULL) {
2338: error("No forward host name.");
1.218 markus 2339: return 0;
1.73 markus 2340: }
1.113 markus 2341: if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
1.87 markus 2342: error("Forward host name too long.");
1.218 markus 2343: return 0;
1.87 markus 2344: }
1.25 markus 2345:
1.28 markus 2346: /*
1.212 djm 2347: * Determine whether or not a port forward listens to loopback,
1.213 deraadt 2348: * specified address or wildcard. On the client, a specified bind
2349: * address will always override gateway_ports. On the server, a
2350: * gateway_ports of 1 (``yes'') will override the client's
2351: * specification and force a wildcard bind, whereas a value of 2
2352: * (``clientspecified'') will bind to whatever address the client
1.212 djm 2353: * asked for.
2354: *
2355: * Special-case listen_addrs are:
2356: *
2357: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2358: * "" (empty string), "*" -> wildcard v4/v6
2359: * "localhost" -> loopback v4/v6
2360: */
2361: addr = NULL;
2362: if (listen_addr == NULL) {
2363: /* No address specified: default to gateway_ports setting */
2364: if (gateway_ports)
2365: wildcard = 1;
2366: } else if (gateway_ports || is_client) {
2367: if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2368: strcmp(listen_addr, "0.0.0.0") == 0) ||
2369: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2370: (!is_client && gateway_ports == 1))
2371: wildcard = 1;
2372: else if (strcmp(listen_addr, "localhost") != 0)
2373: addr = listen_addr;
2374: }
2375:
2376: debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2377: type, wildcard, (addr == NULL) ? "NULL" : addr);
2378:
2379: /*
1.35 markus 2380: * getaddrinfo returns a loopback address if the hostname is
2381: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2382: */
1.35 markus 2383: memset(&hints, 0, sizeof(hints));
2384: hints.ai_family = IPv4or6;
1.212 djm 2385: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 2386: hints.ai_socktype = SOCK_STREAM;
1.73 markus 2387: snprintf(strport, sizeof strport, "%d", listen_port);
1.212 djm 2388: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2389: if (addr == NULL) {
2390: /* This really shouldn't happen */
2391: packet_disconnect("getaddrinfo: fatal error: %s",
2392: gai_strerror(r));
2393: } else {
1.218 markus 2394: error("channel_setup_fwd_listener: "
1.212 djm 2395: "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2396: }
1.218 markus 2397: return 0;
1.212 djm 2398: }
1.35 markus 2399:
2400: for (ai = aitop; ai; ai = ai->ai_next) {
2401: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2402: continue;
2403: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2404: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.160 markus 2405: error("channel_setup_fwd_listener: getnameinfo failed");
1.35 markus 2406: continue;
2407: }
2408: /* Create a port to listen for the host. */
1.189 markus 2409: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 2410: if (sock < 0) {
2411: /* this is no error since kernel may not support ipv6 */
2412: verbose("socket: %.100s", strerror(errno));
2413: continue;
2414: }
1.226 djm 2415:
2416: channel_set_reuseaddr(sock);
1.182 stevesk 2417:
1.35 markus 2418: debug("Local forwarding listening on %s port %s.", ntop, strport);
2419:
2420: /* Bind the socket to the address. */
2421: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2422: /* address can be in use ipv6 address is already bound */
2423: verbose("bind: %.100s", strerror(errno));
2424: close(sock);
2425: continue;
2426: }
2427: /* Start listening for connections on the socket. */
1.199 markus 2428: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2429: error("listen: %.100s", strerror(errno));
2430: close(sock);
2431: continue;
2432: }
2433: /* Allocate a channel number for the socket. */
1.121 markus 2434: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2435: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 2436: 0, "port listener", 1);
1.113 markus 2437: strlcpy(c->path, host, sizeof(c->path));
2438: c->host_port = port_to_connect;
2439: c->listening_port = listen_port;
1.35 markus 2440: success = 1;
2441: }
2442: if (success == 0)
1.160 markus 2443: error("channel_setup_fwd_listener: cannot listen to port: %d",
1.87 markus 2444: listen_port);
1.35 markus 2445: freeaddrinfo(aitop);
1.87 markus 2446: return success;
1.25 markus 2447: }
1.1 deraadt 2448:
1.202 djm 2449: int
2450: channel_cancel_rport_listener(const char *host, u_short port)
2451: {
1.209 avsm 2452: u_int i;
2453: int found = 0;
1.202 djm 2454:
1.213 deraadt 2455: for (i = 0; i < channels_alloc; i++) {
1.202 djm 2456: Channel *c = channels[i];
2457:
2458: if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2459: strncmp(c->path, host, sizeof(c->path)) == 0 &&
1.208 deraadt 2460: c->listening_port == port) {
1.210 djm 2461: debug2("%s: close channel %d", __func__, i);
1.202 djm 2462: channel_free(c);
2463: found = 1;
2464: }
2465: }
2466:
2467: return (found);
2468: }
2469:
1.160 markus 2470: /* protocol local port fwd, used by ssh (and sshd in v1) */
2471: int
1.212 djm 2472: channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
1.160 markus 2473: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2474: {
2475: return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
1.212 djm 2476: listen_host, listen_port, host_to_connect, port_to_connect,
2477: gateway_ports);
1.160 markus 2478: }
2479:
2480: /* protocol v2 remote port fwd, used by sshd */
2481: int
2482: channel_setup_remote_fwd_listener(const char *listen_address,
2483: u_short listen_port, int gateway_ports)
2484: {
2485: return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2486: listen_address, listen_port, NULL, 0, gateway_ports);
2487: }
2488:
1.27 markus 2489: /*
2490: * Initiate forwarding of connections to port "port" on remote host through
2491: * the secure channel to host:port from local side.
2492: */
1.1 deraadt 2493:
1.253 markus 2494: int
1.212 djm 2495: channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
1.73 markus 2496: const char *host_to_connect, u_short port_to_connect)
1.25 markus 2497: {
1.153 markus 2498: int type, success = 0;
1.73 markus 2499:
1.25 markus 2500: /* Record locally that connection to this host/port is permitted. */
2501: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2502: fatal("channel_request_remote_forwarding: too many forwards");
2503:
2504: /* Send the forward request to the remote side. */
1.44 markus 2505: if (compat20) {
1.212 djm 2506: const char *address_to_bind;
2507: if (listen_host == NULL)
2508: address_to_bind = "localhost";
2509: else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2510: address_to_bind = "";
2511: else
2512: address_to_bind = listen_host;
2513:
1.44 markus 2514: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2515: packet_put_cstring("tcpip-forward");
1.173 markus 2516: packet_put_char(1); /* boolean: want reply */
1.44 markus 2517: packet_put_cstring(address_to_bind);
2518: packet_put_int(listen_port);
1.73 markus 2519: packet_send();
2520: packet_write_wait();
2521: /* Assume that server accepts the request */
2522: success = 1;
1.44 markus 2523: } else {
2524: packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50 markus 2525: packet_put_int(listen_port);
2526: packet_put_cstring(host_to_connect);
1.44 markus 2527: packet_put_int(port_to_connect);
2528: packet_send();
2529: packet_write_wait();
1.73 markus 2530:
2531: /* Wait for response from the remote side. */
1.153 markus 2532: type = packet_read();
1.73 markus 2533: switch (type) {
2534: case SSH_SMSG_SUCCESS:
2535: success = 1;
2536: break;
2537: case SSH_SMSG_FAILURE:
2538: break;
2539: default:
2540: /* Unknown packet */
2541: packet_disconnect("Protocol error for port forward request:"
2542: "received packet type %d.", type);
2543: }
2544: }
2545: if (success) {
2546: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2547: permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2548: permitted_opens[num_permitted_opens].listen_port = listen_port;
2549: num_permitted_opens++;
1.44 markus 2550: }
1.253 markus 2551: return (success ? 0 : -1);
1.1 deraadt 2552: }
2553:
1.27 markus 2554: /*
1.208 deraadt 2555: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 2556: * local side.
2557: */
2558: void
1.212 djm 2559: channel_request_rforward_cancel(const char *host, u_short port)
1.202 djm 2560: {
2561: int i;
2562:
2563: if (!compat20)
2564: return;
2565:
2566: for (i = 0; i < num_permitted_opens; i++) {
1.208 deraadt 2567: if (permitted_opens[i].host_to_connect != NULL &&
1.202 djm 2568: permitted_opens[i].listen_port == port)
2569: break;
2570: }
2571: if (i >= num_permitted_opens) {
2572: debug("%s: requested forward not found", __func__);
2573: return;
2574: }
2575: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2576: packet_put_cstring("cancel-tcpip-forward");
2577: packet_put_char(0);
1.212 djm 2578: packet_put_cstring(host == NULL ? "" : host);
1.202 djm 2579: packet_put_int(port);
2580: packet_send();
2581:
2582: permitted_opens[i].listen_port = 0;
2583: permitted_opens[i].port_to_connect = 0;
1.227 stevesk 2584: xfree(permitted_opens[i].host_to_connect);
1.202 djm 2585: permitted_opens[i].host_to_connect = NULL;
2586: }
2587:
2588: /*
1.27 markus 2589: * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2590: * listening for the port, and sends back a success reply (or disconnect
1.253 markus 2591: * message if there was an error).
1.27 markus 2592: */
1.253 markus 2593: int
1.56 markus 2594: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1 deraadt 2595: {
1.31 markus 2596: u_short port, host_port;
1.253 markus 2597: int success = 0;
1.25 markus 2598: char *hostname;
1.1 deraadt 2599:
1.25 markus 2600: /* Get arguments from the packet. */
2601: port = packet_get_int();
2602: hostname = packet_get_string(NULL);
2603: host_port = packet_get_int();
2604:
1.27 markus 2605: /*
2606: * Check that an unprivileged user is not trying to forward a
2607: * privileged port.
2608: */
1.25 markus 2609: if (port < IPPORT_RESERVED && !is_root)
1.192 markus 2610: packet_disconnect(
2611: "Requested forwarding of port %d but user is not root.",
2612: port);
2613: if (host_port == 0)
2614: packet_disconnect("Dynamic forwarding denied.");
2615:
1.73 markus 2616: /* Initiate forwarding */
1.253 markus 2617: success = channel_setup_local_fwd_listener(NULL, port, hostname,
1.212 djm 2618: host_port, gateway_ports);
1.25 markus 2619:
2620: /* Free the argument string. */
2621: xfree(hostname);
1.253 markus 2622:
2623: return (success ? 0 : -1);
1.1 deraadt 2624: }
2625:
1.99 markus 2626: /*
2627: * Permits opening to any host/port if permitted_opens[] is empty. This is
2628: * usually called by the server, because the user could connect to any port
2629: * anyway, and the server has no way to know but to trust the client anyway.
2630: */
2631: void
1.142 itojun 2632: channel_permit_all_opens(void)
1.99 markus 2633: {
2634: if (num_permitted_opens == 0)
2635: all_opens_permitted = 1;
2636: }
2637:
1.101 markus 2638: void
1.99 markus 2639: channel_add_permitted_opens(char *host, int port)
2640: {
2641: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1.253 markus 2642: fatal("channel_add_permitted_opens: too many forwards");
1.99 markus 2643: debug("allow port forwarding to host %s port %d", host, port);
2644:
2645: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2646: permitted_opens[num_permitted_opens].port_to_connect = port;
2647: num_permitted_opens++;
2648:
2649: all_opens_permitted = 0;
2650: }
2651:
1.258 dtucker 2652: int
1.257 dtucker 2653: channel_add_adm_permitted_opens(char *host, int port)
2654: {
2655: if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2656: fatal("channel_add_adm_permitted_opens: too many forwards");
1.258 dtucker 2657: debug("config allows port forwarding to host %s port %d", host, port);
1.257 dtucker 2658:
2659: permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2660: = xstrdup(host);
2661: permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
1.258 dtucker 2662: return ++num_adm_permitted_opens;
1.257 dtucker 2663: }
2664:
2665: void
1.99 markus 2666: channel_clear_permitted_opens(void)
2667: {
2668: int i;
2669:
2670: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2671: if (permitted_opens[i].host_to_connect != NULL)
2672: xfree(permitted_opens[i].host_to_connect);
1.99 markus 2673: num_permitted_opens = 0;
1.257 dtucker 2674: }
2675:
2676: void
2677: channel_clear_adm_permitted_opens(void)
2678: {
2679: int i;
1.99 markus 2680:
1.257 dtucker 2681: for (i = 0; i < num_adm_permitted_opens; i++)
2682: if (permitted_adm_opens[i].host_to_connect != NULL)
2683: xfree(permitted_adm_opens[i].host_to_connect);
2684: num_adm_permitted_opens = 0;
1.99 markus 2685: }
2686:
2687: /* return socket to remote host, port */
1.127 itojun 2688: static int
1.99 markus 2689: connect_to(const char *host, u_short port)
1.41 markus 2690: {
2691: struct addrinfo hints, *ai, *aitop;
2692: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2693: int gaierr;
2694: int sock = -1;
2695:
2696: memset(&hints, 0, sizeof(hints));
2697: hints.ai_family = IPv4or6;
2698: hints.ai_socktype = SOCK_STREAM;
1.99 markus 2699: snprintf(strport, sizeof strport, "%d", port);
1.41 markus 2700: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1.99 markus 2701: error("connect_to %.100s: unknown host (%s)", host,
2702: gai_strerror(gaierr));
1.41 markus 2703: return -1;
2704: }
2705: for (ai = aitop; ai; ai = ai->ai_next) {
2706: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2707: continue;
2708: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2709: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.99 markus 2710: error("connect_to: getnameinfo failed");
1.41 markus 2711: continue;
2712: }
1.189 markus 2713: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.41 markus 2714: if (sock < 0) {
1.186 djm 2715: if (ai->ai_next == NULL)
2716: error("socket: %.100s", strerror(errno));
2717: else
2718: verbose("socket: %.100s", strerror(errno));
1.41 markus 2719: continue;
2720: }
1.205 djm 2721: if (set_nonblock(sock) == -1)
2722: fatal("%s: set_nonblock(%d)", __func__, sock);
1.75 markus 2723: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2724: errno != EINPROGRESS) {
1.99 markus 2725: error("connect_to %.100s port %s: %.100s", ntop, strport,
1.41 markus 2726: strerror(errno));
2727: close(sock);
1.89 stevesk 2728: continue; /* fail -- try next */
1.41 markus 2729: }
2730: break; /* success */
2731:
2732: }
2733: freeaddrinfo(aitop);
2734: if (!ai) {
1.99 markus 2735: error("connect_to %.100s port %d: failed.", host, port);
1.41 markus 2736: return -1;
2737: }
2738: /* success */
1.169 stevesk 2739: set_nodelay(sock);
1.41 markus 2740: return sock;
2741: }
1.99 markus 2742:
1.73 markus 2743: int
1.130 stevesk 2744: channel_connect_by_listen_address(u_short listen_port)
1.73 markus 2745: {
2746: int i;
1.99 markus 2747:
1.73 markus 2748: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2749: if (permitted_opens[i].host_to_connect != NULL &&
2750: permitted_opens[i].listen_port == listen_port)
1.99 markus 2751: return connect_to(
1.73 markus 2752: permitted_opens[i].host_to_connect,
2753: permitted_opens[i].port_to_connect);
1.74 markus 2754: error("WARNING: Server requests forwarding for unknown listen_port %d",
2755: listen_port);
1.73 markus 2756: return -1;
2757: }
2758:
1.99 markus 2759: /* Check if connecting to that port is permitted and connect. */
2760: int
2761: channel_connect_to(const char *host, u_short port)
2762: {
1.257 dtucker 2763: int i, permit, permit_adm = 1;
1.99 markus 2764:
2765: permit = all_opens_permitted;
2766: if (!permit) {
2767: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2768: if (permitted_opens[i].host_to_connect != NULL &&
2769: permitted_opens[i].port_to_connect == port &&
1.99 markus 2770: strcmp(permitted_opens[i].host_to_connect, host) == 0)
2771: permit = 1;
1.257 dtucker 2772: }
1.99 markus 2773:
1.257 dtucker 2774: if (num_adm_permitted_opens > 0) {
2775: permit_adm = 0;
2776: for (i = 0; i < num_adm_permitted_opens; i++)
2777: if (permitted_adm_opens[i].host_to_connect != NULL &&
2778: permitted_adm_opens[i].port_to_connect == port &&
2779: strcmp(permitted_adm_opens[i].host_to_connect, host)
2780: == 0)
2781: permit_adm = 1;
1.99 markus 2782: }
1.257 dtucker 2783:
2784: if (!permit || !permit_adm) {
1.188 itojun 2785: logit("Received request to connect to host %.100s port %d, "
1.99 markus 2786: "but the request was denied.", host, port);
2787: return -1;
2788: }
2789: return connect_to(host, port);
1.204 djm 2790: }
2791:
2792: void
2793: channel_send_window_changes(void)
2794: {
1.209 avsm 2795: u_int i;
1.204 djm 2796: struct winsize ws;
2797:
2798: for (i = 0; i < channels_alloc; i++) {
1.213 deraadt 2799: if (channels[i] == NULL || !channels[i]->client_tty ||
1.204 djm 2800: channels[i]->type != SSH_CHANNEL_OPEN)
2801: continue;
2802: if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2803: continue;
2804: channel_request_start(i, "window-change", 0);
1.238 deraadt 2805: packet_put_int((u_int)ws.ws_col);
2806: packet_put_int((u_int)ws.ws_row);
2807: packet_put_int((u_int)ws.ws_xpixel);
2808: packet_put_int((u_int)ws.ws_ypixel);
1.204 djm 2809: packet_send();
2810: }
1.99 markus 2811: }
2812:
1.121 markus 2813: /* -- X11 forwarding */
1.1 deraadt 2814:
1.27 markus 2815: /*
2816: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 2817: * Returns 0 and a suitable display number for the DISPLAY variable
2818: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 2819: */
1.141 stevesk 2820: int
1.163 stevesk 2821: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.222 djm 2822: int single_connection, u_int *display_numberp, int **chanids)
1.1 deraadt 2823: {
1.149 markus 2824: Channel *nc = NULL;
1.31 markus 2825: int display_number, sock;
2826: u_short port;
1.35 markus 2827: struct addrinfo hints, *ai, *aitop;
2828: char strport[NI_MAXSERV];
2829: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 2830:
1.224 markus 2831: if (chanids == NULL)
2832: return -1;
2833:
1.33 markus 2834: for (display_number = x11_display_offset;
1.148 deraadt 2835: display_number < MAX_DISPLAYS;
2836: display_number++) {
1.25 markus 2837: port = 6000 + display_number;
1.35 markus 2838: memset(&hints, 0, sizeof(hints));
2839: hints.ai_family = IPv4or6;
1.163 stevesk 2840: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 2841: hints.ai_socktype = SOCK_STREAM;
2842: snprintf(strport, sizeof strport, "%d", port);
2843: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2844: error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.141 stevesk 2845: return -1;
1.25 markus 2846: }
1.35 markus 2847: for (ai = aitop; ai; ai = ai->ai_next) {
2848: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2849: continue;
1.189 markus 2850: sock = socket(ai->ai_family, ai->ai_socktype,
2851: ai->ai_protocol);
1.35 markus 2852: if (sock < 0) {
2853: error("socket: %.100s", strerror(errno));
1.203 markus 2854: freeaddrinfo(aitop);
1.141 stevesk 2855: return -1;
1.35 markus 2856: }
1.226 djm 2857: channel_set_reuseaddr(sock);
1.35 markus 2858: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 2859: debug2("bind port %d: %.100s", port, strerror(errno));
1.35 markus 2860: close(sock);
1.183 itojun 2861:
2862: if (ai->ai_next)
2863: continue;
2864:
1.35 markus 2865: for (n = 0; n < num_socks; n++) {
2866: close(socks[n]);
2867: }
2868: num_socks = 0;
2869: break;
2870: }
2871: socks[num_socks++] = sock;
2872: if (num_socks == NUM_SOCKS)
2873: break;
1.25 markus 2874: }
1.83 stevesk 2875: freeaddrinfo(aitop);
1.35 markus 2876: if (num_socks > 0)
2877: break;
1.25 markus 2878: }
2879: if (display_number >= MAX_DISPLAYS) {
2880: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 2881: return -1;
1.25 markus 2882: }
2883: /* Start listening for connections on the socket. */
1.35 markus 2884: for (n = 0; n < num_socks; n++) {
2885: sock = socks[n];
1.199 markus 2886: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2887: error("listen: %.100s", strerror(errno));
2888: close(sock);
1.141 stevesk 2889: return -1;
1.35 markus 2890: }
1.25 markus 2891: }
1.35 markus 2892:
2893: /* Allocate a channel for each socket. */
1.242 djm 2894: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 2895: for (n = 0; n < num_socks; n++) {
2896: sock = socks[n];
1.149 markus 2897: nc = channel_new("x11 listener",
1.51 markus 2898: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2899: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 2900: 0, "X11 inet listener", 1);
1.167 markus 2901: nc->single_connection = single_connection;
1.224 markus 2902: (*chanids)[n] = nc->self;
1.35 markus 2903: }
1.224 markus 2904: (*chanids)[n] = -1;
1.1 deraadt 2905:
1.141 stevesk 2906: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 2907: *display_numberp = display_number;
2908: return (0);
1.1 deraadt 2909: }
2910:
1.127 itojun 2911: static int
1.77 markus 2912: connect_local_xsocket(u_int dnr)
1.1 deraadt 2913: {
1.25 markus 2914: int sock;
2915: struct sockaddr_un addr;
2916:
1.147 stevesk 2917: sock = socket(AF_UNIX, SOCK_STREAM, 0);
2918: if (sock < 0)
2919: error("socket: %.100s", strerror(errno));
2920: memset(&addr, 0, sizeof(addr));
2921: addr.sun_family = AF_UNIX;
2922: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 2923: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 2924: return sock;
2925: close(sock);
1.25 markus 2926: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2927: return -1;
1.1 deraadt 2928: }
2929:
1.51 markus 2930: int
2931: x11_connect_display(void)
1.1 deraadt 2932: {
1.248 deraadt 2933: u_int display_number;
1.25 markus 2934: const char *display;
1.51 markus 2935: char buf[1024], *cp;
1.35 markus 2936: struct addrinfo hints, *ai, *aitop;
2937: char strport[NI_MAXSERV];
1.248 deraadt 2938: int gaierr, sock = 0;
1.25 markus 2939:
2940: /* Try to open a socket for the local X server. */
2941: display = getenv("DISPLAY");
2942: if (!display) {
2943: error("DISPLAY not set.");
1.51 markus 2944: return -1;
1.25 markus 2945: }
1.27 markus 2946: /*
2947: * Now we decode the value of the DISPLAY variable and make a
2948: * connection to the real X server.
2949: */
2950:
2951: /*
2952: * Check if it is a unix domain socket. Unix domain displays are in
2953: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2954: */
1.25 markus 2955: if (strncmp(display, "unix:", 5) == 0 ||
2956: display[0] == ':') {
2957: /* Connect to the unix domain socket. */
1.248 deraadt 2958: if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
1.25 markus 2959: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2960: display);
1.51 markus 2961: return -1;
1.25 markus 2962: }
2963: /* Create a socket. */
2964: sock = connect_local_xsocket(display_number);
2965: if (sock < 0)
1.51 markus 2966: return -1;
1.25 markus 2967:
2968: /* OK, we now have a connection to the display. */
1.51 markus 2969: return sock;
1.25 markus 2970: }
1.27 markus 2971: /*
2972: * Connect to an inet socket. The DISPLAY value is supposedly
2973: * hostname:d[.s], where hostname may also be numeric IP address.
2974: */
1.145 stevesk 2975: strlcpy(buf, display, sizeof(buf));
1.25 markus 2976: cp = strchr(buf, ':');
2977: if (!cp) {
2978: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 2979: return -1;
1.25 markus 2980: }
2981: *cp = 0;
1.27 markus 2982: /* buf now contains the host name. But first we parse the display number. */
1.248 deraadt 2983: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 2984: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2985: display);
1.51 markus 2986: return -1;
1.25 markus 2987: }
1.35 markus 2988:
2989: /* Look up the host address */
2990: memset(&hints, 0, sizeof(hints));
2991: hints.ai_family = IPv4or6;
2992: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 2993: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 2994: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2995: error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.51 markus 2996: return -1;
1.25 markus 2997: }
1.35 markus 2998: for (ai = aitop; ai; ai = ai->ai_next) {
2999: /* Create a socket. */
1.189 markus 3000: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 3001: if (sock < 0) {
1.194 markus 3002: debug2("socket: %.100s", strerror(errno));
1.41 markus 3003: continue;
3004: }
3005: /* Connect it to the display. */
3006: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.248 deraadt 3007: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 3008: 6000 + display_number, strerror(errno));
3009: close(sock);
3010: continue;
3011: }
3012: /* Success */
3013: break;
1.35 markus 3014: }
3015: freeaddrinfo(aitop);
3016: if (!ai) {
1.248 deraadt 3017: error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
1.35 markus 3018: strerror(errno));
1.51 markus 3019: return -1;
1.25 markus 3020: }
1.162 stevesk 3021: set_nodelay(sock);
1.51 markus 3022: return sock;
3023: }
3024:
3025: /*
3026: * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3027: * the remote channel number. We should do whatever we want, and respond
3028: * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3029: */
3030:
1.259 stevesk 3031: /* ARGSUSED */
1.51 markus 3032: void
1.154 markus 3033: x11_input_open(int type, u_int32_t seq, void *ctxt)
1.51 markus 3034: {
1.113 markus 3035: Channel *c = NULL;
3036: int remote_id, sock = 0;
1.51 markus 3037: char *remote_host;
1.25 markus 3038:
1.113 markus 3039: debug("Received X11 open request.");
1.51 markus 3040:
1.113 markus 3041: remote_id = packet_get_int();
1.121 markus 3042:
3043: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113 markus 3044: remote_host = packet_get_string(NULL);
1.51 markus 3045: } else {
3046: remote_host = xstrdup("unknown (remote did not supply name)");
3047: }
1.152 markus 3048: packet_check_eom();
1.25 markus 3049:
1.51 markus 3050: /* Obtain a connection to the real X display. */
3051: sock = x11_connect_display();
1.113 markus 3052: if (sock != -1) {
3053: /* Allocate a channel for this connection. */
3054: c = channel_new("connected x11 socket",
3055: SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3056: remote_host, 1);
1.167 markus 3057: c->remote_id = remote_id;
3058: c->force_drain = 1;
1.113 markus 3059: }
1.190 markus 3060: xfree(remote_host);
1.113 markus 3061: if (c == NULL) {
1.51 markus 3062: /* Send refusal to the remote host. */
3063: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 3064: packet_put_int(remote_id);
1.51 markus 3065: } else {
3066: /* Send a confirmation to the remote host. */
3067: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113 markus 3068: packet_put_int(remote_id);
3069: packet_put_int(c->self);
1.51 markus 3070: }
1.113 markus 3071: packet_send();
1.72 markus 3072: }
3073:
3074: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
1.259 stevesk 3075: /* ARGSUSED */
1.72 markus 3076: void
1.154 markus 3077: deny_input_open(int type, u_int32_t seq, void *ctxt)
1.72 markus 3078: {
3079: int rchan = packet_get_int();
1.180 deraadt 3080:
1.143 deraadt 3081: switch (type) {
1.72 markus 3082: case SSH_SMSG_AGENT_OPEN:
3083: error("Warning: ssh server tried agent forwarding.");
3084: break;
3085: case SSH_SMSG_X11_OPEN:
3086: error("Warning: ssh server tried X11 forwarding.");
3087: break;
3088: default:
1.154 markus 3089: error("deny_input_open: type %d", type);
1.72 markus 3090: break;
3091: }
1.230 stevesk 3092: error("Warning: this is probably a break-in attempt by a malicious server.");
1.72 markus 3093: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3094: packet_put_int(rchan);
3095: packet_send();
1.1 deraadt 3096: }
3097:
1.27 markus 3098: /*
3099: * Requests forwarding of X11 connections, generates fake authentication
3100: * data, and enables authentication spoofing.
1.121 markus 3101: * This should be called in the client only.
1.27 markus 3102: */
1.49 markus 3103: void
1.215 djm 3104: x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
1.51 markus 3105: const char *proto, const char *data)
1.1 deraadt 3106: {
1.77 markus 3107: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 3108: u_int i, value;
1.25 markus 3109: char *new_data;
3110: int screen_number;
3111: const char *cp;
1.207 avsm 3112: u_int32_t rnd = 0;
1.25 markus 3113:
1.220 markus 3114: if (x11_saved_display == NULL)
3115: x11_saved_display = xstrdup(disp);
3116: else if (strcmp(disp, x11_saved_display) != 0) {
1.219 djm 3117: error("x11_request_forwarding_with_spoofing: different "
3118: "$DISPLAY already forwarded");
3119: return;
3120: }
3121:
1.215 djm 3122: cp = disp;
3123: if (disp)
3124: cp = strchr(disp, ':');
1.25 markus 3125: if (cp)
3126: cp = strchr(cp, '.');
3127: if (cp)
1.245 deraadt 3128: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 3129: else
3130: screen_number = 0;
3131:
1.219 djm 3132: if (x11_saved_proto == NULL) {
3133: /* Save protocol name. */
3134: x11_saved_proto = xstrdup(proto);
3135: /*
1.221 djm 3136: * Extract real authentication data and generate fake data
1.219 djm 3137: * of the same length.
3138: */
3139: x11_saved_data = xmalloc(data_len);
3140: x11_fake_data = xmalloc(data_len);
3141: for (i = 0; i < data_len; i++) {
3142: if (sscanf(data + 2 * i, "%2x", &value) != 1)
3143: fatal("x11_request_forwarding: bad "
3144: "authentication data: %.100s", data);
3145: if (i % 4 == 0)
3146: rnd = arc4random();
3147: x11_saved_data[i] = value;
3148: x11_fake_data[i] = rnd & 0xff;
3149: rnd >>= 8;
3150: }
3151: x11_saved_data_len = data_len;
3152: x11_fake_data_len = data_len;
1.25 markus 3153: }
3154:
3155: /* Convert the fake data into hex. */
1.219 djm 3156: new_data = tohex(x11_fake_data, data_len);
1.25 markus 3157:
3158: /* Send the request packet. */
1.51 markus 3159: if (compat20) {
3160: channel_request_start(client_session_id, "x11-req", 0);
3161: packet_put_char(0); /* XXX bool single connection */
3162: } else {
3163: packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3164: }
3165: packet_put_cstring(proto);
3166: packet_put_cstring(new_data);
1.25 markus 3167: packet_put_int(screen_number);
3168: packet_send();
3169: packet_write_wait();
3170: xfree(new_data);
1.1 deraadt 3171: }
3172:
1.121 markus 3173:
3174: /* -- agent forwarding */
3175:
1.1 deraadt 3176: /* Sends a message to the server to request authentication fd forwarding. */
3177:
1.49 markus 3178: void
1.142 itojun 3179: auth_request_forwarding(void)
1.1 deraadt 3180: {
1.25 markus 3181: packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3182: packet_send();
3183: packet_write_wait();
1.1 deraadt 3184: }