djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.377 2017/12/05 01:30:19 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains functions for generic socket connection forwarding. |
| 7 | * There is also code for initiating connection forwarding for X11 connections, |
| 8 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 18 | * Copyright (c) 1999 Dug Song. All rights reserved. |
| 19 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 43 | |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 44 | #include <sys/types.h> |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 45 | #include <sys/stat.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include <sys/ioctl.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 47 | #include <sys/un.h> |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 48 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 49 | #ifdef HAVE_SYS_TIME_H |
| 50 | # include <sys/time.h> |
| 51 | #endif |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 52 | |
| 53 | #include <netinet/in.h> |
| 54 | #include <arpa/inet.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 55 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 56 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 57 | #include <fcntl.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 58 | #include <limits.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 59 | #include <netdb.h> |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 60 | #include <stdarg.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 61 | #ifdef HAVE_STDINT_H |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 62 | #include <stdint.h> |
Darren Tucker | 37f9220 | 2015-02-23 03:07:24 +1100 | [diff] [blame] | 63 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 64 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 65 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 66 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 67 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 68 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 70 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 73 | #include "ssh2.h" |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 74 | #include "ssherr.h" |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 75 | #include "sshbuf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 77 | #include "log.h" |
| 78 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 81 | #include "canohost.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 82 | #include "key.h" |
| 83 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 84 | #include "pathnames.h" |
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 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 439 | channel_close_fd(ssh, &c->sock); |
| 440 | channel_close_fd(ssh, &c->rfd); |
| 441 | channel_close_fd(ssh, &c->wfd); |
| 442 | channel_close_fd(ssh, &c->efd); |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | fwd_perm_clear(ForwardPermission *fp) |
| 447 | { |
| 448 | free(fp->host_to_connect); |
| 449 | free(fp->listen_host); |
| 450 | free(fp->listen_path); |
| 451 | bzero(fp, sizeof(*fp)); |
| 452 | } |
| 453 | |
| 454 | enum { FWDPERM_USER, FWDPERM_ADMIN }; |
| 455 | |
| 456 | static int |
| 457 | fwd_perm_list_add(struct ssh *ssh, int which, |
| 458 | const char *host_to_connect, int port_to_connect, |
| 459 | const char *listen_host, const char *listen_path, int listen_port, |
| 460 | Channel *downstream) |
| 461 | { |
| 462 | ForwardPermission **fpl; |
| 463 | u_int n, *nfpl; |
| 464 | |
| 465 | switch (which) { |
| 466 | case FWDPERM_USER: |
| 467 | fpl = &ssh->chanctxt->permitted_opens; |
| 468 | nfpl = &ssh->chanctxt->num_permitted_opens; |
| 469 | break; |
| 470 | case FWDPERM_ADMIN: |
| 471 | fpl = &ssh->chanctxt->permitted_adm_opens; |
| 472 | nfpl = &ssh->chanctxt->num_adm_permitted_opens; |
| 473 | break; |
| 474 | default: |
| 475 | fatal("%s: invalid list %d", __func__, which); |
| 476 | } |
| 477 | |
| 478 | if (*nfpl >= INT_MAX) |
| 479 | fatal("%s: overflow", __func__); |
| 480 | |
| 481 | *fpl = xrecallocarray(*fpl, *nfpl, *nfpl + 1, sizeof(**fpl)); |
| 482 | n = (*nfpl)++; |
| 483 | #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s)) |
| 484 | (*fpl)[n].host_to_connect = MAYBE_DUP(host_to_connect); |
| 485 | (*fpl)[n].port_to_connect = port_to_connect; |
| 486 | (*fpl)[n].listen_host = MAYBE_DUP(listen_host); |
| 487 | (*fpl)[n].listen_path = MAYBE_DUP(listen_path); |
| 488 | (*fpl)[n].listen_port = listen_port; |
| 489 | (*fpl)[n].downstream = downstream; |
| 490 | #undef MAYBE_DUP |
| 491 | return (int)n; |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | mux_remove_remote_forwardings(struct ssh *ssh, Channel *c) |
| 496 | { |
| 497 | struct ssh_channels *sc = ssh->chanctxt; |
| 498 | ForwardPermission *fp; |
| 499 | int r; |
| 500 | u_int i; |
| 501 | |
| 502 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 503 | fp = &sc->permitted_opens[i]; |
| 504 | if (fp->downstream != c) |
| 505 | continue; |
| 506 | |
| 507 | /* cancel on the server, since mux client is gone */ |
| 508 | debug("channel %d: cleanup remote forward for %s:%u", |
| 509 | c->self, fp->listen_host, fp->listen_port); |
| 510 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 511 | (r = sshpkt_put_cstring(ssh, |
| 512 | "cancel-tcpip-forward")) != 0 || |
| 513 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 514 | (r = sshpkt_put_cstring(ssh, |
| 515 | channel_rfwd_bind_host(fp->listen_host))) != 0 || |
| 516 | (r = sshpkt_put_u32(ssh, fp->listen_port)) != 0 || |
| 517 | (r = sshpkt_send(ssh)) != 0) { |
| 518 | fatal("%s: channel %i: %s", __func__, |
| 519 | c->self, ssh_err(r)); |
| 520 | } |
| 521 | fwd_perm_clear(fp); /* unregister */ |
| 522 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 526 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 527 | channel_free(struct ssh *ssh, Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 528 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 529 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 530 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 531 | u_int i, n; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 532 | Channel *other; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 533 | struct channel_confirm *cc; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 534 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 535 | for (n = 0, i = 0; i < sc->channels_alloc; i++) { |
| 536 | if ((other = sc->channels[i]) == NULL) |
| 537 | continue; |
| 538 | n++; |
| 539 | /* detach from mux client and prepare for closing */ |
| 540 | if (c->type == SSH_CHANNEL_MUX_CLIENT && |
| 541 | other->type == SSH_CHANNEL_MUX_PROXY && |
| 542 | other->mux_ctx == c) { |
| 543 | other->mux_ctx = NULL; |
| 544 | other->type = SSH_CHANNEL_OPEN; |
| 545 | other->istate = CHAN_INPUT_CLOSED; |
| 546 | other->ostate = CHAN_OUTPUT_CLOSED; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 549 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 550 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 551 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 552 | if (c->type == SSH_CHANNEL_MUX_CLIENT) |
| 553 | mux_remove_remote_forwardings(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 554 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 555 | s = channel_open_message(ssh); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 556 | debug3("channel %d: status: %s", c->self, s); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 557 | free(s); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 558 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 559 | channel_close_fds(ssh, c); |
| 560 | sshbuf_free(c->input); |
| 561 | sshbuf_free(c->output); |
| 562 | sshbuf_free(c->extended); |
| 563 | c->input = c->output = c->extended = NULL; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 564 | free(c->remote_name); |
| 565 | c->remote_name = NULL; |
| 566 | free(c->path); |
| 567 | c->path = NULL; |
| 568 | free(c->listening_addr); |
| 569 | c->listening_addr = NULL; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 570 | while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) { |
| 571 | if (cc->abandon_cb != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 572 | cc->abandon_cb(ssh, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 573 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 574 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 575 | free(cc); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 576 | } |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 577 | if (c->filter_cleanup != NULL && c->filter_ctx != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 578 | c->filter_cleanup(ssh, c->self, c->filter_ctx); |
| 579 | sc->channels[c->self] = NULL; |
millert@openbsd.org | 3e8d185 | 2017-09-19 12:10:30 +0000 | [diff] [blame] | 580 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 581 | free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 582 | } |
| 583 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 584 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 585 | channel_free_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 586 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 587 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 588 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 589 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 590 | if (ssh->chanctxt->channels[i] != NULL) |
| 591 | channel_free(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 596 | * descriptors after a fork. |
| 597 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 598 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 599 | channel_close_all(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 600 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 601 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 602 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 603 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) |
| 604 | if (ssh->chanctxt->channels[i] != NULL) |
| 605 | channel_close_fds(ssh, ssh->chanctxt->channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 609 | * Stop listening to channels. |
| 610 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 611 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 612 | channel_stop_listening(struct ssh *ssh) |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 613 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 614 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 615 | Channel *c; |
| 616 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 617 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 618 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 619 | if (c != NULL) { |
| 620 | switch (c->type) { |
| 621 | case SSH_CHANNEL_AUTH_SOCKET: |
| 622 | case SSH_CHANNEL_PORT_LISTENER: |
| 623 | case SSH_CHANNEL_RPORT_LISTENER: |
| 624 | case SSH_CHANNEL_X11_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 625 | case SSH_CHANNEL_UNIX_LISTENER: |
| 626 | case SSH_CHANNEL_RUNIX_LISTENER: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 627 | channel_close_fd(ssh, &c->sock); |
| 628 | channel_free(ssh, c); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 636 | * Returns true if no channel has too much buffered data, and false if one or |
| 637 | * more channel is overfull. |
| 638 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 639 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 640 | channel_not_very_much_buffered_data(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 641 | { |
| 642 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 643 | u_int maxsize = ssh_packet_get_maxsize(ssh); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 644 | Channel *c; |
| 645 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 646 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 647 | c = ssh->chanctxt->channels[i]; |
| 648 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) |
| 649 | continue; |
| 650 | if (sshbuf_len(c->output) > maxsize) { |
| 651 | debug2("channel %d: big output buffer %zu > %u", |
| 652 | c->self, sshbuf_len(c->output), maxsize); |
| 653 | return 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | return 1; |
| 657 | } |
| 658 | |
| 659 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 660 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 661 | channel_still_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 662 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 663 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 664 | Channel *c; |
| 665 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 666 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 667 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 668 | if (c == NULL) |
| 669 | continue; |
| 670 | switch (c->type) { |
| 671 | case SSH_CHANNEL_X11_LISTENER: |
| 672 | case SSH_CHANNEL_PORT_LISTENER: |
| 673 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 674 | case SSH_CHANNEL_MUX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 675 | case SSH_CHANNEL_CLOSED: |
| 676 | case SSH_CHANNEL_AUTH_SOCKET: |
| 677 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 678 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 679 | case SSH_CHANNEL_CONNECTING: |
| 680 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 681 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 682 | case SSH_CHANNEL_UNIX_LISTENER: |
| 683 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 684 | continue; |
| 685 | case SSH_CHANNEL_LARVAL: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 686 | continue; |
| 687 | case SSH_CHANNEL_OPENING: |
| 688 | case SSH_CHANNEL_OPEN: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 689 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 690 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 691 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 692 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 693 | return 1; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 694 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 695 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 696 | /* NOTREACHED */ |
| 697 | } |
| 698 | } |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 703 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 704 | channel_find_open(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 705 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 706 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 707 | Channel *c; |
| 708 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 709 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 710 | c = ssh->chanctxt->channels[i]; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 711 | if (c == NULL || !c->have_remote_id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 712 | continue; |
| 713 | switch (c->type) { |
| 714 | case SSH_CHANNEL_CLOSED: |
| 715 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 716 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 717 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 718 | case SSH_CHANNEL_X11_LISTENER: |
| 719 | case SSH_CHANNEL_PORT_LISTENER: |
| 720 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 721 | case SSH_CHANNEL_MUX_LISTENER: |
| 722 | case SSH_CHANNEL_MUX_CLIENT: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 723 | case SSH_CHANNEL_MUX_PROXY: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 724 | case SSH_CHANNEL_OPENING: |
| 725 | case SSH_CHANNEL_CONNECTING: |
| 726 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 727 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 728 | case SSH_CHANNEL_UNIX_LISTENER: |
| 729 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 730 | continue; |
| 731 | case SSH_CHANNEL_LARVAL: |
| 732 | case SSH_CHANNEL_AUTH_SOCKET: |
| 733 | case SSH_CHANNEL_OPEN: |
| 734 | case SSH_CHANNEL_X11_OPEN: |
| 735 | return i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 736 | default: |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 737 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 738 | /* NOTREACHED */ |
| 739 | } |
| 740 | } |
| 741 | return -1; |
| 742 | } |
| 743 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 744 | /* |
| 745 | * Returns a message describing the currently open forwarded connections, |
| 746 | * suitable for sending to the client. The message contains crlf pairs for |
| 747 | * newlines. |
| 748 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 749 | char * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 750 | channel_open_message(struct ssh *ssh) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 751 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 752 | struct sshbuf *buf; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 753 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 754 | u_int i; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 755 | int r; |
| 756 | char *ret; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 757 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 758 | if ((buf = sshbuf_new()) == NULL) |
| 759 | fatal("%s: sshbuf_new", __func__); |
| 760 | if ((r = sshbuf_putf(buf, |
| 761 | "The following connections are open:\r\n")) != 0) |
| 762 | fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r)); |
| 763 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 764 | c = ssh->chanctxt->channels[i]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 765 | if (c == NULL) |
| 766 | continue; |
| 767 | switch (c->type) { |
| 768 | case SSH_CHANNEL_X11_LISTENER: |
| 769 | case SSH_CHANNEL_PORT_LISTENER: |
| 770 | case SSH_CHANNEL_RPORT_LISTENER: |
| 771 | case SSH_CHANNEL_CLOSED: |
| 772 | case SSH_CHANNEL_AUTH_SOCKET: |
| 773 | case SSH_CHANNEL_ZOMBIE: |
Damien Miller | 3618709 | 2013-06-10 13:07:11 +1000 | [diff] [blame] | 774 | case SSH_CHANNEL_ABANDONED: |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 775 | case SSH_CHANNEL_MUX_LISTENER: |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 776 | case SSH_CHANNEL_UNIX_LISTENER: |
| 777 | case SSH_CHANNEL_RUNIX_LISTENER: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 778 | continue; |
| 779 | case SSH_CHANNEL_LARVAL: |
| 780 | case SSH_CHANNEL_OPENING: |
| 781 | case SSH_CHANNEL_CONNECTING: |
| 782 | case SSH_CHANNEL_DYNAMIC: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 783 | case SSH_CHANNEL_RDYNAMIC_OPEN: |
| 784 | case SSH_CHANNEL_RDYNAMIC_FINISH: |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 785 | case SSH_CHANNEL_OPEN: |
| 786 | case SSH_CHANNEL_X11_OPEN: |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 787 | case SSH_CHANNEL_MUX_PROXY: |
| 788 | case SSH_CHANNEL_MUX_CLIENT: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 789 | if ((r = sshbuf_putf(buf, " #%d %.300s " |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 790 | "(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] | 791 | c->self, c->remote_name, |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 792 | c->type, |
| 793 | c->have_remote_id ? "r" : "nr", c->remote_id, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 794 | c->istate, sshbuf_len(c->input), |
| 795 | c->ostate, sshbuf_len(c->output), |
| 796 | c->rfd, c->wfd, c->ctl_chan)) != 0) |
| 797 | fatal("%s: sshbuf_putf: %s", |
| 798 | __func__, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 799 | continue; |
| 800 | default: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 801 | fatal("%s: bad channel type %d", __func__, c->type); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 802 | /* NOTREACHED */ |
| 803 | } |
| 804 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 805 | if ((ret = sshbuf_dup_string(buf)) == NULL) |
| 806 | fatal("%s: sshbuf_dup_string", __func__); |
| 807 | sshbuf_free(buf); |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | static void |
| 812 | open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type) |
| 813 | { |
| 814 | int r; |
| 815 | |
| 816 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || |
| 817 | (r = sshpkt_put_cstring(ssh, type)) != 0 || |
| 818 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 819 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 820 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 821 | fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r)); |
| 822 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 826 | channel_send_open(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 827 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 828 | Channel *c = channel_lookup(ssh, id); |
| 829 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 830 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 831 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 832 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 833 | return; |
| 834 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 835 | debug2("channel %d: send open", id); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 836 | open_preamble(ssh, __func__, c, c->ctype); |
| 837 | if ((r = sshpkt_send(ssh)) != 0) |
| 838 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 842 | channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 843 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 844 | Channel *c = channel_lookup(ssh, id); |
| 845 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 846 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 847 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 848 | logit("%s: %d: unknown channel id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 849 | return; |
| 850 | } |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 851 | if (!c->have_remote_id) |
| 852 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 853 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 854 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 855 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || |
| 856 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 857 | (r = sshpkt_put_cstring(ssh, service)) != 0 || |
| 858 | (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) { |
| 859 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
| 860 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 861 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 862 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 863 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 864 | channel_register_status_confirm(struct ssh *ssh, int id, |
| 865 | channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 866 | { |
| 867 | struct channel_confirm *cc; |
| 868 | Channel *c; |
| 869 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 870 | if ((c = channel_lookup(ssh, id)) == NULL) |
| 871 | fatal("%s: %d: bad id", __func__, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 872 | |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 873 | cc = xcalloc(1, sizeof(*cc)); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 874 | cc->cb = cb; |
| 875 | cc->abandon_cb = abandon_cb; |
| 876 | cc->ctx = ctx; |
| 877 | TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry); |
| 878 | } |
| 879 | |
| 880 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 881 | channel_register_open_confirm(struct ssh *ssh, int id, |
| 882 | channel_open_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 883 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 884 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 885 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 886 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 887 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 888 | return; |
| 889 | } |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 890 | c->open_confirm = fn; |
| 891 | c->open_confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 892 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 893 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 894 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 895 | channel_register_cleanup(struct ssh *ssh, int id, |
| 896 | channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 897 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 898 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 899 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 900 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 901 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 904 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 905 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 906 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 907 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 908 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 909 | channel_cancel_cleanup(struct ssh *ssh, int id) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 910 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 911 | Channel *c = channel_by_id(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 912 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 913 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 914 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 915 | return; |
| 916 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 917 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 918 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 919 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 920 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 921 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 922 | channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn, |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 923 | channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 924 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 925 | Channel *c = channel_lookup(ssh, id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 926 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 927 | if (c == NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 928 | logit("%s: %d: bad id", __func__, id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 929 | return; |
| 930 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 931 | c->input_filter = ifn; |
| 932 | c->output_filter = ofn; |
Darren Tucker | 2fb66ca | 2008-06-13 04:49:33 +1000 | [diff] [blame] | 933 | c->filter_ctx = ctx; |
Darren Tucker | 84c56f5 | 2008-06-13 04:55:46 +1000 | [diff] [blame] | 934 | c->filter_cleanup = cfn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 938 | 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] | 939 | int extusage, int nonblock, int is_tty, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 940 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 941 | Channel *c = channel_lookup(ssh, id); |
| 942 | int r; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 943 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 944 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 945 | fatal("channel_activate for non-larval channel %d.", id); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 946 | if (!c->have_remote_id) |
| 947 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 948 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 949 | channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 950 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 951 | c->local_window = c->local_window_max = window_max; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 952 | |
| 953 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 954 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 955 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 956 | (r = sshpkt_send(ssh)) != 0) |
| 957 | fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r)); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 960 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 961 | channel_pre_listener(struct ssh *ssh, Channel *c, |
| 962 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 963 | { |
| 964 | FD_SET(c->sock, readset); |
| 965 | } |
| 966 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 967 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 968 | channel_pre_connecting(struct ssh *ssh, Channel *c, |
| 969 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 970 | { |
| 971 | debug3("channel %d: waiting for connection", c->self); |
| 972 | FD_SET(c->sock, writeset); |
| 973 | } |
| 974 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 975 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 976 | channel_pre_open(struct ssh *ssh, Channel *c, |
| 977 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 978 | { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 979 | if (c->istate == CHAN_INPUT_OPEN && |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 980 | c->remote_window > 0 && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 981 | sshbuf_len(c->input) < c->remote_window && |
| 982 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 983 | FD_SET(c->rfd, readset); |
| 984 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 985 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 986 | if (sshbuf_len(c->output) > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 987 | FD_SET(c->wfd, writeset); |
| 988 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 989 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 990 | debug2("channel %d: " |
| 991 | "obuf_empty delayed efd %d/(%zu)", c->self, |
| 992 | c->efd, sshbuf_len(c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 993 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 994 | chan_obuf_empty(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | /** XXX check close conditions, too */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 998 | if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED && |
| 999 | c->ostate == CHAN_OUTPUT_CLOSED)) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1000 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1001 | sshbuf_len(c->extended) > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1002 | FD_SET(c->efd, writeset); |
Damien Miller | 8853ca5 | 2010-06-26 10:00:14 +1000 | [diff] [blame] | 1003 | else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) && |
| 1004 | (c->extended_usage == CHAN_EXTENDED_READ || |
| 1005 | c->extended_usage == CHAN_EXTENDED_IGNORE) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1006 | sshbuf_len(c->extended) < c->remote_window) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1007 | FD_SET(c->efd, readset); |
| 1008 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1009 | /* XXX: What about efd? races? */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1010 | } |
| 1011 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1012 | /* |
| 1013 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 1014 | * connection (when authentication spoofing is being done) remains in this |
| 1015 | * state until the first packet has been completely read. The authentication |
| 1016 | * data in that packet is then substituted by the real data if it matches the |
| 1017 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1018 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1019 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1020 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1021 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1022 | x11_open_helper(struct ssh *ssh, struct sshbuf *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1023 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1024 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1025 | u_char *ucp; |
| 1026 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1027 | |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1028 | /* Is this being called after the refusal deadline? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1029 | if (sc->x11_refuse_time != 0 && |
| 1030 | (u_int)monotime() >= sc->x11_refuse_time) { |
djm@openbsd.org | 1bf477d | 2015-07-01 02:26:31 +0000 | [diff] [blame] | 1031 | verbose("Rejected X11 connection after ForwardX11Timeout " |
| 1032 | "expired"); |
| 1033 | return -1; |
| 1034 | } |
| 1035 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1036 | /* 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] | 1037 | if (sshbuf_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1038 | return 0; |
| 1039 | |
| 1040 | /* Parse the lengths of variable-length fields. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1041 | ucp = sshbuf_mutable_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1042 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 1043 | proto_len = 256 * ucp[6] + ucp[7]; |
| 1044 | data_len = 256 * ucp[8] + ucp[9]; |
| 1045 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 1046 | proto_len = ucp[6] + 256 * ucp[7]; |
| 1047 | data_len = ucp[8] + 256 * ucp[9]; |
| 1048 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1049 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1050 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | /* Check if the whole packet is in buffer. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1055 | if (sshbuf_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1056 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 1057 | return 0; |
| 1058 | |
| 1059 | /* Check if authentication protocol matches. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1060 | if (proto_len != strlen(sc->x11_saved_proto) || |
| 1061 | memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1062 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1063 | return -1; |
| 1064 | } |
| 1065 | /* Check if authentication data matches our fake data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1066 | if (data_len != sc->x11_fake_data_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1067 | timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1068 | sc->x11_fake_data, sc->x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1069 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1070 | return -1; |
| 1071 | } |
| 1072 | /* Check fake data length */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1073 | if (sc->x11_fake_data_len != sc->x11_saved_data_len) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1074 | error("X11 fake_data_len %d != saved_data_len %d", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1075 | sc->x11_fake_data_len, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1076 | return -1; |
| 1077 | } |
| 1078 | /* |
| 1079 | * Received authentication protocol and data match |
| 1080 | * our fake data. Substitute the fake data with real |
| 1081 | * data. |
| 1082 | */ |
| 1083 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1084 | sc->x11_saved_data, sc->x11_saved_data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1085 | return 1; |
| 1086 | } |
| 1087 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1088 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1089 | channel_pre_x11_open(struct ssh *ssh, Channel *c, |
| 1090 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1091 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1092 | int ret = x11_open_helper(ssh, c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* c->force_drain = 1; */ |
| 1095 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1096 | if (ret == 1) { |
| 1097 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1098 | channel_pre_open(ssh, c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1099 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1100 | logit("X11 connection rejected because of wrong authentication."); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1101 | debug2("X11 rejected %d i%d/o%d", |
| 1102 | c->self, c->istate, c->ostate); |
| 1103 | chan_read_failed(ssh, c); |
| 1104 | sshbuf_reset(c->input); |
| 1105 | chan_ibuf_empty(ssh, c); |
| 1106 | sshbuf_reset(c->output); |
| 1107 | chan_write_failed(ssh, c); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1108 | 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] | 1109 | } |
| 1110 | } |
| 1111 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1112 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1113 | channel_pre_mux_client(struct ssh *ssh, |
| 1114 | Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1115 | { |
Damien Miller | d530f5f | 2010-05-21 14:57:10 +1000 | [diff] [blame] | 1116 | if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1117 | sshbuf_check_reserve(c->input, CHAN_RBUF) == 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1118 | FD_SET(c->rfd, readset); |
| 1119 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 1120 | /* clear buffer immediately (discard any partial packet) */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1121 | sshbuf_reset(c->input); |
| 1122 | chan_ibuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1123 | /* Start output drain. XXX just kill chan? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1124 | chan_rcvd_oclose(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1125 | } |
| 1126 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 1127 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1128 | if (sshbuf_len(c->output) > 0) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1129 | FD_SET(c->wfd, writeset); |
| 1130 | else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1131 | chan_obuf_empty(ssh, c); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1135 | /* try to decode a socks4 header */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1136 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1137 | channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1138 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1139 | const u_char *p; |
| 1140 | char *host; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1141 | u_int len, have, i, found, need; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1142 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1143 | struct { |
| 1144 | u_int8_t version; |
| 1145 | u_int8_t command; |
| 1146 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1147 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1148 | } s4_req, s4_rsp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1149 | int r; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1150 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1151 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1152 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1153 | have = sshbuf_len(input); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1154 | len = sizeof(s4_req); |
| 1155 | if (have < len) |
| 1156 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1157 | p = sshbuf_ptr(input); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1158 | |
| 1159 | need = 1; |
| 1160 | /* SOCKS4A uses an invalid IP address 0.0.0.x */ |
| 1161 | if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) { |
| 1162 | debug2("channel %d: socks4a request", c->self); |
| 1163 | /* ... and needs an extra string (the hostname) */ |
| 1164 | need = 2; |
| 1165 | } |
| 1166 | /* Check for terminating NUL on the string(s) */ |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1167 | for (found = 0, i = len; i < have; i++) { |
| 1168 | if (p[i] == '\0') { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1169 | found++; |
| 1170 | if (found == need) |
| 1171 | break; |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1172 | } |
| 1173 | if (i > 1024) { |
| 1174 | /* the peer is probably sending garbage */ |
| 1175 | debug("channel %d: decode socks4: too long", |
| 1176 | c->self); |
| 1177 | return -1; |
| 1178 | } |
| 1179 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1180 | if (found < need) |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 1181 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1182 | if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 || |
| 1183 | (r = sshbuf_get(input, &s4_req.command, 1)) != 0 || |
| 1184 | (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 || |
| 1185 | (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1186 | debug("channels %d: decode socks4: %s", c->self, ssh_err(r)); |
| 1187 | return -1; |
| 1188 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1189 | have = sshbuf_len(input); |
| 1190 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1191 | if (memchr(p, '\0', have) == NULL) { |
| 1192 | error("channel %d: decode socks4: user not nul terminated", |
Damien Miller | 1348129 | 2014-02-27 10:18:32 +1100 | [diff] [blame] | 1193 | c->self); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1194 | return -1; |
| 1195 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1196 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1197 | 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] | 1198 | len++; /* trailing '\0' */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1199 | strlcpy(username, p, sizeof(username)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1200 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1201 | fatal("%s: channel %d: consume: %s", __func__, |
| 1202 | c->self, ssh_err(r)); |
| 1203 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1204 | free(c->path); |
| 1205 | c->path = NULL; |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1206 | if (need == 1) { /* SOCKS4: one string */ |
| 1207 | host = inet_ntoa(s4_req.dest_addr); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1208 | c->path = xstrdup(host); |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1209 | } else { /* SOCKS4A: two strings */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1210 | have = sshbuf_len(input); |
| 1211 | p = sshbuf_ptr(input); |
djm@openbsd.org | c04e979 | 2017-05-31 00:43:04 +0000 | [diff] [blame] | 1212 | if (memchr(p, '\0', have) == NULL) { |
| 1213 | error("channel %d: decode socks4a: host not nul " |
| 1214 | "terminated", c->self); |
| 1215 | return -1; |
| 1216 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1217 | len = strlen(p); |
| 1218 | debug2("channel %d: decode socks4a: host %s/%d", |
| 1219 | c->self, p, len); |
| 1220 | len++; /* trailing '\0' */ |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1221 | if (len > NI_MAXHOST) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1222 | error("channel %d: hostname \"%.100s\" too long", |
| 1223 | c->self, p); |
| 1224 | return -1; |
| 1225 | } |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1226 | c->path = xstrdup(p); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1227 | if ((r = sshbuf_consume(input, len)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1228 | fatal("%s: channel %d: consume: %s", __func__, |
| 1229 | c->self, ssh_err(r)); |
| 1230 | } |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1231 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1232 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1233 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1234 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1235 | c->self, c->path, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1236 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1237 | if (s4_req.command != 1) { |
Damien Miller | 1781f53 | 2009-01-28 16:24:41 +1100 | [diff] [blame] | 1238 | debug("channel %d: cannot handle: %s cn %d", |
| 1239 | c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1240 | return -1; |
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 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1243 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1244 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1245 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1246 | if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1247 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1248 | c->self, ssh_err(r)); |
| 1249 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1250 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1253 | /* try to decode a socks5 header */ |
| 1254 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1255 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1256 | #define SSH_SOCKS5_IPV4 0x01 |
| 1257 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1258 | #define SSH_SOCKS5_IPV6 0x04 |
| 1259 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1260 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1261 | |
| 1262 | static int |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1263 | channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1264 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1265 | /* XXX use get/put_u8 instead of trusting struct padding */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1266 | struct { |
| 1267 | u_int8_t version; |
| 1268 | u_int8_t command; |
| 1269 | u_int8_t reserved; |
| 1270 | u_int8_t atyp; |
| 1271 | } s5_req, s5_rsp; |
| 1272 | u_int16_t dest_port; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 1273 | char dest_addr[255+1], ntop[INET6_ADDRSTRLEN]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1274 | const u_char *p; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1275 | u_int have, need, i, found, nmethods, addrlen, af; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1276 | int r; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1277 | |
| 1278 | debug2("channel %d: decode socks5", c->self); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1279 | p = sshbuf_ptr(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1280 | if (p[0] != 0x05) |
| 1281 | return -1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1282 | have = sshbuf_len(input); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1283 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1284 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1285 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1286 | return 0; |
| 1287 | nmethods = p[1]; |
| 1288 | if (have < nmethods + 2) |
| 1289 | return 0; |
| 1290 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 1291 | for (found = 0, i = 2; i < nmethods + 2; i++) { |
Damien Miller | 4dec5d7 | 2006-08-05 11:38:40 +1000 | [diff] [blame] | 1292 | if (p[i] == SSH_SOCKS5_NOAUTH) { |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1293 | found = 1; |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
| 1297 | if (!found) { |
| 1298 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1299 | c->self); |
| 1300 | return -1; |
| 1301 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1302 | if ((r = sshbuf_consume(input, nmethods + 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1303 | fatal("%s: channel %d: consume: %s", __func__, |
| 1304 | c->self, ssh_err(r)); |
| 1305 | } |
| 1306 | /* version, method */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1307 | if ((r = sshbuf_put_u8(output, 0x05)) != 0 || |
| 1308 | (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1309 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1310 | c->self, ssh_err(r)); |
| 1311 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1312 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1313 | debug2("channel %d: socks5 auth done", c->self); |
| 1314 | return 0; /* need more */ |
| 1315 | } |
| 1316 | debug2("channel %d: socks5 post auth", c->self); |
| 1317 | if (have < sizeof(s5_req)+1) |
| 1318 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1319 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1320 | if (s5_req.version != 0x05 || |
| 1321 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1322 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1323 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1324 | return -1; |
| 1325 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1326 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1327 | case SSH_SOCKS5_IPV4: |
| 1328 | addrlen = 4; |
| 1329 | af = AF_INET; |
| 1330 | break; |
| 1331 | case SSH_SOCKS5_DOMAIN: |
| 1332 | addrlen = p[sizeof(s5_req)]; |
| 1333 | af = -1; |
| 1334 | break; |
| 1335 | case SSH_SOCKS5_IPV6: |
| 1336 | addrlen = 16; |
| 1337 | af = AF_INET6; |
| 1338 | break; |
| 1339 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1340 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1341 | return -1; |
| 1342 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1343 | need = sizeof(s5_req) + addrlen + 2; |
| 1344 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1345 | need++; |
| 1346 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1347 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1348 | if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1349 | fatal("%s: channel %d: consume: %s", __func__, |
| 1350 | c->self, ssh_err(r)); |
| 1351 | } |
| 1352 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
| 1353 | /* host string length */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1354 | if ((r = sshbuf_consume(input, 1)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1355 | fatal("%s: channel %d: consume: %s", __func__, |
| 1356 | c->self, ssh_err(r)); |
| 1357 | } |
| 1358 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1359 | if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 || |
| 1360 | (r = sshbuf_get(input, &dest_port, 2)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1361 | debug("channel %d: parse addr/port: %s", c->self, ssh_err(r)); |
| 1362 | return -1; |
| 1363 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1364 | dest_addr[addrlen] = '\0'; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1365 | free(c->path); |
| 1366 | c->path = NULL; |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1367 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) { |
Damien Miller | 9576ac4 | 2009-01-28 16:30:33 +1100 | [diff] [blame] | 1368 | if (addrlen >= NI_MAXHOST) { |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 1369 | error("channel %d: dynamic request: socks5 hostname " |
| 1370 | "\"%.100s\" too long", c->self, dest_addr); |
| 1371 | return -1; |
| 1372 | } |
| 1373 | c->path = xstrdup(dest_addr); |
| 1374 | } else { |
| 1375 | if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL) |
| 1376 | return -1; |
| 1377 | c->path = xstrdup(ntop); |
| 1378 | } |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1379 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1380 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1381 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1382 | c->self, c->path, c->host_port, s5_req.command); |
| 1383 | |
| 1384 | s5_rsp.version = 0x05; |
| 1385 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1386 | s5_rsp.reserved = 0; /* ignored */ |
| 1387 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1388 | dest_port = 0; /* ignored */ |
| 1389 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1390 | if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 || |
| 1391 | (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 || |
| 1392 | (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1393 | fatal("%s: channel %d: append reply: %s", __func__, |
| 1394 | c->self, ssh_err(r)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1395 | return 1; |
| 1396 | } |
| 1397 | |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1398 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1399 | channel_connect_stdio_fwd(struct ssh *ssh, |
| 1400 | 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] | 1401 | { |
| 1402 | Channel *c; |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1403 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1404 | debug("%s %s:%d", __func__, host_to_connect, port_to_connect); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1405 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1406 | c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out, |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1407 | -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 1408 | 0, "stdio-forward", /*nonblock*/0); |
| 1409 | |
| 1410 | c->path = xstrdup(host_to_connect); |
| 1411 | c->host_port = port_to_connect; |
| 1412 | c->listening_port = 0; |
| 1413 | c->force_drain = 1; |
| 1414 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1415 | channel_register_fds(ssh, c, in, out, -1, 0, 1, 0); |
| 1416 | port_open_helper(ssh, c, "direct-tcpip"); |
Darren Tucker | 7ad8dd2 | 2010-01-12 19:40:27 +1100 | [diff] [blame] | 1417 | |
| 1418 | return c; |
| 1419 | } |
| 1420 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1421 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1422 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1423 | channel_pre_dynamic(struct ssh *ssh, Channel *c, |
| 1424 | fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1425 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1426 | const u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1427 | u_int have; |
| 1428 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1429 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1430 | have = sshbuf_len(c->input); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1431 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1432 | /* sshbuf_dump(c->input, stderr); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1433 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1434 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1435 | /* need more */ |
| 1436 | FD_SET(c->sock, readset); |
| 1437 | return; |
| 1438 | } |
| 1439 | /* try to guess the protocol */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1440 | p = sshbuf_ptr(c->input); |
| 1441 | /* XXX sshbuf_peek_u8? */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1442 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1443 | case 0x04: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1444 | ret = channel_decode_socks4(c, c->input, c->output); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1445 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1446 | case 0x05: |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1447 | ret = channel_decode_socks5(c, c->input, c->output); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1448 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1449 | default: |
| 1450 | ret = -1; |
| 1451 | break; |
| 1452 | } |
| 1453 | if (ret < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1454 | chan_mark_dead(ssh, c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1455 | } else if (ret == 0) { |
| 1456 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1457 | /* need more */ |
| 1458 | FD_SET(c->sock, readset); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1459 | if (sshbuf_len(c->output)) |
| 1460 | FD_SET(c->sock, writeset); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1461 | } else { |
| 1462 | /* switch to the next state */ |
| 1463 | c->type = SSH_CHANNEL_OPENING; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1464 | port_open_helper(ssh, c, "direct-tcpip"); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1467 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 1468 | /* simulate read-error */ |
| 1469 | static void |
| 1470 | rdynamic_close(struct ssh *ssh, Channel *c) |
| 1471 | { |
| 1472 | c->type = SSH_CHANNEL_OPEN; |
| 1473 | chan_read_failed(ssh, c); |
| 1474 | sshbuf_reset(c->input); |
| 1475 | chan_ibuf_empty(ssh, c); |
| 1476 | sshbuf_reset(c->output); |
| 1477 | chan_write_failed(ssh, c); |
| 1478 | } |
| 1479 | |
| 1480 | /* reverse dynamic port forwarding */ |
| 1481 | static void |
| 1482 | channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c) |
| 1483 | { |
| 1484 | const u_char *p; |
| 1485 | u_int have, len; |
| 1486 | int r, ret; |
| 1487 | |
| 1488 | have = sshbuf_len(c->output); |
| 1489 | debug2("channel %d: pre_rdynamic: have %d", c->self, have); |
| 1490 | /* sshbuf_dump(c->output, stderr); */ |
| 1491 | /* EOF received */ |
| 1492 | if (c->flags & CHAN_EOF_RCVD) { |
| 1493 | if ((r = sshbuf_consume(c->output, have)) != 0) { |
| 1494 | fatal("%s: channel %d: consume: %s", |
| 1495 | __func__, c->self, ssh_err(r)); |
| 1496 | } |
| 1497 | rdynamic_close(ssh, c); |
| 1498 | return; |
| 1499 | } |
| 1500 | /* check if the fixed size part of the packet is in buffer. */ |
| 1501 | if (have < 3) |
| 1502 | return; |
| 1503 | /* try to guess the protocol */ |
| 1504 | p = sshbuf_ptr(c->output); |
| 1505 | switch (p[0]) { |
| 1506 | case 0x04: |
| 1507 | /* switch input/output for reverse forwarding */ |
| 1508 | ret = channel_decode_socks4(c, c->output, c->input); |
| 1509 | break; |
| 1510 | case 0x05: |
| 1511 | ret = channel_decode_socks5(c, c->output, c->input); |
| 1512 | break; |
| 1513 | default: |
| 1514 | ret = -1; |
| 1515 | break; |
| 1516 | } |
| 1517 | if (ret < 0) { |
| 1518 | rdynamic_close(ssh, c); |
| 1519 | } else if (ret == 0) { |
| 1520 | debug2("channel %d: pre_rdynamic: need more", c->self); |
| 1521 | /* send socks request to peer */ |
| 1522 | len = sshbuf_len(c->input); |
| 1523 | if (len > 0 && len < c->remote_window) { |
| 1524 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 1525 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 1526 | (r = sshpkt_put_stringb(ssh, c->input)) != 0 || |
| 1527 | (r = sshpkt_send(ssh)) != 0) { |
| 1528 | fatal("%s: channel %i: rdynamic: %s", __func__, |
| 1529 | c->self, ssh_err(r)); |
| 1530 | } |
| 1531 | if ((r = sshbuf_consume(c->input, len)) != 0) { |
| 1532 | fatal("%s: channel %d: consume: %s", |
| 1533 | __func__, c->self, ssh_err(r)); |
| 1534 | } |
| 1535 | c->remote_window -= len; |
| 1536 | } |
| 1537 | } else if (rdynamic_connect_finish(ssh, c) < 0) { |
| 1538 | /* the connect failed */ |
| 1539 | rdynamic_close(ssh, c); |
| 1540 | } |
| 1541 | } |
| 1542 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1543 | /* This is our fake X11 server socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1544 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1545 | channel_post_x11_listener(struct ssh *ssh, Channel *c, |
| 1546 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1547 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1548 | Channel *nc; |
Damien Miller | 163886f | 2008-07-14 11:28:58 +1000 | [diff] [blame] | 1549 | struct sockaddr_storage addr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1550 | int r, newsock, oerrno, remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1551 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1552 | char buf[16384], *remote_ipaddr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1553 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1554 | if (!FD_ISSET(c->sock, readset)) |
| 1555 | return; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1556 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1557 | debug("X11 connection requested."); |
| 1558 | addrlen = sizeof(addr); |
| 1559 | newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen); |
| 1560 | if (c->single_connection) { |
| 1561 | oerrno = errno; |
| 1562 | debug2("single_connection: closing X11 listener."); |
| 1563 | channel_close_fd(ssh, &c->sock); |
| 1564 | chan_mark_dead(ssh, c); |
| 1565 | errno = oerrno; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1566 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1567 | if (newsock < 0) { |
| 1568 | if (errno != EINTR && errno != EWOULDBLOCK && |
| 1569 | errno != ECONNABORTED) |
| 1570 | error("accept: %.100s", strerror(errno)); |
| 1571 | if (errno == EMFILE || errno == ENFILE) |
| 1572 | c->notbefore = monotime() + 1; |
| 1573 | return; |
| 1574 | } |
| 1575 | set_nodelay(newsock); |
| 1576 | remote_ipaddr = get_peer_ipaddr(newsock); |
| 1577 | remote_port = get_peer_port(newsock); |
| 1578 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 1579 | remote_ipaddr, remote_port); |
| 1580 | |
| 1581 | nc = channel_new(ssh, "accepted x11 socket", |
| 1582 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1583 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
| 1584 | open_preamble(ssh, __func__, nc, "x11"); |
| 1585 | if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0) { |
| 1586 | fatal("%s: channel %i: reply %s", __func__, |
| 1587 | c->self, ssh_err(r)); |
| 1588 | } |
| 1589 | if ((datafellows & SSH_BUG_X11FWD) != 0) |
| 1590 | debug2("channel %d: ssh2 x11 bug compat mode", nc->self); |
| 1591 | else if ((r = sshpkt_put_u32(ssh, remote_port)) != 0) { |
| 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 || |
| 1827 | (r = sshpkt_put_u32(ssh, SSH2_OPEN_CONNECT_FAILED)) |
| 1828 | != 0) |
| 1829 | fatal("%s: channel %i: failure: %s", __func__, |
| 1830 | c->self, ssh_err(r)); |
| 1831 | if ((datafellows & SSH_BUG_OPENFAILURE) == 0 && |
| 1832 | ((r = sshpkt_put_cstring(ssh, strerror(err))) != 0 || |
| 1833 | (r = sshpkt_put_cstring(ssh, "")) != 0)) |
| 1834 | fatal("%s: channel %i: failure: %s", __func__, |
| 1835 | c->self, ssh_err(r)); |
| 1836 | if ((r = sshpkt_send(ssh)) != 0) |
| 1837 | fatal("%s: channel %i: %s", __func__, c->self, |
| 1838 | ssh_err(r)); |
| 1839 | chan_mark_dead(ssh, c); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1840 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1841 | } |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1844 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1845 | channel_handle_rfd(struct ssh *ssh, Channel *c, |
| 1846 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1847 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1848 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1849 | ssize_t len; |
| 1850 | int r, force; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1851 | |
Damien Miller | 835284b | 2007-06-11 13:03:16 +1000 | [diff] [blame] | 1852 | force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset))) |
| 1855 | return 1; |
| 1856 | |
| 1857 | errno = 0; |
| 1858 | len = read(c->rfd, buf, sizeof(buf)); |
| 1859 | if (len < 0 && (errno == EINTR || |
| 1860 | ((errno == EAGAIN || errno == EWOULDBLOCK) && !force))) |
| 1861 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1862 | #ifndef PTY_ZEROREAD |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1863 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1864 | #else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1865 | if ((!c->isatty && len <= 0) || |
| 1866 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1867 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1868 | debug2("channel %d: read<=0 rfd %d len %zd", |
| 1869 | c->self, c->rfd, len); |
| 1870 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1871 | debug2("channel %d: not open", c->self); |
| 1872 | chan_mark_dead(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1873 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1874 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1875 | chan_read_failed(ssh, c); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1876 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1877 | return -1; |
| 1878 | } |
| 1879 | if (c->input_filter != NULL) { |
| 1880 | if (c->input_filter(ssh, c, buf, len) == -1) { |
| 1881 | debug2("channel %d: filter stops", c->self); |
| 1882 | chan_read_failed(ssh, c); |
| 1883 | } |
| 1884 | } else if (c->datagram) { |
| 1885 | if ((r = sshbuf_put_string(c->input, buf, len)) != 0) |
| 1886 | fatal("%s: channel %d: put datagram: %s", __func__, |
| 1887 | c->self, ssh_err(r)); |
| 1888 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 1889 | fatal("%s: channel %d: put data: %s", __func__, |
| 1890 | c->self, ssh_err(r)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1891 | } |
| 1892 | return 1; |
| 1893 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1894 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1895 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1896 | channel_handle_wfd(struct ssh *ssh, Channel *c, |
| 1897 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1898 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1899 | struct termios tio; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1900 | u_char *data = NULL, *buf; /* XXX const; need filter API change */ |
| 1901 | size_t dlen, olen = 0; |
| 1902 | int r, len; |
| 1903 | |
| 1904 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 1905 | sshbuf_len(c->output) == 0) |
| 1906 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1907 | |
| 1908 | /* Send buffered output data to the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1909 | olen = sshbuf_len(c->output); |
| 1910 | if (c->output_filter != NULL) { |
| 1911 | if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) { |
| 1912 | debug2("channel %d: filter stops", c->self); |
| 1913 | if (c->type != SSH_CHANNEL_OPEN) |
| 1914 | chan_mark_dead(ssh, c); |
| 1915 | else |
| 1916 | chan_write_failed(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1917 | return -1; |
| 1918 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1919 | } else if (c->datagram) { |
| 1920 | if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0) |
| 1921 | fatal("%s: channel %d: get datagram: %s", __func__, |
| 1922 | c->self, ssh_err(r)); |
djm@openbsd.org | 9145a73 | 2017-09-12 07:32:04 +0000 | [diff] [blame] | 1923 | buf = data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1924 | } else { |
| 1925 | buf = data = sshbuf_mutable_ptr(c->output); |
| 1926 | dlen = sshbuf_len(c->output); |
| 1927 | } |
| 1928 | |
| 1929 | if (c->datagram) { |
| 1930 | /* ignore truncated writes, datagrams might get lost */ |
djm@openbsd.org | c704f64 | 2017-09-24 09:50:01 +0000 | [diff] [blame] | 1931 | len = write(c->wfd, buf, dlen); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1932 | free(data); |
| 1933 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 1934 | errno == EWOULDBLOCK)) |
| 1935 | return 1; |
| 1936 | if (len <= 0) |
| 1937 | goto write_fail; |
| 1938 | goto out; |
| 1939 | } |
| 1940 | |
| 1941 | #ifdef _AIX |
| 1942 | /* XXX: Later AIX versions can't push as much data to tty */ |
| 1943 | if (c->wfd_isatty) |
| 1944 | dlen = MIN(dlen, 8*1024); |
Darren Tucker | 3980b63 | 2009-08-28 11:02:37 +1000 | [diff] [blame] | 1945 | #endif |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1946 | |
| 1947 | len = write(c->wfd, buf, dlen); |
| 1948 | if (len < 0 && |
| 1949 | (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
| 1950 | return 1; |
| 1951 | if (len <= 0) { |
| 1952 | write_fail: |
| 1953 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1954 | debug2("channel %d: not open", c->self); |
| 1955 | chan_mark_dead(ssh, c); |
| 1956 | return -1; |
| 1957 | } else { |
| 1958 | chan_write_failed(ssh, c); |
| 1959 | } |
| 1960 | return -1; |
| 1961 | } |
| 1962 | #ifndef BROKEN_TCGETATTR_ICANON |
| 1963 | if (c->isatty && dlen >= 1 && buf[0] != '\r') { |
| 1964 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 1965 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 1966 | /* |
| 1967 | * Simulate echo to reduce the impact of |
| 1968 | * traffic analysis. We need to match the |
| 1969 | * size of a SSH2_MSG_CHANNEL_DATA message |
| 1970 | * (4 byte channel id + buf) |
| 1971 | */ |
| 1972 | if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 || |
| 1973 | (r = sshpkt_send(ssh)) != 0) |
| 1974 | fatal("%s: channel %d: ignore: %s", |
| 1975 | __func__, c->self, ssh_err(r)); |
| 1976 | } |
| 1977 | } |
| 1978 | #endif /* BROKEN_TCGETATTR_ICANON */ |
| 1979 | if ((r = sshbuf_consume(c->output, len)) != 0) { |
| 1980 | fatal("%s: channel %d: consume: %s", |
| 1981 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1982 | } |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 1983 | out: |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1984 | c->local_consumed += olen - sshbuf_len(c->output); |
| 1985 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1986 | return 1; |
| 1987 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1988 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1989 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 1990 | channel_handle_efd_write(struct ssh *ssh, Channel *c, |
| 1991 | fd_set *readset, fd_set *writeset) |
| 1992 | { |
| 1993 | int r; |
| 1994 | ssize_t len; |
| 1995 | |
| 1996 | if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0) |
| 1997 | return 1; |
| 1998 | |
| 1999 | len = write(c->efd, sshbuf_ptr(c->extended), |
| 2000 | sshbuf_len(c->extended)); |
| 2001 | debug2("channel %d: written %zd to efd %d", c->self, len, c->efd); |
| 2002 | if (len < 0 && (errno == EINTR || errno == EAGAIN || |
| 2003 | errno == EWOULDBLOCK)) |
| 2004 | return 1; |
| 2005 | if (len <= 0) { |
| 2006 | debug2("channel %d: closing write-efd %d", c->self, c->efd); |
| 2007 | channel_close_fd(ssh, &c->efd); |
| 2008 | } else { |
| 2009 | if ((r = sshbuf_consume(c->extended, len)) != 0) { |
| 2010 | fatal("%s: channel %d: consume: %s", |
| 2011 | __func__, c->self, ssh_err(r)); |
| 2012 | } |
| 2013 | c->local_consumed += len; |
| 2014 | } |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
| 2018 | static int |
| 2019 | channel_handle_efd_read(struct ssh *ssh, Channel *c, |
| 2020 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2021 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 2022 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2023 | int r; |
| 2024 | ssize_t len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2025 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2026 | if (!c->detach_close && !FD_ISSET(c->efd, readset)) |
| 2027 | return 1; |
| 2028 | |
| 2029 | len = read(c->efd, buf, sizeof(buf)); |
| 2030 | debug2("channel %d: read %zd from efd %d", c->self, len, c->efd); |
| 2031 | if (len < 0 && (errno == EINTR || ((errno == EAGAIN || |
| 2032 | errno == EWOULDBLOCK) && !c->detach_close))) |
| 2033 | return 1; |
| 2034 | if (len <= 0) { |
| 2035 | debug2("channel %d: closing read-efd %d", |
| 2036 | c->self, c->efd); |
| 2037 | channel_close_fd(ssh, &c->efd); |
| 2038 | } else { |
| 2039 | if (c->extended_usage == CHAN_EXTENDED_IGNORE) { |
| 2040 | debug3("channel %d: discard efd", |
| 2041 | c->self); |
| 2042 | } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) { |
| 2043 | fatal("%s: channel %d: append: %s", |
| 2044 | __func__, c->self, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | return 1; |
| 2048 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 2049 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2050 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2051 | channel_handle_efd(struct ssh *ssh, Channel *c, |
| 2052 | fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2053 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2054 | if (c->efd == -1) |
| 2055 | return 1; |
| 2056 | |
| 2057 | /** XXX handle drain efd, too */ |
| 2058 | |
| 2059 | if (c->extended_usage == CHAN_EXTENDED_WRITE) |
| 2060 | return channel_handle_efd_write(ssh, c, readset, writeset); |
| 2061 | else if (c->extended_usage == CHAN_EXTENDED_READ || |
| 2062 | c->extended_usage == CHAN_EXTENDED_IGNORE) |
| 2063 | return channel_handle_efd_read(ssh, c, readset, writeset); |
| 2064 | |
| 2065 | return 1; |
| 2066 | } |
| 2067 | |
| 2068 | static int |
| 2069 | channel_check_window(struct ssh *ssh, Channel *c) |
| 2070 | { |
| 2071 | int r; |
| 2072 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 2073 | if (c->type == SSH_CHANNEL_OPEN && |
| 2074 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Darren Tucker | ae09cb8 | 2007-06-25 19:04:46 +1000 | [diff] [blame] | 2075 | ((c->local_window_max - c->local_window > |
Damien Miller | 3191a8e | 2007-06-11 18:33:15 +1000 | [diff] [blame] | 2076 | c->local_maxpacket*3) || |
| 2077 | c->local_window < c->local_window_max/2) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2078 | c->local_consumed > 0) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2079 | if (!c->have_remote_id) |
| 2080 | fatal(":%s: channel %d: no remote id", |
| 2081 | __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2082 | if ((r = sshpkt_start(ssh, |
| 2083 | SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || |
| 2084 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2085 | (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 || |
| 2086 | (r = sshpkt_send(ssh)) != 0) { |
| 2087 | fatal("%s: channel %i: %s", __func__, |
| 2088 | c->self, ssh_err(r)); |
| 2089 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2090 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2091 | c->self, c->local_window, |
| 2092 | c->local_consumed); |
| 2093 | c->local_window += c->local_consumed; |
| 2094 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2095 | } |
| 2096 | return 1; |
| 2097 | } |
| 2098 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2099 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2100 | channel_post_open(struct ssh *ssh, Channel *c, |
| 2101 | fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2102 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2103 | channel_handle_rfd(ssh, c, readset, writeset); |
| 2104 | channel_handle_wfd(ssh, c, readset, writeset); |
| 2105 | channel_handle_efd(ssh, c, readset, writeset); |
| 2106 | channel_check_window(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2107 | } |
| 2108 | |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2109 | static u_int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2110 | read_mux(struct ssh *ssh, Channel *c, u_int need) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2111 | { |
| 2112 | char buf[CHAN_RBUF]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2113 | ssize_t len; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2114 | u_int rlen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2115 | int r; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2116 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2117 | if (sshbuf_len(c->input) < need) { |
| 2118 | rlen = need - sshbuf_len(c->input); |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 2119 | len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF)); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2120 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2121 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2122 | if (len <= 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2123 | debug2("channel %d: ctl read<=0 rfd %d len %zd", |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2124 | c->self, c->rfd, len); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2125 | chan_read_failed(ssh, c); |
naddy@openbsd.org | 603ba41 | 2016-02-05 13:28:19 +0000 | [diff] [blame] | 2126 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2127 | } else if ((r = sshbuf_put(c->input, buf, len)) != 0) { |
| 2128 | fatal("%s: channel %d: append: %s", |
| 2129 | __func__, c->self, ssh_err(r)); |
| 2130 | } |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2131 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2132 | return sshbuf_len(c->input); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2136 | channel_post_mux_client_read(struct ssh *ssh, Channel *c, |
| 2137 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2138 | { |
| 2139 | u_int need; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2140 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2141 | if (c->rfd == -1 || !FD_ISSET(c->rfd, readset)) |
| 2142 | return; |
| 2143 | if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN) |
| 2144 | return; |
| 2145 | if (c->mux_pause) |
| 2146 | return; |
| 2147 | |
| 2148 | /* |
| 2149 | * Don't not read past the precise end of packets to |
| 2150 | * avoid disrupting fd passing. |
| 2151 | */ |
| 2152 | if (read_mux(ssh, c, 4) < 4) /* read header */ |
| 2153 | return; |
| 2154 | /* XXX sshbuf_peek_u32 */ |
| 2155 | need = PEEK_U32(sshbuf_ptr(c->input)); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2156 | #define CHANNEL_MUX_MAX_PACKET (256 * 1024) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2157 | if (need > CHANNEL_MUX_MAX_PACKET) { |
| 2158 | debug2("channel %d: packet too big %u > %u", |
| 2159 | c->self, CHANNEL_MUX_MAX_PACKET, need); |
| 2160 | chan_rcvd_oclose(ssh, c); |
| 2161 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2162 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2163 | if (read_mux(ssh, c, need + 4) < need + 4) /* read body */ |
| 2164 | return; |
| 2165 | if (c->mux_rcb(ssh, c) != 0) { |
| 2166 | debug("channel %d: mux_rcb failed", c->self); |
| 2167 | chan_mark_dead(ssh, c); |
| 2168 | return; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2173 | channel_post_mux_client_write(struct ssh *ssh, Channel *c, |
| 2174 | fd_set *readset, fd_set *writeset) |
| 2175 | { |
| 2176 | ssize_t len; |
| 2177 | int r; |
| 2178 | |
| 2179 | if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) || |
| 2180 | sshbuf_len(c->output) == 0) |
| 2181 | return; |
| 2182 | |
| 2183 | len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output)); |
| 2184 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 2185 | return; |
| 2186 | if (len <= 0) { |
| 2187 | chan_mark_dead(ssh, c); |
| 2188 | return; |
| 2189 | } |
| 2190 | if ((r = sshbuf_consume(c->output, len)) != 0) |
| 2191 | fatal("%s: channel %d: consume: %s", __func__, |
| 2192 | c->self, ssh_err(r)); |
| 2193 | } |
| 2194 | |
| 2195 | static void |
| 2196 | channel_post_mux_client(struct ssh *ssh, Channel *c, |
| 2197 | fd_set *readset, fd_set *writeset) |
| 2198 | { |
| 2199 | channel_post_mux_client_read(ssh, c, readset, writeset); |
| 2200 | channel_post_mux_client_write(ssh, c, readset, writeset); |
| 2201 | } |
| 2202 | |
| 2203 | static void |
| 2204 | channel_post_mux_listener(struct ssh *ssh, Channel *c, |
| 2205 | fd_set *readset, fd_set *writeset) |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2206 | { |
| 2207 | Channel *nc; |
| 2208 | struct sockaddr_storage addr; |
| 2209 | socklen_t addrlen; |
| 2210 | int newsock; |
| 2211 | uid_t euid; |
| 2212 | gid_t egid; |
| 2213 | |
| 2214 | if (!FD_ISSET(c->sock, readset)) |
| 2215 | return; |
| 2216 | |
| 2217 | debug("multiplexing control connection"); |
| 2218 | |
| 2219 | /* |
| 2220 | * Accept connection on control socket |
| 2221 | */ |
| 2222 | memset(&addr, 0, sizeof(addr)); |
| 2223 | addrlen = sizeof(addr); |
| 2224 | if ((newsock = accept(c->sock, (struct sockaddr*)&addr, |
| 2225 | &addrlen)) == -1) { |
| 2226 | error("%s accept: %s", __func__, strerror(errno)); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2227 | if (errno == EMFILE || errno == ENFILE) |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2228 | c->notbefore = monotime() + 1; |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2229 | return; |
| 2230 | } |
| 2231 | |
| 2232 | if (getpeereid(newsock, &euid, &egid) < 0) { |
| 2233 | error("%s getpeereid failed: %s", __func__, |
| 2234 | strerror(errno)); |
| 2235 | close(newsock); |
| 2236 | return; |
| 2237 | } |
| 2238 | if ((euid != 0) && (getuid() != euid)) { |
| 2239 | error("multiplex uid mismatch: peer euid %u != uid %u", |
| 2240 | (u_int)euid, (u_int)getuid()); |
| 2241 | close(newsock); |
| 2242 | return; |
| 2243 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2244 | nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT, |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2245 | newsock, newsock, -1, c->local_window_max, |
| 2246 | c->local_maxpacket, 0, "mux-control", 1); |
| 2247 | nc->mux_rcb = c->mux_rcb; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2248 | 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] | 2249 | /* establish state */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2250 | nc->mux_rcb(ssh, nc); |
Damien Miller | e1537f9 | 2010-01-26 13:26:22 +1100 | [diff] [blame] | 2251 | /* mux state transitions must not elicit protocol messages */ |
| 2252 | nc->flags |= CHAN_LOCAL; |
| 2253 | } |
| 2254 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2255 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2256 | channel_handler_init(struct ssh_channels *sc) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2257 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2258 | chan_fn **pre, **post; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 2259 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2260 | if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL || |
| 2261 | (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL) |
| 2262 | fatal("%s: allocation failed", __func__); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2263 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2264 | pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
| 2265 | pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
| 2266 | pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 2267 | pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
| 2268 | pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener; |
| 2269 | pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener; |
| 2270 | pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 2271 | pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 2272 | pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
| 2273 | pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2274 | pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2275 | pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener; |
| 2276 | pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client; |
| 2277 | |
| 2278 | post[SSH_CHANNEL_OPEN] = &channel_post_open; |
| 2279 | post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 2280 | post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
| 2281 | post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener; |
| 2282 | post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener; |
| 2283 | post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 2284 | post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 2285 | post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
| 2286 | post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2287 | post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2288 | post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener; |
| 2289 | post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client; |
| 2290 | |
| 2291 | sc->channel_pre = pre; |
| 2292 | sc->channel_post = post; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2293 | } |
| 2294 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2295 | /* gc dead channels */ |
| 2296 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2297 | channel_garbage_collect(struct ssh *ssh, Channel *c) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2298 | { |
| 2299 | if (c == NULL) |
| 2300 | return; |
| 2301 | if (c->detach_user != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2302 | if (!chan_is_dead(ssh, c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2303 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2304 | debug2("channel %d: gc: notify user", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2305 | c->detach_user(ssh, c->self, NULL); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2306 | /* if we still have a callback */ |
| 2307 | if (c->detach_user != NULL) |
| 2308 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2309 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2310 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2311 | if (!chan_is_dead(ssh, c, 1)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2312 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2313 | debug2("channel %d: garbage collecting", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2314 | channel_free(ssh, c); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 2315 | } |
| 2316 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2317 | enum channel_table { CHAN_PRE, CHAN_POST }; |
| 2318 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2319 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2320 | channel_handler(struct ssh *ssh, int table, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2321 | fd_set *readset, fd_set *writeset, time_t *unpause_secs) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2322 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2323 | struct ssh_channels *sc = ssh->chanctxt; |
| 2324 | chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2325 | u_int i, oalloc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2326 | Channel *c; |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2327 | time_t now; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2328 | |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 2329 | now = monotime(); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2330 | if (unpause_secs != NULL) |
| 2331 | *unpause_secs = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2332 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2333 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2334 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2335 | continue; |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2336 | if (c->delayed) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2337 | if (table == CHAN_PRE) |
Darren Tucker | 876045b | 2010-01-08 17:08:00 +1100 | [diff] [blame] | 2338 | c->delayed = 0; |
| 2339 | else |
| 2340 | continue; |
| 2341 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2342 | if (ftab[c->type] != NULL) { |
| 2343 | /* |
| 2344 | * Run handlers that are not paused. |
| 2345 | */ |
| 2346 | if (c->notbefore <= now) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2347 | (*ftab[c->type])(ssh, c, readset, writeset); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2348 | else if (unpause_secs != NULL) { |
| 2349 | /* |
| 2350 | * Collect the time that the earliest |
| 2351 | * channel comes off pause. |
| 2352 | */ |
| 2353 | debug3("%s: chan %d: skip for %d more seconds", |
| 2354 | __func__, c->self, |
| 2355 | (int)(c->notbefore - now)); |
| 2356 | if (*unpause_secs == 0 || |
| 2357 | (c->notbefore - now) < *unpause_secs) |
| 2358 | *unpause_secs = c->notbefore - now; |
| 2359 | } |
| 2360 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2361 | channel_garbage_collect(ssh, c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2362 | } |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2363 | if (unpause_secs != NULL && *unpause_secs != 0) |
| 2364 | debug3("%s: first channel unpauses in %d seconds", |
| 2365 | __func__, (int)*unpause_secs); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2366 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2367 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2368 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2369 | * Create sockets before allocating the select bitmasks. |
| 2370 | * This is necessary for things that need to happen after reading |
| 2371 | * the network-input but before channel_prepare_select(). |
| 2372 | */ |
| 2373 | static void |
| 2374 | channel_before_prepare_select(struct ssh *ssh) |
| 2375 | { |
| 2376 | struct ssh_channels *sc = ssh->chanctxt; |
| 2377 | Channel *c; |
| 2378 | u_int i, oalloc; |
| 2379 | |
| 2380 | for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) { |
| 2381 | c = sc->channels[i]; |
| 2382 | if (c == NULL) |
| 2383 | continue; |
| 2384 | if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN) |
| 2385 | channel_before_prepare_select_rdynamic(ssh, c); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2390 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 2391 | * select bitmasks. |
| 2392 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2393 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2394 | channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp, |
| 2395 | int *maxfdp, u_int *nallocp, time_t *minwait_secs) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2396 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2397 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2398 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2399 | channel_before_prepare_select(ssh); /* might update channel_max_fd */ |
| 2400 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2401 | n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2402 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2403 | nfdset = howmany(n+1, NFDBITS); |
| 2404 | /* Explicitly test here, because xrealloc isn't always called */ |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 2405 | if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask)) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 2406 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 2407 | sz = nfdset * sizeof(fd_mask); |
| 2408 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2409 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 2410 | if (*readsetp == NULL || sz > *nallocp) { |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 2411 | *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask)); |
| 2412 | *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2413 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2414 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 2415 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2416 | memset(*readsetp, 0, sz); |
| 2417 | memset(*writesetp, 0, sz); |
| 2418 | |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2419 | if (!ssh_packet_is_rekeying(ssh)) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2420 | channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp, |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 2421 | minwait_secs); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2422 | } |
| 2423 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2424 | /* |
| 2425 | * After select, perform any appropriate operations for channels which have |
| 2426 | * events pending. |
| 2427 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2428 | void |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 2429 | channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2430 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2431 | channel_handler(ssh, CHAN_POST, readset, writeset, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2432 | } |
| 2433 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2434 | /* |
| 2435 | * Enqueue data for channels with open or draining c->input. |
| 2436 | */ |
| 2437 | static void |
| 2438 | channel_output_poll_input_open(struct ssh *ssh, Channel *c) |
| 2439 | { |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2440 | size_t len, plen; |
| 2441 | const u_char *pkt; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2442 | int r; |
| 2443 | |
| 2444 | if ((len = sshbuf_len(c->input)) == 0) { |
| 2445 | if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 2446 | /* |
| 2447 | * input-buffer is empty and read-socket shutdown: |
| 2448 | * tell peer, that we will not send more data: |
| 2449 | * send IEOF. |
| 2450 | * hack for extended data: delay EOF if EFD still |
| 2451 | * in use. |
| 2452 | */ |
| 2453 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
| 2454 | debug2("channel %d: " |
| 2455 | "ibuf_empty delayed efd %d/(%zu)", |
| 2456 | c->self, c->efd, sshbuf_len(c->extended)); |
| 2457 | else |
| 2458 | chan_ibuf_empty(ssh, c); |
| 2459 | } |
| 2460 | return; |
| 2461 | } |
| 2462 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2463 | if (!c->have_remote_id) |
| 2464 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
| 2465 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2466 | if (c->datagram) { |
| 2467 | /* Check datagram will fit; drop if not */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2468 | if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0) |
| 2469 | fatal("%s: channel %d: get datagram: %s", __func__, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2470 | c->self, ssh_err(r)); |
| 2471 | /* |
| 2472 | * XXX this does tail-drop on the datagram queue which is |
| 2473 | * usually suboptimal compared to head-drop. Better to have |
| 2474 | * backpressure at read time? (i.e. read + discard) |
| 2475 | */ |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2476 | if (plen > c->remote_window || plen > c->remote_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2477 | debug("channel %d: datagram too big", c->self); |
| 2478 | return; |
| 2479 | } |
| 2480 | /* Enqueue it */ |
| 2481 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2482 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2483 | (r = sshpkt_put_string(ssh, pkt, plen)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2484 | (r = sshpkt_send(ssh)) != 0) { |
| 2485 | fatal("%s: channel %i: datagram: %s", __func__, |
| 2486 | c->self, ssh_err(r)); |
| 2487 | } |
djm@openbsd.org | 55486f5 | 2017-09-23 22:04:07 +0000 | [diff] [blame] | 2488 | c->remote_window -= plen; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2489 | return; |
| 2490 | } |
| 2491 | |
| 2492 | /* Enqueue packet for buffered data. */ |
| 2493 | if (len > c->remote_window) |
| 2494 | len = c->remote_window; |
| 2495 | if (len > c->remote_maxpacket) |
| 2496 | len = c->remote_maxpacket; |
| 2497 | if (len == 0) |
| 2498 | return; |
| 2499 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 || |
| 2500 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2501 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 || |
| 2502 | (r = sshpkt_send(ssh)) != 0) { |
| 2503 | fatal("%s: channel %i: data: %s", __func__, |
| 2504 | c->self, ssh_err(r)); |
| 2505 | } |
| 2506 | if ((r = sshbuf_consume(c->input, len)) != 0) |
| 2507 | fatal("%s: channel %i: consume: %s", __func__, |
| 2508 | c->self, ssh_err(r)); |
| 2509 | c->remote_window -= len; |
| 2510 | } |
| 2511 | |
| 2512 | /* |
| 2513 | * Enqueue data for channels with open c->extended in read mode. |
| 2514 | */ |
| 2515 | static void |
| 2516 | channel_output_poll_extended_read(struct ssh *ssh, Channel *c) |
| 2517 | { |
| 2518 | size_t len; |
| 2519 | int r; |
| 2520 | |
| 2521 | if ((len = sshbuf_len(c->extended)) == 0) |
| 2522 | return; |
| 2523 | |
| 2524 | debug2("channel %d: rwin %u elen %zu euse %d", c->self, |
| 2525 | c->remote_window, sshbuf_len(c->extended), c->extended_usage); |
| 2526 | if (len > c->remote_window) |
| 2527 | len = c->remote_window; |
| 2528 | if (len > c->remote_maxpacket) |
| 2529 | len = c->remote_maxpacket; |
| 2530 | if (len == 0) |
| 2531 | return; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2532 | if (!c->have_remote_id) |
| 2533 | fatal(":%s: channel %d: no remote id", __func__, c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2534 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || |
| 2535 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 2536 | (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || |
| 2537 | (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 || |
| 2538 | (r = sshpkt_send(ssh)) != 0) { |
| 2539 | fatal("%s: channel %i: data: %s", __func__, |
| 2540 | c->self, ssh_err(r)); |
| 2541 | } |
| 2542 | if ((r = sshbuf_consume(c->extended, len)) != 0) |
| 2543 | fatal("%s: channel %i: consume: %s", __func__, |
| 2544 | c->self, ssh_err(r)); |
| 2545 | c->remote_window -= len; |
| 2546 | debug2("channel %d: sent ext data %zu", c->self, len); |
| 2547 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2548 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 2549 | /* 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] | 2550 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2551 | channel_output_poll(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2552 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2553 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2554 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2555 | u_int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2556 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2557 | for (i = 0; i < sc->channels_alloc; i++) { |
| 2558 | c = sc->channels[i]; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2559 | if (c == NULL) |
| 2560 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2561 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2562 | /* |
| 2563 | * We are only interested in channels that can have buffered |
| 2564 | * incoming data. |
| 2565 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2566 | if (c->type != SSH_CHANNEL_OPEN) |
| 2567 | continue; |
| 2568 | if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 2569 | /* XXX is this true? */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2570 | debug3("channel %d: will not send data after close", |
| 2571 | c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2572 | continue; |
| 2573 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2574 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2575 | /* Get the amount of buffered data for this channel. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2576 | if (c->istate == CHAN_INPUT_OPEN || |
| 2577 | c->istate == CHAN_INPUT_WAIT_DRAIN) |
| 2578 | channel_output_poll_input_open(ssh, c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2579 | /* Send extended data, i.e. stderr */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2580 | if (!(c->flags & CHAN_EOF_SENT) && |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2581 | c->extended_usage == CHAN_EXTENDED_READ) |
| 2582 | channel_output_poll_extended_read(ssh, c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2583 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2584 | } |
| 2585 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2586 | /* -- mux proxy support */ |
| 2587 | |
| 2588 | /* |
| 2589 | * When multiplexing channel messages for mux clients we have to deal |
| 2590 | * with downstream messages from the mux client and upstream messages |
| 2591 | * from the ssh server: |
| 2592 | * 1) Handling downstream messages is straightforward and happens |
| 2593 | * in channel_proxy_downstream(): |
| 2594 | * - We forward all messages (mostly) unmodified to the server. |
| 2595 | * - However, in order to route messages from upstream to the correct |
| 2596 | * downstream client, we have to replace the channel IDs used by the |
| 2597 | * mux clients with a unique channel ID because the mux clients might |
| 2598 | * use conflicting channel IDs. |
| 2599 | * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and |
| 2600 | * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local |
| 2601 | * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID |
| 2602 | * with the newly allocated channel ID. |
| 2603 | * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY |
| 2604 | * channels and procesed by channel_proxy_upstream(). The local channel ID |
| 2605 | * is then translated back to the original mux client ID. |
| 2606 | * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE |
| 2607 | * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels. |
| 2608 | * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the |
| 2609 | * downstream mux client are removed. |
| 2610 | * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server |
| 2611 | * requires more work, because they are not addressed to a specific |
| 2612 | * channel. E.g. client_request_forwarded_tcpip() needs to figure |
| 2613 | * out whether the request is addressed to the local client or a |
| 2614 | * specific downstream client based on the listen-address/port. |
| 2615 | * 6) Agent and X11-Forwarding have a similar problem and are currenly |
| 2616 | * not supported as the matching session/channel cannot be identified |
| 2617 | * easily. |
| 2618 | */ |
| 2619 | |
| 2620 | /* |
| 2621 | * receive packets from downstream mux clients: |
| 2622 | * channel callback fired on read from mux client, creates |
| 2623 | * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs |
| 2624 | * on channel creation. |
| 2625 | */ |
| 2626 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2627 | channel_proxy_downstream(struct ssh *ssh, Channel *downstream) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2628 | { |
| 2629 | Channel *c = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2630 | struct sshbuf *original = NULL, *modified = NULL; |
| 2631 | const u_char *cp; |
| 2632 | char *ctype = NULL, *listen_host = NULL; |
| 2633 | u_char type; |
| 2634 | size_t have; |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2635 | int ret = -1, r; |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2636 | u_int id, remote_id, listen_port; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2637 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2638 | /* sshbuf_dump(downstream->input, stderr); */ |
| 2639 | if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2640 | != 0) { |
| 2641 | error("%s: malformed message: %s", __func__, ssh_err(r)); |
| 2642 | return -1; |
| 2643 | } |
| 2644 | if (have < 2) { |
| 2645 | error("%s: short message", __func__); |
| 2646 | return -1; |
| 2647 | } |
| 2648 | type = cp[1]; |
| 2649 | /* skip padlen + type */ |
| 2650 | cp += 2; |
| 2651 | have -= 2; |
| 2652 | if (ssh_packet_log_type(type)) |
| 2653 | debug3("%s: channel %u: down->up: type %u", __func__, |
| 2654 | downstream->self, type); |
| 2655 | |
| 2656 | switch (type) { |
| 2657 | case SSH2_MSG_CHANNEL_OPEN: |
| 2658 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2659 | (modified = sshbuf_new()) == NULL) { |
| 2660 | error("%s: alloc", __func__); |
| 2661 | goto out; |
| 2662 | } |
| 2663 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 || |
| 2664 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2665 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2666 | goto out; |
| 2667 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2668 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2669 | -1, -1, -1, 0, 0, 0, ctype, 1); |
| 2670 | c->mux_ctx = downstream; /* point to mux client */ |
| 2671 | c->mux_downstream_id = id; /* original downstream id */ |
| 2672 | if ((r = sshbuf_put_cstring(modified, ctype)) != 0 || |
| 2673 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2674 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2675 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2676 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2677 | goto out; |
| 2678 | } |
| 2679 | break; |
| 2680 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2681 | /* |
| 2682 | * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we |
| 2683 | * need to parse 'remote_id' instead of 'ctype'. |
| 2684 | */ |
| 2685 | if ((original = sshbuf_from(cp, have)) == NULL || |
| 2686 | (modified = sshbuf_new()) == NULL) { |
| 2687 | error("%s: alloc", __func__); |
| 2688 | goto out; |
| 2689 | } |
| 2690 | if ((r = sshbuf_get_u32(original, &remote_id)) != 0 || |
| 2691 | (r = sshbuf_get_u32(original, &id)) != 0) { |
| 2692 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2693 | goto out; |
| 2694 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2695 | c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY, |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2696 | -1, -1, -1, 0, 0, 0, "mux-down-connect", 1); |
| 2697 | c->mux_ctx = downstream; /* point to mux client */ |
| 2698 | c->mux_downstream_id = id; |
| 2699 | c->remote_id = remote_id; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2700 | c->have_remote_id = 1; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2701 | if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || |
| 2702 | (r = sshbuf_put_u32(modified, c->self)) != 0 || |
| 2703 | (r = sshbuf_putb(modified, original)) != 0) { |
| 2704 | error("%s: compose error %s", __func__, ssh_err(r)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2705 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2706 | goto out; |
| 2707 | } |
| 2708 | break; |
| 2709 | case SSH2_MSG_GLOBAL_REQUEST: |
| 2710 | if ((original = sshbuf_from(cp, have)) == NULL) { |
| 2711 | error("%s: alloc", __func__); |
| 2712 | goto out; |
| 2713 | } |
| 2714 | if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) { |
| 2715 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2716 | goto out; |
| 2717 | } |
| 2718 | if (strcmp(ctype, "tcpip-forward") != 0) { |
| 2719 | error("%s: unsupported request %s", __func__, ctype); |
| 2720 | goto out; |
| 2721 | } |
| 2722 | if ((r = sshbuf_get_u8(original, NULL)) != 0 || |
| 2723 | (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 || |
| 2724 | (r = sshbuf_get_u32(original, &listen_port)) != 0) { |
| 2725 | error("%s: parse error %s", __func__, ssh_err(r)); |
| 2726 | goto out; |
| 2727 | } |
djm@openbsd.org | 2f78a2a | 2016-09-30 20:24:46 +0000 | [diff] [blame] | 2728 | if (listen_port > 65535) { |
| 2729 | error("%s: tcpip-forward for %s: bad port %u", |
| 2730 | __func__, listen_host, listen_port); |
| 2731 | goto out; |
| 2732 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2733 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | 4ec0bb9 | 2017-09-12 07:55:48 +0000 | [diff] [blame] | 2734 | fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2735 | listen_host, NULL, (int)listen_port, downstream); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2736 | listen_host = NULL; |
| 2737 | break; |
| 2738 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2739 | if (have < 4) |
| 2740 | break; |
| 2741 | remote_id = PEEK_U32(cp); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2742 | if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2743 | if (c->flags & CHAN_CLOSE_RCVD) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2744 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2745 | else |
| 2746 | c->flags |= CHAN_CLOSE_SENT; |
| 2747 | } |
| 2748 | break; |
| 2749 | } |
| 2750 | if (modified) { |
| 2751 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2752 | (r = sshpkt_putb(ssh, modified)) != 0 || |
| 2753 | (r = sshpkt_send(ssh)) != 0) { |
| 2754 | error("%s: send %s", __func__, ssh_err(r)); |
| 2755 | goto out; |
| 2756 | } |
| 2757 | } else { |
| 2758 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 2759 | (r = sshpkt_put(ssh, cp, have)) != 0 || |
| 2760 | (r = sshpkt_send(ssh)) != 0) { |
| 2761 | error("%s: send %s", __func__, ssh_err(r)); |
| 2762 | goto out; |
| 2763 | } |
| 2764 | } |
| 2765 | ret = 0; |
| 2766 | out: |
| 2767 | free(ctype); |
| 2768 | free(listen_host); |
| 2769 | sshbuf_free(original); |
| 2770 | sshbuf_free(modified); |
| 2771 | return ret; |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | * receive packets from upstream server and de-multiplex packets |
| 2776 | * to correct downstream: |
| 2777 | * implemented as a helper for channel input handlers, |
| 2778 | * replaces local (proxy) channel ID with downstream channel ID. |
| 2779 | */ |
| 2780 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2781 | 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] | 2782 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2783 | struct sshbuf *b = NULL; |
| 2784 | Channel *downstream; |
| 2785 | const u_char *cp = NULL; |
| 2786 | size_t len; |
| 2787 | int r; |
| 2788 | |
| 2789 | /* |
| 2790 | * When receiving packets from the peer we need to check whether we |
| 2791 | * need to forward the packets to the mux client. In this case we |
| 2792 | * restore the orignal channel id and keep track of CLOSE messages, |
| 2793 | * so we can cleanup the channel. |
| 2794 | */ |
| 2795 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2796 | return 0; |
| 2797 | if ((downstream = c->mux_ctx) == NULL) |
| 2798 | return 0; |
| 2799 | switch (type) { |
| 2800 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2801 | case SSH2_MSG_CHANNEL_DATA: |
| 2802 | case SSH2_MSG_CHANNEL_EOF: |
| 2803 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2804 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2805 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2806 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2807 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2808 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2809 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2810 | break; |
| 2811 | default: |
| 2812 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2813 | c->self, type); |
| 2814 | return 0; |
| 2815 | } |
| 2816 | if ((b = sshbuf_new()) == NULL) { |
| 2817 | error("%s: alloc reply", __func__); |
| 2818 | goto out; |
| 2819 | } |
| 2820 | /* get remaining payload (after id) */ |
| 2821 | cp = sshpkt_ptr(ssh, &len); |
| 2822 | if (cp == NULL) { |
| 2823 | error("%s: no packet", __func__); |
| 2824 | goto out; |
| 2825 | } |
| 2826 | /* translate id and send to muxclient */ |
| 2827 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2828 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2829 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2830 | (r = sshbuf_put(b, cp, len)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2831 | (r = sshbuf_put_stringb(downstream->output, b)) != 0) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2832 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2833 | goto out; |
| 2834 | } |
| 2835 | /* sshbuf_dump(b, stderr); */ |
| 2836 | if (ssh_packet_log_type(type)) |
| 2837 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2838 | type); |
| 2839 | out: |
| 2840 | /* update state */ |
| 2841 | switch (type) { |
| 2842 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2843 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2844 | if (cp && len > 4) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2845 | c->remote_id = PEEK_U32(cp); |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 2846 | c->have_remote_id = 1; |
| 2847 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2848 | break; |
| 2849 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2850 | if (c->flags & CHAN_CLOSE_SENT) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2851 | channel_free(ssh, c); |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2852 | else |
| 2853 | c->flags |= CHAN_CLOSE_RCVD; |
| 2854 | break; |
| 2855 | } |
| 2856 | sshbuf_free(b); |
| 2857 | return 1; |
| 2858 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2859 | |
| 2860 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2861 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2862 | /* Parse a channel ID from the current packet */ |
| 2863 | static int |
| 2864 | channel_parse_id(struct ssh *ssh, const char *where, const char *what) |
| 2865 | { |
| 2866 | u_int32_t id; |
| 2867 | int r; |
| 2868 | |
| 2869 | if ((r = sshpkt_get_u32(ssh, &id)) != 0) { |
| 2870 | error("%s: parse id: %s", where, ssh_err(r)); |
| 2871 | ssh_packet_disconnect(ssh, "Invalid %s message", what); |
| 2872 | } |
| 2873 | if (id > INT_MAX) { |
| 2874 | error("%s: bad channel id %u: %s", where, id, ssh_err(r)); |
| 2875 | ssh_packet_disconnect(ssh, "Invalid %s channel id", what); |
| 2876 | } |
| 2877 | return (int)id; |
| 2878 | } |
| 2879 | |
| 2880 | /* Lookup a channel from an ID in the current packet */ |
| 2881 | static Channel * |
| 2882 | channel_from_packet_id(struct ssh *ssh, const char *where, const char *what) |
| 2883 | { |
| 2884 | int id = channel_parse_id(ssh, where, what); |
| 2885 | Channel *c; |
| 2886 | |
| 2887 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 2888 | ssh_packet_disconnect(ssh, |
| 2889 | "%s packet referred to nonexistent channel %d", what, id); |
| 2890 | } |
| 2891 | return c; |
| 2892 | } |
| 2893 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2894 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2895 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2896 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2897 | const u_char *data; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2898 | size_t data_len, win_len; |
| 2899 | Channel *c = channel_from_packet_id(ssh, __func__, "data"); |
| 2900 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2901 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2902 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2903 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2904 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2905 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2906 | if (c->type != SSH_CHANNEL_OPEN && |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 2907 | c->type != SSH_CHANNEL_RDYNAMIC_OPEN && |
| 2908 | c->type != SSH_CHANNEL_RDYNAMIC_FINISH && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2909 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2910 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2911 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2912 | /* Get the data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2913 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) |
| 2914 | fatal("%s: channel %d: get data: %s", __func__, |
| 2915 | c->self, ssh_err(r)); |
| 2916 | ssh_packet_check_eom(ssh); |
| 2917 | |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2918 | win_len = data_len; |
| 2919 | if (c->datagram) |
| 2920 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2921 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2922 | /* |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2923 | * The sending side reduces its window as it sends data, so we |
| 2924 | * must 'fake' consumption of the data in order to ensure that window |
| 2925 | * updates are sent back. Otherwise the connection might deadlock. |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2926 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2927 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 2928 | c->local_window -= win_len; |
| 2929 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2930 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2931 | } |
| 2932 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2933 | if (win_len > c->local_maxpacket) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2934 | logit("channel %d: rcvd big packet %zu, maxpack %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2935 | c->self, win_len, c->local_maxpacket); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2936 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2937 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2938 | if (win_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2939 | logit("channel %d: rcvd too much data %zu, win %u", |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2940 | c->self, win_len, c->local_window); |
| 2941 | return 0; |
| 2942 | } |
| 2943 | c->local_window -= win_len; |
| 2944 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2945 | if (c->datagram) { |
| 2946 | if ((r = sshbuf_put_string(c->output, data, data_len)) != 0) |
| 2947 | fatal("%s: channel %d: append datagram: %s", |
| 2948 | __func__, c->self, ssh_err(r)); |
| 2949 | } else if ((r = sshbuf_put(c->output, data, data_len)) != 0) |
| 2950 | fatal("%s: channel %d: append data: %s", |
| 2951 | __func__, c->self, ssh_err(r)); |
| 2952 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2953 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2954 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2955 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2956 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2957 | 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] | 2958 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2959 | const u_char *data; |
| 2960 | size_t data_len; |
| 2961 | u_int32_t tcode; |
| 2962 | Channel *c = channel_from_packet_id(ssh, __func__, "extended data"); |
| 2963 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2964 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2965 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2966 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2967 | if (c->type != SSH_CHANNEL_OPEN) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2968 | logit("channel %d: ext data for non open", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2969 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2970 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2971 | if (c->flags & CHAN_EOF_RCVD) { |
| 2972 | if (datafellows & SSH_BUG_EXTEOF) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2973 | debug("channel %d: accepting ext data after eof", |
| 2974 | c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2975 | else |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2976 | ssh_packet_disconnect(ssh, "Received extended_data " |
| 2977 | "after EOF on channel %d.", c->self); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2978 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2979 | |
| 2980 | if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) { |
| 2981 | error("%s: parse tcode: %s", __func__, ssh_err(r)); |
| 2982 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 2983 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2984 | if (c->efd == -1 || |
| 2985 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 2986 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2987 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2988 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2989 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2990 | if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) { |
| 2991 | error("%s: parse data: %s", __func__, ssh_err(r)); |
| 2992 | ssh_packet_disconnect(ssh, "Invalid extended_data message"); |
| 2993 | } |
| 2994 | ssh_packet_check_eom(ssh); |
| 2995 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2996 | if (data_len > c->local_window) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 2997 | logit("channel %d: rcvd too much extended_data %zu, win %u", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2998 | c->self, data_len, c->local_window); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2999 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3000 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3001 | debug2("channel %d: rcvd ext data %zu", c->self, data_len); |
| 3002 | /* XXX sshpkt_getb? */ |
| 3003 | if ((r = sshbuf_put(c->extended, data, data_len)) != 0) |
| 3004 | error("%s: append: %s", __func__, ssh_err(r)); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3005 | c->local_window -= data_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3006 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3007 | } |
| 3008 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3009 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3010 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
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 | Channel *c = channel_from_packet_id(ssh, __func__, "ieof"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3013 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3014 | ssh_packet_check_eom(ssh); |
| 3015 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3016 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3017 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3018 | chan_rcvd_ieof(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3019 | |
| 3020 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 3021 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3022 | debug("channel %d: FORCE input drain", c->self); |
| 3023 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3024 | if (sshbuf_len(c->input) == 0) |
| 3025 | chan_ibuf_empty(ssh, c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 3026 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3027 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3028 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3029 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3030 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3031 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3032 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3033 | Channel *c = channel_from_packet_id(ssh, __func__, "oclose"); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 3034 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3035 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3036 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3037 | ssh_packet_check_eom(ssh); |
| 3038 | chan_rcvd_oclose(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3039 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3040 | } |
| 3041 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3042 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3043 | 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] | 3044 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3045 | Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation"); |
| 3046 | u_int32_t remote_window, remote_maxpacket; |
| 3047 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3048 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3049 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3050 | return 0; |
| 3051 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3052 | packet_disconnect("Received open confirmation for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3053 | "non-opening channel %d.", c->self); |
| 3054 | /* |
| 3055 | * Record the remote channel number and mark that the channel |
| 3056 | * is now open. |
| 3057 | */ |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3058 | if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 || |
| 3059 | (r = sshpkt_get_u32(ssh, &remote_window)) != 0 || |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3060 | (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) { |
| 3061 | error("%s: window/maxpacket: %s", __func__, ssh_err(r)); |
| 3062 | packet_disconnect("Invalid open confirmation message"); |
| 3063 | } |
| 3064 | ssh_packet_check_eom(ssh); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3065 | |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 3066 | c->have_remote_id = 1; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3067 | c->remote_window = remote_window; |
| 3068 | c->remote_maxpacket = remote_maxpacket; |
| 3069 | c->type = SSH_CHANNEL_OPEN; |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3070 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3071 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3072 | c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx); |
| 3073 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3074 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3075 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 3076 | c->remote_window, c->remote_maxpacket); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3077 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3078 | } |
| 3079 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3080 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3081 | reason2txt(int reason) |
| 3082 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3083 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3084 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 3085 | return "administratively prohibited"; |
| 3086 | case SSH2_OPEN_CONNECT_FAILED: |
| 3087 | return "connect failed"; |
| 3088 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 3089 | return "unknown channel type"; |
| 3090 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 3091 | return "resource shortage"; |
| 3092 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 3093 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3096 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3097 | 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] | 3098 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3099 | Channel *c = channel_from_packet_id(ssh, __func__, "open failure"); |
| 3100 | u_int32_t reason; |
| 3101 | char *msg = NULL; |
| 3102 | int r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3103 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3104 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3105 | return 0; |
| 3106 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3107 | packet_disconnect("Received open failure for " |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3108 | "non-opening channel %d.", c->self); |
| 3109 | if ((r = sshpkt_get_u32(ssh, &reason)) != 0) { |
| 3110 | error("%s: reason: %s", __func__, ssh_err(r)); |
| 3111 | packet_disconnect("Invalid open failure message"); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3112 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3113 | if ((datafellows & SSH_BUG_OPENFAILURE) == 0) { |
| 3114 | /* skip language */ |
| 3115 | if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
djm@openbsd.org | 218e6f9 | 2017-09-24 13:45:34 +0000 | [diff] [blame] | 3116 | (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3117 | error("%s: message/lang: %s", __func__, ssh_err(r)); |
| 3118 | packet_disconnect("Invalid open failure message"); |
| 3119 | } |
| 3120 | } |
| 3121 | ssh_packet_check_eom(ssh); |
| 3122 | logit("channel %d: open failed: %s%s%s", c->self, |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3123 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 3124 | free(msg); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3125 | if (c->open_confirm) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3126 | debug2("%s: channel %d: callback start", __func__, c->self); |
| 3127 | c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx); |
| 3128 | debug2("%s: channel %d: callback done", __func__, c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3129 | } |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 3130 | /* Schedule the channel for cleanup/deletion. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3131 | chan_mark_dead(ssh, c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3132 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3133 | } |
| 3134 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3135 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3136 | 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] | 3137 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3138 | int id = channel_parse_id(ssh, __func__, "window adjust"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3139 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3140 | u_int32_t adjust; |
| 3141 | u_int new_rwin; |
| 3142 | int r; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3143 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3144 | if ((c = channel_lookup(ssh, id)) == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 3145 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3146 | return 0; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 3147 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3148 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3149 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3150 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3151 | if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) { |
| 3152 | error("%s: adjust: %s", __func__, ssh_err(r)); |
| 3153 | packet_disconnect("Invalid window adjust message"); |
| 3154 | } |
| 3155 | ssh_packet_check_eom(ssh); |
| 3156 | debug2("channel %d: rcvd adjust %u", c->self, adjust); |
| 3157 | if ((new_rwin = c->remote_window + adjust) < c->remote_window) { |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 3158 | fatal("channel %d: adjust %u overflows remote window %u", |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3159 | c->self, adjust, c->remote_window); |
| 3160 | } |
| 3161 | c->remote_window = new_rwin; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3162 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3163 | } |
| 3164 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3165 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3166 | 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] | 3167 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3168 | int id = channel_parse_id(ssh, __func__, "status confirm"); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3169 | Channel *c; |
| 3170 | struct channel_confirm *cc; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3171 | |
| 3172 | /* Reset keepalive timeout */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 3173 | packet_set_alive_timeouts(0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3174 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3175 | debug2("%s: type %d id %d", __func__, type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3176 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3177 | if ((c = channel_lookup(ssh, id)) == NULL) { |
| 3178 | logit("%s: %d: unknown", __func__, id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3179 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3180 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 3181 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3182 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3183 | ssh_packet_check_eom(ssh); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3184 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3185 | return 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3186 | cc->cb(ssh, type, c, cc->ctx); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3187 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3188 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3189 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 3190 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 3191 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 3192 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3193 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3194 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3195 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3196 | channel_set_af(struct ssh *ssh, int af) |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3197 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3198 | ssh->chanctxt->IPv4or6 = af; |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3201 | |
| 3202 | /* |
| 3203 | * Determine whether or not a port forward listens to loopback, the |
| 3204 | * specified address or wildcard. On the client, a specified bind |
| 3205 | * address will always override gateway_ports. On the server, a |
| 3206 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 3207 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 3208 | * will bind to whatever address the client asked for. |
| 3209 | * |
| 3210 | * Special-case listen_addrs are: |
| 3211 | * |
| 3212 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 3213 | * "" (empty string), "*" -> wildcard v4/v6 |
| 3214 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3215 | * "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] | 3216 | */ |
| 3217 | static const char * |
| 3218 | channel_fwd_bind_addr(const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3219 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3220 | { |
| 3221 | const char *addr = NULL; |
| 3222 | int wildcard = 0; |
| 3223 | |
| 3224 | if (listen_addr == NULL) { |
| 3225 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3226 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3227 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3228 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3229 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 3230 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 3231 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3232 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3233 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 3234 | /* |
| 3235 | * Notify client if they requested a specific listen |
| 3236 | * address and it was overridden. |
| 3237 | */ |
| 3238 | if (*listen_addr != '\0' && |
| 3239 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 3240 | strcmp(listen_addr, "*") != 0) { |
| 3241 | packet_send_debug("Forwarding listen address " |
| 3242 | "\"%s\" overridden by server " |
| 3243 | "GatewayPorts", listen_addr); |
| 3244 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3245 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 3246 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3247 | strcmp(listen_addr, "::1") == 0) { |
| 3248 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 3249 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 3250 | } |
| 3251 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 3252 | strcmp(listen_addr, "::1") == 0) { |
| 3253 | /* |
| 3254 | * If a specific IPv4/IPv6 localhost address has been |
| 3255 | * requested then accept it even if gateway_ports is in |
| 3256 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 3257 | */ |
| 3258 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3259 | } |
| 3260 | if (wildcardp != NULL) |
| 3261 | *wildcardp = wildcard; |
| 3262 | return addr; |
| 3263 | } |
| 3264 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3265 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3266 | channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type, |
| 3267 | struct Forward *fwd, int *allocated_listen_port, |
| 3268 | struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3269 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3270 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3271 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3272 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3273 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3274 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3275 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3276 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3277 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3278 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 3279 | if (is_client && fwd->connect_path != NULL) { |
| 3280 | host = fwd->connect_path; |
| 3281 | } else { |
| 3282 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 3283 | fwd->listen_host : fwd->connect_host; |
| 3284 | if (host == NULL) { |
| 3285 | error("No forward host name."); |
| 3286 | return 0; |
| 3287 | } |
| 3288 | if (strlen(host) >= NI_MAXHOST) { |
| 3289 | error("Forward host name too long."); |
| 3290 | return 0; |
| 3291 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3292 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3293 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3294 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3295 | addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard, |
| 3296 | is_client, fwd_opts); |
| 3297 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3298 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 3299 | |
| 3300 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3301 | * getaddrinfo returns a loopback address if the hostname is |
| 3302 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3303 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3304 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3305 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3306 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3307 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3308 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3309 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 3310 | if (addr == NULL) { |
| 3311 | /* This really shouldn't happen */ |
| 3312 | packet_disconnect("getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3313 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3314 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3315 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3316 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3317 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 3318 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 3319 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3320 | if (allocated_listen_port != NULL) |
| 3321 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3322 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3323 | switch (ai->ai_family) { |
| 3324 | case AF_INET: |
| 3325 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 3326 | sin_port; |
| 3327 | break; |
| 3328 | case AF_INET6: |
| 3329 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 3330 | sin6_port; |
| 3331 | break; |
| 3332 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3333 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3334 | } |
| 3335 | /* |
| 3336 | * If allocating a port for -R forwards, then use the |
| 3337 | * same port for all address families. |
| 3338 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3339 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3340 | fwd->listen_port == 0 && allocated_listen_port != NULL && |
| 3341 | *allocated_listen_port > 0) |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3342 | *lport_p = htons(*allocated_listen_port); |
| 3343 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3344 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3345 | strport, sizeof(strport), |
| 3346 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3347 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3348 | continue; |
| 3349 | } |
| 3350 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3351 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3352 | if (sock < 0) { |
| 3353 | /* this is no error since kernel may not support ipv6 */ |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3354 | verbose("socket [%s]:%s: %.100s", ntop, strport, |
| 3355 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3356 | continue; |
| 3357 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 3358 | |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 3359 | set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3360 | if (ai->ai_family == AF_INET6) |
| 3361 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 3362 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3363 | debug("Local forwarding listening on %s port %s.", |
| 3364 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3365 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3366 | /* Bind the socket to the address. */ |
| 3367 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3368 | /* |
| 3369 | * address can be in if use ipv6 address is |
| 3370 | * already bound |
| 3371 | */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3372 | if (!ai->ai_next) |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3373 | error("bind [%s]:%s: %.100s", |
| 3374 | ntop, strport, strerror(errno)); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3375 | else |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3376 | verbose("bind [%s]:%s: %.100s", |
| 3377 | ntop, strport, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3378 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3379 | close(sock); |
| 3380 | continue; |
| 3381 | } |
| 3382 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3383 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3384 | error("listen: %.100s", strerror(errno)); |
djm@openbsd.org | 927f851 | 2017-12-05 01:30:19 +0000 | [diff] [blame] | 3385 | error("listen [%s]:%s: %.100s", ntop, strport, |
| 3386 | strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3387 | close(sock); |
| 3388 | continue; |
| 3389 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3390 | |
| 3391 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3392 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3393 | * record what we got. |
| 3394 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3395 | if (type == SSH_CHANNEL_RPORT_LISTENER && |
| 3396 | fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3397 | allocated_listen_port != NULL && |
| 3398 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 3399 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 3400 | debug("Allocated listen port %d", |
| 3401 | *allocated_listen_port); |
| 3402 | } |
| 3403 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3404 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3405 | c = channel_new(ssh, "port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3406 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3407 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 3408 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3409 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3410 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3411 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3412 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 3413 | c->listening_port = *allocated_listen_port; |
| 3414 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3415 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3416 | success = 1; |
| 3417 | } |
| 3418 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3419 | error("%s: cannot listen to port: %d", __func__, |
| 3420 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3421 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 3422 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3423 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3424 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3425 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3426 | channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type, |
| 3427 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3428 | { |
| 3429 | struct sockaddr_un sunaddr; |
| 3430 | const char *path; |
| 3431 | Channel *c; |
| 3432 | int port, sock; |
| 3433 | mode_t omask; |
| 3434 | |
| 3435 | switch (type) { |
| 3436 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3437 | if (fwd->connect_path != NULL) { |
| 3438 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3439 | error("Local connecting path too long: %s", |
| 3440 | fwd->connect_path); |
| 3441 | return 0; |
| 3442 | } |
| 3443 | path = fwd->connect_path; |
| 3444 | port = PORT_STREAMLOCAL; |
| 3445 | } else { |
| 3446 | if (fwd->connect_host == NULL) { |
| 3447 | error("No forward host name."); |
| 3448 | return 0; |
| 3449 | } |
| 3450 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3451 | error("Forward host name too long."); |
| 3452 | return 0; |
| 3453 | } |
| 3454 | path = fwd->connect_host; |
| 3455 | port = fwd->connect_port; |
| 3456 | } |
| 3457 | break; |
| 3458 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3459 | path = fwd->listen_path; |
| 3460 | port = PORT_STREAMLOCAL; |
| 3461 | break; |
| 3462 | default: |
| 3463 | error("%s: unexpected channel type %d", __func__, type); |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | if (fwd->listen_path == NULL) { |
| 3468 | error("No forward path name."); |
| 3469 | return 0; |
| 3470 | } |
| 3471 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3472 | error("Local listening path too long: %s", fwd->listen_path); |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
| 3476 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3477 | |
| 3478 | /* Start a Unix domain listener. */ |
| 3479 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3480 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3481 | fwd_opts->streamlocal_bind_unlink); |
| 3482 | umask(omask); |
| 3483 | if (sock < 0) |
| 3484 | return 0; |
| 3485 | |
| 3486 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3487 | |
| 3488 | /* Allocate a channel number for the socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3489 | c = channel_new(ssh, "unix listener", type, sock, sock, -1, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3490 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3491 | 0, "unix listener", 1); |
| 3492 | c->path = xstrdup(path); |
| 3493 | c->host_port = port; |
| 3494 | c->listening_port = PORT_STREAMLOCAL; |
| 3495 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
| 3499 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3500 | channel_cancel_rport_listener_tcpip(struct ssh *ssh, |
| 3501 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3502 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3503 | u_int i; |
| 3504 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3505 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3506 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3507 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3508 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3509 | continue; |
| 3510 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3511 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3512 | channel_free(ssh, c); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3513 | found = 1; |
| 3514 | } |
| 3515 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3516 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3517 | return found; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3518 | } |
| 3519 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3520 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3521 | channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3522 | { |
| 3523 | u_int i; |
| 3524 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3525 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3526 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3527 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3528 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3529 | continue; |
| 3530 | if (c->path == NULL) |
| 3531 | continue; |
| 3532 | if (strcmp(c->path, path) == 0) { |
| 3533 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3534 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3535 | found = 1; |
| 3536 | } |
| 3537 | } |
| 3538 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3539 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3543 | channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3544 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3545 | if (fwd->listen_path != NULL) { |
| 3546 | return channel_cancel_rport_listener_streamlocal(ssh, |
| 3547 | fwd->listen_path); |
| 3548 | } else { |
| 3549 | return channel_cancel_rport_listener_tcpip(ssh, |
| 3550 | fwd->listen_host, fwd->listen_port); |
| 3551 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3555 | channel_cancel_lport_listener_tcpip(struct ssh *ssh, |
| 3556 | const char *lhost, u_short lport, int cport, |
| 3557 | struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3558 | { |
| 3559 | u_int i; |
| 3560 | int found = 0; |
| 3561 | const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3562 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3563 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3564 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3565 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3566 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3567 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3568 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3569 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3570 | /* skip dynamic forwardings */ |
| 3571 | if (c->host_port == 0) |
| 3572 | continue; |
| 3573 | } else { |
| 3574 | if (c->host_port != cport) |
| 3575 | continue; |
| 3576 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3577 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3578 | (c->listening_addr != NULL && addr == NULL)) |
| 3579 | continue; |
| 3580 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3581 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3582 | channel_free(ssh, c); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3583 | found = 1; |
| 3584 | } |
| 3585 | } |
| 3586 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3587 | return found; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3588 | } |
| 3589 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3590 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3591 | channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3592 | { |
| 3593 | u_int i; |
| 3594 | int found = 0; |
| 3595 | |
| 3596 | if (path == NULL) { |
| 3597 | error("%s: no path specified.", __func__); |
| 3598 | return 0; |
| 3599 | } |
| 3600 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3601 | for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { |
| 3602 | Channel *c = ssh->chanctxt->channels[i]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3603 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3604 | continue; |
| 3605 | if (c->listening_addr == NULL) |
| 3606 | continue; |
| 3607 | if (strcmp(c->listening_addr, path) == 0) { |
| 3608 | debug2("%s: close channel %d", __func__, i); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3609 | channel_free(ssh, c); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3610 | found = 1; |
| 3611 | } |
| 3612 | } |
| 3613 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3614 | return found; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3618 | channel_cancel_lport_listener(struct ssh *ssh, |
| 3619 | struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3620 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3621 | if (fwd->listen_path != NULL) { |
| 3622 | return channel_cancel_lport_listener_streamlocal(ssh, |
| 3623 | fwd->listen_path); |
| 3624 | } else { |
| 3625 | return channel_cancel_lport_listener_tcpip(ssh, |
| 3626 | fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3627 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3628 | } |
| 3629 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3630 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3631 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3632 | channel_setup_local_fwd_listener(struct ssh *ssh, |
| 3633 | struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3634 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3635 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3636 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3637 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3638 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3639 | return channel_setup_fwd_listener_tcpip(ssh, |
| 3640 | SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3641 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /* protocol v2 remote port fwd, used by sshd */ |
| 3645 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3646 | channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3647 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3648 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3649 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3650 | return channel_setup_fwd_listener_streamlocal(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3651 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3652 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3653 | return channel_setup_fwd_listener_tcpip(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3654 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3655 | fwd_opts); |
| 3656 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3657 | } |
| 3658 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3659 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3660 | * Translate the requested rfwd listen host to something usable for |
| 3661 | * this server. |
| 3662 | */ |
| 3663 | static const char * |
| 3664 | channel_rfwd_bind_host(const char *listen_host) |
| 3665 | { |
| 3666 | if (listen_host == NULL) { |
| 3667 | if (datafellows & SSH_BUG_RFWD_ADDR) |
| 3668 | return "127.0.0.1"; |
| 3669 | else |
| 3670 | return "localhost"; |
| 3671 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
| 3672 | if (datafellows & SSH_BUG_RFWD_ADDR) |
| 3673 | return "0.0.0.0"; |
| 3674 | else |
| 3675 | return ""; |
| 3676 | } else |
| 3677 | return listen_host; |
| 3678 | } |
| 3679 | |
| 3680 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3681 | * Initiate forwarding of connections to port "port" on remote host through |
| 3682 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3683 | * Returns handle (index) for updating the dynamic listen port with |
| 3684 | * channel_update_permitted_opens(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3685 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3686 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3687 | channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3688 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3689 | int r, success = 0, idx = -1; |
| 3690 | char *host_to_connect, *listen_host, *listen_path; |
| 3691 | int port_to_connect, listen_port; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3692 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3693 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3694 | if (fwd->listen_path != NULL) { |
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, |
| 3697 | "streamlocal-forward@openssh.com")) != 0 || |
| 3698 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3699 | (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 || |
| 3700 | (r = sshpkt_send(ssh)) != 0 || |
| 3701 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3702 | fatal("%s: request streamlocal: %s", |
| 3703 | __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3704 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3705 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3706 | (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 || |
| 3707 | (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */ |
| 3708 | (r = sshpkt_put_cstring(ssh, |
| 3709 | channel_rfwd_bind_host(fwd->listen_host))) != 0 || |
| 3710 | (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 || |
| 3711 | (r = sshpkt_send(ssh)) != 0 || |
| 3712 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 3713 | fatal("%s: request tcpip-forward: %s", |
| 3714 | __func__, ssh_err(r)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3715 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3716 | /* Assume that server accepts the request */ |
| 3717 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3718 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3719 | /* Record that connection to this host/port is permitted. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3720 | host_to_connect = listen_host = listen_path = NULL; |
| 3721 | port_to_connect = listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3722 | if (fwd->connect_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3723 | host_to_connect = xstrdup(fwd->connect_path); |
| 3724 | port_to_connect = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3725 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3726 | host_to_connect = xstrdup(fwd->connect_host); |
| 3727 | port_to_connect = fwd->connect_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3728 | } |
| 3729 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3730 | listen_path = xstrdup(fwd->listen_path); |
| 3731 | listen_port = PORT_STREAMLOCAL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3732 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3733 | if (fwd->listen_host != NULL) |
| 3734 | listen_host = xstrdup(fwd->listen_host); |
| 3735 | listen_port = fwd->listen_port; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3736 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3737 | idx = fwd_perm_list_add(ssh, FWDPERM_USER, |
| 3738 | host_to_connect, port_to_connect, |
| 3739 | listen_host, listen_path, listen_port, NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3740 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3741 | return idx; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3742 | } |
| 3743 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3744 | static int |
| 3745 | open_match(ForwardPermission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3746 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3747 | { |
| 3748 | if (allowed_open->host_to_connect == NULL) |
| 3749 | return 0; |
| 3750 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3751 | allowed_open->port_to_connect != requestedport) |
| 3752 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3753 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3754 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3755 | return 0; |
| 3756 | return 1; |
| 3757 | } |
| 3758 | |
| 3759 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3760 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3761 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3762 | * need to translate between the configured-host (listen_host) |
| 3763 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3764 | */ |
| 3765 | static int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3766 | open_listen_match_tcpip(ForwardPermission *allowed_open, |
| 3767 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3768 | { |
| 3769 | const char *allowed_host; |
| 3770 | |
| 3771 | if (allowed_open->host_to_connect == NULL) |
| 3772 | return 0; |
| 3773 | if (allowed_open->listen_port != requestedport) |
| 3774 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3775 | if (!translate && allowed_open->listen_host == NULL && |
| 3776 | requestedhost == NULL) |
| 3777 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3778 | allowed_host = translate ? |
| 3779 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3780 | allowed_open->listen_host; |
| 3781 | if (allowed_host == NULL || |
| 3782 | strcmp(allowed_host, requestedhost) != 0) |
| 3783 | return 0; |
| 3784 | return 1; |
| 3785 | } |
| 3786 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3787 | static int |
| 3788 | open_listen_match_streamlocal(ForwardPermission *allowed_open, |
| 3789 | const char *requestedpath) |
| 3790 | { |
| 3791 | if (allowed_open->host_to_connect == NULL) |
| 3792 | return 0; |
| 3793 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3794 | return 0; |
| 3795 | if (allowed_open->listen_path == NULL || |
| 3796 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3797 | return 0; |
| 3798 | return 1; |
| 3799 | } |
| 3800 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3801 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3802 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3803 | * local side. |
| 3804 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3805 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3806 | channel_request_rforward_cancel_tcpip(struct ssh *ssh, |
| 3807 | const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3808 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3809 | struct ssh_channels *sc = ssh->chanctxt; |
| 3810 | int r; |
| 3811 | u_int i; |
| 3812 | ForwardPermission *fp; |
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 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 3815 | fp = &sc->permitted_opens[i]; |
| 3816 | if (open_listen_match_tcpip(fp, host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3817 | break; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3818 | fp = NULL; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3819 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3820 | if (fp == NULL) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3821 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3822 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3823 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3824 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3825 | (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 || |
| 3826 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3827 | (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 || |
| 3828 | (r = sshpkt_put_u32(ssh, port)) != 0 || |
| 3829 | (r = sshpkt_send(ssh)) != 0) |
| 3830 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3831 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3832 | fwd_perm_clear(fp); /* unregister */ |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3833 | |
| 3834 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3838 | * Request cancellation of remote forwarding of Unix domain socket |
| 3839 | * path from local side. |
| 3840 | */ |
| 3841 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3842 | channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3843 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3844 | struct ssh_channels *sc = ssh->chanctxt; |
| 3845 | int r; |
| 3846 | u_int i; |
| 3847 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3848 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3849 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 3850 | fp = &sc->permitted_opens[i]; |
| 3851 | if (open_listen_match_streamlocal(fp, path)) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3852 | break; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3853 | fp = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3854 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3855 | if (fp == NULL) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3856 | debug("%s: requested forward not found", __func__); |
| 3857 | return -1; |
| 3858 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3859 | if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || |
| 3860 | (r = sshpkt_put_cstring(ssh, |
| 3861 | "cancel-streamlocal-forward@openssh.com")) != 0 || |
| 3862 | (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */ |
| 3863 | (r = sshpkt_put_cstring(ssh, path)) != 0 || |
| 3864 | (r = sshpkt_send(ssh)) != 0) |
| 3865 | fatal("%s: send cancel: %s", __func__, ssh_err(r)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3866 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3867 | fwd_perm_clear(fp); /* unregister */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3868 | |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
| 3872 | /* |
| 3873 | * Request cancellation of remote forwarding of a connection from local side. |
| 3874 | */ |
| 3875 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3876 | channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3877 | { |
| 3878 | if (fwd->listen_path != NULL) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3879 | return channel_request_rforward_cancel_streamlocal(ssh, |
| 3880 | fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3881 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3882 | return channel_request_rforward_cancel_tcpip(ssh, |
| 3883 | fwd->listen_host, |
| 3884 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | /* |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3889 | * Permits opening to any host/port if permitted_opens[] is empty. This is |
| 3890 | * usually called by the server, because the user could connect to any port |
| 3891 | * anyway, and the server has no way to know but to trust the client anyway. |
| 3892 | */ |
| 3893 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3894 | channel_permit_all_opens(struct ssh *ssh) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3895 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3896 | if (ssh->chanctxt->num_permitted_opens == 0) |
| 3897 | ssh->chanctxt->all_opens_permitted = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 3900 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3901 | channel_add_permitted_opens(struct ssh *ssh, char *host, int port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3902 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3903 | struct ssh_channels *sc = ssh->chanctxt; |
| 3904 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3905 | debug("allow port forwarding to host %s port %d", host, port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3906 | fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL); |
| 3907 | sc->all_opens_permitted = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3910 | /* |
| 3911 | * Update the listen port for a dynamic remote forward, after |
| 3912 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 3913 | * passed then they entry will be invalidated. |
| 3914 | */ |
| 3915 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3916 | channel_update_permitted_opens(struct ssh *ssh, int idx, int newport) |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3917 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3918 | struct ssh_channels *sc = ssh->chanctxt; |
| 3919 | |
| 3920 | if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) { |
| 3921 | debug("%s: index out of range: %d num_permitted_opens %d", |
| 3922 | __func__, idx, sc->num_permitted_opens); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3923 | return; |
| 3924 | } |
| 3925 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 3926 | newport > 0 ? "Updating" : "Removing", |
| 3927 | newport, |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3928 | sc->permitted_opens[idx].host_to_connect, |
| 3929 | sc->permitted_opens[idx].port_to_connect); |
| 3930 | if (newport <= 0) |
| 3931 | fwd_perm_clear(&sc->permitted_opens[idx]); |
| 3932 | else { |
| 3933 | sc->permitted_opens[idx].listen_port = |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3934 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3938 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3939 | channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port) |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3940 | { |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3941 | 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] | 3942 | return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port, |
| 3943 | NULL, NULL, 0, NULL); |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3947 | channel_disable_adm_local_opens(struct ssh *ssh) |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3948 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3949 | channel_clear_adm_permitted_opens(ssh); |
| 3950 | 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] | 3951 | } |
| 3952 | |
| 3953 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3954 | channel_clear_permitted_opens(struct ssh *ssh) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3955 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3956 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3957 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3958 | sc->permitted_opens = xrecallocarray(sc->permitted_opens, |
| 3959 | sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens)); |
| 3960 | sc->num_permitted_opens = 0; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3961 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3962 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3963 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3964 | channel_clear_adm_permitted_opens(struct ssh *ssh) |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3965 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3966 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3967 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3968 | sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens, |
| 3969 | sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens)); |
| 3970 | sc->num_adm_permitted_opens = 0; |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3971 | } |
| 3972 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 3973 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 3974 | int |
| 3975 | permitopen_port(const char *p) |
| 3976 | { |
| 3977 | int port; |
| 3978 | |
| 3979 | if (strcmp(p, "*") == 0) |
| 3980 | return FWD_PERMIT_ANY_PORT; |
| 3981 | if ((port = a2port(p)) > 0) |
| 3982 | return port; |
| 3983 | return -1; |
| 3984 | } |
| 3985 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3986 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3987 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3988 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3989 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3990 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3991 | struct sockaddr_un *sunaddr; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 3992 | char ntop[NI_MAXHOST]; |
| 3993 | char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3994 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3995 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3996 | switch (cctx->ai->ai_family) { |
| 3997 | case AF_UNIX: |
| 3998 | /* unix:pathname instead of host:port */ |
| 3999 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 4000 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 4001 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 4002 | break; |
| 4003 | case AF_INET: |
| 4004 | case AF_INET6: |
| 4005 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 4006 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 4007 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 4008 | error("connect_next: getnameinfo failed"); |
| 4009 | continue; |
| 4010 | } |
| 4011 | break; |
| 4012 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4013 | continue; |
| 4014 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4015 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 4016 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4017 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 4018 | error("socket: %.100s", strerror(errno)); |
| 4019 | else |
| 4020 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4021 | continue; |
| 4022 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 4023 | if (set_nonblock(sock) == -1) |
| 4024 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4025 | if (connect(sock, cctx->ai->ai_addr, |
| 4026 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 4027 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 4028 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4029 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4030 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4031 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4032 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 4033 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4034 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4035 | if (cctx->ai->ai_family != AF_UNIX) |
| 4036 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4037 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 4038 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 4039 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4040 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4041 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 4042 | return -1; |
| 4043 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4044 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4045 | static void |
| 4046 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 4047 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4048 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4049 | if (cctx->aitop) { |
| 4050 | if (cctx->aitop->ai_family == AF_UNIX) |
| 4051 | free(cctx->aitop); |
| 4052 | else |
| 4053 | freeaddrinfo(cctx->aitop); |
| 4054 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 4055 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4056 | } |
| 4057 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4058 | /* |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4059 | * Return connecting socket to remote host:port or local socket path, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4060 | * passing back the failure reason if appropriate. |
| 4061 | */ |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4062 | static int |
| 4063 | connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype, |
| 4064 | char *ctype, char *rname, struct channel_connect *cctx, |
| 4065 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4066 | { |
| 4067 | struct addrinfo hints; |
| 4068 | int gaierr; |
| 4069 | int sock = -1; |
| 4070 | char strport[NI_MAXSERV]; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4071 | |
| 4072 | if (port == PORT_STREAMLOCAL) { |
| 4073 | struct sockaddr_un *sunaddr; |
| 4074 | struct addrinfo *ai; |
| 4075 | |
| 4076 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 4077 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4078 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | /* |
| 4082 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 4083 | * channel_connect_ctx_free() must check ai_family |
| 4084 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 4085 | */ |
| 4086 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 4087 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 4088 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 4089 | ai->ai_addrlen = sizeof(*sunaddr); |
| 4090 | ai->ai_family = AF_UNIX; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4091 | ai->ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4092 | ai->ai_protocol = PF_UNSPEC; |
| 4093 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 4094 | sunaddr->sun_family = AF_UNIX; |
| 4095 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4096 | cctx->aitop = ai; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4097 | } else { |
| 4098 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4099 | hints.ai_family = ssh->chanctxt->IPv4or6; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4100 | hints.ai_socktype = socktype; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4101 | snprintf(strport, sizeof strport, "%d", port); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4102 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop)) |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4103 | != 0) { |
| 4104 | if (errmsg != NULL) |
| 4105 | *errmsg = ssh_gai_strerror(gaierr); |
| 4106 | if (reason != NULL) |
| 4107 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4108 | error("connect_to %.100s: unknown host (%s)", name, |
| 4109 | ssh_gai_strerror(gaierr)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4110 | return -1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4111 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4112 | } |
| 4113 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4114 | cctx->host = xstrdup(name); |
| 4115 | cctx->port = port; |
| 4116 | cctx->ai = cctx->aitop; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4117 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4118 | if ((sock = connect_next(cctx)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4119 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4120 | name, port, strerror(errno)); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4121 | return -1; |
| 4122 | } |
| 4123 | |
| 4124 | return sock; |
| 4125 | } |
| 4126 | |
| 4127 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 4128 | static Channel * |
| 4129 | connect_to(struct ssh *ssh, const char *host, int port, |
| 4130 | char *ctype, char *rname) |
| 4131 | { |
| 4132 | struct channel_connect cctx; |
| 4133 | Channel *c; |
| 4134 | int sock; |
| 4135 | |
| 4136 | memset(&cctx, 0, sizeof(cctx)); |
| 4137 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4138 | &cctx, NULL, NULL); |
| 4139 | if (sock == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4140 | channel_connect_ctx_free(&cctx); |
| 4141 | return NULL; |
| 4142 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4143 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4144 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4145 | c->host_port = port; |
| 4146 | c->path = xstrdup(host); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4147 | c->connect_ctx = cctx; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4148 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4149 | return c; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 4152 | /* |
| 4153 | * returns either the newly connected channel or the downstream channel |
| 4154 | * that needs to deal with this connection. |
| 4155 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4156 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4157 | channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4158 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4159 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4160 | struct ssh_channels *sc = ssh->chanctxt; |
| 4161 | u_int i; |
| 4162 | ForwardPermission *fp; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4163 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4164 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4165 | fp = &sc->permitted_opens[i]; |
| 4166 | if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) { |
| 4167 | if (fp->downstream) |
| 4168 | return fp->downstream; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4169 | if (fp->port_to_connect == 0) |
| 4170 | return rdynamic_connect_prepare(ssh, |
| 4171 | ctype, rname); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4172 | return connect_to(ssh, |
| 4173 | fp->host_to_connect, fp->port_to_connect, |
| 4174 | ctype, rname); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 4178 | listen_port); |
| 4179 | return NULL; |
| 4180 | } |
| 4181 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4182 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4183 | channel_connect_by_listen_path(struct ssh *ssh, const char *path, |
| 4184 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4185 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4186 | struct ssh_channels *sc = ssh->chanctxt; |
| 4187 | u_int i; |
| 4188 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4189 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4190 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4191 | fp = &sc->permitted_opens[i]; |
| 4192 | if (open_listen_match_streamlocal(fp, path)) { |
| 4193 | return connect_to(ssh, |
| 4194 | fp->host_to_connect, fp->port_to_connect, |
| 4195 | ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 4199 | path); |
| 4200 | return NULL; |
| 4201 | } |
| 4202 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4203 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4204 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4205 | channel_connect_to_port(struct ssh *ssh, const char *host, u_short port, |
| 4206 | char *ctype, char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4207 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4208 | struct ssh_channels *sc = ssh->chanctxt; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4209 | struct channel_connect cctx; |
| 4210 | Channel *c; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4211 | u_int i, permit, permit_adm = 1; |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4212 | int sock; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4213 | ForwardPermission *fp; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4214 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4215 | permit = sc->all_opens_permitted; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4216 | if (!permit) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4217 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4218 | fp = &sc->permitted_opens[i]; |
| 4219 | if (open_match(fp, host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4220 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4221 | break; |
| 4222 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4223 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4224 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4225 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4226 | if (sc->num_adm_permitted_opens > 0) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4227 | permit_adm = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4228 | for (i = 0; i < sc->num_adm_permitted_opens; i++) { |
| 4229 | fp = &sc->permitted_adm_opens[i]; |
| 4230 | if (open_match(fp, host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4231 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 4232 | break; |
| 4233 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4234 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 4238 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4239 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 4240 | if (reason != NULL) |
| 4241 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 4242 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4243 | } |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4244 | |
| 4245 | memset(&cctx, 0, sizeof(cctx)); |
| 4246 | sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname, |
| 4247 | &cctx, reason, errmsg); |
| 4248 | if (sock == -1) { |
| 4249 | channel_connect_ctx_free(&cctx); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | |
| 4253 | c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 4254 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4255 | c->host_port = port; |
| 4256 | c->path = xstrdup(host); |
| 4257 | c->connect_ctx = cctx; |
| 4258 | |
| 4259 | return c; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4262 | /* Check if connecting to that path is permitted and connect. */ |
| 4263 | Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4264 | channel_connect_to_path(struct ssh *ssh, const char *path, |
| 4265 | char *ctype, char *rname) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4266 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4267 | struct ssh_channels *sc = ssh->chanctxt; |
| 4268 | u_int i, permit, permit_adm = 1; |
| 4269 | ForwardPermission *fp; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4270 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4271 | permit = sc->all_opens_permitted; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4272 | if (!permit) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4273 | for (i = 0; i < sc->num_permitted_opens; i++) { |
| 4274 | fp = &sc->permitted_opens[i]; |
| 4275 | if (open_match(fp, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4276 | permit = 1; |
| 4277 | break; |
| 4278 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4279 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4280 | } |
| 4281 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4282 | if (sc->num_adm_permitted_opens > 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4283 | permit_adm = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4284 | for (i = 0; i < sc->num_adm_permitted_opens; i++) { |
| 4285 | fp = &sc->permitted_adm_opens[i]; |
| 4286 | if (open_match(fp, path, PORT_STREAMLOCAL)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4287 | permit_adm = 1; |
| 4288 | break; |
| 4289 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4290 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | if (!permit || !permit_adm) { |
| 4294 | logit("Received request to connect to path %.100s, " |
| 4295 | "but the request was denied.", path); |
| 4296 | return NULL; |
| 4297 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4298 | return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 4299 | } |
| 4300 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4301 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4302 | channel_send_window_changes(struct ssh *ssh) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4303 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4304 | struct ssh_channels *sc = ssh->chanctxt; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4305 | struct winsize ws; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4306 | int r; |
| 4307 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4308 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4309 | for (i = 0; i < sc->channels_alloc; i++) { |
| 4310 | if (sc->channels[i] == NULL || !sc->channels[i]->client_tty || |
| 4311 | sc->channels[i]->type != SSH_CHANNEL_OPEN) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4312 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4313 | if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4314 | continue; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4315 | channel_request_start(ssh, i, "window-change", 0); |
| 4316 | if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || |
| 4317 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || |
| 4318 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || |
| 4319 | (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 || |
| 4320 | (r = sshpkt_send(ssh)) != 0) |
| 4321 | fatal("%s: channel %u: send window-change: %s", |
| 4322 | __func__, i, ssh_err(r)); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | |
markus@openbsd.org | 609d7a6 | 2017-09-21 19:16:53 +0000 | [diff] [blame] | 4326 | /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */ |
| 4327 | static Channel * |
| 4328 | rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname) |
| 4329 | { |
| 4330 | Channel *c; |
| 4331 | int r; |
| 4332 | |
| 4333 | c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1, |
| 4334 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 4335 | c->host_port = 0; |
| 4336 | c->path = NULL; |
| 4337 | |
| 4338 | /* |
| 4339 | * We need to open the channel before we have a FD, |
| 4340 | * so that we can get SOCKS header from peer. |
| 4341 | */ |
| 4342 | if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || |
| 4343 | (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || |
| 4344 | (r = sshpkt_put_u32(ssh, c->self)) != 0 || |
| 4345 | (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || |
| 4346 | (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) { |
| 4347 | fatal("%s: channel %i: confirm: %s", __func__, |
| 4348 | c->self, ssh_err(r)); |
| 4349 | } |
| 4350 | return c; |
| 4351 | } |
| 4352 | |
| 4353 | /* Return CONNECTING socket to remote host:port or local socket path */ |
| 4354 | static int |
| 4355 | rdynamic_connect_finish(struct ssh *ssh, Channel *c) |
| 4356 | { |
| 4357 | struct channel_connect cctx; |
| 4358 | int sock; |
| 4359 | |
| 4360 | memset(&cctx, 0, sizeof(cctx)); |
| 4361 | sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL, |
| 4362 | NULL, &cctx, NULL, NULL); |
| 4363 | if (sock == -1) |
| 4364 | channel_connect_ctx_free(&cctx); |
| 4365 | else { |
| 4366 | /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */ |
| 4367 | c->type = SSH_CHANNEL_RDYNAMIC_FINISH; |
| 4368 | c->connect_ctx = cctx; |
| 4369 | channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0); |
| 4370 | } |
| 4371 | return sock; |
| 4372 | } |
| 4373 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4374 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4375 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4376 | /* |
| 4377 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4378 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 4379 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4380 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4381 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4382 | x11_create_display_inet(struct ssh *ssh, int x11_display_offset, |
| 4383 | int x11_use_localhost, int single_connection, |
| 4384 | u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4385 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 4386 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 4387 | int display_number, sock; |
| 4388 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4389 | struct addrinfo hints, *ai, *aitop; |
| 4390 | char strport[NI_MAXSERV]; |
| 4391 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4392 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4393 | if (chanids == NULL) |
| 4394 | return -1; |
| 4395 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 4396 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4397 | display_number < MAX_DISPLAYS; |
| 4398 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4399 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4400 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4401 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 4402 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4403 | hints.ai_socktype = SOCK_STREAM; |
| 4404 | snprintf(strport, sizeof strport, "%d", port); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4405 | if ((gaierr = getaddrinfo(NULL, strport, |
| 4406 | &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4407 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4408 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4409 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4410 | for (ai = aitop; ai; ai = ai->ai_next) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4411 | if (ai->ai_family != AF_INET && |
| 4412 | ai->ai_family != AF_INET6) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4413 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4414 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 4415 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4416 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 4417 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 4418 | #ifdef EPFNOSUPPORT |
| 4419 | && (errno != EPFNOSUPPORT) |
| 4420 | #endif |
| 4421 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4422 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 4423 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4424 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4425 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 4426 | debug("x11_create_display_inet: Socket family %d not supported", |
| 4427 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 4428 | continue; |
| 4429 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4430 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 4431 | if (ai->ai_family == AF_INET6) |
| 4432 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 4433 | if (x11_use_localhost) |
djm@openbsd.org | acf559e | 2017-10-25 00:15:35 +0000 | [diff] [blame] | 4434 | set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4435 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4436 | debug2("%s: bind port %d: %.100s", __func__, |
| 4437 | port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4438 | close(sock); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4439 | for (n = 0; n < num_socks; n++) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4440 | close(socks[n]); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4441 | num_socks = 0; |
| 4442 | break; |
| 4443 | } |
| 4444 | socks[num_socks++] = sock; |
| 4445 | if (num_socks == NUM_SOCKS) |
| 4446 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4447 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 4448 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4449 | if (num_socks > 0) |
| 4450 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4451 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4452 | if (display_number >= MAX_DISPLAYS) { |
| 4453 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4454 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4455 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4456 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4457 | for (n = 0; n < num_socks; n++) { |
| 4458 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 4459 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4460 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4461 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4462 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4463 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4464 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4465 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4466 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 4467 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4468 | for (n = 0; n < num_socks; n++) { |
| 4469 | sock = socks[n]; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4470 | nc = channel_new(ssh, "x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4471 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 4472 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 4473 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 4474 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4475 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4476 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 4477 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4478 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 4479 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 4480 | *display_numberp = display_number; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4481 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4482 | } |
| 4483 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 4484 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4485 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4486 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4487 | int sock; |
| 4488 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4489 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4490 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 4491 | if (sock < 0) |
| 4492 | error("socket: %.100s", strerror(errno)); |
| 4493 | memset(&addr, 0, sizeof(addr)); |
| 4494 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4495 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 4496 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 4497 | return sock; |
| 4498 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4499 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4500 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4501 | } |
| 4502 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4503 | static int |
| 4504 | connect_local_xsocket(u_int dnr) |
| 4505 | { |
| 4506 | char buf[1024]; |
| 4507 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4508 | return connect_local_xsocket_path(buf); |
| 4509 | } |
| 4510 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4511 | #ifdef __APPLE__ |
| 4512 | static int |
| 4513 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4514 | { |
| 4515 | struct stat sbuf; |
| 4516 | |
| 4517 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4518 | error("%s: display path too long", __func__); |
| 4519 | return 0; |
| 4520 | } |
| 4521 | if (display[0] != '/') |
| 4522 | return 0; |
| 4523 | if (stat(path, &sbuf) == 0) { |
| 4524 | return 1; |
| 4525 | } else { |
| 4526 | char *dot = strrchr(path, '.'); |
| 4527 | if (dot != NULL) { |
| 4528 | *dot = '\0'; |
| 4529 | if (stat(path, &sbuf) == 0) { |
| 4530 | return 1; |
| 4531 | } |
| 4532 | } |
| 4533 | } |
| 4534 | return 0; |
| 4535 | } |
| 4536 | #endif |
| 4537 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4538 | int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4539 | x11_connect_display(struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4540 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4541 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4542 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4543 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4544 | struct addrinfo hints, *ai, *aitop; |
| 4545 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4546 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4547 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4548 | /* Try to open a socket for the local X server. */ |
| 4549 | display = getenv("DISPLAY"); |
| 4550 | if (!display) { |
| 4551 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4552 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4553 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4554 | /* |
| 4555 | * Now we decode the value of the DISPLAY variable and make a |
| 4556 | * connection to the real X server. |
| 4557 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4558 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4559 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4560 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4561 | { |
| 4562 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4563 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4564 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4565 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4566 | |
| 4567 | /* Create a socket. */ |
| 4568 | sock = connect_local_xsocket_path(path); |
| 4569 | if (sock < 0) |
| 4570 | return -1; |
| 4571 | |
| 4572 | /* OK, we now have a connection to the display. */ |
| 4573 | return sock; |
| 4574 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4575 | } |
| 4576 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4577 | /* |
| 4578 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4579 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4580 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4581 | if (strncmp(display, "unix:", 5) == 0 || |
| 4582 | display[0] == ':') { |
| 4583 | /* Connect to the unix domain socket. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4584 | if (sscanf(strrchr(display, ':') + 1, "%u", |
| 4585 | &display_number) != 1) { |
| 4586 | error("Could not parse display number from DISPLAY: " |
| 4587 | "%.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4588 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4589 | } |
| 4590 | /* Create a socket. */ |
| 4591 | sock = connect_local_xsocket(display_number); |
| 4592 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4593 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4594 | |
| 4595 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4596 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4597 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4598 | /* |
| 4599 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4600 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4601 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4602 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4603 | cp = strchr(buf, ':'); |
| 4604 | if (!cp) { |
| 4605 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4606 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4607 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4608 | *cp = 0; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4609 | /* |
| 4610 | * buf now contains the host name. But first we parse the |
| 4611 | * display number. |
| 4612 | */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4613 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4614 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4615 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4616 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4617 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4618 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4619 | /* Look up the host address */ |
| 4620 | memset(&hints, 0, sizeof(hints)); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4621 | hints.ai_family = ssh->chanctxt->IPv4or6; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4622 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4623 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4624 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4625 | error("%.100s: unknown host. (%s)", buf, |
| 4626 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4627 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4628 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4629 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4630 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4631 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4632 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4633 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4634 | continue; |
| 4635 | } |
| 4636 | /* Connect it to the display. */ |
| 4637 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4638 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4639 | 6000 + display_number, strerror(errno)); |
| 4640 | close(sock); |
| 4641 | continue; |
| 4642 | } |
| 4643 | /* Success */ |
| 4644 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4645 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4646 | freeaddrinfo(aitop); |
| 4647 | if (!ai) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4648 | error("connect %.100s port %u: %.100s", buf, |
| 4649 | 6000 + display_number, strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4650 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4651 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4652 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4653 | return sock; |
| 4654 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4655 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4656 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4657 | * Requests forwarding of X11 connections, generates fake authentication |
| 4658 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4659 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4660 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4661 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4662 | x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id, |
| 4663 | const char *disp, const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4664 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4665 | struct ssh_channels *sc = ssh->chanctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4666 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4667 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4668 | const char *cp; |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4669 | char *new_data; |
| 4670 | int r, screen_number; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4671 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4672 | if (sc->x11_saved_display == NULL) |
| 4673 | sc->x11_saved_display = xstrdup(disp); |
| 4674 | else if (strcmp(disp, sc->x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4675 | error("x11_request_forwarding_with_spoofing: different " |
| 4676 | "$DISPLAY already forwarded"); |
| 4677 | return; |
| 4678 | } |
| 4679 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4680 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4681 | if (cp) |
| 4682 | cp = strchr(cp, '.'); |
| 4683 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4684 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4685 | else |
| 4686 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4687 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4688 | if (sc->x11_saved_proto == NULL) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4689 | /* Save protocol name. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4690 | sc->x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4691 | |
| 4692 | /* Extract real authentication data. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4693 | sc->x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4694 | for (i = 0; i < data_len; i++) { |
| 4695 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4696 | fatal("x11_request_forwarding: bad " |
| 4697 | "authentication data: %.100s", data); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4698 | sc->x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4699 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4700 | sc->x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4701 | |
| 4702 | /* Generate fake data of the same length. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4703 | sc->x11_fake_data = xmalloc(data_len); |
| 4704 | arc4random_buf(sc->x11_fake_data, data_len); |
| 4705 | sc->x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4706 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4707 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4708 | /* Convert the fake data into hex. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4709 | new_data = tohex(sc->x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4710 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4711 | /* Send the request packet. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 4712 | channel_request_start(ssh, client_session_id, "x11-req", want_reply); |
| 4713 | if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */ |
| 4714 | (r = sshpkt_put_cstring(ssh, proto)) != 0 || |
| 4715 | (r = sshpkt_put_cstring(ssh, new_data)) != 0 || |
| 4716 | (r = sshpkt_put_u32(ssh, screen_number)) != 0 || |
| 4717 | (r = sshpkt_send(ssh)) != 0 || |
| 4718 | (r = ssh_packet_write_wait(ssh)) != 0) |
| 4719 | fatal("%s: send x11-req: %s", __func__, ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4720 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4721 | } |