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