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