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