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