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