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