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