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