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