djm@openbsd.org | 0fa174e | 2019-01-19 21:31:32 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.388 2019/01/19 21:31:32 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains functions for generic socket connection forwarding. |
| 7 | * There is also code for initiating connection forwarding for X11 connections, |
| 8 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * Copyright (c) 1999 Dug Song. All rights reserved. |
| 19 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 43 | |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 44 | #include <sys/types.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 45 | #include <sys/stat.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include <sys/ioctl.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 47 | #include <sys/un.h> |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 48 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 49 | #ifdef HAVE_SYS_TIME_H |
| 50 | # include <sys/time.h> |
| 51 | #endif |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 52 | |
| 53 | #include <netinet/in.h> |
| 54 | #include <arpa/inet.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 55 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 56 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 57 | #include <fcntl.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 58 | #include <limits.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 59 | #include <netdb.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 60 | #include <stdarg.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 61 | #ifdef HAVE_STDINT_H |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 62 | #include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 66 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 67 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 70 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh2.h" |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 74 | #include "ssherr.h" |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 75 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "canohost.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 82 | #include "sshkey.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 83 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 84 | #include "pathnames.h" |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 85 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | |
djm@openbsd.org | 0fa174e | 2019-01-19 21:31:32 +0000 | [diff] [blame] | 87 | #include "opacket.h" /* XXX */ |
| 88 | extern struct ssh *active_state; /* XXX */ |
| 89 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 90 | /* -- agent forwarding */ |
| 91 | #define NUM_SOCKS 10 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 93 | /* -- tcp forwarding */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 94 | /* special-case port number meaning allow any port */ |
| 95 | #define FWD_PERMIT_ANY_PORT 0 |
| 96 | |
| 97 | /* special-case wildcard meaning allow any host */ |
| 98 | #define FWD_PERMIT_ANY_HOST "*" |
| 99 | |
| 100 | /* -- X11 forwarding */ |
| 101 | /* Maximum number of fake X11 displays to try. */ |
| 102 | #define MAX_DISPLAYS 1000 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 104 | /* Per-channel callback for pre/post select() actions */ |
| 105 | typedef void chan_fn(struct ssh *, Channel *c, |
| 106 | fd_set *readset, fd_set *writeset); |
| 107 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 108 | /* |
| 109 | * Data structure for storing which hosts are permitted for forward requests. |
| 110 | * The local sides of any remote forwards are stored in this array to prevent |
| 111 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 112 | * network (which might be behind a firewall). |
| 113 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 114 | /* XXX: streamlocal wants a path instead of host:port */ |
| 115 | /* Overload host_to_connect; we could just make this match Forward */ |
| 116 | /* XXX - can we use listen_host instead of listen_path? */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 117 | struct permission { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 118 | char *host_to_connect; /* Connect to 'host'. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 119 | int port_to_connect; /* Connect to 'port'. */ |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 120 | char *listen_host; /* Remote side should listen address. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 121 | char *listen_path; /* Remote side should listen path. */ |
| 122 | int listen_port; /* Remote side should listen port. */ |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 123 | Channel *downstream; /* Downstream mux*/ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 124 | }; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 126 | /* |
| 127 | * Stores the forwarding permission state for a single direction (local or |
| 128 | * remote). |
| 129 | */ |
| 130 | struct permission_set { |
| 131 | /* |
| 132 | * List of all local permitted host/port pairs to allow for the |
| 133 | * user. |
| 134 | */ |
| 135 | u_int num_permitted_user; |
| 136 | struct permission *permitted_user; |
| 137 | |
| 138 | /* |
| 139 | * List of all permitted host/port pairs to allow for the admin. |
| 140 | */ |
| 141 | u_int num_permitted_admin; |
| 142 | struct permission *permitted_admin; |
| 143 | |
| 144 | /* |
| 145 | * If this is true, all opens/listens are permitted. This is the |
| 146 | * case on the server on which we have to trust the client anyway, |
| 147 | * and the user could do anything after logging in. |
| 148 | */ |
| 149 | int all_permitted; |
| 150 | }; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 151 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 152 | /* Master structure for channels state */ |
| 153 | struct ssh_channels { |
| 154 | /* |
| 155 | * Pointer to an array containing all allocated channels. The array |
| 156 | * is dynamically extended as needed. |
| 157 | */ |
| 158 | Channel **channels; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 159 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 160 | /* |
| 161 | * Size of the channel array. All slots of the array must always be |
| 162 | * initialized (at least the type field); unused slots set to NULL |
| 163 | */ |
| 164 | u_int channels_alloc; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 165 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 166 | /* |
| 167 | * Maximum file descriptor value used in any of the channels. This is |
| 168 | * updated in channel_new. |
| 169 | */ |
| 170 | int channel_max_fd; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 171 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 172 | /* |
| 173 | * 'channel_pre*' are called just before select() to add any bits |
| 174 | * relevant to channels in the select bitmasks. |
| 175 | * |
| 176 | * 'channel_post*': perform any appropriate operations for |
| 177 | * channels which have events pending. |
| 178 | */ |
| 179 | chan_fn **channel_pre; |
| 180 | chan_fn **channel_post; |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 181 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 182 | /* -- tcp forwarding */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 183 | struct permission_set local_perms; |
| 184 | struct permission_set remote_perms; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 185 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 186 | /* -- X11 forwarding */ |
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 local (client) display. */ |
| 189 | char *x11_saved_display; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 190 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 191 | /* Saved X11 authentication protocol name. */ |
| 192 | char *x11_saved_proto; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 193 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 194 | /* Saved X11 authentication data. This is the real data. */ |
| 195 | char *x11_saved_data; |
| 196 | u_int x11_saved_data_len; |
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 | /* Deadline after which all X11 connections are refused */ |
| 199 | u_int x11_refuse_time; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 200 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 201 | /* |
| 202 | * Fake X11 authentication data. This is what the server will be |
| 203 | * sending us; we should replace any occurrences of this by the |
| 204 | * real data. |
| 205 | */ |
| 206 | u_char *x11_fake_data; |
| 207 | u_int x11_fake_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 208 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 209 | /* AF_UNSPEC or AF_INET or AF_INET6 */ |
| 210 | int IPv4or6; |
| 211 | }; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 212 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 213 | /* helper */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 214 | 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] | 215 | static const char *channel_rfwd_bind_host(const char *listen_host); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 216 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 217 | /* non-blocking connect helpers */ |
| 218 | static int connect_next(struct channel_connect *); |
| 219 | static void channel_connect_ctx_free(struct channel_connect *); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 220 | static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *); |
| 221 | static int rdynamic_connect_finish(struct ssh *, Channel *); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 222 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 223 | /* Setup helper */ |
| 224 | static void channel_handler_init(struct ssh_channels *sc); |
| 225 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 226 | /* -- channel core */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 227 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 228 | void |
| 229 | channel_init_channels(struct ssh *ssh) |
| 230 | { |
| 231 | struct ssh_channels *sc; |
| 232 | |
djm@openbsd.org | 285310b | 2018-12-07 02:31:20 +0000 | [diff] [blame] | 233 | if ((sc = calloc(1, sizeof(*sc))) == NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 234 | fatal("%s: allocation failed", __func__); |
| 235 | sc->channels_alloc = 10; |
| 236 | sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels)); |
| 237 | sc->IPv4or6 = AF_UNSPEC; |
| 238 | channel_handler_init(sc); |
| 239 | |
| 240 | ssh->chanctxt = sc; |
| 241 | } |
| 242 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 243 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 244 | channel_by_id(struct ssh *ssh, int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 245 | { |
| 246 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 247 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 248 | if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) { |
| 249 | logit("%s: %d: bad id", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 252 | c = ssh->chanctxt->channels[id]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 253 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 254 | logit("%s: %d: bad id: channel free", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | return c; |
| 258 | } |
| 259 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 260 | Channel * |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 261 | channel_by_remote_id(struct ssh *ssh, u_int remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 262 | { |
| 263 | Channel *c; |
| 264 | u_int i; |
| 265 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 266 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 267 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 268 | 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] | 269 | return c; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 274 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 275 | * Returns the channel if it is allowed to receive protocol messages. |
| 276 | * Private channels, like listening sockets, may not receive messages. |
| 277 | */ |
| 278 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 279 | channel_lookup(struct ssh *ssh, int id) |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 280 | { |
| 281 | Channel *c; |
| 282 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 283 | if ((c = channel_by_id(ssh, id)) == NULL) |
| 284 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 285 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 286 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 287 | case SSH_CHANNEL_X11_OPEN: |
| 288 | case SSH_CHANNEL_LARVAL: |
| 289 | case SSH_CHANNEL_CONNECTING: |
| 290 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 291 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 292 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 293 | case SSH_CHANNEL_OPENING: |
| 294 | case SSH_CHANNEL_OPEN: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 295 | case SSH_CHANNEL_ABANDONED: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 296 | case SSH_CHANNEL_MUX_PROXY: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 297 | return c; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 298 | } |
| 299 | logit("Non-public channel %d, type %d.", id, c->type); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 300 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 304 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 305 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 306 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 307 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 308 | 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] | 309 | int extusage, int nonblock, int is_tty) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 310 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 311 | struct ssh_channels *sc = ssh->chanctxt; |
| 312 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | /* Update the maximum file descriptor value. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 314 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd); |
| 315 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd); |
| 316 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 317 | |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 318 | if (rfd != -1) |
| 319 | fcntl(rfd, F_SETFD, FD_CLOEXEC); |
| 320 | if (wfd != -1 && wfd != rfd) |
| 321 | fcntl(wfd, F_SETFD, FD_CLOEXEC); |
| 322 | if (efd != -1 && efd != rfd && efd != wfd) |
| 323 | fcntl(efd, F_SETFD, FD_CLOEXEC); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 324 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 325 | c->rfd = rfd; |
| 326 | c->wfd = wfd; |
| 327 | c->sock = (rfd == wfd) ? rfd : -1; |
| 328 | c->efd = efd; |
| 329 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 330 | |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 331 | if ((c->isatty = is_tty) != 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 332 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 333 | #ifdef _AIX |
| 334 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 1a48aec | 2008-06-16 23:35:56 +1000 | [diff] [blame] | 335 | c->wfd_isatty = is_tty || isatty(c->wfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 336 | #endif |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 337 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 338 | /* enable nonblocking mode */ |
| 339 | if (nonblock) { |
| 340 | if (rfd != -1) |
| 341 | set_nonblock(rfd); |
| 342 | if (wfd != -1) |
| 343 | set_nonblock(wfd); |
| 344 | if (efd != -1) |
| 345 | set_nonblock(efd); |
| 346 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Allocate a new channel object and set its type and socket. This will cause |
| 351 | * remote_name to be freed. |
| 352 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 353 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 354 | 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] | 355 | 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] | 356 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 357 | struct ssh_channels *sc = ssh->chanctxt; |
| 358 | u_int i, found; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 359 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 360 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | /* 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] | 362 | for (i = 0; i < sc->channels_alloc; i++) { |
| 363 | if (sc->channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | /* Found a free slot. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 365 | found = i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 366 | break; |
| 367 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 368 | } |
| 369 | if (i >= sc->channels_alloc) { |
| 370 | /* |
| 371 | * There are no free slots. Take last+1 slot and expand |
| 372 | * the array. |
| 373 | */ |
| 374 | found = sc->channels_alloc; |
| 375 | if (sc->channels_alloc > CHANNELS_MAX_CHANNELS) |
| 376 | fatal("%s: internal error: channels_alloc %d too big", |
| 377 | __func__, sc->channels_alloc); |
| 378 | sc->channels = xrecallocarray(sc->channels, sc->channels_alloc, |
| 379 | sc->channels_alloc + 10, sizeof(*sc->channels)); |
| 380 | sc->channels_alloc += 10; |
| 381 | debug2("channel: expanding %d", sc->channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 382 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 383 | /* Initialize and return new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 384 | c = sc->channels[found] = xcalloc(1, sizeof(Channel)); |
| 385 | if ((c->input = sshbuf_new()) == NULL || |
| 386 | (c->output = sshbuf_new()) == NULL || |
| 387 | (c->extended = sshbuf_new()) == NULL) |
| 388 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 389 | c->ostate = CHAN_OUTPUT_OPEN; |
| 390 | c->istate = CHAN_INPUT_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 391 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 392 | c->self = found; |
| 393 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 394 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 395 | c->local_window = window; |
| 396 | c->local_window_max = window; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 397 | c->local_maxpacket = maxpack; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 398 | c->remote_name = xstrdup(remote_name); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 399 | c->ctl_chan = -1; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 400 | c->delayed = 1; /* prevent call to channel_post handler */ |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 401 | TAILQ_INIT(&c->status_confirms); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 402 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 403 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 404 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 405 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 406 | static void |
| 407 | channel_find_maxfd(struct ssh_channels *sc) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 408 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 409 | u_int i; |
| 410 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 411 | Channel *c; |
| 412 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 413 | for (i = 0; i < sc->channels_alloc; i++) { |
| 414 | c = sc->channels[i]; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 415 | if (c != NULL) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 416 | max = MAXIMUM(max, c->rfd); |
| 417 | max = MAXIMUM(max, c->wfd); |
| 418 | max = MAXIMUM(max, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 421 | sc->channel_max_fd = max; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 425 | channel_close_fd(struct ssh *ssh, int *fdp) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 426 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 427 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 428 | int ret = 0, fd = *fdp; |
| 429 | |
| 430 | if (fd != -1) { |
| 431 | ret = close(fd); |
| 432 | *fdp = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 433 | if (fd == sc->channel_max_fd) |
| 434 | channel_find_maxfd(sc); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 435 | } |
| 436 | return ret; |
| 437 | } |
| 438 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 439 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 440 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 441 | channel_close_fds(struct ssh *ssh, Channel *c) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 442 | { |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 443 | int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd; |
| 444 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 445 | channel_close_fd(ssh, &c->sock); |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 446 | if (rfd != sock) |
| 447 | channel_close_fd(ssh, &c->rfd); |
| 448 | if (wfd != sock && wfd != rfd) |
| 449 | channel_close_fd(ssh, &c->wfd); |
| 450 | if (efd != sock && efd != rfd && efd != wfd) |
| 451 | channel_close_fd(ssh, &c->efd); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 455 | fwd_perm_clear(struct permission *perm) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 456 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 457 | free(perm->host_to_connect); |
| 458 | free(perm->listen_host); |
| 459 | free(perm->listen_path); |
| 460 | bzero(perm, sizeof(*perm)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 461 | } |
| 462 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 463 | /* Returns an printable name for the specified forwarding permission list */ |
| 464 | static const char * |
| 465 | fwd_ident(int who, int where) |
| 466 | { |
| 467 | if (who == FORWARD_ADM) { |
| 468 | if (where == FORWARD_LOCAL) |
| 469 | return "admin local"; |
| 470 | else if (where == FORWARD_REMOTE) |
| 471 | return "admin remote"; |
| 472 | } else if (who == FORWARD_USER) { |
| 473 | if (where == FORWARD_LOCAL) |
| 474 | return "user local"; |
| 475 | else if (where == FORWARD_REMOTE) |
| 476 | return "user remote"; |
| 477 | } |
| 478 | fatal("Unknown forward permission list %d/%d", who, where); |
| 479 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 480 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 481 | /* Returns the forwarding permission list for the specified direction */ |
| 482 | static struct permission_set * |
| 483 | permission_set_get(struct ssh *ssh, int where) |
| 484 | { |
| 485 | struct ssh_channels *sc = ssh->chanctxt; |
| 486 | |
| 487 | switch (where) { |
| 488 | case FORWARD_LOCAL: |
| 489 | return &sc->local_perms; |
| 490 | break; |
| 491 | case FORWARD_REMOTE: |
| 492 | return &sc->remote_perms; |
| 493 | break; |
| 494 | default: |
| 495 | fatal("%s: invalid forwarding direction %d", __func__, where); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* Reutrns pointers to the specified forwarding list and its element count */ |
| 500 | static void |
| 501 | permission_set_get_array(struct ssh *ssh, int who, int where, |
| 502 | struct permission ***permpp, u_int **npermpp) |
| 503 | { |
| 504 | struct permission_set *pset = permission_set_get(ssh, where); |
| 505 | |
| 506 | switch (who) { |
| 507 | case FORWARD_USER: |
| 508 | *permpp = &pset->permitted_user; |
| 509 | *npermpp = &pset->num_permitted_user; |
| 510 | break; |
| 511 | case FORWARD_ADM: |
| 512 | *permpp = &pset->permitted_admin; |
| 513 | *npermpp = &pset->num_permitted_admin; |
| 514 | break; |
| 515 | default: |
| 516 | fatal("%s: invalid forwarding client %d", __func__, who); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* Adds an entry to the spcified forwarding list */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 521 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 522 | permission_set_add(struct ssh *ssh, int who, int where, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 523 | const char *host_to_connect, int port_to_connect, |
| 524 | const char *listen_host, const char *listen_path, int listen_port, |
| 525 | Channel *downstream) |
| 526 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 527 | struct permission **permp; |
| 528 | u_int n, *npermp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 529 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 530 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
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 | if (*npermp >= INT_MAX) |
| 533 | fatal("%s: %s overflow", __func__, fwd_ident(who, where)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 534 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 535 | *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp)); |
| 536 | n = (*npermp)++; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 537 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 538 | (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 539 | (*permp)[n].port_to_connect = port_to_connect; |
| 540 | (*permp)[n].listen_host = MAYBE_DUP(listen_host); |
| 541 | (*permp)[n].listen_path = MAYBE_DUP(listen_path); |
| 542 | (*permp)[n].listen_port = listen_port; |
| 543 | (*permp)[n].downstream = downstream; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 544 | #undef MAYBE_DUP |
| 545 | return (int)n; |
| 546 | } |
| 547 | |
| 548 | static void |
| 549 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 550 | { |
| 551 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 552 | struct permission_set *pset = &sc->local_perms; |
| 553 | struct permission *perm; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 554 | int r; |
| 555 | u_int i; |
| 556 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 557 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 558 | perm = &pset->permitted_user[i]; |
| 559 | if (perm->downstream != c) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 560 | continue; |
| 561 | |
| 562 | /* cancel on the server, since mux client is gone */ |
| 563 | debug("channel %d: cleanup remote forward for %s:%u", |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 564 | c->self, perm->listen_host, perm->listen_port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 565 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 566 | (r = sshpkt_put_cstring(ssh, |
| 567 | "cancel-tcpip-forward")) != 0 || |
| 568 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 569 | (r = sshpkt_put_cstring(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 570 | channel_rfwd_bind_host(perm->listen_host))) != 0 || |
| 571 | (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 572 | (r = sshpkt_send(ssh)) != 0) { |
| 573 | fatal("%s: channel %i: %s", __func__, |
| 574 | c->self, ssh_err(r)); |
| 575 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 576 | fwd_perm_clear(perm); /* unregister */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 577 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 581 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 582 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 583 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 584 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 585 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 586 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 587 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 588 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 589 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 590 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 591 | if ((other = sc->channels[i]) == NULL) |
| 592 | continue; |
| 593 | n++; |
| 594 | /* detach from mux client and prepare for closing */ |
| 595 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 596 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 597 | other->mux_ctx == c) { |
| 598 | other->mux_ctx = NULL; |
| 599 | other->type = SSH_CHANNEL_OPEN; |
| 600 | other->istate = CHAN_INPUT_CLOSED; |
| 601 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 604 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 605 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 606 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 607 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 608 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 609 | |
markus@openbsd.org | 5d14019 | 2018-07-27 12:03:17 +0000 | [diff] [blame] | 610 | if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) { |
| 611 | s = channel_open_message(ssh); |
| 612 | debug3("channel %d: status: %s", c->self, s); |
| 613 | free(s); |
| 614 | } |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 615 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 616 | channel_close_fds(ssh, c); |
| 617 | sshbuf_free(c->input); |
| 618 | sshbuf_free(c->output); |
| 619 | sshbuf_free(c->extended); |
| 620 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 621 | free(c->remote_name); |
| 622 | c->remote_name = NULL; |
| 623 | free(c->path); |
| 624 | c->path = NULL; |
| 625 | free(c->listening_addr); |
| 626 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 627 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 628 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 629 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 630 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 631 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 632 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 633 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 634 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 635 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 636 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 637 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 638 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 639 | } |
| 640 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 641 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 642 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 643 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 644 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 645 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 646 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 647 | if (ssh->chanctxt->channels[i] != NULL) |
| 648 | channel_free(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 653 | * descriptors after a fork. |
| 654 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 655 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 656 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 657 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 658 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 659 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 660 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 661 | if (ssh->chanctxt->channels[i] != NULL) |
| 662 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 666 | * Stop listening to channels. |
| 667 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 668 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 669 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 670 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 671 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 672 | Channel *c; |
| 673 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 674 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 675 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 676 | if (c != NULL) { |
| 677 | switch (c->type) { |
| 678 | case SSH_CHANNEL_AUTH_SOCKET: |
| 679 | case SSH_CHANNEL_PORT_LISTENER: |
| 680 | case SSH_CHANNEL_RPORT_LISTENER: |
| 681 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 682 | case SSH_CHANNEL_UNIX_LISTENER: |
| 683 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 684 | channel_close_fd(ssh, &c->sock); |
| 685 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 693 | * Returns true if no channel has too much buffered data, and false if one or |
| 694 | * more channel is overfull. |
| 695 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 696 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 697 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 698 | { |
| 699 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 700 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 701 | Channel *c; |
| 702 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 703 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 704 | c = ssh->chanctxt->channels[i]; |
| 705 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 706 | continue; |
| 707 | if (sshbuf_len(c->output) > maxsize) { |
| 708 | debug2("channel %d: big output buffer %zu > %u", |
| 709 | c->self, sshbuf_len(c->output), maxsize); |
| 710 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 717 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 718 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 719 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 720 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 721 | Channel *c; |
| 722 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 723 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 724 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 725 | if (c == NULL) |
| 726 | continue; |
| 727 | switch (c->type) { |
| 728 | case SSH_CHANNEL_X11_LISTENER: |
| 729 | case SSH_CHANNEL_PORT_LISTENER: |
| 730 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 731 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 732 | case SSH_CHANNEL_CLOSED: |
| 733 | case SSH_CHANNEL_AUTH_SOCKET: |
| 734 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 735 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 736 | case SSH_CHANNEL_CONNECTING: |
| 737 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 738 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 739 | case SSH_CHANNEL_UNIX_LISTENER: |
| 740 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 741 | continue; |
| 742 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 743 | continue; |
| 744 | case SSH_CHANNEL_OPENING: |
| 745 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 746 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 747 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 748 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 749 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 750 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 751 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 752 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 753 | /* NOTREACHED */ |
| 754 | } |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 760 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 761 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 762 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 763 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 764 | Channel *c; |
| 765 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 766 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 767 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 768 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 769 | continue; |
| 770 | switch (c->type) { |
| 771 | case SSH_CHANNEL_CLOSED: |
| 772 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 773 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 774 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 775 | case SSH_CHANNEL_X11_LISTENER: |
| 776 | case SSH_CHANNEL_PORT_LISTENER: |
| 777 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 778 | case SSH_CHANNEL_MUX_LISTENER: |
| 779 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 780 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 781 | case SSH_CHANNEL_OPENING: |
| 782 | case SSH_CHANNEL_CONNECTING: |
| 783 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 784 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 785 | case SSH_CHANNEL_UNIX_LISTENER: |
| 786 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 787 | continue; |
| 788 | case SSH_CHANNEL_LARVAL: |
| 789 | case SSH_CHANNEL_AUTH_SOCKET: |
| 790 | case SSH_CHANNEL_OPEN: |
| 791 | case SSH_CHANNEL_X11_OPEN: |
| 792 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 793 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 794 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 795 | /* NOTREACHED */ |
| 796 | } |
| 797 | } |
| 798 | return -1; |
| 799 | } |
| 800 | |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 801 | /* Returns the state of the channel's extended usage flag */ |
| 802 | const char * |
| 803 | channel_format_extended_usage(const Channel *c) |
| 804 | { |
| 805 | if (c->efd == -1) |
| 806 | return "closed"; |
| 807 | |
| 808 | switch (c->extended_usage) { |
| 809 | case CHAN_EXTENDED_WRITE: |
| 810 | return "write"; |
| 811 | case CHAN_EXTENDED_READ: |
| 812 | return "read"; |
| 813 | case CHAN_EXTENDED_IGNORE: |
| 814 | return "ignore"; |
| 815 | default: |
| 816 | return "UNKNOWN"; |
| 817 | } |
| 818 | } |
| 819 | |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 820 | static char * |
| 821 | channel_format_status(const Channel *c) |
| 822 | { |
| 823 | char *ret = NULL; |
| 824 | |
| 825 | xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu " |
| 826 | "fd %d/%d/%d sock %d cc %d", |
| 827 | c->type, |
| 828 | c->have_remote_id ? "r" : "nr", c->remote_id, |
| 829 | c->istate, sshbuf_len(c->input), |
| 830 | c->ostate, sshbuf_len(c->output), |
| 831 | channel_format_extended_usage(c), sshbuf_len(c->extended), |
| 832 | c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan); |
| 833 | return ret; |
| 834 | } |
| 835 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 836 | /* |
| 837 | * Returns a message describing the currently open forwarded connections, |
| 838 | * suitable for sending to the client. The message contains crlf pairs for |
| 839 | * newlines. |
| 840 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 841 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 842 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 843 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 844 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 845 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 846 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 847 | int r; |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 848 | char *cp, *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 849 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 850 | if ((buf = sshbuf_new()) == NULL) |
| 851 | fatal("%s: sshbuf_new", __func__); |
| 852 | if ((r = sshbuf_putf(buf, |
| 853 | "The following connections are open:\r\n")) != 0) |
| 854 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 855 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 856 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 857 | if (c == NULL) |
| 858 | continue; |
| 859 | switch (c->type) { |
| 860 | case SSH_CHANNEL_X11_LISTENER: |
| 861 | case SSH_CHANNEL_PORT_LISTENER: |
| 862 | case SSH_CHANNEL_RPORT_LISTENER: |
| 863 | case SSH_CHANNEL_CLOSED: |
| 864 | case SSH_CHANNEL_AUTH_SOCKET: |
| 865 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 866 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 867 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 868 | case SSH_CHANNEL_UNIX_LISTENER: |
| 869 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 870 | continue; |
| 871 | case SSH_CHANNEL_LARVAL: |
| 872 | case SSH_CHANNEL_OPENING: |
| 873 | case SSH_CHANNEL_CONNECTING: |
| 874 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 875 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 876 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 877 | case SSH_CHANNEL_OPEN: |
| 878 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 879 | case SSH_CHANNEL_MUX_PROXY: |
| 880 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 881 | cp = channel_format_status(c); |
| 882 | if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n", |
| 883 | c->self, c->remote_name, cp)) != 0) { |
| 884 | free(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 885 | fatal("%s: sshbuf_putf: %s", |
| 886 | __func__, ssh_err(r)); |
djm@openbsd.org | 6f1aabb | 2018-10-04 01:04:52 +0000 | [diff] [blame] | 887 | } |
| 888 | free(cp); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 889 | continue; |
| 890 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 891 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 892 | /* NOTREACHED */ |
| 893 | } |
| 894 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 895 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 896 | fatal("%s: sshbuf_dup_string", __func__); |
| 897 | sshbuf_free(buf); |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | static void |
| 902 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 903 | { |
| 904 | int r; |
| 905 | |
| 906 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 907 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 908 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 909 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 910 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 911 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 912 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 916 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 917 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 918 | Channel *c = channel_lookup(ssh, id); |
| 919 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 920 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 921 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 922 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 923 | return; |
| 924 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 925 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 926 | open_preamble(ssh, __func__, c, c->ctype); |
| 927 | if ((r = sshpkt_send(ssh)) != 0) |
| 928 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 932 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 933 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 934 | Channel *c = channel_lookup(ssh, id); |
| 935 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 936 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 937 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 938 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 941 | if (!c->have_remote_id) |
| 942 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 943 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 944 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 945 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 946 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 947 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 948 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 949 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 950 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 951 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 952 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 953 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 954 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 955 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 956 | { |
| 957 | struct channel_confirm *cc; |
| 958 | Channel *c; |
| 959 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 960 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 961 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 962 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 963 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 964 | cc->cb = cb; |
| 965 | cc->abandon_cb = abandon_cb; |
| 966 | cc->ctx = ctx; |
| 967 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 968 | } |
| 969 | |
| 970 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 971 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 972 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 973 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 974 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 975 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 976 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 977 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 978 | return; |
| 979 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 980 | c->open_confirm = fn; |
| 981 | c->open_confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 982 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 983 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 984 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 985 | channel_register_cleanup(struct ssh *ssh, int id, |
| 986 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 987 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 988 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 989 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 990 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 991 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 992 | return; |
| 993 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 994 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 995 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 996 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 997 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 998 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 999 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1000 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1001 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1002 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1003 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1004 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1005 | return; |
| 1006 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 1007 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1008 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1009 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1010 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1011 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1012 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1013 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1014 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1015 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1016 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1017 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1018 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1021 | c->input_filter = ifn; |
| 1022 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 1023 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 1024 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1028 | 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] | 1029 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1030 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1031 | Channel *c = channel_lookup(ssh, id); |
| 1032 | int r; |
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 || c->type != SSH_CHANNEL_LARVAL) |
| 1035 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1036 | if (!c->have_remote_id) |
| 1037 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 1038 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1039 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1040 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 1041 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1042 | |
| 1043 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 1044 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1045 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1046 | (r = sshpkt_send(ssh)) != 0) |
| 1047 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1050 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1051 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 1052 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1053 | { |
| 1054 | FD_SET(c->sock, readset); |
| 1055 | } |
| 1056 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1057 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1058 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 1059 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1060 | { |
| 1061 | debug3("channel %d: waiting for connection", c->self); |
| 1062 | FD_SET(c->sock, writeset); |
| 1063 | } |
| 1064 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1065 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1066 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 1067 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1068 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1069 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1070 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1071 | sshbuf_len(c->input) < c->remote_window && |
| 1072 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1073 | FD_SET(c->rfd, readset); |
| 1074 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1075 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1076 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1077 | FD_SET(c->wfd, writeset); |
| 1078 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1079 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1080 | debug2("channel %d: " |
| 1081 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 1082 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1083 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1084 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1088 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 1089 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1090 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1091 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1092 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1093 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1094 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1095 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1096 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1097 | FD_SET(c->efd, readset); |
| 1098 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1099 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1100 | } |
| 1101 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1102 | /* |
| 1103 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1104 | * connection (when authentication spoofing is being done) remains in this |
| 1105 | * state until the first packet has been completely read. The authentication |
| 1106 | * data in that packet is then substituted by the real data if it matches the |
| 1107 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1108 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1109 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1110 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1111 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1112 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1113 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1114 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1115 | u_char *ucp; |
| 1116 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1117 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1118 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1119 | if (sc->x11_refuse_time != 0 && |
| 1120 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1121 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1122 | "expired"); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1126 | /* 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] | 1127 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1128 | return 0; |
| 1129 | |
| 1130 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1131 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1132 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1133 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1134 | data_len = 256 * ucp[8] + ucp[9]; |
| 1135 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1136 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1137 | data_len = ucp[8] + 256 * ucp[9]; |
| 1138 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1139 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1140 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1141 | return -1; |
| 1142 | } |
| 1143 | |
| 1144 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1145 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1146 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1147 | return 0; |
| 1148 | |
| 1149 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1150 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1151 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1152 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1153 | return -1; |
| 1154 | } |
| 1155 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1156 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1157 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1158 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1159 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1160 | return -1; |
| 1161 | } |
| 1162 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1163 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1164 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1165 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1166 | return -1; |
| 1167 | } |
| 1168 | /* |
| 1169 | * Received authentication protocol and data match |
| 1170 | * our fake data. Substitute the fake data with real |
| 1171 | * data. |
| 1172 | */ |
| 1173 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1174 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1175 | return 1; |
| 1176 | } |
| 1177 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1178 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1179 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1180 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1181 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1182 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* c->force_drain = 1; */ |
| 1185 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1186 | if (ret == 1) { |
| 1187 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1188 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1189 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1190 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1191 | debug2("X11 rejected %d i%d/o%d", |
| 1192 | c->self, c->istate, c->ostate); |
| 1193 | chan_read_failed(ssh, c); |
| 1194 | sshbuf_reset(c->input); |
| 1195 | chan_ibuf_empty(ssh, c); |
| 1196 | sshbuf_reset(c->output); |
| 1197 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1198 | 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] | 1199 | } |
| 1200 | } |
| 1201 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1202 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1203 | channel_pre_mux_client(struct ssh *ssh, |
| 1204 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1205 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1206 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1207 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1208 | FD_SET(c->rfd, readset); |
| 1209 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1210 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1211 | sshbuf_reset(c->input); |
| 1212 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1213 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1214 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1215 | } |
| 1216 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1217 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1218 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1219 | FD_SET(c->wfd, writeset); |
| 1220 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1221 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1225 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1226 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1227 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1228 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1229 | const u_char *p; |
| 1230 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1231 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1232 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1233 | struct { |
| 1234 | u_int8_t version; |
| 1235 | u_int8_t command; |
| 1236 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1237 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1238 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1239 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1240 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1241 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1242 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1243 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1244 | len = sizeof(s4_req); |
| 1245 | if (have < len) |
| 1246 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1247 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1248 | |
| 1249 | need = 1; |
| 1250 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1251 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1252 | debug2("channel %d: socks4a request", c->self); |
| 1253 | /* ... and needs an extra string (the hostname) */ |
| 1254 | need = 2; |
| 1255 | } |
| 1256 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1257 | for (found = 0, i = len; i < have; i++) { |
| 1258 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1259 | found++; |
| 1260 | if (found == need) |
| 1261 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1262 | } |
| 1263 | if (i > 1024) { |
| 1264 | /* the peer is probably sending garbage */ |
| 1265 | debug("channel %d: decode socks4: too long", |
| 1266 | c->self); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1270 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1271 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1272 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1273 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1274 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1275 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1276 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1277 | return -1; |
| 1278 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1279 | have = sshbuf_len(input); |
| 1280 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1281 | if (memchr(p, '\0', have) == NULL) { |
| 1282 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1283 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1284 | return -1; |
| 1285 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1286 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1287 | 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] | 1288 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1289 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1290 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1291 | fatal("%s: channel %d: consume: %s", __func__, |
| 1292 | c->self, ssh_err(r)); |
| 1293 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1294 | free(c->path); |
| 1295 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1296 | if (need == 1) { /* SOCKS4: one string */ |
| 1297 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1298 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1299 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1300 | have = sshbuf_len(input); |
| 1301 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1302 | if (memchr(p, '\0', have) == NULL) { |
| 1303 | error("channel %d: decode socks4a: host not nul " |
| 1304 | "terminated", c->self); |
| 1305 | return -1; |
| 1306 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1307 | len = strlen(p); |
| 1308 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1309 | c->self, p, len); |
| 1310 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1311 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1312 | error("channel %d: hostname \"%.100s\" too long", |
| 1313 | c->self, p); |
| 1314 | return -1; |
| 1315 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1316 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1317 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1318 | fatal("%s: channel %d: consume: %s", __func__, |
| 1319 | c->self, ssh_err(r)); |
| 1320 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1321 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1322 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1323 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1324 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1325 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1326 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1327 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1328 | debug("channel %d: cannot handle: %s cn %d", |
| 1329 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1330 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1331 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1332 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1333 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1334 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1335 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1336 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1337 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1338 | c->self, ssh_err(r)); |
| 1339 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1340 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1343 | /* try to decode a socks5 header */ |
| 1344 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1345 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1346 | #define SSH_SOCKS5_IPV4 0x01 |
| 1347 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1348 | #define SSH_SOCKS5_IPV6 0x04 |
| 1349 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1350 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1351 | |
| 1352 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1353 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1354 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1355 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1356 | struct { |
| 1357 | u_int8_t version; |
| 1358 | u_int8_t command; |
| 1359 | u_int8_t reserved; |
| 1360 | u_int8_t atyp; |
| 1361 | } s5_req, s5_rsp; |
| 1362 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1363 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1364 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1365 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1366 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1367 | |
| 1368 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1369 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1370 | if (p[0] != 0x05) |
| 1371 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1372 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1373 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1374 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1375 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1376 | return 0; |
| 1377 | nmethods = p[1]; |
| 1378 | if (have < nmethods + 2) |
| 1379 | return 0; |
| 1380 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1381 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1382 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1383 | found = 1; |
| 1384 | break; |
| 1385 | } |
| 1386 | } |
| 1387 | if (!found) { |
| 1388 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1389 | c->self); |
| 1390 | return -1; |
| 1391 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1392 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1393 | fatal("%s: channel %d: consume: %s", __func__, |
| 1394 | c->self, ssh_err(r)); |
| 1395 | } |
| 1396 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1397 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1398 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1399 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1400 | c->self, ssh_err(r)); |
| 1401 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1402 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1403 | debug2("channel %d: socks5 auth done", c->self); |
| 1404 | return 0; /* need more */ |
| 1405 | } |
| 1406 | debug2("channel %d: socks5 post auth", c->self); |
| 1407 | if (have < sizeof(s5_req)+1) |
| 1408 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1409 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1410 | if (s5_req.version != 0x05 || |
| 1411 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1412 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1413 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1414 | return -1; |
| 1415 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1416 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1417 | case SSH_SOCKS5_IPV4: |
| 1418 | addrlen = 4; |
| 1419 | af = AF_INET; |
| 1420 | break; |
| 1421 | case SSH_SOCKS5_DOMAIN: |
| 1422 | addrlen = p[sizeof(s5_req)]; |
| 1423 | af = -1; |
| 1424 | break; |
| 1425 | case SSH_SOCKS5_IPV6: |
| 1426 | addrlen = 16; |
| 1427 | af = AF_INET6; |
| 1428 | break; |
| 1429 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1430 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1431 | return -1; |
| 1432 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1433 | need = sizeof(s5_req) + addrlen + 2; |
| 1434 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1435 | need++; |
| 1436 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1437 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1438 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1439 | fatal("%s: channel %d: consume: %s", __func__, |
| 1440 | c->self, ssh_err(r)); |
| 1441 | } |
| 1442 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1443 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1444 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1445 | fatal("%s: channel %d: consume: %s", __func__, |
| 1446 | c->self, ssh_err(r)); |
| 1447 | } |
| 1448 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1449 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1450 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1451 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1452 | return -1; |
| 1453 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1454 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1455 | free(c->path); |
| 1456 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1457 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1458 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1459 | error("channel %d: dynamic request: socks5 hostname " |
| 1460 | "\"%.100s\" too long", c->self, dest_addr); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | c->path = xstrdup(dest_addr); |
| 1464 | } else { |
| 1465 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1466 | return -1; |
| 1467 | c->path = xstrdup(ntop); |
| 1468 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1469 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1470 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1471 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1472 | c->self, c->path, c->host_port, s5_req.command); |
| 1473 | |
| 1474 | s5_rsp.version = 0x05; |
| 1475 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1476 | s5_rsp.reserved = 0; /* ignored */ |
| 1477 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1478 | dest_port = 0; /* ignored */ |
| 1479 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1480 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1481 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1482 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1483 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1484 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1485 | return 1; |
| 1486 | } |
| 1487 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1488 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1489 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1490 | 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] | 1491 | { |
| 1492 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1493 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1494 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1495 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1496 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1497 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1498 | 0, "stdio-forward", /*nonblock*/0); |
| 1499 | |
| 1500 | c->path = xstrdup(host_to_connect); |
| 1501 | c->host_port = port_to_connect; |
| 1502 | c->listening_port = 0; |
| 1503 | c->force_drain = 1; |
| 1504 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1505 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1506 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1507 | |
| 1508 | return c; |
| 1509 | } |
| 1510 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1511 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1512 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1513 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1514 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1515 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1516 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1517 | u_int have; |
| 1518 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1519 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1520 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1521 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1522 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1523 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1524 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1525 | /* need more */ |
| 1526 | FD_SET(c->sock, readset); |
| 1527 | return; |
| 1528 | } |
| 1529 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1530 | p = sshbuf_ptr(c->input); |
| 1531 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1532 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1533 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1534 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1535 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1536 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1537 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1538 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1539 | default: |
| 1540 | ret = -1; |
| 1541 | break; |
| 1542 | } |
| 1543 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1544 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1545 | } else if (ret == 0) { |
| 1546 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1547 | /* need more */ |
| 1548 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1549 | if (sshbuf_len(c->output)) |
| 1550 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1551 | } else { |
| 1552 | /* switch to the next state */ |
| 1553 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1554 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1557 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1558 | /* simulate read-error */ |
| 1559 | static void |
| 1560 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1561 | { |
| 1562 | c->type = SSH_CHANNEL_OPEN; |
| 1563 | chan_read_failed(ssh, c); |
| 1564 | sshbuf_reset(c->input); |
| 1565 | chan_ibuf_empty(ssh, c); |
| 1566 | sshbuf_reset(c->output); |
| 1567 | chan_write_failed(ssh, c); |
| 1568 | } |
| 1569 | |
| 1570 | /* reverse dynamic port forwarding */ |
| 1571 | static void |
| 1572 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1573 | { |
| 1574 | const u_char *p; |
| 1575 | u_int have, len; |
| 1576 | int r, ret; |
| 1577 | |
| 1578 | have = sshbuf_len(c->output); |
| 1579 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1580 | /* sshbuf_dump(c->output, stderr); */ |
| 1581 | /* EOF received */ |
| 1582 | if (c->flags & CHAN_EOF_RCVD) { |
| 1583 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1584 | fatal("%s: channel %d: consume: %s", |
| 1585 | __func__, c->self, ssh_err(r)); |
| 1586 | } |
| 1587 | rdynamic_close(ssh, c); |
| 1588 | return; |
| 1589 | } |
| 1590 | /* check if the fixed size part of the packet is in buffer. */ |
| 1591 | if (have < 3) |
| 1592 | return; |
| 1593 | /* try to guess the protocol */ |
| 1594 | p = sshbuf_ptr(c->output); |
| 1595 | switch (p[0]) { |
| 1596 | case 0x04: |
| 1597 | /* switch input/output for reverse forwarding */ |
| 1598 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1599 | break; |
| 1600 | case 0x05: |
| 1601 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1602 | break; |
| 1603 | default: |
| 1604 | ret = -1; |
| 1605 | break; |
| 1606 | } |
| 1607 | if (ret < 0) { |
| 1608 | rdynamic_close(ssh, c); |
| 1609 | } else if (ret == 0) { |
| 1610 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1611 | /* send socks request to peer */ |
| 1612 | len = sshbuf_len(c->input); |
| 1613 | if (len > 0 && len < c->remote_window) { |
| 1614 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1615 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1616 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1617 | (r = sshpkt_send(ssh)) != 0) { |
| 1618 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1619 | c->self, ssh_err(r)); |
| 1620 | } |
| 1621 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1622 | fatal("%s: channel %d: consume: %s", |
| 1623 | __func__, c->self, ssh_err(r)); |
| 1624 | } |
| 1625 | c->remote_window -= len; |
| 1626 | } |
| 1627 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1628 | /* the connect failed */ |
| 1629 | rdynamic_close(ssh, c); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1633 | /* This is our fake X11 server socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1634 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1635 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1636 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1637 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1638 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1639 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1640 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1641 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1642 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1643 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1644 | if (!FD_ISSET(c->sock, readset)) |
| 1645 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1646 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1647 | debug("X11 connection requested."); |
| 1648 | addrlen = sizeof(addr); |
| 1649 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1650 | if (c->single_connection) { |
| 1651 | oerrno = errno; |
| 1652 | debug2("single_connection: closing X11 listener."); |
| 1653 | channel_close_fd(ssh, &c->sock); |
| 1654 | chan_mark_dead(ssh, c); |
| 1655 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1656 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1657 | if (newsock < 0) { |
| 1658 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1659 | errno != ECONNABORTED) |
| 1660 | error("accept: %.100s", strerror(errno)); |
| 1661 | if (errno == EMFILE || errno == ENFILE) |
| 1662 | c->notbefore = monotime() + 1; |
| 1663 | return; |
| 1664 | } |
| 1665 | set_nodelay(newsock); |
| 1666 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1667 | remote_port = get_peer_port(newsock); |
| 1668 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1669 | remote_ipaddr, remote_port); |
| 1670 | |
| 1671 | nc = channel_new(ssh, "accepted x11 socket", |
| 1672 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1673 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1674 | open_preamble(ssh, __func__, nc, "x11"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1675 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1676 | (r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1677 | fatal("%s: channel %i: reply %s", __func__, |
| 1678 | c->self, ssh_err(r)); |
| 1679 | } |
| 1680 | if ((r = sshpkt_send(ssh)) != 0) |
| 1681 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
| 1682 | free(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1683 | } |
| 1684 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1685 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1686 | port_open_helper(struct ssh *ssh, Channel *c, char *rtype) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1687 | { |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1688 | char *local_ipaddr = get_local_ipaddr(c->sock); |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 1689 | int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1690 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1691 | int remote_port = get_peer_port(c->sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1692 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1693 | |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1694 | if (remote_port == -1) { |
| 1695 | /* Fake addr/port to appease peers that validate it (Tectia) */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1696 | free(remote_ipaddr); |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1697 | remote_ipaddr = xstrdup("127.0.0.1"); |
| 1698 | remote_port = 65535; |
| 1699 | } |
| 1700 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1701 | free(c->remote_name); |
| 1702 | xasprintf(&c->remote_name, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1703 | "%s: listening port %d for %.100s port %d, " |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1704 | "connect from %.200s port %d to %.100s port %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1705 | rtype, c->listening_port, c->path, c->host_port, |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1706 | remote_ipaddr, remote_port, local_ipaddr, local_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1707 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1708 | open_preamble(ssh, __func__, c, rtype); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1709 | if (strcmp(rtype, "direct-tcpip") == 0) { |
| 1710 | /* target host, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1711 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1712 | (r = sshpkt_put_u32(ssh, c->host_port)) != 0) { |
| 1713 | fatal("%s: channel %i: reply %s", __func__, |
| 1714 | c->self, ssh_err(r)); |
| 1715 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1716 | } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) { |
| 1717 | /* target path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1718 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1719 | fatal("%s: channel %i: reply %s", __func__, |
| 1720 | c->self, ssh_err(r)); |
| 1721 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1722 | } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1723 | /* listen path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1724 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1725 | fatal("%s: channel %i: reply %s", __func__, |
| 1726 | c->self, ssh_err(r)); |
| 1727 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1728 | } else { |
| 1729 | /* listen address, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1730 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1731 | (r = sshpkt_put_u32(ssh, local_port)) != 0) { |
| 1732 | fatal("%s: channel %i: reply %s", __func__, |
| 1733 | c->self, ssh_err(r)); |
| 1734 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1735 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1736 | if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1737 | /* reserved for future owner/mode info */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1738 | if ((r = sshpkt_put_cstring(ssh, "")) != 0) { |
| 1739 | fatal("%s: channel %i: reply %s", __func__, |
| 1740 | c->self, ssh_err(r)); |
| 1741 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1742 | } else { |
| 1743 | /* originator host and port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1744 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1745 | (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) { |
| 1746 | fatal("%s: channel %i: reply %s", __func__, |
| 1747 | c->self, ssh_err(r)); |
| 1748 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1749 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1750 | if ((r = sshpkt_send(ssh)) != 0) |
| 1751 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1752 | free(remote_ipaddr); |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1753 | free(local_ipaddr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1756 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1757 | 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] | 1758 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1759 | ssh->chanctxt->x11_refuse_time = refuse_time; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1762 | /* |
| 1763 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1764 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1765 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1766 | channel_post_port_listener(struct ssh *ssh, Channel *c, |
| 1767 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1768 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1769 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1770 | struct sockaddr_storage addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1771 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1772 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1773 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1774 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1775 | if (!FD_ISSET(c->sock, readset)) |
| 1776 | return; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1777 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1778 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
| 1779 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1780 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1781 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1782 | nextstate = SSH_CHANNEL_OPENING; |
| 1783 | rtype = "forwarded-tcpip"; |
| 1784 | } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) { |
| 1785 | nextstate = SSH_CHANNEL_OPENING; |
| 1786 | rtype = "forwarded-streamlocal@openssh.com"; |
| 1787 | } else if (c->host_port == PORT_STREAMLOCAL) { |
| 1788 | nextstate = SSH_CHANNEL_OPENING; |
| 1789 | rtype = "direct-streamlocal@openssh.com"; |
| 1790 | } else if (c->host_port == 0) { |
| 1791 | nextstate = SSH_CHANNEL_DYNAMIC; |
| 1792 | rtype = "dynamic-tcpip"; |
| 1793 | } else { |
| 1794 | nextstate = SSH_CHANNEL_OPENING; |
| 1795 | rtype = "direct-tcpip"; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1796 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1797 | |
| 1798 | addrlen = sizeof(addr); |
| 1799 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1800 | if (newsock < 0) { |
| 1801 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1802 | errno != ECONNABORTED) |
| 1803 | error("accept: %.100s", strerror(errno)); |
| 1804 | if (errno == EMFILE || errno == ENFILE) |
| 1805 | c->notbefore = monotime() + 1; |
| 1806 | return; |
| 1807 | } |
| 1808 | if (c->host_port != PORT_STREAMLOCAL) |
| 1809 | set_nodelay(newsock); |
| 1810 | nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1, |
| 1811 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
| 1812 | nc->listening_port = c->listening_port; |
| 1813 | nc->host_port = c->host_port; |
| 1814 | if (c->path != NULL) |
| 1815 | nc->path = xstrdup(c->path); |
| 1816 | |
| 1817 | if (nextstate != SSH_CHANNEL_DYNAMIC) |
| 1818 | port_open_helper(ssh, nc, rtype); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * This is the authentication agent socket listening for connections from |
| 1823 | * clients. |
| 1824 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1825 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1826 | channel_post_auth_listener(struct ssh *ssh, Channel *c, |
| 1827 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1828 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1829 | Channel *nc; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1830 | int r, newsock; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1831 | struct sockaddr_storage addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1832 | socklen_t addrlen; |
| 1833 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1834 | if (!FD_ISSET(c->sock, readset)) |
| 1835 | return; |
| 1836 | |
| 1837 | addrlen = sizeof(addr); |
| 1838 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1839 | if (newsock < 0) { |
| 1840 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1841 | if (errno == EMFILE || errno == ENFILE) |
| 1842 | c->notbefore = monotime() + 1; |
| 1843 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1844 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1845 | nc = channel_new(ssh, "accepted auth socket", |
| 1846 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1847 | c->local_window_max, c->local_maxpacket, |
| 1848 | 0, "accepted auth socket", 1); |
| 1849 | open_preamble(ssh, __func__, nc, "auth-agent@openssh.com"); |
| 1850 | if ((r = sshpkt_send(ssh)) != 0) |
| 1851 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1852 | } |
| 1853 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1854 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1855 | channel_post_connecting(struct ssh *ssh, Channel *c, |
| 1856 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1857 | { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1858 | int err = 0, sock, isopen, r; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1859 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1860 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1861 | if (!FD_ISSET(c->sock, writeset)) |
| 1862 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1863 | if (!c->have_remote_id) |
| 1864 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1865 | /* for rdynamic the OPEN_CONFIRMATION has been sent already */ |
| 1866 | isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1867 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { |
| 1868 | err = errno; |
| 1869 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1870 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1871 | if (err == 0) { |
| 1872 | debug("channel %d: connected to %s port %d", |
| 1873 | c->self, c->connect_ctx.host, c->connect_ctx.port); |
| 1874 | channel_connect_ctx_free(&c->connect_ctx); |
| 1875 | c->type = SSH_CHANNEL_OPEN; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1876 | if (isopen) { |
| 1877 | /* no message necessary */ |
| 1878 | } else { |
| 1879 | if ((r = sshpkt_start(ssh, |
| 1880 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1881 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1882 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1883 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1884 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) |
| 1885 | != 0) |
| 1886 | fatal("%s: channel %i: confirm: %s", __func__, |
| 1887 | c->self, ssh_err(r)); |
| 1888 | if ((r = sshpkt_send(ssh)) != 0) |
| 1889 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1890 | ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } else { |
| 1893 | debug("channel %d: connection failed: %s", |
| 1894 | c->self, strerror(err)); |
| 1895 | /* Try next address, if any */ |
| 1896 | if ((sock = connect_next(&c->connect_ctx)) > 0) { |
| 1897 | close(c->sock); |
| 1898 | c->sock = c->rfd = c->wfd = sock; |
| 1899 | channel_find_maxfd(ssh->chanctxt); |
| 1900 | return; |
| 1901 | } |
| 1902 | /* Exhausted all addresses */ |
| 1903 | error("connect_to %.100s port %d: failed.", |
| 1904 | c->connect_ctx.host, c->connect_ctx.port); |
| 1905 | channel_connect_ctx_free(&c->connect_ctx); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1906 | if (isopen) { |
| 1907 | rdynamic_close(ssh, c); |
| 1908 | } else { |
| 1909 | if ((r = sshpkt_start(ssh, |
| 1910 | SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1911 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1912 | (r = sshpkt_put_u32(ssh, |
| 1913 | SSH2_OPEN_CONNECT_FAILED)) != 0 || |
| 1914 | (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1915 | (r = sshpkt_put_cstring(ssh, "")) != 0) { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1916 | fatal("%s: channel %i: failure: %s", __func__, |
| 1917 | c->self, ssh_err(r)); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1918 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1919 | if ((r = sshpkt_send(ssh)) != 0) |
| 1920 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1921 | ssh_err(r)); |
| 1922 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1923 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1924 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1927 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1928 | channel_handle_rfd(struct ssh *ssh, Channel *c, |
| 1929 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1930 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1931 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1932 | ssize_t len; |
| 1933 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1934 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1935 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1936 | |
| 1937 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1938 | return 1; |
| 1939 | |
| 1940 | errno = 0; |
| 1941 | len = read(c->rfd, buf, sizeof(buf)); |
| 1942 | if (len < 0 && (errno == EINTR || |
| 1943 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1944 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1945 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1946 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1947 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1948 | if ((!c->isatty && len <= 0) || |
| 1949 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1950 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1951 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1952 | c->self, c->rfd, len); |
| 1953 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1954 | debug2("channel %d: not open", c->self); |
| 1955 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1956 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1957 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1958 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1959 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1960 | return -1; |
| 1961 | } |
| 1962 | if (c->input_filter != NULL) { |
| 1963 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1964 | debug2("channel %d: filter stops", c->self); |
| 1965 | chan_read_failed(ssh, c); |
| 1966 | } |
| 1967 | } else if (c->datagram) { |
| 1968 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1969 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1970 | c->self, ssh_err(r)); |
| 1971 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1972 | fatal("%s: channel %d: put data: %s", __func__, |
| 1973 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1974 | } |
| 1975 | return 1; |
| 1976 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1977 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1978 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1979 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1980 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1981 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1982 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1983 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 1984 | size_t dlen, olen = 0; |
| 1985 | int r, len; |
| 1986 | |
| 1987 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 1988 | sshbuf_len(c->output) == 0) |
| 1989 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1990 | |
| 1991 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1992 | olen = sshbuf_len(c->output); |
| 1993 | if (c->output_filter != NULL) { |
| 1994 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 1995 | debug2("channel %d: filter stops", c->self); |
| 1996 | if (c->type != SSH_CHANNEL_OPEN) |
| 1997 | chan_mark_dead(ssh, c); |
| 1998 | else |
| 1999 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2000 | return -1; |
| 2001 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2002 | } else if (c->datagram) { |
| 2003 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 2004 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 2005 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 2006 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2007 | } else { |
| 2008 | buf = data = sshbuf_mutable_ptr(c->output); |
| 2009 | dlen = sshbuf_len(c->output); |
| 2010 | } |
| 2011 | |
| 2012 | if (c->datagram) { |
| 2013 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 2014 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2015 | free(data); |
| 2016 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2017 | errno == EWOULDBLOCK)) |
| 2018 | return 1; |
| 2019 | if (len <= 0) |
| 2020 | goto write_fail; |
| 2021 | goto out; |
| 2022 | } |
| 2023 | |
| 2024 | #ifdef _AIX |
| 2025 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 2026 | if (c->wfd_isatty) |
| 2027 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 2028 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2029 | |
| 2030 | len = write(c->wfd, buf, dlen); |
| 2031 | if (len < 0 && |
| 2032 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 2033 | return 1; |
| 2034 | if (len <= 0) { |
| 2035 | write_fail: |
| 2036 | if (c->type != SSH_CHANNEL_OPEN) { |
| 2037 | debug2("channel %d: not open", c->self); |
| 2038 | chan_mark_dead(ssh, c); |
| 2039 | return -1; |
| 2040 | } else { |
| 2041 | chan_write_failed(ssh, c); |
| 2042 | } |
| 2043 | return -1; |
| 2044 | } |
| 2045 | #ifndef BROKEN_TCGETATTR_ICANON |
| 2046 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 2047 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 2048 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 2049 | /* |
| 2050 | * Simulate echo to reduce the impact of |
| 2051 | * traffic analysis. We need to match the |
| 2052 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 2053 | * (4 byte channel id + buf) |
| 2054 | */ |
| 2055 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 2056 | (r = sshpkt_send(ssh)) != 0) |
| 2057 | fatal("%s: channel %d: ignore: %s", |
| 2058 | __func__, c->self, ssh_err(r)); |
| 2059 | } |
| 2060 | } |
| 2061 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 2062 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 2063 | fatal("%s: channel %d: consume: %s", |
| 2064 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2065 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2066 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2067 | c->local_consumed += olen - sshbuf_len(c->output); |
| 2068 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2069 | return 1; |
| 2070 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2071 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2072 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2073 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 2074 | fd_set *readset, fd_set *writeset) |
| 2075 | { |
| 2076 | int r; |
| 2077 | ssize_t len; |
| 2078 | |
| 2079 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 2080 | return 1; |
| 2081 | |
| 2082 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 2083 | sshbuf_len(c->extended)); |
| 2084 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
| 2085 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2086 | errno == EWOULDBLOCK)) |
| 2087 | return 1; |
| 2088 | if (len <= 0) { |
| 2089 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2090 | channel_close_fd(ssh, &c->efd); |
| 2091 | } else { |
| 2092 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2093 | fatal("%s: channel %d: consume: %s", |
| 2094 | __func__, c->self, ssh_err(r)); |
| 2095 | } |
| 2096 | c->local_consumed += len; |
| 2097 | } |
| 2098 | return 1; |
| 2099 | } |
| 2100 | |
| 2101 | static int |
| 2102 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2103 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2104 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2105 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2106 | int r; |
| 2107 | ssize_t len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2108 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2109 | if (!c->detach_close && !FD_ISSET(c->efd, readset)) |
| 2110 | return 1; |
| 2111 | |
| 2112 | len = read(c->efd, buf, sizeof(buf)); |
| 2113 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
| 2114 | if (len < 0 && (errno == EINTR || ((errno == EAGAIN || |
| 2115 | errno == EWOULDBLOCK) && !c->detach_close))) |
| 2116 | return 1; |
| 2117 | if (len <= 0) { |
| 2118 | debug2("channel %d: closing read-efd %d", |
| 2119 | c->self, c->efd); |
| 2120 | channel_close_fd(ssh, &c->efd); |
| 2121 | } else { |
| 2122 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2123 | debug3("channel %d: discard efd", |
| 2124 | c->self); |
| 2125 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2126 | fatal("%s: channel %d: append: %s", |
| 2127 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | return 1; |
| 2131 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2132 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2133 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2134 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2135 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2136 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2137 | if (c->efd == -1) |
| 2138 | return 1; |
| 2139 | |
| 2140 | /** XXX handle drain efd, too */ |
| 2141 | |
| 2142 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2143 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2144 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2145 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2146 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2147 | |
| 2148 | return 1; |
| 2149 | } |
| 2150 | |
| 2151 | static int |
| 2152 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2153 | { |
| 2154 | int r; |
| 2155 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2156 | if (c->type == SSH_CHANNEL_OPEN && |
| 2157 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2158 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2159 | c->local_maxpacket*3) || |
| 2160 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2161 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2162 | if (!c->have_remote_id) |
| 2163 | fatal(":%s: channel %d: no remote id", |
| 2164 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2165 | if ((r = sshpkt_start(ssh, |
| 2166 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2167 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2168 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2169 | (r = sshpkt_send(ssh)) != 0) { |
| 2170 | fatal("%s: channel %i: %s", __func__, |
| 2171 | c->self, ssh_err(r)); |
| 2172 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2173 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2174 | c->self, c->local_window, |
| 2175 | c->local_consumed); |
| 2176 | c->local_window += c->local_consumed; |
| 2177 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2178 | } |
| 2179 | return 1; |
| 2180 | } |
| 2181 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2182 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2183 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2184 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2185 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2186 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2187 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2188 | channel_handle_efd(ssh, c, readset, writeset); |
| 2189 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2190 | } |
| 2191 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2192 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2193 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2194 | { |
| 2195 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2196 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2197 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2198 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2199 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2200 | if (sshbuf_len(c->input) < need) { |
| 2201 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2202 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2203 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2204 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2205 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2206 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2207 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2208 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2209 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2210 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2211 | fatal("%s: channel %d: append: %s", |
| 2212 | __func__, c->self, ssh_err(r)); |
| 2213 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2214 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2215 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2219 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2220 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2221 | { |
| 2222 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2223 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2224 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2225 | return; |
| 2226 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2227 | return; |
| 2228 | if (c->mux_pause) |
| 2229 | return; |
| 2230 | |
| 2231 | /* |
| 2232 | * Don't not read past the precise end of packets to |
| 2233 | * avoid disrupting fd passing. |
| 2234 | */ |
| 2235 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2236 | return; |
| 2237 | /* XXX sshbuf_peek_u32 */ |
| 2238 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2239 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2240 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2241 | debug2("channel %d: packet too big %u > %u", |
| 2242 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2243 | chan_rcvd_oclose(ssh, c); |
| 2244 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2245 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2246 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2247 | return; |
| 2248 | if (c->mux_rcb(ssh, c) != 0) { |
| 2249 | debug("channel %d: mux_rcb failed", c->self); |
| 2250 | chan_mark_dead(ssh, c); |
| 2251 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2256 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2257 | fd_set *readset, fd_set *writeset) |
| 2258 | { |
| 2259 | ssize_t len; |
| 2260 | int r; |
| 2261 | |
| 2262 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2263 | sshbuf_len(c->output) == 0) |
| 2264 | return; |
| 2265 | |
| 2266 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
| 2267 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 2268 | return; |
| 2269 | if (len <= 0) { |
| 2270 | chan_mark_dead(ssh, c); |
| 2271 | return; |
| 2272 | } |
| 2273 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2274 | fatal("%s: channel %d: consume: %s", __func__, |
| 2275 | c->self, ssh_err(r)); |
| 2276 | } |
| 2277 | |
| 2278 | static void |
| 2279 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2280 | fd_set *readset, fd_set *writeset) |
| 2281 | { |
| 2282 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2283 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2284 | } |
| 2285 | |
| 2286 | static void |
| 2287 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2288 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2289 | { |
| 2290 | Channel *nc; |
| 2291 | struct sockaddr_storage addr; |
| 2292 | socklen_t addrlen; |
| 2293 | int newsock; |
| 2294 | uid_t euid; |
| 2295 | gid_t egid; |
| 2296 | |
| 2297 | if (!FD_ISSET(c->sock, readset)) |
| 2298 | return; |
| 2299 | |
| 2300 | debug("multiplexing control connection"); |
| 2301 | |
| 2302 | /* |
| 2303 | * Accept connection on control socket |
| 2304 | */ |
| 2305 | memset(&addr, 0, sizeof(addr)); |
| 2306 | addrlen = sizeof(addr); |
| 2307 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2308 | &addrlen)) == -1) { |
| 2309 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2310 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2311 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2312 | return; |
| 2313 | } |
| 2314 | |
| 2315 | if (getpeereid(newsock, &euid, &egid) < 0) { |
| 2316 | error("%s getpeereid failed: %s", __func__, |
| 2317 | strerror(errno)); |
| 2318 | close(newsock); |
| 2319 | return; |
| 2320 | } |
| 2321 | if ((euid != 0) && (getuid() != euid)) { |
| 2322 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2323 | (u_int)euid, (u_int)getuid()); |
| 2324 | close(newsock); |
| 2325 | return; |
| 2326 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2327 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2328 | newsock, newsock, -1, c->local_window_max, |
| 2329 | c->local_maxpacket, 0, "mux-control", 1); |
| 2330 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2331 | 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] | 2332 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2333 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2334 | /* mux state transitions must not elicit protocol messages */ |
| 2335 | nc->flags |= CHAN_LOCAL; |
| 2336 | } |
| 2337 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2338 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2339 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2340 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2341 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2342 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2343 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2344 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2345 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2346 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2347 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2348 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2349 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2350 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2351 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2352 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2353 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2354 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2355 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2356 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2357 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2358 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2359 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2360 | |
| 2361 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2362 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2363 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2364 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2365 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2366 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2367 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2368 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2369 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2370 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2371 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2372 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2373 | |
| 2374 | sc->channel_pre = pre; |
| 2375 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2376 | } |
| 2377 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2378 | /* gc dead channels */ |
| 2379 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2380 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2381 | { |
| 2382 | if (c == NULL) |
| 2383 | return; |
| 2384 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2385 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2386 | return; |
djm@openbsd.org | f1dd179 | 2018-10-04 00:10:11 +0000 | [diff] [blame] | 2387 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2388 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2389 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2390 | /* if we still have a callback */ |
| 2391 | if (c->detach_user != NULL) |
| 2392 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2393 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2394 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2395 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2396 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2397 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2398 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2399 | } |
| 2400 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2401 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2402 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2403 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2404 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2405 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2406 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2407 | struct ssh_channels *sc = ssh->chanctxt; |
| 2408 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2409 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2410 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2411 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2412 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2413 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2414 | if (unpause_secs != NULL) |
| 2415 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2416 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2417 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2418 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2419 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2420 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2421 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2422 | c->delayed = 0; |
| 2423 | else |
| 2424 | continue; |
| 2425 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2426 | if (ftab[c->type] != NULL) { |
| 2427 | /* |
| 2428 | * Run handlers that are not paused. |
| 2429 | */ |
| 2430 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2431 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2432 | else if (unpause_secs != NULL) { |
| 2433 | /* |
| 2434 | * Collect the time that the earliest |
| 2435 | * channel comes off pause. |
| 2436 | */ |
| 2437 | debug3("%s: chan %d: skip for %d more seconds", |
| 2438 | __func__, c->self, |
| 2439 | (int)(c->notbefore - now)); |
| 2440 | if (*unpause_secs == 0 || |
| 2441 | (c->notbefore - now) < *unpause_secs) |
| 2442 | *unpause_secs = c->notbefore - now; |
| 2443 | } |
| 2444 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2445 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2446 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2447 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2448 | debug3("%s: first channel unpauses in %d seconds", |
| 2449 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2450 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2451 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2452 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2453 | * Create sockets before allocating the select bitmasks. |
| 2454 | * This is necessary for things that need to happen after reading |
| 2455 | * the network-input but before channel_prepare_select(). |
| 2456 | */ |
| 2457 | static void |
| 2458 | channel_before_prepare_select(struct ssh *ssh) |
| 2459 | { |
| 2460 | struct ssh_channels *sc = ssh->chanctxt; |
| 2461 | Channel *c; |
| 2462 | u_int i, oalloc; |
| 2463 | |
| 2464 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2465 | c = sc->channels[i]; |
| 2466 | if (c == NULL) |
| 2467 | continue; |
| 2468 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2469 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2474 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2475 | * select bitmasks. |
| 2476 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2477 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2478 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2479 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2480 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2481 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2482 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2483 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2484 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2485 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2486 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2487 | nfdset = howmany(n+1, NFDBITS); |
| 2488 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2489 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2490 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2491 | sz = nfdset * sizeof(fd_mask); |
| 2492 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2493 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2494 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2495 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2496 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2497 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2498 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2499 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2500 | memset(*readsetp, 0, sz); |
| 2501 | memset(*writesetp, 0, sz); |
| 2502 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2503 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2504 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2505 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2506 | } |
| 2507 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2508 | /* |
| 2509 | * After select, perform any appropriate operations for channels which have |
| 2510 | * events pending. |
| 2511 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2512 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2513 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2514 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2515 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2516 | } |
| 2517 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2518 | /* |
| 2519 | * Enqueue data for channels with open or draining c->input. |
| 2520 | */ |
| 2521 | static void |
| 2522 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2523 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2524 | size_t len, plen; |
| 2525 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2526 | int r; |
| 2527 | |
| 2528 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2529 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2530 | /* |
| 2531 | * input-buffer is empty and read-socket shutdown: |
| 2532 | * tell peer, that we will not send more data: |
| 2533 | * send IEOF. |
| 2534 | * hack for extended data: delay EOF if EFD still |
| 2535 | * in use. |
| 2536 | */ |
| 2537 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2538 | debug2("channel %d: " |
| 2539 | "ibuf_empty delayed efd %d/(%zu)", |
| 2540 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2541 | else |
| 2542 | chan_ibuf_empty(ssh, c); |
| 2543 | } |
| 2544 | return; |
| 2545 | } |
| 2546 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2547 | if (!c->have_remote_id) |
| 2548 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2549 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2550 | if (c->datagram) { |
| 2551 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2552 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2553 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2554 | c->self, ssh_err(r)); |
| 2555 | /* |
| 2556 | * XXX this does tail-drop on the datagram queue which is |
| 2557 | * usually suboptimal compared to head-drop. Better to have |
| 2558 | * backpressure at read time? (i.e. read + discard) |
| 2559 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2560 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2561 | debug("channel %d: datagram too big", c->self); |
| 2562 | return; |
| 2563 | } |
| 2564 | /* Enqueue it */ |
| 2565 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2566 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2567 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2568 | (r = sshpkt_send(ssh)) != 0) { |
| 2569 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2570 | c->self, ssh_err(r)); |
| 2571 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2572 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | /* Enqueue packet for buffered data. */ |
| 2577 | if (len > c->remote_window) |
| 2578 | len = c->remote_window; |
| 2579 | if (len > c->remote_maxpacket) |
| 2580 | len = c->remote_maxpacket; |
| 2581 | if (len == 0) |
| 2582 | return; |
| 2583 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2584 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2585 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2586 | (r = sshpkt_send(ssh)) != 0) { |
| 2587 | fatal("%s: channel %i: data: %s", __func__, |
| 2588 | c->self, ssh_err(r)); |
| 2589 | } |
| 2590 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2591 | fatal("%s: channel %i: consume: %s", __func__, |
| 2592 | c->self, ssh_err(r)); |
| 2593 | c->remote_window -= len; |
| 2594 | } |
| 2595 | |
| 2596 | /* |
| 2597 | * Enqueue data for channels with open c->extended in read mode. |
| 2598 | */ |
| 2599 | static void |
| 2600 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2601 | { |
| 2602 | size_t len; |
| 2603 | int r; |
| 2604 | |
| 2605 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2606 | return; |
| 2607 | |
| 2608 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2609 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2610 | if (len > c->remote_window) |
| 2611 | len = c->remote_window; |
| 2612 | if (len > c->remote_maxpacket) |
| 2613 | len = c->remote_maxpacket; |
| 2614 | if (len == 0) |
| 2615 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2616 | if (!c->have_remote_id) |
| 2617 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2618 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2619 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2620 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2621 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2622 | (r = sshpkt_send(ssh)) != 0) { |
| 2623 | fatal("%s: channel %i: data: %s", __func__, |
| 2624 | c->self, ssh_err(r)); |
| 2625 | } |
| 2626 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2627 | fatal("%s: channel %i: consume: %s", __func__, |
| 2628 | c->self, ssh_err(r)); |
| 2629 | c->remote_window -= len; |
| 2630 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2631 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2632 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2633 | /* 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] | 2634 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2635 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2636 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2637 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2638 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2639 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2640 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2641 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2642 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2643 | if (c == NULL) |
| 2644 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2645 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2646 | /* |
| 2647 | * We are only interested in channels that can have buffered |
| 2648 | * incoming data. |
| 2649 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2650 | if (c->type != SSH_CHANNEL_OPEN) |
| 2651 | continue; |
| 2652 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2653 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2654 | debug3("channel %d: will not send data after close", |
| 2655 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2656 | continue; |
| 2657 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2658 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2659 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2660 | if (c->istate == CHAN_INPUT_OPEN || |
| 2661 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2662 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2663 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2664 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2665 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2666 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2667 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2668 | } |
| 2669 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2670 | /* -- mux proxy support */ |
| 2671 | |
| 2672 | /* |
| 2673 | * When multiplexing channel messages for mux clients we have to deal |
| 2674 | * with downstream messages from the mux client and upstream messages |
| 2675 | * from the ssh server: |
| 2676 | * 1) Handling downstream messages is straightforward and happens |
| 2677 | * in channel_proxy_downstream(): |
| 2678 | * - We forward all messages (mostly) unmodified to the server. |
| 2679 | * - However, in order to route messages from upstream to the correct |
| 2680 | * downstream client, we have to replace the channel IDs used by the |
| 2681 | * mux clients with a unique channel ID because the mux clients might |
| 2682 | * use conflicting channel IDs. |
| 2683 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2684 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2685 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2686 | * with the newly allocated channel ID. |
| 2687 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2688 | * channels and processed by channel_proxy_upstream(). The local channel ID |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2689 | * is then translated back to the original mux client ID. |
| 2690 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2691 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2692 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2693 | * downstream mux client are removed. |
| 2694 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2695 | * requires more work, because they are not addressed to a specific |
| 2696 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2697 | * out whether the request is addressed to the local client or a |
| 2698 | * specific downstream client based on the listen-address/port. |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 2699 | * 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] | 2700 | * not supported as the matching session/channel cannot be identified |
| 2701 | * easily. |
| 2702 | */ |
| 2703 | |
| 2704 | /* |
| 2705 | * receive packets from downstream mux clients: |
| 2706 | * channel callback fired on read from mux client, creates |
| 2707 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2708 | * on channel creation. |
| 2709 | */ |
| 2710 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2711 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2712 | { |
| 2713 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2714 | struct sshbuf *original = NULL, *modified = NULL; |
| 2715 | const u_char *cp; |
| 2716 | char *ctype = NULL, *listen_host = NULL; |
| 2717 | u_char type; |
| 2718 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2719 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2720 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2721 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2722 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2723 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2724 | != 0) { |
| 2725 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2726 | return -1; |
| 2727 | } |
| 2728 | if (have < 2) { |
| 2729 | error("%s: short message", __func__); |
| 2730 | return -1; |
| 2731 | } |
| 2732 | type = cp[1]; |
| 2733 | /* skip padlen + type */ |
| 2734 | cp += 2; |
| 2735 | have -= 2; |
| 2736 | if (ssh_packet_log_type(type)) |
| 2737 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2738 | downstream->self, type); |
| 2739 | |
| 2740 | switch (type) { |
| 2741 | case SSH2_MSG_CHANNEL_OPEN: |
| 2742 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2743 | (modified = sshbuf_new()) == NULL) { |
| 2744 | error("%s: alloc", __func__); |
| 2745 | goto out; |
| 2746 | } |
| 2747 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2748 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2749 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2750 | goto out; |
| 2751 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2752 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2753 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2754 | c->mux_ctx = downstream; /* point to mux client */ |
| 2755 | c->mux_downstream_id = id; /* original downstream id */ |
| 2756 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2757 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2758 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2759 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2760 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2761 | goto out; |
| 2762 | } |
| 2763 | break; |
| 2764 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2765 | /* |
| 2766 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2767 | * need to parse 'remote_id' instead of 'ctype'. |
| 2768 | */ |
| 2769 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2770 | (modified = sshbuf_new()) == NULL) { |
| 2771 | error("%s: alloc", __func__); |
| 2772 | goto out; |
| 2773 | } |
| 2774 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2775 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2776 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2777 | goto out; |
| 2778 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2779 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2780 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2781 | c->mux_ctx = downstream; /* point to mux client */ |
| 2782 | c->mux_downstream_id = id; |
| 2783 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2784 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2785 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2786 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2787 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2788 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2789 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2790 | goto out; |
| 2791 | } |
| 2792 | break; |
| 2793 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2794 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2795 | error("%s: alloc", __func__); |
| 2796 | goto out; |
| 2797 | } |
| 2798 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2799 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2800 | goto out; |
| 2801 | } |
| 2802 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2803 | error("%s: unsupported request %s", __func__, ctype); |
| 2804 | goto out; |
| 2805 | } |
| 2806 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2807 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2808 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2809 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2810 | goto out; |
| 2811 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2812 | if (listen_port > 65535) { |
| 2813 | error("%s: tcpip-forward for %s: bad port %u", |
| 2814 | __func__, listen_host, listen_port); |
| 2815 | goto out; |
| 2816 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2817 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 2818 | permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2819 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2820 | listen_host = NULL; |
| 2821 | break; |
| 2822 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2823 | if (have < 4) |
| 2824 | break; |
| 2825 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2826 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2827 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2828 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2829 | else |
| 2830 | c->flags |= CHAN_CLOSE_SENT; |
| 2831 | } |
| 2832 | break; |
| 2833 | } |
| 2834 | if (modified) { |
| 2835 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2836 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2837 | (r = sshpkt_send(ssh)) != 0) { |
| 2838 | error("%s: send %s", __func__, ssh_err(r)); |
| 2839 | goto out; |
| 2840 | } |
| 2841 | } else { |
| 2842 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2843 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2844 | (r = sshpkt_send(ssh)) != 0) { |
| 2845 | error("%s: send %s", __func__, ssh_err(r)); |
| 2846 | goto out; |
| 2847 | } |
| 2848 | } |
| 2849 | ret = 0; |
| 2850 | out: |
| 2851 | free(ctype); |
| 2852 | free(listen_host); |
| 2853 | sshbuf_free(original); |
| 2854 | sshbuf_free(modified); |
| 2855 | return ret; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * receive packets from upstream server and de-multiplex packets |
| 2860 | * to correct downstream: |
| 2861 | * implemented as a helper for channel input handlers, |
| 2862 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2863 | */ |
| 2864 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2865 | 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] | 2866 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2867 | struct sshbuf *b = NULL; |
| 2868 | Channel *downstream; |
| 2869 | const u_char *cp = NULL; |
| 2870 | size_t len; |
| 2871 | int r; |
| 2872 | |
| 2873 | /* |
| 2874 | * When receiving packets from the peer we need to check whether we |
| 2875 | * 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] | 2876 | * restore the original channel id and keep track of CLOSE messages, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2877 | * so we can cleanup the channel. |
| 2878 | */ |
| 2879 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2880 | return 0; |
| 2881 | if ((downstream = c->mux_ctx) == NULL) |
| 2882 | return 0; |
| 2883 | switch (type) { |
| 2884 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2885 | case SSH2_MSG_CHANNEL_DATA: |
| 2886 | case SSH2_MSG_CHANNEL_EOF: |
| 2887 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2888 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2889 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2890 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2891 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2892 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2893 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2894 | break; |
| 2895 | default: |
| 2896 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2897 | c->self, type); |
| 2898 | return 0; |
| 2899 | } |
| 2900 | if ((b = sshbuf_new()) == NULL) { |
| 2901 | error("%s: alloc reply", __func__); |
| 2902 | goto out; |
| 2903 | } |
| 2904 | /* get remaining payload (after id) */ |
| 2905 | cp = sshpkt_ptr(ssh, &len); |
| 2906 | if (cp == NULL) { |
| 2907 | error("%s: no packet", __func__); |
| 2908 | goto out; |
| 2909 | } |
| 2910 | /* translate id and send to muxclient */ |
| 2911 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2912 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2913 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2914 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2915 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2916 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2917 | goto out; |
| 2918 | } |
| 2919 | /* sshbuf_dump(b, stderr); */ |
| 2920 | if (ssh_packet_log_type(type)) |
| 2921 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2922 | type); |
| 2923 | out: |
| 2924 | /* update state */ |
| 2925 | switch (type) { |
| 2926 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2927 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2928 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2929 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2930 | c->have_remote_id = 1; |
| 2931 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2932 | break; |
| 2933 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2934 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2935 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2936 | else |
| 2937 | c->flags |= CHAN_CLOSE_RCVD; |
| 2938 | break; |
| 2939 | } |
| 2940 | sshbuf_free(b); |
| 2941 | return 1; |
| 2942 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2943 | |
| 2944 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2945 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2946 | /* Parse a channel ID from the current packet */ |
| 2947 | static int |
| 2948 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2949 | { |
| 2950 | u_int32_t id; |
| 2951 | int r; |
| 2952 | |
| 2953 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2954 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2955 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2956 | } |
| 2957 | if (id > INT_MAX) { |
| 2958 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2959 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2960 | } |
| 2961 | return (int)id; |
| 2962 | } |
| 2963 | |
| 2964 | /* Lookup a channel from an ID in the current packet */ |
| 2965 | static Channel * |
| 2966 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2967 | { |
| 2968 | int id = channel_parse_id(ssh, where, what); |
| 2969 | Channel *c; |
| 2970 | |
| 2971 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2972 | ssh_packet_disconnect(ssh, |
| 2973 | "%s packet referred to nonexistent channel %d", what, id); |
| 2974 | } |
| 2975 | return c; |
| 2976 | } |
| 2977 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2978 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2979 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2980 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2981 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2982 | size_t data_len, win_len; |
| 2983 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 2984 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2985 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2986 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2987 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2988 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2989 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2990 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2991 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 2992 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2993 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2994 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2995 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2996 | /* Get the data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2997 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) |
| 2998 | fatal("%s: channel %d: get data: %s", __func__, |
| 2999 | c->self, ssh_err(r)); |
| 3000 | ssh_packet_check_eom(ssh); |
| 3001 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 3002 | win_len = data_len; |
| 3003 | if (c->datagram) |
| 3004 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3005 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3006 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3007 | * The sending side reduces its window as it sends data, so we |
| 3008 | * must 'fake' consumption of the data in order to ensure that window |
| 3009 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3010 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3011 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 3012 | c->local_window -= win_len; |
| 3013 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3014 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 3015 | } |
| 3016 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3017 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3018 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3019 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3020 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3021 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3022 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3023 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3024 | c->self, win_len, c->local_window); |
| 3025 | return 0; |
| 3026 | } |
| 3027 | c->local_window -= win_len; |
| 3028 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3029 | if (c->datagram) { |
| 3030 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 3031 | fatal("%s: channel %d: append datagram: %s", |
| 3032 | __func__, c->self, ssh_err(r)); |
| 3033 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 3034 | fatal("%s: channel %d: append data: %s", |
| 3035 | __func__, c->self, ssh_err(r)); |
| 3036 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3037 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3038 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3039 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3040 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3041 | 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] | 3042 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3043 | const u_char *data; |
| 3044 | size_t data_len; |
| 3045 | u_int32_t tcode; |
| 3046 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 3047 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3048 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3049 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3050 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3051 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3052 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3053 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3054 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3055 | if (c->flags & CHAN_EOF_RCVD) { |
| 3056 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3057 | debug("channel %d: accepting ext data after eof", |
| 3058 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3059 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3060 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 3061 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 3062 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3063 | |
| 3064 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 3065 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 3066 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3067 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3068 | if (c->efd == -1 || |
| 3069 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 3070 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 3071 | logit("channel %d: bad ext data", 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 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3074 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) { |
| 3075 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 3076 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 3077 | } |
| 3078 | ssh_packet_check_eom(ssh); |
| 3079 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3080 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3081 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3082 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3083 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3084 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3085 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3086 | /* XXX sshpkt_getb? */ |
| 3087 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3088 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3089 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3090 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3091 | } |
| 3092 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3093 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3094 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3095 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3096 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3097 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3098 | ssh_packet_check_eom(ssh); |
| 3099 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3100 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3101 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3102 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3103 | |
| 3104 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3105 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3106 | debug("channel %d: FORCE input drain", c->self); |
| 3107 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3108 | if (sshbuf_len(c->input) == 0) |
| 3109 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3110 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3111 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3112 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3113 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3114 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3115 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3116 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3117 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3118 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3119 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3120 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3121 | ssh_packet_check_eom(ssh); |
| 3122 | chan_rcvd_oclose(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3123 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3124 | } |
| 3125 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3126 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3127 | 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] | 3128 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3129 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3130 | u_int32_t remote_window, remote_maxpacket; |
| 3131 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3132 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3133 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3134 | return 0; |
| 3135 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3136 | packet_disconnect("Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3137 | "non-opening channel %d.", c->self); |
| 3138 | /* |
| 3139 | * Record the remote channel number and mark that the channel |
| 3140 | * is now open. |
| 3141 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3142 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3143 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3144 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) { |
| 3145 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
| 3146 | packet_disconnect("Invalid open confirmation message"); |
| 3147 | } |
| 3148 | ssh_packet_check_eom(ssh); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3149 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3150 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3151 | c->remote_window = remote_window; |
| 3152 | c->remote_maxpacket = remote_maxpacket; |
| 3153 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3154 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3155 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3156 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3157 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3158 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3159 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3160 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3161 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3162 | } |
| 3163 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3164 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3165 | reason2txt(int reason) |
| 3166 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3167 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3168 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3169 | return "administratively prohibited"; |
| 3170 | case SSH2_OPEN_CONNECT_FAILED: |
| 3171 | return "connect failed"; |
| 3172 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3173 | return "unknown channel type"; |
| 3174 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3175 | return "resource shortage"; |
| 3176 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3177 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3180 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3181 | 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] | 3182 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3183 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3184 | u_int32_t reason; |
| 3185 | char *msg = NULL; |
| 3186 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3187 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3188 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3189 | return 0; |
| 3190 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3191 | packet_disconnect("Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3192 | "non-opening channel %d.", c->self); |
| 3193 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3194 | error("%s: reason: %s", __func__, ssh_err(r)); |
| 3195 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3196 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3197 | /* skip language */ |
| 3198 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
| 3199 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) { |
| 3200 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
| 3201 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3202 | } |
| 3203 | ssh_packet_check_eom(ssh); |
| 3204 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3205 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3206 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3207 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3208 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3209 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3210 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3211 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3212 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3213 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3214 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3215 | } |
| 3216 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3217 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3218 | 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] | 3219 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3220 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3221 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3222 | u_int32_t adjust; |
| 3223 | u_int new_rwin; |
| 3224 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3225 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3226 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3227 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3228 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3229 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3230 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3231 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3232 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3233 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) { |
| 3234 | error("%s: adjust: %s", __func__, ssh_err(r)); |
| 3235 | packet_disconnect("Invalid window adjust message"); |
| 3236 | } |
| 3237 | ssh_packet_check_eom(ssh); |
| 3238 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3239 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3240 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3241 | c->self, adjust, c->remote_window); |
| 3242 | } |
| 3243 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3244 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3245 | } |
| 3246 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3247 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3248 | 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] | 3249 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3250 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3251 | Channel *c; |
| 3252 | struct channel_confirm *cc; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3253 | |
| 3254 | /* Reset keepalive timeout */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 3255 | packet_set_alive_timeouts(0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3256 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3257 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3258 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3259 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3260 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3261 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3262 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3263 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3264 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3265 | ssh_packet_check_eom(ssh); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3266 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3267 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3268 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3269 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3270 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3271 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3272 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3273 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3274 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3275 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3276 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3277 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3278 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3279 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3280 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3283 | |
| 3284 | /* |
| 3285 | * Determine whether or not a port forward listens to loopback, the |
| 3286 | * specified address or wildcard. On the client, a specified bind |
| 3287 | * address will always override gateway_ports. On the server, a |
| 3288 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3289 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3290 | * will bind to whatever address the client asked for. |
| 3291 | * |
| 3292 | * Special-case listen_addrs are: |
| 3293 | * |
| 3294 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3295 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3296 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3297 | * "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] | 3298 | */ |
| 3299 | static const char * |
| 3300 | channel_fwd_bind_addr(const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3301 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3302 | { |
| 3303 | const char *addr = NULL; |
| 3304 | int wildcard = 0; |
| 3305 | |
| 3306 | if (listen_addr == NULL) { |
| 3307 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3308 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3309 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3310 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3311 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3312 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3313 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3314 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3315 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3316 | /* |
| 3317 | * Notify client if they requested a specific listen |
| 3318 | * address and it was overridden. |
| 3319 | */ |
| 3320 | if (*listen_addr != '\0' && |
| 3321 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3322 | strcmp(listen_addr, "*") != 0) { |
| 3323 | packet_send_debug("Forwarding listen address " |
| 3324 | "\"%s\" overridden by server " |
| 3325 | "GatewayPorts", listen_addr); |
| 3326 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3327 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3328 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3329 | strcmp(listen_addr, "::1") == 0) { |
| 3330 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3331 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3332 | } |
| 3333 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3334 | strcmp(listen_addr, "::1") == 0) { |
| 3335 | /* |
| 3336 | * If a specific IPv4/IPv6 localhost address has been |
| 3337 | * requested then accept it even if gateway_ports is in |
| 3338 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3339 | */ |
| 3340 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3341 | } |
| 3342 | if (wildcardp != NULL) |
| 3343 | *wildcardp = wildcard; |
| 3344 | return addr; |
| 3345 | } |
| 3346 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3347 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3348 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3349 | struct Forward *fwd, int *allocated_listen_port, |
| 3350 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3351 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3352 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3353 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3354 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3355 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3356 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3357 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3358 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3359 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3360 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3361 | if (is_client && fwd->connect_path != NULL) { |
| 3362 | host = fwd->connect_path; |
| 3363 | } else { |
| 3364 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3365 | fwd->listen_host : fwd->connect_host; |
| 3366 | if (host == NULL) { |
| 3367 | error("No forward host name."); |
| 3368 | return 0; |
| 3369 | } |
| 3370 | if (strlen(host) >= NI_MAXHOST) { |
| 3371 | error("Forward host name too long."); |
| 3372 | return 0; |
| 3373 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3374 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3375 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3376 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3377 | addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard, |
| 3378 | is_client, fwd_opts); |
| 3379 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3380 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3381 | |
| 3382 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3383 | * getaddrinfo returns a loopback address if the hostname is |
| 3384 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3385 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3386 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3387 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3388 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3389 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3390 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3391 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3392 | if (addr == NULL) { |
| 3393 | /* This really shouldn't happen */ |
| 3394 | packet_disconnect("getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3395 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3396 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3397 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3398 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3399 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3400 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3401 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3402 | if (allocated_listen_port != NULL) |
| 3403 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3404 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3405 | switch (ai->ai_family) { |
| 3406 | case AF_INET: |
| 3407 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3408 | sin_port; |
| 3409 | break; |
| 3410 | case AF_INET6: |
| 3411 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3412 | sin6_port; |
| 3413 | break; |
| 3414 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3415 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3416 | } |
| 3417 | /* |
| 3418 | * If allocating a port for -R forwards, then use the |
| 3419 | * same port for all address families. |
| 3420 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3421 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3422 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3423 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3424 | *lport_p = htons(*allocated_listen_port); |
| 3425 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3426 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3427 | strport, sizeof(strport), |
| 3428 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3429 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3430 | continue; |
| 3431 | } |
| 3432 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3433 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3434 | if (sock < 0) { |
| 3435 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3436 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3437 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3438 | continue; |
| 3439 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3440 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3441 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3442 | if (ai->ai_family == AF_INET6) |
| 3443 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3444 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3445 | debug("Local forwarding listening on %s port %s.", |
| 3446 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3447 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3448 | /* Bind the socket to the address. */ |
| 3449 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3450 | /* |
| 3451 | * address can be in if use ipv6 address is |
| 3452 | * already bound |
| 3453 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3454 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3455 | error("bind [%s]:%s: %.100s", |
| 3456 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3457 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3458 | verbose("bind [%s]:%s: %.100s", |
| 3459 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3460 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3461 | close(sock); |
| 3462 | continue; |
| 3463 | } |
| 3464 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3465 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3466 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3467 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3468 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3469 | close(sock); |
| 3470 | continue; |
| 3471 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3472 | |
| 3473 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3474 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3475 | * record what we got. |
| 3476 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3477 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3478 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3479 | allocated_listen_port != NULL && |
| 3480 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3481 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3482 | debug("Allocated listen port %d", |
| 3483 | *allocated_listen_port); |
| 3484 | } |
| 3485 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3486 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3487 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3488 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3489 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3490 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3491 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3492 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3493 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3494 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3495 | c->listening_port = *allocated_listen_port; |
| 3496 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3497 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3498 | success = 1; |
| 3499 | } |
| 3500 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3501 | error("%s: cannot listen to port: %d", __func__, |
| 3502 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3503 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3504 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3505 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3506 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3507 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3508 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3509 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3510 | { |
| 3511 | struct sockaddr_un sunaddr; |
| 3512 | const char *path; |
| 3513 | Channel *c; |
| 3514 | int port, sock; |
| 3515 | mode_t omask; |
| 3516 | |
| 3517 | switch (type) { |
| 3518 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3519 | if (fwd->connect_path != NULL) { |
| 3520 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3521 | error("Local connecting path too long: %s", |
| 3522 | fwd->connect_path); |
| 3523 | return 0; |
| 3524 | } |
| 3525 | path = fwd->connect_path; |
| 3526 | port = PORT_STREAMLOCAL; |
| 3527 | } else { |
| 3528 | if (fwd->connect_host == NULL) { |
| 3529 | error("No forward host name."); |
| 3530 | return 0; |
| 3531 | } |
| 3532 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3533 | error("Forward host name too long."); |
| 3534 | return 0; |
| 3535 | } |
| 3536 | path = fwd->connect_host; |
| 3537 | port = fwd->connect_port; |
| 3538 | } |
| 3539 | break; |
| 3540 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3541 | path = fwd->listen_path; |
| 3542 | port = PORT_STREAMLOCAL; |
| 3543 | break; |
| 3544 | default: |
| 3545 | error("%s: unexpected channel type %d", __func__, type); |
| 3546 | return 0; |
| 3547 | } |
| 3548 | |
| 3549 | if (fwd->listen_path == NULL) { |
| 3550 | error("No forward path name."); |
| 3551 | return 0; |
| 3552 | } |
| 3553 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3554 | error("Local listening path too long: %s", fwd->listen_path); |
| 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3559 | |
| 3560 | /* Start a Unix domain listener. */ |
| 3561 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3562 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3563 | fwd_opts->streamlocal_bind_unlink); |
| 3564 | umask(omask); |
| 3565 | if (sock < 0) |
| 3566 | return 0; |
| 3567 | |
| 3568 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3569 | |
| 3570 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3571 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3572 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3573 | 0, "unix listener", 1); |
| 3574 | c->path = xstrdup(path); |
| 3575 | c->host_port = port; |
| 3576 | c->listening_port = PORT_STREAMLOCAL; |
| 3577 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3578 | return 1; |
| 3579 | } |
| 3580 | |
| 3581 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3582 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3583 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3584 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3585 | u_int i; |
| 3586 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3587 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3588 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3589 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3590 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3591 | continue; |
| 3592 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3593 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3594 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3595 | found = 1; |
| 3596 | } |
| 3597 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3598 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3599 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3600 | } |
| 3601 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3602 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3603 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3604 | { |
| 3605 | u_int i; |
| 3606 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3607 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3608 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3609 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3610 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3611 | continue; |
| 3612 | if (c->path == NULL) |
| 3613 | continue; |
| 3614 | if (strcmp(c->path, path) == 0) { |
| 3615 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3616 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3617 | found = 1; |
| 3618 | } |
| 3619 | } |
| 3620 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3621 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3625 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3626 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3627 | if (fwd->listen_path != NULL) { |
| 3628 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3629 | fwd->listen_path); |
| 3630 | } else { |
| 3631 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3632 | fwd->listen_host, fwd->listen_port); |
| 3633 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3637 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3638 | const char *lhost, u_short lport, int cport, |
| 3639 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3640 | { |
| 3641 | u_int i; |
| 3642 | int found = 0; |
| 3643 | const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3644 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3645 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3646 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3647 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3648 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3649 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3650 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3651 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3652 | /* skip dynamic forwardings */ |
| 3653 | if (c->host_port == 0) |
| 3654 | continue; |
| 3655 | } else { |
| 3656 | if (c->host_port != cport) |
| 3657 | continue; |
| 3658 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3659 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3660 | (c->listening_addr != NULL && addr == NULL)) |
| 3661 | continue; |
| 3662 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3663 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3664 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3665 | found = 1; |
| 3666 | } |
| 3667 | } |
| 3668 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3669 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3670 | } |
| 3671 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3672 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3673 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3674 | { |
| 3675 | u_int i; |
| 3676 | int found = 0; |
| 3677 | |
| 3678 | if (path == NULL) { |
| 3679 | error("%s: no path specified.", __func__); |
| 3680 | return 0; |
| 3681 | } |
| 3682 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3683 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3684 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3685 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3686 | continue; |
| 3687 | if (c->listening_addr == NULL) |
| 3688 | continue; |
| 3689 | if (strcmp(c->listening_addr, path) == 0) { |
| 3690 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3691 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3692 | found = 1; |
| 3693 | } |
| 3694 | } |
| 3695 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3696 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3700 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3701 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3702 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3703 | if (fwd->listen_path != NULL) { |
| 3704 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3705 | fwd->listen_path); |
| 3706 | } else { |
| 3707 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3708 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3709 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3710 | } |
| 3711 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3712 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3713 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3714 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3715 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3716 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3717 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3718 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3719 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3720 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3721 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3722 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3723 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3724 | } |
| 3725 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3726 | /* Matches a remote forwarding permission against a requested forwarding */ |
| 3727 | static int |
| 3728 | remote_open_match(struct permission *allowed_open, struct Forward *fwd) |
| 3729 | { |
| 3730 | int ret; |
| 3731 | char *lhost; |
| 3732 | |
| 3733 | /* XXX add ACLs for streamlocal */ |
| 3734 | if (fwd->listen_path != NULL) |
| 3735 | return 1; |
| 3736 | |
| 3737 | if (fwd->listen_host == NULL || allowed_open->listen_host == NULL) |
| 3738 | return 0; |
| 3739 | |
| 3740 | if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT && |
| 3741 | allowed_open->listen_port != fwd->listen_port) |
| 3742 | return 0; |
| 3743 | |
| 3744 | /* Match hostnames case-insensitively */ |
| 3745 | lhost = xstrdup(fwd->listen_host); |
| 3746 | lowercase(lhost); |
| 3747 | ret = match_pattern(lhost, allowed_open->listen_host); |
| 3748 | free(lhost); |
| 3749 | |
| 3750 | return ret; |
| 3751 | } |
| 3752 | |
| 3753 | /* Checks whether a requested remote forwarding is permitted */ |
| 3754 | static int |
| 3755 | check_rfwd_permission(struct ssh *ssh, struct Forward *fwd) |
| 3756 | { |
| 3757 | struct ssh_channels *sc = ssh->chanctxt; |
| 3758 | struct permission_set *pset = &sc->remote_perms; |
| 3759 | u_int i, permit, permit_adm = 1; |
| 3760 | struct permission *perm; |
| 3761 | |
| 3762 | /* XXX apply GatewayPorts override before checking? */ |
| 3763 | |
| 3764 | permit = pset->all_permitted; |
| 3765 | if (!permit) { |
| 3766 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3767 | perm = &pset->permitted_user[i]; |
| 3768 | if (remote_open_match(perm, fwd)) { |
| 3769 | permit = 1; |
| 3770 | break; |
| 3771 | } |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | if (pset->num_permitted_admin > 0) { |
| 3776 | permit_adm = 0; |
| 3777 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 3778 | perm = &pset->permitted_admin[i]; |
| 3779 | if (remote_open_match(perm, fwd)) { |
| 3780 | permit_adm = 1; |
| 3781 | break; |
| 3782 | } |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | return permit && permit_adm; |
| 3787 | } |
| 3788 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3789 | /* protocol v2 remote port fwd, used by sshd */ |
| 3790 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3791 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3792 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3793 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3794 | if (!check_rfwd_permission(ssh, fwd)) { |
| 3795 | packet_send_debug("port forwarding refused"); |
| 3796 | return 0; |
| 3797 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3798 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3799 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3800 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3801 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3802 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3803 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3804 | fwd_opts); |
| 3805 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3806 | } |
| 3807 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3808 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3809 | * Translate the requested rfwd listen host to something usable for |
| 3810 | * this server. |
| 3811 | */ |
| 3812 | static const char * |
| 3813 | channel_rfwd_bind_host(const char *listen_host) |
| 3814 | { |
| 3815 | if (listen_host == NULL) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3816 | return "localhost"; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3817 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3818 | return ""; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3819 | } else |
| 3820 | return listen_host; |
| 3821 | } |
| 3822 | |
| 3823 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3824 | * Initiate forwarding of connections to port "port" on remote host through |
| 3825 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3826 | * Returns handle (index) for updating the dynamic listen port with |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3827 | * channel_update_permission(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3828 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3829 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3830 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3831 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3832 | int r, success = 0, idx = -1; |
| 3833 | char *host_to_connect, *listen_host, *listen_path; |
| 3834 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3835 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3836 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3837 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3838 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3839 | (r = sshpkt_put_cstring(ssh, |
| 3840 | "streamlocal-forward@openssh.com")) != 0 || |
| 3841 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3842 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3843 | (r = sshpkt_send(ssh)) != 0 || |
| 3844 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3845 | fatal("%s: request streamlocal: %s", |
| 3846 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3847 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3848 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3849 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3850 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3851 | (r = sshpkt_put_cstring(ssh, |
| 3852 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3853 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3854 | (r = sshpkt_send(ssh)) != 0 || |
| 3855 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3856 | fatal("%s: request tcpip-forward: %s", |
| 3857 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3858 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3859 | /* Assume that server accepts the request */ |
| 3860 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3861 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3862 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3863 | host_to_connect = listen_host = listen_path = NULL; |
| 3864 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3865 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3866 | host_to_connect = xstrdup(fwd->connect_path); |
| 3867 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3868 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3869 | host_to_connect = xstrdup(fwd->connect_host); |
| 3870 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3871 | } |
| 3872 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3873 | listen_path = xstrdup(fwd->listen_path); |
| 3874 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3875 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3876 | if (fwd->listen_host != NULL) |
| 3877 | listen_host = xstrdup(fwd->listen_host); |
| 3878 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3879 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3880 | idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3881 | host_to_connect, port_to_connect, |
| 3882 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3883 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3884 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3885 | } |
| 3886 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3887 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3888 | open_match(struct permission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3889 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3890 | { |
| 3891 | if (allowed_open->host_to_connect == NULL) |
| 3892 | return 0; |
| 3893 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3894 | allowed_open->port_to_connect != requestedport) |
| 3895 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3896 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3897 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3898 | return 0; |
| 3899 | return 1; |
| 3900 | } |
| 3901 | |
| 3902 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3903 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3904 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3905 | * need to translate between the configured-host (listen_host) |
| 3906 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3907 | */ |
| 3908 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3909 | open_listen_match_tcpip(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3910 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3911 | { |
| 3912 | const char *allowed_host; |
| 3913 | |
| 3914 | if (allowed_open->host_to_connect == NULL) |
| 3915 | return 0; |
| 3916 | if (allowed_open->listen_port != requestedport) |
| 3917 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3918 | if (!translate && allowed_open->listen_host == NULL && |
| 3919 | requestedhost == NULL) |
| 3920 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3921 | allowed_host = translate ? |
| 3922 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3923 | allowed_open->listen_host; |
djm@openbsd.org | 90e51d6 | 2018-06-25 22:28:33 +0000 | [diff] [blame] | 3924 | if (allowed_host == NULL || requestedhost == NULL || |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3925 | strcmp(allowed_host, requestedhost) != 0) |
| 3926 | return 0; |
| 3927 | return 1; |
| 3928 | } |
| 3929 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3930 | static int |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3931 | open_listen_match_streamlocal(struct permission *allowed_open, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3932 | const char *requestedpath) |
| 3933 | { |
| 3934 | if (allowed_open->host_to_connect == NULL) |
| 3935 | return 0; |
| 3936 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3937 | return 0; |
| 3938 | if (allowed_open->listen_path == NULL || |
| 3939 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3940 | return 0; |
| 3941 | return 1; |
| 3942 | } |
| 3943 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3944 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3945 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3946 | * local side. |
| 3947 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3948 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3949 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3950 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3951 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3952 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3953 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3954 | int r; |
| 3955 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3956 | struct permission *perm; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3957 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3958 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3959 | perm = &pset->permitted_user[i]; |
| 3960 | if (open_listen_match_tcpip(perm, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3961 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3962 | perm = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3963 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3964 | if (perm == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3965 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3966 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3967 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3968 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3969 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 3970 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3971 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 3972 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 3973 | (r = sshpkt_send(ssh)) != 0) |
| 3974 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3975 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3976 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3977 | |
| 3978 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3982 | * Request cancellation of remote forwarding of Unix domain socket |
| 3983 | * path from local side. |
| 3984 | */ |
| 3985 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3986 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3987 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3988 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3989 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3990 | int r; |
| 3991 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3992 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3993 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3994 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 3995 | perm = &pset->permitted_user[i]; |
| 3996 | if (open_listen_match_streamlocal(perm, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3997 | break; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 3998 | perm = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3999 | } |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4000 | if (perm == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4001 | debug("%s: requested forward not found", __func__); |
| 4002 | return -1; |
| 4003 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4004 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 4005 | (r = sshpkt_put_cstring(ssh, |
| 4006 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 4007 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 4008 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 4009 | (r = sshpkt_send(ssh)) != 0) |
| 4010 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4011 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4012 | fwd_perm_clear(perm); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4013 | |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | /* |
| 4018 | * Request cancellation of remote forwarding of a connection from local side. |
| 4019 | */ |
| 4020 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4021 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4022 | { |
| 4023 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4024 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 4025 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4026 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4027 | return channel_request_rforward_cancel_tcpip(ssh, |
| 4028 | fwd->listen_host, |
| 4029 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /* |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4034 | * 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] | 4035 | * usually called by the server, because the user could connect to any port |
| 4036 | * anyway, and the server has no way to know but to trust the client anyway. |
| 4037 | */ |
| 4038 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4039 | channel_permit_all(struct ssh *ssh, int where) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4040 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4041 | struct permission_set *pset = permission_set_get(ssh, where); |
| 4042 | |
| 4043 | if (pset->num_permitted_user == 0) |
| 4044 | pset->all_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4047 | /* |
| 4048 | * Permit the specified host/port for forwarding. |
| 4049 | */ |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 4050 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4051 | channel_add_permission(struct ssh *ssh, int who, int where, |
| 4052 | char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4053 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4054 | int local = where == FORWARD_LOCAL; |
| 4055 | struct permission_set *pset = permission_set_get(ssh, where); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4056 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4057 | debug("allow %s forwarding to host %s port %d", |
| 4058 | fwd_ident(who, where), host, port); |
| 4059 | /* |
| 4060 | * Remote forwards set listen_host/port, local forwards set |
| 4061 | * host/port_to_connect. |
| 4062 | */ |
| 4063 | permission_set_add(ssh, who, where, |
| 4064 | local ? host : 0, local ? port : 0, |
| 4065 | local ? NULL : host, NULL, local ? 0 : port, NULL); |
| 4066 | pset->all_permitted = 0; |
| 4067 | } |
| 4068 | |
| 4069 | /* |
| 4070 | * Administratively disable forwarding. |
| 4071 | */ |
| 4072 | void |
| 4073 | channel_disable_admin(struct ssh *ssh, int where) |
| 4074 | { |
| 4075 | channel_clear_permission(ssh, FORWARD_ADM, where); |
| 4076 | permission_set_add(ssh, FORWARD_ADM, where, |
| 4077 | NULL, 0, NULL, NULL, 0, NULL); |
| 4078 | } |
| 4079 | |
| 4080 | /* |
| 4081 | * Clear a list of permitted opens. |
| 4082 | */ |
| 4083 | void |
| 4084 | channel_clear_permission(struct ssh *ssh, int who, int where) |
| 4085 | { |
| 4086 | struct permission **permp; |
| 4087 | u_int *npermp; |
| 4088 | |
| 4089 | permission_set_get_array(ssh, who, where, &permp, &npermp); |
| 4090 | *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp)); |
| 4091 | *npermp = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4094 | /* |
| 4095 | * Update the listen port for a dynamic remote forward, after |
| 4096 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 4097 | * passed then they entry will be invalidated. |
| 4098 | */ |
| 4099 | void |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4100 | channel_update_permission(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4101 | { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4102 | struct permission_set *pset = &ssh->chanctxt->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4103 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4104 | if (idx < 0 || (u_int)idx >= pset->num_permitted_user) { |
| 4105 | debug("%s: index out of range: %d num_permitted_user %d", |
| 4106 | __func__, idx, pset->num_permitted_user); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4107 | return; |
| 4108 | } |
| 4109 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 4110 | newport > 0 ? "Updating" : "Removing", |
| 4111 | newport, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4112 | pset->permitted_user[idx].host_to_connect, |
| 4113 | pset->permitted_user[idx].port_to_connect); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4114 | if (newport <= 0) |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4115 | fwd_perm_clear(&pset->permitted_user[idx]); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4116 | else { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4117 | pset->permitted_user[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4118 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 4122 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 4123 | int |
| 4124 | permitopen_port(const char *p) |
| 4125 | { |
| 4126 | int port; |
| 4127 | |
| 4128 | if (strcmp(p, "*") == 0) |
| 4129 | return FWD_PERMIT_ANY_PORT; |
| 4130 | if ((port = a2port(p)) > 0) |
| 4131 | return port; |
| 4132 | return -1; |
| 4133 | } |
| 4134 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4135 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4136 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4137 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4138 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4139 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4140 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4141 | char ntop[NI_MAXHOST]; |
| 4142 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4143 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4144 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4145 | switch (cctx->ai->ai_family) { |
| 4146 | case AF_UNIX: |
| 4147 | /* unix:pathname instead of host:port */ |
| 4148 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 4149 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 4150 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 4151 | break; |
| 4152 | case AF_INET: |
| 4153 | case AF_INET6: |
| 4154 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 4155 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 4156 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 4157 | error("connect_next: getnameinfo failed"); |
| 4158 | continue; |
| 4159 | } |
| 4160 | break; |
| 4161 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4162 | continue; |
| 4163 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4164 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4165 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4166 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4167 | error("socket: %.100s", strerror(errno)); |
| 4168 | else |
| 4169 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4170 | continue; |
| 4171 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4172 | if (set_nonblock(sock) == -1) |
| 4173 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4174 | if (connect(sock, cctx->ai->ai_addr, |
| 4175 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4176 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4177 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4178 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4179 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4180 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4181 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4182 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4183 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4184 | if (cctx->ai->ai_family != AF_UNIX) |
| 4185 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4186 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4187 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4188 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4189 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4190 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4191 | return -1; |
| 4192 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4193 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4194 | static void |
| 4195 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4196 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4197 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4198 | if (cctx->aitop) { |
| 4199 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4200 | free(cctx->aitop); |
| 4201 | else |
| 4202 | freeaddrinfo(cctx->aitop); |
| 4203 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4204 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4205 | } |
| 4206 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4207 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4208 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4209 | * passing back the failure reason if appropriate. |
| 4210 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4211 | static int |
| 4212 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4213 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4214 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4215 | { |
| 4216 | struct addrinfo hints; |
| 4217 | int gaierr; |
| 4218 | int sock = -1; |
| 4219 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4220 | |
| 4221 | if (port == PORT_STREAMLOCAL) { |
| 4222 | struct sockaddr_un *sunaddr; |
| 4223 | struct addrinfo *ai; |
| 4224 | |
| 4225 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4226 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4227 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /* |
| 4231 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4232 | * channel_connect_ctx_free() must check ai_family |
| 4233 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4234 | */ |
| 4235 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4236 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4237 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4238 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4239 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4240 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4241 | ai->ai_protocol = PF_UNSPEC; |
| 4242 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4243 | sunaddr->sun_family = AF_UNIX; |
| 4244 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4245 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4246 | } else { |
| 4247 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4248 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4249 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4250 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4251 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4252 | != 0) { |
| 4253 | if (errmsg != NULL) |
| 4254 | *errmsg = ssh_gai_strerror(gaierr); |
| 4255 | if (reason != NULL) |
| 4256 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4257 | error("connect_to %.100s: unknown host (%s)", name, |
| 4258 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4259 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4260 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4261 | } |
| 4262 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4263 | cctx->host = xstrdup(name); |
| 4264 | cctx->port = port; |
| 4265 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4266 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4267 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4268 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4269 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4270 | return -1; |
| 4271 | } |
| 4272 | |
| 4273 | return sock; |
| 4274 | } |
| 4275 | |
| 4276 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4277 | static Channel * |
| 4278 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4279 | char *ctype, char *rname) |
| 4280 | { |
| 4281 | struct channel_connect cctx; |
| 4282 | Channel *c; |
| 4283 | int sock; |
| 4284 | |
| 4285 | memset(&cctx, 0, sizeof(cctx)); |
| 4286 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4287 | &cctx, NULL, NULL); |
| 4288 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4289 | channel_connect_ctx_free(&cctx); |
| 4290 | return NULL; |
| 4291 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4292 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4293 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4294 | c->host_port = port; |
| 4295 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4296 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4297 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4298 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4301 | /* |
| 4302 | * returns either the newly connected channel or the downstream channel |
| 4303 | * that needs to deal with this connection. |
| 4304 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4305 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4306 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4307 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4308 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4309 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4310 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4311 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4312 | struct permission *perm; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4313 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4314 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4315 | perm = &pset->permitted_user[i]; |
| 4316 | if (open_listen_match_tcpip(perm, |
| 4317 | listen_host, listen_port, 1)) { |
| 4318 | if (perm->downstream) |
| 4319 | return perm->downstream; |
| 4320 | if (perm->port_to_connect == 0) |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4321 | return rdynamic_connect_prepare(ssh, |
| 4322 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4323 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4324 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4325 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4326 | } |
| 4327 | } |
| 4328 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4329 | listen_port); |
| 4330 | return NULL; |
| 4331 | } |
| 4332 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4333 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4334 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4335 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4336 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4337 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4338 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4339 | u_int i; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4340 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4341 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4342 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4343 | perm = &pset->permitted_user[i]; |
| 4344 | if (open_listen_match_streamlocal(perm, path)) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4345 | return connect_to(ssh, |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4346 | perm->host_to_connect, perm->port_to_connect, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4347 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4351 | path); |
| 4352 | return NULL; |
| 4353 | } |
| 4354 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4355 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4356 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4357 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4358 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4359 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4360 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4361 | struct permission_set *pset = &sc->local_perms; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4362 | struct channel_connect cctx; |
| 4363 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4364 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4365 | int sock; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4366 | struct permission *perm; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4367 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4368 | permit = pset->all_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4369 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4370 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4371 | perm = &pset->permitted_user[i]; |
| 4372 | if (open_match(perm, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4373 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4374 | break; |
| 4375 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4376 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4377 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4378 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4379 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4380 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4381 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4382 | perm = &pset->permitted_admin[i]; |
| 4383 | if (open_match(perm, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4384 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4385 | break; |
| 4386 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4387 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 4391 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4392 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4393 | if (reason != NULL) |
| 4394 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4395 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4396 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4397 | |
| 4398 | memset(&cctx, 0, sizeof(cctx)); |
| 4399 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4400 | &cctx, reason, errmsg); |
| 4401 | if (sock == -1) { |
| 4402 | channel_connect_ctx_free(&cctx); |
| 4403 | return NULL; |
| 4404 | } |
| 4405 | |
| 4406 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4407 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4408 | c->host_port = port; |
| 4409 | c->path = xstrdup(host); |
| 4410 | c->connect_ctx = cctx; |
| 4411 | |
| 4412 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4415 | /* Check if connecting to that path is permitted and connect. */ |
| 4416 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4417 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4418 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4419 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4420 | struct ssh_channels *sc = ssh->chanctxt; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4421 | struct permission_set *pset = &sc->local_perms; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4422 | u_int i, permit, permit_adm = 1; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4423 | struct permission *perm; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4424 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4425 | permit = pset->all_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4426 | if (!permit) { |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4427 | for (i = 0; i < pset->num_permitted_user; i++) { |
| 4428 | perm = &pset->permitted_user[i]; |
| 4429 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4430 | permit = 1; |
| 4431 | break; |
| 4432 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4433 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4434 | } |
| 4435 | |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4436 | if (pset->num_permitted_admin > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4437 | permit_adm = 0; |
djm@openbsd.org | 115063a | 2018-06-06 18:22:41 +0000 | [diff] [blame] | 4438 | for (i = 0; i < pset->num_permitted_admin; i++) { |
| 4439 | perm = &pset->permitted_admin[i]; |
| 4440 | if (open_match(perm, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4441 | permit_adm = 1; |
| 4442 | break; |
| 4443 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4444 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | if (!permit || !permit_adm) { |
| 4448 | logit("Received request to connect to path %.100s, " |
| 4449 | "but the request was denied.", path); |
| 4450 | return NULL; |
| 4451 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4452 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4453 | } |
| 4454 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4455 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4456 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4457 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4458 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4459 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4460 | int r; |
| 4461 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4462 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4463 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4464 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4465 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4466 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4467 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4468 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4469 | channel_request_start(ssh, i, "window-change", 0); |
| 4470 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4471 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4472 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4473 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4474 | (r = sshpkt_send(ssh)) != 0) |
| 4475 | fatal("%s: channel %u: send window-change: %s", |
| 4476 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4477 | } |
| 4478 | } |
| 4479 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4480 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4481 | static Channel * |
| 4482 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4483 | { |
| 4484 | Channel *c; |
| 4485 | int r; |
| 4486 | |
| 4487 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4488 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4489 | c->host_port = 0; |
| 4490 | c->path = NULL; |
| 4491 | |
| 4492 | /* |
| 4493 | * We need to open the channel before we have a FD, |
| 4494 | * so that we can get SOCKS header from peer. |
| 4495 | */ |
| 4496 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4497 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4498 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4499 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4500 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4501 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4502 | c->self, ssh_err(r)); |
| 4503 | } |
| 4504 | return c; |
| 4505 | } |
| 4506 | |
| 4507 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4508 | static int |
| 4509 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4510 | { |
| 4511 | struct channel_connect cctx; |
| 4512 | int sock; |
| 4513 | |
| 4514 | memset(&cctx, 0, sizeof(cctx)); |
| 4515 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4516 | NULL, &cctx, NULL, NULL); |
| 4517 | if (sock == -1) |
| 4518 | channel_connect_ctx_free(&cctx); |
| 4519 | else { |
| 4520 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4521 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4522 | c->connect_ctx = cctx; |
| 4523 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4524 | } |
| 4525 | return sock; |
| 4526 | } |
| 4527 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4528 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4529 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4530 | /* |
| 4531 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4532 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4533 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4534 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4535 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4536 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4537 | int x11_use_localhost, int single_connection, |
| 4538 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4539 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4540 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4541 | int display_number, sock; |
| 4542 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4543 | struct addrinfo hints, *ai, *aitop; |
| 4544 | char strport[NI_MAXSERV]; |
| 4545 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4546 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4547 | if (chanids == NULL) |
| 4548 | return -1; |
| 4549 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4550 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4551 | display_number < MAX_DISPLAYS; |
| 4552 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4553 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4554 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4555 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4556 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4557 | hints.ai_socktype = SOCK_STREAM; |
| 4558 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4559 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4560 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4561 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4562 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4563 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4564 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4565 | if (ai->ai_family != AF_INET && |
| 4566 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4567 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4568 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4569 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4570 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4571 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4572 | #ifdef EPFNOSUPPORT |
| 4573 | && (errno != EPFNOSUPPORT) |
| 4574 | #endif |
| 4575 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4576 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4577 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4578 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4579 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4580 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4581 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4582 | continue; |
| 4583 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4584 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4585 | if (ai->ai_family == AF_INET6) |
| 4586 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4587 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4588 | set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4589 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4590 | debug2("%s: bind port %d: %.100s", __func__, |
| 4591 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4592 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4593 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4594 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4595 | num_socks = 0; |
| 4596 | break; |
| 4597 | } |
| 4598 | socks[num_socks++] = sock; |
| 4599 | if (num_socks == NUM_SOCKS) |
| 4600 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4601 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4602 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4603 | if (num_socks > 0) |
| 4604 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4605 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4606 | if (display_number >= MAX_DISPLAYS) { |
| 4607 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4608 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4609 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4610 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4611 | for (n = 0; n < num_socks; n++) { |
| 4612 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 4613 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4614 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4615 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4616 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4617 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4618 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4619 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4620 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4621 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4622 | for (n = 0; n < num_socks; n++) { |
| 4623 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4624 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4625 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4626 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4627 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4628 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4629 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4630 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4631 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4632 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4633 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4634 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4635 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4636 | } |
| 4637 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4638 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4639 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4640 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4641 | int sock; |
| 4642 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4643 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4644 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4645 | if (sock < 0) |
| 4646 | error("socket: %.100s", strerror(errno)); |
| 4647 | memset(&addr, 0, sizeof(addr)); |
| 4648 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4649 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4650 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4651 | return sock; |
| 4652 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4653 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4654 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4655 | } |
| 4656 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4657 | static int |
| 4658 | connect_local_xsocket(u_int dnr) |
| 4659 | { |
| 4660 | char buf[1024]; |
| 4661 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4662 | return connect_local_xsocket_path(buf); |
| 4663 | } |
| 4664 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4665 | #ifdef __APPLE__ |
| 4666 | static int |
| 4667 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4668 | { |
| 4669 | struct stat sbuf; |
| 4670 | |
| 4671 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4672 | error("%s: display path too long", __func__); |
| 4673 | return 0; |
| 4674 | } |
| 4675 | if (display[0] != '/') |
| 4676 | return 0; |
| 4677 | if (stat(path, &sbuf) == 0) { |
| 4678 | return 1; |
| 4679 | } else { |
| 4680 | char *dot = strrchr(path, '.'); |
| 4681 | if (dot != NULL) { |
| 4682 | *dot = '\0'; |
| 4683 | if (stat(path, &sbuf) == 0) { |
| 4684 | return 1; |
| 4685 | } |
| 4686 | } |
| 4687 | } |
| 4688 | return 0; |
| 4689 | } |
| 4690 | #endif |
| 4691 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4692 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4693 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4694 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4695 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4696 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4697 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4698 | struct addrinfo hints, *ai, *aitop; |
| 4699 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4700 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4701 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4702 | /* Try to open a socket for the local X server. */ |
| 4703 | display = getenv("DISPLAY"); |
| 4704 | if (!display) { |
| 4705 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4706 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4707 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4708 | /* |
| 4709 | * Now we decode the value of the DISPLAY variable and make a |
| 4710 | * connection to the real X server. |
| 4711 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4712 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4713 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4714 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4715 | { |
| 4716 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4717 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4718 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4719 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4720 | |
| 4721 | /* Create a socket. */ |
| 4722 | sock = connect_local_xsocket_path(path); |
| 4723 | if (sock < 0) |
| 4724 | return -1; |
| 4725 | |
| 4726 | /* OK, we now have a connection to the display. */ |
| 4727 | return sock; |
| 4728 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4729 | } |
| 4730 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4731 | /* |
| 4732 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4733 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4734 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4735 | if (strncmp(display, "unix:", 5) == 0 || |
| 4736 | display[0] == ':') { |
| 4737 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4738 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4739 | &display_number) != 1) { |
| 4740 | error("Could not parse display number from DISPLAY: " |
| 4741 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4742 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4743 | } |
| 4744 | /* Create a socket. */ |
| 4745 | sock = connect_local_xsocket(display_number); |
| 4746 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4747 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4748 | |
| 4749 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4750 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4751 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4752 | /* |
| 4753 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4754 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4755 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4756 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4757 | cp = strchr(buf, ':'); |
| 4758 | if (!cp) { |
| 4759 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4760 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4761 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4762 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4763 | /* |
| 4764 | * buf now contains the host name. But first we parse the |
| 4765 | * display number. |
| 4766 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4767 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4768 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4769 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4770 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4771 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4772 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4773 | /* Look up the host address */ |
| 4774 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4775 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4776 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4777 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4778 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4779 | error("%.100s: unknown host. (%s)", buf, |
| 4780 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4781 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4782 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4783 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4784 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4785 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4786 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4787 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4788 | continue; |
| 4789 | } |
| 4790 | /* Connect it to the display. */ |
| 4791 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4792 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4793 | 6000 + display_number, strerror(errno)); |
| 4794 | close(sock); |
| 4795 | continue; |
| 4796 | } |
| 4797 | /* Success */ |
| 4798 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4799 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4800 | freeaddrinfo(aitop); |
| 4801 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4802 | error("connect %.100s port %u: %.100s", buf, |
| 4803 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4804 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4805 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4806 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4807 | return sock; |
| 4808 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4809 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4810 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4811 | * Requests forwarding of X11 connections, generates fake authentication |
| 4812 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4813 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4814 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4815 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4816 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4817 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4818 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4819 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4820 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4821 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4822 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4823 | char *new_data; |
| 4824 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4825 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4826 | if (sc->x11_saved_display == NULL) |
| 4827 | sc->x11_saved_display = xstrdup(disp); |
| 4828 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4829 | error("x11_request_forwarding_with_spoofing: different " |
| 4830 | "$DISPLAY already forwarded"); |
| 4831 | return; |
| 4832 | } |
| 4833 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4834 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4835 | if (cp) |
| 4836 | cp = strchr(cp, '.'); |
| 4837 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4838 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4839 | else |
| 4840 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4841 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4842 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4843 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4844 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4847 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4848 | for (i = 0; i < data_len; i++) { |
| 4849 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4850 | fatal("x11_request_forwarding: bad " |
| 4851 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4852 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4853 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4854 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4855 | |
| 4856 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4857 | sc->x11_fake_data = xmalloc(data_len); |
| 4858 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4859 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4860 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4861 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4862 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4863 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4864 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4865 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4866 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4867 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4868 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4869 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4870 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4871 | (r = sshpkt_send(ssh)) != 0 || |
| 4872 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4873 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4874 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4875 | } |