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