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