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