dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.394 2019/07/07 01:05:00 dtucker 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 |
Damien Miller | cfc1897 | 2019-10-09 09:06:35 +1100 | [diff] [blame] | 62 | # include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 66 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 67 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 70 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh2.h" |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 74 | #include "ssherr.h" |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 75 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "canohost.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 82 | #include "sshkey.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 83 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 84 | #include "pathnames.h" |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 85 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 87 | /* -- agent forwarding */ |
| 88 | #define NUM_SOCKS 10 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 90 | /* -- tcp forwarding */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 91 | /* special-case port number meaning allow any port */ |
| 92 | #define FWD_PERMIT_ANY_PORT 0 |
| 93 | |
| 94 | /* special-case wildcard meaning allow any host */ |
| 95 | #define FWD_PERMIT_ANY_HOST "*" |
| 96 | |
| 97 | /* -- X11 forwarding */ |
| 98 | /* Maximum number of fake X11 displays to try. */ |
| 99 | #define MAX_DISPLAYS 1000 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 101 | /* Per-channel callback for pre/post select() actions */ |
| 102 | typedef void chan_fn(struct ssh *, Channel *c, |
| 103 | fd_set *readset, fd_set *writeset); |
| 104 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 105 | /* |
| 106 | * Data structure for storing which hosts are permitted for forward requests. |
| 107 | * The local sides of any remote forwards are stored in this array to prevent |
| 108 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 109 | * network (which might be behind a firewall). |
| 110 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 111 | /* XXX: streamlocal wants a path instead of host:port */ |
| 112 | /* Overload host_to_connect; we could just make this match Forward */ |
| 113 | /* XXX - can we use listen_host instead of listen_path? */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 114 | struct permission { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 115 | char *host_to_connect; /* Connect to 'host'. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 116 | int port_to_connect; /* Connect to 'port'. */ |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 117 | char *listen_host; /* Remote side should listen address. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 118 | char *listen_path; /* Remote side should listen path. */ |
| 119 | int listen_port; /* Remote side should listen port. */ |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 120 | Channel *downstream; /* Downstream mux*/ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 121 | }; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Stores the forwarding permission state for a single direction (local or |
| 125 | * remote). |
| 126 | */ |
| 127 | struct permission_set { |
| 128 | /* |
| 129 | * List of all local permitted host/port pairs to allow for the |
| 130 | * user. |
| 131 | */ |
| 132 | u_int num_permitted_user; |
| 133 | struct permission *permitted_user; |
| 134 | |
| 135 | /* |
| 136 | * List of all permitted host/port pairs to allow for the admin. |
| 137 | */ |
| 138 | u_int num_permitted_admin; |
| 139 | struct permission *permitted_admin; |
| 140 | |
| 141 | /* |
| 142 | * If this is true, all opens/listens are permitted. This is the |
| 143 | * case on the server on which we have to trust the client anyway, |
| 144 | * and the user could do anything after logging in. |
| 145 | */ |
| 146 | int all_permitted; |
| 147 | }; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 148 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 149 | /* Master structure for channels state */ |
| 150 | struct ssh_channels { |
| 151 | /* |
| 152 | * Pointer to an array containing all allocated channels. The array |
| 153 | * is dynamically extended as needed. |
| 154 | */ |
| 155 | Channel **channels; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 156 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Size of the channel array. All slots of the array must always be |
| 159 | * initialized (at least the type field); unused slots set to NULL |
| 160 | */ |
| 161 | u_int channels_alloc; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 162 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Maximum file descriptor value used in any of the channels. This is |
| 165 | * updated in channel_new. |
| 166 | */ |
| 167 | int channel_max_fd; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 168 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 169 | /* |
| 170 | * 'channel_pre*' are called just before select() to add any bits |
| 171 | * relevant to channels in the select bitmasks. |
| 172 | * |
| 173 | * 'channel_post*': perform any appropriate operations for |
| 174 | * channels which have events pending. |
| 175 | */ |
| 176 | chan_fn **channel_pre; |
| 177 | chan_fn **channel_post; |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 178 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 179 | /* -- tcp forwarding */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 180 | struct permission_set local_perms; |
| 181 | struct permission_set remote_perms; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 182 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 183 | /* -- X11 forwarding */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 184 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 185 | /* Saved X11 local (client) display. */ |
| 186 | char *x11_saved_display; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 187 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 188 | /* Saved X11 authentication protocol name. */ |
| 189 | char *x11_saved_proto; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 190 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 191 | /* Saved X11 authentication data. This is the real data. */ |
| 192 | char *x11_saved_data; |
| 193 | u_int x11_saved_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 194 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 195 | /* Deadline after which all X11 connections are refused */ |
| 196 | u_int x11_refuse_time; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 197 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Fake X11 authentication data. This is what the server will be |
| 200 | * sending us; we should replace any occurrences of this by the |
| 201 | * real data. |
| 202 | */ |
| 203 | u_char *x11_fake_data; |
| 204 | u_int x11_fake_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 205 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 206 | /* AF_UNSPEC or AF_INET or AF_INET6 */ |
| 207 | int IPv4or6; |
| 208 | }; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 209 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 210 | /* helper */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 211 | static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 212 | static const char *channel_rfwd_bind_host(const char *listen_host); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 213 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 214 | /* non-blocking connect helpers */ |
| 215 | static int connect_next(struct channel_connect *); |
| 216 | static void channel_connect_ctx_free(struct channel_connect *); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 217 | static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *); |
| 218 | static int rdynamic_connect_finish(struct ssh *, Channel *); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 219 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 220 | /* Setup helper */ |
| 221 | static void channel_handler_init(struct ssh_channels *sc); |
| 222 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 223 | /* -- channel core */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 224 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 225 | void |
| 226 | channel_init_channels(struct ssh *ssh) |
| 227 | { |
| 228 | struct ssh_channels *sc; |
| 229 | |
djm@openbsd.org | 285310b | 2018-12-07 02:31:20 +0000 | [diff] [blame] | 230 | if ((sc = calloc(1, sizeof(*sc))) == NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 231 | fatal("%s: allocation failed", __func__); |
| 232 | sc->channels_alloc = 10; |
| 233 | sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels)); |
| 234 | sc->IPv4or6 = AF_UNSPEC; |
| 235 | channel_handler_init(sc); |
| 236 | |
| 237 | ssh->chanctxt = sc; |
| 238 | } |
| 239 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 240 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 241 | channel_by_id(struct ssh *ssh, int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 242 | { |
| 243 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 244 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 245 | if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) { |
| 246 | logit("%s: %d: bad id", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 249 | c = ssh->chanctxt->channels[id]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 250 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 251 | logit("%s: %d: bad id: channel free", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | return c; |
| 255 | } |
| 256 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 257 | Channel * |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 258 | channel_by_remote_id(struct ssh *ssh, u_int remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 259 | { |
| 260 | Channel *c; |
| 261 | u_int i; |
| 262 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 263 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 264 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 265 | if (c != NULL && c->have_remote_id && c->remote_id == remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 266 | return c; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 271 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 272 | * Returns the channel if it is allowed to receive protocol messages. |
| 273 | * Private channels, like listening sockets, may not receive messages. |
| 274 | */ |
| 275 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 276 | channel_lookup(struct ssh *ssh, int id) |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 277 | { |
| 278 | Channel *c; |
| 279 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 280 | if ((c = channel_by_id(ssh, id)) == NULL) |
| 281 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 282 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 283 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 284 | case SSH_CHANNEL_X11_OPEN: |
| 285 | case SSH_CHANNEL_LARVAL: |
| 286 | case SSH_CHANNEL_CONNECTING: |
| 287 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 288 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 289 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 290 | case SSH_CHANNEL_OPENING: |
| 291 | case SSH_CHANNEL_OPEN: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 292 | case SSH_CHANNEL_ABANDONED: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 293 | case SSH_CHANNEL_MUX_PROXY: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 294 | return c; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 295 | } |
| 296 | logit("Non-public channel %d, type %d.", id, c->type); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 297 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 301 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 302 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 303 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 304 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 305 | channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd, |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 306 | int extusage, int nonblock, int is_tty) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 307 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 308 | struct ssh_channels *sc = ssh->chanctxt; |
| 309 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | /* Update the maximum file descriptor value. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 311 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd); |
| 312 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd); |
| 313 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 314 | |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 315 | if (rfd != -1) |
| 316 | fcntl(rfd, F_SETFD, FD_CLOEXEC); |
| 317 | if (wfd != -1 && wfd != rfd) |
| 318 | fcntl(wfd, F_SETFD, FD_CLOEXEC); |
| 319 | if (efd != -1 && efd != rfd && efd != wfd) |
| 320 | fcntl(efd, F_SETFD, FD_CLOEXEC); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 321 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 322 | c->rfd = rfd; |
| 323 | c->wfd = wfd; |
| 324 | c->sock = (rfd == wfd) ? rfd : -1; |
| 325 | c->efd = efd; |
| 326 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 327 | |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 328 | if ((c->isatty = is_tty) != 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 329 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 330 | #ifdef _AIX |
| 331 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 1a48aec | 2008-06-16 23:35:56 +1000 | [diff] [blame] | 332 | c->wfd_isatty = is_tty || isatty(c->wfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 333 | #endif |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 334 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 335 | /* enable nonblocking mode */ |
| 336 | if (nonblock) { |
| 337 | if (rfd != -1) |
| 338 | set_nonblock(rfd); |
| 339 | if (wfd != -1) |
| 340 | set_nonblock(wfd); |
| 341 | if (efd != -1) |
| 342 | set_nonblock(efd); |
| 343 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Allocate a new channel object and set its type and socket. This will cause |
| 348 | * remote_name to be freed. |
| 349 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 350 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 351 | channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd, |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 352 | u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 353 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 354 | struct ssh_channels *sc = ssh->chanctxt; |
| 355 | u_int i, found; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 356 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 357 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 358 | /* Try to find a free slot where to put the new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 359 | for (i = 0; i < sc->channels_alloc; i++) { |
| 360 | if (sc->channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | /* Found a free slot. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 362 | found = i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 363 | break; |
| 364 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 365 | } |
| 366 | if (i >= sc->channels_alloc) { |
| 367 | /* |
| 368 | * There are no free slots. Take last+1 slot and expand |
| 369 | * the array. |
| 370 | */ |
| 371 | found = sc->channels_alloc; |
| 372 | if (sc->channels_alloc > CHANNELS_MAX_CHANNELS) |
| 373 | fatal("%s: internal error: channels_alloc %d too big", |
| 374 | __func__, sc->channels_alloc); |
| 375 | sc->channels = xrecallocarray(sc->channels, sc->channels_alloc, |
| 376 | sc->channels_alloc + 10, sizeof(*sc->channels)); |
| 377 | sc->channels_alloc += 10; |
| 378 | debug2("channel: expanding %d", sc->channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 380 | /* Initialize and return new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 381 | c = sc->channels[found] = xcalloc(1, sizeof(Channel)); |
| 382 | if ((c->input = sshbuf_new()) == NULL || |
| 383 | (c->output = sshbuf_new()) == NULL || |
| 384 | (c->extended = sshbuf_new()) == NULL) |
| 385 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 386 | c->ostate = CHAN_OUTPUT_OPEN; |
| 387 | c->istate = CHAN_INPUT_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 388 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | c->self = found; |
| 390 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 391 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 392 | c->local_window = window; |
| 393 | c->local_window_max = window; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 394 | c->local_maxpacket = maxpack; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 395 | c->remote_name = xstrdup(remote_name); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 396 | c->ctl_chan = -1; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 397 | c->delayed = 1; /* prevent call to channel_post handler */ |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 398 | TAILQ_INIT(&c->status_confirms); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 400 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 401 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 402 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 403 | static void |
| 404 | channel_find_maxfd(struct ssh_channels *sc) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 405 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 406 | u_int i; |
| 407 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 408 | Channel *c; |
| 409 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 410 | for (i = 0; i < sc->channels_alloc; i++) { |
| 411 | c = sc->channels[i]; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 412 | if (c != NULL) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 413 | max = MAXIMUM(max, c->rfd); |
| 414 | max = MAXIMUM(max, c->wfd); |
| 415 | max = MAXIMUM(max, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 418 | sc->channel_max_fd = max; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 422 | channel_close_fd(struct ssh *ssh, int *fdp) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 423 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 424 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 425 | int ret = 0, fd = *fdp; |
| 426 | |
| 427 | if (fd != -1) { |
| 428 | ret = close(fd); |
| 429 | *fdp = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 430 | if (fd == sc->channel_max_fd) |
| 431 | channel_find_maxfd(sc); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 432 | } |
| 433 | return ret; |
| 434 | } |
| 435 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 436 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 437 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 438 | channel_close_fds(struct ssh *ssh, Channel *c) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 439 | { |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 440 | int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd; |
| 441 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 442 | channel_close_fd(ssh, &c->sock); |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 443 | if (rfd != sock) |
| 444 | channel_close_fd(ssh, &c->rfd); |
| 445 | if (wfd != sock && wfd != rfd) |
| 446 | channel_close_fd(ssh, &c->wfd); |
| 447 | if (efd != sock && efd != rfd && efd != wfd) |
| 448 | channel_close_fd(ssh, &c->efd); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 452 | fwd_perm_clear(struct permission *perm) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 453 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 454 | free(perm->host_to_connect); |
| 455 | free(perm->listen_host); |
| 456 | free(perm->listen_path); |
| 457 | bzero(perm, sizeof(*perm)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 458 | } |
| 459 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 460 | /* Returns an printable name for the specified forwarding permission list */ |
| 461 | static const char * |
| 462 | fwd_ident(int who, int where) |
| 463 | { |
| 464 | if (who == FORWARD_ADM) { |
| 465 | if (where == FORWARD_LOCAL) |
| 466 | return "admin local"; |
| 467 | else if (where == FORWARD_REMOTE) |
| 468 | return "admin remote"; |
| 469 | } else if (who == FORWARD_USER) { |
| 470 | if (where == FORWARD_LOCAL) |
| 471 | return "user local"; |
| 472 | else if (where == FORWARD_REMOTE) |
| 473 | return "user remote"; |
| 474 | } |
| 475 | fatal("Unknown forward permission list %d/%d", who, where); |
| 476 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 477 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 478 | /* Returns the forwarding permission list for the specified direction */ |
| 479 | static struct permission_set * |
| 480 | permission_set_get(struct ssh *ssh, int where) |
| 481 | { |
| 482 | struct ssh_channels *sc = ssh->chanctxt; |
| 483 | |
| 484 | switch (where) { |
| 485 | case FORWARD_LOCAL: |
| 486 | return &sc->local_perms; |
| 487 | break; |
| 488 | case FORWARD_REMOTE: |
| 489 | return &sc->remote_perms; |
| 490 | break; |
| 491 | default: |
| 492 | fatal("%s: invalid forwarding direction %d", __func__, where); |
| 493 | } |
| 494 | } |
| 495 | |
dtucker@openbsd.org | 4b7dd22 | 2019-06-07 14:18:48 +0000 | [diff] [blame] | 496 | /* Returns pointers to the specified forwarding list and its element count */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 497 | static void |
| 498 | permission_set_get_array(struct ssh *ssh, int who, int where, |
| 499 | struct permission ***permpp, u_int **npermpp) |
| 500 | { |
| 501 | struct permission_set *pset = permission_set_get(ssh, where); |
| 502 | |
| 503 | switch (who) { |
| 504 | case FORWARD_USER: |
| 505 | *permpp = &pset->permitted_user; |
| 506 | *npermpp = &pset->num_permitted_user; |
| 507 | break; |
| 508 | case FORWARD_ADM: |
| 509 | *permpp = &pset->permitted_admin; |
| 510 | *npermpp = &pset->num_permitted_admin; |
| 511 | break; |
| 512 | default: |
| 513 | fatal("%s: invalid forwarding client %d", __func__, who); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Adds an entry to the spcified forwarding list */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 518 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 519 | permission_set_add(struct ssh *ssh, int who, int where, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 520 | const char *host_to_connect, int port_to_connect, |
| 521 | const char *listen_host, const char *listen_path, int listen_port, |
| 522 | Channel *downstream) |
| 523 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 524 | struct permission **permp; |
| 525 | u_int n, *npermp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 526 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 527 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 528 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 529 | if (*npermp >= INT_MAX) |
| 530 | fatal("%s: %s overflow", __func__, fwd_ident(who, where)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 531 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 532 | *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp)); |
| 533 | n = (*npermp)++; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 534 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 535 | (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 536 | (*permp)[n].port_to_connect = port_to_connect; |
| 537 | (*permp)[n].listen_host = MAYBE_DUP(listen_host); |
| 538 | (*permp)[n].listen_path = MAYBE_DUP(listen_path); |
| 539 | (*permp)[n].listen_port = listen_port; |
| 540 | (*permp)[n].downstream = downstream; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 541 | #undef MAYBE_DUP |
| 542 | return (int)n; |
| 543 | } |
| 544 | |
| 545 | static void |
| 546 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 547 | { |
| 548 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 549 | struct permission_set *pset = &sc->local_perms; |
| 550 | struct permission *perm; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 551 | int r; |
| 552 | u_int i; |
| 553 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 554 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 555 | perm = &pset->permitted_user[i]; |
| 556 | if (perm->downstream != c) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 557 | continue; |
| 558 | |
| 559 | /* cancel on the server, since mux client is gone */ |
| 560 | debug("channel %d: cleanup remote forward for %s:%u", |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 561 | c->self, perm->listen_host, perm->listen_port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 562 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 563 | (r = sshpkt_put_cstring(ssh, |
| 564 | "cancel-tcpip-forward")) != 0 || |
| 565 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 566 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 567 | channel_rfwd_bind_host(perm->listen_host))) != 0 || |
| 568 | (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 569 | (r = sshpkt_send(ssh)) != 0) { |
| 570 | fatal("%s: channel %i: %s", __func__, |
| 571 | c->self, ssh_err(r)); |
| 572 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 573 | fwd_perm_clear(perm); /* unregister */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 574 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 578 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 579 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 580 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 581 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 582 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 583 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 584 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 585 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 586 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 587 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 588 | if ((other = sc->channels[i]) == NULL) |
| 589 | continue; |
| 590 | n++; |
| 591 | /* detach from mux client and prepare for closing */ |
| 592 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 593 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 594 | other->mux_ctx == c) { |
| 595 | other->mux_ctx = NULL; |
| 596 | other->type = SSH_CHANNEL_OPEN; |
| 597 | other->istate = CHAN_INPUT_CLOSED; |
| 598 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 601 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 602 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 603 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 604 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 605 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 606 | |
markus@openbsd.org | 5d14019 | 2018-07-27 12:03:17 +0000 | [diff] [blame] | 607 | if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) { |
| 608 | s = channel_open_message(ssh); |
| 609 | debug3("channel %d: status: %s", c->self, s); |
| 610 | free(s); |
| 611 | } |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 612 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 613 | channel_close_fds(ssh, c); |
| 614 | sshbuf_free(c->input); |
| 615 | sshbuf_free(c->output); |
| 616 | sshbuf_free(c->extended); |
| 617 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 618 | free(c->remote_name); |
| 619 | c->remote_name = NULL; |
| 620 | free(c->path); |
| 621 | c->path = NULL; |
| 622 | free(c->listening_addr); |
| 623 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 624 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 625 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 626 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 627 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 628 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 629 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 630 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 631 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 632 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 633 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 634 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 635 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 636 | } |
| 637 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 638 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 639 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 640 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 641 | u_int i; |
dtucker@openbsd.org | 62be1ff | 2019-05-03 04:11:00 +0000 | [diff] [blame] | 642 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 643 | |
dtucker@openbsd.org | 62be1ff | 2019-05-03 04:11:00 +0000 | [diff] [blame] | 644 | for (i = 0; i < sc->channels_alloc; i++) |
| 645 | if (sc->channels[i] != NULL) |
| 646 | channel_free(ssh, sc->channels[i]); |
| 647 | |
| 648 | free(sc->channels); |
| 649 | sc->channels = NULL; |
| 650 | sc->channels_alloc = 0; |
| 651 | sc->channel_max_fd = 0; |
| 652 | |
| 653 | free(sc->x11_saved_display); |
| 654 | sc->x11_saved_display = NULL; |
| 655 | |
| 656 | free(sc->x11_saved_proto); |
| 657 | sc->x11_saved_proto = NULL; |
| 658 | |
| 659 | free(sc->x11_saved_data); |
| 660 | sc->x11_saved_data = NULL; |
| 661 | sc->x11_saved_data_len = 0; |
| 662 | |
| 663 | free(sc->x11_fake_data); |
| 664 | sc->x11_fake_data = NULL; |
| 665 | sc->x11_fake_data_len = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 670 | * descriptors after a fork. |
| 671 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 672 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 673 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 674 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 675 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 676 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 677 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 678 | if (ssh->chanctxt->channels[i] != NULL) |
| 679 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 683 | * Stop listening to channels. |
| 684 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 685 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 686 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 687 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 688 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 689 | Channel *c; |
| 690 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 691 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 692 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 693 | if (c != NULL) { |
| 694 | switch (c->type) { |
| 695 | case SSH_CHANNEL_AUTH_SOCKET: |
| 696 | case SSH_CHANNEL_PORT_LISTENER: |
| 697 | case SSH_CHANNEL_RPORT_LISTENER: |
| 698 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 699 | case SSH_CHANNEL_UNIX_LISTENER: |
| 700 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 701 | channel_close_fd(ssh, &c->sock); |
| 702 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 710 | * Returns true if no channel has too much buffered data, and false if one or |
| 711 | * more channel is overfull. |
| 712 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 713 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 714 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 715 | { |
| 716 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 717 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 718 | Channel *c; |
| 719 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 720 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 721 | c = ssh->chanctxt->channels[i]; |
| 722 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 723 | continue; |
| 724 | if (sshbuf_len(c->output) > maxsize) { |
| 725 | debug2("channel %d: big output buffer %zu > %u", |
| 726 | c->self, sshbuf_len(c->output), maxsize); |
| 727 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | return 1; |
| 731 | } |
| 732 | |
| 733 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 734 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 735 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 736 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 737 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 738 | Channel *c; |
| 739 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 740 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 741 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 742 | if (c == NULL) |
| 743 | continue; |
| 744 | switch (c->type) { |
| 745 | case SSH_CHANNEL_X11_LISTENER: |
| 746 | case SSH_CHANNEL_PORT_LISTENER: |
| 747 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 748 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 749 | case SSH_CHANNEL_CLOSED: |
| 750 | case SSH_CHANNEL_AUTH_SOCKET: |
| 751 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 752 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 753 | case SSH_CHANNEL_CONNECTING: |
| 754 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 755 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 756 | case SSH_CHANNEL_UNIX_LISTENER: |
| 757 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 758 | continue; |
| 759 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 760 | continue; |
| 761 | case SSH_CHANNEL_OPENING: |
| 762 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 763 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 764 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 765 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 766 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 767 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 768 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 769 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 770 | /* NOTREACHED */ |
| 771 | } |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 777 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 778 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 779 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 780 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 781 | Channel *c; |
| 782 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 783 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 784 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 785 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 786 | continue; |
| 787 | switch (c->type) { |
| 788 | case SSH_CHANNEL_CLOSED: |
| 789 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 790 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 791 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 792 | case SSH_CHANNEL_X11_LISTENER: |
| 793 | case SSH_CHANNEL_PORT_LISTENER: |
| 794 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 795 | case SSH_CHANNEL_MUX_LISTENER: |
| 796 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 797 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 798 | case SSH_CHANNEL_OPENING: |
| 799 | case SSH_CHANNEL_CONNECTING: |
| 800 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 801 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 802 | case SSH_CHANNEL_UNIX_LISTENER: |
| 803 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 804 | continue; |
| 805 | case SSH_CHANNEL_LARVAL: |
| 806 | case SSH_CHANNEL_AUTH_SOCKET: |
| 807 | case SSH_CHANNEL_OPEN: |
| 808 | case SSH_CHANNEL_X11_OPEN: |
| 809 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 810 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 811 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 812 | /* NOTREACHED */ |
| 813 | } |
| 814 | } |
| 815 | return -1; |
| 816 | } |
| 817 | |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 818 | /* Returns the state of the channel's extended usage flag */ |
| 819 | const char * |
| 820 | channel_format_extended_usage(const Channel *c) |
| 821 | { |
| 822 | if (c->efd == -1) |
| 823 | return "closed"; |
| 824 | |
| 825 | switch (c->extended_usage) { |
| 826 | case CHAN_EXTENDED_WRITE: |
| 827 | return "write"; |
| 828 | case CHAN_EXTENDED_READ: |
| 829 | return "read"; |
| 830 | case CHAN_EXTENDED_IGNORE: |
| 831 | return "ignore"; |
| 832 | default: |
| 833 | return "UNKNOWN"; |
| 834 | } |
| 835 | } |
| 836 | |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 837 | static char * |
| 838 | channel_format_status(const Channel *c) |
| 839 | { |
| 840 | char *ret = NULL; |
| 841 | |
| 842 | xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu " |
| 843 | "fd %d/%d/%d sock %d cc %d", |
| 844 | c->type, |
| 845 | c->have_remote_id ? "r" : "nr", c->remote_id, |
| 846 | c->istate, sshbuf_len(c->input), |
| 847 | c->ostate, sshbuf_len(c->output), |
| 848 | channel_format_extended_usage(c), sshbuf_len(c->extended), |
| 849 | c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan); |
| 850 | return ret; |
| 851 | } |
| 852 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 853 | /* |
| 854 | * Returns a message describing the currently open forwarded connections, |
| 855 | * suitable for sending to the client. The message contains crlf pairs for |
| 856 | * newlines. |
| 857 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 858 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 859 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 860 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 861 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 862 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 863 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 864 | int r; |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 865 | char *cp, *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 866 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 867 | if ((buf = sshbuf_new()) == NULL) |
| 868 | fatal("%s: sshbuf_new", __func__); |
| 869 | if ((r = sshbuf_putf(buf, |
| 870 | "The following connections are open:\r\n")) != 0) |
| 871 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 872 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 873 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 874 | if (c == NULL) |
| 875 | continue; |
| 876 | switch (c->type) { |
| 877 | case SSH_CHANNEL_X11_LISTENER: |
| 878 | case SSH_CHANNEL_PORT_LISTENER: |
| 879 | case SSH_CHANNEL_RPORT_LISTENER: |
| 880 | case SSH_CHANNEL_CLOSED: |
| 881 | case SSH_CHANNEL_AUTH_SOCKET: |
| 882 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 883 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 884 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 885 | case SSH_CHANNEL_UNIX_LISTENER: |
| 886 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 887 | continue; |
| 888 | case SSH_CHANNEL_LARVAL: |
| 889 | case SSH_CHANNEL_OPENING: |
| 890 | case SSH_CHANNEL_CONNECTING: |
| 891 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 892 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 893 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 894 | case SSH_CHANNEL_OPEN: |
| 895 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 896 | case SSH_CHANNEL_MUX_PROXY: |
| 897 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 898 | cp = channel_format_status(c); |
| 899 | if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n", |
| 900 | c->self, c->remote_name, cp)) != 0) { |
| 901 | free(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 902 | fatal("%s: sshbuf_putf: %s", |
| 903 | __func__, ssh_err(r)); |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 904 | } |
| 905 | free(cp); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 906 | continue; |
| 907 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 908 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 909 | /* NOTREACHED */ |
| 910 | } |
| 911 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 912 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 913 | fatal("%s: sshbuf_dup_string", __func__); |
| 914 | sshbuf_free(buf); |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 920 | { |
| 921 | int r; |
| 922 | |
| 923 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 924 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 925 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 926 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 927 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 928 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 929 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 933 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 934 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 935 | Channel *c = channel_lookup(ssh, id); |
| 936 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 937 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 938 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 939 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 942 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 943 | open_preamble(ssh, __func__, c, c->ctype); |
| 944 | if ((r = sshpkt_send(ssh)) != 0) |
| 945 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 949 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 950 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 951 | Channel *c = channel_lookup(ssh, id); |
| 952 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 953 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 954 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 955 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 958 | if (!c->have_remote_id) |
| 959 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 960 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 961 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 962 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 963 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 964 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 965 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 966 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 967 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 968 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 969 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 970 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 971 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 972 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 973 | { |
| 974 | struct channel_confirm *cc; |
| 975 | Channel *c; |
| 976 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 977 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 978 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 979 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 980 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 981 | cc->cb = cb; |
| 982 | cc->abandon_cb = abandon_cb; |
| 983 | cc->ctx = ctx; |
| 984 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 985 | } |
| 986 | |
| 987 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 988 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 989 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 990 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 991 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 992 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 993 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 994 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 995 | return; |
| 996 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 997 | c->open_confirm = fn; |
| 998 | c->open_confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 999 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1000 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1001 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1002 | channel_register_cleanup(struct ssh *ssh, int id, |
| 1003 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1004 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1005 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1006 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1007 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1008 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 1011 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1012 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1013 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1014 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1015 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1016 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1017 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1018 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1019 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1020 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1021 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 1024 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1025 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1026 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1027 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1028 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1029 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1030 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1031 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1032 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1033 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1034 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1035 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1036 | return; |
| 1037 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1038 | c->input_filter = ifn; |
| 1039 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1040 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1041 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1045 | 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] | 1046 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1047 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1048 | Channel *c = channel_lookup(ssh, id); |
| 1049 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1050 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1051 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 1052 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1053 | if (!c->have_remote_id) |
| 1054 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 1055 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1056 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1057 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 1058 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1059 | |
| 1060 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 1061 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1062 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1063 | (r = sshpkt_send(ssh)) != 0) |
| 1064 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1067 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1068 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 1069 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1070 | { |
| 1071 | FD_SET(c->sock, readset); |
| 1072 | } |
| 1073 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1074 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1075 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 1076 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1077 | { |
| 1078 | debug3("channel %d: waiting for connection", c->self); |
| 1079 | FD_SET(c->sock, writeset); |
| 1080 | } |
| 1081 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1082 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1083 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 1084 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1085 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1086 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1087 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1088 | sshbuf_len(c->input) < c->remote_window && |
| 1089 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1090 | FD_SET(c->rfd, readset); |
| 1091 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1092 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1093 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1094 | FD_SET(c->wfd, writeset); |
| 1095 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1096 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1097 | debug2("channel %d: " |
| 1098 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 1099 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1100 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1101 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1105 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 1106 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1107 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1108 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1109 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1110 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1111 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1112 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1113 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1114 | FD_SET(c->efd, readset); |
| 1115 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1116 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1117 | } |
| 1118 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1119 | /* |
| 1120 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1121 | * connection (when authentication spoofing is being done) remains in this |
| 1122 | * state until the first packet has been completely read. The authentication |
| 1123 | * data in that packet is then substituted by the real data if it matches the |
| 1124 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1125 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1126 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1127 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1128 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1129 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1130 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1131 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1132 | u_char *ucp; |
| 1133 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1134 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1135 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1136 | if (sc->x11_refuse_time != 0 && |
| 1137 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1138 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1139 | "expired"); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1143 | /* 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] | 1144 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1145 | return 0; |
| 1146 | |
| 1147 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1148 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1149 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1150 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1151 | data_len = 256 * ucp[8] + ucp[9]; |
| 1152 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1153 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1154 | data_len = ucp[8] + 256 * ucp[9]; |
| 1155 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1156 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1157 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1158 | return -1; |
| 1159 | } |
| 1160 | |
| 1161 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1162 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1163 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1164 | return 0; |
| 1165 | |
| 1166 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1167 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1168 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1169 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1170 | return -1; |
| 1171 | } |
| 1172 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1173 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1174 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1175 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1176 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1177 | return -1; |
| 1178 | } |
| 1179 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1180 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1181 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1182 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1183 | return -1; |
| 1184 | } |
| 1185 | /* |
| 1186 | * Received authentication protocol and data match |
| 1187 | * our fake data. Substitute the fake data with real |
| 1188 | * data. |
| 1189 | */ |
| 1190 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1191 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1192 | return 1; |
| 1193 | } |
| 1194 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1195 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1196 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1197 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1198 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1199 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1200 | |
| 1201 | /* c->force_drain = 1; */ |
| 1202 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1203 | if (ret == 1) { |
| 1204 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1205 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1206 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1207 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1208 | debug2("X11 rejected %d i%d/o%d", |
| 1209 | c->self, c->istate, c->ostate); |
| 1210 | chan_read_failed(ssh, c); |
| 1211 | sshbuf_reset(c->input); |
| 1212 | chan_ibuf_empty(ssh, c); |
| 1213 | sshbuf_reset(c->output); |
| 1214 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1215 | 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] | 1216 | } |
| 1217 | } |
| 1218 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1219 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1220 | channel_pre_mux_client(struct ssh *ssh, |
| 1221 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1222 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1223 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1224 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1225 | FD_SET(c->rfd, readset); |
| 1226 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1227 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1228 | sshbuf_reset(c->input); |
| 1229 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1230 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1231 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1232 | } |
| 1233 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1234 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1235 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1236 | FD_SET(c->wfd, writeset); |
| 1237 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1238 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1242 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1243 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1244 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1245 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1246 | const u_char *p; |
| 1247 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1248 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1249 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1250 | struct { |
| 1251 | u_int8_t version; |
| 1252 | u_int8_t command; |
| 1253 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1254 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1255 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1256 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1257 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1258 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1259 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1260 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1261 | len = sizeof(s4_req); |
| 1262 | if (have < len) |
| 1263 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1264 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1265 | |
| 1266 | need = 1; |
| 1267 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1268 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1269 | debug2("channel %d: socks4a request", c->self); |
| 1270 | /* ... and needs an extra string (the hostname) */ |
| 1271 | need = 2; |
| 1272 | } |
| 1273 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1274 | for (found = 0, i = len; i < have; i++) { |
| 1275 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1276 | found++; |
| 1277 | if (found == need) |
| 1278 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1279 | } |
| 1280 | if (i > 1024) { |
| 1281 | /* the peer is probably sending garbage */ |
| 1282 | debug("channel %d: decode socks4: too long", |
| 1283 | c->self); |
| 1284 | return -1; |
| 1285 | } |
| 1286 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1287 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1288 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1289 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1290 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1291 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1292 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1293 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1294 | return -1; |
| 1295 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1296 | have = sshbuf_len(input); |
| 1297 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1298 | if (memchr(p, '\0', have) == NULL) { |
| 1299 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1300 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1301 | return -1; |
| 1302 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1303 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1304 | 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] | 1305 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1306 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1307 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1308 | fatal("%s: channel %d: consume: %s", __func__, |
| 1309 | c->self, ssh_err(r)); |
| 1310 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1311 | free(c->path); |
| 1312 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1313 | if (need == 1) { /* SOCKS4: one string */ |
| 1314 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1315 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1316 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1317 | have = sshbuf_len(input); |
| 1318 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1319 | if (memchr(p, '\0', have) == NULL) { |
| 1320 | error("channel %d: decode socks4a: host not nul " |
| 1321 | "terminated", c->self); |
| 1322 | return -1; |
| 1323 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1324 | len = strlen(p); |
| 1325 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1326 | c->self, p, len); |
| 1327 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1328 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1329 | error("channel %d: hostname \"%.100s\" too long", |
| 1330 | c->self, p); |
| 1331 | return -1; |
| 1332 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1333 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1334 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1335 | fatal("%s: channel %d: consume: %s", __func__, |
| 1336 | c->self, ssh_err(r)); |
| 1337 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1338 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1339 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1340 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1341 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1342 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1343 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1344 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1345 | debug("channel %d: cannot handle: %s cn %d", |
| 1346 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1347 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1348 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1349 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1350 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1351 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1352 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1353 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1354 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1355 | c->self, ssh_err(r)); |
| 1356 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1357 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1360 | /* try to decode a socks5 header */ |
| 1361 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1362 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1363 | #define SSH_SOCKS5_IPV4 0x01 |
| 1364 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1365 | #define SSH_SOCKS5_IPV6 0x04 |
| 1366 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1367 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1368 | |
| 1369 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1370 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1371 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1372 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1373 | struct { |
| 1374 | u_int8_t version; |
| 1375 | u_int8_t command; |
| 1376 | u_int8_t reserved; |
| 1377 | u_int8_t atyp; |
| 1378 | } s5_req, s5_rsp; |
| 1379 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1380 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1381 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1382 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1383 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1384 | |
| 1385 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1386 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1387 | if (p[0] != 0x05) |
| 1388 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1389 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1390 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1391 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1392 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1393 | return 0; |
| 1394 | nmethods = p[1]; |
| 1395 | if (have < nmethods + 2) |
| 1396 | return 0; |
| 1397 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1398 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1399 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1400 | found = 1; |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | if (!found) { |
| 1405 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1406 | c->self); |
| 1407 | return -1; |
| 1408 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1409 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1410 | fatal("%s: channel %d: consume: %s", __func__, |
| 1411 | c->self, ssh_err(r)); |
| 1412 | } |
| 1413 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1414 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1415 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1416 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1417 | c->self, ssh_err(r)); |
| 1418 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1419 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1420 | debug2("channel %d: socks5 auth done", c->self); |
| 1421 | return 0; /* need more */ |
| 1422 | } |
| 1423 | debug2("channel %d: socks5 post auth", c->self); |
| 1424 | if (have < sizeof(s5_req)+1) |
| 1425 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1426 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1427 | if (s5_req.version != 0x05 || |
| 1428 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1429 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1430 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1431 | return -1; |
| 1432 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1433 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1434 | case SSH_SOCKS5_IPV4: |
| 1435 | addrlen = 4; |
| 1436 | af = AF_INET; |
| 1437 | break; |
| 1438 | case SSH_SOCKS5_DOMAIN: |
| 1439 | addrlen = p[sizeof(s5_req)]; |
| 1440 | af = -1; |
| 1441 | break; |
| 1442 | case SSH_SOCKS5_IPV6: |
| 1443 | addrlen = 16; |
| 1444 | af = AF_INET6; |
| 1445 | break; |
| 1446 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1447 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1448 | return -1; |
| 1449 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1450 | need = sizeof(s5_req) + addrlen + 2; |
| 1451 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1452 | need++; |
| 1453 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1454 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1455 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1456 | fatal("%s: channel %d: consume: %s", __func__, |
| 1457 | c->self, ssh_err(r)); |
| 1458 | } |
| 1459 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1460 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1461 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1462 | fatal("%s: channel %d: consume: %s", __func__, |
| 1463 | c->self, ssh_err(r)); |
| 1464 | } |
| 1465 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1466 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1467 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1468 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1469 | return -1; |
| 1470 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1471 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1472 | free(c->path); |
| 1473 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1474 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1475 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1476 | error("channel %d: dynamic request: socks5 hostname " |
| 1477 | "\"%.100s\" too long", c->self, dest_addr); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | c->path = xstrdup(dest_addr); |
| 1481 | } else { |
| 1482 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1483 | return -1; |
| 1484 | c->path = xstrdup(ntop); |
| 1485 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1486 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1487 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1488 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1489 | c->self, c->path, c->host_port, s5_req.command); |
| 1490 | |
| 1491 | s5_rsp.version = 0x05; |
| 1492 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1493 | s5_rsp.reserved = 0; /* ignored */ |
| 1494 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1495 | dest_port = 0; /* ignored */ |
| 1496 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1497 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1498 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1499 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1500 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1501 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1502 | return 1; |
| 1503 | } |
| 1504 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1505 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1506 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1507 | 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] | 1508 | { |
| 1509 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1510 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1511 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1512 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1513 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1514 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1515 | 0, "stdio-forward", /*nonblock*/0); |
| 1516 | |
| 1517 | c->path = xstrdup(host_to_connect); |
| 1518 | c->host_port = port_to_connect; |
| 1519 | c->listening_port = 0; |
| 1520 | c->force_drain = 1; |
| 1521 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1522 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1523 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1524 | |
| 1525 | return c; |
| 1526 | } |
| 1527 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1528 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1529 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1530 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1531 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1532 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1533 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1534 | u_int have; |
| 1535 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1536 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1537 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1538 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1539 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1540 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1541 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1542 | /* need more */ |
| 1543 | FD_SET(c->sock, readset); |
| 1544 | return; |
| 1545 | } |
| 1546 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1547 | p = sshbuf_ptr(c->input); |
| 1548 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1549 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1550 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1551 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1552 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1553 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1554 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1555 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1556 | default: |
| 1557 | ret = -1; |
| 1558 | break; |
| 1559 | } |
| 1560 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1561 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1562 | } else if (ret == 0) { |
| 1563 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1564 | /* need more */ |
| 1565 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1566 | if (sshbuf_len(c->output)) |
| 1567 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1568 | } else { |
| 1569 | /* switch to the next state */ |
| 1570 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1571 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1574 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1575 | /* simulate read-error */ |
| 1576 | static void |
| 1577 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1578 | { |
| 1579 | c->type = SSH_CHANNEL_OPEN; |
| 1580 | chan_read_failed(ssh, c); |
| 1581 | sshbuf_reset(c->input); |
| 1582 | chan_ibuf_empty(ssh, c); |
| 1583 | sshbuf_reset(c->output); |
| 1584 | chan_write_failed(ssh, c); |
| 1585 | } |
| 1586 | |
| 1587 | /* reverse dynamic port forwarding */ |
| 1588 | static void |
| 1589 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1590 | { |
| 1591 | const u_char *p; |
| 1592 | u_int have, len; |
| 1593 | int r, ret; |
| 1594 | |
| 1595 | have = sshbuf_len(c->output); |
| 1596 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1597 | /* sshbuf_dump(c->output, stderr); */ |
| 1598 | /* EOF received */ |
| 1599 | if (c->flags & CHAN_EOF_RCVD) { |
| 1600 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1601 | fatal("%s: channel %d: consume: %s", |
| 1602 | __func__, c->self, ssh_err(r)); |
| 1603 | } |
| 1604 | rdynamic_close(ssh, c); |
| 1605 | return; |
| 1606 | } |
| 1607 | /* check if the fixed size part of the packet is in buffer. */ |
| 1608 | if (have < 3) |
| 1609 | return; |
| 1610 | /* try to guess the protocol */ |
| 1611 | p = sshbuf_ptr(c->output); |
| 1612 | switch (p[0]) { |
| 1613 | case 0x04: |
| 1614 | /* switch input/output for reverse forwarding */ |
| 1615 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1616 | break; |
| 1617 | case 0x05: |
| 1618 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1619 | break; |
| 1620 | default: |
| 1621 | ret = -1; |
| 1622 | break; |
| 1623 | } |
| 1624 | if (ret < 0) { |
| 1625 | rdynamic_close(ssh, c); |
| 1626 | } else if (ret == 0) { |
| 1627 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1628 | /* send socks request to peer */ |
| 1629 | len = sshbuf_len(c->input); |
| 1630 | if (len > 0 && len < c->remote_window) { |
| 1631 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1632 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1633 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1634 | (r = sshpkt_send(ssh)) != 0) { |
| 1635 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1636 | c->self, ssh_err(r)); |
| 1637 | } |
| 1638 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1639 | fatal("%s: channel %d: consume: %s", |
| 1640 | __func__, c->self, ssh_err(r)); |
| 1641 | } |
| 1642 | c->remote_window -= len; |
| 1643 | } |
| 1644 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1645 | /* the connect failed */ |
| 1646 | rdynamic_close(ssh, c); |
| 1647 | } |
| 1648 | } |
| 1649 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1650 | /* This is our fake X11 server socket. */ |
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 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1653 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1654 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1655 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1656 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1657 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1658 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1659 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1660 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1661 | if (!FD_ISSET(c->sock, readset)) |
| 1662 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1663 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1664 | debug("X11 connection requested."); |
| 1665 | addrlen = sizeof(addr); |
| 1666 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1667 | if (c->single_connection) { |
| 1668 | oerrno = errno; |
| 1669 | debug2("single_connection: closing X11 listener."); |
| 1670 | channel_close_fd(ssh, &c->sock); |
| 1671 | chan_mark_dead(ssh, c); |
| 1672 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1673 | } |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1674 | if (newsock == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1675 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1676 | errno != ECONNABORTED) |
| 1677 | error("accept: %.100s", strerror(errno)); |
| 1678 | if (errno == EMFILE || errno == ENFILE) |
| 1679 | c->notbefore = monotime() + 1; |
| 1680 | return; |
| 1681 | } |
| 1682 | set_nodelay(newsock); |
| 1683 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1684 | remote_port = get_peer_port(newsock); |
| 1685 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1686 | remote_ipaddr, remote_port); |
| 1687 | |
| 1688 | nc = channel_new(ssh, "accepted x11 socket", |
| 1689 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1690 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1691 | open_preamble(ssh, __func__, nc, "x11"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1692 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1693 | (r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1694 | fatal("%s: channel %i: reply %s", __func__, |
| 1695 | c->self, ssh_err(r)); |
| 1696 | } |
| 1697 | if ((r = sshpkt_send(ssh)) != 0) |
| 1698 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
| 1699 | free(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1700 | } |
| 1701 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1702 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1703 | port_open_helper(struct ssh *ssh, Channel *c, char *rtype) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1704 | { |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1705 | char *local_ipaddr = get_local_ipaddr(c->sock); |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 1706 | int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1707 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1708 | int remote_port = get_peer_port(c->sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1709 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1710 | |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1711 | if (remote_port == -1) { |
| 1712 | /* Fake addr/port to appease peers that validate it (Tectia) */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1713 | free(remote_ipaddr); |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1714 | remote_ipaddr = xstrdup("127.0.0.1"); |
| 1715 | remote_port = 65535; |
| 1716 | } |
| 1717 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1718 | free(c->remote_name); |
| 1719 | xasprintf(&c->remote_name, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1720 | "%s: listening port %d for %.100s port %d, " |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1721 | "connect from %.200s port %d to %.100s port %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1722 | rtype, c->listening_port, c->path, c->host_port, |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1723 | remote_ipaddr, remote_port, local_ipaddr, local_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1724 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1725 | open_preamble(ssh, __func__, c, rtype); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1726 | if (strcmp(rtype, "direct-tcpip") == 0) { |
| 1727 | /* target host, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1728 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1729 | (r = sshpkt_put_u32(ssh, c->host_port)) != 0) { |
| 1730 | fatal("%s: channel %i: reply %s", __func__, |
| 1731 | c->self, ssh_err(r)); |
| 1732 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1733 | } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) { |
| 1734 | /* target path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1735 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1736 | fatal("%s: channel %i: reply %s", __func__, |
| 1737 | c->self, ssh_err(r)); |
| 1738 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1739 | } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1740 | /* listen path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1741 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1742 | fatal("%s: channel %i: reply %s", __func__, |
| 1743 | c->self, ssh_err(r)); |
| 1744 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1745 | } else { |
| 1746 | /* listen address, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1747 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1748 | (r = sshpkt_put_u32(ssh, local_port)) != 0) { |
| 1749 | fatal("%s: channel %i: reply %s", __func__, |
| 1750 | c->self, ssh_err(r)); |
| 1751 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1752 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1753 | if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1754 | /* reserved for future owner/mode info */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1755 | if ((r = sshpkt_put_cstring(ssh, "")) != 0) { |
| 1756 | fatal("%s: channel %i: reply %s", __func__, |
| 1757 | c->self, ssh_err(r)); |
| 1758 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1759 | } else { |
| 1760 | /* originator host and port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1761 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1762 | (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) { |
| 1763 | fatal("%s: channel %i: reply %s", __func__, |
| 1764 | c->self, ssh_err(r)); |
| 1765 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1766 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1767 | if ((r = sshpkt_send(ssh)) != 0) |
| 1768 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1769 | free(remote_ipaddr); |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1770 | free(local_ipaddr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1773 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1774 | 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] | 1775 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1776 | ssh->chanctxt->x11_refuse_time = refuse_time; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1779 | /* |
| 1780 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1781 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1782 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1783 | channel_post_port_listener(struct ssh *ssh, Channel *c, |
| 1784 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1785 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1786 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1787 | struct sockaddr_storage addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1788 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1789 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1790 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1791 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1792 | if (!FD_ISSET(c->sock, readset)) |
| 1793 | return; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1794 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1795 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
| 1796 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1797 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1798 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1799 | nextstate = SSH_CHANNEL_OPENING; |
| 1800 | rtype = "forwarded-tcpip"; |
| 1801 | } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) { |
| 1802 | nextstate = SSH_CHANNEL_OPENING; |
| 1803 | rtype = "forwarded-streamlocal@openssh.com"; |
| 1804 | } else if (c->host_port == PORT_STREAMLOCAL) { |
| 1805 | nextstate = SSH_CHANNEL_OPENING; |
| 1806 | rtype = "direct-streamlocal@openssh.com"; |
| 1807 | } else if (c->host_port == 0) { |
| 1808 | nextstate = SSH_CHANNEL_DYNAMIC; |
| 1809 | rtype = "dynamic-tcpip"; |
| 1810 | } else { |
| 1811 | nextstate = SSH_CHANNEL_OPENING; |
| 1812 | rtype = "direct-tcpip"; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1813 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1814 | |
| 1815 | addrlen = sizeof(addr); |
| 1816 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1817 | if (newsock == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1818 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1819 | errno != ECONNABORTED) |
| 1820 | error("accept: %.100s", strerror(errno)); |
| 1821 | if (errno == EMFILE || errno == ENFILE) |
| 1822 | c->notbefore = monotime() + 1; |
| 1823 | return; |
| 1824 | } |
| 1825 | if (c->host_port != PORT_STREAMLOCAL) |
| 1826 | set_nodelay(newsock); |
| 1827 | nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1, |
| 1828 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
| 1829 | nc->listening_port = c->listening_port; |
| 1830 | nc->host_port = c->host_port; |
| 1831 | if (c->path != NULL) |
| 1832 | nc->path = xstrdup(c->path); |
| 1833 | |
| 1834 | if (nextstate != SSH_CHANNEL_DYNAMIC) |
| 1835 | port_open_helper(ssh, nc, rtype); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * This is the authentication agent socket listening for connections from |
| 1840 | * clients. |
| 1841 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1842 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1843 | channel_post_auth_listener(struct ssh *ssh, Channel *c, |
| 1844 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1845 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1846 | Channel *nc; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1847 | int r, newsock; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1848 | struct sockaddr_storage addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1849 | socklen_t addrlen; |
| 1850 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1851 | if (!FD_ISSET(c->sock, readset)) |
| 1852 | return; |
| 1853 | |
| 1854 | addrlen = sizeof(addr); |
| 1855 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1856 | if (newsock == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1857 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1858 | if (errno == EMFILE || errno == ENFILE) |
| 1859 | c->notbefore = monotime() + 1; |
| 1860 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1861 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1862 | nc = channel_new(ssh, "accepted auth socket", |
| 1863 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1864 | c->local_window_max, c->local_maxpacket, |
| 1865 | 0, "accepted auth socket", 1); |
| 1866 | open_preamble(ssh, __func__, nc, "auth-agent@openssh.com"); |
| 1867 | if ((r = sshpkt_send(ssh)) != 0) |
| 1868 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1869 | } |
| 1870 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1871 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1872 | channel_post_connecting(struct ssh *ssh, Channel *c, |
| 1873 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1874 | { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1875 | int err = 0, sock, isopen, r; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1876 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1877 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1878 | if (!FD_ISSET(c->sock, writeset)) |
| 1879 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1880 | if (!c->have_remote_id) |
| 1881 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1882 | /* for rdynamic the OPEN_CONFIRMATION has been sent already */ |
| 1883 | isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1884 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1885 | err = errno; |
| 1886 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1887 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1888 | if (err == 0) { |
| 1889 | debug("channel %d: connected to %s port %d", |
| 1890 | c->self, c->connect_ctx.host, c->connect_ctx.port); |
| 1891 | channel_connect_ctx_free(&c->connect_ctx); |
| 1892 | c->type = SSH_CHANNEL_OPEN; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1893 | if (isopen) { |
| 1894 | /* no message necessary */ |
| 1895 | } else { |
| 1896 | if ((r = sshpkt_start(ssh, |
| 1897 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1898 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1899 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1900 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1901 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) |
| 1902 | != 0) |
| 1903 | fatal("%s: channel %i: confirm: %s", __func__, |
| 1904 | c->self, ssh_err(r)); |
| 1905 | if ((r = sshpkt_send(ssh)) != 0) |
| 1906 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1907 | ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } else { |
| 1910 | debug("channel %d: connection failed: %s", |
| 1911 | c->self, strerror(err)); |
| 1912 | /* Try next address, if any */ |
| 1913 | if ((sock = connect_next(&c->connect_ctx)) > 0) { |
| 1914 | close(c->sock); |
| 1915 | c->sock = c->rfd = c->wfd = sock; |
| 1916 | channel_find_maxfd(ssh->chanctxt); |
| 1917 | return; |
| 1918 | } |
| 1919 | /* Exhausted all addresses */ |
| 1920 | error("connect_to %.100s port %d: failed.", |
| 1921 | c->connect_ctx.host, c->connect_ctx.port); |
| 1922 | channel_connect_ctx_free(&c->connect_ctx); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1923 | if (isopen) { |
| 1924 | rdynamic_close(ssh, c); |
| 1925 | } else { |
| 1926 | if ((r = sshpkt_start(ssh, |
| 1927 | SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1928 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1929 | (r = sshpkt_put_u32(ssh, |
| 1930 | SSH2_OPEN_CONNECT_FAILED)) != 0 || |
| 1931 | (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1932 | (r = sshpkt_put_cstring(ssh, "")) != 0) { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1933 | fatal("%s: channel %i: failure: %s", __func__, |
| 1934 | c->self, ssh_err(r)); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1935 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1936 | if ((r = sshpkt_send(ssh)) != 0) |
| 1937 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1938 | ssh_err(r)); |
| 1939 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1940 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1941 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1942 | } |
| 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_rfd(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 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1948 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1949 | ssize_t len; |
| 1950 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1951 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1952 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1955 | return 1; |
| 1956 | |
| 1957 | errno = 0; |
| 1958 | len = read(c->rfd, buf, sizeof(buf)); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 1959 | if (len == -1 && (errno == EINTR || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1960 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1961 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1962 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1963 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1964 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1965 | if ((!c->isatty && len <= 0) || |
| 1966 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1967 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1968 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1969 | c->self, c->rfd, len); |
| 1970 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1971 | debug2("channel %d: not open", c->self); |
| 1972 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1973 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1974 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1975 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1976 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1977 | return -1; |
| 1978 | } |
| 1979 | if (c->input_filter != NULL) { |
| 1980 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1981 | debug2("channel %d: filter stops", c->self); |
| 1982 | chan_read_failed(ssh, c); |
| 1983 | } |
| 1984 | } else if (c->datagram) { |
| 1985 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1986 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1987 | c->self, ssh_err(r)); |
| 1988 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1989 | fatal("%s: channel %d: put data: %s", __func__, |
| 1990 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1991 | } |
| 1992 | return 1; |
| 1993 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1994 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1995 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1996 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1997 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1998 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1999 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2000 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 2001 | size_t dlen, olen = 0; |
| 2002 | int r, len; |
| 2003 | |
| 2004 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2005 | sshbuf_len(c->output) == 0) |
| 2006 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2007 | |
| 2008 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2009 | olen = sshbuf_len(c->output); |
| 2010 | if (c->output_filter != NULL) { |
| 2011 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 2012 | debug2("channel %d: filter stops", c->self); |
| 2013 | if (c->type != SSH_CHANNEL_OPEN) |
| 2014 | chan_mark_dead(ssh, c); |
| 2015 | else |
| 2016 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2017 | return -1; |
| 2018 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2019 | } else if (c->datagram) { |
| 2020 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 2021 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 2022 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 2023 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2024 | } else { |
| 2025 | buf = data = sshbuf_mutable_ptr(c->output); |
| 2026 | dlen = sshbuf_len(c->output); |
| 2027 | } |
| 2028 | |
| 2029 | if (c->datagram) { |
| 2030 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 2031 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2032 | free(data); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2033 | if (len == -1 && (errno == EINTR || errno == EAGAIN || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2034 | errno == EWOULDBLOCK)) |
| 2035 | return 1; |
| 2036 | if (len <= 0) |
| 2037 | goto write_fail; |
| 2038 | goto out; |
| 2039 | } |
| 2040 | |
| 2041 | #ifdef _AIX |
| 2042 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 2043 | if (c->wfd_isatty) |
| 2044 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 2045 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2046 | |
| 2047 | len = write(c->wfd, buf, dlen); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2048 | if (len == -1 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2049 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 2050 | return 1; |
| 2051 | if (len <= 0) { |
| 2052 | write_fail: |
| 2053 | if (c->type != SSH_CHANNEL_OPEN) { |
| 2054 | debug2("channel %d: not open", c->self); |
| 2055 | chan_mark_dead(ssh, c); |
| 2056 | return -1; |
| 2057 | } else { |
| 2058 | chan_write_failed(ssh, c); |
| 2059 | } |
| 2060 | return -1; |
| 2061 | } |
| 2062 | #ifndef BROKEN_TCGETATTR_ICANON |
| 2063 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 2064 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 2065 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 2066 | /* |
| 2067 | * Simulate echo to reduce the impact of |
| 2068 | * traffic analysis. We need to match the |
| 2069 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 2070 | * (4 byte channel id + buf) |
| 2071 | */ |
| 2072 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 2073 | (r = sshpkt_send(ssh)) != 0) |
| 2074 | fatal("%s: channel %d: ignore: %s", |
| 2075 | __func__, c->self, ssh_err(r)); |
| 2076 | } |
| 2077 | } |
| 2078 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 2079 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 2080 | fatal("%s: channel %d: consume: %s", |
| 2081 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2082 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2083 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2084 | c->local_consumed += olen - sshbuf_len(c->output); |
| 2085 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2086 | return 1; |
| 2087 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2088 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2089 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2090 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 2091 | fd_set *readset, fd_set *writeset) |
| 2092 | { |
| 2093 | int r; |
| 2094 | ssize_t len; |
| 2095 | |
| 2096 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 2097 | return 1; |
| 2098 | |
| 2099 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 2100 | sshbuf_len(c->extended)); |
| 2101 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2102 | if (len == -1 && (errno == EINTR || errno == EAGAIN || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2103 | errno == EWOULDBLOCK)) |
| 2104 | return 1; |
| 2105 | if (len <= 0) { |
| 2106 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2107 | channel_close_fd(ssh, &c->efd); |
| 2108 | } else { |
| 2109 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2110 | fatal("%s: channel %d: consume: %s", |
| 2111 | __func__, c->self, ssh_err(r)); |
| 2112 | } |
| 2113 | c->local_consumed += len; |
| 2114 | } |
| 2115 | return 1; |
| 2116 | } |
| 2117 | |
| 2118 | static int |
| 2119 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2120 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2121 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2122 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2123 | ssize_t len; |
Damien Miller | 03e92dd | 2019-02-08 14:50:36 +1100 | [diff] [blame] | 2124 | int r, force; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2125 | |
Damien Miller | 03e92dd | 2019-02-08 14:50:36 +1100 | [diff] [blame] | 2126 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
| 2127 | |
| 2128 | if (c->efd == -1 || (!force && !FD_ISSET(c->efd, readset))) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2129 | return 1; |
| 2130 | |
| 2131 | len = read(c->efd, buf, sizeof(buf)); |
| 2132 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2133 | if (len == -1 && (errno == EINTR || ((errno == EAGAIN || |
Damien Miller | 03e92dd | 2019-02-08 14:50:36 +1100 | [diff] [blame] | 2134 | errno == EWOULDBLOCK) && !force))) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2135 | return 1; |
| 2136 | if (len <= 0) { |
| 2137 | debug2("channel %d: closing read-efd %d", |
| 2138 | c->self, c->efd); |
| 2139 | channel_close_fd(ssh, &c->efd); |
| 2140 | } else { |
| 2141 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2142 | debug3("channel %d: discard efd", |
| 2143 | c->self); |
| 2144 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2145 | fatal("%s: channel %d: append: %s", |
| 2146 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | return 1; |
| 2150 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2151 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2152 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2153 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2154 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2155 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2156 | if (c->efd == -1) |
| 2157 | return 1; |
| 2158 | |
| 2159 | /** XXX handle drain efd, too */ |
| 2160 | |
| 2161 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2162 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2163 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2164 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2165 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2166 | |
| 2167 | return 1; |
| 2168 | } |
| 2169 | |
| 2170 | static int |
| 2171 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2172 | { |
| 2173 | int r; |
| 2174 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2175 | if (c->type == SSH_CHANNEL_OPEN && |
| 2176 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2177 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2178 | c->local_maxpacket*3) || |
| 2179 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2180 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2181 | if (!c->have_remote_id) |
| 2182 | fatal(":%s: channel %d: no remote id", |
| 2183 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2184 | if ((r = sshpkt_start(ssh, |
| 2185 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2186 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2187 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2188 | (r = sshpkt_send(ssh)) != 0) { |
| 2189 | fatal("%s: channel %i: %s", __func__, |
| 2190 | c->self, ssh_err(r)); |
| 2191 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2192 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2193 | c->self, c->local_window, |
| 2194 | c->local_consumed); |
| 2195 | c->local_window += c->local_consumed; |
| 2196 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2197 | } |
| 2198 | return 1; |
| 2199 | } |
| 2200 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2201 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2202 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2203 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2204 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2205 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2206 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2207 | channel_handle_efd(ssh, c, readset, writeset); |
| 2208 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2209 | } |
| 2210 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2211 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2212 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2213 | { |
| 2214 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2215 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2216 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2217 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2218 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2219 | if (sshbuf_len(c->input) < need) { |
| 2220 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2221 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2222 | if (len == -1 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2223 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2224 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2225 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2226 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2227 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2228 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2229 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2230 | fatal("%s: channel %d: append: %s", |
| 2231 | __func__, c->self, ssh_err(r)); |
| 2232 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2233 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2234 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2238 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2239 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2240 | { |
| 2241 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2242 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2243 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2244 | return; |
| 2245 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2246 | return; |
| 2247 | if (c->mux_pause) |
| 2248 | return; |
| 2249 | |
| 2250 | /* |
| 2251 | * Don't not read past the precise end of packets to |
| 2252 | * avoid disrupting fd passing. |
| 2253 | */ |
| 2254 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2255 | return; |
| 2256 | /* XXX sshbuf_peek_u32 */ |
| 2257 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2258 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2259 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2260 | debug2("channel %d: packet too big %u > %u", |
| 2261 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2262 | chan_rcvd_oclose(ssh, c); |
| 2263 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2264 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2265 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2266 | return; |
| 2267 | if (c->mux_rcb(ssh, c) != 0) { |
| 2268 | debug("channel %d: mux_rcb failed", c->self); |
| 2269 | chan_mark_dead(ssh, c); |
| 2270 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2275 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2276 | fd_set *readset, fd_set *writeset) |
| 2277 | { |
| 2278 | ssize_t len; |
| 2279 | int r; |
| 2280 | |
| 2281 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2282 | sshbuf_len(c->output) == 0) |
| 2283 | return; |
| 2284 | |
| 2285 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2286 | if (len == -1 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2287 | return; |
| 2288 | if (len <= 0) { |
| 2289 | chan_mark_dead(ssh, c); |
| 2290 | return; |
| 2291 | } |
| 2292 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2293 | fatal("%s: channel %d: consume: %s", __func__, |
| 2294 | c->self, ssh_err(r)); |
| 2295 | } |
| 2296 | |
| 2297 | static void |
| 2298 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2299 | fd_set *readset, fd_set *writeset) |
| 2300 | { |
| 2301 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2302 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2303 | } |
| 2304 | |
| 2305 | static void |
| 2306 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2307 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2308 | { |
| 2309 | Channel *nc; |
| 2310 | struct sockaddr_storage addr; |
| 2311 | socklen_t addrlen; |
| 2312 | int newsock; |
| 2313 | uid_t euid; |
| 2314 | gid_t egid; |
| 2315 | |
| 2316 | if (!FD_ISSET(c->sock, readset)) |
| 2317 | return; |
| 2318 | |
| 2319 | debug("multiplexing control connection"); |
| 2320 | |
| 2321 | /* |
| 2322 | * Accept connection on control socket |
| 2323 | */ |
| 2324 | memset(&addr, 0, sizeof(addr)); |
| 2325 | addrlen = sizeof(addr); |
| 2326 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2327 | &addrlen)) == -1) { |
| 2328 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2329 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2330 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2331 | return; |
| 2332 | } |
| 2333 | |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 2334 | if (getpeereid(newsock, &euid, &egid) == -1) { |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2335 | error("%s getpeereid failed: %s", __func__, |
| 2336 | strerror(errno)); |
| 2337 | close(newsock); |
| 2338 | return; |
| 2339 | } |
| 2340 | if ((euid != 0) && (getuid() != euid)) { |
| 2341 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2342 | (u_int)euid, (u_int)getuid()); |
| 2343 | close(newsock); |
| 2344 | return; |
| 2345 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2346 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2347 | newsock, newsock, -1, c->local_window_max, |
| 2348 | c->local_maxpacket, 0, "mux-control", 1); |
| 2349 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2350 | 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] | 2351 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2352 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2353 | /* mux state transitions must not elicit protocol messages */ |
| 2354 | nc->flags |= CHAN_LOCAL; |
| 2355 | } |
| 2356 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2357 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2358 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2359 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2360 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2361 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2362 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2363 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2364 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2365 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2366 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2367 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2368 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2369 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2370 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2371 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2372 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2373 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2374 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2375 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2376 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2377 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2378 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2379 | |
| 2380 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2381 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2382 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2383 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2384 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2385 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2386 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2387 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2388 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2389 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2390 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2391 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2392 | |
| 2393 | sc->channel_pre = pre; |
| 2394 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2395 | } |
| 2396 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2397 | /* gc dead channels */ |
| 2398 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2399 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2400 | { |
| 2401 | if (c == NULL) |
| 2402 | return; |
| 2403 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2404 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2405 | return; |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 2406 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2407 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2408 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2409 | /* if we still have a callback */ |
| 2410 | if (c->detach_user != NULL) |
| 2411 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2412 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2413 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2414 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2415 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2416 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2417 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2418 | } |
| 2419 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2420 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2421 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2422 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2423 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2424 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2425 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2426 | struct ssh_channels *sc = ssh->chanctxt; |
| 2427 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2428 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2429 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2430 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2431 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2432 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2433 | if (unpause_secs != NULL) |
| 2434 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2435 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2436 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2437 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2438 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2439 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2440 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2441 | c->delayed = 0; |
| 2442 | else |
| 2443 | continue; |
| 2444 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2445 | if (ftab[c->type] != NULL) { |
| 2446 | /* |
| 2447 | * Run handlers that are not paused. |
| 2448 | */ |
| 2449 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2450 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2451 | else if (unpause_secs != NULL) { |
| 2452 | /* |
| 2453 | * Collect the time that the earliest |
| 2454 | * channel comes off pause. |
| 2455 | */ |
| 2456 | debug3("%s: chan %d: skip for %d more seconds", |
| 2457 | __func__, c->self, |
| 2458 | (int)(c->notbefore - now)); |
| 2459 | if (*unpause_secs == 0 || |
| 2460 | (c->notbefore - now) < *unpause_secs) |
| 2461 | *unpause_secs = c->notbefore - now; |
| 2462 | } |
| 2463 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2464 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2465 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2466 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2467 | debug3("%s: first channel unpauses in %d seconds", |
| 2468 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2469 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2470 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2471 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2472 | * Create sockets before allocating the select bitmasks. |
| 2473 | * This is necessary for things that need to happen after reading |
| 2474 | * the network-input but before channel_prepare_select(). |
| 2475 | */ |
| 2476 | static void |
| 2477 | channel_before_prepare_select(struct ssh *ssh) |
| 2478 | { |
| 2479 | struct ssh_channels *sc = ssh->chanctxt; |
| 2480 | Channel *c; |
| 2481 | u_int i, oalloc; |
| 2482 | |
| 2483 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2484 | c = sc->channels[i]; |
| 2485 | if (c == NULL) |
| 2486 | continue; |
| 2487 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2488 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2493 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2494 | * select bitmasks. |
| 2495 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2496 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2497 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2498 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2499 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2500 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2501 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2502 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2503 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2504 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2505 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2506 | nfdset = howmany(n+1, NFDBITS); |
| 2507 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2508 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2509 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2510 | sz = nfdset * sizeof(fd_mask); |
| 2511 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2512 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2513 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2514 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2515 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2516 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2517 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2518 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2519 | memset(*readsetp, 0, sz); |
| 2520 | memset(*writesetp, 0, sz); |
| 2521 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2522 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2523 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2524 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2525 | } |
| 2526 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2527 | /* |
| 2528 | * After select, perform any appropriate operations for channels which have |
| 2529 | * events pending. |
| 2530 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2531 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2532 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2533 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2534 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2535 | } |
| 2536 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2537 | /* |
| 2538 | * Enqueue data for channels with open or draining c->input. |
| 2539 | */ |
| 2540 | static void |
| 2541 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2542 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2543 | size_t len, plen; |
| 2544 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2545 | int r; |
| 2546 | |
| 2547 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2548 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2549 | /* |
| 2550 | * input-buffer is empty and read-socket shutdown: |
| 2551 | * tell peer, that we will not send more data: |
| 2552 | * send IEOF. |
| 2553 | * hack for extended data: delay EOF if EFD still |
| 2554 | * in use. |
| 2555 | */ |
| 2556 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2557 | debug2("channel %d: " |
| 2558 | "ibuf_empty delayed efd %d/(%zu)", |
| 2559 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2560 | else |
| 2561 | chan_ibuf_empty(ssh, c); |
| 2562 | } |
| 2563 | return; |
| 2564 | } |
| 2565 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2566 | if (!c->have_remote_id) |
| 2567 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2568 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2569 | if (c->datagram) { |
| 2570 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2571 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2572 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2573 | c->self, ssh_err(r)); |
| 2574 | /* |
| 2575 | * XXX this does tail-drop on the datagram queue which is |
| 2576 | * usually suboptimal compared to head-drop. Better to have |
| 2577 | * backpressure at read time? (i.e. read + discard) |
| 2578 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2579 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2580 | debug("channel %d: datagram too big", c->self); |
| 2581 | return; |
| 2582 | } |
| 2583 | /* Enqueue it */ |
| 2584 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2585 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2586 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2587 | (r = sshpkt_send(ssh)) != 0) { |
| 2588 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2589 | c->self, ssh_err(r)); |
| 2590 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2591 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2592 | return; |
| 2593 | } |
| 2594 | |
| 2595 | /* Enqueue packet for buffered data. */ |
| 2596 | if (len > c->remote_window) |
| 2597 | len = c->remote_window; |
| 2598 | if (len > c->remote_maxpacket) |
| 2599 | len = c->remote_maxpacket; |
| 2600 | if (len == 0) |
| 2601 | return; |
| 2602 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2603 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2604 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2605 | (r = sshpkt_send(ssh)) != 0) { |
| 2606 | fatal("%s: channel %i: data: %s", __func__, |
| 2607 | c->self, ssh_err(r)); |
| 2608 | } |
| 2609 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2610 | fatal("%s: channel %i: consume: %s", __func__, |
| 2611 | c->self, ssh_err(r)); |
| 2612 | c->remote_window -= len; |
| 2613 | } |
| 2614 | |
| 2615 | /* |
| 2616 | * Enqueue data for channels with open c->extended in read mode. |
| 2617 | */ |
| 2618 | static void |
| 2619 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2620 | { |
| 2621 | size_t len; |
| 2622 | int r; |
| 2623 | |
| 2624 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2625 | return; |
| 2626 | |
| 2627 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2628 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2629 | if (len > c->remote_window) |
| 2630 | len = c->remote_window; |
| 2631 | if (len > c->remote_maxpacket) |
| 2632 | len = c->remote_maxpacket; |
| 2633 | if (len == 0) |
| 2634 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2635 | if (!c->have_remote_id) |
| 2636 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2637 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2638 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2639 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2640 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2641 | (r = sshpkt_send(ssh)) != 0) { |
| 2642 | fatal("%s: channel %i: data: %s", __func__, |
| 2643 | c->self, ssh_err(r)); |
| 2644 | } |
| 2645 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2646 | fatal("%s: channel %i: consume: %s", __func__, |
| 2647 | c->self, ssh_err(r)); |
| 2648 | c->remote_window -= len; |
| 2649 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2650 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2651 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2652 | /* 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] | 2653 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2654 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2655 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2656 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2657 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2658 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2659 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2660 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2661 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2662 | if (c == NULL) |
| 2663 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2664 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2665 | /* |
| 2666 | * We are only interested in channels that can have buffered |
| 2667 | * incoming data. |
| 2668 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2669 | if (c->type != SSH_CHANNEL_OPEN) |
| 2670 | continue; |
| 2671 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2672 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2673 | debug3("channel %d: will not send data after close", |
| 2674 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2675 | continue; |
| 2676 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2677 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2678 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2679 | if (c->istate == CHAN_INPUT_OPEN || |
| 2680 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2681 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2682 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2683 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2684 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2685 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2686 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2687 | } |
| 2688 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2689 | /* -- mux proxy support */ |
| 2690 | |
| 2691 | /* |
| 2692 | * When multiplexing channel messages for mux clients we have to deal |
| 2693 | * with downstream messages from the mux client and upstream messages |
| 2694 | * from the ssh server: |
| 2695 | * 1) Handling downstream messages is straightforward and happens |
| 2696 | * in channel_proxy_downstream(): |
| 2697 | * - We forward all messages (mostly) unmodified to the server. |
| 2698 | * - However, in order to route messages from upstream to the correct |
| 2699 | * downstream client, we have to replace the channel IDs used by the |
| 2700 | * mux clients with a unique channel ID because the mux clients might |
| 2701 | * use conflicting channel IDs. |
| 2702 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2703 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2704 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2705 | * with the newly allocated channel ID. |
| 2706 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2707 | * channels and processed by channel_proxy_upstream(). The local channel ID |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2708 | * is then translated back to the original mux client ID. |
| 2709 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2710 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2711 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2712 | * downstream mux client are removed. |
| 2713 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2714 | * requires more work, because they are not addressed to a specific |
| 2715 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2716 | * out whether the request is addressed to the local client or a |
| 2717 | * specific downstream client based on the listen-address/port. |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2718 | * 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] | 2719 | * not supported as the matching session/channel cannot be identified |
| 2720 | * easily. |
| 2721 | */ |
| 2722 | |
| 2723 | /* |
| 2724 | * receive packets from downstream mux clients: |
| 2725 | * channel callback fired on read from mux client, creates |
| 2726 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2727 | * on channel creation. |
| 2728 | */ |
| 2729 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2730 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2731 | { |
| 2732 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2733 | struct sshbuf *original = NULL, *modified = NULL; |
| 2734 | const u_char *cp; |
| 2735 | char *ctype = NULL, *listen_host = NULL; |
| 2736 | u_char type; |
| 2737 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2738 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2739 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2740 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2741 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2742 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2743 | != 0) { |
| 2744 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2745 | return -1; |
| 2746 | } |
| 2747 | if (have < 2) { |
| 2748 | error("%s: short message", __func__); |
| 2749 | return -1; |
| 2750 | } |
| 2751 | type = cp[1]; |
| 2752 | /* skip padlen + type */ |
| 2753 | cp += 2; |
| 2754 | have -= 2; |
| 2755 | if (ssh_packet_log_type(type)) |
| 2756 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2757 | downstream->self, type); |
| 2758 | |
| 2759 | switch (type) { |
| 2760 | case SSH2_MSG_CHANNEL_OPEN: |
| 2761 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2762 | (modified = sshbuf_new()) == NULL) { |
| 2763 | error("%s: alloc", __func__); |
| 2764 | goto out; |
| 2765 | } |
| 2766 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2767 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2768 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2769 | goto out; |
| 2770 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2771 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2772 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2773 | c->mux_ctx = downstream; /* point to mux client */ |
| 2774 | c->mux_downstream_id = id; /* original downstream id */ |
| 2775 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2776 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2777 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2778 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2779 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2780 | goto out; |
| 2781 | } |
| 2782 | break; |
| 2783 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2784 | /* |
| 2785 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2786 | * need to parse 'remote_id' instead of 'ctype'. |
| 2787 | */ |
| 2788 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2789 | (modified = sshbuf_new()) == NULL) { |
| 2790 | error("%s: alloc", __func__); |
| 2791 | goto out; |
| 2792 | } |
| 2793 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2794 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2795 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2796 | goto out; |
| 2797 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2798 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2799 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2800 | c->mux_ctx = downstream; /* point to mux client */ |
| 2801 | c->mux_downstream_id = id; |
| 2802 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2803 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2804 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2805 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2806 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2807 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2808 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2809 | goto out; |
| 2810 | } |
| 2811 | break; |
| 2812 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2813 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2814 | error("%s: alloc", __func__); |
| 2815 | goto out; |
| 2816 | } |
| 2817 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2818 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2819 | goto out; |
| 2820 | } |
| 2821 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2822 | error("%s: unsupported request %s", __func__, ctype); |
| 2823 | goto out; |
| 2824 | } |
| 2825 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2826 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2827 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2828 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2829 | goto out; |
| 2830 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2831 | if (listen_port > 65535) { |
| 2832 | error("%s: tcpip-forward for %s: bad port %u", |
| 2833 | __func__, listen_host, listen_port); |
| 2834 | goto out; |
| 2835 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2836 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 2837 | permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2838 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2839 | listen_host = NULL; |
| 2840 | break; |
| 2841 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2842 | if (have < 4) |
| 2843 | break; |
| 2844 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2845 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2846 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2847 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2848 | else |
| 2849 | c->flags |= CHAN_CLOSE_SENT; |
| 2850 | } |
| 2851 | break; |
| 2852 | } |
| 2853 | if (modified) { |
| 2854 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2855 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2856 | (r = sshpkt_send(ssh)) != 0) { |
| 2857 | error("%s: send %s", __func__, ssh_err(r)); |
| 2858 | goto out; |
| 2859 | } |
| 2860 | } else { |
| 2861 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2862 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2863 | (r = sshpkt_send(ssh)) != 0) { |
| 2864 | error("%s: send %s", __func__, ssh_err(r)); |
| 2865 | goto out; |
| 2866 | } |
| 2867 | } |
| 2868 | ret = 0; |
| 2869 | out: |
| 2870 | free(ctype); |
| 2871 | free(listen_host); |
| 2872 | sshbuf_free(original); |
| 2873 | sshbuf_free(modified); |
| 2874 | return ret; |
| 2875 | } |
| 2876 | |
| 2877 | /* |
| 2878 | * receive packets from upstream server and de-multiplex packets |
| 2879 | * to correct downstream: |
| 2880 | * implemented as a helper for channel input handlers, |
| 2881 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2882 | */ |
| 2883 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2884 | 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] | 2885 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2886 | struct sshbuf *b = NULL; |
| 2887 | Channel *downstream; |
| 2888 | const u_char *cp = NULL; |
| 2889 | size_t len; |
| 2890 | int r; |
| 2891 | |
| 2892 | /* |
| 2893 | * When receiving packets from the peer we need to check whether we |
| 2894 | * 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] | 2895 | * restore the original channel id and keep track of CLOSE messages, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2896 | * so we can cleanup the channel. |
| 2897 | */ |
| 2898 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2899 | return 0; |
| 2900 | if ((downstream = c->mux_ctx) == NULL) |
| 2901 | return 0; |
| 2902 | switch (type) { |
| 2903 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2904 | case SSH2_MSG_CHANNEL_DATA: |
| 2905 | case SSH2_MSG_CHANNEL_EOF: |
| 2906 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2907 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2908 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2909 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2910 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2911 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2912 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2913 | break; |
| 2914 | default: |
| 2915 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2916 | c->self, type); |
| 2917 | return 0; |
| 2918 | } |
| 2919 | if ((b = sshbuf_new()) == NULL) { |
| 2920 | error("%s: alloc reply", __func__); |
| 2921 | goto out; |
| 2922 | } |
| 2923 | /* get remaining payload (after id) */ |
| 2924 | cp = sshpkt_ptr(ssh, &len); |
| 2925 | if (cp == NULL) { |
| 2926 | error("%s: no packet", __func__); |
| 2927 | goto out; |
| 2928 | } |
| 2929 | /* translate id and send to muxclient */ |
| 2930 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2931 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2932 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2933 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2934 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2935 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2936 | goto out; |
| 2937 | } |
| 2938 | /* sshbuf_dump(b, stderr); */ |
| 2939 | if (ssh_packet_log_type(type)) |
| 2940 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2941 | type); |
| 2942 | out: |
| 2943 | /* update state */ |
| 2944 | switch (type) { |
| 2945 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2946 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2947 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2948 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2949 | c->have_remote_id = 1; |
| 2950 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2953 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2954 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2955 | else |
| 2956 | c->flags |= CHAN_CLOSE_RCVD; |
| 2957 | break; |
| 2958 | } |
| 2959 | sshbuf_free(b); |
| 2960 | return 1; |
| 2961 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2964 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2965 | /* Parse a channel ID from the current packet */ |
| 2966 | static int |
| 2967 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2968 | { |
| 2969 | u_int32_t id; |
| 2970 | int r; |
| 2971 | |
| 2972 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2973 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2974 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2975 | } |
| 2976 | if (id > INT_MAX) { |
| 2977 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2978 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2979 | } |
| 2980 | return (int)id; |
| 2981 | } |
| 2982 | |
| 2983 | /* Lookup a channel from an ID in the current packet */ |
| 2984 | static Channel * |
| 2985 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2986 | { |
| 2987 | int id = channel_parse_id(ssh, where, what); |
| 2988 | Channel *c; |
| 2989 | |
| 2990 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2991 | ssh_packet_disconnect(ssh, |
| 2992 | "%s packet referred to nonexistent channel %d", what, id); |
| 2993 | } |
| 2994 | return c; |
| 2995 | } |
| 2996 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2997 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2998 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2999 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 3000 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3001 | size_t data_len, win_len; |
| 3002 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 3003 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3004 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3005 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3006 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3007 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3008 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3009 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3010 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 3011 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3012 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3013 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3014 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3015 | /* Get the data. */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3016 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 || |
| 3017 | (r = sshpkt_get_end(ssh)) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3018 | fatal("%s: channel %d: get data: %s", __func__, |
| 3019 | c->self, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3020 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 3021 | win_len = data_len; |
| 3022 | if (c->datagram) |
| 3023 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3024 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3025 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3026 | * The sending side reduces its window as it sends data, so we |
| 3027 | * must 'fake' consumption of the data in order to ensure that window |
| 3028 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3029 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3030 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 3031 | c->local_window -= win_len; |
| 3032 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3033 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3034 | } |
| 3035 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3036 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3037 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3038 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3039 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3040 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3041 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3042 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3043 | c->self, win_len, c->local_window); |
| 3044 | return 0; |
| 3045 | } |
| 3046 | c->local_window -= win_len; |
| 3047 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3048 | if (c->datagram) { |
| 3049 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 3050 | fatal("%s: channel %d: append datagram: %s", |
| 3051 | __func__, c->self, ssh_err(r)); |
| 3052 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 3053 | fatal("%s: channel %d: append data: %s", |
| 3054 | __func__, c->self, ssh_err(r)); |
| 3055 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3056 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3057 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3058 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3059 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3060 | 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] | 3061 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3062 | const u_char *data; |
| 3063 | size_t data_len; |
| 3064 | u_int32_t tcode; |
| 3065 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 3066 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3067 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3068 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3069 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3070 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3071 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3072 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3073 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3074 | if (c->flags & CHAN_EOF_RCVD) { |
| 3075 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3076 | debug("channel %d: accepting ext data after eof", |
| 3077 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3078 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3079 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 3080 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3081 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3082 | |
| 3083 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 3084 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 3085 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3086 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3087 | if (c->efd == -1 || |
| 3088 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 3089 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 3090 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3091 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3092 | } |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3093 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 || |
| 3094 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3095 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3096 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3097 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3098 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3099 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3100 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3101 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3102 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3103 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3104 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3105 | /* XXX sshpkt_getb? */ |
| 3106 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3107 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3108 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3109 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3110 | } |
| 3111 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3112 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3113 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3114 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3115 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3116 | int r; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3117 | |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3118 | if ((r = sshpkt_get_end(ssh)) != 0) { |
| 3119 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3120 | ssh_packet_disconnect(ssh, "Invalid ieof message"); |
| 3121 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3122 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3123 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3124 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3125 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3126 | |
| 3127 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3128 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3129 | debug("channel %d: FORCE input drain", c->self); |
| 3130 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3131 | if (sshbuf_len(c->input) == 0) |
| 3132 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3133 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3134 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3135 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3136 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3137 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3138 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3139 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3140 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3141 | int r; |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3142 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3143 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3144 | return 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3145 | if ((r = sshpkt_get_end(ssh)) != 0) { |
| 3146 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3147 | ssh_packet_disconnect(ssh, "Invalid oclose message"); |
| 3148 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3149 | chan_rcvd_oclose(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3150 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3151 | } |
| 3152 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3153 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3154 | 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] | 3155 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3156 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3157 | u_int32_t remote_window, remote_maxpacket; |
| 3158 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3159 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3160 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3161 | return 0; |
| 3162 | if (c->type != SSH_CHANNEL_OPENING) |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3163 | ssh_packet_disconnect(ssh, "Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3164 | "non-opening channel %d.", c->self); |
| 3165 | /* |
| 3166 | * Record the remote channel number and mark that the channel |
| 3167 | * is now open. |
| 3168 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3169 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3170 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3171 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 || |
| 3172 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3173 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3174 | ssh_packet_disconnect(ssh, "Invalid open confirmation message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3175 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3176 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3177 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3178 | c->remote_window = remote_window; |
| 3179 | c->remote_maxpacket = remote_maxpacket; |
| 3180 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3181 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3182 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3183 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3184 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3185 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3186 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3187 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3188 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3189 | } |
| 3190 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3191 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3192 | reason2txt(int reason) |
| 3193 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3194 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3195 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3196 | return "administratively prohibited"; |
| 3197 | case SSH2_OPEN_CONNECT_FAILED: |
| 3198 | return "connect failed"; |
| 3199 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3200 | return "unknown channel type"; |
| 3201 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3202 | return "resource shortage"; |
| 3203 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3204 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3207 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3208 | 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] | 3209 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3210 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3211 | u_int32_t reason; |
| 3212 | char *msg = NULL; |
| 3213 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3214 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3215 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3216 | return 0; |
| 3217 | if (c->type != SSH_CHANNEL_OPENING) |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3218 | ssh_packet_disconnect(ssh, "Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3219 | "non-opening channel %d.", c->self); |
| 3220 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3221 | error("%s: reason: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3222 | ssh_packet_disconnect(ssh, "Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3223 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3224 | /* skip language */ |
| 3225 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3226 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 || |
| 3227 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3228 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3229 | ssh_packet_disconnect(ssh, "Invalid open failure message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3230 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3231 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3232 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3233 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3234 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3235 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3236 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3237 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3238 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3239 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3240 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3241 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3242 | } |
| 3243 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3244 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3245 | 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] | 3246 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3247 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3248 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3249 | u_int32_t adjust; |
| 3250 | u_int new_rwin; |
| 3251 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3252 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3253 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3254 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3255 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3256 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3257 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3258 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3259 | return 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3260 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 || |
| 3261 | (r = sshpkt_get_end(ssh)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3262 | error("%s: adjust: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3263 | ssh_packet_disconnect(ssh, "Invalid window adjust message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3264 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3265 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3266 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3267 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3268 | c->self, adjust, c->remote_window); |
| 3269 | } |
| 3270 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3271 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3272 | } |
| 3273 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3274 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3275 | 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] | 3276 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3277 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3278 | Channel *c; |
| 3279 | struct channel_confirm *cc; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3280 | |
| 3281 | /* Reset keepalive timeout */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3282 | ssh_packet_set_alive_timeouts(ssh, 0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3283 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3284 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3285 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3286 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3287 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3288 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3289 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3290 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3291 | return 0; |
dtucker@openbsd.org | 696fb42 | 2019-07-07 01:05:00 +0000 | [diff] [blame] | 3292 | if (sshpkt_get_end(ssh) != 0) |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3293 | ssh_packet_disconnect(ssh, "Invalid status confirm message"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3294 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3295 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3296 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3297 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3298 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3299 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3300 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3301 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3302 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3303 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3304 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3305 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3306 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3307 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3308 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3311 | |
| 3312 | /* |
| 3313 | * Determine whether or not a port forward listens to loopback, the |
| 3314 | * specified address or wildcard. On the client, a specified bind |
| 3315 | * address will always override gateway_ports. On the server, a |
| 3316 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3317 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3318 | * will bind to whatever address the client asked for. |
| 3319 | * |
| 3320 | * Special-case listen_addrs are: |
| 3321 | * |
| 3322 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3323 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3324 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3325 | * "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] | 3326 | */ |
| 3327 | static const char * |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3328 | channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3329 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3330 | { |
| 3331 | const char *addr = NULL; |
| 3332 | int wildcard = 0; |
| 3333 | |
| 3334 | if (listen_addr == NULL) { |
| 3335 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3336 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3337 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3338 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3339 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3340 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3341 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3342 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3343 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3344 | /* |
| 3345 | * Notify client if they requested a specific listen |
| 3346 | * address and it was overridden. |
| 3347 | */ |
| 3348 | if (*listen_addr != '\0' && |
| 3349 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3350 | strcmp(listen_addr, "*") != 0) { |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3351 | ssh_packet_send_debug(ssh, |
| 3352 | "Forwarding listen address " |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3353 | "\"%s\" overridden by server " |
| 3354 | "GatewayPorts", listen_addr); |
| 3355 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3356 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3357 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3358 | strcmp(listen_addr, "::1") == 0) { |
| 3359 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3360 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3361 | } |
| 3362 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3363 | strcmp(listen_addr, "::1") == 0) { |
| 3364 | /* |
| 3365 | * If a specific IPv4/IPv6 localhost address has been |
| 3366 | * requested then accept it even if gateway_ports is in |
| 3367 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3368 | */ |
| 3369 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3370 | } |
| 3371 | if (wildcardp != NULL) |
| 3372 | *wildcardp = wildcard; |
| 3373 | return addr; |
| 3374 | } |
| 3375 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3376 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3377 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3378 | struct Forward *fwd, int *allocated_listen_port, |
| 3379 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3380 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3381 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3382 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3383 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3384 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3385 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3386 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3387 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3388 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3389 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3390 | if (is_client && fwd->connect_path != NULL) { |
| 3391 | host = fwd->connect_path; |
| 3392 | } else { |
| 3393 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3394 | fwd->listen_host : fwd->connect_host; |
| 3395 | if (host == NULL) { |
| 3396 | error("No forward host name."); |
| 3397 | return 0; |
| 3398 | } |
| 3399 | if (strlen(host) >= NI_MAXHOST) { |
| 3400 | error("Forward host name too long."); |
| 3401 | return 0; |
| 3402 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3403 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3404 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3405 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3406 | addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3407 | is_client, fwd_opts); |
| 3408 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3409 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3410 | |
| 3411 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3412 | * getaddrinfo returns a loopback address if the hostname is |
| 3413 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3414 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3415 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3416 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3417 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3418 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3419 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3420 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3421 | if (addr == NULL) { |
| 3422 | /* This really shouldn't happen */ |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3423 | ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3424 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3425 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3426 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3427 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3428 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3429 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3430 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3431 | if (allocated_listen_port != NULL) |
| 3432 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3433 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3434 | switch (ai->ai_family) { |
| 3435 | case AF_INET: |
| 3436 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3437 | sin_port; |
| 3438 | break; |
| 3439 | case AF_INET6: |
| 3440 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3441 | sin6_port; |
| 3442 | break; |
| 3443 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3444 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3445 | } |
| 3446 | /* |
| 3447 | * If allocating a port for -R forwards, then use the |
| 3448 | * same port for all address families. |
| 3449 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3450 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3451 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3452 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3453 | *lport_p = htons(*allocated_listen_port); |
| 3454 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3455 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3456 | strport, sizeof(strport), |
| 3457 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3458 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3459 | continue; |
| 3460 | } |
| 3461 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3462 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 3463 | if (sock == -1) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3464 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3465 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3466 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3467 | continue; |
| 3468 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3469 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3470 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3471 | if (ai->ai_family == AF_INET6) |
| 3472 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3473 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3474 | debug("Local forwarding listening on %s port %s.", |
| 3475 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3476 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3477 | /* Bind the socket to the address. */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 3478 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3479 | /* |
| 3480 | * address can be in if use ipv6 address is |
| 3481 | * already bound |
| 3482 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3483 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3484 | error("bind [%s]:%s: %.100s", |
| 3485 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3486 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3487 | verbose("bind [%s]:%s: %.100s", |
| 3488 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3489 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3490 | close(sock); |
| 3491 | continue; |
| 3492 | } |
| 3493 | /* Start listening for connections on the socket. */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 3494 | if (listen(sock, SSH_LISTEN_BACKLOG) == -1) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3495 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3496 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3497 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3498 | close(sock); |
| 3499 | continue; |
| 3500 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3501 | |
| 3502 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3503 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3504 | * record what we got. |
| 3505 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3506 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3507 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3508 | allocated_listen_port != NULL && |
| 3509 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3510 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3511 | debug("Allocated listen port %d", |
| 3512 | *allocated_listen_port); |
| 3513 | } |
| 3514 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3515 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3516 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3517 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3518 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3519 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3520 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3521 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3522 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3523 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3524 | c->listening_port = *allocated_listen_port; |
| 3525 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3526 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3527 | success = 1; |
| 3528 | } |
| 3529 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3530 | error("%s: cannot listen to port: %d", __func__, |
| 3531 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3532 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3533 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3534 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3535 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3536 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3537 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3538 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3539 | { |
| 3540 | struct sockaddr_un sunaddr; |
| 3541 | const char *path; |
| 3542 | Channel *c; |
| 3543 | int port, sock; |
| 3544 | mode_t omask; |
| 3545 | |
| 3546 | switch (type) { |
| 3547 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3548 | if (fwd->connect_path != NULL) { |
| 3549 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3550 | error("Local connecting path too long: %s", |
| 3551 | fwd->connect_path); |
| 3552 | return 0; |
| 3553 | } |
| 3554 | path = fwd->connect_path; |
| 3555 | port = PORT_STREAMLOCAL; |
| 3556 | } else { |
| 3557 | if (fwd->connect_host == NULL) { |
| 3558 | error("No forward host name."); |
| 3559 | return 0; |
| 3560 | } |
| 3561 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3562 | error("Forward host name too long."); |
| 3563 | return 0; |
| 3564 | } |
| 3565 | path = fwd->connect_host; |
| 3566 | port = fwd->connect_port; |
| 3567 | } |
| 3568 | break; |
| 3569 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3570 | path = fwd->listen_path; |
| 3571 | port = PORT_STREAMLOCAL; |
| 3572 | break; |
| 3573 | default: |
| 3574 | error("%s: unexpected channel type %d", __func__, type); |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | if (fwd->listen_path == NULL) { |
| 3579 | error("No forward path name."); |
| 3580 | return 0; |
| 3581 | } |
| 3582 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3583 | error("Local listening path too long: %s", fwd->listen_path); |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3588 | |
| 3589 | /* Start a Unix domain listener. */ |
| 3590 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3591 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3592 | fwd_opts->streamlocal_bind_unlink); |
| 3593 | umask(omask); |
| 3594 | if (sock < 0) |
| 3595 | return 0; |
| 3596 | |
| 3597 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3598 | |
| 3599 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3600 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3601 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3602 | 0, "unix listener", 1); |
| 3603 | c->path = xstrdup(path); |
| 3604 | c->host_port = port; |
| 3605 | c->listening_port = PORT_STREAMLOCAL; |
| 3606 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3607 | return 1; |
| 3608 | } |
| 3609 | |
| 3610 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3611 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3612 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3613 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3614 | u_int i; |
| 3615 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3616 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3617 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3618 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3619 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3620 | continue; |
| 3621 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3622 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3623 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3624 | found = 1; |
| 3625 | } |
| 3626 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3627 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3628 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3629 | } |
| 3630 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3631 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3632 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3633 | { |
| 3634 | u_int i; |
| 3635 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3636 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3637 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3638 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3639 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3640 | continue; |
| 3641 | if (c->path == NULL) |
| 3642 | continue; |
| 3643 | if (strcmp(c->path, path) == 0) { |
| 3644 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3645 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3646 | found = 1; |
| 3647 | } |
| 3648 | } |
| 3649 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3650 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3654 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3655 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3656 | if (fwd->listen_path != NULL) { |
| 3657 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3658 | fwd->listen_path); |
| 3659 | } else { |
| 3660 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3661 | fwd->listen_host, fwd->listen_port); |
| 3662 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3666 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3667 | const char *lhost, u_short lport, int cport, |
| 3668 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3669 | { |
| 3670 | u_int i; |
| 3671 | int found = 0; |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3672 | const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3673 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3674 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3675 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3676 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3677 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3678 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3679 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3680 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3681 | /* skip dynamic forwardings */ |
| 3682 | if (c->host_port == 0) |
| 3683 | continue; |
| 3684 | } else { |
| 3685 | if (c->host_port != cport) |
| 3686 | continue; |
| 3687 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3688 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3689 | (c->listening_addr != NULL && addr == NULL)) |
| 3690 | continue; |
| 3691 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3692 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3693 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3694 | found = 1; |
| 3695 | } |
| 3696 | } |
| 3697 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3698 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3699 | } |
| 3700 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3701 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3702 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3703 | { |
| 3704 | u_int i; |
| 3705 | int found = 0; |
| 3706 | |
| 3707 | if (path == NULL) { |
| 3708 | error("%s: no path specified.", __func__); |
| 3709 | return 0; |
| 3710 | } |
| 3711 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3712 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3713 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3714 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3715 | continue; |
| 3716 | if (c->listening_addr == NULL) |
| 3717 | continue; |
| 3718 | if (strcmp(c->listening_addr, path) == 0) { |
| 3719 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3720 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3721 | found = 1; |
| 3722 | } |
| 3723 | } |
| 3724 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3725 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3729 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3730 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3731 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3732 | if (fwd->listen_path != NULL) { |
| 3733 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3734 | fwd->listen_path); |
| 3735 | } else { |
| 3736 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3737 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3738 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3739 | } |
| 3740 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3741 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3742 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3743 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3744 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3745 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3746 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3747 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3748 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3749 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3750 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3751 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3752 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3753 | } |
| 3754 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3755 | /* Matches a remote forwarding permission against a requested forwarding */ |
| 3756 | static int |
| 3757 | remote_open_match(struct permission *allowed_open, struct Forward *fwd) |
| 3758 | { |
| 3759 | int ret; |
| 3760 | char *lhost; |
| 3761 | |
| 3762 | /* XXX add ACLs for streamlocal */ |
| 3763 | if (fwd->listen_path != NULL) |
| 3764 | return 1; |
| 3765 | |
| 3766 | if (fwd->listen_host == NULL || allowed_open->listen_host == NULL) |
| 3767 | return 0; |
| 3768 | |
| 3769 | if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT && |
| 3770 | allowed_open->listen_port != fwd->listen_port) |
| 3771 | return 0; |
| 3772 | |
| 3773 | /* Match hostnames case-insensitively */ |
| 3774 | lhost = xstrdup(fwd->listen_host); |
| 3775 | lowercase(lhost); |
| 3776 | ret = match_pattern(lhost, allowed_open->listen_host); |
| 3777 | free(lhost); |
| 3778 | |
| 3779 | return ret; |
| 3780 | } |
| 3781 | |
| 3782 | /* Checks whether a requested remote forwarding is permitted */ |
| 3783 | static int |
| 3784 | check_rfwd_permission(struct ssh *ssh, struct Forward *fwd) |
| 3785 | { |
| 3786 | struct ssh_channels *sc = ssh->chanctxt; |
| 3787 | struct permission_set *pset = &sc->remote_perms; |
| 3788 | u_int i, permit, permit_adm = 1; |
| 3789 | struct permission *perm; |
| 3790 | |
| 3791 | /* XXX apply GatewayPorts override before checking? */ |
| 3792 | |
| 3793 | permit = pset->all_permitted; |
| 3794 | if (!permit) { |
| 3795 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3796 | perm = &pset->permitted_user[i]; |
| 3797 | if (remote_open_match(perm, fwd)) { |
| 3798 | permit = 1; |
| 3799 | break; |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | if (pset->num_permitted_admin > 0) { |
| 3805 | permit_adm = 0; |
| 3806 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 3807 | perm = &pset->permitted_admin[i]; |
| 3808 | if (remote_open_match(perm, fwd)) { |
| 3809 | permit_adm = 1; |
| 3810 | break; |
| 3811 | } |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | return permit && permit_adm; |
| 3816 | } |
| 3817 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3818 | /* protocol v2 remote port fwd, used by sshd */ |
| 3819 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3820 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3821 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3822 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3823 | if (!check_rfwd_permission(ssh, fwd)) { |
djm@openbsd.org | 8cc7a67 | 2019-01-19 21:37:13 +0000 | [diff] [blame] | 3824 | ssh_packet_send_debug(ssh, "port forwarding refused"); |
florian@openbsd.org | cb4accb | 2019-05-10 18:55:17 +0000 | [diff] [blame] | 3825 | if (fwd->listen_path != NULL) |
| 3826 | /* XXX always allowed, see remote_open_match() */ |
| 3827 | logit("Received request from %.100s port %d to " |
| 3828 | "remote forward to path \"%.100s\", " |
| 3829 | "but the request was denied.", |
| 3830 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
| 3831 | fwd->listen_path); |
| 3832 | else if(fwd->listen_host != NULL) |
| 3833 | logit("Received request from %.100s port %d to " |
| 3834 | "remote forward to host %.100s port %d, " |
| 3835 | "but the request was denied.", |
| 3836 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), |
| 3837 | fwd->listen_host, fwd->listen_port ); |
| 3838 | else |
| 3839 | logit("Received request from %.100s port %d to remote " |
| 3840 | "forward, but the request was denied.", |
| 3841 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3842 | return 0; |
| 3843 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3844 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3845 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3846 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3847 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3848 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3849 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3850 | fwd_opts); |
| 3851 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3852 | } |
| 3853 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3854 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3855 | * Translate the requested rfwd listen host to something usable for |
| 3856 | * this server. |
| 3857 | */ |
| 3858 | static const char * |
| 3859 | channel_rfwd_bind_host(const char *listen_host) |
| 3860 | { |
| 3861 | if (listen_host == NULL) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3862 | return "localhost"; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3863 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3864 | return ""; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3865 | } else |
| 3866 | return listen_host; |
| 3867 | } |
| 3868 | |
| 3869 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3870 | * Initiate forwarding of connections to port "port" on remote host through |
| 3871 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3872 | * Returns handle (index) for updating the dynamic listen port with |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3873 | * channel_update_permission(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3874 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3875 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3876 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3877 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3878 | int r, success = 0, idx = -1; |
| 3879 | char *host_to_connect, *listen_host, *listen_path; |
| 3880 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3881 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3882 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3883 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3884 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3885 | (r = sshpkt_put_cstring(ssh, |
| 3886 | "streamlocal-forward@openssh.com")) != 0 || |
| 3887 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3888 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3889 | (r = sshpkt_send(ssh)) != 0 || |
| 3890 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3891 | fatal("%s: request streamlocal: %s", |
| 3892 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3893 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3894 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3895 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3896 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3897 | (r = sshpkt_put_cstring(ssh, |
| 3898 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3899 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3900 | (r = sshpkt_send(ssh)) != 0 || |
| 3901 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3902 | fatal("%s: request tcpip-forward: %s", |
| 3903 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3904 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3905 | /* Assume that server accepts the request */ |
| 3906 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3907 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3908 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3909 | host_to_connect = listen_host = listen_path = NULL; |
| 3910 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3911 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3912 | host_to_connect = xstrdup(fwd->connect_path); |
| 3913 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3914 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3915 | host_to_connect = xstrdup(fwd->connect_host); |
| 3916 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3917 | } |
| 3918 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3919 | listen_path = xstrdup(fwd->listen_path); |
| 3920 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3921 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3922 | if (fwd->listen_host != NULL) |
| 3923 | listen_host = xstrdup(fwd->listen_host); |
| 3924 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3925 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3926 | idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3927 | host_to_connect, port_to_connect, |
| 3928 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3929 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3930 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3931 | } |
| 3932 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3933 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3934 | open_match(struct permission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3935 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3936 | { |
| 3937 | if (allowed_open->host_to_connect == NULL) |
| 3938 | return 0; |
| 3939 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3940 | allowed_open->port_to_connect != requestedport) |
| 3941 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3942 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3943 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3944 | return 0; |
| 3945 | return 1; |
| 3946 | } |
| 3947 | |
| 3948 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3949 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3950 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3951 | * need to translate between the configured-host (listen_host) |
| 3952 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3953 | */ |
| 3954 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3955 | open_listen_match_tcpip(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3956 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3957 | { |
| 3958 | const char *allowed_host; |
| 3959 | |
| 3960 | if (allowed_open->host_to_connect == NULL) |
| 3961 | return 0; |
| 3962 | if (allowed_open->listen_port != requestedport) |
| 3963 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3964 | if (!translate && allowed_open->listen_host == NULL && |
| 3965 | requestedhost == NULL) |
| 3966 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3967 | allowed_host = translate ? |
| 3968 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3969 | allowed_open->listen_host; |
djm@openbsd.org | 90e51d6 | 2018-06-25 22:28:33 +0000 | [diff] [blame] | 3970 | if (allowed_host == NULL || requestedhost == NULL || |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3971 | strcmp(allowed_host, requestedhost) != 0) |
| 3972 | return 0; |
| 3973 | return 1; |
| 3974 | } |
| 3975 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3976 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3977 | open_listen_match_streamlocal(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3978 | const char *requestedpath) |
| 3979 | { |
| 3980 | if (allowed_open->host_to_connect == NULL) |
| 3981 | return 0; |
| 3982 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3983 | return 0; |
| 3984 | if (allowed_open->listen_path == NULL || |
| 3985 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3986 | return 0; |
| 3987 | return 1; |
| 3988 | } |
| 3989 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3990 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3991 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3992 | * local side. |
| 3993 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3994 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3995 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3996 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3997 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3998 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3999 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4000 | int r; |
| 4001 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4002 | struct permission *perm; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4003 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4004 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4005 | perm = &pset->permitted_user[i]; |
| 4006 | if (open_listen_match_tcpip(perm, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4007 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4008 | perm = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4009 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4010 | if (perm == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4011 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 4012 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4013 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4014 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 4015 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 4016 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 4017 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 4018 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 4019 | (r = sshpkt_send(ssh)) != 0) |
| 4020 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4021 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4022 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 4023 | |
| 4024 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4028 | * Request cancellation of remote forwarding of Unix domain socket |
| 4029 | * path from local side. |
| 4030 | */ |
| 4031 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4032 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4033 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4034 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4035 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4036 | int r; |
| 4037 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4038 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4039 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4040 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4041 | perm = &pset->permitted_user[i]; |
| 4042 | if (open_listen_match_streamlocal(perm, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4043 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4044 | perm = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4045 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4046 | if (perm == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4047 | debug("%s: requested forward not found", __func__); |
| 4048 | return -1; |
| 4049 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4050 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 4051 | (r = sshpkt_put_cstring(ssh, |
| 4052 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 4053 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 4054 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 4055 | (r = sshpkt_send(ssh)) != 0) |
| 4056 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4057 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4058 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4059 | |
| 4060 | return 0; |
| 4061 | } |
| 4062 | |
| 4063 | /* |
| 4064 | * Request cancellation of remote forwarding of a connection from local side. |
| 4065 | */ |
| 4066 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4067 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4068 | { |
| 4069 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4070 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 4071 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4072 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4073 | return channel_request_rforward_cancel_tcpip(ssh, |
| 4074 | fwd->listen_host, |
| 4075 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | /* |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4080 | * 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] | 4081 | * usually called by the server, because the user could connect to any port |
| 4082 | * anyway, and the server has no way to know but to trust the client anyway. |
| 4083 | */ |
| 4084 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4085 | channel_permit_all(struct ssh *ssh, int where) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4086 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4087 | struct permission_set *pset = permission_set_get(ssh, where); |
| 4088 | |
| 4089 | if (pset->num_permitted_user == 0) |
| 4090 | pset->all_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4093 | /* |
| 4094 | * Permit the specified host/port for forwarding. |
| 4095 | */ |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 4096 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4097 | channel_add_permission(struct ssh *ssh, int who, int where, |
| 4098 | char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4099 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4100 | int local = where == FORWARD_LOCAL; |
| 4101 | struct permission_set *pset = permission_set_get(ssh, where); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4102 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4103 | debug("allow %s forwarding to host %s port %d", |
| 4104 | fwd_ident(who, where), host, port); |
| 4105 | /* |
| 4106 | * Remote forwards set listen_host/port, local forwards set |
| 4107 | * host/port_to_connect. |
| 4108 | */ |
| 4109 | permission_set_add(ssh, who, where, |
| 4110 | local ? host : 0, local ? port : 0, |
| 4111 | local ? NULL : host, NULL, local ? 0 : port, NULL); |
| 4112 | pset->all_permitted = 0; |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Administratively disable forwarding. |
| 4117 | */ |
| 4118 | void |
| 4119 | channel_disable_admin(struct ssh *ssh, int where) |
| 4120 | { |
| 4121 | channel_clear_permission(ssh, FORWARD_ADM, where); |
| 4122 | permission_set_add(ssh, FORWARD_ADM, where, |
| 4123 | NULL, 0, NULL, NULL, 0, NULL); |
| 4124 | } |
| 4125 | |
| 4126 | /* |
| 4127 | * Clear a list of permitted opens. |
| 4128 | */ |
| 4129 | void |
| 4130 | channel_clear_permission(struct ssh *ssh, int who, int where) |
| 4131 | { |
| 4132 | struct permission **permp; |
| 4133 | u_int *npermp; |
| 4134 | |
| 4135 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
| 4136 | *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp)); |
| 4137 | *npermp = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4140 | /* |
| 4141 | * Update the listen port for a dynamic remote forward, after |
| 4142 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 4143 | * passed then they entry will be invalidated. |
| 4144 | */ |
| 4145 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4146 | channel_update_permission(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4147 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4148 | struct permission_set *pset = &ssh->chanctxt->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4149 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4150 | if (idx < 0 || (u_int)idx >= pset->num_permitted_user) { |
| 4151 | debug("%s: index out of range: %d num_permitted_user %d", |
| 4152 | __func__, idx, pset->num_permitted_user); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4153 | return; |
| 4154 | } |
| 4155 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 4156 | newport > 0 ? "Updating" : "Removing", |
| 4157 | newport, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4158 | pset->permitted_user[idx].host_to_connect, |
| 4159 | pset->permitted_user[idx].port_to_connect); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4160 | if (newport <= 0) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4161 | fwd_perm_clear(&pset->permitted_user[idx]); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4162 | else { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4163 | pset->permitted_user[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4164 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4165 | } |
| 4166 | } |
| 4167 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 4168 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 4169 | int |
| 4170 | permitopen_port(const char *p) |
| 4171 | { |
| 4172 | int port; |
| 4173 | |
| 4174 | if (strcmp(p, "*") == 0) |
| 4175 | return FWD_PERMIT_ANY_PORT; |
| 4176 | if ((port = a2port(p)) > 0) |
| 4177 | return port; |
| 4178 | return -1; |
| 4179 | } |
| 4180 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4181 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4182 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4183 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4184 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4185 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4186 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4187 | char ntop[NI_MAXHOST]; |
| 4188 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4189 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4190 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4191 | switch (cctx->ai->ai_family) { |
| 4192 | case AF_UNIX: |
| 4193 | /* unix:pathname instead of host:port */ |
| 4194 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 4195 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 4196 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 4197 | break; |
| 4198 | case AF_INET: |
| 4199 | case AF_INET6: |
| 4200 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 4201 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 4202 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 4203 | error("connect_next: getnameinfo failed"); |
| 4204 | continue; |
| 4205 | } |
| 4206 | break; |
| 4207 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4208 | continue; |
| 4209 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4210 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4211 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4212 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4213 | error("socket: %.100s", strerror(errno)); |
| 4214 | else |
| 4215 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4216 | continue; |
| 4217 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4218 | if (set_nonblock(sock) == -1) |
| 4219 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4220 | if (connect(sock, cctx->ai->ai_addr, |
| 4221 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4222 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4223 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4224 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4225 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4226 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4227 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4228 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4229 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4230 | if (cctx->ai->ai_family != AF_UNIX) |
| 4231 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4232 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4233 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4234 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4235 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4236 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4237 | return -1; |
| 4238 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4239 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4240 | static void |
| 4241 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4242 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4243 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4244 | if (cctx->aitop) { |
| 4245 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4246 | free(cctx->aitop); |
| 4247 | else |
| 4248 | freeaddrinfo(cctx->aitop); |
| 4249 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4250 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4251 | } |
| 4252 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4253 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4254 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4255 | * passing back the failure reason if appropriate. |
| 4256 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4257 | static int |
| 4258 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4259 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4260 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4261 | { |
| 4262 | struct addrinfo hints; |
| 4263 | int gaierr; |
| 4264 | int sock = -1; |
| 4265 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4266 | |
| 4267 | if (port == PORT_STREAMLOCAL) { |
| 4268 | struct sockaddr_un *sunaddr; |
| 4269 | struct addrinfo *ai; |
| 4270 | |
| 4271 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4272 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4273 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4278 | * channel_connect_ctx_free() must check ai_family |
| 4279 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4280 | */ |
| 4281 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4282 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4283 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4284 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4285 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4286 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4287 | ai->ai_protocol = PF_UNSPEC; |
| 4288 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4289 | sunaddr->sun_family = AF_UNIX; |
| 4290 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4291 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4292 | } else { |
| 4293 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4294 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4295 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4296 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4297 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4298 | != 0) { |
| 4299 | if (errmsg != NULL) |
| 4300 | *errmsg = ssh_gai_strerror(gaierr); |
| 4301 | if (reason != NULL) |
| 4302 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4303 | error("connect_to %.100s: unknown host (%s)", name, |
| 4304 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4305 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4306 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4307 | } |
| 4308 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4309 | cctx->host = xstrdup(name); |
| 4310 | cctx->port = port; |
| 4311 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4312 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4313 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4314 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4315 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4316 | return -1; |
| 4317 | } |
| 4318 | |
| 4319 | return sock; |
| 4320 | } |
| 4321 | |
| 4322 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4323 | static Channel * |
| 4324 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4325 | char *ctype, char *rname) |
| 4326 | { |
| 4327 | struct channel_connect cctx; |
| 4328 | Channel *c; |
| 4329 | int sock; |
| 4330 | |
| 4331 | memset(&cctx, 0, sizeof(cctx)); |
| 4332 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4333 | &cctx, NULL, NULL); |
| 4334 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4335 | channel_connect_ctx_free(&cctx); |
| 4336 | return NULL; |
| 4337 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4338 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4339 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4340 | c->host_port = port; |
| 4341 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4342 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4343 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4344 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4345 | } |
| 4346 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4347 | /* |
| 4348 | * returns either the newly connected channel or the downstream channel |
| 4349 | * that needs to deal with this connection. |
| 4350 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4351 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4352 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4353 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4354 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4355 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4356 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4357 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4358 | struct permission *perm; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4359 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4360 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4361 | perm = &pset->permitted_user[i]; |
| 4362 | if (open_listen_match_tcpip(perm, |
| 4363 | listen_host, listen_port, 1)) { |
| 4364 | if (perm->downstream) |
| 4365 | return perm->downstream; |
| 4366 | if (perm->port_to_connect == 0) |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4367 | return rdynamic_connect_prepare(ssh, |
| 4368 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4369 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4370 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4371 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4375 | listen_port); |
| 4376 | return NULL; |
| 4377 | } |
| 4378 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4379 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4380 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4381 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4382 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4383 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4384 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4385 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4386 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4387 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4388 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4389 | perm = &pset->permitted_user[i]; |
| 4390 | if (open_listen_match_streamlocal(perm, path)) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4391 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4392 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4393 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4397 | path); |
| 4398 | return NULL; |
| 4399 | } |
| 4400 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4401 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4402 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4403 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4404 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4405 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4406 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4407 | struct permission_set *pset = &sc->local_perms; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4408 | struct channel_connect cctx; |
| 4409 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4410 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4411 | int sock; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4412 | struct permission *perm; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4413 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4414 | permit = pset->all_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4415 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4416 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4417 | perm = &pset->permitted_user[i]; |
| 4418 | if (open_match(perm, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4419 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4420 | break; |
| 4421 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4422 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4423 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4424 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4425 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4426 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4427 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4428 | perm = &pset->permitted_admin[i]; |
| 4429 | if (open_match(perm, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4430 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4431 | break; |
| 4432 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4433 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | if (!permit || !permit_adm) { |
florian@openbsd.org | cb4accb | 2019-05-10 18:55:17 +0000 | [diff] [blame] | 4437 | logit("Received request from %.100s port %d to connect to " |
| 4438 | "host %.100s port %d, but the request was denied.", |
| 4439 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4440 | if (reason != NULL) |
| 4441 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4442 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4443 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4444 | |
| 4445 | memset(&cctx, 0, sizeof(cctx)); |
| 4446 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4447 | &cctx, reason, errmsg); |
| 4448 | if (sock == -1) { |
| 4449 | channel_connect_ctx_free(&cctx); |
| 4450 | return NULL; |
| 4451 | } |
| 4452 | |
| 4453 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4454 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4455 | c->host_port = port; |
| 4456 | c->path = xstrdup(host); |
| 4457 | c->connect_ctx = cctx; |
| 4458 | |
| 4459 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4462 | /* Check if connecting to that path is permitted and connect. */ |
| 4463 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4464 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4465 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4466 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4467 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4468 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4469 | u_int i, permit, permit_adm = 1; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4470 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4471 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4472 | permit = pset->all_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4473 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4474 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4475 | perm = &pset->permitted_user[i]; |
| 4476 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4477 | permit = 1; |
| 4478 | break; |
| 4479 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4480 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4481 | } |
| 4482 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4483 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4484 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4485 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4486 | perm = &pset->permitted_admin[i]; |
| 4487 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4488 | permit_adm = 1; |
| 4489 | break; |
| 4490 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4491 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | if (!permit || !permit_adm) { |
| 4495 | logit("Received request to connect to path %.100s, " |
| 4496 | "but the request was denied.", path); |
| 4497 | return NULL; |
| 4498 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4499 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4500 | } |
| 4501 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4502 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4503 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4504 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4505 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4506 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4507 | int r; |
| 4508 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4509 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4510 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4511 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4512 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4513 | continue; |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4514 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) == -1) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4515 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4516 | channel_request_start(ssh, i, "window-change", 0); |
| 4517 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4518 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4519 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4520 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4521 | (r = sshpkt_send(ssh)) != 0) |
| 4522 | fatal("%s: channel %u: send window-change: %s", |
| 4523 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4527 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4528 | static Channel * |
| 4529 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4530 | { |
| 4531 | Channel *c; |
| 4532 | int r; |
| 4533 | |
| 4534 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4535 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4536 | c->host_port = 0; |
| 4537 | c->path = NULL; |
| 4538 | |
| 4539 | /* |
| 4540 | * We need to open the channel before we have a FD, |
| 4541 | * so that we can get SOCKS header from peer. |
| 4542 | */ |
| 4543 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4544 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4545 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4546 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4547 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4548 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4549 | c->self, ssh_err(r)); |
| 4550 | } |
| 4551 | return c; |
| 4552 | } |
| 4553 | |
| 4554 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4555 | static int |
| 4556 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4557 | { |
| 4558 | struct channel_connect cctx; |
| 4559 | int sock; |
| 4560 | |
| 4561 | memset(&cctx, 0, sizeof(cctx)); |
| 4562 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4563 | NULL, &cctx, NULL, NULL); |
| 4564 | if (sock == -1) |
| 4565 | channel_connect_ctx_free(&cctx); |
| 4566 | else { |
| 4567 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4568 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4569 | c->connect_ctx = cctx; |
| 4570 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4571 | } |
| 4572 | return sock; |
| 4573 | } |
| 4574 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4575 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4576 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4577 | /* |
| 4578 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4579 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4580 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4581 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4582 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4583 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4584 | int x11_use_localhost, int single_connection, |
| 4585 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4586 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4587 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4588 | int display_number, sock; |
| 4589 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4590 | struct addrinfo hints, *ai, *aitop; |
| 4591 | char strport[NI_MAXSERV]; |
| 4592 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4593 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4594 | if (chanids == NULL) |
| 4595 | return -1; |
| 4596 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4597 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4598 | display_number < MAX_DISPLAYS; |
| 4599 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4600 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4601 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4602 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4603 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4604 | hints.ai_socktype = SOCK_STREAM; |
| 4605 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4606 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4607 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4608 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4609 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4610 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4611 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4612 | if (ai->ai_family != AF_INET && |
| 4613 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4614 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4615 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4616 | ai->ai_protocol); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4617 | if (sock == -1) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4618 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4619 | #ifdef EPFNOSUPPORT |
| 4620 | && (errno != EPFNOSUPPORT) |
| 4621 | #endif |
| 4622 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4623 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4624 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4625 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4626 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4627 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4628 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4629 | continue; |
| 4630 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4631 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4632 | if (ai->ai_family == AF_INET6) |
| 4633 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4634 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4635 | set_reuseaddr(sock); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4636 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4637 | debug2("%s: bind port %d: %.100s", __func__, |
| 4638 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4639 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4640 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4641 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4642 | num_socks = 0; |
| 4643 | break; |
| 4644 | } |
| 4645 | socks[num_socks++] = sock; |
| 4646 | if (num_socks == NUM_SOCKS) |
| 4647 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4648 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4649 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4650 | if (num_socks > 0) |
| 4651 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4652 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4653 | if (display_number >= MAX_DISPLAYS) { |
| 4654 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4655 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4656 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4657 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4658 | for (n = 0; n < num_socks; n++) { |
| 4659 | sock = socks[n]; |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4660 | if (listen(sock, SSH_LISTEN_BACKLOG) == -1) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4661 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4662 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4663 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4664 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4665 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4666 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4667 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4668 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4669 | for (n = 0; n < num_socks; n++) { |
| 4670 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4671 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4672 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4673 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4674 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4675 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4676 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4677 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4678 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4679 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4680 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4681 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4682 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4683 | } |
| 4684 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4685 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4686 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4687 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4688 | int sock; |
| 4689 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4690 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4691 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4692 | if (sock == -1) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4693 | error("socket: %.100s", strerror(errno)); |
| 4694 | memset(&addr, 0, sizeof(addr)); |
| 4695 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4696 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4697 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4698 | return sock; |
| 4699 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4700 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4701 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4702 | } |
| 4703 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4704 | static int |
| 4705 | connect_local_xsocket(u_int dnr) |
| 4706 | { |
| 4707 | char buf[1024]; |
| 4708 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4709 | return connect_local_xsocket_path(buf); |
| 4710 | } |
| 4711 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4712 | #ifdef __APPLE__ |
| 4713 | static int |
| 4714 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4715 | { |
| 4716 | struct stat sbuf; |
| 4717 | |
| 4718 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4719 | error("%s: display path too long", __func__); |
| 4720 | return 0; |
| 4721 | } |
| 4722 | if (display[0] != '/') |
| 4723 | return 0; |
| 4724 | if (stat(path, &sbuf) == 0) { |
| 4725 | return 1; |
| 4726 | } else { |
| 4727 | char *dot = strrchr(path, '.'); |
| 4728 | if (dot != NULL) { |
| 4729 | *dot = '\0'; |
| 4730 | if (stat(path, &sbuf) == 0) { |
| 4731 | return 1; |
| 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | return 0; |
| 4736 | } |
| 4737 | #endif |
| 4738 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4739 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4740 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4741 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4742 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4743 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4744 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4745 | struct addrinfo hints, *ai, *aitop; |
| 4746 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4747 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4748 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4749 | /* Try to open a socket for the local X server. */ |
| 4750 | display = getenv("DISPLAY"); |
| 4751 | if (!display) { |
| 4752 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4753 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4754 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4755 | /* |
| 4756 | * Now we decode the value of the DISPLAY variable and make a |
| 4757 | * connection to the real X server. |
| 4758 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4759 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4760 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4761 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4762 | { |
| 4763 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4764 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4765 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4766 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4767 | |
| 4768 | /* Create a socket. */ |
| 4769 | sock = connect_local_xsocket_path(path); |
| 4770 | if (sock < 0) |
| 4771 | return -1; |
| 4772 | |
| 4773 | /* OK, we now have a connection to the display. */ |
| 4774 | return sock; |
| 4775 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4776 | } |
| 4777 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4778 | /* |
| 4779 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4780 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4781 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4782 | if (strncmp(display, "unix:", 5) == 0 || |
| 4783 | display[0] == ':') { |
| 4784 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4785 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4786 | &display_number) != 1) { |
| 4787 | error("Could not parse display number from DISPLAY: " |
| 4788 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4789 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4790 | } |
| 4791 | /* Create a socket. */ |
| 4792 | sock = connect_local_xsocket(display_number); |
| 4793 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4794 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4795 | |
| 4796 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4797 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4798 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4799 | /* |
| 4800 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4801 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4802 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4803 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4804 | cp = strchr(buf, ':'); |
| 4805 | if (!cp) { |
| 4806 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4807 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4808 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4809 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4810 | /* |
| 4811 | * buf now contains the host name. But first we parse the |
| 4812 | * display number. |
| 4813 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4814 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4815 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4816 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4817 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4818 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4819 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4820 | /* Look up the host address */ |
| 4821 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4822 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4823 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4824 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4825 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4826 | error("%.100s: unknown host. (%s)", buf, |
| 4827 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4828 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4829 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4830 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4831 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4832 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4833 | if (sock == -1) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4834 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4835 | continue; |
| 4836 | } |
| 4837 | /* Connect it to the display. */ |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 4838 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4839 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4840 | 6000 + display_number, strerror(errno)); |
| 4841 | close(sock); |
| 4842 | continue; |
| 4843 | } |
| 4844 | /* Success */ |
| 4845 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4846 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4847 | freeaddrinfo(aitop); |
| 4848 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4849 | error("connect %.100s port %u: %.100s", buf, |
| 4850 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4851 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4852 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4853 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4854 | return sock; |
| 4855 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4856 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4857 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4858 | * Requests forwarding of X11 connections, generates fake authentication |
| 4859 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4860 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4861 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4862 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4863 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4864 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4865 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4866 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4867 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4868 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4869 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4870 | char *new_data; |
| 4871 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4872 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4873 | if (sc->x11_saved_display == NULL) |
| 4874 | sc->x11_saved_display = xstrdup(disp); |
| 4875 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4876 | error("x11_request_forwarding_with_spoofing: different " |
| 4877 | "$DISPLAY already forwarded"); |
| 4878 | return; |
| 4879 | } |
| 4880 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4881 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4882 | if (cp) |
| 4883 | cp = strchr(cp, '.'); |
| 4884 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4885 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4886 | else |
| 4887 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4888 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4889 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4890 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4891 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4892 | |
| 4893 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4894 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4895 | for (i = 0; i < data_len; i++) { |
| 4896 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4897 | fatal("x11_request_forwarding: bad " |
| 4898 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4899 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4900 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4901 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4902 | |
| 4903 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4904 | sc->x11_fake_data = xmalloc(data_len); |
| 4905 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4906 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4907 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4908 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4909 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4910 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4911 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4912 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4913 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4914 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4915 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4916 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4917 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4918 | (r = sshpkt_send(ssh)) != 0 || |
| 4919 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4920 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4921 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4922 | } |