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