Annotation of src/usr.bin/ssh/channels.c, Revision 1.271
1.271 ! dtucker 1: /* $OpenBSD: channels.c,v 1.270 2007/06/25 08:20:03 dtucker 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:
1.265 deraadt 42: #include <sys/types.h>
1.234 stevesk 43: #include <sys/ioctl.h>
1.235 stevesk 44: #include <sys/un.h>
1.251 stevesk 45: #include <sys/socket.h>
1.261 stevesk 46: #include <sys/time.h>
1.251 stevesk 47:
48: #include <netinet/in.h>
49: #include <arpa/inet.h>
1.233 stevesk 50:
1.254 stevesk 51: #include <errno.h>
1.255 stevesk 52: #include <netdb.h>
1.263 stevesk 53: #include <stdio.h>
1.262 stevesk 54: #include <stdlib.h>
1.260 stevesk 55: #include <string.h>
1.233 stevesk 56: #include <termios.h>
1.256 stevesk 57: #include <unistd.h>
1.265 deraadt 58: #include <stdarg.h>
1.1 deraadt 59:
1.265 deraadt 60: #include "xmalloc.h"
1.1 deraadt 61: #include "ssh.h"
1.82 markus 62: #include "ssh1.h"
63: #include "ssh2.h"
1.1 deraadt 64: #include "packet.h"
1.82 markus 65: #include "log.h"
66: #include "misc.h"
1.265 deraadt 67: #include "buffer.h"
1.14 markus 68: #include "channels.h"
69: #include "compat.h"
1.82 markus 70: #include "canohost.h"
1.64 markus 71: #include "key.h"
72: #include "authfd.h"
1.147 stevesk 73: #include "pathnames.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" */
1.268 stevesk 1050: for (found = 0, i = 2; i < nmethods + 2; i++) {
1.264 stevesk 1051: if (p[i] == SSH_SOCKS5_NOAUTH) {
1.193 markus 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.270 dtucker 1643: ((c->local_window_max - c->local_window >
1.269 markus 1644: c->local_maxpacket*3) ||
1645: c->local_window < c->local_window_max/2) &&
1.44 markus 1646: c->local_consumed > 0) {
1647: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1648: packet_put_int(c->remote_id);
1649: packet_put_int(c->local_consumed);
1650: packet_send();
1.70 markus 1651: debug2("channel %d: window %d sent adjust %d",
1.44 markus 1652: c->self, c->local_window,
1653: c->local_consumed);
1654: c->local_window += c->local_consumed;
1655: c->local_consumed = 0;
1.1 deraadt 1656: }
1.41 markus 1657: return 1;
1.1 deraadt 1658: }
1659:
1.127 itojun 1660: static void
1.237 deraadt 1661: channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1662: {
1.137 markus 1663: if (c->delayed)
1664: return;
1.41 markus 1665: channel_handle_rfd(c, readset, writeset);
1666: channel_handle_wfd(c, readset, writeset);
1.157 markus 1667: if (!compat20)
1.137 markus 1668: return;
1.44 markus 1669: channel_handle_efd(c, readset, writeset);
1.204 djm 1670: channel_handle_ctl(c, readset, writeset);
1.93 markus 1671: channel_check_window(c);
1.44 markus 1672: }
1673:
1.259 stevesk 1674: /* ARGSUSED */
1.127 itojun 1675: static void
1.237 deraadt 1676: channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
1.1 deraadt 1677: {
1.41 markus 1678: int len;
1.180 deraadt 1679:
1.41 markus 1680: /* Send buffered output data to the socket. */
1681: if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1682: len = write(c->sock, buffer_ptr(&c->output),
1683: buffer_len(&c->output));
1684: if (len <= 0)
1.158 markus 1685: buffer_clear(&c->output);
1.41 markus 1686: else
1687: buffer_consume(&c->output, len);
1688: }
1689: }
1.25 markus 1690:
1.127 itojun 1691: static void
1.44 markus 1692: channel_handler_init_20(void)
1693: {
1.157 markus 1694: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1695: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.44 markus 1696: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1.73 markus 1697: channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1.51 markus 1698: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1.73 markus 1699: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1700: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1701: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.44 markus 1702:
1.157 markus 1703: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.44 markus 1704: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1.73 markus 1705: channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1.51 markus 1706: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1.73 markus 1707: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.75 markus 1708: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1709: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.44 markus 1710: }
1711:
1.127 itojun 1712: static void
1.41 markus 1713: channel_handler_init_13(void)
1714: {
1715: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1716: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1717: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1718: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1719: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1720: channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1721: channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1.75 markus 1722: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1723: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1724:
1.157 markus 1725: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.41 markus 1726: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1727: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1728: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1729: channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1.75 markus 1730: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1731: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1732: }
1.25 markus 1733:
1.127 itojun 1734: static void
1.41 markus 1735: channel_handler_init_15(void)
1736: {
1.157 markus 1737: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1738: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.41 markus 1739: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1740: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1741: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1742: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1743: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1744:
1.41 markus 1745: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1746: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1747: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.157 markus 1748: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.75 markus 1749: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1750: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1751: }
1.27 markus 1752:
1.127 itojun 1753: static void
1.41 markus 1754: channel_handler_init(void)
1755: {
1756: int i;
1.180 deraadt 1757:
1.148 deraadt 1758: for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1.41 markus 1759: channel_pre[i] = NULL;
1760: channel_post[i] = NULL;
1761: }
1.44 markus 1762: if (compat20)
1763: channel_handler_init_20();
1764: else if (compat13)
1.41 markus 1765: channel_handler_init_13();
1766: else
1767: channel_handler_init_15();
1768: }
1.25 markus 1769:
1.140 markus 1770: /* gc dead channels */
1771: static void
1772: channel_garbage_collect(Channel *c)
1773: {
1774: if (c == NULL)
1775: return;
1776: if (c->detach_user != NULL) {
1.225 djm 1777: if (!chan_is_dead(c, c->detach_close))
1.140 markus 1778: return;
1.194 markus 1779: debug2("channel %d: gc: notify user", c->self);
1.140 markus 1780: c->detach_user(c->self, NULL);
1781: /* if we still have a callback */
1782: if (c->detach_user != NULL)
1783: return;
1.194 markus 1784: debug2("channel %d: gc: user detached", c->self);
1.140 markus 1785: }
1786: if (!chan_is_dead(c, 1))
1787: return;
1.194 markus 1788: debug2("channel %d: garbage collecting", c->self);
1.140 markus 1789: channel_free(c);
1790: }
1791:
1.127 itojun 1792: static void
1.237 deraadt 1793: channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
1.41 markus 1794: {
1795: static int did_init = 0;
1.209 avsm 1796: u_int i;
1.41 markus 1797: Channel *c;
1.25 markus 1798:
1.41 markus 1799: if (!did_init) {
1800: channel_handler_init();
1801: did_init = 1;
1802: }
1803: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1804: c = channels[i];
1805: if (c == NULL)
1.41 markus 1806: continue;
1.118 markus 1807: if (ftab[c->type] != NULL)
1808: (*ftab[c->type])(c, readset, writeset);
1.140 markus 1809: channel_garbage_collect(c);
1.1 deraadt 1810: }
1811: }
1812:
1.121 markus 1813: /*
1814: * Allocate/update select bitmasks and add any bits relevant to channels in
1815: * select bitmasks.
1816: */
1.49 markus 1817: void
1.100 markus 1818: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1.209 avsm 1819: u_int *nallocp, int rekeying)
1.41 markus 1820: {
1.243 djm 1821: u_int n, sz, nfdset;
1.84 markus 1822:
1823: n = MAX(*maxfdp, channel_max_fd);
1824:
1.243 djm 1825: nfdset = howmany(n+1, NFDBITS);
1826: /* Explicitly test here, because xrealloc isn't always called */
1827: if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1828: fatal("channel_prepare_select: max_fd (%d) is too large", n);
1829: sz = nfdset * sizeof(fd_mask);
1830:
1.132 markus 1831: /* perhaps check sz < nalloc/2 and shrink? */
1832: if (*readsetp == NULL || sz > *nallocp) {
1.243 djm 1833: *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1834: *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
1.132 markus 1835: *nallocp = sz;
1.84 markus 1836: }
1.132 markus 1837: *maxfdp = n;
1.84 markus 1838: memset(*readsetp, 0, sz);
1839: memset(*writesetp, 0, sz);
1840:
1.100 markus 1841: if (!rekeying)
1842: channel_handler(channel_pre, *readsetp, *writesetp);
1.41 markus 1843: }
1844:
1.121 markus 1845: /*
1846: * After select, perform any appropriate operations for channels which have
1847: * events pending.
1848: */
1.49 markus 1849: void
1.237 deraadt 1850: channel_after_select(fd_set *readset, fd_set *writeset)
1.41 markus 1851: {
1852: channel_handler(channel_post, readset, writeset);
1853: }
1854:
1.121 markus 1855:
1.84 markus 1856: /* If there is data to send to the connection, enqueue some of it now. */
1.49 markus 1857: void
1.142 itojun 1858: channel_output_poll(void)
1.1 deraadt 1859: {
1.41 markus 1860: Channel *c;
1.209 avsm 1861: u_int i, len;
1.1 deraadt 1862:
1.25 markus 1863: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1864: c = channels[i];
1865: if (c == NULL)
1866: continue;
1.37 markus 1867:
1.121 markus 1868: /*
1869: * We are only interested in channels that can have buffered
1870: * incoming data.
1871: */
1.37 markus 1872: if (compat13) {
1.41 markus 1873: if (c->type != SSH_CHANNEL_OPEN &&
1874: c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37 markus 1875: continue;
1876: } else {
1.41 markus 1877: if (c->type != SSH_CHANNEL_OPEN)
1.37 markus 1878: continue;
1879: }
1.46 markus 1880: if (compat20 &&
1881: (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 1882: /* XXX is this true? */
1.140 markus 1883: debug3("channel %d: will not send data after close", c->self);
1.44 markus 1884: continue;
1885: }
1.25 markus 1886:
1887: /* Get the amount of buffered data for this channel. */
1.93 markus 1888: if ((c->istate == CHAN_INPUT_OPEN ||
1889: c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1890: (len = buffer_len(&c->input)) > 0) {
1.228 reyk 1891: if (c->datagram) {
1892: if (len > 0) {
1893: u_char *data;
1894: u_int dlen;
1895:
1896: data = buffer_get_string(&c->input,
1897: &dlen);
1898: packet_start(SSH2_MSG_CHANNEL_DATA);
1899: packet_put_int(c->remote_id);
1900: packet_put_string(data, dlen);
1901: packet_send();
1902: c->remote_window -= dlen + 4;
1903: xfree(data);
1904: }
1905: continue;
1906: }
1.121 markus 1907: /*
1908: * Send some data for the other side over the secure
1909: * connection.
1910: */
1.44 markus 1911: if (compat20) {
1912: if (len > c->remote_window)
1913: len = c->remote_window;
1914: if (len > c->remote_maxpacket)
1915: len = c->remote_maxpacket;
1.25 markus 1916: } else {
1.44 markus 1917: if (packet_is_interactive()) {
1918: if (len > 1024)
1919: len = 512;
1920: } else {
1921: /* Keep the packets at reasonable size. */
1922: if (len > packet_get_maxsize()/2)
1923: len = packet_get_maxsize()/2;
1924: }
1.25 markus 1925: }
1.41 markus 1926: if (len > 0) {
1.44 markus 1927: packet_start(compat20 ?
1928: SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41 markus 1929: packet_put_int(c->remote_id);
1930: packet_put_string(buffer_ptr(&c->input), len);
1931: packet_send();
1932: buffer_consume(&c->input, len);
1933: c->remote_window -= len;
1934: }
1935: } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25 markus 1936: if (compat13)
1937: fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27 markus 1938: /*
1939: * input-buffer is empty and read-socket shutdown:
1.172 markus 1940: * tell peer, that we will not send more data: send IEOF.
1941: * hack for extended data: delay EOF if EFD still in use.
1.27 markus 1942: */
1.172 markus 1943: if (CHANNEL_EFD_INPUT_ACTIVE(c))
1.223 djm 1944: debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1945: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 1946: else
1947: chan_ibuf_empty(c);
1.25 markus 1948: }
1.44 markus 1949: /* Send extended data, i.e. stderr */
1950: if (compat20 &&
1.172 markus 1951: !(c->flags & CHAN_EOF_SENT) &&
1.44 markus 1952: c->remote_window > 0 &&
1953: (len = buffer_len(&c->extended)) > 0 &&
1954: c->extended_usage == CHAN_EXTENDED_READ) {
1.178 markus 1955: debug2("channel %d: rwin %u elen %u euse %d",
1.93 markus 1956: c->self, c->remote_window, buffer_len(&c->extended),
1957: c->extended_usage);
1.44 markus 1958: if (len > c->remote_window)
1959: len = c->remote_window;
1960: if (len > c->remote_maxpacket)
1961: len = c->remote_maxpacket;
1962: packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1963: packet_put_int(c->remote_id);
1964: packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1965: packet_put_string(buffer_ptr(&c->extended), len);
1966: packet_send();
1967: buffer_consume(&c->extended, len);
1968: c->remote_window -= len;
1.93 markus 1969: debug2("channel %d: sent ext data %d", c->self, len);
1.44 markus 1970: }
1.25 markus 1971: }
1.1 deraadt 1972: }
1973:
1.121 markus 1974:
1975: /* -- protocol input */
1.249 djm 1976:
1977: /* ARGSUSED */
1.49 markus 1978: void
1.154 markus 1979: channel_input_data(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 1980: {
1.37 markus 1981: int id;
1.25 markus 1982: char *data;
1.77 markus 1983: u_int data_len;
1.41 markus 1984: Channel *c;
1.25 markus 1985:
1986: /* Get the channel number and verify it. */
1.37 markus 1987: id = packet_get_int();
1.41 markus 1988: c = channel_lookup(id);
1989: if (c == NULL)
1.37 markus 1990: packet_disconnect("Received data for nonexistent channel %d.", id);
1.25 markus 1991:
1992: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 1993: if (c->type != SSH_CHANNEL_OPEN &&
1994: c->type != SSH_CHANNEL_X11_OPEN)
1.37 markus 1995: return;
1996:
1.25 markus 1997: /* Get the data. */
1998: data = packet_get_string(&data_len);
1.200 markus 1999:
2000: /*
2001: * Ignore data for protocol > 1.3 if output end is no longer open.
2002: * For protocol 2 the sending side is reducing its window as it sends
2003: * data, so we must 'fake' consumption of the data in order to ensure
2004: * that window updates are sent back. Otherwise the connection might
2005: * deadlock.
2006: */
2007: if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2008: if (compat20) {
2009: c->local_window -= data_len;
2010: c->local_consumed += data_len;
2011: }
2012: xfree(data);
2013: return;
2014: }
1.41 markus 2015:
1.143 deraadt 2016: if (compat20) {
1.44 markus 2017: if (data_len > c->local_maxpacket) {
1.188 itojun 2018: logit("channel %d: rcvd big packet %d, maxpack %d",
1.44 markus 2019: c->self, data_len, c->local_maxpacket);
2020: }
2021: if (data_len > c->local_window) {
1.188 itojun 2022: logit("channel %d: rcvd too much data %d, win %d",
1.44 markus 2023: c->self, data_len, c->local_window);
2024: xfree(data);
2025: return;
2026: }
2027: c->local_window -= data_len;
2028: }
1.152 markus 2029: packet_check_eom();
1.228 reyk 2030: if (c->datagram)
2031: buffer_put_string(&c->output, data, data_len);
2032: else
2033: buffer_append(&c->output, data, data_len);
1.25 markus 2034: xfree(data);
1.1 deraadt 2035: }
1.121 markus 2036:
1.249 djm 2037: /* ARGSUSED */
1.49 markus 2038: void
1.154 markus 2039: channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1.44 markus 2040: {
2041: int id;
2042: char *data;
1.177 markus 2043: u_int data_len, tcode;
1.44 markus 2044: Channel *c;
2045:
2046: /* Get the channel number and verify it. */
2047: id = packet_get_int();
2048: c = channel_lookup(id);
2049:
2050: if (c == NULL)
2051: packet_disconnect("Received extended_data for bad channel %d.", id);
2052: if (c->type != SSH_CHANNEL_OPEN) {
1.188 itojun 2053: logit("channel %d: ext data for non open", id);
1.44 markus 2054: return;
1.172 markus 2055: }
2056: if (c->flags & CHAN_EOF_RCVD) {
2057: if (datafellows & SSH_BUG_EXTEOF)
2058: debug("channel %d: accepting ext data after eof", id);
2059: else
2060: packet_disconnect("Received extended_data after EOF "
2061: "on channel %d.", id);
1.44 markus 2062: }
2063: tcode = packet_get_int();
2064: if (c->efd == -1 ||
2065: c->extended_usage != CHAN_EXTENDED_WRITE ||
2066: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 2067: logit("channel %d: bad ext data", c->self);
1.44 markus 2068: return;
2069: }
2070: data = packet_get_string(&data_len);
1.152 markus 2071: packet_check_eom();
1.44 markus 2072: if (data_len > c->local_window) {
1.188 itojun 2073: logit("channel %d: rcvd too much extended_data %d, win %d",
1.44 markus 2074: c->self, data_len, c->local_window);
2075: xfree(data);
2076: return;
2077: }
1.70 markus 2078: debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44 markus 2079: c->local_window -= data_len;
2080: buffer_append(&c->extended, data, data_len);
2081: xfree(data);
2082: }
2083:
1.249 djm 2084: /* ARGSUSED */
1.49 markus 2085: void
1.154 markus 2086: channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1.41 markus 2087: {
2088: int id;
2089: Channel *c;
2090:
2091: id = packet_get_int();
1.152 markus 2092: packet_check_eom();
1.41 markus 2093: c = channel_lookup(id);
2094: if (c == NULL)
2095: packet_disconnect("Received ieof for nonexistent channel %d.", id);
2096: chan_rcvd_ieof(c);
1.133 markus 2097:
2098: /* XXX force input close */
1.156 markus 2099: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 2100: debug("channel %d: FORCE input drain", c->self);
2101: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.161 markus 2102: if (buffer_len(&c->input) == 0)
2103: chan_ibuf_empty(c);
1.133 markus 2104: }
2105:
1.41 markus 2106: }
1.1 deraadt 2107:
1.249 djm 2108: /* ARGSUSED */
1.49 markus 2109: void
1.154 markus 2110: channel_input_close(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2111: {
1.41 markus 2112: int id;
2113: Channel *c;
1.1 deraadt 2114:
1.41 markus 2115: id = packet_get_int();
1.152 markus 2116: packet_check_eom();
1.41 markus 2117: c = channel_lookup(id);
2118: if (c == NULL)
2119: packet_disconnect("Received close for nonexistent channel %d.", id);
1.27 markus 2120:
2121: /*
2122: * Send a confirmation that we have closed the channel and no more
2123: * data is coming for it.
2124: */
1.25 markus 2125: packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41 markus 2126: packet_put_int(c->remote_id);
1.25 markus 2127: packet_send();
2128:
1.27 markus 2129: /*
2130: * If the channel is in closed state, we have sent a close request,
2131: * and the other side will eventually respond with a confirmation.
2132: * Thus, we cannot free the channel here, because then there would be
2133: * no-one to receive the confirmation. The channel gets freed when
2134: * the confirmation arrives.
2135: */
1.41 markus 2136: if (c->type != SSH_CHANNEL_CLOSED) {
1.27 markus 2137: /*
2138: * Not a closed channel - mark it as draining, which will
2139: * cause it to be freed later.
2140: */
1.158 markus 2141: buffer_clear(&c->input);
1.41 markus 2142: c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25 markus 2143: }
1.1 deraadt 2144: }
2145:
1.41 markus 2146: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.249 djm 2147: /* ARGSUSED */
1.49 markus 2148: void
1.154 markus 2149: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41 markus 2150: {
2151: int id = packet_get_int();
2152: Channel *c = channel_lookup(id);
1.151 markus 2153:
1.152 markus 2154: packet_check_eom();
1.41 markus 2155: if (c == NULL)
2156: packet_disconnect("Received oclose for nonexistent channel %d.", id);
2157: chan_rcvd_oclose(c);
2158: }
1.1 deraadt 2159:
1.249 djm 2160: /* ARGSUSED */
1.49 markus 2161: void
1.154 markus 2162: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2163: {
1.41 markus 2164: int id = packet_get_int();
2165: Channel *c = channel_lookup(id);
1.1 deraadt 2166:
1.152 markus 2167: packet_check_eom();
1.41 markus 2168: if (c == NULL)
2169: packet_disconnect("Received close confirmation for "
2170: "out-of-range channel %d.", id);
2171: if (c->type != SSH_CHANNEL_CLOSED)
2172: packet_disconnect("Received close confirmation for "
2173: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 2174: channel_free(c);
1.1 deraadt 2175: }
2176:
1.249 djm 2177: /* ARGSUSED */
1.49 markus 2178: void
1.154 markus 2179: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2180: {
1.41 markus 2181: int id, remote_id;
2182: Channel *c;
1.1 deraadt 2183:
1.41 markus 2184: id = packet_get_int();
2185: c = channel_lookup(id);
1.25 markus 2186:
1.41 markus 2187: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2188: packet_disconnect("Received open confirmation for "
2189: "non-opening channel %d.", id);
2190: remote_id = packet_get_int();
1.27 markus 2191: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 2192: c->remote_id = remote_id;
2193: c->type = SSH_CHANNEL_OPEN;
1.44 markus 2194:
2195: if (compat20) {
2196: c->remote_window = packet_get_int();
2197: c->remote_maxpacket = packet_get_int();
1.165 markus 2198: if (c->confirm) {
1.70 markus 2199: debug2("callback start");
1.204 djm 2200: c->confirm(c->self, c->confirm_ctx);
1.70 markus 2201: debug2("callback done");
1.44 markus 2202: }
1.194 markus 2203: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
1.44 markus 2204: c->remote_window, c->remote_maxpacket);
2205: }
1.152 markus 2206: packet_check_eom();
1.1 deraadt 2207: }
2208:
1.127 itojun 2209: static char *
1.114 markus 2210: reason2txt(int reason)
2211: {
1.143 deraadt 2212: switch (reason) {
1.114 markus 2213: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2214: return "administratively prohibited";
2215: case SSH2_OPEN_CONNECT_FAILED:
2216: return "connect failed";
2217: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2218: return "unknown channel type";
2219: case SSH2_OPEN_RESOURCE_SHORTAGE:
2220: return "resource shortage";
2221: }
1.117 stevesk 2222: return "unknown reason";
1.114 markus 2223: }
2224:
1.249 djm 2225: /* ARGSUSED */
1.49 markus 2226: void
1.154 markus 2227: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2228: {
1.86 markus 2229: int id, reason;
2230: char *msg = NULL, *lang = NULL;
1.41 markus 2231: Channel *c;
2232:
2233: id = packet_get_int();
2234: c = channel_lookup(id);
1.25 markus 2235:
1.41 markus 2236: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2237: packet_disconnect("Received open failure for "
2238: "non-opening channel %d.", id);
1.44 markus 2239: if (compat20) {
1.86 markus 2240: reason = packet_get_int();
1.110 markus 2241: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86 markus 2242: msg = packet_get_string(NULL);
2243: lang = packet_get_string(NULL);
2244: }
1.188 itojun 2245: logit("channel %d: open failed: %s%s%s", id,
1.114 markus 2246: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86 markus 2247: if (msg != NULL)
2248: xfree(msg);
2249: if (lang != NULL)
2250: xfree(lang);
1.44 markus 2251: }
1.152 markus 2252: packet_check_eom();
1.25 markus 2253: /* Free the channel. This will also close the socket. */
1.113 markus 2254: channel_free(c);
1.44 markus 2255: }
2256:
1.249 djm 2257: /* ARGSUSED */
1.121 markus 2258: void
1.154 markus 2259: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121 markus 2260: {
2261: Channel *c;
1.178 markus 2262: int id;
2263: u_int adjust;
1.121 markus 2264:
2265: if (!compat20)
2266: return;
2267:
2268: /* Get the channel number and verify it. */
2269: id = packet_get_int();
2270: c = channel_lookup(id);
1.1 deraadt 2271:
1.229 markus 2272: if (c == NULL) {
2273: logit("Received window adjust for non-open channel %d.", id);
1.121 markus 2274: return;
2275: }
2276: adjust = packet_get_int();
1.152 markus 2277: packet_check_eom();
1.178 markus 2278: debug2("channel %d: rcvd adjust %u", id, adjust);
1.121 markus 2279: c->remote_window += adjust;
2280: }
1.1 deraadt 2281:
1.249 djm 2282: /* ARGSUSED */
1.121 markus 2283: void
1.154 markus 2284: channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2285: {
1.121 markus 2286: Channel *c = NULL;
2287: u_short host_port;
2288: char *host, *originator_string;
2289: int remote_id, sock = -1;
2290:
2291: remote_id = packet_get_int();
2292: host = packet_get_string(NULL);
2293: host_port = packet_get_int();
1.25 markus 2294:
1.121 markus 2295: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2296: originator_string = packet_get_string(NULL);
2297: } else {
2298: originator_string = xstrdup("unknown (remote did not supply name)");
2299: }
1.152 markus 2300: packet_check_eom();
1.121 markus 2301: sock = channel_connect_to(host, host_port);
2302: if (sock != -1) {
2303: c = channel_new("connected socket",
2304: SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2305: originator_string, 1);
1.167 markus 2306: c->remote_id = remote_id;
1.25 markus 2307: }
1.190 markus 2308: xfree(originator_string);
1.121 markus 2309: if (c == NULL) {
2310: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2311: packet_put_int(remote_id);
2312: packet_send();
2313: }
2314: xfree(host);
1.1 deraadt 2315: }
2316:
1.121 markus 2317:
2318: /* -- tcp forwarding */
1.135 markus 2319:
2320: void
2321: channel_set_af(int af)
2322: {
2323: IPv4or6 = af;
2324: }
1.121 markus 2325:
1.160 markus 2326: static int
2327: channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2328: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
1.25 markus 2329: {
1.113 markus 2330: Channel *c;
1.226 djm 2331: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 2332: struct addrinfo hints, *ai, *aitop;
1.212 djm 2333: const char *host, *addr;
1.35 markus 2334: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.25 markus 2335:
1.160 markus 2336: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2337: listen_addr : host_to_connect;
1.212 djm 2338: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 2339:
1.160 markus 2340: if (host == NULL) {
2341: error("No forward host name.");
1.218 markus 2342: return 0;
1.73 markus 2343: }
1.113 markus 2344: if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
1.87 markus 2345: error("Forward host name too long.");
1.218 markus 2346: return 0;
1.87 markus 2347: }
1.25 markus 2348:
1.28 markus 2349: /*
1.212 djm 2350: * Determine whether or not a port forward listens to loopback,
1.213 deraadt 2351: * specified address or wildcard. On the client, a specified bind
2352: * address will always override gateway_ports. On the server, a
2353: * gateway_ports of 1 (``yes'') will override the client's
2354: * specification and force a wildcard bind, whereas a value of 2
2355: * (``clientspecified'') will bind to whatever address the client
1.212 djm 2356: * asked for.
2357: *
2358: * Special-case listen_addrs are:
2359: *
2360: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2361: * "" (empty string), "*" -> wildcard v4/v6
2362: * "localhost" -> loopback v4/v6
2363: */
2364: addr = NULL;
2365: if (listen_addr == NULL) {
2366: /* No address specified: default to gateway_ports setting */
2367: if (gateway_ports)
2368: wildcard = 1;
2369: } else if (gateway_ports || is_client) {
2370: if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2371: strcmp(listen_addr, "0.0.0.0") == 0) ||
2372: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2373: (!is_client && gateway_ports == 1))
2374: wildcard = 1;
2375: else if (strcmp(listen_addr, "localhost") != 0)
2376: addr = listen_addr;
2377: }
2378:
2379: debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2380: type, wildcard, (addr == NULL) ? "NULL" : addr);
2381:
2382: /*
1.35 markus 2383: * getaddrinfo returns a loopback address if the hostname is
2384: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2385: */
1.35 markus 2386: memset(&hints, 0, sizeof(hints));
2387: hints.ai_family = IPv4or6;
1.212 djm 2388: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 2389: hints.ai_socktype = SOCK_STREAM;
1.73 markus 2390: snprintf(strport, sizeof strport, "%d", listen_port);
1.212 djm 2391: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2392: if (addr == NULL) {
2393: /* This really shouldn't happen */
2394: packet_disconnect("getaddrinfo: fatal error: %s",
1.271 ! dtucker 2395: ssh_gai_strerror(r));
1.212 djm 2396: } else {
1.218 markus 2397: error("channel_setup_fwd_listener: "
1.271 ! dtucker 2398: "getaddrinfo(%.64s): %s", addr,
! 2399: ssh_gai_strerror(r));
1.212 djm 2400: }
1.218 markus 2401: return 0;
1.212 djm 2402: }
1.35 markus 2403:
2404: for (ai = aitop; ai; ai = ai->ai_next) {
2405: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2406: continue;
2407: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2408: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.160 markus 2409: error("channel_setup_fwd_listener: getnameinfo failed");
1.35 markus 2410: continue;
2411: }
2412: /* Create a port to listen for the host. */
1.189 markus 2413: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 2414: if (sock < 0) {
2415: /* this is no error since kernel may not support ipv6 */
2416: verbose("socket: %.100s", strerror(errno));
2417: continue;
2418: }
1.226 djm 2419:
2420: channel_set_reuseaddr(sock);
1.182 stevesk 2421:
1.35 markus 2422: debug("Local forwarding listening on %s port %s.", ntop, strport);
2423:
2424: /* Bind the socket to the address. */
2425: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2426: /* address can be in use ipv6 address is already bound */
2427: verbose("bind: %.100s", strerror(errno));
2428: close(sock);
2429: continue;
2430: }
2431: /* Start listening for connections on the socket. */
1.199 markus 2432: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2433: error("listen: %.100s", strerror(errno));
2434: close(sock);
2435: continue;
2436: }
2437: /* Allocate a channel number for the socket. */
1.121 markus 2438: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2439: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 2440: 0, "port listener", 1);
1.113 markus 2441: strlcpy(c->path, host, sizeof(c->path));
2442: c->host_port = port_to_connect;
2443: c->listening_port = listen_port;
1.35 markus 2444: success = 1;
2445: }
2446: if (success == 0)
1.160 markus 2447: error("channel_setup_fwd_listener: cannot listen to port: %d",
1.87 markus 2448: listen_port);
1.35 markus 2449: freeaddrinfo(aitop);
1.87 markus 2450: return success;
1.25 markus 2451: }
1.1 deraadt 2452:
1.202 djm 2453: int
2454: channel_cancel_rport_listener(const char *host, u_short port)
2455: {
1.209 avsm 2456: u_int i;
2457: int found = 0;
1.202 djm 2458:
1.213 deraadt 2459: for (i = 0; i < channels_alloc; i++) {
1.202 djm 2460: Channel *c = channels[i];
2461:
2462: if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2463: strncmp(c->path, host, sizeof(c->path)) == 0 &&
1.208 deraadt 2464: c->listening_port == port) {
1.210 djm 2465: debug2("%s: close channel %d", __func__, i);
1.202 djm 2466: channel_free(c);
2467: found = 1;
2468: }
2469: }
2470:
2471: return (found);
2472: }
2473:
1.160 markus 2474: /* protocol local port fwd, used by ssh (and sshd in v1) */
2475: int
1.212 djm 2476: channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
1.160 markus 2477: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2478: {
2479: return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
1.212 djm 2480: listen_host, listen_port, host_to_connect, port_to_connect,
2481: gateway_ports);
1.160 markus 2482: }
2483:
2484: /* protocol v2 remote port fwd, used by sshd */
2485: int
2486: channel_setup_remote_fwd_listener(const char *listen_address,
2487: u_short listen_port, int gateway_ports)
2488: {
2489: return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2490: listen_address, listen_port, NULL, 0, gateway_ports);
2491: }
2492:
1.27 markus 2493: /*
2494: * Initiate forwarding of connections to port "port" on remote host through
2495: * the secure channel to host:port from local side.
2496: */
1.1 deraadt 2497:
1.253 markus 2498: int
1.212 djm 2499: channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
1.73 markus 2500: const char *host_to_connect, u_short port_to_connect)
1.25 markus 2501: {
1.153 markus 2502: int type, success = 0;
1.73 markus 2503:
1.25 markus 2504: /* Record locally that connection to this host/port is permitted. */
2505: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2506: fatal("channel_request_remote_forwarding: too many forwards");
2507:
2508: /* Send the forward request to the remote side. */
1.44 markus 2509: if (compat20) {
1.212 djm 2510: const char *address_to_bind;
1.267 djm 2511: if (listen_host == NULL) {
2512: if (datafellows & SSH_BUG_RFWD_ADDR)
2513: address_to_bind = "127.0.0.1";
2514: else
2515: address_to_bind = "localhost";
2516: } else if (*listen_host == '\0' ||
2517: strcmp(listen_host, "*") == 0) {
2518: if (datafellows & SSH_BUG_RFWD_ADDR)
2519: address_to_bind = "0.0.0.0";
2520: else
2521: address_to_bind = "";
2522: } else
1.212 djm 2523: address_to_bind = listen_host;
2524:
1.44 markus 2525: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2526: packet_put_cstring("tcpip-forward");
1.173 markus 2527: packet_put_char(1); /* boolean: want reply */
1.44 markus 2528: packet_put_cstring(address_to_bind);
2529: packet_put_int(listen_port);
1.73 markus 2530: packet_send();
2531: packet_write_wait();
2532: /* Assume that server accepts the request */
2533: success = 1;
1.44 markus 2534: } else {
2535: packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50 markus 2536: packet_put_int(listen_port);
2537: packet_put_cstring(host_to_connect);
1.44 markus 2538: packet_put_int(port_to_connect);
2539: packet_send();
2540: packet_write_wait();
1.73 markus 2541:
2542: /* Wait for response from the remote side. */
1.153 markus 2543: type = packet_read();
1.73 markus 2544: switch (type) {
2545: case SSH_SMSG_SUCCESS:
2546: success = 1;
2547: break;
2548: case SSH_SMSG_FAILURE:
2549: break;
2550: default:
2551: /* Unknown packet */
2552: packet_disconnect("Protocol error for port forward request:"
2553: "received packet type %d.", type);
2554: }
2555: }
2556: if (success) {
2557: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2558: permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2559: permitted_opens[num_permitted_opens].listen_port = listen_port;
2560: num_permitted_opens++;
1.44 markus 2561: }
1.253 markus 2562: return (success ? 0 : -1);
1.1 deraadt 2563: }
2564:
1.27 markus 2565: /*
1.208 deraadt 2566: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 2567: * local side.
2568: */
2569: void
1.212 djm 2570: channel_request_rforward_cancel(const char *host, u_short port)
1.202 djm 2571: {
2572: int i;
2573:
2574: if (!compat20)
2575: return;
2576:
2577: for (i = 0; i < num_permitted_opens; i++) {
1.208 deraadt 2578: if (permitted_opens[i].host_to_connect != NULL &&
1.202 djm 2579: permitted_opens[i].listen_port == port)
2580: break;
2581: }
2582: if (i >= num_permitted_opens) {
2583: debug("%s: requested forward not found", __func__);
2584: return;
2585: }
2586: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2587: packet_put_cstring("cancel-tcpip-forward");
2588: packet_put_char(0);
1.212 djm 2589: packet_put_cstring(host == NULL ? "" : host);
1.202 djm 2590: packet_put_int(port);
2591: packet_send();
2592:
2593: permitted_opens[i].listen_port = 0;
2594: permitted_opens[i].port_to_connect = 0;
1.227 stevesk 2595: xfree(permitted_opens[i].host_to_connect);
1.202 djm 2596: permitted_opens[i].host_to_connect = NULL;
2597: }
2598:
2599: /*
1.27 markus 2600: * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2601: * listening for the port, and sends back a success reply (or disconnect
1.253 markus 2602: * message if there was an error).
1.27 markus 2603: */
1.253 markus 2604: int
1.56 markus 2605: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1 deraadt 2606: {
1.31 markus 2607: u_short port, host_port;
1.253 markus 2608: int success = 0;
1.25 markus 2609: char *hostname;
1.1 deraadt 2610:
1.25 markus 2611: /* Get arguments from the packet. */
2612: port = packet_get_int();
2613: hostname = packet_get_string(NULL);
2614: host_port = packet_get_int();
2615:
1.27 markus 2616: /*
2617: * Check that an unprivileged user is not trying to forward a
2618: * privileged port.
2619: */
1.25 markus 2620: if (port < IPPORT_RESERVED && !is_root)
1.192 markus 2621: packet_disconnect(
2622: "Requested forwarding of port %d but user is not root.",
2623: port);
2624: if (host_port == 0)
2625: packet_disconnect("Dynamic forwarding denied.");
2626:
1.73 markus 2627: /* Initiate forwarding */
1.253 markus 2628: success = channel_setup_local_fwd_listener(NULL, port, hostname,
1.212 djm 2629: host_port, gateway_ports);
1.25 markus 2630:
2631: /* Free the argument string. */
2632: xfree(hostname);
1.253 markus 2633:
2634: return (success ? 0 : -1);
1.1 deraadt 2635: }
2636:
1.99 markus 2637: /*
2638: * Permits opening to any host/port if permitted_opens[] is empty. This is
2639: * usually called by the server, because the user could connect to any port
2640: * anyway, and the server has no way to know but to trust the client anyway.
2641: */
2642: void
1.142 itojun 2643: channel_permit_all_opens(void)
1.99 markus 2644: {
2645: if (num_permitted_opens == 0)
2646: all_opens_permitted = 1;
2647: }
2648:
1.101 markus 2649: void
1.99 markus 2650: channel_add_permitted_opens(char *host, int port)
2651: {
2652: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1.253 markus 2653: fatal("channel_add_permitted_opens: too many forwards");
1.99 markus 2654: debug("allow port forwarding to host %s port %d", host, port);
2655:
2656: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2657: permitted_opens[num_permitted_opens].port_to_connect = port;
2658: num_permitted_opens++;
2659:
2660: all_opens_permitted = 0;
2661: }
2662:
1.258 dtucker 2663: int
1.257 dtucker 2664: channel_add_adm_permitted_opens(char *host, int port)
2665: {
2666: if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2667: fatal("channel_add_adm_permitted_opens: too many forwards");
1.258 dtucker 2668: debug("config allows port forwarding to host %s port %d", host, port);
1.257 dtucker 2669:
2670: permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2671: = xstrdup(host);
2672: permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
1.258 dtucker 2673: return ++num_adm_permitted_opens;
1.257 dtucker 2674: }
2675:
2676: void
1.99 markus 2677: channel_clear_permitted_opens(void)
2678: {
2679: int i;
2680:
2681: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2682: if (permitted_opens[i].host_to_connect != NULL)
2683: xfree(permitted_opens[i].host_to_connect);
1.99 markus 2684: num_permitted_opens = 0;
1.257 dtucker 2685: }
2686:
2687: void
2688: channel_clear_adm_permitted_opens(void)
2689: {
2690: int i;
1.99 markus 2691:
1.257 dtucker 2692: for (i = 0; i < num_adm_permitted_opens; i++)
2693: if (permitted_adm_opens[i].host_to_connect != NULL)
2694: xfree(permitted_adm_opens[i].host_to_connect);
2695: num_adm_permitted_opens = 0;
1.99 markus 2696: }
2697:
2698: /* return socket to remote host, port */
1.127 itojun 2699: static int
1.99 markus 2700: connect_to(const char *host, u_short port)
1.41 markus 2701: {
2702: struct addrinfo hints, *ai, *aitop;
2703: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2704: int gaierr;
2705: int sock = -1;
2706:
2707: memset(&hints, 0, sizeof(hints));
2708: hints.ai_family = IPv4or6;
2709: hints.ai_socktype = SOCK_STREAM;
1.99 markus 2710: snprintf(strport, sizeof strport, "%d", port);
1.41 markus 2711: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1.99 markus 2712: error("connect_to %.100s: unknown host (%s)", host,
1.271 ! dtucker 2713: ssh_gai_strerror(gaierr));
1.41 markus 2714: return -1;
2715: }
2716: for (ai = aitop; ai; ai = ai->ai_next) {
2717: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2718: continue;
2719: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2720: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.99 markus 2721: error("connect_to: getnameinfo failed");
1.41 markus 2722: continue;
2723: }
1.189 markus 2724: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.41 markus 2725: if (sock < 0) {
1.186 djm 2726: if (ai->ai_next == NULL)
2727: error("socket: %.100s", strerror(errno));
2728: else
2729: verbose("socket: %.100s", strerror(errno));
1.41 markus 2730: continue;
2731: }
1.205 djm 2732: if (set_nonblock(sock) == -1)
2733: fatal("%s: set_nonblock(%d)", __func__, sock);
1.75 markus 2734: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2735: errno != EINPROGRESS) {
1.99 markus 2736: error("connect_to %.100s port %s: %.100s", ntop, strport,
1.41 markus 2737: strerror(errno));
2738: close(sock);
1.89 stevesk 2739: continue; /* fail -- try next */
1.41 markus 2740: }
2741: break; /* success */
2742:
2743: }
2744: freeaddrinfo(aitop);
2745: if (!ai) {
1.99 markus 2746: error("connect_to %.100s port %d: failed.", host, port);
1.41 markus 2747: return -1;
2748: }
2749: /* success */
1.169 stevesk 2750: set_nodelay(sock);
1.41 markus 2751: return sock;
2752: }
1.99 markus 2753:
1.73 markus 2754: int
1.130 stevesk 2755: channel_connect_by_listen_address(u_short listen_port)
1.73 markus 2756: {
2757: int i;
1.99 markus 2758:
1.73 markus 2759: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2760: if (permitted_opens[i].host_to_connect != NULL &&
2761: permitted_opens[i].listen_port == listen_port)
1.99 markus 2762: return connect_to(
1.73 markus 2763: permitted_opens[i].host_to_connect,
2764: permitted_opens[i].port_to_connect);
1.74 markus 2765: error("WARNING: Server requests forwarding for unknown listen_port %d",
2766: listen_port);
1.73 markus 2767: return -1;
2768: }
2769:
1.99 markus 2770: /* Check if connecting to that port is permitted and connect. */
2771: int
2772: channel_connect_to(const char *host, u_short port)
2773: {
1.257 dtucker 2774: int i, permit, permit_adm = 1;
1.99 markus 2775:
2776: permit = all_opens_permitted;
2777: if (!permit) {
2778: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2779: if (permitted_opens[i].host_to_connect != NULL &&
2780: permitted_opens[i].port_to_connect == port &&
1.99 markus 2781: strcmp(permitted_opens[i].host_to_connect, host) == 0)
2782: permit = 1;
1.257 dtucker 2783: }
1.99 markus 2784:
1.257 dtucker 2785: if (num_adm_permitted_opens > 0) {
2786: permit_adm = 0;
2787: for (i = 0; i < num_adm_permitted_opens; i++)
2788: if (permitted_adm_opens[i].host_to_connect != NULL &&
2789: permitted_adm_opens[i].port_to_connect == port &&
2790: strcmp(permitted_adm_opens[i].host_to_connect, host)
2791: == 0)
2792: permit_adm = 1;
1.99 markus 2793: }
1.257 dtucker 2794:
2795: if (!permit || !permit_adm) {
1.188 itojun 2796: logit("Received request to connect to host %.100s port %d, "
1.99 markus 2797: "but the request was denied.", host, port);
2798: return -1;
2799: }
2800: return connect_to(host, port);
1.204 djm 2801: }
2802:
2803: void
2804: channel_send_window_changes(void)
2805: {
1.209 avsm 2806: u_int i;
1.204 djm 2807: struct winsize ws;
2808:
2809: for (i = 0; i < channels_alloc; i++) {
1.213 deraadt 2810: if (channels[i] == NULL || !channels[i]->client_tty ||
1.204 djm 2811: channels[i]->type != SSH_CHANNEL_OPEN)
2812: continue;
2813: if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2814: continue;
2815: channel_request_start(i, "window-change", 0);
1.238 deraadt 2816: packet_put_int((u_int)ws.ws_col);
2817: packet_put_int((u_int)ws.ws_row);
2818: packet_put_int((u_int)ws.ws_xpixel);
2819: packet_put_int((u_int)ws.ws_ypixel);
1.204 djm 2820: packet_send();
2821: }
1.99 markus 2822: }
2823:
1.121 markus 2824: /* -- X11 forwarding */
1.1 deraadt 2825:
1.27 markus 2826: /*
2827: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 2828: * Returns 0 and a suitable display number for the DISPLAY variable
2829: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 2830: */
1.141 stevesk 2831: int
1.163 stevesk 2832: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.222 djm 2833: int single_connection, u_int *display_numberp, int **chanids)
1.1 deraadt 2834: {
1.149 markus 2835: Channel *nc = NULL;
1.31 markus 2836: int display_number, sock;
2837: u_short port;
1.35 markus 2838: struct addrinfo hints, *ai, *aitop;
2839: char strport[NI_MAXSERV];
2840: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 2841:
1.224 markus 2842: if (chanids == NULL)
2843: return -1;
2844:
1.33 markus 2845: for (display_number = x11_display_offset;
1.148 deraadt 2846: display_number < MAX_DISPLAYS;
2847: display_number++) {
1.25 markus 2848: port = 6000 + display_number;
1.35 markus 2849: memset(&hints, 0, sizeof(hints));
2850: hints.ai_family = IPv4or6;
1.163 stevesk 2851: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 2852: hints.ai_socktype = SOCK_STREAM;
2853: snprintf(strport, sizeof strport, "%d", port);
2854: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1.271 ! dtucker 2855: error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
1.141 stevesk 2856: return -1;
1.25 markus 2857: }
1.35 markus 2858: for (ai = aitop; ai; ai = ai->ai_next) {
2859: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2860: continue;
1.189 markus 2861: sock = socket(ai->ai_family, ai->ai_socktype,
2862: ai->ai_protocol);
1.35 markus 2863: if (sock < 0) {
2864: error("socket: %.100s", strerror(errno));
1.203 markus 2865: freeaddrinfo(aitop);
1.141 stevesk 2866: return -1;
1.35 markus 2867: }
1.226 djm 2868: channel_set_reuseaddr(sock);
1.35 markus 2869: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 2870: debug2("bind port %d: %.100s", port, strerror(errno));
1.35 markus 2871: close(sock);
1.183 itojun 2872:
2873: if (ai->ai_next)
2874: continue;
2875:
1.35 markus 2876: for (n = 0; n < num_socks; n++) {
2877: close(socks[n]);
2878: }
2879: num_socks = 0;
2880: break;
2881: }
2882: socks[num_socks++] = sock;
2883: if (num_socks == NUM_SOCKS)
2884: break;
1.25 markus 2885: }
1.83 stevesk 2886: freeaddrinfo(aitop);
1.35 markus 2887: if (num_socks > 0)
2888: break;
1.25 markus 2889: }
2890: if (display_number >= MAX_DISPLAYS) {
2891: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 2892: return -1;
1.25 markus 2893: }
2894: /* Start listening for connections on the socket. */
1.35 markus 2895: for (n = 0; n < num_socks; n++) {
2896: sock = socks[n];
1.199 markus 2897: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2898: error("listen: %.100s", strerror(errno));
2899: close(sock);
1.141 stevesk 2900: return -1;
1.35 markus 2901: }
1.25 markus 2902: }
1.35 markus 2903:
2904: /* Allocate a channel for each socket. */
1.242 djm 2905: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 2906: for (n = 0; n < num_socks; n++) {
2907: sock = socks[n];
1.149 markus 2908: nc = channel_new("x11 listener",
1.51 markus 2909: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2910: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 2911: 0, "X11 inet listener", 1);
1.167 markus 2912: nc->single_connection = single_connection;
1.224 markus 2913: (*chanids)[n] = nc->self;
1.35 markus 2914: }
1.224 markus 2915: (*chanids)[n] = -1;
1.1 deraadt 2916:
1.141 stevesk 2917: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 2918: *display_numberp = display_number;
2919: return (0);
1.1 deraadt 2920: }
2921:
1.127 itojun 2922: static int
1.77 markus 2923: connect_local_xsocket(u_int dnr)
1.1 deraadt 2924: {
1.25 markus 2925: int sock;
2926: struct sockaddr_un addr;
2927:
1.147 stevesk 2928: sock = socket(AF_UNIX, SOCK_STREAM, 0);
2929: if (sock < 0)
2930: error("socket: %.100s", strerror(errno));
2931: memset(&addr, 0, sizeof(addr));
2932: addr.sun_family = AF_UNIX;
2933: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 2934: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 2935: return sock;
2936: close(sock);
1.25 markus 2937: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2938: return -1;
1.1 deraadt 2939: }
2940:
1.51 markus 2941: int
2942: x11_connect_display(void)
1.1 deraadt 2943: {
1.248 deraadt 2944: u_int display_number;
1.25 markus 2945: const char *display;
1.51 markus 2946: char buf[1024], *cp;
1.35 markus 2947: struct addrinfo hints, *ai, *aitop;
2948: char strport[NI_MAXSERV];
1.248 deraadt 2949: int gaierr, sock = 0;
1.25 markus 2950:
2951: /* Try to open a socket for the local X server. */
2952: display = getenv("DISPLAY");
2953: if (!display) {
2954: error("DISPLAY not set.");
1.51 markus 2955: return -1;
1.25 markus 2956: }
1.27 markus 2957: /*
2958: * Now we decode the value of the DISPLAY variable and make a
2959: * connection to the real X server.
2960: */
2961:
2962: /*
2963: * Check if it is a unix domain socket. Unix domain displays are in
2964: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2965: */
1.25 markus 2966: if (strncmp(display, "unix:", 5) == 0 ||
2967: display[0] == ':') {
2968: /* Connect to the unix domain socket. */
1.248 deraadt 2969: if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
1.25 markus 2970: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2971: display);
1.51 markus 2972: return -1;
1.25 markus 2973: }
2974: /* Create a socket. */
2975: sock = connect_local_xsocket(display_number);
2976: if (sock < 0)
1.51 markus 2977: return -1;
1.25 markus 2978:
2979: /* OK, we now have a connection to the display. */
1.51 markus 2980: return sock;
1.25 markus 2981: }
1.27 markus 2982: /*
2983: * Connect to an inet socket. The DISPLAY value is supposedly
2984: * hostname:d[.s], where hostname may also be numeric IP address.
2985: */
1.145 stevesk 2986: strlcpy(buf, display, sizeof(buf));
1.25 markus 2987: cp = strchr(buf, ':');
2988: if (!cp) {
2989: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 2990: return -1;
1.25 markus 2991: }
2992: *cp = 0;
1.27 markus 2993: /* buf now contains the host name. But first we parse the display number. */
1.248 deraadt 2994: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 2995: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2996: display);
1.51 markus 2997: return -1;
1.25 markus 2998: }
1.35 markus 2999:
3000: /* Look up the host address */
3001: memset(&hints, 0, sizeof(hints));
3002: hints.ai_family = IPv4or6;
3003: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 3004: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 3005: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1.271 ! dtucker 3006: error("%.100s: unknown host. (%s)", buf,
! 3007: ssh_gai_strerror(gaierr));
1.51 markus 3008: return -1;
1.25 markus 3009: }
1.35 markus 3010: for (ai = aitop; ai; ai = ai->ai_next) {
3011: /* Create a socket. */
1.189 markus 3012: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 3013: if (sock < 0) {
1.194 markus 3014: debug2("socket: %.100s", strerror(errno));
1.41 markus 3015: continue;
3016: }
3017: /* Connect it to the display. */
3018: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.248 deraadt 3019: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 3020: 6000 + display_number, strerror(errno));
3021: close(sock);
3022: continue;
3023: }
3024: /* Success */
3025: break;
1.35 markus 3026: }
3027: freeaddrinfo(aitop);
3028: if (!ai) {
1.248 deraadt 3029: error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
1.35 markus 3030: strerror(errno));
1.51 markus 3031: return -1;
1.25 markus 3032: }
1.162 stevesk 3033: set_nodelay(sock);
1.51 markus 3034: return sock;
3035: }
3036:
3037: /*
3038: * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3039: * the remote channel number. We should do whatever we want, and respond
3040: * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3041: */
3042:
1.259 stevesk 3043: /* ARGSUSED */
1.51 markus 3044: void
1.154 markus 3045: x11_input_open(int type, u_int32_t seq, void *ctxt)
1.51 markus 3046: {
1.113 markus 3047: Channel *c = NULL;
3048: int remote_id, sock = 0;
1.51 markus 3049: char *remote_host;
1.25 markus 3050:
1.113 markus 3051: debug("Received X11 open request.");
1.51 markus 3052:
1.113 markus 3053: remote_id = packet_get_int();
1.121 markus 3054:
3055: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113 markus 3056: remote_host = packet_get_string(NULL);
1.51 markus 3057: } else {
3058: remote_host = xstrdup("unknown (remote did not supply name)");
3059: }
1.152 markus 3060: packet_check_eom();
1.25 markus 3061:
1.51 markus 3062: /* Obtain a connection to the real X display. */
3063: sock = x11_connect_display();
1.113 markus 3064: if (sock != -1) {
3065: /* Allocate a channel for this connection. */
3066: c = channel_new("connected x11 socket",
3067: SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3068: remote_host, 1);
1.167 markus 3069: c->remote_id = remote_id;
3070: c->force_drain = 1;
1.113 markus 3071: }
1.190 markus 3072: xfree(remote_host);
1.113 markus 3073: if (c == NULL) {
1.51 markus 3074: /* Send refusal to the remote host. */
3075: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 3076: packet_put_int(remote_id);
1.51 markus 3077: } else {
3078: /* Send a confirmation to the remote host. */
3079: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113 markus 3080: packet_put_int(remote_id);
3081: packet_put_int(c->self);
1.51 markus 3082: }
1.113 markus 3083: packet_send();
1.72 markus 3084: }
3085:
3086: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
1.259 stevesk 3087: /* ARGSUSED */
1.72 markus 3088: void
1.154 markus 3089: deny_input_open(int type, u_int32_t seq, void *ctxt)
1.72 markus 3090: {
3091: int rchan = packet_get_int();
1.180 deraadt 3092:
1.143 deraadt 3093: switch (type) {
1.72 markus 3094: case SSH_SMSG_AGENT_OPEN:
3095: error("Warning: ssh server tried agent forwarding.");
3096: break;
3097: case SSH_SMSG_X11_OPEN:
3098: error("Warning: ssh server tried X11 forwarding.");
3099: break;
3100: default:
1.154 markus 3101: error("deny_input_open: type %d", type);
1.72 markus 3102: break;
3103: }
1.230 stevesk 3104: error("Warning: this is probably a break-in attempt by a malicious server.");
1.72 markus 3105: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3106: packet_put_int(rchan);
3107: packet_send();
1.1 deraadt 3108: }
3109:
1.27 markus 3110: /*
3111: * Requests forwarding of X11 connections, generates fake authentication
3112: * data, and enables authentication spoofing.
1.121 markus 3113: * This should be called in the client only.
1.27 markus 3114: */
1.49 markus 3115: void
1.215 djm 3116: x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
1.51 markus 3117: const char *proto, const char *data)
1.1 deraadt 3118: {
1.77 markus 3119: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 3120: u_int i, value;
1.25 markus 3121: char *new_data;
3122: int screen_number;
3123: const char *cp;
1.207 avsm 3124: u_int32_t rnd = 0;
1.25 markus 3125:
1.220 markus 3126: if (x11_saved_display == NULL)
3127: x11_saved_display = xstrdup(disp);
3128: else if (strcmp(disp, x11_saved_display) != 0) {
1.219 djm 3129: error("x11_request_forwarding_with_spoofing: different "
3130: "$DISPLAY already forwarded");
3131: return;
3132: }
3133:
1.266 djm 3134: cp = strchr(disp, ':');
1.25 markus 3135: if (cp)
3136: cp = strchr(cp, '.');
3137: if (cp)
1.245 deraadt 3138: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 3139: else
3140: screen_number = 0;
3141:
1.219 djm 3142: if (x11_saved_proto == NULL) {
3143: /* Save protocol name. */
3144: x11_saved_proto = xstrdup(proto);
3145: /*
1.221 djm 3146: * Extract real authentication data and generate fake data
1.219 djm 3147: * of the same length.
3148: */
3149: x11_saved_data = xmalloc(data_len);
3150: x11_fake_data = xmalloc(data_len);
3151: for (i = 0; i < data_len; i++) {
3152: if (sscanf(data + 2 * i, "%2x", &value) != 1)
3153: fatal("x11_request_forwarding: bad "
3154: "authentication data: %.100s", data);
3155: if (i % 4 == 0)
3156: rnd = arc4random();
3157: x11_saved_data[i] = value;
3158: x11_fake_data[i] = rnd & 0xff;
3159: rnd >>= 8;
3160: }
3161: x11_saved_data_len = data_len;
3162: x11_fake_data_len = data_len;
1.25 markus 3163: }
3164:
3165: /* Convert the fake data into hex. */
1.219 djm 3166: new_data = tohex(x11_fake_data, data_len);
1.25 markus 3167:
3168: /* Send the request packet. */
1.51 markus 3169: if (compat20) {
3170: channel_request_start(client_session_id, "x11-req", 0);
3171: packet_put_char(0); /* XXX bool single connection */
3172: } else {
3173: packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3174: }
3175: packet_put_cstring(proto);
3176: packet_put_cstring(new_data);
1.25 markus 3177: packet_put_int(screen_number);
3178: packet_send();
3179: packet_write_wait();
3180: xfree(new_data);
1.1 deraadt 3181: }
3182:
1.121 markus 3183:
3184: /* -- agent forwarding */
3185:
1.1 deraadt 3186: /* Sends a message to the server to request authentication fd forwarding. */
3187:
1.49 markus 3188: void
1.142 itojun 3189: auth_request_forwarding(void)
1.1 deraadt 3190: {
1.25 markus 3191: packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3192: packet_send();
3193: packet_write_wait();
1.1 deraadt 3194: }