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