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