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