blob: 957b4a428aae3c4dc1319bf5ce8c0546dadc3151 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * channels.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Fri Mar 24 16:35:24 1995 ylo
11 *
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.
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "includes.h"
Damien Miller78928792000-04-12 20:17:38 +100020RCSID("$Id: channels.c,v 1.24 2000/04/12 10:17:38 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "ssh.h"
23#include "packet.h"
24#include "xmalloc.h"
25#include "buffer.h"
26#include "authfd.h"
27#include "uidswap.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110028#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "servconf.h"
30
31#include "channels.h"
32#include "nchan.h"
33#include "compat.h"
34
Damien Miller33b13562000-04-04 14:38:59 +100035#include "ssh2.h"
36
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037/* 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
Damien Millerb38eff82000-04-01 11:09:21 +100043/* default buffer for tcp-fwd-channel */
44#define CHAN_WINDOW_DEFAULT (8*1024)
45#define CHAN_PACKET_DEFAULT (CHAN_WINDOW_DEFAULT/2)
46
Damien Miller5428f641999-11-25 11:54:57 +110047/*
48 * Pointer to an array containing all allocated channels. The array is
49 * dynamically extended as needed.
50 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051static Channel *channels = NULL;
52
Damien Miller5428f641999-11-25 11:54:57 +110053/*
54 * Size of the channel array. All slots of the array must always be
55 * initialized (at least the type field); unused slots are marked with type
56 * SSH_CHANNEL_FREE.
57 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058static int channels_alloc = 0;
59
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Maximum file descriptor value used in any of the channels. This is
62 * updated in channel_allocate.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064static int channel_max_fd_value = 0;
65
66/* Name and directory of socket for authentication agent forwarding. */
67static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110068static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
70/* Saved X11 authentication protocol name. */
71char *x11_saved_proto = NULL;
72
73/* Saved X11 authentication data. This is the real data. */
74char *x11_saved_data = NULL;
75unsigned int x11_saved_data_len = 0;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Fake X11 authentication data. This is what the server will be sending us;
79 * we should replace any occurrences of this by the real data.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081char *x11_fake_data = NULL;
82unsigned int x11_fake_data_len;
83
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Data structure for storing which hosts are permitted for forward requests.
86 * The local sides of any remote forwards are stored in this array to prevent
87 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
88 * network (which might be behind a firewall).
89 */
Damien Miller95def091999-11-25 00:26:21 +110090typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100091 char *host_to_connect; /* Connect to 'host'. */
92 u_short port_to_connect; /* Connect to 'port'. */
93 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094} ForwardPermission;
95
96/* List of all permitted host/port pairs to connect. */
97static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
98/* Number of permitted host/port pairs in the array. */
99static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * If this is true, all opens are permitted. This is the case on the server
102 * on which we have to trust the client anyway, and the user could do
103 * anything after logging in anyway.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105static int all_opens_permitted = 0;
106
107/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
108static int have_hostname_in_open = 0;
109
110/* Sets specific protocol options. */
111
Damien Miller95def091999-11-25 00:26:21 +1100112void
113channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114{
Damien Miller95def091999-11-25 00:26:21 +1100115 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116}
117
Damien Miller5428f641999-11-25 11:54:57 +1100118/*
119 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
120 * called by the server, because the user could connect to any port anyway,
121 * and the server has no way to know but to trust the client anyway.
122 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124void
125channel_permit_all_opens()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller95def091999-11-25 00:26:21 +1100127 all_opens_permitted = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128}
129
Damien Millerb38eff82000-04-01 11:09:21 +1000130/* lookup channel by id */
131
132Channel *
133channel_lookup(int id)
134{
135 Channel *c;
136 if (id < 0 && id > channels_alloc) {
137 log("channel_lookup: %d: bad id", id);
138 return NULL;
139 }
140 c = &channels[id];
141 if (c->type == SSH_CHANNEL_FREE) {
142 log("channel_lookup: %d: bad id: channel free", id);
143 return NULL;
144 }
145 return c;
146}
147
Damien Miller5428f641999-11-25 11:54:57 +1100148/*
149 * Allocate a new channel object and set its type and socket. This will cause
150 * remote_name to be freed.
151 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller95def091999-11-25 00:26:21 +1100153int
Damien Millerb38eff82000-04-01 11:09:21 +1000154channel_new(char *ctype, int type, int rfd, int wfd, int efd,
155 int window, int maxpack, int extended_usage, char *remote_name)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156{
Damien Miller95def091999-11-25 00:26:21 +1100157 int i, found;
158 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller95def091999-11-25 00:26:21 +1100160 /* Update the maximum file descriptor value. */
Damien Millerb38eff82000-04-01 11:09:21 +1000161 if (rfd > channel_max_fd_value)
162 channel_max_fd_value = rfd;
163 if (wfd > channel_max_fd_value)
164 channel_max_fd_value = wfd;
165 if (efd > channel_max_fd_value)
166 channel_max_fd_value = efd;
Damien Miller5428f641999-11-25 11:54:57 +1100167 /* XXX set close-on-exec -markus */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169 /* Do initial allocation if this is the first call. */
170 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000171 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100172 channels_alloc = 10;
173 channels = xmalloc(channels_alloc * sizeof(Channel));
174 for (i = 0; i < channels_alloc; i++)
175 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100176 /*
177 * Kludge: arrange a call to channel_stop_listening if we
178 * terminate with fatal().
179 */
Damien Miller95def091999-11-25 00:26:21 +1100180 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
181 }
182 /* Try to find a free slot where to put the new channel. */
183 for (found = -1, i = 0; i < channels_alloc; i++)
184 if (channels[i].type == SSH_CHANNEL_FREE) {
185 /* Found a free slot. */
186 found = i;
187 break;
188 }
189 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100190 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100191 found = channels_alloc;
192 channels_alloc += 10;
193 debug("channel: expanding %d", channels_alloc);
194 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
195 for (i = found; i < channels_alloc; i++)
196 channels[i].type = SSH_CHANNEL_FREE;
197 }
198 /* Initialize and return new channel number. */
199 c = &channels[found];
200 buffer_init(&c->input);
201 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000202 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100203 chan_init_iostates(c);
204 c->self = found;
205 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000206 c->ctype = ctype;
207 c->local_window = window;
208 c->local_window_max = window;
209 c->local_consumed = 0;
210 c->local_maxpacket = maxpack;
211 c->remote_window = 0;
212 c->remote_maxpacket = 0;
213 c->rfd = rfd;
214 c->wfd = wfd;
215 c->sock = (rfd == wfd) ? rfd : -1;
216 c->efd = efd;
217 c->extended_usage = extended_usage;
Damien Miller95def091999-11-25 00:26:21 +1100218 c->remote_id = -1;
219 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000220 c->remote_window = 0;
221 c->remote_maxpacket = 0;
222 c->cb_fn = NULL;
223 c->cb_arg = NULL;
224 c->cb_event = 0;
225 c->dettach_user = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100226 debug("channel %d: new [%s]", found, remote_name);
227 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
Damien Millerb38eff82000-04-01 11:09:21 +1000229int
230channel_allocate(int type, int sock, char *remote_name)
231{
232 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
233}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
235/* Free the channel and close its socket. */
236
Damien Miller95def091999-11-25 00:26:21 +1100237void
Damien Millerb38eff82000-04-01 11:09:21 +1000238channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Millerb38eff82000-04-01 11:09:21 +1000240 Channel *c = channel_lookup(id);
241 if (c == NULL)
242 packet_disconnect("channel free: bad local channel %d", id);
243 debug("channel_free: channel %d: status: %s", id, channel_open_message());
Damien Miller33b13562000-04-04 14:38:59 +1000244 if (c->dettach_user != NULL) {
245 debug("channel_free: channel %d: dettaching channel user", id);
246 c->dettach_user(c->self, NULL);
247 }
Damien Millerb38eff82000-04-01 11:09:21 +1000248 if (c->sock != -1) {
249 shutdown(c->sock, SHUT_RDWR);
250 close(c->sock);
Damien Miller33b13562000-04-04 14:38:59 +1000251 c->sock = -1;
252 }
253 if (compat20) {
254 if (c->rfd != -1) {
255 close(c->rfd);
256 c->rfd = -1;
257 }
258 if (c->wfd != -1) {
259 close(c->wfd);
260 c->wfd = -1;
261 }
262 if (c->efd != -1) {
263 close(c->efd);
264 c->efd = -1;
265 }
Damien Millerb38eff82000-04-01 11:09:21 +1000266 }
267 buffer_free(&c->input);
268 buffer_free(&c->output);
269 buffer_free(&c->extended);
270 c->type = SSH_CHANNEL_FREE;
271 if (c->remote_name) {
272 xfree(c->remote_name);
273 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275}
276
Damien Miller5428f641999-11-25 11:54:57 +1100277/*
Damien Millerb38eff82000-04-01 11:09:21 +1000278 * 'channel_pre*' are called just before select() to add any bits relevant to
279 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100280 */
Damien Millerb38eff82000-04-01 11:09:21 +1000281/*
282 * 'channel_post*': perform any appropriate operations for channels which
283 * have events pending.
284 */
285typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
286chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
287chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
288
289void
290channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
291{
292 FD_SET(c->sock, readset);
293}
294
295void
296channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
297{
298 if (buffer_len(&c->input) < packet_get_maxsize())
299 FD_SET(c->sock, readset);
300 if (buffer_len(&c->output) > 0)
301 FD_SET(c->sock, writeset);
302}
303
304void
305channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
306{
307 /* test whether sockets are 'alive' for read/write */
308 if (c->istate == CHAN_INPUT_OPEN)
309 if (buffer_len(&c->input) < packet_get_maxsize())
310 FD_SET(c->sock, readset);
311 if (c->ostate == CHAN_OUTPUT_OPEN ||
312 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
313 if (buffer_len(&c->output) > 0) {
314 FD_SET(c->sock, writeset);
315 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
316 chan_obuf_empty(c);
317 }
318 }
319}
320
321void
Damien Miller33b13562000-04-04 14:38:59 +1000322channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
323{
324 if (c->istate == CHAN_INPUT_OPEN &&
325 c->remote_window > 0 &&
326 buffer_len(&c->input) < c->remote_window)
327 FD_SET(c->rfd, readset);
328 if (c->ostate == CHAN_OUTPUT_OPEN ||
329 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
330 if (buffer_len(&c->output) > 0) {
331 FD_SET(c->wfd, writeset);
332 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
333 chan_obuf_empty(c);
334 }
335 }
336 /** XXX check close conditions, too */
337 if (c->efd != -1) {
338 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
339 buffer_len(&c->extended) > 0)
340 FD_SET(c->efd, writeset);
341 else if (c->extended_usage == CHAN_EXTENDED_READ &&
342 buffer_len(&c->extended) < c->remote_window)
343 FD_SET(c->efd, readset);
344 }
345}
346
347void
Damien Millerb38eff82000-04-01 11:09:21 +1000348channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
349{
350 if (buffer_len(&c->input) == 0) {
351 packet_start(SSH_MSG_CHANNEL_CLOSE);
352 packet_put_int(c->remote_id);
353 packet_send();
354 c->type = SSH_CHANNEL_CLOSED;
355 debug("Closing channel %d after input drain.", c->self);
356 }
357}
358
359void
360channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
361{
362 if (buffer_len(&c->output) == 0)
363 channel_free(c->self);
364 else
365 FD_SET(c->sock, writeset);
366}
367
368/*
369 * This is a special state for X11 authentication spoofing. An opened X11
370 * connection (when authentication spoofing is being done) remains in this
371 * state until the first packet has been completely read. The authentication
372 * data in that packet is then substituted by the real data if it matches the
373 * fake data, and the channel is put into normal mode.
374 */
375int
376x11_open_helper(Channel *c)
377{
378 unsigned char *ucp;
379 unsigned int proto_len, data_len;
380
381 /* Check if the fixed size part of the packet is in buffer. */
382 if (buffer_len(&c->output) < 12)
383 return 0;
384
385 /* Parse the lengths of variable-length fields. */
386 ucp = (unsigned char *) buffer_ptr(&c->output);
387 if (ucp[0] == 0x42) { /* Byte order MSB first. */
388 proto_len = 256 * ucp[6] + ucp[7];
389 data_len = 256 * ucp[8] + ucp[9];
390 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
391 proto_len = ucp[6] + 256 * ucp[7];
392 data_len = ucp[8] + 256 * ucp[9];
393 } else {
394 debug("Initial X11 packet contains bad byte order byte: 0x%x",
395 ucp[0]);
396 return -1;
397 }
398
399 /* Check if the whole packet is in buffer. */
400 if (buffer_len(&c->output) <
401 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
402 return 0;
403
404 /* Check if authentication protocol matches. */
405 if (proto_len != strlen(x11_saved_proto) ||
406 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
407 debug("X11 connection uses different authentication protocol.");
408 return -1;
409 }
410 /* Check if authentication data matches our fake data. */
411 if (data_len != x11_fake_data_len ||
412 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
413 x11_fake_data, x11_fake_data_len) != 0) {
414 debug("X11 auth data does not match fake data.");
415 return -1;
416 }
417 /* Check fake data length */
418 if (x11_fake_data_len != x11_saved_data_len) {
419 error("X11 fake_data_len %d != saved_data_len %d",
420 x11_fake_data_len, x11_saved_data_len);
421 return -1;
422 }
423 /*
424 * Received authentication protocol and data match
425 * our fake data. Substitute the fake data with real
426 * data.
427 */
428 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
429 x11_saved_data, x11_saved_data_len);
430 return 1;
431}
432
433void
434channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
435{
436 int ret = x11_open_helper(c);
437 if (ret == 1) {
438 /* Start normal processing for the channel. */
439 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000440 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000441 } else if (ret == -1) {
442 /*
443 * We have received an X11 connection that has bad
444 * authentication information.
445 */
446 log("X11 connection rejected because of wrong authentication.\r\n");
447 buffer_clear(&c->input);
448 buffer_clear(&c->output);
449 close(c->sock);
450 c->sock = -1;
451 c->type = SSH_CHANNEL_CLOSED;
452 packet_start(SSH_MSG_CHANNEL_CLOSE);
453 packet_put_int(c->remote_id);
454 packet_send();
455 }
456}
457
458void
459channel_pre_x11_open_15(Channel *c, fd_set * readset, fd_set * writeset)
460{
461 int ret = x11_open_helper(c);
462 if (ret == 1) {
463 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000464 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000465 } else if (ret == -1) {
466 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
467 chan_read_failed(c);
468 chan_write_failed(c);
469 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
470 }
471}
472
473/* This is our fake X11 server socket. */
474void
475channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
476{
477 struct sockaddr addr;
478 int newsock, newch;
479 socklen_t addrlen;
480 char buf[16384], *remote_hostname;
481
482 if (FD_ISSET(c->sock, readset)) {
483 debug("X11 connection requested.");
484 addrlen = sizeof(addr);
485 newsock = accept(c->sock, &addr, &addrlen);
486 if (newsock < 0) {
487 error("accept: %.100s", strerror(errno));
488 return;
489 }
490 remote_hostname = get_remote_hostname(newsock);
491 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
492 remote_hostname, get_peer_port(newsock));
493 xfree(remote_hostname);
494 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
495 xstrdup(buf));
496 packet_start(SSH_SMSG_X11_OPEN);
497 packet_put_int(newch);
498 if (have_hostname_in_open)
499 packet_put_string(buf, strlen(buf));
500 packet_send();
501 }
502}
503
504/*
505 * This socket is listening for connections to a forwarded TCP/IP port.
506 */
507void
508channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
509{
510 struct sockaddr addr;
511 int newsock, newch;
512 socklen_t addrlen;
513 char buf[1024], *remote_hostname;
514 int remote_port;
515
516 if (FD_ISSET(c->sock, readset)) {
517 debug("Connection to port %d forwarding "
518 "to %.100s port %d requested.",
519 c->listening_port, c->path, c->host_port);
520 addrlen = sizeof(addr);
521 newsock = accept(c->sock, &addr, &addrlen);
522 if (newsock < 0) {
523 error("accept: %.100s", strerror(errno));
524 return;
525 }
526 remote_hostname = get_remote_hostname(newsock);
527 remote_port = get_peer_port(newsock);
528 snprintf(buf, sizeof buf,
529 "listen port %d for %.100s port %d, "
530 "connect from %.200s port %d",
531 c->listening_port, c->path, c->host_port,
532 remote_hostname, remote_port);
533 newch = channel_new("direct-tcpip",
534 SSH_CHANNEL_OPENING, newsock, newsock, -1,
535 c->local_window_max, c->local_maxpacket,
536 0, xstrdup(buf));
Damien Miller33b13562000-04-04 14:38:59 +1000537 if (compat20) {
538 packet_start(SSH2_MSG_CHANNEL_OPEN);
539 packet_put_cstring("direct-tcpip");
540 packet_put_int(newch);
541 packet_put_int(c->local_window_max);
542 packet_put_int(c->local_maxpacket);
543 packet_put_string(c->path, strlen(c->path));
544 packet_put_int(c->host_port);
545 packet_put_cstring(remote_hostname);
546 packet_put_int(remote_port);
547 packet_send();
548 } else {
549 packet_start(SSH_MSG_PORT_OPEN);
550 packet_put_int(newch);
551 packet_put_string(c->path, strlen(c->path));
552 packet_put_int(c->host_port);
553 if (have_hostname_in_open) {
554 packet_put_string(buf, strlen(buf));
555 }
556 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000557 }
Damien Millerb38eff82000-04-01 11:09:21 +1000558 xfree(remote_hostname);
559 }
560}
561
562/*
563 * This is the authentication agent socket listening for connections from
564 * clients.
565 */
566void
567channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
568{
569 struct sockaddr addr;
570 int newsock, newch;
571 socklen_t addrlen;
572
573 if (FD_ISSET(c->sock, readset)) {
574 addrlen = sizeof(addr);
575 newsock = accept(c->sock, &addr, &addrlen);
576 if (newsock < 0) {
577 error("accept from auth socket: %.100s", strerror(errno));
578 return;
579 }
580 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
581 xstrdup("accepted auth socket"));
582 packet_start(SSH_SMSG_AGENT_OPEN);
583 packet_put_int(newch);
584 packet_send();
585 }
586}
587
588int
589channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
590{
591 char buf[16*1024];
592 int len;
593
594 if (c->rfd != -1 &&
595 FD_ISSET(c->rfd, readset)) {
596 len = read(c->rfd, buf, sizeof(buf));
597 if (len <= 0) {
598 debug("channel %d: read<0 rfd %d len %d",
599 c->self, c->rfd, len);
600 if (compat13) {
601 buffer_consume(&c->output, buffer_len(&c->output));
602 c->type = SSH_CHANNEL_INPUT_DRAINING;
603 debug("Channel %d status set to input draining.", c->self);
604 } else {
605 chan_read_failed(c);
606 }
607 return -1;
608 }
609 buffer_append(&c->input, buf, len);
610 }
611 return 1;
612}
613int
614channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
615{
616 int len;
617
618 /* Send buffered output data to the socket. */
619 if (c->wfd != -1 &&
620 FD_ISSET(c->wfd, writeset) &&
621 buffer_len(&c->output) > 0) {
622 len = write(c->wfd, buffer_ptr(&c->output),
623 buffer_len(&c->output));
624 if (len <= 0) {
625 if (compat13) {
626 buffer_consume(&c->output, buffer_len(&c->output));
627 debug("Channel %d status set to input draining.", c->self);
628 c->type = SSH_CHANNEL_INPUT_DRAINING;
629 } else {
630 chan_write_failed(c);
631 }
632 return -1;
633 }
634 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000635 if (compat20 && len > 0) {
636 c->local_consumed += len;
637 }
638 }
639 return 1;
640}
641int
642channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
643{
644 char buf[16*1024];
645 int len;
646
Damien Miller1383bd82000-04-06 12:32:37 +1000647/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000648 if (c->efd != -1) {
649 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
650 FD_ISSET(c->efd, writeset) &&
651 buffer_len(&c->extended) > 0) {
652 len = write(c->efd, buffer_ptr(&c->extended),
653 buffer_len(&c->extended));
654 debug("channel %d: written %d to efd %d",
655 c->self, len, c->efd);
656 if (len > 0) {
657 buffer_consume(&c->extended, len);
658 c->local_consumed += len;
659 }
660 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
661 FD_ISSET(c->efd, readset)) {
662 len = read(c->efd, buf, sizeof(buf));
663 debug("channel %d: read %d from efd %d",
664 c->self, len, c->efd);
Damien Miller1383bd82000-04-06 12:32:37 +1000665 if (len == 0) {
666 debug("channel %d: closing efd %d",
667 c->self, c->efd);
668 close(c->efd);
669 c->efd = -1;
670 } else if (len > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000671 buffer_append(&c->extended, buf, len);
672 }
673 }
674 return 1;
675}
676int
677channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
678{
Damien Millerefb4afe2000-04-12 18:45:05 +1000679 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000680 c->local_window < c->local_window_max/2 &&
681 c->local_consumed > 0) {
682 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
683 packet_put_int(c->remote_id);
684 packet_put_int(c->local_consumed);
685 packet_send();
686 debug("channel %d: window %d sent adjust %d",
687 c->self, c->local_window,
688 c->local_consumed);
689 c->local_window += c->local_consumed;
690 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000691 }
692 return 1;
693}
694
695void
696channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
697{
698 channel_handle_rfd(c, readset, writeset);
699 channel_handle_wfd(c, readset, writeset);
700}
701
702void
Damien Miller33b13562000-04-04 14:38:59 +1000703channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
704{
705 channel_handle_rfd(c, readset, writeset);
706 channel_handle_wfd(c, readset, writeset);
707 channel_handle_efd(c, readset, writeset);
708 channel_check_window(c, readset, writeset);
709}
710
711void
Damien Millerb38eff82000-04-01 11:09:21 +1000712channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
713{
714 int len;
715 /* Send buffered output data to the socket. */
716 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
717 len = write(c->sock, buffer_ptr(&c->output),
718 buffer_len(&c->output));
719 if (len <= 0)
720 buffer_consume(&c->output, buffer_len(&c->output));
721 else
722 buffer_consume(&c->output, len);
723 }
724}
725
726void
Damien Miller33b13562000-04-04 14:38:59 +1000727channel_handler_init_20(void)
728{
729 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
730 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
731
732 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
733 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
734}
735
736void
Damien Millerb38eff82000-04-01 11:09:21 +1000737channel_handler_init_13(void)
738{
739 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
740 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
741 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
742 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
743 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
744 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
745 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
746
747 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
748 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
749 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
750 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
751 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
752}
753
754void
755channel_handler_init_15(void)
756{
757 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
758 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_15;
759 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
760 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
761 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
762
763 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
764 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
765 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
766 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
767}
768
769void
770channel_handler_init(void)
771{
772 int i;
773 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
774 channel_pre[i] = NULL;
775 channel_post[i] = NULL;
776 }
Damien Miller33b13562000-04-04 14:38:59 +1000777 if (compat20)
778 channel_handler_init_20();
779 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000780 channel_handler_init_13();
781 else
782 channel_handler_init_15();
783}
784
785void
786channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
787{
788 static int did_init = 0;
789 int i;
790 Channel *c;
791
792 if (!did_init) {
793 channel_handler_init();
794 did_init = 1;
795 }
796 for (i = 0; i < channels_alloc; i++) {
797 c = &channels[i];
798 if (c->type == SSH_CHANNEL_FREE)
799 continue;
800 if (ftab[c->type] == NULL)
801 continue;
802 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000803 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000804 }
805}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807void
808channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809{
Damien Millerb38eff82000-04-01 11:09:21 +1000810 channel_handler(channel_pre, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}
812
Damien Miller95def091999-11-25 00:26:21 +1100813void
814channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815{
Damien Millerb38eff82000-04-01 11:09:21 +1000816 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817}
818
819/* If there is data to send to the connection, send some of it now. */
820
Damien Miller95def091999-11-25 00:26:21 +1100821void
822channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823{
Damien Miller95def091999-11-25 00:26:21 +1100824 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000825 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826
Damien Miller95def091999-11-25 00:26:21 +1100827 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000828 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +1100829
Damien Miller5428f641999-11-25 11:54:57 +1100830 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +1100831 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000832 if (c->type != SSH_CHANNEL_OPEN &&
833 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +1100834 continue;
835 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000836 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100837 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000838 if (c->istate != CHAN_INPUT_OPEN &&
839 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +1100840 continue;
841 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000842 if (compat20 &&
843 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Damien Miller33b13562000-04-04 14:38:59 +1000844 debug("channel: %d: no data after CLOSE", c->self);
845 continue;
846 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Miller95def091999-11-25 00:26:21 +1100848 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +1000849 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +1100850 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100851 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +1000852 if (compat20) {
853 if (len > c->remote_window)
854 len = c->remote_window;
855 if (len > c->remote_maxpacket)
856 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +1100857 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000858 if (packet_is_interactive()) {
859 if (len > 1024)
860 len = 512;
861 } else {
862 /* Keep the packets at reasonable size. */
863 if (len > packet_get_maxsize()/2)
864 len = packet_get_maxsize()/2;
865 }
Damien Miller95def091999-11-25 00:26:21 +1100866 }
Damien Millerb38eff82000-04-01 11:09:21 +1000867 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000868 packet_start(compat20 ?
869 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +1000870 packet_put_int(c->remote_id);
871 packet_put_string(buffer_ptr(&c->input), len);
872 packet_send();
873 buffer_consume(&c->input, len);
874 c->remote_window -= len;
Damien Miller33b13562000-04-04 14:38:59 +1000875 debug("channel %d: send data len %d", c->self, len);
Damien Millerb38eff82000-04-01 11:09:21 +1000876 }
877 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +1100878 if (compat13)
879 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +1100880 /*
881 * input-buffer is empty and read-socket shutdown:
882 * tell peer, that we will not send more data: send IEOF
883 */
Damien Millerb38eff82000-04-01 11:09:21 +1000884 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +1100885 }
Damien Miller33b13562000-04-04 14:38:59 +1000886 /* Send extended data, i.e. stderr */
887 if (compat20 &&
888 c->remote_window > 0 &&
889 (len = buffer_len(&c->extended)) > 0 &&
890 c->extended_usage == CHAN_EXTENDED_READ) {
891 if (len > c->remote_window)
892 len = c->remote_window;
893 if (len > c->remote_maxpacket)
894 len = c->remote_maxpacket;
895 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
896 packet_put_int(c->remote_id);
897 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
898 packet_put_string(buffer_ptr(&c->extended), len);
899 packet_send();
900 buffer_consume(&c->extended, len);
901 c->remote_window -= len;
902 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904}
905
Damien Miller5428f641999-11-25 11:54:57 +1100906/*
907 * This is called when a packet of type CHANNEL_DATA has just been received.
908 * The message type has already been consumed, but channel number and data is
909 * still there.
910 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911
Damien Miller95def091999-11-25 00:26:21 +1100912void
Damien Millerb38eff82000-04-01 11:09:21 +1000913channel_input_data(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914{
Damien Miller34132e52000-01-14 15:45:46 +1100915 int id;
Damien Miller95def091999-11-25 00:26:21 +1100916 char *data;
917 unsigned int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000918 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Damien Miller95def091999-11-25 00:26:21 +1100920 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +1100921 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +1000922 c = channel_lookup(id);
923 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +1100924 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925
Damien Miller95def091999-11-25 00:26:21 +1100926 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +1000927 if (c->type != SSH_CHANNEL_OPEN &&
928 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100929 return;
930
931 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +1000932 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +1100933 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934
Damien Miller95def091999-11-25 00:26:21 +1100935 /* Get the data. */
936 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000937
Damien Miller33b13562000-04-04 14:38:59 +1000938 if (compat20){
939 if (data_len > c->local_maxpacket) {
940 log("channel %d: rcvd big packet %d, maxpack %d",
941 c->self, data_len, c->local_maxpacket);
942 }
943 if (data_len > c->local_window) {
944 log("channel %d: rcvd too much data %d, win %d",
945 c->self, data_len, c->local_window);
946 xfree(data);
947 return;
948 }
949 c->local_window -= data_len;
950 }else{
951 packet_integrity_check(plen, 4 + 4 + data_len, type);
952 }
Damien Millerb38eff82000-04-01 11:09:21 +1000953 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +1100954 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955}
Damien Miller33b13562000-04-04 14:38:59 +1000956void
957channel_input_extended_data(int type, int plen)
958{
959 int id;
960 int tcode;
961 char *data;
962 unsigned int data_len;
963 Channel *c;
964
965 /* Get the channel number and verify it. */
966 id = packet_get_int();
967 c = channel_lookup(id);
968
969 if (c == NULL)
970 packet_disconnect("Received extended_data for bad channel %d.", id);
971 if (c->type != SSH_CHANNEL_OPEN) {
972 log("channel %d: ext data for non open", id);
973 return;
974 }
975 tcode = packet_get_int();
976 if (c->efd == -1 ||
977 c->extended_usage != CHAN_EXTENDED_WRITE ||
978 tcode != SSH2_EXTENDED_DATA_STDERR) {
979 log("channel %d: bad ext data", c->self);
980 return;
981 }
982 data = packet_get_string(&data_len);
983 if (data_len > c->local_window) {
984 log("channel %d: rcvd too much extended_data %d, win %d",
985 c->self, data_len, c->local_window);
986 xfree(data);
987 return;
988 }
989 debug("channel %d: rcvd ext data %d", c->self, data_len);
990 c->local_window -= data_len;
991 buffer_append(&c->extended, data, data_len);
992 xfree(data);
993}
994
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995
Damien Miller5428f641999-11-25 11:54:57 +1100996/*
997 * Returns true if no channel has too much buffered data, and false if one or
998 * more channel is overfull.
999 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001int
1002channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003{
Damien Miller95def091999-11-25 00:26:21 +11001004 unsigned int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001005 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001006
1007 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001008 c = &channels[i];
1009 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001010 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001011 debug("channel %d: big input buffer %d",
1012 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001013 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001014 }
1015 if (buffer_len(&c->output) > packet_get_maxsize()) {
1016 debug("channel %d: big output buffer %d",
1017 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001018 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001019 }
Damien Miller95def091999-11-25 00:26:21 +11001020 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021 }
Damien Miller95def091999-11-25 00:26:21 +11001022 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023}
1024
Damien Millerb38eff82000-04-01 11:09:21 +10001025void
1026channel_input_ieof(int type, int plen)
1027{
1028 int id;
1029 Channel *c;
1030
1031 packet_integrity_check(plen, 4, type);
1032
1033 id = packet_get_int();
1034 c = channel_lookup(id);
1035 if (c == NULL)
1036 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1037 chan_rcvd_ieof(c);
1038}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039
Damien Miller95def091999-11-25 00:26:21 +11001040void
Damien Millerb38eff82000-04-01 11:09:21 +10001041channel_input_close(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042{
Damien Millerb38eff82000-04-01 11:09:21 +10001043 int id;
1044 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045
Damien Millerb38eff82000-04-01 11:09:21 +10001046 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047
Damien Millerb38eff82000-04-01 11:09:21 +10001048 id = packet_get_int();
1049 c = channel_lookup(id);
1050 if (c == NULL)
1051 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001052
1053 /*
1054 * Send a confirmation that we have closed the channel and no more
1055 * data is coming for it.
1056 */
Damien Miller95def091999-11-25 00:26:21 +11001057 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001058 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001059 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060
Damien Miller5428f641999-11-25 11:54:57 +11001061 /*
1062 * If the channel is in closed state, we have sent a close request,
1063 * and the other side will eventually respond with a confirmation.
1064 * Thus, we cannot free the channel here, because then there would be
1065 * no-one to receive the confirmation. The channel gets freed when
1066 * the confirmation arrives.
1067 */
Damien Millerb38eff82000-04-01 11:09:21 +10001068 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001069 /*
1070 * Not a closed channel - mark it as draining, which will
1071 * cause it to be freed later.
1072 */
Damien Millerb38eff82000-04-01 11:09:21 +10001073 buffer_consume(&c->input, buffer_len(&c->input));
1074 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001075 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076}
1077
Damien Millerb38eff82000-04-01 11:09:21 +10001078/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller95def091999-11-25 00:26:21 +11001079void
Damien Millerb38eff82000-04-01 11:09:21 +10001080channel_input_oclose(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081{
Damien Millerb38eff82000-04-01 11:09:21 +10001082 int id = packet_get_int();
1083 Channel *c = channel_lookup(id);
1084 packet_integrity_check(plen, 4, type);
1085 if (c == NULL)
1086 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1087 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088}
1089
Damien Millerb38eff82000-04-01 11:09:21 +10001090void
1091channel_input_close_confirmation(int type, int plen)
1092{
1093 int id = packet_get_int();
1094 Channel *c = channel_lookup(id);
1095
1096 if (c == NULL)
1097 packet_disconnect("Received close confirmation for "
1098 "out-of-range channel %d.", id);
1099 if (c->type != SSH_CHANNEL_CLOSED)
1100 packet_disconnect("Received close confirmation for "
1101 "non-closed channel %d (type %d).", id, c->type);
1102 channel_free(c->self);
1103}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104
Damien Miller95def091999-11-25 00:26:21 +11001105void
Damien Millerb38eff82000-04-01 11:09:21 +10001106channel_input_open_confirmation(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107{
Damien Millerb38eff82000-04-01 11:09:21 +10001108 int id, remote_id;
1109 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110
Damien Miller33b13562000-04-04 14:38:59 +10001111 if (!compat20)
1112 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113
Damien Millerb38eff82000-04-01 11:09:21 +10001114 id = packet_get_int();
1115 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116
Damien Millerb38eff82000-04-01 11:09:21 +10001117 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1118 packet_disconnect("Received open confirmation for "
1119 "non-opening channel %d.", id);
1120 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001121 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001122 c->remote_id = remote_id;
1123 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001124
1125 if (compat20) {
1126 c->remote_window = packet_get_int();
1127 c->remote_maxpacket = packet_get_int();
1128 if (c->cb_fn != NULL && c->cb_event == type) {
1129 debug("callback start");
1130 c->cb_fn(c->self, c->cb_arg);
1131 debug("callback done");
1132 }
1133 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1134 c->remote_window, c->remote_maxpacket);
1135 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136}
1137
Damien Miller95def091999-11-25 00:26:21 +11001138void
Damien Millerb38eff82000-04-01 11:09:21 +10001139channel_input_open_failure(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140{
Damien Millerb38eff82000-04-01 11:09:21 +10001141 int id;
1142 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143
Damien Miller33b13562000-04-04 14:38:59 +10001144 if (!compat20)
1145 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001146
1147 id = packet_get_int();
1148 c = channel_lookup(id);
1149
1150 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1151 packet_disconnect("Received open failure for "
1152 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001153 if (compat20) {
1154 int reason = packet_get_int();
1155 char *msg = packet_get_string(NULL);
1156 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
1157 xfree(msg);
1158 }
Damien Miller95def091999-11-25 00:26:21 +11001159 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001160 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161}
1162
Damien Miller33b13562000-04-04 14:38:59 +10001163void
1164channel_input_channel_request(int type, int plen)
1165{
1166 int id;
1167 Channel *c;
1168
1169 id = packet_get_int();
1170 c = channel_lookup(id);
1171
1172 if (c == NULL ||
1173 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1174 packet_disconnect("Received request for "
1175 "non-open channel %d.", id);
1176 if (c->cb_fn != NULL && c->cb_event == type) {
1177 debug("callback start");
1178 c->cb_fn(c->self, c->cb_arg);
1179 debug("callback done");
1180 } else {
1181 char *service = packet_get_string(NULL);
1182 debug("channel: %d rcvd request for %s", c->self, service);
1183debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1184 xfree(service);
1185 }
1186}
1187
1188void
1189channel_input_window_adjust(int type, int plen)
1190{
1191 Channel *c;
1192 int id, adjust;
1193
1194 if (!compat20)
1195 return;
1196
1197 /* Get the channel number and verify it. */
1198 id = packet_get_int();
1199 c = channel_lookup(id);
1200
1201 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1202 log("Received window adjust for "
1203 "non-open channel %d.", id);
1204 return;
1205 }
1206 adjust = packet_get_int();
1207 debug("channel %d: rcvd adjust %d", id, adjust);
1208 c->remote_window += adjust;
1209}
1210
Damien Miller5428f641999-11-25 11:54:57 +11001211/*
1212 * Stops listening for channels, and removes any unix domain sockets that we
1213 * might have.
1214 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215
Damien Miller95def091999-11-25 00:26:21 +11001216void
1217channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218{
Damien Miller95def091999-11-25 00:26:21 +11001219 int i;
1220 for (i = 0; i < channels_alloc; i++) {
1221 switch (channels[i].type) {
1222 case SSH_CHANNEL_AUTH_SOCKET:
1223 close(channels[i].sock);
1224 remove(channels[i].path);
1225 channel_free(i);
1226 break;
1227 case SSH_CHANNEL_PORT_LISTENER:
1228 case SSH_CHANNEL_X11_LISTENER:
1229 close(channels[i].sock);
1230 channel_free(i);
1231 break;
1232 default:
1233 break;
1234 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236}
1237
Damien Miller5428f641999-11-25 11:54:57 +11001238/*
1239 * Closes the sockets of all channels. This is used to close extra file
1240 * descriptors after a fork.
1241 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242
Damien Miller95def091999-11-25 00:26:21 +11001243void
1244channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245{
Damien Miller95def091999-11-25 00:26:21 +11001246 int i;
1247 for (i = 0; i < channels_alloc; i++) {
1248 if (channels[i].type != SSH_CHANNEL_FREE)
1249 close(channels[i].sock);
1250 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251}
1252
1253/* Returns the maximum file descriptor number used by the channels. */
1254
Damien Miller95def091999-11-25 00:26:21 +11001255int
1256channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257{
Damien Miller95def091999-11-25 00:26:21 +11001258 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259}
1260
1261/* Returns true if any channel is still open. */
1262
Damien Miller95def091999-11-25 00:26:21 +11001263int
1264channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265{
Damien Miller95def091999-11-25 00:26:21 +11001266 unsigned int i;
1267 for (i = 0; i < channels_alloc; i++)
1268 switch (channels[i].type) {
1269 case SSH_CHANNEL_FREE:
1270 case SSH_CHANNEL_X11_LISTENER:
1271 case SSH_CHANNEL_PORT_LISTENER:
1272 case SSH_CHANNEL_CLOSED:
1273 case SSH_CHANNEL_AUTH_SOCKET:
1274 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001275 case SSH_CHANNEL_LARVAL:
1276 if (!compat20)
1277 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1278 continue;
Damien Miller95def091999-11-25 00:26:21 +11001279 case SSH_CHANNEL_OPENING:
1280 case SSH_CHANNEL_OPEN:
1281 case SSH_CHANNEL_X11_OPEN:
1282 return 1;
1283 case SSH_CHANNEL_INPUT_DRAINING:
1284 case SSH_CHANNEL_OUTPUT_DRAINING:
1285 if (!compat13)
1286 fatal("cannot happen: OUT_DRAIN");
1287 return 1;
1288 default:
1289 fatal("channel_still_open: bad channel type %d", channels[i].type);
1290 /* NOTREACHED */
1291 }
1292 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293}
1294
Damien Miller5428f641999-11-25 11:54:57 +11001295/*
1296 * Returns a message describing the currently open forwarded connections,
1297 * suitable for sending to the client. The message contains crlf pairs for
1298 * newlines.
1299 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300
Damien Miller95def091999-11-25 00:26:21 +11001301char *
1302channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303{
Damien Miller95def091999-11-25 00:26:21 +11001304 Buffer buffer;
1305 int i;
1306 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307
Damien Miller95def091999-11-25 00:26:21 +11001308 buffer_init(&buffer);
1309 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001311 for (i = 0; i < channels_alloc; i++) {
1312 Channel *c = &channels[i];
1313 switch (c->type) {
1314 case SSH_CHANNEL_FREE:
1315 case SSH_CHANNEL_X11_LISTENER:
1316 case SSH_CHANNEL_PORT_LISTENER:
1317 case SSH_CHANNEL_CLOSED:
1318 case SSH_CHANNEL_AUTH_SOCKET:
1319 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001320 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001321 case SSH_CHANNEL_OPENING:
1322 case SSH_CHANNEL_OPEN:
1323 case SSH_CHANNEL_X11_OPEN:
1324 case SSH_CHANNEL_INPUT_DRAINING:
1325 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001326 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
Damien Miller34132e52000-01-14 15:45:46 +11001327 c->self, c->remote_name,
1328 c->type, c->remote_id,
1329 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001330 c->ostate, buffer_len(&c->output),
1331 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001332 buffer_append(&buffer, buf, strlen(buf));
1333 continue;
1334 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001335 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001336 /* NOTREACHED */
1337 }
1338 }
1339 buffer_append(&buffer, "\0", 1);
1340 cp = xstrdup(buffer_ptr(&buffer));
1341 buffer_free(&buffer);
1342 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343}
1344
Damien Miller5428f641999-11-25 11:54:57 +11001345/*
1346 * Initiate forwarding of connections to local port "port" through the secure
1347 * channel to host:port from remote side.
1348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349
Damien Miller95def091999-11-25 00:26:21 +11001350void
Damien Milleraae6c611999-12-06 11:47:28 +11001351channel_request_local_forwarding(u_short port, const char *host,
Damien Millera34a28b1999-12-14 10:47:15 +11001352 u_short host_port, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353{
Damien Miller34132e52000-01-14 15:45:46 +11001354 int success, ch, sock, on = 1;
1355 struct addrinfo hints, *ai, *aitop;
1356 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller5428f641999-11-25 11:54:57 +11001357 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Damien Miller95def091999-11-25 00:26:21 +11001359 if (strlen(host) > sizeof(channels[0].path) - 1)
1360 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Damien Miller5428f641999-11-25 11:54:57 +11001362 /*
Damien Miller34132e52000-01-14 15:45:46 +11001363 * getaddrinfo returns a loopback address if the hostname is
1364 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001365 */
Damien Miller34132e52000-01-14 15:45:46 +11001366 memset(&hints, 0, sizeof(hints));
1367 hints.ai_family = IPv4or6;
1368 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1369 hints.ai_socktype = SOCK_STREAM;
1370 snprintf(strport, sizeof strport, "%d", port);
1371 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1372 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001373
Damien Miller34132e52000-01-14 15:45:46 +11001374 success = 0;
1375 for (ai = aitop; ai; ai = ai->ai_next) {
1376 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1377 continue;
1378 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1379 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1380 error("channel_request_local_forwarding: getnameinfo failed");
1381 continue;
1382 }
1383 /* Create a port to listen for the host. */
1384 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1385 if (sock < 0) {
1386 /* this is no error since kernel may not support ipv6 */
1387 verbose("socket: %.100s", strerror(errno));
1388 continue;
1389 }
1390 /*
1391 * Set socket options. We would like the socket to disappear
1392 * as soon as it has been closed for whatever reason.
1393 */
1394 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1395 linger.l_onoff = 1;
1396 linger.l_linger = 5;
1397 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1398 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001399
Damien Miller34132e52000-01-14 15:45:46 +11001400 /* Bind the socket to the address. */
1401 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1402 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001403 if (!ai->ai_next)
1404 error("bind: %.100s", strerror(errno));
1405 else
1406 verbose("bind: %.100s", strerror(errno));
1407
Damien Miller34132e52000-01-14 15:45:46 +11001408 close(sock);
1409 continue;
1410 }
1411 /* Start listening for connections on the socket. */
1412 if (listen(sock, 5) < 0) {
1413 error("listen: %.100s", strerror(errno));
1414 close(sock);
1415 continue;
1416 }
1417 /* Allocate a channel number for the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001418 ch = channel_new(
1419 "port listener", SSH_CHANNEL_PORT_LISTENER,
1420 sock, sock, -1,
1421 CHAN_WINDOW_DEFAULT, CHAN_PACKET_DEFAULT,
1422 0, xstrdup("port listener"));
Damien Miller34132e52000-01-14 15:45:46 +11001423 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1424 channels[ch].host_port = host_port;
1425 channels[ch].listening_port = port;
1426 success = 1;
1427 }
1428 if (success == 0)
1429 packet_disconnect("cannot listen port: %d", port);
1430 freeaddrinfo(aitop);
Damien Miller95def091999-11-25 00:26:21 +11001431}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432
Damien Miller5428f641999-11-25 11:54:57 +11001433/*
1434 * Initiate forwarding of connections to port "port" on remote host through
1435 * the secure channel to host:port from local side.
1436 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Damien Miller95def091999-11-25 00:26:21 +11001438void
Damien Millerb38eff82000-04-01 11:09:21 +10001439channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1440 u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441{
Damien Miller95def091999-11-25 00:26:21 +11001442 int payload_len;
1443 /* Record locally that connection to this host/port is permitted. */
1444 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1445 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446
Damien Millerb38eff82000-04-01 11:09:21 +10001447 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1448 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1449 permitted_opens[num_permitted_opens].listen_port = listen_port;
Damien Miller95def091999-11-25 00:26:21 +11001450 num_permitted_opens++;
1451
1452 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001453 if (compat20) {
1454 const char *address_to_bind = "0.0.0.0";
1455 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1456 packet_put_cstring("tcpip-forward");
1457 packet_put_char(0); /* boolean: want reply */
1458 packet_put_cstring(address_to_bind);
1459 packet_put_int(listen_port);
1460 } else {
1461 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1462 packet_put_int(port_to_connect);
1463 packet_put_cstring(host_to_connect);
1464 packet_put_int(listen_port);
1465 packet_send();
1466 packet_write_wait();
1467 /*
1468 * Wait for response from the remote side. It will send a disconnect
1469 * message on failure, and we will never see it here.
1470 */
1471 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1472 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473}
1474
Damien Miller5428f641999-11-25 11:54:57 +11001475/*
1476 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1477 * listening for the port, and sends back a success reply (or disconnect
1478 * message if there was an error). This never returns if there was an error.
1479 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001480
Damien Miller95def091999-11-25 00:26:21 +11001481void
1482channel_input_port_forward_request(int is_root)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483{
Damien Milleraae6c611999-12-06 11:47:28 +11001484 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001485 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486
Damien Miller95def091999-11-25 00:26:21 +11001487 /* Get arguments from the packet. */
1488 port = packet_get_int();
1489 hostname = packet_get_string(NULL);
1490 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
Damien Miller5428f641999-11-25 11:54:57 +11001492 /*
1493 * Check that an unprivileged user is not trying to forward a
1494 * privileged port.
1495 */
Damien Miller95def091999-11-25 00:26:21 +11001496 if (port < IPPORT_RESERVED && !is_root)
1497 packet_disconnect("Requested forwarding of port %d but user is not root.",
1498 port);
Damien Millera34a28b1999-12-14 10:47:15 +11001499 /*
1500 * Initiate forwarding,
1501 * bind port to localhost only (gateway ports == 0).
1502 */
1503 channel_request_local_forwarding(port, hostname, host_port, 0);
Damien Miller95def091999-11-25 00:26:21 +11001504
1505 /* Free the argument string. */
1506 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507}
1508
Damien Millerb38eff82000-04-01 11:09:21 +10001509/* XXX move to aux.c */
1510int
1511channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001512{
Damien Miller34132e52000-01-14 15:45:46 +11001513 struct addrinfo hints, *ai, *aitop;
1514 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1515 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001516 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001517
Damien Miller34132e52000-01-14 15:45:46 +11001518 memset(&hints, 0, sizeof(hints));
1519 hints.ai_family = IPv4or6;
1520 hints.ai_socktype = SOCK_STREAM;
1521 snprintf(strport, sizeof strport, "%d", host_port);
1522 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1523 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001524 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525 }
Damien Miller34132e52000-01-14 15:45:46 +11001526 for (ai = aitop; ai; ai = ai->ai_next) {
1527 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1528 continue;
1529 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1530 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001531 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001532 continue;
1533 }
1534 /* Create the socket. */
1535 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1536 if (sock < 0) {
1537 error("socket: %.100s", strerror(errno));
1538 continue;
1539 }
1540 /* Connect to the host/port. */
1541 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1542 error("connect %.100s port %s: %.100s", ntop, strport,
1543 strerror(errno));
1544 close(sock);
1545 continue; /* fail -- try next */
1546 }
1547 break; /* success */
1548
1549 }
1550 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001551 if (!ai) {
1552 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001553 return -1;
1554 }
1555 /* success */
1556 return sock;
1557}
1558
1559/*
1560 * This is called after receiving PORT_OPEN message. This attempts to
1561 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1562 * or CHANNEL_OPEN_FAILURE.
1563 */
1564
1565void
1566channel_input_port_open(int type, int plen)
1567{
1568 u_short host_port;
1569 char *host, *originator_string;
1570 int remote_channel, sock = -1, newch, i, denied;
1571 unsigned int host_len, originator_len;
1572
1573 /* Get remote channel number. */
1574 remote_channel = packet_get_int();
1575
1576 /* Get host name to connect to. */
1577 host = packet_get_string(&host_len);
1578
1579 /* Get port to connect to. */
1580 host_port = packet_get_int();
1581
1582 /* Get remote originator name. */
1583 if (have_hostname_in_open) {
1584 originator_string = packet_get_string(&originator_len);
1585 originator_len += 4; /* size of packet_int */
1586 } else {
1587 originator_string = xstrdup("unknown (remote did not supply name)");
1588 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001589 }
Damien Miller34132e52000-01-14 15:45:46 +11001590
Damien Millerb38eff82000-04-01 11:09:21 +10001591 packet_integrity_check(plen,
1592 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593
Damien Millerb38eff82000-04-01 11:09:21 +10001594 /* Check if opening that port is permitted. */
1595 denied = 0;
1596 if (!all_opens_permitted) {
1597 /* Go trough all permitted ports. */
1598 for (i = 0; i < num_permitted_opens; i++)
1599 if (permitted_opens[i].port_to_connect == host_port &&
1600 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1601 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602
Damien Millerb38eff82000-04-01 11:09:21 +10001603 /* Check if we found the requested port among those permitted. */
1604 if (i >= num_permitted_opens) {
1605 /* The port is not permitted. */
1606 log("Received request to connect to %.100s:%d, but the request was denied.",
1607 host, host_port);
1608 denied = 1;
1609 }
1610 }
1611 sock = denied ? -1 : channel_connect_to(host, host_port);
1612 if (sock > 0) {
1613 /* Allocate a channel for this connection. */
1614 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1615 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001616
Damien Millerb38eff82000-04-01 11:09:21 +10001617 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1618 packet_put_int(remote_channel);
1619 packet_put_int(newch);
1620 packet_send();
1621 } else {
1622 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1623 packet_put_int(remote_channel);
1624 packet_send();
1625 }
Damien Miller95def091999-11-25 00:26:21 +11001626 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627}
1628
Damien Miller5428f641999-11-25 11:54:57 +11001629/*
1630 * Creates an internet domain socket for listening for X11 connections.
1631 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1632 * occurs.
1633 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634
Damien Miller34132e52000-01-14 15:45:46 +11001635#define NUM_SOCKS 10
1636
Damien Miller95def091999-11-25 00:26:21 +11001637char *
Damien Millera34a28b1999-12-14 10:47:15 +11001638x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001639{
Damien Milleraae6c611999-12-06 11:47:28 +11001640 int display_number, sock;
1641 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001642 struct addrinfo hints, *ai, *aitop;
1643 char strport[NI_MAXSERV];
1644 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1645 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001646 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647
Damien Millera34a28b1999-12-14 10:47:15 +11001648 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001649 display_number < MAX_DISPLAYS;
1650 display_number++) {
1651 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001652 memset(&hints, 0, sizeof(hints));
1653 hints.ai_family = IPv4or6;
1654 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1655 hints.ai_socktype = SOCK_STREAM;
1656 snprintf(strport, sizeof strport, "%d", port);
1657 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1658 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001659 return NULL;
1660 }
Damien Miller34132e52000-01-14 15:45:46 +11001661 for (ai = aitop; ai; ai = ai->ai_next) {
1662 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1663 continue;
1664 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1665 if (sock < 0) {
Damien Millere2192732000-01-17 13:22:55 +11001666 if (errno != EINVAL) {
1667 error("socket: %.100s", strerror(errno));
1668 return NULL;
1669 } else {
1670 debug("Socket family %d not supported [X11 disp create]", ai->ai_family);
1671 continue;
1672 }
Damien Miller34132e52000-01-14 15:45:46 +11001673 }
1674 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1675 debug("bind port %d: %.100s", port, strerror(errno));
1676 shutdown(sock, SHUT_RDWR);
1677 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001678
1679 if (ai->ai_next)
1680 continue;
1681
Damien Miller34132e52000-01-14 15:45:46 +11001682 for (n = 0; n < num_socks; n++) {
1683 shutdown(socks[n], SHUT_RDWR);
1684 close(socks[n]);
1685 }
1686 num_socks = 0;
1687 break;
1688 }
1689 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001690#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001691 if (num_socks == NUM_SOCKS)
1692 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001693#else
1694 break;
1695#endif
Damien Miller95def091999-11-25 00:26:21 +11001696 }
Damien Miller34132e52000-01-14 15:45:46 +11001697 if (num_socks > 0)
1698 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001699 }
Damien Miller95def091999-11-25 00:26:21 +11001700 if (display_number >= MAX_DISPLAYS) {
1701 error("Failed to allocate internet-domain X11 display socket.");
1702 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703 }
Damien Miller95def091999-11-25 00:26:21 +11001704 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001705 for (n = 0; n < num_socks; n++) {
1706 sock = socks[n];
1707 if (listen(sock, 5) < 0) {
1708 error("listen: %.100s", strerror(errno));
1709 shutdown(sock, SHUT_RDWR);
1710 close(sock);
1711 return NULL;
1712 }
Damien Miller95def091999-11-25 00:26:21 +11001713 }
Damien Miller34132e52000-01-14 15:45:46 +11001714
Damien Miller95def091999-11-25 00:26:21 +11001715 /* Set up a suitable value for the DISPLAY variable. */
Damien Miller76112de1999-12-21 11:18:08 +11001716
Damien Miller95def091999-11-25 00:26:21 +11001717 if (gethostname(hostname, sizeof(hostname)) < 0)
1718 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001719
1720#ifdef IPADDR_IN_DISPLAY
1721 /*
1722 * HPUX detects the local hostname in the DISPLAY variable and tries
1723 * to set up a shared memory connection to the server, which it
1724 * incorrectly supposes to be local.
1725 *
1726 * The workaround - as used in later $$H and other programs - is
1727 * is to set display to the host's IP address.
1728 */
1729 {
1730 struct hostent *he;
1731 struct in_addr my_addr;
1732
1733 he = gethostbyname(hostname);
1734 if (he == NULL) {
1735 error("[X11-broken-fwd-hostname-workaround] Could not get "
1736 "IP address for hostname %s.", hostname);
1737
1738 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1739 "Could not get IP address for hostname %s.", hostname);
1740
1741 shutdown(sock, SHUT_RDWR);
1742 close(sock);
1743
1744 return NULL;
1745 }
1746
1747 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1748
1749 /* Set DISPLAY to <ip address>:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001750 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Damien Miller76112de1999-12-21 11:18:08 +11001751 display_number, screen_number);
1752 }
1753#else /* IPADDR_IN_DISPLAY */
1754 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001755 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Damien Miller76112de1999-12-21 11:18:08 +11001756 display_number, screen_number);
1757#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758
Damien Miller34132e52000-01-14 15:45:46 +11001759 /* Allocate a channel for each socket. */
1760 for (n = 0; n < num_socks; n++) {
1761 sock = socks[n];
1762 (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
1763 xstrdup("X11 inet listener"));
1764 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001765
Damien Miller95def091999-11-25 00:26:21 +11001766 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11001767 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001768}
1769
1770#ifndef X_UNIX_PATH
1771#define X_UNIX_PATH "/tmp/.X11-unix/X"
1772#endif
1773
1774static
1775int
Damien Miller81b25cf1999-12-07 16:47:28 +11001776connect_local_xsocket(unsigned int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001777{
Damien Miller95def091999-11-25 00:26:21 +11001778 static const char *const x_sockets[] = {
1779 X_UNIX_PATH "%u",
1780 "/var/X/.X11-unix/X" "%u",
1781 "/usr/spool/sockets/X11/" "%u",
1782 NULL
1783 };
1784 int sock;
1785 struct sockaddr_un addr;
1786 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001787
Damien Miller95def091999-11-25 00:26:21 +11001788 for (path = x_sockets; *path; ++path) {
1789 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1790 if (sock < 0)
1791 error("socket: %.100s", strerror(errno));
1792 memset(&addr, 0, sizeof(addr));
1793 addr.sun_family = AF_UNIX;
1794 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1795 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1796 return sock;
1797 close(sock);
1798 }
1799 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1800 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001801}
1802
1803
Damien Miller5428f641999-11-25 11:54:57 +11001804/*
1805 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1806 * the remote channel number. We should do whatever we want, and respond
1807 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1808 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001809
Damien Miller95def091999-11-25 00:26:21 +11001810void
Damien Millerb38eff82000-04-01 11:09:21 +10001811x11_input_open(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001812{
Damien Miller34132e52000-01-14 15:45:46 +11001813 int remote_channel, display_number, sock = 0, newch;
Damien Miller95def091999-11-25 00:26:21 +11001814 const char *display;
Damien Miller95def091999-11-25 00:26:21 +11001815 char buf[1024], *cp, *remote_host;
Damien Miller7684ee12000-03-17 23:40:15 +11001816 unsigned int remote_len;
Damien Miller34132e52000-01-14 15:45:46 +11001817 struct addrinfo hints, *ai, *aitop;
1818 char strport[NI_MAXSERV];
1819 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001820
Damien Miller95def091999-11-25 00:26:21 +11001821 /* Get remote channel number. */
1822 remote_channel = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823
Damien Miller95def091999-11-25 00:26:21 +11001824 /* Get remote originator name. */
Damien Miller5428f641999-11-25 11:54:57 +11001825 if (have_hostname_in_open) {
Damien Miller95def091999-11-25 00:26:21 +11001826 remote_host = packet_get_string(&remote_len);
Damien Miller5428f641999-11-25 11:54:57 +11001827 remote_len += 4;
1828 } else {
Damien Miller95def091999-11-25 00:26:21 +11001829 remote_host = xstrdup("unknown (remote did not supply name)");
Damien Miller5428f641999-11-25 11:54:57 +11001830 remote_len = 0;
1831 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832
Damien Miller95def091999-11-25 00:26:21 +11001833 debug("Received X11 open request.");
Damien Millerb38eff82000-04-01 11:09:21 +10001834 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001835
Damien Miller95def091999-11-25 00:26:21 +11001836 /* Try to open a socket for the local X server. */
1837 display = getenv("DISPLAY");
1838 if (!display) {
1839 error("DISPLAY not set.");
1840 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001841 }
Damien Miller5428f641999-11-25 11:54:57 +11001842 /*
1843 * Now we decode the value of the DISPLAY variable and make a
1844 * connection to the real X server.
1845 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846
Damien Miller5428f641999-11-25 11:54:57 +11001847 /*
1848 * Check if it is a unix domain socket. Unix domain displays are in
1849 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1850 */
Damien Miller95def091999-11-25 00:26:21 +11001851 if (strncmp(display, "unix:", 5) == 0 ||
1852 display[0] == ':') {
1853 /* Connect to the unix domain socket. */
1854 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1855 error("Could not parse display number from DISPLAY: %.100s",
1856 display);
1857 goto fail;
1858 }
1859 /* Create a socket. */
1860 sock = connect_local_xsocket(display_number);
1861 if (sock < 0)
1862 goto fail;
1863
1864 /* OK, we now have a connection to the display. */
1865 goto success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001866 }
Damien Miller5428f641999-11-25 11:54:57 +11001867 /*
1868 * Connect to an inet socket. The DISPLAY value is supposedly
1869 * hostname:d[.s], where hostname may also be numeric IP address.
1870 */
Damien Miller95def091999-11-25 00:26:21 +11001871 strncpy(buf, display, sizeof(buf));
1872 buf[sizeof(buf) - 1] = 0;
1873 cp = strchr(buf, ':');
1874 if (!cp) {
1875 error("Could not find ':' in DISPLAY: %.100s", display);
1876 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001877 }
Damien Miller95def091999-11-25 00:26:21 +11001878 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001879 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11001880 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1881 error("Could not parse display number from DISPLAY: %.100s",
1882 display);
1883 goto fail;
1884 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001885
Damien Miller34132e52000-01-14 15:45:46 +11001886 /* Look up the host address */
1887 memset(&hints, 0, sizeof(hints));
1888 hints.ai_family = IPv4or6;
1889 hints.ai_socktype = SOCK_STREAM;
1890 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1891 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1892 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001893 goto fail;
1894 }
Damien Miller34132e52000-01-14 15:45:46 +11001895 for (ai = aitop; ai; ai = ai->ai_next) {
1896 /* Create a socket. */
1897 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1898 if (sock < 0) {
1899 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001900 continue;
1901 }
1902 /* Connect it to the display. */
1903 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1904 debug("connect %.100s port %d: %.100s", buf,
1905 6000 + display_number, strerror(errno));
1906 close(sock);
1907 continue;
1908 }
1909 /* Success */
1910 break;
Damien Miller34132e52000-01-14 15:45:46 +11001911 }
Damien Miller34132e52000-01-14 15:45:46 +11001912 freeaddrinfo(aitop);
1913 if (!ai) {
1914 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1915 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001916 goto fail;
1917 }
1918success:
1919 /* We have successfully obtained a connection to the real X display. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920
Damien Miller95def091999-11-25 00:26:21 +11001921 /* Allocate a channel for this connection. */
1922 if (x11_saved_proto == NULL)
1923 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
1924 else
1925 newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
1926 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927
Damien Miller95def091999-11-25 00:26:21 +11001928 /* Send a confirmation to the remote host. */
1929 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1930 packet_put_int(remote_channel);
1931 packet_put_int(newch);
1932 packet_send();
1933
1934 return;
1935
1936fail:
1937 /* Send refusal to the remote host. */
1938 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1939 packet_put_int(remote_channel);
1940 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941}
1942
Damien Miller5428f641999-11-25 11:54:57 +11001943/*
1944 * Requests forwarding of X11 connections, generates fake authentication
1945 * data, and enables authentication spoofing.
1946 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001947
Damien Miller95def091999-11-25 00:26:21 +11001948void
1949x11_request_forwarding_with_spoofing(const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950{
Damien Miller95def091999-11-25 00:26:21 +11001951 unsigned int data_len = (unsigned int) strlen(data) / 2;
1952 unsigned int i, value;
1953 char *new_data;
1954 int screen_number;
1955 const char *cp;
1956 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001957
Damien Miller95def091999-11-25 00:26:21 +11001958 cp = getenv("DISPLAY");
1959 if (cp)
1960 cp = strchr(cp, ':');
1961 if (cp)
1962 cp = strchr(cp, '.');
1963 if (cp)
1964 screen_number = atoi(cp + 1);
1965 else
1966 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001967
Damien Miller95def091999-11-25 00:26:21 +11001968 /* Save protocol name. */
1969 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970
Damien Miller5428f641999-11-25 11:54:57 +11001971 /*
1972 * Extract real authentication data and generate fake data of the
1973 * same length.
1974 */
Damien Miller95def091999-11-25 00:26:21 +11001975 x11_saved_data = xmalloc(data_len);
1976 x11_fake_data = xmalloc(data_len);
1977 for (i = 0; i < data_len; i++) {
1978 if (sscanf(data + 2 * i, "%2x", &value) != 1)
1979 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
1980 if (i % 4 == 0)
1981 rand = arc4random();
1982 x11_saved_data[i] = value;
1983 x11_fake_data[i] = rand & 0xff;
1984 rand >>= 8;
1985 }
1986 x11_saved_data_len = data_len;
1987 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001988
Damien Miller95def091999-11-25 00:26:21 +11001989 /* Convert the fake data into hex. */
1990 new_data = xmalloc(2 * data_len + 1);
1991 for (i = 0; i < data_len; i++)
1992 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001993
Damien Miller95def091999-11-25 00:26:21 +11001994 /* Send the request packet. */
1995 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
1996 packet_put_string(proto, strlen(proto));
1997 packet_put_string(new_data, strlen(new_data));
1998 packet_put_int(screen_number);
1999 packet_send();
2000 packet_write_wait();
2001 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002}
2003
2004/* Sends a message to the server to request authentication fd forwarding. */
2005
Damien Miller95def091999-11-25 00:26:21 +11002006void
2007auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002008{
Damien Miller95def091999-11-25 00:26:21 +11002009 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2010 packet_send();
2011 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012}
2013
Damien Miller5428f641999-11-25 11:54:57 +11002014/*
2015 * Returns the name of the forwarded authentication socket. Returns NULL if
2016 * there is no forwarded authentication socket. The returned value points to
2017 * a static buffer.
2018 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002019
Damien Miller95def091999-11-25 00:26:21 +11002020char *
2021auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002022{
Damien Miller95def091999-11-25 00:26:21 +11002023 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024}
2025
2026/* removes the agent forwarding socket */
2027
Damien Miller95def091999-11-25 00:26:21 +11002028void
2029cleanup_socket(void)
2030{
2031 remove(channel_forwarded_auth_socket_name);
2032 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002033}
2034
Damien Miller5428f641999-11-25 11:54:57 +11002035/*
2036 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
2037 * This starts forwarding authentication requests.
2038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002039
Damien Miller95def091999-11-25 00:26:21 +11002040void
2041auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002042{
Damien Miller95def091999-11-25 00:26:21 +11002043 int sock, newch;
2044 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002045
Damien Miller95def091999-11-25 00:26:21 +11002046 if (auth_get_socket_name() != NULL)
2047 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002048
Damien Miller95def091999-11-25 00:26:21 +11002049 /* Temporarily drop privileged uid for mkdir/bind. */
2050 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002051
Damien Miller95def091999-11-25 00:26:21 +11002052 /* Allocate a buffer for the socket name, and format the name. */
2053 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2054 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2055 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002056
Damien Miller95def091999-11-25 00:26:21 +11002057 /* Create private directory for socket */
2058 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
2059 packet_disconnect("mkdtemp: %.100s", strerror(errno));
2060 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2061 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002062
Damien Miller95def091999-11-25 00:26:21 +11002063 if (atexit(cleanup_socket) < 0) {
2064 int saved = errno;
2065 cleanup_socket();
2066 packet_disconnect("socket: %.100s", strerror(saved));
2067 }
2068 /* Create the socket. */
2069 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2070 if (sock < 0)
2071 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002072
Damien Miller95def091999-11-25 00:26:21 +11002073 /* Bind it to the name. */
2074 memset(&sunaddr, 0, sizeof(sunaddr));
2075 sunaddr.sun_family = AF_UNIX;
2076 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2077 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002078
Damien Miller95def091999-11-25 00:26:21 +11002079 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2080 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002081
Damien Miller95def091999-11-25 00:26:21 +11002082 /* Restore the privileged uid. */
2083 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002084
Damien Miller95def091999-11-25 00:26:21 +11002085 /* Start listening on the socket. */
2086 if (listen(sock, 5) < 0)
2087 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002088
Damien Miller95def091999-11-25 00:26:21 +11002089 /* Allocate a channel for the authentication agent socket. */
2090 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
2091 xstrdup("auth socket"));
Damien Miller037a0dc1999-12-07 15:38:31 +11002092 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2093 sizeof(channels[newch].path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094}
2095
2096/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2097
Damien Miller95def091999-11-25 00:26:21 +11002098void
Damien Millerb38eff82000-04-01 11:09:21 +10002099auth_input_open_request(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100{
Damien Miller95def091999-11-25 00:26:21 +11002101 int remch, sock, newch;
2102 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002103
Damien Millerb38eff82000-04-01 11:09:21 +10002104 packet_integrity_check(plen, 4, type);
2105
Damien Miller95def091999-11-25 00:26:21 +11002106 /* Read the remote channel number from the message. */
2107 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002108
Damien Miller5428f641999-11-25 11:54:57 +11002109 /*
2110 * Get a connection to the local authentication agent (this may again
2111 * get forwarded).
2112 */
Damien Miller95def091999-11-25 00:26:21 +11002113 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002114
Damien Miller5428f641999-11-25 11:54:57 +11002115 /*
2116 * If we could not connect the agent, send an error message back to
2117 * the server. This should never happen unless the agent dies,
2118 * because authentication forwarding is only enabled if we have an
2119 * agent.
2120 */
Damien Miller95def091999-11-25 00:26:21 +11002121 if (sock < 0) {
2122 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2123 packet_put_int(remch);
2124 packet_send();
2125 return;
2126 }
2127 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002128
Damien Miller5428f641999-11-25 11:54:57 +11002129 /*
2130 * Dummy host name. This will be freed when the channel is freed; it
2131 * will still be valid in the packet_put_string below since the
2132 * channel cannot yet be freed at that point.
2133 */
Damien Miller95def091999-11-25 00:26:21 +11002134 dummyname = xstrdup("authentication agent connection");
2135
2136 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2137 channels[newch].remote_id = remch;
2138
2139 /* Send a confirmation to the remote host. */
2140 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2141 packet_put_int(remch);
2142 packet_put_int(newch);
2143 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002144}
Damien Miller33b13562000-04-04 14:38:59 +10002145
2146void
2147channel_open(int id)
2148{
2149 Channel *c = channel_lookup(id);
2150 if (c == NULL) {
2151 log("channel_open: %d: bad id", id);
2152 return;
2153 }
2154 packet_start(SSH2_MSG_CHANNEL_OPEN);
2155 packet_put_cstring(c->ctype);
2156 packet_put_int(c->self);
2157 packet_put_int(c->local_window);
2158 packet_put_int(c->local_maxpacket);
2159 packet_send();
2160 debug("channel open %d", id);
2161}
2162void
2163channel_request(int id, char *service, int wantconfirm)
2164{
2165 channel_request_start(id, service, wantconfirm);
2166 packet_send();
2167 debug("channel request %d: %s", id, service) ;
2168}
2169void
2170channel_request_start(int id, char *service, int wantconfirm)
2171{
2172 Channel *c = channel_lookup(id);
2173 if (c == NULL) {
2174 log("channel_request: %d: bad id", id);
2175 return;
2176 }
2177 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2178 packet_put_int(c->remote_id);
2179 packet_put_cstring(service);
2180 packet_put_char(wantconfirm);
2181}
2182void
2183channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2184{
2185 Channel *c = channel_lookup(id);
2186 if (c == NULL) {
2187 log("channel_register_callback: %d: bad id", id);
2188 return;
2189 }
2190 c->cb_event = mtype;
2191 c->cb_fn = fn;
2192 c->cb_arg = arg;
2193}
2194void
2195channel_register_cleanup(int id, channel_callback_fn *fn)
2196{
2197 Channel *c = channel_lookup(id);
2198 if (c == NULL) {
2199 log("channel_register_cleanup: %d: bad id", id);
2200 return;
2201 }
2202 c->dettach_user = fn;
2203}
2204void
2205channel_cancel_cleanup(int id)
2206{
2207 Channel *c = channel_lookup(id);
2208 if (c == NULL) {
2209 log("channel_cancel_cleanup: %d: bad id", id);
2210 return;
2211 }
2212 c->dettach_user = NULL;
2213}
2214
2215void
2216channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
2217{
2218 Channel *c = channel_lookup(id);
2219 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2220 fatal("channel_activate for non-larval channel %d.", id);
2221 if (rfd > channel_max_fd_value)
2222 channel_max_fd_value = rfd;
2223 if (wfd > channel_max_fd_value)
2224 channel_max_fd_value = wfd;
2225 if (efd > channel_max_fd_value)
2226 channel_max_fd_value = efd;
2227 c->type = SSH_CHANNEL_OPEN;
2228 c->rfd = rfd;
2229 c->wfd = wfd;
2230 c->efd = efd;
2231 c->extended_usage = extusage;
2232 /* XXX window size? */
2233 c->local_window = c->local_window_max = c->local_maxpacket/2;
2234 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2235 packet_put_int(c->remote_id);
2236 packet_put_int(c->local_window);
2237 packet_send();
2238}