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