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