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