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