djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.389 2019/01/19 21:37:13 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 43 | |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 44 | #include <sys/types.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 45 | #include <sys/stat.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include <sys/ioctl.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 47 | #include <sys/un.h> |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 48 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 49 | #ifdef HAVE_SYS_TIME_H |
| 50 | # include <sys/time.h> |
| 51 | #endif |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 52 | |
| 53 | #include <netinet/in.h> |
| 54 | #include <arpa/inet.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 55 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 56 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 57 | #include <fcntl.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 58 | #include <limits.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 59 | #include <netdb.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 60 | #include <stdarg.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 61 | #ifdef HAVE_STDINT_H |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 62 | #include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 66 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 67 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 70 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh2.h" |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 74 | #include "ssherr.h" |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 75 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "canohost.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 82 | #include "sshkey.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 83 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 84 | #include "pathnames.h" |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 85 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 87 | /* -- agent forwarding */ |
| 88 | #define NUM_SOCKS 10 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 90 | /* -- tcp forwarding */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 91 | /* special-case port number meaning allow any port */ |
| 92 | #define FWD_PERMIT_ANY_PORT 0 |
| 93 | |
| 94 | /* special-case wildcard meaning allow any host */ |
| 95 | #define FWD_PERMIT_ANY_HOST "*" |
| 96 | |
| 97 | /* -- X11 forwarding */ |
| 98 | /* Maximum number of fake X11 displays to try. */ |
| 99 | #define MAX_DISPLAYS 1000 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 101 | /* Per-channel callback for pre/post select() actions */ |
| 102 | typedef void chan_fn(struct ssh *, Channel *c, |
| 103 | fd_set *readset, fd_set *writeset); |
| 104 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 105 | /* |
| 106 | * Data structure for storing which hosts are permitted for forward requests. |
| 107 | * The local sides of any remote forwards are stored in this array to prevent |
| 108 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 109 | * network (which might be behind a firewall). |
| 110 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 111 | /* XXX: streamlocal wants a path instead of host:port */ |
| 112 | /* Overload host_to_connect; we could just make this match Forward */ |
| 113 | /* XXX - can we use listen_host instead of listen_path? */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 114 | struct permission { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 115 | char *host_to_connect; /* Connect to 'host'. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 116 | int port_to_connect; /* Connect to 'port'. */ |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 117 | char *listen_host; /* Remote side should listen address. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 118 | char *listen_path; /* Remote side should listen path. */ |
| 119 | int listen_port; /* Remote side should listen port. */ |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 120 | Channel *downstream; /* Downstream mux*/ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 121 | }; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Stores the forwarding permission state for a single direction (local or |
| 125 | * remote). |
| 126 | */ |
| 127 | struct permission_set { |
| 128 | /* |
| 129 | * List of all local permitted host/port pairs to allow for the |
| 130 | * user. |
| 131 | */ |
| 132 | u_int num_permitted_user; |
| 133 | struct permission *permitted_user; |
| 134 | |
| 135 | /* |
| 136 | * List of all permitted host/port pairs to allow for the admin. |
| 137 | */ |
| 138 | u_int num_permitted_admin; |
| 139 | struct permission *permitted_admin; |
| 140 | |
| 141 | /* |
| 142 | * If this is true, all opens/listens are permitted. This is the |
| 143 | * case on the server on which we have to trust the client anyway, |
| 144 | * and the user could do anything after logging in. |
| 145 | */ |
| 146 | int all_permitted; |
| 147 | }; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 148 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 149 | /* Master structure for channels state */ |
| 150 | struct ssh_channels { |
| 151 | /* |
| 152 | * Pointer to an array containing all allocated channels. The array |
| 153 | * is dynamically extended as needed. |
| 154 | */ |
| 155 | Channel **channels; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 156 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Size of the channel array. All slots of the array must always be |
| 159 | * initialized (at least the type field); unused slots set to NULL |
| 160 | */ |
| 161 | u_int channels_alloc; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 162 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Maximum file descriptor value used in any of the channels. This is |
| 165 | * updated in channel_new. |
| 166 | */ |
| 167 | int channel_max_fd; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 168 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 169 | /* |
| 170 | * 'channel_pre*' are called just before select() to add any bits |
| 171 | * relevant to channels in the select bitmasks. |
| 172 | * |
| 173 | * 'channel_post*': perform any appropriate operations for |
| 174 | * channels which have events pending. |
| 175 | */ |
| 176 | chan_fn **channel_pre; |
| 177 | chan_fn **channel_post; |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 178 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 179 | /* -- tcp forwarding */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 180 | struct permission_set local_perms; |
| 181 | struct permission_set remote_perms; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 182 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 183 | /* -- X11 forwarding */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 184 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 185 | /* Saved X11 local (client) display. */ |
| 186 | char *x11_saved_display; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 187 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 188 | /* Saved X11 authentication protocol name. */ |
| 189 | char *x11_saved_proto; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 190 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 191 | /* Saved X11 authentication data. This is the real data. */ |
| 192 | char *x11_saved_data; |
| 193 | u_int x11_saved_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 194 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 195 | /* Deadline after which all X11 connections are refused */ |
| 196 | u_int x11_refuse_time; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 197 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Fake X11 authentication data. This is what the server will be |
| 200 | * sending us; we should replace any occurrences of this by the |
| 201 | * real data. |
| 202 | */ |
| 203 | u_char *x11_fake_data; |
| 204 | u_int x11_fake_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 205 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 206 | /* AF_UNSPEC or AF_INET or AF_INET6 */ |
| 207 | int IPv4or6; |
| 208 | }; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 209 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 210 | /* helper */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 211 | static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 212 | static const char *channel_rfwd_bind_host(const char *listen_host); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 213 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 214 | /* non-blocking connect helpers */ |
| 215 | static int connect_next(struct channel_connect *); |
| 216 | static void channel_connect_ctx_free(struct channel_connect *); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 217 | static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *); |
| 218 | static int rdynamic_connect_finish(struct ssh *, Channel *); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 219 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 220 | /* Setup helper */ |
| 221 | static void channel_handler_init(struct ssh_channels *sc); |
| 222 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 223 | /* -- channel core */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 224 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 225 | void |
| 226 | channel_init_channels(struct ssh *ssh) |
| 227 | { |
| 228 | struct ssh_channels *sc; |
| 229 | |
djm@openbsd.org | 285310b | 2018-12-07 02:31:20 +0000 | [diff] [blame] | 230 | if ((sc = calloc(1, sizeof(*sc))) == NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 231 | fatal("%s: allocation failed", __func__); |
| 232 | sc->channels_alloc = 10; |
| 233 | sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels)); |
| 234 | sc->IPv4or6 = AF_UNSPEC; |
| 235 | channel_handler_init(sc); |
| 236 | |
| 237 | ssh->chanctxt = sc; |
| 238 | } |
| 239 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 240 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 241 | channel_by_id(struct ssh *ssh, int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 242 | { |
| 243 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 244 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 245 | if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) { |
| 246 | logit("%s: %d: bad id", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 249 | c = ssh->chanctxt->channels[id]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 250 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 251 | logit("%s: %d: bad id: channel free", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | return c; |
| 255 | } |
| 256 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 257 | Channel * |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 258 | channel_by_remote_id(struct ssh *ssh, u_int remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 259 | { |
| 260 | Channel *c; |
| 261 | u_int i; |
| 262 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 263 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 264 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 265 | if (c != NULL && c->have_remote_id && c->remote_id == remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 266 | return c; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 271 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 272 | * Returns the channel if it is allowed to receive protocol messages. |
| 273 | * Private channels, like listening sockets, may not receive messages. |
| 274 | */ |
| 275 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 276 | channel_lookup(struct ssh *ssh, int id) |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 277 | { |
| 278 | Channel *c; |
| 279 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 280 | if ((c = channel_by_id(ssh, id)) == NULL) |
| 281 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 282 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 283 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 284 | case SSH_CHANNEL_X11_OPEN: |
| 285 | case SSH_CHANNEL_LARVAL: |
| 286 | case SSH_CHANNEL_CONNECTING: |
| 287 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 288 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 289 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 290 | case SSH_CHANNEL_OPENING: |
| 291 | case SSH_CHANNEL_OPEN: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 292 | case SSH_CHANNEL_ABANDONED: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 293 | case SSH_CHANNEL_MUX_PROXY: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 294 | return c; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 295 | } |
| 296 | logit("Non-public channel %d, type %d.", id, c->type); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 297 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 301 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 302 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 303 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 304 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 305 | channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd, |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 306 | int extusage, int nonblock, int is_tty) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 308 | struct ssh_channels *sc = ssh->chanctxt; |
| 309 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | /* Update the maximum file descriptor value. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 311 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd); |
| 312 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd); |
| 313 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 314 | |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 315 | if (rfd != -1) |
| 316 | fcntl(rfd, F_SETFD, FD_CLOEXEC); |
| 317 | if (wfd != -1 && wfd != rfd) |
| 318 | fcntl(wfd, F_SETFD, FD_CLOEXEC); |
| 319 | if (efd != -1 && efd != rfd && efd != wfd) |
| 320 | fcntl(efd, F_SETFD, FD_CLOEXEC); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 321 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 322 | c->rfd = rfd; |
| 323 | c->wfd = wfd; |
| 324 | c->sock = (rfd == wfd) ? rfd : -1; |
| 325 | c->efd = efd; |
| 326 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 327 | |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 328 | if ((c->isatty = is_tty) != 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 329 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 330 | #ifdef _AIX |
| 331 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 1a48aec | 2008-06-16 23:35:56 +1000 | [diff] [blame] | 332 | c->wfd_isatty = is_tty || isatty(c->wfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 333 | #endif |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 334 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 335 | /* enable nonblocking mode */ |
| 336 | if (nonblock) { |
| 337 | if (rfd != -1) |
| 338 | set_nonblock(rfd); |
| 339 | if (wfd != -1) |
| 340 | set_nonblock(wfd); |
| 341 | if (efd != -1) |
| 342 | set_nonblock(efd); |
| 343 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Allocate a new channel object and set its type and socket. This will cause |
| 348 | * remote_name to be freed. |
| 349 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 350 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 351 | channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd, |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 352 | u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 353 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 354 | struct ssh_channels *sc = ssh->chanctxt; |
| 355 | u_int i, found; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 356 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 357 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 358 | /* Try to find a free slot where to put the new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 359 | for (i = 0; i < sc->channels_alloc; i++) { |
| 360 | if (sc->channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | /* Found a free slot. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 362 | found = i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 363 | break; |
| 364 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 365 | } |
| 366 | if (i >= sc->channels_alloc) { |
| 367 | /* |
| 368 | * There are no free slots. Take last+1 slot and expand |
| 369 | * the array. |
| 370 | */ |
| 371 | found = sc->channels_alloc; |
| 372 | if (sc->channels_alloc > CHANNELS_MAX_CHANNELS) |
| 373 | fatal("%s: internal error: channels_alloc %d too big", |
| 374 | __func__, sc->channels_alloc); |
| 375 | sc->channels = xrecallocarray(sc->channels, sc->channels_alloc, |
| 376 | sc->channels_alloc + 10, sizeof(*sc->channels)); |
| 377 | sc->channels_alloc += 10; |
| 378 | debug2("channel: expanding %d", sc->channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 380 | /* Initialize and return new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 381 | c = sc->channels[found] = xcalloc(1, sizeof(Channel)); |
| 382 | if ((c->input = sshbuf_new()) == NULL || |
| 383 | (c->output = sshbuf_new()) == NULL || |
| 384 | (c->extended = sshbuf_new()) == NULL) |
| 385 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 386 | c->ostate = CHAN_OUTPUT_OPEN; |
| 387 | c->istate = CHAN_INPUT_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 388 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | c->self = found; |
| 390 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 391 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 392 | c->local_window = window; |
| 393 | c->local_window_max = window; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 394 | c->local_maxpacket = maxpack; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 395 | c->remote_name = xstrdup(remote_name); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 396 | c->ctl_chan = -1; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 397 | c->delayed = 1; /* prevent call to channel_post handler */ |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 398 | TAILQ_INIT(&c->status_confirms); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 400 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 401 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 402 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 403 | static void |
| 404 | channel_find_maxfd(struct ssh_channels *sc) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 405 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 406 | u_int i; |
| 407 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 408 | Channel *c; |
| 409 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 410 | for (i = 0; i < sc->channels_alloc; i++) { |
| 411 | c = sc->channels[i]; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 412 | if (c != NULL) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 413 | max = MAXIMUM(max, c->rfd); |
| 414 | max = MAXIMUM(max, c->wfd); |
| 415 | max = MAXIMUM(max, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 418 | sc->channel_max_fd = max; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 422 | channel_close_fd(struct ssh *ssh, int *fdp) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 423 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 424 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 425 | int ret = 0, fd = *fdp; |
| 426 | |
| 427 | if (fd != -1) { |
| 428 | ret = close(fd); |
| 429 | *fdp = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 430 | if (fd == sc->channel_max_fd) |
| 431 | channel_find_maxfd(sc); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 432 | } |
| 433 | return ret; |
| 434 | } |
| 435 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 436 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 437 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 438 | channel_close_fds(struct ssh *ssh, Channel *c) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 439 | { |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 440 | int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd; |
| 441 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 442 | channel_close_fd(ssh, &c->sock); |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 443 | if (rfd != sock) |
| 444 | channel_close_fd(ssh, &c->rfd); |
| 445 | if (wfd != sock && wfd != rfd) |
| 446 | channel_close_fd(ssh, &c->wfd); |
| 447 | if (efd != sock && efd != rfd && efd != wfd) |
| 448 | channel_close_fd(ssh, &c->efd); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 452 | fwd_perm_clear(struct permission *perm) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 453 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 454 | free(perm->host_to_connect); |
| 455 | free(perm->listen_host); |
| 456 | free(perm->listen_path); |
| 457 | bzero(perm, sizeof(*perm)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 458 | } |
| 459 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 460 | /* Returns an printable name for the specified forwarding permission list */ |
| 461 | static const char * |
| 462 | fwd_ident(int who, int where) |
| 463 | { |
| 464 | if (who == FORWARD_ADM) { |
| 465 | if (where == FORWARD_LOCAL) |
| 466 | return "admin local"; |
| 467 | else if (where == FORWARD_REMOTE) |
| 468 | return "admin remote"; |
| 469 | } else if (who == FORWARD_USER) { |
| 470 | if (where == FORWARD_LOCAL) |
| 471 | return "user local"; |
| 472 | else if (where == FORWARD_REMOTE) |
| 473 | return "user remote"; |
| 474 | } |
| 475 | fatal("Unknown forward permission list %d/%d", who, where); |
| 476 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 477 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 478 | /* Returns the forwarding permission list for the specified direction */ |
| 479 | static struct permission_set * |
| 480 | permission_set_get(struct ssh *ssh, int where) |
| 481 | { |
| 482 | struct ssh_channels *sc = ssh->chanctxt; |
| 483 | |
| 484 | switch (where) { |
| 485 | case FORWARD_LOCAL: |
| 486 | return &sc->local_perms; |
| 487 | break; |
| 488 | case FORWARD_REMOTE: |
| 489 | return &sc->remote_perms; |
| 490 | break; |
| 491 | default: |
| 492 | fatal("%s: invalid forwarding direction %d", __func__, where); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* Reutrns pointers to the specified forwarding list and its element count */ |
| 497 | static void |
| 498 | permission_set_get_array(struct ssh *ssh, int who, int where, |
| 499 | struct permission ***permpp, u_int **npermpp) |
| 500 | { |
| 501 | struct permission_set *pset = permission_set_get(ssh, where); |
| 502 | |
| 503 | switch (who) { |
| 504 | case FORWARD_USER: |
| 505 | *permpp = &pset->permitted_user; |
| 506 | *npermpp = &pset->num_permitted_user; |
| 507 | break; |
| 508 | case FORWARD_ADM: |
| 509 | *permpp = &pset->permitted_admin; |
| 510 | *npermpp = &pset->num_permitted_admin; |
| 511 | break; |
| 512 | default: |
| 513 | fatal("%s: invalid forwarding client %d", __func__, who); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Adds an entry to the spcified forwarding list */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 518 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 519 | permission_set_add(struct ssh *ssh, int who, int where, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 520 | const char *host_to_connect, int port_to_connect, |
| 521 | const char *listen_host, const char *listen_path, int listen_port, |
| 522 | Channel *downstream) |
| 523 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 524 | struct permission **permp; |
| 525 | u_int n, *npermp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 526 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 527 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 528 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 529 | if (*npermp >= INT_MAX) |
| 530 | fatal("%s: %s overflow", __func__, fwd_ident(who, where)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 531 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 532 | *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp)); |
| 533 | n = (*npermp)++; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 534 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 535 | (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 536 | (*permp)[n].port_to_connect = port_to_connect; |
| 537 | (*permp)[n].listen_host = MAYBE_DUP(listen_host); |
| 538 | (*permp)[n].listen_path = MAYBE_DUP(listen_path); |
| 539 | (*permp)[n].listen_port = listen_port; |
| 540 | (*permp)[n].downstream = downstream; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 541 | #undef MAYBE_DUP |
| 542 | return (int)n; |
| 543 | } |
| 544 | |
| 545 | static void |
| 546 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 547 | { |
| 548 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 549 | struct permission_set *pset = &sc->local_perms; |
| 550 | struct permission *perm; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 551 | int r; |
| 552 | u_int i; |
| 553 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 554 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 555 | perm = &pset->permitted_user[i]; |
| 556 | if (perm->downstream != c) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 557 | continue; |
| 558 | |
| 559 | /* cancel on the server, since mux client is gone */ |
| 560 | debug("channel %d: cleanup remote forward for %s:%u", |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 561 | c->self, perm->listen_host, perm->listen_port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 562 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 563 | (r = sshpkt_put_cstring(ssh, |
| 564 | "cancel-tcpip-forward")) != 0 || |
| 565 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 566 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 567 | channel_rfwd_bind_host(perm->listen_host))) != 0 || |
| 568 | (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 569 | (r = sshpkt_send(ssh)) != 0) { |
| 570 | fatal("%s: channel %i: %s", __func__, |
| 571 | c->self, ssh_err(r)); |
| 572 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 573 | fwd_perm_clear(perm); /* unregister */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 574 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 578 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 579 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 580 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 581 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 582 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 583 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 584 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 585 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 586 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 587 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 588 | if ((other = sc->channels[i]) == NULL) |
| 589 | continue; |
| 590 | n++; |
| 591 | /* detach from mux client and prepare for closing */ |
| 592 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 593 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 594 | other->mux_ctx == c) { |
| 595 | other->mux_ctx = NULL; |
| 596 | other->type = SSH_CHANNEL_OPEN; |
| 597 | other->istate = CHAN_INPUT_CLOSED; |
| 598 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 601 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 602 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 603 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 604 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 605 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 606 | |
markus@openbsd.org | 5d14019 | 2018-07-27 12:03:17 +0000 | [diff] [blame] | 607 | if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) { |
| 608 | s = channel_open_message(ssh); |
| 609 | debug3("channel %d: status: %s", c->self, s); |
| 610 | free(s); |
| 611 | } |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 612 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 613 | channel_close_fds(ssh, c); |
| 614 | sshbuf_free(c->input); |
| 615 | sshbuf_free(c->output); |
| 616 | sshbuf_free(c->extended); |
| 617 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 618 | free(c->remote_name); |
| 619 | c->remote_name = NULL; |
| 620 | free(c->path); |
| 621 | c->path = NULL; |
| 622 | free(c->listening_addr); |
| 623 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 624 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 625 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 626 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 627 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 628 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 629 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 630 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 631 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 632 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 633 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 634 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 635 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 636 | } |
| 637 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 638 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 639 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 640 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 641 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 642 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 643 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 644 | if (ssh->chanctxt->channels[i] != NULL) |
| 645 | channel_free(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 650 | * descriptors after a fork. |
| 651 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 652 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 653 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 654 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 655 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 656 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 657 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 658 | if (ssh->chanctxt->channels[i] != NULL) |
| 659 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 663 | * Stop listening to channels. |
| 664 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 665 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 666 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 667 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 668 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 669 | Channel *c; |
| 670 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 671 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 672 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 673 | if (c != NULL) { |
| 674 | switch (c->type) { |
| 675 | case SSH_CHANNEL_AUTH_SOCKET: |
| 676 | case SSH_CHANNEL_PORT_LISTENER: |
| 677 | case SSH_CHANNEL_RPORT_LISTENER: |
| 678 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 679 | case SSH_CHANNEL_UNIX_LISTENER: |
| 680 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 681 | channel_close_fd(ssh, &c->sock); |
| 682 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 690 | * Returns true if no channel has too much buffered data, and false if one or |
| 691 | * more channel is overfull. |
| 692 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 693 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 694 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 695 | { |
| 696 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 697 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 698 | Channel *c; |
| 699 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 700 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 701 | c = ssh->chanctxt->channels[i]; |
| 702 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 703 | continue; |
| 704 | if (sshbuf_len(c->output) > maxsize) { |
| 705 | debug2("channel %d: big output buffer %zu > %u", |
| 706 | c->self, sshbuf_len(c->output), maxsize); |
| 707 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | return 1; |
| 711 | } |
| 712 | |
| 713 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 714 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 715 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 716 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 717 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 718 | Channel *c; |
| 719 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 720 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 721 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 722 | if (c == NULL) |
| 723 | continue; |
| 724 | switch (c->type) { |
| 725 | case SSH_CHANNEL_X11_LISTENER: |
| 726 | case SSH_CHANNEL_PORT_LISTENER: |
| 727 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 728 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 729 | case SSH_CHANNEL_CLOSED: |
| 730 | case SSH_CHANNEL_AUTH_SOCKET: |
| 731 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 732 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 733 | case SSH_CHANNEL_CONNECTING: |
| 734 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 735 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 736 | case SSH_CHANNEL_UNIX_LISTENER: |
| 737 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 738 | continue; |
| 739 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 740 | continue; |
| 741 | case SSH_CHANNEL_OPENING: |
| 742 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 743 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 744 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 745 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 746 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 747 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 748 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 749 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 750 | /* NOTREACHED */ |
| 751 | } |
| 752 | } |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 757 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 758 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 759 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 760 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 761 | Channel *c; |
| 762 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 763 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 764 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 765 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 766 | continue; |
| 767 | switch (c->type) { |
| 768 | case SSH_CHANNEL_CLOSED: |
| 769 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 770 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 771 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 772 | case SSH_CHANNEL_X11_LISTENER: |
| 773 | case SSH_CHANNEL_PORT_LISTENER: |
| 774 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 775 | case SSH_CHANNEL_MUX_LISTENER: |
| 776 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 777 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 778 | case SSH_CHANNEL_OPENING: |
| 779 | case SSH_CHANNEL_CONNECTING: |
| 780 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 781 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 782 | case SSH_CHANNEL_UNIX_LISTENER: |
| 783 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 784 | continue; |
| 785 | case SSH_CHANNEL_LARVAL: |
| 786 | case SSH_CHANNEL_AUTH_SOCKET: |
| 787 | case SSH_CHANNEL_OPEN: |
| 788 | case SSH_CHANNEL_X11_OPEN: |
| 789 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 790 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 791 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 792 | /* NOTREACHED */ |
| 793 | } |
| 794 | } |
| 795 | return -1; |
| 796 | } |
| 797 | |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 798 | /* Returns the state of the channel's extended usage flag */ |
| 799 | const char * |
| 800 | channel_format_extended_usage(const Channel *c) |
| 801 | { |
| 802 | if (c->efd == -1) |
| 803 | return "closed"; |
| 804 | |
| 805 | switch (c->extended_usage) { |
| 806 | case CHAN_EXTENDED_WRITE: |
| 807 | return "write"; |
| 808 | case CHAN_EXTENDED_READ: |
| 809 | return "read"; |
| 810 | case CHAN_EXTENDED_IGNORE: |
| 811 | return "ignore"; |
| 812 | default: |
| 813 | return "UNKNOWN"; |
| 814 | } |
| 815 | } |
| 816 | |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 817 | static char * |
| 818 | channel_format_status(const Channel *c) |
| 819 | { |
| 820 | char *ret = NULL; |
| 821 | |
| 822 | xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu " |
| 823 | "fd %d/%d/%d sock %d cc %d", |
| 824 | c->type, |
| 825 | c->have_remote_id ? "r" : "nr", c->remote_id, |
| 826 | c->istate, sshbuf_len(c->input), |
| 827 | c->ostate, sshbuf_len(c->output), |
| 828 | channel_format_extended_usage(c), sshbuf_len(c->extended), |
| 829 | c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan); |
| 830 | return ret; |
| 831 | } |
| 832 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 833 | /* |
| 834 | * Returns a message describing the currently open forwarded connections, |
| 835 | * suitable for sending to the client. The message contains crlf pairs for |
| 836 | * newlines. |
| 837 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 838 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 839 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 840 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 841 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 842 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 843 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 844 | int r; |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 845 | char *cp, *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 846 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 847 | if ((buf = sshbuf_new()) == NULL) |
| 848 | fatal("%s: sshbuf_new", __func__); |
| 849 | if ((r = sshbuf_putf(buf, |
| 850 | "The following connections are open:\r\n")) != 0) |
| 851 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 852 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 853 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 854 | if (c == NULL) |
| 855 | continue; |
| 856 | switch (c->type) { |
| 857 | case SSH_CHANNEL_X11_LISTENER: |
| 858 | case SSH_CHANNEL_PORT_LISTENER: |
| 859 | case SSH_CHANNEL_RPORT_LISTENER: |
| 860 | case SSH_CHANNEL_CLOSED: |
| 861 | case SSH_CHANNEL_AUTH_SOCKET: |
| 862 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 863 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 864 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 865 | case SSH_CHANNEL_UNIX_LISTENER: |
| 866 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 867 | continue; |
| 868 | case SSH_CHANNEL_LARVAL: |
| 869 | case SSH_CHANNEL_OPENING: |
| 870 | case SSH_CHANNEL_CONNECTING: |
| 871 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 872 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 873 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 874 | case SSH_CHANNEL_OPEN: |
| 875 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 876 | case SSH_CHANNEL_MUX_PROXY: |
| 877 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 878 | cp = channel_format_status(c); |
| 879 | if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n", |
| 880 | c->self, c->remote_name, cp)) != 0) { |
| 881 | free(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 882 | fatal("%s: sshbuf_putf: %s", |
| 883 | __func__, ssh_err(r)); |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 884 | } |
| 885 | free(cp); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 886 | continue; |
| 887 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 888 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 889 | /* NOTREACHED */ |
| 890 | } |
| 891 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 892 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 893 | fatal("%s: sshbuf_dup_string", __func__); |
| 894 | sshbuf_free(buf); |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | static void |
| 899 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 900 | { |
| 901 | int r; |
| 902 | |
| 903 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 904 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 905 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 906 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 907 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 908 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 909 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 913 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 914 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 915 | Channel *c = channel_lookup(ssh, id); |
| 916 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 917 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 918 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 919 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 922 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 923 | open_preamble(ssh, __func__, c, c->ctype); |
| 924 | if ((r = sshpkt_send(ssh)) != 0) |
| 925 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 929 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 930 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 931 | Channel *c = channel_lookup(ssh, id); |
| 932 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 933 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 934 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 935 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 936 | return; |
| 937 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 938 | if (!c->have_remote_id) |
| 939 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 940 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 941 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 942 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 943 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 944 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 945 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 946 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 947 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 948 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 949 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 950 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 951 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 952 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 953 | { |
| 954 | struct channel_confirm *cc; |
| 955 | Channel *c; |
| 956 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 957 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 958 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 959 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 960 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 961 | cc->cb = cb; |
| 962 | cc->abandon_cb = abandon_cb; |
| 963 | cc->ctx = ctx; |
| 964 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 965 | } |
| 966 | |
| 967 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 968 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 969 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 970 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 971 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 972 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 973 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 974 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 975 | return; |
| 976 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 977 | c->open_confirm = fn; |
| 978 | c->open_confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 979 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 980 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 981 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 982 | channel_register_cleanup(struct ssh *ssh, int id, |
| 983 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 984 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 985 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 986 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 987 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 988 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 991 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 992 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 993 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 994 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 995 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 996 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 997 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 998 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 999 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1000 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1001 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 1004 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1005 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1006 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1007 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1008 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1009 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1010 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1011 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1012 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1013 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1014 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1015 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1016 | return; |
| 1017 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1018 | c->input_filter = ifn; |
| 1019 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1020 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1021 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1025 | channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd, |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 1026 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1027 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1028 | Channel *c = channel_lookup(ssh, id); |
| 1029 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1030 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1031 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 1032 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1033 | if (!c->have_remote_id) |
| 1034 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 1035 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1036 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1037 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 1038 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1039 | |
| 1040 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 1041 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1042 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1043 | (r = sshpkt_send(ssh)) != 0) |
| 1044 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1047 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1048 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 1049 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1050 | { |
| 1051 | FD_SET(c->sock, readset); |
| 1052 | } |
| 1053 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1054 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1055 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 1056 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1057 | { |
| 1058 | debug3("channel %d: waiting for connection", c->self); |
| 1059 | FD_SET(c->sock, writeset); |
| 1060 | } |
| 1061 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1062 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1063 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 1064 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1065 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1066 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1067 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1068 | sshbuf_len(c->input) < c->remote_window && |
| 1069 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1070 | FD_SET(c->rfd, readset); |
| 1071 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1072 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1073 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1074 | FD_SET(c->wfd, writeset); |
| 1075 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1076 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1077 | debug2("channel %d: " |
| 1078 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 1079 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1080 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1081 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1085 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 1086 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1087 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1088 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1089 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1090 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1091 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1092 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1093 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1094 | FD_SET(c->efd, readset); |
| 1095 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1096 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1097 | } |
| 1098 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1099 | /* |
| 1100 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1101 | * connection (when authentication spoofing is being done) remains in this |
| 1102 | * state until the first packet has been completely read. The authentication |
| 1103 | * data in that packet is then substituted by the real data if it matches the |
| 1104 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1105 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1106 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1107 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1108 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1109 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1110 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1111 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1112 | u_char *ucp; |
| 1113 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1114 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1115 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1116 | if (sc->x11_refuse_time != 0 && |
| 1117 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1118 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1119 | "expired"); |
| 1120 | return -1; |
| 1121 | } |
| 1122 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1123 | /* Check if the fixed size part of the packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1124 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1125 | return 0; |
| 1126 | |
| 1127 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1128 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1129 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1130 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1131 | data_len = 256 * ucp[8] + ucp[9]; |
| 1132 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1133 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1134 | data_len = ucp[8] + 256 * ucp[9]; |
| 1135 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1136 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1137 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1138 | return -1; |
| 1139 | } |
| 1140 | |
| 1141 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1142 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1143 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1144 | return 0; |
| 1145 | |
| 1146 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1147 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1148 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1149 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1150 | return -1; |
| 1151 | } |
| 1152 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1153 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1154 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1155 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1156 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1157 | return -1; |
| 1158 | } |
| 1159 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1160 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1161 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1162 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1163 | return -1; |
| 1164 | } |
| 1165 | /* |
| 1166 | * Received authentication protocol and data match |
| 1167 | * our fake data. Substitute the fake data with real |
| 1168 | * data. |
| 1169 | */ |
| 1170 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1171 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1175 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1176 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1177 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1178 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1179 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1180 | |
| 1181 | /* c->force_drain = 1; */ |
| 1182 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1183 | if (ret == 1) { |
| 1184 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1185 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1186 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1187 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1188 | debug2("X11 rejected %d i%d/o%d", |
| 1189 | c->self, c->istate, c->ostate); |
| 1190 | chan_read_failed(ssh, c); |
| 1191 | sshbuf_reset(c->input); |
| 1192 | chan_ibuf_empty(ssh, c); |
| 1193 | sshbuf_reset(c->output); |
| 1194 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1195 | debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1199 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1200 | channel_pre_mux_client(struct ssh *ssh, |
| 1201 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1202 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1203 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1204 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1205 | FD_SET(c->rfd, readset); |
| 1206 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1207 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1208 | sshbuf_reset(c->input); |
| 1209 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1210 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1211 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1212 | } |
| 1213 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1214 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1215 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1216 | FD_SET(c->wfd, writeset); |
| 1217 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1218 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1222 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1223 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1224 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1225 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1226 | const u_char *p; |
| 1227 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1228 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1229 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1230 | struct { |
| 1231 | u_int8_t version; |
| 1232 | u_int8_t command; |
| 1233 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1234 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1235 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1236 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1237 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1238 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1239 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1240 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1241 | len = sizeof(s4_req); |
| 1242 | if (have < len) |
| 1243 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1244 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1245 | |
| 1246 | need = 1; |
| 1247 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1248 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1249 | debug2("channel %d: socks4a request", c->self); |
| 1250 | /* ... and needs an extra string (the hostname) */ |
| 1251 | need = 2; |
| 1252 | } |
| 1253 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1254 | for (found = 0, i = len; i < have; i++) { |
| 1255 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1256 | found++; |
| 1257 | if (found == need) |
| 1258 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1259 | } |
| 1260 | if (i > 1024) { |
| 1261 | /* the peer is probably sending garbage */ |
| 1262 | debug("channel %d: decode socks4: too long", |
| 1263 | c->self); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1267 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1268 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1269 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1270 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1271 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1272 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1273 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1274 | return -1; |
| 1275 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1276 | have = sshbuf_len(input); |
| 1277 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1278 | if (memchr(p, '\0', have) == NULL) { |
| 1279 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1280 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1281 | return -1; |
| 1282 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1283 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1284 | debug2("channel %d: decode socks4: user %s/%d", c->self, p, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1285 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1286 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1287 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1288 | fatal("%s: channel %d: consume: %s", __func__, |
| 1289 | c->self, ssh_err(r)); |
| 1290 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1291 | free(c->path); |
| 1292 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1293 | if (need == 1) { /* SOCKS4: one string */ |
| 1294 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1295 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1296 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1297 | have = sshbuf_len(input); |
| 1298 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1299 | if (memchr(p, '\0', have) == NULL) { |
| 1300 | error("channel %d: decode socks4a: host not nul " |
| 1301 | "terminated", c->self); |
| 1302 | return -1; |
| 1303 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1304 | len = strlen(p); |
| 1305 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1306 | c->self, p, len); |
| 1307 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1308 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1309 | error("channel %d: hostname \"%.100s\" too long", |
| 1310 | c->self, p); |
| 1311 | return -1; |
| 1312 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1313 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1314 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1315 | fatal("%s: channel %d: consume: %s", __func__, |
| 1316 | c->self, ssh_err(r)); |
| 1317 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1318 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1319 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1320 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1321 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1322 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1323 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1324 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1325 | debug("channel %d: cannot handle: %s cn %d", |
| 1326 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1327 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1328 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1329 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1330 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1331 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1332 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1333 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1334 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1335 | c->self, ssh_err(r)); |
| 1336 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1337 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1340 | /* try to decode a socks5 header */ |
| 1341 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1342 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1343 | #define SSH_SOCKS5_IPV4 0x01 |
| 1344 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1345 | #define SSH_SOCKS5_IPV6 0x04 |
| 1346 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1347 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1348 | |
| 1349 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1350 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1351 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1352 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1353 | struct { |
| 1354 | u_int8_t version; |
| 1355 | u_int8_t command; |
| 1356 | u_int8_t reserved; |
| 1357 | u_int8_t atyp; |
| 1358 | } s5_req, s5_rsp; |
| 1359 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1360 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1361 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1362 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1363 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1364 | |
| 1365 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1366 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1367 | if (p[0] != 0x05) |
| 1368 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1369 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1370 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1371 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1372 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1373 | return 0; |
| 1374 | nmethods = p[1]; |
| 1375 | if (have < nmethods + 2) |
| 1376 | return 0; |
| 1377 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1378 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1379 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1380 | found = 1; |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | if (!found) { |
| 1385 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1386 | c->self); |
| 1387 | return -1; |
| 1388 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1389 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1390 | fatal("%s: channel %d: consume: %s", __func__, |
| 1391 | c->self, ssh_err(r)); |
| 1392 | } |
| 1393 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1394 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1395 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1396 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1397 | c->self, ssh_err(r)); |
| 1398 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1399 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1400 | debug2("channel %d: socks5 auth done", c->self); |
| 1401 | return 0; /* need more */ |
| 1402 | } |
| 1403 | debug2("channel %d: socks5 post auth", c->self); |
| 1404 | if (have < sizeof(s5_req)+1) |
| 1405 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1406 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1407 | if (s5_req.version != 0x05 || |
| 1408 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1409 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1410 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1411 | return -1; |
| 1412 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1413 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1414 | case SSH_SOCKS5_IPV4: |
| 1415 | addrlen = 4; |
| 1416 | af = AF_INET; |
| 1417 | break; |
| 1418 | case SSH_SOCKS5_DOMAIN: |
| 1419 | addrlen = p[sizeof(s5_req)]; |
| 1420 | af = -1; |
| 1421 | break; |
| 1422 | case SSH_SOCKS5_IPV6: |
| 1423 | addrlen = 16; |
| 1424 | af = AF_INET6; |
| 1425 | break; |
| 1426 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1427 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1428 | return -1; |
| 1429 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1430 | need = sizeof(s5_req) + addrlen + 2; |
| 1431 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1432 | need++; |
| 1433 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1434 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1435 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1436 | fatal("%s: channel %d: consume: %s", __func__, |
| 1437 | c->self, ssh_err(r)); |
| 1438 | } |
| 1439 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1440 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1441 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1442 | fatal("%s: channel %d: consume: %s", __func__, |
| 1443 | c->self, ssh_err(r)); |
| 1444 | } |
| 1445 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1446 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1447 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1448 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1449 | return -1; |
| 1450 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1451 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1452 | free(c->path); |
| 1453 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1454 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1455 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1456 | error("channel %d: dynamic request: socks5 hostname " |
| 1457 | "\"%.100s\" too long", c->self, dest_addr); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | c->path = xstrdup(dest_addr); |
| 1461 | } else { |
| 1462 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1463 | return -1; |
| 1464 | c->path = xstrdup(ntop); |
| 1465 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1466 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1467 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1468 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1469 | c->self, c->path, c->host_port, s5_req.command); |
| 1470 | |
| 1471 | s5_rsp.version = 0x05; |
| 1472 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1473 | s5_rsp.reserved = 0; /* ignored */ |
| 1474 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1475 | dest_port = 0; /* ignored */ |
| 1476 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1477 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1478 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1479 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1480 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1481 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1482 | return 1; |
| 1483 | } |
| 1484 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1485 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1486 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1487 | const char *host_to_connect, u_short port_to_connect, int in, int out) |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1488 | { |
| 1489 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1490 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1491 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1492 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1493 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1494 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1495 | 0, "stdio-forward", /*nonblock*/0); |
| 1496 | |
| 1497 | c->path = xstrdup(host_to_connect); |
| 1498 | c->host_port = port_to_connect; |
| 1499 | c->listening_port = 0; |
| 1500 | c->force_drain = 1; |
| 1501 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1502 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1503 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1504 | |
| 1505 | return c; |
| 1506 | } |
| 1507 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1508 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1509 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1510 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1511 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1512 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1513 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1514 | u_int have; |
| 1515 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1516 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1517 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1518 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1519 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1520 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1521 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1522 | /* need more */ |
| 1523 | FD_SET(c->sock, readset); |
| 1524 | return; |
| 1525 | } |
| 1526 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1527 | p = sshbuf_ptr(c->input); |
| 1528 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1529 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1530 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1531 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1532 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1533 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1534 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1535 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1536 | default: |
| 1537 | ret = -1; |
| 1538 | break; |
| 1539 | } |
| 1540 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1541 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1542 | } else if (ret == 0) { |
| 1543 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1544 | /* need more */ |
| 1545 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1546 | if (sshbuf_len(c->output)) |
| 1547 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1548 | } else { |
| 1549 | /* switch to the next state */ |
| 1550 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1551 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1554 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1555 | /* simulate read-error */ |
| 1556 | static void |
| 1557 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1558 | { |
| 1559 | c->type = SSH_CHANNEL_OPEN; |
| 1560 | chan_read_failed(ssh, c); |
| 1561 | sshbuf_reset(c->input); |
| 1562 | chan_ibuf_empty(ssh, c); |
| 1563 | sshbuf_reset(c->output); |
| 1564 | chan_write_failed(ssh, c); |
| 1565 | } |
| 1566 | |
| 1567 | /* reverse dynamic port forwarding */ |
| 1568 | static void |
| 1569 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1570 | { |
| 1571 | const u_char *p; |
| 1572 | u_int have, len; |
| 1573 | int r, ret; |
| 1574 | |
| 1575 | have = sshbuf_len(c->output); |
| 1576 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1577 | /* sshbuf_dump(c->output, stderr); */ |
| 1578 | /* EOF received */ |
| 1579 | if (c->flags & CHAN_EOF_RCVD) { |
| 1580 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1581 | fatal("%s: channel %d: consume: %s", |
| 1582 | __func__, c->self, ssh_err(r)); |
| 1583 | } |
| 1584 | rdynamic_close(ssh, c); |
| 1585 | return; |
| 1586 | } |
| 1587 | /* check if the fixed size part of the packet is in buffer. */ |
| 1588 | if (have < 3) |
| 1589 | return; |
| 1590 | /* try to guess the protocol */ |
| 1591 | p = sshbuf_ptr(c->output); |
| 1592 | switch (p[0]) { |
| 1593 | case 0x04: |
| 1594 | /* switch input/output for reverse forwarding */ |
| 1595 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1596 | break; |
| 1597 | case 0x05: |
| 1598 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1599 | break; |
| 1600 | default: |
| 1601 | ret = -1; |
| 1602 | break; |
| 1603 | } |
| 1604 | if (ret < 0) { |
| 1605 | rdynamic_close(ssh, c); |
| 1606 | } else if (ret == 0) { |
| 1607 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1608 | /* send socks request to peer */ |
| 1609 | len = sshbuf_len(c->input); |
| 1610 | if (len > 0 && len < c->remote_window) { |
| 1611 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1612 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1613 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1614 | (r = sshpkt_send(ssh)) != 0) { |
| 1615 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1616 | c->self, ssh_err(r)); |
| 1617 | } |
| 1618 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1619 | fatal("%s: channel %d: consume: %s", |
| 1620 | __func__, c->self, ssh_err(r)); |
| 1621 | } |
| 1622 | c->remote_window -= len; |
| 1623 | } |
| 1624 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1625 | /* the connect failed */ |
| 1626 | rdynamic_close(ssh, c); |
| 1627 | } |
| 1628 | } |
| 1629 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1630 | /* This is our fake X11 server socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1631 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1632 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1633 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1634 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1635 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1636 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1637 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1638 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1639 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1640 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1641 | if (!FD_ISSET(c->sock, readset)) |
| 1642 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1643 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1644 | debug("X11 connection requested."); |
| 1645 | addrlen = sizeof(addr); |
| 1646 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1647 | if (c->single_connection) { |
| 1648 | oerrno = errno; |
| 1649 | debug2("single_connection: closing X11 listener."); |
| 1650 | channel_close_fd(ssh, &c->sock); |
| 1651 | chan_mark_dead(ssh, c); |
| 1652 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1653 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1654 | if (newsock < 0) { |
| 1655 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1656 | errno != ECONNABORTED) |
| 1657 | error("accept: %.100s", strerror(errno)); |
| 1658 | if (errno == EMFILE || errno == ENFILE) |
| 1659 | c->notbefore = monotime() + 1; |
| 1660 | return; |
| 1661 | } |
| 1662 | set_nodelay(newsock); |
| 1663 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1664 | remote_port = get_peer_port(newsock); |
| 1665 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1666 | remote_ipaddr, remote_port); |
| 1667 | |
| 1668 | nc = channel_new(ssh, "accepted x11 socket", |
| 1669 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1670 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1671 | open_preamble(ssh, __func__, nc, "x11"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1672 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1673 | (r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1674 | fatal("%s: channel %i: reply %s", __func__, |
| 1675 | c->self, ssh_err(r)); |
| 1676 | } |
| 1677 | if ((r = sshpkt_send(ssh)) != 0) |
| 1678 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
| 1679 | free(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1680 | } |
| 1681 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1682 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1683 | port_open_helper(struct ssh *ssh, Channel *c, char *rtype) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1684 | { |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1685 | char *local_ipaddr = get_local_ipaddr(c->sock); |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 1686 | int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1687 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1688 | int remote_port = get_peer_port(c->sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1689 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1690 | |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1691 | if (remote_port == -1) { |
| 1692 | /* Fake addr/port to appease peers that validate it (Tectia) */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1693 | free(remote_ipaddr); |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1694 | remote_ipaddr = xstrdup("127.0.0.1"); |
| 1695 | remote_port = 65535; |
| 1696 | } |
| 1697 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1698 | free(c->remote_name); |
| 1699 | xasprintf(&c->remote_name, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1700 | "%s: listening port %d for %.100s port %d, " |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1701 | "connect from %.200s port %d to %.100s port %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1702 | rtype, c->listening_port, c->path, c->host_port, |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1703 | remote_ipaddr, remote_port, local_ipaddr, local_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1704 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1705 | open_preamble(ssh, __func__, c, rtype); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1706 | if (strcmp(rtype, "direct-tcpip") == 0) { |
| 1707 | /* target host, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1708 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1709 | (r = sshpkt_put_u32(ssh, c->host_port)) != 0) { |
| 1710 | fatal("%s: channel %i: reply %s", __func__, |
| 1711 | c->self, ssh_err(r)); |
| 1712 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1713 | } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) { |
| 1714 | /* target path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1715 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1716 | fatal("%s: channel %i: reply %s", __func__, |
| 1717 | c->self, ssh_err(r)); |
| 1718 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1719 | } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1720 | /* listen path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1721 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1722 | fatal("%s: channel %i: reply %s", __func__, |
| 1723 | c->self, ssh_err(r)); |
| 1724 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1725 | } else { |
| 1726 | /* listen address, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1727 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1728 | (r = sshpkt_put_u32(ssh, local_port)) != 0) { |
| 1729 | fatal("%s: channel %i: reply %s", __func__, |
| 1730 | c->self, ssh_err(r)); |
| 1731 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1732 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1733 | if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1734 | /* reserved for future owner/mode info */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1735 | if ((r = sshpkt_put_cstring(ssh, "")) != 0) { |
| 1736 | fatal("%s: channel %i: reply %s", __func__, |
| 1737 | c->self, ssh_err(r)); |
| 1738 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1739 | } else { |
| 1740 | /* originator host and port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1741 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1742 | (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) { |
| 1743 | fatal("%s: channel %i: reply %s", __func__, |
| 1744 | c->self, ssh_err(r)); |
| 1745 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1746 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1747 | if ((r = sshpkt_send(ssh)) != 0) |
| 1748 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1749 | free(remote_ipaddr); |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1750 | free(local_ipaddr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1753 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1754 | channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time) |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1755 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1756 | ssh->chanctxt->x11_refuse_time = refuse_time; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1759 | /* |
| 1760 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1761 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1762 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1763 | channel_post_port_listener(struct ssh *ssh, Channel *c, |
| 1764 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1765 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1766 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1767 | struct sockaddr_storage addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1768 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1769 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1770 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1771 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1772 | if (!FD_ISSET(c->sock, readset)) |
| 1773 | return; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1774 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1775 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
| 1776 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1777 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1778 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1779 | nextstate = SSH_CHANNEL_OPENING; |
| 1780 | rtype = "forwarded-tcpip"; |
| 1781 | } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) { |
| 1782 | nextstate = SSH_CHANNEL_OPENING; |
| 1783 | rtype = "forwarded-streamlocal@openssh.com"; |
| 1784 | } else if (c->host_port == PORT_STREAMLOCAL) { |
| 1785 | nextstate = SSH_CHANNEL_OPENING; |
| 1786 | rtype = "direct-streamlocal@openssh.com"; |
| 1787 | } else if (c->host_port == 0) { |
| 1788 | nextstate = SSH_CHANNEL_DYNAMIC; |
| 1789 | rtype = "dynamic-tcpip"; |
| 1790 | } else { |
| 1791 | nextstate = SSH_CHANNEL_OPENING; |
| 1792 | rtype = "direct-tcpip"; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1793 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1794 | |
| 1795 | addrlen = sizeof(addr); |
| 1796 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1797 | if (newsock < 0) { |
| 1798 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1799 | errno != ECONNABORTED) |
| 1800 | error("accept: %.100s", strerror(errno)); |
| 1801 | if (errno == EMFILE || errno == ENFILE) |
| 1802 | c->notbefore = monotime() + 1; |
| 1803 | return; |
| 1804 | } |
| 1805 | if (c->host_port != PORT_STREAMLOCAL) |
| 1806 | set_nodelay(newsock); |
| 1807 | nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1, |
| 1808 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
| 1809 | nc->listening_port = c->listening_port; |
| 1810 | nc->host_port = c->host_port; |
| 1811 | if (c->path != NULL) |
| 1812 | nc->path = xstrdup(c->path); |
| 1813 | |
| 1814 | if (nextstate != SSH_CHANNEL_DYNAMIC) |
| 1815 | port_open_helper(ssh, nc, rtype); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | /* |
| 1819 | * This is the authentication agent socket listening for connections from |
| 1820 | * clients. |
| 1821 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1822 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1823 | channel_post_auth_listener(struct ssh *ssh, Channel *c, |
| 1824 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1825 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1826 | Channel *nc; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1827 | int r, newsock; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1828 | struct sockaddr_storage addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1829 | socklen_t addrlen; |
| 1830 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1831 | if (!FD_ISSET(c->sock, readset)) |
| 1832 | return; |
| 1833 | |
| 1834 | addrlen = sizeof(addr); |
| 1835 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1836 | if (newsock < 0) { |
| 1837 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1838 | if (errno == EMFILE || errno == ENFILE) |
| 1839 | c->notbefore = monotime() + 1; |
| 1840 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1841 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1842 | nc = channel_new(ssh, "accepted auth socket", |
| 1843 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1844 | c->local_window_max, c->local_maxpacket, |
| 1845 | 0, "accepted auth socket", 1); |
| 1846 | open_preamble(ssh, __func__, nc, "auth-agent@openssh.com"); |
| 1847 | if ((r = sshpkt_send(ssh)) != 0) |
| 1848 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1849 | } |
| 1850 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1851 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1852 | channel_post_connecting(struct ssh *ssh, Channel *c, |
| 1853 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1854 | { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1855 | int err = 0, sock, isopen, r; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1856 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1857 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1858 | if (!FD_ISSET(c->sock, writeset)) |
| 1859 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1860 | if (!c->have_remote_id) |
| 1861 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1862 | /* for rdynamic the OPEN_CONFIRMATION has been sent already */ |
| 1863 | isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1864 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { |
| 1865 | err = errno; |
| 1866 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1867 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1868 | if (err == 0) { |
| 1869 | debug("channel %d: connected to %s port %d", |
| 1870 | c->self, c->connect_ctx.host, c->connect_ctx.port); |
| 1871 | channel_connect_ctx_free(&c->connect_ctx); |
| 1872 | c->type = SSH_CHANNEL_OPEN; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1873 | if (isopen) { |
| 1874 | /* no message necessary */ |
| 1875 | } else { |
| 1876 | if ((r = sshpkt_start(ssh, |
| 1877 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1878 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1879 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1880 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1881 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) |
| 1882 | != 0) |
| 1883 | fatal("%s: channel %i: confirm: %s", __func__, |
| 1884 | c->self, ssh_err(r)); |
| 1885 | if ((r = sshpkt_send(ssh)) != 0) |
| 1886 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1887 | ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1888 | } |
| 1889 | } else { |
| 1890 | debug("channel %d: connection failed: %s", |
| 1891 | c->self, strerror(err)); |
| 1892 | /* Try next address, if any */ |
| 1893 | if ((sock = connect_next(&c->connect_ctx)) > 0) { |
| 1894 | close(c->sock); |
| 1895 | c->sock = c->rfd = c->wfd = sock; |
| 1896 | channel_find_maxfd(ssh->chanctxt); |
| 1897 | return; |
| 1898 | } |
| 1899 | /* Exhausted all addresses */ |
| 1900 | error("connect_to %.100s port %d: failed.", |
| 1901 | c->connect_ctx.host, c->connect_ctx.port); |
| 1902 | channel_connect_ctx_free(&c->connect_ctx); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1903 | if (isopen) { |
| 1904 | rdynamic_close(ssh, c); |
| 1905 | } else { |
| 1906 | if ((r = sshpkt_start(ssh, |
| 1907 | SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1908 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1909 | (r = sshpkt_put_u32(ssh, |
| 1910 | SSH2_OPEN_CONNECT_FAILED)) != 0 || |
| 1911 | (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1912 | (r = sshpkt_put_cstring(ssh, "")) != 0) { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1913 | fatal("%s: channel %i: failure: %s", __func__, |
| 1914 | c->self, ssh_err(r)); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1915 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1916 | if ((r = sshpkt_send(ssh)) != 0) |
| 1917 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1918 | ssh_err(r)); |
| 1919 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1920 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1921 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1924 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1925 | channel_handle_rfd(struct ssh *ssh, Channel *c, |
| 1926 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1927 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1928 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1929 | ssize_t len; |
| 1930 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1931 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1932 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1933 | |
| 1934 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1935 | return 1; |
| 1936 | |
| 1937 | errno = 0; |
| 1938 | len = read(c->rfd, buf, sizeof(buf)); |
| 1939 | if (len < 0 && (errno == EINTR || |
| 1940 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1941 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1942 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1943 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1944 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1945 | if ((!c->isatty && len <= 0) || |
| 1946 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1947 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1948 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1949 | c->self, c->rfd, len); |
| 1950 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1951 | debug2("channel %d: not open", c->self); |
| 1952 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1953 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1954 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1955 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1956 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1957 | return -1; |
| 1958 | } |
| 1959 | if (c->input_filter != NULL) { |
| 1960 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1961 | debug2("channel %d: filter stops", c->self); |
| 1962 | chan_read_failed(ssh, c); |
| 1963 | } |
| 1964 | } else if (c->datagram) { |
| 1965 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1966 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1967 | c->self, ssh_err(r)); |
| 1968 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1969 | fatal("%s: channel %d: put data: %s", __func__, |
| 1970 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1971 | } |
| 1972 | return 1; |
| 1973 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1974 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1975 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1976 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1977 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1978 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1979 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1980 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 1981 | size_t dlen, olen = 0; |
| 1982 | int r, len; |
| 1983 | |
| 1984 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 1985 | sshbuf_len(c->output) == 0) |
| 1986 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1987 | |
| 1988 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1989 | olen = sshbuf_len(c->output); |
| 1990 | if (c->output_filter != NULL) { |
| 1991 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 1992 | debug2("channel %d: filter stops", c->self); |
| 1993 | if (c->type != SSH_CHANNEL_OPEN) |
| 1994 | chan_mark_dead(ssh, c); |
| 1995 | else |
| 1996 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1997 | return -1; |
| 1998 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1999 | } else if (c->datagram) { |
| 2000 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 2001 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 2002 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 2003 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2004 | } else { |
| 2005 | buf = data = sshbuf_mutable_ptr(c->output); |
| 2006 | dlen = sshbuf_len(c->output); |
| 2007 | } |
| 2008 | |
| 2009 | if (c->datagram) { |
| 2010 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 2011 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2012 | free(data); |
| 2013 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2014 | errno == EWOULDBLOCK)) |
| 2015 | return 1; |
| 2016 | if (len <= 0) |
| 2017 | goto write_fail; |
| 2018 | goto out; |
| 2019 | } |
| 2020 | |
| 2021 | #ifdef _AIX |
| 2022 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 2023 | if (c->wfd_isatty) |
| 2024 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 2025 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2026 | |
| 2027 | len = write(c->wfd, buf, dlen); |
| 2028 | if (len < 0 && |
| 2029 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 2030 | return 1; |
| 2031 | if (len <= 0) { |
| 2032 | write_fail: |
| 2033 | if (c->type != SSH_CHANNEL_OPEN) { |
| 2034 | debug2("channel %d: not open", c->self); |
| 2035 | chan_mark_dead(ssh, c); |
| 2036 | return -1; |
| 2037 | } else { |
| 2038 | chan_write_failed(ssh, c); |
| 2039 | } |
| 2040 | return -1; |
| 2041 | } |
| 2042 | #ifndef BROKEN_TCGETATTR_ICANON |
| 2043 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 2044 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 2045 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 2046 | /* |
| 2047 | * Simulate echo to reduce the impact of |
| 2048 | * traffic analysis. We need to match the |
| 2049 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 2050 | * (4 byte channel id + buf) |
| 2051 | */ |
| 2052 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 2053 | (r = sshpkt_send(ssh)) != 0) |
| 2054 | fatal("%s: channel %d: ignore: %s", |
| 2055 | __func__, c->self, ssh_err(r)); |
| 2056 | } |
| 2057 | } |
| 2058 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 2059 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 2060 | fatal("%s: channel %d: consume: %s", |
| 2061 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2062 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2063 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2064 | c->local_consumed += olen - sshbuf_len(c->output); |
| 2065 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2066 | return 1; |
| 2067 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2068 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2069 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2070 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 2071 | fd_set *readset, fd_set *writeset) |
| 2072 | { |
| 2073 | int r; |
| 2074 | ssize_t len; |
| 2075 | |
| 2076 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 2077 | return 1; |
| 2078 | |
| 2079 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 2080 | sshbuf_len(c->extended)); |
| 2081 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
| 2082 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2083 | errno == EWOULDBLOCK)) |
| 2084 | return 1; |
| 2085 | if (len <= 0) { |
| 2086 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2087 | channel_close_fd(ssh, &c->efd); |
| 2088 | } else { |
| 2089 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2090 | fatal("%s: channel %d: consume: %s", |
| 2091 | __func__, c->self, ssh_err(r)); |
| 2092 | } |
| 2093 | c->local_consumed += len; |
| 2094 | } |
| 2095 | return 1; |
| 2096 | } |
| 2097 | |
| 2098 | static int |
| 2099 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2100 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2101 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2102 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2103 | int r; |
| 2104 | ssize_t len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2105 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2106 | if (!c->detach_close && !FD_ISSET(c->efd, readset)) |
| 2107 | return 1; |
| 2108 | |
| 2109 | len = read(c->efd, buf, sizeof(buf)); |
| 2110 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
| 2111 | if (len < 0 && (errno == EINTR || ((errno == EAGAIN || |
| 2112 | errno == EWOULDBLOCK) && !c->detach_close))) |
| 2113 | return 1; |
| 2114 | if (len <= 0) { |
| 2115 | debug2("channel %d: closing read-efd %d", |
| 2116 | c->self, c->efd); |
| 2117 | channel_close_fd(ssh, &c->efd); |
| 2118 | } else { |
| 2119 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2120 | debug3("channel %d: discard efd", |
| 2121 | c->self); |
| 2122 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2123 | fatal("%s: channel %d: append: %s", |
| 2124 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | return 1; |
| 2128 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2129 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2130 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2131 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2132 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2133 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2134 | if (c->efd == -1) |
| 2135 | return 1; |
| 2136 | |
| 2137 | /** XXX handle drain efd, too */ |
| 2138 | |
| 2139 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2140 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2141 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2142 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2143 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2144 | |
| 2145 | return 1; |
| 2146 | } |
| 2147 | |
| 2148 | static int |
| 2149 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2150 | { |
| 2151 | int r; |
| 2152 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2153 | if (c->type == SSH_CHANNEL_OPEN && |
| 2154 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2155 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2156 | c->local_maxpacket*3) || |
| 2157 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2158 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2159 | if (!c->have_remote_id) |
| 2160 | fatal(":%s: channel %d: no remote id", |
| 2161 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2162 | if ((r = sshpkt_start(ssh, |
| 2163 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2164 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2165 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2166 | (r = sshpkt_send(ssh)) != 0) { |
| 2167 | fatal("%s: channel %i: %s", __func__, |
| 2168 | c->self, ssh_err(r)); |
| 2169 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2170 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2171 | c->self, c->local_window, |
| 2172 | c->local_consumed); |
| 2173 | c->local_window += c->local_consumed; |
| 2174 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2175 | } |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2179 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2180 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2181 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2182 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2183 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2184 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2185 | channel_handle_efd(ssh, c, readset, writeset); |
| 2186 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2187 | } |
| 2188 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2189 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2190 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2191 | { |
| 2192 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2193 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2194 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2195 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2196 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2197 | if (sshbuf_len(c->input) < need) { |
| 2198 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2199 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2200 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2201 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2202 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2203 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2204 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2205 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2206 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2207 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2208 | fatal("%s: channel %d: append: %s", |
| 2209 | __func__, c->self, ssh_err(r)); |
| 2210 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2211 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2212 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2216 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2217 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2218 | { |
| 2219 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2220 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2221 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2222 | return; |
| 2223 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2224 | return; |
| 2225 | if (c->mux_pause) |
| 2226 | return; |
| 2227 | |
| 2228 | /* |
| 2229 | * Don't not read past the precise end of packets to |
| 2230 | * avoid disrupting fd passing. |
| 2231 | */ |
| 2232 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2233 | return; |
| 2234 | /* XXX sshbuf_peek_u32 */ |
| 2235 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2236 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2237 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2238 | debug2("channel %d: packet too big %u > %u", |
| 2239 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2240 | chan_rcvd_oclose(ssh, c); |
| 2241 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2242 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2243 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2244 | return; |
| 2245 | if (c->mux_rcb(ssh, c) != 0) { |
| 2246 | debug("channel %d: mux_rcb failed", c->self); |
| 2247 | chan_mark_dead(ssh, c); |
| 2248 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2253 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2254 | fd_set *readset, fd_set *writeset) |
| 2255 | { |
| 2256 | ssize_t len; |
| 2257 | int r; |
| 2258 | |
| 2259 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2260 | sshbuf_len(c->output) == 0) |
| 2261 | return; |
| 2262 | |
| 2263 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
| 2264 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 2265 | return; |
| 2266 | if (len <= 0) { |
| 2267 | chan_mark_dead(ssh, c); |
| 2268 | return; |
| 2269 | } |
| 2270 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2271 | fatal("%s: channel %d: consume: %s", __func__, |
| 2272 | c->self, ssh_err(r)); |
| 2273 | } |
| 2274 | |
| 2275 | static void |
| 2276 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2277 | fd_set *readset, fd_set *writeset) |
| 2278 | { |
| 2279 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2280 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2281 | } |
| 2282 | |
| 2283 | static void |
| 2284 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2285 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2286 | { |
| 2287 | Channel *nc; |
| 2288 | struct sockaddr_storage addr; |
| 2289 | socklen_t addrlen; |
| 2290 | int newsock; |
| 2291 | uid_t euid; |
| 2292 | gid_t egid; |
| 2293 | |
| 2294 | if (!FD_ISSET(c->sock, readset)) |
| 2295 | return; |
| 2296 | |
| 2297 | debug("multiplexing control connection"); |
| 2298 | |
| 2299 | /* |
| 2300 | * Accept connection on control socket |
| 2301 | */ |
| 2302 | memset(&addr, 0, sizeof(addr)); |
| 2303 | addrlen = sizeof(addr); |
| 2304 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2305 | &addrlen)) == -1) { |
| 2306 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2307 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2308 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2309 | return; |
| 2310 | } |
| 2311 | |
| 2312 | if (getpeereid(newsock, &euid, &egid) < 0) { |
| 2313 | error("%s getpeereid failed: %s", __func__, |
| 2314 | strerror(errno)); |
| 2315 | close(newsock); |
| 2316 | return; |
| 2317 | } |
| 2318 | if ((euid != 0) && (getuid() != euid)) { |
| 2319 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2320 | (u_int)euid, (u_int)getuid()); |
| 2321 | close(newsock); |
| 2322 | return; |
| 2323 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2324 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2325 | newsock, newsock, -1, c->local_window_max, |
| 2326 | c->local_maxpacket, 0, "mux-control", 1); |
| 2327 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2328 | debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2329 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2330 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2331 | /* mux state transitions must not elicit protocol messages */ |
| 2332 | nc->flags |= CHAN_LOCAL; |
| 2333 | } |
| 2334 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2335 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2336 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2337 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2338 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2339 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2340 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2341 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2342 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2343 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2344 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2345 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2346 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2347 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2348 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2349 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2350 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2351 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2352 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2353 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2354 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2355 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2356 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2357 | |
| 2358 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2359 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2360 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2361 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2362 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2363 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2364 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2365 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2366 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2367 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2368 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2369 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2370 | |
| 2371 | sc->channel_pre = pre; |
| 2372 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2373 | } |
| 2374 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2375 | /* gc dead channels */ |
| 2376 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2377 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2378 | { |
| 2379 | if (c == NULL) |
| 2380 | return; |
| 2381 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2382 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2383 | return; |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 2384 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2385 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2386 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2387 | /* if we still have a callback */ |
| 2388 | if (c->detach_user != NULL) |
| 2389 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2390 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2391 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2392 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2393 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2394 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2395 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2396 | } |
| 2397 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2398 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2399 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2400 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2401 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2402 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2403 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2404 | struct ssh_channels *sc = ssh->chanctxt; |
| 2405 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2406 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2407 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2408 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2409 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2410 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2411 | if (unpause_secs != NULL) |
| 2412 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2413 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2414 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2415 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2416 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2417 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2418 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2419 | c->delayed = 0; |
| 2420 | else |
| 2421 | continue; |
| 2422 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2423 | if (ftab[c->type] != NULL) { |
| 2424 | /* |
| 2425 | * Run handlers that are not paused. |
| 2426 | */ |
| 2427 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2428 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2429 | else if (unpause_secs != NULL) { |
| 2430 | /* |
| 2431 | * Collect the time that the earliest |
| 2432 | * channel comes off pause. |
| 2433 | */ |
| 2434 | debug3("%s: chan %d: skip for %d more seconds", |
| 2435 | __func__, c->self, |
| 2436 | (int)(c->notbefore - now)); |
| 2437 | if (*unpause_secs == 0 || |
| 2438 | (c->notbefore - now) < *unpause_secs) |
| 2439 | *unpause_secs = c->notbefore - now; |
| 2440 | } |
| 2441 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2442 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2443 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2444 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2445 | debug3("%s: first channel unpauses in %d seconds", |
| 2446 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2447 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2448 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2449 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2450 | * Create sockets before allocating the select bitmasks. |
| 2451 | * This is necessary for things that need to happen after reading |
| 2452 | * the network-input but before channel_prepare_select(). |
| 2453 | */ |
| 2454 | static void |
| 2455 | channel_before_prepare_select(struct ssh *ssh) |
| 2456 | { |
| 2457 | struct ssh_channels *sc = ssh->chanctxt; |
| 2458 | Channel *c; |
| 2459 | u_int i, oalloc; |
| 2460 | |
| 2461 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2462 | c = sc->channels[i]; |
| 2463 | if (c == NULL) |
| 2464 | continue; |
| 2465 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2466 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2471 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2472 | * select bitmasks. |
| 2473 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2474 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2475 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2476 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2477 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2478 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2479 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2480 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2481 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2482 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2483 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2484 | nfdset = howmany(n+1, NFDBITS); |
| 2485 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2486 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2487 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2488 | sz = nfdset * sizeof(fd_mask); |
| 2489 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2490 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2491 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2492 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2493 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2494 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2495 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2496 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2497 | memset(*readsetp, 0, sz); |
| 2498 | memset(*writesetp, 0, sz); |
| 2499 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2500 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2501 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2502 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2503 | } |
| 2504 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2505 | /* |
| 2506 | * After select, perform any appropriate operations for channels which have |
| 2507 | * events pending. |
| 2508 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2509 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2510 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2511 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2512 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2513 | } |
| 2514 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2515 | /* |
| 2516 | * Enqueue data for channels with open or draining c->input. |
| 2517 | */ |
| 2518 | static void |
| 2519 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2520 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2521 | size_t len, plen; |
| 2522 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2523 | int r; |
| 2524 | |
| 2525 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2526 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2527 | /* |
| 2528 | * input-buffer is empty and read-socket shutdown: |
| 2529 | * tell peer, that we will not send more data: |
| 2530 | * send IEOF. |
| 2531 | * hack for extended data: delay EOF if EFD still |
| 2532 | * in use. |
| 2533 | */ |
| 2534 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2535 | debug2("channel %d: " |
| 2536 | "ibuf_empty delayed efd %d/(%zu)", |
| 2537 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2538 | else |
| 2539 | chan_ibuf_empty(ssh, c); |
| 2540 | } |
| 2541 | return; |
| 2542 | } |
| 2543 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2544 | if (!c->have_remote_id) |
| 2545 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2546 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2547 | if (c->datagram) { |
| 2548 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2549 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2550 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2551 | c->self, ssh_err(r)); |
| 2552 | /* |
| 2553 | * XXX this does tail-drop on the datagram queue which is |
| 2554 | * usually suboptimal compared to head-drop. Better to have |
| 2555 | * backpressure at read time? (i.e. read + discard) |
| 2556 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2557 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2558 | debug("channel %d: datagram too big", c->self); |
| 2559 | return; |
| 2560 | } |
| 2561 | /* Enqueue it */ |
| 2562 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2563 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2564 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2565 | (r = sshpkt_send(ssh)) != 0) { |
| 2566 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2567 | c->self, ssh_err(r)); |
| 2568 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2569 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2570 | return; |
| 2571 | } |
| 2572 | |
| 2573 | /* Enqueue packet for buffered data. */ |
| 2574 | if (len > c->remote_window) |
| 2575 | len = c->remote_window; |
| 2576 | if (len > c->remote_maxpacket) |
| 2577 | len = c->remote_maxpacket; |
| 2578 | if (len == 0) |
| 2579 | return; |
| 2580 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2581 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2582 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2583 | (r = sshpkt_send(ssh)) != 0) { |
| 2584 | fatal("%s: channel %i: data: %s", __func__, |
| 2585 | c->self, ssh_err(r)); |
| 2586 | } |
| 2587 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2588 | fatal("%s: channel %i: consume: %s", __func__, |
| 2589 | c->self, ssh_err(r)); |
| 2590 | c->remote_window -= len; |
| 2591 | } |
| 2592 | |
| 2593 | /* |
| 2594 | * Enqueue data for channels with open c->extended in read mode. |
| 2595 | */ |
| 2596 | static void |
| 2597 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2598 | { |
| 2599 | size_t len; |
| 2600 | int r; |
| 2601 | |
| 2602 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2603 | return; |
| 2604 | |
| 2605 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2606 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2607 | if (len > c->remote_window) |
| 2608 | len = c->remote_window; |
| 2609 | if (len > c->remote_maxpacket) |
| 2610 | len = c->remote_maxpacket; |
| 2611 | if (len == 0) |
| 2612 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2613 | if (!c->have_remote_id) |
| 2614 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2615 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2616 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2617 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2618 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2619 | (r = sshpkt_send(ssh)) != 0) { |
| 2620 | fatal("%s: channel %i: data: %s", __func__, |
| 2621 | c->self, ssh_err(r)); |
| 2622 | } |
| 2623 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2624 | fatal("%s: channel %i: consume: %s", __func__, |
| 2625 | c->self, ssh_err(r)); |
| 2626 | c->remote_window -= len; |
| 2627 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2628 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2629 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2630 | /* If there is data to send to the connection, enqueue some of it now. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2631 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2632 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2633 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2634 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2635 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2636 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2637 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2638 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2639 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2640 | if (c == NULL) |
| 2641 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2642 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2643 | /* |
| 2644 | * We are only interested in channels that can have buffered |
| 2645 | * incoming data. |
| 2646 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2647 | if (c->type != SSH_CHANNEL_OPEN) |
| 2648 | continue; |
| 2649 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2650 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2651 | debug3("channel %d: will not send data after close", |
| 2652 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2653 | continue; |
| 2654 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2655 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2656 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2657 | if (c->istate == CHAN_INPUT_OPEN || |
| 2658 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2659 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2660 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2661 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2662 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2663 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2664 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2665 | } |
| 2666 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2667 | /* -- mux proxy support */ |
| 2668 | |
| 2669 | /* |
| 2670 | * When multiplexing channel messages for mux clients we have to deal |
| 2671 | * with downstream messages from the mux client and upstream messages |
| 2672 | * from the ssh server: |
| 2673 | * 1) Handling downstream messages is straightforward and happens |
| 2674 | * in channel_proxy_downstream(): |
| 2675 | * - We forward all messages (mostly) unmodified to the server. |
| 2676 | * - However, in order to route messages from upstream to the correct |
| 2677 | * downstream client, we have to replace the channel IDs used by the |
| 2678 | * mux clients with a unique channel ID because the mux clients might |
| 2679 | * use conflicting channel IDs. |
| 2680 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2681 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2682 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2683 | * with the newly allocated channel ID. |
| 2684 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2685 | * channels and processed by channel_proxy_upstream(). The local channel ID |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2686 | * is then translated back to the original mux client ID. |
| 2687 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2688 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2689 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2690 | * downstream mux client are removed. |
| 2691 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2692 | * requires more work, because they are not addressed to a specific |
| 2693 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2694 | * out whether the request is addressed to the local client or a |
| 2695 | * specific downstream client based on the listen-address/port. |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2696 | * 6) Agent and X11-Forwarding have a similar problem and are currently |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2697 | * not supported as the matching session/channel cannot be identified |
| 2698 | * easily. |
| 2699 | */ |
| 2700 | |
| 2701 | /* |
| 2702 | * receive packets from downstream mux clients: |
| 2703 | * channel callback fired on read from mux client, creates |
| 2704 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2705 | * on channel creation. |
| 2706 | */ |
| 2707 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2708 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2709 | { |
| 2710 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2711 | struct sshbuf *original = NULL, *modified = NULL; |
| 2712 | const u_char *cp; |
| 2713 | char *ctype = NULL, *listen_host = NULL; |
| 2714 | u_char type; |
| 2715 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2716 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2717 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2718 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2719 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2720 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2721 | != 0) { |
| 2722 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2723 | return -1; |
| 2724 | } |
| 2725 | if (have < 2) { |
| 2726 | error("%s: short message", __func__); |
| 2727 | return -1; |
| 2728 | } |
| 2729 | type = cp[1]; |
| 2730 | /* skip padlen + type */ |
| 2731 | cp += 2; |
| 2732 | have -= 2; |
| 2733 | if (ssh_packet_log_type(type)) |
| 2734 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2735 | downstream->self, type); |
| 2736 | |
| 2737 | switch (type) { |
| 2738 | case SSH2_MSG_CHANNEL_OPEN: |
| 2739 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2740 | (modified = sshbuf_new()) == NULL) { |
| 2741 | error("%s: alloc", __func__); |
| 2742 | goto out; |
| 2743 | } |
| 2744 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2745 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2746 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2747 | goto out; |
| 2748 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2749 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2750 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2751 | c->mux_ctx = downstream; /* point to mux client */ |
| 2752 | c->mux_downstream_id = id; /* original downstream id */ |
| 2753 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2754 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2755 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2756 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2757 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2758 | goto out; |
| 2759 | } |
| 2760 | break; |
| 2761 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2762 | /* |
| 2763 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2764 | * need to parse 'remote_id' instead of 'ctype'. |
| 2765 | */ |
| 2766 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2767 | (modified = sshbuf_new()) == NULL) { |
| 2768 | error("%s: alloc", __func__); |
| 2769 | goto out; |
| 2770 | } |
| 2771 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2772 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2773 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2774 | goto out; |
| 2775 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2776 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2777 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2778 | c->mux_ctx = downstream; /* point to mux client */ |
| 2779 | c->mux_downstream_id = id; |
| 2780 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2781 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2782 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2783 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2784 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2785 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2786 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2787 | goto out; |
| 2788 | } |
| 2789 | break; |
| 2790 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2791 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2792 | error("%s: alloc", __func__); |
| 2793 | goto out; |
| 2794 | } |
| 2795 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2796 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2797 | goto out; |
| 2798 | } |
| 2799 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2800 | error("%s: unsupported request %s", __func__, ctype); |
| 2801 | goto out; |
| 2802 | } |
| 2803 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2804 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2805 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2806 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2807 | goto out; |
| 2808 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2809 | if (listen_port > 65535) { |
| 2810 | error("%s: tcpip-forward for %s: bad port %u", |
| 2811 | __func__, listen_host, listen_port); |
| 2812 | goto out; |
| 2813 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2814 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 2815 | permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2816 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2817 | listen_host = NULL; |
| 2818 | break; |
| 2819 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2820 | if (have < 4) |
| 2821 | break; |
| 2822 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2823 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2824 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2825 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2826 | else |
| 2827 | c->flags |= CHAN_CLOSE_SENT; |
| 2828 | } |
| 2829 | break; |
| 2830 | } |
| 2831 | if (modified) { |
| 2832 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2833 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2834 | (r = sshpkt_send(ssh)) != 0) { |
| 2835 | error("%s: send %s", __func__, ssh_err(r)); |
| 2836 | goto out; |
| 2837 | } |
| 2838 | } else { |
| 2839 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2840 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2841 | (r = sshpkt_send(ssh)) != 0) { |
| 2842 | error("%s: send %s", __func__, ssh_err(r)); |
| 2843 | goto out; |
| 2844 | } |
| 2845 | } |
| 2846 | ret = 0; |
| 2847 | out: |
| 2848 | free(ctype); |
| 2849 | free(listen_host); |
| 2850 | sshbuf_free(original); |
| 2851 | sshbuf_free(modified); |
| 2852 | return ret; |
| 2853 | } |
| 2854 | |
| 2855 | /* |
| 2856 | * receive packets from upstream server and de-multiplex packets |
| 2857 | * to correct downstream: |
| 2858 | * implemented as a helper for channel input handlers, |
| 2859 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2860 | */ |
| 2861 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2862 | channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2863 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2864 | struct sshbuf *b = NULL; |
| 2865 | Channel *downstream; |
| 2866 | const u_char *cp = NULL; |
| 2867 | size_t len; |
| 2868 | int r; |
| 2869 | |
| 2870 | /* |
| 2871 | * When receiving packets from the peer we need to check whether we |
| 2872 | * need to forward the packets to the mux client. In this case we |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2873 | * restore the original channel id and keep track of CLOSE messages, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2874 | * so we can cleanup the channel. |
| 2875 | */ |
| 2876 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2877 | return 0; |
| 2878 | if ((downstream = c->mux_ctx) == NULL) |
| 2879 | return 0; |
| 2880 | switch (type) { |
| 2881 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2882 | case SSH2_MSG_CHANNEL_DATA: |
| 2883 | case SSH2_MSG_CHANNEL_EOF: |
| 2884 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2885 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2886 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2887 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2888 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2889 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2890 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2891 | break; |
| 2892 | default: |
| 2893 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2894 | c->self, type); |
| 2895 | return 0; |
| 2896 | } |
| 2897 | if ((b = sshbuf_new()) == NULL) { |
| 2898 | error("%s: alloc reply", __func__); |
| 2899 | goto out; |
| 2900 | } |
| 2901 | /* get remaining payload (after id) */ |
| 2902 | cp = sshpkt_ptr(ssh, &len); |
| 2903 | if (cp == NULL) { |
| 2904 | error("%s: no packet", __func__); |
| 2905 | goto out; |
| 2906 | } |
| 2907 | /* translate id and send to muxclient */ |
| 2908 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2909 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2910 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2911 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2912 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2913 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2914 | goto out; |
| 2915 | } |
| 2916 | /* sshbuf_dump(b, stderr); */ |
| 2917 | if (ssh_packet_log_type(type)) |
| 2918 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2919 | type); |
| 2920 | out: |
| 2921 | /* update state */ |
| 2922 | switch (type) { |
| 2923 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2924 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2925 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2926 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2927 | c->have_remote_id = 1; |
| 2928 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2929 | break; |
| 2930 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2931 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2932 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2933 | else |
| 2934 | c->flags |= CHAN_CLOSE_RCVD; |
| 2935 | break; |
| 2936 | } |
| 2937 | sshbuf_free(b); |
| 2938 | return 1; |
| 2939 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2940 | |
| 2941 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2942 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2943 | /* Parse a channel ID from the current packet */ |
| 2944 | static int |
| 2945 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2946 | { |
| 2947 | u_int32_t id; |
| 2948 | int r; |
| 2949 | |
| 2950 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2951 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2952 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2953 | } |
| 2954 | if (id > INT_MAX) { |
| 2955 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2956 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2957 | } |
| 2958 | return (int)id; |
| 2959 | } |
| 2960 | |
| 2961 | /* Lookup a channel from an ID in the current packet */ |
| 2962 | static Channel * |
| 2963 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2964 | { |
| 2965 | int id = channel_parse_id(ssh, where, what); |
| 2966 | Channel *c; |
| 2967 | |
| 2968 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2969 | ssh_packet_disconnect(ssh, |
| 2970 | "%s packet referred to nonexistent channel %d", what, id); |
| 2971 | } |
| 2972 | return c; |
| 2973 | } |
| 2974 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2975 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2976 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2977 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2978 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2979 | size_t data_len, win_len; |
| 2980 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 2981 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2982 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2983 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2984 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2985 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2986 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2987 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2988 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 2989 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2990 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2991 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2992 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2993 | /* Get the data. */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 2994 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 || |
| 2995 | (r = sshpkt_get_end(ssh)) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2996 | fatal("%s: channel %d: get data: %s", __func__, |
| 2997 | c->self, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2998 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2999 | win_len = data_len; |
| 3000 | if (c->datagram) |
| 3001 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3002 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3003 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3004 | * The sending side reduces its window as it sends data, so we |
| 3005 | * must 'fake' consumption of the data in order to ensure that window |
| 3006 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3007 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3008 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 3009 | c->local_window -= win_len; |
| 3010 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3011 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3012 | } |
| 3013 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3014 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3015 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3016 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3017 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3018 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3019 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3020 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3021 | c->self, win_len, c->local_window); |
| 3022 | return 0; |
| 3023 | } |
| 3024 | c->local_window -= win_len; |
| 3025 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3026 | if (c->datagram) { |
| 3027 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 3028 | fatal("%s: channel %d: append datagram: %s", |
| 3029 | __func__, c->self, ssh_err(r)); |
| 3030 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 3031 | fatal("%s: channel %d: append data: %s", |
| 3032 | __func__, c->self, ssh_err(r)); |
| 3033 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3034 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3035 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3036 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3037 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3038 | channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3039 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3040 | const u_char *data; |
| 3041 | size_t data_len; |
| 3042 | u_int32_t tcode; |
| 3043 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 3044 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3045 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3046 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3047 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3048 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3049 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3050 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3051 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3052 | if (c->flags & CHAN_EOF_RCVD) { |
| 3053 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3054 | debug("channel %d: accepting ext data after eof", |
| 3055 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3056 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3057 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 3058 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3059 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3060 | |
| 3061 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 3062 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 3063 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3064 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3065 | if (c->efd == -1 || |
| 3066 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 3067 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 3068 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3069 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3070 | } |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3071 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 || |
| 3072 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3073 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3074 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3075 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3076 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3077 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3078 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3079 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3080 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3081 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3082 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3083 | /* XXX sshpkt_getb? */ |
| 3084 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3085 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3086 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3087 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3088 | } |
| 3089 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3090 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3091 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3092 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3093 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3094 | int r; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3095 | |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3096 | if ((r = sshpkt_get_end(ssh)) != 0) { |
| 3097 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3098 | ssh_packet_disconnect(ssh, "Invalid ieof message"); |
| 3099 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3100 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3101 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3102 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3103 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3104 | |
| 3105 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3106 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3107 | debug("channel %d: FORCE input drain", c->self); |
| 3108 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3109 | if (sshbuf_len(c->input) == 0) |
| 3110 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3111 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3112 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3113 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3114 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3115 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3116 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3117 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3118 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3119 | int r; |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3120 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3121 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3122 | return 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3123 | if ((r = sshpkt_get_end(ssh)) != 0) { |
| 3124 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3125 | ssh_packet_disconnect(ssh, "Invalid oclose message"); |
| 3126 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3127 | chan_rcvd_oclose(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3128 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3129 | } |
| 3130 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3131 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3132 | channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3133 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3134 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3135 | u_int32_t remote_window, remote_maxpacket; |
| 3136 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3137 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3138 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3139 | return 0; |
| 3140 | if (c->type != SSH_CHANNEL_OPENING) |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3141 | ssh_packet_disconnect(ssh, "Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3142 | "non-opening channel %d.", c->self); |
| 3143 | /* |
| 3144 | * Record the remote channel number and mark that the channel |
| 3145 | * is now open. |
| 3146 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3147 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3148 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3149 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 || |
| 3150 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3151 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3152 | ssh_packet_disconnect(ssh, "Invalid open confirmation message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3153 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3154 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3155 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3156 | c->remote_window = remote_window; |
| 3157 | c->remote_maxpacket = remote_maxpacket; |
| 3158 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3159 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3160 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3161 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3162 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3163 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3164 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3165 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3166 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3167 | } |
| 3168 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3169 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3170 | reason2txt(int reason) |
| 3171 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3172 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3173 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3174 | return "administratively prohibited"; |
| 3175 | case SSH2_OPEN_CONNECT_FAILED: |
| 3176 | return "connect failed"; |
| 3177 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3178 | return "unknown channel type"; |
| 3179 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3180 | return "resource shortage"; |
| 3181 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3182 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3185 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3186 | channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3187 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3188 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3189 | u_int32_t reason; |
| 3190 | char *msg = NULL; |
| 3191 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3192 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3193 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3194 | return 0; |
| 3195 | if (c->type != SSH_CHANNEL_OPENING) |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3196 | ssh_packet_disconnect(ssh, "Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3197 | "non-opening channel %d.", c->self); |
| 3198 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3199 | error("%s: reason: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3200 | ssh_packet_disconnect(ssh, "Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3201 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3202 | /* skip language */ |
| 3203 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3204 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 || |
| 3205 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3206 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3207 | ssh_packet_disconnect(ssh, "Invalid open failure message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3208 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3209 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3210 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3211 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3212 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3213 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3214 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3215 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3216 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3217 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3218 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3219 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3220 | } |
| 3221 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3222 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3223 | channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3224 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3225 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3226 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3227 | u_int32_t adjust; |
| 3228 | u_int new_rwin; |
| 3229 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3230 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3231 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3232 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3233 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3234 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3235 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3236 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3237 | return 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3238 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 || |
| 3239 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3240 | error("%s: adjust: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3241 | ssh_packet_disconnect(ssh, "Invalid window adjust message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3242 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3243 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3244 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3245 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3246 | c->self, adjust, c->remote_window); |
| 3247 | } |
| 3248 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3249 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3250 | } |
| 3251 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3252 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3253 | channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3254 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3255 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3256 | Channel *c; |
| 3257 | struct channel_confirm *cc; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3258 | int r; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3259 | |
| 3260 | /* Reset keepalive timeout */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3261 | ssh_packet_set_alive_timeouts(ssh, 0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3262 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3263 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3264 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3265 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3266 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3267 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3268 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3269 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3270 | return 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3271 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 3272 | ssh_packet_disconnect(ssh, "Invalid status confirm message"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3273 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3274 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3275 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3276 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3277 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3278 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3279 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3280 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3281 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3282 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3283 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3284 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3285 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3286 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3287 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3290 | |
| 3291 | /* |
| 3292 | * Determine whether or not a port forward listens to loopback, the |
| 3293 | * specified address or wildcard. On the client, a specified bind |
| 3294 | * address will always override gateway_ports. On the server, a |
| 3295 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3296 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3297 | * will bind to whatever address the client asked for. |
| 3298 | * |
| 3299 | * Special-case listen_addrs are: |
| 3300 | * |
| 3301 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3302 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3303 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3304 | * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3305 | */ |
| 3306 | static const char * |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3307 | channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3308 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3309 | { |
| 3310 | const char *addr = NULL; |
| 3311 | int wildcard = 0; |
| 3312 | |
| 3313 | if (listen_addr == NULL) { |
| 3314 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3315 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3316 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3317 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3318 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3319 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3320 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3321 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3322 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3323 | /* |
| 3324 | * Notify client if they requested a specific listen |
| 3325 | * address and it was overridden. |
| 3326 | */ |
| 3327 | if (*listen_addr != '\0' && |
| 3328 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3329 | strcmp(listen_addr, "*") != 0) { |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3330 | ssh_packet_send_debug(ssh, |
| 3331 | "Forwarding listen address " |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3332 | "\"%s\" overridden by server " |
| 3333 | "GatewayPorts", listen_addr); |
| 3334 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3335 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3336 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3337 | strcmp(listen_addr, "::1") == 0) { |
| 3338 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3339 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3340 | } |
| 3341 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3342 | strcmp(listen_addr, "::1") == 0) { |
| 3343 | /* |
| 3344 | * If a specific IPv4/IPv6 localhost address has been |
| 3345 | * requested then accept it even if gateway_ports is in |
| 3346 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3347 | */ |
| 3348 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3349 | } |
| 3350 | if (wildcardp != NULL) |
| 3351 | *wildcardp = wildcard; |
| 3352 | return addr; |
| 3353 | } |
| 3354 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3355 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3356 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3357 | struct Forward *fwd, int *allocated_listen_port, |
| 3358 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3359 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3360 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3361 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3362 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3363 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3364 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3365 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3366 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3367 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3368 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3369 | if (is_client && fwd->connect_path != NULL) { |
| 3370 | host = fwd->connect_path; |
| 3371 | } else { |
| 3372 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3373 | fwd->listen_host : fwd->connect_host; |
| 3374 | if (host == NULL) { |
| 3375 | error("No forward host name."); |
| 3376 | return 0; |
| 3377 | } |
| 3378 | if (strlen(host) >= NI_MAXHOST) { |
| 3379 | error("Forward host name too long."); |
| 3380 | return 0; |
| 3381 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3382 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3383 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3384 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3385 | addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3386 | is_client, fwd_opts); |
| 3387 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3388 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3389 | |
| 3390 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3391 | * getaddrinfo returns a loopback address if the hostname is |
| 3392 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3393 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3394 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3395 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3396 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3397 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3398 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3399 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3400 | if (addr == NULL) { |
| 3401 | /* This really shouldn't happen */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3402 | ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3403 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3404 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3405 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3406 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3407 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3408 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3409 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3410 | if (allocated_listen_port != NULL) |
| 3411 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3412 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3413 | switch (ai->ai_family) { |
| 3414 | case AF_INET: |
| 3415 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3416 | sin_port; |
| 3417 | break; |
| 3418 | case AF_INET6: |
| 3419 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3420 | sin6_port; |
| 3421 | break; |
| 3422 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3423 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3424 | } |
| 3425 | /* |
| 3426 | * If allocating a port for -R forwards, then use the |
| 3427 | * same port for all address families. |
| 3428 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3429 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3430 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3431 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3432 | *lport_p = htons(*allocated_listen_port); |
| 3433 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3434 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3435 | strport, sizeof(strport), |
| 3436 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3437 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3438 | continue; |
| 3439 | } |
| 3440 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3441 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3442 | if (sock < 0) { |
| 3443 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3444 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3445 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3446 | continue; |
| 3447 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3448 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3449 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3450 | if (ai->ai_family == AF_INET6) |
| 3451 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3452 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3453 | debug("Local forwarding listening on %s port %s.", |
| 3454 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3455 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3456 | /* Bind the socket to the address. */ |
| 3457 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3458 | /* |
| 3459 | * address can be in if use ipv6 address is |
| 3460 | * already bound |
| 3461 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3462 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3463 | error("bind [%s]:%s: %.100s", |
| 3464 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3465 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3466 | verbose("bind [%s]:%s: %.100s", |
| 3467 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3468 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3469 | close(sock); |
| 3470 | continue; |
| 3471 | } |
| 3472 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3473 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3474 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3475 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3476 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3477 | close(sock); |
| 3478 | continue; |
| 3479 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3480 | |
| 3481 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3482 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3483 | * record what we got. |
| 3484 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3485 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3486 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3487 | allocated_listen_port != NULL && |
| 3488 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3489 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3490 | debug("Allocated listen port %d", |
| 3491 | *allocated_listen_port); |
| 3492 | } |
| 3493 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3494 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3495 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3496 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3497 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3498 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3499 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3500 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3501 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3502 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3503 | c->listening_port = *allocated_listen_port; |
| 3504 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3505 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3506 | success = 1; |
| 3507 | } |
| 3508 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3509 | error("%s: cannot listen to port: %d", __func__, |
| 3510 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3511 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3512 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3513 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3514 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3515 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3516 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3517 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3518 | { |
| 3519 | struct sockaddr_un sunaddr; |
| 3520 | const char *path; |
| 3521 | Channel *c; |
| 3522 | int port, sock; |
| 3523 | mode_t omask; |
| 3524 | |
| 3525 | switch (type) { |
| 3526 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3527 | if (fwd->connect_path != NULL) { |
| 3528 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3529 | error("Local connecting path too long: %s", |
| 3530 | fwd->connect_path); |
| 3531 | return 0; |
| 3532 | } |
| 3533 | path = fwd->connect_path; |
| 3534 | port = PORT_STREAMLOCAL; |
| 3535 | } else { |
| 3536 | if (fwd->connect_host == NULL) { |
| 3537 | error("No forward host name."); |
| 3538 | return 0; |
| 3539 | } |
| 3540 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3541 | error("Forward host name too long."); |
| 3542 | return 0; |
| 3543 | } |
| 3544 | path = fwd->connect_host; |
| 3545 | port = fwd->connect_port; |
| 3546 | } |
| 3547 | break; |
| 3548 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3549 | path = fwd->listen_path; |
| 3550 | port = PORT_STREAMLOCAL; |
| 3551 | break; |
| 3552 | default: |
| 3553 | error("%s: unexpected channel type %d", __func__, type); |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
| 3557 | if (fwd->listen_path == NULL) { |
| 3558 | error("No forward path name."); |
| 3559 | return 0; |
| 3560 | } |
| 3561 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3562 | error("Local listening path too long: %s", fwd->listen_path); |
| 3563 | return 0; |
| 3564 | } |
| 3565 | |
| 3566 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3567 | |
| 3568 | /* Start a Unix domain listener. */ |
| 3569 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3570 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3571 | fwd_opts->streamlocal_bind_unlink); |
| 3572 | umask(omask); |
| 3573 | if (sock < 0) |
| 3574 | return 0; |
| 3575 | |
| 3576 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3577 | |
| 3578 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3579 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3580 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3581 | 0, "unix listener", 1); |
| 3582 | c->path = xstrdup(path); |
| 3583 | c->host_port = port; |
| 3584 | c->listening_port = PORT_STREAMLOCAL; |
| 3585 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3586 | return 1; |
| 3587 | } |
| 3588 | |
| 3589 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3590 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3591 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3592 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3593 | u_int i; |
| 3594 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3595 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3596 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3597 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3598 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3599 | continue; |
| 3600 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3601 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3602 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3603 | found = 1; |
| 3604 | } |
| 3605 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3606 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3607 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3608 | } |
| 3609 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3610 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3611 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3612 | { |
| 3613 | u_int i; |
| 3614 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3615 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3616 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3617 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3618 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3619 | continue; |
| 3620 | if (c->path == NULL) |
| 3621 | continue; |
| 3622 | if (strcmp(c->path, path) == 0) { |
| 3623 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3624 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3625 | found = 1; |
| 3626 | } |
| 3627 | } |
| 3628 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3629 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3633 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3634 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3635 | if (fwd->listen_path != NULL) { |
| 3636 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3637 | fwd->listen_path); |
| 3638 | } else { |
| 3639 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3640 | fwd->listen_host, fwd->listen_port); |
| 3641 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3645 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3646 | const char *lhost, u_short lport, int cport, |
| 3647 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3648 | { |
| 3649 | u_int i; |
| 3650 | int found = 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3651 | const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3652 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3653 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3654 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3655 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3656 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3657 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3658 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3659 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3660 | /* skip dynamic forwardings */ |
| 3661 | if (c->host_port == 0) |
| 3662 | continue; |
| 3663 | } else { |
| 3664 | if (c->host_port != cport) |
| 3665 | continue; |
| 3666 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3667 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3668 | (c->listening_addr != NULL && addr == NULL)) |
| 3669 | continue; |
| 3670 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3671 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3672 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3673 | found = 1; |
| 3674 | } |
| 3675 | } |
| 3676 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3677 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3678 | } |
| 3679 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3680 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3681 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3682 | { |
| 3683 | u_int i; |
| 3684 | int found = 0; |
| 3685 | |
| 3686 | if (path == NULL) { |
| 3687 | error("%s: no path specified.", __func__); |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3691 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3692 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3693 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3694 | continue; |
| 3695 | if (c->listening_addr == NULL) |
| 3696 | continue; |
| 3697 | if (strcmp(c->listening_addr, path) == 0) { |
| 3698 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3699 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3700 | found = 1; |
| 3701 | } |
| 3702 | } |
| 3703 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3704 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3708 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3709 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3710 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3711 | if (fwd->listen_path != NULL) { |
| 3712 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3713 | fwd->listen_path); |
| 3714 | } else { |
| 3715 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3716 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3717 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3718 | } |
| 3719 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3720 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3721 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3722 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3723 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3724 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3725 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3726 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3727 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3728 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3729 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3730 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3731 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3732 | } |
| 3733 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3734 | /* Matches a remote forwarding permission against a requested forwarding */ |
| 3735 | static int |
| 3736 | remote_open_match(struct permission *allowed_open, struct Forward *fwd) |
| 3737 | { |
| 3738 | int ret; |
| 3739 | char *lhost; |
| 3740 | |
| 3741 | /* XXX add ACLs for streamlocal */ |
| 3742 | if (fwd->listen_path != NULL) |
| 3743 | return 1; |
| 3744 | |
| 3745 | if (fwd->listen_host == NULL || allowed_open->listen_host == NULL) |
| 3746 | return 0; |
| 3747 | |
| 3748 | if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT && |
| 3749 | allowed_open->listen_port != fwd->listen_port) |
| 3750 | return 0; |
| 3751 | |
| 3752 | /* Match hostnames case-insensitively */ |
| 3753 | lhost = xstrdup(fwd->listen_host); |
| 3754 | lowercase(lhost); |
| 3755 | ret = match_pattern(lhost, allowed_open->listen_host); |
| 3756 | free(lhost); |
| 3757 | |
| 3758 | return ret; |
| 3759 | } |
| 3760 | |
| 3761 | /* Checks whether a requested remote forwarding is permitted */ |
| 3762 | static int |
| 3763 | check_rfwd_permission(struct ssh *ssh, struct Forward *fwd) |
| 3764 | { |
| 3765 | struct ssh_channels *sc = ssh->chanctxt; |
| 3766 | struct permission_set *pset = &sc->remote_perms; |
| 3767 | u_int i, permit, permit_adm = 1; |
| 3768 | struct permission *perm; |
| 3769 | |
| 3770 | /* XXX apply GatewayPorts override before checking? */ |
| 3771 | |
| 3772 | permit = pset->all_permitted; |
| 3773 | if (!permit) { |
| 3774 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3775 | perm = &pset->permitted_user[i]; |
| 3776 | if (remote_open_match(perm, fwd)) { |
| 3777 | permit = 1; |
| 3778 | break; |
| 3779 | } |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | if (pset->num_permitted_admin > 0) { |
| 3784 | permit_adm = 0; |
| 3785 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 3786 | perm = &pset->permitted_admin[i]; |
| 3787 | if (remote_open_match(perm, fwd)) { |
| 3788 | permit_adm = 1; |
| 3789 | break; |
| 3790 | } |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | return permit && permit_adm; |
| 3795 | } |
| 3796 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3797 | /* protocol v2 remote port fwd, used by sshd */ |
| 3798 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3799 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3800 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3801 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3802 | if (!check_rfwd_permission(ssh, fwd)) { |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3803 | ssh_packet_send_debug(ssh, "port forwarding refused"); |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3804 | return 0; |
| 3805 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3806 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3807 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3808 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3809 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3810 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3811 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3812 | fwd_opts); |
| 3813 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3814 | } |
| 3815 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3816 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3817 | * Translate the requested rfwd listen host to something usable for |
| 3818 | * this server. |
| 3819 | */ |
| 3820 | static const char * |
| 3821 | channel_rfwd_bind_host(const char *listen_host) |
| 3822 | { |
| 3823 | if (listen_host == NULL) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3824 | return "localhost"; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3825 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3826 | return ""; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3827 | } else |
| 3828 | return listen_host; |
| 3829 | } |
| 3830 | |
| 3831 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3832 | * Initiate forwarding of connections to port "port" on remote host through |
| 3833 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3834 | * Returns handle (index) for updating the dynamic listen port with |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3835 | * channel_update_permission(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3836 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3837 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3838 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3839 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3840 | int r, success = 0, idx = -1; |
| 3841 | char *host_to_connect, *listen_host, *listen_path; |
| 3842 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3843 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3844 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3845 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3846 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3847 | (r = sshpkt_put_cstring(ssh, |
| 3848 | "streamlocal-forward@openssh.com")) != 0 || |
| 3849 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3850 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3851 | (r = sshpkt_send(ssh)) != 0 || |
| 3852 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3853 | fatal("%s: request streamlocal: %s", |
| 3854 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3855 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3856 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3857 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3858 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3859 | (r = sshpkt_put_cstring(ssh, |
| 3860 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3861 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3862 | (r = sshpkt_send(ssh)) != 0 || |
| 3863 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3864 | fatal("%s: request tcpip-forward: %s", |
| 3865 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3866 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3867 | /* Assume that server accepts the request */ |
| 3868 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3869 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3870 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3871 | host_to_connect = listen_host = listen_path = NULL; |
| 3872 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3873 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3874 | host_to_connect = xstrdup(fwd->connect_path); |
| 3875 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3876 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3877 | host_to_connect = xstrdup(fwd->connect_host); |
| 3878 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3879 | } |
| 3880 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3881 | listen_path = xstrdup(fwd->listen_path); |
| 3882 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3883 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3884 | if (fwd->listen_host != NULL) |
| 3885 | listen_host = xstrdup(fwd->listen_host); |
| 3886 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3887 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3888 | idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3889 | host_to_connect, port_to_connect, |
| 3890 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3891 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3892 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3893 | } |
| 3894 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3895 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3896 | open_match(struct permission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3897 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3898 | { |
| 3899 | if (allowed_open->host_to_connect == NULL) |
| 3900 | return 0; |
| 3901 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3902 | allowed_open->port_to_connect != requestedport) |
| 3903 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3904 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3905 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3906 | return 0; |
| 3907 | return 1; |
| 3908 | } |
| 3909 | |
| 3910 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3911 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3912 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3913 | * need to translate between the configured-host (listen_host) |
| 3914 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3915 | */ |
| 3916 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3917 | open_listen_match_tcpip(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3918 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3919 | { |
| 3920 | const char *allowed_host; |
| 3921 | |
| 3922 | if (allowed_open->host_to_connect == NULL) |
| 3923 | return 0; |
| 3924 | if (allowed_open->listen_port != requestedport) |
| 3925 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3926 | if (!translate && allowed_open->listen_host == NULL && |
| 3927 | requestedhost == NULL) |
| 3928 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3929 | allowed_host = translate ? |
| 3930 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3931 | allowed_open->listen_host; |
djm@openbsd.org | 90e51d6 | 2018-06-25 22:28:33 +0000 | [diff] [blame] | 3932 | if (allowed_host == NULL || requestedhost == NULL || |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3933 | strcmp(allowed_host, requestedhost) != 0) |
| 3934 | return 0; |
| 3935 | return 1; |
| 3936 | } |
| 3937 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3938 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3939 | open_listen_match_streamlocal(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3940 | const char *requestedpath) |
| 3941 | { |
| 3942 | if (allowed_open->host_to_connect == NULL) |
| 3943 | return 0; |
| 3944 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3945 | return 0; |
| 3946 | if (allowed_open->listen_path == NULL || |
| 3947 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3948 | return 0; |
| 3949 | return 1; |
| 3950 | } |
| 3951 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3952 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3953 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3954 | * local side. |
| 3955 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3956 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3957 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3958 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3959 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3960 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3961 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3962 | int r; |
| 3963 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3964 | struct permission *perm; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3965 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3966 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3967 | perm = &pset->permitted_user[i]; |
| 3968 | if (open_listen_match_tcpip(perm, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3969 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3970 | perm = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3971 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3972 | if (perm == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3973 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3974 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3975 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3976 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3977 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 3978 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3979 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 3980 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 3981 | (r = sshpkt_send(ssh)) != 0) |
| 3982 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3983 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3984 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3985 | |
| 3986 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3990 | * Request cancellation of remote forwarding of Unix domain socket |
| 3991 | * path from local side. |
| 3992 | */ |
| 3993 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3994 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3995 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3996 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3997 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3998 | int r; |
| 3999 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4000 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4001 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4002 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4003 | perm = &pset->permitted_user[i]; |
| 4004 | if (open_listen_match_streamlocal(perm, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4005 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4006 | perm = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4007 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4008 | if (perm == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4009 | debug("%s: requested forward not found", __func__); |
| 4010 | return -1; |
| 4011 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4012 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 4013 | (r = sshpkt_put_cstring(ssh, |
| 4014 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 4015 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 4016 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 4017 | (r = sshpkt_send(ssh)) != 0) |
| 4018 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4019 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4020 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4021 | |
| 4022 | return 0; |
| 4023 | } |
| 4024 | |
| 4025 | /* |
| 4026 | * Request cancellation of remote forwarding of a connection from local side. |
| 4027 | */ |
| 4028 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4029 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4030 | { |
| 4031 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4032 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 4033 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4034 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4035 | return channel_request_rforward_cancel_tcpip(ssh, |
| 4036 | fwd->listen_host, |
| 4037 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | /* |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4042 | * Permits opening to any host/port if permitted_user[] is empty. This is |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4043 | * usually called by the server, because the user could connect to any port |
| 4044 | * anyway, and the server has no way to know but to trust the client anyway. |
| 4045 | */ |
| 4046 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4047 | channel_permit_all(struct ssh *ssh, int where) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4048 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4049 | struct permission_set *pset = permission_set_get(ssh, where); |
| 4050 | |
| 4051 | if (pset->num_permitted_user == 0) |
| 4052 | pset->all_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4055 | /* |
| 4056 | * Permit the specified host/port for forwarding. |
| 4057 | */ |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 4058 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4059 | channel_add_permission(struct ssh *ssh, int who, int where, |
| 4060 | char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4061 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4062 | int local = where == FORWARD_LOCAL; |
| 4063 | struct permission_set *pset = permission_set_get(ssh, where); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4064 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4065 | debug("allow %s forwarding to host %s port %d", |
| 4066 | fwd_ident(who, where), host, port); |
| 4067 | /* |
| 4068 | * Remote forwards set listen_host/port, local forwards set |
| 4069 | * host/port_to_connect. |
| 4070 | */ |
| 4071 | permission_set_add(ssh, who, where, |
| 4072 | local ? host : 0, local ? port : 0, |
| 4073 | local ? NULL : host, NULL, local ? 0 : port, NULL); |
| 4074 | pset->all_permitted = 0; |
| 4075 | } |
| 4076 | |
| 4077 | /* |
| 4078 | * Administratively disable forwarding. |
| 4079 | */ |
| 4080 | void |
| 4081 | channel_disable_admin(struct ssh *ssh, int where) |
| 4082 | { |
| 4083 | channel_clear_permission(ssh, FORWARD_ADM, where); |
| 4084 | permission_set_add(ssh, FORWARD_ADM, where, |
| 4085 | NULL, 0, NULL, NULL, 0, NULL); |
| 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | * Clear a list of permitted opens. |
| 4090 | */ |
| 4091 | void |
| 4092 | channel_clear_permission(struct ssh *ssh, int who, int where) |
| 4093 | { |
| 4094 | struct permission **permp; |
| 4095 | u_int *npermp; |
| 4096 | |
| 4097 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
| 4098 | *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp)); |
| 4099 | *npermp = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4102 | /* |
| 4103 | * Update the listen port for a dynamic remote forward, after |
| 4104 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 4105 | * passed then they entry will be invalidated. |
| 4106 | */ |
| 4107 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4108 | channel_update_permission(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4109 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4110 | struct permission_set *pset = &ssh->chanctxt->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4111 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4112 | if (idx < 0 || (u_int)idx >= pset->num_permitted_user) { |
| 4113 | debug("%s: index out of range: %d num_permitted_user %d", |
| 4114 | __func__, idx, pset->num_permitted_user); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4115 | return; |
| 4116 | } |
| 4117 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 4118 | newport > 0 ? "Updating" : "Removing", |
| 4119 | newport, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4120 | pset->permitted_user[idx].host_to_connect, |
| 4121 | pset->permitted_user[idx].port_to_connect); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4122 | if (newport <= 0) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4123 | fwd_perm_clear(&pset->permitted_user[idx]); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4124 | else { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4125 | pset->permitted_user[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4126 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4127 | } |
| 4128 | } |
| 4129 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 4130 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 4131 | int |
| 4132 | permitopen_port(const char *p) |
| 4133 | { |
| 4134 | int port; |
| 4135 | |
| 4136 | if (strcmp(p, "*") == 0) |
| 4137 | return FWD_PERMIT_ANY_PORT; |
| 4138 | if ((port = a2port(p)) > 0) |
| 4139 | return port; |
| 4140 | return -1; |
| 4141 | } |
| 4142 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4143 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4144 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4145 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4146 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4147 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4148 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4149 | char ntop[NI_MAXHOST]; |
| 4150 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4151 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4152 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4153 | switch (cctx->ai->ai_family) { |
| 4154 | case AF_UNIX: |
| 4155 | /* unix:pathname instead of host:port */ |
| 4156 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 4157 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 4158 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 4159 | break; |
| 4160 | case AF_INET: |
| 4161 | case AF_INET6: |
| 4162 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 4163 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 4164 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 4165 | error("connect_next: getnameinfo failed"); |
| 4166 | continue; |
| 4167 | } |
| 4168 | break; |
| 4169 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4170 | continue; |
| 4171 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4172 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4173 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4174 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4175 | error("socket: %.100s", strerror(errno)); |
| 4176 | else |
| 4177 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4178 | continue; |
| 4179 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4180 | if (set_nonblock(sock) == -1) |
| 4181 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4182 | if (connect(sock, cctx->ai->ai_addr, |
| 4183 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4184 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4185 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4186 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4187 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4188 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4189 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4190 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4191 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4192 | if (cctx->ai->ai_family != AF_UNIX) |
| 4193 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4194 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4195 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4196 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4197 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4198 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4199 | return -1; |
| 4200 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4201 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4202 | static void |
| 4203 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4204 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4205 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4206 | if (cctx->aitop) { |
| 4207 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4208 | free(cctx->aitop); |
| 4209 | else |
| 4210 | freeaddrinfo(cctx->aitop); |
| 4211 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4212 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4213 | } |
| 4214 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4215 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4216 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4217 | * passing back the failure reason if appropriate. |
| 4218 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4219 | static int |
| 4220 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4221 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4222 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4223 | { |
| 4224 | struct addrinfo hints; |
| 4225 | int gaierr; |
| 4226 | int sock = -1; |
| 4227 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4228 | |
| 4229 | if (port == PORT_STREAMLOCAL) { |
| 4230 | struct sockaddr_un *sunaddr; |
| 4231 | struct addrinfo *ai; |
| 4232 | |
| 4233 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4234 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4235 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | /* |
| 4239 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4240 | * channel_connect_ctx_free() must check ai_family |
| 4241 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4242 | */ |
| 4243 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4244 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4245 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4246 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4247 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4248 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4249 | ai->ai_protocol = PF_UNSPEC; |
| 4250 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4251 | sunaddr->sun_family = AF_UNIX; |
| 4252 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4253 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4254 | } else { |
| 4255 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4256 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4257 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4258 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4259 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4260 | != 0) { |
| 4261 | if (errmsg != NULL) |
| 4262 | *errmsg = ssh_gai_strerror(gaierr); |
| 4263 | if (reason != NULL) |
| 4264 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4265 | error("connect_to %.100s: unknown host (%s)", name, |
| 4266 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4267 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4268 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4269 | } |
| 4270 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4271 | cctx->host = xstrdup(name); |
| 4272 | cctx->port = port; |
| 4273 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4274 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4275 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4276 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4277 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4278 | return -1; |
| 4279 | } |
| 4280 | |
| 4281 | return sock; |
| 4282 | } |
| 4283 | |
| 4284 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4285 | static Channel * |
| 4286 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4287 | char *ctype, char *rname) |
| 4288 | { |
| 4289 | struct channel_connect cctx; |
| 4290 | Channel *c; |
| 4291 | int sock; |
| 4292 | |
| 4293 | memset(&cctx, 0, sizeof(cctx)); |
| 4294 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4295 | &cctx, NULL, NULL); |
| 4296 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4297 | channel_connect_ctx_free(&cctx); |
| 4298 | return NULL; |
| 4299 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4300 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4301 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4302 | c->host_port = port; |
| 4303 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4304 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4305 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4306 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4309 | /* |
| 4310 | * returns either the newly connected channel or the downstream channel |
| 4311 | * that needs to deal with this connection. |
| 4312 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4313 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4314 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4315 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4316 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4317 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4318 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4319 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4320 | struct permission *perm; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4321 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4322 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4323 | perm = &pset->permitted_user[i]; |
| 4324 | if (open_listen_match_tcpip(perm, |
| 4325 | listen_host, listen_port, 1)) { |
| 4326 | if (perm->downstream) |
| 4327 | return perm->downstream; |
| 4328 | if (perm->port_to_connect == 0) |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4329 | return rdynamic_connect_prepare(ssh, |
| 4330 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4331 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4332 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4333 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4334 | } |
| 4335 | } |
| 4336 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4337 | listen_port); |
| 4338 | return NULL; |
| 4339 | } |
| 4340 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4341 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4342 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4343 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4344 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4345 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4346 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4347 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4348 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4349 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4350 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4351 | perm = &pset->permitted_user[i]; |
| 4352 | if (open_listen_match_streamlocal(perm, path)) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4353 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4354 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4355 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4356 | } |
| 4357 | } |
| 4358 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4359 | path); |
| 4360 | return NULL; |
| 4361 | } |
| 4362 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4363 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4364 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4365 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4366 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4367 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4368 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4369 | struct permission_set *pset = &sc->local_perms; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4370 | struct channel_connect cctx; |
| 4371 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4372 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4373 | int sock; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4374 | struct permission *perm; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4375 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4376 | permit = pset->all_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4377 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4378 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4379 | perm = &pset->permitted_user[i]; |
| 4380 | if (open_match(perm, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4381 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4382 | break; |
| 4383 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4384 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4385 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4386 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4387 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4388 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4389 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4390 | perm = &pset->permitted_admin[i]; |
| 4391 | if (open_match(perm, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4392 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4393 | break; |
| 4394 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4395 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 4399 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4400 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4401 | if (reason != NULL) |
| 4402 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4403 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4404 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4405 | |
| 4406 | memset(&cctx, 0, sizeof(cctx)); |
| 4407 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4408 | &cctx, reason, errmsg); |
| 4409 | if (sock == -1) { |
| 4410 | channel_connect_ctx_free(&cctx); |
| 4411 | return NULL; |
| 4412 | } |
| 4413 | |
| 4414 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4415 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4416 | c->host_port = port; |
| 4417 | c->path = xstrdup(host); |
| 4418 | c->connect_ctx = cctx; |
| 4419 | |
| 4420 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4423 | /* Check if connecting to that path is permitted and connect. */ |
| 4424 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4425 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4426 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4427 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4428 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4429 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4430 | u_int i, permit, permit_adm = 1; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4431 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4432 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4433 | permit = pset->all_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4434 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4435 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4436 | perm = &pset->permitted_user[i]; |
| 4437 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4438 | permit = 1; |
| 4439 | break; |
| 4440 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4441 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4442 | } |
| 4443 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4444 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4445 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4446 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4447 | perm = &pset->permitted_admin[i]; |
| 4448 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4449 | permit_adm = 1; |
| 4450 | break; |
| 4451 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4452 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | if (!permit || !permit_adm) { |
| 4456 | logit("Received request to connect to path %.100s, " |
| 4457 | "but the request was denied.", path); |
| 4458 | return NULL; |
| 4459 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4460 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4461 | } |
| 4462 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4463 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4464 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4465 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4466 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4467 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4468 | int r; |
| 4469 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4470 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4471 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4472 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4473 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4474 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4475 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4476 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4477 | channel_request_start(ssh, i, "window-change", 0); |
| 4478 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4479 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4480 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4481 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4482 | (r = sshpkt_send(ssh)) != 0) |
| 4483 | fatal("%s: channel %u: send window-change: %s", |
| 4484 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4485 | } |
| 4486 | } |
| 4487 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4488 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4489 | static Channel * |
| 4490 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4491 | { |
| 4492 | Channel *c; |
| 4493 | int r; |
| 4494 | |
| 4495 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4496 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4497 | c->host_port = 0; |
| 4498 | c->path = NULL; |
| 4499 | |
| 4500 | /* |
| 4501 | * We need to open the channel before we have a FD, |
| 4502 | * so that we can get SOCKS header from peer. |
| 4503 | */ |
| 4504 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4505 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4506 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4507 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4508 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4509 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4510 | c->self, ssh_err(r)); |
| 4511 | } |
| 4512 | return c; |
| 4513 | } |
| 4514 | |
| 4515 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4516 | static int |
| 4517 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4518 | { |
| 4519 | struct channel_connect cctx; |
| 4520 | int sock; |
| 4521 | |
| 4522 | memset(&cctx, 0, sizeof(cctx)); |
| 4523 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4524 | NULL, &cctx, NULL, NULL); |
| 4525 | if (sock == -1) |
| 4526 | channel_connect_ctx_free(&cctx); |
| 4527 | else { |
| 4528 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4529 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4530 | c->connect_ctx = cctx; |
| 4531 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4532 | } |
| 4533 | return sock; |
| 4534 | } |
| 4535 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4536 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4537 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4538 | /* |
| 4539 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4540 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4541 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4542 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4543 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4544 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4545 | int x11_use_localhost, int single_connection, |
| 4546 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4547 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4548 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4549 | int display_number, sock; |
| 4550 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4551 | struct addrinfo hints, *ai, *aitop; |
| 4552 | char strport[NI_MAXSERV]; |
| 4553 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4554 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4555 | if (chanids == NULL) |
| 4556 | return -1; |
| 4557 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4558 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4559 | display_number < MAX_DISPLAYS; |
| 4560 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4561 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4562 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4563 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4564 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4565 | hints.ai_socktype = SOCK_STREAM; |
| 4566 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4567 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4568 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4569 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4570 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4571 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4572 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4573 | if (ai->ai_family != AF_INET && |
| 4574 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4575 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4576 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4577 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4578 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4579 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4580 | #ifdef EPFNOSUPPORT |
| 4581 | && (errno != EPFNOSUPPORT) |
| 4582 | #endif |
| 4583 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4584 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4585 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4586 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4587 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4588 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4589 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4590 | continue; |
| 4591 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4592 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4593 | if (ai->ai_family == AF_INET6) |
| 4594 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4595 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4596 | set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4597 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4598 | debug2("%s: bind port %d: %.100s", __func__, |
| 4599 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4600 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4601 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4602 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4603 | num_socks = 0; |
| 4604 | break; |
| 4605 | } |
| 4606 | socks[num_socks++] = sock; |
| 4607 | if (num_socks == NUM_SOCKS) |
| 4608 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4609 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4610 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4611 | if (num_socks > 0) |
| 4612 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4613 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4614 | if (display_number >= MAX_DISPLAYS) { |
| 4615 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4616 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4617 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4618 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4619 | for (n = 0; n < num_socks; n++) { |
| 4620 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 4621 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4622 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4623 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4624 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4625 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4626 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4627 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4628 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4629 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4630 | for (n = 0; n < num_socks; n++) { |
| 4631 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4632 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4633 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4634 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4635 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4636 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4637 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4638 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4639 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4640 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4641 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4642 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4643 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4644 | } |
| 4645 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4646 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4647 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4648 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4649 | int sock; |
| 4650 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4651 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4652 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4653 | if (sock < 0) |
| 4654 | error("socket: %.100s", strerror(errno)); |
| 4655 | memset(&addr, 0, sizeof(addr)); |
| 4656 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4657 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4658 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4659 | return sock; |
| 4660 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4661 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4662 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4663 | } |
| 4664 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4665 | static int |
| 4666 | connect_local_xsocket(u_int dnr) |
| 4667 | { |
| 4668 | char buf[1024]; |
| 4669 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4670 | return connect_local_xsocket_path(buf); |
| 4671 | } |
| 4672 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4673 | #ifdef __APPLE__ |
| 4674 | static int |
| 4675 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4676 | { |
| 4677 | struct stat sbuf; |
| 4678 | |
| 4679 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4680 | error("%s: display path too long", __func__); |
| 4681 | return 0; |
| 4682 | } |
| 4683 | if (display[0] != '/') |
| 4684 | return 0; |
| 4685 | if (stat(path, &sbuf) == 0) { |
| 4686 | return 1; |
| 4687 | } else { |
| 4688 | char *dot = strrchr(path, '.'); |
| 4689 | if (dot != NULL) { |
| 4690 | *dot = '\0'; |
| 4691 | if (stat(path, &sbuf) == 0) { |
| 4692 | return 1; |
| 4693 | } |
| 4694 | } |
| 4695 | } |
| 4696 | return 0; |
| 4697 | } |
| 4698 | #endif |
| 4699 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4700 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4701 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4702 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4703 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4704 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4705 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4706 | struct addrinfo hints, *ai, *aitop; |
| 4707 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4708 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4709 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4710 | /* Try to open a socket for the local X server. */ |
| 4711 | display = getenv("DISPLAY"); |
| 4712 | if (!display) { |
| 4713 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4714 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4715 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4716 | /* |
| 4717 | * Now we decode the value of the DISPLAY variable and make a |
| 4718 | * connection to the real X server. |
| 4719 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4720 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4721 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4722 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4723 | { |
| 4724 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4725 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4726 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4727 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4728 | |
| 4729 | /* Create a socket. */ |
| 4730 | sock = connect_local_xsocket_path(path); |
| 4731 | if (sock < 0) |
| 4732 | return -1; |
| 4733 | |
| 4734 | /* OK, we now have a connection to the display. */ |
| 4735 | return sock; |
| 4736 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4737 | } |
| 4738 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4739 | /* |
| 4740 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4741 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4742 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4743 | if (strncmp(display, "unix:", 5) == 0 || |
| 4744 | display[0] == ':') { |
| 4745 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4746 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4747 | &display_number) != 1) { |
| 4748 | error("Could not parse display number from DISPLAY: " |
| 4749 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4750 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4751 | } |
| 4752 | /* Create a socket. */ |
| 4753 | sock = connect_local_xsocket(display_number); |
| 4754 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4755 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4756 | |
| 4757 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4758 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4759 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4760 | /* |
| 4761 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4762 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4763 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4764 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4765 | cp = strchr(buf, ':'); |
| 4766 | if (!cp) { |
| 4767 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4768 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4769 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4770 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4771 | /* |
| 4772 | * buf now contains the host name. But first we parse the |
| 4773 | * display number. |
| 4774 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4775 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4776 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4777 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4778 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4779 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4780 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4781 | /* Look up the host address */ |
| 4782 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4783 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4784 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4785 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4786 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4787 | error("%.100s: unknown host. (%s)", buf, |
| 4788 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4789 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4790 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4791 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4792 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4793 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4794 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4795 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4796 | continue; |
| 4797 | } |
| 4798 | /* Connect it to the display. */ |
| 4799 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4800 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4801 | 6000 + display_number, strerror(errno)); |
| 4802 | close(sock); |
| 4803 | continue; |
| 4804 | } |
| 4805 | /* Success */ |
| 4806 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4807 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4808 | freeaddrinfo(aitop); |
| 4809 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4810 | error("connect %.100s port %u: %.100s", buf, |
| 4811 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4812 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4813 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4814 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4815 | return sock; |
| 4816 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4817 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4818 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4819 | * Requests forwarding of X11 connections, generates fake authentication |
| 4820 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4821 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4822 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4823 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4824 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4825 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4826 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4827 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4828 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4829 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4830 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4831 | char *new_data; |
| 4832 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4833 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4834 | if (sc->x11_saved_display == NULL) |
| 4835 | sc->x11_saved_display = xstrdup(disp); |
| 4836 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4837 | error("x11_request_forwarding_with_spoofing: different " |
| 4838 | "$DISPLAY already forwarded"); |
| 4839 | return; |
| 4840 | } |
| 4841 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4842 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4843 | if (cp) |
| 4844 | cp = strchr(cp, '.'); |
| 4845 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4846 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4847 | else |
| 4848 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4849 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4850 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4851 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4852 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4853 | |
| 4854 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4855 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4856 | for (i = 0; i < data_len; i++) { |
| 4857 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4858 | fatal("x11_request_forwarding: bad " |
| 4859 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4860 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4861 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4862 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4863 | |
| 4864 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4865 | sc->x11_fake_data = xmalloc(data_len); |
| 4866 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4867 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4868 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4869 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4870 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4871 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4872 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4873 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4874 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4875 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4876 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4877 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4878 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4879 | (r = sshpkt_send(ssh)) != 0 || |
| 4880 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4881 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4882 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4883 | } |