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