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