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