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