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