blob: 61ba76dec347f39bfbf5cb4b0d6bc95961f8b8df [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 *
16 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +110019RCSID("$Id: channels.c,v 1.8 1999/11/25 00:54:58 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "ssh.h"
22#include "packet.h"
23#include "xmalloc.h"
24#include "buffer.h"
25#include "authfd.h"
26#include "uidswap.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110027#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028#include "servconf.h"
29
30#include "channels.h"
31#include "nchan.h"
32#include "compat.h"
33
34/* Maximum number of fake X11 displays to try. */
35#define MAX_DISPLAYS 1000
36
37/* Max len of agent socket */
38#define MAX_SOCKET_NAME 100
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Pointer to an array containing all allocated channels. The array is
42 * dynamically extended as needed.
43 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044static Channel *channels = NULL;
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * Size of the channel array. All slots of the array must always be
48 * initialized (at least the type field); unused slots are marked with type
49 * SSH_CHANNEL_FREE.
50 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051static int channels_alloc = 0;
52
Damien Miller5428f641999-11-25 11:54:57 +110053/*
54 * Maximum file descriptor value used in any of the channels. This is
55 * updated in channel_allocate.
56 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057static int channel_max_fd_value = 0;
58
59/* Name and directory of socket for authentication agent forwarding. */
60static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110061static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63/* Saved X11 authentication protocol name. */
64char *x11_saved_proto = NULL;
65
66/* Saved X11 authentication data. This is the real data. */
67char *x11_saved_data = NULL;
68unsigned int x11_saved_data_len = 0;
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * Fake X11 authentication data. This is what the server will be sending us;
72 * we should replace any occurrences of this by the real data.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074char *x11_fake_data = NULL;
75unsigned int x11_fake_data_len;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Data structure for storing which hosts are permitted for forward requests.
79 * The local sides of any remote forwards are stored in this array to prevent
80 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
81 * network (which might be behind a firewall).
82 */
Damien Miller95def091999-11-25 00:26:21 +110083typedef struct {
84 char *host; /* Host name. */
85 int port; /* Port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086} ForwardPermission;
87
88/* List of all permitted host/port pairs to connect. */
89static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
90/* Number of permitted host/port pairs in the array. */
91static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +110092/*
93 * If this is true, all opens are permitted. This is the case on the server
94 * on which we have to trust the client anyway, and the user could do
95 * anything after logging in anyway.
96 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097static int all_opens_permitted = 0;
98
99/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
100static int have_hostname_in_open = 0;
101
102/* Sets specific protocol options. */
103
Damien Miller95def091999-11-25 00:26:21 +1100104void
105channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106{
Damien Miller95def091999-11-25 00:26:21 +1100107 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108}
109
Damien Miller5428f641999-11-25 11:54:57 +1100110/*
111 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
112 * called by the server, because the user could connect to any port anyway,
113 * and the server has no way to know but to trust the client anyway.
114 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116void
117channel_permit_all_opens()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118{
Damien Miller95def091999-11-25 00:26:21 +1100119 all_opens_permitted = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120}
121
Damien Miller5428f641999-11-25 11:54:57 +1100122/*
123 * Allocate a new channel object and set its type and socket. This will cause
124 * remote_name to be freed.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127int
128channel_allocate(int type, int sock, char *remote_name)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Miller95def091999-11-25 00:26:21 +1100130 int i, found;
131 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 /* Update the maximum file descriptor value. */
134 if (sock > channel_max_fd_value)
135 channel_max_fd_value = sock;
Damien Miller5428f641999-11-25 11:54:57 +1100136 /* XXX set close-on-exec -markus */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138 /* Do initial allocation if this is the first call. */
139 if (channels_alloc == 0) {
140 channels_alloc = 10;
141 channels = xmalloc(channels_alloc * sizeof(Channel));
142 for (i = 0; i < channels_alloc; i++)
143 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100144 /*
145 * Kludge: arrange a call to channel_stop_listening if we
146 * terminate with fatal().
147 */
Damien Miller95def091999-11-25 00:26:21 +1100148 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
149 }
150 /* Try to find a free slot where to put the new channel. */
151 for (found = -1, i = 0; i < channels_alloc; i++)
152 if (channels[i].type == SSH_CHANNEL_FREE) {
153 /* Found a free slot. */
154 found = i;
155 break;
156 }
157 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100158 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100159 found = channels_alloc;
160 channels_alloc += 10;
161 debug("channel: expanding %d", channels_alloc);
162 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
163 for (i = found; i < channels_alloc; i++)
164 channels[i].type = SSH_CHANNEL_FREE;
165 }
166 /* Initialize and return new channel number. */
167 c = &channels[found];
168 buffer_init(&c->input);
169 buffer_init(&c->output);
170 chan_init_iostates(c);
171 c->self = found;
172 c->type = type;
173 c->sock = sock;
174 c->remote_id = -1;
175 c->remote_name = remote_name;
176 debug("channel %d: new [%s]", found, remote_name);
177 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
180/* Free the channel and close its socket. */
181
Damien Miller95def091999-11-25 00:26:21 +1100182void
183channel_free(int channel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller95def091999-11-25 00:26:21 +1100185 if (channel < 0 || channel >= channels_alloc ||
186 channels[channel].type == SSH_CHANNEL_FREE)
187 packet_disconnect("channel free: bad local channel %d", channel);
Damien Millerfd7c9111999-11-08 16:15:55 +1100188
Damien Miller95def091999-11-25 00:26:21 +1100189 if (compat13)
190 shutdown(channels[channel].sock, SHUT_RDWR);
191 close(channels[channel].sock);
192 buffer_free(&channels[channel].input);
193 buffer_free(&channels[channel].output);
194 channels[channel].type = SSH_CHANNEL_FREE;
195 if (channels[channel].remote_name) {
196 xfree(channels[channel].remote_name);
197 channels[channel].remote_name = NULL;
198 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199}
200
Damien Miller5428f641999-11-25 11:54:57 +1100201/*
202 * This is called just before select() to add any bits relevant to channels
203 * in the select bitmasks.
204 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller95def091999-11-25 00:26:21 +1100206void
207channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 int i;
210 Channel *ch;
211 unsigned char *ucp;
212 unsigned int proto_len, data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214 for (i = 0; i < channels_alloc; i++) {
215 ch = &channels[i];
216redo:
217 switch (ch->type) {
218 case SSH_CHANNEL_X11_LISTENER:
219 case SSH_CHANNEL_PORT_LISTENER:
220 case SSH_CHANNEL_AUTH_SOCKET:
221 FD_SET(ch->sock, readset);
222 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller95def091999-11-25 00:26:21 +1100224 case SSH_CHANNEL_OPEN:
225 if (compat13) {
226 if (buffer_len(&ch->input) < packet_get_maxsize())
227 FD_SET(ch->sock, readset);
228 if (buffer_len(&ch->output) > 0)
229 FD_SET(ch->sock, writeset);
230 break;
231 }
232 /* test whether sockets are 'alive' for read/write */
233 if (ch->istate == CHAN_INPUT_OPEN)
234 if (buffer_len(&ch->input) < packet_get_maxsize())
235 FD_SET(ch->sock, readset);
236 if (ch->ostate == CHAN_OUTPUT_OPEN ||
237 ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
238 if (buffer_len(&ch->output) > 0) {
239 FD_SET(ch->sock, writeset);
240 } else if (ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
241 chan_obuf_empty(ch);
242 }
243 }
244 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller95def091999-11-25 00:26:21 +1100246 case SSH_CHANNEL_INPUT_DRAINING:
247 if (!compat13)
248 fatal("cannot happen: IN_DRAIN");
249 if (buffer_len(&ch->input) == 0) {
250 packet_start(SSH_MSG_CHANNEL_CLOSE);
251 packet_put_int(ch->remote_id);
252 packet_send();
253 ch->type = SSH_CHANNEL_CLOSED;
254 debug("Closing channel %d after input drain.", i);
255 break;
256 }
257 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 case SSH_CHANNEL_OUTPUT_DRAINING:
260 if (!compat13)
261 fatal("cannot happen: OUT_DRAIN");
262 if (buffer_len(&ch->output) == 0) {
263 channel_free(i);
264 break;
265 }
266 FD_SET(ch->sock, writeset);
267 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Miller95def091999-11-25 00:26:21 +1100269 case SSH_CHANNEL_X11_OPEN:
Damien Miller5428f641999-11-25 11:54:57 +1100270 /*
271 * This is a special state for X11 authentication
272 * spoofing. An opened X11 connection (when
273 * authentication spoofing is being done) remains in
274 * this state until the first packet has been
275 * completely read. The authentication data in that
276 * packet is then substituted by the real data if it
277 * matches the fake data, and the channel is put into
278 * normal mode.
279 */
Damien Miller95def091999-11-25 00:26:21 +1100280 /* Check if the fixed size part of the packet is in buffer. */
281 if (buffer_len(&ch->output) < 12)
282 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller95def091999-11-25 00:26:21 +1100284 /* Parse the lengths of variable-length fields. */
285 ucp = (unsigned char *) buffer_ptr(&ch->output);
286 if (ucp[0] == 0x42) { /* Byte order MSB first. */
287 proto_len = 256 * ucp[6] + ucp[7];
288 data_len = 256 * ucp[8] + ucp[9];
289 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
290 proto_len = ucp[6] + 256 * ucp[7];
291 data_len = ucp[8] + 256 * ucp[9];
292 } else {
293 debug("Initial X11 packet contains bad byte order byte: 0x%x",
294 ucp[0]);
295 ch->type = SSH_CHANNEL_OPEN;
296 goto reject;
297 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller95def091999-11-25 00:26:21 +1100299 /* Check if the whole packet is in buffer. */
300 if (buffer_len(&ch->output) <
301 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
302 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 /* Check if authentication protocol matches. */
305 if (proto_len != strlen(x11_saved_proto) ||
306 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
307 debug("X11 connection uses different authentication protocol.");
308 ch->type = SSH_CHANNEL_OPEN;
309 goto reject;
310 }
311 /* Check if authentication data matches our fake data. */
312 if (data_len != x11_fake_data_len ||
313 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
314 x11_fake_data, x11_fake_data_len) != 0) {
315 debug("X11 auth data does not match fake data.");
316 ch->type = SSH_CHANNEL_OPEN;
317 goto reject;
318 }
319 /* Check fake data length */
320 if (x11_fake_data_len != x11_saved_data_len) {
321 error("X11 fake_data_len %d != saved_data_len %d",
322 x11_fake_data_len, x11_saved_data_len);
323 ch->type = SSH_CHANNEL_OPEN;
324 goto reject;
325 }
Damien Miller5428f641999-11-25 11:54:57 +1100326 /*
327 * Received authentication protocol and data match
328 * our fake data. Substitute the fake data with real
329 * data.
330 */
Damien Miller95def091999-11-25 00:26:21 +1100331 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
332 x11_saved_data, x11_saved_data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Miller95def091999-11-25 00:26:21 +1100334 /* Start normal processing for the channel. */
335 ch->type = SSH_CHANNEL_OPEN;
336 goto redo;
Damien Millerfd7c9111999-11-08 16:15:55 +1100337
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338 reject:
Damien Miller5428f641999-11-25 11:54:57 +1100339 /*
340 * We have received an X11 connection that has bad
341 * authentication information.
342 */
Damien Miller95def091999-11-25 00:26:21 +1100343 log("X11 connection rejected because of wrong authentication.\r\n");
344 buffer_clear(&ch->input);
345 buffer_clear(&ch->output);
346 if (compat13) {
347 close(ch->sock);
348 ch->sock = -1;
349 ch->type = SSH_CHANNEL_CLOSED;
350 packet_start(SSH_MSG_CHANNEL_CLOSE);
351 packet_put_int(ch->remote_id);
352 packet_send();
353 } else {
354 debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate);
355 chan_read_failed(ch);
356 chan_write_failed(ch);
357 debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate);
358 }
359 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Damien Miller95def091999-11-25 00:26:21 +1100361 case SSH_CHANNEL_FREE:
362 default:
363 continue;
364 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366}
367
Damien Miller5428f641999-11-25 11:54:57 +1100368/*
369 * After select, perform any appropriate operations for channels which have
370 * events pending.
371 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Miller95def091999-11-25 00:26:21 +1100373void
374channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller95def091999-11-25 00:26:21 +1100376 struct sockaddr addr;
377 int addrlen, newsock, i, newch, len;
378 Channel *ch;
379 char buf[16384], *remote_hostname;
380
381 /* Loop over all channels... */
382 for (i = 0; i < channels_alloc; i++) {
383 ch = &channels[i];
384 switch (ch->type) {
385 case SSH_CHANNEL_X11_LISTENER:
386 /* This is our fake X11 server socket. */
387 if (FD_ISSET(ch->sock, readset)) {
388 debug("X11 connection requested.");
389 addrlen = sizeof(addr);
390 newsock = accept(ch->sock, &addr, &addrlen);
391 if (newsock < 0) {
392 error("accept: %.100s", strerror(errno));
393 break;
394 }
395 remote_hostname = get_remote_hostname(newsock);
396 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
397 remote_hostname, get_peer_port(newsock));
398 xfree(remote_hostname);
399 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
400 xstrdup(buf));
401 packet_start(SSH_SMSG_X11_OPEN);
402 packet_put_int(newch);
403 if (have_hostname_in_open)
404 packet_put_string(buf, strlen(buf));
405 packet_send();
406 }
407 break;
408
409 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller5428f641999-11-25 11:54:57 +1100410 /*
411 * This socket is listening for connections to a
412 * forwarded TCP/IP port.
413 */
Damien Miller95def091999-11-25 00:26:21 +1100414 if (FD_ISSET(ch->sock, readset)) {
415 debug("Connection to port %d forwarding to %.100s:%d requested.",
416 ch->listening_port, ch->path, ch->host_port);
417 addrlen = sizeof(addr);
418 newsock = accept(ch->sock, &addr, &addrlen);
419 if (newsock < 0) {
420 error("accept: %.100s", strerror(errno));
421 break;
422 }
423 remote_hostname = get_remote_hostname(newsock);
424 snprintf(buf, sizeof buf, "listen port %d:%.100s:%d, connect from %.200s:%d",
425 ch->listening_port, ch->path, ch->host_port,
426 remote_hostname, get_peer_port(newsock));
427 xfree(remote_hostname);
428 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
429 xstrdup(buf));
430 packet_start(SSH_MSG_PORT_OPEN);
431 packet_put_int(newch);
432 packet_put_string(ch->path, strlen(ch->path));
433 packet_put_int(ch->host_port);
434 if (have_hostname_in_open)
435 packet_put_string(buf, strlen(buf));
436 packet_send();
437 }
438 break;
439
440 case SSH_CHANNEL_AUTH_SOCKET:
Damien Miller5428f641999-11-25 11:54:57 +1100441 /*
442 * This is the authentication agent socket listening
443 * for connections from clients.
444 */
Damien Miller95def091999-11-25 00:26:21 +1100445 if (FD_ISSET(ch->sock, readset)) {
446 int nchan;
447 len = sizeof(addr);
448 newsock = accept(ch->sock, &addr, &len);
449 if (newsock < 0) {
450 error("accept from auth socket: %.100s", strerror(errno));
451 break;
452 }
453 nchan = channel_allocate(SSH_CHANNEL_OPENING, newsock,
454 xstrdup("accepted auth socket"));
455 packet_start(SSH_SMSG_AGENT_OPEN);
456 packet_put_int(nchan);
457 packet_send();
458 }
459 break;
460
461 case SSH_CHANNEL_OPEN:
Damien Miller5428f641999-11-25 11:54:57 +1100462 /*
463 * This is an open two-way communication channel. It
464 * is not of interest to us at this point what kind
465 * of data is being transmitted.
466 */
Damien Miller95def091999-11-25 00:26:21 +1100467
Damien Miller5428f641999-11-25 11:54:57 +1100468 /*
469 * Read available incoming data and append it to
470 * buffer; shutdown socket, if read or write failes
471 */
Damien Miller95def091999-11-25 00:26:21 +1100472 if (FD_ISSET(ch->sock, readset)) {
473 len = read(ch->sock, buf, sizeof(buf));
474 if (len <= 0) {
475 if (compat13) {
476 buffer_consume(&ch->output, buffer_len(&ch->output));
477 ch->type = SSH_CHANNEL_INPUT_DRAINING;
478 debug("Channel %d status set to input draining.", i);
479 } else {
480 chan_read_failed(ch);
481 }
482 break;
483 }
484 buffer_append(&ch->input, buf, len);
485 }
486 /* Send buffered output data to the socket. */
487 if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) {
488 len = write(ch->sock, buffer_ptr(&ch->output),
489 buffer_len(&ch->output));
490 if (len <= 0) {
491 if (compat13) {
492 buffer_consume(&ch->output, buffer_len(&ch->output));
493 debug("Channel %d status set to input draining.", i);
494 ch->type = SSH_CHANNEL_INPUT_DRAINING;
495 } else {
496 chan_write_failed(ch);
497 }
498 break;
499 }
500 buffer_consume(&ch->output, len);
501 }
502 break;
503
504 case SSH_CHANNEL_OUTPUT_DRAINING:
505 if (!compat13)
506 fatal("cannot happen: OUT_DRAIN");
507 /* Send buffered output data to the socket. */
508 if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) {
509 len = write(ch->sock, buffer_ptr(&ch->output),
510 buffer_len(&ch->output));
511 if (len <= 0)
512 buffer_consume(&ch->output, buffer_len(&ch->output));
513 else
514 buffer_consume(&ch->output, len);
515 }
516 break;
517
518 case SSH_CHANNEL_X11_OPEN:
519 case SSH_CHANNEL_FREE:
520 default:
521 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524}
525
526/* If there is data to send to the connection, send some of it now. */
527
Damien Miller95def091999-11-25 00:26:21 +1100528void
529channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530{
Damien Miller95def091999-11-25 00:26:21 +1100531 int len, i;
532 Channel *ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 for (i = 0; i < channels_alloc; i++) {
535 ch = &channels[i];
Damien Miller5428f641999-11-25 11:54:57 +1100536 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller95def091999-11-25 00:26:21 +1100537 if (ch->type != SSH_CHANNEL_OPEN &&
538 ch->type != SSH_CHANNEL_INPUT_DRAINING)
539 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540
Damien Miller95def091999-11-25 00:26:21 +1100541 /* Get the amount of buffered data for this channel. */
542 len = buffer_len(&ch->input);
543 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100544 /* Send some data for the other side over the secure connection. */
Damien Miller95def091999-11-25 00:26:21 +1100545 if (packet_is_interactive()) {
546 if (len > 1024)
547 len = 512;
548 } else {
549 /* Keep the packets at reasonable size. */
550 if (len > 16384)
551 len = 16384;
552 }
553 packet_start(SSH_MSG_CHANNEL_DATA);
554 packet_put_int(ch->remote_id);
555 packet_put_string(buffer_ptr(&ch->input), len);
556 packet_send();
557 buffer_consume(&ch->input, len);
558 } else if (ch->istate == CHAN_INPUT_WAIT_DRAIN) {
559 if (compat13)
560 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +1100561 /*
562 * input-buffer is empty and read-socket shutdown:
563 * tell peer, that we will not send more data: send IEOF
564 */
Damien Miller95def091999-11-25 00:26:21 +1100565 chan_ibuf_empty(ch);
566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568}
569
Damien Miller5428f641999-11-25 11:54:57 +1100570/*
571 * This is called when a packet of type CHANNEL_DATA has just been received.
572 * The message type has already been consumed, but channel number and data is
573 * still there.
574 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller95def091999-11-25 00:26:21 +1100576void
577channel_input_data(int payload_len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578{
Damien Miller95def091999-11-25 00:26:21 +1100579 int channel;
580 char *data;
581 unsigned int data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
Damien Miller95def091999-11-25 00:26:21 +1100583 /* Get the channel number and verify it. */
584 channel = packet_get_int();
585 if (channel < 0 || channel >= channels_alloc ||
586 channels[channel].type == SSH_CHANNEL_FREE)
587 packet_disconnect("Received data for nonexistent channel %d.", channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588
Damien Miller95def091999-11-25 00:26:21 +1100589 /* Ignore any data for non-open channels (might happen on close) */
590 if (channels[channel].type != SSH_CHANNEL_OPEN &&
591 channels[channel].type != SSH_CHANNEL_X11_OPEN)
592 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593
Damien Miller95def091999-11-25 00:26:21 +1100594 /* Get the data. */
595 data = packet_get_string(&data_len);
596 packet_integrity_check(payload_len, 4 + 4 + data_len, SSH_MSG_CHANNEL_DATA);
597 buffer_append(&channels[channel].output, data, data_len);
598 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599}
600
Damien Miller5428f641999-11-25 11:54:57 +1100601/*
602 * Returns true if no channel has too much buffered data, and false if one or
603 * more channel is overfull.
604 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605
Damien Miller95def091999-11-25 00:26:21 +1100606int
607channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608{
Damien Miller95def091999-11-25 00:26:21 +1100609 unsigned int i;
610 Channel *ch;
611
612 for (i = 0; i < channels_alloc; i++) {
613 ch = &channels[i];
614 switch (ch->type) {
615 case SSH_CHANNEL_X11_LISTENER:
616 case SSH_CHANNEL_PORT_LISTENER:
617 case SSH_CHANNEL_AUTH_SOCKET:
618 continue;
619 case SSH_CHANNEL_OPEN:
620 if (buffer_len(&ch->input) > packet_get_maxsize())
621 return 0;
622 if (buffer_len(&ch->output) > packet_get_maxsize())
623 return 0;
624 continue;
625 case SSH_CHANNEL_INPUT_DRAINING:
626 case SSH_CHANNEL_OUTPUT_DRAINING:
627 case SSH_CHANNEL_X11_OPEN:
628 case SSH_CHANNEL_FREE:
629 default:
630 continue;
631 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632 }
Damien Miller95def091999-11-25 00:26:21 +1100633 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634}
635
636/* This is called after receiving CHANNEL_CLOSE/IEOF. */
637
Damien Miller95def091999-11-25 00:26:21 +1100638void
639channel_input_close()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640{
Damien Miller95def091999-11-25 00:26:21 +1100641 int channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642
Damien Miller95def091999-11-25 00:26:21 +1100643 /* Get the channel number and verify it. */
644 channel = packet_get_int();
645 if (channel < 0 || channel >= channels_alloc ||
646 channels[channel].type == SSH_CHANNEL_FREE)
647 packet_disconnect("Received data for nonexistent channel %d.", channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648
Damien Miller95def091999-11-25 00:26:21 +1100649 if (!compat13) {
650 /* proto version 1.5 overloads CLOSE with IEOF */
651 chan_rcvd_ieof(&channels[channel]);
652 return;
653 }
Damien Miller5428f641999-11-25 11:54:57 +1100654
655 /*
656 * Send a confirmation that we have closed the channel and no more
657 * data is coming for it.
658 */
Damien Miller95def091999-11-25 00:26:21 +1100659 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
660 packet_put_int(channels[channel].remote_id);
661 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662
Damien Miller5428f641999-11-25 11:54:57 +1100663 /*
664 * If the channel is in closed state, we have sent a close request,
665 * and the other side will eventually respond with a confirmation.
666 * Thus, we cannot free the channel here, because then there would be
667 * no-one to receive the confirmation. The channel gets freed when
668 * the confirmation arrives.
669 */
Damien Miller95def091999-11-25 00:26:21 +1100670 if (channels[channel].type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +1100671 /*
672 * Not a closed channel - mark it as draining, which will
673 * cause it to be freed later.
674 */
Damien Miller95def091999-11-25 00:26:21 +1100675 buffer_consume(&channels[channel].input,
676 buffer_len(&channels[channel].input));
677 channels[channel].type = SSH_CHANNEL_OUTPUT_DRAINING;
678 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679}
680
681/* This is called after receiving CHANNEL_CLOSE_CONFIRMATION/OCLOSE. */
682
Damien Miller95def091999-11-25 00:26:21 +1100683void
684channel_input_close_confirmation()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685{
Damien Miller95def091999-11-25 00:26:21 +1100686 int channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687
Damien Miller95def091999-11-25 00:26:21 +1100688 /* Get the channel number and verify it. */
689 channel = packet_get_int();
690 if (channel < 0 || channel >= channels_alloc)
691 packet_disconnect("Received close confirmation for out-of-range channel %d.",
692 channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693
Damien Miller95def091999-11-25 00:26:21 +1100694 if (!compat13) {
695 /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
696 chan_rcvd_oclose(&channels[channel]);
697 return;
698 }
699 if (channels[channel].type != SSH_CHANNEL_CLOSED)
700 packet_disconnect("Received close confirmation for non-closed channel %d (type %d).",
701 channel, channels[channel].type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702
Damien Miller95def091999-11-25 00:26:21 +1100703 /* Free the channel. */
704 channel_free(channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705}
706
707/* This is called after receiving CHANNEL_OPEN_CONFIRMATION. */
708
Damien Miller95def091999-11-25 00:26:21 +1100709void
710channel_input_open_confirmation()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711{
Damien Miller95def091999-11-25 00:26:21 +1100712 int channel, remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713
Damien Miller95def091999-11-25 00:26:21 +1100714 /* Get the channel number and verify it. */
715 channel = packet_get_int();
716 if (channel < 0 || channel >= channels_alloc ||
717 channels[channel].type != SSH_CHANNEL_OPENING)
718 packet_disconnect("Received open confirmation for non-opening channel %d.",
719 channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Damien Miller95def091999-11-25 00:26:21 +1100721 /* Get remote side's id for this channel. */
722 remote_channel = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723
Damien Miller5428f641999-11-25 11:54:57 +1100724 /* Record the remote channel number and mark that the channel is now open. */
Damien Miller95def091999-11-25 00:26:21 +1100725 channels[channel].remote_id = remote_channel;
726 channels[channel].type = SSH_CHANNEL_OPEN;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727}
728
729/* This is called after receiving CHANNEL_OPEN_FAILURE from the other side. */
730
Damien Miller95def091999-11-25 00:26:21 +1100731void
732channel_input_open_failure()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733{
Damien Miller95def091999-11-25 00:26:21 +1100734 int channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735
Damien Miller95def091999-11-25 00:26:21 +1100736 /* Get the channel number and verify it. */
737 channel = packet_get_int();
738 if (channel < 0 || channel >= channels_alloc ||
739 channels[channel].type != SSH_CHANNEL_OPENING)
740 packet_disconnect("Received open failure for non-opening channel %d.",
741 channel);
742
743 /* Free the channel. This will also close the socket. */
744 channel_free(channel);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745}
746
Damien Miller5428f641999-11-25 11:54:57 +1100747/*
748 * Stops listening for channels, and removes any unix domain sockets that we
749 * might have.
750 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751
Damien Miller95def091999-11-25 00:26:21 +1100752void
753channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754{
Damien Miller95def091999-11-25 00:26:21 +1100755 int i;
756 for (i = 0; i < channels_alloc; i++) {
757 switch (channels[i].type) {
758 case SSH_CHANNEL_AUTH_SOCKET:
759 close(channels[i].sock);
760 remove(channels[i].path);
761 channel_free(i);
762 break;
763 case SSH_CHANNEL_PORT_LISTENER:
764 case SSH_CHANNEL_X11_LISTENER:
765 close(channels[i].sock);
766 channel_free(i);
767 break;
768 default:
769 break;
770 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772}
773
Damien Miller5428f641999-11-25 11:54:57 +1100774/*
775 * Closes the sockets of all channels. This is used to close extra file
776 * descriptors after a fork.
777 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778
Damien Miller95def091999-11-25 00:26:21 +1100779void
780channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781{
Damien Miller95def091999-11-25 00:26:21 +1100782 int i;
783 for (i = 0; i < channels_alloc; i++) {
784 if (channels[i].type != SSH_CHANNEL_FREE)
785 close(channels[i].sock);
786 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787}
788
789/* Returns the maximum file descriptor number used by the channels. */
790
Damien Miller95def091999-11-25 00:26:21 +1100791int
792channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793{
Damien Miller95def091999-11-25 00:26:21 +1100794 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
797/* Returns true if any channel is still open. */
798
Damien Miller95def091999-11-25 00:26:21 +1100799int
800channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801{
Damien Miller95def091999-11-25 00:26:21 +1100802 unsigned int i;
803 for (i = 0; i < channels_alloc; i++)
804 switch (channels[i].type) {
805 case SSH_CHANNEL_FREE:
806 case SSH_CHANNEL_X11_LISTENER:
807 case SSH_CHANNEL_PORT_LISTENER:
808 case SSH_CHANNEL_CLOSED:
809 case SSH_CHANNEL_AUTH_SOCKET:
810 continue;
811 case SSH_CHANNEL_OPENING:
812 case SSH_CHANNEL_OPEN:
813 case SSH_CHANNEL_X11_OPEN:
814 return 1;
815 case SSH_CHANNEL_INPUT_DRAINING:
816 case SSH_CHANNEL_OUTPUT_DRAINING:
817 if (!compat13)
818 fatal("cannot happen: OUT_DRAIN");
819 return 1;
820 default:
821 fatal("channel_still_open: bad channel type %d", channels[i].type);
822 /* NOTREACHED */
823 }
824 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825}
826
Damien Miller5428f641999-11-25 11:54:57 +1100827/*
828 * Returns a message describing the currently open forwarded connections,
829 * suitable for sending to the client. The message contains crlf pairs for
830 * newlines.
831 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832
Damien Miller95def091999-11-25 00:26:21 +1100833char *
834channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835{
Damien Miller95def091999-11-25 00:26:21 +1100836 Buffer buffer;
837 int i;
838 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839
Damien Miller95def091999-11-25 00:26:21 +1100840 buffer_init(&buffer);
841 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +1100843 for (i = 0; i < channels_alloc; i++) {
844 Channel *c = &channels[i];
845 switch (c->type) {
846 case SSH_CHANNEL_FREE:
847 case SSH_CHANNEL_X11_LISTENER:
848 case SSH_CHANNEL_PORT_LISTENER:
849 case SSH_CHANNEL_CLOSED:
850 case SSH_CHANNEL_AUTH_SOCKET:
851 continue;
852 case SSH_CHANNEL_OPENING:
853 case SSH_CHANNEL_OPEN:
854 case SSH_CHANNEL_X11_OPEN:
855 case SSH_CHANNEL_INPUT_DRAINING:
856 case SSH_CHANNEL_OUTPUT_DRAINING:
857 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d o%d)\r\n",
858 c->self, c->remote_name,
859 c->type, c->remote_id, c->istate, c->ostate);
860 buffer_append(&buffer, buf, strlen(buf));
861 continue;
862 default:
863 fatal("channel_still_open: bad channel type %d", c->type);
864 /* NOTREACHED */
865 }
866 }
867 buffer_append(&buffer, "\0", 1);
868 cp = xstrdup(buffer_ptr(&buffer));
869 buffer_free(&buffer);
870 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871}
872
Damien Miller5428f641999-11-25 11:54:57 +1100873/*
874 * Initiate forwarding of connections to local port "port" through the secure
875 * channel to host:port from remote side.
876 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877
Damien Miller95def091999-11-25 00:26:21 +1100878void
879channel_request_local_forwarding(int port, const char *host,
880 int host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881{
Damien Miller5428f641999-11-25 11:54:57 +1100882 int ch, sock, on = 1;
Damien Miller95def091999-11-25 00:26:21 +1100883 struct sockaddr_in sin;
884 extern Options options;
Damien Miller5428f641999-11-25 11:54:57 +1100885 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886
Damien Miller95def091999-11-25 00:26:21 +1100887 if (strlen(host) > sizeof(channels[0].path) - 1)
888 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889
Damien Miller95def091999-11-25 00:26:21 +1100890 /* Create a port to listen for the host. */
891 sock = socket(AF_INET, SOCK_STREAM, 0);
892 if (sock < 0)
893 packet_disconnect("socket: %.100s", strerror(errno));
894
895 /* Initialize socket address. */
896 memset(&sin, 0, sizeof(sin));
897 sin.sin_family = AF_INET;
898 if (options.gateway_ports == 1)
899 sin.sin_addr.s_addr = htonl(INADDR_ANY);
900 else
901 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
902 sin.sin_port = htons(port);
903
Damien Miller5428f641999-11-25 11:54:57 +1100904 /*
905 * Set socket options. We would like the socket to disappear as soon
906 * as it has been closed for whatever reason.
907 */
908 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
909 linger.l_onoff = 1;
910 linger.l_linger = 5;
911 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
912
Damien Miller95def091999-11-25 00:26:21 +1100913 /* Bind the socket to the address. */
914 if (bind(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0)
915 packet_disconnect("bind: %.100s", strerror(errno));
916
917 /* Start listening for connections on the socket. */
918 if (listen(sock, 5) < 0)
919 packet_disconnect("listen: %.100s", strerror(errno));
920
921 /* Allocate a channel number for the socket. */
922 ch = channel_allocate(SSH_CHANNEL_PORT_LISTENER, sock,
923 xstrdup("port listener"));
924 strcpy(channels[ch].path, host);
925 channels[ch].host_port = host_port;
926 channels[ch].listening_port = port;
927}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928
Damien Miller5428f641999-11-25 11:54:57 +1100929/*
930 * Initiate forwarding of connections to port "port" on remote host through
931 * the secure channel to host:port from local side.
932 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933
Damien Miller95def091999-11-25 00:26:21 +1100934void
935channel_request_remote_forwarding(int port, const char *host,
936 int remote_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937{
Damien Miller95def091999-11-25 00:26:21 +1100938 int payload_len;
939 /* Record locally that connection to this host/port is permitted. */
940 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
941 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942
Damien Miller95def091999-11-25 00:26:21 +1100943 permitted_opens[num_permitted_opens].host = xstrdup(host);
944 permitted_opens[num_permitted_opens].port = remote_port;
945 num_permitted_opens++;
946
947 /* Send the forward request to the remote side. */
948 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
949 packet_put_int(port);
950 packet_put_string(host, strlen(host));
951 packet_put_int(remote_port);
952 packet_send();
953 packet_write_wait();
954
Damien Miller5428f641999-11-25 11:54:57 +1100955 /*
956 * Wait for response from the remote side. It will send a disconnect
957 * message on failure, and we will never see it here.
958 */
Damien Miller95def091999-11-25 00:26:21 +1100959 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
Damien Miller5428f641999-11-25 11:54:57 +1100962/*
963 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
964 * listening for the port, and sends back a success reply (or disconnect
965 * message if there was an error). This never returns if there was an error.
966 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968void
969channel_input_port_forward_request(int is_root)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970{
Damien Miller95def091999-11-25 00:26:21 +1100971 int port, host_port;
972 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973
Damien Miller95def091999-11-25 00:26:21 +1100974 /* Get arguments from the packet. */
975 port = packet_get_int();
976 hostname = packet_get_string(NULL);
977 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 /* Port numbers are 16 bit quantities. */
980 if ((port & 0xffff) != port)
981 packet_disconnect("Requested forwarding of nonexistent port %d.", port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller5428f641999-11-25 11:54:57 +1100983 /*
984 * Check that an unprivileged user is not trying to forward a
985 * privileged port.
986 */
Damien Miller95def091999-11-25 00:26:21 +1100987 if (port < IPPORT_RESERVED && !is_root)
988 packet_disconnect("Requested forwarding of port %d but user is not root.",
989 port);
990
991 /* Initiate forwarding. */
992 channel_request_local_forwarding(port, hostname, host_port);
993
994 /* Free the argument string. */
995 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996}
997
Damien Miller5428f641999-11-25 11:54:57 +1100998/*
999 * This is called after receiving PORT_OPEN message. This attempts to
1000 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1001 * or CHANNEL_OPEN_FAILURE.
1002 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003
Damien Miller95def091999-11-25 00:26:21 +11001004void
1005channel_input_port_open(int payload_len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006{
Damien Miller95def091999-11-25 00:26:21 +11001007 int remote_channel, sock, newch, host_port, i;
1008 struct sockaddr_in sin;
1009 char *host, *originator_string;
1010 struct hostent *hp;
1011 int host_len, originator_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Get remote channel number. */
1014 remote_channel = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015
Damien Miller95def091999-11-25 00:26:21 +11001016 /* Get host name to connect to. */
1017 host = packet_get_string(&host_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller95def091999-11-25 00:26:21 +11001019 /* Get port to connect to. */
1020 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021
Damien Miller95def091999-11-25 00:26:21 +11001022 /* Get remote originator name. */
Damien Miller5428f641999-11-25 11:54:57 +11001023 if (have_hostname_in_open) {
Damien Miller95def091999-11-25 00:26:21 +11001024 originator_string = packet_get_string(&originator_len);
Damien Miller5428f641999-11-25 11:54:57 +11001025 originator_len += 4; /* size of packet_int */
1026 } else {
Damien Miller95def091999-11-25 00:26:21 +11001027 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller5428f641999-11-25 11:54:57 +11001028 originator_len = 0; /* no originator supplied */
1029 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Miller95def091999-11-25 00:26:21 +11001031 packet_integrity_check(payload_len,
Damien Miller5428f641999-11-25 11:54:57 +11001032 4 + 4 + host_len + 4 + originator_len,
Damien Miller95def091999-11-25 00:26:21 +11001033 SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034
Damien Miller95def091999-11-25 00:26:21 +11001035 /* Check if opening that port is permitted. */
1036 if (!all_opens_permitted) {
1037 /* Go trough all permitted ports. */
1038 for (i = 0; i < num_permitted_opens; i++)
1039 if (permitted_opens[i].port == host_port &&
1040 strcmp(permitted_opens[i].host, host) == 0)
1041 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042
Damien Miller95def091999-11-25 00:26:21 +11001043 /* Check if we found the requested port among those permitted. */
1044 if (i >= num_permitted_opens) {
1045 /* The port is not permitted. */
1046 log("Received request to connect to %.100s:%d, but the request was denied.",
1047 host, host_port);
1048 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1049 packet_put_int(remote_channel);
1050 packet_send();
1051 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052 }
Damien Miller95def091999-11-25 00:26:21 +11001053 memset(&sin, 0, sizeof(sin));
1054 sin.sin_addr.s_addr = inet_addr(host);
1055 if ((sin.sin_addr.s_addr & 0xffffffff) != 0xffffffff) {
1056 /* It was a valid numeric host address. */
1057 sin.sin_family = AF_INET;
1058 } else {
1059 /* Look up the host address from the name servers. */
1060 hp = gethostbyname(host);
1061 if (!hp) {
1062 error("%.100s: unknown host.", host);
1063 goto fail;
1064 }
1065 if (!hp->h_addr_list[0]) {
1066 error("%.100s: host has no IP address.", host);
1067 goto fail;
1068 }
1069 sin.sin_family = hp->h_addrtype;
1070 memcpy(&sin.sin_addr, hp->h_addr_list[0],
1071 sizeof(sin.sin_addr));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 sin.sin_port = htons(host_port);
1074
1075 /* Create the socket. */
1076 sock = socket(sin.sin_family, SOCK_STREAM, 0);
1077 if (sock < 0) {
1078 error("socket: %.100s", strerror(errno));
1079 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080 }
Damien Miller95def091999-11-25 00:26:21 +11001081 /* Connect to the host/port. */
1082 if (connect(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0) {
1083 error("connect %.100s:%d: %.100s", host, host_port,
1084 strerror(errno));
1085 close(sock);
1086 goto fail;
1087 }
1088 /* Successful connection. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089
Damien Miller95def091999-11-25 00:26:21 +11001090 /* Allocate a channel for this connection. */
1091 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1092 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093
Damien Miller95def091999-11-25 00:26:21 +11001094 /* Send a confirmation to the remote host. */
1095 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1096 packet_put_int(remote_channel);
1097 packet_put_int(newch);
1098 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099
Damien Miller95def091999-11-25 00:26:21 +11001100 /* Free the argument string. */
1101 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
Damien Miller95def091999-11-25 00:26:21 +11001103 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104
Damien Miller95def091999-11-25 00:26:21 +11001105fail:
1106 /* Free the argument string. */
1107 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108
Damien Miller95def091999-11-25 00:26:21 +11001109 /* Send refusal to the remote host. */
1110 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1111 packet_put_int(remote_channel);
1112 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113}
1114
Damien Miller5428f641999-11-25 11:54:57 +11001115/*
1116 * Creates an internet domain socket for listening for X11 connections.
1117 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1118 * occurs.
1119 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120
Damien Miller95def091999-11-25 00:26:21 +11001121char *
1122x11_create_display_inet(int screen_number)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123{
Damien Miller95def091999-11-25 00:26:21 +11001124 extern ServerOptions options;
1125 int display_number, port, sock;
1126 struct sockaddr_in sin;
1127 char buf[512];
1128 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
Damien Miller95def091999-11-25 00:26:21 +11001130 for (display_number = options.x11_display_offset;
1131 display_number < MAX_DISPLAYS;
1132 display_number++) {
1133 port = 6000 + display_number;
1134 memset(&sin, 0, sizeof(sin));
1135 sin.sin_family = AF_INET;
1136 sin.sin_addr.s_addr = htonl(INADDR_ANY);
1137 sin.sin_port = htons(port);
1138
1139 sock = socket(AF_INET, SOCK_STREAM, 0);
1140 if (sock < 0) {
1141 error("socket: %.100s", strerror(errno));
1142 return NULL;
1143 }
1144 if (bind(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0) {
1145 debug("bind port %d: %.100s", port, strerror(errno));
1146 shutdown(sock, SHUT_RDWR);
1147 close(sock);
1148 continue;
1149 }
1150 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151 }
Damien Miller95def091999-11-25 00:26:21 +11001152 if (display_number >= MAX_DISPLAYS) {
1153 error("Failed to allocate internet-domain X11 display socket.");
1154 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 }
Damien Miller95def091999-11-25 00:26:21 +11001156 /* Start listening for connections on the socket. */
1157 if (listen(sock, 5) < 0) {
1158 error("listen: %.100s", strerror(errno));
1159 shutdown(sock, SHUT_RDWR);
1160 close(sock);
1161 return NULL;
1162 }
1163 /* Set up a suitable value for the DISPLAY variable. */
1164 if (gethostname(hostname, sizeof(hostname)) < 0)
1165 fatal("gethostname: %.100s", strerror(errno));
1166 snprintf(buf, sizeof buf, "%.400s:%d.%d", hostname,
1167 display_number, screen_number);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168
Damien Miller95def091999-11-25 00:26:21 +11001169 /* Allocate a channel for the socket. */
1170 (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
1171 xstrdup("X11 inet listener"));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172
Damien Miller95def091999-11-25 00:26:21 +11001173 /* Return a suitable value for the DISPLAY environment variable. */
1174 return xstrdup(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175}
1176
1177#ifndef X_UNIX_PATH
1178#define X_UNIX_PATH "/tmp/.X11-unix/X"
1179#endif
1180
1181static
1182int
1183connect_local_xsocket(unsigned dnr)
1184{
Damien Miller95def091999-11-25 00:26:21 +11001185 static const char *const x_sockets[] = {
1186 X_UNIX_PATH "%u",
1187 "/var/X/.X11-unix/X" "%u",
1188 "/usr/spool/sockets/X11/" "%u",
1189 NULL
1190 };
1191 int sock;
1192 struct sockaddr_un addr;
1193 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194
Damien Miller95def091999-11-25 00:26:21 +11001195 for (path = x_sockets; *path; ++path) {
1196 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1197 if (sock < 0)
1198 error("socket: %.100s", strerror(errno));
1199 memset(&addr, 0, sizeof(addr));
1200 addr.sun_family = AF_UNIX;
1201 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1202 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1203 return sock;
1204 close(sock);
1205 }
1206 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1207 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208}
1209
1210
Damien Miller5428f641999-11-25 11:54:57 +11001211/*
1212 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1213 * the remote channel number. We should do whatever we want, and respond
1214 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1215 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216
Damien Miller95def091999-11-25 00:26:21 +11001217void
1218x11_input_open(int payload_len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219{
Damien Miller95def091999-11-25 00:26:21 +11001220 int remote_channel, display_number, sock, newch;
1221 const char *display;
1222 struct sockaddr_in sin;
1223 char buf[1024], *cp, *remote_host;
1224 struct hostent *hp;
1225 int remote_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226
Damien Miller95def091999-11-25 00:26:21 +11001227 /* Get remote channel number. */
1228 remote_channel = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229
Damien Miller95def091999-11-25 00:26:21 +11001230 /* Get remote originator name. */
Damien Miller5428f641999-11-25 11:54:57 +11001231 if (have_hostname_in_open) {
Damien Miller95def091999-11-25 00:26:21 +11001232 remote_host = packet_get_string(&remote_len);
Damien Miller5428f641999-11-25 11:54:57 +11001233 remote_len += 4;
1234 } else {
Damien Miller95def091999-11-25 00:26:21 +11001235 remote_host = xstrdup("unknown (remote did not supply name)");
Damien Miller5428f641999-11-25 11:54:57 +11001236 remote_len = 0;
1237 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001238
Damien Miller95def091999-11-25 00:26:21 +11001239 debug("Received X11 open request.");
Damien Miller5428f641999-11-25 11:54:57 +11001240 packet_integrity_check(payload_len, 4 + remote_len, SSH_SMSG_X11_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241
Damien Miller95def091999-11-25 00:26:21 +11001242 /* Try to open a socket for the local X server. */
1243 display = getenv("DISPLAY");
1244 if (!display) {
1245 error("DISPLAY not set.");
1246 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247 }
Damien Miller5428f641999-11-25 11:54:57 +11001248 /*
1249 * Now we decode the value of the DISPLAY variable and make a
1250 * connection to the real X server.
1251 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252
Damien Miller5428f641999-11-25 11:54:57 +11001253 /*
1254 * Check if it is a unix domain socket. Unix domain displays are in
1255 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1256 */
Damien Miller95def091999-11-25 00:26:21 +11001257 if (strncmp(display, "unix:", 5) == 0 ||
1258 display[0] == ':') {
1259 /* Connect to the unix domain socket. */
1260 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1261 error("Could not parse display number from DISPLAY: %.100s",
1262 display);
1263 goto fail;
1264 }
1265 /* Create a socket. */
1266 sock = connect_local_xsocket(display_number);
1267 if (sock < 0)
1268 goto fail;
1269
1270 /* OK, we now have a connection to the display. */
1271 goto success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272 }
Damien Miller5428f641999-11-25 11:54:57 +11001273 /*
1274 * Connect to an inet socket. The DISPLAY value is supposedly
1275 * hostname:d[.s], where hostname may also be numeric IP address.
1276 */
Damien Miller95def091999-11-25 00:26:21 +11001277 strncpy(buf, display, sizeof(buf));
1278 buf[sizeof(buf) - 1] = 0;
1279 cp = strchr(buf, ':');
1280 if (!cp) {
1281 error("Could not find ':' in DISPLAY: %.100s", display);
1282 goto fail;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283 }
Damien Miller95def091999-11-25 00:26:21 +11001284 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001285 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11001286 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1287 error("Could not parse display number from DISPLAY: %.100s",
1288 display);
1289 goto fail;
1290 }
1291 /* Try to parse the host name as a numeric IP address. */
1292 memset(&sin, 0, sizeof(sin));
1293 sin.sin_addr.s_addr = inet_addr(buf);
1294 if ((sin.sin_addr.s_addr & 0xffffffff) != 0xffffffff) {
1295 /* It was a valid numeric host address. */
1296 sin.sin_family = AF_INET;
1297 } else {
1298 /* Not a numeric IP address. */
1299 /* Look up the host address from the name servers. */
1300 hp = gethostbyname(buf);
1301 if (!hp) {
1302 error("%.100s: unknown host.", buf);
1303 goto fail;
1304 }
1305 if (!hp->h_addr_list[0]) {
1306 error("%.100s: host has no IP address.", buf);
1307 goto fail;
1308 }
1309 sin.sin_family = hp->h_addrtype;
1310 memcpy(&sin.sin_addr, hp->h_addr_list[0],
1311 sizeof(sin.sin_addr));
1312 }
1313 /* Set port number. */
1314 sin.sin_port = htons(6000 + display_number);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
Damien Miller95def091999-11-25 00:26:21 +11001316 /* Create a socket. */
1317 sock = socket(sin.sin_family, SOCK_STREAM, 0);
1318 if (sock < 0) {
1319 error("socket: %.100s", strerror(errno));
1320 goto fail;
1321 }
1322 /* Connect it to the display. */
1323 if (connect(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0) {
1324 error("connect %.100s:%d: %.100s", buf, 6000 + display_number,
1325 strerror(errno));
1326 close(sock);
1327 goto fail;
1328 }
1329success:
1330 /* We have successfully obtained a connection to the real X display. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331
Damien Miller95def091999-11-25 00:26:21 +11001332 /* Allocate a channel for this connection. */
1333 if (x11_saved_proto == NULL)
1334 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
1335 else
1336 newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
1337 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338
Damien Miller95def091999-11-25 00:26:21 +11001339 /* Send a confirmation to the remote host. */
1340 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1341 packet_put_int(remote_channel);
1342 packet_put_int(newch);
1343 packet_send();
1344
1345 return;
1346
1347fail:
1348 /* Send refusal to the remote host. */
1349 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1350 packet_put_int(remote_channel);
1351 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001352}
1353
Damien Miller5428f641999-11-25 11:54:57 +11001354/*
1355 * Requests forwarding of X11 connections, generates fake authentication
1356 * data, and enables authentication spoofing.
1357 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Damien Miller95def091999-11-25 00:26:21 +11001359void
1360x11_request_forwarding_with_spoofing(const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361{
Damien Miller95def091999-11-25 00:26:21 +11001362 unsigned int data_len = (unsigned int) strlen(data) / 2;
1363 unsigned int i, value;
1364 char *new_data;
1365 int screen_number;
1366 const char *cp;
1367 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368
Damien Miller95def091999-11-25 00:26:21 +11001369 cp = getenv("DISPLAY");
1370 if (cp)
1371 cp = strchr(cp, ':');
1372 if (cp)
1373 cp = strchr(cp, '.');
1374 if (cp)
1375 screen_number = atoi(cp + 1);
1376 else
1377 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
Damien Miller95def091999-11-25 00:26:21 +11001379 /* Save protocol name. */
1380 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
Damien Miller5428f641999-11-25 11:54:57 +11001382 /*
1383 * Extract real authentication data and generate fake data of the
1384 * same length.
1385 */
Damien Miller95def091999-11-25 00:26:21 +11001386 x11_saved_data = xmalloc(data_len);
1387 x11_fake_data = xmalloc(data_len);
1388 for (i = 0; i < data_len; i++) {
1389 if (sscanf(data + 2 * i, "%2x", &value) != 1)
1390 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
1391 if (i % 4 == 0)
1392 rand = arc4random();
1393 x11_saved_data[i] = value;
1394 x11_fake_data[i] = rand & 0xff;
1395 rand >>= 8;
1396 }
1397 x11_saved_data_len = data_len;
1398 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001399
Damien Miller95def091999-11-25 00:26:21 +11001400 /* Convert the fake data into hex. */
1401 new_data = xmalloc(2 * data_len + 1);
1402 for (i = 0; i < data_len; i++)
1403 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Damien Miller95def091999-11-25 00:26:21 +11001405 /* Send the request packet. */
1406 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
1407 packet_put_string(proto, strlen(proto));
1408 packet_put_string(new_data, strlen(new_data));
1409 packet_put_int(screen_number);
1410 packet_send();
1411 packet_write_wait();
1412 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413}
1414
1415/* Sends a message to the server to request authentication fd forwarding. */
1416
Damien Miller95def091999-11-25 00:26:21 +11001417void
1418auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419{
Damien Miller95def091999-11-25 00:26:21 +11001420 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
1421 packet_send();
1422 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423}
1424
Damien Miller5428f641999-11-25 11:54:57 +11001425/*
1426 * Returns the name of the forwarded authentication socket. Returns NULL if
1427 * there is no forwarded authentication socket. The returned value points to
1428 * a static buffer.
1429 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430
Damien Miller95def091999-11-25 00:26:21 +11001431char *
1432auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433{
Damien Miller95def091999-11-25 00:26:21 +11001434 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001435}
1436
1437/* removes the agent forwarding socket */
1438
Damien Miller95def091999-11-25 00:26:21 +11001439void
1440cleanup_socket(void)
1441{
1442 remove(channel_forwarded_auth_socket_name);
1443 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444}
1445
Damien Miller5428f641999-11-25 11:54:57 +11001446/*
1447 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
1448 * This starts forwarding authentication requests.
1449 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450
Damien Miller95def091999-11-25 00:26:21 +11001451void
1452auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453{
Damien Miller95def091999-11-25 00:26:21 +11001454 int sock, newch;
1455 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Damien Miller95def091999-11-25 00:26:21 +11001457 if (auth_get_socket_name() != NULL)
1458 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459
Damien Miller95def091999-11-25 00:26:21 +11001460 /* Temporarily drop privileged uid for mkdir/bind. */
1461 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463 /* Allocate a buffer for the socket name, and format the name. */
1464 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
1465 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
1466 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Damien Miller95def091999-11-25 00:26:21 +11001468 /* Create private directory for socket */
1469 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
1470 packet_disconnect("mkdtemp: %.100s", strerror(errno));
1471 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
1472 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473
Damien Miller95def091999-11-25 00:26:21 +11001474 if (atexit(cleanup_socket) < 0) {
1475 int saved = errno;
1476 cleanup_socket();
1477 packet_disconnect("socket: %.100s", strerror(saved));
1478 }
1479 /* Create the socket. */
1480 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1481 if (sock < 0)
1482 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483
Damien Miller95def091999-11-25 00:26:21 +11001484 /* Bind it to the name. */
1485 memset(&sunaddr, 0, sizeof(sunaddr));
1486 sunaddr.sun_family = AF_UNIX;
1487 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
1488 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller95def091999-11-25 00:26:21 +11001490 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
1491 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492
Damien Miller95def091999-11-25 00:26:21 +11001493 /* Restore the privileged uid. */
1494 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Damien Miller95def091999-11-25 00:26:21 +11001496 /* Start listening on the socket. */
1497 if (listen(sock, 5) < 0)
1498 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Damien Miller95def091999-11-25 00:26:21 +11001500 /* Allocate a channel for the authentication agent socket. */
1501 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
1502 xstrdup("auth socket"));
1503 strcpy(channels[newch].path, channel_forwarded_auth_socket_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504}
1505
1506/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
1507
Damien Miller95def091999-11-25 00:26:21 +11001508void
1509auth_input_open_request()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510{
Damien Miller95def091999-11-25 00:26:21 +11001511 int remch, sock, newch;
1512 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513
Damien Miller95def091999-11-25 00:26:21 +11001514 /* Read the remote channel number from the message. */
1515 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Damien Miller5428f641999-11-25 11:54:57 +11001517 /*
1518 * Get a connection to the local authentication agent (this may again
1519 * get forwarded).
1520 */
Damien Miller95def091999-11-25 00:26:21 +11001521 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522
Damien Miller5428f641999-11-25 11:54:57 +11001523 /*
1524 * If we could not connect the agent, send an error message back to
1525 * the server. This should never happen unless the agent dies,
1526 * because authentication forwarding is only enabled if we have an
1527 * agent.
1528 */
Damien Miller95def091999-11-25 00:26:21 +11001529 if (sock < 0) {
1530 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1531 packet_put_int(remch);
1532 packet_send();
1533 return;
1534 }
1535 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Damien Miller5428f641999-11-25 11:54:57 +11001537 /*
1538 * Dummy host name. This will be freed when the channel is freed; it
1539 * will still be valid in the packet_put_string below since the
1540 * channel cannot yet be freed at that point.
1541 */
Damien Miller95def091999-11-25 00:26:21 +11001542 dummyname = xstrdup("authentication agent connection");
1543
1544 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
1545 channels[newch].remote_id = remch;
1546
1547 /* Send a confirmation to the remote host. */
1548 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1549 packet_put_int(remch);
1550 packet_put_int(newch);
1551 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552}