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