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