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