Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * This file contains functions for generic socket connection forwarding. |
| 6 | * There is also code for initiating connection forwarding for X11 connections, |
| 7 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
| 14 | * |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. |
| 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" |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 43 | RCSID("$OpenBSD: channels.c,v 1.80 2001/01/08 22:03:23 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
| 45 | #include "ssh.h" |
| 46 | #include "packet.h" |
| 47 | #include "xmalloc.h" |
| 48 | #include "buffer.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 49 | #include "uidswap.h" |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 50 | #include "readconf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | #include "servconf.h" |
| 52 | |
| 53 | #include "channels.h" |
| 54 | #include "nchan.h" |
| 55 | #include "compat.h" |
| 56 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 57 | #include "ssh2.h" |
| 58 | |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 59 | #include <openssl/rsa.h> |
| 60 | #include <openssl/dsa.h> |
| 61 | #include "key.h" |
| 62 | #include "authfd.h" |
| 63 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | /* Maximum number of fake X11 displays to try. */ |
| 65 | #define MAX_DISPLAYS 1000 |
| 66 | |
| 67 | /* Max len of agent socket */ |
| 68 | #define MAX_SOCKET_NAME 100 |
| 69 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 70 | /* |
| 71 | * Pointer to an array containing all allocated channels. The array is |
| 72 | * dynamically extended as needed. |
| 73 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | static Channel *channels = NULL; |
| 75 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 76 | /* |
| 77 | * Size of the channel array. All slots of the array must always be |
| 78 | * initialized (at least the type field); unused slots are marked with type |
| 79 | * SSH_CHANNEL_FREE. |
| 80 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 81 | static int channels_alloc = 0; |
| 82 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 83 | /* |
| 84 | * Maximum file descriptor value used in any of the channels. This is |
| 85 | * updated in channel_allocate. |
| 86 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | static int channel_max_fd_value = 0; |
| 88 | |
| 89 | /* Name and directory of socket for authentication agent forwarding. */ |
| 90 | static char *channel_forwarded_auth_socket_name = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | static char *channel_forwarded_auth_socket_dir = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
| 93 | /* Saved X11 authentication protocol name. */ |
| 94 | char *x11_saved_proto = NULL; |
| 95 | |
| 96 | /* Saved X11 authentication data. This is the real data. */ |
| 97 | char *x11_saved_data = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 98 | u_int x11_saved_data_len = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 100 | /* |
| 101 | * Fake X11 authentication data. This is what the server will be sending us; |
| 102 | * we should replace any occurrences of this by the real data. |
| 103 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | char *x11_fake_data = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 105 | u_int x11_fake_data_len; |
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 | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | typedef struct { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 114 | char *host_to_connect; /* Connect to 'host'. */ |
| 115 | u_short port_to_connect; /* Connect to 'port'. */ |
| 116 | u_short listen_port; /* Remote side should listen port number. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | } ForwardPermission; |
| 118 | |
| 119 | /* List of all permitted host/port pairs to connect. */ |
| 120 | static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION]; |
| 121 | /* Number of permitted host/port pairs in the array. */ |
| 122 | static int num_permitted_opens = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 123 | /* |
| 124 | * If this is true, all opens are permitted. This is the case on the server |
| 125 | * on which we have to trust the client anyway, and the user could do |
| 126 | * anything after logging in anyway. |
| 127 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | static int all_opens_permitted = 0; |
| 129 | |
| 130 | /* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */ |
| 131 | static int have_hostname_in_open = 0; |
| 132 | |
| 133 | /* Sets specific protocol options. */ |
| 134 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 135 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | channel_set_options(int hostname_in_open) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | have_hostname_in_open = hostname_in_open; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 141 | /* |
| 142 | * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually |
| 143 | * called by the server, because the user could connect to any port anyway, |
| 144 | * and the server has no way to know but to trust the client anyway. |
| 145 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 147 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | channel_permit_all_opens() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | all_opens_permitted = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | } |
| 152 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 153 | /* lookup channel by id */ |
| 154 | |
| 155 | Channel * |
| 156 | channel_lookup(int id) |
| 157 | { |
| 158 | Channel *c; |
Damien Miller | c0fd17f | 2000-06-26 10:22:53 +1000 | [diff] [blame] | 159 | if (id < 0 || id > channels_alloc) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 160 | log("channel_lookup: %d: bad id", id); |
| 161 | return NULL; |
| 162 | } |
| 163 | c = &channels[id]; |
| 164 | if (c->type == SSH_CHANNEL_FREE) { |
| 165 | log("channel_lookup: %d: bad id: channel free", id); |
| 166 | return NULL; |
| 167 | } |
| 168 | return c; |
| 169 | } |
| 170 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 171 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 172 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 173 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 174 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 176 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 177 | channel_register_fds(Channel *c, int rfd, int wfd, int efd, |
| 178 | int extusage, int nonblock) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | /* Update the maximum file descriptor value. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 181 | if (rfd > channel_max_fd_value) |
| 182 | channel_max_fd_value = rfd; |
| 183 | if (wfd > channel_max_fd_value) |
| 184 | channel_max_fd_value = wfd; |
| 185 | if (efd > channel_max_fd_value) |
| 186 | channel_max_fd_value = efd; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 187 | /* XXX set close-on-exec -markus */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 188 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 189 | c->rfd = rfd; |
| 190 | c->wfd = wfd; |
| 191 | c->sock = (rfd == wfd) ? rfd : -1; |
| 192 | c->efd = efd; |
| 193 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 194 | |
| 195 | /* enable nonblocking mode */ |
| 196 | if (nonblock) { |
| 197 | if (rfd != -1) |
| 198 | set_nonblock(rfd); |
| 199 | if (wfd != -1) |
| 200 | set_nonblock(wfd); |
| 201 | if (efd != -1) |
| 202 | set_nonblock(efd); |
| 203 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Allocate a new channel object and set its type and socket. This will cause |
| 208 | * remote_name to be freed. |
| 209 | */ |
| 210 | |
| 211 | int |
| 212 | channel_new(char *ctype, int type, int rfd, int wfd, int efd, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 213 | int window, int maxpack, int extusage, char *remote_name, int nonblock) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 214 | { |
| 215 | int i, found; |
| 216 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 217 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | /* Do initial allocation if this is the first call. */ |
| 219 | if (channels_alloc == 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 220 | chan_init(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 221 | channels_alloc = 10; |
| 222 | channels = xmalloc(channels_alloc * sizeof(Channel)); |
| 223 | for (i = 0; i < channels_alloc; i++) |
| 224 | channels[i].type = SSH_CHANNEL_FREE; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 225 | /* |
| 226 | * Kludge: arrange a call to channel_stop_listening if we |
| 227 | * terminate with fatal(). |
| 228 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL); |
| 230 | } |
| 231 | /* Try to find a free slot where to put the new channel. */ |
| 232 | for (found = -1, i = 0; i < channels_alloc; i++) |
| 233 | if (channels[i].type == SSH_CHANNEL_FREE) { |
| 234 | /* Found a free slot. */ |
| 235 | found = i; |
| 236 | break; |
| 237 | } |
| 238 | if (found == -1) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 239 | /* 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] | 240 | found = channels_alloc; |
| 241 | channels_alloc += 10; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 242 | debug2("channel: expanding %d", channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 243 | channels = xrealloc(channels, channels_alloc * sizeof(Channel)); |
| 244 | for (i = found; i < channels_alloc; i++) |
| 245 | channels[i].type = SSH_CHANNEL_FREE; |
| 246 | } |
| 247 | /* Initialize and return new channel number. */ |
| 248 | c = &channels[found]; |
| 249 | buffer_init(&c->input); |
| 250 | buffer_init(&c->output); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 251 | buffer_init(&c->extended); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 252 | chan_init_iostates(c); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 253 | channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | c->self = found; |
| 255 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 256 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 257 | c->local_window = window; |
| 258 | c->local_window_max = window; |
| 259 | c->local_consumed = 0; |
| 260 | c->local_maxpacket = maxpack; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 261 | c->remote_id = -1; |
| 262 | c->remote_name = remote_name; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 263 | c->remote_window = 0; |
| 264 | c->remote_maxpacket = 0; |
| 265 | c->cb_fn = NULL; |
| 266 | c->cb_arg = NULL; |
| 267 | c->cb_event = 0; |
| 268 | c->dettach_user = NULL; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 269 | c->input_filter = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 270 | debug("channel %d: new [%s]", found, remote_name); |
| 271 | return found; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 272 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 273 | /* old interface XXX */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 274 | int |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 275 | channel_allocate(int type, int sock, char *remote_name) |
| 276 | { |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 277 | return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 278 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 279 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 280 | |
| 281 | /* Close all channel fd/socket. */ |
| 282 | |
| 283 | void |
| 284 | channel_close_fds(Channel *c) |
| 285 | { |
| 286 | if (c->sock != -1) { |
| 287 | close(c->sock); |
| 288 | c->sock = -1; |
| 289 | } |
| 290 | if (c->rfd != -1) { |
| 291 | close(c->rfd); |
| 292 | c->rfd = -1; |
| 293 | } |
| 294 | if (c->wfd != -1) { |
| 295 | close(c->wfd); |
| 296 | c->wfd = -1; |
| 297 | } |
| 298 | if (c->efd != -1) { |
| 299 | close(c->efd); |
| 300 | c->efd = -1; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* Free the channel and close its fd/socket. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 306 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 307 | channel_free(int id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 308 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 309 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 310 | char *s = channel_open_message(); |
| 311 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 312 | if (c == NULL) |
| 313 | packet_disconnect("channel free: bad local channel %d", id); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 314 | debug("channel_free: channel %d: status: %s", id, s); |
| 315 | xfree(s); |
| 316 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 317 | if (c->dettach_user != NULL) { |
| 318 | debug("channel_free: channel %d: dettaching channel user", id); |
| 319 | c->dettach_user(c->self, NULL); |
| 320 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 321 | if (c->sock != -1) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 322 | shutdown(c->sock, SHUT_RDWR); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 323 | channel_close_fds(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 324 | buffer_free(&c->input); |
| 325 | buffer_free(&c->output); |
| 326 | buffer_free(&c->extended); |
| 327 | c->type = SSH_CHANNEL_FREE; |
| 328 | if (c->remote_name) { |
| 329 | xfree(c->remote_name); |
| 330 | c->remote_name = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 331 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | } |
| 333 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 334 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 335 | * 'channel_pre*' are called just before select() to add any bits relevant to |
| 336 | * channels in the select bitmasks. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 337 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 338 | /* |
| 339 | * 'channel_post*': perform any appropriate operations for channels which |
| 340 | * have events pending. |
| 341 | */ |
| 342 | typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset); |
| 343 | chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE]; |
| 344 | chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE]; |
| 345 | |
| 346 | void |
| 347 | channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset) |
| 348 | { |
| 349 | FD_SET(c->sock, readset); |
| 350 | } |
| 351 | |
| 352 | void |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 353 | channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset) |
| 354 | { |
| 355 | debug3("channel %d: waiting for connection", c->self); |
| 356 | FD_SET(c->sock, writeset); |
| 357 | } |
| 358 | |
| 359 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 360 | channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset) |
| 361 | { |
| 362 | if (buffer_len(&c->input) < packet_get_maxsize()) |
| 363 | FD_SET(c->sock, readset); |
| 364 | if (buffer_len(&c->output) > 0) |
| 365 | FD_SET(c->sock, writeset); |
| 366 | } |
| 367 | |
| 368 | void |
| 369 | channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset) |
| 370 | { |
| 371 | /* test whether sockets are 'alive' for read/write */ |
| 372 | if (c->istate == CHAN_INPUT_OPEN) |
| 373 | if (buffer_len(&c->input) < packet_get_maxsize()) |
| 374 | FD_SET(c->sock, readset); |
| 375 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 376 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 377 | if (buffer_len(&c->output) > 0) { |
| 378 | FD_SET(c->sock, writeset); |
| 379 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 380 | chan_obuf_empty(c); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 386 | channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset) |
| 387 | { |
| 388 | if (c->istate == CHAN_INPUT_OPEN && |
| 389 | c->remote_window > 0 && |
| 390 | buffer_len(&c->input) < c->remote_window) |
| 391 | FD_SET(c->rfd, readset); |
| 392 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 393 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 394 | if (buffer_len(&c->output) > 0) { |
| 395 | FD_SET(c->wfd, writeset); |
| 396 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 397 | chan_obuf_empty(c); |
| 398 | } |
| 399 | } |
| 400 | /** XXX check close conditions, too */ |
| 401 | if (c->efd != -1) { |
| 402 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
| 403 | buffer_len(&c->extended) > 0) |
| 404 | FD_SET(c->efd, writeset); |
| 405 | else if (c->extended_usage == CHAN_EXTENDED_READ && |
| 406 | buffer_len(&c->extended) < c->remote_window) |
| 407 | FD_SET(c->efd, readset); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 412 | channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset) |
| 413 | { |
| 414 | if (buffer_len(&c->input) == 0) { |
| 415 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 416 | packet_put_int(c->remote_id); |
| 417 | packet_send(); |
| 418 | c->type = SSH_CHANNEL_CLOSED; |
| 419 | debug("Closing channel %d after input drain.", c->self); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset) |
| 425 | { |
| 426 | if (buffer_len(&c->output) == 0) |
| 427 | channel_free(c->self); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 428 | else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 429 | FD_SET(c->sock, writeset); |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 434 | * connection (when authentication spoofing is being done) remains in this |
| 435 | * state until the first packet has been completely read. The authentication |
| 436 | * data in that packet is then substituted by the real data if it matches the |
| 437 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 438 | * XXX All this happens at the client side. |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 439 | */ |
| 440 | int |
| 441 | x11_open_helper(Channel *c) |
| 442 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 443 | u_char *ucp; |
| 444 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 445 | |
| 446 | /* Check if the fixed size part of the packet is in buffer. */ |
| 447 | if (buffer_len(&c->output) < 12) |
| 448 | return 0; |
| 449 | |
| 450 | /* Parse the lengths of variable-length fields. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 451 | ucp = (u_char *) buffer_ptr(&c->output); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 452 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 453 | proto_len = 256 * ucp[6] + ucp[7]; |
| 454 | data_len = 256 * ucp[8] + ucp[9]; |
| 455 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 456 | proto_len = ucp[6] + 256 * ucp[7]; |
| 457 | data_len = ucp[8] + 256 * ucp[9]; |
| 458 | } else { |
| 459 | debug("Initial X11 packet contains bad byte order byte: 0x%x", |
| 460 | ucp[0]); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | /* Check if the whole packet is in buffer. */ |
| 465 | if (buffer_len(&c->output) < |
| 466 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 467 | return 0; |
| 468 | |
| 469 | /* Check if authentication protocol matches. */ |
| 470 | if (proto_len != strlen(x11_saved_proto) || |
| 471 | memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) { |
| 472 | debug("X11 connection uses different authentication protocol."); |
| 473 | return -1; |
| 474 | } |
| 475 | /* Check if authentication data matches our fake data. */ |
| 476 | if (data_len != x11_fake_data_len || |
| 477 | memcmp(ucp + 12 + ((proto_len + 3) & ~3), |
| 478 | x11_fake_data, x11_fake_data_len) != 0) { |
| 479 | debug("X11 auth data does not match fake data."); |
| 480 | return -1; |
| 481 | } |
| 482 | /* Check fake data length */ |
| 483 | if (x11_fake_data_len != x11_saved_data_len) { |
| 484 | error("X11 fake_data_len %d != saved_data_len %d", |
| 485 | x11_fake_data_len, x11_saved_data_len); |
| 486 | return -1; |
| 487 | } |
| 488 | /* |
| 489 | * Received authentication protocol and data match |
| 490 | * our fake data. Substitute the fake data with real |
| 491 | * data. |
| 492 | */ |
| 493 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
| 494 | x11_saved_data, x11_saved_data_len); |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | void |
| 499 | channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset) |
| 500 | { |
| 501 | int ret = x11_open_helper(c); |
| 502 | if (ret == 1) { |
| 503 | /* Start normal processing for the channel. */ |
| 504 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 505 | channel_pre_open_13(c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 506 | } else if (ret == -1) { |
| 507 | /* |
| 508 | * We have received an X11 connection that has bad |
| 509 | * authentication information. |
| 510 | */ |
| 511 | log("X11 connection rejected because of wrong authentication.\r\n"); |
| 512 | buffer_clear(&c->input); |
| 513 | buffer_clear(&c->output); |
| 514 | close(c->sock); |
| 515 | c->sock = -1; |
| 516 | c->type = SSH_CHANNEL_CLOSED; |
| 517 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 518 | packet_put_int(c->remote_id); |
| 519 | packet_send(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 524 | channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 525 | { |
| 526 | int ret = x11_open_helper(c); |
| 527 | if (ret == 1) { |
| 528 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 529 | if (compat20) |
| 530 | channel_pre_open_20(c, readset, writeset); |
| 531 | else |
| 532 | channel_pre_open_15(c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 533 | } else if (ret == -1) { |
| 534 | debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 535 | chan_read_failed(c); /** force close? */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 536 | chan_write_failed(c); |
| 537 | debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* This is our fake X11 server socket. */ |
| 542 | void |
| 543 | channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset) |
| 544 | { |
| 545 | struct sockaddr addr; |
| 546 | int newsock, newch; |
| 547 | socklen_t addrlen; |
| 548 | char buf[16384], *remote_hostname; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 549 | int remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 550 | |
| 551 | if (FD_ISSET(c->sock, readset)) { |
| 552 | debug("X11 connection requested."); |
| 553 | addrlen = sizeof(addr); |
| 554 | newsock = accept(c->sock, &addr, &addrlen); |
| 555 | if (newsock < 0) { |
| 556 | error("accept: %.100s", strerror(errno)); |
| 557 | return; |
| 558 | } |
| 559 | remote_hostname = get_remote_hostname(newsock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 560 | remote_port = get_peer_port(newsock); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 561 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 562 | remote_hostname, remote_port); |
| 563 | |
| 564 | newch = channel_new("x11", |
| 565 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 566 | c->local_window_max, c->local_maxpacket, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 567 | 0, xstrdup(buf), 1); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 568 | if (compat20) { |
| 569 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 570 | packet_put_cstring("x11"); |
| 571 | packet_put_int(newch); |
| 572 | packet_put_int(c->local_window_max); |
| 573 | packet_put_int(c->local_maxpacket); |
| 574 | /* originator host and port */ |
| 575 | packet_put_cstring(remote_hostname); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 576 | if (datafellows & SSH_BUG_X11FWD) { |
| 577 | debug("ssh2 x11 bug compat mode"); |
| 578 | } else { |
| 579 | packet_put_int(remote_port); |
| 580 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 581 | packet_send(); |
| 582 | } else { |
| 583 | packet_start(SSH_SMSG_X11_OPEN); |
| 584 | packet_put_int(newch); |
| 585 | if (have_hostname_in_open) |
| 586 | packet_put_string(buf, strlen(buf)); |
| 587 | packet_send(); |
| 588 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 589 | xfree(remote_hostname); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 595 | */ |
| 596 | void |
| 597 | channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset) |
| 598 | { |
| 599 | struct sockaddr addr; |
| 600 | int newsock, newch; |
| 601 | socklen_t addrlen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 602 | char buf[1024], *remote_hostname, *rtype; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 603 | int remote_port; |
| 604 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 605 | rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 606 | "forwarded-tcpip" : "direct-tcpip"; |
| 607 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 608 | if (FD_ISSET(c->sock, readset)) { |
| 609 | debug("Connection to port %d forwarding " |
| 610 | "to %.100s port %d requested.", |
| 611 | c->listening_port, c->path, c->host_port); |
| 612 | addrlen = sizeof(addr); |
| 613 | newsock = accept(c->sock, &addr, &addrlen); |
| 614 | if (newsock < 0) { |
| 615 | error("accept: %.100s", strerror(errno)); |
| 616 | return; |
| 617 | } |
| 618 | remote_hostname = get_remote_hostname(newsock); |
| 619 | remote_port = get_peer_port(newsock); |
| 620 | snprintf(buf, sizeof buf, |
| 621 | "listen port %d for %.100s port %d, " |
| 622 | "connect from %.200s port %d", |
| 623 | c->listening_port, c->path, c->host_port, |
| 624 | remote_hostname, remote_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 625 | |
| 626 | newch = channel_new(rtype, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 627 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 628 | c->local_window_max, c->local_maxpacket, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 629 | 0, xstrdup(buf), 1); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 630 | if (compat20) { |
| 631 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 632 | packet_put_cstring(rtype); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 633 | packet_put_int(newch); |
| 634 | packet_put_int(c->local_window_max); |
| 635 | packet_put_int(c->local_maxpacket); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 636 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 637 | /* listen address, port */ |
| 638 | packet_put_string(c->path, strlen(c->path)); |
| 639 | packet_put_int(c->listening_port); |
| 640 | } else { |
| 641 | /* target host, port */ |
| 642 | packet_put_string(c->path, strlen(c->path)); |
| 643 | packet_put_int(c->host_port); |
| 644 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 645 | /* originator host and port */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 646 | packet_put_cstring(remote_hostname); |
| 647 | packet_put_int(remote_port); |
| 648 | packet_send(); |
| 649 | } else { |
| 650 | packet_start(SSH_MSG_PORT_OPEN); |
| 651 | packet_put_int(newch); |
| 652 | packet_put_string(c->path, strlen(c->path)); |
| 653 | packet_put_int(c->host_port); |
| 654 | if (have_hostname_in_open) { |
| 655 | packet_put_string(buf, strlen(buf)); |
| 656 | } |
| 657 | packet_send(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 658 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 659 | xfree(remote_hostname); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * This is the authentication agent socket listening for connections from |
| 665 | * clients. |
| 666 | */ |
| 667 | void |
| 668 | channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset) |
| 669 | { |
| 670 | struct sockaddr addr; |
| 671 | int newsock, newch; |
| 672 | socklen_t addrlen; |
| 673 | |
| 674 | if (FD_ISSET(c->sock, readset)) { |
| 675 | addrlen = sizeof(addr); |
| 676 | newsock = accept(c->sock, &addr, &addrlen); |
| 677 | if (newsock < 0) { |
| 678 | error("accept from auth socket: %.100s", strerror(errno)); |
| 679 | return; |
| 680 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 681 | newch = channel_new("accepted auth socket", |
| 682 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 683 | c->local_window_max, c->local_maxpacket, |
| 684 | 0, xstrdup("accepted auth socket"), 1); |
| 685 | if (compat20) { |
| 686 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 687 | packet_put_cstring("auth-agent@openssh.com"); |
| 688 | packet_put_int(newch); |
| 689 | packet_put_int(c->local_window_max); |
| 690 | packet_put_int(c->local_maxpacket); |
| 691 | } else { |
| 692 | packet_start(SSH_SMSG_AGENT_OPEN); |
| 693 | packet_put_int(newch); |
| 694 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 695 | packet_send(); |
| 696 | } |
| 697 | } |
| 698 | |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 699 | void |
| 700 | channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset) |
| 701 | { |
| 702 | if (FD_ISSET(c->sock, writeset)) { |
| 703 | int err = 0; |
| 704 | int sz = sizeof(err); |
| 705 | c->type = SSH_CHANNEL_OPEN; |
| 706 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) { |
| 707 | debug("getsockopt SO_ERROR failed"); |
| 708 | } else { |
| 709 | if (err == 0) { |
| 710 | debug("channel %d: connected)", c->self); |
| 711 | } else { |
| 712 | debug("channel %d: not connected: %s", |
| 713 | c->self, strerror(err)); |
| 714 | chan_read_failed(c); |
| 715 | chan_write_failed(c); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 721 | int |
| 722 | channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset) |
| 723 | { |
| 724 | char buf[16*1024]; |
| 725 | int len; |
| 726 | |
| 727 | if (c->rfd != -1 && |
| 728 | FD_ISSET(c->rfd, readset)) { |
| 729 | len = read(c->rfd, buf, sizeof(buf)); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 730 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 731 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 732 | if (len <= 0) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 733 | debug("channel %d: read<=0 rfd %d len %d", |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 734 | c->self, c->rfd, len); |
| 735 | if (compat13) { |
| 736 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 737 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
| 738 | debug("Channel %d status set to input draining.", c->self); |
| 739 | } else { |
| 740 | chan_read_failed(c); |
| 741 | } |
| 742 | return -1; |
| 743 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 744 | if(c->input_filter != NULL) { |
| 745 | if (c->input_filter(c, buf, len) == -1) { |
| 746 | debug("filter stops channel %d", c->self); |
| 747 | chan_read_failed(c); |
| 748 | } |
| 749 | } else { |
| 750 | buffer_append(&c->input, buf, len); |
| 751 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 752 | } |
| 753 | return 1; |
| 754 | } |
| 755 | int |
| 756 | channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset) |
| 757 | { |
| 758 | int len; |
| 759 | |
| 760 | /* Send buffered output data to the socket. */ |
| 761 | if (c->wfd != -1 && |
| 762 | FD_ISSET(c->wfd, writeset) && |
| 763 | buffer_len(&c->output) > 0) { |
| 764 | len = write(c->wfd, buffer_ptr(&c->output), |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 765 | buffer_len(&c->output)); |
| 766 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 767 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 768 | if (len <= 0) { |
| 769 | if (compat13) { |
| 770 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 771 | debug("Channel %d status set to input draining.", c->self); |
| 772 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
| 773 | } else { |
| 774 | chan_write_failed(c); |
| 775 | } |
| 776 | return -1; |
| 777 | } |
| 778 | buffer_consume(&c->output, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 779 | if (compat20 && len > 0) { |
| 780 | c->local_consumed += len; |
| 781 | } |
| 782 | } |
| 783 | return 1; |
| 784 | } |
| 785 | int |
| 786 | channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset) |
| 787 | { |
| 788 | char buf[16*1024]; |
| 789 | int len; |
| 790 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 791 | /** XXX handle drain efd, too */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 792 | if (c->efd != -1) { |
| 793 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
| 794 | FD_ISSET(c->efd, writeset) && |
| 795 | buffer_len(&c->extended) > 0) { |
| 796 | len = write(c->efd, buffer_ptr(&c->extended), |
| 797 | buffer_len(&c->extended)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 798 | debug2("channel %d: written %d to efd %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 799 | c->self, len, c->efd); |
| 800 | if (len > 0) { |
| 801 | buffer_consume(&c->extended, len); |
| 802 | c->local_consumed += len; |
| 803 | } |
| 804 | } else if (c->extended_usage == CHAN_EXTENDED_READ && |
| 805 | FD_ISSET(c->efd, readset)) { |
| 806 | len = read(c->efd, buf, sizeof(buf)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 807 | debug2("channel %d: read %d from efd %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 808 | c->self, len, c->efd); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 809 | if (len == 0) { |
| 810 | debug("channel %d: closing efd %d", |
| 811 | c->self, c->efd); |
| 812 | close(c->efd); |
| 813 | c->efd = -1; |
| 814 | } else if (len > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 815 | buffer_append(&c->extended, buf, len); |
| 816 | } |
| 817 | } |
| 818 | return 1; |
| 819 | } |
| 820 | int |
| 821 | channel_check_window(Channel *c, fd_set * readset, fd_set * writeset) |
| 822 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 823 | if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 824 | c->local_window < c->local_window_max/2 && |
| 825 | c->local_consumed > 0) { |
| 826 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 827 | packet_put_int(c->remote_id); |
| 828 | packet_put_int(c->local_consumed); |
| 829 | packet_send(); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 830 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 831 | c->self, c->local_window, |
| 832 | c->local_consumed); |
| 833 | c->local_window += c->local_consumed; |
| 834 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 835 | } |
| 836 | return 1; |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset) |
| 841 | { |
| 842 | channel_handle_rfd(c, readset, writeset); |
| 843 | channel_handle_wfd(c, readset, writeset); |
| 844 | } |
| 845 | |
| 846 | void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 847 | channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset) |
| 848 | { |
| 849 | channel_handle_rfd(c, readset, writeset); |
| 850 | channel_handle_wfd(c, readset, writeset); |
| 851 | channel_handle_efd(c, readset, writeset); |
| 852 | channel_check_window(c, readset, writeset); |
| 853 | } |
| 854 | |
| 855 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 856 | channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset) |
| 857 | { |
| 858 | int len; |
| 859 | /* Send buffered output data to the socket. */ |
| 860 | if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) { |
| 861 | len = write(c->sock, buffer_ptr(&c->output), |
| 862 | buffer_len(&c->output)); |
| 863 | if (len <= 0) |
| 864 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 865 | else |
| 866 | buffer_consume(&c->output, len); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 871 | channel_handler_init_20(void) |
| 872 | { |
| 873 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 874 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 875 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 876 | channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 877 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 878 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 879 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 880 | |
| 881 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2; |
| 882 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 883 | channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 884 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 885 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 886 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 890 | channel_handler_init_13(void) |
| 891 | { |
| 892 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13; |
| 893 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13; |
| 894 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 895 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 896 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 897 | channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining; |
| 898 | channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 899 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 900 | |
| 901 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1; |
| 902 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 903 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 904 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 905 | channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 906 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | void |
| 910 | channel_handler_init_15(void) |
| 911 | { |
| 912 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 913 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 914 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 915 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 916 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 917 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 918 | |
| 919 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 920 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 921 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 922 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 923 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void |
| 927 | channel_handler_init(void) |
| 928 | { |
| 929 | int i; |
| 930 | for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) { |
| 931 | channel_pre[i] = NULL; |
| 932 | channel_post[i] = NULL; |
| 933 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 934 | if (compat20) |
| 935 | channel_handler_init_20(); |
| 936 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 937 | channel_handler_init_13(); |
| 938 | else |
| 939 | channel_handler_init_15(); |
| 940 | } |
| 941 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 942 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 943 | channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset) |
| 944 | { |
| 945 | static int did_init = 0; |
| 946 | int i; |
| 947 | Channel *c; |
| 948 | |
| 949 | if (!did_init) { |
| 950 | channel_handler_init(); |
| 951 | did_init = 1; |
| 952 | } |
| 953 | for (i = 0; i < channels_alloc; i++) { |
| 954 | c = &channels[i]; |
| 955 | if (c->type == SSH_CHANNEL_FREE) |
| 956 | continue; |
| 957 | if (ftab[c->type] == NULL) |
| 958 | continue; |
| 959 | (*ftab[c->type])(c, readset, writeset); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 960 | chan_delete_if_full_closed(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 961 | } |
| 962 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 963 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 964 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 965 | channel_prepare_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 966 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 967 | channel_handler(channel_pre, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 968 | } |
| 969 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 970 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 971 | channel_after_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 972 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 973 | channel_handler(channel_post, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* If there is data to send to the connection, send some of it now. */ |
| 977 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 978 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 979 | channel_output_poll() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 980 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | int len, i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 982 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 983 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 984 | for (i = 0; i < channels_alloc; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 985 | c = &channels[i]; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 986 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 987 | /* We are only interested in channels that can have buffered incoming data. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 988 | if (compat13) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 989 | if (c->type != SSH_CHANNEL_OPEN && |
| 990 | c->type != SSH_CHANNEL_INPUT_DRAINING) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 991 | continue; |
| 992 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 993 | if (c->type != SSH_CHANNEL_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 994 | continue; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 995 | if (c->istate != CHAN_INPUT_OPEN && |
| 996 | c->istate != CHAN_INPUT_WAIT_DRAIN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 997 | continue; |
| 998 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 999 | if (compat20 && |
| 1000 | (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1001 | debug("channel: %d: no data after CLOSE", c->self); |
| 1002 | continue; |
| 1003 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1004 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1005 | /* Get the amount of buffered data for this channel. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1006 | len = buffer_len(&c->input); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1007 | if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1008 | /* Send some data for the other side over the secure connection. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1009 | if (compat20) { |
| 1010 | if (len > c->remote_window) |
| 1011 | len = c->remote_window; |
| 1012 | if (len > c->remote_maxpacket) |
| 1013 | len = c->remote_maxpacket; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1014 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1015 | if (packet_is_interactive()) { |
| 1016 | if (len > 1024) |
| 1017 | len = 512; |
| 1018 | } else { |
| 1019 | /* Keep the packets at reasonable size. */ |
| 1020 | if (len > packet_get_maxsize()/2) |
| 1021 | len = packet_get_maxsize()/2; |
| 1022 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1023 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1024 | if (len > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1025 | packet_start(compat20 ? |
| 1026 | SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1027 | packet_put_int(c->remote_id); |
| 1028 | packet_put_string(buffer_ptr(&c->input), len); |
| 1029 | packet_send(); |
| 1030 | buffer_consume(&c->input, len); |
| 1031 | c->remote_window -= len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1032 | } |
| 1033 | } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1034 | if (compat13) |
| 1035 | fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1036 | /* |
| 1037 | * input-buffer is empty and read-socket shutdown: |
| 1038 | * tell peer, that we will not send more data: send IEOF |
| 1039 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1040 | chan_ibuf_empty(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1041 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1042 | /* Send extended data, i.e. stderr */ |
| 1043 | if (compat20 && |
| 1044 | c->remote_window > 0 && |
| 1045 | (len = buffer_len(&c->extended)) > 0 && |
| 1046 | c->extended_usage == CHAN_EXTENDED_READ) { |
| 1047 | if (len > c->remote_window) |
| 1048 | len = c->remote_window; |
| 1049 | if (len > c->remote_maxpacket) |
| 1050 | len = c->remote_maxpacket; |
| 1051 | packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
| 1052 | packet_put_int(c->remote_id); |
| 1053 | packet_put_int(SSH2_EXTENDED_DATA_STDERR); |
| 1054 | packet_put_string(buffer_ptr(&c->extended), len); |
| 1055 | packet_send(); |
| 1056 | buffer_consume(&c->extended, len); |
| 1057 | c->remote_window -= len; |
| 1058 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1059 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1060 | } |
| 1061 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1062 | /* |
| 1063 | * This is called when a packet of type CHANNEL_DATA has just been received. |
| 1064 | * The message type has already been consumed, but channel number and data is |
| 1065 | * still there. |
| 1066 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1067 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1068 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1069 | channel_input_data(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1070 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1071 | int id; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1072 | char *data; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1073 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1074 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1075 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1076 | /* Get the channel number and verify it. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1077 | id = packet_get_int(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1078 | c = channel_lookup(id); |
| 1079 | if (c == NULL) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1080 | packet_disconnect("Received data for nonexistent channel %d.", id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1081 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1082 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1083 | if (c->type != SSH_CHANNEL_OPEN && |
| 1084 | c->type != SSH_CHANNEL_X11_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1085 | return; |
| 1086 | |
| 1087 | /* same for protocol 1.5 if output end is no longer open */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1088 | if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1089 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1090 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1091 | /* Get the data. */ |
| 1092 | data = packet_get_string(&data_len); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1093 | packet_done(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1094 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1095 | if (compat20){ |
| 1096 | if (data_len > c->local_maxpacket) { |
| 1097 | log("channel %d: rcvd big packet %d, maxpack %d", |
| 1098 | c->self, data_len, c->local_maxpacket); |
| 1099 | } |
| 1100 | if (data_len > c->local_window) { |
| 1101 | log("channel %d: rcvd too much data %d, win %d", |
| 1102 | c->self, data_len, c->local_window); |
| 1103 | xfree(data); |
| 1104 | return; |
| 1105 | } |
| 1106 | c->local_window -= data_len; |
| 1107 | }else{ |
| 1108 | packet_integrity_check(plen, 4 + 4 + data_len, type); |
| 1109 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1110 | buffer_append(&c->output, data, data_len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1111 | xfree(data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1112 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1113 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1114 | channel_input_extended_data(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1115 | { |
| 1116 | int id; |
| 1117 | int tcode; |
| 1118 | char *data; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1119 | u_int data_len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1120 | Channel *c; |
| 1121 | |
| 1122 | /* Get the channel number and verify it. */ |
| 1123 | id = packet_get_int(); |
| 1124 | c = channel_lookup(id); |
| 1125 | |
| 1126 | if (c == NULL) |
| 1127 | packet_disconnect("Received extended_data for bad channel %d.", id); |
| 1128 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1129 | log("channel %d: ext data for non open", id); |
| 1130 | return; |
| 1131 | } |
| 1132 | tcode = packet_get_int(); |
| 1133 | if (c->efd == -1 || |
| 1134 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 1135 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
| 1136 | log("channel %d: bad ext data", c->self); |
| 1137 | return; |
| 1138 | } |
| 1139 | data = packet_get_string(&data_len); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1140 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1141 | if (data_len > c->local_window) { |
| 1142 | log("channel %d: rcvd too much extended_data %d, win %d", |
| 1143 | c->self, data_len, c->local_window); |
| 1144 | xfree(data); |
| 1145 | return; |
| 1146 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1147 | debug2("channel %d: rcvd ext data %d", c->self, data_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1148 | c->local_window -= data_len; |
| 1149 | buffer_append(&c->extended, data, data_len); |
| 1150 | xfree(data); |
| 1151 | } |
| 1152 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1153 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1154 | /* |
| 1155 | * Returns true if no channel has too much buffered data, and false if one or |
| 1156 | * more channel is overfull. |
| 1157 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1158 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1159 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1160 | channel_not_very_much_buffered_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1161 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1162 | u_int i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1163 | Channel *c; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1164 | |
| 1165 | for (i = 0; i < channels_alloc; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1166 | c = &channels[i]; |
| 1167 | if (c->type == SSH_CHANNEL_OPEN) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1168 | if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1169 | debug("channel %d: big input buffer %d", |
| 1170 | c->self, buffer_len(&c->input)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1171 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1172 | } |
| 1173 | if (buffer_len(&c->output) > packet_get_maxsize()) { |
| 1174 | debug("channel %d: big output buffer %d", |
| 1175 | c->self, buffer_len(&c->output)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1176 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1177 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1178 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1179 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1180 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1181 | } |
| 1182 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1183 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1184 | channel_input_ieof(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1185 | { |
| 1186 | int id; |
| 1187 | Channel *c; |
| 1188 | |
| 1189 | packet_integrity_check(plen, 4, type); |
| 1190 | |
| 1191 | id = packet_get_int(); |
| 1192 | c = channel_lookup(id); |
| 1193 | if (c == NULL) |
| 1194 | packet_disconnect("Received ieof for nonexistent channel %d.", id); |
| 1195 | chan_rcvd_ieof(c); |
| 1196 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1197 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1198 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1199 | channel_input_close(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1200 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1201 | int id; |
| 1202 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1203 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1204 | packet_integrity_check(plen, 4, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1205 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1206 | id = packet_get_int(); |
| 1207 | c = channel_lookup(id); |
| 1208 | if (c == NULL) |
| 1209 | packet_disconnect("Received close for nonexistent channel %d.", id); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1210 | |
| 1211 | /* |
| 1212 | * Send a confirmation that we have closed the channel and no more |
| 1213 | * data is coming for it. |
| 1214 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1215 | packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1216 | packet_put_int(c->remote_id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1217 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1218 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1219 | /* |
| 1220 | * If the channel is in closed state, we have sent a close request, |
| 1221 | * and the other side will eventually respond with a confirmation. |
| 1222 | * Thus, we cannot free the channel here, because then there would be |
| 1223 | * no-one to receive the confirmation. The channel gets freed when |
| 1224 | * the confirmation arrives. |
| 1225 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1226 | if (c->type != SSH_CHANNEL_CLOSED) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1227 | /* |
| 1228 | * Not a closed channel - mark it as draining, which will |
| 1229 | * cause it to be freed later. |
| 1230 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1231 | buffer_consume(&c->input, buffer_len(&c->input)); |
| 1232 | c->type = SSH_CHANNEL_OUTPUT_DRAINING; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1233 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1234 | } |
| 1235 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1236 | /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1237 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1238 | channel_input_oclose(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1239 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1240 | int id = packet_get_int(); |
| 1241 | Channel *c = channel_lookup(id); |
| 1242 | packet_integrity_check(plen, 4, type); |
| 1243 | if (c == NULL) |
| 1244 | packet_disconnect("Received oclose for nonexistent channel %d.", id); |
| 1245 | chan_rcvd_oclose(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1246 | } |
| 1247 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1248 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1249 | channel_input_close_confirmation(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1250 | { |
| 1251 | int id = packet_get_int(); |
| 1252 | Channel *c = channel_lookup(id); |
| 1253 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1254 | packet_done(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1255 | if (c == NULL) |
| 1256 | packet_disconnect("Received close confirmation for " |
| 1257 | "out-of-range channel %d.", id); |
| 1258 | if (c->type != SSH_CHANNEL_CLOSED) |
| 1259 | packet_disconnect("Received close confirmation for " |
| 1260 | "non-closed channel %d (type %d).", id, c->type); |
| 1261 | channel_free(c->self); |
| 1262 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1263 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1264 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1265 | channel_input_open_confirmation(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1266 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1267 | int id, remote_id; |
| 1268 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1269 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1270 | if (!compat20) |
| 1271 | packet_integrity_check(plen, 4 + 4, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1272 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1273 | id = packet_get_int(); |
| 1274 | c = channel_lookup(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1275 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1276 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 1277 | packet_disconnect("Received open confirmation for " |
| 1278 | "non-opening channel %d.", id); |
| 1279 | remote_id = packet_get_int(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1280 | /* 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] | 1281 | c->remote_id = remote_id; |
| 1282 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1283 | |
| 1284 | if (compat20) { |
| 1285 | c->remote_window = packet_get_int(); |
| 1286 | c->remote_maxpacket = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1287 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1288 | if (c->cb_fn != NULL && c->cb_event == type) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1289 | debug2("callback start"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1290 | c->cb_fn(c->self, c->cb_arg); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1291 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1292 | } |
| 1293 | debug("channel %d: open confirm rwindow %d rmax %d", c->self, |
| 1294 | c->remote_window, c->remote_maxpacket); |
| 1295 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1296 | } |
| 1297 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1298 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1299 | channel_input_open_failure(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1300 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1301 | int id; |
| 1302 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1303 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1304 | if (!compat20) |
| 1305 | packet_integrity_check(plen, 4, type); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1306 | |
| 1307 | id = packet_get_int(); |
| 1308 | c = channel_lookup(id); |
| 1309 | |
| 1310 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 1311 | packet_disconnect("Received open failure for " |
| 1312 | "non-opening channel %d.", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1313 | if (compat20) { |
| 1314 | int reason = packet_get_int(); |
| 1315 | char *msg = packet_get_string(NULL); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1316 | char *lang = packet_get_string(NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1317 | log("channel_open_failure: %d: reason %d: %s", id, reason, msg); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1318 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1319 | xfree(msg); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1320 | xfree(lang); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1321 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1322 | /* Free the channel. This will also close the socket. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1323 | channel_free(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1324 | } |
| 1325 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1326 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1327 | channel_input_channel_request(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1328 | { |
| 1329 | int id; |
| 1330 | Channel *c; |
| 1331 | |
| 1332 | id = packet_get_int(); |
| 1333 | c = channel_lookup(id); |
| 1334 | |
| 1335 | if (c == NULL || |
| 1336 | (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL)) |
| 1337 | packet_disconnect("Received request for " |
| 1338 | "non-open channel %d.", id); |
| 1339 | if (c->cb_fn != NULL && c->cb_event == type) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1340 | debug2("callback start"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1341 | c->cb_fn(c->self, c->cb_arg); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1342 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1343 | } else { |
| 1344 | char *service = packet_get_string(NULL); |
| 1345 | debug("channel: %d rcvd request for %s", c->self, service); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1346 | debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1347 | xfree(service); |
| 1348 | } |
| 1349 | } |
| 1350 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1351 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1352 | channel_input_window_adjust(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1353 | { |
| 1354 | Channel *c; |
| 1355 | int id, adjust; |
| 1356 | |
| 1357 | if (!compat20) |
| 1358 | return; |
| 1359 | |
| 1360 | /* Get the channel number and verify it. */ |
| 1361 | id = packet_get_int(); |
| 1362 | c = channel_lookup(id); |
| 1363 | |
| 1364 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) { |
| 1365 | log("Received window adjust for " |
| 1366 | "non-open channel %d.", id); |
| 1367 | return; |
| 1368 | } |
| 1369 | adjust = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1370 | packet_done(); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1371 | debug2("channel %d: rcvd adjust %d", id, adjust); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1372 | c->remote_window += adjust; |
| 1373 | } |
| 1374 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1375 | /* |
| 1376 | * Stops listening for channels, and removes any unix domain sockets that we |
| 1377 | * might have. |
| 1378 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1379 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1380 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1381 | channel_stop_listening() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1382 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1383 | int i; |
| 1384 | for (i = 0; i < channels_alloc; i++) { |
| 1385 | switch (channels[i].type) { |
| 1386 | case SSH_CHANNEL_AUTH_SOCKET: |
| 1387 | close(channels[i].sock); |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1388 | unlink(channels[i].path); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1389 | channel_free(i); |
| 1390 | break; |
| 1391 | case SSH_CHANNEL_PORT_LISTENER: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1392 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1393 | case SSH_CHANNEL_X11_LISTENER: |
| 1394 | close(channels[i].sock); |
| 1395 | channel_free(i); |
| 1396 | break; |
| 1397 | default: |
| 1398 | break; |
| 1399 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1400 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1401 | } |
| 1402 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1403 | /* |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1404 | * Closes the sockets/fds of all channels. This is used to close extra file |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1405 | * descriptors after a fork. |
| 1406 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1407 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1408 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1409 | channel_close_all() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1410 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1411 | int i; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1412 | for (i = 0; i < channels_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1413 | if (channels[i].type != SSH_CHANNEL_FREE) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1414 | channel_close_fds(&channels[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /* Returns the maximum file descriptor number used by the channels. */ |
| 1418 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1419 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1420 | channel_max_fd() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1421 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1422 | return channel_max_fd_value; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* Returns true if any channel is still open. */ |
| 1426 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1427 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1428 | channel_still_open() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1429 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1430 | u_int i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1431 | for (i = 0; i < channels_alloc; i++) |
| 1432 | switch (channels[i].type) { |
| 1433 | case SSH_CHANNEL_FREE: |
| 1434 | case SSH_CHANNEL_X11_LISTENER: |
| 1435 | case SSH_CHANNEL_PORT_LISTENER: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1436 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1437 | case SSH_CHANNEL_CLOSED: |
| 1438 | case SSH_CHANNEL_AUTH_SOCKET: |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1439 | case SSH_CHANNEL_CONNECTING: /* XXX ??? */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1440 | continue; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1441 | case SSH_CHANNEL_LARVAL: |
| 1442 | if (!compat20) |
| 1443 | fatal("cannot happen: SSH_CHANNEL_LARVAL"); |
| 1444 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1445 | case SSH_CHANNEL_OPENING: |
| 1446 | case SSH_CHANNEL_OPEN: |
| 1447 | case SSH_CHANNEL_X11_OPEN: |
| 1448 | return 1; |
| 1449 | case SSH_CHANNEL_INPUT_DRAINING: |
| 1450 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 1451 | if (!compat13) |
| 1452 | fatal("cannot happen: OUT_DRAIN"); |
| 1453 | return 1; |
| 1454 | default: |
| 1455 | fatal("channel_still_open: bad channel type %d", channels[i].type); |
| 1456 | /* NOTREACHED */ |
| 1457 | } |
| 1458 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1459 | } |
| 1460 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1461 | /* |
| 1462 | * Returns a message describing the currently open forwarded connections, |
| 1463 | * suitable for sending to the client. The message contains crlf pairs for |
| 1464 | * newlines. |
| 1465 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1466 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1467 | char * |
| 1468 | channel_open_message() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1469 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1470 | Buffer buffer; |
| 1471 | int i; |
| 1472 | char buf[512], *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1473 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1474 | buffer_init(&buffer); |
| 1475 | snprintf(buf, sizeof buf, "The following connections are open:\r\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1476 | buffer_append(&buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1477 | for (i = 0; i < channels_alloc; i++) { |
| 1478 | Channel *c = &channels[i]; |
| 1479 | switch (c->type) { |
| 1480 | case SSH_CHANNEL_FREE: |
| 1481 | case SSH_CHANNEL_X11_LISTENER: |
| 1482 | case SSH_CHANNEL_PORT_LISTENER: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1483 | case SSH_CHANNEL_RPORT_LISTENER: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1484 | case SSH_CHANNEL_CLOSED: |
| 1485 | case SSH_CHANNEL_AUTH_SOCKET: |
| 1486 | continue; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1487 | case SSH_CHANNEL_LARVAL: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1488 | case SSH_CHANNEL_OPENING: |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1489 | case SSH_CHANNEL_CONNECTING: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1490 | case SSH_CHANNEL_OPEN: |
| 1491 | case SSH_CHANNEL_X11_OPEN: |
| 1492 | case SSH_CHANNEL_INPUT_DRAINING: |
| 1493 | case SSH_CHANNEL_OUTPUT_DRAINING: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1494 | snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n", |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1495 | c->self, c->remote_name, |
| 1496 | c->type, c->remote_id, |
| 1497 | c->istate, buffer_len(&c->input), |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1498 | c->ostate, buffer_len(&c->output), |
| 1499 | c->rfd, c->wfd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1500 | buffer_append(&buffer, buf, strlen(buf)); |
| 1501 | continue; |
| 1502 | default: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1503 | fatal("channel_open_message: bad channel type %d", c->type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1504 | /* NOTREACHED */ |
| 1505 | } |
| 1506 | } |
| 1507 | buffer_append(&buffer, "\0", 1); |
| 1508 | cp = xstrdup(buffer_ptr(&buffer)); |
| 1509 | buffer_free(&buffer); |
| 1510 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1511 | } |
| 1512 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1513 | /* |
| 1514 | * Initiate forwarding of connections to local port "port" through the secure |
| 1515 | * channel to host:port from remote side. |
| 1516 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1517 | void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1518 | channel_request_local_forwarding(u_short listen_port, const char *host_to_connect, |
| 1519 | u_short port_to_connect, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1520 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1521 | channel_request_forwarding( |
| 1522 | NULL, listen_port, |
| 1523 | host_to_connect, port_to_connect, |
| 1524 | gateway_ports, /*remote_fwd*/ 0); |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * If 'remote_fwd' is true we have a '-R style' listener for protocol 2 |
| 1529 | * (SSH_CHANNEL_RPORT_LISTENER). |
| 1530 | */ |
| 1531 | void |
| 1532 | channel_request_forwarding( |
| 1533 | const char *listen_address, u_short listen_port, |
| 1534 | const char *host_to_connect, u_short port_to_connect, |
| 1535 | int gateway_ports, int remote_fwd) |
| 1536 | { |
| 1537 | int success, ch, sock, on = 1, ctype; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1538 | struct addrinfo hints, *ai, *aitop; |
| 1539 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1540 | const char *host; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1541 | struct linger linger; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1542 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1543 | if (remote_fwd) { |
| 1544 | host = listen_address; |
| 1545 | ctype = SSH_CHANNEL_RPORT_LISTENER; |
| 1546 | } else { |
| 1547 | host = host_to_connect; |
| 1548 | ctype =SSH_CHANNEL_PORT_LISTENER; |
| 1549 | } |
| 1550 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1551 | if (strlen(host) > sizeof(channels[0].path) - 1) |
| 1552 | packet_disconnect("Forward host name too long."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1553 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1554 | /* XXX listen_address is currently ignored */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1555 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1556 | * getaddrinfo returns a loopback address if the hostname is |
| 1557 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1558 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1559 | memset(&hints, 0, sizeof(hints)); |
| 1560 | hints.ai_family = IPv4or6; |
| 1561 | hints.ai_flags = gateway_ports ? AI_PASSIVE : 0; |
| 1562 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1563 | snprintf(strport, sizeof strport, "%d", listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1564 | if (getaddrinfo(NULL, strport, &hints, &aitop) != 0) |
| 1565 | packet_disconnect("getaddrinfo: fatal error"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1566 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1567 | success = 0; |
| 1568 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1569 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1570 | continue; |
| 1571 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 1572 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1573 | error("channel_request_forwarding: getnameinfo failed"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1574 | continue; |
| 1575 | } |
| 1576 | /* Create a port to listen for the host. */ |
| 1577 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1578 | if (sock < 0) { |
| 1579 | /* this is no error since kernel may not support ipv6 */ |
| 1580 | verbose("socket: %.100s", strerror(errno)); |
| 1581 | continue; |
| 1582 | } |
| 1583 | /* |
| 1584 | * Set socket options. We would like the socket to disappear |
| 1585 | * as soon as it has been closed for whatever reason. |
| 1586 | */ |
| 1587 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); |
| 1588 | linger.l_onoff = 1; |
| 1589 | linger.l_linger = 5; |
| 1590 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger)); |
| 1591 | debug("Local forwarding listening on %s port %s.", ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1592 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1593 | /* Bind the socket to the address. */ |
| 1594 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1595 | /* address can be in use ipv6 address is already bound */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 1596 | if (!ai->ai_next) |
| 1597 | error("bind: %.100s", strerror(errno)); |
| 1598 | else |
| 1599 | verbose("bind: %.100s", strerror(errno)); |
| 1600 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1601 | close(sock); |
| 1602 | continue; |
| 1603 | } |
| 1604 | /* Start listening for connections on the socket. */ |
| 1605 | if (listen(sock, 5) < 0) { |
| 1606 | error("listen: %.100s", strerror(errno)); |
| 1607 | close(sock); |
| 1608 | continue; |
| 1609 | } |
| 1610 | /* Allocate a channel number for the socket. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1611 | ch = channel_new("port listener", ctype, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1612 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1613 | 0, xstrdup("port listener"), 1); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1614 | strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1615 | channels[ch].host_port = port_to_connect; |
| 1616 | channels[ch].listening_port = listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1617 | success = 1; |
| 1618 | } |
| 1619 | if (success == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1620 | packet_disconnect("cannot listen port: %d", listen_port); /*XXX ?disconnect? */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1621 | freeaddrinfo(aitop); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1622 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1623 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1624 | /* |
| 1625 | * Initiate forwarding of connections to port "port" on remote host through |
| 1626 | * the secure channel to host:port from local side. |
| 1627 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1628 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1629 | void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1630 | channel_request_remote_forwarding(u_short listen_port, |
| 1631 | const char *host_to_connect, u_short port_to_connect) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1632 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1633 | int payload_len, type, success = 0; |
| 1634 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1635 | /* Record locally that connection to this host/port is permitted. */ |
| 1636 | if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 1637 | fatal("channel_request_remote_forwarding: too many forwards"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1638 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1639 | /* Send the forward request to the remote side. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1640 | if (compat20) { |
| 1641 | const char *address_to_bind = "0.0.0.0"; |
| 1642 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 1643 | packet_put_cstring("tcpip-forward"); |
| 1644 | packet_put_char(0); /* boolean: want reply */ |
| 1645 | packet_put_cstring(address_to_bind); |
| 1646 | packet_put_int(listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1647 | packet_send(); |
| 1648 | packet_write_wait(); |
| 1649 | /* Assume that server accepts the request */ |
| 1650 | success = 1; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1651 | } else { |
| 1652 | packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1653 | packet_put_int(listen_port); |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 1654 | packet_put_cstring(host_to_connect); |
| 1655 | packet_put_int(port_to_connect); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1656 | packet_send(); |
| 1657 | packet_write_wait(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1658 | |
| 1659 | /* Wait for response from the remote side. */ |
| 1660 | type = packet_read(&payload_len); |
| 1661 | switch (type) { |
| 1662 | case SSH_SMSG_SUCCESS: |
| 1663 | success = 1; |
| 1664 | break; |
| 1665 | case SSH_SMSG_FAILURE: |
| 1666 | log("Warning: Server denied remote port forwarding."); |
| 1667 | break; |
| 1668 | default: |
| 1669 | /* Unknown packet */ |
| 1670 | packet_disconnect("Protocol error for port forward request:" |
| 1671 | "received packet type %d.", type); |
| 1672 | } |
| 1673 | } |
| 1674 | if (success) { |
| 1675 | permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect); |
| 1676 | permitted_opens[num_permitted_opens].port_to_connect = port_to_connect; |
| 1677 | permitted_opens[num_permitted_opens].listen_port = listen_port; |
| 1678 | num_permitted_opens++; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1679 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1680 | } |
| 1681 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1682 | /* |
| 1683 | * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates |
| 1684 | * listening for the port, and sends back a success reply (or disconnect |
| 1685 | * message if there was an error). This never returns if there was an error. |
| 1686 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1687 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1688 | void |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1689 | channel_input_port_forward_request(int is_root, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1690 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1691 | u_short port, host_port; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1692 | char *hostname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1693 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1694 | /* Get arguments from the packet. */ |
| 1695 | port = packet_get_int(); |
| 1696 | hostname = packet_get_string(NULL); |
| 1697 | host_port = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1698 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1699 | #ifndef HAVE_CYGWIN |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1700 | /* |
| 1701 | * Check that an unprivileged user is not trying to forward a |
| 1702 | * privileged port. |
| 1703 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1704 | if (port < IPPORT_RESERVED && !is_root) |
| 1705 | packet_disconnect("Requested forwarding of port %d but user is not root.", |
| 1706 | port); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1707 | #endif |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1708 | /* Initiate forwarding */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1709 | channel_request_local_forwarding(port, hostname, host_port, gateway_ports); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1710 | |
| 1711 | /* Free the argument string. */ |
| 1712 | xfree(hostname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1713 | } |
| 1714 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1715 | /* XXX move to aux.c */ |
| 1716 | int |
| 1717 | channel_connect_to(const char *host, u_short host_port) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1718 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1719 | struct addrinfo hints, *ai, *aitop; |
| 1720 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 1721 | int gaierr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1722 | int sock = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1723 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1724 | memset(&hints, 0, sizeof(hints)); |
| 1725 | hints.ai_family = IPv4or6; |
| 1726 | hints.ai_socktype = SOCK_STREAM; |
| 1727 | snprintf(strport, sizeof strport, "%d", host_port); |
| 1728 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { |
| 1729 | error("%.100s: unknown host (%s)", host, gai_strerror(gaierr)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1730 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1731 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1732 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1733 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1734 | continue; |
| 1735 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 1736 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1737 | error("channel_connect_to: getnameinfo failed"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1738 | continue; |
| 1739 | } |
| 1740 | /* Create the socket. */ |
| 1741 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1742 | if (sock < 0) { |
| 1743 | error("socket: %.100s", strerror(errno)); |
| 1744 | continue; |
| 1745 | } |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 1746 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1747 | fatal("connect_to: F_SETFL: %s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1748 | /* Connect to the host/port. */ |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1749 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 && |
| 1750 | errno != EINPROGRESS) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1751 | error("connect %.100s port %s: %.100s", ntop, strport, |
| 1752 | strerror(errno)); |
| 1753 | close(sock); |
| 1754 | continue; /* fail -- try next */ |
| 1755 | } |
| 1756 | break; /* success */ |
| 1757 | |
| 1758 | } |
| 1759 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1760 | if (!ai) { |
| 1761 | error("connect %.100s port %d: failed.", host, host_port); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1762 | return -1; |
| 1763 | } |
| 1764 | /* success */ |
| 1765 | return sock; |
| 1766 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1767 | int |
| 1768 | channel_connect_by_listen_adress(u_short listen_port) |
| 1769 | { |
| 1770 | int i; |
| 1771 | for (i = 0; i < num_permitted_opens; i++) |
| 1772 | if (permitted_opens[i].listen_port == listen_port) |
| 1773 | return channel_connect_to( |
| 1774 | permitted_opens[i].host_to_connect, |
| 1775 | permitted_opens[i].port_to_connect); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 1776 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 1777 | listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1778 | return -1; |
| 1779 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1780 | |
| 1781 | /* |
| 1782 | * This is called after receiving PORT_OPEN message. This attempts to |
| 1783 | * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION |
| 1784 | * or CHANNEL_OPEN_FAILURE. |
| 1785 | */ |
| 1786 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1787 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1788 | channel_input_port_open(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1789 | { |
| 1790 | u_short host_port; |
| 1791 | char *host, *originator_string; |
| 1792 | int remote_channel, sock = -1, newch, i, denied; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1793 | u_int host_len, originator_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1794 | |
| 1795 | /* Get remote channel number. */ |
| 1796 | remote_channel = packet_get_int(); |
| 1797 | |
| 1798 | /* Get host name to connect to. */ |
| 1799 | host = packet_get_string(&host_len); |
| 1800 | |
| 1801 | /* Get port to connect to. */ |
| 1802 | host_port = packet_get_int(); |
| 1803 | |
| 1804 | /* Get remote originator name. */ |
| 1805 | if (have_hostname_in_open) { |
| 1806 | originator_string = packet_get_string(&originator_len); |
| 1807 | originator_len += 4; /* size of packet_int */ |
| 1808 | } else { |
| 1809 | originator_string = xstrdup("unknown (remote did not supply name)"); |
| 1810 | originator_len = 0; /* no originator supplied */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1811 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1812 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1813 | packet_integrity_check(plen, |
| 1814 | 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1815 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1816 | /* Check if opening that port is permitted. */ |
| 1817 | denied = 0; |
| 1818 | if (!all_opens_permitted) { |
| 1819 | /* Go trough all permitted ports. */ |
| 1820 | for (i = 0; i < num_permitted_opens; i++) |
| 1821 | if (permitted_opens[i].port_to_connect == host_port && |
| 1822 | strcmp(permitted_opens[i].host_to_connect, host) == 0) |
| 1823 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1824 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1825 | /* Check if we found the requested port among those permitted. */ |
| 1826 | if (i >= num_permitted_opens) { |
| 1827 | /* The port is not permitted. */ |
| 1828 | log("Received request to connect to %.100s:%d, but the request was denied.", |
| 1829 | host, host_port); |
| 1830 | denied = 1; |
| 1831 | } |
| 1832 | } |
| 1833 | sock = denied ? -1 : channel_connect_to(host, host_port); |
| 1834 | if (sock > 0) { |
| 1835 | /* Allocate a channel for this connection. */ |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1836 | newch = channel_allocate(SSH_CHANNEL_CONNECTING, |
| 1837 | sock, originator_string); |
| 1838 | /*XXX delay answer? */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1839 | channels[newch].remote_id = remote_channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1840 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1841 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1842 | packet_put_int(remote_channel); |
| 1843 | packet_put_int(newch); |
| 1844 | packet_send(); |
| 1845 | } else { |
| 1846 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1847 | packet_put_int(remote_channel); |
| 1848 | packet_send(); |
| 1849 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1850 | xfree(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1851 | } |
| 1852 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1853 | /* |
| 1854 | * Creates an internet domain socket for listening for X11 connections. |
| 1855 | * Returns a suitable value for the DISPLAY variable, or NULL if an error |
| 1856 | * occurs. |
| 1857 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1858 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1859 | #define NUM_SOCKS 10 |
| 1860 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1861 | char * |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1862 | x11_create_display_inet(int screen_number, int x11_display_offset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1863 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1864 | int display_number, sock; |
| 1865 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1866 | struct addrinfo hints, *ai, *aitop; |
| 1867 | char strport[NI_MAXSERV]; |
| 1868 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
| 1869 | char display[512]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1870 | char hostname[MAXHOSTNAMELEN]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1871 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1872 | for (display_number = x11_display_offset; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1873 | display_number < MAX_DISPLAYS; |
| 1874 | display_number++) { |
| 1875 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1876 | memset(&hints, 0, sizeof(hints)); |
| 1877 | hints.ai_family = IPv4or6; |
| 1878 | hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */ |
| 1879 | hints.ai_socktype = SOCK_STREAM; |
| 1880 | snprintf(strport, sizeof strport, "%d", port); |
| 1881 | if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) { |
| 1882 | error("getaddrinfo: %.100s", gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1883 | return NULL; |
| 1884 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1885 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1886 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1887 | continue; |
| 1888 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1889 | if (sock < 0) { |
Damien Miller | 89d9796 | 2000-10-14 12:37:19 +1100 | [diff] [blame] | 1890 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 1891 | error("socket: %.100s", strerror(errno)); |
| 1892 | return NULL; |
| 1893 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1894 | debug("x11_create_display_inet: Socket family %d not supported", |
| 1895 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 1896 | continue; |
| 1897 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1898 | } |
| 1899 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1900 | debug("bind port %d: %.100s", port, strerror(errno)); |
| 1901 | shutdown(sock, SHUT_RDWR); |
| 1902 | close(sock); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 1903 | |
| 1904 | if (ai->ai_next) |
| 1905 | continue; |
| 1906 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1907 | for (n = 0; n < num_socks; n++) { |
| 1908 | shutdown(socks[n], SHUT_RDWR); |
| 1909 | close(socks[n]); |
| 1910 | } |
| 1911 | num_socks = 0; |
| 1912 | break; |
| 1913 | } |
| 1914 | socks[num_socks++] = sock; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1915 | #ifndef DONT_TRY_OTHER_AF |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1916 | if (num_socks == NUM_SOCKS) |
| 1917 | break; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1918 | #else |
| 1919 | break; |
| 1920 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1921 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1922 | if (num_socks > 0) |
| 1923 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1924 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1925 | if (display_number >= MAX_DISPLAYS) { |
| 1926 | error("Failed to allocate internet-domain X11 display socket."); |
| 1927 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1928 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1929 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1930 | for (n = 0; n < num_socks; n++) { |
| 1931 | sock = socks[n]; |
| 1932 | if (listen(sock, 5) < 0) { |
| 1933 | error("listen: %.100s", strerror(errno)); |
| 1934 | shutdown(sock, SHUT_RDWR); |
| 1935 | close(sock); |
| 1936 | return NULL; |
| 1937 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1938 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1939 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1940 | /* Set up a suitable value for the DISPLAY variable. */ |
| 1941 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 1942 | fatal("gethostname: %.100s", strerror(errno)); |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1943 | |
| 1944 | #ifdef IPADDR_IN_DISPLAY |
| 1945 | /* |
| 1946 | * HPUX detects the local hostname in the DISPLAY variable and tries |
| 1947 | * to set up a shared memory connection to the server, which it |
| 1948 | * incorrectly supposes to be local. |
| 1949 | * |
| 1950 | * The workaround - as used in later $$H and other programs - is |
| 1951 | * is to set display to the host's IP address. |
| 1952 | */ |
| 1953 | { |
| 1954 | struct hostent *he; |
| 1955 | struct in_addr my_addr; |
| 1956 | |
| 1957 | he = gethostbyname(hostname); |
| 1958 | if (he == NULL) { |
| 1959 | error("[X11-broken-fwd-hostname-workaround] Could not get " |
| 1960 | "IP address for hostname %s.", hostname); |
| 1961 | |
| 1962 | packet_send_debug("[X11-broken-fwd-hostname-workaround]" |
| 1963 | "Could not get IP address for hostname %s.", hostname); |
| 1964 | |
| 1965 | shutdown(sock, SHUT_RDWR); |
| 1966 | close(sock); |
| 1967 | |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | |
| 1971 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 1972 | |
| 1973 | /* Set DISPLAY to <ip address>:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1974 | snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr), |
Kevin Steves | da6cd59 | 2000-12-29 18:41:21 +0000 | [diff] [blame] | 1975 | display_number, screen_number); |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1976 | } |
| 1977 | #else /* IPADDR_IN_DISPLAY */ |
| 1978 | /* Just set DISPLAY to hostname:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1979 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, |
Kevin Steves | da6cd59 | 2000-12-29 18:41:21 +0000 | [diff] [blame] | 1980 | display_number, screen_number); |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1981 | #endif /* IPADDR_IN_DISPLAY */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1982 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1983 | /* Allocate a channel for each socket. */ |
| 1984 | for (n = 0; n < num_socks; n++) { |
| 1985 | sock = socks[n]; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1986 | (void) channel_new("x11 listener", |
| 1987 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 1988 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1989 | 0, xstrdup("X11 inet listener"), 1); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1990 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1991 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1992 | /* Return a suitable value for the DISPLAY environment variable. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1993 | return xstrdup(display); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | #ifndef X_UNIX_PATH |
| 1997 | #define X_UNIX_PATH "/tmp/.X11-unix/X" |
| 1998 | #endif |
| 1999 | |
| 2000 | static |
| 2001 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2002 | connect_local_xsocket(u_int dnr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2003 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2004 | static const char *const x_sockets[] = { |
| 2005 | X_UNIX_PATH "%u", |
| 2006 | "/var/X/.X11-unix/X" "%u", |
| 2007 | "/usr/spool/sockets/X11/" "%u", |
| 2008 | NULL |
| 2009 | }; |
| 2010 | int sock; |
| 2011 | struct sockaddr_un addr; |
| 2012 | const char *const * path; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2013 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2014 | for (path = x_sockets; *path; ++path) { |
| 2015 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 2016 | if (sock < 0) |
| 2017 | error("socket: %.100s", strerror(errno)); |
| 2018 | memset(&addr, 0, sizeof(addr)); |
| 2019 | addr.sun_family = AF_UNIX; |
| 2020 | snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr); |
| 2021 | if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0) |
| 2022 | return sock; |
| 2023 | close(sock); |
| 2024 | } |
| 2025 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 2026 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2027 | } |
| 2028 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2029 | int |
| 2030 | x11_connect_display(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2031 | { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2032 | int display_number, sock = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2033 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2034 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2035 | struct addrinfo hints, *ai, *aitop; |
| 2036 | char strport[NI_MAXSERV]; |
| 2037 | int gaierr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2038 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2039 | /* Try to open a socket for the local X server. */ |
| 2040 | display = getenv("DISPLAY"); |
| 2041 | if (!display) { |
| 2042 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2043 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2044 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2045 | /* |
| 2046 | * Now we decode the value of the DISPLAY variable and make a |
| 2047 | * connection to the real X server. |
| 2048 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2049 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2050 | /* |
| 2051 | * Check if it is a unix domain socket. Unix domain displays are in |
| 2052 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 2053 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2054 | if (strncmp(display, "unix:", 5) == 0 || |
| 2055 | display[0] == ':') { |
| 2056 | /* Connect to the unix domain socket. */ |
| 2057 | if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) { |
| 2058 | error("Could not parse display number from DISPLAY: %.100s", |
| 2059 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2060 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2061 | } |
| 2062 | /* Create a socket. */ |
| 2063 | sock = connect_local_xsocket(display_number); |
| 2064 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2065 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2066 | |
| 2067 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2068 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2069 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2070 | /* |
| 2071 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 2072 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 2073 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2074 | strncpy(buf, display, sizeof(buf)); |
| 2075 | buf[sizeof(buf) - 1] = 0; |
| 2076 | cp = strchr(buf, ':'); |
| 2077 | if (!cp) { |
| 2078 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2079 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2080 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2081 | *cp = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2082 | /* buf now contains the host name. But first we parse the display number. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2083 | if (sscanf(cp + 1, "%d", &display_number) != 1) { |
| 2084 | error("Could not parse display number from DISPLAY: %.100s", |
| 2085 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2086 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2087 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2088 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2089 | /* Look up the host address */ |
| 2090 | memset(&hints, 0, sizeof(hints)); |
| 2091 | hints.ai_family = IPv4or6; |
| 2092 | hints.ai_socktype = SOCK_STREAM; |
| 2093 | snprintf(strport, sizeof strport, "%d", 6000 + display_number); |
| 2094 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
| 2095 | error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2096 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2097 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2098 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 2099 | /* Create a socket. */ |
| 2100 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 2101 | if (sock < 0) { |
| 2102 | debug("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2103 | continue; |
| 2104 | } |
| 2105 | /* Connect it to the display. */ |
| 2106 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 2107 | debug("connect %.100s port %d: %.100s", buf, |
| 2108 | 6000 + display_number, strerror(errno)); |
| 2109 | close(sock); |
| 2110 | continue; |
| 2111 | } |
| 2112 | /* Success */ |
| 2113 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2114 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2115 | freeaddrinfo(aitop); |
| 2116 | if (!ai) { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2117 | error("connect %.100s port %d: %.100s", buf, 6000 + display_number, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2118 | strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2119 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2120 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2121 | return sock; |
| 2122 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2123 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2124 | /* |
| 2125 | * This is called when SSH_SMSG_X11_OPEN is received. The packet contains |
| 2126 | * the remote channel number. We should do whatever we want, and respond |
| 2127 | * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. |
| 2128 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2129 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2130 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2131 | x11_input_open(int type, int plen, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2132 | { |
| 2133 | int remote_channel, sock = 0, newch; |
| 2134 | char *remote_host; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2135 | u_int remote_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2136 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2137 | /* Get remote channel number. */ |
| 2138 | remote_channel = packet_get_int(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2139 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2140 | /* Get remote originator name. */ |
| 2141 | if (have_hostname_in_open) { |
| 2142 | remote_host = packet_get_string(&remote_len); |
| 2143 | remote_len += 4; |
| 2144 | } else { |
| 2145 | remote_host = xstrdup("unknown (remote did not supply name)"); |
| 2146 | remote_len = 0; |
| 2147 | } |
| 2148 | |
| 2149 | debug("Received X11 open request."); |
| 2150 | packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN); |
| 2151 | |
| 2152 | /* Obtain a connection to the real X display. */ |
| 2153 | sock = x11_connect_display(); |
| 2154 | if (sock == -1) { |
| 2155 | /* Send refusal to the remote host. */ |
| 2156 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2157 | packet_put_int(remote_channel); |
| 2158 | packet_send(); |
| 2159 | } else { |
| 2160 | /* Allocate a channel for this connection. */ |
| 2161 | newch = channel_allocate( |
| 2162 | (x11_saved_proto == NULL) ? |
| 2163 | SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN, |
| 2164 | sock, remote_host); |
| 2165 | channels[newch].remote_id = remote_channel; |
| 2166 | |
| 2167 | /* Send a confirmation to the remote host. */ |
| 2168 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 2169 | packet_put_int(remote_channel); |
| 2170 | packet_put_int(newch); |
| 2171 | packet_send(); |
| 2172 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2173 | } |
| 2174 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2175 | /* dummy protocol handler that denies SSH-1 requests (agent/x11) */ |
| 2176 | void |
| 2177 | deny_input_open(int type, int plen, void *ctxt) |
| 2178 | { |
| 2179 | int rchan = packet_get_int(); |
| 2180 | switch(type){ |
| 2181 | case SSH_SMSG_AGENT_OPEN: |
| 2182 | error("Warning: ssh server tried agent forwarding."); |
| 2183 | break; |
| 2184 | case SSH_SMSG_X11_OPEN: |
| 2185 | error("Warning: ssh server tried X11 forwarding."); |
| 2186 | break; |
| 2187 | default: |
| 2188 | error("deny_input_open: type %d plen %d", type, plen); |
| 2189 | break; |
| 2190 | } |
| 2191 | error("Warning: this is probably a break in attempt by a malicious server."); |
| 2192 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2193 | packet_put_int(rchan); |
| 2194 | packet_send(); |
| 2195 | } |
| 2196 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2197 | /* |
| 2198 | * Requests forwarding of X11 connections, generates fake authentication |
| 2199 | * data, and enables authentication spoofing. |
| 2200 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2201 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2202 | void |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2203 | x11_request_forwarding_with_spoofing(int client_session_id, |
| 2204 | const char *proto, const char *data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2205 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2206 | u_int data_len = (u_int) strlen(data) / 2; |
| 2207 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2208 | char *new_data; |
| 2209 | int screen_number; |
| 2210 | const char *cp; |
| 2211 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2212 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2213 | cp = getenv("DISPLAY"); |
| 2214 | if (cp) |
| 2215 | cp = strchr(cp, ':'); |
| 2216 | if (cp) |
| 2217 | cp = strchr(cp, '.'); |
| 2218 | if (cp) |
| 2219 | screen_number = atoi(cp + 1); |
| 2220 | else |
| 2221 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2222 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2223 | /* Save protocol name. */ |
| 2224 | x11_saved_proto = xstrdup(proto); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2225 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2226 | /* |
| 2227 | * Extract real authentication data and generate fake data of the |
| 2228 | * same length. |
| 2229 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2230 | x11_saved_data = xmalloc(data_len); |
| 2231 | x11_fake_data = xmalloc(data_len); |
| 2232 | for (i = 0; i < data_len; i++) { |
| 2233 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 2234 | fatal("x11_request_forwarding: bad authentication data: %.100s", data); |
| 2235 | if (i % 4 == 0) |
| 2236 | rand = arc4random(); |
| 2237 | x11_saved_data[i] = value; |
| 2238 | x11_fake_data[i] = rand & 0xff; |
| 2239 | rand >>= 8; |
| 2240 | } |
| 2241 | x11_saved_data_len = data_len; |
| 2242 | x11_fake_data_len = data_len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2243 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2244 | /* Convert the fake data into hex. */ |
| 2245 | new_data = xmalloc(2 * data_len + 1); |
| 2246 | for (i = 0; i < data_len; i++) |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2247 | sprintf(new_data + 2 * i, "%02x", (u_char) x11_fake_data[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2248 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2249 | /* Send the request packet. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2250 | if (compat20) { |
| 2251 | channel_request_start(client_session_id, "x11-req", 0); |
| 2252 | packet_put_char(0); /* XXX bool single connection */ |
| 2253 | } else { |
| 2254 | packet_start(SSH_CMSG_X11_REQUEST_FORWARDING); |
| 2255 | } |
| 2256 | packet_put_cstring(proto); |
| 2257 | packet_put_cstring(new_data); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2258 | packet_put_int(screen_number); |
| 2259 | packet_send(); |
| 2260 | packet_write_wait(); |
| 2261 | xfree(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* Sends a message to the server to request authentication fd forwarding. */ |
| 2265 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2266 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2267 | auth_request_forwarding() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2268 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2269 | packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING); |
| 2270 | packet_send(); |
| 2271 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2272 | } |
| 2273 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2274 | /* |
| 2275 | * Returns the name of the forwarded authentication socket. Returns NULL if |
| 2276 | * there is no forwarded authentication socket. The returned value points to |
| 2277 | * a static buffer. |
| 2278 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2279 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2280 | char * |
| 2281 | auth_get_socket_name() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2282 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2283 | return channel_forwarded_auth_socket_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | /* removes the agent forwarding socket */ |
| 2287 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2288 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2289 | cleanup_socket(void) |
| 2290 | { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 2291 | unlink(channel_forwarded_auth_socket_name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2292 | rmdir(channel_forwarded_auth_socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2293 | } |
| 2294 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2295 | /* |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2296 | * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2297 | * This starts forwarding authentication requests. |
| 2298 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2299 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2300 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2301 | auth_input_request_forwarding(struct passwd * pw) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2302 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2303 | int sock, newch; |
| 2304 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2305 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2306 | if (auth_get_socket_name() != NULL) |
| 2307 | fatal("Protocol error: authentication forwarding requested twice."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2308 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2309 | /* Temporarily drop privileged uid for mkdir/bind. */ |
| 2310 | temporarily_use_uid(pw->pw_uid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2311 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2312 | /* Allocate a buffer for the socket name, and format the name. */ |
| 2313 | channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME); |
| 2314 | channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME); |
| 2315 | strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2316 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2317 | /* Create private directory for socket */ |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2318 | if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) { |
| 2319 | packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s", |
| 2320 | strerror(errno)); |
| 2321 | restore_uid(); |
| 2322 | xfree(channel_forwarded_auth_socket_name); |
| 2323 | xfree(channel_forwarded_auth_socket_dir); |
| 2324 | channel_forwarded_auth_socket_name = NULL; |
| 2325 | channel_forwarded_auth_socket_dir = NULL; |
| 2326 | return 0; |
| 2327 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2328 | snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d", |
| 2329 | channel_forwarded_auth_socket_dir, (int) getpid()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2330 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2331 | if (atexit(cleanup_socket) < 0) { |
| 2332 | int saved = errno; |
| 2333 | cleanup_socket(); |
| 2334 | packet_disconnect("socket: %.100s", strerror(saved)); |
| 2335 | } |
| 2336 | /* Create the socket. */ |
| 2337 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 2338 | if (sock < 0) |
| 2339 | packet_disconnect("socket: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2340 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2341 | /* Bind it to the name. */ |
| 2342 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 2343 | sunaddr.sun_family = AF_UNIX; |
| 2344 | strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name, |
| 2345 | sizeof(sunaddr.sun_path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2346 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2347 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) |
| 2348 | packet_disconnect("bind: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2349 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2350 | /* Restore the privileged uid. */ |
| 2351 | restore_uid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2352 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2353 | /* Start listening on the socket. */ |
| 2354 | if (listen(sock, 5) < 0) |
| 2355 | packet_disconnect("listen: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2356 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2357 | /* Allocate a channel for the authentication agent socket. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2358 | newch = channel_new("auth socket", |
| 2359 | SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, |
| 2360 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
| 2361 | 0, xstrdup("auth socket"), 1); |
| 2362 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 2363 | strlcpy(channels[newch].path, channel_forwarded_auth_socket_name, |
| 2364 | sizeof(channels[newch].path)); |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2365 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | /* This is called to process an SSH_SMSG_AGENT_OPEN message. */ |
| 2369 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2370 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2371 | auth_input_open_request(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2372 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2373 | int remch, sock, newch; |
| 2374 | char *dummyname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2375 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2376 | packet_integrity_check(plen, 4, type); |
| 2377 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2378 | /* Read the remote channel number from the message. */ |
| 2379 | remch = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2380 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2381 | /* |
| 2382 | * Get a connection to the local authentication agent (this may again |
| 2383 | * get forwarded). |
| 2384 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2385 | sock = ssh_get_authentication_socket(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2386 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2387 | /* |
| 2388 | * If we could not connect the agent, send an error message back to |
| 2389 | * the server. This should never happen unless the agent dies, |
| 2390 | * because authentication forwarding is only enabled if we have an |
| 2391 | * agent. |
| 2392 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2393 | if (sock < 0) { |
| 2394 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2395 | packet_put_int(remch); |
| 2396 | packet_send(); |
| 2397 | return; |
| 2398 | } |
| 2399 | debug("Forwarding authentication connection."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2400 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2401 | /* |
| 2402 | * Dummy host name. This will be freed when the channel is freed; it |
| 2403 | * will still be valid in the packet_put_string below since the |
| 2404 | * channel cannot yet be freed at that point. |
| 2405 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2406 | dummyname = xstrdup("authentication agent connection"); |
| 2407 | |
| 2408 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname); |
| 2409 | channels[newch].remote_id = remch; |
| 2410 | |
| 2411 | /* Send a confirmation to the remote host. */ |
| 2412 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 2413 | packet_put_int(remch); |
| 2414 | packet_put_int(newch); |
| 2415 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2416 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2417 | |
| 2418 | void |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2419 | channel_start_open(int id) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2420 | { |
| 2421 | Channel *c = channel_lookup(id); |
| 2422 | if (c == NULL) { |
| 2423 | log("channel_open: %d: bad id", id); |
| 2424 | return; |
| 2425 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2426 | debug("send channel open %d", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2427 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 2428 | packet_put_cstring(c->ctype); |
| 2429 | packet_put_int(c->self); |
| 2430 | packet_put_int(c->local_window); |
| 2431 | packet_put_int(c->local_maxpacket); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2432 | } |
| 2433 | void |
| 2434 | channel_open(int id) |
| 2435 | { |
| 2436 | /* XXX REMOVE ME */ |
| 2437 | channel_start_open(id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2438 | packet_send(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2439 | } |
| 2440 | void |
| 2441 | channel_request(int id, char *service, int wantconfirm) |
| 2442 | { |
| 2443 | channel_request_start(id, service, wantconfirm); |
| 2444 | packet_send(); |
| 2445 | debug("channel request %d: %s", id, service) ; |
| 2446 | } |
| 2447 | void |
| 2448 | channel_request_start(int id, char *service, int wantconfirm) |
| 2449 | { |
| 2450 | Channel *c = channel_lookup(id); |
| 2451 | if (c == NULL) { |
| 2452 | log("channel_request: %d: bad id", id); |
| 2453 | return; |
| 2454 | } |
| 2455 | packet_start(SSH2_MSG_CHANNEL_REQUEST); |
| 2456 | packet_put_int(c->remote_id); |
| 2457 | packet_put_cstring(service); |
| 2458 | packet_put_char(wantconfirm); |
| 2459 | } |
| 2460 | void |
| 2461 | channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg) |
| 2462 | { |
| 2463 | Channel *c = channel_lookup(id); |
| 2464 | if (c == NULL) { |
| 2465 | log("channel_register_callback: %d: bad id", id); |
| 2466 | return; |
| 2467 | } |
| 2468 | c->cb_event = mtype; |
| 2469 | c->cb_fn = fn; |
| 2470 | c->cb_arg = arg; |
| 2471 | } |
| 2472 | void |
| 2473 | channel_register_cleanup(int id, channel_callback_fn *fn) |
| 2474 | { |
| 2475 | Channel *c = channel_lookup(id); |
| 2476 | if (c == NULL) { |
| 2477 | log("channel_register_cleanup: %d: bad id", id); |
| 2478 | return; |
| 2479 | } |
| 2480 | c->dettach_user = fn; |
| 2481 | } |
| 2482 | void |
| 2483 | channel_cancel_cleanup(int id) |
| 2484 | { |
| 2485 | Channel *c = channel_lookup(id); |
| 2486 | if (c == NULL) { |
| 2487 | log("channel_cancel_cleanup: %d: bad id", id); |
| 2488 | return; |
| 2489 | } |
| 2490 | c->dettach_user = NULL; |
| 2491 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 2492 | void |
| 2493 | channel_register_filter(int id, channel_filter_fn *fn) |
| 2494 | { |
| 2495 | Channel *c = channel_lookup(id); |
| 2496 | if (c == NULL) { |
| 2497 | log("channel_register_filter: %d: bad id", id); |
| 2498 | return; |
| 2499 | } |
| 2500 | c->input_filter = fn; |
| 2501 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2502 | |
| 2503 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2504 | channel_set_fds(int id, int rfd, int wfd, int efd, |
| 2505 | int extusage, int nonblock) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2506 | { |
| 2507 | Channel *c = channel_lookup(id); |
| 2508 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 2509 | fatal("channel_activate for non-larval channel %d.", id); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2510 | channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2511 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2512 | /* XXX window size? */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2513 | c->local_window = c->local_window_max = c->local_maxpacket * 2; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2514 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 2515 | packet_put_int(c->remote_id); |
| 2516 | packet_put_int(c->local_window); |
| 2517 | packet_send(); |
| 2518 | } |