tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.379 2018/02/05 05:36:49 tb Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains functions for generic socket connection forwarding. |
| 7 | * There is also code for initiating connection forwarding for X11 connections, |
| 8 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * Copyright (c) 1999 Dug Song. All rights reserved. |
| 19 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 43 | |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 44 | #include <sys/types.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 45 | #include <sys/stat.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include <sys/ioctl.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 47 | #include <sys/un.h> |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 48 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 49 | #ifdef HAVE_SYS_TIME_H |
| 50 | # include <sys/time.h> |
| 51 | #endif |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 52 | |
| 53 | #include <netinet/in.h> |
| 54 | #include <arpa/inet.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 55 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 56 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 57 | #include <fcntl.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 58 | #include <limits.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 59 | #include <netdb.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 60 | #include <stdarg.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 61 | #ifdef HAVE_STDINT_H |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 62 | #include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 66 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 67 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 70 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh2.h" |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 74 | #include "ssherr.h" |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 75 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "canohost.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 82 | #include "key.h" |
| 83 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 84 | #include "pathnames.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 86 | /* -- agent forwarding */ |
| 87 | #define NUM_SOCKS 10 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 89 | /* -- tcp forwarding */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 90 | /* special-case port number meaning allow any port */ |
| 91 | #define FWD_PERMIT_ANY_PORT 0 |
| 92 | |
| 93 | /* special-case wildcard meaning allow any host */ |
| 94 | #define FWD_PERMIT_ANY_HOST "*" |
| 95 | |
| 96 | /* -- X11 forwarding */ |
| 97 | /* Maximum number of fake X11 displays to try. */ |
| 98 | #define MAX_DISPLAYS 1000 |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 100 | /* |
| 101 | * Data structure for storing which hosts are permitted for forward requests. |
| 102 | * The local sides of any remote forwards are stored in this array to prevent |
| 103 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 104 | * network (which might be behind a firewall). |
| 105 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 106 | /* XXX: streamlocal wants a path instead of host:port */ |
| 107 | /* Overload host_to_connect; we could just make this match Forward */ |
| 108 | /* XXX - can we use listen_host instead of listen_path? */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | typedef struct { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 110 | char *host_to_connect; /* Connect to 'host'. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 111 | int port_to_connect; /* Connect to 'port'. */ |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 112 | char *listen_host; /* Remote side should listen address. */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 113 | char *listen_path; /* Remote side should listen path. */ |
| 114 | int listen_port; /* Remote side should listen port. */ |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 115 | Channel *downstream; /* Downstream mux*/ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | } ForwardPermission; |
| 117 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 118 | typedef void chan_fn(struct ssh *, Channel *c, |
| 119 | fd_set *readset, fd_set *writeset); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 120 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 121 | /* Master structure for channels state */ |
| 122 | struct ssh_channels { |
| 123 | /* |
| 124 | * Pointer to an array containing all allocated channels. The array |
| 125 | * is dynamically extended as needed. |
| 126 | */ |
| 127 | Channel **channels; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 128 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Size of the channel array. All slots of the array must always be |
| 131 | * initialized (at least the type field); unused slots set to NULL |
| 132 | */ |
| 133 | u_int channels_alloc; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 134 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Maximum file descriptor value used in any of the channels. This is |
| 137 | * updated in channel_new. |
| 138 | */ |
| 139 | int channel_max_fd; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 140 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 141 | /* |
| 142 | * 'channel_pre*' are called just before select() to add any bits |
| 143 | * relevant to channels in the select bitmasks. |
| 144 | * |
| 145 | * 'channel_post*': perform any appropriate operations for |
| 146 | * channels which have events pending. |
| 147 | */ |
| 148 | chan_fn **channel_pre; |
| 149 | chan_fn **channel_post; |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 150 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 151 | /* -- tcp forwarding */ |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 152 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 153 | /* List of all permitted host/port pairs to connect by the user. */ |
| 154 | ForwardPermission *permitted_opens; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 156 | /* List of all permitted host/port pairs to connect by the admin. */ |
| 157 | ForwardPermission *permitted_adm_opens; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 158 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Number of permitted host/port pairs in the array permitted by |
| 161 | * the user. |
| 162 | */ |
| 163 | u_int num_permitted_opens; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 164 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 165 | /* |
| 166 | * Number of permitted host/port pair in the array permitted by |
| 167 | * the admin. |
| 168 | */ |
| 169 | u_int num_adm_permitted_opens; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 170 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 171 | /* |
| 172 | * If this is true, all opens are permitted. This is the case on |
| 173 | * the server on which we have to trust the client anyway, and the |
| 174 | * user could do anything after logging in anyway. |
| 175 | */ |
| 176 | int all_opens_permitted; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 177 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 178 | /* -- X11 forwarding */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 179 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 180 | /* Saved X11 local (client) display. */ |
| 181 | char *x11_saved_display; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 182 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 183 | /* Saved X11 authentication protocol name. */ |
| 184 | char *x11_saved_proto; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 185 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 186 | /* Saved X11 authentication data. This is the real data. */ |
| 187 | char *x11_saved_data; |
| 188 | u_int x11_saved_data_len; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 189 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 190 | /* Deadline after which all X11 connections are refused */ |
| 191 | u_int x11_refuse_time; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 192 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Fake X11 authentication data. This is what the server will be |
| 195 | * sending us; we should replace any occurrences of this by the |
| 196 | * real data. |
| 197 | */ |
| 198 | u_char *x11_fake_data; |
| 199 | u_int x11_fake_data_len; |
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 | /* AF_UNSPEC or AF_INET or AF_INET6 */ |
| 202 | int IPv4or6; |
| 203 | }; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 204 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 205 | /* helper */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 206 | 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] | 207 | static const char *channel_rfwd_bind_host(const char *listen_host); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 208 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 209 | /* non-blocking connect helpers */ |
| 210 | static int connect_next(struct channel_connect *); |
| 211 | static void channel_connect_ctx_free(struct channel_connect *); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 212 | static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *); |
| 213 | static int rdynamic_connect_finish(struct ssh *, Channel *); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 214 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 215 | /* Setup helper */ |
| 216 | static void channel_handler_init(struct ssh_channels *sc); |
| 217 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 218 | /* -- channel core */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 219 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 220 | void |
| 221 | channel_init_channels(struct ssh *ssh) |
| 222 | { |
| 223 | struct ssh_channels *sc; |
| 224 | |
| 225 | if ((sc = calloc(1, sizeof(*sc))) == NULL || |
| 226 | (sc->channel_pre = calloc(SSH_CHANNEL_MAX_TYPE, |
| 227 | sizeof(*sc->channel_pre))) == NULL || |
| 228 | (sc->channel_post = calloc(SSH_CHANNEL_MAX_TYPE, |
| 229 | sizeof(*sc->channel_post))) == NULL) |
| 230 | fatal("%s: allocation failed", __func__); |
| 231 | sc->channels_alloc = 10; |
| 232 | sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels)); |
| 233 | sc->IPv4or6 = AF_UNSPEC; |
| 234 | channel_handler_init(sc); |
| 235 | |
| 236 | ssh->chanctxt = sc; |
| 237 | } |
| 238 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 239 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 240 | channel_by_id(struct ssh *ssh, int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 241 | { |
| 242 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 243 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 244 | if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) { |
| 245 | logit("%s: %d: bad id", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 246 | return NULL; |
| 247 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 248 | c = ssh->chanctxt->channels[id]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 249 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 250 | logit("%s: %d: bad id: channel free", __func__, id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 251 | return NULL; |
| 252 | } |
| 253 | return c; |
| 254 | } |
| 255 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 256 | Channel * |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 257 | channel_by_remote_id(struct ssh *ssh, u_int remote_id) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 258 | { |
| 259 | Channel *c; |
| 260 | u_int i; |
| 261 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 262 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 263 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 264 | 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] | 265 | return c; |
| 266 | } |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 270 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 271 | * Returns the channel if it is allowed to receive protocol messages. |
| 272 | * Private channels, like listening sockets, may not receive messages. |
| 273 | */ |
| 274 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 275 | channel_lookup(struct ssh *ssh, int id) |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 276 | { |
| 277 | Channel *c; |
| 278 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 279 | if ((c = channel_by_id(ssh, id)) == NULL) |
| 280 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 281 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 282 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 283 | case SSH_CHANNEL_X11_OPEN: |
| 284 | case SSH_CHANNEL_LARVAL: |
| 285 | case SSH_CHANNEL_CONNECTING: |
| 286 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 287 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 288 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 289 | case SSH_CHANNEL_OPENING: |
| 290 | case SSH_CHANNEL_OPEN: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 291 | case SSH_CHANNEL_ABANDONED: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 292 | case SSH_CHANNEL_MUX_PROXY: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 293 | return c; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 294 | } |
| 295 | logit("Non-public channel %d, type %d.", id, c->type); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 296 | return NULL; |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 300 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 301 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 302 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 303 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 304 | 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] | 305 | int extusage, int nonblock, int is_tty) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 306 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 307 | struct ssh_channels *sc = ssh->chanctxt; |
| 308 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 309 | /* Update the maximum file descriptor value. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 310 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd); |
| 311 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd); |
| 312 | sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 313 | |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 314 | if (rfd != -1) |
| 315 | fcntl(rfd, F_SETFD, FD_CLOEXEC); |
| 316 | if (wfd != -1 && wfd != rfd) |
| 317 | fcntl(wfd, F_SETFD, FD_CLOEXEC); |
| 318 | if (efd != -1 && efd != rfd && efd != wfd) |
| 319 | fcntl(efd, F_SETFD, FD_CLOEXEC); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 320 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 321 | c->rfd = rfd; |
| 322 | c->wfd = wfd; |
| 323 | c->sock = (rfd == wfd) ? rfd : -1; |
| 324 | c->efd = efd; |
| 325 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 326 | |
Darren Tucker | ed3cdc0 | 2008-06-16 23:29:18 +1000 | [diff] [blame] | 327 | if ((c->isatty = is_tty) != 0) |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 328 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 329 | #ifdef _AIX |
| 330 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 1a48aec | 2008-06-16 23:35:56 +1000 | [diff] [blame] | 331 | c->wfd_isatty = is_tty || isatty(c->wfd); |
Damien Miller | c192a4c | 2013-08-01 14:29:20 +1000 | [diff] [blame] | 332 | #endif |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 333 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 334 | /* enable nonblocking mode */ |
| 335 | if (nonblock) { |
| 336 | if (rfd != -1) |
| 337 | set_nonblock(rfd); |
| 338 | if (wfd != -1) |
| 339 | set_nonblock(wfd); |
| 340 | if (efd != -1) |
| 341 | set_nonblock(efd); |
| 342 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Allocate a new channel object and set its type and socket. This will cause |
| 347 | * remote_name to be freed. |
| 348 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 349 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 350 | 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] | 351 | 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] | 352 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 353 | struct ssh_channels *sc = ssh->chanctxt; |
| 354 | u_int i, found; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 355 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 357 | /* 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] | 358 | for (i = 0; i < sc->channels_alloc; i++) { |
| 359 | if (sc->channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 360 | /* Found a free slot. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 361 | found = i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 362 | break; |
| 363 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 364 | } |
| 365 | if (i >= sc->channels_alloc) { |
| 366 | /* |
| 367 | * There are no free slots. Take last+1 slot and expand |
| 368 | * the array. |
| 369 | */ |
| 370 | found = sc->channels_alloc; |
| 371 | if (sc->channels_alloc > CHANNELS_MAX_CHANNELS) |
| 372 | fatal("%s: internal error: channels_alloc %d too big", |
| 373 | __func__, sc->channels_alloc); |
| 374 | sc->channels = xrecallocarray(sc->channels, sc->channels_alloc, |
| 375 | sc->channels_alloc + 10, sizeof(*sc->channels)); |
| 376 | sc->channels_alloc += 10; |
| 377 | debug2("channel: expanding %d", sc->channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 378 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 379 | /* Initialize and return new channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 380 | c = sc->channels[found] = xcalloc(1, sizeof(Channel)); |
| 381 | if ((c->input = sshbuf_new()) == NULL || |
| 382 | (c->output = sshbuf_new()) == NULL || |
| 383 | (c->extended = sshbuf_new()) == NULL) |
| 384 | fatal("%s: sshbuf_new failed", __func__); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 385 | c->ostate = CHAN_OUTPUT_OPEN; |
| 386 | c->istate = CHAN_INPUT_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 387 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 388 | c->self = found; |
| 389 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 390 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 391 | c->local_window = window; |
| 392 | c->local_window_max = window; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 393 | c->local_maxpacket = maxpack; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 394 | c->remote_name = xstrdup(remote_name); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 395 | c->ctl_chan = -1; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 396 | c->delayed = 1; /* prevent call to channel_post handler */ |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 397 | TAILQ_INIT(&c->status_confirms); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 398 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 399 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 400 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 401 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 402 | static void |
| 403 | channel_find_maxfd(struct ssh_channels *sc) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 404 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 405 | u_int i; |
| 406 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 407 | Channel *c; |
| 408 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 409 | for (i = 0; i < sc->channels_alloc; i++) { |
| 410 | c = sc->channels[i]; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 411 | if (c != NULL) { |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 412 | max = MAXIMUM(max, c->rfd); |
| 413 | max = MAXIMUM(max, c->wfd); |
| 414 | max = MAXIMUM(max, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 417 | sc->channel_max_fd = max; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 421 | channel_close_fd(struct ssh *ssh, int *fdp) |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 422 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 423 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 424 | int ret = 0, fd = *fdp; |
| 425 | |
| 426 | if (fd != -1) { |
| 427 | ret = close(fd); |
| 428 | *fdp = -1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 429 | if (fd == sc->channel_max_fd) |
| 430 | channel_find_maxfd(sc); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 431 | } |
| 432 | return ret; |
| 433 | } |
| 434 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 435 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 436 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 437 | channel_close_fds(struct ssh *ssh, Channel *c) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 438 | { |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 439 | int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd; |
| 440 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 441 | channel_close_fd(ssh, &c->sock); |
tb@openbsd.org | 5069320 | 2018-02-05 05:36:49 +0000 | [diff] [blame] | 442 | if (rfd != sock) |
| 443 | channel_close_fd(ssh, &c->rfd); |
| 444 | if (wfd != sock && wfd != rfd) |
| 445 | channel_close_fd(ssh, &c->wfd); |
| 446 | if (efd != sock && efd != rfd && efd != wfd) |
| 447 | channel_close_fd(ssh, &c->efd); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void |
| 451 | fwd_perm_clear(ForwardPermission *fp) |
| 452 | { |
| 453 | free(fp->host_to_connect); |
| 454 | free(fp->listen_host); |
| 455 | free(fp->listen_path); |
| 456 | bzero(fp, sizeof(*fp)); |
| 457 | } |
| 458 | |
| 459 | enum { FWDPERM_USER, FWDPERM_ADMIN }; |
| 460 | |
| 461 | static int |
| 462 | fwd_perm_list_add(struct ssh *ssh, int which, |
| 463 | const char *host_to_connect, int port_to_connect, |
| 464 | const char *listen_host, const char *listen_path, int listen_port, |
| 465 | Channel *downstream) |
| 466 | { |
| 467 | ForwardPermission **fpl; |
| 468 | u_int n, *nfpl; |
| 469 | |
| 470 | switch (which) { |
| 471 | case FWDPERM_USER: |
| 472 | fpl = &ssh->chanctxt->permitted_opens; |
| 473 | nfpl = &ssh->chanctxt->num_permitted_opens; |
| 474 | break; |
| 475 | case FWDPERM_ADMIN: |
| 476 | fpl = &ssh->chanctxt->permitted_adm_opens; |
| 477 | nfpl = &ssh->chanctxt->num_adm_permitted_opens; |
| 478 | break; |
| 479 | default: |
| 480 | fatal("%s: invalid list %d", __func__, which); |
| 481 | } |
| 482 | |
| 483 | if (*nfpl >= INT_MAX) |
| 484 | fatal("%s: overflow", __func__); |
| 485 | |
| 486 | *fpl = xrecallocarray(*fpl, *nfpl, *nfpl + 1, sizeof(**fpl)); |
| 487 | n = (*nfpl)++; |
| 488 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
| 489 | (*fpl)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 490 | (*fpl)[n].port_to_connect = port_to_connect; |
| 491 | (*fpl)[n].listen_host = MAYBE_DUP(listen_host); |
| 492 | (*fpl)[n].listen_path = MAYBE_DUP(listen_path); |
| 493 | (*fpl)[n].listen_port = listen_port; |
| 494 | (*fpl)[n].downstream = downstream; |
| 495 | #undef MAYBE_DUP |
| 496 | return (int)n; |
| 497 | } |
| 498 | |
| 499 | static void |
| 500 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 501 | { |
| 502 | struct ssh_channels *sc = ssh->chanctxt; |
| 503 | ForwardPermission *fp; |
| 504 | int r; |
| 505 | u_int i; |
| 506 | |
| 507 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 508 | fp = &sc->permitted_opens[i]; |
| 509 | if (fp->downstream != c) |
| 510 | continue; |
| 511 | |
| 512 | /* cancel on the server, since mux client is gone */ |
| 513 | debug("channel %d: cleanup remote forward for %s:%u", |
| 514 | c->self, fp->listen_host, fp->listen_port); |
| 515 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 516 | (r = sshpkt_put_cstring(ssh, |
| 517 | "cancel-tcpip-forward")) != 0 || |
| 518 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 519 | (r = sshpkt_put_cstring(ssh, |
| 520 | channel_rfwd_bind_host(fp->listen_host))) != 0 || |
| 521 | (r = sshpkt_put_u32(ssh, fp->listen_port)) != 0 || |
| 522 | (r = sshpkt_send(ssh)) != 0) { |
| 523 | fatal("%s: channel %i: %s", __func__, |
| 524 | c->self, ssh_err(r)); |
| 525 | } |
| 526 | fwd_perm_clear(fp); /* unregister */ |
| 527 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 531 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 532 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 533 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 534 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 535 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 536 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 537 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 538 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 539 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 540 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 541 | if ((other = sc->channels[i]) == NULL) |
| 542 | continue; |
| 543 | n++; |
| 544 | /* detach from mux client and prepare for closing */ |
| 545 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 546 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 547 | other->mux_ctx == c) { |
| 548 | other->mux_ctx = NULL; |
| 549 | other->type = SSH_CHANNEL_OPEN; |
| 550 | other->istate = CHAN_INPUT_CLOSED; |
| 551 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 554 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 555 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 556 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 557 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 558 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 559 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 560 | s = channel_open_message(ssh); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 561 | debug3("channel %d: status: %s", c->self, s); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 562 | free(s); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 563 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 564 | channel_close_fds(ssh, c); |
| 565 | sshbuf_free(c->input); |
| 566 | sshbuf_free(c->output); |
| 567 | sshbuf_free(c->extended); |
| 568 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 569 | free(c->remote_name); |
| 570 | c->remote_name = NULL; |
| 571 | free(c->path); |
| 572 | c->path = NULL; |
| 573 | free(c->listening_addr); |
| 574 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 575 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 576 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 577 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 578 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 579 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 580 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 581 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 582 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 583 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 584 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 585 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 586 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | } |
| 588 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 589 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 590 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 591 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 592 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 593 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 594 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 595 | if (ssh->chanctxt->channels[i] != NULL) |
| 596 | channel_free(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 601 | * descriptors after a fork. |
| 602 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 603 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 604 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 605 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 606 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 607 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 608 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 609 | if (ssh->chanctxt->channels[i] != NULL) |
| 610 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 614 | * Stop listening to channels. |
| 615 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 616 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 617 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 618 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 619 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 620 | Channel *c; |
| 621 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 622 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 623 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 624 | if (c != NULL) { |
| 625 | switch (c->type) { |
| 626 | case SSH_CHANNEL_AUTH_SOCKET: |
| 627 | case SSH_CHANNEL_PORT_LISTENER: |
| 628 | case SSH_CHANNEL_RPORT_LISTENER: |
| 629 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 630 | case SSH_CHANNEL_UNIX_LISTENER: |
| 631 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 632 | channel_close_fd(ssh, &c->sock); |
| 633 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 641 | * Returns true if no channel has too much buffered data, and false if one or |
| 642 | * more channel is overfull. |
| 643 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 644 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 645 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 646 | { |
| 647 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 648 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 649 | Channel *c; |
| 650 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 651 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 652 | c = ssh->chanctxt->channels[i]; |
| 653 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 654 | continue; |
| 655 | if (sshbuf_len(c->output) > maxsize) { |
| 656 | debug2("channel %d: big output buffer %zu > %u", |
| 657 | c->self, sshbuf_len(c->output), maxsize); |
| 658 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 665 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 666 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 667 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 668 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 669 | Channel *c; |
| 670 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 671 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 672 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 673 | if (c == NULL) |
| 674 | continue; |
| 675 | switch (c->type) { |
| 676 | case SSH_CHANNEL_X11_LISTENER: |
| 677 | case SSH_CHANNEL_PORT_LISTENER: |
| 678 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 679 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 680 | case SSH_CHANNEL_CLOSED: |
| 681 | case SSH_CHANNEL_AUTH_SOCKET: |
| 682 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 683 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 684 | case SSH_CHANNEL_CONNECTING: |
| 685 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 686 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 687 | case SSH_CHANNEL_UNIX_LISTENER: |
| 688 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 689 | continue; |
| 690 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 691 | continue; |
| 692 | case SSH_CHANNEL_OPENING: |
| 693 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 694 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 695 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 696 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 697 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 698 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 699 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 700 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 701 | /* NOTREACHED */ |
| 702 | } |
| 703 | } |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 708 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 709 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 710 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 711 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 712 | Channel *c; |
| 713 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 714 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 715 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 716 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 717 | continue; |
| 718 | switch (c->type) { |
| 719 | case SSH_CHANNEL_CLOSED: |
| 720 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 721 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 722 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 723 | case SSH_CHANNEL_X11_LISTENER: |
| 724 | case SSH_CHANNEL_PORT_LISTENER: |
| 725 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 726 | case SSH_CHANNEL_MUX_LISTENER: |
| 727 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 728 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 729 | case SSH_CHANNEL_OPENING: |
| 730 | case SSH_CHANNEL_CONNECTING: |
| 731 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 732 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 733 | case SSH_CHANNEL_UNIX_LISTENER: |
| 734 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 735 | continue; |
| 736 | case SSH_CHANNEL_LARVAL: |
| 737 | case SSH_CHANNEL_AUTH_SOCKET: |
| 738 | case SSH_CHANNEL_OPEN: |
| 739 | case SSH_CHANNEL_X11_OPEN: |
| 740 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 741 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 742 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 743 | /* NOTREACHED */ |
| 744 | } |
| 745 | } |
| 746 | return -1; |
| 747 | } |
| 748 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 749 | /* |
| 750 | * Returns a message describing the currently open forwarded connections, |
| 751 | * suitable for sending to the client. The message contains crlf pairs for |
| 752 | * newlines. |
| 753 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 754 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 755 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 756 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 757 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 758 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 759 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 760 | int r; |
| 761 | char *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 762 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 763 | if ((buf = sshbuf_new()) == NULL) |
| 764 | fatal("%s: sshbuf_new", __func__); |
| 765 | if ((r = sshbuf_putf(buf, |
| 766 | "The following connections are open:\r\n")) != 0) |
| 767 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 768 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 769 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 770 | if (c == NULL) |
| 771 | continue; |
| 772 | switch (c->type) { |
| 773 | case SSH_CHANNEL_X11_LISTENER: |
| 774 | case SSH_CHANNEL_PORT_LISTENER: |
| 775 | case SSH_CHANNEL_RPORT_LISTENER: |
| 776 | case SSH_CHANNEL_CLOSED: |
| 777 | case SSH_CHANNEL_AUTH_SOCKET: |
| 778 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 779 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 780 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 781 | case SSH_CHANNEL_UNIX_LISTENER: |
| 782 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 783 | continue; |
| 784 | case SSH_CHANNEL_LARVAL: |
| 785 | case SSH_CHANNEL_OPENING: |
| 786 | case SSH_CHANNEL_CONNECTING: |
| 787 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 788 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 789 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 790 | case SSH_CHANNEL_OPEN: |
| 791 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 792 | case SSH_CHANNEL_MUX_PROXY: |
| 793 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 794 | if ((r = sshbuf_putf(buf, " #%d %.300s " |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 795 | "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n", |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 796 | c->self, c->remote_name, |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 797 | c->type, |
| 798 | c->have_remote_id ? "r" : "nr", c->remote_id, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 799 | c->istate, sshbuf_len(c->input), |
| 800 | c->ostate, sshbuf_len(c->output), |
| 801 | c->rfd, c->wfd, c->ctl_chan)) != 0) |
| 802 | fatal("%s: sshbuf_putf: %s", |
| 803 | __func__, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 804 | continue; |
| 805 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 806 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 807 | /* NOTREACHED */ |
| 808 | } |
| 809 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 810 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 811 | fatal("%s: sshbuf_dup_string", __func__); |
| 812 | sshbuf_free(buf); |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | static void |
| 817 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 818 | { |
| 819 | int r; |
| 820 | |
| 821 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 822 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 823 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 824 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 825 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 826 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 827 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 831 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 832 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 833 | Channel *c = channel_lookup(ssh, id); |
| 834 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 835 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 836 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 837 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 838 | return; |
| 839 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 840 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 841 | open_preamble(ssh, __func__, c, c->ctype); |
| 842 | if ((r = sshpkt_send(ssh)) != 0) |
| 843 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 847 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 848 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 849 | Channel *c = channel_lookup(ssh, id); |
| 850 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 851 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 852 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 853 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 854 | return; |
| 855 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 856 | if (!c->have_remote_id) |
| 857 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 858 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 859 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 860 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 861 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 862 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 863 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 864 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 865 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 866 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 867 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 868 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 869 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 870 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 871 | { |
| 872 | struct channel_confirm *cc; |
| 873 | Channel *c; |
| 874 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 875 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 876 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 877 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 878 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 879 | cc->cb = cb; |
| 880 | cc->abandon_cb = abandon_cb; |
| 881 | cc->ctx = ctx; |
| 882 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 883 | } |
| 884 | |
| 885 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 886 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 887 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 888 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 889 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 890 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 891 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 892 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 895 | c->open_confirm = fn; |
| 896 | c->open_confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 897 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 898 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 899 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 900 | channel_register_cleanup(struct ssh *ssh, int id, |
| 901 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 902 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 903 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 904 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 905 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 906 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 907 | return; |
| 908 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 909 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 910 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 911 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 912 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 913 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 914 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 915 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 916 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 917 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 918 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 919 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 922 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 923 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 924 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 925 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 926 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 927 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 928 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 929 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 930 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 931 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 932 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 933 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 934 | return; |
| 935 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 936 | c->input_filter = ifn; |
| 937 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 938 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 939 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 943 | 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] | 944 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 945 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 946 | Channel *c = channel_lookup(ssh, id); |
| 947 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 948 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 949 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 950 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 951 | if (!c->have_remote_id) |
| 952 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 953 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 954 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 955 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 956 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 957 | |
| 958 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 959 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 960 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 961 | (r = sshpkt_send(ssh)) != 0) |
| 962 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 965 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 966 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 967 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 968 | { |
| 969 | FD_SET(c->sock, readset); |
| 970 | } |
| 971 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 972 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 973 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 974 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 975 | { |
| 976 | debug3("channel %d: waiting for connection", c->self); |
| 977 | FD_SET(c->sock, writeset); |
| 978 | } |
| 979 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 980 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 981 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 982 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 983 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 984 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 985 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 986 | sshbuf_len(c->input) < c->remote_window && |
| 987 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 988 | FD_SET(c->rfd, readset); |
| 989 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 990 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 991 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 992 | FD_SET(c->wfd, writeset); |
| 993 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 994 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 995 | debug2("channel %d: " |
| 996 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 997 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 998 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 999 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1003 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 1004 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1005 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1006 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1007 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1008 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1009 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1010 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1011 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1012 | FD_SET(c->efd, readset); |
| 1013 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1014 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1015 | } |
| 1016 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1017 | /* |
| 1018 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1019 | * connection (when authentication spoofing is being done) remains in this |
| 1020 | * state until the first packet has been completely read. The authentication |
| 1021 | * data in that packet is then substituted by the real data if it matches the |
| 1022 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1023 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1024 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1025 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1026 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1027 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1028 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1029 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1030 | u_char *ucp; |
| 1031 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1032 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1033 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1034 | if (sc->x11_refuse_time != 0 && |
| 1035 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1036 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1037 | "expired"); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1041 | /* 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] | 1042 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1043 | return 0; |
| 1044 | |
| 1045 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1046 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1047 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1048 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1049 | data_len = 256 * ucp[8] + ucp[9]; |
| 1050 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1051 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1052 | data_len = ucp[8] + 256 * ucp[9]; |
| 1053 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1054 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1055 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1060 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1061 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1062 | return 0; |
| 1063 | |
| 1064 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1065 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1066 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1067 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1068 | return -1; |
| 1069 | } |
| 1070 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1071 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1072 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1073 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1074 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1075 | return -1; |
| 1076 | } |
| 1077 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1078 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1079 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1080 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1081 | return -1; |
| 1082 | } |
| 1083 | /* |
| 1084 | * Received authentication protocol and data match |
| 1085 | * our fake data. Substitute the fake data with real |
| 1086 | * data. |
| 1087 | */ |
| 1088 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1089 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1090 | return 1; |
| 1091 | } |
| 1092 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1093 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1094 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1095 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1096 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1097 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* c->force_drain = 1; */ |
| 1100 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1101 | if (ret == 1) { |
| 1102 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1103 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1104 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1105 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1106 | debug2("X11 rejected %d i%d/o%d", |
| 1107 | c->self, c->istate, c->ostate); |
| 1108 | chan_read_failed(ssh, c); |
| 1109 | sshbuf_reset(c->input); |
| 1110 | chan_ibuf_empty(ssh, c); |
| 1111 | sshbuf_reset(c->output); |
| 1112 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1113 | 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] | 1114 | } |
| 1115 | } |
| 1116 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1117 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1118 | channel_pre_mux_client(struct ssh *ssh, |
| 1119 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1120 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1121 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1122 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1123 | FD_SET(c->rfd, readset); |
| 1124 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1125 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1126 | sshbuf_reset(c->input); |
| 1127 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1128 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1129 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1130 | } |
| 1131 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1132 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1133 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1134 | FD_SET(c->wfd, writeset); |
| 1135 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1136 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1140 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1141 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1142 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1143 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1144 | const u_char *p; |
| 1145 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1146 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1147 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1148 | struct { |
| 1149 | u_int8_t version; |
| 1150 | u_int8_t command; |
| 1151 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1152 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1153 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1154 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1155 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1156 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1157 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1158 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1159 | len = sizeof(s4_req); |
| 1160 | if (have < len) |
| 1161 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1162 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1163 | |
| 1164 | need = 1; |
| 1165 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1166 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1167 | debug2("channel %d: socks4a request", c->self); |
| 1168 | /* ... and needs an extra string (the hostname) */ |
| 1169 | need = 2; |
| 1170 | } |
| 1171 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1172 | for (found = 0, i = len; i < have; i++) { |
| 1173 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1174 | found++; |
| 1175 | if (found == need) |
| 1176 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1177 | } |
| 1178 | if (i > 1024) { |
| 1179 | /* the peer is probably sending garbage */ |
| 1180 | debug("channel %d: decode socks4: too long", |
| 1181 | c->self); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1185 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1186 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1187 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1188 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1189 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1190 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1191 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1192 | return -1; |
| 1193 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1194 | have = sshbuf_len(input); |
| 1195 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1196 | if (memchr(p, '\0', have) == NULL) { |
| 1197 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1198 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1199 | return -1; |
| 1200 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1201 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1202 | 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] | 1203 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1204 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1205 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1206 | fatal("%s: channel %d: consume: %s", __func__, |
| 1207 | c->self, ssh_err(r)); |
| 1208 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1209 | free(c->path); |
| 1210 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1211 | if (need == 1) { /* SOCKS4: one string */ |
| 1212 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1213 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1214 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1215 | have = sshbuf_len(input); |
| 1216 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1217 | if (memchr(p, '\0', have) == NULL) { |
| 1218 | error("channel %d: decode socks4a: host not nul " |
| 1219 | "terminated", c->self); |
| 1220 | return -1; |
| 1221 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1222 | len = strlen(p); |
| 1223 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1224 | c->self, p, len); |
| 1225 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1226 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1227 | error("channel %d: hostname \"%.100s\" too long", |
| 1228 | c->self, p); |
| 1229 | return -1; |
| 1230 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1231 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1232 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1233 | fatal("%s: channel %d: consume: %s", __func__, |
| 1234 | c->self, ssh_err(r)); |
| 1235 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1236 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1237 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1238 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1239 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1240 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1241 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1242 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1243 | debug("channel %d: cannot handle: %s cn %d", |
| 1244 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1245 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1246 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1247 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1248 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1249 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1250 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1251 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1252 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1253 | c->self, ssh_err(r)); |
| 1254 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1255 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1258 | /* try to decode a socks5 header */ |
| 1259 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1260 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1261 | #define SSH_SOCKS5_IPV4 0x01 |
| 1262 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1263 | #define SSH_SOCKS5_IPV6 0x04 |
| 1264 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1265 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1266 | |
| 1267 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1268 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1269 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1270 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1271 | struct { |
| 1272 | u_int8_t version; |
| 1273 | u_int8_t command; |
| 1274 | u_int8_t reserved; |
| 1275 | u_int8_t atyp; |
| 1276 | } s5_req, s5_rsp; |
| 1277 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1278 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1279 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1280 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1281 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1282 | |
| 1283 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1284 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1285 | if (p[0] != 0x05) |
| 1286 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1287 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1288 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1289 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1290 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1291 | return 0; |
| 1292 | nmethods = p[1]; |
| 1293 | if (have < nmethods + 2) |
| 1294 | return 0; |
| 1295 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1296 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1297 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1298 | found = 1; |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | if (!found) { |
| 1303 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1304 | c->self); |
| 1305 | return -1; |
| 1306 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1307 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1308 | fatal("%s: channel %d: consume: %s", __func__, |
| 1309 | c->self, ssh_err(r)); |
| 1310 | } |
| 1311 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1312 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1313 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1314 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1315 | c->self, ssh_err(r)); |
| 1316 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1317 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1318 | debug2("channel %d: socks5 auth done", c->self); |
| 1319 | return 0; /* need more */ |
| 1320 | } |
| 1321 | debug2("channel %d: socks5 post auth", c->self); |
| 1322 | if (have < sizeof(s5_req)+1) |
| 1323 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1324 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1325 | if (s5_req.version != 0x05 || |
| 1326 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1327 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1328 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1329 | return -1; |
| 1330 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1331 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1332 | case SSH_SOCKS5_IPV4: |
| 1333 | addrlen = 4; |
| 1334 | af = AF_INET; |
| 1335 | break; |
| 1336 | case SSH_SOCKS5_DOMAIN: |
| 1337 | addrlen = p[sizeof(s5_req)]; |
| 1338 | af = -1; |
| 1339 | break; |
| 1340 | case SSH_SOCKS5_IPV6: |
| 1341 | addrlen = 16; |
| 1342 | af = AF_INET6; |
| 1343 | break; |
| 1344 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1345 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1346 | return -1; |
| 1347 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1348 | need = sizeof(s5_req) + addrlen + 2; |
| 1349 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1350 | need++; |
| 1351 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1352 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1353 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1354 | fatal("%s: channel %d: consume: %s", __func__, |
| 1355 | c->self, ssh_err(r)); |
| 1356 | } |
| 1357 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1358 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1359 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1360 | fatal("%s: channel %d: consume: %s", __func__, |
| 1361 | c->self, ssh_err(r)); |
| 1362 | } |
| 1363 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1364 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1365 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1366 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1367 | return -1; |
| 1368 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1369 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1370 | free(c->path); |
| 1371 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1372 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1373 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1374 | error("channel %d: dynamic request: socks5 hostname " |
| 1375 | "\"%.100s\" too long", c->self, dest_addr); |
| 1376 | return -1; |
| 1377 | } |
| 1378 | c->path = xstrdup(dest_addr); |
| 1379 | } else { |
| 1380 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1381 | return -1; |
| 1382 | c->path = xstrdup(ntop); |
| 1383 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1384 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1385 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1386 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1387 | c->self, c->path, c->host_port, s5_req.command); |
| 1388 | |
| 1389 | s5_rsp.version = 0x05; |
| 1390 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1391 | s5_rsp.reserved = 0; /* ignored */ |
| 1392 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1393 | dest_port = 0; /* ignored */ |
| 1394 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1395 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1396 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1397 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1398 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1399 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1400 | return 1; |
| 1401 | } |
| 1402 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1403 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1404 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1405 | 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] | 1406 | { |
| 1407 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1408 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1409 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1410 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1411 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1412 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1413 | 0, "stdio-forward", /*nonblock*/0); |
| 1414 | |
| 1415 | c->path = xstrdup(host_to_connect); |
| 1416 | c->host_port = port_to_connect; |
| 1417 | c->listening_port = 0; |
| 1418 | c->force_drain = 1; |
| 1419 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1420 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1421 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1422 | |
| 1423 | return c; |
| 1424 | } |
| 1425 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1426 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1427 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1428 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1429 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1430 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1431 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1432 | u_int have; |
| 1433 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1434 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1435 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1436 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1437 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1438 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1439 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1440 | /* need more */ |
| 1441 | FD_SET(c->sock, readset); |
| 1442 | return; |
| 1443 | } |
| 1444 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1445 | p = sshbuf_ptr(c->input); |
| 1446 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1447 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1448 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1449 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1450 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1451 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1452 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1453 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1454 | default: |
| 1455 | ret = -1; |
| 1456 | break; |
| 1457 | } |
| 1458 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1459 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1460 | } else if (ret == 0) { |
| 1461 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1462 | /* need more */ |
| 1463 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1464 | if (sshbuf_len(c->output)) |
| 1465 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1466 | } else { |
| 1467 | /* switch to the next state */ |
| 1468 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1469 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1472 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1473 | /* simulate read-error */ |
| 1474 | static void |
| 1475 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1476 | { |
| 1477 | c->type = SSH_CHANNEL_OPEN; |
| 1478 | chan_read_failed(ssh, c); |
| 1479 | sshbuf_reset(c->input); |
| 1480 | chan_ibuf_empty(ssh, c); |
| 1481 | sshbuf_reset(c->output); |
| 1482 | chan_write_failed(ssh, c); |
| 1483 | } |
| 1484 | |
| 1485 | /* reverse dynamic port forwarding */ |
| 1486 | static void |
| 1487 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1488 | { |
| 1489 | const u_char *p; |
| 1490 | u_int have, len; |
| 1491 | int r, ret; |
| 1492 | |
| 1493 | have = sshbuf_len(c->output); |
| 1494 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1495 | /* sshbuf_dump(c->output, stderr); */ |
| 1496 | /* EOF received */ |
| 1497 | if (c->flags & CHAN_EOF_RCVD) { |
| 1498 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1499 | fatal("%s: channel %d: consume: %s", |
| 1500 | __func__, c->self, ssh_err(r)); |
| 1501 | } |
| 1502 | rdynamic_close(ssh, c); |
| 1503 | return; |
| 1504 | } |
| 1505 | /* check if the fixed size part of the packet is in buffer. */ |
| 1506 | if (have < 3) |
| 1507 | return; |
| 1508 | /* try to guess the protocol */ |
| 1509 | p = sshbuf_ptr(c->output); |
| 1510 | switch (p[0]) { |
| 1511 | case 0x04: |
| 1512 | /* switch input/output for reverse forwarding */ |
| 1513 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1514 | break; |
| 1515 | case 0x05: |
| 1516 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1517 | break; |
| 1518 | default: |
| 1519 | ret = -1; |
| 1520 | break; |
| 1521 | } |
| 1522 | if (ret < 0) { |
| 1523 | rdynamic_close(ssh, c); |
| 1524 | } else if (ret == 0) { |
| 1525 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1526 | /* send socks request to peer */ |
| 1527 | len = sshbuf_len(c->input); |
| 1528 | if (len > 0 && len < c->remote_window) { |
| 1529 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1530 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1531 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1532 | (r = sshpkt_send(ssh)) != 0) { |
| 1533 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1534 | c->self, ssh_err(r)); |
| 1535 | } |
| 1536 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1537 | fatal("%s: channel %d: consume: %s", |
| 1538 | __func__, c->self, ssh_err(r)); |
| 1539 | } |
| 1540 | c->remote_window -= len; |
| 1541 | } |
| 1542 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1543 | /* the connect failed */ |
| 1544 | rdynamic_close(ssh, c); |
| 1545 | } |
| 1546 | } |
| 1547 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1548 | /* This is our fake X11 server socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1549 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1550 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1551 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1552 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1553 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1554 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1555 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1556 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1557 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1558 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1559 | if (!FD_ISSET(c->sock, readset)) |
| 1560 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1561 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1562 | debug("X11 connection requested."); |
| 1563 | addrlen = sizeof(addr); |
| 1564 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1565 | if (c->single_connection) { |
| 1566 | oerrno = errno; |
| 1567 | debug2("single_connection: closing X11 listener."); |
| 1568 | channel_close_fd(ssh, &c->sock); |
| 1569 | chan_mark_dead(ssh, c); |
| 1570 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1571 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1572 | if (newsock < 0) { |
| 1573 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1574 | errno != ECONNABORTED) |
| 1575 | error("accept: %.100s", strerror(errno)); |
| 1576 | if (errno == EMFILE || errno == ENFILE) |
| 1577 | c->notbefore = monotime() + 1; |
| 1578 | return; |
| 1579 | } |
| 1580 | set_nodelay(newsock); |
| 1581 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1582 | remote_port = get_peer_port(newsock); |
| 1583 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1584 | remote_ipaddr, remote_port); |
| 1585 | |
| 1586 | nc = channel_new(ssh, "accepted x11 socket", |
| 1587 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1588 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1589 | open_preamble(ssh, __func__, nc, "x11"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1590 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1591 | (r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1592 | fatal("%s: channel %i: reply %s", __func__, |
| 1593 | c->self, ssh_err(r)); |
| 1594 | } |
| 1595 | if ((r = sshpkt_send(ssh)) != 0) |
| 1596 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
| 1597 | free(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1598 | } |
| 1599 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1600 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1601 | port_open_helper(struct ssh *ssh, Channel *c, char *rtype) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1602 | { |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1603 | char *local_ipaddr = get_local_ipaddr(c->sock); |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 1604 | int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1605 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1606 | int remote_port = get_peer_port(c->sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1607 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1608 | |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1609 | if (remote_port == -1) { |
| 1610 | /* Fake addr/port to appease peers that validate it (Tectia) */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1611 | free(remote_ipaddr); |
Damien Miller | d636943 | 2010-02-02 17:02:07 +1100 | [diff] [blame] | 1612 | remote_ipaddr = xstrdup("127.0.0.1"); |
| 1613 | remote_port = 65535; |
| 1614 | } |
| 1615 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1616 | free(c->remote_name); |
| 1617 | xasprintf(&c->remote_name, |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1618 | "%s: listening port %d for %.100s port %d, " |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1619 | "connect from %.200s port %d to %.100s port %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1620 | rtype, c->listening_port, c->path, c->host_port, |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1621 | remote_ipaddr, remote_port, local_ipaddr, local_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1622 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1623 | open_preamble(ssh, __func__, c, rtype); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1624 | if (strcmp(rtype, "direct-tcpip") == 0) { |
| 1625 | /* target host, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1626 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1627 | (r = sshpkt_put_u32(ssh, c->host_port)) != 0) { |
| 1628 | fatal("%s: channel %i: reply %s", __func__, |
| 1629 | c->self, ssh_err(r)); |
| 1630 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1631 | } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) { |
| 1632 | /* target path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1633 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1634 | fatal("%s: channel %i: reply %s", __func__, |
| 1635 | c->self, ssh_err(r)); |
| 1636 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1637 | } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1638 | /* listen path */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1639 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) { |
| 1640 | fatal("%s: channel %i: reply %s", __func__, |
| 1641 | c->self, ssh_err(r)); |
| 1642 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1643 | } else { |
| 1644 | /* listen address, port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1645 | if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 || |
| 1646 | (r = sshpkt_put_u32(ssh, local_port)) != 0) { |
| 1647 | fatal("%s: channel %i: reply %s", __func__, |
| 1648 | c->self, ssh_err(r)); |
| 1649 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1650 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1651 | if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) { |
| 1652 | /* reserved for future owner/mode info */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1653 | if ((r = sshpkt_put_cstring(ssh, "")) != 0) { |
| 1654 | fatal("%s: channel %i: reply %s", __func__, |
| 1655 | c->self, ssh_err(r)); |
| 1656 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1657 | } else { |
| 1658 | /* originator host and port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1659 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 || |
| 1660 | (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) { |
| 1661 | fatal("%s: channel %i: reply %s", __func__, |
| 1662 | c->self, ssh_err(r)); |
| 1663 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 1664 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1665 | if ((r = sshpkt_send(ssh)) != 0) |
| 1666 | fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1667 | free(remote_ipaddr); |
Damien Miller | 4def184 | 2013-12-29 17:45:26 +1100 | [diff] [blame] | 1668 | free(local_ipaddr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1671 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1672 | 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] | 1673 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1674 | ssh->chanctxt->x11_refuse_time = refuse_time; |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1677 | /* |
| 1678 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1679 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1680 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1681 | channel_post_port_listener(struct ssh *ssh, Channel *c, |
| 1682 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1683 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1684 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1685 | struct sockaddr_storage addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1686 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1687 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1688 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1689 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1690 | if (!FD_ISSET(c->sock, readset)) |
| 1691 | return; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1692 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1693 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
| 1694 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1695 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1696 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1697 | nextstate = SSH_CHANNEL_OPENING; |
| 1698 | rtype = "forwarded-tcpip"; |
| 1699 | } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) { |
| 1700 | nextstate = SSH_CHANNEL_OPENING; |
| 1701 | rtype = "forwarded-streamlocal@openssh.com"; |
| 1702 | } else if (c->host_port == PORT_STREAMLOCAL) { |
| 1703 | nextstate = SSH_CHANNEL_OPENING; |
| 1704 | rtype = "direct-streamlocal@openssh.com"; |
| 1705 | } else if (c->host_port == 0) { |
| 1706 | nextstate = SSH_CHANNEL_DYNAMIC; |
| 1707 | rtype = "dynamic-tcpip"; |
| 1708 | } else { |
| 1709 | nextstate = SSH_CHANNEL_OPENING; |
| 1710 | rtype = "direct-tcpip"; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1711 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1712 | |
| 1713 | addrlen = sizeof(addr); |
| 1714 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1715 | if (newsock < 0) { |
| 1716 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1717 | errno != ECONNABORTED) |
| 1718 | error("accept: %.100s", strerror(errno)); |
| 1719 | if (errno == EMFILE || errno == ENFILE) |
| 1720 | c->notbefore = monotime() + 1; |
| 1721 | return; |
| 1722 | } |
| 1723 | if (c->host_port != PORT_STREAMLOCAL) |
| 1724 | set_nodelay(newsock); |
| 1725 | nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1, |
| 1726 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
| 1727 | nc->listening_port = c->listening_port; |
| 1728 | nc->host_port = c->host_port; |
| 1729 | if (c->path != NULL) |
| 1730 | nc->path = xstrdup(c->path); |
| 1731 | |
| 1732 | if (nextstate != SSH_CHANNEL_DYNAMIC) |
| 1733 | port_open_helper(ssh, nc, rtype); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | /* |
| 1737 | * This is the authentication agent socket listening for connections from |
| 1738 | * clients. |
| 1739 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1740 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1741 | channel_post_auth_listener(struct ssh *ssh, Channel *c, |
| 1742 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1743 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1744 | Channel *nc; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1745 | int r, newsock; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1746 | struct sockaddr_storage addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1747 | socklen_t addrlen; |
| 1748 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1749 | if (!FD_ISSET(c->sock, readset)) |
| 1750 | return; |
| 1751 | |
| 1752 | addrlen = sizeof(addr); |
| 1753 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1754 | if (newsock < 0) { |
| 1755 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1756 | if (errno == EMFILE || errno == ENFILE) |
| 1757 | c->notbefore = monotime() + 1; |
| 1758 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1759 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1760 | nc = channel_new(ssh, "accepted auth socket", |
| 1761 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1762 | c->local_window_max, c->local_maxpacket, |
| 1763 | 0, "accepted auth socket", 1); |
| 1764 | open_preamble(ssh, __func__, nc, "auth-agent@openssh.com"); |
| 1765 | if ((r = sshpkt_send(ssh)) != 0) |
| 1766 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1767 | } |
| 1768 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1769 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1770 | channel_post_connecting(struct ssh *ssh, Channel *c, |
| 1771 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1772 | { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1773 | int err = 0, sock, isopen, r; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1774 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1775 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1776 | if (!FD_ISSET(c->sock, writeset)) |
| 1777 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 1778 | if (!c->have_remote_id) |
| 1779 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1780 | /* for rdynamic the OPEN_CONFIRMATION has been sent already */ |
| 1781 | isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1782 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { |
| 1783 | err = errno; |
| 1784 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1785 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1786 | if (err == 0) { |
| 1787 | debug("channel %d: connected to %s port %d", |
| 1788 | c->self, c->connect_ctx.host, c->connect_ctx.port); |
| 1789 | channel_connect_ctx_free(&c->connect_ctx); |
| 1790 | c->type = SSH_CHANNEL_OPEN; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1791 | if (isopen) { |
| 1792 | /* no message necessary */ |
| 1793 | } else { |
| 1794 | if ((r = sshpkt_start(ssh, |
| 1795 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 1796 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1797 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 1798 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 1799 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) |
| 1800 | != 0) |
| 1801 | fatal("%s: channel %i: confirm: %s", __func__, |
| 1802 | c->self, ssh_err(r)); |
| 1803 | if ((r = sshpkt_send(ssh)) != 0) |
| 1804 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1805 | ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } else { |
| 1808 | debug("channel %d: connection failed: %s", |
| 1809 | c->self, strerror(err)); |
| 1810 | /* Try next address, if any */ |
| 1811 | if ((sock = connect_next(&c->connect_ctx)) > 0) { |
| 1812 | close(c->sock); |
| 1813 | c->sock = c->rfd = c->wfd = sock; |
| 1814 | channel_find_maxfd(ssh->chanctxt); |
| 1815 | return; |
| 1816 | } |
| 1817 | /* Exhausted all addresses */ |
| 1818 | error("connect_to %.100s port %d: failed.", |
| 1819 | c->connect_ctx.host, c->connect_ctx.port); |
| 1820 | channel_connect_ctx_free(&c->connect_ctx); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1821 | if (isopen) { |
| 1822 | rdynamic_close(ssh, c); |
| 1823 | } else { |
| 1824 | if ((r = sshpkt_start(ssh, |
| 1825 | SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || |
| 1826 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1827 | (r = sshpkt_put_u32(ssh, |
| 1828 | SSH2_OPEN_CONNECT_FAILED)) != 0 || |
| 1829 | (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1830 | (r = sshpkt_put_cstring(ssh, "")) != 0) { |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1831 | fatal("%s: channel %i: failure: %s", __func__, |
| 1832 | c->self, ssh_err(r)); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 1833 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1834 | if ((r = sshpkt_send(ssh)) != 0) |
| 1835 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1836 | ssh_err(r)); |
| 1837 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1838 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1839 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1842 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1843 | channel_handle_rfd(struct ssh *ssh, Channel *c, |
| 1844 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1845 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1846 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1847 | ssize_t len; |
| 1848 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1849 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1850 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1853 | return 1; |
| 1854 | |
| 1855 | errno = 0; |
| 1856 | len = read(c->rfd, buf, sizeof(buf)); |
| 1857 | if (len < 0 && (errno == EINTR || |
| 1858 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1859 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1860 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1861 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1862 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1863 | if ((!c->isatty && len <= 0) || |
| 1864 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1865 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1866 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1867 | c->self, c->rfd, len); |
| 1868 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1869 | debug2("channel %d: not open", c->self); |
| 1870 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1871 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1872 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1873 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1874 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1875 | return -1; |
| 1876 | } |
| 1877 | if (c->input_filter != NULL) { |
| 1878 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1879 | debug2("channel %d: filter stops", c->self); |
| 1880 | chan_read_failed(ssh, c); |
| 1881 | } |
| 1882 | } else if (c->datagram) { |
| 1883 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1884 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1885 | c->self, ssh_err(r)); |
| 1886 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1887 | fatal("%s: channel %d: put data: %s", __func__, |
| 1888 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1889 | } |
| 1890 | return 1; |
| 1891 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1892 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1893 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1894 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1895 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1896 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1897 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1898 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 1899 | size_t dlen, olen = 0; |
| 1900 | int r, len; |
| 1901 | |
| 1902 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 1903 | sshbuf_len(c->output) == 0) |
| 1904 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1905 | |
| 1906 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1907 | olen = sshbuf_len(c->output); |
| 1908 | if (c->output_filter != NULL) { |
| 1909 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 1910 | debug2("channel %d: filter stops", c->self); |
| 1911 | if (c->type != SSH_CHANNEL_OPEN) |
| 1912 | chan_mark_dead(ssh, c); |
| 1913 | else |
| 1914 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1915 | return -1; |
| 1916 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1917 | } else if (c->datagram) { |
| 1918 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 1919 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 1920 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 1921 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1922 | } else { |
| 1923 | buf = data = sshbuf_mutable_ptr(c->output); |
| 1924 | dlen = sshbuf_len(c->output); |
| 1925 | } |
| 1926 | |
| 1927 | if (c->datagram) { |
| 1928 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 1929 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1930 | free(data); |
| 1931 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 1932 | errno == EWOULDBLOCK)) |
| 1933 | return 1; |
| 1934 | if (len <= 0) |
| 1935 | goto write_fail; |
| 1936 | goto out; |
| 1937 | } |
| 1938 | |
| 1939 | #ifdef _AIX |
| 1940 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 1941 | if (c->wfd_isatty) |
| 1942 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 1943 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1944 | |
| 1945 | len = write(c->wfd, buf, dlen); |
| 1946 | if (len < 0 && |
| 1947 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 1948 | return 1; |
| 1949 | if (len <= 0) { |
| 1950 | write_fail: |
| 1951 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1952 | debug2("channel %d: not open", c->self); |
| 1953 | chan_mark_dead(ssh, c); |
| 1954 | return -1; |
| 1955 | } else { |
| 1956 | chan_write_failed(ssh, c); |
| 1957 | } |
| 1958 | return -1; |
| 1959 | } |
| 1960 | #ifndef BROKEN_TCGETATTR_ICANON |
| 1961 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 1962 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 1963 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 1964 | /* |
| 1965 | * Simulate echo to reduce the impact of |
| 1966 | * traffic analysis. We need to match the |
| 1967 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 1968 | * (4 byte channel id + buf) |
| 1969 | */ |
| 1970 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 1971 | (r = sshpkt_send(ssh)) != 0) |
| 1972 | fatal("%s: channel %d: ignore: %s", |
| 1973 | __func__, c->self, ssh_err(r)); |
| 1974 | } |
| 1975 | } |
| 1976 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 1977 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 1978 | fatal("%s: channel %d: consume: %s", |
| 1979 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1980 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 1981 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1982 | c->local_consumed += olen - sshbuf_len(c->output); |
| 1983 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1984 | return 1; |
| 1985 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1986 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1987 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1988 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 1989 | fd_set *readset, fd_set *writeset) |
| 1990 | { |
| 1991 | int r; |
| 1992 | ssize_t len; |
| 1993 | |
| 1994 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 1995 | return 1; |
| 1996 | |
| 1997 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 1998 | sshbuf_len(c->extended)); |
| 1999 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
| 2000 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2001 | errno == EWOULDBLOCK)) |
| 2002 | return 1; |
| 2003 | if (len <= 0) { |
| 2004 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2005 | channel_close_fd(ssh, &c->efd); |
| 2006 | } else { |
| 2007 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2008 | fatal("%s: channel %d: consume: %s", |
| 2009 | __func__, c->self, ssh_err(r)); |
| 2010 | } |
| 2011 | c->local_consumed += len; |
| 2012 | } |
| 2013 | return 1; |
| 2014 | } |
| 2015 | |
| 2016 | static int |
| 2017 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2018 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2019 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2020 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2021 | int r; |
| 2022 | ssize_t len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2023 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2024 | if (!c->detach_close && !FD_ISSET(c->efd, readset)) |
| 2025 | return 1; |
| 2026 | |
| 2027 | len = read(c->efd, buf, sizeof(buf)); |
| 2028 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
| 2029 | if (len < 0 && (errno == EINTR || ((errno == EAGAIN || |
| 2030 | errno == EWOULDBLOCK) && !c->detach_close))) |
| 2031 | return 1; |
| 2032 | if (len <= 0) { |
| 2033 | debug2("channel %d: closing read-efd %d", |
| 2034 | c->self, c->efd); |
| 2035 | channel_close_fd(ssh, &c->efd); |
| 2036 | } else { |
| 2037 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2038 | debug3("channel %d: discard efd", |
| 2039 | c->self); |
| 2040 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2041 | fatal("%s: channel %d: append: %s", |
| 2042 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | return 1; |
| 2046 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2047 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2048 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2049 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2050 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2051 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2052 | if (c->efd == -1) |
| 2053 | return 1; |
| 2054 | |
| 2055 | /** XXX handle drain efd, too */ |
| 2056 | |
| 2057 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2058 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2059 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2060 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2061 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2062 | |
| 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | static int |
| 2067 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2068 | { |
| 2069 | int r; |
| 2070 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2071 | if (c->type == SSH_CHANNEL_OPEN && |
| 2072 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2073 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2074 | c->local_maxpacket*3) || |
| 2075 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2076 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2077 | if (!c->have_remote_id) |
| 2078 | fatal(":%s: channel %d: no remote id", |
| 2079 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2080 | if ((r = sshpkt_start(ssh, |
| 2081 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2082 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2083 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2084 | (r = sshpkt_send(ssh)) != 0) { |
| 2085 | fatal("%s: channel %i: %s", __func__, |
| 2086 | c->self, ssh_err(r)); |
| 2087 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2088 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2089 | c->self, c->local_window, |
| 2090 | c->local_consumed); |
| 2091 | c->local_window += c->local_consumed; |
| 2092 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2093 | } |
| 2094 | return 1; |
| 2095 | } |
| 2096 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2097 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2098 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2099 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2100 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2101 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2102 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2103 | channel_handle_efd(ssh, c, readset, writeset); |
| 2104 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2105 | } |
| 2106 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2107 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2108 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2109 | { |
| 2110 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2111 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2112 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2113 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2114 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2115 | if (sshbuf_len(c->input) < need) { |
| 2116 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2117 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2118 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2119 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2120 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2121 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2122 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2123 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2124 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2125 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2126 | fatal("%s: channel %d: append: %s", |
| 2127 | __func__, c->self, ssh_err(r)); |
| 2128 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2129 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2130 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2134 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2135 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2136 | { |
| 2137 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2138 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2139 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2140 | return; |
| 2141 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2142 | return; |
| 2143 | if (c->mux_pause) |
| 2144 | return; |
| 2145 | |
| 2146 | /* |
| 2147 | * Don't not read past the precise end of packets to |
| 2148 | * avoid disrupting fd passing. |
| 2149 | */ |
| 2150 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2151 | return; |
| 2152 | /* XXX sshbuf_peek_u32 */ |
| 2153 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2154 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2155 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2156 | debug2("channel %d: packet too big %u > %u", |
| 2157 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2158 | chan_rcvd_oclose(ssh, c); |
| 2159 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2160 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2161 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2162 | return; |
| 2163 | if (c->mux_rcb(ssh, c) != 0) { |
| 2164 | debug("channel %d: mux_rcb failed", c->self); |
| 2165 | chan_mark_dead(ssh, c); |
| 2166 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2171 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2172 | fd_set *readset, fd_set *writeset) |
| 2173 | { |
| 2174 | ssize_t len; |
| 2175 | int r; |
| 2176 | |
| 2177 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2178 | sshbuf_len(c->output) == 0) |
| 2179 | return; |
| 2180 | |
| 2181 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
| 2182 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 2183 | return; |
| 2184 | if (len <= 0) { |
| 2185 | chan_mark_dead(ssh, c); |
| 2186 | return; |
| 2187 | } |
| 2188 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2189 | fatal("%s: channel %d: consume: %s", __func__, |
| 2190 | c->self, ssh_err(r)); |
| 2191 | } |
| 2192 | |
| 2193 | static void |
| 2194 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2195 | fd_set *readset, fd_set *writeset) |
| 2196 | { |
| 2197 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2198 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2199 | } |
| 2200 | |
| 2201 | static void |
| 2202 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2203 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2204 | { |
| 2205 | Channel *nc; |
| 2206 | struct sockaddr_storage addr; |
| 2207 | socklen_t addrlen; |
| 2208 | int newsock; |
| 2209 | uid_t euid; |
| 2210 | gid_t egid; |
| 2211 | |
| 2212 | if (!FD_ISSET(c->sock, readset)) |
| 2213 | return; |
| 2214 | |
| 2215 | debug("multiplexing control connection"); |
| 2216 | |
| 2217 | /* |
| 2218 | * Accept connection on control socket |
| 2219 | */ |
| 2220 | memset(&addr, 0, sizeof(addr)); |
| 2221 | addrlen = sizeof(addr); |
| 2222 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2223 | &addrlen)) == -1) { |
| 2224 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2225 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2226 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2227 | return; |
| 2228 | } |
| 2229 | |
| 2230 | if (getpeereid(newsock, &euid, &egid) < 0) { |
| 2231 | error("%s getpeereid failed: %s", __func__, |
| 2232 | strerror(errno)); |
| 2233 | close(newsock); |
| 2234 | return; |
| 2235 | } |
| 2236 | if ((euid != 0) && (getuid() != euid)) { |
| 2237 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2238 | (u_int)euid, (u_int)getuid()); |
| 2239 | close(newsock); |
| 2240 | return; |
| 2241 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2242 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2243 | newsock, newsock, -1, c->local_window_max, |
| 2244 | c->local_maxpacket, 0, "mux-control", 1); |
| 2245 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2246 | 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] | 2247 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2248 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2249 | /* mux state transitions must not elicit protocol messages */ |
| 2250 | nc->flags |= CHAN_LOCAL; |
| 2251 | } |
| 2252 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2253 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2254 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2255 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2256 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2257 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2258 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2259 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2260 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2261 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2262 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2263 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2264 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2265 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2266 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2267 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2268 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2269 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2270 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2271 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2272 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2273 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2274 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2275 | |
| 2276 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2277 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2278 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2279 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2280 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2281 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2282 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2283 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2284 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2285 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2286 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2287 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2288 | |
| 2289 | sc->channel_pre = pre; |
| 2290 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2291 | } |
| 2292 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2293 | /* gc dead channels */ |
| 2294 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2295 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2296 | { |
| 2297 | if (c == NULL) |
| 2298 | return; |
| 2299 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2300 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2301 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2302 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2303 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2304 | /* if we still have a callback */ |
| 2305 | if (c->detach_user != NULL) |
| 2306 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2307 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2308 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2309 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2310 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2311 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2312 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2313 | } |
| 2314 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2315 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2316 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2317 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2318 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2319 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2320 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2321 | struct ssh_channels *sc = ssh->chanctxt; |
| 2322 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2323 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2324 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2325 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2326 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2327 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2328 | if (unpause_secs != NULL) |
| 2329 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2330 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2331 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2332 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2333 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2334 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2335 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2336 | c->delayed = 0; |
| 2337 | else |
| 2338 | continue; |
| 2339 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2340 | if (ftab[c->type] != NULL) { |
| 2341 | /* |
| 2342 | * Run handlers that are not paused. |
| 2343 | */ |
| 2344 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2345 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2346 | else if (unpause_secs != NULL) { |
| 2347 | /* |
| 2348 | * Collect the time that the earliest |
| 2349 | * channel comes off pause. |
| 2350 | */ |
| 2351 | debug3("%s: chan %d: skip for %d more seconds", |
| 2352 | __func__, c->self, |
| 2353 | (int)(c->notbefore - now)); |
| 2354 | if (*unpause_secs == 0 || |
| 2355 | (c->notbefore - now) < *unpause_secs) |
| 2356 | *unpause_secs = c->notbefore - now; |
| 2357 | } |
| 2358 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2359 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2360 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2361 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2362 | debug3("%s: first channel unpauses in %d seconds", |
| 2363 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2364 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2365 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2366 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2367 | * Create sockets before allocating the select bitmasks. |
| 2368 | * This is necessary for things that need to happen after reading |
| 2369 | * the network-input but before channel_prepare_select(). |
| 2370 | */ |
| 2371 | static void |
| 2372 | channel_before_prepare_select(struct ssh *ssh) |
| 2373 | { |
| 2374 | struct ssh_channels *sc = ssh->chanctxt; |
| 2375 | Channel *c; |
| 2376 | u_int i, oalloc; |
| 2377 | |
| 2378 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2379 | c = sc->channels[i]; |
| 2380 | if (c == NULL) |
| 2381 | continue; |
| 2382 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2383 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2388 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2389 | * select bitmasks. |
| 2390 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2391 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2392 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2393 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2394 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2395 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2396 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2397 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2398 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2399 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2400 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2401 | nfdset = howmany(n+1, NFDBITS); |
| 2402 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2403 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2404 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2405 | sz = nfdset * sizeof(fd_mask); |
| 2406 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2407 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2408 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2409 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2410 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2411 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2412 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2413 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2414 | memset(*readsetp, 0, sz); |
| 2415 | memset(*writesetp, 0, sz); |
| 2416 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2417 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2418 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2419 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2420 | } |
| 2421 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2422 | /* |
| 2423 | * After select, perform any appropriate operations for channels which have |
| 2424 | * events pending. |
| 2425 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2426 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2427 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2428 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2429 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2430 | } |
| 2431 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2432 | /* |
| 2433 | * Enqueue data for channels with open or draining c->input. |
| 2434 | */ |
| 2435 | static void |
| 2436 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2437 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2438 | size_t len, plen; |
| 2439 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2440 | int r; |
| 2441 | |
| 2442 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2443 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2444 | /* |
| 2445 | * input-buffer is empty and read-socket shutdown: |
| 2446 | * tell peer, that we will not send more data: |
| 2447 | * send IEOF. |
| 2448 | * hack for extended data: delay EOF if EFD still |
| 2449 | * in use. |
| 2450 | */ |
| 2451 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2452 | debug2("channel %d: " |
| 2453 | "ibuf_empty delayed efd %d/(%zu)", |
| 2454 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2455 | else |
| 2456 | chan_ibuf_empty(ssh, c); |
| 2457 | } |
| 2458 | return; |
| 2459 | } |
| 2460 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2461 | if (!c->have_remote_id) |
| 2462 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2463 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2464 | if (c->datagram) { |
| 2465 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2466 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2467 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2468 | c->self, ssh_err(r)); |
| 2469 | /* |
| 2470 | * XXX this does tail-drop on the datagram queue which is |
| 2471 | * usually suboptimal compared to head-drop. Better to have |
| 2472 | * backpressure at read time? (i.e. read + discard) |
| 2473 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2474 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2475 | debug("channel %d: datagram too big", c->self); |
| 2476 | return; |
| 2477 | } |
| 2478 | /* Enqueue it */ |
| 2479 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2480 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2481 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2482 | (r = sshpkt_send(ssh)) != 0) { |
| 2483 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2484 | c->self, ssh_err(r)); |
| 2485 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2486 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2487 | return; |
| 2488 | } |
| 2489 | |
| 2490 | /* Enqueue packet for buffered data. */ |
| 2491 | if (len > c->remote_window) |
| 2492 | len = c->remote_window; |
| 2493 | if (len > c->remote_maxpacket) |
| 2494 | len = c->remote_maxpacket; |
| 2495 | if (len == 0) |
| 2496 | return; |
| 2497 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2498 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2499 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2500 | (r = sshpkt_send(ssh)) != 0) { |
| 2501 | fatal("%s: channel %i: data: %s", __func__, |
| 2502 | c->self, ssh_err(r)); |
| 2503 | } |
| 2504 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2505 | fatal("%s: channel %i: consume: %s", __func__, |
| 2506 | c->self, ssh_err(r)); |
| 2507 | c->remote_window -= len; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * Enqueue data for channels with open c->extended in read mode. |
| 2512 | */ |
| 2513 | static void |
| 2514 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2515 | { |
| 2516 | size_t len; |
| 2517 | int r; |
| 2518 | |
| 2519 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2520 | return; |
| 2521 | |
| 2522 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2523 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2524 | if (len > c->remote_window) |
| 2525 | len = c->remote_window; |
| 2526 | if (len > c->remote_maxpacket) |
| 2527 | len = c->remote_maxpacket; |
| 2528 | if (len == 0) |
| 2529 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2530 | if (!c->have_remote_id) |
| 2531 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2532 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2533 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2534 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2535 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2536 | (r = sshpkt_send(ssh)) != 0) { |
| 2537 | fatal("%s: channel %i: data: %s", __func__, |
| 2538 | c->self, ssh_err(r)); |
| 2539 | } |
| 2540 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2541 | fatal("%s: channel %i: consume: %s", __func__, |
| 2542 | c->self, ssh_err(r)); |
| 2543 | c->remote_window -= len; |
| 2544 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2545 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2546 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2547 | /* 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] | 2548 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2549 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2550 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2551 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2552 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2553 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2554 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2555 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2556 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2557 | if (c == NULL) |
| 2558 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2559 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2560 | /* |
| 2561 | * We are only interested in channels that can have buffered |
| 2562 | * incoming data. |
| 2563 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2564 | if (c->type != SSH_CHANNEL_OPEN) |
| 2565 | continue; |
| 2566 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2567 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2568 | debug3("channel %d: will not send data after close", |
| 2569 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2570 | continue; |
| 2571 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2572 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2573 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2574 | if (c->istate == CHAN_INPUT_OPEN || |
| 2575 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2576 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2577 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2578 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2579 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2580 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2581 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2582 | } |
| 2583 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2584 | /* -- mux proxy support */ |
| 2585 | |
| 2586 | /* |
| 2587 | * When multiplexing channel messages for mux clients we have to deal |
| 2588 | * with downstream messages from the mux client and upstream messages |
| 2589 | * from the ssh server: |
| 2590 | * 1) Handling downstream messages is straightforward and happens |
| 2591 | * in channel_proxy_downstream(): |
| 2592 | * - We forward all messages (mostly) unmodified to the server. |
| 2593 | * - However, in order to route messages from upstream to the correct |
| 2594 | * downstream client, we have to replace the channel IDs used by the |
| 2595 | * mux clients with a unique channel ID because the mux clients might |
| 2596 | * use conflicting channel IDs. |
| 2597 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2598 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2599 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2600 | * with the newly allocated channel ID. |
| 2601 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
| 2602 | * channels and procesed by channel_proxy_upstream(). The local channel ID |
| 2603 | * is then translated back to the original mux client ID. |
| 2604 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2605 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2606 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2607 | * downstream mux client are removed. |
| 2608 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2609 | * requires more work, because they are not addressed to a specific |
| 2610 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2611 | * out whether the request is addressed to the local client or a |
| 2612 | * specific downstream client based on the listen-address/port. |
| 2613 | * 6) Agent and X11-Forwarding have a similar problem and are currenly |
| 2614 | * not supported as the matching session/channel cannot be identified |
| 2615 | * easily. |
| 2616 | */ |
| 2617 | |
| 2618 | /* |
| 2619 | * receive packets from downstream mux clients: |
| 2620 | * channel callback fired on read from mux client, creates |
| 2621 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2622 | * on channel creation. |
| 2623 | */ |
| 2624 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2625 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2626 | { |
| 2627 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2628 | struct sshbuf *original = NULL, *modified = NULL; |
| 2629 | const u_char *cp; |
| 2630 | char *ctype = NULL, *listen_host = NULL; |
| 2631 | u_char type; |
| 2632 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2633 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2634 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2635 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2636 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2637 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2638 | != 0) { |
| 2639 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2640 | return -1; |
| 2641 | } |
| 2642 | if (have < 2) { |
| 2643 | error("%s: short message", __func__); |
| 2644 | return -1; |
| 2645 | } |
| 2646 | type = cp[1]; |
| 2647 | /* skip padlen + type */ |
| 2648 | cp += 2; |
| 2649 | have -= 2; |
| 2650 | if (ssh_packet_log_type(type)) |
| 2651 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2652 | downstream->self, type); |
| 2653 | |
| 2654 | switch (type) { |
| 2655 | case SSH2_MSG_CHANNEL_OPEN: |
| 2656 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2657 | (modified = sshbuf_new()) == NULL) { |
| 2658 | error("%s: alloc", __func__); |
| 2659 | goto out; |
| 2660 | } |
| 2661 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2662 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2663 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2664 | goto out; |
| 2665 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2666 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2667 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2668 | c->mux_ctx = downstream; /* point to mux client */ |
| 2669 | c->mux_downstream_id = id; /* original downstream id */ |
| 2670 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2671 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2672 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2673 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2674 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2675 | goto out; |
| 2676 | } |
| 2677 | break; |
| 2678 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2679 | /* |
| 2680 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2681 | * need to parse 'remote_id' instead of 'ctype'. |
| 2682 | */ |
| 2683 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2684 | (modified = sshbuf_new()) == NULL) { |
| 2685 | error("%s: alloc", __func__); |
| 2686 | goto out; |
| 2687 | } |
| 2688 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2689 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2690 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2691 | goto out; |
| 2692 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2693 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2694 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2695 | c->mux_ctx = downstream; /* point to mux client */ |
| 2696 | c->mux_downstream_id = id; |
| 2697 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2698 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2699 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2700 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2701 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2702 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2703 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2704 | goto out; |
| 2705 | } |
| 2706 | break; |
| 2707 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2708 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2709 | error("%s: alloc", __func__); |
| 2710 | goto out; |
| 2711 | } |
| 2712 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2713 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2714 | goto out; |
| 2715 | } |
| 2716 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2717 | error("%s: unsupported request %s", __func__, ctype); |
| 2718 | goto out; |
| 2719 | } |
| 2720 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2721 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2722 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2723 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2724 | goto out; |
| 2725 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2726 | if (listen_port > 65535) { |
| 2727 | error("%s: tcpip-forward for %s: bad port %u", |
| 2728 | __func__, listen_host, listen_port); |
| 2729 | goto out; |
| 2730 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2731 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2732 | fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2733 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2734 | listen_host = NULL; |
| 2735 | break; |
| 2736 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2737 | if (have < 4) |
| 2738 | break; |
| 2739 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2740 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2741 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2742 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2743 | else |
| 2744 | c->flags |= CHAN_CLOSE_SENT; |
| 2745 | } |
| 2746 | break; |
| 2747 | } |
| 2748 | if (modified) { |
| 2749 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2750 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2751 | (r = sshpkt_send(ssh)) != 0) { |
| 2752 | error("%s: send %s", __func__, ssh_err(r)); |
| 2753 | goto out; |
| 2754 | } |
| 2755 | } else { |
| 2756 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2757 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2758 | (r = sshpkt_send(ssh)) != 0) { |
| 2759 | error("%s: send %s", __func__, ssh_err(r)); |
| 2760 | goto out; |
| 2761 | } |
| 2762 | } |
| 2763 | ret = 0; |
| 2764 | out: |
| 2765 | free(ctype); |
| 2766 | free(listen_host); |
| 2767 | sshbuf_free(original); |
| 2768 | sshbuf_free(modified); |
| 2769 | return ret; |
| 2770 | } |
| 2771 | |
| 2772 | /* |
| 2773 | * receive packets from upstream server and de-multiplex packets |
| 2774 | * to correct downstream: |
| 2775 | * implemented as a helper for channel input handlers, |
| 2776 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2777 | */ |
| 2778 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2779 | 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] | 2780 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2781 | struct sshbuf *b = NULL; |
| 2782 | Channel *downstream; |
| 2783 | const u_char *cp = NULL; |
| 2784 | size_t len; |
| 2785 | int r; |
| 2786 | |
| 2787 | /* |
| 2788 | * When receiving packets from the peer we need to check whether we |
| 2789 | * need to forward the packets to the mux client. In this case we |
| 2790 | * restore the orignal channel id and keep track of CLOSE messages, |
| 2791 | * so we can cleanup the channel. |
| 2792 | */ |
| 2793 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2794 | return 0; |
| 2795 | if ((downstream = c->mux_ctx) == NULL) |
| 2796 | return 0; |
| 2797 | switch (type) { |
| 2798 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2799 | case SSH2_MSG_CHANNEL_DATA: |
| 2800 | case SSH2_MSG_CHANNEL_EOF: |
| 2801 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2802 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2803 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2804 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2805 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2806 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2807 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2808 | break; |
| 2809 | default: |
| 2810 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2811 | c->self, type); |
| 2812 | return 0; |
| 2813 | } |
| 2814 | if ((b = sshbuf_new()) == NULL) { |
| 2815 | error("%s: alloc reply", __func__); |
| 2816 | goto out; |
| 2817 | } |
| 2818 | /* get remaining payload (after id) */ |
| 2819 | cp = sshpkt_ptr(ssh, &len); |
| 2820 | if (cp == NULL) { |
| 2821 | error("%s: no packet", __func__); |
| 2822 | goto out; |
| 2823 | } |
| 2824 | /* translate id and send to muxclient */ |
| 2825 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2826 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2827 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2828 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2829 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2830 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2831 | goto out; |
| 2832 | } |
| 2833 | /* sshbuf_dump(b, stderr); */ |
| 2834 | if (ssh_packet_log_type(type)) |
| 2835 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2836 | type); |
| 2837 | out: |
| 2838 | /* update state */ |
| 2839 | switch (type) { |
| 2840 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2841 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2842 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2843 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2844 | c->have_remote_id = 1; |
| 2845 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2846 | break; |
| 2847 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2848 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2849 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2850 | else |
| 2851 | c->flags |= CHAN_CLOSE_RCVD; |
| 2852 | break; |
| 2853 | } |
| 2854 | sshbuf_free(b); |
| 2855 | return 1; |
| 2856 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2857 | |
| 2858 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2859 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2860 | /* Parse a channel ID from the current packet */ |
| 2861 | static int |
| 2862 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2863 | { |
| 2864 | u_int32_t id; |
| 2865 | int r; |
| 2866 | |
| 2867 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2868 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2869 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2870 | } |
| 2871 | if (id > INT_MAX) { |
| 2872 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2873 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2874 | } |
| 2875 | return (int)id; |
| 2876 | } |
| 2877 | |
| 2878 | /* Lookup a channel from an ID in the current packet */ |
| 2879 | static Channel * |
| 2880 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2881 | { |
| 2882 | int id = channel_parse_id(ssh, where, what); |
| 2883 | Channel *c; |
| 2884 | |
| 2885 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2886 | ssh_packet_disconnect(ssh, |
| 2887 | "%s packet referred to nonexistent channel %d", what, id); |
| 2888 | } |
| 2889 | return c; |
| 2890 | } |
| 2891 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2892 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2893 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2894 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2895 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2896 | size_t data_len, win_len; |
| 2897 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 2898 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2899 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2900 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2901 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2902 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2903 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2904 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2905 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 2906 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2907 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2908 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2909 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2910 | /* Get the data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2911 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) |
| 2912 | fatal("%s: channel %d: get data: %s", __func__, |
| 2913 | c->self, ssh_err(r)); |
| 2914 | ssh_packet_check_eom(ssh); |
| 2915 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2916 | win_len = data_len; |
| 2917 | if (c->datagram) |
| 2918 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2919 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2920 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2921 | * The sending side reduces its window as it sends data, so we |
| 2922 | * must 'fake' consumption of the data in order to ensure that window |
| 2923 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2924 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2925 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 2926 | c->local_window -= win_len; |
| 2927 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2928 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2929 | } |
| 2930 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2931 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2932 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2933 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2934 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2935 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2936 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2937 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2938 | c->self, win_len, c->local_window); |
| 2939 | return 0; |
| 2940 | } |
| 2941 | c->local_window -= win_len; |
| 2942 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2943 | if (c->datagram) { |
| 2944 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 2945 | fatal("%s: channel %d: append datagram: %s", |
| 2946 | __func__, c->self, ssh_err(r)); |
| 2947 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 2948 | fatal("%s: channel %d: append data: %s", |
| 2949 | __func__, c->self, ssh_err(r)); |
| 2950 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2951 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2952 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2953 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2954 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2955 | 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] | 2956 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2957 | const u_char *data; |
| 2958 | size_t data_len; |
| 2959 | u_int32_t tcode; |
| 2960 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 2961 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2962 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2963 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2964 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2965 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2966 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2967 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2968 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2969 | if (c->flags & CHAN_EOF_RCVD) { |
| 2970 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2971 | debug("channel %d: accepting ext data after eof", |
| 2972 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2973 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2974 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 2975 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2976 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2977 | |
| 2978 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 2979 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 2980 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 2981 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2982 | if (c->efd == -1 || |
| 2983 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 2984 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2985 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2986 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2987 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2988 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) { |
| 2989 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 2990 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 2991 | } |
| 2992 | ssh_packet_check_eom(ssh); |
| 2993 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2994 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2995 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2996 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2997 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2998 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2999 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3000 | /* XXX sshpkt_getb? */ |
| 3001 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3002 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3003 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3004 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3005 | } |
| 3006 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3007 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3008 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3009 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3010 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3011 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3012 | ssh_packet_check_eom(ssh); |
| 3013 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3014 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3015 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3016 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3017 | |
| 3018 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3019 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3020 | debug("channel %d: FORCE input drain", c->self); |
| 3021 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3022 | if (sshbuf_len(c->input) == 0) |
| 3023 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3024 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3025 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3026 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3027 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3028 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3029 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3030 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3031 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3032 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3033 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3034 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3035 | ssh_packet_check_eom(ssh); |
| 3036 | chan_rcvd_oclose(ssh, c); |
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 | } |
| 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_open_confirmation(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3042 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3043 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3044 | u_int32_t remote_window, remote_maxpacket; |
| 3045 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3046 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3047 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3048 | return 0; |
| 3049 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3050 | packet_disconnect("Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3051 | "non-opening channel %d.", c->self); |
| 3052 | /* |
| 3053 | * Record the remote channel number and mark that the channel |
| 3054 | * is now open. |
| 3055 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3056 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3057 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3058 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) { |
| 3059 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
| 3060 | packet_disconnect("Invalid open confirmation message"); |
| 3061 | } |
| 3062 | ssh_packet_check_eom(ssh); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3063 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3064 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3065 | c->remote_window = remote_window; |
| 3066 | c->remote_maxpacket = remote_maxpacket; |
| 3067 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3068 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3069 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3070 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3071 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3072 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3073 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3074 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3075 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3076 | } |
| 3077 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3078 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3079 | reason2txt(int reason) |
| 3080 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3081 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3082 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3083 | return "administratively prohibited"; |
| 3084 | case SSH2_OPEN_CONNECT_FAILED: |
| 3085 | return "connect failed"; |
| 3086 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3087 | return "unknown channel type"; |
| 3088 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3089 | return "resource shortage"; |
| 3090 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3091 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3094 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3095 | 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] | 3096 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3097 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3098 | u_int32_t reason; |
| 3099 | char *msg = NULL; |
| 3100 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3101 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3102 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3103 | return 0; |
| 3104 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3105 | packet_disconnect("Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3106 | "non-opening channel %d.", c->self); |
| 3107 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3108 | error("%s: reason: %s", __func__, ssh_err(r)); |
| 3109 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3110 | } |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3111 | /* skip language */ |
| 3112 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
| 3113 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) { |
| 3114 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
| 3115 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3116 | } |
| 3117 | ssh_packet_check_eom(ssh); |
| 3118 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3119 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3120 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3121 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3122 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3123 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3124 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3125 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3126 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3127 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3128 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3129 | } |
| 3130 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3131 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3132 | channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3133 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3134 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3135 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3136 | u_int32_t adjust; |
| 3137 | u_int new_rwin; |
| 3138 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3139 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3140 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3141 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3142 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3143 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3144 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3145 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3146 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3147 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) { |
| 3148 | error("%s: adjust: %s", __func__, ssh_err(r)); |
| 3149 | packet_disconnect("Invalid window adjust message"); |
| 3150 | } |
| 3151 | ssh_packet_check_eom(ssh); |
| 3152 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3153 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3154 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3155 | c->self, adjust, c->remote_window); |
| 3156 | } |
| 3157 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3158 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3159 | } |
| 3160 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3161 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3162 | 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] | 3163 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3164 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3165 | Channel *c; |
| 3166 | struct channel_confirm *cc; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3167 | |
| 3168 | /* Reset keepalive timeout */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 3169 | packet_set_alive_timeouts(0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3170 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3171 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3172 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3173 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3174 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3175 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3176 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3177 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3178 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3179 | ssh_packet_check_eom(ssh); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3180 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3181 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3182 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3183 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3184 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3185 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3186 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3187 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3188 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3189 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3190 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3191 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3192 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3193 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3194 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3197 | |
| 3198 | /* |
| 3199 | * Determine whether or not a port forward listens to loopback, the |
| 3200 | * specified address or wildcard. On the client, a specified bind |
| 3201 | * address will always override gateway_ports. On the server, a |
| 3202 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3203 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3204 | * will bind to whatever address the client asked for. |
| 3205 | * |
| 3206 | * Special-case listen_addrs are: |
| 3207 | * |
| 3208 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3209 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3210 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3211 | * "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] | 3212 | */ |
| 3213 | static const char * |
| 3214 | channel_fwd_bind_addr(const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3215 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3216 | { |
| 3217 | const char *addr = NULL; |
| 3218 | int wildcard = 0; |
| 3219 | |
| 3220 | if (listen_addr == NULL) { |
| 3221 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3222 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3223 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3224 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3225 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3226 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3227 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3228 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3229 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3230 | /* |
| 3231 | * Notify client if they requested a specific listen |
| 3232 | * address and it was overridden. |
| 3233 | */ |
| 3234 | if (*listen_addr != '\0' && |
| 3235 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3236 | strcmp(listen_addr, "*") != 0) { |
| 3237 | packet_send_debug("Forwarding listen address " |
| 3238 | "\"%s\" overridden by server " |
| 3239 | "GatewayPorts", listen_addr); |
| 3240 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3241 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3242 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3243 | strcmp(listen_addr, "::1") == 0) { |
| 3244 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3245 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3246 | } |
| 3247 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3248 | strcmp(listen_addr, "::1") == 0) { |
| 3249 | /* |
| 3250 | * If a specific IPv4/IPv6 localhost address has been |
| 3251 | * requested then accept it even if gateway_ports is in |
| 3252 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3253 | */ |
| 3254 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3255 | } |
| 3256 | if (wildcardp != NULL) |
| 3257 | *wildcardp = wildcard; |
| 3258 | return addr; |
| 3259 | } |
| 3260 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3261 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3262 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3263 | struct Forward *fwd, int *allocated_listen_port, |
| 3264 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3265 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3266 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3267 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3268 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3269 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3270 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3271 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3272 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3273 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3274 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3275 | if (is_client && fwd->connect_path != NULL) { |
| 3276 | host = fwd->connect_path; |
| 3277 | } else { |
| 3278 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3279 | fwd->listen_host : fwd->connect_host; |
| 3280 | if (host == NULL) { |
| 3281 | error("No forward host name."); |
| 3282 | return 0; |
| 3283 | } |
| 3284 | if (strlen(host) >= NI_MAXHOST) { |
| 3285 | error("Forward host name too long."); |
| 3286 | return 0; |
| 3287 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3288 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3289 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3290 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3291 | addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard, |
| 3292 | is_client, fwd_opts); |
| 3293 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3294 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3295 | |
| 3296 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3297 | * getaddrinfo returns a loopback address if the hostname is |
| 3298 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3299 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3300 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3301 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3302 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3303 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3304 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3305 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3306 | if (addr == NULL) { |
| 3307 | /* This really shouldn't happen */ |
| 3308 | packet_disconnect("getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3309 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3310 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3311 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3312 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3313 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3314 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3315 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3316 | if (allocated_listen_port != NULL) |
| 3317 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3318 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3319 | switch (ai->ai_family) { |
| 3320 | case AF_INET: |
| 3321 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3322 | sin_port; |
| 3323 | break; |
| 3324 | case AF_INET6: |
| 3325 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3326 | sin6_port; |
| 3327 | break; |
| 3328 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3329 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3330 | } |
| 3331 | /* |
| 3332 | * If allocating a port for -R forwards, then use the |
| 3333 | * same port for all address families. |
| 3334 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3335 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3336 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3337 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3338 | *lport_p = htons(*allocated_listen_port); |
| 3339 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3340 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3341 | strport, sizeof(strport), |
| 3342 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3343 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3344 | continue; |
| 3345 | } |
| 3346 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3347 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3348 | if (sock < 0) { |
| 3349 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3350 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3351 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3352 | continue; |
| 3353 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3354 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3355 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3356 | if (ai->ai_family == AF_INET6) |
| 3357 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3358 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3359 | debug("Local forwarding listening on %s port %s.", |
| 3360 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3361 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3362 | /* Bind the socket to the address. */ |
| 3363 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3364 | /* |
| 3365 | * address can be in if use ipv6 address is |
| 3366 | * already bound |
| 3367 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3368 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3369 | error("bind [%s]:%s: %.100s", |
| 3370 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3371 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3372 | verbose("bind [%s]:%s: %.100s", |
| 3373 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3374 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3375 | close(sock); |
| 3376 | continue; |
| 3377 | } |
| 3378 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3379 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3380 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3381 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3382 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3383 | close(sock); |
| 3384 | continue; |
| 3385 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3386 | |
| 3387 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3388 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3389 | * record what we got. |
| 3390 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3391 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3392 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3393 | allocated_listen_port != NULL && |
| 3394 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3395 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3396 | debug("Allocated listen port %d", |
| 3397 | *allocated_listen_port); |
| 3398 | } |
| 3399 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3400 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3401 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3402 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3403 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3404 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3405 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3406 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3407 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3408 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3409 | c->listening_port = *allocated_listen_port; |
| 3410 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3411 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3412 | success = 1; |
| 3413 | } |
| 3414 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3415 | error("%s: cannot listen to port: %d", __func__, |
| 3416 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3417 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3418 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3419 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3420 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3421 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3422 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3423 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3424 | { |
| 3425 | struct sockaddr_un sunaddr; |
| 3426 | const char *path; |
| 3427 | Channel *c; |
| 3428 | int port, sock; |
| 3429 | mode_t omask; |
| 3430 | |
| 3431 | switch (type) { |
| 3432 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3433 | if (fwd->connect_path != NULL) { |
| 3434 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3435 | error("Local connecting path too long: %s", |
| 3436 | fwd->connect_path); |
| 3437 | return 0; |
| 3438 | } |
| 3439 | path = fwd->connect_path; |
| 3440 | port = PORT_STREAMLOCAL; |
| 3441 | } else { |
| 3442 | if (fwd->connect_host == NULL) { |
| 3443 | error("No forward host name."); |
| 3444 | return 0; |
| 3445 | } |
| 3446 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3447 | error("Forward host name too long."); |
| 3448 | return 0; |
| 3449 | } |
| 3450 | path = fwd->connect_host; |
| 3451 | port = fwd->connect_port; |
| 3452 | } |
| 3453 | break; |
| 3454 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3455 | path = fwd->listen_path; |
| 3456 | port = PORT_STREAMLOCAL; |
| 3457 | break; |
| 3458 | default: |
| 3459 | error("%s: unexpected channel type %d", __func__, type); |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
| 3463 | if (fwd->listen_path == NULL) { |
| 3464 | error("No forward path name."); |
| 3465 | return 0; |
| 3466 | } |
| 3467 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3468 | error("Local listening path too long: %s", fwd->listen_path); |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
| 3472 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3473 | |
| 3474 | /* Start a Unix domain listener. */ |
| 3475 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3476 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3477 | fwd_opts->streamlocal_bind_unlink); |
| 3478 | umask(omask); |
| 3479 | if (sock < 0) |
| 3480 | return 0; |
| 3481 | |
| 3482 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3483 | |
| 3484 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3485 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3486 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3487 | 0, "unix listener", 1); |
| 3488 | c->path = xstrdup(path); |
| 3489 | c->host_port = port; |
| 3490 | c->listening_port = PORT_STREAMLOCAL; |
| 3491 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3492 | return 1; |
| 3493 | } |
| 3494 | |
| 3495 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3496 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3497 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3498 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3499 | u_int i; |
| 3500 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3501 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3502 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3503 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3504 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3505 | continue; |
| 3506 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3507 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3508 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3509 | found = 1; |
| 3510 | } |
| 3511 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3512 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3513 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3514 | } |
| 3515 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3516 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3517 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3518 | { |
| 3519 | u_int i; |
| 3520 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3521 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3522 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3523 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3524 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3525 | continue; |
| 3526 | if (c->path == NULL) |
| 3527 | continue; |
| 3528 | if (strcmp(c->path, path) == 0) { |
| 3529 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3530 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3531 | found = 1; |
| 3532 | } |
| 3533 | } |
| 3534 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3535 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3539 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3540 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3541 | if (fwd->listen_path != NULL) { |
| 3542 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3543 | fwd->listen_path); |
| 3544 | } else { |
| 3545 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3546 | fwd->listen_host, fwd->listen_port); |
| 3547 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3551 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3552 | const char *lhost, u_short lport, int cport, |
| 3553 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3554 | { |
| 3555 | u_int i; |
| 3556 | int found = 0; |
| 3557 | const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3558 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3559 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3560 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3561 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3562 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3563 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3564 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3565 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3566 | /* skip dynamic forwardings */ |
| 3567 | if (c->host_port == 0) |
| 3568 | continue; |
| 3569 | } else { |
| 3570 | if (c->host_port != cport) |
| 3571 | continue; |
| 3572 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3573 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3574 | (c->listening_addr != NULL && addr == NULL)) |
| 3575 | continue; |
| 3576 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3577 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3578 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3579 | found = 1; |
| 3580 | } |
| 3581 | } |
| 3582 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3583 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3584 | } |
| 3585 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3586 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3587 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3588 | { |
| 3589 | u_int i; |
| 3590 | int found = 0; |
| 3591 | |
| 3592 | if (path == NULL) { |
| 3593 | error("%s: no path specified.", __func__); |
| 3594 | return 0; |
| 3595 | } |
| 3596 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3597 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3598 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3599 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3600 | continue; |
| 3601 | if (c->listening_addr == NULL) |
| 3602 | continue; |
| 3603 | if (strcmp(c->listening_addr, path) == 0) { |
| 3604 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3605 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3606 | found = 1; |
| 3607 | } |
| 3608 | } |
| 3609 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3610 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3614 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3615 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3616 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3617 | if (fwd->listen_path != NULL) { |
| 3618 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3619 | fwd->listen_path); |
| 3620 | } else { |
| 3621 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3622 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3623 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3624 | } |
| 3625 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3626 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3627 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3628 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3629 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3630 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3631 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3632 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3633 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3634 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3635 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3636 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3637 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | /* protocol v2 remote port fwd, used by sshd */ |
| 3641 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3642 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3643 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3644 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3645 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3646 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3647 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3648 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3649 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3650 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3651 | fwd_opts); |
| 3652 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3653 | } |
| 3654 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3655 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3656 | * Translate the requested rfwd listen host to something usable for |
| 3657 | * this server. |
| 3658 | */ |
| 3659 | static const char * |
| 3660 | channel_rfwd_bind_host(const char *listen_host) |
| 3661 | { |
| 3662 | if (listen_host == NULL) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3663 | return "localhost"; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3664 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 3665 | return ""; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3666 | } else |
| 3667 | return listen_host; |
| 3668 | } |
| 3669 | |
| 3670 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3671 | * Initiate forwarding of connections to port "port" on remote host through |
| 3672 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3673 | * Returns handle (index) for updating the dynamic listen port with |
| 3674 | * channel_update_permitted_opens(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3675 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3676 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3677 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3678 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3679 | int r, success = 0, idx = -1; |
| 3680 | char *host_to_connect, *listen_host, *listen_path; |
| 3681 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3682 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3683 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3684 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3685 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3686 | (r = sshpkt_put_cstring(ssh, |
| 3687 | "streamlocal-forward@openssh.com")) != 0 || |
| 3688 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3689 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3690 | (r = sshpkt_send(ssh)) != 0 || |
| 3691 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3692 | fatal("%s: request streamlocal: %s", |
| 3693 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3694 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3695 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3696 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3697 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3698 | (r = sshpkt_put_cstring(ssh, |
| 3699 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3700 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3701 | (r = sshpkt_send(ssh)) != 0 || |
| 3702 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3703 | fatal("%s: request tcpip-forward: %s", |
| 3704 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3705 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3706 | /* Assume that server accepts the request */ |
| 3707 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3708 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3709 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3710 | host_to_connect = listen_host = listen_path = NULL; |
| 3711 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3712 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3713 | host_to_connect = xstrdup(fwd->connect_path); |
| 3714 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3715 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3716 | host_to_connect = xstrdup(fwd->connect_host); |
| 3717 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3718 | } |
| 3719 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3720 | listen_path = xstrdup(fwd->listen_path); |
| 3721 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3722 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3723 | if (fwd->listen_host != NULL) |
| 3724 | listen_host = xstrdup(fwd->listen_host); |
| 3725 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3726 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3727 | idx = fwd_perm_list_add(ssh, FWDPERM_USER, |
| 3728 | host_to_connect, port_to_connect, |
| 3729 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3730 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3731 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3732 | } |
| 3733 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3734 | static int |
| 3735 | open_match(ForwardPermission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3736 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3737 | { |
| 3738 | if (allowed_open->host_to_connect == NULL) |
| 3739 | return 0; |
| 3740 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3741 | allowed_open->port_to_connect != requestedport) |
| 3742 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3743 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3744 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3745 | return 0; |
| 3746 | return 1; |
| 3747 | } |
| 3748 | |
| 3749 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3750 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3751 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3752 | * need to translate between the configured-host (listen_host) |
| 3753 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3754 | */ |
| 3755 | static int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3756 | open_listen_match_tcpip(ForwardPermission *allowed_open, |
| 3757 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3758 | { |
| 3759 | const char *allowed_host; |
| 3760 | |
| 3761 | if (allowed_open->host_to_connect == NULL) |
| 3762 | return 0; |
| 3763 | if (allowed_open->listen_port != requestedport) |
| 3764 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3765 | if (!translate && allowed_open->listen_host == NULL && |
| 3766 | requestedhost == NULL) |
| 3767 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3768 | allowed_host = translate ? |
| 3769 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3770 | allowed_open->listen_host; |
| 3771 | if (allowed_host == NULL || |
| 3772 | strcmp(allowed_host, requestedhost) != 0) |
| 3773 | return 0; |
| 3774 | return 1; |
| 3775 | } |
| 3776 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3777 | static int |
| 3778 | open_listen_match_streamlocal(ForwardPermission *allowed_open, |
| 3779 | const char *requestedpath) |
| 3780 | { |
| 3781 | if (allowed_open->host_to_connect == NULL) |
| 3782 | return 0; |
| 3783 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3784 | return 0; |
| 3785 | if (allowed_open->listen_path == NULL || |
| 3786 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3787 | return 0; |
| 3788 | return 1; |
| 3789 | } |
| 3790 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3791 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3792 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3793 | * local side. |
| 3794 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3795 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3796 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3797 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3798 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3799 | struct ssh_channels *sc = ssh->chanctxt; |
| 3800 | int r; |
| 3801 | u_int i; |
| 3802 | ForwardPermission *fp; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3803 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3804 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 3805 | fp = &sc->permitted_opens[i]; |
| 3806 | if (open_listen_match_tcpip(fp, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3807 | break; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3808 | fp = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3809 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3810 | if (fp == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3811 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3812 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3813 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3814 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3815 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 3816 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3817 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 3818 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 3819 | (r = sshpkt_send(ssh)) != 0) |
| 3820 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3821 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3822 | fwd_perm_clear(fp); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3823 | |
| 3824 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3828 | * Request cancellation of remote forwarding of Unix domain socket |
| 3829 | * path from local side. |
| 3830 | */ |
| 3831 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3832 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3833 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3834 | struct ssh_channels *sc = ssh->chanctxt; |
| 3835 | int r; |
| 3836 | u_int i; |
| 3837 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3838 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3839 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 3840 | fp = &sc->permitted_opens[i]; |
| 3841 | if (open_listen_match_streamlocal(fp, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3842 | break; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3843 | fp = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3844 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3845 | if (fp == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3846 | debug("%s: requested forward not found", __func__); |
| 3847 | return -1; |
| 3848 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3849 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3850 | (r = sshpkt_put_cstring(ssh, |
| 3851 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 3852 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3853 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 3854 | (r = sshpkt_send(ssh)) != 0) |
| 3855 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3856 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3857 | fwd_perm_clear(fp); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3858 | |
| 3859 | return 0; |
| 3860 | } |
| 3861 | |
| 3862 | /* |
| 3863 | * Request cancellation of remote forwarding of a connection from local side. |
| 3864 | */ |
| 3865 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3866 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3867 | { |
| 3868 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3869 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 3870 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3871 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3872 | return channel_request_rforward_cancel_tcpip(ssh, |
| 3873 | fwd->listen_host, |
| 3874 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | /* |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3879 | * Permits opening to any host/port if permitted_opens[] is empty. This is |
| 3880 | * usually called by the server, because the user could connect to any port |
| 3881 | * anyway, and the server has no way to know but to trust the client anyway. |
| 3882 | */ |
| 3883 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3884 | channel_permit_all_opens(struct ssh *ssh) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3885 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3886 | if (ssh->chanctxt->num_permitted_opens == 0) |
| 3887 | ssh->chanctxt->all_opens_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 3890 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3891 | channel_add_permitted_opens(struct ssh *ssh, char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3892 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3893 | struct ssh_channels *sc = ssh->chanctxt; |
| 3894 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3895 | debug("allow port forwarding to host %s port %d", host, port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3896 | fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL); |
| 3897 | sc->all_opens_permitted = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3900 | /* |
| 3901 | * Update the listen port for a dynamic remote forward, after |
| 3902 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 3903 | * passed then they entry will be invalidated. |
| 3904 | */ |
| 3905 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3906 | channel_update_permitted_opens(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3907 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3908 | struct ssh_channels *sc = ssh->chanctxt; |
| 3909 | |
| 3910 | if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) { |
| 3911 | debug("%s: index out of range: %d num_permitted_opens %d", |
| 3912 | __func__, idx, sc->num_permitted_opens); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3913 | return; |
| 3914 | } |
| 3915 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 3916 | newport > 0 ? "Updating" : "Removing", |
| 3917 | newport, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3918 | sc->permitted_opens[idx].host_to_connect, |
| 3919 | sc->permitted_opens[idx].port_to_connect); |
| 3920 | if (newport <= 0) |
| 3921 | fwd_perm_clear(&sc->permitted_opens[idx]); |
| 3922 | else { |
| 3923 | sc->permitted_opens[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3924 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3928 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3929 | channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port) |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3930 | { |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3931 | debug("config allows port forwarding to host %s port %d", host, port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3932 | return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port, |
| 3933 | NULL, NULL, 0, NULL); |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3937 | channel_disable_adm_local_opens(struct ssh *ssh) |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3938 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3939 | channel_clear_adm_permitted_opens(ssh); |
| 3940 | fwd_perm_list_add(ssh, FWDPERM_ADMIN, NULL, 0, NULL, NULL, 0, NULL); |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3944 | channel_clear_permitted_opens(struct ssh *ssh) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3945 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3946 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3947 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3948 | sc->permitted_opens = xrecallocarray(sc->permitted_opens, |
| 3949 | sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens)); |
| 3950 | sc->num_permitted_opens = 0; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3951 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3952 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3953 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3954 | channel_clear_adm_permitted_opens(struct ssh *ssh) |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3955 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3956 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3957 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3958 | sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens, |
| 3959 | sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens)); |
| 3960 | sc->num_adm_permitted_opens = 0; |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3961 | } |
| 3962 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 3963 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 3964 | int |
| 3965 | permitopen_port(const char *p) |
| 3966 | { |
| 3967 | int port; |
| 3968 | |
| 3969 | if (strcmp(p, "*") == 0) |
| 3970 | return FWD_PERMIT_ANY_PORT; |
| 3971 | if ((port = a2port(p)) > 0) |
| 3972 | return port; |
| 3973 | return -1; |
| 3974 | } |
| 3975 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3976 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3977 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3978 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3979 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3980 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3981 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3982 | char ntop[NI_MAXHOST]; |
| 3983 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3984 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3985 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3986 | switch (cctx->ai->ai_family) { |
| 3987 | case AF_UNIX: |
| 3988 | /* unix:pathname instead of host:port */ |
| 3989 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 3990 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 3991 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 3992 | break; |
| 3993 | case AF_INET: |
| 3994 | case AF_INET6: |
| 3995 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 3996 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 3997 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 3998 | error("connect_next: getnameinfo failed"); |
| 3999 | continue; |
| 4000 | } |
| 4001 | break; |
| 4002 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4003 | continue; |
| 4004 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4005 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4006 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4007 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4008 | error("socket: %.100s", strerror(errno)); |
| 4009 | else |
| 4010 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4011 | continue; |
| 4012 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4013 | if (set_nonblock(sock) == -1) |
| 4014 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4015 | if (connect(sock, cctx->ai->ai_addr, |
| 4016 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4017 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4018 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4019 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4020 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4021 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4022 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4023 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4024 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4025 | if (cctx->ai->ai_family != AF_UNIX) |
| 4026 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4027 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4028 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4029 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4030 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4031 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4032 | return -1; |
| 4033 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4034 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4035 | static void |
| 4036 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4037 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4038 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4039 | if (cctx->aitop) { |
| 4040 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4041 | free(cctx->aitop); |
| 4042 | else |
| 4043 | freeaddrinfo(cctx->aitop); |
| 4044 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4045 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4046 | } |
| 4047 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4048 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4049 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4050 | * passing back the failure reason if appropriate. |
| 4051 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4052 | static int |
| 4053 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4054 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4055 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4056 | { |
| 4057 | struct addrinfo hints; |
| 4058 | int gaierr; |
| 4059 | int sock = -1; |
| 4060 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4061 | |
| 4062 | if (port == PORT_STREAMLOCAL) { |
| 4063 | struct sockaddr_un *sunaddr; |
| 4064 | struct addrinfo *ai; |
| 4065 | |
| 4066 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4067 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4068 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | /* |
| 4072 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4073 | * channel_connect_ctx_free() must check ai_family |
| 4074 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4075 | */ |
| 4076 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4077 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4078 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4079 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4080 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4081 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4082 | ai->ai_protocol = PF_UNSPEC; |
| 4083 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4084 | sunaddr->sun_family = AF_UNIX; |
| 4085 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4086 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4087 | } else { |
| 4088 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4089 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4090 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4091 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4092 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4093 | != 0) { |
| 4094 | if (errmsg != NULL) |
| 4095 | *errmsg = ssh_gai_strerror(gaierr); |
| 4096 | if (reason != NULL) |
| 4097 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4098 | error("connect_to %.100s: unknown host (%s)", name, |
| 4099 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4100 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4101 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4102 | } |
| 4103 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4104 | cctx->host = xstrdup(name); |
| 4105 | cctx->port = port; |
| 4106 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4107 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4108 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4109 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4110 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4111 | return -1; |
| 4112 | } |
| 4113 | |
| 4114 | return sock; |
| 4115 | } |
| 4116 | |
| 4117 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4118 | static Channel * |
| 4119 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4120 | char *ctype, char *rname) |
| 4121 | { |
| 4122 | struct channel_connect cctx; |
| 4123 | Channel *c; |
| 4124 | int sock; |
| 4125 | |
| 4126 | memset(&cctx, 0, sizeof(cctx)); |
| 4127 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4128 | &cctx, NULL, NULL); |
| 4129 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4130 | channel_connect_ctx_free(&cctx); |
| 4131 | return NULL; |
| 4132 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4133 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4134 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4135 | c->host_port = port; |
| 4136 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4137 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4138 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4139 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4142 | /* |
| 4143 | * returns either the newly connected channel or the downstream channel |
| 4144 | * that needs to deal with this connection. |
| 4145 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4146 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4147 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4148 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4149 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4150 | struct ssh_channels *sc = ssh->chanctxt; |
| 4151 | u_int i; |
| 4152 | ForwardPermission *fp; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4153 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4154 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4155 | fp = &sc->permitted_opens[i]; |
| 4156 | if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) { |
| 4157 | if (fp->downstream) |
| 4158 | return fp->downstream; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4159 | if (fp->port_to_connect == 0) |
| 4160 | return rdynamic_connect_prepare(ssh, |
| 4161 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4162 | return connect_to(ssh, |
| 4163 | fp->host_to_connect, fp->port_to_connect, |
| 4164 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4165 | } |
| 4166 | } |
| 4167 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4168 | listen_port); |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4172 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4173 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4174 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4175 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4176 | struct ssh_channels *sc = ssh->chanctxt; |
| 4177 | u_int i; |
| 4178 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4179 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4180 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4181 | fp = &sc->permitted_opens[i]; |
| 4182 | if (open_listen_match_streamlocal(fp, path)) { |
| 4183 | return connect_to(ssh, |
| 4184 | fp->host_to_connect, fp->port_to_connect, |
| 4185 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4186 | } |
| 4187 | } |
| 4188 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4189 | path); |
| 4190 | return NULL; |
| 4191 | } |
| 4192 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4193 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4194 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4195 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4196 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4197 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4198 | struct ssh_channels *sc = ssh->chanctxt; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4199 | struct channel_connect cctx; |
| 4200 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4201 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4202 | int sock; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4203 | ForwardPermission *fp; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4204 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4205 | permit = sc->all_opens_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4206 | if (!permit) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4207 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4208 | fp = &sc->permitted_opens[i]; |
| 4209 | if (open_match(fp, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4210 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4211 | break; |
| 4212 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4213 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4214 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4215 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4216 | if (sc->num_adm_permitted_opens > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4217 | permit_adm = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4218 | for (i = 0; i < sc->num_adm_permitted_opens; i++) { |
| 4219 | fp = &sc->permitted_adm_opens[i]; |
| 4220 | if (open_match(fp, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4221 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4222 | break; |
| 4223 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4224 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 4228 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4229 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4230 | if (reason != NULL) |
| 4231 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4232 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4233 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4234 | |
| 4235 | memset(&cctx, 0, sizeof(cctx)); |
| 4236 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4237 | &cctx, reason, errmsg); |
| 4238 | if (sock == -1) { |
| 4239 | channel_connect_ctx_free(&cctx); |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | |
| 4243 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4244 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4245 | c->host_port = port; |
| 4246 | c->path = xstrdup(host); |
| 4247 | c->connect_ctx = cctx; |
| 4248 | |
| 4249 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4252 | /* Check if connecting to that path is permitted and connect. */ |
| 4253 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4254 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4255 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4256 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4257 | struct ssh_channels *sc = ssh->chanctxt; |
| 4258 | u_int i, permit, permit_adm = 1; |
| 4259 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4260 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4261 | permit = sc->all_opens_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4262 | if (!permit) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4263 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4264 | fp = &sc->permitted_opens[i]; |
| 4265 | if (open_match(fp, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4266 | permit = 1; |
| 4267 | break; |
| 4268 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4269 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4270 | } |
| 4271 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4272 | if (sc->num_adm_permitted_opens > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4273 | permit_adm = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4274 | for (i = 0; i < sc->num_adm_permitted_opens; i++) { |
| 4275 | fp = &sc->permitted_adm_opens[i]; |
| 4276 | if (open_match(fp, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4277 | permit_adm = 1; |
| 4278 | break; |
| 4279 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4280 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | if (!permit || !permit_adm) { |
| 4284 | logit("Received request to connect to path %.100s, " |
| 4285 | "but the request was denied.", path); |
| 4286 | return NULL; |
| 4287 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4288 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4289 | } |
| 4290 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4291 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4292 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4293 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4294 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4295 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4296 | int r; |
| 4297 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4298 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4299 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4300 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4301 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4302 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4303 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4304 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4305 | channel_request_start(ssh, i, "window-change", 0); |
| 4306 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4307 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4308 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4309 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4310 | (r = sshpkt_send(ssh)) != 0) |
| 4311 | fatal("%s: channel %u: send window-change: %s", |
| 4312 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4316 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4317 | static Channel * |
| 4318 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4319 | { |
| 4320 | Channel *c; |
| 4321 | int r; |
| 4322 | |
| 4323 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4324 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4325 | c->host_port = 0; |
| 4326 | c->path = NULL; |
| 4327 | |
| 4328 | /* |
| 4329 | * We need to open the channel before we have a FD, |
| 4330 | * so that we can get SOCKS header from peer. |
| 4331 | */ |
| 4332 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4333 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4334 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4335 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4336 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4337 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4338 | c->self, ssh_err(r)); |
| 4339 | } |
| 4340 | return c; |
| 4341 | } |
| 4342 | |
| 4343 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4344 | static int |
| 4345 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4346 | { |
| 4347 | struct channel_connect cctx; |
| 4348 | int sock; |
| 4349 | |
| 4350 | memset(&cctx, 0, sizeof(cctx)); |
| 4351 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4352 | NULL, &cctx, NULL, NULL); |
| 4353 | if (sock == -1) |
| 4354 | channel_connect_ctx_free(&cctx); |
| 4355 | else { |
| 4356 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4357 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4358 | c->connect_ctx = cctx; |
| 4359 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4360 | } |
| 4361 | return sock; |
| 4362 | } |
| 4363 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4364 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4365 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4366 | /* |
| 4367 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4368 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4369 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4370 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4371 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4372 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4373 | int x11_use_localhost, int single_connection, |
| 4374 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4375 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4376 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4377 | int display_number, sock; |
| 4378 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4379 | struct addrinfo hints, *ai, *aitop; |
| 4380 | char strport[NI_MAXSERV]; |
| 4381 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4382 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4383 | if (chanids == NULL) |
| 4384 | return -1; |
| 4385 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4386 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4387 | display_number < MAX_DISPLAYS; |
| 4388 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4389 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4390 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4391 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4392 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4393 | hints.ai_socktype = SOCK_STREAM; |
| 4394 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4395 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4396 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4397 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4398 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4399 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4400 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4401 | if (ai->ai_family != AF_INET && |
| 4402 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4403 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4404 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4405 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4406 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4407 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4408 | #ifdef EPFNOSUPPORT |
| 4409 | && (errno != EPFNOSUPPORT) |
| 4410 | #endif |
| 4411 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4412 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4413 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4414 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4415 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4416 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4417 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4418 | continue; |
| 4419 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4420 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4421 | if (ai->ai_family == AF_INET6) |
| 4422 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4423 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4424 | set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4425 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4426 | debug2("%s: bind port %d: %.100s", __func__, |
| 4427 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4428 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4429 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4430 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4431 | num_socks = 0; |
| 4432 | break; |
| 4433 | } |
| 4434 | socks[num_socks++] = sock; |
| 4435 | if (num_socks == NUM_SOCKS) |
| 4436 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4437 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4438 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4439 | if (num_socks > 0) |
| 4440 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4441 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4442 | if (display_number >= MAX_DISPLAYS) { |
| 4443 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4444 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4445 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4446 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4447 | for (n = 0; n < num_socks; n++) { |
| 4448 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 4449 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4450 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4451 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4452 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4453 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4454 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4455 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4456 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4457 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4458 | for (n = 0; n < num_socks; n++) { |
| 4459 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4460 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4461 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4462 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4463 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4464 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4465 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4466 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4467 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4468 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4469 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4470 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4471 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4472 | } |
| 4473 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4474 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4475 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4476 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4477 | int sock; |
| 4478 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4479 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4480 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4481 | if (sock < 0) |
| 4482 | error("socket: %.100s", strerror(errno)); |
| 4483 | memset(&addr, 0, sizeof(addr)); |
| 4484 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4485 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4486 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4487 | return sock; |
| 4488 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4489 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4490 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4491 | } |
| 4492 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4493 | static int |
| 4494 | connect_local_xsocket(u_int dnr) |
| 4495 | { |
| 4496 | char buf[1024]; |
| 4497 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4498 | return connect_local_xsocket_path(buf); |
| 4499 | } |
| 4500 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4501 | #ifdef __APPLE__ |
| 4502 | static int |
| 4503 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4504 | { |
| 4505 | struct stat sbuf; |
| 4506 | |
| 4507 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4508 | error("%s: display path too long", __func__); |
| 4509 | return 0; |
| 4510 | } |
| 4511 | if (display[0] != '/') |
| 4512 | return 0; |
| 4513 | if (stat(path, &sbuf) == 0) { |
| 4514 | return 1; |
| 4515 | } else { |
| 4516 | char *dot = strrchr(path, '.'); |
| 4517 | if (dot != NULL) { |
| 4518 | *dot = '\0'; |
| 4519 | if (stat(path, &sbuf) == 0) { |
| 4520 | return 1; |
| 4521 | } |
| 4522 | } |
| 4523 | } |
| 4524 | return 0; |
| 4525 | } |
| 4526 | #endif |
| 4527 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4528 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4529 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4530 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4531 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4532 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4533 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4534 | struct addrinfo hints, *ai, *aitop; |
| 4535 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4536 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4537 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4538 | /* Try to open a socket for the local X server. */ |
| 4539 | display = getenv("DISPLAY"); |
| 4540 | if (!display) { |
| 4541 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4542 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4543 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4544 | /* |
| 4545 | * Now we decode the value of the DISPLAY variable and make a |
| 4546 | * connection to the real X server. |
| 4547 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4548 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4549 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4550 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4551 | { |
| 4552 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4553 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4554 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4555 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4556 | |
| 4557 | /* Create a socket. */ |
| 4558 | sock = connect_local_xsocket_path(path); |
| 4559 | if (sock < 0) |
| 4560 | return -1; |
| 4561 | |
| 4562 | /* OK, we now have a connection to the display. */ |
| 4563 | return sock; |
| 4564 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4565 | } |
| 4566 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4567 | /* |
| 4568 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4569 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4570 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4571 | if (strncmp(display, "unix:", 5) == 0 || |
| 4572 | display[0] == ':') { |
| 4573 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4574 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4575 | &display_number) != 1) { |
| 4576 | error("Could not parse display number from DISPLAY: " |
| 4577 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4578 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4579 | } |
| 4580 | /* Create a socket. */ |
| 4581 | sock = connect_local_xsocket(display_number); |
| 4582 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4583 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4584 | |
| 4585 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4586 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4587 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4588 | /* |
| 4589 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4590 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4591 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4592 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4593 | cp = strchr(buf, ':'); |
| 4594 | if (!cp) { |
| 4595 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4596 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4597 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4598 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4599 | /* |
| 4600 | * buf now contains the host name. But first we parse the |
| 4601 | * display number. |
| 4602 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4603 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4604 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4605 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4606 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4607 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4608 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4609 | /* Look up the host address */ |
| 4610 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4611 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4612 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4613 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4614 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4615 | error("%.100s: unknown host. (%s)", buf, |
| 4616 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4617 | return -1; |
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 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4620 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4621 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4622 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4623 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4624 | continue; |
| 4625 | } |
| 4626 | /* Connect it to the display. */ |
| 4627 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4628 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4629 | 6000 + display_number, strerror(errno)); |
| 4630 | close(sock); |
| 4631 | continue; |
| 4632 | } |
| 4633 | /* Success */ |
| 4634 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4635 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4636 | freeaddrinfo(aitop); |
| 4637 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4638 | error("connect %.100s port %u: %.100s", buf, |
| 4639 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4640 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4641 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4642 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4643 | return sock; |
| 4644 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4645 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4646 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4647 | * Requests forwarding of X11 connections, generates fake authentication |
| 4648 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4649 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4650 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4651 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4652 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4653 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4654 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4655 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4656 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4657 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4658 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4659 | char *new_data; |
| 4660 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4661 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4662 | if (sc->x11_saved_display == NULL) |
| 4663 | sc->x11_saved_display = xstrdup(disp); |
| 4664 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4665 | error("x11_request_forwarding_with_spoofing: different " |
| 4666 | "$DISPLAY already forwarded"); |
| 4667 | return; |
| 4668 | } |
| 4669 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4670 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4671 | if (cp) |
| 4672 | cp = strchr(cp, '.'); |
| 4673 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4674 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4675 | else |
| 4676 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4677 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4678 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4679 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4680 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4681 | |
| 4682 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4683 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4684 | for (i = 0; i < data_len; i++) { |
| 4685 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4686 | fatal("x11_request_forwarding: bad " |
| 4687 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4688 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4689 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4690 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4691 | |
| 4692 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4693 | sc->x11_fake_data = xmalloc(data_len); |
| 4694 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4695 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4696 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4697 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4698 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4699 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4700 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4701 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4702 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4703 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4704 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4705 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4706 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4707 | (r = sshpkt_send(ssh)) != 0 || |
| 4708 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4709 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4710 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4711 | } |