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