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