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