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