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