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