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