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