djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.381 2018/06/06 18:22:41 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" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 82 | #include "key.h" |
| 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 | |
| 230 | if ((sc = calloc(1, sizeof(*sc))) == NULL || |
| 231 | (sc->channel_pre = calloc(SSH_CHANNEL_MAX_TYPE, |
| 232 | sizeof(*sc->channel_pre))) == NULL || |
| 233 | (sc->channel_post = calloc(SSH_CHANNEL_MAX_TYPE, |
| 234 | sizeof(*sc->channel_post))) == NULL) |
| 235 | fatal("%s: allocation failed", __func__); |
| 236 | sc->channels_alloc = 10; |
| 237 | sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels)); |
| 238 | sc->IPv4or6 = AF_UNSPEC; |
| 239 | channel_handler_init(sc); |
| 240 | |
| 241 | ssh->chanctxt = sc; |
| 242 | } |
| 243 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 244 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 245 | channel_by_id(struct ssh *ssh, int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 246 | { |
| 247 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 248 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 249 | if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) { |
| 250 | logit("%s: %d: bad id", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 251 | return NULL; |
| 252 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 253 | c = ssh->chanctxt->channels[id]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 254 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 255 | logit("%s: %d: bad id: channel free", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
| 258 | return c; |
| 259 | } |
| 260 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 261 | Channel * |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 262 | channel_by_remote_id(struct ssh *ssh, u_int remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 263 | { |
| 264 | Channel *c; |
| 265 | u_int i; |
| 266 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 267 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 268 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 269 | 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] | 270 | return c; |
| 271 | } |
| 272 | return NULL; |
| 273 | } |
| 274 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 275 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 276 | * Returns the channel if it is allowed to receive protocol messages. |
| 277 | * Private channels, like listening sockets, may not receive messages. |
| 278 | */ |
| 279 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 280 | channel_lookup(struct ssh *ssh, int id) |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 281 | { |
| 282 | Channel *c; |
| 283 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 284 | if ((c = channel_by_id(ssh, id)) == NULL) |
| 285 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 286 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 287 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 288 | case SSH_CHANNEL_X11_OPEN: |
| 289 | case SSH_CHANNEL_LARVAL: |
| 290 | case SSH_CHANNEL_CONNECTING: |
| 291 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 292 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 293 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 294 | case SSH_CHANNEL_OPENING: |
| 295 | case SSH_CHANNEL_OPEN: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 296 | case SSH_CHANNEL_ABANDONED: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 297 | case SSH_CHANNEL_MUX_PROXY: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 298 | return c; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 299 | } |
| 300 | logit("Non-public channel %d, type %d.", id, c->type); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 301 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 305 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 306 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 307 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 308 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 309 | 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] | 310 | int extusage, int nonblock, int is_tty) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 312 | struct ssh_channels *sc = ssh->chanctxt; |
| 313 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 314 | /* Update the maximum file descriptor value. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 315 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd); |
| 316 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd); |
| 317 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 318 | |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 319 | if (rfd != -1) |
| 320 | fcntl(rfd, F_SETFD, FD_CLOEXEC); |
| 321 | if (wfd != -1 && wfd != rfd) |
| 322 | fcntl(wfd, F_SETFD, FD_CLOEXEC); |
| 323 | if (efd != -1 && efd != rfd && efd != wfd) |
| 324 | fcntl(efd, F_SETFD, FD_CLOEXEC); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 325 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 326 | c->rfd = rfd; |
| 327 | c->wfd = wfd; |
| 328 | c->sock = (rfd == wfd) ? rfd : -1; |
| 329 | c->efd = efd; |
| 330 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 331 | |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 332 | if ((c->isatty = is_tty) != 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 333 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 334 | #ifdef _AIX |
| 335 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 1a48aec | 2008-06-16 23:35:56 +1000 | [diff] [blame] | 336 | c->wfd_isatty = is_tty || isatty(c->wfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 337 | #endif |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 338 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 339 | /* enable nonblocking mode */ |
| 340 | if (nonblock) { |
| 341 | if (rfd != -1) |
| 342 | set_nonblock(rfd); |
| 343 | if (wfd != -1) |
| 344 | set_nonblock(wfd); |
| 345 | if (efd != -1) |
| 346 | set_nonblock(efd); |
| 347 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Allocate a new channel object and set its type and socket. This will cause |
| 352 | * remote_name to be freed. |
| 353 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 354 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 355 | 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] | 356 | 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] | 357 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 358 | struct ssh_channels *sc = ssh->chanctxt; |
| 359 | u_int i, found; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 360 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 361 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 362 | /* 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] | 363 | for (i = 0; i < sc->channels_alloc; i++) { |
| 364 | if (sc->channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | /* Found a free slot. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 366 | found = i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 367 | break; |
| 368 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 369 | } |
| 370 | if (i >= sc->channels_alloc) { |
| 371 | /* |
| 372 | * There are no free slots. Take last+1 slot and expand |
| 373 | * the array. |
| 374 | */ |
| 375 | found = sc->channels_alloc; |
| 376 | if (sc->channels_alloc > CHANNELS_MAX_CHANNELS) |
| 377 | fatal("%s: internal error: channels_alloc %d too big", |
| 378 | __func__, sc->channels_alloc); |
| 379 | sc->channels = xrecallocarray(sc->channels, sc->channels_alloc, |
| 380 | sc->channels_alloc + 10, sizeof(*sc->channels)); |
| 381 | sc->channels_alloc += 10; |
| 382 | debug2("channel: expanding %d", sc->channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 383 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 384 | /* Initialize and return new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 385 | c = sc->channels[found] = xcalloc(1, sizeof(Channel)); |
| 386 | if ((c->input = sshbuf_new()) == NULL || |
| 387 | (c->output = sshbuf_new()) == NULL || |
| 388 | (c->extended = sshbuf_new()) == NULL) |
| 389 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 390 | c->ostate = CHAN_OUTPUT_OPEN; |
| 391 | c->istate = CHAN_INPUT_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 392 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | c->self = found; |
| 394 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 395 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 396 | c->local_window = window; |
| 397 | c->local_window_max = window; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 398 | c->local_maxpacket = maxpack; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 399 | c->remote_name = xstrdup(remote_name); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 400 | c->ctl_chan = -1; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 401 | c->delayed = 1; /* prevent call to channel_post handler */ |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 402 | TAILQ_INIT(&c->status_confirms); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 403 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 404 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 405 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 406 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 407 | static void |
| 408 | channel_find_maxfd(struct ssh_channels *sc) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 409 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 410 | u_int i; |
| 411 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 412 | Channel *c; |
| 413 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 414 | for (i = 0; i < sc->channels_alloc; i++) { |
| 415 | c = sc->channels[i]; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 416 | if (c != NULL) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 417 | max = MAXIMUM(max, c->rfd); |
| 418 | max = MAXIMUM(max, c->wfd); |
| 419 | max = MAXIMUM(max, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 422 | sc->channel_max_fd = max; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 426 | channel_close_fd(struct ssh *ssh, int *fdp) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 427 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 428 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 429 | int ret = 0, fd = *fdp; |
| 430 | |
| 431 | if (fd != -1) { |
| 432 | ret = close(fd); |
| 433 | *fdp = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 434 | if (fd == sc->channel_max_fd) |
| 435 | channel_find_maxfd(sc); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 436 | } |
| 437 | return ret; |
| 438 | } |
| 439 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 440 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 441 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 442 | channel_close_fds(struct ssh *ssh, Channel *c) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 443 | { |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 444 | int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd; |
| 445 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 446 | channel_close_fd(ssh, &c->sock); |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 447 | if (rfd != sock) |
| 448 | channel_close_fd(ssh, &c->rfd); |
| 449 | if (wfd != sock && wfd != rfd) |
| 450 | channel_close_fd(ssh, &c->wfd); |
| 451 | if (efd != sock && efd != rfd && efd != wfd) |
| 452 | channel_close_fd(ssh, &c->efd); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 456 | fwd_perm_clear(struct permission *perm) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 457 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 458 | free(perm->host_to_connect); |
| 459 | free(perm->listen_host); |
| 460 | free(perm->listen_path); |
| 461 | bzero(perm, sizeof(*perm)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 462 | } |
| 463 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 464 | /* Returns an printable name for the specified forwarding permission list */ |
| 465 | static const char * |
| 466 | fwd_ident(int who, int where) |
| 467 | { |
| 468 | if (who == FORWARD_ADM) { |
| 469 | if (where == FORWARD_LOCAL) |
| 470 | return "admin local"; |
| 471 | else if (where == FORWARD_REMOTE) |
| 472 | return "admin remote"; |
| 473 | } else if (who == FORWARD_USER) { |
| 474 | if (where == FORWARD_LOCAL) |
| 475 | return "user local"; |
| 476 | else if (where == FORWARD_REMOTE) |
| 477 | return "user remote"; |
| 478 | } |
| 479 | fatal("Unknown forward permission list %d/%d", who, where); |
| 480 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 481 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 482 | /* Returns the forwarding permission list for the specified direction */ |
| 483 | static struct permission_set * |
| 484 | permission_set_get(struct ssh *ssh, int where) |
| 485 | { |
| 486 | struct ssh_channels *sc = ssh->chanctxt; |
| 487 | |
| 488 | switch (where) { |
| 489 | case FORWARD_LOCAL: |
| 490 | return &sc->local_perms; |
| 491 | break; |
| 492 | case FORWARD_REMOTE: |
| 493 | return &sc->remote_perms; |
| 494 | break; |
| 495 | default: |
| 496 | fatal("%s: invalid forwarding direction %d", __func__, where); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* Reutrns pointers to the specified forwarding list and its element count */ |
| 501 | static void |
| 502 | permission_set_get_array(struct ssh *ssh, int who, int where, |
| 503 | struct permission ***permpp, u_int **npermpp) |
| 504 | { |
| 505 | struct permission_set *pset = permission_set_get(ssh, where); |
| 506 | |
| 507 | switch (who) { |
| 508 | case FORWARD_USER: |
| 509 | *permpp = &pset->permitted_user; |
| 510 | *npermpp = &pset->num_permitted_user; |
| 511 | break; |
| 512 | case FORWARD_ADM: |
| 513 | *permpp = &pset->permitted_admin; |
| 514 | *npermpp = &pset->num_permitted_admin; |
| 515 | break; |
| 516 | default: |
| 517 | fatal("%s: invalid forwarding client %d", __func__, who); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* Adds an entry to the spcified forwarding list */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 522 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 523 | permission_set_add(struct ssh *ssh, int who, int where, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 524 | const char *host_to_connect, int port_to_connect, |
| 525 | const char *listen_host, const char *listen_path, int listen_port, |
| 526 | Channel *downstream) |
| 527 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 528 | struct permission **permp; |
| 529 | u_int n, *npermp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 530 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 531 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 532 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 533 | if (*npermp >= INT_MAX) |
| 534 | fatal("%s: %s overflow", __func__, fwd_ident(who, where)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 535 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 536 | *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp)); |
| 537 | n = (*npermp)++; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 538 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 539 | (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 540 | (*permp)[n].port_to_connect = port_to_connect; |
| 541 | (*permp)[n].listen_host = MAYBE_DUP(listen_host); |
| 542 | (*permp)[n].listen_path = MAYBE_DUP(listen_path); |
| 543 | (*permp)[n].listen_port = listen_port; |
| 544 | (*permp)[n].downstream = downstream; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 545 | #undef MAYBE_DUP |
| 546 | return (int)n; |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 551 | { |
| 552 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 553 | struct permission_set *pset = &sc->local_perms; |
| 554 | struct permission *perm; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 555 | int r; |
| 556 | u_int i; |
| 557 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 558 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 559 | perm = &pset->permitted_user[i]; |
| 560 | if (perm->downstream != c) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 561 | continue; |
| 562 | |
| 563 | /* cancel on the server, since mux client is gone */ |
| 564 | debug("channel %d: cleanup remote forward for %s:%u", |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 565 | c->self, perm->listen_host, perm->listen_port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 566 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 567 | (r = sshpkt_put_cstring(ssh, |
| 568 | "cancel-tcpip-forward")) != 0 || |
| 569 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 570 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 571 | channel_rfwd_bind_host(perm->listen_host))) != 0 || |
| 572 | (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 573 | (r = sshpkt_send(ssh)) != 0) { |
| 574 | fatal("%s: channel %i: %s", __func__, |
| 575 | c->self, ssh_err(r)); |
| 576 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 577 | fwd_perm_clear(perm); /* unregister */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 578 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 582 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 583 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 584 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 585 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 586 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 587 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 588 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 589 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 590 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 591 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 592 | if ((other = sc->channels[i]) == NULL) |
| 593 | continue; |
| 594 | n++; |
| 595 | /* detach from mux client and prepare for closing */ |
| 596 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 597 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 598 | other->mux_ctx == c) { |
| 599 | other->mux_ctx = NULL; |
| 600 | other->type = SSH_CHANNEL_OPEN; |
| 601 | other->istate = CHAN_INPUT_CLOSED; |
| 602 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 605 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 606 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 607 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 608 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 609 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 610 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 611 | s = channel_open_message(ssh); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 612 | debug3("channel %d: status: %s", c->self, s); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 613 | free(s); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 614 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 615 | channel_close_fds(ssh, c); |
| 616 | sshbuf_free(c->input); |
| 617 | sshbuf_free(c->output); |
| 618 | sshbuf_free(c->extended); |
| 619 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 620 | free(c->remote_name); |
| 621 | c->remote_name = NULL; |
| 622 | free(c->path); |
| 623 | c->path = NULL; |
| 624 | free(c->listening_addr); |
| 625 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 626 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 627 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 628 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 629 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 630 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 631 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 632 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 633 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 634 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 635 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 636 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 637 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 638 | } |
| 639 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 640 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 641 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 642 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 643 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 644 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 645 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 646 | if (ssh->chanctxt->channels[i] != NULL) |
| 647 | channel_free(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 652 | * descriptors after a fork. |
| 653 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 654 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 655 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 656 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 657 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 658 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 659 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 660 | if (ssh->chanctxt->channels[i] != NULL) |
| 661 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 665 | * Stop listening to channels. |
| 666 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 667 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 668 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 669 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 670 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 671 | Channel *c; |
| 672 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 673 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 674 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 675 | if (c != NULL) { |
| 676 | switch (c->type) { |
| 677 | case SSH_CHANNEL_AUTH_SOCKET: |
| 678 | case SSH_CHANNEL_PORT_LISTENER: |
| 679 | case SSH_CHANNEL_RPORT_LISTENER: |
| 680 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 681 | case SSH_CHANNEL_UNIX_LISTENER: |
| 682 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 683 | channel_close_fd(ssh, &c->sock); |
| 684 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 692 | * Returns true if no channel has too much buffered data, and false if one or |
| 693 | * more channel is overfull. |
| 694 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 695 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 696 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 697 | { |
| 698 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 699 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 700 | Channel *c; |
| 701 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 702 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 703 | c = ssh->chanctxt->channels[i]; |
| 704 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 705 | continue; |
| 706 | if (sshbuf_len(c->output) > maxsize) { |
| 707 | debug2("channel %d: big output buffer %zu > %u", |
| 708 | c->self, sshbuf_len(c->output), maxsize); |
| 709 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 716 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 717 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 718 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 719 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 720 | Channel *c; |
| 721 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 722 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 723 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 724 | if (c == NULL) |
| 725 | continue; |
| 726 | switch (c->type) { |
| 727 | case SSH_CHANNEL_X11_LISTENER: |
| 728 | case SSH_CHANNEL_PORT_LISTENER: |
| 729 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 730 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 731 | case SSH_CHANNEL_CLOSED: |
| 732 | case SSH_CHANNEL_AUTH_SOCKET: |
| 733 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 734 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 735 | case SSH_CHANNEL_CONNECTING: |
| 736 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 737 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 738 | case SSH_CHANNEL_UNIX_LISTENER: |
| 739 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 740 | continue; |
| 741 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 742 | continue; |
| 743 | case SSH_CHANNEL_OPENING: |
| 744 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 745 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 746 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 747 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 748 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 749 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 750 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 751 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 752 | /* NOTREACHED */ |
| 753 | } |
| 754 | } |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 759 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 760 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 761 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 762 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 763 | Channel *c; |
| 764 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 765 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 766 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 767 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 768 | continue; |
| 769 | switch (c->type) { |
| 770 | case SSH_CHANNEL_CLOSED: |
| 771 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 772 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 773 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 774 | case SSH_CHANNEL_X11_LISTENER: |
| 775 | case SSH_CHANNEL_PORT_LISTENER: |
| 776 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 777 | case SSH_CHANNEL_MUX_LISTENER: |
| 778 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 779 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 780 | case SSH_CHANNEL_OPENING: |
| 781 | case SSH_CHANNEL_CONNECTING: |
| 782 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 783 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 784 | case SSH_CHANNEL_UNIX_LISTENER: |
| 785 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 786 | continue; |
| 787 | case SSH_CHANNEL_LARVAL: |
| 788 | case SSH_CHANNEL_AUTH_SOCKET: |
| 789 | case SSH_CHANNEL_OPEN: |
| 790 | case SSH_CHANNEL_X11_OPEN: |
| 791 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 792 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 793 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 794 | /* NOTREACHED */ |
| 795 | } |
| 796 | } |
| 797 | return -1; |
| 798 | } |
| 799 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 800 | /* |
| 801 | * Returns a message describing the currently open forwarded connections, |
| 802 | * suitable for sending to the client. The message contains crlf pairs for |
| 803 | * newlines. |
| 804 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 805 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 806 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 807 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 808 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 809 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 810 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 811 | int r; |
| 812 | char *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 813 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 814 | if ((buf = sshbuf_new()) == NULL) |
| 815 | fatal("%s: sshbuf_new", __func__); |
| 816 | if ((r = sshbuf_putf(buf, |
| 817 | "The following connections are open:\r\n")) != 0) |
| 818 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 819 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 820 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 821 | if (c == NULL) |
| 822 | continue; |
| 823 | switch (c->type) { |
| 824 | case SSH_CHANNEL_X11_LISTENER: |
| 825 | case SSH_CHANNEL_PORT_LISTENER: |
| 826 | case SSH_CHANNEL_RPORT_LISTENER: |
| 827 | case SSH_CHANNEL_CLOSED: |
| 828 | case SSH_CHANNEL_AUTH_SOCKET: |
| 829 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 830 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 831 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 832 | case SSH_CHANNEL_UNIX_LISTENER: |
| 833 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 834 | continue; |
| 835 | case SSH_CHANNEL_LARVAL: |
| 836 | case SSH_CHANNEL_OPENING: |
| 837 | case SSH_CHANNEL_CONNECTING: |
| 838 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 839 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 840 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 841 | case SSH_CHANNEL_OPEN: |
| 842 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 843 | case SSH_CHANNEL_MUX_PROXY: |
| 844 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 845 | if ((r = sshbuf_putf(buf, " #%d %.300s " |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 846 | "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n", |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 847 | c->self, c->remote_name, |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 848 | c->type, |
| 849 | c->have_remote_id ? "r" : "nr", c->remote_id, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 850 | c->istate, sshbuf_len(c->input), |
| 851 | c->ostate, sshbuf_len(c->output), |
| 852 | c->rfd, c->wfd, c->ctl_chan)) != 0) |
| 853 | fatal("%s: sshbuf_putf: %s", |
| 854 | __func__, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 855 | continue; |
| 856 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 857 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 858 | /* NOTREACHED */ |
| 859 | } |
| 860 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 861 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 862 | fatal("%s: sshbuf_dup_string", __func__); |
| 863 | sshbuf_free(buf); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | static void |
| 868 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 869 | { |
| 870 | int r; |
| 871 | |
| 872 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 873 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 874 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 875 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 876 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 877 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 878 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 882 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 883 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 884 | Channel *c = channel_lookup(ssh, id); |
| 885 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 886 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 887 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 888 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 891 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 892 | open_preamble(ssh, __func__, c, c->ctype); |
| 893 | if ((r = sshpkt_send(ssh)) != 0) |
| 894 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 898 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 899 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 900 | Channel *c = channel_lookup(ssh, id); |
| 901 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 902 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 903 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 904 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 907 | if (!c->have_remote_id) |
| 908 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 909 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 910 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 911 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 912 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 913 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 914 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 915 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 916 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 917 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 918 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 919 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 920 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 921 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 922 | { |
| 923 | struct channel_confirm *cc; |
| 924 | Channel *c; |
| 925 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 926 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 927 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 928 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 929 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 930 | cc->cb = cb; |
| 931 | cc->abandon_cb = abandon_cb; |
| 932 | cc->ctx = ctx; |
| 933 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 934 | } |
| 935 | |
| 936 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 937 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 938 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 939 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 940 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 941 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 942 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 943 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 946 | c->open_confirm = fn; |
| 947 | c->open_confirm_ctx = ctx; |
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_cleanup(struct ssh *ssh, int id, |
| 952 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 953 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 954 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 955 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 956 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 957 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 958 | return; |
| 959 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 960 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 961 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 962 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 963 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 964 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 965 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 966 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 967 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 968 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 969 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 970 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 971 | return; |
| 972 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 973 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 974 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 975 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 976 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 977 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 978 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 979 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 980 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 981 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 982 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 983 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 984 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 987 | c->input_filter = ifn; |
| 988 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 989 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 990 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 994 | 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] | 995 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 996 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 997 | Channel *c = channel_lookup(ssh, id); |
| 998 | int r; |
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 || c->type != SSH_CHANNEL_LARVAL) |
| 1001 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1002 | if (!c->have_remote_id) |
| 1003 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 1004 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1005 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1006 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 1007 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1008 | |
| 1009 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 1010 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1011 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1012 | (r = sshpkt_send(ssh)) != 0) |
| 1013 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1016 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1017 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 1018 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1019 | { |
| 1020 | FD_SET(c->sock, readset); |
| 1021 | } |
| 1022 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1023 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1024 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 1025 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1026 | { |
| 1027 | debug3("channel %d: waiting for connection", c->self); |
| 1028 | FD_SET(c->sock, writeset); |
| 1029 | } |
| 1030 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1031 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1032 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 1033 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1034 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1035 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1036 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1037 | sshbuf_len(c->input) < c->remote_window && |
| 1038 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1039 | FD_SET(c->rfd, readset); |
| 1040 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1041 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1042 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1043 | FD_SET(c->wfd, writeset); |
| 1044 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1045 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1046 | debug2("channel %d: " |
| 1047 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 1048 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1049 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1050 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1054 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 1055 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1056 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1057 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1058 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1059 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1060 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1061 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1062 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1063 | FD_SET(c->efd, readset); |
| 1064 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1065 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1066 | } |
| 1067 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1068 | /* |
| 1069 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1070 | * connection (when authentication spoofing is being done) remains in this |
| 1071 | * state until the first packet has been completely read. The authentication |
| 1072 | * data in that packet is then substituted by the real data if it matches the |
| 1073 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1074 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1075 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1076 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1077 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1078 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1079 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1080 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1081 | u_char *ucp; |
| 1082 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1083 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1084 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1085 | if (sc->x11_refuse_time != 0 && |
| 1086 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1087 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1088 | "expired"); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1092 | /* 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] | 1093 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1094 | return 0; |
| 1095 | |
| 1096 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1097 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1098 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1099 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1100 | data_len = 256 * ucp[8] + ucp[9]; |
| 1101 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1102 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1103 | data_len = ucp[8] + 256 * ucp[9]; |
| 1104 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1105 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1106 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1107 | return -1; |
| 1108 | } |
| 1109 | |
| 1110 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1111 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1112 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1113 | return 0; |
| 1114 | |
| 1115 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1116 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1117 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1118 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1119 | return -1; |
| 1120 | } |
| 1121 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1122 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1123 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1124 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1125 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1126 | return -1; |
| 1127 | } |
| 1128 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1129 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1130 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1131 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
| 1134 | /* |
| 1135 | * Received authentication protocol and data match |
| 1136 | * our fake data. Substitute the fake data with real |
| 1137 | * data. |
| 1138 | */ |
| 1139 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1140 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1141 | return 1; |
| 1142 | } |
| 1143 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1144 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1145 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1146 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1147 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1148 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* c->force_drain = 1; */ |
| 1151 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1152 | if (ret == 1) { |
| 1153 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1154 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1155 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1156 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1157 | debug2("X11 rejected %d i%d/o%d", |
| 1158 | c->self, c->istate, c->ostate); |
| 1159 | chan_read_failed(ssh, c); |
| 1160 | sshbuf_reset(c->input); |
| 1161 | chan_ibuf_empty(ssh, c); |
| 1162 | sshbuf_reset(c->output); |
| 1163 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1164 | 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] | 1165 | } |
| 1166 | } |
| 1167 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1168 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1169 | channel_pre_mux_client(struct ssh *ssh, |
| 1170 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1171 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1172 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1173 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1174 | FD_SET(c->rfd, readset); |
| 1175 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1176 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1177 | sshbuf_reset(c->input); |
| 1178 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1179 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1180 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1181 | } |
| 1182 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1183 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1184 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1185 | FD_SET(c->wfd, writeset); |
| 1186 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1187 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1191 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1192 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1193 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1194 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1195 | const u_char *p; |
| 1196 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1197 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1198 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1199 | struct { |
| 1200 | u_int8_t version; |
| 1201 | u_int8_t command; |
| 1202 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1203 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1204 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1205 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1206 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1207 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1208 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1209 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1210 | len = sizeof(s4_req); |
| 1211 | if (have < len) |
| 1212 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1213 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1214 | |
| 1215 | need = 1; |
| 1216 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1217 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1218 | debug2("channel %d: socks4a request", c->self); |
| 1219 | /* ... and needs an extra string (the hostname) */ |
| 1220 | need = 2; |
| 1221 | } |
| 1222 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1223 | for (found = 0, i = len; i < have; i++) { |
| 1224 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1225 | found++; |
| 1226 | if (found == need) |
| 1227 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1228 | } |
| 1229 | if (i > 1024) { |
| 1230 | /* the peer is probably sending garbage */ |
| 1231 | debug("channel %d: decode socks4: too long", |
| 1232 | c->self); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1236 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1237 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1238 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1239 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1240 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1241 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1242 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1243 | return -1; |
| 1244 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1245 | have = sshbuf_len(input); |
| 1246 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1247 | if (memchr(p, '\0', have) == NULL) { |
| 1248 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1249 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1250 | return -1; |
| 1251 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1252 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1253 | 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] | 1254 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1255 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1256 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1257 | fatal("%s: channel %d: consume: %s", __func__, |
| 1258 | c->self, ssh_err(r)); |
| 1259 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1260 | free(c->path); |
| 1261 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1262 | if (need == 1) { /* SOCKS4: one string */ |
| 1263 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1264 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1265 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1266 | have = sshbuf_len(input); |
| 1267 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1268 | if (memchr(p, '\0', have) == NULL) { |
| 1269 | error("channel %d: decode socks4a: host not nul " |
| 1270 | "terminated", c->self); |
| 1271 | return -1; |
| 1272 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1273 | len = strlen(p); |
| 1274 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1275 | c->self, p, len); |
| 1276 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1277 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1278 | error("channel %d: hostname \"%.100s\" too long", |
| 1279 | c->self, p); |
| 1280 | return -1; |
| 1281 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1282 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1283 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1284 | fatal("%s: channel %d: consume: %s", __func__, |
| 1285 | c->self, ssh_err(r)); |
| 1286 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1287 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1288 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1289 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1290 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1291 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1292 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1293 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1294 | debug("channel %d: cannot handle: %s cn %d", |
| 1295 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1296 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1297 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1298 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1299 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1300 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1301 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1302 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1303 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1304 | c->self, ssh_err(r)); |
| 1305 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1306 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1309 | /* try to decode a socks5 header */ |
| 1310 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1311 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1312 | #define SSH_SOCKS5_IPV4 0x01 |
| 1313 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1314 | #define SSH_SOCKS5_IPV6 0x04 |
| 1315 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1316 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1317 | |
| 1318 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1319 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1320 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1321 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1322 | struct { |
| 1323 | u_int8_t version; |
| 1324 | u_int8_t command; |
| 1325 | u_int8_t reserved; |
| 1326 | u_int8_t atyp; |
| 1327 | } s5_req, s5_rsp; |
| 1328 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1329 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1330 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1331 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1332 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1333 | |
| 1334 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1335 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1336 | if (p[0] != 0x05) |
| 1337 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1338 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1339 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1340 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1341 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1342 | return 0; |
| 1343 | nmethods = p[1]; |
| 1344 | if (have < nmethods + 2) |
| 1345 | return 0; |
| 1346 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1347 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1348 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1349 | found = 1; |
| 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | if (!found) { |
| 1354 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1355 | c->self); |
| 1356 | return -1; |
| 1357 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1358 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1359 | fatal("%s: channel %d: consume: %s", __func__, |
| 1360 | c->self, ssh_err(r)); |
| 1361 | } |
| 1362 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1363 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1364 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1365 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1366 | c->self, ssh_err(r)); |
| 1367 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1368 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1369 | debug2("channel %d: socks5 auth done", c->self); |
| 1370 | return 0; /* need more */ |
| 1371 | } |
| 1372 | debug2("channel %d: socks5 post auth", c->self); |
| 1373 | if (have < sizeof(s5_req)+1) |
| 1374 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1375 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1376 | if (s5_req.version != 0x05 || |
| 1377 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1378 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1379 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1380 | return -1; |
| 1381 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1382 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1383 | case SSH_SOCKS5_IPV4: |
| 1384 | addrlen = 4; |
| 1385 | af = AF_INET; |
| 1386 | break; |
| 1387 | case SSH_SOCKS5_DOMAIN: |
| 1388 | addrlen = p[sizeof(s5_req)]; |
| 1389 | af = -1; |
| 1390 | break; |
| 1391 | case SSH_SOCKS5_IPV6: |
| 1392 | addrlen = 16; |
| 1393 | af = AF_INET6; |
| 1394 | break; |
| 1395 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1396 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1397 | return -1; |
| 1398 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1399 | need = sizeof(s5_req) + addrlen + 2; |
| 1400 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1401 | need++; |
| 1402 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1403 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1404 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1405 | fatal("%s: channel %d: consume: %s", __func__, |
| 1406 | c->self, ssh_err(r)); |
| 1407 | } |
| 1408 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1409 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1410 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1411 | fatal("%s: channel %d: consume: %s", __func__, |
| 1412 | c->self, ssh_err(r)); |
| 1413 | } |
| 1414 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1415 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1416 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1417 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1418 | return -1; |
| 1419 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1420 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1421 | free(c->path); |
| 1422 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1423 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1424 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1425 | error("channel %d: dynamic request: socks5 hostname " |
| 1426 | "\"%.100s\" too long", c->self, dest_addr); |
| 1427 | return -1; |
| 1428 | } |
| 1429 | c->path = xstrdup(dest_addr); |
| 1430 | } else { |
| 1431 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1432 | return -1; |
| 1433 | c->path = xstrdup(ntop); |
| 1434 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1435 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1436 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1437 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1438 | c->self, c->path, c->host_port, s5_req.command); |
| 1439 | |
| 1440 | s5_rsp.version = 0x05; |
| 1441 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1442 | s5_rsp.reserved = 0; /* ignored */ |
| 1443 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1444 | dest_port = 0; /* ignored */ |
| 1445 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1446 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1447 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1448 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1449 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1450 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1451 | return 1; |
| 1452 | } |
| 1453 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1454 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1455 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1456 | 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] | 1457 | { |
| 1458 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1459 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1460 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1461 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1462 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1463 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1464 | 0, "stdio-forward", /*nonblock*/0); |
| 1465 | |
| 1466 | c->path = xstrdup(host_to_connect); |
| 1467 | c->host_port = port_to_connect; |
| 1468 | c->listening_port = 0; |
| 1469 | c->force_drain = 1; |
| 1470 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1471 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1472 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1473 | |
| 1474 | return c; |
| 1475 | } |
| 1476 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1477 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1478 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1479 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1480 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1481 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1482 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1483 | u_int have; |
| 1484 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1485 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1486 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1487 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1488 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1489 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1490 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1491 | /* need more */ |
| 1492 | FD_SET(c->sock, readset); |
| 1493 | return; |
| 1494 | } |
| 1495 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1496 | p = sshbuf_ptr(c->input); |
| 1497 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1498 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1499 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1500 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1501 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1502 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1503 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1504 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1505 | default: |
| 1506 | ret = -1; |
| 1507 | break; |
| 1508 | } |
| 1509 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1510 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1511 | } else if (ret == 0) { |
| 1512 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1513 | /* need more */ |
| 1514 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1515 | if (sshbuf_len(c->output)) |
| 1516 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1517 | } else { |
| 1518 | /* switch to the next state */ |
| 1519 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1520 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1523 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1524 | /* simulate read-error */ |
| 1525 | static void |
| 1526 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1527 | { |
| 1528 | c->type = SSH_CHANNEL_OPEN; |
| 1529 | chan_read_failed(ssh, c); |
| 1530 | sshbuf_reset(c->input); |
| 1531 | chan_ibuf_empty(ssh, c); |
| 1532 | sshbuf_reset(c->output); |
| 1533 | chan_write_failed(ssh, c); |
| 1534 | } |
| 1535 | |
| 1536 | /* reverse dynamic port forwarding */ |
| 1537 | static void |
| 1538 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1539 | { |
| 1540 | const u_char *p; |
| 1541 | u_int have, len; |
| 1542 | int r, ret; |
| 1543 | |
| 1544 | have = sshbuf_len(c->output); |
| 1545 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1546 | /* sshbuf_dump(c->output, stderr); */ |
| 1547 | /* EOF received */ |
| 1548 | if (c->flags & CHAN_EOF_RCVD) { |
| 1549 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1550 | fatal("%s: channel %d: consume: %s", |
| 1551 | __func__, c->self, ssh_err(r)); |
| 1552 | } |
| 1553 | rdynamic_close(ssh, c); |
| 1554 | return; |
| 1555 | } |
| 1556 | /* check if the fixed size part of the packet is in buffer. */ |
| 1557 | if (have < 3) |
| 1558 | return; |
| 1559 | /* try to guess the protocol */ |
| 1560 | p = sshbuf_ptr(c->output); |
| 1561 | switch (p[0]) { |
| 1562 | case 0x04: |
| 1563 | /* switch input/output for reverse forwarding */ |
| 1564 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1565 | break; |
| 1566 | case 0x05: |
| 1567 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1568 | break; |
| 1569 | default: |
| 1570 | ret = -1; |
| 1571 | break; |
| 1572 | } |
| 1573 | if (ret < 0) { |
| 1574 | rdynamic_close(ssh, c); |
| 1575 | } else if (ret == 0) { |
| 1576 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1577 | /* send socks request to peer */ |
| 1578 | len = sshbuf_len(c->input); |
| 1579 | if (len > 0 && len < c->remote_window) { |
| 1580 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1581 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1582 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1583 | (r = sshpkt_send(ssh)) != 0) { |
| 1584 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1585 | c->self, ssh_err(r)); |
| 1586 | } |
| 1587 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1588 | fatal("%s: channel %d: consume: %s", |
| 1589 | __func__, c->self, ssh_err(r)); |
| 1590 | } |
| 1591 | c->remote_window -= len; |
| 1592 | } |
| 1593 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1594 | /* the connect failed */ |
| 1595 | rdynamic_close(ssh, c); |
| 1596 | } |
| 1597 | } |
| 1598 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1599 | /* This is our fake X11 server socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1600 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1601 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1602 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1603 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1604 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1605 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1606 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1607 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1608 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1609 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1610 | if (!FD_ISSET(c->sock, readset)) |
| 1611 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1612 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1613 | debug("X11 connection requested."); |
| 1614 | addrlen = sizeof(addr); |
| 1615 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1616 | if (c->single_connection) { |
| 1617 | oerrno = errno; |
| 1618 | debug2("single_connection: closing X11 listener."); |
| 1619 | channel_close_fd(ssh, &c->sock); |
| 1620 | chan_mark_dead(ssh, c); |
| 1621 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1622 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1623 | if (newsock < 0) { |
| 1624 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1625 | errno != ECONNABORTED) |
| 1626 | error("accept: %.100s", strerror(errno)); |
| 1627 | if (errno == EMFILE || errno == ENFILE) |
| 1628 | c->notbefore = monotime() + 1; |
| 1629 | return; |
| 1630 | } |
| 1631 | set_nodelay(newsock); |
| 1632 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1633 | remote_port = get_peer_port(newsock); |
| 1634 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1635 | remote_ipaddr, remote_port); |
| 1636 | |
| 1637 | nc = channel_new(ssh, "accepted x11 socket", |
| 1638 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1639 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1640 | open_preamble(ssh, __func__, nc, "x11"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1641 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1642 | (r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1643 | fatal("%s: channel %i: reply %s", __func__, |
| 1644 | c->self, ssh_err(r)); |
| 1645 | } |
| 1646 | if ((r = sshpkt_send(ssh)) != 0) |
| 1647 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
| 1648 | free(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1649 | } |
| 1650 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1651 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1652 | port_open_helper(struct ssh *ssh, Channel *c, char *rtype) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1653 | { |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1654 | char *local_ipaddr = get_local_ipaddr(c->sock); |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 1655 | int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1656 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1657 | int remote_port = get_peer_port(c->sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1658 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1659 | |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1660 | if (remote_port == -1) { |
| 1661 | /* Fake addr/port to appease peers that validate it (Tectia) */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1662 | free(remote_ipaddr); |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1663 | remote_ipaddr = xstrdup("127.0.0.1"); |
| 1664 | remote_port = 65535; |
| 1665 | } |
| 1666 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1667 | free(c->remote_name); |
| 1668 | xasprintf(&c->remote_name, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1669 | "%s: listening port %d for %.100s port %d, " |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1670 | "connect from %.200s port %d to %.100s port %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1671 | rtype, c->listening_port, c->path, c->host_port, |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1672 | remote_ipaddr, remote_port, local_ipaddr, local_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1673 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1674 | open_preamble(ssh, __func__, c, rtype); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1675 | if (strcmp(rtype, "direct-tcpip") == 0) { |
| 1676 | /* target host, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1677 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1678 | (r = sshpkt_put_u32(ssh, c->host_port)) != 0) { |
| 1679 | fatal("%s: channel %i: reply %s", __func__, |
| 1680 | c->self, ssh_err(r)); |
| 1681 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1682 | } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) { |
| 1683 | /* target path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1684 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1685 | fatal("%s: channel %i: reply %s", __func__, |
| 1686 | c->self, ssh_err(r)); |
| 1687 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1688 | } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1689 | /* listen path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1690 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1691 | fatal("%s: channel %i: reply %s", __func__, |
| 1692 | c->self, ssh_err(r)); |
| 1693 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1694 | } else { |
| 1695 | /* listen address, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1696 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1697 | (r = sshpkt_put_u32(ssh, local_port)) != 0) { |
| 1698 | fatal("%s: channel %i: reply %s", __func__, |
| 1699 | c->self, ssh_err(r)); |
| 1700 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1701 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1702 | if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1703 | /* reserved for future owner/mode info */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1704 | if ((r = sshpkt_put_cstring(ssh, "")) != 0) { |
| 1705 | fatal("%s: channel %i: reply %s", __func__, |
| 1706 | c->self, ssh_err(r)); |
| 1707 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1708 | } else { |
| 1709 | /* originator host and port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1710 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1711 | (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) { |
| 1712 | fatal("%s: channel %i: reply %s", __func__, |
| 1713 | c->self, ssh_err(r)); |
| 1714 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1715 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1716 | if ((r = sshpkt_send(ssh)) != 0) |
| 1717 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1718 | free(remote_ipaddr); |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1719 | free(local_ipaddr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1722 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1723 | 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] | 1724 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1725 | ssh->chanctxt->x11_refuse_time = refuse_time; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1728 | /* |
| 1729 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1730 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1731 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1732 | channel_post_port_listener(struct ssh *ssh, Channel *c, |
| 1733 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1734 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1735 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1736 | struct sockaddr_storage addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1737 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1738 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1739 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1740 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1741 | if (!FD_ISSET(c->sock, readset)) |
| 1742 | return; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1743 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1744 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
| 1745 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1746 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1747 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1748 | nextstate = SSH_CHANNEL_OPENING; |
| 1749 | rtype = "forwarded-tcpip"; |
| 1750 | } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) { |
| 1751 | nextstate = SSH_CHANNEL_OPENING; |
| 1752 | rtype = "forwarded-streamlocal@openssh.com"; |
| 1753 | } else if (c->host_port == PORT_STREAMLOCAL) { |
| 1754 | nextstate = SSH_CHANNEL_OPENING; |
| 1755 | rtype = "direct-streamlocal@openssh.com"; |
| 1756 | } else if (c->host_port == 0) { |
| 1757 | nextstate = SSH_CHANNEL_DYNAMIC; |
| 1758 | rtype = "dynamic-tcpip"; |
| 1759 | } else { |
| 1760 | nextstate = SSH_CHANNEL_OPENING; |
| 1761 | rtype = "direct-tcpip"; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1762 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1763 | |
| 1764 | addrlen = sizeof(addr); |
| 1765 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1766 | if (newsock < 0) { |
| 1767 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1768 | errno != ECONNABORTED) |
| 1769 | error("accept: %.100s", strerror(errno)); |
| 1770 | if (errno == EMFILE || errno == ENFILE) |
| 1771 | c->notbefore = monotime() + 1; |
| 1772 | return; |
| 1773 | } |
| 1774 | if (c->host_port != PORT_STREAMLOCAL) |
| 1775 | set_nodelay(newsock); |
| 1776 | nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1, |
| 1777 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
| 1778 | nc->listening_port = c->listening_port; |
| 1779 | nc->host_port = c->host_port; |
| 1780 | if (c->path != NULL) |
| 1781 | nc->path = xstrdup(c->path); |
| 1782 | |
| 1783 | if (nextstate != SSH_CHANNEL_DYNAMIC) |
| 1784 | port_open_helper(ssh, nc, rtype); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * This is the authentication agent socket listening for connections from |
| 1789 | * clients. |
| 1790 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1791 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1792 | channel_post_auth_listener(struct ssh *ssh, Channel *c, |
| 1793 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1794 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1795 | Channel *nc; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1796 | int r, newsock; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1797 | struct sockaddr_storage addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1798 | socklen_t addrlen; |
| 1799 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1800 | if (!FD_ISSET(c->sock, readset)) |
| 1801 | return; |
| 1802 | |
| 1803 | addrlen = sizeof(addr); |
| 1804 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1805 | if (newsock < 0) { |
| 1806 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1807 | if (errno == EMFILE || errno == ENFILE) |
| 1808 | c->notbefore = monotime() + 1; |
| 1809 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1810 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1811 | nc = channel_new(ssh, "accepted auth socket", |
| 1812 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1813 | c->local_window_max, c->local_maxpacket, |
| 1814 | 0, "accepted auth socket", 1); |
| 1815 | open_preamble(ssh, __func__, nc, "auth-agent@openssh.com"); |
| 1816 | if ((r = sshpkt_send(ssh)) != 0) |
| 1817 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1818 | } |
| 1819 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1820 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1821 | channel_post_connecting(struct ssh *ssh, Channel *c, |
| 1822 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1823 | { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1824 | int err = 0, sock, isopen, r; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1825 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1826 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1827 | if (!FD_ISSET(c->sock, writeset)) |
| 1828 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1829 | if (!c->have_remote_id) |
| 1830 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1831 | /* for rdynamic the OPEN_CONFIRMATION has been sent already */ |
| 1832 | isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1833 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { |
| 1834 | err = errno; |
| 1835 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1836 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1837 | if (err == 0) { |
| 1838 | debug("channel %d: connected to %s port %d", |
| 1839 | c->self, c->connect_ctx.host, c->connect_ctx.port); |
| 1840 | channel_connect_ctx_free(&c->connect_ctx); |
| 1841 | c->type = SSH_CHANNEL_OPEN; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1842 | if (isopen) { |
| 1843 | /* no message necessary */ |
| 1844 | } else { |
| 1845 | if ((r = sshpkt_start(ssh, |
| 1846 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1847 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1848 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1849 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1850 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) |
| 1851 | != 0) |
| 1852 | fatal("%s: channel %i: confirm: %s", __func__, |
| 1853 | c->self, ssh_err(r)); |
| 1854 | if ((r = sshpkt_send(ssh)) != 0) |
| 1855 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1856 | ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } else { |
| 1859 | debug("channel %d: connection failed: %s", |
| 1860 | c->self, strerror(err)); |
| 1861 | /* Try next address, if any */ |
| 1862 | if ((sock = connect_next(&c->connect_ctx)) > 0) { |
| 1863 | close(c->sock); |
| 1864 | c->sock = c->rfd = c->wfd = sock; |
| 1865 | channel_find_maxfd(ssh->chanctxt); |
| 1866 | return; |
| 1867 | } |
| 1868 | /* Exhausted all addresses */ |
| 1869 | error("connect_to %.100s port %d: failed.", |
| 1870 | c->connect_ctx.host, c->connect_ctx.port); |
| 1871 | channel_connect_ctx_free(&c->connect_ctx); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1872 | if (isopen) { |
| 1873 | rdynamic_close(ssh, c); |
| 1874 | } else { |
| 1875 | if ((r = sshpkt_start(ssh, |
| 1876 | SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1877 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1878 | (r = sshpkt_put_u32(ssh, |
| 1879 | SSH2_OPEN_CONNECT_FAILED)) != 0 || |
| 1880 | (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1881 | (r = sshpkt_put_cstring(ssh, "")) != 0) { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1882 | fatal("%s: channel %i: failure: %s", __func__, |
| 1883 | c->self, ssh_err(r)); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1884 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1885 | if ((r = sshpkt_send(ssh)) != 0) |
| 1886 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1887 | ssh_err(r)); |
| 1888 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1889 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1890 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1893 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1894 | channel_handle_rfd(struct ssh *ssh, Channel *c, |
| 1895 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1896 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1897 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1898 | ssize_t len; |
| 1899 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1900 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1901 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1902 | |
| 1903 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1904 | return 1; |
| 1905 | |
| 1906 | errno = 0; |
| 1907 | len = read(c->rfd, buf, sizeof(buf)); |
| 1908 | if (len < 0 && (errno == EINTR || |
| 1909 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1910 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1911 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1912 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1913 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1914 | if ((!c->isatty && len <= 0) || |
| 1915 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1916 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1917 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1918 | c->self, c->rfd, len); |
| 1919 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1920 | debug2("channel %d: not open", c->self); |
| 1921 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1922 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1923 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1924 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1925 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1926 | return -1; |
| 1927 | } |
| 1928 | if (c->input_filter != NULL) { |
| 1929 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1930 | debug2("channel %d: filter stops", c->self); |
| 1931 | chan_read_failed(ssh, c); |
| 1932 | } |
| 1933 | } else if (c->datagram) { |
| 1934 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1935 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1936 | c->self, ssh_err(r)); |
| 1937 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1938 | fatal("%s: channel %d: put data: %s", __func__, |
| 1939 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1940 | } |
| 1941 | return 1; |
| 1942 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1943 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1944 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1945 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1946 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1947 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1948 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1949 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 1950 | size_t dlen, olen = 0; |
| 1951 | int r, len; |
| 1952 | |
| 1953 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 1954 | sshbuf_len(c->output) == 0) |
| 1955 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1956 | |
| 1957 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1958 | olen = sshbuf_len(c->output); |
| 1959 | if (c->output_filter != NULL) { |
| 1960 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 1961 | debug2("channel %d: filter stops", c->self); |
| 1962 | if (c->type != SSH_CHANNEL_OPEN) |
| 1963 | chan_mark_dead(ssh, c); |
| 1964 | else |
| 1965 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1966 | return -1; |
| 1967 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1968 | } else if (c->datagram) { |
| 1969 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 1970 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 1971 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 1972 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1973 | } else { |
| 1974 | buf = data = sshbuf_mutable_ptr(c->output); |
| 1975 | dlen = sshbuf_len(c->output); |
| 1976 | } |
| 1977 | |
| 1978 | if (c->datagram) { |
| 1979 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 1980 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1981 | free(data); |
| 1982 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 1983 | errno == EWOULDBLOCK)) |
| 1984 | return 1; |
| 1985 | if (len <= 0) |
| 1986 | goto write_fail; |
| 1987 | goto out; |
| 1988 | } |
| 1989 | |
| 1990 | #ifdef _AIX |
| 1991 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 1992 | if (c->wfd_isatty) |
| 1993 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 1994 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1995 | |
| 1996 | len = write(c->wfd, buf, dlen); |
| 1997 | if (len < 0 && |
| 1998 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 1999 | return 1; |
| 2000 | if (len <= 0) { |
| 2001 | write_fail: |
| 2002 | if (c->type != SSH_CHANNEL_OPEN) { |
| 2003 | debug2("channel %d: not open", c->self); |
| 2004 | chan_mark_dead(ssh, c); |
| 2005 | return -1; |
| 2006 | } else { |
| 2007 | chan_write_failed(ssh, c); |
| 2008 | } |
| 2009 | return -1; |
| 2010 | } |
| 2011 | #ifndef BROKEN_TCGETATTR_ICANON |
| 2012 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 2013 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 2014 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 2015 | /* |
| 2016 | * Simulate echo to reduce the impact of |
| 2017 | * traffic analysis. We need to match the |
| 2018 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 2019 | * (4 byte channel id + buf) |
| 2020 | */ |
| 2021 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 2022 | (r = sshpkt_send(ssh)) != 0) |
| 2023 | fatal("%s: channel %d: ignore: %s", |
| 2024 | __func__, c->self, ssh_err(r)); |
| 2025 | } |
| 2026 | } |
| 2027 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 2028 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 2029 | fatal("%s: channel %d: consume: %s", |
| 2030 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2031 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2032 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2033 | c->local_consumed += olen - sshbuf_len(c->output); |
| 2034 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2035 | return 1; |
| 2036 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2037 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2038 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2039 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 2040 | fd_set *readset, fd_set *writeset) |
| 2041 | { |
| 2042 | int r; |
| 2043 | ssize_t len; |
| 2044 | |
| 2045 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 2046 | return 1; |
| 2047 | |
| 2048 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 2049 | sshbuf_len(c->extended)); |
| 2050 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
| 2051 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2052 | errno == EWOULDBLOCK)) |
| 2053 | return 1; |
| 2054 | if (len <= 0) { |
| 2055 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2056 | channel_close_fd(ssh, &c->efd); |
| 2057 | } else { |
| 2058 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2059 | fatal("%s: channel %d: consume: %s", |
| 2060 | __func__, c->self, ssh_err(r)); |
| 2061 | } |
| 2062 | c->local_consumed += len; |
| 2063 | } |
| 2064 | return 1; |
| 2065 | } |
| 2066 | |
| 2067 | static int |
| 2068 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2069 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2070 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2071 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2072 | int r; |
| 2073 | ssize_t len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2074 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2075 | if (!c->detach_close && !FD_ISSET(c->efd, readset)) |
| 2076 | return 1; |
| 2077 | |
| 2078 | len = read(c->efd, buf, sizeof(buf)); |
| 2079 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
| 2080 | if (len < 0 && (errno == EINTR || ((errno == EAGAIN || |
| 2081 | errno == EWOULDBLOCK) && !c->detach_close))) |
| 2082 | return 1; |
| 2083 | if (len <= 0) { |
| 2084 | debug2("channel %d: closing read-efd %d", |
| 2085 | c->self, c->efd); |
| 2086 | channel_close_fd(ssh, &c->efd); |
| 2087 | } else { |
| 2088 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2089 | debug3("channel %d: discard efd", |
| 2090 | c->self); |
| 2091 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2092 | fatal("%s: channel %d: append: %s", |
| 2093 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | return 1; |
| 2097 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2098 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2099 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2100 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2101 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2102 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2103 | if (c->efd == -1) |
| 2104 | return 1; |
| 2105 | |
| 2106 | /** XXX handle drain efd, too */ |
| 2107 | |
| 2108 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2109 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2110 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2111 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2112 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2113 | |
| 2114 | return 1; |
| 2115 | } |
| 2116 | |
| 2117 | static int |
| 2118 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2119 | { |
| 2120 | int r; |
| 2121 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2122 | if (c->type == SSH_CHANNEL_OPEN && |
| 2123 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2124 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2125 | c->local_maxpacket*3) || |
| 2126 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2127 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2128 | if (!c->have_remote_id) |
| 2129 | fatal(":%s: channel %d: no remote id", |
| 2130 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2131 | if ((r = sshpkt_start(ssh, |
| 2132 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2133 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2134 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2135 | (r = sshpkt_send(ssh)) != 0) { |
| 2136 | fatal("%s: channel %i: %s", __func__, |
| 2137 | c->self, ssh_err(r)); |
| 2138 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2139 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2140 | c->self, c->local_window, |
| 2141 | c->local_consumed); |
| 2142 | c->local_window += c->local_consumed; |
| 2143 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2144 | } |
| 2145 | return 1; |
| 2146 | } |
| 2147 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2148 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2149 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2150 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2151 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2152 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2153 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2154 | channel_handle_efd(ssh, c, readset, writeset); |
| 2155 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2156 | } |
| 2157 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2158 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2159 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2160 | { |
| 2161 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2162 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2163 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2164 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2165 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2166 | if (sshbuf_len(c->input) < need) { |
| 2167 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2168 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2169 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2170 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2171 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2172 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2173 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2174 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2175 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2176 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2177 | fatal("%s: channel %d: append: %s", |
| 2178 | __func__, c->self, ssh_err(r)); |
| 2179 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2180 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2181 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2185 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2186 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2187 | { |
| 2188 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2189 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2190 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2191 | return; |
| 2192 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2193 | return; |
| 2194 | if (c->mux_pause) |
| 2195 | return; |
| 2196 | |
| 2197 | /* |
| 2198 | * Don't not read past the precise end of packets to |
| 2199 | * avoid disrupting fd passing. |
| 2200 | */ |
| 2201 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2202 | return; |
| 2203 | /* XXX sshbuf_peek_u32 */ |
| 2204 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2205 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2206 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2207 | debug2("channel %d: packet too big %u > %u", |
| 2208 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2209 | chan_rcvd_oclose(ssh, c); |
| 2210 | return; |
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 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2213 | return; |
| 2214 | if (c->mux_rcb(ssh, c) != 0) { |
| 2215 | debug("channel %d: mux_rcb failed", c->self); |
| 2216 | chan_mark_dead(ssh, c); |
| 2217 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2222 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2223 | fd_set *readset, fd_set *writeset) |
| 2224 | { |
| 2225 | ssize_t len; |
| 2226 | int r; |
| 2227 | |
| 2228 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2229 | sshbuf_len(c->output) == 0) |
| 2230 | return; |
| 2231 | |
| 2232 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
| 2233 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 2234 | return; |
| 2235 | if (len <= 0) { |
| 2236 | chan_mark_dead(ssh, c); |
| 2237 | return; |
| 2238 | } |
| 2239 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2240 | fatal("%s: channel %d: consume: %s", __func__, |
| 2241 | c->self, ssh_err(r)); |
| 2242 | } |
| 2243 | |
| 2244 | static void |
| 2245 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2246 | fd_set *readset, fd_set *writeset) |
| 2247 | { |
| 2248 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2249 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2250 | } |
| 2251 | |
| 2252 | static void |
| 2253 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2254 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2255 | { |
| 2256 | Channel *nc; |
| 2257 | struct sockaddr_storage addr; |
| 2258 | socklen_t addrlen; |
| 2259 | int newsock; |
| 2260 | uid_t euid; |
| 2261 | gid_t egid; |
| 2262 | |
| 2263 | if (!FD_ISSET(c->sock, readset)) |
| 2264 | return; |
| 2265 | |
| 2266 | debug("multiplexing control connection"); |
| 2267 | |
| 2268 | /* |
| 2269 | * Accept connection on control socket |
| 2270 | */ |
| 2271 | memset(&addr, 0, sizeof(addr)); |
| 2272 | addrlen = sizeof(addr); |
| 2273 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2274 | &addrlen)) == -1) { |
| 2275 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2276 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2277 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2278 | return; |
| 2279 | } |
| 2280 | |
| 2281 | if (getpeereid(newsock, &euid, &egid) < 0) { |
| 2282 | error("%s getpeereid failed: %s", __func__, |
| 2283 | strerror(errno)); |
| 2284 | close(newsock); |
| 2285 | return; |
| 2286 | } |
| 2287 | if ((euid != 0) && (getuid() != euid)) { |
| 2288 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2289 | (u_int)euid, (u_int)getuid()); |
| 2290 | close(newsock); |
| 2291 | return; |
| 2292 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2293 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2294 | newsock, newsock, -1, c->local_window_max, |
| 2295 | c->local_maxpacket, 0, "mux-control", 1); |
| 2296 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2297 | 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] | 2298 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2299 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2300 | /* mux state transitions must not elicit protocol messages */ |
| 2301 | nc->flags |= CHAN_LOCAL; |
| 2302 | } |
| 2303 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2304 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2305 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2306 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2307 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2308 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2309 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2310 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2311 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2312 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2313 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2314 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2315 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2316 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2317 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2318 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2319 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2320 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2321 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2322 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2323 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2324 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2325 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2326 | |
| 2327 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2328 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2329 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2330 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2331 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2332 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2333 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2334 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2335 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2336 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2337 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2338 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2339 | |
| 2340 | sc->channel_pre = pre; |
| 2341 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2342 | } |
| 2343 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2344 | /* gc dead channels */ |
| 2345 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2346 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2347 | { |
| 2348 | if (c == NULL) |
| 2349 | return; |
| 2350 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2351 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2352 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2353 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2354 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2355 | /* if we still have a callback */ |
| 2356 | if (c->detach_user != NULL) |
| 2357 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2358 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2359 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2360 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2361 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2362 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2363 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2364 | } |
| 2365 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2366 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2367 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2368 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2369 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2370 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2371 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2372 | struct ssh_channels *sc = ssh->chanctxt; |
| 2373 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2374 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2375 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2376 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2377 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2378 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2379 | if (unpause_secs != NULL) |
| 2380 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2381 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2382 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2383 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2384 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2385 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2386 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2387 | c->delayed = 0; |
| 2388 | else |
| 2389 | continue; |
| 2390 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2391 | if (ftab[c->type] != NULL) { |
| 2392 | /* |
| 2393 | * Run handlers that are not paused. |
| 2394 | */ |
| 2395 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2396 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2397 | else if (unpause_secs != NULL) { |
| 2398 | /* |
| 2399 | * Collect the time that the earliest |
| 2400 | * channel comes off pause. |
| 2401 | */ |
| 2402 | debug3("%s: chan %d: skip for %d more seconds", |
| 2403 | __func__, c->self, |
| 2404 | (int)(c->notbefore - now)); |
| 2405 | if (*unpause_secs == 0 || |
| 2406 | (c->notbefore - now) < *unpause_secs) |
| 2407 | *unpause_secs = c->notbefore - now; |
| 2408 | } |
| 2409 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2410 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2411 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2412 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2413 | debug3("%s: first channel unpauses in %d seconds", |
| 2414 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2415 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2416 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2417 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2418 | * Create sockets before allocating the select bitmasks. |
| 2419 | * This is necessary for things that need to happen after reading |
| 2420 | * the network-input but before channel_prepare_select(). |
| 2421 | */ |
| 2422 | static void |
| 2423 | channel_before_prepare_select(struct ssh *ssh) |
| 2424 | { |
| 2425 | struct ssh_channels *sc = ssh->chanctxt; |
| 2426 | Channel *c; |
| 2427 | u_int i, oalloc; |
| 2428 | |
| 2429 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2430 | c = sc->channels[i]; |
| 2431 | if (c == NULL) |
| 2432 | continue; |
| 2433 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2434 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2439 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2440 | * select bitmasks. |
| 2441 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2442 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2443 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2444 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2445 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2446 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2447 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2448 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2449 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2450 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2451 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2452 | nfdset = howmany(n+1, NFDBITS); |
| 2453 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2454 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2455 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2456 | sz = nfdset * sizeof(fd_mask); |
| 2457 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2458 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2459 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2460 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2461 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2462 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2463 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2464 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2465 | memset(*readsetp, 0, sz); |
| 2466 | memset(*writesetp, 0, sz); |
| 2467 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2468 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2469 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2470 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2471 | } |
| 2472 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2473 | /* |
| 2474 | * After select, perform any appropriate operations for channels which have |
| 2475 | * events pending. |
| 2476 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2477 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2478 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2479 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2480 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2481 | } |
| 2482 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2483 | /* |
| 2484 | * Enqueue data for channels with open or draining c->input. |
| 2485 | */ |
| 2486 | static void |
| 2487 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2488 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2489 | size_t len, plen; |
| 2490 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2491 | int r; |
| 2492 | |
| 2493 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2494 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2495 | /* |
| 2496 | * input-buffer is empty and read-socket shutdown: |
| 2497 | * tell peer, that we will not send more data: |
| 2498 | * send IEOF. |
| 2499 | * hack for extended data: delay EOF if EFD still |
| 2500 | * in use. |
| 2501 | */ |
| 2502 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2503 | debug2("channel %d: " |
| 2504 | "ibuf_empty delayed efd %d/(%zu)", |
| 2505 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2506 | else |
| 2507 | chan_ibuf_empty(ssh, c); |
| 2508 | } |
| 2509 | return; |
| 2510 | } |
| 2511 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2512 | if (!c->have_remote_id) |
| 2513 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2514 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2515 | if (c->datagram) { |
| 2516 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2517 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2518 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2519 | c->self, ssh_err(r)); |
| 2520 | /* |
| 2521 | * XXX this does tail-drop on the datagram queue which is |
| 2522 | * usually suboptimal compared to head-drop. Better to have |
| 2523 | * backpressure at read time? (i.e. read + discard) |
| 2524 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2525 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2526 | debug("channel %d: datagram too big", c->self); |
| 2527 | return; |
| 2528 | } |
| 2529 | /* Enqueue it */ |
| 2530 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2531 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2532 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2533 | (r = sshpkt_send(ssh)) != 0) { |
| 2534 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2535 | c->self, ssh_err(r)); |
| 2536 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2537 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
| 2540 | |
| 2541 | /* Enqueue packet for buffered data. */ |
| 2542 | if (len > c->remote_window) |
| 2543 | len = c->remote_window; |
| 2544 | if (len > c->remote_maxpacket) |
| 2545 | len = c->remote_maxpacket; |
| 2546 | if (len == 0) |
| 2547 | return; |
| 2548 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2549 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2550 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2551 | (r = sshpkt_send(ssh)) != 0) { |
| 2552 | fatal("%s: channel %i: data: %s", __func__, |
| 2553 | c->self, ssh_err(r)); |
| 2554 | } |
| 2555 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2556 | fatal("%s: channel %i: consume: %s", __func__, |
| 2557 | c->self, ssh_err(r)); |
| 2558 | c->remote_window -= len; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * Enqueue data for channels with open c->extended in read mode. |
| 2563 | */ |
| 2564 | static void |
| 2565 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2566 | { |
| 2567 | size_t len; |
| 2568 | int r; |
| 2569 | |
| 2570 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2571 | return; |
| 2572 | |
| 2573 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2574 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2575 | if (len > c->remote_window) |
| 2576 | len = c->remote_window; |
| 2577 | if (len > c->remote_maxpacket) |
| 2578 | len = c->remote_maxpacket; |
| 2579 | if (len == 0) |
| 2580 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2581 | if (!c->have_remote_id) |
| 2582 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2583 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2584 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2585 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2586 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2587 | (r = sshpkt_send(ssh)) != 0) { |
| 2588 | fatal("%s: channel %i: data: %s", __func__, |
| 2589 | c->self, ssh_err(r)); |
| 2590 | } |
| 2591 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2592 | fatal("%s: channel %i: consume: %s", __func__, |
| 2593 | c->self, ssh_err(r)); |
| 2594 | c->remote_window -= len; |
| 2595 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2596 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2597 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2598 | /* 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] | 2599 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2600 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2601 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2602 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2603 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2604 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2605 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2606 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2607 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2608 | if (c == NULL) |
| 2609 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2610 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2611 | /* |
| 2612 | * We are only interested in channels that can have buffered |
| 2613 | * incoming data. |
| 2614 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2615 | if (c->type != SSH_CHANNEL_OPEN) |
| 2616 | continue; |
| 2617 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2618 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2619 | debug3("channel %d: will not send data after close", |
| 2620 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2621 | continue; |
| 2622 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2623 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2624 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2625 | if (c->istate == CHAN_INPUT_OPEN || |
| 2626 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2627 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2628 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2629 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2630 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2631 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2632 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2633 | } |
| 2634 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2635 | /* -- mux proxy support */ |
| 2636 | |
| 2637 | /* |
| 2638 | * When multiplexing channel messages for mux clients we have to deal |
| 2639 | * with downstream messages from the mux client and upstream messages |
| 2640 | * from the ssh server: |
| 2641 | * 1) Handling downstream messages is straightforward and happens |
| 2642 | * in channel_proxy_downstream(): |
| 2643 | * - We forward all messages (mostly) unmodified to the server. |
| 2644 | * - However, in order to route messages from upstream to the correct |
| 2645 | * downstream client, we have to replace the channel IDs used by the |
| 2646 | * mux clients with a unique channel ID because the mux clients might |
| 2647 | * use conflicting channel IDs. |
| 2648 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2649 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2650 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2651 | * with the newly allocated channel ID. |
| 2652 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2653 | * channels and processed by channel_proxy_upstream(). The local channel ID |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2654 | * is then translated back to the original mux client ID. |
| 2655 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2656 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2657 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2658 | * downstream mux client are removed. |
| 2659 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2660 | * requires more work, because they are not addressed to a specific |
| 2661 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2662 | * out whether the request is addressed to the local client or a |
| 2663 | * specific downstream client based on the listen-address/port. |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2664 | * 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] | 2665 | * not supported as the matching session/channel cannot be identified |
| 2666 | * easily. |
| 2667 | */ |
| 2668 | |
| 2669 | /* |
| 2670 | * receive packets from downstream mux clients: |
| 2671 | * channel callback fired on read from mux client, creates |
| 2672 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2673 | * on channel creation. |
| 2674 | */ |
| 2675 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2676 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2677 | { |
| 2678 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2679 | struct sshbuf *original = NULL, *modified = NULL; |
| 2680 | const u_char *cp; |
| 2681 | char *ctype = NULL, *listen_host = NULL; |
| 2682 | u_char type; |
| 2683 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2684 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2685 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2686 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2687 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2688 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2689 | != 0) { |
| 2690 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2691 | return -1; |
| 2692 | } |
| 2693 | if (have < 2) { |
| 2694 | error("%s: short message", __func__); |
| 2695 | return -1; |
| 2696 | } |
| 2697 | type = cp[1]; |
| 2698 | /* skip padlen + type */ |
| 2699 | cp += 2; |
| 2700 | have -= 2; |
| 2701 | if (ssh_packet_log_type(type)) |
| 2702 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2703 | downstream->self, type); |
| 2704 | |
| 2705 | switch (type) { |
| 2706 | case SSH2_MSG_CHANNEL_OPEN: |
| 2707 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2708 | (modified = sshbuf_new()) == NULL) { |
| 2709 | error("%s: alloc", __func__); |
| 2710 | goto out; |
| 2711 | } |
| 2712 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2713 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2714 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2715 | goto out; |
| 2716 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2717 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2718 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2719 | c->mux_ctx = downstream; /* point to mux client */ |
| 2720 | c->mux_downstream_id = id; /* original downstream id */ |
| 2721 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2722 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2723 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2724 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2725 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2726 | goto out; |
| 2727 | } |
| 2728 | break; |
| 2729 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2730 | /* |
| 2731 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2732 | * need to parse 'remote_id' instead of 'ctype'. |
| 2733 | */ |
| 2734 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2735 | (modified = sshbuf_new()) == NULL) { |
| 2736 | error("%s: alloc", __func__); |
| 2737 | goto out; |
| 2738 | } |
| 2739 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2740 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2741 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2742 | goto out; |
| 2743 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2744 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2745 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2746 | c->mux_ctx = downstream; /* point to mux client */ |
| 2747 | c->mux_downstream_id = id; |
| 2748 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2749 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2750 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2751 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2752 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2753 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2754 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2755 | goto out; |
| 2756 | } |
| 2757 | break; |
| 2758 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2759 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2760 | error("%s: alloc", __func__); |
| 2761 | goto out; |
| 2762 | } |
| 2763 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2764 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2765 | goto out; |
| 2766 | } |
| 2767 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2768 | error("%s: unsupported request %s", __func__, ctype); |
| 2769 | goto out; |
| 2770 | } |
| 2771 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2772 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2773 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2774 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2775 | goto out; |
| 2776 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2777 | if (listen_port > 65535) { |
| 2778 | error("%s: tcpip-forward for %s: bad port %u", |
| 2779 | __func__, listen_host, listen_port); |
| 2780 | goto out; |
| 2781 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2782 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 2783 | permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2784 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2785 | listen_host = NULL; |
| 2786 | break; |
| 2787 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2788 | if (have < 4) |
| 2789 | break; |
| 2790 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2791 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2792 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2793 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2794 | else |
| 2795 | c->flags |= CHAN_CLOSE_SENT; |
| 2796 | } |
| 2797 | break; |
| 2798 | } |
| 2799 | if (modified) { |
| 2800 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2801 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2802 | (r = sshpkt_send(ssh)) != 0) { |
| 2803 | error("%s: send %s", __func__, ssh_err(r)); |
| 2804 | goto out; |
| 2805 | } |
| 2806 | } else { |
| 2807 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2808 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2809 | (r = sshpkt_send(ssh)) != 0) { |
| 2810 | error("%s: send %s", __func__, ssh_err(r)); |
| 2811 | goto out; |
| 2812 | } |
| 2813 | } |
| 2814 | ret = 0; |
| 2815 | out: |
| 2816 | free(ctype); |
| 2817 | free(listen_host); |
| 2818 | sshbuf_free(original); |
| 2819 | sshbuf_free(modified); |
| 2820 | return ret; |
| 2821 | } |
| 2822 | |
| 2823 | /* |
| 2824 | * receive packets from upstream server and de-multiplex packets |
| 2825 | * to correct downstream: |
| 2826 | * implemented as a helper for channel input handlers, |
| 2827 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2828 | */ |
| 2829 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2830 | 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] | 2831 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2832 | struct sshbuf *b = NULL; |
| 2833 | Channel *downstream; |
| 2834 | const u_char *cp = NULL; |
| 2835 | size_t len; |
| 2836 | int r; |
| 2837 | |
| 2838 | /* |
| 2839 | * When receiving packets from the peer we need to check whether we |
| 2840 | * 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] | 2841 | * restore the original channel id and keep track of CLOSE messages, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2842 | * so we can cleanup the channel. |
| 2843 | */ |
| 2844 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2845 | return 0; |
| 2846 | if ((downstream = c->mux_ctx) == NULL) |
| 2847 | return 0; |
| 2848 | switch (type) { |
| 2849 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2850 | case SSH2_MSG_CHANNEL_DATA: |
| 2851 | case SSH2_MSG_CHANNEL_EOF: |
| 2852 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2853 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2854 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2855 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2856 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2857 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2858 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2859 | break; |
| 2860 | default: |
| 2861 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2862 | c->self, type); |
| 2863 | return 0; |
| 2864 | } |
| 2865 | if ((b = sshbuf_new()) == NULL) { |
| 2866 | error("%s: alloc reply", __func__); |
| 2867 | goto out; |
| 2868 | } |
| 2869 | /* get remaining payload (after id) */ |
| 2870 | cp = sshpkt_ptr(ssh, &len); |
| 2871 | if (cp == NULL) { |
| 2872 | error("%s: no packet", __func__); |
| 2873 | goto out; |
| 2874 | } |
| 2875 | /* translate id and send to muxclient */ |
| 2876 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2877 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2878 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2879 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2880 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2881 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2882 | goto out; |
| 2883 | } |
| 2884 | /* sshbuf_dump(b, stderr); */ |
| 2885 | if (ssh_packet_log_type(type)) |
| 2886 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2887 | type); |
| 2888 | out: |
| 2889 | /* update state */ |
| 2890 | switch (type) { |
| 2891 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2892 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2893 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2894 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2895 | c->have_remote_id = 1; |
| 2896 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2897 | break; |
| 2898 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2899 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2900 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2901 | else |
| 2902 | c->flags |= CHAN_CLOSE_RCVD; |
| 2903 | break; |
| 2904 | } |
| 2905 | sshbuf_free(b); |
| 2906 | return 1; |
| 2907 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2908 | |
| 2909 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2910 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2911 | /* Parse a channel ID from the current packet */ |
| 2912 | static int |
| 2913 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2914 | { |
| 2915 | u_int32_t id; |
| 2916 | int r; |
| 2917 | |
| 2918 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2919 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2920 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2921 | } |
| 2922 | if (id > INT_MAX) { |
| 2923 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2924 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2925 | } |
| 2926 | return (int)id; |
| 2927 | } |
| 2928 | |
| 2929 | /* Lookup a channel from an ID in the current packet */ |
| 2930 | static Channel * |
| 2931 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2932 | { |
| 2933 | int id = channel_parse_id(ssh, where, what); |
| 2934 | Channel *c; |
| 2935 | |
| 2936 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2937 | ssh_packet_disconnect(ssh, |
| 2938 | "%s packet referred to nonexistent channel %d", what, id); |
| 2939 | } |
| 2940 | return c; |
| 2941 | } |
| 2942 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2943 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2944 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2945 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2946 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2947 | size_t data_len, win_len; |
| 2948 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 2949 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2950 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2951 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2952 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2953 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2954 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2955 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2956 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 2957 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2958 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2959 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2960 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2961 | /* Get the data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2962 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) |
| 2963 | fatal("%s: channel %d: get data: %s", __func__, |
| 2964 | c->self, ssh_err(r)); |
| 2965 | ssh_packet_check_eom(ssh); |
| 2966 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2967 | win_len = data_len; |
| 2968 | if (c->datagram) |
| 2969 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2970 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2971 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2972 | * The sending side reduces its window as it sends data, so we |
| 2973 | * must 'fake' consumption of the data in order to ensure that window |
| 2974 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2975 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2976 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 2977 | c->local_window -= win_len; |
| 2978 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2979 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2980 | } |
| 2981 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2982 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2983 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2984 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2985 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2986 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2987 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2988 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2989 | c->self, win_len, c->local_window); |
| 2990 | return 0; |
| 2991 | } |
| 2992 | c->local_window -= win_len; |
| 2993 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2994 | if (c->datagram) { |
| 2995 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 2996 | fatal("%s: channel %d: append datagram: %s", |
| 2997 | __func__, c->self, ssh_err(r)); |
| 2998 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 2999 | fatal("%s: channel %d: append data: %s", |
| 3000 | __func__, c->self, ssh_err(r)); |
| 3001 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3002 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3003 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3004 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3005 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3006 | 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] | 3007 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3008 | const u_char *data; |
| 3009 | size_t data_len; |
| 3010 | u_int32_t tcode; |
| 3011 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 3012 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3013 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3014 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3015 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3016 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3017 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3018 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3019 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3020 | if (c->flags & CHAN_EOF_RCVD) { |
| 3021 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3022 | debug("channel %d: accepting ext data after eof", |
| 3023 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3024 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3025 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 3026 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3027 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3028 | |
| 3029 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 3030 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 3031 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3032 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3033 | if (c->efd == -1 || |
| 3034 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 3035 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 3036 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3037 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3038 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3039 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) { |
| 3040 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3041 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3042 | } |
| 3043 | ssh_packet_check_eom(ssh); |
| 3044 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3045 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3046 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3047 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3048 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3049 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3050 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3051 | /* XXX sshpkt_getb? */ |
| 3052 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3053 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3054 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3055 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3056 | } |
| 3057 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3058 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3059 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3060 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3061 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3062 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3063 | ssh_packet_check_eom(ssh); |
| 3064 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3065 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3066 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3067 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3068 | |
| 3069 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3070 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3071 | debug("channel %d: FORCE input drain", c->self); |
| 3072 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3073 | if (sshbuf_len(c->input) == 0) |
| 3074 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3075 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3076 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3077 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3078 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3079 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3080 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3081 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3082 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3083 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3084 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3085 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3086 | ssh_packet_check_eom(ssh); |
| 3087 | chan_rcvd_oclose(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3088 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3089 | } |
| 3090 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3091 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3092 | 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] | 3093 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3094 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3095 | u_int32_t remote_window, remote_maxpacket; |
| 3096 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3097 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3098 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3099 | return 0; |
| 3100 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3101 | packet_disconnect("Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3102 | "non-opening channel %d.", c->self); |
| 3103 | /* |
| 3104 | * Record the remote channel number and mark that the channel |
| 3105 | * is now open. |
| 3106 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3107 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3108 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3109 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) { |
| 3110 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
| 3111 | packet_disconnect("Invalid open confirmation message"); |
| 3112 | } |
| 3113 | ssh_packet_check_eom(ssh); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3114 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3115 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3116 | c->remote_window = remote_window; |
| 3117 | c->remote_maxpacket = remote_maxpacket; |
| 3118 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3119 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3120 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3121 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3122 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3123 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3124 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3125 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3126 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3127 | } |
| 3128 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3129 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3130 | reason2txt(int reason) |
| 3131 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3132 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3133 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3134 | return "administratively prohibited"; |
| 3135 | case SSH2_OPEN_CONNECT_FAILED: |
| 3136 | return "connect failed"; |
| 3137 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3138 | return "unknown channel type"; |
| 3139 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3140 | return "resource shortage"; |
| 3141 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3142 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3145 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3146 | 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] | 3147 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3148 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3149 | u_int32_t reason; |
| 3150 | char *msg = NULL; |
| 3151 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3152 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3153 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3154 | return 0; |
| 3155 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3156 | packet_disconnect("Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3157 | "non-opening channel %d.", c->self); |
| 3158 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3159 | error("%s: reason: %s", __func__, ssh_err(r)); |
| 3160 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3161 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3162 | /* skip language */ |
| 3163 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
| 3164 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) { |
| 3165 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
| 3166 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3167 | } |
| 3168 | ssh_packet_check_eom(ssh); |
| 3169 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3170 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3171 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3172 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3173 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3174 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3175 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3176 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3177 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3178 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3179 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3180 | } |
| 3181 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3182 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3183 | 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] | 3184 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3185 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3186 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3187 | u_int32_t adjust; |
| 3188 | u_int new_rwin; |
| 3189 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3190 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3191 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3192 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3193 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3194 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3195 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3196 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3197 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3198 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) { |
| 3199 | error("%s: adjust: %s", __func__, ssh_err(r)); |
| 3200 | packet_disconnect("Invalid window adjust message"); |
| 3201 | } |
| 3202 | ssh_packet_check_eom(ssh); |
| 3203 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3204 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3205 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3206 | c->self, adjust, c->remote_window); |
| 3207 | } |
| 3208 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3209 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3210 | } |
| 3211 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3212 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3213 | 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] | 3214 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3215 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3216 | Channel *c; |
| 3217 | struct channel_confirm *cc; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3218 | |
| 3219 | /* Reset keepalive timeout */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 3220 | packet_set_alive_timeouts(0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3221 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3222 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3223 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3224 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3225 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3226 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3227 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3228 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3229 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3230 | ssh_packet_check_eom(ssh); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3231 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3232 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3233 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3234 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3235 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3236 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3237 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3238 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3239 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3240 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3241 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3242 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3243 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3244 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3245 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3248 | |
| 3249 | /* |
| 3250 | * Determine whether or not a port forward listens to loopback, the |
| 3251 | * specified address or wildcard. On the client, a specified bind |
| 3252 | * address will always override gateway_ports. On the server, a |
| 3253 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3254 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3255 | * will bind to whatever address the client asked for. |
| 3256 | * |
| 3257 | * Special-case listen_addrs are: |
| 3258 | * |
| 3259 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3260 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3261 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3262 | * "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] | 3263 | */ |
| 3264 | static const char * |
| 3265 | channel_fwd_bind_addr(const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3266 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3267 | { |
| 3268 | const char *addr = NULL; |
| 3269 | int wildcard = 0; |
| 3270 | |
| 3271 | if (listen_addr == NULL) { |
| 3272 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3273 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3274 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3275 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3276 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3277 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3278 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3279 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3280 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3281 | /* |
| 3282 | * Notify client if they requested a specific listen |
| 3283 | * address and it was overridden. |
| 3284 | */ |
| 3285 | if (*listen_addr != '\0' && |
| 3286 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3287 | strcmp(listen_addr, "*") != 0) { |
| 3288 | packet_send_debug("Forwarding listen address " |
| 3289 | "\"%s\" overridden by server " |
| 3290 | "GatewayPorts", listen_addr); |
| 3291 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3292 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3293 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3294 | strcmp(listen_addr, "::1") == 0) { |
| 3295 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3296 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3297 | } |
| 3298 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3299 | strcmp(listen_addr, "::1") == 0) { |
| 3300 | /* |
| 3301 | * If a specific IPv4/IPv6 localhost address has been |
| 3302 | * requested then accept it even if gateway_ports is in |
| 3303 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3304 | */ |
| 3305 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3306 | } |
| 3307 | if (wildcardp != NULL) |
| 3308 | *wildcardp = wildcard; |
| 3309 | return addr; |
| 3310 | } |
| 3311 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3312 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3313 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3314 | struct Forward *fwd, int *allocated_listen_port, |
| 3315 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3316 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3317 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3318 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3319 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3320 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3321 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3322 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3323 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3324 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3325 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3326 | if (is_client && fwd->connect_path != NULL) { |
| 3327 | host = fwd->connect_path; |
| 3328 | } else { |
| 3329 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3330 | fwd->listen_host : fwd->connect_host; |
| 3331 | if (host == NULL) { |
| 3332 | error("No forward host name."); |
| 3333 | return 0; |
| 3334 | } |
| 3335 | if (strlen(host) >= NI_MAXHOST) { |
| 3336 | error("Forward host name too long."); |
| 3337 | return 0; |
| 3338 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3339 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3340 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3341 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3342 | addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard, |
| 3343 | is_client, fwd_opts); |
| 3344 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3345 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3346 | |
| 3347 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3348 | * getaddrinfo returns a loopback address if the hostname is |
| 3349 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3350 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3351 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3352 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3353 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3354 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3355 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3356 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3357 | if (addr == NULL) { |
| 3358 | /* This really shouldn't happen */ |
| 3359 | packet_disconnect("getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3360 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3361 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3362 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3363 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3364 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3365 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3366 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3367 | if (allocated_listen_port != NULL) |
| 3368 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3369 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3370 | switch (ai->ai_family) { |
| 3371 | case AF_INET: |
| 3372 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3373 | sin_port; |
| 3374 | break; |
| 3375 | case AF_INET6: |
| 3376 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3377 | sin6_port; |
| 3378 | break; |
| 3379 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3380 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3381 | } |
| 3382 | /* |
| 3383 | * If allocating a port for -R forwards, then use the |
| 3384 | * same port for all address families. |
| 3385 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3386 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3387 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3388 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3389 | *lport_p = htons(*allocated_listen_port); |
| 3390 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3391 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3392 | strport, sizeof(strport), |
| 3393 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3394 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3395 | continue; |
| 3396 | } |
| 3397 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3398 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3399 | if (sock < 0) { |
| 3400 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3401 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3402 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3403 | continue; |
| 3404 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3405 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3406 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3407 | if (ai->ai_family == AF_INET6) |
| 3408 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3409 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3410 | debug("Local forwarding listening on %s port %s.", |
| 3411 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3412 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3413 | /* Bind the socket to the address. */ |
| 3414 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3415 | /* |
| 3416 | * address can be in if use ipv6 address is |
| 3417 | * already bound |
| 3418 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3419 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3420 | error("bind [%s]:%s: %.100s", |
| 3421 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3422 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3423 | verbose("bind [%s]:%s: %.100s", |
| 3424 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3425 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3426 | close(sock); |
| 3427 | continue; |
| 3428 | } |
| 3429 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3430 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3431 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3432 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3433 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3434 | close(sock); |
| 3435 | continue; |
| 3436 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3437 | |
| 3438 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3439 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3440 | * record what we got. |
| 3441 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3442 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3443 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3444 | allocated_listen_port != NULL && |
| 3445 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3446 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3447 | debug("Allocated listen port %d", |
| 3448 | *allocated_listen_port); |
| 3449 | } |
| 3450 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3451 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3452 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3453 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3454 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3455 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3456 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3457 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3458 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3459 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3460 | c->listening_port = *allocated_listen_port; |
| 3461 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3462 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3463 | success = 1; |
| 3464 | } |
| 3465 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3466 | error("%s: cannot listen to port: %d", __func__, |
| 3467 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3468 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3469 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3470 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3471 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3472 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3473 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3474 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3475 | { |
| 3476 | struct sockaddr_un sunaddr; |
| 3477 | const char *path; |
| 3478 | Channel *c; |
| 3479 | int port, sock; |
| 3480 | mode_t omask; |
| 3481 | |
| 3482 | switch (type) { |
| 3483 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3484 | if (fwd->connect_path != NULL) { |
| 3485 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3486 | error("Local connecting path too long: %s", |
| 3487 | fwd->connect_path); |
| 3488 | return 0; |
| 3489 | } |
| 3490 | path = fwd->connect_path; |
| 3491 | port = PORT_STREAMLOCAL; |
| 3492 | } else { |
| 3493 | if (fwd->connect_host == NULL) { |
| 3494 | error("No forward host name."); |
| 3495 | return 0; |
| 3496 | } |
| 3497 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3498 | error("Forward host name too long."); |
| 3499 | return 0; |
| 3500 | } |
| 3501 | path = fwd->connect_host; |
| 3502 | port = fwd->connect_port; |
| 3503 | } |
| 3504 | break; |
| 3505 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3506 | path = fwd->listen_path; |
| 3507 | port = PORT_STREAMLOCAL; |
| 3508 | break; |
| 3509 | default: |
| 3510 | error("%s: unexpected channel type %d", __func__, type); |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | if (fwd->listen_path == NULL) { |
| 3515 | error("No forward path name."); |
| 3516 | return 0; |
| 3517 | } |
| 3518 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3519 | error("Local listening path too long: %s", fwd->listen_path); |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
| 3523 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3524 | |
| 3525 | /* Start a Unix domain listener. */ |
| 3526 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3527 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3528 | fwd_opts->streamlocal_bind_unlink); |
| 3529 | umask(omask); |
| 3530 | if (sock < 0) |
| 3531 | return 0; |
| 3532 | |
| 3533 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3534 | |
| 3535 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3536 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3537 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3538 | 0, "unix listener", 1); |
| 3539 | c->path = xstrdup(path); |
| 3540 | c->host_port = port; |
| 3541 | c->listening_port = PORT_STREAMLOCAL; |
| 3542 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3543 | return 1; |
| 3544 | } |
| 3545 | |
| 3546 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3547 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3548 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3549 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3550 | u_int i; |
| 3551 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3552 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3553 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3554 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3555 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3556 | continue; |
| 3557 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3558 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3559 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3560 | found = 1; |
| 3561 | } |
| 3562 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3563 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3564 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3565 | } |
| 3566 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3567 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3568 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3569 | { |
| 3570 | u_int i; |
| 3571 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3572 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3573 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3574 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3575 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3576 | continue; |
| 3577 | if (c->path == NULL) |
| 3578 | continue; |
| 3579 | if (strcmp(c->path, path) == 0) { |
| 3580 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3581 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3582 | found = 1; |
| 3583 | } |
| 3584 | } |
| 3585 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3586 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3590 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3591 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3592 | if (fwd->listen_path != NULL) { |
| 3593 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3594 | fwd->listen_path); |
| 3595 | } else { |
| 3596 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3597 | fwd->listen_host, fwd->listen_port); |
| 3598 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3602 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3603 | const char *lhost, u_short lport, int cport, |
| 3604 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3605 | { |
| 3606 | u_int i; |
| 3607 | int found = 0; |
| 3608 | const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3609 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3610 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3611 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3612 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3613 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3614 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3615 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3616 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3617 | /* skip dynamic forwardings */ |
| 3618 | if (c->host_port == 0) |
| 3619 | continue; |
| 3620 | } else { |
| 3621 | if (c->host_port != cport) |
| 3622 | continue; |
| 3623 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3624 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3625 | (c->listening_addr != NULL && addr == NULL)) |
| 3626 | continue; |
| 3627 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3628 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3629 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3630 | found = 1; |
| 3631 | } |
| 3632 | } |
| 3633 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3634 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3635 | } |
| 3636 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3637 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3638 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3639 | { |
| 3640 | u_int i; |
| 3641 | int found = 0; |
| 3642 | |
| 3643 | if (path == NULL) { |
| 3644 | error("%s: no path specified.", __func__); |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3648 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3649 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3650 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3651 | continue; |
| 3652 | if (c->listening_addr == NULL) |
| 3653 | continue; |
| 3654 | if (strcmp(c->listening_addr, path) == 0) { |
| 3655 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3656 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3657 | found = 1; |
| 3658 | } |
| 3659 | } |
| 3660 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3661 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3665 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3666 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3667 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3668 | if (fwd->listen_path != NULL) { |
| 3669 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3670 | fwd->listen_path); |
| 3671 | } else { |
| 3672 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3673 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3674 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3675 | } |
| 3676 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3677 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3678 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3679 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3680 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3681 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3682 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3683 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3684 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3685 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3686 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3687 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3688 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3689 | } |
| 3690 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3691 | /* Matches a remote forwarding permission against a requested forwarding */ |
| 3692 | static int |
| 3693 | remote_open_match(struct permission *allowed_open, struct Forward *fwd) |
| 3694 | { |
| 3695 | int ret; |
| 3696 | char *lhost; |
| 3697 | |
| 3698 | /* XXX add ACLs for streamlocal */ |
| 3699 | if (fwd->listen_path != NULL) |
| 3700 | return 1; |
| 3701 | |
| 3702 | if (fwd->listen_host == NULL || allowed_open->listen_host == NULL) |
| 3703 | return 0; |
| 3704 | |
| 3705 | if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT && |
| 3706 | allowed_open->listen_port != fwd->listen_port) |
| 3707 | return 0; |
| 3708 | |
| 3709 | /* Match hostnames case-insensitively */ |
| 3710 | lhost = xstrdup(fwd->listen_host); |
| 3711 | lowercase(lhost); |
| 3712 | ret = match_pattern(lhost, allowed_open->listen_host); |
| 3713 | free(lhost); |
| 3714 | |
| 3715 | return ret; |
| 3716 | } |
| 3717 | |
| 3718 | /* Checks whether a requested remote forwarding is permitted */ |
| 3719 | static int |
| 3720 | check_rfwd_permission(struct ssh *ssh, struct Forward *fwd) |
| 3721 | { |
| 3722 | struct ssh_channels *sc = ssh->chanctxt; |
| 3723 | struct permission_set *pset = &sc->remote_perms; |
| 3724 | u_int i, permit, permit_adm = 1; |
| 3725 | struct permission *perm; |
| 3726 | |
| 3727 | /* XXX apply GatewayPorts override before checking? */ |
| 3728 | |
| 3729 | permit = pset->all_permitted; |
| 3730 | if (!permit) { |
| 3731 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3732 | perm = &pset->permitted_user[i]; |
| 3733 | if (remote_open_match(perm, fwd)) { |
| 3734 | permit = 1; |
| 3735 | break; |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | if (pset->num_permitted_admin > 0) { |
| 3741 | permit_adm = 0; |
| 3742 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 3743 | perm = &pset->permitted_admin[i]; |
| 3744 | if (remote_open_match(perm, fwd)) { |
| 3745 | permit_adm = 1; |
| 3746 | break; |
| 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | return permit && permit_adm; |
| 3752 | } |
| 3753 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3754 | /* protocol v2 remote port fwd, used by sshd */ |
| 3755 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3756 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3757 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3758 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3759 | if (!check_rfwd_permission(ssh, fwd)) { |
| 3760 | packet_send_debug("port forwarding refused"); |
| 3761 | return 0; |
| 3762 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3763 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3764 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3765 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3766 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3767 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3768 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3769 | fwd_opts); |
| 3770 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3771 | } |
| 3772 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3773 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3774 | * Translate the requested rfwd listen host to something usable for |
| 3775 | * this server. |
| 3776 | */ |
| 3777 | static const char * |
| 3778 | channel_rfwd_bind_host(const char *listen_host) |
| 3779 | { |
| 3780 | if (listen_host == NULL) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3781 | return "localhost"; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3782 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3783 | return ""; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3784 | } else |
| 3785 | return listen_host; |
| 3786 | } |
| 3787 | |
| 3788 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3789 | * Initiate forwarding of connections to port "port" on remote host through |
| 3790 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3791 | * Returns handle (index) for updating the dynamic listen port with |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3792 | * channel_update_permission(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3793 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3794 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3795 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3796 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3797 | int r, success = 0, idx = -1; |
| 3798 | char *host_to_connect, *listen_host, *listen_path; |
| 3799 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3800 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3801 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3802 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3803 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3804 | (r = sshpkt_put_cstring(ssh, |
| 3805 | "streamlocal-forward@openssh.com")) != 0 || |
| 3806 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3807 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3808 | (r = sshpkt_send(ssh)) != 0 || |
| 3809 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3810 | fatal("%s: request streamlocal: %s", |
| 3811 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3812 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3813 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3814 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3815 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3816 | (r = sshpkt_put_cstring(ssh, |
| 3817 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3818 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3819 | (r = sshpkt_send(ssh)) != 0 || |
| 3820 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3821 | fatal("%s: request tcpip-forward: %s", |
| 3822 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3823 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3824 | /* Assume that server accepts the request */ |
| 3825 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3826 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3827 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3828 | host_to_connect = listen_host = listen_path = NULL; |
| 3829 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3830 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3831 | host_to_connect = xstrdup(fwd->connect_path); |
| 3832 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3833 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3834 | host_to_connect = xstrdup(fwd->connect_host); |
| 3835 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3836 | } |
| 3837 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3838 | listen_path = xstrdup(fwd->listen_path); |
| 3839 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3840 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3841 | if (fwd->listen_host != NULL) |
| 3842 | listen_host = xstrdup(fwd->listen_host); |
| 3843 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3844 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3845 | idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3846 | host_to_connect, port_to_connect, |
| 3847 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3848 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3849 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3850 | } |
| 3851 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3852 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3853 | open_match(struct permission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3854 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3855 | { |
| 3856 | if (allowed_open->host_to_connect == NULL) |
| 3857 | return 0; |
| 3858 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3859 | allowed_open->port_to_connect != requestedport) |
| 3860 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3861 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3862 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3863 | return 0; |
| 3864 | return 1; |
| 3865 | } |
| 3866 | |
| 3867 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3868 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3869 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3870 | * need to translate between the configured-host (listen_host) |
| 3871 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3872 | */ |
| 3873 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3874 | open_listen_match_tcpip(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3875 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3876 | { |
| 3877 | const char *allowed_host; |
| 3878 | |
| 3879 | if (allowed_open->host_to_connect == NULL) |
| 3880 | return 0; |
| 3881 | if (allowed_open->listen_port != requestedport) |
| 3882 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3883 | if (!translate && allowed_open->listen_host == NULL && |
| 3884 | requestedhost == NULL) |
| 3885 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3886 | allowed_host = translate ? |
| 3887 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3888 | allowed_open->listen_host; |
| 3889 | if (allowed_host == NULL || |
| 3890 | strcmp(allowed_host, requestedhost) != 0) |
| 3891 | return 0; |
| 3892 | return 1; |
| 3893 | } |
| 3894 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3895 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3896 | open_listen_match_streamlocal(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3897 | const char *requestedpath) |
| 3898 | { |
| 3899 | if (allowed_open->host_to_connect == NULL) |
| 3900 | return 0; |
| 3901 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3902 | return 0; |
| 3903 | if (allowed_open->listen_path == NULL || |
| 3904 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3905 | return 0; |
| 3906 | return 1; |
| 3907 | } |
| 3908 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3909 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3910 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3911 | * local side. |
| 3912 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3913 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3914 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3915 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3916 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3917 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3918 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3919 | int r; |
| 3920 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3921 | struct permission *perm; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3922 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3923 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3924 | perm = &pset->permitted_user[i]; |
| 3925 | if (open_listen_match_tcpip(perm, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3926 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3927 | perm = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3928 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3929 | if (perm == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3930 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3931 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3932 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3933 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3934 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 3935 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3936 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 3937 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 3938 | (r = sshpkt_send(ssh)) != 0) |
| 3939 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3940 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3941 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3942 | |
| 3943 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3947 | * Request cancellation of remote forwarding of Unix domain socket |
| 3948 | * path from local side. |
| 3949 | */ |
| 3950 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3951 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3952 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3953 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3954 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3955 | int r; |
| 3956 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3957 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3958 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3959 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3960 | perm = &pset->permitted_user[i]; |
| 3961 | if (open_listen_match_streamlocal(perm, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3962 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3963 | perm = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3964 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3965 | if (perm == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3966 | debug("%s: requested forward not found", __func__); |
| 3967 | return -1; |
| 3968 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3969 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3970 | (r = sshpkt_put_cstring(ssh, |
| 3971 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 3972 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3973 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 3974 | (r = sshpkt_send(ssh)) != 0) |
| 3975 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3976 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3977 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3978 | |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
| 3982 | /* |
| 3983 | * Request cancellation of remote forwarding of a connection from local side. |
| 3984 | */ |
| 3985 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3986 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3987 | { |
| 3988 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3989 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 3990 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3991 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3992 | return channel_request_rforward_cancel_tcpip(ssh, |
| 3993 | fwd->listen_host, |
| 3994 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | /* |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3999 | * 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] | 4000 | * usually called by the server, because the user could connect to any port |
| 4001 | * anyway, and the server has no way to know but to trust the client anyway. |
| 4002 | */ |
| 4003 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4004 | channel_permit_all(struct ssh *ssh, int where) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4005 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4006 | struct permission_set *pset = permission_set_get(ssh, where); |
| 4007 | |
| 4008 | if (pset->num_permitted_user == 0) |
| 4009 | pset->all_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4012 | /* |
| 4013 | * Permit the specified host/port for forwarding. |
| 4014 | */ |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 4015 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4016 | channel_add_permission(struct ssh *ssh, int who, int where, |
| 4017 | char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4018 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4019 | int local = where == FORWARD_LOCAL; |
| 4020 | struct permission_set *pset = permission_set_get(ssh, where); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4021 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4022 | debug("allow %s forwarding to host %s port %d", |
| 4023 | fwd_ident(who, where), host, port); |
| 4024 | /* |
| 4025 | * Remote forwards set listen_host/port, local forwards set |
| 4026 | * host/port_to_connect. |
| 4027 | */ |
| 4028 | permission_set_add(ssh, who, where, |
| 4029 | local ? host : 0, local ? port : 0, |
| 4030 | local ? NULL : host, NULL, local ? 0 : port, NULL); |
| 4031 | pset->all_permitted = 0; |
| 4032 | } |
| 4033 | |
| 4034 | /* |
| 4035 | * Administratively disable forwarding. |
| 4036 | */ |
| 4037 | void |
| 4038 | channel_disable_admin(struct ssh *ssh, int where) |
| 4039 | { |
| 4040 | channel_clear_permission(ssh, FORWARD_ADM, where); |
| 4041 | permission_set_add(ssh, FORWARD_ADM, where, |
| 4042 | NULL, 0, NULL, NULL, 0, NULL); |
| 4043 | } |
| 4044 | |
| 4045 | /* |
| 4046 | * Clear a list of permitted opens. |
| 4047 | */ |
| 4048 | void |
| 4049 | channel_clear_permission(struct ssh *ssh, int who, int where) |
| 4050 | { |
| 4051 | struct permission **permp; |
| 4052 | u_int *npermp; |
| 4053 | |
| 4054 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
| 4055 | *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp)); |
| 4056 | *npermp = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4059 | /* |
| 4060 | * Update the listen port for a dynamic remote forward, after |
| 4061 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 4062 | * passed then they entry will be invalidated. |
| 4063 | */ |
| 4064 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4065 | channel_update_permission(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4066 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4067 | struct permission_set *pset = &ssh->chanctxt->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4068 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4069 | if (idx < 0 || (u_int)idx >= pset->num_permitted_user) { |
| 4070 | debug("%s: index out of range: %d num_permitted_user %d", |
| 4071 | __func__, idx, pset->num_permitted_user); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4072 | return; |
| 4073 | } |
| 4074 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 4075 | newport > 0 ? "Updating" : "Removing", |
| 4076 | newport, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4077 | pset->permitted_user[idx].host_to_connect, |
| 4078 | pset->permitted_user[idx].port_to_connect); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4079 | if (newport <= 0) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4080 | fwd_perm_clear(&pset->permitted_user[idx]); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4081 | else { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4082 | pset->permitted_user[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4083 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 4087 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 4088 | int |
| 4089 | permitopen_port(const char *p) |
| 4090 | { |
| 4091 | int port; |
| 4092 | |
| 4093 | if (strcmp(p, "*") == 0) |
| 4094 | return FWD_PERMIT_ANY_PORT; |
| 4095 | if ((port = a2port(p)) > 0) |
| 4096 | return port; |
| 4097 | return -1; |
| 4098 | } |
| 4099 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4100 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4101 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4102 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4103 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4104 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4105 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4106 | char ntop[NI_MAXHOST]; |
| 4107 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4108 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4109 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4110 | switch (cctx->ai->ai_family) { |
| 4111 | case AF_UNIX: |
| 4112 | /* unix:pathname instead of host:port */ |
| 4113 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 4114 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 4115 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 4116 | break; |
| 4117 | case AF_INET: |
| 4118 | case AF_INET6: |
| 4119 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 4120 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 4121 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 4122 | error("connect_next: getnameinfo failed"); |
| 4123 | continue; |
| 4124 | } |
| 4125 | break; |
| 4126 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4127 | continue; |
| 4128 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4129 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4130 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4131 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4132 | error("socket: %.100s", strerror(errno)); |
| 4133 | else |
| 4134 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4135 | continue; |
| 4136 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4137 | if (set_nonblock(sock) == -1) |
| 4138 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4139 | if (connect(sock, cctx->ai->ai_addr, |
| 4140 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4141 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4142 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4143 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4144 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4145 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4146 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4147 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4148 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4149 | if (cctx->ai->ai_family != AF_UNIX) |
| 4150 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4151 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4152 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4153 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4154 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4155 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4156 | return -1; |
| 4157 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4158 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4159 | static void |
| 4160 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4161 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4162 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4163 | if (cctx->aitop) { |
| 4164 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4165 | free(cctx->aitop); |
| 4166 | else |
| 4167 | freeaddrinfo(cctx->aitop); |
| 4168 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4169 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4170 | } |
| 4171 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4172 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4173 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4174 | * passing back the failure reason if appropriate. |
| 4175 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4176 | static int |
| 4177 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4178 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4179 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4180 | { |
| 4181 | struct addrinfo hints; |
| 4182 | int gaierr; |
| 4183 | int sock = -1; |
| 4184 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4185 | |
| 4186 | if (port == PORT_STREAMLOCAL) { |
| 4187 | struct sockaddr_un *sunaddr; |
| 4188 | struct addrinfo *ai; |
| 4189 | |
| 4190 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4191 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4192 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | /* |
| 4196 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4197 | * channel_connect_ctx_free() must check ai_family |
| 4198 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4199 | */ |
| 4200 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4201 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4202 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4203 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4204 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4205 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4206 | ai->ai_protocol = PF_UNSPEC; |
| 4207 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4208 | sunaddr->sun_family = AF_UNIX; |
| 4209 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4210 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4211 | } else { |
| 4212 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4213 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4214 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4215 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4216 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4217 | != 0) { |
| 4218 | if (errmsg != NULL) |
| 4219 | *errmsg = ssh_gai_strerror(gaierr); |
| 4220 | if (reason != NULL) |
| 4221 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4222 | error("connect_to %.100s: unknown host (%s)", name, |
| 4223 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4224 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4225 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4226 | } |
| 4227 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4228 | cctx->host = xstrdup(name); |
| 4229 | cctx->port = port; |
| 4230 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4231 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4232 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4233 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4234 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4235 | return -1; |
| 4236 | } |
| 4237 | |
| 4238 | return sock; |
| 4239 | } |
| 4240 | |
| 4241 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4242 | static Channel * |
| 4243 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4244 | char *ctype, char *rname) |
| 4245 | { |
| 4246 | struct channel_connect cctx; |
| 4247 | Channel *c; |
| 4248 | int sock; |
| 4249 | |
| 4250 | memset(&cctx, 0, sizeof(cctx)); |
| 4251 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4252 | &cctx, NULL, NULL); |
| 4253 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4254 | channel_connect_ctx_free(&cctx); |
| 4255 | return NULL; |
| 4256 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4257 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4258 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4259 | c->host_port = port; |
| 4260 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4261 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4262 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4263 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4266 | /* |
| 4267 | * returns either the newly connected channel or the downstream channel |
| 4268 | * that needs to deal with this connection. |
| 4269 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4270 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4271 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4272 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4273 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4274 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4275 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4276 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4277 | struct permission *perm; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4278 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4279 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4280 | perm = &pset->permitted_user[i]; |
| 4281 | if (open_listen_match_tcpip(perm, |
| 4282 | listen_host, listen_port, 1)) { |
| 4283 | if (perm->downstream) |
| 4284 | return perm->downstream; |
| 4285 | if (perm->port_to_connect == 0) |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4286 | return rdynamic_connect_prepare(ssh, |
| 4287 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4288 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4289 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4290 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4294 | listen_port); |
| 4295 | return NULL; |
| 4296 | } |
| 4297 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4298 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4299 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4300 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4301 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4302 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4303 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4304 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4305 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4306 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4307 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4308 | perm = &pset->permitted_user[i]; |
| 4309 | if (open_listen_match_streamlocal(perm, path)) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4310 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4311 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4312 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4316 | path); |
| 4317 | return NULL; |
| 4318 | } |
| 4319 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4320 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4321 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4322 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4323 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4324 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4325 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4326 | struct permission_set *pset = &sc->local_perms; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4327 | struct channel_connect cctx; |
| 4328 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4329 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4330 | int sock; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4331 | struct permission *perm; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4332 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4333 | permit = pset->all_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4334 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4335 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4336 | perm = &pset->permitted_user[i]; |
| 4337 | if (open_match(perm, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4338 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4339 | break; |
| 4340 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4341 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4342 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4343 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4344 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4345 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4346 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4347 | perm = &pset->permitted_admin[i]; |
| 4348 | if (open_match(perm, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4349 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4350 | break; |
| 4351 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4352 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 4356 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4357 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4358 | if (reason != NULL) |
| 4359 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4360 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4361 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4362 | |
| 4363 | memset(&cctx, 0, sizeof(cctx)); |
| 4364 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4365 | &cctx, reason, errmsg); |
| 4366 | if (sock == -1) { |
| 4367 | channel_connect_ctx_free(&cctx); |
| 4368 | return NULL; |
| 4369 | } |
| 4370 | |
| 4371 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4372 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4373 | c->host_port = port; |
| 4374 | c->path = xstrdup(host); |
| 4375 | c->connect_ctx = cctx; |
| 4376 | |
| 4377 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4380 | /* Check if connecting to that path is permitted and connect. */ |
| 4381 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4382 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4383 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4384 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4385 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4386 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4387 | u_int i, permit, permit_adm = 1; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4388 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4389 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4390 | permit = pset->all_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4391 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4392 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4393 | perm = &pset->permitted_user[i]; |
| 4394 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4395 | permit = 1; |
| 4396 | break; |
| 4397 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4398 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4399 | } |
| 4400 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4401 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4402 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4403 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4404 | perm = &pset->permitted_admin[i]; |
| 4405 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4406 | permit_adm = 1; |
| 4407 | break; |
| 4408 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4409 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | if (!permit || !permit_adm) { |
| 4413 | logit("Received request to connect to path %.100s, " |
| 4414 | "but the request was denied.", path); |
| 4415 | return NULL; |
| 4416 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4417 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4418 | } |
| 4419 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4420 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4421 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4422 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4423 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4424 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4425 | int r; |
| 4426 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4427 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4428 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4429 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4430 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4431 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4432 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4433 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4434 | channel_request_start(ssh, i, "window-change", 0); |
| 4435 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4436 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4437 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4438 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4439 | (r = sshpkt_send(ssh)) != 0) |
| 4440 | fatal("%s: channel %u: send window-change: %s", |
| 4441 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4445 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4446 | static Channel * |
| 4447 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4448 | { |
| 4449 | Channel *c; |
| 4450 | int r; |
| 4451 | |
| 4452 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4453 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4454 | c->host_port = 0; |
| 4455 | c->path = NULL; |
| 4456 | |
| 4457 | /* |
| 4458 | * We need to open the channel before we have a FD, |
| 4459 | * so that we can get SOCKS header from peer. |
| 4460 | */ |
| 4461 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4462 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4463 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4464 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4465 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4466 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4467 | c->self, ssh_err(r)); |
| 4468 | } |
| 4469 | return c; |
| 4470 | } |
| 4471 | |
| 4472 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4473 | static int |
| 4474 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4475 | { |
| 4476 | struct channel_connect cctx; |
| 4477 | int sock; |
| 4478 | |
| 4479 | memset(&cctx, 0, sizeof(cctx)); |
| 4480 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4481 | NULL, &cctx, NULL, NULL); |
| 4482 | if (sock == -1) |
| 4483 | channel_connect_ctx_free(&cctx); |
| 4484 | else { |
| 4485 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4486 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4487 | c->connect_ctx = cctx; |
| 4488 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4489 | } |
| 4490 | return sock; |
| 4491 | } |
| 4492 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4493 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4494 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4495 | /* |
| 4496 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4497 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4498 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4499 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4500 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4501 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4502 | int x11_use_localhost, int single_connection, |
| 4503 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4504 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4505 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4506 | int display_number, sock; |
| 4507 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4508 | struct addrinfo hints, *ai, *aitop; |
| 4509 | char strport[NI_MAXSERV]; |
| 4510 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4511 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4512 | if (chanids == NULL) |
| 4513 | return -1; |
| 4514 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4515 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4516 | display_number < MAX_DISPLAYS; |
| 4517 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4518 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4519 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4520 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4521 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4522 | hints.ai_socktype = SOCK_STREAM; |
| 4523 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4524 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4525 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4526 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4527 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4528 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4529 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4530 | if (ai->ai_family != AF_INET && |
| 4531 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4532 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4533 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4534 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4535 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4536 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4537 | #ifdef EPFNOSUPPORT |
| 4538 | && (errno != EPFNOSUPPORT) |
| 4539 | #endif |
| 4540 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4541 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4542 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4543 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4544 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4545 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4546 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4547 | continue; |
| 4548 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4549 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4550 | if (ai->ai_family == AF_INET6) |
| 4551 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4552 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4553 | set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4554 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4555 | debug2("%s: bind port %d: %.100s", __func__, |
| 4556 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4557 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4558 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4559 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4560 | num_socks = 0; |
| 4561 | break; |
| 4562 | } |
| 4563 | socks[num_socks++] = sock; |
| 4564 | if (num_socks == NUM_SOCKS) |
| 4565 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4566 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4567 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4568 | if (num_socks > 0) |
| 4569 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4570 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4571 | if (display_number >= MAX_DISPLAYS) { |
| 4572 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4573 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4574 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4575 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4576 | for (n = 0; n < num_socks; n++) { |
| 4577 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 4578 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4579 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4580 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4581 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4582 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4583 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4584 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4585 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4586 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4587 | for (n = 0; n < num_socks; n++) { |
| 4588 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4589 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4590 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4591 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4592 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4593 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4594 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4595 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4596 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4597 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4598 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4599 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4600 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4601 | } |
| 4602 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4603 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4604 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4605 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4606 | int sock; |
| 4607 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4608 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4609 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4610 | if (sock < 0) |
| 4611 | error("socket: %.100s", strerror(errno)); |
| 4612 | memset(&addr, 0, sizeof(addr)); |
| 4613 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4614 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4615 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4616 | return sock; |
| 4617 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4618 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4619 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4620 | } |
| 4621 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4622 | static int |
| 4623 | connect_local_xsocket(u_int dnr) |
| 4624 | { |
| 4625 | char buf[1024]; |
| 4626 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4627 | return connect_local_xsocket_path(buf); |
| 4628 | } |
| 4629 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4630 | #ifdef __APPLE__ |
| 4631 | static int |
| 4632 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4633 | { |
| 4634 | struct stat sbuf; |
| 4635 | |
| 4636 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4637 | error("%s: display path too long", __func__); |
| 4638 | return 0; |
| 4639 | } |
| 4640 | if (display[0] != '/') |
| 4641 | return 0; |
| 4642 | if (stat(path, &sbuf) == 0) { |
| 4643 | return 1; |
| 4644 | } else { |
| 4645 | char *dot = strrchr(path, '.'); |
| 4646 | if (dot != NULL) { |
| 4647 | *dot = '\0'; |
| 4648 | if (stat(path, &sbuf) == 0) { |
| 4649 | return 1; |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | return 0; |
| 4654 | } |
| 4655 | #endif |
| 4656 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4657 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4658 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4659 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4660 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4661 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4662 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4663 | struct addrinfo hints, *ai, *aitop; |
| 4664 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4665 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4666 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4667 | /* Try to open a socket for the local X server. */ |
| 4668 | display = getenv("DISPLAY"); |
| 4669 | if (!display) { |
| 4670 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4671 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4672 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4673 | /* |
| 4674 | * Now we decode the value of the DISPLAY variable and make a |
| 4675 | * connection to the real X server. |
| 4676 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4677 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4678 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4679 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4680 | { |
| 4681 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4682 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4683 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4684 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4685 | |
| 4686 | /* Create a socket. */ |
| 4687 | sock = connect_local_xsocket_path(path); |
| 4688 | if (sock < 0) |
| 4689 | return -1; |
| 4690 | |
| 4691 | /* OK, we now have a connection to the display. */ |
| 4692 | return sock; |
| 4693 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4694 | } |
| 4695 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4696 | /* |
| 4697 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4698 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4699 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4700 | if (strncmp(display, "unix:", 5) == 0 || |
| 4701 | display[0] == ':') { |
| 4702 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4703 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4704 | &display_number) != 1) { |
| 4705 | error("Could not parse display number from DISPLAY: " |
| 4706 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4707 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4708 | } |
| 4709 | /* Create a socket. */ |
| 4710 | sock = connect_local_xsocket(display_number); |
| 4711 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4712 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4713 | |
| 4714 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4715 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4716 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4717 | /* |
| 4718 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4719 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4720 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4721 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4722 | cp = strchr(buf, ':'); |
| 4723 | if (!cp) { |
| 4724 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4725 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4726 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4727 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4728 | /* |
| 4729 | * buf now contains the host name. But first we parse the |
| 4730 | * display number. |
| 4731 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4732 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4733 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4734 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4735 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4736 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4737 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4738 | /* Look up the host address */ |
| 4739 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4740 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4741 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4742 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4743 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4744 | error("%.100s: unknown host. (%s)", buf, |
| 4745 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4746 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4747 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4748 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4749 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4750 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4751 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4752 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4753 | continue; |
| 4754 | } |
| 4755 | /* Connect it to the display. */ |
| 4756 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4757 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4758 | 6000 + display_number, strerror(errno)); |
| 4759 | close(sock); |
| 4760 | continue; |
| 4761 | } |
| 4762 | /* Success */ |
| 4763 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4764 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4765 | freeaddrinfo(aitop); |
| 4766 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4767 | error("connect %.100s port %u: %.100s", buf, |
| 4768 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4769 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4770 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4771 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4772 | return sock; |
| 4773 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4774 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4775 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4776 | * Requests forwarding of X11 connections, generates fake authentication |
| 4777 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4778 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4779 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4780 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4781 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4782 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4783 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4784 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4785 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4786 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4787 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4788 | char *new_data; |
| 4789 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4790 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4791 | if (sc->x11_saved_display == NULL) |
| 4792 | sc->x11_saved_display = xstrdup(disp); |
| 4793 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4794 | error("x11_request_forwarding_with_spoofing: different " |
| 4795 | "$DISPLAY already forwarded"); |
| 4796 | return; |
| 4797 | } |
| 4798 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4799 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4800 | if (cp) |
| 4801 | cp = strchr(cp, '.'); |
| 4802 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4803 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4804 | else |
| 4805 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4806 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4807 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4808 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4809 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4810 | |
| 4811 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4812 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4813 | for (i = 0; i < data_len; i++) { |
| 4814 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4815 | fatal("x11_request_forwarding: bad " |
| 4816 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4817 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4818 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4819 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4820 | |
| 4821 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4822 | sc->x11_fake_data = xmalloc(data_len); |
| 4823 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4824 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4825 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4826 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4827 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4828 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4829 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4830 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4831 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4832 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4833 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4834 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4835 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4836 | (r = sshpkt_send(ssh)) != 0 || |
| 4837 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4838 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4839 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4840 | } |