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