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