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