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