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