markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.363 2017/05/30 14:23:52 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 |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2397 | channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2398 | { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2399 | struct sshbuf *b = NULL; |
| 2400 | Channel *downstream; |
| 2401 | const u_char *cp = NULL; |
| 2402 | size_t len; |
| 2403 | int r; |
| 2404 | |
| 2405 | /* |
| 2406 | * When receiving packets from the peer we need to check whether we |
| 2407 | * need to forward the packets to the mux client. In this case we |
| 2408 | * restore the orignal channel id and keep track of CLOSE messages, |
| 2409 | * so we can cleanup the channel. |
| 2410 | */ |
| 2411 | if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY) |
| 2412 | return 0; |
| 2413 | if ((downstream = c->mux_ctx) == NULL) |
| 2414 | return 0; |
| 2415 | switch (type) { |
| 2416 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2417 | case SSH2_MSG_CHANNEL_DATA: |
| 2418 | case SSH2_MSG_CHANNEL_EOF: |
| 2419 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
| 2420 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2421 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
| 2422 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 2423 | case SSH2_MSG_CHANNEL_SUCCESS: |
| 2424 | case SSH2_MSG_CHANNEL_FAILURE: |
| 2425 | case SSH2_MSG_CHANNEL_REQUEST: |
| 2426 | break; |
| 2427 | default: |
| 2428 | debug2("%s: channel %u: unsupported type %u", __func__, |
| 2429 | c->self, type); |
| 2430 | return 0; |
| 2431 | } |
| 2432 | if ((b = sshbuf_new()) == NULL) { |
| 2433 | error("%s: alloc reply", __func__); |
| 2434 | goto out; |
| 2435 | } |
| 2436 | /* get remaining payload (after id) */ |
| 2437 | cp = sshpkt_ptr(ssh, &len); |
| 2438 | if (cp == NULL) { |
| 2439 | error("%s: no packet", __func__); |
| 2440 | goto out; |
| 2441 | } |
| 2442 | /* translate id and send to muxclient */ |
| 2443 | if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ |
| 2444 | (r = sshbuf_put_u8(b, type)) != 0 || |
| 2445 | (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 || |
| 2446 | (r = sshbuf_put(b, cp, len)) != 0 || |
| 2447 | (r = sshbuf_put_stringb(&downstream->output, b)) != 0) { |
| 2448 | error("%s: compose for muxclient %s", __func__, ssh_err(r)); |
| 2449 | goto out; |
| 2450 | } |
| 2451 | /* sshbuf_dump(b, stderr); */ |
| 2452 | if (ssh_packet_log_type(type)) |
| 2453 | debug3("%s: channel %u: up->down: type %u", __func__, c->self, |
| 2454 | type); |
| 2455 | out: |
| 2456 | /* update state */ |
| 2457 | switch (type) { |
| 2458 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
| 2459 | /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ |
| 2460 | if (cp && len > 4) |
| 2461 | c->remote_id = PEEK_U32(cp); |
| 2462 | break; |
| 2463 | case SSH2_MSG_CHANNEL_CLOSE: |
| 2464 | if (c->flags & CHAN_CLOSE_SENT) |
| 2465 | channel_free(c); |
| 2466 | else |
| 2467 | c->flags |= CHAN_CLOSE_RCVD; |
| 2468 | break; |
| 2469 | } |
| 2470 | sshbuf_free(b); |
| 2471 | return 1; |
| 2472 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2473 | |
| 2474 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2475 | |
| 2476 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2477 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2478 | channel_input_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2479 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2480 | int id; |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 2481 | const u_char *data; |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2482 | u_int data_len, win_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2483 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2484 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2485 | /* Get the channel number and verify it. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2486 | id = packet_get_int(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2487 | c = channel_lookup(id); |
| 2488 | if (c == NULL) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2489 | packet_disconnect("Received data for nonexistent channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2490 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2491 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2492 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2493 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2494 | if (c->type != SSH_CHANNEL_OPEN && |
| 2495 | c->type != SSH_CHANNEL_X11_OPEN) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2496 | return 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2497 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2498 | /* Get the data. */ |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 2499 | data = packet_get_string_ptr(&data_len); |
Damien Miller | 7d45718 | 2010-08-05 23:09:48 +1000 | [diff] [blame] | 2500 | win_len = data_len; |
| 2501 | if (c->datagram) |
| 2502 | win_len += 4; /* string length header */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2503 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2504 | /* |
| 2505 | * Ignore data for protocol > 1.3 if output end is no longer open. |
| 2506 | * For protocol 2 the sending side is reducing its window as it sends |
| 2507 | * data, so we must 'fake' consumption of the data in order to ensure |
| 2508 | * that window updates are sent back. Otherwise the connection might |
| 2509 | * deadlock. |
| 2510 | */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2511 | if (c->ostate != CHAN_OUTPUT_OPEN) { |
| 2512 | c->local_window -= win_len; |
| 2513 | c->local_consumed += win_len; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2514 | return 0; |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2515 | } |
| 2516 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2517 | if (win_len > c->local_maxpacket) { |
| 2518 | logit("channel %d: rcvd big packet %d, maxpack %d", |
| 2519 | c->self, win_len, c->local_maxpacket); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2520 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2521 | if (win_len > c->local_window) { |
| 2522 | logit("channel %d: rcvd too much data %d, win %d", |
| 2523 | c->self, win_len, c->local_window); |
| 2524 | return 0; |
| 2525 | } |
| 2526 | c->local_window -= win_len; |
| 2527 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 2528 | if (c->datagram) |
| 2529 | buffer_put_string(&c->output, data, data_len); |
| 2530 | else |
| 2531 | buffer_append(&c->output, data, data_len); |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 2532 | packet_check_eom(); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2533 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2534 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2535 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2536 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2537 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2538 | channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2539 | { |
| 2540 | int id; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2541 | char *data; |
Ben Lindstrom | daa2179 | 2002-06-25 23:15:30 +0000 | [diff] [blame] | 2542 | u_int data_len, tcode; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2543 | Channel *c; |
| 2544 | |
| 2545 | /* Get the channel number and verify it. */ |
| 2546 | id = packet_get_int(); |
| 2547 | c = channel_lookup(id); |
| 2548 | |
| 2549 | if (c == NULL) |
| 2550 | packet_disconnect("Received extended_data for bad channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2551 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2552 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2553 | if (c->type != SSH_CHANNEL_OPEN) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2554 | logit("channel %d: ext data for non open", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2555 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2556 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2557 | if (c->flags & CHAN_EOF_RCVD) { |
| 2558 | if (datafellows & SSH_BUG_EXTEOF) |
| 2559 | debug("channel %d: accepting ext data after eof", id); |
| 2560 | else |
| 2561 | packet_disconnect("Received extended_data after EOF " |
| 2562 | "on channel %d.", id); |
| 2563 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2564 | tcode = packet_get_int(); |
| 2565 | if (c->efd == -1 || |
| 2566 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 2567 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2568 | logit("channel %d: bad ext data", c->self); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2569 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2570 | } |
| 2571 | data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2572 | packet_check_eom(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2573 | if (data_len > c->local_window) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2574 | logit("channel %d: rcvd too much extended_data %d, win %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2575 | c->self, data_len, c->local_window); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2576 | free(data); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2577 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2578 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2579 | debug2("channel %d: rcvd ext data %d", c->self, data_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2580 | c->local_window -= data_len; |
| 2581 | buffer_append(&c->extended, data, data_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2582 | free(data); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2583 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2584 | } |
| 2585 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2586 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2587 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2588 | channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2589 | { |
| 2590 | int id; |
| 2591 | Channel *c; |
| 2592 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2593 | id = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2594 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2595 | c = channel_lookup(id); |
| 2596 | if (c == NULL) |
| 2597 | packet_disconnect("Received ieof for nonexistent channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2598 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2599 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2600 | chan_rcvd_ieof(c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2601 | |
| 2602 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 2603 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2604 | debug("channel %d: FORCE input drain", c->self); |
| 2605 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
Damien Miller | 73f1074 | 2002-01-22 23:34:52 +1100 | [diff] [blame] | 2606 | if (buffer_len(&c->input) == 0) |
| 2607 | chan_ibuf_empty(c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2608 | } |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2609 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2610 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2611 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2612 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2613 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2614 | channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2615 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2616 | int id = packet_get_int(); |
| 2617 | Channel *c = channel_lookup(id); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 2618 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2619 | if (c == NULL) |
| 2620 | packet_disconnect("Received oclose for nonexistent channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2621 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2622 | return 0; |
| 2623 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2624 | chan_rcvd_oclose(c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2625 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2626 | } |
| 2627 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2628 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2629 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2630 | channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2631 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2632 | int id, remote_id; |
| 2633 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2634 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2635 | id = packet_get_int(); |
| 2636 | c = channel_lookup(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2637 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2638 | if (c==NULL) |
| 2639 | packet_disconnect("Received open confirmation for " |
| 2640 | "unknown channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2641 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2642 | return 0; |
| 2643 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2644 | packet_disconnect("Received open confirmation for " |
| 2645 | "non-opening channel %d.", id); |
| 2646 | remote_id = packet_get_int(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2647 | /* 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] | 2648 | c->remote_id = remote_id; |
| 2649 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2650 | |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2651 | c->remote_window = packet_get_int(); |
| 2652 | c->remote_maxpacket = packet_get_int(); |
| 2653 | if (c->open_confirm) { |
| 2654 | debug2("callback start"); |
| 2655 | c->open_confirm(c->self, 1, c->open_confirm_ctx); |
| 2656 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2657 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2658 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
| 2659 | c->remote_window, c->remote_maxpacket); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2660 | packet_check_eom(); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2661 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2662 | } |
| 2663 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2664 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2665 | reason2txt(int reason) |
| 2666 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 2667 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2668 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 2669 | return "administratively prohibited"; |
| 2670 | case SSH2_OPEN_CONNECT_FAILED: |
| 2671 | return "connect failed"; |
| 2672 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 2673 | return "unknown channel type"; |
| 2674 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 2675 | return "resource shortage"; |
| 2676 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 2677 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2680 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2681 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2682 | channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2683 | { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2684 | int id, reason; |
| 2685 | char *msg = NULL, *lang = NULL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2686 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2687 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2688 | id = packet_get_int(); |
| 2689 | c = channel_lookup(id); |
| 2690 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2691 | if (c==NULL) |
| 2692 | packet_disconnect("Received open failure for " |
| 2693 | "unknown channel %d.", id); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2694 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2695 | return 0; |
| 2696 | if (c->type != SSH_CHANNEL_OPENING) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2697 | packet_disconnect("Received open failure for " |
| 2698 | "non-opening channel %d.", id); |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 2699 | reason = packet_get_int(); |
| 2700 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
| 2701 | msg = packet_get_string(NULL); |
| 2702 | lang = packet_get_string(NULL); |
| 2703 | } |
| 2704 | logit("channel %d: open failed: %s%s%s", id, |
| 2705 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
| 2706 | free(msg); |
| 2707 | free(lang); |
| 2708 | if (c->open_confirm) { |
| 2709 | debug2("callback start"); |
| 2710 | c->open_confirm(c->self, 0, c->open_confirm_ctx); |
| 2711 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2712 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2713 | packet_check_eom(); |
Damien Miller | 7a60621 | 2009-01-28 16:22:34 +1100 | [diff] [blame] | 2714 | /* Schedule the channel for cleanup/deletion. */ |
| 2715 | chan_mark_dead(c); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2716 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2717 | } |
| 2718 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2719 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2720 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2721 | channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2722 | { |
| 2723 | Channel *c; |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 2724 | int id; |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 2725 | u_int adjust, tmp; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2726 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2727 | /* Get the channel number and verify it. */ |
| 2728 | id = packet_get_int(); |
| 2729 | c = channel_lookup(id); |
| 2730 | |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 2731 | if (c == NULL) { |
| 2732 | logit("Received window adjust for non-open channel %d.", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2733 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2734 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2735 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2736 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2737 | adjust = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2738 | packet_check_eom(); |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 2739 | debug2("channel %d: rcvd adjust %u", id, adjust); |
djm@openbsd.org | 629df77 | 2015-06-30 05:25:07 +0000 | [diff] [blame] | 2740 | if ((tmp = c->remote_window + adjust) < c->remote_window) |
| 2741 | fatal("channel %d: adjust %u overflows remote window %u", |
| 2742 | id, adjust, c->remote_window); |
| 2743 | c->remote_window = tmp; |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2744 | return 0; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2745 | } |
| 2746 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2747 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2748 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2749 | channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2750 | { |
| 2751 | Channel *c; |
| 2752 | struct channel_confirm *cc; |
Damien Miller | 16a7307 | 2008-12-08 09:55:25 +1100 | [diff] [blame] | 2753 | int id; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2754 | |
| 2755 | /* Reset keepalive timeout */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 2756 | packet_set_alive_timeouts(0); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2757 | |
Damien Miller | 16a7307 | 2008-12-08 09:55:25 +1100 | [diff] [blame] | 2758 | id = packet_get_int(); |
Damien Miller | 16a7307 | 2008-12-08 09:55:25 +1100 | [diff] [blame] | 2759 | debug2("channel_input_status_confirm: type %d id %d", type, id); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2760 | |
Damien Miller | 16a7307 | 2008-12-08 09:55:25 +1100 | [diff] [blame] | 2761 | if ((c = channel_lookup(id)) == NULL) { |
| 2762 | logit("channel_input_status_confirm: %d: unknown", id); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2763 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2764 | } |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 2765 | if (channel_proxy_upstream(c, type, seq, ssh)) |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 2766 | return 0; |
| 2767 | packet_check_eom(); |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2768 | if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL) |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2769 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2770 | cc->cb(type, c, cc->ctx); |
| 2771 | TAILQ_REMOVE(&c->status_confirms, cc, entry); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 2772 | explicit_bzero(cc, sizeof(*cc)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2773 | free(cc); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 2774 | return 0; |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 2775 | } |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 2776 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2777 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2778 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 2779 | void |
| 2780 | channel_set_af(int af) |
| 2781 | { |
| 2782 | IPv4or6 = af; |
| 2783 | } |
| 2784 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2785 | |
| 2786 | /* |
| 2787 | * Determine whether or not a port forward listens to loopback, the |
| 2788 | * specified address or wildcard. On the client, a specified bind |
| 2789 | * address will always override gateway_ports. On the server, a |
| 2790 | * gateway_ports of 1 (``yes'') will override the client's specification |
| 2791 | * and force a wildcard bind, whereas a value of 2 (``clientspecified'') |
| 2792 | * will bind to whatever address the client asked for. |
| 2793 | * |
| 2794 | * Special-case listen_addrs are: |
| 2795 | * |
| 2796 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 2797 | * "" (empty string), "*" -> wildcard v4/v6 |
| 2798 | * "localhost" -> loopback v4/v6 |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 2799 | * "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] | 2800 | */ |
| 2801 | static const char * |
| 2802 | channel_fwd_bind_addr(const char *listen_addr, int *wildcardp, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2803 | int is_client, struct ForwardOptions *fwd_opts) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2804 | { |
| 2805 | const char *addr = NULL; |
| 2806 | int wildcard = 0; |
| 2807 | |
| 2808 | if (listen_addr == NULL) { |
| 2809 | /* No address specified: default to gateway_ports setting */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2810 | if (fwd_opts->gateway_ports) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2811 | wildcard = 1; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2812 | } else if (fwd_opts->gateway_ports || is_client) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2813 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 2814 | strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) || |
| 2815 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2816 | (!is_client && fwd_opts->gateway_ports == 1)) { |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2817 | wildcard = 1; |
Darren Tucker | 71152bc | 2013-10-10 10:27:21 +1100 | [diff] [blame] | 2818 | /* |
| 2819 | * Notify client if they requested a specific listen |
| 2820 | * address and it was overridden. |
| 2821 | */ |
| 2822 | if (*listen_addr != '\0' && |
| 2823 | strcmp(listen_addr, "0.0.0.0") != 0 && |
| 2824 | strcmp(listen_addr, "*") != 0) { |
| 2825 | packet_send_debug("Forwarding listen address " |
| 2826 | "\"%s\" overridden by server " |
| 2827 | "GatewayPorts", listen_addr); |
| 2828 | } |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 2829 | } else if (strcmp(listen_addr, "localhost") != 0 || |
| 2830 | strcmp(listen_addr, "127.0.0.1") == 0 || |
| 2831 | strcmp(listen_addr, "::1") == 0) { |
| 2832 | /* Accept localhost address when GatewayPorts=yes */ |
Damien Miller | e84d103 | 2014-05-21 17:13:36 +1000 | [diff] [blame] | 2833 | addr = listen_addr; |
Damien Miller | 602943d | 2014-07-04 08:59:41 +1000 | [diff] [blame] | 2834 | } |
| 2835 | } else if (strcmp(listen_addr, "127.0.0.1") == 0 || |
| 2836 | strcmp(listen_addr, "::1") == 0) { |
| 2837 | /* |
| 2838 | * If a specific IPv4/IPv6 localhost address has been |
| 2839 | * requested then accept it even if gateway_ports is in |
| 2840 | * effect. This allows the client to prefer IPv4 or IPv6. |
| 2841 | */ |
| 2842 | addr = listen_addr; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2843 | } |
| 2844 | if (wildcardp != NULL) |
| 2845 | *wildcardp = wildcard; |
| 2846 | return addr; |
| 2847 | } |
| 2848 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2849 | static int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2850 | channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd, |
| 2851 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2852 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2853 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 2854 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2855 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2856 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2857 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2858 | in_port_t *lport_p; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2859 | |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2860 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2861 | |
millert@openbsd.org | ec04dc4 | 2015-06-05 15:13:13 +0000 | [diff] [blame] | 2862 | if (is_client && fwd->connect_path != NULL) { |
| 2863 | host = fwd->connect_path; |
| 2864 | } else { |
| 2865 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 2866 | fwd->listen_host : fwd->connect_host; |
| 2867 | if (host == NULL) { |
| 2868 | error("No forward host name."); |
| 2869 | return 0; |
| 2870 | } |
| 2871 | if (strlen(host) >= NI_MAXHOST) { |
| 2872 | error("Forward host name too long."); |
| 2873 | return 0; |
| 2874 | } |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2875 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2876 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2877 | /* Determine the bind address, cf. channel_fwd_bind_addr() comment */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2878 | addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard, |
| 2879 | is_client, fwd_opts); |
| 2880 | debug3("%s: type %d wildcard %d addr %s", __func__, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2881 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 2882 | |
| 2883 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2884 | * getaddrinfo returns a loopback address if the hostname is |
| 2885 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2886 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2887 | memset(&hints, 0, sizeof(hints)); |
| 2888 | hints.ai_family = IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2889 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2890 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2891 | snprintf(strport, sizeof strport, "%d", fwd->listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2892 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 2893 | if (addr == NULL) { |
| 2894 | /* This really shouldn't happen */ |
| 2895 | packet_disconnect("getaddrinfo: fatal error: %s", |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 2896 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2897 | } else { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2898 | error("%s: getaddrinfo(%.64s): %s", __func__, addr, |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 2899 | ssh_gai_strerror(r)); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2900 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 2901 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2902 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2903 | if (allocated_listen_port != NULL) |
| 2904 | *allocated_listen_port = 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2905 | for (ai = aitop; ai; ai = ai->ai_next) { |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2906 | switch (ai->ai_family) { |
| 2907 | case AF_INET: |
| 2908 | lport_p = &((struct sockaddr_in *)ai->ai_addr)-> |
| 2909 | sin_port; |
| 2910 | break; |
| 2911 | case AF_INET6: |
| 2912 | lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> |
| 2913 | sin6_port; |
| 2914 | break; |
| 2915 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2916 | continue; |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2917 | } |
| 2918 | /* |
| 2919 | * If allocating a port for -R forwards, then use the |
| 2920 | * same port for all address families. |
| 2921 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2922 | if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2923 | allocated_listen_port != NULL && *allocated_listen_port > 0) |
| 2924 | *lport_p = htons(*allocated_listen_port); |
| 2925 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2926 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 2927 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2928 | error("%s: getnameinfo failed", __func__); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2929 | continue; |
| 2930 | } |
| 2931 | /* Create a port to listen for the host. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 2932 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2933 | if (sock < 0) { |
| 2934 | /* this is no error since kernel may not support ipv6 */ |
| 2935 | verbose("socket: %.100s", strerror(errno)); |
| 2936 | continue; |
| 2937 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 2938 | |
| 2939 | channel_set_reuseaddr(sock); |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 2940 | if (ai->ai_family == AF_INET6) |
| 2941 | sock_set_v6only(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 2942 | |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2943 | debug("Local forwarding listening on %s port %s.", |
| 2944 | ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2945 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2946 | /* Bind the socket to the address. */ |
| 2947 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 2948 | /* address can be in use ipv6 address is already bound */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 2949 | if (!ai->ai_next) |
| 2950 | error("bind: %.100s", strerror(errno)); |
| 2951 | else |
| 2952 | verbose("bind: %.100s", strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 2953 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2954 | close(sock); |
| 2955 | continue; |
| 2956 | } |
| 2957 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 2958 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2959 | error("listen: %.100s", strerror(errno)); |
| 2960 | close(sock); |
| 2961 | continue; |
| 2962 | } |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2963 | |
| 2964 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2965 | * fwd->listen_port == 0 requests a dynamically allocated port - |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2966 | * record what we got. |
| 2967 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2968 | if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 && |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2969 | allocated_listen_port != NULL && |
| 2970 | *allocated_listen_port == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 2971 | *allocated_listen_port = get_local_port(sock); |
Damien Miller | 4bf648f | 2009-02-14 16:28:21 +1100 | [diff] [blame] | 2972 | debug("Allocated listen port %d", |
| 2973 | *allocated_listen_port); |
| 2974 | } |
| 2975 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2976 | /* Allocate a channel number for the socket. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2977 | c = channel_new("port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2978 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 2979 | 0, "port listener", 1); |
Damien Miller | a1c1b6c | 2009-01-28 16:29:49 +1100 | [diff] [blame] | 2980 | c->path = xstrdup(host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2981 | c->host_port = fwd->connect_port; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 2982 | c->listening_addr = addr == NULL ? NULL : xstrdup(addr); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2983 | if (fwd->listen_port == 0 && allocated_listen_port != NULL && |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 2984 | !(datafellows & SSH_BUG_DYNAMIC_RPORT)) |
| 2985 | c->listening_port = *allocated_listen_port; |
| 2986 | else |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2987 | c->listening_port = fwd->listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2988 | success = 1; |
| 2989 | } |
| 2990 | if (success == 0) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2991 | error("%s: cannot listen to port: %d", __func__, |
| 2992 | fwd->listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2993 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2994 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2995 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2996 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 2997 | static int |
| 2998 | channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd, |
| 2999 | struct ForwardOptions *fwd_opts) |
| 3000 | { |
| 3001 | struct sockaddr_un sunaddr; |
| 3002 | const char *path; |
| 3003 | Channel *c; |
| 3004 | int port, sock; |
| 3005 | mode_t omask; |
| 3006 | |
| 3007 | switch (type) { |
| 3008 | case SSH_CHANNEL_UNIX_LISTENER: |
| 3009 | if (fwd->connect_path != NULL) { |
| 3010 | if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) { |
| 3011 | error("Local connecting path too long: %s", |
| 3012 | fwd->connect_path); |
| 3013 | return 0; |
| 3014 | } |
| 3015 | path = fwd->connect_path; |
| 3016 | port = PORT_STREAMLOCAL; |
| 3017 | } else { |
| 3018 | if (fwd->connect_host == NULL) { |
| 3019 | error("No forward host name."); |
| 3020 | return 0; |
| 3021 | } |
| 3022 | if (strlen(fwd->connect_host) >= NI_MAXHOST) { |
| 3023 | error("Forward host name too long."); |
| 3024 | return 0; |
| 3025 | } |
| 3026 | path = fwd->connect_host; |
| 3027 | port = fwd->connect_port; |
| 3028 | } |
| 3029 | break; |
| 3030 | case SSH_CHANNEL_RUNIX_LISTENER: |
| 3031 | path = fwd->listen_path; |
| 3032 | port = PORT_STREAMLOCAL; |
| 3033 | break; |
| 3034 | default: |
| 3035 | error("%s: unexpected channel type %d", __func__, type); |
| 3036 | return 0; |
| 3037 | } |
| 3038 | |
| 3039 | if (fwd->listen_path == NULL) { |
| 3040 | error("No forward path name."); |
| 3041 | return 0; |
| 3042 | } |
| 3043 | if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) { |
| 3044 | error("Local listening path too long: %s", fwd->listen_path); |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
| 3048 | debug3("%s: type %d path %s", __func__, type, fwd->listen_path); |
| 3049 | |
| 3050 | /* Start a Unix domain listener. */ |
| 3051 | omask = umask(fwd_opts->streamlocal_bind_mask); |
| 3052 | sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG, |
| 3053 | fwd_opts->streamlocal_bind_unlink); |
| 3054 | umask(omask); |
| 3055 | if (sock < 0) |
| 3056 | return 0; |
| 3057 | |
| 3058 | debug("Local forwarding listening on path %s.", fwd->listen_path); |
| 3059 | |
| 3060 | /* Allocate a channel number for the socket. */ |
| 3061 | c = channel_new("unix listener", type, sock, sock, -1, |
| 3062 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
| 3063 | 0, "unix listener", 1); |
| 3064 | c->path = xstrdup(path); |
| 3065 | c->host_port = port; |
| 3066 | c->listening_port = PORT_STREAMLOCAL; |
| 3067 | c->listening_addr = xstrdup(fwd->listen_path); |
| 3068 | return 1; |
| 3069 | } |
| 3070 | |
| 3071 | static int |
| 3072 | channel_cancel_rport_listener_tcpip(const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3073 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3074 | u_int i; |
| 3075 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3076 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 3077 | for (i = 0; i < channels_alloc; i++) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3078 | Channel *c = channels[i]; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3079 | if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER) |
| 3080 | continue; |
| 3081 | if (strcmp(c->path, host) == 0 && c->listening_port == port) { |
| 3082 | debug2("%s: close channel %d", __func__, i); |
| 3083 | channel_free(c); |
| 3084 | found = 1; |
| 3085 | } |
| 3086 | } |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3087 | |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3088 | return (found); |
| 3089 | } |
| 3090 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3091 | static int |
| 3092 | channel_cancel_rport_listener_streamlocal(const char *path) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3093 | { |
| 3094 | u_int i; |
| 3095 | int found = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3096 | |
| 3097 | for (i = 0; i < channels_alloc; i++) { |
| 3098 | Channel *c = channels[i]; |
| 3099 | if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER) |
| 3100 | continue; |
| 3101 | if (c->path == NULL) |
| 3102 | continue; |
| 3103 | if (strcmp(c->path, path) == 0) { |
| 3104 | debug2("%s: close channel %d", __func__, i); |
| 3105 | channel_free(c); |
| 3106 | found = 1; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | return (found); |
| 3111 | } |
| 3112 | |
| 3113 | int |
| 3114 | channel_cancel_rport_listener(struct Forward *fwd) |
| 3115 | { |
| 3116 | if (fwd->listen_path != NULL) |
| 3117 | return channel_cancel_rport_listener_streamlocal(fwd->listen_path); |
| 3118 | else |
| 3119 | return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port); |
| 3120 | } |
| 3121 | |
| 3122 | static int |
| 3123 | channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport, |
| 3124 | int cport, struct ForwardOptions *fwd_opts) |
| 3125 | { |
| 3126 | u_int i; |
| 3127 | int found = 0; |
| 3128 | const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3129 | |
| 3130 | for (i = 0; i < channels_alloc; i++) { |
| 3131 | Channel *c = channels[i]; |
| 3132 | if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER) |
| 3133 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3134 | if (c->listening_port != lport) |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3135 | continue; |
Damien Miller | ff77364 | 2011-09-22 21:39:48 +1000 | [diff] [blame] | 3136 | if (cport == CHANNEL_CANCEL_PORT_STATIC) { |
| 3137 | /* skip dynamic forwardings */ |
| 3138 | if (c->host_port == 0) |
| 3139 | continue; |
| 3140 | } else { |
| 3141 | if (c->host_port != cport) |
| 3142 | continue; |
| 3143 | } |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3144 | if ((c->listening_addr == NULL && addr != NULL) || |
| 3145 | (c->listening_addr != NULL && addr == NULL)) |
| 3146 | continue; |
| 3147 | if (addr == NULL || strcmp(c->listening_addr, addr) == 0) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 3148 | debug2("%s: close channel %d", __func__, i); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3149 | channel_free(c); |
| 3150 | found = 1; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | return (found); |
| 3155 | } |
| 3156 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3157 | static int |
| 3158 | channel_cancel_lport_listener_streamlocal(const char *path) |
| 3159 | { |
| 3160 | u_int i; |
| 3161 | int found = 0; |
| 3162 | |
| 3163 | if (path == NULL) { |
| 3164 | error("%s: no path specified.", __func__); |
| 3165 | return 0; |
| 3166 | } |
| 3167 | |
| 3168 | for (i = 0; i < channels_alloc; i++) { |
| 3169 | Channel *c = channels[i]; |
| 3170 | if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER) |
| 3171 | continue; |
| 3172 | if (c->listening_addr == NULL) |
| 3173 | continue; |
| 3174 | if (strcmp(c->listening_addr, path) == 0) { |
| 3175 | debug2("%s: close channel %d", __func__, i); |
| 3176 | channel_free(c); |
| 3177 | found = 1; |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | return (found); |
| 3182 | } |
| 3183 | |
| 3184 | int |
| 3185 | channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts) |
| 3186 | { |
| 3187 | if (fwd->listen_path != NULL) |
| 3188 | return channel_cancel_lport_listener_streamlocal(fwd->listen_path); |
| 3189 | else |
| 3190 | return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts); |
| 3191 | } |
| 3192 | |
markus@openbsd.org | c221219 | 2017-05-30 08:49:58 +0000 | [diff] [blame] | 3193 | /* protocol local port fwd, used by ssh */ |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3194 | int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3195 | channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3196 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3197 | if (fwd->listen_path != NULL) { |
| 3198 | return channel_setup_fwd_listener_streamlocal( |
| 3199 | SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts); |
| 3200 | } else { |
| 3201 | return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER, |
| 3202 | fwd, NULL, fwd_opts); |
| 3203 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /* protocol v2 remote port fwd, used by sshd */ |
| 3207 | int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3208 | channel_setup_remote_fwd_listener(struct Forward *fwd, |
| 3209 | int *allocated_listen_port, struct ForwardOptions *fwd_opts) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3210 | { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3211 | if (fwd->listen_path != NULL) { |
| 3212 | return channel_setup_fwd_listener_streamlocal( |
| 3213 | SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts); |
| 3214 | } else { |
| 3215 | return channel_setup_fwd_listener_tcpip( |
| 3216 | SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port, |
| 3217 | fwd_opts); |
| 3218 | } |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 3219 | } |
| 3220 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3221 | /* |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3222 | * Translate the requested rfwd listen host to something usable for |
| 3223 | * this server. |
| 3224 | */ |
| 3225 | static const char * |
| 3226 | channel_rfwd_bind_host(const char *listen_host) |
| 3227 | { |
| 3228 | if (listen_host == NULL) { |
| 3229 | if (datafellows & SSH_BUG_RFWD_ADDR) |
| 3230 | return "127.0.0.1"; |
| 3231 | else |
| 3232 | return "localhost"; |
| 3233 | } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) { |
| 3234 | if (datafellows & SSH_BUG_RFWD_ADDR) |
| 3235 | return "0.0.0.0"; |
| 3236 | else |
| 3237 | return ""; |
| 3238 | } else |
| 3239 | return listen_host; |
| 3240 | } |
| 3241 | |
| 3242 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3243 | * Initiate forwarding of connections to port "port" on remote host through |
| 3244 | * the secure channel to host:port from local side. |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3245 | * Returns handle (index) for updating the dynamic listen port with |
| 3246 | * channel_update_permitted_opens(). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3247 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 3248 | int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3249 | channel_request_remote_forwarding(struct Forward *fwd) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3250 | { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3251 | int success = 0, idx = -1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3252 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3253 | /* Send the forward request to the remote side. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3254 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 3255 | if (fwd->listen_path != NULL) { |
| 3256 | packet_put_cstring("streamlocal-forward@openssh.com"); |
| 3257 | packet_put_char(1); /* boolean: want reply */ |
| 3258 | packet_put_cstring(fwd->listen_path); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3259 | } else { |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3260 | packet_put_cstring("tcpip-forward"); |
| 3261 | packet_put_char(1); /* boolean: want reply */ |
| 3262 | packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host)); |
| 3263 | packet_put_int(fwd->listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3264 | } |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 3265 | packet_send(); |
| 3266 | packet_write_wait(); |
| 3267 | /* Assume that server accepts the request */ |
| 3268 | success = 1; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3269 | if (success) { |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3270 | /* Record that connection to this host/port is permitted. */ |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 3271 | permitted_opens = xreallocarray(permitted_opens, |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3272 | num_permitted_opens + 1, sizeof(*permitted_opens)); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3273 | idx = num_permitted_opens++; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3274 | if (fwd->connect_path != NULL) { |
| 3275 | permitted_opens[idx].host_to_connect = |
| 3276 | xstrdup(fwd->connect_path); |
| 3277 | permitted_opens[idx].port_to_connect = |
| 3278 | PORT_STREAMLOCAL; |
| 3279 | } else { |
| 3280 | permitted_opens[idx].host_to_connect = |
| 3281 | xstrdup(fwd->connect_host); |
| 3282 | permitted_opens[idx].port_to_connect = |
| 3283 | fwd->connect_port; |
| 3284 | } |
| 3285 | if (fwd->listen_path != NULL) { |
| 3286 | permitted_opens[idx].listen_host = NULL; |
| 3287 | permitted_opens[idx].listen_path = |
| 3288 | xstrdup(fwd->listen_path); |
| 3289 | permitted_opens[idx].listen_port = PORT_STREAMLOCAL; |
| 3290 | } else { |
| 3291 | permitted_opens[idx].listen_host = |
| 3292 | fwd->listen_host ? xstrdup(fwd->listen_host) : NULL; |
| 3293 | permitted_opens[idx].listen_path = NULL; |
| 3294 | permitted_opens[idx].listen_port = fwd->listen_port; |
| 3295 | } |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3296 | permitted_opens[idx].downstream = NULL; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 3297 | } |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3298 | return (idx); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3299 | } |
| 3300 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3301 | static int |
| 3302 | open_match(ForwardPermission *allowed_open, const char *requestedhost, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3303 | int requestedport) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3304 | { |
| 3305 | if (allowed_open->host_to_connect == NULL) |
| 3306 | return 0; |
| 3307 | if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT && |
| 3308 | allowed_open->port_to_connect != requestedport) |
| 3309 | return 0; |
dtucker@openbsd.org | d7eabc8 | 2016-07-19 11:38:53 +0000 | [diff] [blame] | 3310 | if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 && |
| 3311 | strcmp(allowed_open->host_to_connect, requestedhost) != 0) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3312 | return 0; |
| 3313 | return 1; |
| 3314 | } |
| 3315 | |
| 3316 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3317 | * Note that in the listen host/port case |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3318 | * we don't support FWD_PERMIT_ANY_PORT and |
| 3319 | * need to translate between the configured-host (listen_host) |
| 3320 | * and what we've sent to the remote server (channel_rfwd_bind_host) |
| 3321 | */ |
| 3322 | static int |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3323 | open_listen_match_tcpip(ForwardPermission *allowed_open, |
| 3324 | const char *requestedhost, u_short requestedport, int translate) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3325 | { |
| 3326 | const char *allowed_host; |
| 3327 | |
| 3328 | if (allowed_open->host_to_connect == NULL) |
| 3329 | return 0; |
| 3330 | if (allowed_open->listen_port != requestedport) |
| 3331 | return 0; |
Damien Miller | 3a48cc0 | 2014-07-06 09:32:49 +1000 | [diff] [blame] | 3332 | if (!translate && allowed_open->listen_host == NULL && |
| 3333 | requestedhost == NULL) |
| 3334 | return 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3335 | allowed_host = translate ? |
| 3336 | channel_rfwd_bind_host(allowed_open->listen_host) : |
| 3337 | allowed_open->listen_host; |
| 3338 | if (allowed_host == NULL || |
| 3339 | strcmp(allowed_host, requestedhost) != 0) |
| 3340 | return 0; |
| 3341 | return 1; |
| 3342 | } |
| 3343 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3344 | static int |
| 3345 | open_listen_match_streamlocal(ForwardPermission *allowed_open, |
| 3346 | const char *requestedpath) |
| 3347 | { |
| 3348 | if (allowed_open->host_to_connect == NULL) |
| 3349 | return 0; |
| 3350 | if (allowed_open->listen_port != PORT_STREAMLOCAL) |
| 3351 | return 0; |
| 3352 | if (allowed_open->listen_path == NULL || |
| 3353 | strcmp(allowed_open->listen_path, requestedpath) != 0) |
| 3354 | return 0; |
| 3355 | return 1; |
| 3356 | } |
| 3357 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3358 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 3359 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3360 | * local side. |
| 3361 | */ |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3362 | static int |
| 3363 | channel_request_rforward_cancel_tcpip(const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3364 | { |
| 3365 | int i; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3366 | |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3367 | for (i = 0; i < num_permitted_opens; i++) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3368 | if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0)) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3369 | break; |
| 3370 | } |
| 3371 | if (i >= num_permitted_opens) { |
| 3372 | debug("%s: requested forward not found", __func__); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3373 | return -1; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3374 | } |
| 3375 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 3376 | packet_put_cstring("cancel-tcpip-forward"); |
| 3377 | packet_put_char(0); |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3378 | packet_put_cstring(channel_rfwd_bind_host(host)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3379 | packet_put_int(port); |
| 3380 | packet_send(); |
| 3381 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3382 | permitted_opens[i].listen_port = 0; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3383 | permitted_opens[i].port_to_connect = 0; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3384 | free(permitted_opens[i].host_to_connect); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3385 | permitted_opens[i].host_to_connect = NULL; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3386 | free(permitted_opens[i].listen_host); |
| 3387 | permitted_opens[i].listen_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3388 | permitted_opens[i].listen_path = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3389 | permitted_opens[i].downstream = NULL; |
Damien Miller | f6dff7c | 2011-09-22 21:38:52 +1000 | [diff] [blame] | 3390 | |
| 3391 | return 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /* |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3395 | * Request cancellation of remote forwarding of Unix domain socket |
| 3396 | * path from local side. |
| 3397 | */ |
| 3398 | static int |
| 3399 | channel_request_rforward_cancel_streamlocal(const char *path) |
| 3400 | { |
| 3401 | int i; |
| 3402 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3403 | for (i = 0; i < num_permitted_opens; i++) { |
| 3404 | if (open_listen_match_streamlocal(&permitted_opens[i], path)) |
| 3405 | break; |
| 3406 | } |
| 3407 | if (i >= num_permitted_opens) { |
| 3408 | debug("%s: requested forward not found", __func__); |
| 3409 | return -1; |
| 3410 | } |
| 3411 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 3412 | packet_put_cstring("cancel-streamlocal-forward@openssh.com"); |
| 3413 | packet_put_char(0); |
| 3414 | packet_put_cstring(path); |
| 3415 | packet_send(); |
| 3416 | |
| 3417 | permitted_opens[i].listen_port = 0; |
| 3418 | permitted_opens[i].port_to_connect = 0; |
| 3419 | free(permitted_opens[i].host_to_connect); |
| 3420 | permitted_opens[i].host_to_connect = NULL; |
| 3421 | permitted_opens[i].listen_host = NULL; |
| 3422 | free(permitted_opens[i].listen_path); |
| 3423 | permitted_opens[i].listen_path = NULL; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3424 | permitted_opens[i].downstream = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3425 | |
| 3426 | return 0; |
| 3427 | } |
| 3428 | |
| 3429 | /* |
| 3430 | * Request cancellation of remote forwarding of a connection from local side. |
| 3431 | */ |
| 3432 | int |
| 3433 | channel_request_rforward_cancel(struct Forward *fwd) |
| 3434 | { |
| 3435 | if (fwd->listen_path != NULL) { |
| 3436 | return (channel_request_rforward_cancel_streamlocal( |
| 3437 | fwd->listen_path)); |
| 3438 | } else { |
| 3439 | return (channel_request_rforward_cancel_tcpip(fwd->listen_host, |
| 3440 | fwd->listen_port ? fwd->listen_port : fwd->allocated_port)); |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | /* |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3445 | * Permits opening to any host/port if permitted_opens[] is empty. This is |
| 3446 | * usually called by the server, because the user could connect to any port |
| 3447 | * anyway, and the server has no way to know but to trust the client anyway. |
| 3448 | */ |
| 3449 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 3450 | channel_permit_all_opens(void) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3451 | { |
| 3452 | if (num_permitted_opens == 0) |
| 3453 | all_opens_permitted = 1; |
| 3454 | } |
| 3455 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 3456 | void |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3457 | channel_add_permitted_opens(char *host, int port) |
| 3458 | { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3459 | debug("allow port forwarding to host %s port %d", host, port); |
| 3460 | |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 3461 | permitted_opens = xreallocarray(permitted_opens, |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3462 | num_permitted_opens + 1, sizeof(*permitted_opens)); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3463 | permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host); |
| 3464 | permitted_opens[num_permitted_opens].port_to_connect = port; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3465 | permitted_opens[num_permitted_opens].listen_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3466 | permitted_opens[num_permitted_opens].listen_path = NULL; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3467 | permitted_opens[num_permitted_opens].listen_port = 0; |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3468 | permitted_opens[num_permitted_opens].downstream = NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3469 | num_permitted_opens++; |
| 3470 | |
| 3471 | all_opens_permitted = 0; |
| 3472 | } |
| 3473 | |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3474 | /* |
| 3475 | * Update the listen port for a dynamic remote forward, after |
| 3476 | * the actual 'newport' has been allocated. If 'newport' < 0 is |
| 3477 | * passed then they entry will be invalidated. |
| 3478 | */ |
| 3479 | void |
| 3480 | channel_update_permitted_opens(int idx, int newport) |
| 3481 | { |
| 3482 | if (idx < 0 || idx >= num_permitted_opens) { |
| 3483 | debug("channel_update_permitted_opens: index out of range:" |
| 3484 | " %d num_permitted_opens %d", idx, num_permitted_opens); |
| 3485 | return; |
| 3486 | } |
| 3487 | debug("%s allowed port %d for forwarding to host %s port %d", |
| 3488 | newport > 0 ? "Updating" : "Removing", |
| 3489 | newport, |
| 3490 | permitted_opens[idx].host_to_connect, |
| 3491 | permitted_opens[idx].port_to_connect); |
| 3492 | if (newport >= 0) { |
| 3493 | permitted_opens[idx].listen_port = |
| 3494 | (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport; |
| 3495 | } else { |
| 3496 | permitted_opens[idx].listen_port = 0; |
| 3497 | permitted_opens[idx].port_to_connect = 0; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3498 | free(permitted_opens[idx].host_to_connect); |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3499 | permitted_opens[idx].host_to_connect = NULL; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3500 | free(permitted_opens[idx].listen_host); |
| 3501 | permitted_opens[idx].listen_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3502 | free(permitted_opens[idx].listen_path); |
| 3503 | permitted_opens[idx].listen_path = NULL; |
Darren Tucker | 68afb8c | 2011-10-02 18:59:03 +1100 | [diff] [blame] | 3504 | } |
| 3505 | } |
| 3506 | |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3507 | int |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3508 | channel_add_adm_permitted_opens(char *host, int port) |
| 3509 | { |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3510 | debug("config allows port forwarding to host %s port %d", host, port); |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3511 | |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 3512 | permitted_adm_opens = xreallocarray(permitted_adm_opens, |
Damien Miller | 232cfb1 | 2010-06-26 09:50:30 +1000 | [diff] [blame] | 3513 | num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens)); |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3514 | permitted_adm_opens[num_adm_permitted_opens].host_to_connect |
| 3515 | = xstrdup(host); |
| 3516 | permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3517 | permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3518 | permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3519 | permitted_adm_opens[num_adm_permitted_opens].listen_port = 0; |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 3520 | return ++num_adm_permitted_opens; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | void |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3524 | channel_disable_adm_local_opens(void) |
| 3525 | { |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 3526 | channel_clear_adm_permitted_opens(); |
dtucker@openbsd.org | 297060f | 2015-05-08 03:25:07 +0000 | [diff] [blame] | 3527 | permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1); |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 3528 | permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL; |
| 3529 | num_adm_permitted_opens = 1; |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | void |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3533 | channel_clear_permitted_opens(void) |
| 3534 | { |
| 3535 | int i; |
| 3536 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3537 | for (i = 0; i < num_permitted_opens; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3538 | free(permitted_opens[i].host_to_connect); |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3539 | free(permitted_opens[i].listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3540 | free(permitted_opens[i].listen_path); |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3541 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3542 | free(permitted_opens); |
| 3543 | permitted_opens = NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3544 | num_permitted_opens = 0; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3545 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3546 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3547 | void |
| 3548 | channel_clear_adm_permitted_opens(void) |
| 3549 | { |
| 3550 | int i; |
| 3551 | |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3552 | for (i = 0; i < num_adm_permitted_opens; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3553 | free(permitted_adm_opens[i].host_to_connect); |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3554 | free(permitted_adm_opens[i].listen_host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3555 | free(permitted_adm_opens[i].listen_path); |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3556 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3557 | free(permitted_adm_opens); |
| 3558 | permitted_adm_opens = NULL; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3559 | num_adm_permitted_opens = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3562 | void |
| 3563 | channel_print_adm_permitted_opens(void) |
| 3564 | { |
Damien Miller | 6ef1749 | 2008-07-16 22:42:06 +1000 | [diff] [blame] | 3565 | int i; |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3566 | |
Damien Miller | b53d8a1 | 2009-01-28 16:13:04 +1100 | [diff] [blame] | 3567 | printf("permitopen"); |
Darren Tucker | 22662e8 | 2008-11-11 16:40:22 +1100 | [diff] [blame] | 3568 | if (num_adm_permitted_opens == 0) { |
Damien Miller | b53d8a1 | 2009-01-28 16:13:04 +1100 | [diff] [blame] | 3569 | printf(" any\n"); |
Darren Tucker | 22662e8 | 2008-11-11 16:40:22 +1100 | [diff] [blame] | 3570 | return; |
| 3571 | } |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3572 | for (i = 0; i < num_adm_permitted_opens; i++) |
Damien Miller | c608148 | 2012-04-22 11:18:53 +1000 | [diff] [blame] | 3573 | if (permitted_adm_opens[i].host_to_connect == NULL) |
| 3574 | printf(" none"); |
| 3575 | else |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3576 | printf(" %s:%d", permitted_adm_opens[i].host_to_connect, |
| 3577 | permitted_adm_opens[i].port_to_connect); |
Damien Miller | b53d8a1 | 2009-01-28 16:13:04 +1100 | [diff] [blame] | 3578 | printf("\n"); |
Darren Tucker | e7140f2 | 2008-06-10 23:01:51 +1000 | [diff] [blame] | 3579 | } |
| 3580 | |
Darren Tucker | 1338b9e | 2011-10-02 18:57:35 +1100 | [diff] [blame] | 3581 | /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */ |
| 3582 | int |
| 3583 | permitopen_port(const char *p) |
| 3584 | { |
| 3585 | int port; |
| 3586 | |
| 3587 | if (strcmp(p, "*") == 0) |
| 3588 | return FWD_PERMIT_ANY_PORT; |
| 3589 | if ((port = a2port(p)) > 0) |
| 3590 | return port; |
| 3591 | return -1; |
| 3592 | } |
| 3593 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3594 | /* Try to start non-blocking connect to next host in cctx list */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3595 | static int |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3596 | connect_next(struct channel_connect *cctx) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3597 | { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3598 | int sock, saved_errno; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3599 | struct sockaddr_un *sunaddr; |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 3600 | char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3601 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3602 | for (; cctx->ai; cctx->ai = cctx->ai->ai_next) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3603 | switch (cctx->ai->ai_family) { |
| 3604 | case AF_UNIX: |
| 3605 | /* unix:pathname instead of host:port */ |
| 3606 | sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr; |
| 3607 | strlcpy(ntop, "unix", sizeof(ntop)); |
| 3608 | strlcpy(strport, sunaddr->sun_path, sizeof(strport)); |
| 3609 | break; |
| 3610 | case AF_INET: |
| 3611 | case AF_INET6: |
| 3612 | if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen, |
| 3613 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 3614 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 3615 | error("connect_next: getnameinfo failed"); |
| 3616 | continue; |
| 3617 | } |
| 3618 | break; |
| 3619 | default: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3620 | continue; |
| 3621 | } |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3622 | if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype, |
| 3623 | cctx->ai->ai_protocol)) == -1) { |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3624 | if (cctx->ai->ai_next == NULL) |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 3625 | error("socket: %.100s", strerror(errno)); |
| 3626 | else |
| 3627 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3628 | continue; |
| 3629 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 3630 | if (set_nonblock(sock) == -1) |
| 3631 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3632 | if (connect(sock, cctx->ai->ai_addr, |
| 3633 | cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) { |
| 3634 | debug("connect_next: host %.100s ([%.100s]:%s): " |
| 3635 | "%.100s", cctx->host, ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3636 | strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3637 | saved_errno = errno; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3638 | close(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3639 | errno = saved_errno; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 3640 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3641 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3642 | if (cctx->ai->ai_family != AF_UNIX) |
| 3643 | set_nodelay(sock); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3644 | debug("connect_next: host %.100s ([%.100s]:%s) " |
| 3645 | "in progress, fd=%d", cctx->host, ntop, strport, sock); |
| 3646 | cctx->ai = cctx->ai->ai_next; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3647 | return sock; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3648 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 3649 | return -1; |
| 3650 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3651 | |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3652 | static void |
| 3653 | channel_connect_ctx_free(struct channel_connect *cctx) |
| 3654 | { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 3655 | free(cctx->host); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3656 | if (cctx->aitop) { |
| 3657 | if (cctx->aitop->ai_family == AF_UNIX) |
| 3658 | free(cctx->aitop); |
| 3659 | else |
| 3660 | freeaddrinfo(cctx->aitop); |
| 3661 | } |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 3662 | memset(cctx, 0, sizeof(*cctx)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3663 | } |
| 3664 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3665 | /* |
| 3666 | * Return CONNECTING channel to remote host:port or local socket path, |
| 3667 | * passing back the failure reason if appropriate. |
| 3668 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3669 | static Channel * |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3670 | connect_to_reason(const char *name, int port, char *ctype, char *rname, |
| 3671 | int *reason, const char **errmsg) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3672 | { |
| 3673 | struct addrinfo hints; |
| 3674 | int gaierr; |
| 3675 | int sock = -1; |
| 3676 | char strport[NI_MAXSERV]; |
| 3677 | struct channel_connect cctx; |
| 3678 | Channel *c; |
| 3679 | |
Damien Miller | 2bcb866 | 2008-07-12 17:12:29 +1000 | [diff] [blame] | 3680 | memset(&cctx, 0, sizeof(cctx)); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3681 | |
| 3682 | if (port == PORT_STREAMLOCAL) { |
| 3683 | struct sockaddr_un *sunaddr; |
| 3684 | struct addrinfo *ai; |
| 3685 | |
| 3686 | if (strlen(name) > sizeof(sunaddr->sun_path)) { |
| 3687 | error("%.100s: %.100s", name, strerror(ENAMETOOLONG)); |
| 3688 | return (NULL); |
| 3689 | } |
| 3690 | |
| 3691 | /* |
| 3692 | * Fake up a struct addrinfo for AF_UNIX connections. |
| 3693 | * channel_connect_ctx_free() must check ai_family |
| 3694 | * and use free() not freeaddirinfo() for AF_UNIX. |
| 3695 | */ |
| 3696 | ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr)); |
| 3697 | memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr)); |
| 3698 | ai->ai_addr = (struct sockaddr *)(ai + 1); |
| 3699 | ai->ai_addrlen = sizeof(*sunaddr); |
| 3700 | ai->ai_family = AF_UNIX; |
| 3701 | ai->ai_socktype = SOCK_STREAM; |
| 3702 | ai->ai_protocol = PF_UNSPEC; |
| 3703 | sunaddr = (struct sockaddr_un *)ai->ai_addr; |
| 3704 | sunaddr->sun_family = AF_UNIX; |
| 3705 | strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path)); |
| 3706 | cctx.aitop = ai; |
| 3707 | } else { |
| 3708 | memset(&hints, 0, sizeof(hints)); |
| 3709 | hints.ai_family = IPv4or6; |
| 3710 | hints.ai_socktype = SOCK_STREAM; |
| 3711 | snprintf(strport, sizeof strport, "%d", port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3712 | if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop)) |
| 3713 | != 0) { |
| 3714 | if (errmsg != NULL) |
| 3715 | *errmsg = ssh_gai_strerror(gaierr); |
| 3716 | if (reason != NULL) |
| 3717 | *reason = SSH2_OPEN_CONNECT_FAILED; |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3718 | error("connect_to %.100s: unknown host (%s)", name, |
| 3719 | ssh_gai_strerror(gaierr)); |
| 3720 | return NULL; |
| 3721 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3722 | } |
| 3723 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3724 | cctx.host = xstrdup(name); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3725 | cctx.port = port; |
| 3726 | cctx.ai = cctx.aitop; |
| 3727 | |
| 3728 | if ((sock = connect_next(&cctx)) == -1) { |
| 3729 | error("connect to %.100s port %d failed: %s", |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3730 | name, port, strerror(errno)); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3731 | channel_connect_ctx_free(&cctx); |
| 3732 | return NULL; |
| 3733 | } |
| 3734 | c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1, |
| 3735 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1); |
| 3736 | c->connect_ctx = cctx; |
| 3737 | return c; |
| 3738 | } |
| 3739 | |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3740 | /* Return CONNECTING channel to remote host:port or local socket path */ |
| 3741 | static Channel * |
| 3742 | connect_to(const char *name, int port, char *ctype, char *rname) |
| 3743 | { |
| 3744 | return connect_to_reason(name, port, ctype, rname, NULL, NULL); |
| 3745 | } |
| 3746 | |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3747 | /* |
| 3748 | * returns either the newly connected channel or the downstream channel |
| 3749 | * that needs to deal with this connection. |
| 3750 | */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3751 | Channel * |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3752 | channel_connect_by_listen_address(const char *listen_host, |
| 3753 | u_short listen_port, char *ctype, char *rname) |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3754 | { |
| 3755 | int i; |
| 3756 | |
| 3757 | for (i = 0; i < num_permitted_opens; i++) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3758 | if (open_listen_match_tcpip(&permitted_opens[i], listen_host, |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3759 | listen_port, 1)) { |
markus@openbsd.org | 8d05784 | 2016-09-30 09:19:13 +0000 | [diff] [blame] | 3760 | if (permitted_opens[i].downstream) |
| 3761 | return permitted_opens[i].downstream; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3762 | return connect_to( |
| 3763 | permitted_opens[i].host_to_connect, |
| 3764 | permitted_opens[i].port_to_connect, ctype, rname); |
| 3765 | } |
| 3766 | } |
| 3767 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 3768 | listen_port); |
| 3769 | return NULL; |
| 3770 | } |
| 3771 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3772 | Channel * |
| 3773 | channel_connect_by_listen_path(const char *path, char *ctype, char *rname) |
| 3774 | { |
| 3775 | int i; |
| 3776 | |
| 3777 | for (i = 0; i < num_permitted_opens; i++) { |
| 3778 | if (open_listen_match_streamlocal(&permitted_opens[i], path)) { |
| 3779 | return connect_to( |
| 3780 | permitted_opens[i].host_to_connect, |
| 3781 | permitted_opens[i].port_to_connect, ctype, rname); |
| 3782 | } |
| 3783 | } |
| 3784 | error("WARNING: Server requests forwarding for unknown path %.100s", |
| 3785 | path); |
| 3786 | return NULL; |
| 3787 | } |
| 3788 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3789 | /* Check if connecting to that port is permitted and connect. */ |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3790 | Channel * |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3791 | channel_connect_to_port(const char *host, u_short port, char *ctype, |
| 3792 | char *rname, int *reason, const char **errmsg) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3793 | { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3794 | int i, permit, permit_adm = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3795 | |
| 3796 | permit = all_opens_permitted; |
| 3797 | if (!permit) { |
| 3798 | for (i = 0; i < num_permitted_opens; i++) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3799 | if (open_match(&permitted_opens[i], host, port)) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3800 | permit = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3801 | break; |
| 3802 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3803 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3804 | |
| 3805 | if (num_adm_permitted_opens > 0) { |
| 3806 | permit_adm = 0; |
| 3807 | for (i = 0; i < num_adm_permitted_opens; i++) |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3808 | if (open_match(&permitted_adm_opens[i], host, port)) { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3809 | permit_adm = 1; |
Damien Miller | 4b3ed64 | 2014-07-02 15:29:40 +1000 | [diff] [blame] | 3810 | break; |
| 3811 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 3815 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3816 | "but the request was denied.", host, port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3817 | if (reason != NULL) |
| 3818 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 3819 | return NULL; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3820 | } |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 3821 | return connect_to_reason(host, port, ctype, rname, reason, errmsg); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 3824 | /* Check if connecting to that path is permitted and connect. */ |
| 3825 | Channel * |
| 3826 | channel_connect_to_path(const char *path, char *ctype, char *rname) |
| 3827 | { |
| 3828 | int i, permit, permit_adm = 1; |
| 3829 | |
| 3830 | permit = all_opens_permitted; |
| 3831 | if (!permit) { |
| 3832 | for (i = 0; i < num_permitted_opens; i++) |
| 3833 | if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) { |
| 3834 | permit = 1; |
| 3835 | break; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | if (num_adm_permitted_opens > 0) { |
| 3840 | permit_adm = 0; |
| 3841 | for (i = 0; i < num_adm_permitted_opens; i++) |
| 3842 | if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) { |
| 3843 | permit_adm = 1; |
| 3844 | break; |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | if (!permit || !permit_adm) { |
| 3849 | logit("Received request to connect to path %.100s, " |
| 3850 | "but the request was denied.", path); |
| 3851 | return NULL; |
| 3852 | } |
| 3853 | return connect_to(path, PORT_STREAMLOCAL, ctype, rname); |
| 3854 | } |
| 3855 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 3856 | void |
| 3857 | channel_send_window_changes(void) |
| 3858 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 3859 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 3860 | struct winsize ws; |
| 3861 | |
| 3862 | for (i = 0; i < channels_alloc; i++) { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 3863 | if (channels[i] == NULL || !channels[i]->client_tty || |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 3864 | channels[i]->type != SSH_CHANNEL_OPEN) |
| 3865 | continue; |
| 3866 | if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
| 3867 | continue; |
| 3868 | channel_request_start(i, "window-change", 0); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 3869 | packet_put_int((u_int)ws.ws_col); |
| 3870 | packet_put_int((u_int)ws.ws_row); |
| 3871 | packet_put_int((u_int)ws.ws_xpixel); |
| 3872 | packet_put_int((u_int)ws.ws_ypixel); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 3873 | packet_send(); |
| 3874 | } |
| 3875 | } |
| 3876 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3877 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3878 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3879 | /* |
| 3880 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 3881 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 3882 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3883 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3884 | int |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 3885 | x11_create_display_inet(int x11_display_offset, int x11_use_localhost, |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 3886 | int single_connection, u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3887 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 3888 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 3889 | int display_number, sock; |
| 3890 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3891 | struct addrinfo hints, *ai, *aitop; |
| 3892 | char strport[NI_MAXSERV]; |
| 3893 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3894 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 3895 | if (chanids == NULL) |
| 3896 | return -1; |
| 3897 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 3898 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 3899 | display_number < MAX_DISPLAYS; |
| 3900 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3901 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3902 | memset(&hints, 0, sizeof(hints)); |
| 3903 | hints.ai_family = IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 3904 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3905 | hints.ai_socktype = SOCK_STREAM; |
| 3906 | snprintf(strport, sizeof strport, "%d", port); |
| 3907 | if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 3908 | error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3909 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3910 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3911 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 3912 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 3913 | continue; |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 3914 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 3915 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3916 | if (sock < 0) { |
Damien Miller | 6480c63 | 2010-03-26 11:09:44 +1100 | [diff] [blame] | 3917 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT) |
| 3918 | #ifdef EPFNOSUPPORT |
| 3919 | && (errno != EPFNOSUPPORT) |
| 3920 | #endif |
| 3921 | ) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 3922 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 3923 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3924 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 3925 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 3926 | debug("x11_create_display_inet: Socket family %d not supported", |
| 3927 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 3928 | continue; |
| 3929 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3930 | } |
Damien Miller | 04ee0f8 | 2009-11-18 17:48:30 +1100 | [diff] [blame] | 3931 | if (ai->ai_family == AF_INET6) |
| 3932 | sock_set_v6only(sock); |
Damien Miller | 4401e45 | 2008-06-12 06:05:12 +1000 | [diff] [blame] | 3933 | if (x11_use_localhost) |
| 3934 | channel_set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3935 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 3936 | debug2("bind port %d: %.100s", port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3937 | close(sock); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 3938 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3939 | for (n = 0; n < num_socks; n++) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3940 | close(socks[n]); |
| 3941 | } |
| 3942 | num_socks = 0; |
| 3943 | break; |
| 3944 | } |
| 3945 | socks[num_socks++] = sock; |
| 3946 | if (num_socks == NUM_SOCKS) |
| 3947 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3948 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 3949 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3950 | if (num_socks > 0) |
| 3951 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3952 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3953 | if (display_number >= MAX_DISPLAYS) { |
| 3954 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3955 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3956 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3957 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3958 | for (n = 0; n < num_socks; n++) { |
| 3959 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 3960 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3961 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3962 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3963 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3964 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3965 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3966 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3967 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 3968 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3969 | for (n = 0; n < num_socks; n++) { |
| 3970 | sock = socks[n]; |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 3971 | nc = channel_new("x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3972 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 3973 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3974 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 3975 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 3976 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3977 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 3978 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3979 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 3980 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 3981 | *display_numberp = display_number; |
| 3982 | return (0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3983 | } |
| 3984 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 3985 | static int |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 3986 | connect_local_xsocket_path(const char *pathname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3987 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3988 | int sock; |
| 3989 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3990 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 3991 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 3992 | if (sock < 0) |
| 3993 | error("socket: %.100s", strerror(errno)); |
| 3994 | memset(&addr, 0, sizeof(addr)); |
| 3995 | addr.sun_family = AF_UNIX; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 3996 | strlcpy(addr.sun_path, pathname, sizeof addr.sun_path); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 3997 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 3998 | return sock; |
| 3999 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4000 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 4001 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4002 | } |
| 4003 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4004 | static int |
| 4005 | connect_local_xsocket(u_int dnr) |
| 4006 | { |
| 4007 | char buf[1024]; |
| 4008 | snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr); |
| 4009 | return connect_local_xsocket_path(buf); |
| 4010 | } |
| 4011 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4012 | #ifdef __APPLE__ |
| 4013 | static int |
| 4014 | is_path_to_xsocket(const char *display, char *path, size_t pathlen) |
| 4015 | { |
| 4016 | struct stat sbuf; |
| 4017 | |
| 4018 | if (strlcpy(path, display, pathlen) >= pathlen) { |
| 4019 | error("%s: display path too long", __func__); |
| 4020 | return 0; |
| 4021 | } |
| 4022 | if (display[0] != '/') |
| 4023 | return 0; |
| 4024 | if (stat(path, &sbuf) == 0) { |
| 4025 | return 1; |
| 4026 | } else { |
| 4027 | char *dot = strrchr(path, '.'); |
| 4028 | if (dot != NULL) { |
| 4029 | *dot = '\0'; |
| 4030 | if (stat(path, &sbuf) == 0) { |
| 4031 | return 1; |
| 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | return 0; |
| 4036 | } |
| 4037 | #endif |
| 4038 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4039 | int |
| 4040 | x11_connect_display(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4041 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4042 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4043 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4044 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4045 | struct addrinfo hints, *ai, *aitop; |
| 4046 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4047 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4048 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4049 | /* Try to open a socket for the local X server. */ |
| 4050 | display = getenv("DISPLAY"); |
| 4051 | if (!display) { |
| 4052 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4053 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4054 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4055 | /* |
| 4056 | * Now we decode the value of the DISPLAY variable and make a |
| 4057 | * connection to the real X server. |
| 4058 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4059 | |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4060 | #ifdef __APPLE__ |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4061 | /* Check if display is a path to a socket (as set by launchd). */ |
| 4062 | { |
| 4063 | char path[PATH_MAX]; |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4064 | |
Darren Tucker | da39b09 | 2017-03-10 13:22:32 +1100 | [diff] [blame] | 4065 | if (is_path_to_xsocket(display, path, sizeof(path))) { |
| 4066 | debug("x11_connect_display: $DISPLAY is launchd"); |
| 4067 | |
| 4068 | /* Create a socket. */ |
| 4069 | sock = connect_local_xsocket_path(path); |
| 4070 | if (sock < 0) |
| 4071 | return -1; |
| 4072 | |
| 4073 | /* OK, we now have a connection to the display. */ |
| 4074 | return sock; |
| 4075 | } |
Damien Miller | 819dbb6 | 2009-01-21 16:46:26 +1100 | [diff] [blame] | 4076 | } |
| 4077 | #endif |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4078 | /* |
| 4079 | * Check if it is a unix domain socket. Unix domain displays are in |
| 4080 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 4081 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4082 | if (strncmp(display, "unix:", 5) == 0 || |
| 4083 | display[0] == ':') { |
| 4084 | /* Connect to the unix domain socket. */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4085 | if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4086 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4087 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4088 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4089 | } |
| 4090 | /* Create a socket. */ |
| 4091 | sock = connect_local_xsocket(display_number); |
| 4092 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4093 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4094 | |
| 4095 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4096 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4097 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4098 | /* |
| 4099 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 4100 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 4101 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 4102 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4103 | cp = strchr(buf, ':'); |
| 4104 | if (!cp) { |
| 4105 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4106 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4107 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4108 | *cp = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4109 | /* 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] | 4110 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4111 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 4112 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4113 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4114 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4115 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4116 | /* Look up the host address */ |
| 4117 | memset(&hints, 0, sizeof(hints)); |
| 4118 | hints.ai_family = IPv4or6; |
| 4119 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4120 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4121 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
Darren Tucker | 4abde77 | 2007-12-29 02:43:51 +1100 | [diff] [blame] | 4122 | error("%.100s: unknown host. (%s)", buf, |
| 4123 | ssh_gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4124 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4125 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4126 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 4127 | /* Create a socket. */ |
Darren Tucker | 7bd98e7 | 2010-01-10 10:31:12 +1100 | [diff] [blame] | 4128 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4129 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 4130 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4131 | continue; |
| 4132 | } |
| 4133 | /* Connect it to the display. */ |
| 4134 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4135 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 4136 | 6000 + display_number, strerror(errno)); |
| 4137 | close(sock); |
| 4138 | continue; |
| 4139 | } |
| 4140 | /* Success */ |
| 4141 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4142 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4143 | freeaddrinfo(aitop); |
| 4144 | if (!ai) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 4145 | error("connect %.100s port %u: %.100s", buf, 6000 + display_number, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 4146 | strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4147 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4148 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 4149 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4150 | return sock; |
| 4151 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4152 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4153 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4154 | * Requests forwarding of X11 connections, generates fake authentication |
| 4155 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 4156 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 4157 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4158 | void |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 4159 | x11_request_forwarding_with_spoofing(int client_session_id, const char *disp, |
Damien Miller | 6d7b437 | 2011-06-23 08:31:57 +1000 | [diff] [blame] | 4160 | const char *proto, const char *data, int want_reply) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4161 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 4162 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4163 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4164 | char *new_data; |
| 4165 | int screen_number; |
| 4166 | const char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4167 | |
Damien Miller | f92c079 | 2005-07-06 09:45:26 +1000 | [diff] [blame] | 4168 | if (x11_saved_display == NULL) |
| 4169 | x11_saved_display = xstrdup(disp); |
| 4170 | else if (strcmp(disp, x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4171 | error("x11_request_forwarding_with_spoofing: different " |
| 4172 | "$DISPLAY already forwarded"); |
| 4173 | return; |
| 4174 | } |
| 4175 | |
Damien Miller | d5fe0ba | 2006-08-30 11:07:39 +1000 | [diff] [blame] | 4176 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4177 | if (cp) |
| 4178 | cp = strchr(cp, '.'); |
| 4179 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 4180 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4181 | else |
| 4182 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4183 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4184 | if (x11_saved_proto == NULL) { |
| 4185 | /* Save protocol name. */ |
| 4186 | x11_saved_proto = xstrdup(proto); |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4187 | |
| 4188 | /* Extract real authentication data. */ |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4189 | x11_saved_data = xmalloc(data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4190 | for (i = 0; i < data_len; i++) { |
| 4191 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 4192 | fatal("x11_request_forwarding: bad " |
| 4193 | "authentication data: %.100s", data); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4194 | x11_saved_data[i] = value; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4195 | } |
| 4196 | x11_saved_data_len = data_len; |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 4197 | |
| 4198 | /* Generate fake data of the same length. */ |
| 4199 | x11_fake_data = xmalloc(data_len); |
| 4200 | arc4random_buf(x11_fake_data, data_len); |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4201 | x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4202 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4203 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4204 | /* Convert the fake data into hex. */ |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 4205 | new_data = tohex(x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4206 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4207 | /* Send the request packet. */ |
djm@openbsd.org | 97f4d30 | 2017-04-30 23:13:25 +0000 | [diff] [blame] | 4208 | channel_request_start(client_session_id, "x11-req", want_reply); |
| 4209 | packet_put_char(0); /* XXX bool single connection */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 4210 | packet_put_cstring(proto); |
| 4211 | packet_put_cstring(new_data); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4212 | packet_put_int(screen_number); |
| 4213 | packet_send(); |
| 4214 | packet_write_wait(); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 4215 | free(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 4216 | } |