Annotation of src/usr.bin/ssh/channels.c, Revision 1.425
1.425 ! djm 1: /* $OpenBSD: channels.c,v 1.424 2023/01/06 02:41:49 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.336 millert 43: #include <sys/stat.h>
1.234 stevesk 44: #include <sys/ioctl.h>
1.235 stevesk 45: #include <sys/un.h>
1.251 stevesk 46: #include <sys/socket.h>
1.261 stevesk 47: #include <sys/time.h>
1.275 djm 48: #include <sys/queue.h>
1.251 stevesk 49:
50: #include <netinet/in.h>
51: #include <arpa/inet.h>
1.233 stevesk 52:
1.254 stevesk 53: #include <errno.h>
1.298 dtucker 54: #include <fcntl.h>
1.367 djm 55: #include <limits.h>
1.255 stevesk 56: #include <netdb.h>
1.411 djm 57: #include <poll.h>
1.367 djm 58: #include <stdarg.h>
1.341 millert 59: #include <stdint.h>
1.263 stevesk 60: #include <stdio.h>
1.262 stevesk 61: #include <stdlib.h>
1.260 stevesk 62: #include <string.h>
1.233 stevesk 63: #include <termios.h>
1.256 stevesk 64: #include <unistd.h>
1.1 deraadt 65:
1.265 deraadt 66: #include "xmalloc.h"
1.1 deraadt 67: #include "ssh.h"
1.82 markus 68: #include "ssh2.h"
1.354 markus 69: #include "ssherr.h"
1.367 djm 70: #include "sshbuf.h"
1.1 deraadt 71: #include "packet.h"
1.82 markus 72: #include "log.h"
73: #include "misc.h"
1.14 markus 74: #include "channels.h"
75: #include "compat.h"
1.82 markus 76: #include "canohost.h"
1.383 markus 77: #include "sshkey.h"
1.64 markus 78: #include "authfd.h"
1.147 stevesk 79: #include "pathnames.h"
1.381 djm 80: #include "match.h"
1.388 djm 81:
1.411 djm 82: /* XXX remove once we're satisfied there's no lurking bugs */
83: /* #define DEBUG_CHANNEL_POLL 1 */
84:
1.367 djm 85: /* -- agent forwarding */
86: #define NUM_SOCKS 10
1.12 markus 87:
1.367 djm 88: /* -- tcp forwarding */
89: /* special-case port number meaning allow any port */
90: #define FWD_PERMIT_ANY_PORT 0
1.1 deraadt 91:
1.367 djm 92: /* special-case wildcard meaning allow any host */
93: #define FWD_PERMIT_ANY_HOST "*"
1.1 deraadt 94:
1.367 djm 95: /* -- X11 forwarding */
96: /* Maximum number of fake X11 displays to try. */
97: #define MAX_DISPLAYS 1000
1.1 deraadt 98:
1.411 djm 99: /* Per-channel callback for pre/post IO actions */
1.410 djm 100: typedef void chan_fn(struct ssh *, Channel *c);
1.381 djm 101:
1.27 markus 102: /*
103: * Data structure for storing which hosts are permitted for forward requests.
104: * The local sides of any remote forwards are stored in this array to prevent
105: * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
106: * network (which might be behind a firewall).
107: */
1.336 millert 108: /* XXX: streamlocal wants a path instead of host:port */
109: /* Overload host_to_connect; we could just make this match Forward */
110: /* XXX - can we use listen_host instead of listen_path? */
1.381 djm 111: struct permission {
1.41 markus 112: char *host_to_connect; /* Connect to 'host'. */
1.336 millert 113: int port_to_connect; /* Connect to 'port'. */
1.333 markus 114: char *listen_host; /* Remote side should listen address. */
1.336 millert 115: char *listen_path; /* Remote side should listen path. */
116: int listen_port; /* Remote side should listen port. */
1.354 markus 117: Channel *downstream; /* Downstream mux*/
1.381 djm 118: };
119:
120: /*
121: * Stores the forwarding permission state for a single direction (local or
122: * remote).
123: */
124: struct permission_set {
125: /*
126: * List of all local permitted host/port pairs to allow for the
127: * user.
128: */
129: u_int num_permitted_user;
130: struct permission *permitted_user;
1.1 deraadt 131:
1.381 djm 132: /*
133: * List of all permitted host/port pairs to allow for the admin.
134: */
135: u_int num_permitted_admin;
136: struct permission *permitted_admin;
137:
138: /*
139: * If this is true, all opens/listens are permitted. This is the
140: * case on the server on which we have to trust the client anyway,
141: * and the user could do anything after logging in.
142: */
143: int all_permitted;
144: };
1.121 markus 145:
1.367 djm 146: /* Master structure for channels state */
147: struct ssh_channels {
148: /*
149: * Pointer to an array containing all allocated channels. The array
150: * is dynamically extended as needed.
151: */
152: Channel **channels;
1.257 dtucker 153:
1.367 djm 154: /*
155: * Size of the channel array. All slots of the array must always be
156: * initialized (at least the type field); unused slots set to NULL
157: */
158: u_int channels_alloc;
1.257 dtucker 159:
1.367 djm 160: /*
1.411 djm 161: * 'channel_pre*' are called just before IO to add any bits
162: * relevant to channels in the c->io_want bitmasks.
1.367 djm 163: *
164: * 'channel_post*': perform any appropriate operations for
1.411 djm 165: * channels which have c->io_ready events pending.
1.367 djm 166: */
167: chan_fn **channel_pre;
168: chan_fn **channel_post;
1.314 dtucker 169:
1.367 djm 170: /* -- tcp forwarding */
1.381 djm 171: struct permission_set local_perms;
172: struct permission_set remote_perms;
1.219 djm 173:
1.367 djm 174: /* -- X11 forwarding */
1.121 markus 175:
1.367 djm 176: /* Saved X11 local (client) display. */
177: char *x11_saved_display;
1.121 markus 178:
1.367 djm 179: /* Saved X11 authentication protocol name. */
180: char *x11_saved_proto;
1.347 djm 181:
1.367 djm 182: /* Saved X11 authentication data. This is the real data. */
183: char *x11_saved_data;
184: u_int x11_saved_data_len;
1.121 markus 185:
1.367 djm 186: /* Deadline after which all X11 connections are refused */
187: u_int x11_refuse_time;
1.121 markus 188:
1.367 djm 189: /*
190: * Fake X11 authentication data. This is what the server will be
191: * sending us; we should replace any occurrences of this by the
192: * real data.
193: */
194: u_char *x11_fake_data;
195: u_int x11_fake_data_len;
1.121 markus 196:
1.367 djm 197: /* AF_UNSPEC or AF_INET or AF_INET6 */
198: int IPv4or6;
199: };
1.1 deraadt 200:
1.121 markus 201: /* helper */
1.367 djm 202: static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
1.354 markus 203: static const char *channel_rfwd_bind_host(const char *listen_host);
1.103 markus 204:
1.276 djm 205: /* non-blocking connect helpers */
206: static int connect_next(struct channel_connect *);
207: static void channel_connect_ctx_free(struct channel_connect *);
1.372 markus 208: static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *);
209: static int rdynamic_connect_finish(struct ssh *, Channel *);
1.276 djm 210:
1.367 djm 211: /* Setup helper */
212: static void channel_handler_init(struct ssh_channels *sc);
213:
1.121 markus 214: /* -- channel core */
1.41 markus 215:
1.367 djm 216: void
217: channel_init_channels(struct ssh *ssh)
218: {
219: struct ssh_channels *sc;
220:
1.387 djm 221: if ((sc = calloc(1, sizeof(*sc))) == NULL)
1.403 djm 222: fatal_f("allocation failed");
1.367 djm 223: sc->channels_alloc = 10;
224: sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
225: sc->IPv4or6 = AF_UNSPEC;
226: channel_handler_init(sc);
227:
228: ssh->chanctxt = sc;
229: }
230:
1.41 markus 231: Channel *
1.367 djm 232: channel_by_id(struct ssh *ssh, int id)
1.41 markus 233: {
234: Channel *c;
1.113 markus 235:
1.367 djm 236: if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
1.403 djm 237: logit_f("%d: bad id", id);
1.41 markus 238: return NULL;
239: }
1.367 djm 240: c = ssh->chanctxt->channels[id];
1.113 markus 241: if (c == NULL) {
1.403 djm 242: logit_f("%d: bad id: channel free", id);
1.41 markus 243: return NULL;
244: }
245: return c;
1.55 markus 246: }
247:
1.354 markus 248: Channel *
1.368 djm 249: channel_by_remote_id(struct ssh *ssh, u_int remote_id)
1.354 markus 250: {
251: Channel *c;
252: u_int i;
253:
1.367 djm 254: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
255: c = ssh->chanctxt->channels[i];
1.368 djm 256: if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
1.354 markus 257: return c;
258: }
259: return NULL;
260: }
261:
1.27 markus 262: /*
1.229 markus 263: * Returns the channel if it is allowed to receive protocol messages.
264: * Private channels, like listening sockets, may not receive messages.
265: */
266: Channel *
1.367 djm 267: channel_lookup(struct ssh *ssh, int id)
1.229 markus 268: {
269: Channel *c;
270:
1.367 djm 271: if ((c = channel_by_id(ssh, id)) == NULL)
272: return NULL;
1.229 markus 273:
1.237 deraadt 274: switch (c->type) {
1.229 markus 275: case SSH_CHANNEL_X11_OPEN:
276: case SSH_CHANNEL_LARVAL:
277: case SSH_CHANNEL_CONNECTING:
278: case SSH_CHANNEL_DYNAMIC:
1.372 markus 279: case SSH_CHANNEL_RDYNAMIC_OPEN:
280: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.229 markus 281: case SSH_CHANNEL_OPENING:
282: case SSH_CHANNEL_OPEN:
1.323 dtucker 283: case SSH_CHANNEL_ABANDONED:
1.354 markus 284: case SSH_CHANNEL_MUX_PROXY:
1.367 djm 285: return c;
1.229 markus 286: }
287: logit("Non-public channel %d, type %d.", id, c->type);
1.367 djm 288: return NULL;
1.229 markus 289: }
290:
291: /*
1.425 ! djm 292: * Sets "extended type" of a channel; used by session layer to add additional
! 293: * information about channel types (e.g. shell, login, subsystem) that can then
! 294: * be used to select timeouts.
! 295: */
! 296: void
! 297: channel_set_xtype(struct ssh *ssh, int id, const char *xctype)
! 298: {
! 299: Channel *c;
! 300:
! 301: if ((c = channel_by_id(ssh, id)) == NULL)
! 302: fatal_f("missing channel %d", id);
! 303: if (c->xctype != NULL)
! 304: free(c->xctype);
! 305: c->xctype = xstrdup(xctype);
! 306: debug2_f("labeled channel %d as %s", id, xctype);
! 307: }
! 308:
! 309: /*
1.55 markus 310: * Register filedescriptors for a channel, used when allocating a channel or
1.52 markus 311: * when the channel consumer/producer is ready, e.g. shell exec'd
1.27 markus 312: */
1.127 itojun 313: static void
1.367 djm 314: channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
1.282 dtucker 315: int extusage, int nonblock, int is_tty)
1.1 deraadt 316: {
1.418 markus 317: int val;
318:
1.298 dtucker 319: if (rfd != -1)
320: fcntl(rfd, F_SETFD, FD_CLOEXEC);
321: if (wfd != -1 && wfd != rfd)
322: fcntl(wfd, F_SETFD, FD_CLOEXEC);
323: if (efd != -1 && efd != rfd && efd != wfd)
324: fcntl(efd, F_SETFD, FD_CLOEXEC);
1.55 markus 325:
1.52 markus 326: c->rfd = rfd;
327: c->wfd = wfd;
328: c->sock = (rfd == wfd) ? rfd : -1;
329: c->efd = efd;
330: c->extended_usage = extusage;
1.71 markus 331:
1.282 dtucker 332: if ((c->isatty = is_tty) != 0)
1.194 markus 333: debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1.91 markus 334:
1.71 markus 335: /* enable nonblocking mode */
1.407 djm 336: c->restore_block = 0;
337: if (nonblock == CHANNEL_NONBLOCK_STDIO) {
338: /*
339: * Special handling for stdio file descriptors: do not set
340: * non-blocking mode if they are TTYs. Otherwise prepare to
341: * restore their blocking state on exit to avoid interfering
342: * with other programs that follow.
343: */
1.418 markus 344: if (rfd != -1 && !isatty(rfd) &&
345: (val = fcntl(rfd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.407 djm 346: c->restore_block |= CHANNEL_RESTORE_RFD;
347: set_nonblock(rfd);
348: }
1.418 markus 349: if (wfd != -1 && !isatty(wfd) &&
350: (val = fcntl(wfd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.407 djm 351: c->restore_block |= CHANNEL_RESTORE_WFD;
352: set_nonblock(wfd);
353: }
1.418 markus 354: if (efd != -1 && !isatty(efd) &&
355: (val = fcntl(efd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.407 djm 356: c->restore_block |= CHANNEL_RESTORE_EFD;
357: set_nonblock(efd);
358: }
359: } else if (nonblock) {
1.71 markus 360: if (rfd != -1)
361: set_nonblock(rfd);
362: if (wfd != -1)
363: set_nonblock(wfd);
364: if (efd != -1)
365: set_nonblock(efd);
366: }
1.52 markus 367: }
368:
369: /*
1.419 djm 370: * Allocate a new channel object and set its type and socket.
1.52 markus 371: */
1.113 markus 372: Channel *
1.367 djm 373: channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
1.419 djm 374: u_int window, u_int maxpack, int extusage, const char *remote_name,
375: int nonblock)
1.52 markus 376: {
1.367 djm 377: struct ssh_channels *sc = ssh->chanctxt;
378: u_int i, found;
1.52 markus 379: Channel *c;
1.402 djm 380: int r;
1.25 markus 381:
382: /* Try to find a free slot where to put the new channel. */
1.367 djm 383: for (i = 0; i < sc->channels_alloc; i++) {
384: if (sc->channels[i] == NULL) {
1.25 markus 385: /* Found a free slot. */
1.367 djm 386: found = i;
1.25 markus 387: break;
388: }
1.367 djm 389: }
390: if (i >= sc->channels_alloc) {
391: /*
392: * There are no free slots. Take last+1 slot and expand
393: * the array.
394: */
395: found = sc->channels_alloc;
396: if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
1.403 djm 397: fatal_f("internal error: channels_alloc %d too big",
398: sc->channels_alloc);
1.367 djm 399: sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
400: sc->channels_alloc + 10, sizeof(*sc->channels));
401: sc->channels_alloc += 10;
402: debug2("channel: expanding %d", sc->channels_alloc);
1.25 markus 403: }
1.113 markus 404: /* Initialize and return new channel. */
1.367 djm 405: c = sc->channels[found] = xcalloc(1, sizeof(Channel));
406: if ((c->input = sshbuf_new()) == NULL ||
407: (c->output = sshbuf_new()) == NULL ||
408: (c->extended = sshbuf_new()) == NULL)
1.403 djm 409: fatal_f("sshbuf_new failed");
1.402 djm 410: if ((r = sshbuf_set_max_size(c->input, CHAN_INPUT_MAX)) != 0)
1.403 djm 411: fatal_fr(r, "sshbuf_set_max_size");
1.159 markus 412: c->ostate = CHAN_OUTPUT_OPEN;
413: c->istate = CHAN_INPUT_OPEN;
1.367 djm 414: channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
1.25 markus 415: c->self = found;
416: c->type = type;
1.41 markus 417: c->ctype = ctype;
1.51 markus 418: c->local_window = window;
419: c->local_window_max = window;
420: c->local_maxpacket = maxpack;
1.190 markus 421: c->remote_name = xstrdup(remote_name);
1.302 djm 422: c->ctl_chan = -1;
1.299 markus 423: c->delayed = 1; /* prevent call to channel_post handler */
1.275 djm 424: TAILQ_INIT(&c->status_confirms);
1.25 markus 425: debug("channel %d: new [%s]", found, remote_name);
1.113 markus 426: return c;
1.1 deraadt 427: }
1.52 markus 428:
1.132 markus 429: int
1.407 djm 430: channel_close_fd(struct ssh *ssh, Channel *c, int *fdp)
1.132 markus 431: {
1.407 djm 432: int ret, fd = *fdp;
1.132 markus 433:
1.407 djm 434: if (fd == -1)
435: return 0;
436:
437: if ((*fdp == c->rfd && (c->restore_block & CHANNEL_RESTORE_RFD) != 0) ||
438: (*fdp == c->wfd && (c->restore_block & CHANNEL_RESTORE_WFD) != 0) ||
439: (*fdp == c->efd && (c->restore_block & CHANNEL_RESTORE_EFD) != 0))
440: (void)fcntl(*fdp, F_SETFL, 0); /* restore blocking */
441:
1.411 djm 442: if (*fdp == c->rfd) {
443: c->io_want &= ~SSH_CHAN_IO_RFD;
444: c->io_ready &= ~SSH_CHAN_IO_RFD;
445: c->rfd = -1;
1.415 djm 446: c->pfds[0] = -1;
1.411 djm 447: }
448: if (*fdp == c->wfd) {
449: c->io_want &= ~SSH_CHAN_IO_WFD;
450: c->io_ready &= ~SSH_CHAN_IO_WFD;
451: c->wfd = -1;
1.415 djm 452: c->pfds[1] = -1;
1.411 djm 453: }
454: if (*fdp == c->efd) {
455: c->io_want &= ~SSH_CHAN_IO_EFD;
456: c->io_ready &= ~SSH_CHAN_IO_EFD;
457: c->efd = -1;
1.415 djm 458: c->pfds[2] = -1;
1.411 djm 459: }
460: if (*fdp == c->sock) {
461: c->io_want &= ~SSH_CHAN_IO_SOCK;
462: c->io_ready &= ~SSH_CHAN_IO_SOCK;
463: c->sock = -1;
1.415 djm 464: c->pfds[3] = -1;
1.411 djm 465: }
466:
1.407 djm 467: ret = close(fd);
1.411 djm 468: *fdp = -1; /* probably redundant */
1.132 markus 469: return ret;
470: }
471:
1.52 markus 472: /* Close all channel fd/socket. */
1.127 itojun 473: static void
1.367 djm 474: channel_close_fds(struct ssh *ssh, Channel *c)
475: {
1.379 tb 476: int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
477:
1.407 djm 478: channel_close_fd(ssh, c, &c->sock);
1.379 tb 479: if (rfd != sock)
1.407 djm 480: channel_close_fd(ssh, c, &c->rfd);
1.379 tb 481: if (wfd != sock && wfd != rfd)
1.407 djm 482: channel_close_fd(ssh, c, &c->wfd);
1.379 tb 483: if (efd != sock && efd != rfd && efd != wfd)
1.407 djm 484: channel_close_fd(ssh, c, &c->efd);
1.367 djm 485: }
486:
487: static void
1.381 djm 488: fwd_perm_clear(struct permission *perm)
1.52 markus 489: {
1.381 djm 490: free(perm->host_to_connect);
491: free(perm->listen_host);
492: free(perm->listen_path);
1.398 dtucker 493: memset(perm, 0, sizeof(*perm));
1.367 djm 494: }
495:
1.381 djm 496: /* Returns an printable name for the specified forwarding permission list */
497: static const char *
498: fwd_ident(int who, int where)
499: {
500: if (who == FORWARD_ADM) {
501: if (where == FORWARD_LOCAL)
502: return "admin local";
503: else if (where == FORWARD_REMOTE)
504: return "admin remote";
505: } else if (who == FORWARD_USER) {
506: if (where == FORWARD_LOCAL)
507: return "user local";
508: else if (where == FORWARD_REMOTE)
509: return "user remote";
510: }
511: fatal("Unknown forward permission list %d/%d", who, where);
512: }
513:
514: /* Returns the forwarding permission list for the specified direction */
515: static struct permission_set *
516: permission_set_get(struct ssh *ssh, int where)
517: {
518: struct ssh_channels *sc = ssh->chanctxt;
1.367 djm 519:
1.381 djm 520: switch (where) {
521: case FORWARD_LOCAL:
522: return &sc->local_perms;
523: break;
524: case FORWARD_REMOTE:
525: return &sc->remote_perms;
526: break;
527: default:
1.403 djm 528: fatal_f("invalid forwarding direction %d", where);
1.381 djm 529: }
530: }
531:
1.392 dtucker 532: /* Returns pointers to the specified forwarding list and its element count */
1.381 djm 533: static void
534: permission_set_get_array(struct ssh *ssh, int who, int where,
535: struct permission ***permpp, u_int **npermpp)
536: {
537: struct permission_set *pset = permission_set_get(ssh, where);
538:
539: switch (who) {
540: case FORWARD_USER:
541: *permpp = &pset->permitted_user;
542: *npermpp = &pset->num_permitted_user;
543: break;
544: case FORWARD_ADM:
545: *permpp = &pset->permitted_admin;
546: *npermpp = &pset->num_permitted_admin;
547: break;
548: default:
1.403 djm 549: fatal_f("invalid forwarding client %d", who);
1.381 djm 550: }
551: }
552:
1.409 jsg 553: /* Adds an entry to the specified forwarding list */
1.367 djm 554: static int
1.381 djm 555: permission_set_add(struct ssh *ssh, int who, int where,
1.367 djm 556: const char *host_to_connect, int port_to_connect,
557: const char *listen_host, const char *listen_path, int listen_port,
558: Channel *downstream)
559: {
1.381 djm 560: struct permission **permp;
561: u_int n, *npermp;
1.367 djm 562:
1.381 djm 563: permission_set_get_array(ssh, who, where, &permp, &npermp);
1.367 djm 564:
1.381 djm 565: if (*npermp >= INT_MAX)
1.403 djm 566: fatal_f("%s overflow", fwd_ident(who, where));
1.367 djm 567:
1.381 djm 568: *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp));
569: n = (*npermp)++;
1.367 djm 570: #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
1.381 djm 571: (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect);
572: (*permp)[n].port_to_connect = port_to_connect;
573: (*permp)[n].listen_host = MAYBE_DUP(listen_host);
574: (*permp)[n].listen_path = MAYBE_DUP(listen_path);
575: (*permp)[n].listen_port = listen_port;
576: (*permp)[n].downstream = downstream;
1.367 djm 577: #undef MAYBE_DUP
578: return (int)n;
579: }
580:
581: static void
582: mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
583: {
584: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 585: struct permission_set *pset = &sc->local_perms;
586: struct permission *perm;
1.367 djm 587: int r;
588: u_int i;
589:
1.381 djm 590: for (i = 0; i < pset->num_permitted_user; i++) {
591: perm = &pset->permitted_user[i];
592: if (perm->downstream != c)
1.367 djm 593: continue;
594:
595: /* cancel on the server, since mux client is gone */
596: debug("channel %d: cleanup remote forward for %s:%u",
1.381 djm 597: c->self, perm->listen_host, perm->listen_port);
1.367 djm 598: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
599: (r = sshpkt_put_cstring(ssh,
600: "cancel-tcpip-forward")) != 0 ||
601: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
602: (r = sshpkt_put_cstring(ssh,
1.381 djm 603: channel_rfwd_bind_host(perm->listen_host))) != 0 ||
604: (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 ||
1.367 djm 605: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 606: fatal_fr(r, "channel %i", c->self);
1.367 djm 607: }
1.381 djm 608: fwd_perm_clear(perm); /* unregister */
1.367 djm 609: }
1.121 markus 610: }
611:
612: /* Free the channel and close its fd/socket. */
613: void
1.367 djm 614: channel_free(struct ssh *ssh, Channel *c)
1.121 markus 615: {
1.367 djm 616: struct ssh_channels *sc = ssh->chanctxt;
1.121 markus 617: char *s;
1.209 avsm 618: u_int i, n;
1.354 markus 619: Channel *other;
1.275 djm 620: struct channel_confirm *cc;
1.121 markus 621:
1.367 djm 622: for (n = 0, i = 0; i < sc->channels_alloc; i++) {
623: if ((other = sc->channels[i]) == NULL)
624: continue;
625: n++;
626: /* detach from mux client and prepare for closing */
627: if (c->type == SSH_CHANNEL_MUX_CLIENT &&
628: other->type == SSH_CHANNEL_MUX_PROXY &&
629: other->mux_ctx == c) {
630: other->mux_ctx = NULL;
631: other->type = SSH_CHANNEL_OPEN;
632: other->istate = CHAN_INPUT_CLOSED;
633: other->ostate = CHAN_OUTPUT_CLOSED;
1.354 markus 634: }
635: }
1.209 avsm 636: debug("channel %d: free: %s, nchannels %u", c->self,
1.121 markus 637: c->remote_name ? c->remote_name : "???", n);
638:
1.408 mbuhl 639: if (c->type == SSH_CHANNEL_MUX_CLIENT) {
1.367 djm 640: mux_remove_remote_forwardings(ssh, c);
1.408 mbuhl 641: free(c->mux_ctx);
642: c->mux_ctx = NULL;
643: } else if (c->type == SSH_CHANNEL_MUX_LISTENER) {
1.401 djm 644: free(c->mux_ctx);
645: c->mux_ctx = NULL;
646: }
1.354 markus 647:
1.384 markus 648: if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) {
649: s = channel_open_message(ssh);
650: debug3("channel %d: status: %s", c->self, s);
651: free(s);
652: }
1.121 markus 653:
1.367 djm 654: channel_close_fds(ssh, c);
655: sshbuf_free(c->input);
656: sshbuf_free(c->output);
657: sshbuf_free(c->extended);
658: c->input = c->output = c->extended = NULL;
1.321 djm 659: free(c->remote_name);
660: c->remote_name = NULL;
661: free(c->path);
662: c->path = NULL;
663: free(c->listening_addr);
664: c->listening_addr = NULL;
1.425 ! djm 665: free(c->xctype);
! 666: c->xctype = NULL;
1.275 djm 667: while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
668: if (cc->abandon_cb != NULL)
1.367 djm 669: cc->abandon_cb(ssh, c, cc->ctx);
1.275 djm 670: TAILQ_REMOVE(&c->status_confirms, cc, entry);
1.396 jsg 671: freezero(cc, sizeof(*cc));
1.275 djm 672: }
1.280 djm 673: if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
1.367 djm 674: c->filter_cleanup(ssh, c->self, c->filter_ctx);
675: sc->channels[c->self] = NULL;
1.396 jsg 676: freezero(c, sizeof(*c));
1.121 markus 677: }
678:
679: void
1.367 djm 680: channel_free_all(struct ssh *ssh)
1.121 markus 681: {
1.209 avsm 682: u_int i;
1.390 dtucker 683: struct ssh_channels *sc = ssh->chanctxt;
1.121 markus 684:
1.390 dtucker 685: for (i = 0; i < sc->channels_alloc; i++)
686: if (sc->channels[i] != NULL)
687: channel_free(ssh, sc->channels[i]);
688:
689: free(sc->channels);
690: sc->channels = NULL;
691: sc->channels_alloc = 0;
692:
693: free(sc->x11_saved_display);
694: sc->x11_saved_display = NULL;
695:
696: free(sc->x11_saved_proto);
697: sc->x11_saved_proto = NULL;
698:
699: free(sc->x11_saved_data);
700: sc->x11_saved_data = NULL;
701: sc->x11_saved_data_len = 0;
702:
703: free(sc->x11_fake_data);
704: sc->x11_fake_data = NULL;
705: sc->x11_fake_data_len = 0;
1.131 markus 706: }
707:
1.121 markus 708: /*
709: * Closes the sockets/fds of all channels. This is used to close extra file
710: * descriptors after a fork.
711: */
712: void
1.367 djm 713: channel_close_all(struct ssh *ssh)
1.121 markus 714: {
1.209 avsm 715: u_int i;
1.121 markus 716:
1.367 djm 717: for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
718: if (ssh->chanctxt->channels[i] != NULL)
719: channel_close_fds(ssh, ssh->chanctxt->channels[i]);
1.121 markus 720: }
721:
722: /*
1.131 markus 723: * Stop listening to channels.
724: */
725: void
1.367 djm 726: channel_stop_listening(struct ssh *ssh)
1.131 markus 727: {
1.209 avsm 728: u_int i;
1.131 markus 729: Channel *c;
730:
1.367 djm 731: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
732: c = ssh->chanctxt->channels[i];
1.131 markus 733: if (c != NULL) {
734: switch (c->type) {
735: case SSH_CHANNEL_AUTH_SOCKET:
736: case SSH_CHANNEL_PORT_LISTENER:
737: case SSH_CHANNEL_RPORT_LISTENER:
738: case SSH_CHANNEL_X11_LISTENER:
1.336 millert 739: case SSH_CHANNEL_UNIX_LISTENER:
740: case SSH_CHANNEL_RUNIX_LISTENER:
1.407 djm 741: channel_close_fd(ssh, c, &c->sock);
1.367 djm 742: channel_free(ssh, c);
1.131 markus 743: break;
744: }
745: }
746: }
747: }
748:
749: /*
1.121 markus 750: * Returns true if no channel has too much buffered data, and false if one or
751: * more channel is overfull.
752: */
753: int
1.367 djm 754: channel_not_very_much_buffered_data(struct ssh *ssh)
1.121 markus 755: {
756: u_int i;
1.367 djm 757: u_int maxsize = ssh_packet_get_maxsize(ssh);
1.121 markus 758: Channel *c;
759:
1.367 djm 760: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
761: c = ssh->chanctxt->channels[i];
762: if (c == NULL || c->type != SSH_CHANNEL_OPEN)
763: continue;
764: if (sshbuf_len(c->output) > maxsize) {
765: debug2("channel %d: big output buffer %zu > %u",
766: c->self, sshbuf_len(c->output), maxsize);
767: return 0;
1.121 markus 768: }
769: }
770: return 1;
771: }
772:
773: /* Returns true if any channel is still open. */
774: int
1.367 djm 775: channel_still_open(struct ssh *ssh)
1.121 markus 776: {
1.209 avsm 777: u_int i;
1.121 markus 778: Channel *c;
779:
1.367 djm 780: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
781: c = ssh->chanctxt->channels[i];
1.121 markus 782: if (c == NULL)
783: continue;
784: switch (c->type) {
785: case SSH_CHANNEL_X11_LISTENER:
786: case SSH_CHANNEL_PORT_LISTENER:
787: case SSH_CHANNEL_RPORT_LISTENER:
1.302 djm 788: case SSH_CHANNEL_MUX_LISTENER:
1.121 markus 789: case SSH_CHANNEL_CLOSED:
790: case SSH_CHANNEL_AUTH_SOCKET:
791: case SSH_CHANNEL_DYNAMIC:
1.372 markus 792: case SSH_CHANNEL_RDYNAMIC_OPEN:
1.121 markus 793: case SSH_CHANNEL_CONNECTING:
794: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 795: case SSH_CHANNEL_ABANDONED:
1.336 millert 796: case SSH_CHANNEL_UNIX_LISTENER:
797: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 798: continue;
799: case SSH_CHANNEL_LARVAL:
800: continue;
801: case SSH_CHANNEL_OPENING:
802: case SSH_CHANNEL_OPEN:
1.372 markus 803: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 804: case SSH_CHANNEL_X11_OPEN:
1.302 djm 805: case SSH_CHANNEL_MUX_CLIENT:
1.354 markus 806: case SSH_CHANNEL_MUX_PROXY:
1.121 markus 807: return 1;
808: default:
1.403 djm 809: fatal_f("bad channel type %d", c->type);
1.121 markus 810: /* NOTREACHED */
811: }
812: }
813: return 0;
814: }
815:
816: /* Returns the id of an open channel suitable for keepaliving */
817: int
1.367 djm 818: channel_find_open(struct ssh *ssh)
1.121 markus 819: {
1.209 avsm 820: u_int i;
1.121 markus 821: Channel *c;
822:
1.367 djm 823: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
824: c = ssh->chanctxt->channels[i];
1.368 djm 825: if (c == NULL || !c->have_remote_id)
1.121 markus 826: continue;
827: switch (c->type) {
828: case SSH_CHANNEL_CLOSED:
829: case SSH_CHANNEL_DYNAMIC:
1.372 markus 830: case SSH_CHANNEL_RDYNAMIC_OPEN:
831: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 832: case SSH_CHANNEL_X11_LISTENER:
833: case SSH_CHANNEL_PORT_LISTENER:
834: case SSH_CHANNEL_RPORT_LISTENER:
1.302 djm 835: case SSH_CHANNEL_MUX_LISTENER:
836: case SSH_CHANNEL_MUX_CLIENT:
1.354 markus 837: case SSH_CHANNEL_MUX_PROXY:
1.121 markus 838: case SSH_CHANNEL_OPENING:
839: case SSH_CHANNEL_CONNECTING:
840: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 841: case SSH_CHANNEL_ABANDONED:
1.336 millert 842: case SSH_CHANNEL_UNIX_LISTENER:
843: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 844: continue;
845: case SSH_CHANNEL_LARVAL:
846: case SSH_CHANNEL_AUTH_SOCKET:
847: case SSH_CHANNEL_OPEN:
848: case SSH_CHANNEL_X11_OPEN:
849: return i;
850: default:
1.403 djm 851: fatal_f("bad channel type %d", c->type);
1.121 markus 852: /* NOTREACHED */
853: }
854: }
855: return -1;
856: }
857:
1.385 djm 858: /* Returns the state of the channel's extended usage flag */
859: const char *
860: channel_format_extended_usage(const Channel *c)
861: {
862: if (c->efd == -1)
863: return "closed";
864:
865: switch (c->extended_usage) {
866: case CHAN_EXTENDED_WRITE:
867: return "write";
868: case CHAN_EXTENDED_READ:
869: return "read";
870: case CHAN_EXTENDED_IGNORE:
871: return "ignore";
872: default:
873: return "UNKNOWN";
874: }
875: }
876:
1.386 djm 877: static char *
878: channel_format_status(const Channel *c)
879: {
880: char *ret = NULL;
881:
1.425 ! djm 882: xasprintf(&ret, "t%d [%s] %s%u i%u/%zu o%u/%zu e[%s]/%zu "
1.411 djm 883: "fd %d/%d/%d sock %d cc %d io 0x%02x/0x%02x",
1.425 ! djm 884: c->type, c->xctype != NULL ? c->xctype : c->ctype,
1.386 djm 885: c->have_remote_id ? "r" : "nr", c->remote_id,
886: c->istate, sshbuf_len(c->input),
887: c->ostate, sshbuf_len(c->output),
888: channel_format_extended_usage(c), sshbuf_len(c->extended),
1.411 djm 889: c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan,
890: c->io_want, c->io_ready);
1.386 djm 891: return ret;
892: }
893:
1.121 markus 894: /*
895: * Returns a message describing the currently open forwarded connections,
896: * suitable for sending to the client. The message contains crlf pairs for
897: * newlines.
898: */
899: char *
1.367 djm 900: channel_open_message(struct ssh *ssh)
1.121 markus 901: {
1.367 djm 902: struct sshbuf *buf;
1.121 markus 903: Channel *c;
1.209 avsm 904: u_int i;
1.367 djm 905: int r;
1.386 djm 906: char *cp, *ret;
1.121 markus 907:
1.367 djm 908: if ((buf = sshbuf_new()) == NULL)
1.403 djm 909: fatal_f("sshbuf_new");
1.367 djm 910: if ((r = sshbuf_putf(buf,
911: "The following connections are open:\r\n")) != 0)
1.403 djm 912: fatal_fr(r, "sshbuf_putf");
1.367 djm 913: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
914: c = ssh->chanctxt->channels[i];
1.121 markus 915: if (c == NULL)
916: continue;
917: switch (c->type) {
918: case SSH_CHANNEL_X11_LISTENER:
919: case SSH_CHANNEL_PORT_LISTENER:
920: case SSH_CHANNEL_RPORT_LISTENER:
921: case SSH_CHANNEL_CLOSED:
922: case SSH_CHANNEL_AUTH_SOCKET:
923: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 924: case SSH_CHANNEL_ABANDONED:
1.302 djm 925: case SSH_CHANNEL_MUX_LISTENER:
1.336 millert 926: case SSH_CHANNEL_UNIX_LISTENER:
927: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 928: continue;
929: case SSH_CHANNEL_LARVAL:
930: case SSH_CHANNEL_OPENING:
931: case SSH_CHANNEL_CONNECTING:
932: case SSH_CHANNEL_DYNAMIC:
1.372 markus 933: case SSH_CHANNEL_RDYNAMIC_OPEN:
934: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 935: case SSH_CHANNEL_OPEN:
936: case SSH_CHANNEL_X11_OPEN:
1.354 markus 937: case SSH_CHANNEL_MUX_PROXY:
938: case SSH_CHANNEL_MUX_CLIENT:
1.386 djm 939: cp = channel_format_status(c);
940: if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n",
941: c->self, c->remote_name, cp)) != 0) {
942: free(cp);
1.403 djm 943: fatal_fr(r, "sshbuf_putf");
1.386 djm 944: }
945: free(cp);
1.121 markus 946: continue;
947: default:
1.403 djm 948: fatal_f("bad channel type %d", c->type);
1.121 markus 949: /* NOTREACHED */
950: }
951: }
1.367 djm 952: if ((ret = sshbuf_dup_string(buf)) == NULL)
1.403 djm 953: fatal_f("sshbuf_dup_string");
1.367 djm 954: sshbuf_free(buf);
955: return ret;
956: }
957:
958: static void
959: open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
960: {
961: int r;
962:
963: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
964: (r = sshpkt_put_cstring(ssh, type)) != 0 ||
965: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
966: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
967: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
1.403 djm 968: fatal_r(r, "%s: channel %i: open", where, c->self);
1.367 djm 969: }
1.121 markus 970: }
971:
972: void
1.367 djm 973: channel_send_open(struct ssh *ssh, int id)
1.121 markus 974: {
1.367 djm 975: Channel *c = channel_lookup(ssh, id);
976: int r;
1.180 deraadt 977:
1.121 markus 978: if (c == NULL) {
1.188 itojun 979: logit("channel_send_open: %d: bad id", id);
1.121 markus 980: return;
981: }
1.184 markus 982: debug2("channel %d: send open", id);
1.367 djm 983: open_preamble(ssh, __func__, c, c->ctype);
984: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 985: fatal_fr(r, "channel %i", c->self);
1.121 markus 986: }
987:
988: void
1.367 djm 989: channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
1.121 markus 990: {
1.367 djm 991: Channel *c = channel_lookup(ssh, id);
992: int r;
1.180 deraadt 993:
1.121 markus 994: if (c == NULL) {
1.403 djm 995: logit_f("%d: unknown channel id", id);
1.121 markus 996: return;
997: }
1.368 djm 998: if (!c->have_remote_id)
1.403 djm 999: fatal_f("channel %d: no remote id", c->self);
1.368 djm 1000:
1.204 djm 1001: debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
1.367 djm 1002: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
1003: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1004: (r = sshpkt_put_cstring(ssh, service)) != 0 ||
1005: (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
1.403 djm 1006: fatal_fr(r, "channel %i", c->self);
1.367 djm 1007: }
1.121 markus 1008: }
1.241 deraadt 1009:
1.121 markus 1010: void
1.367 djm 1011: channel_register_status_confirm(struct ssh *ssh, int id,
1012: channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
1.275 djm 1013: {
1014: struct channel_confirm *cc;
1015: Channel *c;
1016:
1.367 djm 1017: if ((c = channel_lookup(ssh, id)) == NULL)
1.403 djm 1018: fatal_f("%d: bad id", id);
1.275 djm 1019:
1.327 djm 1020: cc = xcalloc(1, sizeof(*cc));
1.275 djm 1021: cc->cb = cb;
1022: cc->abandon_cb = abandon_cb;
1023: cc->ctx = ctx;
1024: TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
1025: }
1026:
1027: void
1.367 djm 1028: channel_register_open_confirm(struct ssh *ssh, int id,
1029: channel_open_fn *fn, void *ctx)
1.121 markus 1030: {
1.367 djm 1031: Channel *c = channel_lookup(ssh, id);
1.180 deraadt 1032:
1.121 markus 1033: if (c == NULL) {
1.403 djm 1034: logit_f("%d: bad id", id);
1.121 markus 1035: return;
1036: }
1.275 djm 1037: c->open_confirm = fn;
1038: c->open_confirm_ctx = ctx;
1.121 markus 1039: }
1.241 deraadt 1040:
1.121 markus 1041: void
1.367 djm 1042: channel_register_cleanup(struct ssh *ssh, int id,
1043: channel_callback_fn *fn, int do_close)
1.121 markus 1044: {
1.367 djm 1045: Channel *c = channel_by_id(ssh, id);
1.180 deraadt 1046:
1.121 markus 1047: if (c == NULL) {
1.403 djm 1048: logit_f("%d: bad id", id);
1.121 markus 1049: return;
1050: }
1.131 markus 1051: c->detach_user = fn;
1.225 djm 1052: c->detach_close = do_close;
1.121 markus 1053: }
1.241 deraadt 1054:
1.121 markus 1055: void
1.367 djm 1056: channel_cancel_cleanup(struct ssh *ssh, int id)
1.121 markus 1057: {
1.367 djm 1058: Channel *c = channel_by_id(ssh, id);
1.180 deraadt 1059:
1.121 markus 1060: if (c == NULL) {
1.403 djm 1061: logit_f("%d: bad id", id);
1.121 markus 1062: return;
1.52 markus 1063: }
1.131 markus 1064: c->detach_user = NULL;
1.225 djm 1065: c->detach_close = 0;
1.121 markus 1066: }
1.241 deraadt 1067:
1.121 markus 1068: void
1.367 djm 1069: channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
1.280 djm 1070: channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
1.121 markus 1071: {
1.367 djm 1072: Channel *c = channel_lookup(ssh, id);
1.180 deraadt 1073:
1.121 markus 1074: if (c == NULL) {
1.403 djm 1075: logit_f("%d: bad id", id);
1.121 markus 1076: return;
1.52 markus 1077: }
1.231 reyk 1078: c->input_filter = ifn;
1079: c->output_filter = ofn;
1.279 djm 1080: c->filter_ctx = ctx;
1.280 djm 1081: c->filter_cleanup = cfn;
1.52 markus 1082: }
1083:
1.49 markus 1084: void
1.367 djm 1085: channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
1.282 dtucker 1086: int extusage, int nonblock, int is_tty, u_int window_max)
1.1 deraadt 1087: {
1.367 djm 1088: Channel *c = channel_lookup(ssh, id);
1089: int r;
1.180 deraadt 1090:
1.121 markus 1091: if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
1092: fatal("channel_activate for non-larval channel %d.", id);
1.368 djm 1093: if (!c->have_remote_id)
1.403 djm 1094: fatal_f("channel %d: no remote id", c->self);
1.368 djm 1095:
1.367 djm 1096: channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
1.121 markus 1097: c->type = SSH_CHANNEL_OPEN;
1.168 markus 1098: c->local_window = c->local_window_max = window_max;
1.367 djm 1099:
1100: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1101: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1102: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1103: (r = sshpkt_send(ssh)) != 0)
1.403 djm 1104: fatal_fr(r, "channel %i", c->self);
1.1 deraadt 1105: }
1106:
1.127 itojun 1107: static void
1.410 djm 1108: channel_pre_listener(struct ssh *ssh, Channel *c)
1.41 markus 1109: {
1.410 djm 1110: c->io_want = SSH_CHAN_IO_SOCK_R;
1.41 markus 1111: }
1112:
1.127 itojun 1113: static void
1.410 djm 1114: channel_pre_connecting(struct ssh *ssh, Channel *c)
1.75 markus 1115: {
1116: debug3("channel %d: waiting for connection", c->self);
1.410 djm 1117: c->io_want = SSH_CHAN_IO_SOCK_W;
1.75 markus 1118: }
1119:
1.127 itojun 1120: static void
1.410 djm 1121: channel_pre_open(struct ssh *ssh, Channel *c)
1.41 markus 1122: {
1.410 djm 1123: c->io_want = 0;
1.44 markus 1124: if (c->istate == CHAN_INPUT_OPEN &&
1.358 djm 1125: c->remote_window > 0 &&
1.367 djm 1126: sshbuf_len(c->input) < c->remote_window &&
1127: sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
1.410 djm 1128: c->io_want |= SSH_CHAN_IO_RFD;
1.44 markus 1129: if (c->ostate == CHAN_OUTPUT_OPEN ||
1130: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.367 djm 1131: if (sshbuf_len(c->output) > 0) {
1.410 djm 1132: c->io_want |= SSH_CHAN_IO_WFD;
1.44 markus 1133: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.172 markus 1134: if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.367 djm 1135: debug2("channel %d: "
1136: "obuf_empty delayed efd %d/(%zu)", c->self,
1137: c->efd, sshbuf_len(c->extended));
1.172 markus 1138: else
1.367 djm 1139: chan_obuf_empty(ssh, c);
1.44 markus 1140: }
1141: }
1142: /** XXX check close conditions, too */
1.358 djm 1143: if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1144: c->ostate == CHAN_OUTPUT_CLOSED)) {
1.44 markus 1145: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1.367 djm 1146: sshbuf_len(c->extended) > 0)
1.410 djm 1147: c->io_want |= SSH_CHAN_IO_EFD_W;
1.306 djm 1148: else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1149: (c->extended_usage == CHAN_EXTENDED_READ ||
1150: c->extended_usage == CHAN_EXTENDED_IGNORE) &&
1.367 djm 1151: sshbuf_len(c->extended) < c->remote_window)
1.410 djm 1152: c->io_want |= SSH_CHAN_IO_EFD_R;
1.44 markus 1153: }
1.204 djm 1154: /* XXX: What about efd? races? */
1.44 markus 1155: }
1156:
1.41 markus 1157: /*
1158: * This is a special state for X11 authentication spoofing. An opened X11
1159: * connection (when authentication spoofing is being done) remains in this
1160: * state until the first packet has been completely read. The authentication
1161: * data in that packet is then substituted by the real data if it matches the
1162: * fake data, and the channel is put into normal mode.
1.51 markus 1163: * XXX All this happens at the client side.
1.121 markus 1164: * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41 markus 1165: */
1.127 itojun 1166: static int
1.367 djm 1167: x11_open_helper(struct ssh *ssh, struct sshbuf *b)
1.1 deraadt 1168: {
1.367 djm 1169: struct ssh_channels *sc = ssh->chanctxt;
1.77 markus 1170: u_char *ucp;
1171: u_int proto_len, data_len;
1.25 markus 1172:
1.347 djm 1173: /* Is this being called after the refusal deadline? */
1.367 djm 1174: if (sc->x11_refuse_time != 0 &&
1175: (u_int)monotime() >= sc->x11_refuse_time) {
1.347 djm 1176: verbose("Rejected X11 connection after ForwardX11Timeout "
1177: "expired");
1178: return -1;
1179: }
1180:
1.41 markus 1181: /* Check if the fixed size part of the packet is in buffer. */
1.367 djm 1182: if (sshbuf_len(b) < 12)
1.41 markus 1183: return 0;
1184:
1185: /* Parse the lengths of variable-length fields. */
1.367 djm 1186: ucp = sshbuf_mutable_ptr(b);
1.41 markus 1187: if (ucp[0] == 0x42) { /* Byte order MSB first. */
1188: proto_len = 256 * ucp[6] + ucp[7];
1189: data_len = 256 * ucp[8] + ucp[9];
1190: } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1191: proto_len = ucp[6] + 256 * ucp[7];
1192: data_len = ucp[8] + 256 * ucp[9];
1193: } else {
1.194 markus 1194: debug2("Initial X11 packet contains bad byte order byte: 0x%x",
1.148 deraadt 1195: ucp[0]);
1.41 markus 1196: return -1;
1197: }
1198:
1199: /* Check if the whole packet is in buffer. */
1.367 djm 1200: if (sshbuf_len(b) <
1.41 markus 1201: 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1202: return 0;
1203:
1204: /* Check if authentication protocol matches. */
1.367 djm 1205: if (proto_len != strlen(sc->x11_saved_proto) ||
1206: memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
1.194 markus 1207: debug2("X11 connection uses different authentication protocol.");
1.41 markus 1208: return -1;
1209: }
1210: /* Check if authentication data matches our fake data. */
1.367 djm 1211: if (data_len != sc->x11_fake_data_len ||
1.308 djm 1212: timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
1.367 djm 1213: sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
1.194 markus 1214: debug2("X11 auth data does not match fake data.");
1.41 markus 1215: return -1;
1216: }
1217: /* Check fake data length */
1.367 djm 1218: if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
1.41 markus 1219: error("X11 fake_data_len %d != saved_data_len %d",
1.367 djm 1220: sc->x11_fake_data_len, sc->x11_saved_data_len);
1.41 markus 1221: return -1;
1222: }
1223: /*
1224: * Received authentication protocol and data match
1225: * our fake data. Substitute the fake data with real
1226: * data.
1227: */
1228: memcpy(ucp + 12 + ((proto_len + 3) & ~3),
1.367 djm 1229: sc->x11_saved_data, sc->x11_saved_data_len);
1.41 markus 1230: return 1;
1231: }
1232:
1.423 djm 1233: void
1234: channel_force_close(struct ssh *ssh, Channel *c, int abandon)
1235: {
1236: debug3_f("channel %d: forcibly closing", c->self);
1237: if (c->istate == CHAN_INPUT_OPEN)
1238: chan_read_failed(ssh, c);
1239: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1240: sshbuf_reset(c->input);
1241: chan_ibuf_empty(ssh, c);
1242: }
1243: if (c->ostate == CHAN_OUTPUT_OPEN ||
1244: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1245: sshbuf_reset(c->output);
1246: chan_write_failed(ssh, c);
1247: }
1248: if (c->detach_user)
1249: c->detach_user(ssh, c->self, 1, NULL);
1250: if (c->efd != -1)
1251: channel_close_fd(ssh, c, &c->efd);
1252: if (abandon)
1253: c->type = SSH_CHANNEL_ABANDONED;
1254: }
1255:
1.127 itojun 1256: static void
1.410 djm 1257: channel_pre_x11_open(struct ssh *ssh, Channel *c)
1.41 markus 1258: {
1.367 djm 1259: int ret = x11_open_helper(ssh, c->output);
1.133 markus 1260:
1261: /* c->force_drain = 1; */
1262:
1.41 markus 1263: if (ret == 1) {
1264: c->type = SSH_CHANNEL_OPEN;
1.410 djm 1265: channel_pre_open(ssh, c);
1.41 markus 1266: } else if (ret == -1) {
1.423 djm 1267: logit("X11 connection rejected because of wrong "
1268: "authentication.");
1.367 djm 1269: debug2("X11 rejected %d i%d/o%d",
1270: c->self, c->istate, c->ostate);
1.423 djm 1271: channel_force_close(ssh, c, 0);
1.41 markus 1272: }
1273: }
1.25 markus 1274:
1.302 djm 1275: static void
1.410 djm 1276: channel_pre_mux_client(struct ssh *ssh, Channel *c)
1.302 djm 1277: {
1.410 djm 1278: c->io_want = 0;
1.304 djm 1279: if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
1.367 djm 1280: sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
1.410 djm 1281: c->io_want |= SSH_CHAN_IO_RFD;
1.302 djm 1282: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1283: /* clear buffer immediately (discard any partial packet) */
1.367 djm 1284: sshbuf_reset(c->input);
1285: chan_ibuf_empty(ssh, c);
1.302 djm 1286: /* Start output drain. XXX just kill chan? */
1.367 djm 1287: chan_rcvd_oclose(ssh, c);
1.302 djm 1288: }
1289: if (c->ostate == CHAN_OUTPUT_OPEN ||
1290: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.367 djm 1291: if (sshbuf_len(c->output) > 0)
1.410 djm 1292: c->io_want |= SSH_CHAN_IO_WFD;
1.302 djm 1293: else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1.367 djm 1294: chan_obuf_empty(ssh, c);
1.302 djm 1295: }
1296: }
1297:
1.104 markus 1298: /* try to decode a socks4 header */
1.127 itojun 1299: static int
1.372 markus 1300: channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
1.103 markus 1301: {
1.367 djm 1302: const u_char *p;
1303: char *host;
1.292 djm 1304: u_int len, have, i, found, need;
1.148 deraadt 1305: char username[256];
1.103 markus 1306: struct {
1307: u_int8_t version;
1308: u_int8_t command;
1309: u_int16_t dest_port;
1.104 markus 1310: struct in_addr dest_addr;
1.103 markus 1311: } s4_req, s4_rsp;
1.367 djm 1312: int r;
1.103 markus 1313:
1.104 markus 1314: debug2("channel %d: decode socks4", c->self);
1.109 markus 1315:
1.372 markus 1316: have = sshbuf_len(input);
1.109 markus 1317: len = sizeof(s4_req);
1318: if (have < len)
1319: return 0;
1.372 markus 1320: p = sshbuf_ptr(input);
1.292 djm 1321:
1322: need = 1;
1323: /* SOCKS4A uses an invalid IP address 0.0.0.x */
1324: if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1325: debug2("channel %d: socks4a request", c->self);
1326: /* ... and needs an extra string (the hostname) */
1327: need = 2;
1328: }
1329: /* Check for terminating NUL on the string(s) */
1.109 markus 1330: for (found = 0, i = len; i < have; i++) {
1331: if (p[i] == '\0') {
1.292 djm 1332: found++;
1333: if (found == need)
1334: break;
1.109 markus 1335: }
1336: if (i > 1024) {
1337: /* the peer is probably sending garbage */
1338: debug("channel %d: decode socks4: too long",
1339: c->self);
1340: return -1;
1341: }
1342: }
1.292 djm 1343: if (found < need)
1.109 markus 1344: return 0;
1.372 markus 1345: if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1346: (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1347: (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1348: (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
1.403 djm 1349: debug_r(r, "channels %d: decode socks4", c->self);
1.367 djm 1350: return -1;
1351: }
1.372 markus 1352: have = sshbuf_len(input);
1353: p = sshbuf_ptr(input);
1.364 djm 1354: if (memchr(p, '\0', have) == NULL) {
1.403 djm 1355: error("channel %d: decode socks4: unterminated user", c->self);
1.364 djm 1356: return -1;
1357: }
1.103 markus 1358: len = strlen(p);
1.106 markus 1359: debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.367 djm 1360: len++; /* trailing '\0' */
1.103 markus 1361: strlcpy(username, p, sizeof(username));
1.403 djm 1362: if ((r = sshbuf_consume(input, len)) != 0)
1363: fatal_fr(r, "channel %d: consume", c->self);
1.321 djm 1364: free(c->path);
1365: c->path = NULL;
1.292 djm 1366: if (need == 1) { /* SOCKS4: one string */
1367: host = inet_ntoa(s4_req.dest_addr);
1.293 djm 1368: c->path = xstrdup(host);
1.292 djm 1369: } else { /* SOCKS4A: two strings */
1.372 markus 1370: have = sshbuf_len(input);
1371: p = sshbuf_ptr(input);
1.364 djm 1372: if (memchr(p, '\0', have) == NULL) {
1373: error("channel %d: decode socks4a: host not nul "
1374: "terminated", c->self);
1375: return -1;
1376: }
1.292 djm 1377: len = strlen(p);
1378: debug2("channel %d: decode socks4a: host %s/%d",
1379: c->self, p, len);
1380: len++; /* trailing '\0' */
1.293 djm 1381: if (len > NI_MAXHOST) {
1.292 djm 1382: error("channel %d: hostname \"%.100s\" too long",
1383: c->self, p);
1384: return -1;
1385: }
1.293 djm 1386: c->path = xstrdup(p);
1.403 djm 1387: if ((r = sshbuf_consume(input, len)) != 0)
1388: fatal_fr(r, "channel %d: consume", c->self);
1.292 djm 1389: }
1.103 markus 1390: c->host_port = ntohs(s4_req.dest_port);
1.148 deraadt 1391:
1.194 markus 1392: debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1.292 djm 1393: c->self, c->path, c->host_port, s4_req.command);
1.103 markus 1394:
1.104 markus 1395: if (s4_req.command != 1) {
1.292 djm 1396: debug("channel %d: cannot handle: %s cn %d",
1397: c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
1.104 markus 1398: return -1;
1.103 markus 1399: }
1.104 markus 1400: s4_rsp.version = 0; /* vn: 0 for reply */
1401: s4_rsp.command = 90; /* cd: req granted */
1.103 markus 1402: s4_rsp.dest_port = 0; /* ignored */
1.104 markus 1403: s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1.403 djm 1404: if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0)
1405: fatal_fr(r, "channel %d: append reply", c->self);
1.104 markus 1406: return 1;
1407: }
1408:
1.193 markus 1409: /* try to decode a socks5 header */
1410: #define SSH_SOCKS5_AUTHDONE 0x1000
1411: #define SSH_SOCKS5_NOAUTH 0x00
1412: #define SSH_SOCKS5_IPV4 0x01
1413: #define SSH_SOCKS5_DOMAIN 0x03
1414: #define SSH_SOCKS5_IPV6 0x04
1415: #define SSH_SOCKS5_CONNECT 0x01
1416: #define SSH_SOCKS5_SUCCESS 0x00
1417:
1418: static int
1.372 markus 1419: channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
1.193 markus 1420: {
1.367 djm 1421: /* XXX use get/put_u8 instead of trusting struct padding */
1.193 markus 1422: struct {
1423: u_int8_t version;
1424: u_int8_t command;
1425: u_int8_t reserved;
1426: u_int8_t atyp;
1427: } s5_req, s5_rsp;
1428: u_int16_t dest_port;
1.324 djm 1429: char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1.367 djm 1430: const u_char *p;
1.252 djm 1431: u_int have, need, i, found, nmethods, addrlen, af;
1.367 djm 1432: int r;
1.193 markus 1433:
1434: debug2("channel %d: decode socks5", c->self);
1.372 markus 1435: p = sshbuf_ptr(input);
1.193 markus 1436: if (p[0] != 0x05)
1437: return -1;
1.372 markus 1438: have = sshbuf_len(input);
1.193 markus 1439: if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1440: /* format: ver | nmethods | methods */
1.198 djm 1441: if (have < 2)
1.193 markus 1442: return 0;
1443: nmethods = p[1];
1444: if (have < nmethods + 2)
1445: return 0;
1446: /* look for method: "NO AUTHENTICATION REQUIRED" */
1.268 stevesk 1447: for (found = 0, i = 2; i < nmethods + 2; i++) {
1.264 stevesk 1448: if (p[i] == SSH_SOCKS5_NOAUTH) {
1.193 markus 1449: found = 1;
1450: break;
1451: }
1452: }
1453: if (!found) {
1454: debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1455: c->self);
1456: return -1;
1457: }
1.403 djm 1458: if ((r = sshbuf_consume(input, nmethods + 2)) != 0)
1459: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1460: /* version, method */
1.372 markus 1461: if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1.403 djm 1462: (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0)
1463: fatal_fr(r, "channel %d: append reply", c->self);
1.193 markus 1464: c->flags |= SSH_SOCKS5_AUTHDONE;
1465: debug2("channel %d: socks5 auth done", c->self);
1466: return 0; /* need more */
1467: }
1468: debug2("channel %d: socks5 post auth", c->self);
1469: if (have < sizeof(s5_req)+1)
1470: return 0; /* need more */
1.247 deraadt 1471: memcpy(&s5_req, p, sizeof(s5_req));
1.193 markus 1472: if (s5_req.version != 0x05 ||
1473: s5_req.command != SSH_SOCKS5_CONNECT ||
1474: s5_req.reserved != 0x00) {
1.194 markus 1475: debug2("channel %d: only socks5 connect supported", c->self);
1.193 markus 1476: return -1;
1477: }
1.213 deraadt 1478: switch (s5_req.atyp){
1.193 markus 1479: case SSH_SOCKS5_IPV4:
1480: addrlen = 4;
1481: af = AF_INET;
1482: break;
1483: case SSH_SOCKS5_DOMAIN:
1484: addrlen = p[sizeof(s5_req)];
1485: af = -1;
1486: break;
1487: case SSH_SOCKS5_IPV6:
1488: addrlen = 16;
1489: af = AF_INET6;
1490: break;
1491: default:
1.194 markus 1492: debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1.193 markus 1493: return -1;
1494: }
1.252 djm 1495: need = sizeof(s5_req) + addrlen + 2;
1496: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1497: need++;
1498: if (have < need)
1.193 markus 1499: return 0;
1.403 djm 1500: if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0)
1501: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1502: if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1503: /* host string length */
1.403 djm 1504: if ((r = sshbuf_consume(input, 1)) != 0)
1505: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1506: }
1.372 markus 1507: if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1508: (r = sshbuf_get(input, &dest_port, 2)) != 0) {
1.403 djm 1509: debug_r(r, "channel %d: parse addr/port", c->self);
1.367 djm 1510: return -1;
1511: }
1.193 markus 1512: dest_addr[addrlen] = '\0';
1.321 djm 1513: free(c->path);
1514: c->path = NULL;
1.293 djm 1515: if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1.294 djm 1516: if (addrlen >= NI_MAXHOST) {
1.293 djm 1517: error("channel %d: dynamic request: socks5 hostname "
1518: "\"%.100s\" too long", c->self, dest_addr);
1519: return -1;
1520: }
1521: c->path = xstrdup(dest_addr);
1522: } else {
1523: if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1524: return -1;
1525: c->path = xstrdup(ntop);
1526: }
1.193 markus 1527: c->host_port = ntohs(dest_port);
1.198 djm 1528:
1.194 markus 1529: debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1.193 markus 1530: c->self, c->path, c->host_port, s5_req.command);
1531:
1532: s5_rsp.version = 0x05;
1533: s5_rsp.command = SSH_SOCKS5_SUCCESS;
1534: s5_rsp.reserved = 0; /* ignored */
1535: s5_rsp.atyp = SSH_SOCKS5_IPV4;
1536: dest_port = 0; /* ignored */
1537:
1.372 markus 1538: if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1539: (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1540: (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
1.403 djm 1541: fatal_fr(r, "channel %d: append reply", c->self);
1.193 markus 1542: return 1;
1.301 dtucker 1543: }
1544:
1545: Channel *
1.367 djm 1546: channel_connect_stdio_fwd(struct ssh *ssh,
1.407 djm 1547: const char *host_to_connect, u_short port_to_connect,
1548: int in, int out, int nonblock)
1.301 dtucker 1549: {
1550: Channel *c;
1551:
1.403 djm 1552: debug_f("%s:%d", host_to_connect, port_to_connect);
1.301 dtucker 1553:
1.367 djm 1554: c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
1.301 dtucker 1555: -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.407 djm 1556: 0, "stdio-forward", nonblock);
1.301 dtucker 1557:
1558: c->path = xstrdup(host_to_connect);
1559: c->host_port = port_to_connect;
1560: c->listening_port = 0;
1561: c->force_drain = 1;
1562:
1.367 djm 1563: channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1564: port_open_helper(ssh, c, "direct-tcpip");
1.301 dtucker 1565:
1566: return c;
1.193 markus 1567: }
1568:
1.104 markus 1569: /* dynamic port forwarding */
1.127 itojun 1570: static void
1.410 djm 1571: channel_pre_dynamic(struct ssh *ssh, Channel *c)
1.104 markus 1572: {
1.367 djm 1573: const u_char *p;
1.217 djm 1574: u_int have;
1575: int ret;
1.103 markus 1576:
1.410 djm 1577: c->io_want = 0;
1.367 djm 1578: have = sshbuf_len(c->input);
1.104 markus 1579: debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.367 djm 1580: /* sshbuf_dump(c->input, stderr); */
1.104 markus 1581: /* check if the fixed size part of the packet is in buffer. */
1.193 markus 1582: if (have < 3) {
1.104 markus 1583: /* need more */
1.410 djm 1584: c->io_want |= SSH_CHAN_IO_RFD;
1.104 markus 1585: return;
1586: }
1587: /* try to guess the protocol */
1.367 djm 1588: p = sshbuf_ptr(c->input);
1589: /* XXX sshbuf_peek_u8? */
1.104 markus 1590: switch (p[0]) {
1591: case 0x04:
1.372 markus 1592: ret = channel_decode_socks4(c, c->input, c->output);
1.193 markus 1593: break;
1594: case 0x05:
1.372 markus 1595: ret = channel_decode_socks5(c, c->input, c->output);
1.104 markus 1596: break;
1597: default:
1598: ret = -1;
1599: break;
1600: }
1601: if (ret < 0) {
1.367 djm 1602: chan_mark_dead(ssh, c);
1.104 markus 1603: } else if (ret == 0) {
1604: debug2("channel %d: pre_dynamic: need more", c->self);
1605: /* need more */
1.410 djm 1606: c->io_want |= SSH_CHAN_IO_RFD;
1.372 markus 1607: if (sshbuf_len(c->output))
1.410 djm 1608: c->io_want |= SSH_CHAN_IO_WFD;
1.104 markus 1609: } else {
1610: /* switch to the next state */
1611: c->type = SSH_CHANNEL_OPENING;
1.367 djm 1612: port_open_helper(ssh, c, "direct-tcpip");
1.104 markus 1613: }
1.103 markus 1614: }
1615:
1.372 markus 1616: /* simulate read-error */
1617: static void
1618: rdynamic_close(struct ssh *ssh, Channel *c)
1619: {
1620: c->type = SSH_CHANNEL_OPEN;
1.423 djm 1621: channel_force_close(ssh, c, 0);
1.372 markus 1622: }
1623:
1624: /* reverse dynamic port forwarding */
1625: static void
1.411 djm 1626: channel_before_prepare_io_rdynamic(struct ssh *ssh, Channel *c)
1.372 markus 1627: {
1628: const u_char *p;
1629: u_int have, len;
1630: int r, ret;
1631:
1632: have = sshbuf_len(c->output);
1633: debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1634: /* sshbuf_dump(c->output, stderr); */
1635: /* EOF received */
1636: if (c->flags & CHAN_EOF_RCVD) {
1.403 djm 1637: if ((r = sshbuf_consume(c->output, have)) != 0)
1638: fatal_fr(r, "channel %d: consume", c->self);
1.372 markus 1639: rdynamic_close(ssh, c);
1640: return;
1641: }
1642: /* check if the fixed size part of the packet is in buffer. */
1643: if (have < 3)
1644: return;
1645: /* try to guess the protocol */
1646: p = sshbuf_ptr(c->output);
1647: switch (p[0]) {
1648: case 0x04:
1649: /* switch input/output for reverse forwarding */
1650: ret = channel_decode_socks4(c, c->output, c->input);
1651: break;
1652: case 0x05:
1653: ret = channel_decode_socks5(c, c->output, c->input);
1654: break;
1655: default:
1656: ret = -1;
1657: break;
1658: }
1659: if (ret < 0) {
1660: rdynamic_close(ssh, c);
1661: } else if (ret == 0) {
1662: debug2("channel %d: pre_rdynamic: need more", c->self);
1663: /* send socks request to peer */
1664: len = sshbuf_len(c->input);
1665: if (len > 0 && len < c->remote_window) {
1666: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1667: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1668: (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1669: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 1670: fatal_fr(r, "channel %i: rdynamic", c->self);
1.372 markus 1671: }
1.403 djm 1672: if ((r = sshbuf_consume(c->input, len)) != 0)
1673: fatal_fr(r, "channel %d: consume", c->self);
1.372 markus 1674: c->remote_window -= len;
1675: }
1676: } else if (rdynamic_connect_finish(ssh, c) < 0) {
1677: /* the connect failed */
1678: rdynamic_close(ssh, c);
1679: }
1680: }
1681:
1.41 markus 1682: /* This is our fake X11 server socket. */
1.127 itojun 1683: static void
1.410 djm 1684: channel_post_x11_listener(struct ssh *ssh, Channel *c)
1.41 markus 1685: {
1.113 markus 1686: Channel *nc;
1.285 djm 1687: struct sockaddr_storage addr;
1.367 djm 1688: int r, newsock, oerrno, remote_port;
1.41 markus 1689: socklen_t addrlen;
1.85 markus 1690: char buf[16384], *remote_ipaddr;
1.25 markus 1691:
1.410 djm 1692: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 1693: return;
1694:
1695: debug("X11 connection requested.");
1696: addrlen = sizeof(addr);
1697: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1698: if (c->single_connection) {
1699: oerrno = errno;
1700: debug2("single_connection: closing X11 listener.");
1.407 djm 1701: channel_close_fd(ssh, c, &c->sock);
1.367 djm 1702: chan_mark_dead(ssh, c);
1703: errno = oerrno;
1704: }
1.393 deraadt 1705: if (newsock == -1) {
1.367 djm 1706: if (errno != EINTR && errno != EWOULDBLOCK &&
1707: errno != ECONNABORTED)
1708: error("accept: %.100s", strerror(errno));
1709: if (errno == EMFILE || errno == ENFILE)
1710: c->notbefore = monotime() + 1;
1711: return;
1712: }
1713: set_nodelay(newsock);
1714: remote_ipaddr = get_peer_ipaddr(newsock);
1715: remote_port = get_peer_port(newsock);
1716: snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1717: remote_ipaddr, remote_port);
1718:
1.424 djm 1719: nc = channel_new(ssh, "x11-connection",
1.367 djm 1720: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1721: c->local_window_max, c->local_maxpacket, 0, buf, 1);
1722: open_preamble(ssh, __func__, nc, "x11");
1.378 djm 1723: if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1724: (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
1.403 djm 1725: fatal_fr(r, "channel %i: reply", c->self);
1.41 markus 1726: }
1.367 djm 1727: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1728: fatal_fr(r, "channel %i: send", c->self);
1.367 djm 1729: free(remote_ipaddr);
1.41 markus 1730: }
1.25 markus 1731:
1.127 itojun 1732: static void
1.367 djm 1733: port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
1.103 markus 1734: {
1.328 djm 1735: char *local_ipaddr = get_local_ipaddr(c->sock);
1.350 djm 1736: int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
1.103 markus 1737: char *remote_ipaddr = get_peer_ipaddr(c->sock);
1.216 markus 1738: int remote_port = get_peer_port(c->sock);
1.367 djm 1739: int r;
1.303 djm 1740:
1741: if (remote_port == -1) {
1742: /* Fake addr/port to appease peers that validate it (Tectia) */
1.321 djm 1743: free(remote_ipaddr);
1.303 djm 1744: remote_ipaddr = xstrdup("127.0.0.1");
1745: remote_port = 65535;
1746: }
1.103 markus 1747:
1.367 djm 1748: free(c->remote_name);
1749: xasprintf(&c->remote_name,
1.103 markus 1750: "%s: listening port %d for %.100s port %d, "
1.328 djm 1751: "connect from %.200s port %d to %.100s port %d",
1.103 markus 1752: rtype, c->listening_port, c->path, c->host_port,
1.328 djm 1753: remote_ipaddr, remote_port, local_ipaddr, local_port);
1.103 markus 1754:
1.367 djm 1755: open_preamble(ssh, __func__, c, rtype);
1.358 djm 1756: if (strcmp(rtype, "direct-tcpip") == 0) {
1757: /* target host, port */
1.367 djm 1758: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1.403 djm 1759: (r = sshpkt_put_u32(ssh, c->host_port)) != 0)
1760: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1761: } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1762: /* target path */
1.403 djm 1763: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1764: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1765: } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1766: /* listen path */
1.403 djm 1767: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1768: fatal_fr(r, "channel %i: reply", c->self);
1.103 markus 1769: } else {
1.358 djm 1770: /* listen address, port */
1.367 djm 1771: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1.403 djm 1772: (r = sshpkt_put_u32(ssh, local_port)) != 0)
1773: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1774: }
1775: if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1776: /* reserved for future owner/mode info */
1.403 djm 1777: if ((r = sshpkt_put_cstring(ssh, "")) != 0)
1778: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1779: } else {
1780: /* originator host and port */
1.367 djm 1781: if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1.403 djm 1782: (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0)
1783: fatal_fr(r, "channel %i: reply", c->self);
1.103 markus 1784: }
1.367 djm 1785: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1786: fatal_fr(r, "channel %i: send", c->self);
1.321 djm 1787: free(remote_ipaddr);
1.328 djm 1788: free(local_ipaddr);
1.103 markus 1789: }
1790:
1.347 djm 1791: void
1.367 djm 1792: channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
1.347 djm 1793: {
1.367 djm 1794: ssh->chanctxt->x11_refuse_time = refuse_time;
1.226 djm 1795: }
1796:
1.41 markus 1797: /*
1798: * This socket is listening for connections to a forwarded TCP/IP port.
1799: */
1.127 itojun 1800: static void
1.410 djm 1801: channel_post_port_listener(struct ssh *ssh, Channel *c)
1.41 markus 1802: {
1.103 markus 1803: Channel *nc;
1.285 djm 1804: struct sockaddr_storage addr;
1.113 markus 1805: int newsock, nextstate;
1.41 markus 1806: socklen_t addrlen;
1.103 markus 1807: char *rtype;
1.73 markus 1808:
1.410 djm 1809: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 1810: return;
1811:
1812: debug("Connection to port %d forwarding to %.100s port %d requested.",
1813: c->listening_port, c->path, c->host_port);
1.103 markus 1814:
1.367 djm 1815: if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1816: nextstate = SSH_CHANNEL_OPENING;
1817: rtype = "forwarded-tcpip";
1818: } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1819: nextstate = SSH_CHANNEL_OPENING;
1820: rtype = "forwarded-streamlocal@openssh.com";
1821: } else if (c->host_port == PORT_STREAMLOCAL) {
1822: nextstate = SSH_CHANNEL_OPENING;
1823: rtype = "direct-streamlocal@openssh.com";
1824: } else if (c->host_port == 0) {
1825: nextstate = SSH_CHANNEL_DYNAMIC;
1826: rtype = "dynamic-tcpip";
1827: } else {
1828: nextstate = SSH_CHANNEL_OPENING;
1829: rtype = "direct-tcpip";
1830: }
1.103 markus 1831:
1.367 djm 1832: addrlen = sizeof(addr);
1833: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.393 deraadt 1834: if (newsock == -1) {
1.367 djm 1835: if (errno != EINTR && errno != EWOULDBLOCK &&
1836: errno != ECONNABORTED)
1837: error("accept: %.100s", strerror(errno));
1838: if (errno == EMFILE || errno == ENFILE)
1839: c->notbefore = monotime() + 1;
1840: return;
1.41 markus 1841: }
1.367 djm 1842: if (c->host_port != PORT_STREAMLOCAL)
1843: set_nodelay(newsock);
1844: nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1845: c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1846: nc->listening_port = c->listening_port;
1847: nc->host_port = c->host_port;
1848: if (c->path != NULL)
1849: nc->path = xstrdup(c->path);
1850:
1851: if (nextstate != SSH_CHANNEL_DYNAMIC)
1852: port_open_helper(ssh, nc, rtype);
1.41 markus 1853: }
1.25 markus 1854:
1.41 markus 1855: /*
1856: * This is the authentication agent socket listening for connections from
1857: * clients.
1858: */
1.127 itojun 1859: static void
1.410 djm 1860: channel_post_auth_listener(struct ssh *ssh, Channel *c)
1.41 markus 1861: {
1.113 markus 1862: Channel *nc;
1.367 djm 1863: int r, newsock;
1.285 djm 1864: struct sockaddr_storage addr;
1.41 markus 1865: socklen_t addrlen;
1.25 markus 1866:
1.410 djm 1867: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 1868: return;
1869:
1870: addrlen = sizeof(addr);
1871: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.393 deraadt 1872: if (newsock == -1) {
1.367 djm 1873: error("accept from auth socket: %.100s", strerror(errno));
1874: if (errno == EMFILE || errno == ENFILE)
1875: c->notbefore = monotime() + 1;
1876: return;
1.41 markus 1877: }
1.424 djm 1878: nc = channel_new(ssh, "agent-connection",
1.367 djm 1879: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1880: c->local_window_max, c->local_maxpacket,
1881: 0, "accepted auth socket", 1);
1882: open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1883: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1884: fatal_fr(r, "channel %i", c->self);
1.41 markus 1885: }
1.25 markus 1886:
1.127 itojun 1887: static void
1.410 djm 1888: channel_post_connecting(struct ssh *ssh, Channel *c)
1.75 markus 1889: {
1.372 markus 1890: int err = 0, sock, isopen, r;
1.129 stevesk 1891: socklen_t sz = sizeof(err);
1.114 markus 1892:
1.410 djm 1893: if ((c->io_ready & SSH_CHAN_IO_SOCK_W) == 0)
1.367 djm 1894: return;
1.368 djm 1895: if (!c->have_remote_id)
1.403 djm 1896: fatal_f("channel %d: no remote id", c->self);
1.372 markus 1897: /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1898: isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
1.393 deraadt 1899: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) == -1) {
1.367 djm 1900: err = errno;
1901: error("getsockopt SO_ERROR failed");
1902: }
1903: if (err == 0) {
1904: debug("channel %d: connected to %s port %d",
1905: c->self, c->connect_ctx.host, c->connect_ctx.port);
1906: channel_connect_ctx_free(&c->connect_ctx);
1907: c->type = SSH_CHANNEL_OPEN;
1.372 markus 1908: if (isopen) {
1909: /* no message necessary */
1910: } else {
1911: if ((r = sshpkt_start(ssh,
1912: SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1913: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1914: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1915: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1.403 djm 1916: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1917: (r = sshpkt_send(ssh)) != 0)
1918: fatal_fr(r, "channel %i open confirm", c->self);
1.367 djm 1919: }
1920: } else {
1921: debug("channel %d: connection failed: %s",
1922: c->self, strerror(err));
1923: /* Try next address, if any */
1924: if ((sock = connect_next(&c->connect_ctx)) > 0) {
1925: close(c->sock);
1926: c->sock = c->rfd = c->wfd = sock;
1927: return;
1928: }
1929: /* Exhausted all addresses */
1930: error("connect_to %.100s port %d: failed.",
1931: c->connect_ctx.host, c->connect_ctx.port);
1932: channel_connect_ctx_free(&c->connect_ctx);
1.372 markus 1933: if (isopen) {
1934: rdynamic_close(ssh, c);
1935: } else {
1936: if ((r = sshpkt_start(ssh,
1937: SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1938: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1.378 djm 1939: (r = sshpkt_put_u32(ssh,
1940: SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1941: (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1.403 djm 1942: (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1943: (r = sshpkt_send(ssh)) != 0)
1944: fatal_fr(r, "channel %i: failure", c->self);
1.372 markus 1945: chan_mark_dead(ssh, c);
1.75 markus 1946: }
1947: }
1948: }
1949:
1.127 itojun 1950: static int
1.410 djm 1951: channel_handle_rfd(struct ssh *ssh, Channel *c)
1.41 markus 1952: {
1.214 markus 1953: char buf[CHAN_RBUF];
1.367 djm 1954: ssize_t len;
1955: int r;
1.412 djm 1956: size_t have, avail, maxlen = CHANNEL_MAX_READ;
1.25 markus 1957:
1.410 djm 1958: if ((c->io_ready & SSH_CHAN_IO_RFD) == 0)
1.412 djm 1959: return 1; /* Shouldn't happen */
1960: if ((avail = sshbuf_avail(c->input)) == 0)
1961: return 1; /* Shouldn't happen */
1962:
1963: /*
1964: * For "simple" channels (i.e. not datagram or filtered), we can
1965: * read directly to the channel buffer.
1966: */
1967: if (c->input_filter == NULL && !c->datagram) {
1968: /* Only OPEN channels have valid rwin */
1969: if (c->type == SSH_CHANNEL_OPEN) {
1970: if ((have = sshbuf_len(c->input)) >= c->remote_window)
1971: return 1; /* shouldn't happen */
1972: if (maxlen > c->remote_window - have)
1973: maxlen = c->remote_window - have;
1974: }
1975: if (maxlen > avail)
1976: maxlen = avail;
1977: if ((r = sshbuf_read(c->rfd, c->input, maxlen, NULL)) != 0) {
1.413 djm 1978: if (errno == EINTR || errno == EAGAIN)
1979: return 1;
1.412 djm 1980: debug2("channel %d: read failed rfd %d maxlen %zu: %s",
1981: c->self, c->rfd, maxlen, ssh_err(r));
1982: goto rfail;
1983: }
1.367 djm 1984: return 1;
1.412 djm 1985: }
1.367 djm 1986:
1987: len = read(c->rfd, buf, sizeof(buf));
1.393 deraadt 1988: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 1989: return 1;
1990: if (len <= 0) {
1.412 djm 1991: debug2("channel %d: read<=0 rfd %d len %zd: %s",
1992: c->self, c->rfd, len,
1993: len == 0 ? "closed" : strerror(errno));
1994: rfail:
1.367 djm 1995: if (c->type != SSH_CHANNEL_OPEN) {
1996: debug2("channel %d: not open", c->self);
1997: chan_mark_dead(ssh, c);
1.41 markus 1998: return -1;
1.65 markus 1999: } else {
1.367 djm 2000: chan_read_failed(ssh, c);
1.65 markus 2001: }
1.367 djm 2002: return -1;
2003: }
2004: if (c->input_filter != NULL) {
2005: if (c->input_filter(ssh, c, buf, len) == -1) {
2006: debug2("channel %d: filter stops", c->self);
2007: chan_read_failed(ssh, c);
2008: }
2009: } else if (c->datagram) {
2010: if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1.403 djm 2011: fatal_fr(r, "channel %i: put datagram", c->self);
1.412 djm 2012: }
1.41 markus 2013: return 1;
2014: }
1.241 deraadt 2015:
1.127 itojun 2016: static int
1.410 djm 2017: channel_handle_wfd(struct ssh *ssh, Channel *c)
1.41 markus 2018: {
1.95 markus 2019: struct termios tio;
1.367 djm 2020: u_char *data = NULL, *buf; /* XXX const; need filter API change */
2021: size_t dlen, olen = 0;
2022: int r, len;
2023:
1.410 djm 2024: if ((c->io_ready & SSH_CHAN_IO_WFD) == 0)
2025: return 1;
2026: if (sshbuf_len(c->output) == 0)
1.367 djm 2027: return 1;
1.25 markus 2028:
1.41 markus 2029: /* Send buffered output data to the socket. */
1.367 djm 2030: olen = sshbuf_len(c->output);
2031: if (c->output_filter != NULL) {
2032: if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
2033: debug2("channel %d: filter stops", c->self);
2034: if (c->type != SSH_CHANNEL_OPEN)
2035: chan_mark_dead(ssh, c);
2036: else
2037: chan_write_failed(ssh, c);
2038: return -1;
1.231 reyk 2039: }
1.367 djm 2040: } else if (c->datagram) {
2041: if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
1.403 djm 2042: fatal_fr(r, "channel %i: get datagram", c->self);
1.369 djm 2043: buf = data;
1.367 djm 2044: } else {
2045: buf = data = sshbuf_mutable_ptr(c->output);
2046: dlen = sshbuf_len(c->output);
2047: }
1.231 reyk 2048:
1.367 djm 2049: if (c->datagram) {
2050: /* ignore truncated writes, datagrams might get lost */
1.374 djm 2051: len = write(c->wfd, buf, dlen);
1.367 djm 2052: free(data);
1.393 deraadt 2053: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.53 markus 2054: return 1;
1.367 djm 2055: if (len <= 0)
2056: goto write_fail;
2057: goto out;
2058: }
2059:
2060: len = write(c->wfd, buf, dlen);
1.393 deraadt 2061: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2062: return 1;
2063: if (len <= 0) {
2064: write_fail:
2065: if (c->type != SSH_CHANNEL_OPEN) {
2066: debug2("channel %d: not open", c->self);
2067: chan_mark_dead(ssh, c);
1.41 markus 2068: return -1;
1.367 djm 2069: } else {
2070: chan_write_failed(ssh, c);
1.91 markus 2071: }
1.367 djm 2072: return -1;
2073: }
2074: if (c->isatty && dlen >= 1 && buf[0] != '\r') {
2075: if (tcgetattr(c->wfd, &tio) == 0 &&
2076: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
2077: /*
2078: * Simulate echo to reduce the impact of
2079: * traffic analysis. We need to match the
2080: * size of a SSH2_MSG_CHANNEL_DATA message
2081: * (4 byte channel id + buf)
2082: */
2083: if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2084: (r = sshpkt_send(ssh)) != 0)
1.403 djm 2085: fatal_fr(r, "channel %i: ignore", c->self);
1.25 markus 2086: }
1.367 djm 2087: }
1.403 djm 2088: if ((r = sshbuf_consume(c->output, len)) != 0)
2089: fatal_fr(r, "channel %i: consume", c->self);
1.309 djm 2090: out:
1.367 djm 2091: c->local_consumed += olen - sshbuf_len(c->output);
2092:
2093: return 1;
2094: }
2095:
2096: static int
1.410 djm 2097: channel_handle_efd_write(struct ssh *ssh, Channel *c)
1.367 djm 2098: {
2099: int r;
2100: ssize_t len;
2101:
1.410 djm 2102: if ((c->io_ready & SSH_CHAN_IO_EFD_W) == 0)
2103: return 1;
2104: if (sshbuf_len(c->extended) == 0)
1.367 djm 2105: return 1;
2106:
2107: len = write(c->efd, sshbuf_ptr(c->extended),
2108: sshbuf_len(c->extended));
2109: debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
1.393 deraadt 2110: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2111: return 1;
2112: if (len <= 0) {
2113: debug2("channel %d: closing write-efd %d", c->self, c->efd);
1.407 djm 2114: channel_close_fd(ssh, c, &c->efd);
1.367 djm 2115: } else {
1.403 djm 2116: if ((r = sshbuf_consume(c->extended, len)) != 0)
2117: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2118: c->local_consumed += len;
2119: }
1.44 markus 2120: return 1;
2121: }
1.241 deraadt 2122:
1.127 itojun 2123: static int
1.410 djm 2124: channel_handle_efd_read(struct ssh *ssh, Channel *c)
1.44 markus 2125: {
1.214 markus 2126: char buf[CHAN_RBUF];
1.367 djm 2127: int r;
2128: ssize_t len;
2129:
1.410 djm 2130: if ((c->io_ready & SSH_CHAN_IO_EFD_R) == 0)
1.367 djm 2131: return 1;
1.44 markus 2132:
1.367 djm 2133: len = read(c->efd, buf, sizeof(buf));
2134: debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
1.393 deraadt 2135: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2136: return 1;
2137: if (len <= 0) {
1.403 djm 2138: debug2("channel %d: closing read-efd %d", c->self, c->efd);
1.407 djm 2139: channel_close_fd(ssh, c, &c->efd);
1.403 djm 2140: } else if (c->extended_usage == CHAN_EXTENDED_IGNORE)
2141: debug3("channel %d: discard efd", c->self);
2142: else if ((r = sshbuf_put(c->extended, buf, len)) != 0)
2143: fatal_fr(r, "channel %i: append", c->self);
1.44 markus 2144: return 1;
2145: }
1.241 deraadt 2146:
1.127 itojun 2147: static int
1.410 djm 2148: channel_handle_efd(struct ssh *ssh, Channel *c)
1.44 markus 2149: {
1.367 djm 2150: if (c->efd == -1)
2151: return 1;
2152:
2153: /** XXX handle drain efd, too */
2154:
2155: if (c->extended_usage == CHAN_EXTENDED_WRITE)
1.410 djm 2156: return channel_handle_efd_write(ssh, c);
1.367 djm 2157: else if (c->extended_usage == CHAN_EXTENDED_READ ||
2158: c->extended_usage == CHAN_EXTENDED_IGNORE)
1.410 djm 2159: return channel_handle_efd_read(ssh, c);
1.367 djm 2160:
2161: return 1;
2162: }
2163:
2164: static int
2165: channel_check_window(struct ssh *ssh, Channel *c)
2166: {
2167: int r;
2168:
1.104 markus 2169: if (c->type == SSH_CHANNEL_OPEN &&
2170: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.270 dtucker 2171: ((c->local_window_max - c->local_window >
1.269 markus 2172: c->local_maxpacket*3) ||
2173: c->local_window < c->local_window_max/2) &&
1.44 markus 2174: c->local_consumed > 0) {
1.368 djm 2175: if (!c->have_remote_id)
1.403 djm 2176: fatal_f("channel %d: no remote id", c->self);
1.367 djm 2177: if ((r = sshpkt_start(ssh,
2178: SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2179: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2180: (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2181: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 2182: fatal_fr(r, "channel %i", c->self);
1.367 djm 2183: }
1.403 djm 2184: debug2("channel %d: window %d sent adjust %d", c->self,
2185: c->local_window, c->local_consumed);
1.44 markus 2186: c->local_window += c->local_consumed;
2187: c->local_consumed = 0;
1.1 deraadt 2188: }
1.41 markus 2189: return 1;
1.1 deraadt 2190: }
2191:
1.127 itojun 2192: static void
1.410 djm 2193: channel_post_open(struct ssh *ssh, Channel *c)
1.41 markus 2194: {
1.410 djm 2195: channel_handle_rfd(ssh, c);
2196: channel_handle_wfd(ssh, c);
2197: channel_handle_efd(ssh, c);
1.367 djm 2198: channel_check_window(ssh, c);
1.44 markus 2199: }
2200:
1.302 djm 2201: static u_int
1.367 djm 2202: read_mux(struct ssh *ssh, Channel *c, u_int need)
1.302 djm 2203: {
2204: char buf[CHAN_RBUF];
1.367 djm 2205: ssize_t len;
1.302 djm 2206: u_int rlen;
1.367 djm 2207: int r;
1.302 djm 2208:
1.367 djm 2209: if (sshbuf_len(c->input) < need) {
2210: rlen = need - sshbuf_len(c->input);
1.352 deraadt 2211: len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
1.393 deraadt 2212: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2213: return sshbuf_len(c->input);
1.302 djm 2214: if (len <= 0) {
1.367 djm 2215: debug2("channel %d: ctl read<=0 rfd %d len %zd",
1.349 naddy 2216: c->self, c->rfd, len);
1.367 djm 2217: chan_read_failed(ssh, c);
1.349 naddy 2218: return 0;
1.403 djm 2219: } else if ((r = sshbuf_put(c->input, buf, len)) != 0)
2220: fatal_fr(r, "channel %i: append", c->self);
1.302 djm 2221: }
1.367 djm 2222: return sshbuf_len(c->input);
1.302 djm 2223: }
2224:
2225: static void
1.410 djm 2226: channel_post_mux_client_read(struct ssh *ssh, Channel *c)
1.302 djm 2227: {
2228: u_int need;
2229:
1.410 djm 2230: if ((c->io_ready & SSH_CHAN_IO_RFD) == 0)
1.367 djm 2231: return;
2232: if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2233: return;
2234: if (c->mux_pause)
2235: return;
2236:
2237: /*
2238: * Don't not read past the precise end of packets to
2239: * avoid disrupting fd passing.
2240: */
2241: if (read_mux(ssh, c, 4) < 4) /* read header */
2242: return;
2243: /* XXX sshbuf_peek_u32 */
2244: need = PEEK_U32(sshbuf_ptr(c->input));
1.302 djm 2245: #define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1.367 djm 2246: if (need > CHANNEL_MUX_MAX_PACKET) {
2247: debug2("channel %d: packet too big %u > %u",
2248: c->self, CHANNEL_MUX_MAX_PACKET, need);
2249: chan_rcvd_oclose(ssh, c);
2250: return;
2251: }
2252: if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2253: return;
2254: if (c->mux_rcb(ssh, c) != 0) {
2255: debug("channel %d: mux_rcb failed", c->self);
2256: chan_mark_dead(ssh, c);
2257: return;
1.302 djm 2258: }
1.367 djm 2259: }
2260:
2261: static void
1.410 djm 2262: channel_post_mux_client_write(struct ssh *ssh, Channel *c)
1.367 djm 2263: {
2264: ssize_t len;
2265: int r;
1.302 djm 2266:
1.410 djm 2267: if ((c->io_ready & SSH_CHAN_IO_WFD) == 0)
2268: return;
2269: if (sshbuf_len(c->output) == 0)
1.367 djm 2270: return;
2271:
2272: len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
1.393 deraadt 2273: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2274: return;
2275: if (len <= 0) {
2276: chan_mark_dead(ssh, c);
2277: return;
1.302 djm 2278: }
1.367 djm 2279: if ((r = sshbuf_consume(c->output, len)) != 0)
1.403 djm 2280: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2281: }
2282:
2283: static void
1.410 djm 2284: channel_post_mux_client(struct ssh *ssh, Channel *c)
1.367 djm 2285: {
1.410 djm 2286: channel_post_mux_client_read(ssh, c);
2287: channel_post_mux_client_write(ssh, c);
1.302 djm 2288: }
2289:
2290: static void
1.410 djm 2291: channel_post_mux_listener(struct ssh *ssh, Channel *c)
1.302 djm 2292: {
2293: Channel *nc;
2294: struct sockaddr_storage addr;
2295: socklen_t addrlen;
2296: int newsock;
2297: uid_t euid;
2298: gid_t egid;
2299:
1.410 djm 2300: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.302 djm 2301: return;
2302:
2303: debug("multiplexing control connection");
2304:
2305: /*
2306: * Accept connection on control socket
2307: */
2308: memset(&addr, 0, sizeof(addr));
2309: addrlen = sizeof(addr);
2310: if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2311: &addrlen)) == -1) {
1.403 djm 2312: error_f("accept: %s", strerror(errno));
1.317 djm 2313: if (errno == EMFILE || errno == ENFILE)
1.322 dtucker 2314: c->notbefore = monotime() + 1;
1.302 djm 2315: return;
2316: }
2317:
1.393 deraadt 2318: if (getpeereid(newsock, &euid, &egid) == -1) {
1.403 djm 2319: error_f("getpeereid failed: %s", strerror(errno));
1.302 djm 2320: close(newsock);
2321: return;
2322: }
2323: if ((euid != 0) && (getuid() != euid)) {
2324: error("multiplex uid mismatch: peer euid %u != uid %u",
2325: (u_int)euid, (u_int)getuid());
2326: close(newsock);
2327: return;
2328: }
1.424 djm 2329: nc = channel_new(ssh, "mux-control", SSH_CHANNEL_MUX_CLIENT,
1.302 djm 2330: newsock, newsock, -1, c->local_window_max,
2331: c->local_maxpacket, 0, "mux-control", 1);
2332: nc->mux_rcb = c->mux_rcb;
1.403 djm 2333: debug3_f("new mux channel %d fd %d", nc->self, nc->sock);
1.302 djm 2334: /* establish state */
1.367 djm 2335: nc->mux_rcb(ssh, nc);
1.302 djm 2336: /* mux state transitions must not elicit protocol messages */
2337: nc->flags |= CHAN_LOCAL;
2338: }
2339:
1.127 itojun 2340: static void
1.367 djm 2341: channel_handler_init(struct ssh_channels *sc)
1.1 deraadt 2342: {
1.367 djm 2343: chan_fn **pre, **post;
2344:
2345: if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
1.406 djm 2346: (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
1.403 djm 2347: fatal_f("allocation failed");
1.367 djm 2348:
2349: pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2350: pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2351: pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2352: pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2353: pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2354: pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2355: pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2356: pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2357: pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2358: pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.372 markus 2359: pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
1.367 djm 2360: pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2361: pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2362:
2363: post[SSH_CHANNEL_OPEN] = &channel_post_open;
2364: post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2365: post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2366: post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2367: post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2368: post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2369: post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2370: post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2371: post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.372 markus 2372: post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
1.367 djm 2373: post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2374: post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
1.180 deraadt 2375:
1.367 djm 2376: sc->channel_pre = pre;
2377: sc->channel_post = post;
1.44 markus 2378: }
2379:
1.140 markus 2380: /* gc dead channels */
2381: static void
1.367 djm 2382: channel_garbage_collect(struct ssh *ssh, Channel *c)
1.140 markus 2383: {
2384: if (c == NULL)
2385: return;
2386: if (c->detach_user != NULL) {
1.367 djm 2387: if (!chan_is_dead(ssh, c, c->detach_close))
1.140 markus 2388: return;
1.385 djm 2389:
1.194 markus 2390: debug2("channel %d: gc: notify user", c->self);
1.423 djm 2391: c->detach_user(ssh, c->self, 0, NULL);
1.140 markus 2392: /* if we still have a callback */
2393: if (c->detach_user != NULL)
2394: return;
1.194 markus 2395: debug2("channel %d: gc: user detached", c->self);
1.140 markus 2396: }
1.367 djm 2397: if (!chan_is_dead(ssh, c, 1))
1.140 markus 2398: return;
1.194 markus 2399: debug2("channel %d: garbage collecting", c->self);
1.367 djm 2400: channel_free(ssh, c);
1.140 markus 2401: }
2402:
1.367 djm 2403: enum channel_table { CHAN_PRE, CHAN_POST };
2404:
1.127 itojun 2405: static void
1.422 djm 2406: channel_handler(struct ssh *ssh, int table, struct timespec *timeout)
1.41 markus 2407: {
1.367 djm 2408: struct ssh_channels *sc = ssh->chanctxt;
2409: chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
1.299 markus 2410: u_int i, oalloc;
1.41 markus 2411: Channel *c;
1.317 djm 2412: time_t now;
1.25 markus 2413:
1.322 dtucker 2414: now = monotime();
1.367 djm 2415: for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2416: c = sc->channels[i];
1.113 markus 2417: if (c == NULL)
1.41 markus 2418: continue;
1.417 djm 2419: /* Try to keep IO going while rekeying */
2420: if (ssh_packet_is_rekeying(ssh) && c->type != SSH_CHANNEL_OPEN)
2421: continue;
1.299 markus 2422: if (c->delayed) {
1.367 djm 2423: if (table == CHAN_PRE)
1.299 markus 2424: c->delayed = 0;
2425: else
2426: continue;
2427: }
1.317 djm 2428: if (ftab[c->type] != NULL) {
2429: /*
2430: * Run handlers that are not paused.
2431: */
2432: if (c->notbefore <= now)
1.410 djm 2433: (*ftab[c->type])(ssh, c);
1.422 djm 2434: else if (timeout != NULL) {
1.317 djm 2435: /*
1.422 djm 2436: * Arrange for poll wakeup when channel pause
2437: * timer expires.
1.317 djm 2438: */
1.422 djm 2439: ptimeout_deadline_monotime(timeout,
2440: c->notbefore);
1.317 djm 2441: }
2442: }
1.367 djm 2443: channel_garbage_collect(ssh, c);
1.1 deraadt 2444: }
2445: }
2446:
1.121 markus 2447: /*
1.411 djm 2448: * Create sockets before preparing IO.
1.372 markus 2449: * This is necessary for things that need to happen after reading
1.411 djm 2450: * the network-input but need to be completed before IO event setup, e.g.
2451: * because they may create new channels.
1.372 markus 2452: */
2453: static void
1.411 djm 2454: channel_before_prepare_io(struct ssh *ssh)
1.372 markus 2455: {
2456: struct ssh_channels *sc = ssh->chanctxt;
2457: Channel *c;
2458: u_int i, oalloc;
2459:
2460: for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2461: c = sc->channels[i];
2462: if (c == NULL)
2463: continue;
2464: if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
1.411 djm 2465: channel_before_prepare_io_rdynamic(ssh, c);
2466: }
2467: }
2468:
2469: static void
2470: dump_channel_poll(const char *func, const char *what, Channel *c,
2471: u_int pollfd_offset, struct pollfd *pfd)
2472: {
2473: #ifdef DEBUG_CHANNEL_POLL
1.415 djm 2474: debug3("%s: channel %d: %s r%d w%d e%d s%d c->pfds [ %d %d %d %d ] "
2475: "io_want 0x%02x io_ready 0x%02x pfd[%u].fd=%d "
2476: "pfd.ev 0x%02x pfd.rev 0x%02x", func, c->self, what,
2477: c->rfd, c->wfd, c->efd, c->sock,
2478: c->pfds[0], c->pfds[1], c->pfds[2], c->pfds[3],
2479: c->io_want, c->io_ready,
2480: pollfd_offset, pfd->fd, pfd->events, pfd->revents);
1.411 djm 2481: #endif
2482: }
2483:
2484: /* Prepare pollfd entries for a single channel */
2485: static void
2486: channel_prepare_pollfd(Channel *c, u_int *next_pollfd,
2487: struct pollfd *pfd, u_int npfd)
2488: {
1.415 djm 2489: u_int ev, p = *next_pollfd;
1.411 djm 2490:
2491: if (c == NULL)
2492: return;
2493: if (p + 4 > npfd) {
2494: /* Shouldn't happen */
2495: fatal_f("channel %d: bad pfd offset %u (max %u)",
2496: c->self, p, npfd);
2497: }
1.415 djm 2498: c->pfds[0] = c->pfds[1] = c->pfds[2] = c->pfds[3] = -1;
1.411 djm 2499: /*
2500: * prepare c->rfd
2501: *
2502: * This is a special case, since c->rfd might be the same as
2503: * c->wfd, c->efd and/or c->sock. Handle those here if they want
2504: * IO too.
2505: */
2506: if (c->rfd != -1) {
1.415 djm 2507: ev = 0;
1.411 djm 2508: if ((c->io_want & SSH_CHAN_IO_RFD) != 0)
1.415 djm 2509: ev |= POLLIN;
1.411 djm 2510: /* rfd == wfd */
1.415 djm 2511: if (c->wfd == c->rfd) {
2512: if ((c->io_want & SSH_CHAN_IO_WFD) != 0)
2513: ev |= POLLOUT;
2514: }
1.411 djm 2515: /* rfd == efd */
1.415 djm 2516: if (c->efd == c->rfd) {
2517: if ((c->io_want & SSH_CHAN_IO_EFD_R) != 0)
2518: ev |= POLLIN;
2519: if ((c->io_want & SSH_CHAN_IO_EFD_W) != 0)
2520: ev |= POLLOUT;
2521: }
1.411 djm 2522: /* rfd == sock */
1.415 djm 2523: if (c->sock == c->rfd) {
2524: if ((c->io_want & SSH_CHAN_IO_SOCK_R) != 0)
2525: ev |= POLLIN;
2526: if ((c->io_want & SSH_CHAN_IO_SOCK_W) != 0)
2527: ev |= POLLOUT;
2528: }
2529: /* Pack a pfd entry if any event armed for this fd */
2530: if (ev != 0) {
2531: c->pfds[0] = p;
2532: pfd[p].fd = c->rfd;
2533: pfd[p].events = ev;
2534: dump_channel_poll(__func__, "rfd", c, p, &pfd[p]);
2535: p++;
2536: }
1.411 djm 2537: }
1.415 djm 2538: /* prepare c->wfd if wanting IO and not already handled above */
1.411 djm 2539: if (c->wfd != -1 && c->rfd != c->wfd) {
1.415 djm 2540: ev = 0;
2541: if ((c->io_want & SSH_CHAN_IO_WFD))
2542: ev |= POLLOUT;
2543: /* Pack a pfd entry if any event armed for this fd */
2544: if (ev != 0) {
2545: c->pfds[1] = p;
2546: pfd[p].fd = c->wfd;
2547: pfd[p].events = ev;
2548: dump_channel_poll(__func__, "wfd", c, p, &pfd[p]);
2549: p++;
2550: }
1.411 djm 2551: }
1.415 djm 2552: /* prepare c->efd if wanting IO and not already handled above */
1.411 djm 2553: if (c->efd != -1 && c->rfd != c->efd) {
1.415 djm 2554: ev = 0;
1.411 djm 2555: if ((c->io_want & SSH_CHAN_IO_EFD_R) != 0)
1.415 djm 2556: ev |= POLLIN;
1.411 djm 2557: if ((c->io_want & SSH_CHAN_IO_EFD_W) != 0)
1.415 djm 2558: ev |= POLLOUT;
2559: /* Pack a pfd entry if any event armed for this fd */
2560: if (ev != 0) {
2561: c->pfds[2] = p;
2562: pfd[p].fd = c->efd;
2563: pfd[p].events = ev;
2564: dump_channel_poll(__func__, "efd", c, p, &pfd[p]);
2565: p++;
2566: }
1.411 djm 2567: }
1.415 djm 2568: /* prepare c->sock if wanting IO and not already handled above */
1.411 djm 2569: if (c->sock != -1 && c->rfd != c->sock) {
1.415 djm 2570: ev = 0;
1.411 djm 2571: if ((c->io_want & SSH_CHAN_IO_SOCK_R) != 0)
1.415 djm 2572: ev |= POLLIN;
1.411 djm 2573: if ((c->io_want & SSH_CHAN_IO_SOCK_W) != 0)
1.415 djm 2574: ev |= POLLOUT;
2575: /* Pack a pfd entry if any event armed for this fd */
2576: if (ev != 0) {
2577: c->pfds[3] = p;
2578: pfd[p].fd = c->sock;
2579: pfd[p].events = 0;
2580: dump_channel_poll(__func__, "sock", c, p, &pfd[p]);
2581: p++;
2582: }
1.372 markus 2583: }
1.411 djm 2584: *next_pollfd = p;
1.372 markus 2585: }
2586:
1.411 djm 2587: /* * Allocate/prepare poll structure */
1.49 markus 2588: void
1.411 djm 2589: channel_prepare_poll(struct ssh *ssh, struct pollfd **pfdp, u_int *npfd_allocp,
1.422 djm 2590: u_int *npfd_activep, u_int npfd_reserved, struct timespec *timeout)
1.41 markus 2591: {
1.410 djm 2592: struct ssh_channels *sc = ssh->chanctxt;
1.411 djm 2593: u_int i, oalloc, p, npfd = npfd_reserved;
2594:
2595: channel_before_prepare_io(ssh); /* might create a new channel */
1.416 djm 2596: /* clear out I/O flags from last poll */
2597: for (i = 0; i < sc->channels_alloc; i++) {
2598: if (sc->channels[i] == NULL)
2599: continue;
2600: sc->channels[i]->io_want = sc->channels[i]->io_ready = 0;
2601: }
1.411 djm 2602: /* Allocate 4x pollfd for each channel (rfd, wfd, efd, sock) */
2603: if (sc->channels_alloc >= (INT_MAX / 4) - npfd_reserved)
2604: fatal_f("too many channels"); /* shouldn't happen */
1.417 djm 2605: npfd += sc->channels_alloc * 4;
1.411 djm 2606: if (npfd > *npfd_allocp) {
2607: *pfdp = xrecallocarray(*pfdp, *npfd_allocp,
2608: npfd, sizeof(**pfdp));
2609: *npfd_allocp = npfd;
2610: }
2611: *npfd_activep = npfd_reserved;
2612: oalloc = sc->channels_alloc;
1.372 markus 2613:
1.422 djm 2614: channel_handler(ssh, CHAN_PRE, timeout);
1.84 markus 2615:
1.411 djm 2616: if (oalloc != sc->channels_alloc) {
2617: /* shouldn't happen */
2618: fatal_f("channels_alloc changed during CHAN_PRE "
2619: "(was %u, now %u)", oalloc, sc->channels_alloc);
2620: }
1.84 markus 2621:
1.411 djm 2622: /* Prepare pollfd */
2623: p = npfd_reserved;
2624: for (i = 0; i < sc->channels_alloc; i++)
2625: channel_prepare_pollfd(sc->channels[i], &p, *pfdp, npfd);
2626: *npfd_activep = p;
2627: }
2628:
2629: static void
1.415 djm 2630: fd_ready(Channel *c, int p, struct pollfd *pfds, u_int npfd, int fd,
1.411 djm 2631: const char *what, u_int revents_mask, u_int ready)
2632: {
2633: struct pollfd *pfd = &pfds[p];
1.410 djm 2634:
1.411 djm 2635: if (fd == -1)
2636: return;
1.415 djm 2637: if (p == -1 || (u_int)p >= npfd)
2638: fatal_f("channel %d: bad pfd %d (max %u)", c->self, p, npfd);
1.411 djm 2639: dump_channel_poll(__func__, what, c, p, pfd);
2640: if (pfd->fd != fd) {
2641: fatal("channel %d: inconsistent %s fd=%d pollfd[%u].fd %d "
2642: "r%d w%d e%d s%d", c->self, what, fd, p, pfd->fd,
2643: c->rfd, c->wfd, c->efd, c->sock);
2644: }
2645: if ((pfd->revents & POLLNVAL) != 0) {
2646: fatal("channel %d: invalid %s pollfd[%u].fd %d r%d w%d e%d s%d",
2647: c->self, what, p, pfd->fd, c->rfd, c->wfd, c->efd, c->sock);
1.410 djm 2648: }
1.411 djm 2649: if ((pfd->revents & (revents_mask|POLLHUP|POLLERR)) != 0)
2650: c->io_ready |= ready & c->io_want;
1.41 markus 2651: }
2652:
1.121 markus 2653: /*
1.411 djm 2654: * After poll, perform any appropriate operations for channels which have
1.121 markus 2655: * events pending.
2656: */
1.49 markus 2657: void
1.411 djm 2658: channel_after_poll(struct ssh *ssh, struct pollfd *pfd, u_int npfd)
1.41 markus 2659: {
1.410 djm 2660: struct ssh_channels *sc = ssh->chanctxt;
1.415 djm 2661: u_int i;
2662: int p;
1.410 djm 2663: Channel *c;
2664:
1.411 djm 2665: #ifdef DEBUG_CHANNEL_POLL
1.415 djm 2666: for (p = 0; p < (int)npfd; p++) {
1.411 djm 2667: if (pfd[p].revents == 0)
2668: continue;
2669: debug_f("pfd[%u].fd %d rev 0x%04x",
2670: p, pfd[p].fd, pfd[p].revents);
2671: }
2672: #endif
2673:
2674: /* Convert pollfd into c->io_ready */
2675: for (i = 0; i < sc->channels_alloc; i++) {
1.410 djm 2676: c = sc->channels[i];
1.415 djm 2677: if (c == NULL)
1.410 djm 2678: continue;
1.411 djm 2679: /* if rfd is shared with efd/sock then wfd should be too */
2680: if (c->rfd != -1 && c->wfd != -1 && c->rfd != c->wfd &&
2681: (c->rfd == c->efd || c->rfd == c->sock)) {
2682: /* Shouldn't happen */
2683: fatal_f("channel %d: unexpected fds r%d w%d e%d s%d",
2684: c->self, c->rfd, c->wfd, c->efd, c->sock);
2685: }
1.410 djm 2686: c->io_ready = 0;
1.411 djm 2687: /* rfd, potentially shared with wfd, efd and sock */
1.415 djm 2688: if (c->rfd != -1 && (p = c->pfds[0]) != -1) {
2689: fd_ready(c, p, pfd, npfd, c->rfd,
2690: "rfd", POLLIN, SSH_CHAN_IO_RFD);
1.411 djm 2691: if (c->rfd == c->wfd) {
1.415 djm 2692: fd_ready(c, p, pfd, npfd, c->wfd,
2693: "wfd/r", POLLOUT, SSH_CHAN_IO_WFD);
1.411 djm 2694: }
2695: if (c->rfd == c->efd) {
1.415 djm 2696: fd_ready(c, p, pfd, npfd, c->efd,
2697: "efdr/r", POLLIN, SSH_CHAN_IO_EFD_R);
2698: fd_ready(c, p, pfd, npfd, c->efd,
2699: "efdw/r", POLLOUT, SSH_CHAN_IO_EFD_W);
1.411 djm 2700: }
2701: if (c->rfd == c->sock) {
1.415 djm 2702: fd_ready(c, p, pfd, npfd, c->sock,
2703: "sockr/r", POLLIN, SSH_CHAN_IO_SOCK_R);
2704: fd_ready(c, p, pfd, npfd, c->sock,
2705: "sockw/r", POLLOUT, SSH_CHAN_IO_SOCK_W);
1.411 djm 2706: }
1.415 djm 2707: dump_channel_poll(__func__, "rfd", c, p, pfd);
1.411 djm 2708: }
2709: /* wfd */
1.415 djm 2710: if (c->wfd != -1 && c->wfd != c->rfd &&
2711: (p = c->pfds[1]) != -1) {
2712: fd_ready(c, p, pfd, npfd, c->wfd,
2713: "wfd", POLLOUT, SSH_CHAN_IO_WFD);
2714: dump_channel_poll(__func__, "wfd", c, p, pfd);
1.411 djm 2715: }
2716: /* efd */
1.415 djm 2717: if (c->efd != -1 && c->efd != c->rfd &&
2718: (p = c->pfds[2]) != -1) {
2719: fd_ready(c, p, pfd, npfd, c->efd,
2720: "efdr", POLLIN, SSH_CHAN_IO_EFD_R);
2721: fd_ready(c, p, pfd, npfd, c->efd,
2722: "efdw", POLLOUT, SSH_CHAN_IO_EFD_W);
2723: dump_channel_poll(__func__, "efd", c, p, pfd);
1.411 djm 2724: }
2725: /* sock */
1.415 djm 2726: if (c->sock != -1 && c->sock != c->rfd &&
2727: (p = c->pfds[3]) != -1) {
2728: fd_ready(c, p, pfd, npfd, c->sock,
2729: "sockr", POLLIN, SSH_CHAN_IO_SOCK_R);
2730: fd_ready(c, p, pfd, npfd, c->sock,
2731: "sockw", POLLOUT, SSH_CHAN_IO_SOCK_W);
2732: dump_channel_poll(__func__, "sock", c, p, pfd);
1.411 djm 2733: }
1.410 djm 2734: }
2735: channel_handler(ssh, CHAN_POST, NULL);
1.367 djm 2736: }
2737:
2738: /*
2739: * Enqueue data for channels with open or draining c->input.
2740: */
2741: static void
2742: channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2743: {
1.373 djm 2744: size_t len, plen;
2745: const u_char *pkt;
1.367 djm 2746: int r;
2747:
2748: if ((len = sshbuf_len(c->input)) == 0) {
2749: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2750: /*
2751: * input-buffer is empty and read-socket shutdown:
2752: * tell peer, that we will not send more data:
2753: * send IEOF.
2754: * hack for extended data: delay EOF if EFD still
2755: * in use.
2756: */
2757: if (CHANNEL_EFD_INPUT_ACTIVE(c))
2758: debug2("channel %d: "
2759: "ibuf_empty delayed efd %d/(%zu)",
2760: c->self, c->efd, sshbuf_len(c->extended));
2761: else
2762: chan_ibuf_empty(ssh, c);
2763: }
2764: return;
2765: }
2766:
1.368 djm 2767: if (!c->have_remote_id)
1.403 djm 2768: fatal_f("channel %d: no remote id", c->self);
1.368 djm 2769:
1.367 djm 2770: if (c->datagram) {
2771: /* Check datagram will fit; drop if not */
1.373 djm 2772: if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
1.403 djm 2773: fatal_fr(r, "channel %i: get datagram", c->self);
1.367 djm 2774: /*
2775: * XXX this does tail-drop on the datagram queue which is
2776: * usually suboptimal compared to head-drop. Better to have
2777: * backpressure at read time? (i.e. read + discard)
2778: */
1.373 djm 2779: if (plen > c->remote_window || plen > c->remote_maxpacket) {
1.367 djm 2780: debug("channel %d: datagram too big", c->self);
2781: return;
2782: }
2783: /* Enqueue it */
2784: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2785: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1.373 djm 2786: (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
1.403 djm 2787: (r = sshpkt_send(ssh)) != 0)
2788: fatal_fr(r, "channel %i: send datagram", c->self);
1.373 djm 2789: c->remote_window -= plen;
1.367 djm 2790: return;
2791: }
2792:
2793: /* Enqueue packet for buffered data. */
2794: if (len > c->remote_window)
2795: len = c->remote_window;
2796: if (len > c->remote_maxpacket)
2797: len = c->remote_maxpacket;
2798: if (len == 0)
2799: return;
2800: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2801: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2802: (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
1.403 djm 2803: (r = sshpkt_send(ssh)) != 0)
2804: fatal_fr(r, "channel %i: send data", c->self);
1.367 djm 2805: if ((r = sshbuf_consume(c->input, len)) != 0)
1.403 djm 2806: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2807: c->remote_window -= len;
1.41 markus 2808: }
2809:
1.367 djm 2810: /*
2811: * Enqueue data for channels with open c->extended in read mode.
2812: */
2813: static void
2814: channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2815: {
2816: size_t len;
2817: int r;
2818:
2819: if ((len = sshbuf_len(c->extended)) == 0)
2820: return;
2821:
2822: debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2823: c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2824: if (len > c->remote_window)
2825: len = c->remote_window;
2826: if (len > c->remote_maxpacket)
2827: len = c->remote_maxpacket;
2828: if (len == 0)
2829: return;
1.368 djm 2830: if (!c->have_remote_id)
1.403 djm 2831: fatal_f("channel %d: no remote id", c->self);
1.367 djm 2832: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2833: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2834: (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2835: (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
1.403 djm 2836: (r = sshpkt_send(ssh)) != 0)
2837: fatal_fr(r, "channel %i: data", c->self);
1.367 djm 2838: if ((r = sshbuf_consume(c->extended, len)) != 0)
1.403 djm 2839: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2840: c->remote_window -= len;
2841: debug2("channel %d: sent ext data %zu", c->self, len);
2842: }
1.121 markus 2843:
1.84 markus 2844: /* If there is data to send to the connection, enqueue some of it now. */
1.49 markus 2845: void
1.367 djm 2846: channel_output_poll(struct ssh *ssh)
1.1 deraadt 2847: {
1.367 djm 2848: struct ssh_channels *sc = ssh->chanctxt;
1.41 markus 2849: Channel *c;
1.367 djm 2850: u_int i;
1.1 deraadt 2851:
1.367 djm 2852: for (i = 0; i < sc->channels_alloc; i++) {
2853: c = sc->channels[i];
1.113 markus 2854: if (c == NULL)
2855: continue;
1.37 markus 2856:
1.121 markus 2857: /*
2858: * We are only interested in channels that can have buffered
2859: * incoming data.
2860: */
1.358 djm 2861: if (c->type != SSH_CHANNEL_OPEN)
2862: continue;
2863: if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 2864: /* XXX is this true? */
1.367 djm 2865: debug3("channel %d: will not send data after close",
2866: c->self);
1.44 markus 2867: continue;
2868: }
1.25 markus 2869:
2870: /* Get the amount of buffered data for this channel. */
1.367 djm 2871: if (c->istate == CHAN_INPUT_OPEN ||
2872: c->istate == CHAN_INPUT_WAIT_DRAIN)
2873: channel_output_poll_input_open(ssh, c);
1.44 markus 2874: /* Send extended data, i.e. stderr */
1.358 djm 2875: if (!(c->flags & CHAN_EOF_SENT) &&
1.367 djm 2876: c->extended_usage == CHAN_EXTENDED_READ)
2877: channel_output_poll_extended_read(ssh, c);
1.25 markus 2878: }
1.1 deraadt 2879: }
2880:
1.354 markus 2881: /* -- mux proxy support */
2882:
2883: /*
2884: * When multiplexing channel messages for mux clients we have to deal
2885: * with downstream messages from the mux client and upstream messages
2886: * from the ssh server:
2887: * 1) Handling downstream messages is straightforward and happens
2888: * in channel_proxy_downstream():
2889: * - We forward all messages (mostly) unmodified to the server.
2890: * - However, in order to route messages from upstream to the correct
2891: * downstream client, we have to replace the channel IDs used by the
2892: * mux clients with a unique channel ID because the mux clients might
2893: * use conflicting channel IDs.
2894: * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2895: * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2896: * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2897: * with the newly allocated channel ID.
2898: * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
1.380 djm 2899: * channels and processed by channel_proxy_upstream(). The local channel ID
1.354 markus 2900: * is then translated back to the original mux client ID.
2901: * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2902: * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2903: * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2904: * downstream mux client are removed.
2905: * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2906: * requires more work, because they are not addressed to a specific
2907: * channel. E.g. client_request_forwarded_tcpip() needs to figure
2908: * out whether the request is addressed to the local client or a
2909: * specific downstream client based on the listen-address/port.
1.380 djm 2910: * 6) Agent and X11-Forwarding have a similar problem and are currently
1.354 markus 2911: * not supported as the matching session/channel cannot be identified
2912: * easily.
2913: */
2914:
2915: /*
2916: * receive packets from downstream mux clients:
2917: * channel callback fired on read from mux client, creates
2918: * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2919: * on channel creation.
2920: */
2921: int
1.367 djm 2922: channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
1.354 markus 2923: {
2924: Channel *c = NULL;
2925: struct sshbuf *original = NULL, *modified = NULL;
2926: const u_char *cp;
2927: char *ctype = NULL, *listen_host = NULL;
2928: u_char type;
2929: size_t have;
1.370 djm 2930: int ret = -1, r;
1.355 djm 2931: u_int id, remote_id, listen_port;
1.354 markus 2932:
1.367 djm 2933: /* sshbuf_dump(downstream->input, stderr); */
2934: if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
1.354 markus 2935: != 0) {
1.403 djm 2936: error_fr(r, "parse");
1.354 markus 2937: return -1;
2938: }
2939: if (have < 2) {
1.403 djm 2940: error_f("short message");
1.354 markus 2941: return -1;
2942: }
2943: type = cp[1];
2944: /* skip padlen + type */
2945: cp += 2;
2946: have -= 2;
2947: if (ssh_packet_log_type(type))
1.403 djm 2948: debug3_f("channel %u: down->up: type %u",
1.354 markus 2949: downstream->self, type);
2950:
2951: switch (type) {
2952: case SSH2_MSG_CHANNEL_OPEN:
2953: if ((original = sshbuf_from(cp, have)) == NULL ||
2954: (modified = sshbuf_new()) == NULL) {
1.403 djm 2955: error_f("alloc");
1.354 markus 2956: goto out;
2957: }
2958: if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2959: (r = sshbuf_get_u32(original, &id)) != 0) {
1.403 djm 2960: error_fr(r, "parse");
1.354 markus 2961: goto out;
2962: }
1.424 djm 2963: c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
1.406 djm 2964: -1, -1, -1, 0, 0, 0, ctype, 1);
1.354 markus 2965: c->mux_ctx = downstream; /* point to mux client */
2966: c->mux_downstream_id = id; /* original downstream id */
2967: if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2968: (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2969: (r = sshbuf_putb(modified, original)) != 0) {
1.403 djm 2970: error_fr(r, "compose");
1.367 djm 2971: channel_free(ssh, c);
1.354 markus 2972: goto out;
2973: }
2974: break;
2975: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2976: /*
2977: * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2978: * need to parse 'remote_id' instead of 'ctype'.
2979: */
2980: if ((original = sshbuf_from(cp, have)) == NULL ||
2981: (modified = sshbuf_new()) == NULL) {
1.403 djm 2982: error_f("alloc");
1.354 markus 2983: goto out;
2984: }
2985: if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2986: (r = sshbuf_get_u32(original, &id)) != 0) {
1.403 djm 2987: error_fr(r, "parse");
1.354 markus 2988: goto out;
2989: }
1.424 djm 2990: c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
1.406 djm 2991: -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
1.354 markus 2992: c->mux_ctx = downstream; /* point to mux client */
2993: c->mux_downstream_id = id;
2994: c->remote_id = remote_id;
1.368 djm 2995: c->have_remote_id = 1;
1.354 markus 2996: if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2997: (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2998: (r = sshbuf_putb(modified, original)) != 0) {
1.403 djm 2999: error_fr(r, "compose");
1.367 djm 3000: channel_free(ssh, c);
1.354 markus 3001: goto out;
3002: }
3003: break;
3004: case SSH2_MSG_GLOBAL_REQUEST:
3005: if ((original = sshbuf_from(cp, have)) == NULL) {
1.403 djm 3006: error_f("alloc");
1.354 markus 3007: goto out;
3008: }
3009: if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
1.403 djm 3010: error_fr(r, "parse");
1.354 markus 3011: goto out;
3012: }
3013: if (strcmp(ctype, "tcpip-forward") != 0) {
1.403 djm 3014: error_f("unsupported request %s", ctype);
1.354 markus 3015: goto out;
3016: }
3017: if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
3018: (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
3019: (r = sshbuf_get_u32(original, &listen_port)) != 0) {
1.403 djm 3020: error_fr(r, "parse");
1.354 markus 3021: goto out;
3022: }
1.355 djm 3023: if (listen_port > 65535) {
1.403 djm 3024: error_f("tcpip-forward for %s: bad port %u",
3025: listen_host, listen_port);
1.355 djm 3026: goto out;
3027: }
1.354 markus 3028: /* Record that connection to this host/port is permitted. */
1.381 djm 3029: permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1,
1.367 djm 3030: listen_host, NULL, (int)listen_port, downstream);
1.354 markus 3031: listen_host = NULL;
3032: break;
3033: case SSH2_MSG_CHANNEL_CLOSE:
3034: if (have < 4)
3035: break;
3036: remote_id = PEEK_U32(cp);
1.367 djm 3037: if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
1.354 markus 3038: if (c->flags & CHAN_CLOSE_RCVD)
1.367 djm 3039: channel_free(ssh, c);
1.354 markus 3040: else
3041: c->flags |= CHAN_CLOSE_SENT;
3042: }
3043: break;
3044: }
3045: if (modified) {
3046: if ((r = sshpkt_start(ssh, type)) != 0 ||
3047: (r = sshpkt_putb(ssh, modified)) != 0 ||
3048: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 3049: error_fr(r, "send");
1.354 markus 3050: goto out;
3051: }
3052: } else {
3053: if ((r = sshpkt_start(ssh, type)) != 0 ||
3054: (r = sshpkt_put(ssh, cp, have)) != 0 ||
3055: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 3056: error_fr(r, "send");
1.354 markus 3057: goto out;
3058: }
3059: }
3060: ret = 0;
3061: out:
3062: free(ctype);
3063: free(listen_host);
3064: sshbuf_free(original);
3065: sshbuf_free(modified);
3066: return ret;
3067: }
3068:
3069: /*
3070: * receive packets from upstream server and de-multiplex packets
3071: * to correct downstream:
3072: * implemented as a helper for channel input handlers,
3073: * replaces local (proxy) channel ID with downstream channel ID.
3074: */
3075: int
1.363 markus 3076: channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
1.354 markus 3077: {
3078: struct sshbuf *b = NULL;
3079: Channel *downstream;
3080: const u_char *cp = NULL;
3081: size_t len;
3082: int r;
3083:
3084: /*
3085: * When receiving packets from the peer we need to check whether we
3086: * need to forward the packets to the mux client. In this case we
1.380 djm 3087: * restore the original channel id and keep track of CLOSE messages,
1.354 markus 3088: * so we can cleanup the channel.
3089: */
3090: if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
3091: return 0;
3092: if ((downstream = c->mux_ctx) == NULL)
3093: return 0;
3094: switch (type) {
3095: case SSH2_MSG_CHANNEL_CLOSE:
3096: case SSH2_MSG_CHANNEL_DATA:
3097: case SSH2_MSG_CHANNEL_EOF:
3098: case SSH2_MSG_CHANNEL_EXTENDED_DATA:
3099: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
3100: case SSH2_MSG_CHANNEL_OPEN_FAILURE:
3101: case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
3102: case SSH2_MSG_CHANNEL_SUCCESS:
3103: case SSH2_MSG_CHANNEL_FAILURE:
3104: case SSH2_MSG_CHANNEL_REQUEST:
3105: break;
3106: default:
1.403 djm 3107: debug2_f("channel %u: unsupported type %u", c->self, type);
1.354 markus 3108: return 0;
3109: }
3110: if ((b = sshbuf_new()) == NULL) {
1.403 djm 3111: error_f("alloc reply");
1.354 markus 3112: goto out;
3113: }
3114: /* get remaining payload (after id) */
3115: cp = sshpkt_ptr(ssh, &len);
3116: if (cp == NULL) {
1.403 djm 3117: error_f("no packet");
1.354 markus 3118: goto out;
3119: }
3120: /* translate id and send to muxclient */
3121: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
3122: (r = sshbuf_put_u8(b, type)) != 0 ||
3123: (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
3124: (r = sshbuf_put(b, cp, len)) != 0 ||
1.367 djm 3125: (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
1.403 djm 3126: error_fr(r, "compose muxclient");
1.354 markus 3127: goto out;
3128: }
3129: /* sshbuf_dump(b, stderr); */
3130: if (ssh_packet_log_type(type))
1.403 djm 3131: debug3_f("channel %u: up->down: type %u", c->self, type);
1.354 markus 3132: out:
3133: /* update state */
3134: switch (type) {
3135: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
3136: /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
1.368 djm 3137: if (cp && len > 4) {
1.354 markus 3138: c->remote_id = PEEK_U32(cp);
1.368 djm 3139: c->have_remote_id = 1;
3140: }
1.354 markus 3141: break;
3142: case SSH2_MSG_CHANNEL_CLOSE:
3143: if (c->flags & CHAN_CLOSE_SENT)
1.367 djm 3144: channel_free(ssh, c);
1.354 markus 3145: else
3146: c->flags |= CHAN_CLOSE_RCVD;
3147: break;
3148: }
3149: sshbuf_free(b);
3150: return 1;
3151: }
1.121 markus 3152:
3153: /* -- protocol input */
1.249 djm 3154:
1.367 djm 3155: /* Parse a channel ID from the current packet */
3156: static int
3157: channel_parse_id(struct ssh *ssh, const char *where, const char *what)
3158: {
3159: u_int32_t id;
3160: int r;
3161:
3162: if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
1.403 djm 3163: error_r(r, "%s: parse id", where);
1.367 djm 3164: ssh_packet_disconnect(ssh, "Invalid %s message", what);
3165: }
3166: if (id > INT_MAX) {
1.403 djm 3167: error_r(r, "%s: bad channel id %u", where, id);
1.367 djm 3168: ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
3169: }
3170: return (int)id;
3171: }
3172:
3173: /* Lookup a channel from an ID in the current packet */
3174: static Channel *
3175: channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
3176: {
3177: int id = channel_parse_id(ssh, where, what);
3178: Channel *c;
3179:
3180: if ((c = channel_lookup(ssh, id)) == NULL) {
3181: ssh_packet_disconnect(ssh,
3182: "%s packet referred to nonexistent channel %d", what, id);
3183: }
3184: return c;
3185: }
3186:
1.339 markus 3187: int
1.363 markus 3188: channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3189: {
1.332 djm 3190: const u_char *data;
1.367 djm 3191: size_t data_len, win_len;
3192: Channel *c = channel_from_packet_id(ssh, __func__, "data");
3193: int r;
1.25 markus 3194:
1.363 markus 3195: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3196: return 0;
1.25 markus 3197:
3198: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 3199: if (c->type != SSH_CHANNEL_OPEN &&
1.372 markus 3200: c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
3201: c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
1.41 markus 3202: c->type != SSH_CHANNEL_X11_OPEN)
1.339 markus 3203: return 0;
1.37 markus 3204:
1.25 markus 3205: /* Get the data. */
1.389 djm 3206: if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3207: (r = sshpkt_get_end(ssh)) != 0)
1.403 djm 3208: fatal_fr(r, "channel %i: get data", c->self);
1.367 djm 3209:
1.309 djm 3210: win_len = data_len;
3211: if (c->datagram)
3212: win_len += 4; /* string length header */
1.200 markus 3213:
3214: /*
1.367 djm 3215: * The sending side reduces its window as it sends data, so we
3216: * must 'fake' consumption of the data in order to ensure that window
3217: * updates are sent back. Otherwise the connection might deadlock.
1.200 markus 3218: */
1.358 djm 3219: if (c->ostate != CHAN_OUTPUT_OPEN) {
3220: c->local_window -= win_len;
3221: c->local_consumed += win_len;
1.339 markus 3222: return 0;
1.200 markus 3223: }
1.41 markus 3224:
1.358 djm 3225: if (win_len > c->local_maxpacket) {
1.367 djm 3226: logit("channel %d: rcvd big packet %zu, maxpack %u",
1.358 djm 3227: c->self, win_len, c->local_maxpacket);
1.367 djm 3228: return 0;
1.358 djm 3229: }
3230: if (win_len > c->local_window) {
1.367 djm 3231: logit("channel %d: rcvd too much data %zu, win %u",
1.358 djm 3232: c->self, win_len, c->local_window);
3233: return 0;
1.44 markus 3234: }
1.358 djm 3235: c->local_window -= win_len;
3236:
1.367 djm 3237: if (c->datagram) {
3238: if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
1.403 djm 3239: fatal_fr(r, "channel %i: append datagram", c->self);
1.367 djm 3240: } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
1.403 djm 3241: fatal_fr(r, "channel %i: append data", c->self);
1.367 djm 3242:
1.339 markus 3243: return 0;
1.1 deraadt 3244: }
1.121 markus 3245:
1.339 markus 3246: int
1.363 markus 3247: channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
1.44 markus 3248: {
1.367 djm 3249: const u_char *data;
3250: size_t data_len;
3251: u_int32_t tcode;
3252: Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3253: int r;
1.44 markus 3254:
1.363 markus 3255: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3256: return 0;
1.44 markus 3257: if (c->type != SSH_CHANNEL_OPEN) {
1.367 djm 3258: logit("channel %d: ext data for non open", c->self);
1.339 markus 3259: return 0;
1.172 markus 3260: }
3261: if (c->flags & CHAN_EOF_RCVD) {
1.404 djm 3262: if (ssh->compat & SSH_BUG_EXTEOF)
1.367 djm 3263: debug("channel %d: accepting ext data after eof",
3264: c->self);
1.172 markus 3265: else
1.367 djm 3266: ssh_packet_disconnect(ssh, "Received extended_data "
3267: "after EOF on channel %d.", c->self);
3268: }
3269:
3270: if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
1.403 djm 3271: error_fr(r, "parse tcode");
1.367 djm 3272: ssh_packet_disconnect(ssh, "Invalid extended_data message");
1.44 markus 3273: }
3274: if (c->efd == -1 ||
3275: c->extended_usage != CHAN_EXTENDED_WRITE ||
3276: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 3277: logit("channel %d: bad ext data", c->self);
1.339 markus 3278: return 0;
1.44 markus 3279: }
1.389 djm 3280: if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3281: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3282: error_fr(r, "parse data");
1.367 djm 3283: ssh_packet_disconnect(ssh, "Invalid extended_data message");
3284: }
3285:
1.44 markus 3286: if (data_len > c->local_window) {
1.367 djm 3287: logit("channel %d: rcvd too much extended_data %zu, win %u",
1.44 markus 3288: c->self, data_len, c->local_window);
1.339 markus 3289: return 0;
1.44 markus 3290: }
1.367 djm 3291: debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3292: /* XXX sshpkt_getb? */
3293: if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
1.403 djm 3294: error_fr(r, "append");
1.44 markus 3295: c->local_window -= data_len;
1.339 markus 3296: return 0;
1.44 markus 3297: }
3298:
1.339 markus 3299: int
1.363 markus 3300: channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
1.41 markus 3301: {
1.367 djm 3302: Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
1.389 djm 3303: int r;
1.367 djm 3304:
1.389 djm 3305: if ((r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3306: error_fr(r, "parse data");
1.389 djm 3307: ssh_packet_disconnect(ssh, "Invalid ieof message");
3308: }
1.41 markus 3309:
1.363 markus 3310: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3311: return 0;
1.367 djm 3312: chan_rcvd_ieof(ssh, c);
1.133 markus 3313:
3314: /* XXX force input close */
1.156 markus 3315: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 3316: debug("channel %d: FORCE input drain", c->self);
3317: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.367 djm 3318: if (sshbuf_len(c->input) == 0)
3319: chan_ibuf_empty(ssh, c);
1.133 markus 3320: }
1.339 markus 3321: return 0;
1.41 markus 3322: }
1.1 deraadt 3323:
1.339 markus 3324: int
1.363 markus 3325: channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
1.41 markus 3326: {
1.367 djm 3327: Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
1.389 djm 3328: int r;
1.151 markus 3329:
1.363 markus 3330: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3331: return 0;
1.389 djm 3332: if ((r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3333: error_fr(r, "parse data");
1.389 djm 3334: ssh_packet_disconnect(ssh, "Invalid oclose message");
3335: }
1.367 djm 3336: chan_rcvd_oclose(ssh, c);
1.339 markus 3337: return 0;
1.1 deraadt 3338: }
3339:
1.339 markus 3340: int
1.363 markus 3341: channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3342: {
1.367 djm 3343: Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3344: u_int32_t remote_window, remote_maxpacket;
3345: int r;
1.25 markus 3346:
1.363 markus 3347: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3348: return 0;
3349: if (c->type != SSH_CHANNEL_OPENING)
1.389 djm 3350: ssh_packet_disconnect(ssh, "Received open confirmation for "
1.367 djm 3351: "non-opening channel %d.", c->self);
3352: /*
3353: * Record the remote channel number and mark that the channel
3354: * is now open.
3355: */
1.368 djm 3356: if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3357: (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
1.389 djm 3358: (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 ||
3359: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3360: error_fr(r, "window/maxpacket");
1.389 djm 3361: ssh_packet_disconnect(ssh, "Invalid open confirmation message");
1.367 djm 3362: }
3363:
1.368 djm 3364: c->have_remote_id = 1;
1.367 djm 3365: c->remote_window = remote_window;
3366: c->remote_maxpacket = remote_maxpacket;
1.41 markus 3367: c->type = SSH_CHANNEL_OPEN;
1.358 djm 3368: if (c->open_confirm) {
1.403 djm 3369: debug2_f("channel %d: callback start", c->self);
1.367 djm 3370: c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
1.403 djm 3371: debug2_f("channel %d: callback done", c->self);
1.44 markus 3372: }
1.358 djm 3373: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3374: c->remote_window, c->remote_maxpacket);
1.339 markus 3375: return 0;
1.1 deraadt 3376: }
3377:
1.127 itojun 3378: static char *
1.114 markus 3379: reason2txt(int reason)
3380: {
1.143 deraadt 3381: switch (reason) {
1.114 markus 3382: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3383: return "administratively prohibited";
3384: case SSH2_OPEN_CONNECT_FAILED:
3385: return "connect failed";
3386: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3387: return "unknown channel type";
3388: case SSH2_OPEN_RESOURCE_SHORTAGE:
3389: return "resource shortage";
3390: }
1.117 stevesk 3391: return "unknown reason";
1.114 markus 3392: }
3393:
1.339 markus 3394: int
1.363 markus 3395: channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3396: {
1.367 djm 3397: Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3398: u_int32_t reason;
3399: char *msg = NULL;
3400: int r;
1.25 markus 3401:
1.363 markus 3402: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3403: return 0;
3404: if (c->type != SSH_CHANNEL_OPENING)
1.389 djm 3405: ssh_packet_disconnect(ssh, "Received open failure for "
1.367 djm 3406: "non-opening channel %d.", c->self);
3407: if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
1.403 djm 3408: error_fr(r, "parse reason");
1.389 djm 3409: ssh_packet_disconnect(ssh, "Invalid open failure message");
1.367 djm 3410: }
1.378 djm 3411: /* skip language */
3412: if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
1.389 djm 3413: (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 ||
3414: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3415: error_fr(r, "parse msg/lang");
1.389 djm 3416: ssh_packet_disconnect(ssh, "Invalid open failure message");
1.358 djm 3417: }
1.367 djm 3418: logit("channel %d: open failed: %s%s%s", c->self,
1.358 djm 3419: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3420: free(msg);
3421: if (c->open_confirm) {
1.403 djm 3422: debug2_f("channel %d: callback start", c->self);
1.367 djm 3423: c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
1.403 djm 3424: debug2_f("channel %d: callback done", c->self);
1.44 markus 3425: }
1.291 djm 3426: /* Schedule the channel for cleanup/deletion. */
1.367 djm 3427: chan_mark_dead(ssh, c);
1.339 markus 3428: return 0;
1.44 markus 3429: }
3430:
1.339 markus 3431: int
1.363 markus 3432: channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
1.121 markus 3433: {
1.367 djm 3434: int id = channel_parse_id(ssh, __func__, "window adjust");
1.121 markus 3435: Channel *c;
1.367 djm 3436: u_int32_t adjust;
3437: u_int new_rwin;
3438: int r;
1.1 deraadt 3439:
1.367 djm 3440: if ((c = channel_lookup(ssh, id)) == NULL) {
1.229 markus 3441: logit("Received window adjust for non-open channel %d.", id);
1.339 markus 3442: return 0;
1.372 markus 3443: }
1.367 djm 3444:
1.363 markus 3445: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3446: return 0;
1.389 djm 3447: if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 ||
3448: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3449: error_fr(r, "parse adjust");
1.389 djm 3450: ssh_packet_disconnect(ssh, "Invalid window adjust message");
1.367 djm 3451: }
3452: debug2("channel %d: rcvd adjust %u", c->self, adjust);
3453: if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
1.346 djm 3454: fatal("channel %d: adjust %u overflows remote window %u",
1.367 djm 3455: c->self, adjust, c->remote_window);
3456: }
3457: c->remote_window = new_rwin;
1.339 markus 3458: return 0;
1.121 markus 3459: }
1.1 deraadt 3460:
1.339 markus 3461: int
1.363 markus 3462: channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
1.275 djm 3463: {
1.367 djm 3464: int id = channel_parse_id(ssh, __func__, "status confirm");
1.275 djm 3465: Channel *c;
3466: struct channel_confirm *cc;
3467:
3468: /* Reset keepalive timeout */
1.389 djm 3469: ssh_packet_set_alive_timeouts(ssh, 0);
1.275 djm 3470:
1.403 djm 3471: debug2_f("type %d id %d", type, id);
1.275 djm 3472:
1.367 djm 3473: if ((c = channel_lookup(ssh, id)) == NULL) {
1.403 djm 3474: logit_f("%d: unknown", id);
1.339 markus 3475: return 0;
1.367 djm 3476: }
1.363 markus 3477: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3478: return 0;
1.394 dtucker 3479: if (sshpkt_get_end(ssh) != 0)
1.389 djm 3480: ssh_packet_disconnect(ssh, "Invalid status confirm message");
1.275 djm 3481: if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
1.339 markus 3482: return 0;
1.367 djm 3483: cc->cb(ssh, type, c, cc->ctx);
1.275 djm 3484: TAILQ_REMOVE(&c->status_confirms, cc, entry);
1.396 jsg 3485: freezero(cc, sizeof(*cc));
1.339 markus 3486: return 0;
1.275 djm 3487: }
1.121 markus 3488:
3489: /* -- tcp forwarding */
1.135 markus 3490:
3491: void
1.367 djm 3492: channel_set_af(struct ssh *ssh, int af)
1.135 markus 3493: {
1.367 djm 3494: ssh->chanctxt->IPv4or6 = af;
1.135 markus 3495: }
1.121 markus 3496:
1.312 djm 3497:
3498: /*
3499: * Determine whether or not a port forward listens to loopback, the
3500: * specified address or wildcard. On the client, a specified bind
3501: * address will always override gateway_ports. On the server, a
3502: * gateway_ports of 1 (``yes'') will override the client's specification
3503: * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3504: * will bind to whatever address the client asked for.
3505: *
3506: * Special-case listen_addrs are:
3507: *
3508: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3509: * "" (empty string), "*" -> wildcard v4/v6
3510: * "localhost" -> loopback v4/v6
1.334 djm 3511: * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
1.312 djm 3512: */
3513: static const char *
1.389 djm 3514: channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp,
1.336 millert 3515: int is_client, struct ForwardOptions *fwd_opts)
1.312 djm 3516: {
3517: const char *addr = NULL;
3518: int wildcard = 0;
3519:
3520: if (listen_addr == NULL) {
3521: /* No address specified: default to gateway_ports setting */
1.336 millert 3522: if (fwd_opts->gateway_ports)
1.312 djm 3523: wildcard = 1;
1.336 millert 3524: } else if (fwd_opts->gateway_ports || is_client) {
1.404 djm 3525: if (((ssh->compat & SSH_OLD_FORWARD_ADDR) &&
1.312 djm 3526: strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3527: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
1.336 millert 3528: (!is_client && fwd_opts->gateway_ports == 1)) {
1.312 djm 3529: wildcard = 1;
1.326 djm 3530: /*
3531: * Notify client if they requested a specific listen
3532: * address and it was overridden.
3533: */
3534: if (*listen_addr != '\0' &&
3535: strcmp(listen_addr, "0.0.0.0") != 0 &&
3536: strcmp(listen_addr, "*") != 0) {
1.389 djm 3537: ssh_packet_send_debug(ssh,
3538: "Forwarding listen address "
1.326 djm 3539: "\"%s\" overridden by server "
3540: "GatewayPorts", listen_addr);
3541: }
1.334 djm 3542: } else if (strcmp(listen_addr, "localhost") != 0 ||
3543: strcmp(listen_addr, "127.0.0.1") == 0 ||
3544: strcmp(listen_addr, "::1") == 0) {
1.395 djm 3545: /*
3546: * Accept explicit localhost address when
3547: * GatewayPorts=yes. The "localhost" hostname is
3548: * deliberately skipped here so it will listen on all
3549: * available local address families.
3550: */
1.334 djm 3551: addr = listen_addr;
1.326 djm 3552: }
1.334 djm 3553: } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3554: strcmp(listen_addr, "::1") == 0) {
3555: /*
3556: * If a specific IPv4/IPv6 localhost address has been
3557: * requested then accept it even if gateway_ports is in
3558: * effect. This allows the client to prefer IPv4 or IPv6.
3559: */
3560: addr = listen_addr;
1.312 djm 3561: }
3562: if (wildcardp != NULL)
3563: *wildcardp = wildcard;
3564: return addr;
3565: }
3566:
1.160 markus 3567: static int
1.367 djm 3568: channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3569: struct Forward *fwd, int *allocated_listen_port,
3570: struct ForwardOptions *fwd_opts)
1.25 markus 3571: {
1.113 markus 3572: Channel *c;
1.226 djm 3573: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 3574: struct addrinfo hints, *ai, *aitop;
1.212 djm 3575: const char *host, *addr;
1.35 markus 3576: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.295 djm 3577: in_port_t *lport_p;
1.25 markus 3578:
1.212 djm 3579: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 3580:
1.344 millert 3581: if (is_client && fwd->connect_path != NULL) {
3582: host = fwd->connect_path;
3583: } else {
3584: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3585: fwd->listen_host : fwd->connect_host;
3586: if (host == NULL) {
3587: error("No forward host name.");
3588: return 0;
3589: }
3590: if (strlen(host) >= NI_MAXHOST) {
3591: error("Forward host name too long.");
3592: return 0;
3593: }
1.87 markus 3594: }
1.25 markus 3595:
1.312 djm 3596: /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
1.389 djm 3597: addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard,
1.336 millert 3598: is_client, fwd_opts);
1.403 djm 3599: debug3_f("type %d wildcard %d addr %s", type, wildcard,
3600: (addr == NULL) ? "NULL" : addr);
1.212 djm 3601:
3602: /*
1.35 markus 3603: * getaddrinfo returns a loopback address if the hostname is
3604: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 3605: */
1.35 markus 3606: memset(&hints, 0, sizeof(hints));
1.367 djm 3607: hints.ai_family = ssh->chanctxt->IPv4or6;
1.212 djm 3608: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 3609: hints.ai_socktype = SOCK_STREAM;
1.336 millert 3610: snprintf(strport, sizeof strport, "%d", fwd->listen_port);
1.212 djm 3611: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3612: if (addr == NULL) {
3613: /* This really shouldn't happen */
1.389 djm 3614: ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s",
1.271 dtucker 3615: ssh_gai_strerror(r));
1.212 djm 3616: } else {
1.403 djm 3617: error_f("getaddrinfo(%.64s): %s", addr,
1.271 dtucker 3618: ssh_gai_strerror(r));
1.212 djm 3619: }
1.218 markus 3620: return 0;
1.212 djm 3621: }
1.295 djm 3622: if (allocated_listen_port != NULL)
3623: *allocated_listen_port = 0;
1.35 markus 3624: for (ai = aitop; ai; ai = ai->ai_next) {
1.295 djm 3625: switch (ai->ai_family) {
3626: case AF_INET:
3627: lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3628: sin_port;
3629: break;
3630: case AF_INET6:
3631: lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3632: sin6_port;
3633: break;
3634: default:
1.35 markus 3635: continue;
1.295 djm 3636: }
3637: /*
3638: * If allocating a port for -R forwards, then use the
3639: * same port for all address families.
3640: */
1.367 djm 3641: if (type == SSH_CHANNEL_RPORT_LISTENER &&
3642: fwd->listen_port == 0 && allocated_listen_port != NULL &&
3643: *allocated_listen_port > 0)
1.295 djm 3644: *lport_p = htons(*allocated_listen_port);
3645:
1.35 markus 3646: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1.367 djm 3647: strport, sizeof(strport),
3648: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.403 djm 3649: error_f("getnameinfo failed");
1.35 markus 3650: continue;
3651: }
3652: /* Create a port to listen for the host. */
1.300 dtucker 3653: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.393 deraadt 3654: if (sock == -1) {
1.35 markus 3655: /* this is no error since kernel may not support ipv6 */
1.377 djm 3656: verbose("socket [%s]:%s: %.100s", ntop, strport,
3657: strerror(errno));
1.35 markus 3658: continue;
3659: }
1.226 djm 3660:
1.376 djm 3661: set_reuseaddr(sock);
1.182 stevesk 3662:
1.295 djm 3663: debug("Local forwarding listening on %s port %s.",
3664: ntop, strport);
1.35 markus 3665:
3666: /* Bind the socket to the address. */
1.393 deraadt 3667: if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.367 djm 3668: /*
3669: * address can be in if use ipv6 address is
3670: * already bound
3671: */
1.377 djm 3672: verbose("bind [%s]:%s: %.100s",
3673: ntop, strport, strerror(errno));
1.35 markus 3674: close(sock);
3675: continue;
3676: }
3677: /* Start listening for connections on the socket. */
1.393 deraadt 3678: if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
1.377 djm 3679: error("listen [%s]:%s: %.100s", ntop, strport,
3680: strerror(errno));
1.35 markus 3681: close(sock);
3682: continue;
3683: }
1.295 djm 3684:
3685: /*
1.336 millert 3686: * fwd->listen_port == 0 requests a dynamically allocated port -
1.295 djm 3687: * record what we got.
3688: */
1.367 djm 3689: if (type == SSH_CHANNEL_RPORT_LISTENER &&
3690: fwd->listen_port == 0 &&
1.295 djm 3691: allocated_listen_port != NULL &&
3692: *allocated_listen_port == 0) {
1.350 djm 3693: *allocated_listen_port = get_local_port(sock);
1.295 djm 3694: debug("Allocated listen port %d",
3695: *allocated_listen_port);
3696: }
3697:
1.35 markus 3698: /* Allocate a channel number for the socket. */
1.424 djm 3699: c = channel_new(ssh, "port-listener", type, sock, sock, -1,
1.51 markus 3700: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 3701: 0, "port listener", 1);
1.293 djm 3702: c->path = xstrdup(host);
1.336 millert 3703: c->host_port = fwd->connect_port;
1.312 djm 3704: c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
1.336 millert 3705: if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
1.404 djm 3706: !(ssh->compat & SSH_BUG_DYNAMIC_RPORT))
1.315 markus 3707: c->listening_port = *allocated_listen_port;
3708: else
1.336 millert 3709: c->listening_port = fwd->listen_port;
1.35 markus 3710: success = 1;
3711: }
3712: if (success == 0)
1.403 djm 3713: error_f("cannot listen to port: %d", fwd->listen_port);
1.35 markus 3714: freeaddrinfo(aitop);
1.87 markus 3715: return success;
1.25 markus 3716: }
1.1 deraadt 3717:
1.336 millert 3718: static int
1.367 djm 3719: channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3720: struct Forward *fwd, struct ForwardOptions *fwd_opts)
1.336 millert 3721: {
3722: struct sockaddr_un sunaddr;
3723: const char *path;
3724: Channel *c;
3725: int port, sock;
3726: mode_t omask;
3727:
3728: switch (type) {
3729: case SSH_CHANNEL_UNIX_LISTENER:
3730: if (fwd->connect_path != NULL) {
3731: if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3732: error("Local connecting path too long: %s",
3733: fwd->connect_path);
3734: return 0;
3735: }
3736: path = fwd->connect_path;
3737: port = PORT_STREAMLOCAL;
3738: } else {
3739: if (fwd->connect_host == NULL) {
3740: error("No forward host name.");
3741: return 0;
3742: }
3743: if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3744: error("Forward host name too long.");
3745: return 0;
3746: }
3747: path = fwd->connect_host;
3748: port = fwd->connect_port;
3749: }
3750: break;
3751: case SSH_CHANNEL_RUNIX_LISTENER:
3752: path = fwd->listen_path;
3753: port = PORT_STREAMLOCAL;
3754: break;
3755: default:
1.403 djm 3756: error_f("unexpected channel type %d", type);
1.336 millert 3757: return 0;
3758: }
3759:
3760: if (fwd->listen_path == NULL) {
3761: error("No forward path name.");
3762: return 0;
3763: }
3764: if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3765: error("Local listening path too long: %s", fwd->listen_path);
3766: return 0;
3767: }
3768:
1.403 djm 3769: debug3_f("type %d path %s", type, fwd->listen_path);
1.336 millert 3770:
3771: /* Start a Unix domain listener. */
3772: omask = umask(fwd_opts->streamlocal_bind_mask);
3773: sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3774: fwd_opts->streamlocal_bind_unlink);
3775: umask(omask);
3776: if (sock < 0)
3777: return 0;
3778:
3779: debug("Local forwarding listening on path %s.", fwd->listen_path);
3780:
3781: /* Allocate a channel number for the socket. */
1.424 djm 3782: c = channel_new(ssh, "unix-listener", type, sock, sock, -1,
1.336 millert 3783: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3784: 0, "unix listener", 1);
3785: c->path = xstrdup(path);
3786: c->host_port = port;
3787: c->listening_port = PORT_STREAMLOCAL;
3788: c->listening_addr = xstrdup(fwd->listen_path);
3789: return 1;
3790: }
3791:
3792: static int
1.367 djm 3793: channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3794: const char *host, u_short port)
1.202 djm 3795: {
1.209 avsm 3796: u_int i;
3797: int found = 0;
1.202 djm 3798:
1.367 djm 3799: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3800: Channel *c = ssh->chanctxt->channels[i];
1.312 djm 3801: if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3802: continue;
3803: if (strcmp(c->path, host) == 0 && c->listening_port == port) {
1.403 djm 3804: debug2_f("close channel %d", i);
1.367 djm 3805: channel_free(ssh, c);
1.312 djm 3806: found = 1;
3807: }
3808: }
3809:
1.367 djm 3810: return found;
1.312 djm 3811: }
3812:
1.336 millert 3813: static int
1.367 djm 3814: channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 3815: {
3816: u_int i;
3817: int found = 0;
3818:
1.367 djm 3819: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3820: Channel *c = ssh->chanctxt->channels[i];
1.336 millert 3821: if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3822: continue;
3823: if (c->path == NULL)
3824: continue;
3825: if (strcmp(c->path, path) == 0) {
1.403 djm 3826: debug2_f("close channel %d", i);
1.367 djm 3827: channel_free(ssh, c);
1.336 millert 3828: found = 1;
3829: }
3830: }
3831:
1.367 djm 3832: return found;
1.336 millert 3833: }
3834:
1.312 djm 3835: int
1.367 djm 3836: channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
1.336 millert 3837: {
1.367 djm 3838: if (fwd->listen_path != NULL) {
3839: return channel_cancel_rport_listener_streamlocal(ssh,
3840: fwd->listen_path);
3841: } else {
3842: return channel_cancel_rport_listener_tcpip(ssh,
3843: fwd->listen_host, fwd->listen_port);
3844: }
1.336 millert 3845: }
3846:
3847: static int
1.367 djm 3848: channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3849: const char *lhost, u_short lport, int cport,
3850: struct ForwardOptions *fwd_opts)
1.312 djm 3851: {
3852: u_int i;
3853: int found = 0;
1.389 djm 3854: const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts);
1.202 djm 3855:
1.367 djm 3856: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3857: Channel *c = ssh->chanctxt->channels[i];
1.312 djm 3858: if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3859: continue;
1.313 markus 3860: if (c->listening_port != lport)
1.312 djm 3861: continue;
1.313 markus 3862: if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3863: /* skip dynamic forwardings */
3864: if (c->host_port == 0)
3865: continue;
3866: } else {
3867: if (c->host_port != cport)
3868: continue;
3869: }
1.312 djm 3870: if ((c->listening_addr == NULL && addr != NULL) ||
3871: (c->listening_addr != NULL && addr == NULL))
3872: continue;
3873: if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
1.403 djm 3874: debug2_f("close channel %d", i);
1.367 djm 3875: channel_free(ssh, c);
1.202 djm 3876: found = 1;
3877: }
3878: }
3879:
1.367 djm 3880: return found;
1.202 djm 3881: }
3882:
1.336 millert 3883: static int
1.367 djm 3884: channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 3885: {
3886: u_int i;
3887: int found = 0;
3888:
3889: if (path == NULL) {
1.403 djm 3890: error_f("no path specified.");
1.336 millert 3891: return 0;
3892: }
3893:
1.367 djm 3894: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3895: Channel *c = ssh->chanctxt->channels[i];
1.336 millert 3896: if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3897: continue;
3898: if (c->listening_addr == NULL)
3899: continue;
3900: if (strcmp(c->listening_addr, path) == 0) {
1.403 djm 3901: debug2_f("close channel %d", i);
1.367 djm 3902: channel_free(ssh, c);
1.336 millert 3903: found = 1;
3904: }
3905: }
3906:
1.367 djm 3907: return found;
1.336 millert 3908: }
3909:
3910: int
1.367 djm 3911: channel_cancel_lport_listener(struct ssh *ssh,
3912: struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
1.336 millert 3913: {
1.367 djm 3914: if (fwd->listen_path != NULL) {
3915: return channel_cancel_lport_listener_streamlocal(ssh,
3916: fwd->listen_path);
3917: } else {
3918: return channel_cancel_lport_listener_tcpip(ssh,
3919: fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3920: }
1.336 millert 3921: }
3922:
1.362 markus 3923: /* protocol local port fwd, used by ssh */
1.160 markus 3924: int
1.367 djm 3925: channel_setup_local_fwd_listener(struct ssh *ssh,
3926: struct Forward *fwd, struct ForwardOptions *fwd_opts)
1.160 markus 3927: {
1.336 millert 3928: if (fwd->listen_path != NULL) {
1.367 djm 3929: return channel_setup_fwd_listener_streamlocal(ssh,
1.336 millert 3930: SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3931: } else {
1.367 djm 3932: return channel_setup_fwd_listener_tcpip(ssh,
3933: SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
1.336 millert 3934: }
1.160 markus 3935: }
3936:
1.381 djm 3937: /* Matches a remote forwarding permission against a requested forwarding */
3938: static int
3939: remote_open_match(struct permission *allowed_open, struct Forward *fwd)
3940: {
3941: int ret;
3942: char *lhost;
3943:
3944: /* XXX add ACLs for streamlocal */
3945: if (fwd->listen_path != NULL)
3946: return 1;
3947:
3948: if (fwd->listen_host == NULL || allowed_open->listen_host == NULL)
3949: return 0;
3950:
3951: if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT &&
3952: allowed_open->listen_port != fwd->listen_port)
3953: return 0;
3954:
3955: /* Match hostnames case-insensitively */
3956: lhost = xstrdup(fwd->listen_host);
3957: lowercase(lhost);
3958: ret = match_pattern(lhost, allowed_open->listen_host);
3959: free(lhost);
3960:
3961: return ret;
3962: }
3963:
3964: /* Checks whether a requested remote forwarding is permitted */
3965: static int
3966: check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
3967: {
3968: struct ssh_channels *sc = ssh->chanctxt;
3969: struct permission_set *pset = &sc->remote_perms;
3970: u_int i, permit, permit_adm = 1;
3971: struct permission *perm;
3972:
3973: /* XXX apply GatewayPorts override before checking? */
3974:
3975: permit = pset->all_permitted;
3976: if (!permit) {
3977: for (i = 0; i < pset->num_permitted_user; i++) {
3978: perm = &pset->permitted_user[i];
3979: if (remote_open_match(perm, fwd)) {
3980: permit = 1;
3981: break;
3982: }
3983: }
3984: }
3985:
3986: if (pset->num_permitted_admin > 0) {
3987: permit_adm = 0;
3988: for (i = 0; i < pset->num_permitted_admin; i++) {
3989: perm = &pset->permitted_admin[i];
3990: if (remote_open_match(perm, fwd)) {
3991: permit_adm = 1;
3992: break;
3993: }
3994: }
3995: }
3996:
3997: return permit && permit_adm;
3998: }
3999:
1.160 markus 4000: /* protocol v2 remote port fwd, used by sshd */
4001: int
1.367 djm 4002: channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
1.336 millert 4003: int *allocated_listen_port, struct ForwardOptions *fwd_opts)
1.160 markus 4004: {
1.381 djm 4005: if (!check_rfwd_permission(ssh, fwd)) {
1.389 djm 4006: ssh_packet_send_debug(ssh, "port forwarding refused");
1.391 florian 4007: if (fwd->listen_path != NULL)
4008: /* XXX always allowed, see remote_open_match() */
4009: logit("Received request from %.100s port %d to "
4010: "remote forward to path \"%.100s\", "
4011: "but the request was denied.",
4012: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4013: fwd->listen_path);
4014: else if(fwd->listen_host != NULL)
4015: logit("Received request from %.100s port %d to "
4016: "remote forward to host %.100s port %d, "
4017: "but the request was denied.",
4018: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4019: fwd->listen_host, fwd->listen_port );
4020: else
4021: logit("Received request from %.100s port %d to remote "
4022: "forward, but the request was denied.",
4023: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.381 djm 4024: return 0;
4025: }
1.336 millert 4026: if (fwd->listen_path != NULL) {
1.367 djm 4027: return channel_setup_fwd_listener_streamlocal(ssh,
1.336 millert 4028: SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
4029: } else {
1.367 djm 4030: return channel_setup_fwd_listener_tcpip(ssh,
1.336 millert 4031: SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
4032: fwd_opts);
4033: }
1.160 markus 4034: }
4035:
1.27 markus 4036: /*
1.312 djm 4037: * Translate the requested rfwd listen host to something usable for
4038: * this server.
4039: */
4040: static const char *
4041: channel_rfwd_bind_host(const char *listen_host)
4042: {
4043: if (listen_host == NULL) {
1.378 djm 4044: return "localhost";
1.312 djm 4045: } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
1.378 djm 4046: return "";
1.312 djm 4047: } else
4048: return listen_host;
4049: }
4050:
4051: /*
1.27 markus 4052: * Initiate forwarding of connections to port "port" on remote host through
4053: * the secure channel to host:port from local side.
1.315 markus 4054: * Returns handle (index) for updating the dynamic listen port with
1.381 djm 4055: * channel_update_permission().
1.27 markus 4056: */
1.253 markus 4057: int
1.367 djm 4058: channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
1.25 markus 4059: {
1.367 djm 4060: int r, success = 0, idx = -1;
1.421 mbuhl 4061: const char *host_to_connect, *listen_host, *listen_path;
1.367 djm 4062: int port_to_connect, listen_port;
1.73 markus 4063:
1.25 markus 4064: /* Send the forward request to the remote side. */
1.358 djm 4065: if (fwd->listen_path != NULL) {
1.367 djm 4066: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4067: (r = sshpkt_put_cstring(ssh,
4068: "streamlocal-forward@openssh.com")) != 0 ||
4069: (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4070: (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
4071: (r = sshpkt_send(ssh)) != 0 ||
4072: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 4073: fatal_fr(r, "request streamlocal");
1.336 millert 4074: } else {
1.367 djm 4075: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4076: (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
4077: (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4078: (r = sshpkt_put_cstring(ssh,
4079: channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
4080: (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
4081: (r = sshpkt_send(ssh)) != 0 ||
4082: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 4083: fatal_fr(r, "request tcpip-forward");
1.73 markus 4084: }
1.358 djm 4085: /* Assume that server accepts the request */
4086: success = 1;
1.73 markus 4087: if (success) {
1.305 djm 4088: /* Record that connection to this host/port is permitted. */
1.367 djm 4089: host_to_connect = listen_host = listen_path = NULL;
4090: port_to_connect = listen_port = 0;
1.336 millert 4091: if (fwd->connect_path != NULL) {
1.421 mbuhl 4092: host_to_connect = fwd->connect_path;
1.367 djm 4093: port_to_connect = PORT_STREAMLOCAL;
1.336 millert 4094: } else {
1.421 mbuhl 4095: host_to_connect = fwd->connect_host;
1.367 djm 4096: port_to_connect = fwd->connect_port;
1.336 millert 4097: }
4098: if (fwd->listen_path != NULL) {
1.421 mbuhl 4099: listen_path = fwd->listen_path;
1.367 djm 4100: listen_port = PORT_STREAMLOCAL;
1.336 millert 4101: } else {
1.421 mbuhl 4102: listen_host = fwd->listen_host;
1.367 djm 4103: listen_port = fwd->listen_port;
4104: }
1.381 djm 4105: idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
1.367 djm 4106: host_to_connect, port_to_connect,
4107: listen_host, listen_path, listen_port, NULL);
1.44 markus 4108: }
1.367 djm 4109: return idx;
1.1 deraadt 4110: }
4111:
1.333 markus 4112: static int
1.381 djm 4113: open_match(struct permission *allowed_open, const char *requestedhost,
1.336 millert 4114: int requestedport)
1.333 markus 4115: {
4116: if (allowed_open->host_to_connect == NULL)
4117: return 0;
4118: if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
4119: allowed_open->port_to_connect != requestedport)
4120: return 0;
1.351 dtucker 4121: if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
4122: strcmp(allowed_open->host_to_connect, requestedhost) != 0)
1.333 markus 4123: return 0;
4124: return 1;
4125: }
4126:
4127: /*
1.336 millert 4128: * Note that in the listen host/port case
1.333 markus 4129: * we don't support FWD_PERMIT_ANY_PORT and
4130: * need to translate between the configured-host (listen_host)
4131: * and what we've sent to the remote server (channel_rfwd_bind_host)
4132: */
4133: static int
1.381 djm 4134: open_listen_match_tcpip(struct permission *allowed_open,
1.336 millert 4135: const char *requestedhost, u_short requestedport, int translate)
1.333 markus 4136: {
4137: const char *allowed_host;
4138:
4139: if (allowed_open->host_to_connect == NULL)
4140: return 0;
4141: if (allowed_open->listen_port != requestedport)
4142: return 0;
1.335 djm 4143: if (!translate && allowed_open->listen_host == NULL &&
4144: requestedhost == NULL)
4145: return 1;
1.333 markus 4146: allowed_host = translate ?
4147: channel_rfwd_bind_host(allowed_open->listen_host) :
4148: allowed_open->listen_host;
1.382 djm 4149: if (allowed_host == NULL || requestedhost == NULL ||
1.333 markus 4150: strcmp(allowed_host, requestedhost) != 0)
4151: return 0;
4152: return 1;
4153: }
4154:
1.336 millert 4155: static int
1.381 djm 4156: open_listen_match_streamlocal(struct permission *allowed_open,
1.336 millert 4157: const char *requestedpath)
4158: {
4159: if (allowed_open->host_to_connect == NULL)
4160: return 0;
4161: if (allowed_open->listen_port != PORT_STREAMLOCAL)
4162: return 0;
4163: if (allowed_open->listen_path == NULL ||
4164: strcmp(allowed_open->listen_path, requestedpath) != 0)
4165: return 0;
4166: return 1;
4167: }
4168:
1.27 markus 4169: /*
1.208 deraadt 4170: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 4171: * local side.
4172: */
1.336 millert 4173: static int
1.367 djm 4174: channel_request_rforward_cancel_tcpip(struct ssh *ssh,
4175: const char *host, u_short port)
1.202 djm 4176: {
1.367 djm 4177: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4178: struct permission_set *pset = &sc->local_perms;
1.367 djm 4179: int r;
4180: u_int i;
1.397 markus 4181: struct permission *perm = NULL;
1.202 djm 4182:
1.381 djm 4183: for (i = 0; i < pset->num_permitted_user; i++) {
4184: perm = &pset->permitted_user[i];
4185: if (open_listen_match_tcpip(perm, host, port, 0))
1.202 djm 4186: break;
1.381 djm 4187: perm = NULL;
1.202 djm 4188: }
1.381 djm 4189: if (perm == NULL) {
1.403 djm 4190: debug_f("requested forward not found");
1.312 djm 4191: return -1;
1.202 djm 4192: }
1.367 djm 4193: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4194: (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
4195: (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4196: (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
4197: (r = sshpkt_put_u32(ssh, port)) != 0 ||
4198: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4199: fatal_fr(r, "send cancel");
1.367 djm 4200:
1.381 djm 4201: fwd_perm_clear(perm); /* unregister */
1.336 millert 4202:
4203: return 0;
4204: }
4205:
4206: /*
4207: * Request cancellation of remote forwarding of Unix domain socket
4208: * path from local side.
4209: */
4210: static int
1.367 djm 4211: channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 4212: {
1.367 djm 4213: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4214: struct permission_set *pset = &sc->local_perms;
1.367 djm 4215: int r;
4216: u_int i;
1.397 markus 4217: struct permission *perm = NULL;
1.336 millert 4218:
1.381 djm 4219: for (i = 0; i < pset->num_permitted_user; i++) {
4220: perm = &pset->permitted_user[i];
4221: if (open_listen_match_streamlocal(perm, path))
1.336 millert 4222: break;
1.381 djm 4223: perm = NULL;
1.336 millert 4224: }
1.381 djm 4225: if (perm == NULL) {
1.403 djm 4226: debug_f("requested forward not found");
1.336 millert 4227: return -1;
4228: }
1.367 djm 4229: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4230: (r = sshpkt_put_cstring(ssh,
4231: "cancel-streamlocal-forward@openssh.com")) != 0 ||
4232: (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4233: (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4234: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4235: fatal_fr(r, "send cancel");
1.367 djm 4236:
1.381 djm 4237: fwd_perm_clear(perm); /* unregister */
1.312 djm 4238:
4239: return 0;
1.202 djm 4240: }
4241:
4242: /*
1.336 millert 4243: * Request cancellation of remote forwarding of a connection from local side.
4244: */
4245: int
1.367 djm 4246: channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
1.336 millert 4247: {
4248: if (fwd->listen_path != NULL) {
1.367 djm 4249: return channel_request_rforward_cancel_streamlocal(ssh,
4250: fwd->listen_path);
1.336 millert 4251: } else {
1.367 djm 4252: return channel_request_rforward_cancel_tcpip(ssh,
4253: fwd->listen_host,
4254: fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
1.336 millert 4255: }
1.1 deraadt 4256: }
4257:
1.99 markus 4258: /*
1.381 djm 4259: * Permits opening to any host/port if permitted_user[] is empty. This is
1.99 markus 4260: * usually called by the server, because the user could connect to any port
4261: * anyway, and the server has no way to know but to trust the client anyway.
4262: */
4263: void
1.381 djm 4264: channel_permit_all(struct ssh *ssh, int where)
4265: {
4266: struct permission_set *pset = permission_set_get(ssh, where);
4267:
4268: if (pset->num_permitted_user == 0)
4269: pset->all_permitted = 1;
4270: }
4271:
4272: /*
4273: * Permit the specified host/port for forwarding.
4274: */
4275: void
4276: channel_add_permission(struct ssh *ssh, int who, int where,
4277: char *host, int port)
4278: {
4279: int local = where == FORWARD_LOCAL;
4280: struct permission_set *pset = permission_set_get(ssh, where);
4281:
4282: debug("allow %s forwarding to host %s port %d",
4283: fwd_ident(who, where), host, port);
4284: /*
4285: * Remote forwards set listen_host/port, local forwards set
4286: * host/port_to_connect.
4287: */
4288: permission_set_add(ssh, who, where,
4289: local ? host : 0, local ? port : 0,
4290: local ? NULL : host, NULL, local ? 0 : port, NULL);
4291: pset->all_permitted = 0;
4292: }
4293:
4294: /*
4295: * Administratively disable forwarding.
4296: */
4297: void
4298: channel_disable_admin(struct ssh *ssh, int where)
1.99 markus 4299: {
1.381 djm 4300: channel_clear_permission(ssh, FORWARD_ADM, where);
4301: permission_set_add(ssh, FORWARD_ADM, where,
4302: NULL, 0, NULL, NULL, 0, NULL);
1.99 markus 4303: }
4304:
1.381 djm 4305: /*
4306: * Clear a list of permitted opens.
4307: */
1.101 markus 4308: void
1.381 djm 4309: channel_clear_permission(struct ssh *ssh, int who, int where)
1.99 markus 4310: {
1.381 djm 4311: struct permission **permp;
4312: u_int *npermp;
1.367 djm 4313:
1.381 djm 4314: permission_set_get_array(ssh, who, where, &permp, &npermp);
4315: *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp));
4316: *npermp = 0;
1.315 markus 4317: }
4318:
4319: /*
4320: * Update the listen port for a dynamic remote forward, after
4321: * the actual 'newport' has been allocated. If 'newport' < 0 is
4322: * passed then they entry will be invalidated.
4323: */
4324: void
1.381 djm 4325: channel_update_permission(struct ssh *ssh, int idx, int newport)
1.315 markus 4326: {
1.381 djm 4327: struct permission_set *pset = &ssh->chanctxt->local_perms;
1.367 djm 4328:
1.381 djm 4329: if (idx < 0 || (u_int)idx >= pset->num_permitted_user) {
1.403 djm 4330: debug_f("index out of range: %d num_permitted_user %d",
4331: idx, pset->num_permitted_user);
1.315 markus 4332: return;
4333: }
4334: debug("%s allowed port %d for forwarding to host %s port %d",
4335: newport > 0 ? "Updating" : "Removing",
4336: newport,
1.381 djm 4337: pset->permitted_user[idx].host_to_connect,
4338: pset->permitted_user[idx].port_to_connect);
1.367 djm 4339: if (newport <= 0)
1.381 djm 4340: fwd_perm_clear(&pset->permitted_user[idx]);
1.367 djm 4341: else {
1.381 djm 4342: pset->permitted_user[idx].listen_port =
1.404 djm 4343: (ssh->compat & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
1.315 markus 4344: }
1.99 markus 4345: }
4346:
1.314 dtucker 4347: /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
4348: int
4349: permitopen_port(const char *p)
4350: {
4351: int port;
4352:
4353: if (strcmp(p, "*") == 0)
4354: return FWD_PERMIT_ANY_PORT;
4355: if ((port = a2port(p)) > 0)
4356: return port;
4357: return -1;
4358: }
4359:
1.276 djm 4360: /* Try to start non-blocking connect to next host in cctx list */
1.127 itojun 4361: static int
1.276 djm 4362: connect_next(struct channel_connect *cctx)
1.41 markus 4363: {
1.276 djm 4364: int sock, saved_errno;
1.336 millert 4365: struct sockaddr_un *sunaddr;
1.367 djm 4366: char ntop[NI_MAXHOST];
4367: char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
1.41 markus 4368:
1.276 djm 4369: for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
1.336 millert 4370: switch (cctx->ai->ai_family) {
4371: case AF_UNIX:
4372: /* unix:pathname instead of host:port */
4373: sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4374: strlcpy(ntop, "unix", sizeof(ntop));
4375: strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4376: break;
4377: case AF_INET:
4378: case AF_INET6:
4379: if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4380: ntop, sizeof(ntop), strport, sizeof(strport),
4381: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.420 djm 4382: error_f("getnameinfo failed");
1.336 millert 4383: continue;
4384: }
4385: break;
4386: default:
1.41 markus 4387: continue;
4388: }
1.420 djm 4389: debug_f("start for host %.100s ([%.100s]:%s)",
4390: cctx->host, ntop, strport);
1.300 dtucker 4391: if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4392: cctx->ai->ai_protocol)) == -1) {
1.276 djm 4393: if (cctx->ai->ai_next == NULL)
1.186 djm 4394: error("socket: %.100s", strerror(errno));
4395: else
4396: verbose("socket: %.100s", strerror(errno));
1.41 markus 4397: continue;
4398: }
1.205 djm 4399: if (set_nonblock(sock) == -1)
1.403 djm 4400: fatal_f("set_nonblock(%d)", sock);
1.276 djm 4401: if (connect(sock, cctx->ai->ai_addr,
4402: cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
1.420 djm 4403: debug_f("host %.100s ([%.100s]:%s): %.100s",
4404: cctx->host, ntop, strport, strerror(errno));
1.276 djm 4405: saved_errno = errno;
1.41 markus 4406: close(sock);
1.276 djm 4407: errno = saved_errno;
1.89 stevesk 4408: continue; /* fail -- try next */
1.41 markus 4409: }
1.336 millert 4410: if (cctx->ai->ai_family != AF_UNIX)
4411: set_nodelay(sock);
1.420 djm 4412: debug_f("connect host %.100s ([%.100s]:%s) in progress, fd=%d",
4413: cctx->host, ntop, strport, sock);
1.276 djm 4414: cctx->ai = cctx->ai->ai_next;
4415: return sock;
4416: }
4417: return -1;
4418: }
1.41 markus 4419:
1.276 djm 4420: static void
4421: channel_connect_ctx_free(struct channel_connect *cctx)
4422: {
1.321 djm 4423: free(cctx->host);
1.336 millert 4424: if (cctx->aitop) {
4425: if (cctx->aitop->ai_family == AF_UNIX)
4426: free(cctx->aitop);
4427: else
4428: freeaddrinfo(cctx->aitop);
4429: }
1.329 tedu 4430: memset(cctx, 0, sizeof(*cctx));
1.276 djm 4431: }
4432:
1.357 dtucker 4433: /*
1.372 markus 4434: * Return connecting socket to remote host:port or local socket path,
1.357 dtucker 4435: * passing back the failure reason if appropriate.
4436: */
1.372 markus 4437: static int
4438: connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4439: char *ctype, char *rname, struct channel_connect *cctx,
4440: int *reason, const char **errmsg)
1.276 djm 4441: {
4442: struct addrinfo hints;
4443: int gaierr;
4444: int sock = -1;
4445: char strport[NI_MAXSERV];
1.336 millert 4446:
4447: if (port == PORT_STREAMLOCAL) {
4448: struct sockaddr_un *sunaddr;
4449: struct addrinfo *ai;
4450:
4451: if (strlen(name) > sizeof(sunaddr->sun_path)) {
4452: error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
1.372 markus 4453: return -1;
1.336 millert 4454: }
4455:
4456: /*
4457: * Fake up a struct addrinfo for AF_UNIX connections.
4458: * channel_connect_ctx_free() must check ai_family
4459: * and use free() not freeaddirinfo() for AF_UNIX.
4460: */
4461: ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4462: memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4463: ai->ai_addr = (struct sockaddr *)(ai + 1);
4464: ai->ai_addrlen = sizeof(*sunaddr);
4465: ai->ai_family = AF_UNIX;
1.372 markus 4466: ai->ai_socktype = socktype;
1.336 millert 4467: ai->ai_protocol = PF_UNSPEC;
4468: sunaddr = (struct sockaddr_un *)ai->ai_addr;
4469: sunaddr->sun_family = AF_UNIX;
4470: strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
1.372 markus 4471: cctx->aitop = ai;
1.336 millert 4472: } else {
4473: memset(&hints, 0, sizeof(hints));
1.367 djm 4474: hints.ai_family = ssh->chanctxt->IPv4or6;
1.372 markus 4475: hints.ai_socktype = socktype;
1.336 millert 4476: snprintf(strport, sizeof strport, "%d", port);
1.372 markus 4477: if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
1.357 dtucker 4478: != 0) {
4479: if (errmsg != NULL)
4480: *errmsg = ssh_gai_strerror(gaierr);
4481: if (reason != NULL)
4482: *reason = SSH2_OPEN_CONNECT_FAILED;
1.336 millert 4483: error("connect_to %.100s: unknown host (%s)", name,
4484: ssh_gai_strerror(gaierr));
1.372 markus 4485: return -1;
1.336 millert 4486: }
1.41 markus 4487: }
1.276 djm 4488:
1.372 markus 4489: cctx->host = xstrdup(name);
4490: cctx->port = port;
4491: cctx->ai = cctx->aitop;
1.276 djm 4492:
1.372 markus 4493: if ((sock = connect_next(cctx)) == -1) {
1.276 djm 4494: error("connect to %.100s port %d failed: %s",
1.336 millert 4495: name, port, strerror(errno));
1.372 markus 4496: return -1;
4497: }
4498:
4499: return sock;
4500: }
4501:
4502: /* Return CONNECTING channel to remote host:port or local socket path */
4503: static Channel *
4504: connect_to(struct ssh *ssh, const char *host, int port,
4505: char *ctype, char *rname)
4506: {
4507: struct channel_connect cctx;
4508: Channel *c;
4509: int sock;
4510:
4511: memset(&cctx, 0, sizeof(cctx));
4512: sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4513: &cctx, NULL, NULL);
4514: if (sock == -1) {
1.276 djm 4515: channel_connect_ctx_free(&cctx);
4516: return NULL;
1.41 markus 4517: }
1.367 djm 4518: c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
1.276 djm 4519: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
1.372 markus 4520: c->host_port = port;
4521: c->path = xstrdup(host);
1.276 djm 4522: c->connect_ctx = cctx;
1.372 markus 4523:
1.276 djm 4524: return c;
1.41 markus 4525: }
1.99 markus 4526:
1.354 markus 4527: /*
4528: * returns either the newly connected channel or the downstream channel
4529: * that needs to deal with this connection.
4530: */
1.276 djm 4531: Channel *
1.367 djm 4532: channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
1.333 markus 4533: u_short listen_port, char *ctype, char *rname)
1.73 markus 4534: {
1.367 djm 4535: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4536: struct permission_set *pset = &sc->local_perms;
1.367 djm 4537: u_int i;
1.381 djm 4538: struct permission *perm;
1.99 markus 4539:
1.381 djm 4540: for (i = 0; i < pset->num_permitted_user; i++) {
4541: perm = &pset->permitted_user[i];
4542: if (open_listen_match_tcpip(perm,
4543: listen_host, listen_port, 1)) {
4544: if (perm->downstream)
4545: return perm->downstream;
4546: if (perm->port_to_connect == 0)
1.372 markus 4547: return rdynamic_connect_prepare(ssh,
4548: ctype, rname);
1.367 djm 4549: return connect_to(ssh,
1.381 djm 4550: perm->host_to_connect, perm->port_to_connect,
1.367 djm 4551: ctype, rname);
1.276 djm 4552: }
4553: }
1.74 markus 4554: error("WARNING: Server requests forwarding for unknown listen_port %d",
4555: listen_port);
1.276 djm 4556: return NULL;
1.73 markus 4557: }
4558:
1.336 millert 4559: Channel *
1.367 djm 4560: channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4561: char *ctype, char *rname)
1.336 millert 4562: {
1.367 djm 4563: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4564: struct permission_set *pset = &sc->local_perms;
1.367 djm 4565: u_int i;
1.381 djm 4566: struct permission *perm;
1.336 millert 4567:
1.381 djm 4568: for (i = 0; i < pset->num_permitted_user; i++) {
4569: perm = &pset->permitted_user[i];
4570: if (open_listen_match_streamlocal(perm, path)) {
1.367 djm 4571: return connect_to(ssh,
1.381 djm 4572: perm->host_to_connect, perm->port_to_connect,
1.367 djm 4573: ctype, rname);
1.336 millert 4574: }
4575: }
4576: error("WARNING: Server requests forwarding for unknown path %.100s",
4577: path);
4578: return NULL;
4579: }
4580:
1.99 markus 4581: /* Check if connecting to that port is permitted and connect. */
1.276 djm 4582: Channel *
1.367 djm 4583: channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4584: char *ctype, char *rname, int *reason, const char **errmsg)
1.99 markus 4585: {
1.367 djm 4586: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4587: struct permission_set *pset = &sc->local_perms;
1.372 markus 4588: struct channel_connect cctx;
4589: Channel *c;
1.367 djm 4590: u_int i, permit, permit_adm = 1;
1.372 markus 4591: int sock;
1.381 djm 4592: struct permission *perm;
1.99 markus 4593:
1.381 djm 4594: permit = pset->all_permitted;
1.99 markus 4595: if (!permit) {
1.381 djm 4596: for (i = 0; i < pset->num_permitted_user; i++) {
4597: perm = &pset->permitted_user[i];
4598: if (open_match(perm, host, port)) {
1.99 markus 4599: permit = 1;
1.333 markus 4600: break;
4601: }
1.367 djm 4602: }
1.257 dtucker 4603: }
1.99 markus 4604:
1.381 djm 4605: if (pset->num_permitted_admin > 0) {
1.257 dtucker 4606: permit_adm = 0;
1.381 djm 4607: for (i = 0; i < pset->num_permitted_admin; i++) {
4608: perm = &pset->permitted_admin[i];
4609: if (open_match(perm, host, port)) {
1.257 dtucker 4610: permit_adm = 1;
1.333 markus 4611: break;
4612: }
1.367 djm 4613: }
1.99 markus 4614: }
1.257 dtucker 4615:
4616: if (!permit || !permit_adm) {
1.391 florian 4617: logit("Received request from %.100s port %d to connect to "
4618: "host %.100s port %d, but the request was denied.",
4619: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), host, port);
1.357 dtucker 4620: if (reason != NULL)
4621: *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
1.276 djm 4622: return NULL;
1.99 markus 4623: }
1.372 markus 4624:
4625: memset(&cctx, 0, sizeof(cctx));
4626: sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4627: &cctx, reason, errmsg);
4628: if (sock == -1) {
4629: channel_connect_ctx_free(&cctx);
4630: return NULL;
4631: }
4632:
4633: c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4634: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4635: c->host_port = port;
4636: c->path = xstrdup(host);
4637: c->connect_ctx = cctx;
4638:
4639: return c;
1.336 millert 4640: }
4641:
4642: /* Check if connecting to that path is permitted and connect. */
4643: Channel *
1.367 djm 4644: channel_connect_to_path(struct ssh *ssh, const char *path,
4645: char *ctype, char *rname)
1.336 millert 4646: {
1.367 djm 4647: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4648: struct permission_set *pset = &sc->local_perms;
1.367 djm 4649: u_int i, permit, permit_adm = 1;
1.381 djm 4650: struct permission *perm;
1.336 millert 4651:
1.381 djm 4652: permit = pset->all_permitted;
1.336 millert 4653: if (!permit) {
1.381 djm 4654: for (i = 0; i < pset->num_permitted_user; i++) {
4655: perm = &pset->permitted_user[i];
4656: if (open_match(perm, path, PORT_STREAMLOCAL)) {
1.336 millert 4657: permit = 1;
4658: break;
4659: }
1.367 djm 4660: }
1.336 millert 4661: }
4662:
1.381 djm 4663: if (pset->num_permitted_admin > 0) {
1.336 millert 4664: permit_adm = 0;
1.381 djm 4665: for (i = 0; i < pset->num_permitted_admin; i++) {
4666: perm = &pset->permitted_admin[i];
4667: if (open_match(perm, path, PORT_STREAMLOCAL)) {
1.336 millert 4668: permit_adm = 1;
4669: break;
4670: }
1.367 djm 4671: }
1.336 millert 4672: }
4673:
4674: if (!permit || !permit_adm) {
4675: logit("Received request to connect to path %.100s, "
4676: "but the request was denied.", path);
4677: return NULL;
4678: }
1.367 djm 4679: return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
1.204 djm 4680: }
4681:
4682: void
1.367 djm 4683: channel_send_window_changes(struct ssh *ssh)
1.204 djm 4684: {
1.367 djm 4685: struct ssh_channels *sc = ssh->chanctxt;
4686: struct winsize ws;
4687: int r;
1.209 avsm 4688: u_int i;
1.204 djm 4689:
1.367 djm 4690: for (i = 0; i < sc->channels_alloc; i++) {
4691: if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4692: sc->channels[i]->type != SSH_CHANNEL_OPEN)
1.204 djm 4693: continue;
1.393 deraadt 4694: if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) == -1)
1.204 djm 4695: continue;
1.367 djm 4696: channel_request_start(ssh, i, "window-change", 0);
4697: if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4698: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4699: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4700: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4701: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4702: fatal_fr(r, "channel %u; send window-change", i);
1.204 djm 4703: }
1.372 markus 4704: }
4705:
4706: /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4707: static Channel *
4708: rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4709: {
4710: Channel *c;
4711: int r;
4712:
4713: c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4714: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4715: c->host_port = 0;
4716: c->path = NULL;
4717:
4718: /*
4719: * We need to open the channel before we have a FD,
4720: * so that we can get SOCKS header from peer.
4721: */
4722: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4723: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4724: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4725: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1.403 djm 4726: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0)
4727: fatal_fr(r, "channel %i; confirm", c->self);
1.372 markus 4728: return c;
4729: }
4730:
4731: /* Return CONNECTING socket to remote host:port or local socket path */
4732: static int
4733: rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4734: {
1.405 markus 4735: struct ssh_channels *sc = ssh->chanctxt;
4736: struct permission_set *pset = &sc->local_perms;
4737: struct permission *perm;
1.372 markus 4738: struct channel_connect cctx;
1.405 markus 4739: u_int i, permit_adm = 1;
1.372 markus 4740: int sock;
1.405 markus 4741:
4742: if (pset->num_permitted_admin > 0) {
4743: permit_adm = 0;
4744: for (i = 0; i < pset->num_permitted_admin; i++) {
4745: perm = &pset->permitted_admin[i];
4746: if (open_match(perm, c->path, c->host_port)) {
4747: permit_adm = 1;
4748: break;
4749: }
4750: }
4751: }
4752: if (!permit_adm) {
4753: debug_f("requested forward not permitted");
4754: return -1;
4755: }
1.372 markus 4756:
4757: memset(&cctx, 0, sizeof(cctx));
4758: sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4759: NULL, &cctx, NULL, NULL);
4760: if (sock == -1)
4761: channel_connect_ctx_free(&cctx);
4762: else {
4763: /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4764: c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4765: c->connect_ctx = cctx;
4766: channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4767: }
4768: return sock;
1.99 markus 4769: }
4770:
1.121 markus 4771: /* -- X11 forwarding */
1.1 deraadt 4772:
1.27 markus 4773: /*
4774: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 4775: * Returns 0 and a suitable display number for the DISPLAY variable
4776: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 4777: */
1.141 stevesk 4778: int
1.367 djm 4779: x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4780: int x11_use_localhost, int single_connection,
4781: u_int *display_numberp, int **chanids)
1.1 deraadt 4782: {
1.149 markus 4783: Channel *nc = NULL;
1.31 markus 4784: int display_number, sock;
4785: u_short port;
1.35 markus 4786: struct addrinfo hints, *ai, *aitop;
4787: char strport[NI_MAXSERV];
4788: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 4789:
1.224 markus 4790: if (chanids == NULL)
4791: return -1;
4792:
1.33 markus 4793: for (display_number = x11_display_offset;
1.148 deraadt 4794: display_number < MAX_DISPLAYS;
4795: display_number++) {
1.25 markus 4796: port = 6000 + display_number;
1.35 markus 4797: memset(&hints, 0, sizeof(hints));
1.367 djm 4798: hints.ai_family = ssh->chanctxt->IPv4or6;
1.163 stevesk 4799: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 4800: hints.ai_socktype = SOCK_STREAM;
4801: snprintf(strport, sizeof strport, "%d", port);
1.367 djm 4802: if ((gaierr = getaddrinfo(NULL, strport,
4803: &hints, &aitop)) != 0) {
1.271 dtucker 4804: error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
1.141 stevesk 4805: return -1;
1.25 markus 4806: }
1.35 markus 4807: for (ai = aitop; ai; ai = ai->ai_next) {
1.367 djm 4808: if (ai->ai_family != AF_INET &&
4809: ai->ai_family != AF_INET6)
1.35 markus 4810: continue;
1.300 dtucker 4811: sock = socket(ai->ai_family, ai->ai_socktype,
4812: ai->ai_protocol);
1.393 deraadt 4813: if (sock == -1) {
1.35 markus 4814: error("socket: %.100s", strerror(errno));
1.203 markus 4815: freeaddrinfo(aitop);
1.141 stevesk 4816: return -1;
1.35 markus 4817: }
1.376 djm 4818: set_reuseaddr(sock);
1.393 deraadt 4819: if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.403 djm 4820: debug2_f("bind port %d: %.100s", port,
4821: strerror(errno));
1.35 markus 4822: close(sock);
1.367 djm 4823: for (n = 0; n < num_socks; n++)
1.35 markus 4824: close(socks[n]);
4825: num_socks = 0;
4826: break;
4827: }
4828: socks[num_socks++] = sock;
4829: if (num_socks == NUM_SOCKS)
4830: break;
1.25 markus 4831: }
1.83 stevesk 4832: freeaddrinfo(aitop);
1.35 markus 4833: if (num_socks > 0)
4834: break;
1.25 markus 4835: }
4836: if (display_number >= MAX_DISPLAYS) {
4837: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 4838: return -1;
1.25 markus 4839: }
4840: /* Start listening for connections on the socket. */
1.35 markus 4841: for (n = 0; n < num_socks; n++) {
4842: sock = socks[n];
1.393 deraadt 4843: if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
1.35 markus 4844: error("listen: %.100s", strerror(errno));
4845: close(sock);
1.141 stevesk 4846: return -1;
1.35 markus 4847: }
1.25 markus 4848: }
1.35 markus 4849:
4850: /* Allocate a channel for each socket. */
1.242 djm 4851: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 4852: for (n = 0; n < num_socks; n++) {
4853: sock = socks[n];
1.424 djm 4854: nc = channel_new(ssh, "x11-listener",
1.51 markus 4855: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4856: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 4857: 0, "X11 inet listener", 1);
1.167 markus 4858: nc->single_connection = single_connection;
1.224 markus 4859: (*chanids)[n] = nc->self;
1.35 markus 4860: }
1.224 markus 4861: (*chanids)[n] = -1;
1.1 deraadt 4862:
1.141 stevesk 4863: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 4864: *display_numberp = display_number;
1.367 djm 4865: return 0;
1.1 deraadt 4866: }
4867:
1.127 itojun 4868: static int
1.77 markus 4869: connect_local_xsocket(u_int dnr)
1.1 deraadt 4870: {
1.25 markus 4871: int sock;
4872: struct sockaddr_un addr;
4873:
1.147 stevesk 4874: sock = socket(AF_UNIX, SOCK_STREAM, 0);
1.393 deraadt 4875: if (sock == -1)
1.147 stevesk 4876: error("socket: %.100s", strerror(errno));
4877: memset(&addr, 0, sizeof(addr));
4878: addr.sun_family = AF_UNIX;
4879: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 4880: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 4881: return sock;
4882: close(sock);
1.25 markus 4883: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4884: return -1;
1.1 deraadt 4885: }
4886:
1.51 markus 4887: int
1.367 djm 4888: x11_connect_display(struct ssh *ssh)
1.1 deraadt 4889: {
1.248 deraadt 4890: u_int display_number;
1.25 markus 4891: const char *display;
1.51 markus 4892: char buf[1024], *cp;
1.35 markus 4893: struct addrinfo hints, *ai, *aitop;
4894: char strport[NI_MAXSERV];
1.248 deraadt 4895: int gaierr, sock = 0;
1.25 markus 4896:
4897: /* Try to open a socket for the local X server. */
4898: display = getenv("DISPLAY");
4899: if (!display) {
4900: error("DISPLAY not set.");
1.51 markus 4901: return -1;
1.25 markus 4902: }
1.27 markus 4903: /*
4904: * Now we decode the value of the DISPLAY variable and make a
4905: * connection to the real X server.
4906: */
4907:
4908: /*
4909: * Check if it is a unix domain socket. Unix domain displays are in
4910: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4911: */
1.25 markus 4912: if (strncmp(display, "unix:", 5) == 0 ||
4913: display[0] == ':') {
4914: /* Connect to the unix domain socket. */
1.367 djm 4915: if (sscanf(strrchr(display, ':') + 1, "%u",
4916: &display_number) != 1) {
4917: error("Could not parse display number from DISPLAY: "
4918: "%.100s", display);
1.51 markus 4919: return -1;
1.25 markus 4920: }
4921: /* Create a socket. */
4922: sock = connect_local_xsocket(display_number);
4923: if (sock < 0)
1.51 markus 4924: return -1;
1.25 markus 4925:
4926: /* OK, we now have a connection to the display. */
1.51 markus 4927: return sock;
1.25 markus 4928: }
1.27 markus 4929: /*
4930: * Connect to an inet socket. The DISPLAY value is supposedly
4931: * hostname:d[.s], where hostname may also be numeric IP address.
4932: */
1.145 stevesk 4933: strlcpy(buf, display, sizeof(buf));
1.25 markus 4934: cp = strchr(buf, ':');
4935: if (!cp) {
4936: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 4937: return -1;
1.25 markus 4938: }
4939: *cp = 0;
1.367 djm 4940: /*
4941: * buf now contains the host name. But first we parse the
4942: * display number.
4943: */
1.248 deraadt 4944: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 4945: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 4946: display);
1.51 markus 4947: return -1;
1.25 markus 4948: }
1.35 markus 4949:
4950: /* Look up the host address */
4951: memset(&hints, 0, sizeof(hints));
1.367 djm 4952: hints.ai_family = ssh->chanctxt->IPv4or6;
1.35 markus 4953: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 4954: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 4955: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1.271 dtucker 4956: error("%.100s: unknown host. (%s)", buf,
4957: ssh_gai_strerror(gaierr));
1.51 markus 4958: return -1;
1.25 markus 4959: }
1.35 markus 4960: for (ai = aitop; ai; ai = ai->ai_next) {
4961: /* Create a socket. */
1.300 dtucker 4962: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.393 deraadt 4963: if (sock == -1) {
1.194 markus 4964: debug2("socket: %.100s", strerror(errno));
1.41 markus 4965: continue;
4966: }
4967: /* Connect it to the display. */
1.393 deraadt 4968: if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.248 deraadt 4969: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 4970: 6000 + display_number, strerror(errno));
4971: close(sock);
4972: continue;
4973: }
4974: /* Success */
4975: break;
1.35 markus 4976: }
4977: freeaddrinfo(aitop);
4978: if (!ai) {
1.367 djm 4979: error("connect %.100s port %u: %.100s", buf,
4980: 6000 + display_number, strerror(errno));
1.51 markus 4981: return -1;
1.25 markus 4982: }
1.162 stevesk 4983: set_nodelay(sock);
1.51 markus 4984: return sock;
4985: }
4986:
4987: /*
1.27 markus 4988: * Requests forwarding of X11 connections, generates fake authentication
4989: * data, and enables authentication spoofing.
1.121 markus 4990: * This should be called in the client only.
1.27 markus 4991: */
1.49 markus 4992: void
1.367 djm 4993: x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4994: const char *disp, const char *proto, const char *data, int want_reply)
1.1 deraadt 4995: {
1.367 djm 4996: struct ssh_channels *sc = ssh->chanctxt;
1.77 markus 4997: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 4998: u_int i, value;
1.367 djm 4999: const char *cp;
1.25 markus 5000: char *new_data;
1.367 djm 5001: int r, screen_number;
1.25 markus 5002:
1.367 djm 5003: if (sc->x11_saved_display == NULL)
5004: sc->x11_saved_display = xstrdup(disp);
5005: else if (strcmp(disp, sc->x11_saved_display) != 0) {
1.219 djm 5006: error("x11_request_forwarding_with_spoofing: different "
5007: "$DISPLAY already forwarded");
5008: return;
5009: }
5010:
1.266 djm 5011: cp = strchr(disp, ':');
1.25 markus 5012: if (cp)
5013: cp = strchr(cp, '.');
5014: if (cp)
1.245 deraadt 5015: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 5016: else
5017: screen_number = 0;
5018:
1.367 djm 5019: if (sc->x11_saved_proto == NULL) {
1.219 djm 5020: /* Save protocol name. */
1.367 djm 5021: sc->x11_saved_proto = xstrdup(proto);
1.353 natano 5022:
5023: /* Extract real authentication data. */
1.367 djm 5024: sc->x11_saved_data = xmalloc(data_len);
1.219 djm 5025: for (i = 0; i < data_len; i++) {
1.403 djm 5026: if (sscanf(data + 2 * i, "%2x", &value) != 1) {
1.219 djm 5027: fatal("x11_request_forwarding: bad "
5028: "authentication data: %.100s", data);
1.403 djm 5029: }
1.367 djm 5030: sc->x11_saved_data[i] = value;
1.219 djm 5031: }
1.367 djm 5032: sc->x11_saved_data_len = data_len;
1.353 natano 5033:
5034: /* Generate fake data of the same length. */
1.367 djm 5035: sc->x11_fake_data = xmalloc(data_len);
5036: arc4random_buf(sc->x11_fake_data, data_len);
5037: sc->x11_fake_data_len = data_len;
1.25 markus 5038: }
5039:
5040: /* Convert the fake data into hex. */
1.367 djm 5041: new_data = tohex(sc->x11_fake_data, data_len);
1.25 markus 5042:
5043: /* Send the request packet. */
1.367 djm 5044: channel_request_start(ssh, client_session_id, "x11-req", want_reply);
5045: if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
5046: (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
5047: (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
5048: (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
5049: (r = sshpkt_send(ssh)) != 0 ||
5050: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 5051: fatal_fr(r, "send x11-req");
1.321 djm 5052: free(new_data);
1.1 deraadt 5053: }