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