Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * This file contains functions for generic socket connection forwarding. |
| 6 | * There is also code for initiating connection forwarding for X11 connections, |
| 7 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
| 14 | * |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. |
| 18 | * Copyright (c) 1999 Dug Song. All rights reserved. |
| 19 | * Copyright (c) 1999 Theo de Raadt. All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * |
| 30 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 31 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 32 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 33 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 35 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 39 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 43 | RCSID("$OpenBSD: channels.c,v 1.72 2000/10/27 07:48:22 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; |
| 591 | char buf[1024], *remote_hostname; |
| 592 | int remote_port; |
| 593 | |
| 594 | if (FD_ISSET(c->sock, readset)) { |
| 595 | debug("Connection to port %d forwarding " |
| 596 | "to %.100s port %d requested.", |
| 597 | c->listening_port, c->path, c->host_port); |
| 598 | addrlen = sizeof(addr); |
| 599 | newsock = accept(c->sock, &addr, &addrlen); |
| 600 | if (newsock < 0) { |
| 601 | error("accept: %.100s", strerror(errno)); |
| 602 | return; |
| 603 | } |
| 604 | remote_hostname = get_remote_hostname(newsock); |
| 605 | remote_port = get_peer_port(newsock); |
| 606 | snprintf(buf, sizeof buf, |
| 607 | "listen port %d for %.100s port %d, " |
| 608 | "connect from %.200s port %d", |
| 609 | c->listening_port, c->path, c->host_port, |
| 610 | remote_hostname, remote_port); |
| 611 | newch = channel_new("direct-tcpip", |
| 612 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 613 | c->local_window_max, c->local_maxpacket, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 614 | 0, xstrdup(buf), 1); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 615 | if (compat20) { |
| 616 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 617 | packet_put_cstring("direct-tcpip"); |
| 618 | packet_put_int(newch); |
| 619 | packet_put_int(c->local_window_max); |
| 620 | packet_put_int(c->local_maxpacket); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 621 | /* target host and port */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 622 | packet_put_string(c->path, strlen(c->path)); |
| 623 | packet_put_int(c->host_port); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 624 | /* originator host and port */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 625 | packet_put_cstring(remote_hostname); |
| 626 | packet_put_int(remote_port); |
| 627 | packet_send(); |
| 628 | } else { |
| 629 | packet_start(SSH_MSG_PORT_OPEN); |
| 630 | packet_put_int(newch); |
| 631 | packet_put_string(c->path, strlen(c->path)); |
| 632 | packet_put_int(c->host_port); |
| 633 | if (have_hostname_in_open) { |
| 634 | packet_put_string(buf, strlen(buf)); |
| 635 | } |
| 636 | packet_send(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 637 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 638 | xfree(remote_hostname); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * This is the authentication agent socket listening for connections from |
| 644 | * clients. |
| 645 | */ |
| 646 | void |
| 647 | channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset) |
| 648 | { |
| 649 | struct sockaddr addr; |
| 650 | int newsock, newch; |
| 651 | socklen_t addrlen; |
| 652 | |
| 653 | if (FD_ISSET(c->sock, readset)) { |
| 654 | addrlen = sizeof(addr); |
| 655 | newsock = accept(c->sock, &addr, &addrlen); |
| 656 | if (newsock < 0) { |
| 657 | error("accept from auth socket: %.100s", strerror(errno)); |
| 658 | return; |
| 659 | } |
| 660 | newch = channel_allocate(SSH_CHANNEL_OPENING, newsock, |
| 661 | xstrdup("accepted auth socket")); |
| 662 | packet_start(SSH_SMSG_AGENT_OPEN); |
| 663 | packet_put_int(newch); |
| 664 | packet_send(); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | int |
| 669 | channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset) |
| 670 | { |
| 671 | char buf[16*1024]; |
| 672 | int len; |
| 673 | |
| 674 | if (c->rfd != -1 && |
| 675 | FD_ISSET(c->rfd, readset)) { |
| 676 | len = read(c->rfd, buf, sizeof(buf)); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 677 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 678 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 679 | if (len <= 0) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 680 | debug("channel %d: read<=0 rfd %d len %d", |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 681 | c->self, c->rfd, len); |
| 682 | if (compat13) { |
| 683 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 684 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
| 685 | debug("Channel %d status set to input draining.", c->self); |
| 686 | } else { |
| 687 | chan_read_failed(c); |
| 688 | } |
| 689 | return -1; |
| 690 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 691 | if(c->input_filter != NULL) { |
| 692 | if (c->input_filter(c, buf, len) == -1) { |
| 693 | debug("filter stops channel %d", c->self); |
| 694 | chan_read_failed(c); |
| 695 | } |
| 696 | } else { |
| 697 | buffer_append(&c->input, buf, len); |
| 698 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 699 | } |
| 700 | return 1; |
| 701 | } |
| 702 | int |
| 703 | channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset) |
| 704 | { |
| 705 | int len; |
| 706 | |
| 707 | /* Send buffered output data to the socket. */ |
| 708 | if (c->wfd != -1 && |
| 709 | FD_ISSET(c->wfd, writeset) && |
| 710 | buffer_len(&c->output) > 0) { |
| 711 | len = write(c->wfd, buffer_ptr(&c->output), |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 712 | buffer_len(&c->output)); |
| 713 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 714 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 715 | if (len <= 0) { |
| 716 | if (compat13) { |
| 717 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 718 | debug("Channel %d status set to input draining.", c->self); |
| 719 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
| 720 | } else { |
| 721 | chan_write_failed(c); |
| 722 | } |
| 723 | return -1; |
| 724 | } |
| 725 | buffer_consume(&c->output, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 726 | if (compat20 && len > 0) { |
| 727 | c->local_consumed += len; |
| 728 | } |
| 729 | } |
| 730 | return 1; |
| 731 | } |
| 732 | int |
| 733 | channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset) |
| 734 | { |
| 735 | char buf[16*1024]; |
| 736 | int len; |
| 737 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 738 | /** XXX handle drain efd, too */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 739 | if (c->efd != -1) { |
| 740 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
| 741 | FD_ISSET(c->efd, writeset) && |
| 742 | buffer_len(&c->extended) > 0) { |
| 743 | len = write(c->efd, buffer_ptr(&c->extended), |
| 744 | buffer_len(&c->extended)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 745 | debug2("channel %d: written %d to efd %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 746 | c->self, len, c->efd); |
| 747 | if (len > 0) { |
| 748 | buffer_consume(&c->extended, len); |
| 749 | c->local_consumed += len; |
| 750 | } |
| 751 | } else if (c->extended_usage == CHAN_EXTENDED_READ && |
| 752 | FD_ISSET(c->efd, readset)) { |
| 753 | len = read(c->efd, buf, sizeof(buf)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 754 | debug2("channel %d: read %d from efd %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 755 | c->self, len, c->efd); |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 756 | if (len == 0) { |
| 757 | debug("channel %d: closing efd %d", |
| 758 | c->self, c->efd); |
| 759 | close(c->efd); |
| 760 | c->efd = -1; |
| 761 | } else if (len > 0) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 762 | buffer_append(&c->extended, buf, len); |
| 763 | } |
| 764 | } |
| 765 | return 1; |
| 766 | } |
| 767 | int |
| 768 | channel_check_window(Channel *c, fd_set * readset, fd_set * writeset) |
| 769 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 770 | if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 771 | c->local_window < c->local_window_max/2 && |
| 772 | c->local_consumed > 0) { |
| 773 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 774 | packet_put_int(c->remote_id); |
| 775 | packet_put_int(c->local_consumed); |
| 776 | packet_send(); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 777 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 778 | c->self, c->local_window, |
| 779 | c->local_consumed); |
| 780 | c->local_window += c->local_consumed; |
| 781 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 782 | } |
| 783 | return 1; |
| 784 | } |
| 785 | |
| 786 | void |
| 787 | channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset) |
| 788 | { |
| 789 | channel_handle_rfd(c, readset, writeset); |
| 790 | channel_handle_wfd(c, readset, writeset); |
| 791 | } |
| 792 | |
| 793 | void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 794 | channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset) |
| 795 | { |
| 796 | channel_handle_rfd(c, readset, writeset); |
| 797 | channel_handle_wfd(c, readset, writeset); |
| 798 | channel_handle_efd(c, readset, writeset); |
| 799 | channel_check_window(c, readset, writeset); |
| 800 | } |
| 801 | |
| 802 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 803 | channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset) |
| 804 | { |
| 805 | int len; |
| 806 | /* Send buffered output data to the socket. */ |
| 807 | if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) { |
| 808 | len = write(c->sock, buffer_ptr(&c->output), |
| 809 | buffer_len(&c->output)); |
| 810 | if (len <= 0) |
| 811 | buffer_consume(&c->output, buffer_len(&c->output)); |
| 812 | else |
| 813 | buffer_consume(&c->output, len); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 818 | channel_handler_init_20(void) |
| 819 | { |
| 820 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 821 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 822 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 823 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 824 | |
| 825 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2; |
| 826 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 827 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 831 | channel_handler_init_13(void) |
| 832 | { |
| 833 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13; |
| 834 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13; |
| 835 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 836 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 837 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 838 | channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining; |
| 839 | channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining; |
| 840 | |
| 841 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1; |
| 842 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 843 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 844 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 845 | channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13; |
| 846 | } |
| 847 | |
| 848 | void |
| 849 | channel_handler_init_15(void) |
| 850 | { |
| 851 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 852 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 853 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 854 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 855 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 856 | |
| 857 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 858 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 859 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 860 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1; |
| 861 | } |
| 862 | |
| 863 | void |
| 864 | channel_handler_init(void) |
| 865 | { |
| 866 | int i; |
| 867 | for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) { |
| 868 | channel_pre[i] = NULL; |
| 869 | channel_post[i] = NULL; |
| 870 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 871 | if (compat20) |
| 872 | channel_handler_init_20(); |
| 873 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 874 | channel_handler_init_13(); |
| 875 | else |
| 876 | channel_handler_init_15(); |
| 877 | } |
| 878 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 879 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 880 | channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset) |
| 881 | { |
| 882 | static int did_init = 0; |
| 883 | int i; |
| 884 | Channel *c; |
| 885 | |
| 886 | if (!did_init) { |
| 887 | channel_handler_init(); |
| 888 | did_init = 1; |
| 889 | } |
| 890 | for (i = 0; i < channels_alloc; i++) { |
| 891 | c = &channels[i]; |
| 892 | if (c->type == SSH_CHANNEL_FREE) |
| 893 | continue; |
| 894 | if (ftab[c->type] == NULL) |
| 895 | continue; |
| 896 | (*ftab[c->type])(c, readset, writeset); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 897 | chan_delete_if_full_closed(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 898 | } |
| 899 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 900 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 901 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 902 | channel_prepare_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 903 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 904 | channel_handler(channel_pre, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 905 | } |
| 906 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 907 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 908 | channel_after_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 909 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 910 | channel_handler(channel_post, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* If there is data to send to the connection, send some of it now. */ |
| 914 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 915 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 916 | channel_output_poll() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 917 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 918 | int len, i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 919 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 920 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 921 | for (i = 0; i < channels_alloc; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 922 | c = &channels[i]; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 923 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 924 | /* We are only interested in channels that can have buffered incoming data. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 925 | if (compat13) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 926 | if (c->type != SSH_CHANNEL_OPEN && |
| 927 | c->type != SSH_CHANNEL_INPUT_DRAINING) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 928 | continue; |
| 929 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 930 | if (c->type != SSH_CHANNEL_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 931 | continue; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 932 | if (c->istate != CHAN_INPUT_OPEN && |
| 933 | c->istate != CHAN_INPUT_WAIT_DRAIN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 934 | continue; |
| 935 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 936 | if (compat20 && |
| 937 | (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 938 | debug("channel: %d: no data after CLOSE", c->self); |
| 939 | continue; |
| 940 | } |
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 | /* Get the amount of buffered data for this channel. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 943 | len = buffer_len(&c->input); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 944 | if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 945 | /* Send some data for the other side over the secure connection. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 946 | if (compat20) { |
| 947 | if (len > c->remote_window) |
| 948 | len = c->remote_window; |
| 949 | if (len > c->remote_maxpacket) |
| 950 | len = c->remote_maxpacket; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 951 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 952 | if (packet_is_interactive()) { |
| 953 | if (len > 1024) |
| 954 | len = 512; |
| 955 | } else { |
| 956 | /* Keep the packets at reasonable size. */ |
| 957 | if (len > packet_get_maxsize()/2) |
| 958 | len = packet_get_maxsize()/2; |
| 959 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 960 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 961 | if (len > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 962 | packet_start(compat20 ? |
| 963 | SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 964 | packet_put_int(c->remote_id); |
| 965 | packet_put_string(buffer_ptr(&c->input), len); |
| 966 | packet_send(); |
| 967 | buffer_consume(&c->input, len); |
| 968 | c->remote_window -= len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 969 | } |
| 970 | } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 971 | if (compat13) |
| 972 | fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 973 | /* |
| 974 | * input-buffer is empty and read-socket shutdown: |
| 975 | * tell peer, that we will not send more data: send IEOF |
| 976 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 977 | chan_ibuf_empty(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 978 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 979 | /* Send extended data, i.e. stderr */ |
| 980 | if (compat20 && |
| 981 | c->remote_window > 0 && |
| 982 | (len = buffer_len(&c->extended)) > 0 && |
| 983 | c->extended_usage == CHAN_EXTENDED_READ) { |
| 984 | if (len > c->remote_window) |
| 985 | len = c->remote_window; |
| 986 | if (len > c->remote_maxpacket) |
| 987 | len = c->remote_maxpacket; |
| 988 | packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
| 989 | packet_put_int(c->remote_id); |
| 990 | packet_put_int(SSH2_EXTENDED_DATA_STDERR); |
| 991 | packet_put_string(buffer_ptr(&c->extended), len); |
| 992 | packet_send(); |
| 993 | buffer_consume(&c->extended, len); |
| 994 | c->remote_window -= len; |
| 995 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 996 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 997 | } |
| 998 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 999 | /* |
| 1000 | * This is called when a packet of type CHANNEL_DATA has just been received. |
| 1001 | * The message type has already been consumed, but channel number and data is |
| 1002 | * still there. |
| 1003 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1004 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1005 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1006 | channel_input_data(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1007 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1008 | int id; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1009 | char *data; |
| 1010 | unsigned int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1011 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1012 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1013 | /* Get the channel number and verify it. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1014 | id = packet_get_int(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1015 | c = channel_lookup(id); |
| 1016 | if (c == NULL) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1017 | packet_disconnect("Received data for nonexistent channel %d.", id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1018 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1019 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1020 | if (c->type != SSH_CHANNEL_OPEN && |
| 1021 | c->type != SSH_CHANNEL_X11_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1022 | return; |
| 1023 | |
| 1024 | /* same for protocol 1.5 if output end is no longer open */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1025 | if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1026 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1027 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1028 | /* Get the data. */ |
| 1029 | data = packet_get_string(&data_len); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1030 | packet_done(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1031 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1032 | if (compat20){ |
| 1033 | if (data_len > c->local_maxpacket) { |
| 1034 | log("channel %d: rcvd big packet %d, maxpack %d", |
| 1035 | c->self, data_len, c->local_maxpacket); |
| 1036 | } |
| 1037 | if (data_len > c->local_window) { |
| 1038 | log("channel %d: rcvd too much data %d, win %d", |
| 1039 | c->self, data_len, c->local_window); |
| 1040 | xfree(data); |
| 1041 | return; |
| 1042 | } |
| 1043 | c->local_window -= data_len; |
| 1044 | }else{ |
| 1045 | packet_integrity_check(plen, 4 + 4 + data_len, type); |
| 1046 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1047 | buffer_append(&c->output, data, data_len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1048 | xfree(data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1049 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1050 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1051 | channel_input_extended_data(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1052 | { |
| 1053 | int id; |
| 1054 | int tcode; |
| 1055 | char *data; |
| 1056 | unsigned int data_len; |
| 1057 | Channel *c; |
| 1058 | |
| 1059 | /* Get the channel number and verify it. */ |
| 1060 | id = packet_get_int(); |
| 1061 | c = channel_lookup(id); |
| 1062 | |
| 1063 | if (c == NULL) |
| 1064 | packet_disconnect("Received extended_data for bad channel %d.", id); |
| 1065 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1066 | log("channel %d: ext data for non open", id); |
| 1067 | return; |
| 1068 | } |
| 1069 | tcode = packet_get_int(); |
| 1070 | if (c->efd == -1 || |
| 1071 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 1072 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
| 1073 | log("channel %d: bad ext data", c->self); |
| 1074 | return; |
| 1075 | } |
| 1076 | data = packet_get_string(&data_len); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1077 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1078 | if (data_len > c->local_window) { |
| 1079 | log("channel %d: rcvd too much extended_data %d, win %d", |
| 1080 | c->self, data_len, c->local_window); |
| 1081 | xfree(data); |
| 1082 | return; |
| 1083 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1084 | debug2("channel %d: rcvd ext data %d", c->self, data_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1085 | c->local_window -= data_len; |
| 1086 | buffer_append(&c->extended, data, data_len); |
| 1087 | xfree(data); |
| 1088 | } |
| 1089 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1090 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1091 | /* |
| 1092 | * Returns true if no channel has too much buffered data, and false if one or |
| 1093 | * more channel is overfull. |
| 1094 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1095 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1096 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1097 | channel_not_very_much_buffered_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1098 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1099 | unsigned int i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1100 | Channel *c; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1101 | |
| 1102 | for (i = 0; i < channels_alloc; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1103 | c = &channels[i]; |
| 1104 | if (c->type == SSH_CHANNEL_OPEN) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1105 | if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1106 | debug("channel %d: big input buffer %d", |
| 1107 | c->self, buffer_len(&c->input)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1108 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1109 | } |
| 1110 | if (buffer_len(&c->output) > packet_get_maxsize()) { |
| 1111 | debug("channel %d: big output buffer %d", |
| 1112 | c->self, buffer_len(&c->output)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1113 | return 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1114 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1115 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1116 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1117 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1118 | } |
| 1119 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1120 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1121 | channel_input_ieof(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1122 | { |
| 1123 | int id; |
| 1124 | Channel *c; |
| 1125 | |
| 1126 | packet_integrity_check(plen, 4, type); |
| 1127 | |
| 1128 | id = packet_get_int(); |
| 1129 | c = channel_lookup(id); |
| 1130 | if (c == NULL) |
| 1131 | packet_disconnect("Received ieof for nonexistent channel %d.", id); |
| 1132 | chan_rcvd_ieof(c); |
| 1133 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1134 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1135 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1136 | channel_input_close(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1137 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1138 | int id; |
| 1139 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1140 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1141 | packet_integrity_check(plen, 4, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1142 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1143 | id = packet_get_int(); |
| 1144 | c = channel_lookup(id); |
| 1145 | if (c == NULL) |
| 1146 | packet_disconnect("Received close for nonexistent channel %d.", id); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * Send a confirmation that we have closed the channel and no more |
| 1150 | * data is coming for it. |
| 1151 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1152 | packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1153 | packet_put_int(c->remote_id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1154 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1155 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1156 | /* |
| 1157 | * If the channel is in closed state, we have sent a close request, |
| 1158 | * and the other side will eventually respond with a confirmation. |
| 1159 | * Thus, we cannot free the channel here, because then there would be |
| 1160 | * no-one to receive the confirmation. The channel gets freed when |
| 1161 | * the confirmation arrives. |
| 1162 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1163 | if (c->type != SSH_CHANNEL_CLOSED) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1164 | /* |
| 1165 | * Not a closed channel - mark it as draining, which will |
| 1166 | * cause it to be freed later. |
| 1167 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1168 | buffer_consume(&c->input, buffer_len(&c->input)); |
| 1169 | c->type = SSH_CHANNEL_OUTPUT_DRAINING; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1170 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1171 | } |
| 1172 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1173 | /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1174 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1175 | channel_input_oclose(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1176 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1177 | int id = packet_get_int(); |
| 1178 | Channel *c = channel_lookup(id); |
| 1179 | packet_integrity_check(plen, 4, type); |
| 1180 | if (c == NULL) |
| 1181 | packet_disconnect("Received oclose for nonexistent channel %d.", id); |
| 1182 | chan_rcvd_oclose(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1183 | } |
| 1184 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1185 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1186 | channel_input_close_confirmation(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1187 | { |
| 1188 | int id = packet_get_int(); |
| 1189 | Channel *c = channel_lookup(id); |
| 1190 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1191 | packet_done(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1192 | if (c == NULL) |
| 1193 | packet_disconnect("Received close confirmation for " |
| 1194 | "out-of-range channel %d.", id); |
| 1195 | if (c->type != SSH_CHANNEL_CLOSED) |
| 1196 | packet_disconnect("Received close confirmation for " |
| 1197 | "non-closed channel %d (type %d).", id, c->type); |
| 1198 | channel_free(c->self); |
| 1199 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1200 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1201 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1202 | channel_input_open_confirmation(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1203 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1204 | int id, remote_id; |
| 1205 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1206 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1207 | if (!compat20) |
| 1208 | packet_integrity_check(plen, 4 + 4, type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1209 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1210 | id = packet_get_int(); |
| 1211 | c = channel_lookup(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1212 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1213 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 1214 | packet_disconnect("Received open confirmation for " |
| 1215 | "non-opening channel %d.", id); |
| 1216 | remote_id = packet_get_int(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1217 | /* 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] | 1218 | c->remote_id = remote_id; |
| 1219 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1220 | |
| 1221 | if (compat20) { |
| 1222 | c->remote_window = packet_get_int(); |
| 1223 | c->remote_maxpacket = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1224 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1225 | if (c->cb_fn != NULL && c->cb_event == type) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1226 | debug2("callback start"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1227 | c->cb_fn(c->self, c->cb_arg); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1228 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1229 | } |
| 1230 | debug("channel %d: open confirm rwindow %d rmax %d", c->self, |
| 1231 | c->remote_window, c->remote_maxpacket); |
| 1232 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1233 | } |
| 1234 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1235 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1236 | channel_input_open_failure(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1237 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1238 | int id; |
| 1239 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1240 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1241 | if (!compat20) |
| 1242 | packet_integrity_check(plen, 4, type); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1243 | |
| 1244 | id = packet_get_int(); |
| 1245 | c = channel_lookup(id); |
| 1246 | |
| 1247 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 1248 | packet_disconnect("Received open failure for " |
| 1249 | "non-opening channel %d.", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1250 | if (compat20) { |
| 1251 | int reason = packet_get_int(); |
| 1252 | char *msg = packet_get_string(NULL); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1253 | char *lang = packet_get_string(NULL); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1254 | log("channel_open_failure: %d: reason %d: %s", id, reason, msg); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1255 | packet_done(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1256 | xfree(msg); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1257 | xfree(lang); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1258 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1259 | /* Free the channel. This will also close the socket. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1260 | channel_free(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1261 | } |
| 1262 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1263 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1264 | channel_input_channel_request(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1265 | { |
| 1266 | int id; |
| 1267 | Channel *c; |
| 1268 | |
| 1269 | id = packet_get_int(); |
| 1270 | c = channel_lookup(id); |
| 1271 | |
| 1272 | if (c == NULL || |
| 1273 | (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL)) |
| 1274 | packet_disconnect("Received request for " |
| 1275 | "non-open channel %d.", id); |
| 1276 | if (c->cb_fn != NULL && c->cb_event == type) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1277 | debug2("callback start"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1278 | c->cb_fn(c->self, c->cb_arg); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1279 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1280 | } else { |
| 1281 | char *service = packet_get_string(NULL); |
| 1282 | debug("channel: %d rcvd request for %s", c->self, service); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1283 | 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] | 1284 | xfree(service); |
| 1285 | } |
| 1286 | } |
| 1287 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1288 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1289 | channel_input_window_adjust(int type, int plen, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1290 | { |
| 1291 | Channel *c; |
| 1292 | int id, adjust; |
| 1293 | |
| 1294 | if (!compat20) |
| 1295 | return; |
| 1296 | |
| 1297 | /* Get the channel number and verify it. */ |
| 1298 | id = packet_get_int(); |
| 1299 | c = channel_lookup(id); |
| 1300 | |
| 1301 | if (c == NULL || c->type != SSH_CHANNEL_OPEN) { |
| 1302 | log("Received window adjust for " |
| 1303 | "non-open channel %d.", id); |
| 1304 | return; |
| 1305 | } |
| 1306 | adjust = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1307 | packet_done(); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1308 | debug2("channel %d: rcvd adjust %d", id, adjust); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1309 | c->remote_window += adjust; |
| 1310 | } |
| 1311 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1312 | /* |
| 1313 | * Stops listening for channels, and removes any unix domain sockets that we |
| 1314 | * might have. |
| 1315 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1316 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1317 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1318 | channel_stop_listening() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1319 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1320 | int i; |
| 1321 | for (i = 0; i < channels_alloc; i++) { |
| 1322 | switch (channels[i].type) { |
| 1323 | case SSH_CHANNEL_AUTH_SOCKET: |
| 1324 | close(channels[i].sock); |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 1325 | unlink(channels[i].path); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1326 | channel_free(i); |
| 1327 | break; |
| 1328 | case SSH_CHANNEL_PORT_LISTENER: |
| 1329 | case SSH_CHANNEL_X11_LISTENER: |
| 1330 | close(channels[i].sock); |
| 1331 | channel_free(i); |
| 1332 | break; |
| 1333 | default: |
| 1334 | break; |
| 1335 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1336 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1337 | } |
| 1338 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1339 | /* |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1340 | * 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] | 1341 | * descriptors after a fork. |
| 1342 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1343 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1344 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1345 | channel_close_all() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1346 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1347 | int i; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1348 | for (i = 0; i < channels_alloc; i++) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1349 | if (channels[i].type != SSH_CHANNEL_FREE) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1350 | channel_close_fds(&channels[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* Returns the maximum file descriptor number used by the channels. */ |
| 1354 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1355 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1356 | channel_max_fd() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1357 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1358 | return channel_max_fd_value; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* Returns true if any channel is still open. */ |
| 1362 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1363 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1364 | channel_still_open() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1365 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1366 | unsigned int i; |
| 1367 | for (i = 0; i < channels_alloc; i++) |
| 1368 | switch (channels[i].type) { |
| 1369 | case SSH_CHANNEL_FREE: |
| 1370 | case SSH_CHANNEL_X11_LISTENER: |
| 1371 | case SSH_CHANNEL_PORT_LISTENER: |
| 1372 | case SSH_CHANNEL_CLOSED: |
| 1373 | case SSH_CHANNEL_AUTH_SOCKET: |
| 1374 | continue; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1375 | case SSH_CHANNEL_LARVAL: |
| 1376 | if (!compat20) |
| 1377 | fatal("cannot happen: SSH_CHANNEL_LARVAL"); |
| 1378 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1379 | case SSH_CHANNEL_OPENING: |
| 1380 | case SSH_CHANNEL_OPEN: |
| 1381 | case SSH_CHANNEL_X11_OPEN: |
| 1382 | return 1; |
| 1383 | case SSH_CHANNEL_INPUT_DRAINING: |
| 1384 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 1385 | if (!compat13) |
| 1386 | fatal("cannot happen: OUT_DRAIN"); |
| 1387 | return 1; |
| 1388 | default: |
| 1389 | fatal("channel_still_open: bad channel type %d", channels[i].type); |
| 1390 | /* NOTREACHED */ |
| 1391 | } |
| 1392 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1393 | } |
| 1394 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1395 | /* |
| 1396 | * Returns a message describing the currently open forwarded connections, |
| 1397 | * suitable for sending to the client. The message contains crlf pairs for |
| 1398 | * newlines. |
| 1399 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1400 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1401 | char * |
| 1402 | channel_open_message() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1403 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1404 | Buffer buffer; |
| 1405 | int i; |
| 1406 | char buf[512], *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1407 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1408 | buffer_init(&buffer); |
| 1409 | snprintf(buf, sizeof buf, "The following connections are open:\r\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1410 | buffer_append(&buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1411 | for (i = 0; i < channels_alloc; i++) { |
| 1412 | Channel *c = &channels[i]; |
| 1413 | switch (c->type) { |
| 1414 | case SSH_CHANNEL_FREE: |
| 1415 | case SSH_CHANNEL_X11_LISTENER: |
| 1416 | case SSH_CHANNEL_PORT_LISTENER: |
| 1417 | case SSH_CHANNEL_CLOSED: |
| 1418 | case SSH_CHANNEL_AUTH_SOCKET: |
| 1419 | continue; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1420 | case SSH_CHANNEL_LARVAL: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1421 | case SSH_CHANNEL_OPENING: |
| 1422 | case SSH_CHANNEL_OPEN: |
| 1423 | case SSH_CHANNEL_X11_OPEN: |
| 1424 | case SSH_CHANNEL_INPUT_DRAINING: |
| 1425 | case SSH_CHANNEL_OUTPUT_DRAINING: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1426 | 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] | 1427 | c->self, c->remote_name, |
| 1428 | c->type, c->remote_id, |
| 1429 | c->istate, buffer_len(&c->input), |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1430 | c->ostate, buffer_len(&c->output), |
| 1431 | c->rfd, c->wfd); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1432 | buffer_append(&buffer, buf, strlen(buf)); |
| 1433 | continue; |
| 1434 | default: |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1435 | fatal("channel_open_message: bad channel type %d", c->type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1436 | /* NOTREACHED */ |
| 1437 | } |
| 1438 | } |
| 1439 | buffer_append(&buffer, "\0", 1); |
| 1440 | cp = xstrdup(buffer_ptr(&buffer)); |
| 1441 | buffer_free(&buffer); |
| 1442 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1443 | } |
| 1444 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1445 | /* |
| 1446 | * Initiate forwarding of connections to local port "port" through the secure |
| 1447 | * channel to host:port from remote side. |
| 1448 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1449 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1450 | void |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1451 | channel_request_local_forwarding(u_short port, const char *host, |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1452 | u_short host_port, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1453 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1454 | int success, ch, sock, on = 1; |
| 1455 | struct addrinfo hints, *ai, *aitop; |
| 1456 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1457 | struct linger linger; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1458 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1459 | if (strlen(host) > sizeof(channels[0].path) - 1) |
| 1460 | packet_disconnect("Forward host name too long."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1461 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1462 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1463 | * getaddrinfo returns a loopback address if the hostname is |
| 1464 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1465 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1466 | memset(&hints, 0, sizeof(hints)); |
| 1467 | hints.ai_family = IPv4or6; |
| 1468 | hints.ai_flags = gateway_ports ? AI_PASSIVE : 0; |
| 1469 | hints.ai_socktype = SOCK_STREAM; |
| 1470 | snprintf(strport, sizeof strport, "%d", port); |
| 1471 | if (getaddrinfo(NULL, strport, &hints, &aitop) != 0) |
| 1472 | packet_disconnect("getaddrinfo: fatal error"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1473 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1474 | success = 0; |
| 1475 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1476 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1477 | continue; |
| 1478 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 1479 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 1480 | error("channel_request_local_forwarding: getnameinfo failed"); |
| 1481 | continue; |
| 1482 | } |
| 1483 | /* Create a port to listen for the host. */ |
| 1484 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1485 | if (sock < 0) { |
| 1486 | /* this is no error since kernel may not support ipv6 */ |
| 1487 | verbose("socket: %.100s", strerror(errno)); |
| 1488 | continue; |
| 1489 | } |
| 1490 | /* |
| 1491 | * Set socket options. We would like the socket to disappear |
| 1492 | * as soon as it has been closed for whatever reason. |
| 1493 | */ |
| 1494 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); |
| 1495 | linger.l_onoff = 1; |
| 1496 | linger.l_linger = 5; |
| 1497 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger)); |
| 1498 | debug("Local forwarding listening on %s port %s.", ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1499 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1500 | /* Bind the socket to the address. */ |
| 1501 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1502 | /* address can be in use ipv6 address is already bound */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 1503 | if (!ai->ai_next) |
| 1504 | error("bind: %.100s", strerror(errno)); |
| 1505 | else |
| 1506 | verbose("bind: %.100s", strerror(errno)); |
| 1507 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1508 | close(sock); |
| 1509 | continue; |
| 1510 | } |
| 1511 | /* Start listening for connections on the socket. */ |
| 1512 | if (listen(sock, 5) < 0) { |
| 1513 | error("listen: %.100s", strerror(errno)); |
| 1514 | close(sock); |
| 1515 | continue; |
| 1516 | } |
| 1517 | /* Allocate a channel number for the socket. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1518 | ch = channel_new( |
| 1519 | "port listener", SSH_CHANNEL_PORT_LISTENER, |
| 1520 | sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1521 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1522 | 0, xstrdup("port listener"), 1); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1523 | strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); |
| 1524 | channels[ch].host_port = host_port; |
| 1525 | channels[ch].listening_port = port; |
| 1526 | success = 1; |
| 1527 | } |
| 1528 | if (success == 0) |
| 1529 | packet_disconnect("cannot listen port: %d", port); |
| 1530 | freeaddrinfo(aitop); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1531 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1532 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1533 | /* |
| 1534 | * Initiate forwarding of connections to port "port" on remote host through |
| 1535 | * the secure channel to host:port from local side. |
| 1536 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1537 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1538 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1539 | channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect, |
| 1540 | u_short port_to_connect) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1541 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1542 | int payload_len; |
| 1543 | /* Record locally that connection to this host/port is permitted. */ |
| 1544 | if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 1545 | fatal("channel_request_remote_forwarding: too many forwards"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1546 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1547 | permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect); |
| 1548 | permitted_opens[num_permitted_opens].port_to_connect = port_to_connect; |
| 1549 | permitted_opens[num_permitted_opens].listen_port = listen_port; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1550 | num_permitted_opens++; |
| 1551 | |
| 1552 | /* Send the forward request to the remote side. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1553 | if (compat20) { |
| 1554 | const char *address_to_bind = "0.0.0.0"; |
| 1555 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 1556 | packet_put_cstring("tcpip-forward"); |
| 1557 | packet_put_char(0); /* boolean: want reply */ |
| 1558 | packet_put_cstring(address_to_bind); |
| 1559 | packet_put_int(listen_port); |
| 1560 | } else { |
| 1561 | packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1562 | packet_put_int(listen_port); |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 1563 | packet_put_cstring(host_to_connect); |
| 1564 | packet_put_int(port_to_connect); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1565 | packet_send(); |
| 1566 | packet_write_wait(); |
| 1567 | /* |
| 1568 | * Wait for response from the remote side. It will send a disconnect |
| 1569 | * message on failure, and we will never see it here. |
| 1570 | */ |
| 1571 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); |
| 1572 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1573 | } |
| 1574 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1575 | /* |
| 1576 | * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates |
| 1577 | * listening for the port, and sends back a success reply (or disconnect |
| 1578 | * message if there was an error). This never returns if there was an error. |
| 1579 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1580 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1581 | void |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1582 | channel_input_port_forward_request(int is_root, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1583 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1584 | u_short port, host_port; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1585 | char *hostname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1586 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1587 | /* Get arguments from the packet. */ |
| 1588 | port = packet_get_int(); |
| 1589 | hostname = packet_get_string(NULL); |
| 1590 | host_port = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1591 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1592 | #ifndef HAVE_CYGWIN |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1593 | /* |
| 1594 | * Check that an unprivileged user is not trying to forward a |
| 1595 | * privileged port. |
| 1596 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1597 | if (port < IPPORT_RESERVED && !is_root) |
| 1598 | packet_disconnect("Requested forwarding of port %d but user is not root.", |
| 1599 | port); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1600 | #endif |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1601 | /* |
| 1602 | * Initiate forwarding, |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1603 | */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 1604 | channel_request_local_forwarding(port, hostname, host_port, gateway_ports); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1605 | |
| 1606 | /* Free the argument string. */ |
| 1607 | xfree(hostname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1608 | } |
| 1609 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1610 | /* XXX move to aux.c */ |
| 1611 | int |
| 1612 | channel_connect_to(const char *host, u_short host_port) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1613 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1614 | struct addrinfo hints, *ai, *aitop; |
| 1615 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 1616 | int gaierr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1617 | int sock = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1618 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1619 | memset(&hints, 0, sizeof(hints)); |
| 1620 | hints.ai_family = IPv4or6; |
| 1621 | hints.ai_socktype = SOCK_STREAM; |
| 1622 | snprintf(strport, sizeof strport, "%d", host_port); |
| 1623 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { |
| 1624 | error("%.100s: unknown host (%s)", host, gai_strerror(gaierr)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1625 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1626 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1627 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1628 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1629 | continue; |
| 1630 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 1631 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1632 | error("channel_connect_to: getnameinfo failed"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1633 | continue; |
| 1634 | } |
| 1635 | /* Create the socket. */ |
| 1636 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1637 | if (sock < 0) { |
| 1638 | error("socket: %.100s", strerror(errno)); |
| 1639 | continue; |
| 1640 | } |
| 1641 | /* Connect to the host/port. */ |
| 1642 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1643 | error("connect %.100s port %s: %.100s", ntop, strport, |
| 1644 | strerror(errno)); |
| 1645 | close(sock); |
| 1646 | continue; /* fail -- try next */ |
| 1647 | } |
| 1648 | break; /* success */ |
| 1649 | |
| 1650 | } |
| 1651 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1652 | if (!ai) { |
| 1653 | error("connect %.100s port %d: failed.", host, host_port); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1654 | return -1; |
| 1655 | } |
| 1656 | /* success */ |
| 1657 | return sock; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | * This is called after receiving PORT_OPEN message. This attempts to |
| 1662 | * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION |
| 1663 | * or CHANNEL_OPEN_FAILURE. |
| 1664 | */ |
| 1665 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1666 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1667 | channel_input_port_open(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1668 | { |
| 1669 | u_short host_port; |
| 1670 | char *host, *originator_string; |
| 1671 | int remote_channel, sock = -1, newch, i, denied; |
| 1672 | unsigned int host_len, originator_len; |
| 1673 | |
| 1674 | /* Get remote channel number. */ |
| 1675 | remote_channel = packet_get_int(); |
| 1676 | |
| 1677 | /* Get host name to connect to. */ |
| 1678 | host = packet_get_string(&host_len); |
| 1679 | |
| 1680 | /* Get port to connect to. */ |
| 1681 | host_port = packet_get_int(); |
| 1682 | |
| 1683 | /* Get remote originator name. */ |
| 1684 | if (have_hostname_in_open) { |
| 1685 | originator_string = packet_get_string(&originator_len); |
| 1686 | originator_len += 4; /* size of packet_int */ |
| 1687 | } else { |
| 1688 | originator_string = xstrdup("unknown (remote did not supply name)"); |
| 1689 | originator_len = 0; /* no originator supplied */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1690 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1691 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1692 | packet_integrity_check(plen, |
| 1693 | 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1694 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1695 | /* Check if opening that port is permitted. */ |
| 1696 | denied = 0; |
| 1697 | if (!all_opens_permitted) { |
| 1698 | /* Go trough all permitted ports. */ |
| 1699 | for (i = 0; i < num_permitted_opens; i++) |
| 1700 | if (permitted_opens[i].port_to_connect == host_port && |
| 1701 | strcmp(permitted_opens[i].host_to_connect, host) == 0) |
| 1702 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1703 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1704 | /* Check if we found the requested port among those permitted. */ |
| 1705 | if (i >= num_permitted_opens) { |
| 1706 | /* The port is not permitted. */ |
| 1707 | log("Received request to connect to %.100s:%d, but the request was denied.", |
| 1708 | host, host_port); |
| 1709 | denied = 1; |
| 1710 | } |
| 1711 | } |
| 1712 | sock = denied ? -1 : channel_connect_to(host, host_port); |
| 1713 | if (sock > 0) { |
| 1714 | /* Allocate a channel for this connection. */ |
| 1715 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string); |
| 1716 | channels[newch].remote_id = remote_channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1717 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1718 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1719 | packet_put_int(remote_channel); |
| 1720 | packet_put_int(newch); |
| 1721 | packet_send(); |
| 1722 | } else { |
| 1723 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1724 | packet_put_int(remote_channel); |
| 1725 | packet_send(); |
| 1726 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1727 | xfree(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1728 | } |
| 1729 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1730 | /* |
| 1731 | * Creates an internet domain socket for listening for X11 connections. |
| 1732 | * Returns a suitable value for the DISPLAY variable, or NULL if an error |
| 1733 | * occurs. |
| 1734 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1735 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1736 | #define NUM_SOCKS 10 |
| 1737 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1738 | char * |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1739 | x11_create_display_inet(int screen_number, int x11_display_offset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1740 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1741 | int display_number, sock; |
| 1742 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1743 | struct addrinfo hints, *ai, *aitop; |
| 1744 | char strport[NI_MAXSERV]; |
| 1745 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
| 1746 | char display[512]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1747 | char hostname[MAXHOSTNAMELEN]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1748 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1749 | for (display_number = x11_display_offset; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1750 | display_number < MAX_DISPLAYS; |
| 1751 | display_number++) { |
| 1752 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1753 | memset(&hints, 0, sizeof(hints)); |
| 1754 | hints.ai_family = IPv4or6; |
| 1755 | hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */ |
| 1756 | hints.ai_socktype = SOCK_STREAM; |
| 1757 | snprintf(strport, sizeof strport, "%d", port); |
| 1758 | if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) { |
| 1759 | error("getaddrinfo: %.100s", gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1760 | return NULL; |
| 1761 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1762 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1763 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1764 | continue; |
| 1765 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1766 | if (sock < 0) { |
Damien Miller | 89d9796 | 2000-10-14 12:37:19 +1100 | [diff] [blame] | 1767 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 1768 | error("socket: %.100s", strerror(errno)); |
| 1769 | return NULL; |
| 1770 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 1771 | debug("x11_create_display_inet: Socket family %d not supported", |
| 1772 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 1773 | continue; |
| 1774 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1775 | } |
| 1776 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1777 | debug("bind port %d: %.100s", port, strerror(errno)); |
| 1778 | shutdown(sock, SHUT_RDWR); |
| 1779 | close(sock); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 1780 | |
| 1781 | if (ai->ai_next) |
| 1782 | continue; |
| 1783 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1784 | for (n = 0; n < num_socks; n++) { |
| 1785 | shutdown(socks[n], SHUT_RDWR); |
| 1786 | close(socks[n]); |
| 1787 | } |
| 1788 | num_socks = 0; |
| 1789 | break; |
| 1790 | } |
| 1791 | socks[num_socks++] = sock; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1792 | #ifndef DONT_TRY_OTHER_AF |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1793 | if (num_socks == NUM_SOCKS) |
| 1794 | break; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1795 | #else |
| 1796 | break; |
| 1797 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1798 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1799 | if (num_socks > 0) |
| 1800 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1801 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1802 | if (display_number >= MAX_DISPLAYS) { |
| 1803 | error("Failed to allocate internet-domain X11 display socket."); |
| 1804 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1805 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1806 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1807 | for (n = 0; n < num_socks; n++) { |
| 1808 | sock = socks[n]; |
| 1809 | if (listen(sock, 5) < 0) { |
| 1810 | error("listen: %.100s", strerror(errno)); |
| 1811 | shutdown(sock, SHUT_RDWR); |
| 1812 | close(sock); |
| 1813 | return NULL; |
| 1814 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1815 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1816 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1817 | /* Set up a suitable value for the DISPLAY variable. */ |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1818 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1819 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 1820 | fatal("gethostname: %.100s", strerror(errno)); |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1821 | |
| 1822 | #ifdef IPADDR_IN_DISPLAY |
| 1823 | /* |
| 1824 | * HPUX detects the local hostname in the DISPLAY variable and tries |
| 1825 | * to set up a shared memory connection to the server, which it |
| 1826 | * incorrectly supposes to be local. |
| 1827 | * |
| 1828 | * The workaround - as used in later $$H and other programs - is |
| 1829 | * is to set display to the host's IP address. |
| 1830 | */ |
| 1831 | { |
| 1832 | struct hostent *he; |
| 1833 | struct in_addr my_addr; |
| 1834 | |
| 1835 | he = gethostbyname(hostname); |
| 1836 | if (he == NULL) { |
| 1837 | error("[X11-broken-fwd-hostname-workaround] Could not get " |
| 1838 | "IP address for hostname %s.", hostname); |
| 1839 | |
| 1840 | packet_send_debug("[X11-broken-fwd-hostname-workaround]" |
| 1841 | "Could not get IP address for hostname %s.", hostname); |
| 1842 | |
| 1843 | shutdown(sock, SHUT_RDWR); |
| 1844 | close(sock); |
| 1845 | |
| 1846 | return NULL; |
| 1847 | } |
| 1848 | |
| 1849 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 1850 | |
| 1851 | /* Set DISPLAY to <ip address>:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1852 | snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr), |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1853 | display_number, screen_number); |
| 1854 | } |
| 1855 | #else /* IPADDR_IN_DISPLAY */ |
| 1856 | /* Just set DISPLAY to hostname:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1857 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1858 | display_number, screen_number); |
| 1859 | #endif /* IPADDR_IN_DISPLAY */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1860 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1861 | /* Allocate a channel for each socket. */ |
| 1862 | for (n = 0; n < num_socks; n++) { |
| 1863 | sock = socks[n]; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1864 | (void) channel_new("x11 listener", |
| 1865 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 1866 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 1867 | 0, xstrdup("X11 inet listener"), 1); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1868 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1869 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1870 | /* Return a suitable value for the DISPLAY environment variable. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1871 | return xstrdup(display); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | #ifndef X_UNIX_PATH |
| 1875 | #define X_UNIX_PATH "/tmp/.X11-unix/X" |
| 1876 | #endif |
| 1877 | |
| 1878 | static |
| 1879 | int |
Damien Miller | 81b25cf | 1999-12-07 16:47:28 +1100 | [diff] [blame] | 1880 | connect_local_xsocket(unsigned int dnr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1881 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1882 | static const char *const x_sockets[] = { |
| 1883 | X_UNIX_PATH "%u", |
| 1884 | "/var/X/.X11-unix/X" "%u", |
| 1885 | "/usr/spool/sockets/X11/" "%u", |
| 1886 | NULL |
| 1887 | }; |
| 1888 | int sock; |
| 1889 | struct sockaddr_un addr; |
| 1890 | const char *const * path; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1891 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1892 | for (path = x_sockets; *path; ++path) { |
| 1893 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1894 | if (sock < 0) |
| 1895 | error("socket: %.100s", strerror(errno)); |
| 1896 | memset(&addr, 0, sizeof(addr)); |
| 1897 | addr.sun_family = AF_UNIX; |
| 1898 | snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr); |
| 1899 | if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0) |
| 1900 | return sock; |
| 1901 | close(sock); |
| 1902 | } |
| 1903 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 1904 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1905 | } |
| 1906 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1907 | int |
| 1908 | x11_connect_display(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1909 | { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1910 | int display_number, sock = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1911 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1912 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1913 | struct addrinfo hints, *ai, *aitop; |
| 1914 | char strport[NI_MAXSERV]; |
| 1915 | int gaierr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1916 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1917 | /* Try to open a socket for the local X server. */ |
| 1918 | display = getenv("DISPLAY"); |
| 1919 | if (!display) { |
| 1920 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1921 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1922 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1923 | /* |
| 1924 | * Now we decode the value of the DISPLAY variable and make a |
| 1925 | * connection to the real X server. |
| 1926 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1927 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1928 | /* |
| 1929 | * Check if it is a unix domain socket. Unix domain displays are in |
| 1930 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 1931 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1932 | if (strncmp(display, "unix:", 5) == 0 || |
| 1933 | display[0] == ':') { |
| 1934 | /* Connect to the unix domain socket. */ |
| 1935 | if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) { |
| 1936 | error("Could not parse display number from DISPLAY: %.100s", |
| 1937 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1938 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1939 | } |
| 1940 | /* Create a socket. */ |
| 1941 | sock = connect_local_xsocket(display_number); |
| 1942 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1943 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1944 | |
| 1945 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1946 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1947 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1948 | /* |
| 1949 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 1950 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 1951 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1952 | strncpy(buf, display, sizeof(buf)); |
| 1953 | buf[sizeof(buf) - 1] = 0; |
| 1954 | cp = strchr(buf, ':'); |
| 1955 | if (!cp) { |
| 1956 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1957 | return -1; |
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 | *cp = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1960 | /* 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] | 1961 | if (sscanf(cp + 1, "%d", &display_number) != 1) { |
| 1962 | error("Could not parse display number from DISPLAY: %.100s", |
| 1963 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1964 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1965 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1966 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1967 | /* Look up the host address */ |
| 1968 | memset(&hints, 0, sizeof(hints)); |
| 1969 | hints.ai_family = IPv4or6; |
| 1970 | hints.ai_socktype = SOCK_STREAM; |
| 1971 | snprintf(strport, sizeof strport, "%d", 6000 + display_number); |
| 1972 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
| 1973 | error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1974 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1975 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1976 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1977 | /* Create a socket. */ |
| 1978 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1979 | if (sock < 0) { |
| 1980 | debug("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1981 | continue; |
| 1982 | } |
| 1983 | /* Connect it to the display. */ |
| 1984 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1985 | debug("connect %.100s port %d: %.100s", buf, |
| 1986 | 6000 + display_number, strerror(errno)); |
| 1987 | close(sock); |
| 1988 | continue; |
| 1989 | } |
| 1990 | /* Success */ |
| 1991 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1992 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1993 | freeaddrinfo(aitop); |
| 1994 | if (!ai) { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1995 | error("connect %.100s port %d: %.100s", buf, 6000 + display_number, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1996 | strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1997 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1998 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1999 | return sock; |
| 2000 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2001 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2002 | /* |
| 2003 | * This is called when SSH_SMSG_X11_OPEN is received. The packet contains |
| 2004 | * the remote channel number. We should do whatever we want, and respond |
| 2005 | * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. |
| 2006 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2007 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2008 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2009 | x11_input_open(int type, int plen, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2010 | { |
| 2011 | int remote_channel, sock = 0, newch; |
| 2012 | char *remote_host; |
| 2013 | unsigned int remote_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2014 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2015 | /* Get remote channel number. */ |
| 2016 | remote_channel = packet_get_int(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2017 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2018 | /* Get remote originator name. */ |
| 2019 | if (have_hostname_in_open) { |
| 2020 | remote_host = packet_get_string(&remote_len); |
| 2021 | remote_len += 4; |
| 2022 | } else { |
| 2023 | remote_host = xstrdup("unknown (remote did not supply name)"); |
| 2024 | remote_len = 0; |
| 2025 | } |
| 2026 | |
| 2027 | debug("Received X11 open request."); |
| 2028 | packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN); |
| 2029 | |
| 2030 | /* Obtain a connection to the real X display. */ |
| 2031 | sock = x11_connect_display(); |
| 2032 | if (sock == -1) { |
| 2033 | /* Send refusal to the remote host. */ |
| 2034 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2035 | packet_put_int(remote_channel); |
| 2036 | packet_send(); |
| 2037 | } else { |
| 2038 | /* Allocate a channel for this connection. */ |
| 2039 | newch = channel_allocate( |
| 2040 | (x11_saved_proto == NULL) ? |
| 2041 | SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN, |
| 2042 | sock, remote_host); |
| 2043 | channels[newch].remote_id = remote_channel; |
| 2044 | |
| 2045 | /* Send a confirmation to the remote host. */ |
| 2046 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 2047 | packet_put_int(remote_channel); |
| 2048 | packet_put_int(newch); |
| 2049 | packet_send(); |
| 2050 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2051 | } |
| 2052 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2053 | /* dummy protocol handler that denies SSH-1 requests (agent/x11) */ |
| 2054 | void |
| 2055 | deny_input_open(int type, int plen, void *ctxt) |
| 2056 | { |
| 2057 | int rchan = packet_get_int(); |
| 2058 | switch(type){ |
| 2059 | case SSH_SMSG_AGENT_OPEN: |
| 2060 | error("Warning: ssh server tried agent forwarding."); |
| 2061 | break; |
| 2062 | case SSH_SMSG_X11_OPEN: |
| 2063 | error("Warning: ssh server tried X11 forwarding."); |
| 2064 | break; |
| 2065 | default: |
| 2066 | error("deny_input_open: type %d plen %d", type, plen); |
| 2067 | break; |
| 2068 | } |
| 2069 | error("Warning: this is probably a break in attempt by a malicious server."); |
| 2070 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2071 | packet_put_int(rchan); |
| 2072 | packet_send(); |
| 2073 | } |
| 2074 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2075 | /* |
| 2076 | * Requests forwarding of X11 connections, generates fake authentication |
| 2077 | * data, and enables authentication spoofing. |
| 2078 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2079 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2080 | void |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2081 | x11_request_forwarding_with_spoofing(int client_session_id, |
| 2082 | const char *proto, const char *data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2083 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2084 | unsigned int data_len = (unsigned int) strlen(data) / 2; |
| 2085 | unsigned int i, value; |
| 2086 | char *new_data; |
| 2087 | int screen_number; |
| 2088 | const char *cp; |
| 2089 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2090 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2091 | cp = getenv("DISPLAY"); |
| 2092 | if (cp) |
| 2093 | cp = strchr(cp, ':'); |
| 2094 | if (cp) |
| 2095 | cp = strchr(cp, '.'); |
| 2096 | if (cp) |
| 2097 | screen_number = atoi(cp + 1); |
| 2098 | else |
| 2099 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2100 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2101 | /* Save protocol name. */ |
| 2102 | x11_saved_proto = xstrdup(proto); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2103 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2104 | /* |
| 2105 | * Extract real authentication data and generate fake data of the |
| 2106 | * same length. |
| 2107 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2108 | x11_saved_data = xmalloc(data_len); |
| 2109 | x11_fake_data = xmalloc(data_len); |
| 2110 | for (i = 0; i < data_len; i++) { |
| 2111 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 2112 | fatal("x11_request_forwarding: bad authentication data: %.100s", data); |
| 2113 | if (i % 4 == 0) |
| 2114 | rand = arc4random(); |
| 2115 | x11_saved_data[i] = value; |
| 2116 | x11_fake_data[i] = rand & 0xff; |
| 2117 | rand >>= 8; |
| 2118 | } |
| 2119 | x11_saved_data_len = data_len; |
| 2120 | x11_fake_data_len = data_len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2121 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2122 | /* Convert the fake data into hex. */ |
| 2123 | new_data = xmalloc(2 * data_len + 1); |
| 2124 | for (i = 0; i < data_len; i++) |
| 2125 | sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2126 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2127 | /* Send the request packet. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2128 | if (compat20) { |
| 2129 | channel_request_start(client_session_id, "x11-req", 0); |
| 2130 | packet_put_char(0); /* XXX bool single connection */ |
| 2131 | } else { |
| 2132 | packet_start(SSH_CMSG_X11_REQUEST_FORWARDING); |
| 2133 | } |
| 2134 | packet_put_cstring(proto); |
| 2135 | packet_put_cstring(new_data); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2136 | packet_put_int(screen_number); |
| 2137 | packet_send(); |
| 2138 | packet_write_wait(); |
| 2139 | xfree(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /* Sends a message to the server to request authentication fd forwarding. */ |
| 2143 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2144 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2145 | auth_request_forwarding() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2146 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2147 | packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING); |
| 2148 | packet_send(); |
| 2149 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2150 | } |
| 2151 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2152 | /* |
| 2153 | * Returns the name of the forwarded authentication socket. Returns NULL if |
| 2154 | * there is no forwarded authentication socket. The returned value points to |
| 2155 | * a static buffer. |
| 2156 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2157 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2158 | char * |
| 2159 | auth_get_socket_name() |
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 | return channel_forwarded_auth_socket_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | /* removes the agent forwarding socket */ |
| 2165 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2166 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2167 | cleanup_socket(void) |
| 2168 | { |
Damien Miller | 78315eb | 2000-09-29 23:01:36 +1100 | [diff] [blame] | 2169 | unlink(channel_forwarded_auth_socket_name); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2170 | rmdir(channel_forwarded_auth_socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2171 | } |
| 2172 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2173 | /* |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2174 | * 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] | 2175 | * This starts forwarding authentication requests. |
| 2176 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2177 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2178 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2179 | auth_input_request_forwarding(struct passwd * pw) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2180 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2181 | int sock, newch; |
| 2182 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2183 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2184 | if (auth_get_socket_name() != NULL) |
| 2185 | fatal("Protocol error: authentication forwarding requested twice."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2186 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2187 | /* Temporarily drop privileged uid for mkdir/bind. */ |
| 2188 | temporarily_use_uid(pw->pw_uid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2189 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2190 | /* Allocate a buffer for the socket name, and format the name. */ |
| 2191 | channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME); |
| 2192 | channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME); |
| 2193 | strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2194 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2195 | /* Create private directory for socket */ |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2196 | if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) { |
| 2197 | packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s", |
| 2198 | strerror(errno)); |
| 2199 | restore_uid(); |
| 2200 | xfree(channel_forwarded_auth_socket_name); |
| 2201 | xfree(channel_forwarded_auth_socket_dir); |
| 2202 | channel_forwarded_auth_socket_name = NULL; |
| 2203 | channel_forwarded_auth_socket_dir = NULL; |
| 2204 | return 0; |
| 2205 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2206 | snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d", |
| 2207 | channel_forwarded_auth_socket_dir, (int) getpid()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2208 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2209 | if (atexit(cleanup_socket) < 0) { |
| 2210 | int saved = errno; |
| 2211 | cleanup_socket(); |
| 2212 | packet_disconnect("socket: %.100s", strerror(saved)); |
| 2213 | } |
| 2214 | /* Create the socket. */ |
| 2215 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 2216 | if (sock < 0) |
| 2217 | packet_disconnect("socket: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2218 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2219 | /* Bind it to the name. */ |
| 2220 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 2221 | sunaddr.sun_family = AF_UNIX; |
| 2222 | strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name, |
| 2223 | sizeof(sunaddr.sun_path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2224 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2225 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) |
| 2226 | packet_disconnect("bind: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2227 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2228 | /* Restore the privileged uid. */ |
| 2229 | restore_uid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2230 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2231 | /* Start listening on the socket. */ |
| 2232 | if (listen(sock, 5) < 0) |
| 2233 | packet_disconnect("listen: %.100s", strerror(errno)); |
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 | /* Allocate a channel for the authentication agent socket. */ |
| 2236 | newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock, |
| 2237 | xstrdup("auth socket")); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 2238 | strlcpy(channels[newch].path, channel_forwarded_auth_socket_name, |
| 2239 | sizeof(channels[newch].path)); |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 2240 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* This is called to process an SSH_SMSG_AGENT_OPEN message. */ |
| 2244 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2245 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2246 | auth_input_open_request(int type, int plen, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2247 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2248 | int remch, sock, newch; |
| 2249 | char *dummyname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2250 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2251 | packet_integrity_check(plen, 4, type); |
| 2252 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2253 | /* Read the remote channel number from the message. */ |
| 2254 | remch = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2255 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2256 | /* |
| 2257 | * Get a connection to the local authentication agent (this may again |
| 2258 | * get forwarded). |
| 2259 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2260 | sock = ssh_get_authentication_socket(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2261 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2262 | /* |
| 2263 | * If we could not connect the agent, send an error message back to |
| 2264 | * the server. This should never happen unless the agent dies, |
| 2265 | * because authentication forwarding is only enabled if we have an |
| 2266 | * agent. |
| 2267 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2268 | if (sock < 0) { |
| 2269 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2270 | packet_put_int(remch); |
| 2271 | packet_send(); |
| 2272 | return; |
| 2273 | } |
| 2274 | debug("Forwarding authentication connection."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2275 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2276 | /* |
| 2277 | * Dummy host name. This will be freed when the channel is freed; it |
| 2278 | * will still be valid in the packet_put_string below since the |
| 2279 | * channel cannot yet be freed at that point. |
| 2280 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2281 | dummyname = xstrdup("authentication agent connection"); |
| 2282 | |
| 2283 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname); |
| 2284 | channels[newch].remote_id = remch; |
| 2285 | |
| 2286 | /* Send a confirmation to the remote host. */ |
| 2287 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 2288 | packet_put_int(remch); |
| 2289 | packet_put_int(newch); |
| 2290 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2291 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2292 | |
| 2293 | void |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2294 | channel_start_open(int id) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2295 | { |
| 2296 | Channel *c = channel_lookup(id); |
| 2297 | if (c == NULL) { |
| 2298 | log("channel_open: %d: bad id", id); |
| 2299 | return; |
| 2300 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2301 | debug("send channel open %d", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2302 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 2303 | packet_put_cstring(c->ctype); |
| 2304 | packet_put_int(c->self); |
| 2305 | packet_put_int(c->local_window); |
| 2306 | packet_put_int(c->local_maxpacket); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2307 | } |
| 2308 | void |
| 2309 | channel_open(int id) |
| 2310 | { |
| 2311 | /* XXX REMOVE ME */ |
| 2312 | channel_start_open(id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2313 | packet_send(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2314 | } |
| 2315 | void |
| 2316 | channel_request(int id, char *service, int wantconfirm) |
| 2317 | { |
| 2318 | channel_request_start(id, service, wantconfirm); |
| 2319 | packet_send(); |
| 2320 | debug("channel request %d: %s", id, service) ; |
| 2321 | } |
| 2322 | void |
| 2323 | channel_request_start(int id, char *service, int wantconfirm) |
| 2324 | { |
| 2325 | Channel *c = channel_lookup(id); |
| 2326 | if (c == NULL) { |
| 2327 | log("channel_request: %d: bad id", id); |
| 2328 | return; |
| 2329 | } |
| 2330 | packet_start(SSH2_MSG_CHANNEL_REQUEST); |
| 2331 | packet_put_int(c->remote_id); |
| 2332 | packet_put_cstring(service); |
| 2333 | packet_put_char(wantconfirm); |
| 2334 | } |
| 2335 | void |
| 2336 | channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg) |
| 2337 | { |
| 2338 | Channel *c = channel_lookup(id); |
| 2339 | if (c == NULL) { |
| 2340 | log("channel_register_callback: %d: bad id", id); |
| 2341 | return; |
| 2342 | } |
| 2343 | c->cb_event = mtype; |
| 2344 | c->cb_fn = fn; |
| 2345 | c->cb_arg = arg; |
| 2346 | } |
| 2347 | void |
| 2348 | channel_register_cleanup(int id, channel_callback_fn *fn) |
| 2349 | { |
| 2350 | Channel *c = channel_lookup(id); |
| 2351 | if (c == NULL) { |
| 2352 | log("channel_register_cleanup: %d: bad id", id); |
| 2353 | return; |
| 2354 | } |
| 2355 | c->dettach_user = fn; |
| 2356 | } |
| 2357 | void |
| 2358 | channel_cancel_cleanup(int id) |
| 2359 | { |
| 2360 | Channel *c = channel_lookup(id); |
| 2361 | if (c == NULL) { |
| 2362 | log("channel_cancel_cleanup: %d: bad id", id); |
| 2363 | return; |
| 2364 | } |
| 2365 | c->dettach_user = NULL; |
| 2366 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 2367 | void |
| 2368 | channel_register_filter(int id, channel_filter_fn *fn) |
| 2369 | { |
| 2370 | Channel *c = channel_lookup(id); |
| 2371 | if (c == NULL) { |
| 2372 | log("channel_register_filter: %d: bad id", id); |
| 2373 | return; |
| 2374 | } |
| 2375 | c->input_filter = fn; |
| 2376 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2377 | |
| 2378 | void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2379 | channel_set_fds(int id, int rfd, int wfd, int efd, |
| 2380 | int extusage, int nonblock) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2381 | { |
| 2382 | Channel *c = channel_lookup(id); |
| 2383 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 2384 | fatal("channel_activate for non-larval channel %d.", id); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 2385 | channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2386 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2387 | /* XXX window size? */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2388 | c->local_window = c->local_window_max = c->local_maxpacket * 2; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2389 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 2390 | packet_put_int(c->remote_id); |
| 2391 | packet_put_int(c->local_window); |
| 2392 | packet_send(); |
| 2393 | } |