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