Annotation of src/usr.bin/ssh/channels.c, Revision 1.225
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.225 ! djm 42: RCSID("$OpenBSD: channels.c,v 1.224 2005/09/07 08:53:53 markus 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.41 markus 1232: /*
1233: * This socket is listening for connections to a forwarded TCP/IP port.
1234: */
1.127 itojun 1235: static void
1.41 markus 1236: channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1237: {
1.103 markus 1238: Channel *nc;
1.41 markus 1239: struct sockaddr addr;
1.113 markus 1240: int newsock, nextstate;
1.41 markus 1241: socklen_t addrlen;
1.103 markus 1242: char *rtype;
1.73 markus 1243:
1.41 markus 1244: if (FD_ISSET(c->sock, readset)) {
1245: debug("Connection to port %d forwarding "
1246: "to %.100s port %d requested.",
1247: c->listening_port, c->path, c->host_port);
1.103 markus 1248:
1.137 markus 1249: if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1250: nextstate = SSH_CHANNEL_OPENING;
1251: rtype = "forwarded-tcpip";
1252: } else {
1253: if (c->host_port == 0) {
1254: nextstate = SSH_CHANNEL_DYNAMIC;
1.138 markus 1255: rtype = "dynamic-tcpip";
1.137 markus 1256: } else {
1257: nextstate = SSH_CHANNEL_OPENING;
1258: rtype = "direct-tcpip";
1259: }
1260: }
1.103 markus 1261:
1.41 markus 1262: addrlen = sizeof(addr);
1263: newsock = accept(c->sock, &addr, &addrlen);
1264: if (newsock < 0) {
1265: error("accept: %.100s", strerror(errno));
1266: return;
1267: }
1.169 stevesk 1268: set_nodelay(newsock);
1.190 markus 1269: nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1270: c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1.103 markus 1271: nc->listening_port = c->listening_port;
1272: nc->host_port = c->host_port;
1273: strlcpy(nc->path, c->path, sizeof(nc->path));
1274:
1.137 markus 1275: if (nextstate == SSH_CHANNEL_DYNAMIC) {
1276: /*
1277: * do not call the channel_post handler until
1278: * this flag has been reset by a pre-handler.
1279: * otherwise the FD_ISSET calls might overflow
1280: */
1281: nc->delayed = 1;
1282: } else {
1.103 markus 1283: port_open_helper(nc, rtype);
1.137 markus 1284: }
1.41 markus 1285: }
1286: }
1.25 markus 1287:
1.41 markus 1288: /*
1289: * This is the authentication agent socket listening for connections from
1290: * clients.
1291: */
1.127 itojun 1292: static void
1.41 markus 1293: channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1294: {
1.113 markus 1295: Channel *nc;
1296: int newsock;
1.41 markus 1297: struct sockaddr addr;
1298: socklen_t addrlen;
1.25 markus 1299:
1.41 markus 1300: if (FD_ISSET(c->sock, readset)) {
1301: addrlen = sizeof(addr);
1302: newsock = accept(c->sock, &addr, &addrlen);
1303: if (newsock < 0) {
1304: error("accept from auth socket: %.100s", strerror(errno));
1305: return;
1306: }
1.113 markus 1307: nc = channel_new("accepted auth socket",
1.73 markus 1308: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1309: c->local_window_max, c->local_maxpacket,
1.190 markus 1310: 0, "accepted auth socket", 1);
1.73 markus 1311: if (compat20) {
1312: packet_start(SSH2_MSG_CHANNEL_OPEN);
1313: packet_put_cstring("auth-agent@openssh.com");
1.113 markus 1314: packet_put_int(nc->self);
1.73 markus 1315: packet_put_int(c->local_window_max);
1316: packet_put_int(c->local_maxpacket);
1317: } else {
1318: packet_start(SSH_SMSG_AGENT_OPEN);
1.113 markus 1319: packet_put_int(nc->self);
1.73 markus 1320: }
1.41 markus 1321: packet_send();
1322: }
1323: }
1.25 markus 1324:
1.127 itojun 1325: static void
1.75 markus 1326: channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1327: {
1.114 markus 1328: int err = 0;
1.129 stevesk 1329: socklen_t sz = sizeof(err);
1.114 markus 1330:
1.75 markus 1331: if (FD_ISSET(c->sock, writeset)) {
1.170 stevesk 1332: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1.114 markus 1333: err = errno;
1334: error("getsockopt SO_ERROR failed");
1335: }
1336: if (err == 0) {
1337: debug("channel %d: connected", c->self);
1338: c->type = SSH_CHANNEL_OPEN;
1339: if (compat20) {
1340: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1341: packet_put_int(c->remote_id);
1342: packet_put_int(c->self);
1343: packet_put_int(c->local_window);
1344: packet_put_int(c->local_maxpacket);
1345: } else {
1346: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1347: packet_put_int(c->remote_id);
1348: packet_put_int(c->self);
1349: }
1.75 markus 1350: } else {
1.114 markus 1351: debug("channel %d: not connected: %s",
1352: c->self, strerror(err));
1353: if (compat20) {
1354: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1355: packet_put_int(c->remote_id);
1356: packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1357: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1358: packet_put_cstring(strerror(err));
1359: packet_put_cstring("");
1360: }
1.75 markus 1361: } else {
1.114 markus 1362: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1363: packet_put_int(c->remote_id);
1.75 markus 1364: }
1.114 markus 1365: chan_mark_dead(c);
1.75 markus 1366: }
1.114 markus 1367: packet_send();
1.75 markus 1368: }
1369: }
1370:
1.127 itojun 1371: static int
1.41 markus 1372: channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1373: {
1.214 markus 1374: char buf[CHAN_RBUF];
1.41 markus 1375: int len;
1.25 markus 1376:
1.118 markus 1377: if (c->rfd != -1 &&
1.41 markus 1378: FD_ISSET(c->rfd, readset)) {
1379: len = read(c->rfd, buf, sizeof(buf));
1.53 markus 1380: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1381: return 1;
1.41 markus 1382: if (len <= 0) {
1.194 markus 1383: debug2("channel %d: read<=0 rfd %d len %d",
1.41 markus 1384: c->self, c->rfd, len);
1.104 markus 1385: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1386: debug2("channel %d: not open", c->self);
1.113 markus 1387: chan_mark_dead(c);
1.103 markus 1388: return -1;
1.104 markus 1389: } else if (compat13) {
1.158 markus 1390: buffer_clear(&c->output);
1.41 markus 1391: c->type = SSH_CHANNEL_INPUT_DRAINING;
1.194 markus 1392: debug2("channel %d: input draining.", c->self);
1.25 markus 1393: } else {
1.41 markus 1394: chan_read_failed(c);
1.25 markus 1395: }
1.41 markus 1396: return -1;
1397: }
1.143 deraadt 1398: if (c->input_filter != NULL) {
1.66 markus 1399: if (c->input_filter(c, buf, len) == -1) {
1.194 markus 1400: debug2("channel %d: filter stops", c->self);
1.65 markus 1401: chan_read_failed(c);
1402: }
1403: } else {
1404: buffer_append(&c->input, buf, len);
1405: }
1.41 markus 1406: }
1407: return 1;
1408: }
1.127 itojun 1409: static int
1.41 markus 1410: channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1411: {
1.95 markus 1412: struct termios tio;
1.134 markus 1413: u_char *data;
1414: u_int dlen;
1.41 markus 1415: int len;
1.25 markus 1416:
1.41 markus 1417: /* Send buffered output data to the socket. */
1.118 markus 1418: if (c->wfd != -1 &&
1.41 markus 1419: FD_ISSET(c->wfd, writeset) &&
1420: buffer_len(&c->output) > 0) {
1.134 markus 1421: data = buffer_ptr(&c->output);
1422: dlen = buffer_len(&c->output);
1423: len = write(c->wfd, data, dlen);
1.53 markus 1424: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1425: return 1;
1.41 markus 1426: if (len <= 0) {
1.104 markus 1427: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1428: debug2("channel %d: not open", c->self);
1.113 markus 1429: chan_mark_dead(c);
1.104 markus 1430: return -1;
1431: } else if (compat13) {
1.158 markus 1432: buffer_clear(&c->output);
1.194 markus 1433: debug2("channel %d: input draining.", c->self);
1.41 markus 1434: c->type = SSH_CHANNEL_INPUT_DRAINING;
1435: } else {
1436: chan_write_failed(c);
1437: }
1438: return -1;
1.91 markus 1439: }
1.134 markus 1440: if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
1.91 markus 1441: if (tcgetattr(c->wfd, &tio) == 0 &&
1442: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1443: /*
1444: * Simulate echo to reduce the impact of
1.96 markus 1445: * traffic analysis. We need to match the
1.95 markus 1446: * size of a SSH2_MSG_CHANNEL_DATA message
1447: * (4 byte channel id + data)
1.91 markus 1448: */
1.95 markus 1449: packet_send_ignore(4 + len);
1.91 markus 1450: packet_send();
1451: }
1.25 markus 1452: }
1.41 markus 1453: buffer_consume(&c->output, len);
1.44 markus 1454: if (compat20 && len > 0) {
1455: c->local_consumed += len;
1456: }
1457: }
1458: return 1;
1459: }
1.127 itojun 1460: static int
1.44 markus 1461: channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1462: {
1.214 markus 1463: char buf[CHAN_RBUF];
1.44 markus 1464: int len;
1465:
1.45 markus 1466: /** XXX handle drain efd, too */
1.44 markus 1467: if (c->efd != -1) {
1468: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1469: FD_ISSET(c->efd, writeset) &&
1470: buffer_len(&c->extended) > 0) {
1471: len = write(c->efd, buffer_ptr(&c->extended),
1472: buffer_len(&c->extended));
1.70 markus 1473: debug2("channel %d: written %d to efd %d",
1.44 markus 1474: c->self, len, c->efd);
1.93 markus 1475: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1476: return 1;
1477: if (len <= 0) {
1478: debug2("channel %d: closing write-efd %d",
1479: c->self, c->efd);
1.132 markus 1480: channel_close_fd(&c->efd);
1.93 markus 1481: } else {
1.44 markus 1482: buffer_consume(&c->extended, len);
1483: c->local_consumed += len;
1484: }
1485: } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1486: FD_ISSET(c->efd, readset)) {
1487: len = read(c->efd, buf, sizeof(buf));
1.70 markus 1488: debug2("channel %d: read %d from efd %d",
1.148 deraadt 1489: c->self, len, c->efd);
1.93 markus 1490: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1491: return 1;
1492: if (len <= 0) {
1493: debug2("channel %d: closing read-efd %d",
1.45 markus 1494: c->self, c->efd);
1.132 markus 1495: channel_close_fd(&c->efd);
1.93 markus 1496: } else {
1.44 markus 1497: buffer_append(&c->extended, buf, len);
1.93 markus 1498: }
1.44 markus 1499: }
1500: }
1501: return 1;
1502: }
1.127 itojun 1503: static int
1.204 djm 1504: channel_handle_ctl(Channel *c, fd_set * readset, fd_set * writeset)
1505: {
1506: char buf[16];
1507: int len;
1508:
1509: /* Monitor control fd to detect if the slave client exits */
1510: if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1511: len = read(c->ctl_fd, buf, sizeof(buf));
1512: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1513: return 1;
1514: if (len <= 0) {
1515: debug2("channel %d: ctl read<=0", c->self);
1516: if (c->type != SSH_CHANNEL_OPEN) {
1517: debug2("channel %d: not open", c->self);
1518: chan_mark_dead(c);
1519: return -1;
1520: } else {
1521: chan_read_failed(c);
1522: chan_write_failed(c);
1523: }
1524: return -1;
1525: } else
1526: fatal("%s: unexpected data on ctl fd", __func__);
1527: }
1528: return 1;
1529: }
1530: static int
1.93 markus 1531: channel_check_window(Channel *c)
1.44 markus 1532: {
1.104 markus 1533: if (c->type == SSH_CHANNEL_OPEN &&
1534: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.44 markus 1535: c->local_window < c->local_window_max/2 &&
1536: c->local_consumed > 0) {
1537: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1538: packet_put_int(c->remote_id);
1539: packet_put_int(c->local_consumed);
1540: packet_send();
1.70 markus 1541: debug2("channel %d: window %d sent adjust %d",
1.44 markus 1542: c->self, c->local_window,
1543: c->local_consumed);
1544: c->local_window += c->local_consumed;
1545: c->local_consumed = 0;
1.1 deraadt 1546: }
1.41 markus 1547: return 1;
1.1 deraadt 1548: }
1549:
1.127 itojun 1550: static void
1.157 markus 1551: channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
1.41 markus 1552: {
1.137 markus 1553: if (c->delayed)
1554: return;
1.41 markus 1555: channel_handle_rfd(c, readset, writeset);
1556: channel_handle_wfd(c, readset, writeset);
1.157 markus 1557: if (!compat20)
1.137 markus 1558: return;
1.44 markus 1559: channel_handle_efd(c, readset, writeset);
1.204 djm 1560: channel_handle_ctl(c, readset, writeset);
1.93 markus 1561: channel_check_window(c);
1.44 markus 1562: }
1563:
1.127 itojun 1564: static void
1.41 markus 1565: channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1.1 deraadt 1566: {
1.41 markus 1567: int len;
1.180 deraadt 1568:
1.41 markus 1569: /* Send buffered output data to the socket. */
1570: if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1571: len = write(c->sock, buffer_ptr(&c->output),
1572: buffer_len(&c->output));
1573: if (len <= 0)
1.158 markus 1574: buffer_clear(&c->output);
1.41 markus 1575: else
1576: buffer_consume(&c->output, len);
1577: }
1578: }
1.25 markus 1579:
1.127 itojun 1580: static void
1.44 markus 1581: channel_handler_init_20(void)
1582: {
1.157 markus 1583: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1584: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.44 markus 1585: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1.73 markus 1586: channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1.51 markus 1587: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1.73 markus 1588: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1589: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1590: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.44 markus 1591:
1.157 markus 1592: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.44 markus 1593: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1.73 markus 1594: channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1.51 markus 1595: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1.73 markus 1596: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.75 markus 1597: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1598: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.44 markus 1599: }
1600:
1.127 itojun 1601: static void
1.41 markus 1602: channel_handler_init_13(void)
1603: {
1604: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1605: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1606: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1607: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1608: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1609: channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1610: channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1.75 markus 1611: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1612: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1613:
1.157 markus 1614: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.41 markus 1615: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1616: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1617: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1618: channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1.75 markus 1619: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1620: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1621: }
1.25 markus 1622:
1.127 itojun 1623: static void
1.41 markus 1624: channel_handler_init_15(void)
1625: {
1.157 markus 1626: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1627: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.41 markus 1628: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1629: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1630: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1631: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1632: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1633:
1.41 markus 1634: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1635: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1636: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.157 markus 1637: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.75 markus 1638: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1639: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1640: }
1.27 markus 1641:
1.127 itojun 1642: static void
1.41 markus 1643: channel_handler_init(void)
1644: {
1645: int i;
1.180 deraadt 1646:
1.148 deraadt 1647: for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1.41 markus 1648: channel_pre[i] = NULL;
1649: channel_post[i] = NULL;
1650: }
1.44 markus 1651: if (compat20)
1652: channel_handler_init_20();
1653: else if (compat13)
1.41 markus 1654: channel_handler_init_13();
1655: else
1656: channel_handler_init_15();
1657: }
1.25 markus 1658:
1.140 markus 1659: /* gc dead channels */
1660: static void
1661: channel_garbage_collect(Channel *c)
1662: {
1663: if (c == NULL)
1664: return;
1665: if (c->detach_user != NULL) {
1.225 ! djm 1666: if (!chan_is_dead(c, c->detach_close))
1.140 markus 1667: return;
1.194 markus 1668: debug2("channel %d: gc: notify user", c->self);
1.140 markus 1669: c->detach_user(c->self, NULL);
1670: /* if we still have a callback */
1671: if (c->detach_user != NULL)
1672: return;
1.194 markus 1673: debug2("channel %d: gc: user detached", c->self);
1.140 markus 1674: }
1675: if (!chan_is_dead(c, 1))
1676: return;
1.194 markus 1677: debug2("channel %d: garbage collecting", c->self);
1.140 markus 1678: channel_free(c);
1679: }
1680:
1.127 itojun 1681: static void
1.41 markus 1682: channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1683: {
1684: static int did_init = 0;
1.209 avsm 1685: u_int i;
1.41 markus 1686: Channel *c;
1.25 markus 1687:
1.41 markus 1688: if (!did_init) {
1689: channel_handler_init();
1690: did_init = 1;
1691: }
1692: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1693: c = channels[i];
1694: if (c == NULL)
1.41 markus 1695: continue;
1.118 markus 1696: if (ftab[c->type] != NULL)
1697: (*ftab[c->type])(c, readset, writeset);
1.140 markus 1698: channel_garbage_collect(c);
1.1 deraadt 1699: }
1700: }
1701:
1.121 markus 1702: /*
1703: * Allocate/update select bitmasks and add any bits relevant to channels in
1704: * select bitmasks.
1705: */
1.49 markus 1706: void
1.100 markus 1707: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1.209 avsm 1708: u_int *nallocp, int rekeying)
1.41 markus 1709: {
1.209 avsm 1710: u_int n, sz;
1.84 markus 1711:
1712: n = MAX(*maxfdp, channel_max_fd);
1713:
1714: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.132 markus 1715: /* perhaps check sz < nalloc/2 and shrink? */
1716: if (*readsetp == NULL || sz > *nallocp) {
1717: *readsetp = xrealloc(*readsetp, sz);
1718: *writesetp = xrealloc(*writesetp, sz);
1719: *nallocp = sz;
1.84 markus 1720: }
1.132 markus 1721: *maxfdp = n;
1.84 markus 1722: memset(*readsetp, 0, sz);
1723: memset(*writesetp, 0, sz);
1724:
1.100 markus 1725: if (!rekeying)
1726: channel_handler(channel_pre, *readsetp, *writesetp);
1.41 markus 1727: }
1728:
1.121 markus 1729: /*
1730: * After select, perform any appropriate operations for channels which have
1731: * events pending.
1732: */
1.49 markus 1733: void
1.41 markus 1734: channel_after_select(fd_set * readset, fd_set * writeset)
1735: {
1736: channel_handler(channel_post, readset, writeset);
1737: }
1738:
1.121 markus 1739:
1.84 markus 1740: /* If there is data to send to the connection, enqueue some of it now. */
1.1 deraadt 1741:
1.49 markus 1742: void
1.142 itojun 1743: channel_output_poll(void)
1.1 deraadt 1744: {
1.41 markus 1745: Channel *c;
1.209 avsm 1746: u_int i, len;
1.1 deraadt 1747:
1.25 markus 1748: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1749: c = channels[i];
1750: if (c == NULL)
1751: continue;
1.37 markus 1752:
1.121 markus 1753: /*
1754: * We are only interested in channels that can have buffered
1755: * incoming data.
1756: */
1.37 markus 1757: if (compat13) {
1.41 markus 1758: if (c->type != SSH_CHANNEL_OPEN &&
1759: c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37 markus 1760: continue;
1761: } else {
1.41 markus 1762: if (c->type != SSH_CHANNEL_OPEN)
1.37 markus 1763: continue;
1764: }
1.46 markus 1765: if (compat20 &&
1766: (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 1767: /* XXX is this true? */
1.140 markus 1768: debug3("channel %d: will not send data after close", c->self);
1.44 markus 1769: continue;
1770: }
1.25 markus 1771:
1772: /* Get the amount of buffered data for this channel. */
1.93 markus 1773: if ((c->istate == CHAN_INPUT_OPEN ||
1774: c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1775: (len = buffer_len(&c->input)) > 0) {
1.121 markus 1776: /*
1777: * Send some data for the other side over the secure
1778: * connection.
1779: */
1.44 markus 1780: if (compat20) {
1781: if (len > c->remote_window)
1782: len = c->remote_window;
1783: if (len > c->remote_maxpacket)
1784: len = c->remote_maxpacket;
1.25 markus 1785: } else {
1.44 markus 1786: if (packet_is_interactive()) {
1787: if (len > 1024)
1788: len = 512;
1789: } else {
1790: /* Keep the packets at reasonable size. */
1791: if (len > packet_get_maxsize()/2)
1792: len = packet_get_maxsize()/2;
1793: }
1.25 markus 1794: }
1.41 markus 1795: if (len > 0) {
1.44 markus 1796: packet_start(compat20 ?
1797: SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41 markus 1798: packet_put_int(c->remote_id);
1799: packet_put_string(buffer_ptr(&c->input), len);
1800: packet_send();
1801: buffer_consume(&c->input, len);
1802: c->remote_window -= len;
1803: }
1804: } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25 markus 1805: if (compat13)
1806: fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27 markus 1807: /*
1808: * input-buffer is empty and read-socket shutdown:
1.172 markus 1809: * tell peer, that we will not send more data: send IEOF.
1810: * hack for extended data: delay EOF if EFD still in use.
1.27 markus 1811: */
1.172 markus 1812: if (CHANNEL_EFD_INPUT_ACTIVE(c))
1.223 djm 1813: debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1814: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 1815: else
1816: chan_ibuf_empty(c);
1.25 markus 1817: }
1.44 markus 1818: /* Send extended data, i.e. stderr */
1819: if (compat20 &&
1.172 markus 1820: !(c->flags & CHAN_EOF_SENT) &&
1.44 markus 1821: c->remote_window > 0 &&
1822: (len = buffer_len(&c->extended)) > 0 &&
1823: c->extended_usage == CHAN_EXTENDED_READ) {
1.178 markus 1824: debug2("channel %d: rwin %u elen %u euse %d",
1.93 markus 1825: c->self, c->remote_window, buffer_len(&c->extended),
1826: c->extended_usage);
1.44 markus 1827: if (len > c->remote_window)
1828: len = c->remote_window;
1829: if (len > c->remote_maxpacket)
1830: len = c->remote_maxpacket;
1831: packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1832: packet_put_int(c->remote_id);
1833: packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1834: packet_put_string(buffer_ptr(&c->extended), len);
1835: packet_send();
1836: buffer_consume(&c->extended, len);
1837: c->remote_window -= len;
1.93 markus 1838: debug2("channel %d: sent ext data %d", c->self, len);
1.44 markus 1839: }
1.25 markus 1840: }
1.1 deraadt 1841: }
1842:
1.121 markus 1843:
1844: /* -- protocol input */
1.1 deraadt 1845:
1.49 markus 1846: void
1.154 markus 1847: channel_input_data(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 1848: {
1.37 markus 1849: int id;
1.25 markus 1850: char *data;
1.77 markus 1851: u_int data_len;
1.41 markus 1852: Channel *c;
1.25 markus 1853:
1854: /* Get the channel number and verify it. */
1.37 markus 1855: id = packet_get_int();
1.41 markus 1856: c = channel_lookup(id);
1857: if (c == NULL)
1.37 markus 1858: packet_disconnect("Received data for nonexistent channel %d.", id);
1.25 markus 1859:
1860: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 1861: if (c->type != SSH_CHANNEL_OPEN &&
1862: c->type != SSH_CHANNEL_X11_OPEN)
1.37 markus 1863: return;
1864:
1.25 markus 1865: /* Get the data. */
1866: data = packet_get_string(&data_len);
1.200 markus 1867:
1868: /*
1869: * Ignore data for protocol > 1.3 if output end is no longer open.
1870: * For protocol 2 the sending side is reducing its window as it sends
1871: * data, so we must 'fake' consumption of the data in order to ensure
1872: * that window updates are sent back. Otherwise the connection might
1873: * deadlock.
1874: */
1875: if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
1876: if (compat20) {
1877: c->local_window -= data_len;
1878: c->local_consumed += data_len;
1879: }
1880: xfree(data);
1881: return;
1882: }
1.41 markus 1883:
1.143 deraadt 1884: if (compat20) {
1.44 markus 1885: if (data_len > c->local_maxpacket) {
1.188 itojun 1886: logit("channel %d: rcvd big packet %d, maxpack %d",
1.44 markus 1887: c->self, data_len, c->local_maxpacket);
1888: }
1889: if (data_len > c->local_window) {
1.188 itojun 1890: logit("channel %d: rcvd too much data %d, win %d",
1.44 markus 1891: c->self, data_len, c->local_window);
1892: xfree(data);
1893: return;
1894: }
1895: c->local_window -= data_len;
1896: }
1.152 markus 1897: packet_check_eom();
1.41 markus 1898: buffer_append(&c->output, data, data_len);
1.25 markus 1899: xfree(data);
1.1 deraadt 1900: }
1.121 markus 1901:
1.49 markus 1902: void
1.154 markus 1903: channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1.44 markus 1904: {
1905: int id;
1906: char *data;
1.177 markus 1907: u_int data_len, tcode;
1.44 markus 1908: Channel *c;
1909:
1910: /* Get the channel number and verify it. */
1911: id = packet_get_int();
1912: c = channel_lookup(id);
1913:
1914: if (c == NULL)
1915: packet_disconnect("Received extended_data for bad channel %d.", id);
1916: if (c->type != SSH_CHANNEL_OPEN) {
1.188 itojun 1917: logit("channel %d: ext data for non open", id);
1.44 markus 1918: return;
1.172 markus 1919: }
1920: if (c->flags & CHAN_EOF_RCVD) {
1921: if (datafellows & SSH_BUG_EXTEOF)
1922: debug("channel %d: accepting ext data after eof", id);
1923: else
1924: packet_disconnect("Received extended_data after EOF "
1925: "on channel %d.", id);
1.44 markus 1926: }
1927: tcode = packet_get_int();
1928: if (c->efd == -1 ||
1929: c->extended_usage != CHAN_EXTENDED_WRITE ||
1930: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 1931: logit("channel %d: bad ext data", c->self);
1.44 markus 1932: return;
1933: }
1934: data = packet_get_string(&data_len);
1.152 markus 1935: packet_check_eom();
1.44 markus 1936: if (data_len > c->local_window) {
1.188 itojun 1937: logit("channel %d: rcvd too much extended_data %d, win %d",
1.44 markus 1938: c->self, data_len, c->local_window);
1939: xfree(data);
1940: return;
1941: }
1.70 markus 1942: debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44 markus 1943: c->local_window -= data_len;
1944: buffer_append(&c->extended, data, data_len);
1945: xfree(data);
1946: }
1947:
1.49 markus 1948: void
1.154 markus 1949: channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1.41 markus 1950: {
1951: int id;
1952: Channel *c;
1953:
1954: id = packet_get_int();
1.152 markus 1955: packet_check_eom();
1.41 markus 1956: c = channel_lookup(id);
1957: if (c == NULL)
1958: packet_disconnect("Received ieof for nonexistent channel %d.", id);
1959: chan_rcvd_ieof(c);
1.133 markus 1960:
1961: /* XXX force input close */
1.156 markus 1962: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 1963: debug("channel %d: FORCE input drain", c->self);
1964: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.161 markus 1965: if (buffer_len(&c->input) == 0)
1966: chan_ibuf_empty(c);
1.133 markus 1967: }
1968:
1.41 markus 1969: }
1.1 deraadt 1970:
1.49 markus 1971: void
1.154 markus 1972: channel_input_close(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 1973: {
1.41 markus 1974: int id;
1975: Channel *c;
1.1 deraadt 1976:
1.41 markus 1977: id = packet_get_int();
1.152 markus 1978: packet_check_eom();
1.41 markus 1979: c = channel_lookup(id);
1980: if (c == NULL)
1981: packet_disconnect("Received close for nonexistent channel %d.", id);
1.27 markus 1982:
1983: /*
1984: * Send a confirmation that we have closed the channel and no more
1985: * data is coming for it.
1986: */
1.25 markus 1987: packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41 markus 1988: packet_put_int(c->remote_id);
1.25 markus 1989: packet_send();
1990:
1.27 markus 1991: /*
1992: * If the channel is in closed state, we have sent a close request,
1993: * and the other side will eventually respond with a confirmation.
1994: * Thus, we cannot free the channel here, because then there would be
1995: * no-one to receive the confirmation. The channel gets freed when
1996: * the confirmation arrives.
1997: */
1.41 markus 1998: if (c->type != SSH_CHANNEL_CLOSED) {
1.27 markus 1999: /*
2000: * Not a closed channel - mark it as draining, which will
2001: * cause it to be freed later.
2002: */
1.158 markus 2003: buffer_clear(&c->input);
1.41 markus 2004: c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25 markus 2005: }
1.1 deraadt 2006: }
2007:
1.41 markus 2008: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.49 markus 2009: void
1.154 markus 2010: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41 markus 2011: {
2012: int id = packet_get_int();
2013: Channel *c = channel_lookup(id);
1.151 markus 2014:
1.152 markus 2015: packet_check_eom();
1.41 markus 2016: if (c == NULL)
2017: packet_disconnect("Received oclose for nonexistent channel %d.", id);
2018: chan_rcvd_oclose(c);
2019: }
1.1 deraadt 2020:
1.49 markus 2021: void
1.154 markus 2022: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2023: {
1.41 markus 2024: int id = packet_get_int();
2025: Channel *c = channel_lookup(id);
1.1 deraadt 2026:
1.152 markus 2027: packet_check_eom();
1.41 markus 2028: if (c == NULL)
2029: packet_disconnect("Received close confirmation for "
2030: "out-of-range channel %d.", id);
2031: if (c->type != SSH_CHANNEL_CLOSED)
2032: packet_disconnect("Received close confirmation for "
2033: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 2034: channel_free(c);
1.1 deraadt 2035: }
2036:
1.49 markus 2037: void
1.154 markus 2038: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2039: {
1.41 markus 2040: int id, remote_id;
2041: Channel *c;
1.1 deraadt 2042:
1.41 markus 2043: id = packet_get_int();
2044: c = channel_lookup(id);
1.25 markus 2045:
1.41 markus 2046: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2047: packet_disconnect("Received open confirmation for "
2048: "non-opening channel %d.", id);
2049: remote_id = packet_get_int();
1.27 markus 2050: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 2051: c->remote_id = remote_id;
2052: c->type = SSH_CHANNEL_OPEN;
1.44 markus 2053:
2054: if (compat20) {
2055: c->remote_window = packet_get_int();
2056: c->remote_maxpacket = packet_get_int();
1.165 markus 2057: if (c->confirm) {
1.70 markus 2058: debug2("callback start");
1.204 djm 2059: c->confirm(c->self, c->confirm_ctx);
1.70 markus 2060: debug2("callback done");
1.44 markus 2061: }
1.194 markus 2062: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
1.44 markus 2063: c->remote_window, c->remote_maxpacket);
2064: }
1.152 markus 2065: packet_check_eom();
1.1 deraadt 2066: }
2067:
1.127 itojun 2068: static char *
1.114 markus 2069: reason2txt(int reason)
2070: {
1.143 deraadt 2071: switch (reason) {
1.114 markus 2072: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2073: return "administratively prohibited";
2074: case SSH2_OPEN_CONNECT_FAILED:
2075: return "connect failed";
2076: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2077: return "unknown channel type";
2078: case SSH2_OPEN_RESOURCE_SHORTAGE:
2079: return "resource shortage";
2080: }
1.117 stevesk 2081: return "unknown reason";
1.114 markus 2082: }
2083:
1.49 markus 2084: void
1.154 markus 2085: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2086: {
1.86 markus 2087: int id, reason;
2088: char *msg = NULL, *lang = NULL;
1.41 markus 2089: Channel *c;
2090:
2091: id = packet_get_int();
2092: c = channel_lookup(id);
1.25 markus 2093:
1.41 markus 2094: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2095: packet_disconnect("Received open failure for "
2096: "non-opening channel %d.", id);
1.44 markus 2097: if (compat20) {
1.86 markus 2098: reason = packet_get_int();
1.110 markus 2099: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86 markus 2100: msg = packet_get_string(NULL);
2101: lang = packet_get_string(NULL);
2102: }
1.188 itojun 2103: logit("channel %d: open failed: %s%s%s", id,
1.114 markus 2104: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86 markus 2105: if (msg != NULL)
2106: xfree(msg);
2107: if (lang != NULL)
2108: xfree(lang);
1.44 markus 2109: }
1.152 markus 2110: packet_check_eom();
1.25 markus 2111: /* Free the channel. This will also close the socket. */
1.113 markus 2112: channel_free(c);
1.44 markus 2113: }
2114:
1.121 markus 2115: void
1.154 markus 2116: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121 markus 2117: {
2118: Channel *c;
1.178 markus 2119: int id;
2120: u_int adjust;
1.121 markus 2121:
2122: if (!compat20)
2123: return;
2124:
2125: /* Get the channel number and verify it. */
2126: id = packet_get_int();
2127: c = channel_lookup(id);
1.1 deraadt 2128:
1.121 markus 2129: if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1.188 itojun 2130: logit("Received window adjust for "
1.121 markus 2131: "non-open channel %d.", id);
2132: return;
2133: }
2134: adjust = packet_get_int();
1.152 markus 2135: packet_check_eom();
1.178 markus 2136: debug2("channel %d: rcvd adjust %u", id, adjust);
1.121 markus 2137: c->remote_window += adjust;
2138: }
1.1 deraadt 2139:
1.121 markus 2140: void
1.154 markus 2141: channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2142: {
1.121 markus 2143: Channel *c = NULL;
2144: u_short host_port;
2145: char *host, *originator_string;
2146: int remote_id, sock = -1;
2147:
2148: remote_id = packet_get_int();
2149: host = packet_get_string(NULL);
2150: host_port = packet_get_int();
1.25 markus 2151:
1.121 markus 2152: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2153: originator_string = packet_get_string(NULL);
2154: } else {
2155: originator_string = xstrdup("unknown (remote did not supply name)");
2156: }
1.152 markus 2157: packet_check_eom();
1.121 markus 2158: sock = channel_connect_to(host, host_port);
2159: if (sock != -1) {
2160: c = channel_new("connected socket",
2161: SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2162: originator_string, 1);
1.167 markus 2163: c->remote_id = remote_id;
1.25 markus 2164: }
1.190 markus 2165: xfree(originator_string);
1.121 markus 2166: if (c == NULL) {
2167: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2168: packet_put_int(remote_id);
2169: packet_send();
2170: }
2171: xfree(host);
1.1 deraadt 2172: }
2173:
1.121 markus 2174:
2175: /* -- tcp forwarding */
1.135 markus 2176:
2177: void
2178: channel_set_af(int af)
2179: {
2180: IPv4or6 = af;
2181: }
1.121 markus 2182:
1.160 markus 2183: static int
2184: channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2185: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
1.25 markus 2186: {
1.113 markus 2187: Channel *c;
1.212 djm 2188: int sock, r, success = 0, on = 1, wildcard = 0, is_client;
1.35 markus 2189: struct addrinfo hints, *ai, *aitop;
1.212 djm 2190: const char *host, *addr;
1.35 markus 2191: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.25 markus 2192:
1.160 markus 2193: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2194: listen_addr : host_to_connect;
1.212 djm 2195: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 2196:
1.160 markus 2197: if (host == NULL) {
2198: error("No forward host name.");
1.218 markus 2199: return 0;
1.73 markus 2200: }
1.113 markus 2201: if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
1.87 markus 2202: error("Forward host name too long.");
1.218 markus 2203: return 0;
1.87 markus 2204: }
1.25 markus 2205:
1.28 markus 2206: /*
1.212 djm 2207: * Determine whether or not a port forward listens to loopback,
1.213 deraadt 2208: * specified address or wildcard. On the client, a specified bind
2209: * address will always override gateway_ports. On the server, a
2210: * gateway_ports of 1 (``yes'') will override the client's
2211: * specification and force a wildcard bind, whereas a value of 2
2212: * (``clientspecified'') will bind to whatever address the client
1.212 djm 2213: * asked for.
2214: *
2215: * Special-case listen_addrs are:
2216: *
2217: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2218: * "" (empty string), "*" -> wildcard v4/v6
2219: * "localhost" -> loopback v4/v6
2220: */
2221: addr = NULL;
2222: if (listen_addr == NULL) {
2223: /* No address specified: default to gateway_ports setting */
2224: if (gateway_ports)
2225: wildcard = 1;
2226: } else if (gateway_ports || is_client) {
2227: if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2228: strcmp(listen_addr, "0.0.0.0") == 0) ||
2229: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2230: (!is_client && gateway_ports == 1))
2231: wildcard = 1;
2232: else if (strcmp(listen_addr, "localhost") != 0)
2233: addr = listen_addr;
2234: }
2235:
2236: debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2237: type, wildcard, (addr == NULL) ? "NULL" : addr);
2238:
2239: /*
1.35 markus 2240: * getaddrinfo returns a loopback address if the hostname is
2241: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2242: */
1.35 markus 2243: memset(&hints, 0, sizeof(hints));
2244: hints.ai_family = IPv4or6;
1.212 djm 2245: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 2246: hints.ai_socktype = SOCK_STREAM;
1.73 markus 2247: snprintf(strport, sizeof strport, "%d", listen_port);
1.212 djm 2248: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2249: if (addr == NULL) {
2250: /* This really shouldn't happen */
2251: packet_disconnect("getaddrinfo: fatal error: %s",
2252: gai_strerror(r));
2253: } else {
1.218 markus 2254: error("channel_setup_fwd_listener: "
1.212 djm 2255: "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2256: }
1.218 markus 2257: return 0;
1.212 djm 2258: }
1.35 markus 2259:
2260: for (ai = aitop; ai; ai = ai->ai_next) {
2261: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2262: continue;
2263: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2264: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.160 markus 2265: error("channel_setup_fwd_listener: getnameinfo failed");
1.35 markus 2266: continue;
2267: }
2268: /* Create a port to listen for the host. */
1.189 markus 2269: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 2270: if (sock < 0) {
2271: /* this is no error since kernel may not support ipv6 */
2272: verbose("socket: %.100s", strerror(errno));
2273: continue;
2274: }
2275: /*
1.182 stevesk 2276: * Set socket options.
2277: * Allow local port reuse in TIME_WAIT.
1.35 markus 2278: */
1.182 stevesk 2279: if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
2280: sizeof(on)) == -1)
2281: error("setsockopt SO_REUSEADDR: %s", strerror(errno));
2282:
1.35 markus 2283: debug("Local forwarding listening on %s port %s.", ntop, strport);
2284:
2285: /* Bind the socket to the address. */
2286: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2287: /* address can be in use ipv6 address is already bound */
2288: verbose("bind: %.100s", strerror(errno));
2289: close(sock);
2290: continue;
2291: }
2292: /* Start listening for connections on the socket. */
1.199 markus 2293: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2294: error("listen: %.100s", strerror(errno));
2295: close(sock);
2296: continue;
2297: }
2298: /* Allocate a channel number for the socket. */
1.121 markus 2299: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2300: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 2301: 0, "port listener", 1);
1.113 markus 2302: strlcpy(c->path, host, sizeof(c->path));
2303: c->host_port = port_to_connect;
2304: c->listening_port = listen_port;
1.35 markus 2305: success = 1;
2306: }
2307: if (success == 0)
1.160 markus 2308: error("channel_setup_fwd_listener: cannot listen to port: %d",
1.87 markus 2309: listen_port);
1.35 markus 2310: freeaddrinfo(aitop);
1.87 markus 2311: return success;
1.25 markus 2312: }
1.1 deraadt 2313:
1.202 djm 2314: int
2315: channel_cancel_rport_listener(const char *host, u_short port)
2316: {
1.209 avsm 2317: u_int i;
2318: int found = 0;
1.202 djm 2319:
1.213 deraadt 2320: for (i = 0; i < channels_alloc; i++) {
1.202 djm 2321: Channel *c = channels[i];
2322:
2323: if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2324: strncmp(c->path, host, sizeof(c->path)) == 0 &&
1.208 deraadt 2325: c->listening_port == port) {
1.210 djm 2326: debug2("%s: close channel %d", __func__, i);
1.202 djm 2327: channel_free(c);
2328: found = 1;
2329: }
2330: }
2331:
2332: return (found);
2333: }
2334:
1.160 markus 2335: /* protocol local port fwd, used by ssh (and sshd in v1) */
2336: int
1.212 djm 2337: channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
1.160 markus 2338: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2339: {
2340: return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
1.212 djm 2341: listen_host, listen_port, host_to_connect, port_to_connect,
2342: gateway_ports);
1.160 markus 2343: }
2344:
2345: /* protocol v2 remote port fwd, used by sshd */
2346: int
2347: channel_setup_remote_fwd_listener(const char *listen_address,
2348: u_short listen_port, int gateway_ports)
2349: {
2350: return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2351: listen_address, listen_port, NULL, 0, gateway_ports);
2352: }
2353:
1.27 markus 2354: /*
2355: * Initiate forwarding of connections to port "port" on remote host through
2356: * the secure channel to host:port from local side.
2357: */
1.1 deraadt 2358:
1.49 markus 2359: void
1.212 djm 2360: channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
1.73 markus 2361: const char *host_to_connect, u_short port_to_connect)
1.25 markus 2362: {
1.153 markus 2363: int type, success = 0;
1.73 markus 2364:
1.25 markus 2365: /* Record locally that connection to this host/port is permitted. */
2366: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2367: fatal("channel_request_remote_forwarding: too many forwards");
2368:
2369: /* Send the forward request to the remote side. */
1.44 markus 2370: if (compat20) {
1.212 djm 2371: const char *address_to_bind;
2372: if (listen_host == NULL)
2373: address_to_bind = "localhost";
2374: else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2375: address_to_bind = "";
2376: else
2377: address_to_bind = listen_host;
2378:
1.44 markus 2379: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2380: packet_put_cstring("tcpip-forward");
1.173 markus 2381: packet_put_char(1); /* boolean: want reply */
1.44 markus 2382: packet_put_cstring(address_to_bind);
2383: packet_put_int(listen_port);
1.73 markus 2384: packet_send();
2385: packet_write_wait();
2386: /* Assume that server accepts the request */
2387: success = 1;
1.44 markus 2388: } else {
2389: packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50 markus 2390: packet_put_int(listen_port);
2391: packet_put_cstring(host_to_connect);
1.44 markus 2392: packet_put_int(port_to_connect);
2393: packet_send();
2394: packet_write_wait();
1.73 markus 2395:
2396: /* Wait for response from the remote side. */
1.153 markus 2397: type = packet_read();
1.73 markus 2398: switch (type) {
2399: case SSH_SMSG_SUCCESS:
2400: success = 1;
2401: break;
2402: case SSH_SMSG_FAILURE:
1.188 itojun 2403: logit("Warning: Server denied remote port forwarding.");
1.73 markus 2404: break;
2405: default:
2406: /* Unknown packet */
2407: packet_disconnect("Protocol error for port forward request:"
2408: "received packet type %d.", type);
2409: }
2410: }
2411: if (success) {
2412: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2413: permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2414: permitted_opens[num_permitted_opens].listen_port = listen_port;
2415: num_permitted_opens++;
1.44 markus 2416: }
1.1 deraadt 2417: }
2418:
1.27 markus 2419: /*
1.208 deraadt 2420: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 2421: * local side.
2422: */
2423: void
1.212 djm 2424: channel_request_rforward_cancel(const char *host, u_short port)
1.202 djm 2425: {
2426: int i;
2427:
2428: if (!compat20)
2429: return;
2430:
2431: for (i = 0; i < num_permitted_opens; i++) {
1.208 deraadt 2432: if (permitted_opens[i].host_to_connect != NULL &&
1.202 djm 2433: permitted_opens[i].listen_port == port)
2434: break;
2435: }
2436: if (i >= num_permitted_opens) {
2437: debug("%s: requested forward not found", __func__);
2438: return;
2439: }
2440: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2441: packet_put_cstring("cancel-tcpip-forward");
2442: packet_put_char(0);
1.212 djm 2443: packet_put_cstring(host == NULL ? "" : host);
1.202 djm 2444: packet_put_int(port);
2445: packet_send();
2446:
2447: permitted_opens[i].listen_port = 0;
2448: permitted_opens[i].port_to_connect = 0;
2449: free(permitted_opens[i].host_to_connect);
2450: permitted_opens[i].host_to_connect = NULL;
2451: }
2452:
2453: /*
1.27 markus 2454: * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2455: * listening for the port, and sends back a success reply (or disconnect
2456: * message if there was an error). This never returns if there was an error.
2457: */
1.1 deraadt 2458:
1.49 markus 2459: void
1.56 markus 2460: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1 deraadt 2461: {
1.31 markus 2462: u_short port, host_port;
1.25 markus 2463: char *hostname;
1.1 deraadt 2464:
1.25 markus 2465: /* Get arguments from the packet. */
2466: port = packet_get_int();
2467: hostname = packet_get_string(NULL);
2468: host_port = packet_get_int();
2469:
1.27 markus 2470: /*
2471: * Check that an unprivileged user is not trying to forward a
2472: * privileged port.
2473: */
1.25 markus 2474: if (port < IPPORT_RESERVED && !is_root)
1.192 markus 2475: packet_disconnect(
2476: "Requested forwarding of port %d but user is not root.",
2477: port);
2478: if (host_port == 0)
2479: packet_disconnect("Dynamic forwarding denied.");
2480:
1.73 markus 2481: /* Initiate forwarding */
1.212 djm 2482: channel_setup_local_fwd_listener(NULL, port, hostname,
2483: host_port, gateway_ports);
1.25 markus 2484:
2485: /* Free the argument string. */
2486: xfree(hostname);
1.1 deraadt 2487: }
2488:
1.99 markus 2489: /*
2490: * Permits opening to any host/port if permitted_opens[] is empty. This is
2491: * usually called by the server, because the user could connect to any port
2492: * anyway, and the server has no way to know but to trust the client anyway.
2493: */
2494: void
1.142 itojun 2495: channel_permit_all_opens(void)
1.99 markus 2496: {
2497: if (num_permitted_opens == 0)
2498: all_opens_permitted = 1;
2499: }
2500:
1.101 markus 2501: void
1.99 markus 2502: channel_add_permitted_opens(char *host, int port)
2503: {
2504: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2505: fatal("channel_request_remote_forwarding: too many forwards");
2506: debug("allow port forwarding to host %s port %d", host, port);
2507:
2508: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2509: permitted_opens[num_permitted_opens].port_to_connect = port;
2510: num_permitted_opens++;
2511:
2512: all_opens_permitted = 0;
2513: }
2514:
1.101 markus 2515: void
1.99 markus 2516: channel_clear_permitted_opens(void)
2517: {
2518: int i;
2519:
2520: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2521: if (permitted_opens[i].host_to_connect != NULL)
2522: xfree(permitted_opens[i].host_to_connect);
1.99 markus 2523: num_permitted_opens = 0;
2524:
2525: }
2526:
2527:
2528: /* return socket to remote host, port */
1.127 itojun 2529: static int
1.99 markus 2530: connect_to(const char *host, u_short port)
1.41 markus 2531: {
2532: struct addrinfo hints, *ai, *aitop;
2533: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2534: int gaierr;
2535: int sock = -1;
2536:
2537: memset(&hints, 0, sizeof(hints));
2538: hints.ai_family = IPv4or6;
2539: hints.ai_socktype = SOCK_STREAM;
1.99 markus 2540: snprintf(strport, sizeof strport, "%d", port);
1.41 markus 2541: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1.99 markus 2542: error("connect_to %.100s: unknown host (%s)", host,
2543: gai_strerror(gaierr));
1.41 markus 2544: return -1;
2545: }
2546: for (ai = aitop; ai; ai = ai->ai_next) {
2547: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2548: continue;
2549: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2550: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.99 markus 2551: error("connect_to: getnameinfo failed");
1.41 markus 2552: continue;
2553: }
1.189 markus 2554: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.41 markus 2555: if (sock < 0) {
1.186 djm 2556: if (ai->ai_next == NULL)
2557: error("socket: %.100s", strerror(errno));
2558: else
2559: verbose("socket: %.100s", strerror(errno));
1.41 markus 2560: continue;
2561: }
1.205 djm 2562: if (set_nonblock(sock) == -1)
2563: fatal("%s: set_nonblock(%d)", __func__, sock);
1.75 markus 2564: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2565: errno != EINPROGRESS) {
1.99 markus 2566: error("connect_to %.100s port %s: %.100s", ntop, strport,
1.41 markus 2567: strerror(errno));
2568: close(sock);
1.89 stevesk 2569: continue; /* fail -- try next */
1.41 markus 2570: }
2571: break; /* success */
2572:
2573: }
2574: freeaddrinfo(aitop);
2575: if (!ai) {
1.99 markus 2576: error("connect_to %.100s port %d: failed.", host, port);
1.41 markus 2577: return -1;
2578: }
2579: /* success */
1.169 stevesk 2580: set_nodelay(sock);
1.41 markus 2581: return sock;
2582: }
1.99 markus 2583:
1.73 markus 2584: int
1.130 stevesk 2585: channel_connect_by_listen_address(u_short listen_port)
1.73 markus 2586: {
2587: int i;
1.99 markus 2588:
1.73 markus 2589: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2590: if (permitted_opens[i].host_to_connect != NULL &&
2591: permitted_opens[i].listen_port == listen_port)
1.99 markus 2592: return connect_to(
1.73 markus 2593: permitted_opens[i].host_to_connect,
2594: permitted_opens[i].port_to_connect);
1.74 markus 2595: error("WARNING: Server requests forwarding for unknown listen_port %d",
2596: listen_port);
1.73 markus 2597: return -1;
2598: }
2599:
1.99 markus 2600: /* Check if connecting to that port is permitted and connect. */
2601: int
2602: channel_connect_to(const char *host, u_short port)
2603: {
2604: int i, permit;
2605:
2606: permit = all_opens_permitted;
2607: if (!permit) {
2608: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2609: if (permitted_opens[i].host_to_connect != NULL &&
2610: permitted_opens[i].port_to_connect == port &&
1.99 markus 2611: strcmp(permitted_opens[i].host_to_connect, host) == 0)
2612: permit = 1;
2613:
2614: }
2615: if (!permit) {
1.188 itojun 2616: logit("Received request to connect to host %.100s port %d, "
1.99 markus 2617: "but the request was denied.", host, port);
2618: return -1;
2619: }
2620: return connect_to(host, port);
1.204 djm 2621: }
2622:
2623: void
2624: channel_send_window_changes(void)
2625: {
1.209 avsm 2626: u_int i;
1.204 djm 2627: struct winsize ws;
2628:
2629: for (i = 0; i < channels_alloc; i++) {
1.213 deraadt 2630: if (channels[i] == NULL || !channels[i]->client_tty ||
1.204 djm 2631: channels[i]->type != SSH_CHANNEL_OPEN)
2632: continue;
2633: if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2634: continue;
2635: channel_request_start(i, "window-change", 0);
2636: packet_put_int(ws.ws_col);
2637: packet_put_int(ws.ws_row);
2638: packet_put_int(ws.ws_xpixel);
2639: packet_put_int(ws.ws_ypixel);
2640: packet_send();
2641: }
1.99 markus 2642: }
2643:
1.121 markus 2644: /* -- X11 forwarding */
1.1 deraadt 2645:
1.27 markus 2646: /*
2647: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 2648: * Returns 0 and a suitable display number for the DISPLAY variable
2649: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 2650: */
1.141 stevesk 2651: int
1.163 stevesk 2652: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.222 djm 2653: int single_connection, u_int *display_numberp, int **chanids)
1.1 deraadt 2654: {
1.149 markus 2655: Channel *nc = NULL;
1.31 markus 2656: int display_number, sock;
2657: u_short port;
1.35 markus 2658: struct addrinfo hints, *ai, *aitop;
2659: char strport[NI_MAXSERV];
2660: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 2661:
1.224 markus 2662: if (chanids == NULL)
2663: return -1;
2664:
1.33 markus 2665: for (display_number = x11_display_offset;
1.148 deraadt 2666: display_number < MAX_DISPLAYS;
2667: display_number++) {
1.25 markus 2668: port = 6000 + display_number;
1.35 markus 2669: memset(&hints, 0, sizeof(hints));
2670: hints.ai_family = IPv4or6;
1.163 stevesk 2671: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 2672: hints.ai_socktype = SOCK_STREAM;
2673: snprintf(strport, sizeof strport, "%d", port);
2674: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2675: error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.141 stevesk 2676: return -1;
1.25 markus 2677: }
1.35 markus 2678: for (ai = aitop; ai; ai = ai->ai_next) {
2679: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2680: continue;
1.189 markus 2681: sock = socket(ai->ai_family, ai->ai_socktype,
2682: ai->ai_protocol);
1.35 markus 2683: if (sock < 0) {
2684: error("socket: %.100s", strerror(errno));
1.203 markus 2685: freeaddrinfo(aitop);
1.141 stevesk 2686: return -1;
1.35 markus 2687: }
2688: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 2689: debug2("bind port %d: %.100s", port, strerror(errno));
1.35 markus 2690: close(sock);
1.183 itojun 2691:
2692: if (ai->ai_next)
2693: continue;
2694:
1.35 markus 2695: for (n = 0; n < num_socks; n++) {
2696: close(socks[n]);
2697: }
2698: num_socks = 0;
2699: break;
2700: }
2701: socks[num_socks++] = sock;
2702: if (num_socks == NUM_SOCKS)
2703: break;
1.25 markus 2704: }
1.83 stevesk 2705: freeaddrinfo(aitop);
1.35 markus 2706: if (num_socks > 0)
2707: break;
1.25 markus 2708: }
2709: if (display_number >= MAX_DISPLAYS) {
2710: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 2711: return -1;
1.25 markus 2712: }
2713: /* Start listening for connections on the socket. */
1.35 markus 2714: for (n = 0; n < num_socks; n++) {
2715: sock = socks[n];
1.199 markus 2716: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2717: error("listen: %.100s", strerror(errno));
2718: close(sock);
1.141 stevesk 2719: return -1;
1.35 markus 2720: }
1.25 markus 2721: }
1.35 markus 2722:
2723: /* Allocate a channel for each socket. */
1.224 markus 2724: *chanids = xmalloc(sizeof(**chanids) * (num_socks + 1));
1.35 markus 2725: for (n = 0; n < num_socks; n++) {
2726: sock = socks[n];
1.149 markus 2727: nc = channel_new("x11 listener",
1.51 markus 2728: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2729: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 2730: 0, "X11 inet listener", 1);
1.167 markus 2731: nc->single_connection = single_connection;
1.224 markus 2732: (*chanids)[n] = nc->self;
1.35 markus 2733: }
1.224 markus 2734: (*chanids)[n] = -1;
1.1 deraadt 2735:
1.141 stevesk 2736: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 2737: *display_numberp = display_number;
2738: return (0);
1.1 deraadt 2739: }
2740:
1.127 itojun 2741: static int
1.77 markus 2742: connect_local_xsocket(u_int dnr)
1.1 deraadt 2743: {
1.25 markus 2744: int sock;
2745: struct sockaddr_un addr;
2746:
1.147 stevesk 2747: sock = socket(AF_UNIX, SOCK_STREAM, 0);
2748: if (sock < 0)
2749: error("socket: %.100s", strerror(errno));
2750: memset(&addr, 0, sizeof(addr));
2751: addr.sun_family = AF_UNIX;
2752: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
2753: if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2754: return sock;
2755: close(sock);
1.25 markus 2756: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2757: return -1;
1.1 deraadt 2758: }
2759:
1.51 markus 2760: int
2761: x11_connect_display(void)
1.1 deraadt 2762: {
1.162 stevesk 2763: int display_number, sock = 0;
1.25 markus 2764: const char *display;
1.51 markus 2765: char buf[1024], *cp;
1.35 markus 2766: struct addrinfo hints, *ai, *aitop;
2767: char strport[NI_MAXSERV];
2768: int gaierr;
1.25 markus 2769:
2770: /* Try to open a socket for the local X server. */
2771: display = getenv("DISPLAY");
2772: if (!display) {
2773: error("DISPLAY not set.");
1.51 markus 2774: return -1;
1.25 markus 2775: }
1.27 markus 2776: /*
2777: * Now we decode the value of the DISPLAY variable and make a
2778: * connection to the real X server.
2779: */
2780:
2781: /*
2782: * Check if it is a unix domain socket. Unix domain displays are in
2783: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2784: */
1.25 markus 2785: if (strncmp(display, "unix:", 5) == 0 ||
2786: display[0] == ':') {
2787: /* Connect to the unix domain socket. */
2788: if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2789: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2790: display);
1.51 markus 2791: return -1;
1.25 markus 2792: }
2793: /* Create a socket. */
2794: sock = connect_local_xsocket(display_number);
2795: if (sock < 0)
1.51 markus 2796: return -1;
1.25 markus 2797:
2798: /* OK, we now have a connection to the display. */
1.51 markus 2799: return sock;
1.25 markus 2800: }
1.27 markus 2801: /*
2802: * Connect to an inet socket. The DISPLAY value is supposedly
2803: * hostname:d[.s], where hostname may also be numeric IP address.
2804: */
1.145 stevesk 2805: strlcpy(buf, display, sizeof(buf));
1.25 markus 2806: cp = strchr(buf, ':');
2807: if (!cp) {
2808: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 2809: return -1;
1.25 markus 2810: }
2811: *cp = 0;
1.27 markus 2812: /* buf now contains the host name. But first we parse the display number. */
1.25 markus 2813: if (sscanf(cp + 1, "%d", &display_number) != 1) {
2814: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 2815: display);
1.51 markus 2816: return -1;
1.25 markus 2817: }
1.35 markus 2818:
2819: /* Look up the host address */
2820: memset(&hints, 0, sizeof(hints));
2821: hints.ai_family = IPv4or6;
2822: hints.ai_socktype = SOCK_STREAM;
2823: snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2824: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2825: error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.51 markus 2826: return -1;
1.25 markus 2827: }
1.35 markus 2828: for (ai = aitop; ai; ai = ai->ai_next) {
2829: /* Create a socket. */
1.189 markus 2830: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 2831: if (sock < 0) {
1.194 markus 2832: debug2("socket: %.100s", strerror(errno));
1.41 markus 2833: continue;
2834: }
2835: /* Connect it to the display. */
2836: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 2837: debug2("connect %.100s port %d: %.100s", buf,
1.41 markus 2838: 6000 + display_number, strerror(errno));
2839: close(sock);
2840: continue;
2841: }
2842: /* Success */
2843: break;
1.35 markus 2844: }
2845: freeaddrinfo(aitop);
2846: if (!ai) {
1.49 markus 2847: error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1.35 markus 2848: strerror(errno));
1.51 markus 2849: return -1;
1.25 markus 2850: }
1.162 stevesk 2851: set_nodelay(sock);
1.51 markus 2852: return sock;
2853: }
2854:
2855: /*
2856: * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2857: * the remote channel number. We should do whatever we want, and respond
2858: * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2859: */
2860:
2861: void
1.154 markus 2862: x11_input_open(int type, u_int32_t seq, void *ctxt)
1.51 markus 2863: {
1.113 markus 2864: Channel *c = NULL;
2865: int remote_id, sock = 0;
1.51 markus 2866: char *remote_host;
1.25 markus 2867:
1.113 markus 2868: debug("Received X11 open request.");
1.51 markus 2869:
1.113 markus 2870: remote_id = packet_get_int();
1.121 markus 2871:
2872: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113 markus 2873: remote_host = packet_get_string(NULL);
1.51 markus 2874: } else {
2875: remote_host = xstrdup("unknown (remote did not supply name)");
2876: }
1.152 markus 2877: packet_check_eom();
1.25 markus 2878:
1.51 markus 2879: /* Obtain a connection to the real X display. */
2880: sock = x11_connect_display();
1.113 markus 2881: if (sock != -1) {
2882: /* Allocate a channel for this connection. */
2883: c = channel_new("connected x11 socket",
2884: SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2885: remote_host, 1);
1.167 markus 2886: c->remote_id = remote_id;
2887: c->force_drain = 1;
1.113 markus 2888: }
1.190 markus 2889: xfree(remote_host);
1.113 markus 2890: if (c == NULL) {
1.51 markus 2891: /* Send refusal to the remote host. */
2892: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 2893: packet_put_int(remote_id);
1.51 markus 2894: } else {
2895: /* Send a confirmation to the remote host. */
2896: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113 markus 2897: packet_put_int(remote_id);
2898: packet_put_int(c->self);
1.51 markus 2899: }
1.113 markus 2900: packet_send();
1.72 markus 2901: }
2902:
2903: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2904: void
1.154 markus 2905: deny_input_open(int type, u_int32_t seq, void *ctxt)
1.72 markus 2906: {
2907: int rchan = packet_get_int();
1.180 deraadt 2908:
1.143 deraadt 2909: switch (type) {
1.72 markus 2910: case SSH_SMSG_AGENT_OPEN:
2911: error("Warning: ssh server tried agent forwarding.");
2912: break;
2913: case SSH_SMSG_X11_OPEN:
2914: error("Warning: ssh server tried X11 forwarding.");
2915: break;
2916: default:
1.154 markus 2917: error("deny_input_open: type %d", type);
1.72 markus 2918: break;
2919: }
2920: error("Warning: this is probably a break in attempt by a malicious server.");
2921: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2922: packet_put_int(rchan);
2923: packet_send();
1.1 deraadt 2924: }
2925:
1.27 markus 2926: /*
2927: * Requests forwarding of X11 connections, generates fake authentication
2928: * data, and enables authentication spoofing.
1.121 markus 2929: * This should be called in the client only.
1.27 markus 2930: */
1.49 markus 2931: void
1.215 djm 2932: x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
1.51 markus 2933: const char *proto, const char *data)
1.1 deraadt 2934: {
1.77 markus 2935: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 2936: u_int i, value;
1.25 markus 2937: char *new_data;
2938: int screen_number;
2939: const char *cp;
1.207 avsm 2940: u_int32_t rnd = 0;
1.25 markus 2941:
1.220 markus 2942: if (x11_saved_display == NULL)
2943: x11_saved_display = xstrdup(disp);
2944: else if (strcmp(disp, x11_saved_display) != 0) {
1.219 djm 2945: error("x11_request_forwarding_with_spoofing: different "
2946: "$DISPLAY already forwarded");
2947: return;
2948: }
2949:
1.215 djm 2950: cp = disp;
2951: if (disp)
2952: cp = strchr(disp, ':');
1.25 markus 2953: if (cp)
2954: cp = strchr(cp, '.');
2955: if (cp)
2956: screen_number = atoi(cp + 1);
2957: else
2958: screen_number = 0;
2959:
1.219 djm 2960: if (x11_saved_proto == NULL) {
2961: /* Save protocol name. */
2962: x11_saved_proto = xstrdup(proto);
2963: /*
1.221 djm 2964: * Extract real authentication data and generate fake data
1.219 djm 2965: * of the same length.
2966: */
2967: x11_saved_data = xmalloc(data_len);
2968: x11_fake_data = xmalloc(data_len);
2969: for (i = 0; i < data_len; i++) {
2970: if (sscanf(data + 2 * i, "%2x", &value) != 1)
2971: fatal("x11_request_forwarding: bad "
2972: "authentication data: %.100s", data);
2973: if (i % 4 == 0)
2974: rnd = arc4random();
2975: x11_saved_data[i] = value;
2976: x11_fake_data[i] = rnd & 0xff;
2977: rnd >>= 8;
2978: }
2979: x11_saved_data_len = data_len;
2980: x11_fake_data_len = data_len;
1.25 markus 2981: }
2982:
2983: /* Convert the fake data into hex. */
1.219 djm 2984: new_data = tohex(x11_fake_data, data_len);
1.25 markus 2985:
2986: /* Send the request packet. */
1.51 markus 2987: if (compat20) {
2988: channel_request_start(client_session_id, "x11-req", 0);
2989: packet_put_char(0); /* XXX bool single connection */
2990: } else {
2991: packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2992: }
2993: packet_put_cstring(proto);
2994: packet_put_cstring(new_data);
1.25 markus 2995: packet_put_int(screen_number);
2996: packet_send();
2997: packet_write_wait();
2998: xfree(new_data);
1.1 deraadt 2999: }
3000:
1.121 markus 3001:
3002: /* -- agent forwarding */
3003:
1.1 deraadt 3004: /* Sends a message to the server to request authentication fd forwarding. */
3005:
1.49 markus 3006: void
1.142 itojun 3007: auth_request_forwarding(void)
1.1 deraadt 3008: {
1.25 markus 3009: packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3010: packet_send();
3011: packet_write_wait();
1.1 deraadt 3012: }