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