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