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