blob: 18f667f6b5d242ae9c2bb1210508a13232415524 [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 Miller33b13562000-04-04 14:38:59 +100020RCSID("$Id: channels.c,v 1.21 2000/04/04 04:39:00 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;
440 } else if (ret == -1) {
441 /*
442 * We have received an X11 connection that has bad
443 * authentication information.
444 */
445 log("X11 connection rejected because of wrong authentication.\r\n");
446 buffer_clear(&c->input);
447 buffer_clear(&c->output);
448 close(c->sock);
449 c->sock = -1;
450 c->type = SSH_CHANNEL_CLOSED;
451 packet_start(SSH_MSG_CHANNEL_CLOSE);
452 packet_put_int(c->remote_id);
453 packet_send();
454 }
455}
456
457void
458channel_pre_x11_open_15(Channel *c, fd_set * readset, fd_set * writeset)
459{
460 int ret = x11_open_helper(c);
461 if (ret == 1) {
462 c->type = SSH_CHANNEL_OPEN;
463 } else if (ret == -1) {
464 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
465 chan_read_failed(c);
466 chan_write_failed(c);
467 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
468 }
469}
470
471/* This is our fake X11 server socket. */
472void
473channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
474{
475 struct sockaddr addr;
476 int newsock, newch;
477 socklen_t addrlen;
478 char buf[16384], *remote_hostname;
479
480 if (FD_ISSET(c->sock, readset)) {
481 debug("X11 connection requested.");
482 addrlen = sizeof(addr);
483 newsock = accept(c->sock, &addr, &addrlen);
484 if (newsock < 0) {
485 error("accept: %.100s", strerror(errno));
486 return;
487 }
488 remote_hostname = get_remote_hostname(newsock);
489 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
490 remote_hostname, get_peer_port(newsock));
491 xfree(remote_hostname);
492 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
493 xstrdup(buf));
494 packet_start(SSH_SMSG_X11_OPEN);
495 packet_put_int(newch);
496 if (have_hostname_in_open)
497 packet_put_string(buf, strlen(buf));
498 packet_send();
499 }
500}
501
502/*
503 * This socket is listening for connections to a forwarded TCP/IP port.
504 */
505void
506channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
507{
508 struct sockaddr addr;
509 int newsock, newch;
510 socklen_t addrlen;
511 char buf[1024], *remote_hostname;
512 int remote_port;
513
514 if (FD_ISSET(c->sock, readset)) {
515 debug("Connection to port %d forwarding "
516 "to %.100s port %d requested.",
517 c->listening_port, c->path, c->host_port);
518 addrlen = sizeof(addr);
519 newsock = accept(c->sock, &addr, &addrlen);
520 if (newsock < 0) {
521 error("accept: %.100s", strerror(errno));
522 return;
523 }
524 remote_hostname = get_remote_hostname(newsock);
525 remote_port = get_peer_port(newsock);
526 snprintf(buf, sizeof buf,
527 "listen port %d for %.100s port %d, "
528 "connect from %.200s port %d",
529 c->listening_port, c->path, c->host_port,
530 remote_hostname, remote_port);
531 newch = channel_new("direct-tcpip",
532 SSH_CHANNEL_OPENING, newsock, newsock, -1,
533 c->local_window_max, c->local_maxpacket,
534 0, xstrdup(buf));
Damien Miller33b13562000-04-04 14:38:59 +1000535 if (compat20) {
536 packet_start(SSH2_MSG_CHANNEL_OPEN);
537 packet_put_cstring("direct-tcpip");
538 packet_put_int(newch);
539 packet_put_int(c->local_window_max);
540 packet_put_int(c->local_maxpacket);
541 packet_put_string(c->path, strlen(c->path));
542 packet_put_int(c->host_port);
543 packet_put_cstring(remote_hostname);
544 packet_put_int(remote_port);
545 packet_send();
546 } else {
547 packet_start(SSH_MSG_PORT_OPEN);
548 packet_put_int(newch);
549 packet_put_string(c->path, strlen(c->path));
550 packet_put_int(c->host_port);
551 if (have_hostname_in_open) {
552 packet_put_string(buf, strlen(buf));
553 }
554 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000555 }
Damien Millerb38eff82000-04-01 11:09:21 +1000556 xfree(remote_hostname);
557 }
558}
559
560/*
561 * This is the authentication agent socket listening for connections from
562 * clients.
563 */
564void
565channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
566{
567 struct sockaddr addr;
568 int newsock, newch;
569 socklen_t addrlen;
570
571 if (FD_ISSET(c->sock, readset)) {
572 addrlen = sizeof(addr);
573 newsock = accept(c->sock, &addr, &addrlen);
574 if (newsock < 0) {
575 error("accept from auth socket: %.100s", strerror(errno));
576 return;
577 }
578 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
579 xstrdup("accepted auth socket"));
580 packet_start(SSH_SMSG_AGENT_OPEN);
581 packet_put_int(newch);
582 packet_send();
583 }
584}
585
586int
587channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
588{
589 char buf[16*1024];
590 int len;
591
592 if (c->rfd != -1 &&
593 FD_ISSET(c->rfd, readset)) {
594 len = read(c->rfd, buf, sizeof(buf));
595 if (len <= 0) {
596 debug("channel %d: read<0 rfd %d len %d",
597 c->self, c->rfd, len);
598 if (compat13) {
599 buffer_consume(&c->output, buffer_len(&c->output));
600 c->type = SSH_CHANNEL_INPUT_DRAINING;
601 debug("Channel %d status set to input draining.", c->self);
602 } else {
603 chan_read_failed(c);
604 }
605 return -1;
606 }
607 buffer_append(&c->input, buf, len);
608 }
609 return 1;
610}
611int
612channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
613{
614 int len;
615
616 /* Send buffered output data to the socket. */
617 if (c->wfd != -1 &&
618 FD_ISSET(c->wfd, writeset) &&
619 buffer_len(&c->output) > 0) {
620 len = write(c->wfd, buffer_ptr(&c->output),
621 buffer_len(&c->output));
622 if (len <= 0) {
623 if (compat13) {
624 buffer_consume(&c->output, buffer_len(&c->output));
625 debug("Channel %d status set to input draining.", c->self);
626 c->type = SSH_CHANNEL_INPUT_DRAINING;
627 } else {
628 chan_write_failed(c);
629 }
630 return -1;
631 }
632 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000633 if (compat20 && len > 0) {
634 c->local_consumed += len;
635 }
636 }
637 return 1;
638}
639int
640channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
641{
642 char buf[16*1024];
643 int len;
644
645 if (c->efd != -1) {
646 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
647 FD_ISSET(c->efd, writeset) &&
648 buffer_len(&c->extended) > 0) {
649 len = write(c->efd, buffer_ptr(&c->extended),
650 buffer_len(&c->extended));
651 debug("channel %d: written %d to efd %d",
652 c->self, len, c->efd);
653 if (len > 0) {
654 buffer_consume(&c->extended, len);
655 c->local_consumed += len;
656 }
657 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
658 FD_ISSET(c->efd, readset)) {
659 len = read(c->efd, buf, sizeof(buf));
660 debug("channel %d: read %d from efd %d",
661 c->self, len, c->efd);
662 if (len > 0)
663 buffer_append(&c->extended, buf, len);
664 }
665 }
666 return 1;
667}
668int
669channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
670{
671 if (!(c->flags & CHAN_CLOSE_SENT) &&
672 c->local_window < c->local_window_max/2 &&
673 c->local_consumed > 0) {
674 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
675 packet_put_int(c->remote_id);
676 packet_put_int(c->local_consumed);
677 packet_send();
678 debug("channel %d: window %d sent adjust %d",
679 c->self, c->local_window,
680 c->local_consumed);
681 c->local_window += c->local_consumed;
682 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000683 }
684 return 1;
685}
686
687void
688channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
689{
690 channel_handle_rfd(c, readset, writeset);
691 channel_handle_wfd(c, readset, writeset);
692}
693
694void
Damien Miller33b13562000-04-04 14:38:59 +1000695channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
696{
697 channel_handle_rfd(c, readset, writeset);
698 channel_handle_wfd(c, readset, writeset);
699 channel_handle_efd(c, readset, writeset);
700 channel_check_window(c, readset, writeset);
701}
702
703void
Damien Millerb38eff82000-04-01 11:09:21 +1000704channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
705{
706 int len;
707 /* Send buffered output data to the socket. */
708 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
709 len = write(c->sock, buffer_ptr(&c->output),
710 buffer_len(&c->output));
711 if (len <= 0)
712 buffer_consume(&c->output, buffer_len(&c->output));
713 else
714 buffer_consume(&c->output, len);
715 }
716}
717
718void
Damien Miller33b13562000-04-04 14:38:59 +1000719channel_handler_init_20(void)
720{
721 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
722 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
723
724 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
725 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
726}
727
728void
Damien Millerb38eff82000-04-01 11:09:21 +1000729channel_handler_init_13(void)
730{
731 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
732 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
733 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
734 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
735 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
736 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
737 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
738
739 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
740 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
741 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
742 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
743 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
744}
745
746void
747channel_handler_init_15(void)
748{
749 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
750 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_15;
751 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
752 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
753 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
754
755 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
756 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
757 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
758 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
759}
760
761void
762channel_handler_init(void)
763{
764 int i;
765 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
766 channel_pre[i] = NULL;
767 channel_post[i] = NULL;
768 }
Damien Miller33b13562000-04-04 14:38:59 +1000769 if (compat20)
770 channel_handler_init_20();
771 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000772 channel_handler_init_13();
773 else
774 channel_handler_init_15();
775}
776
777void
778channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
779{
780 static int did_init = 0;
781 int i;
782 Channel *c;
783
784 if (!did_init) {
785 channel_handler_init();
786 did_init = 1;
787 }
788 for (i = 0; i < channels_alloc; i++) {
789 c = &channels[i];
790 if (c->type == SSH_CHANNEL_FREE)
791 continue;
792 if (ftab[c->type] == NULL)
793 continue;
794 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000795 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000796 }
797}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798
Damien Miller95def091999-11-25 00:26:21 +1100799void
800channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801{
Damien Millerb38eff82000-04-01 11:09:21 +1000802 channel_handler(channel_pre, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803}
804
Damien Miller95def091999-11-25 00:26:21 +1100805void
806channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807{
Damien Millerb38eff82000-04-01 11:09:21 +1000808 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809}
810
811/* If there is data to send to the connection, send some of it now. */
812
Damien Miller95def091999-11-25 00:26:21 +1100813void
814channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815{
Damien Miller95def091999-11-25 00:26:21 +1100816 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000817 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Damien Miller95def091999-11-25 00:26:21 +1100819 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000820 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +1100821
Damien Miller5428f641999-11-25 11:54:57 +1100822 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +1100823 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000824 if (c->type != SSH_CHANNEL_OPEN &&
825 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +1100826 continue;
827 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000828 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100829 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000830 if (c->istate != CHAN_INPUT_OPEN &&
831 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +1100832 continue;
833 }
Damien Miller33b13562000-04-04 14:38:59 +1000834 if (compat20 && (c->flags & CHAN_CLOSE_SENT)) {
835 debug("channel: %d: no data after CLOSE", c->self);
836 continue;
837 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838
Damien Miller95def091999-11-25 00:26:21 +1100839 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +1000840 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +1100841 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100842 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +1000843 if (compat20) {
844 if (len > c->remote_window)
845 len = c->remote_window;
846 if (len > c->remote_maxpacket)
847 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +1100848 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000849 if (packet_is_interactive()) {
850 if (len > 1024)
851 len = 512;
852 } else {
853 /* Keep the packets at reasonable size. */
854 if (len > packet_get_maxsize()/2)
855 len = packet_get_maxsize()/2;
856 }
Damien Miller95def091999-11-25 00:26:21 +1100857 }
Damien Millerb38eff82000-04-01 11:09:21 +1000858 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000859 packet_start(compat20 ?
860 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +1000861 packet_put_int(c->remote_id);
862 packet_put_string(buffer_ptr(&c->input), len);
863 packet_send();
864 buffer_consume(&c->input, len);
865 c->remote_window -= len;
Damien Miller33b13562000-04-04 14:38:59 +1000866 debug("channel %d: send data len %d", c->self, len);
Damien Millerb38eff82000-04-01 11:09:21 +1000867 }
868 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +1100869 if (compat13)
870 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +1100871 /*
872 * input-buffer is empty and read-socket shutdown:
873 * tell peer, that we will not send more data: send IEOF
874 */
Damien Millerb38eff82000-04-01 11:09:21 +1000875 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +1100876 }
Damien Miller33b13562000-04-04 14:38:59 +1000877 /* Send extended data, i.e. stderr */
878 if (compat20 &&
879 c->remote_window > 0 &&
880 (len = buffer_len(&c->extended)) > 0 &&
881 c->extended_usage == CHAN_EXTENDED_READ) {
882 if (len > c->remote_window)
883 len = c->remote_window;
884 if (len > c->remote_maxpacket)
885 len = c->remote_maxpacket;
886 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
887 packet_put_int(c->remote_id);
888 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
889 packet_put_string(buffer_ptr(&c->extended), len);
890 packet_send();
891 buffer_consume(&c->extended, len);
892 c->remote_window -= len;
893 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895}
896
Damien Miller5428f641999-11-25 11:54:57 +1100897/*
898 * This is called when a packet of type CHANNEL_DATA has just been received.
899 * The message type has already been consumed, but channel number and data is
900 * still there.
901 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller95def091999-11-25 00:26:21 +1100903void
Damien Millerb38eff82000-04-01 11:09:21 +1000904channel_input_data(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905{
Damien Miller34132e52000-01-14 15:45:46 +1100906 int id;
Damien Miller95def091999-11-25 00:26:21 +1100907 char *data;
908 unsigned int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000909 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910
Damien Miller95def091999-11-25 00:26:21 +1100911 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +1100912 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +1000913 c = channel_lookup(id);
914 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +1100915 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916
Damien Miller95def091999-11-25 00:26:21 +1100917 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +1000918 if (c->type != SSH_CHANNEL_OPEN &&
919 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100920 return;
921
922 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +1000923 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +1100924 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925
Damien Miller95def091999-11-25 00:26:21 +1100926 /* Get the data. */
927 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000928
Damien Miller33b13562000-04-04 14:38:59 +1000929 if (compat20){
930 if (data_len > c->local_maxpacket) {
931 log("channel %d: rcvd big packet %d, maxpack %d",
932 c->self, data_len, c->local_maxpacket);
933 }
934 if (data_len > c->local_window) {
935 log("channel %d: rcvd too much data %d, win %d",
936 c->self, data_len, c->local_window);
937 xfree(data);
938 return;
939 }
940 c->local_window -= data_len;
941 }else{
942 packet_integrity_check(plen, 4 + 4 + data_len, type);
943 }
Damien Millerb38eff82000-04-01 11:09:21 +1000944 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +1100945 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946}
Damien Miller33b13562000-04-04 14:38:59 +1000947void
948channel_input_extended_data(int type, int plen)
949{
950 int id;
951 int tcode;
952 char *data;
953 unsigned int data_len;
954 Channel *c;
955
956 /* Get the channel number and verify it. */
957 id = packet_get_int();
958 c = channel_lookup(id);
959
960 if (c == NULL)
961 packet_disconnect("Received extended_data for bad channel %d.", id);
962 if (c->type != SSH_CHANNEL_OPEN) {
963 log("channel %d: ext data for non open", id);
964 return;
965 }
966 tcode = packet_get_int();
967 if (c->efd == -1 ||
968 c->extended_usage != CHAN_EXTENDED_WRITE ||
969 tcode != SSH2_EXTENDED_DATA_STDERR) {
970 log("channel %d: bad ext data", c->self);
971 return;
972 }
973 data = packet_get_string(&data_len);
974 if (data_len > c->local_window) {
975 log("channel %d: rcvd too much extended_data %d, win %d",
976 c->self, data_len, c->local_window);
977 xfree(data);
978 return;
979 }
980 debug("channel %d: rcvd ext data %d", c->self, data_len);
981 c->local_window -= data_len;
982 buffer_append(&c->extended, data, data_len);
983 xfree(data);
984}
985
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller5428f641999-11-25 11:54:57 +1100987/*
988 * Returns true if no channel has too much buffered data, and false if one or
989 * more channel is overfull.
990 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992int
993channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994{
Damien Miller95def091999-11-25 00:26:21 +1100995 unsigned int i;
Damien Millerb38eff82000-04-01 11:09:21 +1000996 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +1100997
998 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000999 c = &channels[i];
1000 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001001 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001002 debug("channel %d: big input buffer %d",
1003 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001004 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001005 }
1006 if (buffer_len(&c->output) > packet_get_maxsize()) {
1007 debug("channel %d: big output buffer %d",
1008 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001009 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001010 }
Damien Miller95def091999-11-25 00:26:21 +11001011 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 }
Damien Miller95def091999-11-25 00:26:21 +11001013 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014}
1015
Damien Millerb38eff82000-04-01 11:09:21 +10001016void
1017channel_input_ieof(int type, int plen)
1018{
1019 int id;
1020 Channel *c;
1021
1022 packet_integrity_check(plen, 4, type);
1023
1024 id = packet_get_int();
1025 c = channel_lookup(id);
1026 if (c == NULL)
1027 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1028 chan_rcvd_ieof(c);
1029}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Miller95def091999-11-25 00:26:21 +11001031void
Damien Millerb38eff82000-04-01 11:09:21 +10001032channel_input_close(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033{
Damien Millerb38eff82000-04-01 11:09:21 +10001034 int id;
1035 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Millerb38eff82000-04-01 11:09:21 +10001037 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038
Damien Millerb38eff82000-04-01 11:09:21 +10001039 id = packet_get_int();
1040 c = channel_lookup(id);
1041 if (c == NULL)
1042 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001043
1044 /*
1045 * Send a confirmation that we have closed the channel and no more
1046 * data is coming for it.
1047 */
Damien Miller95def091999-11-25 00:26:21 +11001048 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001049 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001050 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051
Damien Miller5428f641999-11-25 11:54:57 +11001052 /*
1053 * If the channel is in closed state, we have sent a close request,
1054 * and the other side will eventually respond with a confirmation.
1055 * Thus, we cannot free the channel here, because then there would be
1056 * no-one to receive the confirmation. The channel gets freed when
1057 * the confirmation arrives.
1058 */
Damien Millerb38eff82000-04-01 11:09:21 +10001059 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001060 /*
1061 * Not a closed channel - mark it as draining, which will
1062 * cause it to be freed later.
1063 */
Damien Millerb38eff82000-04-01 11:09:21 +10001064 buffer_consume(&c->input, buffer_len(&c->input));
1065 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001066 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067}
1068
Damien Millerb38eff82000-04-01 11:09:21 +10001069/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller95def091999-11-25 00:26:21 +11001070void
Damien Millerb38eff82000-04-01 11:09:21 +10001071channel_input_oclose(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072{
Damien Millerb38eff82000-04-01 11:09:21 +10001073 int id = packet_get_int();
1074 Channel *c = channel_lookup(id);
1075 packet_integrity_check(plen, 4, type);
1076 if (c == NULL)
1077 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1078 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079}
1080
Damien Millerb38eff82000-04-01 11:09:21 +10001081void
1082channel_input_close_confirmation(int type, int plen)
1083{
1084 int id = packet_get_int();
1085 Channel *c = channel_lookup(id);
1086
1087 if (c == NULL)
1088 packet_disconnect("Received close confirmation for "
1089 "out-of-range channel %d.", id);
1090 if (c->type != SSH_CHANNEL_CLOSED)
1091 packet_disconnect("Received close confirmation for "
1092 "non-closed channel %d (type %d).", id, c->type);
1093 channel_free(c->self);
1094}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Damien Miller95def091999-11-25 00:26:21 +11001096void
Damien Millerb38eff82000-04-01 11:09:21 +10001097channel_input_open_confirmation(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098{
Damien Millerb38eff82000-04-01 11:09:21 +10001099 int id, remote_id;
1100 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller33b13562000-04-04 14:38:59 +10001102 if (!compat20)
1103 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104
Damien Millerb38eff82000-04-01 11:09:21 +10001105 id = packet_get_int();
1106 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
Damien Millerb38eff82000-04-01 11:09:21 +10001108 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1109 packet_disconnect("Received open confirmation for "
1110 "non-opening channel %d.", id);
1111 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001112 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001113 c->remote_id = remote_id;
1114 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001115
1116 if (compat20) {
1117 c->remote_window = packet_get_int();
1118 c->remote_maxpacket = packet_get_int();
1119 if (c->cb_fn != NULL && c->cb_event == type) {
1120 debug("callback start");
1121 c->cb_fn(c->self, c->cb_arg);
1122 debug("callback done");
1123 }
1124 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1125 c->remote_window, c->remote_maxpacket);
1126 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127}
1128
Damien Miller95def091999-11-25 00:26:21 +11001129void
Damien Millerb38eff82000-04-01 11:09:21 +10001130channel_input_open_failure(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131{
Damien Millerb38eff82000-04-01 11:09:21 +10001132 int id;
1133 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134
Damien Miller33b13562000-04-04 14:38:59 +10001135 if (!compat20)
1136 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001137
1138 id = packet_get_int();
1139 c = channel_lookup(id);
1140
1141 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1142 packet_disconnect("Received open failure for "
1143 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001144 if (compat20) {
1145 int reason = packet_get_int();
1146 char *msg = packet_get_string(NULL);
1147 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
1148 xfree(msg);
1149 }
Damien Miller95def091999-11-25 00:26:21 +11001150 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001151 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152}
1153
Damien Miller33b13562000-04-04 14:38:59 +10001154void
1155channel_input_channel_request(int type, int plen)
1156{
1157 int id;
1158 Channel *c;
1159
1160 id = packet_get_int();
1161 c = channel_lookup(id);
1162
1163 if (c == NULL ||
1164 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1165 packet_disconnect("Received request for "
1166 "non-open channel %d.", id);
1167 if (c->cb_fn != NULL && c->cb_event == type) {
1168 debug("callback start");
1169 c->cb_fn(c->self, c->cb_arg);
1170 debug("callback done");
1171 } else {
1172 char *service = packet_get_string(NULL);
1173 debug("channel: %d rcvd request for %s", c->self, service);
1174debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1175 xfree(service);
1176 }
1177}
1178
1179void
1180channel_input_window_adjust(int type, int plen)
1181{
1182 Channel *c;
1183 int id, adjust;
1184
1185 if (!compat20)
1186 return;
1187
1188 /* Get the channel number and verify it. */
1189 id = packet_get_int();
1190 c = channel_lookup(id);
1191
1192 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1193 log("Received window adjust for "
1194 "non-open channel %d.", id);
1195 return;
1196 }
1197 adjust = packet_get_int();
1198 debug("channel %d: rcvd adjust %d", id, adjust);
1199 c->remote_window += adjust;
1200}
1201
Damien Miller5428f641999-11-25 11:54:57 +11001202/*
1203 * Stops listening for channels, and removes any unix domain sockets that we
1204 * might have.
1205 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
Damien Miller95def091999-11-25 00:26:21 +11001207void
1208channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209{
Damien Miller95def091999-11-25 00:26:21 +11001210 int i;
1211 for (i = 0; i < channels_alloc; i++) {
1212 switch (channels[i].type) {
1213 case SSH_CHANNEL_AUTH_SOCKET:
1214 close(channels[i].sock);
1215 remove(channels[i].path);
1216 channel_free(i);
1217 break;
1218 case SSH_CHANNEL_PORT_LISTENER:
1219 case SSH_CHANNEL_X11_LISTENER:
1220 close(channels[i].sock);
1221 channel_free(i);
1222 break;
1223 default:
1224 break;
1225 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227}
1228
Damien Miller5428f641999-11-25 11:54:57 +11001229/*
1230 * Closes the sockets of all channels. This is used to close extra file
1231 * descriptors after a fork.
1232 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001233
Damien Miller95def091999-11-25 00:26:21 +11001234void
1235channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236{
Damien Miller95def091999-11-25 00:26:21 +11001237 int i;
1238 for (i = 0; i < channels_alloc; i++) {
1239 if (channels[i].type != SSH_CHANNEL_FREE)
1240 close(channels[i].sock);
1241 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
1243
1244/* Returns the maximum file descriptor number used by the channels. */
1245
Damien Miller95def091999-11-25 00:26:21 +11001246int
1247channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248{
Damien Miller95def091999-11-25 00:26:21 +11001249 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250}
1251
1252/* Returns true if any channel is still open. */
1253
Damien Miller95def091999-11-25 00:26:21 +11001254int
1255channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256{
Damien Miller95def091999-11-25 00:26:21 +11001257 unsigned int i;
1258 for (i = 0; i < channels_alloc; i++)
1259 switch (channels[i].type) {
1260 case SSH_CHANNEL_FREE:
1261 case SSH_CHANNEL_X11_LISTENER:
1262 case SSH_CHANNEL_PORT_LISTENER:
1263 case SSH_CHANNEL_CLOSED:
1264 case SSH_CHANNEL_AUTH_SOCKET:
1265 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001266 case SSH_CHANNEL_LARVAL:
1267 if (!compat20)
1268 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1269 continue;
Damien Miller95def091999-11-25 00:26:21 +11001270 case SSH_CHANNEL_OPENING:
1271 case SSH_CHANNEL_OPEN:
1272 case SSH_CHANNEL_X11_OPEN:
1273 return 1;
1274 case SSH_CHANNEL_INPUT_DRAINING:
1275 case SSH_CHANNEL_OUTPUT_DRAINING:
1276 if (!compat13)
1277 fatal("cannot happen: OUT_DRAIN");
1278 return 1;
1279 default:
1280 fatal("channel_still_open: bad channel type %d", channels[i].type);
1281 /* NOTREACHED */
1282 }
1283 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284}
1285
Damien Miller5428f641999-11-25 11:54:57 +11001286/*
1287 * Returns a message describing the currently open forwarded connections,
1288 * suitable for sending to the client. The message contains crlf pairs for
1289 * newlines.
1290 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291
Damien Miller95def091999-11-25 00:26:21 +11001292char *
1293channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294{
Damien Miller95def091999-11-25 00:26:21 +11001295 Buffer buffer;
1296 int i;
1297 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298
Damien Miller95def091999-11-25 00:26:21 +11001299 buffer_init(&buffer);
1300 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001302 for (i = 0; i < channels_alloc; i++) {
1303 Channel *c = &channels[i];
1304 switch (c->type) {
1305 case SSH_CHANNEL_FREE:
1306 case SSH_CHANNEL_X11_LISTENER:
1307 case SSH_CHANNEL_PORT_LISTENER:
1308 case SSH_CHANNEL_CLOSED:
1309 case SSH_CHANNEL_AUTH_SOCKET:
1310 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001311 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001312 case SSH_CHANNEL_OPENING:
1313 case SSH_CHANNEL_OPEN:
1314 case SSH_CHANNEL_X11_OPEN:
1315 case SSH_CHANNEL_INPUT_DRAINING:
1316 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001317 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 +11001318 c->self, c->remote_name,
1319 c->type, c->remote_id,
1320 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001321 c->ostate, buffer_len(&c->output),
1322 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001323 buffer_append(&buffer, buf, strlen(buf));
1324 continue;
1325 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001326 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001327 /* NOTREACHED */
1328 }
1329 }
1330 buffer_append(&buffer, "\0", 1);
1331 cp = xstrdup(buffer_ptr(&buffer));
1332 buffer_free(&buffer);
1333 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334}
1335
Damien Miller5428f641999-11-25 11:54:57 +11001336/*
1337 * Initiate forwarding of connections to local port "port" through the secure
1338 * channel to host:port from remote side.
1339 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340
Damien Miller95def091999-11-25 00:26:21 +11001341void
Damien Milleraae6c611999-12-06 11:47:28 +11001342channel_request_local_forwarding(u_short port, const char *host,
Damien Millera34a28b1999-12-14 10:47:15 +11001343 u_short host_port, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344{
Damien Miller34132e52000-01-14 15:45:46 +11001345 int success, ch, sock, on = 1;
1346 struct addrinfo hints, *ai, *aitop;
1347 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller5428f641999-11-25 11:54:57 +11001348 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349
Damien Miller95def091999-11-25 00:26:21 +11001350 if (strlen(host) > sizeof(channels[0].path) - 1)
1351 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352
Damien Miller5428f641999-11-25 11:54:57 +11001353 /*
Damien Miller34132e52000-01-14 15:45:46 +11001354 * getaddrinfo returns a loopback address if the hostname is
1355 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001356 */
Damien Miller34132e52000-01-14 15:45:46 +11001357 memset(&hints, 0, sizeof(hints));
1358 hints.ai_family = IPv4or6;
1359 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1360 hints.ai_socktype = SOCK_STREAM;
1361 snprintf(strport, sizeof strport, "%d", port);
1362 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1363 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001364
Damien Miller34132e52000-01-14 15:45:46 +11001365 success = 0;
1366 for (ai = aitop; ai; ai = ai->ai_next) {
1367 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1368 continue;
1369 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1370 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1371 error("channel_request_local_forwarding: getnameinfo failed");
1372 continue;
1373 }
1374 /* Create a port to listen for the host. */
1375 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1376 if (sock < 0) {
1377 /* this is no error since kernel may not support ipv6 */
1378 verbose("socket: %.100s", strerror(errno));
1379 continue;
1380 }
1381 /*
1382 * Set socket options. We would like the socket to disappear
1383 * as soon as it has been closed for whatever reason.
1384 */
1385 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1386 linger.l_onoff = 1;
1387 linger.l_linger = 5;
1388 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1389 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001390
Damien Miller34132e52000-01-14 15:45:46 +11001391 /* Bind the socket to the address. */
1392 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1393 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001394 if (!ai->ai_next)
1395 error("bind: %.100s", strerror(errno));
1396 else
1397 verbose("bind: %.100s", strerror(errno));
1398
Damien Miller34132e52000-01-14 15:45:46 +11001399 close(sock);
1400 continue;
1401 }
1402 /* Start listening for connections on the socket. */
1403 if (listen(sock, 5) < 0) {
1404 error("listen: %.100s", strerror(errno));
1405 close(sock);
1406 continue;
1407 }
1408 /* Allocate a channel number for the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001409 ch = channel_new(
1410 "port listener", SSH_CHANNEL_PORT_LISTENER,
1411 sock, sock, -1,
1412 CHAN_WINDOW_DEFAULT, CHAN_PACKET_DEFAULT,
1413 0, xstrdup("port listener"));
Damien Miller34132e52000-01-14 15:45:46 +11001414 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1415 channels[ch].host_port = host_port;
1416 channels[ch].listening_port = port;
1417 success = 1;
1418 }
1419 if (success == 0)
1420 packet_disconnect("cannot listen port: %d", port);
1421 freeaddrinfo(aitop);
Damien Miller95def091999-11-25 00:26:21 +11001422}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Damien Miller5428f641999-11-25 11:54:57 +11001424/*
1425 * Initiate forwarding of connections to port "port" on remote host through
1426 * the secure channel to host:port from local side.
1427 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428
Damien Miller95def091999-11-25 00:26:21 +11001429void
Damien Millerb38eff82000-04-01 11:09:21 +10001430channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1431 u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432{
Damien Miller95def091999-11-25 00:26:21 +11001433 int payload_len;
1434 /* Record locally that connection to this host/port is permitted. */
1435 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1436 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001437
Damien Millerb38eff82000-04-01 11:09:21 +10001438 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1439 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1440 permitted_opens[num_permitted_opens].listen_port = listen_port;
Damien Miller95def091999-11-25 00:26:21 +11001441 num_permitted_opens++;
1442
1443 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001444 if (compat20) {
1445 const char *address_to_bind = "0.0.0.0";
1446 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1447 packet_put_cstring("tcpip-forward");
1448 packet_put_char(0); /* boolean: want reply */
1449 packet_put_cstring(address_to_bind);
1450 packet_put_int(listen_port);
1451 } else {
1452 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1453 packet_put_int(port_to_connect);
1454 packet_put_cstring(host_to_connect);
1455 packet_put_int(listen_port);
1456 packet_send();
1457 packet_write_wait();
1458 /*
1459 * Wait for response from the remote side. It will send a disconnect
1460 * message on failure, and we will never see it here.
1461 */
1462 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1463 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001464}
1465
Damien Miller5428f641999-11-25 11:54:57 +11001466/*
1467 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1468 * listening for the port, and sends back a success reply (or disconnect
1469 * message if there was an error). This never returns if there was an error.
1470 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001471
Damien Miller95def091999-11-25 00:26:21 +11001472void
1473channel_input_port_forward_request(int is_root)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474{
Damien Milleraae6c611999-12-06 11:47:28 +11001475 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001476 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001477
Damien Miller95def091999-11-25 00:26:21 +11001478 /* Get arguments from the packet. */
1479 port = packet_get_int();
1480 hostname = packet_get_string(NULL);
1481 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482
Damien Miller5428f641999-11-25 11:54:57 +11001483 /*
1484 * Check that an unprivileged user is not trying to forward a
1485 * privileged port.
1486 */
Damien Miller95def091999-11-25 00:26:21 +11001487 if (port < IPPORT_RESERVED && !is_root)
1488 packet_disconnect("Requested forwarding of port %d but user is not root.",
1489 port);
Damien Millera34a28b1999-12-14 10:47:15 +11001490 /*
1491 * Initiate forwarding,
1492 * bind port to localhost only (gateway ports == 0).
1493 */
1494 channel_request_local_forwarding(port, hostname, host_port, 0);
Damien Miller95def091999-11-25 00:26:21 +11001495
1496 /* Free the argument string. */
1497 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498}
1499
Damien Millerb38eff82000-04-01 11:09:21 +10001500/* XXX move to aux.c */
1501int
1502channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503{
Damien Miller34132e52000-01-14 15:45:46 +11001504 struct addrinfo hints, *ai, *aitop;
1505 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1506 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001507 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001508
Damien Miller34132e52000-01-14 15:45:46 +11001509 memset(&hints, 0, sizeof(hints));
1510 hints.ai_family = IPv4or6;
1511 hints.ai_socktype = SOCK_STREAM;
1512 snprintf(strport, sizeof strport, "%d", host_port);
1513 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1514 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001515 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516 }
Damien Miller34132e52000-01-14 15:45:46 +11001517 for (ai = aitop; ai; ai = ai->ai_next) {
1518 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1519 continue;
1520 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1521 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001522 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001523 continue;
1524 }
1525 /* Create the socket. */
1526 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1527 if (sock < 0) {
1528 error("socket: %.100s", strerror(errno));
1529 continue;
1530 }
1531 /* Connect to the host/port. */
1532 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1533 error("connect %.100s port %s: %.100s", ntop, strport,
1534 strerror(errno));
1535 close(sock);
1536 continue; /* fail -- try next */
1537 }
1538 break; /* success */
1539
1540 }
1541 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001542 if (!ai) {
1543 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001544 return -1;
1545 }
1546 /* success */
1547 return sock;
1548}
1549
1550/*
1551 * This is called after receiving PORT_OPEN message. This attempts to
1552 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1553 * or CHANNEL_OPEN_FAILURE.
1554 */
1555
1556void
1557channel_input_port_open(int type, int plen)
1558{
1559 u_short host_port;
1560 char *host, *originator_string;
1561 int remote_channel, sock = -1, newch, i, denied;
1562 unsigned int host_len, originator_len;
1563
1564 /* Get remote channel number. */
1565 remote_channel = packet_get_int();
1566
1567 /* Get host name to connect to. */
1568 host = packet_get_string(&host_len);
1569
1570 /* Get port to connect to. */
1571 host_port = packet_get_int();
1572
1573 /* Get remote originator name. */
1574 if (have_hostname_in_open) {
1575 originator_string = packet_get_string(&originator_len);
1576 originator_len += 4; /* size of packet_int */
1577 } else {
1578 originator_string = xstrdup("unknown (remote did not supply name)");
1579 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001580 }
Damien Miller34132e52000-01-14 15:45:46 +11001581
Damien Millerb38eff82000-04-01 11:09:21 +10001582 packet_integrity_check(plen,
1583 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001584
Damien Millerb38eff82000-04-01 11:09:21 +10001585 /* Check if opening that port is permitted. */
1586 denied = 0;
1587 if (!all_opens_permitted) {
1588 /* Go trough all permitted ports. */
1589 for (i = 0; i < num_permitted_opens; i++)
1590 if (permitted_opens[i].port_to_connect == host_port &&
1591 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1592 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593
Damien Millerb38eff82000-04-01 11:09:21 +10001594 /* Check if we found the requested port among those permitted. */
1595 if (i >= num_permitted_opens) {
1596 /* The port is not permitted. */
1597 log("Received request to connect to %.100s:%d, but the request was denied.",
1598 host, host_port);
1599 denied = 1;
1600 }
1601 }
1602 sock = denied ? -1 : channel_connect_to(host, host_port);
1603 if (sock > 0) {
1604 /* Allocate a channel for this connection. */
1605 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1606 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607
Damien Millerb38eff82000-04-01 11:09:21 +10001608 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1609 packet_put_int(remote_channel);
1610 packet_put_int(newch);
1611 packet_send();
1612 } else {
1613 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1614 packet_put_int(remote_channel);
1615 packet_send();
1616 }
Damien Miller95def091999-11-25 00:26:21 +11001617 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001618}
1619
Damien Miller5428f641999-11-25 11:54:57 +11001620/*
1621 * Creates an internet domain socket for listening for X11 connections.
1622 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1623 * occurs.
1624 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625
Damien Miller34132e52000-01-14 15:45:46 +11001626#define NUM_SOCKS 10
1627
Damien Miller95def091999-11-25 00:26:21 +11001628char *
Damien Millera34a28b1999-12-14 10:47:15 +11001629x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001630{
Damien Milleraae6c611999-12-06 11:47:28 +11001631 int display_number, sock;
1632 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001633 struct addrinfo hints, *ai, *aitop;
1634 char strport[NI_MAXSERV];
1635 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1636 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001637 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638
Damien Millera34a28b1999-12-14 10:47:15 +11001639 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001640 display_number < MAX_DISPLAYS;
1641 display_number++) {
1642 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001643 memset(&hints, 0, sizeof(hints));
1644 hints.ai_family = IPv4or6;
1645 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1646 hints.ai_socktype = SOCK_STREAM;
1647 snprintf(strport, sizeof strport, "%d", port);
1648 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1649 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001650 return NULL;
1651 }
Damien Miller34132e52000-01-14 15:45:46 +11001652 for (ai = aitop; ai; ai = ai->ai_next) {
1653 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1654 continue;
1655 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1656 if (sock < 0) {
Damien Millere2192732000-01-17 13:22:55 +11001657 if (errno != EINVAL) {
1658 error("socket: %.100s", strerror(errno));
1659 return NULL;
1660 } else {
1661 debug("Socket family %d not supported [X11 disp create]", ai->ai_family);
1662 continue;
1663 }
Damien Miller34132e52000-01-14 15:45:46 +11001664 }
1665 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1666 debug("bind port %d: %.100s", port, strerror(errno));
1667 shutdown(sock, SHUT_RDWR);
1668 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001669
1670 if (ai->ai_next)
1671 continue;
1672
Damien Miller34132e52000-01-14 15:45:46 +11001673 for (n = 0; n < num_socks; n++) {
1674 shutdown(socks[n], SHUT_RDWR);
1675 close(socks[n]);
1676 }
1677 num_socks = 0;
1678 break;
1679 }
1680 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001681#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001682 if (num_socks == NUM_SOCKS)
1683 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001684#else
1685 break;
1686#endif
Damien Miller95def091999-11-25 00:26:21 +11001687 }
Damien Miller34132e52000-01-14 15:45:46 +11001688 if (num_socks > 0)
1689 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690 }
Damien Miller95def091999-11-25 00:26:21 +11001691 if (display_number >= MAX_DISPLAYS) {
1692 error("Failed to allocate internet-domain X11 display socket.");
1693 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694 }
Damien Miller95def091999-11-25 00:26:21 +11001695 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001696 for (n = 0; n < num_socks; n++) {
1697 sock = socks[n];
1698 if (listen(sock, 5) < 0) {
1699 error("listen: %.100s", strerror(errno));
1700 shutdown(sock, SHUT_RDWR);
1701 close(sock);
1702 return NULL;
1703 }
Damien Miller95def091999-11-25 00:26:21 +11001704 }
Damien Miller34132e52000-01-14 15:45:46 +11001705
Damien Miller95def091999-11-25 00:26:21 +11001706 /* Set up a suitable value for the DISPLAY variable. */
Damien Miller76112de1999-12-21 11:18:08 +11001707
Damien Miller95def091999-11-25 00:26:21 +11001708 if (gethostname(hostname, sizeof(hostname)) < 0)
1709 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001710
1711#ifdef IPADDR_IN_DISPLAY
1712 /*
1713 * HPUX detects the local hostname in the DISPLAY variable and tries
1714 * to set up a shared memory connection to the server, which it
1715 * incorrectly supposes to be local.
1716 *
1717 * The workaround - as used in later $$H and other programs - is
1718 * is to set display to the host's IP address.
1719 */
1720 {
1721 struct hostent *he;
1722 struct in_addr my_addr;
1723
1724 he = gethostbyname(hostname);
1725 if (he == NULL) {
1726 error("[X11-broken-fwd-hostname-workaround] Could not get "
1727 "IP address for hostname %s.", hostname);
1728
1729 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1730 "Could not get IP address for hostname %s.", hostname);
1731
1732 shutdown(sock, SHUT_RDWR);
1733 close(sock);
1734
1735 return NULL;
1736 }
1737
1738 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1739
1740 /* Set DISPLAY to <ip address>:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001741 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Damien Miller76112de1999-12-21 11:18:08 +11001742 display_number, screen_number);
1743 }
1744#else /* IPADDR_IN_DISPLAY */
1745 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001746 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Damien Miller76112de1999-12-21 11:18:08 +11001747 display_number, screen_number);
1748#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001749
Damien Miller34132e52000-01-14 15:45:46 +11001750 /* Allocate a channel for each socket. */
1751 for (n = 0; n < num_socks; n++) {
1752 sock = socks[n];
1753 (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
1754 xstrdup("X11 inet listener"));
1755 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001756
Damien Miller95def091999-11-25 00:26:21 +11001757 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11001758 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001759}
1760
1761#ifndef X_UNIX_PATH
1762#define X_UNIX_PATH "/tmp/.X11-unix/X"
1763#endif
1764
1765static
1766int
Damien Miller81b25cf1999-12-07 16:47:28 +11001767connect_local_xsocket(unsigned int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001768{
Damien Miller95def091999-11-25 00:26:21 +11001769 static const char *const x_sockets[] = {
1770 X_UNIX_PATH "%u",
1771 "/var/X/.X11-unix/X" "%u",
1772 "/usr/spool/sockets/X11/" "%u",
1773 NULL
1774 };
1775 int sock;
1776 struct sockaddr_un addr;
1777 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001778
Damien Miller95def091999-11-25 00:26:21 +11001779 for (path = x_sockets; *path; ++path) {
1780 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1781 if (sock < 0)
1782 error("socket: %.100s", strerror(errno));
1783 memset(&addr, 0, sizeof(addr));
1784 addr.sun_family = AF_UNIX;
1785 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1786 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1787 return sock;
1788 close(sock);
1789 }
1790 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1791 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792}
1793
1794
Damien Miller5428f641999-11-25 11:54:57 +11001795/*
1796 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1797 * the remote channel number. We should do whatever we want, and respond
1798 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1799 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001800
Damien Miller95def091999-11-25 00:26:21 +11001801void
Damien Millerb38eff82000-04-01 11:09:21 +10001802x11_input_open(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001803{
Damien Miller34132e52000-01-14 15:45:46 +11001804 int remote_channel, display_number, sock = 0, newch;
Damien Miller95def091999-11-25 00:26:21 +11001805 const char *display;
Damien Miller95def091999-11-25 00:26:21 +11001806 char buf[1024], *cp, *remote_host;
Damien Miller7684ee12000-03-17 23:40:15 +11001807 unsigned int remote_len;
Damien Miller34132e52000-01-14 15:45:46 +11001808 struct addrinfo hints, *ai, *aitop;
1809 char strport[NI_MAXSERV];
1810 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Miller95def091999-11-25 00:26:21 +11001812 /* Get remote channel number. */
1813 remote_channel = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001814
Damien Miller95def091999-11-25 00:26:21 +11001815 /* Get remote originator name. */
Damien Miller5428f641999-11-25 11:54:57 +11001816 if (have_hostname_in_open) {
Damien Miller95def091999-11-25 00:26:21 +11001817 remote_host = packet_get_string(&remote_len);
Damien Miller5428f641999-11-25 11:54:57 +11001818 remote_len += 4;
1819 } else {
Damien Miller95def091999-11-25 00:26:21 +11001820 remote_host = xstrdup("unknown (remote did not supply name)");
Damien Miller5428f641999-11-25 11:54:57 +11001821 remote_len = 0;
1822 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823
Damien Miller95def091999-11-25 00:26:21 +11001824 debug("Received X11 open request.");
Damien Millerb38eff82000-04-01 11:09:21 +10001825 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001826
Damien Miller95def091999-11-25 00:26:21 +11001827 /* Try to open a socket for the local X server. */
1828 display = getenv("DISPLAY");
1829 if (!display) {
1830 error("DISPLAY not set.");
1831 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832 }
Damien Miller5428f641999-11-25 11:54:57 +11001833 /*
1834 * Now we decode the value of the DISPLAY variable and make a
1835 * connection to the real X server.
1836 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001837
Damien Miller5428f641999-11-25 11:54:57 +11001838 /*
1839 * Check if it is a unix domain socket. Unix domain displays are in
1840 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1841 */
Damien Miller95def091999-11-25 00:26:21 +11001842 if (strncmp(display, "unix:", 5) == 0 ||
1843 display[0] == ':') {
1844 /* Connect to the unix domain socket. */
1845 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1846 error("Could not parse display number from DISPLAY: %.100s",
1847 display);
1848 goto fail;
1849 }
1850 /* Create a socket. */
1851 sock = connect_local_xsocket(display_number);
1852 if (sock < 0)
1853 goto fail;
1854
1855 /* OK, we now have a connection to the display. */
1856 goto success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001857 }
Damien Miller5428f641999-11-25 11:54:57 +11001858 /*
1859 * Connect to an inet socket. The DISPLAY value is supposedly
1860 * hostname:d[.s], where hostname may also be numeric IP address.
1861 */
Damien Miller95def091999-11-25 00:26:21 +11001862 strncpy(buf, display, sizeof(buf));
1863 buf[sizeof(buf) - 1] = 0;
1864 cp = strchr(buf, ':');
1865 if (!cp) {
1866 error("Could not find ':' in DISPLAY: %.100s", display);
1867 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001868 }
Damien Miller95def091999-11-25 00:26:21 +11001869 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001870 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11001871 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1872 error("Could not parse display number from DISPLAY: %.100s",
1873 display);
1874 goto fail;
1875 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001876
Damien Miller34132e52000-01-14 15:45:46 +11001877 /* Look up the host address */
1878 memset(&hints, 0, sizeof(hints));
1879 hints.ai_family = IPv4or6;
1880 hints.ai_socktype = SOCK_STREAM;
1881 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1882 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1883 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001884 goto fail;
1885 }
Damien Miller34132e52000-01-14 15:45:46 +11001886 for (ai = aitop; ai; ai = ai->ai_next) {
1887 /* Create a socket. */
1888 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1889 if (sock < 0) {
1890 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001891 continue;
1892 }
1893 /* Connect it to the display. */
1894 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1895 debug("connect %.100s port %d: %.100s", buf,
1896 6000 + display_number, strerror(errno));
1897 close(sock);
1898 continue;
1899 }
1900 /* Success */
1901 break;
Damien Miller34132e52000-01-14 15:45:46 +11001902 }
Damien Miller34132e52000-01-14 15:45:46 +11001903 freeaddrinfo(aitop);
1904 if (!ai) {
1905 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1906 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001907 goto fail;
1908 }
1909success:
1910 /* We have successfully obtained a connection to the real X display. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001911
Damien Miller95def091999-11-25 00:26:21 +11001912 /* Allocate a channel for this connection. */
1913 if (x11_saved_proto == NULL)
1914 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
1915 else
1916 newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
1917 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918
Damien Miller95def091999-11-25 00:26:21 +11001919 /* Send a confirmation to the remote host. */
1920 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1921 packet_put_int(remote_channel);
1922 packet_put_int(newch);
1923 packet_send();
1924
1925 return;
1926
1927fail:
1928 /* Send refusal to the remote host. */
1929 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1930 packet_put_int(remote_channel);
1931 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001932}
1933
Damien Miller5428f641999-11-25 11:54:57 +11001934/*
1935 * Requests forwarding of X11 connections, generates fake authentication
1936 * data, and enables authentication spoofing.
1937 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938
Damien Miller95def091999-11-25 00:26:21 +11001939void
1940x11_request_forwarding_with_spoofing(const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941{
Damien Miller95def091999-11-25 00:26:21 +11001942 unsigned int data_len = (unsigned int) strlen(data) / 2;
1943 unsigned int i, value;
1944 char *new_data;
1945 int screen_number;
1946 const char *cp;
1947 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001948
Damien Miller95def091999-11-25 00:26:21 +11001949 cp = getenv("DISPLAY");
1950 if (cp)
1951 cp = strchr(cp, ':');
1952 if (cp)
1953 cp = strchr(cp, '.');
1954 if (cp)
1955 screen_number = atoi(cp + 1);
1956 else
1957 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Damien Miller95def091999-11-25 00:26:21 +11001959 /* Save protocol name. */
1960 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001961
Damien Miller5428f641999-11-25 11:54:57 +11001962 /*
1963 * Extract real authentication data and generate fake data of the
1964 * same length.
1965 */
Damien Miller95def091999-11-25 00:26:21 +11001966 x11_saved_data = xmalloc(data_len);
1967 x11_fake_data = xmalloc(data_len);
1968 for (i = 0; i < data_len; i++) {
1969 if (sscanf(data + 2 * i, "%2x", &value) != 1)
1970 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
1971 if (i % 4 == 0)
1972 rand = arc4random();
1973 x11_saved_data[i] = value;
1974 x11_fake_data[i] = rand & 0xff;
1975 rand >>= 8;
1976 }
1977 x11_saved_data_len = data_len;
1978 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001979
Damien Miller95def091999-11-25 00:26:21 +11001980 /* Convert the fake data into hex. */
1981 new_data = xmalloc(2 * data_len + 1);
1982 for (i = 0; i < data_len; i++)
1983 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001984
Damien Miller95def091999-11-25 00:26:21 +11001985 /* Send the request packet. */
1986 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
1987 packet_put_string(proto, strlen(proto));
1988 packet_put_string(new_data, strlen(new_data));
1989 packet_put_int(screen_number);
1990 packet_send();
1991 packet_write_wait();
1992 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001993}
1994
1995/* Sends a message to the server to request authentication fd forwarding. */
1996
Damien Miller95def091999-11-25 00:26:21 +11001997void
1998auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999{
Damien Miller95def091999-11-25 00:26:21 +11002000 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2001 packet_send();
2002 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002003}
2004
Damien Miller5428f641999-11-25 11:54:57 +11002005/*
2006 * Returns the name of the forwarded authentication socket. Returns NULL if
2007 * there is no forwarded authentication socket. The returned value points to
2008 * a static buffer.
2009 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002010
Damien Miller95def091999-11-25 00:26:21 +11002011char *
2012auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002013{
Damien Miller95def091999-11-25 00:26:21 +11002014 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015}
2016
2017/* removes the agent forwarding socket */
2018
Damien Miller95def091999-11-25 00:26:21 +11002019void
2020cleanup_socket(void)
2021{
2022 remove(channel_forwarded_auth_socket_name);
2023 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024}
2025
Damien Miller5428f641999-11-25 11:54:57 +11002026/*
2027 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
2028 * This starts forwarding authentication requests.
2029 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030
Damien Miller95def091999-11-25 00:26:21 +11002031void
2032auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002033{
Damien Miller95def091999-11-25 00:26:21 +11002034 int sock, newch;
2035 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002036
Damien Miller95def091999-11-25 00:26:21 +11002037 if (auth_get_socket_name() != NULL)
2038 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002039
Damien Miller95def091999-11-25 00:26:21 +11002040 /* Temporarily drop privileged uid for mkdir/bind. */
2041 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002042
Damien Miller95def091999-11-25 00:26:21 +11002043 /* Allocate a buffer for the socket name, and format the name. */
2044 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2045 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2046 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002047
Damien Miller95def091999-11-25 00:26:21 +11002048 /* Create private directory for socket */
2049 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
2050 packet_disconnect("mkdtemp: %.100s", strerror(errno));
2051 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2052 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002053
Damien Miller95def091999-11-25 00:26:21 +11002054 if (atexit(cleanup_socket) < 0) {
2055 int saved = errno;
2056 cleanup_socket();
2057 packet_disconnect("socket: %.100s", strerror(saved));
2058 }
2059 /* Create the socket. */
2060 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2061 if (sock < 0)
2062 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002063
Damien Miller95def091999-11-25 00:26:21 +11002064 /* Bind it to the name. */
2065 memset(&sunaddr, 0, sizeof(sunaddr));
2066 sunaddr.sun_family = AF_UNIX;
2067 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2068 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002069
Damien Miller95def091999-11-25 00:26:21 +11002070 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2071 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002072
Damien Miller95def091999-11-25 00:26:21 +11002073 /* Restore the privileged uid. */
2074 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002075
Damien Miller95def091999-11-25 00:26:21 +11002076 /* Start listening on the socket. */
2077 if (listen(sock, 5) < 0)
2078 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002079
Damien Miller95def091999-11-25 00:26:21 +11002080 /* Allocate a channel for the authentication agent socket. */
2081 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
2082 xstrdup("auth socket"));
Damien Miller037a0dc1999-12-07 15:38:31 +11002083 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2084 sizeof(channels[newch].path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002085}
2086
2087/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2088
Damien Miller95def091999-11-25 00:26:21 +11002089void
Damien Millerb38eff82000-04-01 11:09:21 +10002090auth_input_open_request(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091{
Damien Miller95def091999-11-25 00:26:21 +11002092 int remch, sock, newch;
2093 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094
Damien Millerb38eff82000-04-01 11:09:21 +10002095 packet_integrity_check(plen, 4, type);
2096
Damien Miller95def091999-11-25 00:26:21 +11002097 /* Read the remote channel number from the message. */
2098 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099
Damien Miller5428f641999-11-25 11:54:57 +11002100 /*
2101 * Get a connection to the local authentication agent (this may again
2102 * get forwarded).
2103 */
Damien Miller95def091999-11-25 00:26:21 +11002104 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002105
Damien Miller5428f641999-11-25 11:54:57 +11002106 /*
2107 * If we could not connect the agent, send an error message back to
2108 * the server. This should never happen unless the agent dies,
2109 * because authentication forwarding is only enabled if we have an
2110 * agent.
2111 */
Damien Miller95def091999-11-25 00:26:21 +11002112 if (sock < 0) {
2113 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2114 packet_put_int(remch);
2115 packet_send();
2116 return;
2117 }
2118 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119
Damien Miller5428f641999-11-25 11:54:57 +11002120 /*
2121 * Dummy host name. This will be freed when the channel is freed; it
2122 * will still be valid in the packet_put_string below since the
2123 * channel cannot yet be freed at that point.
2124 */
Damien Miller95def091999-11-25 00:26:21 +11002125 dummyname = xstrdup("authentication agent connection");
2126
2127 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2128 channels[newch].remote_id = remch;
2129
2130 /* Send a confirmation to the remote host. */
2131 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2132 packet_put_int(remch);
2133 packet_put_int(newch);
2134 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002135}
Damien Miller33b13562000-04-04 14:38:59 +10002136
2137void
2138channel_open(int id)
2139{
2140 Channel *c = channel_lookup(id);
2141 if (c == NULL) {
2142 log("channel_open: %d: bad id", id);
2143 return;
2144 }
2145 packet_start(SSH2_MSG_CHANNEL_OPEN);
2146 packet_put_cstring(c->ctype);
2147 packet_put_int(c->self);
2148 packet_put_int(c->local_window);
2149 packet_put_int(c->local_maxpacket);
2150 packet_send();
2151 debug("channel open %d", id);
2152}
2153void
2154channel_request(int id, char *service, int wantconfirm)
2155{
2156 channel_request_start(id, service, wantconfirm);
2157 packet_send();
2158 debug("channel request %d: %s", id, service) ;
2159}
2160void
2161channel_request_start(int id, char *service, int wantconfirm)
2162{
2163 Channel *c = channel_lookup(id);
2164 if (c == NULL) {
2165 log("channel_request: %d: bad id", id);
2166 return;
2167 }
2168 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2169 packet_put_int(c->remote_id);
2170 packet_put_cstring(service);
2171 packet_put_char(wantconfirm);
2172}
2173void
2174channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2175{
2176 Channel *c = channel_lookup(id);
2177 if (c == NULL) {
2178 log("channel_register_callback: %d: bad id", id);
2179 return;
2180 }
2181 c->cb_event = mtype;
2182 c->cb_fn = fn;
2183 c->cb_arg = arg;
2184}
2185void
2186channel_register_cleanup(int id, channel_callback_fn *fn)
2187{
2188 Channel *c = channel_lookup(id);
2189 if (c == NULL) {
2190 log("channel_register_cleanup: %d: bad id", id);
2191 return;
2192 }
2193 c->dettach_user = fn;
2194}
2195void
2196channel_cancel_cleanup(int id)
2197{
2198 Channel *c = channel_lookup(id);
2199 if (c == NULL) {
2200 log("channel_cancel_cleanup: %d: bad id", id);
2201 return;
2202 }
2203 c->dettach_user = NULL;
2204}
2205
2206void
2207channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
2208{
2209 Channel *c = channel_lookup(id);
2210 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2211 fatal("channel_activate for non-larval channel %d.", id);
2212 if (rfd > channel_max_fd_value)
2213 channel_max_fd_value = rfd;
2214 if (wfd > channel_max_fd_value)
2215 channel_max_fd_value = wfd;
2216 if (efd > channel_max_fd_value)
2217 channel_max_fd_value = efd;
2218 c->type = SSH_CHANNEL_OPEN;
2219 c->rfd = rfd;
2220 c->wfd = wfd;
2221 c->efd = efd;
2222 c->extended_usage = extusage;
2223 /* XXX window size? */
2224 c->local_window = c->local_window_max = c->local_maxpacket/2;
2225 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2226 packet_put_int(c->remote_id);
2227 packet_put_int(c->local_window);
2228 packet_send();
2229}