Annotation of src/usr.bin/ssh/channels.c, Revision 1.298
1.298 ! dtucker 1: /* $OpenBSD: channels.c,v 1.297 2009/10/28 16:38:18 reyk Exp $ */
1.1 deraadt 2: /*
1.26 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * This file contains functions for generic socket connection forwarding.
7: * There is also code for initiating connection forwarding for X11 connections,
8: * arbitrary tcp/ip connections, and the authentication agent connection.
1.49 markus 9: *
1.67 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
1.44 markus 16: * SSH2 support added by Markus Friedl.
1.156 markus 17: * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
1.67 deraadt 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:
1.265 deraadt 42: #include <sys/types.h>
1.234 stevesk 43: #include <sys/ioctl.h>
1.235 stevesk 44: #include <sys/un.h>
1.251 stevesk 45: #include <sys/socket.h>
1.261 stevesk 46: #include <sys/time.h>
1.275 djm 47: #include <sys/queue.h>
1.251 stevesk 48:
49: #include <netinet/in.h>
50: #include <arpa/inet.h>
1.233 stevesk 51:
1.254 stevesk 52: #include <errno.h>
1.298 ! dtucker 53: #include <fcntl.h>
1.255 stevesk 54: #include <netdb.h>
1.263 stevesk 55: #include <stdio.h>
1.262 stevesk 56: #include <stdlib.h>
1.260 stevesk 57: #include <string.h>
1.233 stevesk 58: #include <termios.h>
1.256 stevesk 59: #include <unistd.h>
1.265 deraadt 60: #include <stdarg.h>
1.1 deraadt 61:
1.265 deraadt 62: #include "xmalloc.h"
1.1 deraadt 63: #include "ssh.h"
1.82 markus 64: #include "ssh1.h"
65: #include "ssh2.h"
1.1 deraadt 66: #include "packet.h"
1.82 markus 67: #include "log.h"
68: #include "misc.h"
1.265 deraadt 69: #include "buffer.h"
1.14 markus 70: #include "channels.h"
71: #include "compat.h"
1.82 markus 72: #include "canohost.h"
1.64 markus 73: #include "key.h"
74: #include "authfd.h"
1.147 stevesk 75: #include "pathnames.h"
1.1 deraadt 76:
1.121 markus 77: /* -- channel core */
1.12 markus 78:
1.27 markus 79: /*
80: * Pointer to an array containing all allocated channels. The array is
81: * dynamically extended as needed.
82: */
1.113 markus 83: static Channel **channels = NULL;
1.1 deraadt 84:
1.27 markus 85: /*
86: * Size of the channel array. All slots of the array must always be
1.113 markus 87: * initialized (at least the type field); unused slots set to NULL
1.27 markus 88: */
1.209 avsm 89: static u_int channels_alloc = 0;
1.1 deraadt 90:
1.27 markus 91: /*
92: * Maximum file descriptor value used in any of the channels. This is
1.113 markus 93: * updated in channel_new.
1.27 markus 94: */
1.84 markus 95: static int channel_max_fd = 0;
1.1 deraadt 96:
97:
1.121 markus 98: /* -- tcp forwarding */
1.1 deraadt 99:
1.27 markus 100: /*
101: * Data structure for storing which hosts are permitted for forward requests.
102: * The local sides of any remote forwards are stored in this array to prevent
103: * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
104: * network (which might be behind a firewall).
105: */
1.25 markus 106: typedef struct {
1.41 markus 107: char *host_to_connect; /* Connect to 'host'. */
108: u_short port_to_connect; /* Connect to 'port'. */
109: u_short listen_port; /* Remote side should listen port number. */
1.1 deraadt 110: } ForwardPermission;
111:
1.257 dtucker 112: /* List of all permitted host/port pairs to connect by the user. */
1.1 deraadt 113: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
1.121 markus 114:
1.257 dtucker 115: /* List of all permitted host/port pairs to connect by the admin. */
116: static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
117:
118: /* Number of permitted host/port pairs in the array permitted by the user. */
1.1 deraadt 119: static int num_permitted_opens = 0;
1.257 dtucker 120:
121: /* Number of permitted host/port pair in the array permitted by the admin. */
122: static int num_adm_permitted_opens = 0;
123:
1.27 markus 124: /*
125: * If this is true, all opens are permitted. This is the case on the server
126: * on which we have to trust the client anyway, and the user could do
127: * anything after logging in anyway.
128: */
1.1 deraadt 129: static int all_opens_permitted = 0;
130:
1.121 markus 131:
132: /* -- X11 forwarding */
133:
134: /* Maximum number of fake X11 displays to try. */
135: #define MAX_DISPLAYS 1000
136:
1.219 djm 137: /* Saved X11 local (client) display. */
138: static char *x11_saved_display = NULL;
139:
1.121 markus 140: /* Saved X11 authentication protocol name. */
141: static char *x11_saved_proto = NULL;
142:
143: /* Saved X11 authentication data. This is the real data. */
144: static char *x11_saved_data = NULL;
145: static u_int x11_saved_data_len = 0;
146:
147: /*
148: * Fake X11 authentication data. This is what the server will be sending us;
149: * we should replace any occurrences of this by the real data.
150: */
1.240 deraadt 151: static u_char *x11_fake_data = NULL;
1.121 markus 152: static u_int x11_fake_data_len;
153:
154:
155: /* -- agent forwarding */
156:
157: #define NUM_SOCKS 10
158:
1.82 markus 159: /* AF_UNSPEC or AF_INET or AF_INET6 */
1.135 markus 160: static int IPv4or6 = AF_UNSPEC;
1.1 deraadt 161:
1.297 reyk 162: /* Set the routing domain a.k.a. VRF */
163: static int channel_rdomain = -1;
164:
1.121 markus 165: /* helper */
1.127 itojun 166: static void port_open_helper(Channel *c, char *rtype);
1.103 markus 167:
1.276 djm 168: /* non-blocking connect helpers */
169: static int connect_next(struct channel_connect *);
170: static void channel_connect_ctx_free(struct channel_connect *);
171:
1.121 markus 172: /* -- channel core */
1.41 markus 173:
174: Channel *
1.229 markus 175: channel_by_id(int id)
1.41 markus 176: {
177: Channel *c;
1.113 markus 178:
1.209 avsm 179: if (id < 0 || (u_int)id >= channels_alloc) {
1.229 markus 180: logit("channel_by_id: %d: bad id", id);
1.41 markus 181: return NULL;
182: }
1.113 markus 183: c = channels[id];
184: if (c == NULL) {
1.229 markus 185: logit("channel_by_id: %d: bad id: channel free", id);
1.41 markus 186: return NULL;
187: }
188: return c;
1.55 markus 189: }
190:
1.27 markus 191: /*
1.229 markus 192: * Returns the channel if it is allowed to receive protocol messages.
193: * Private channels, like listening sockets, may not receive messages.
194: */
195: Channel *
196: channel_lookup(int id)
197: {
198: Channel *c;
199:
200: if ((c = channel_by_id(id)) == NULL)
201: return (NULL);
202:
1.237 deraadt 203: switch (c->type) {
1.229 markus 204: case SSH_CHANNEL_X11_OPEN:
205: case SSH_CHANNEL_LARVAL:
206: case SSH_CHANNEL_CONNECTING:
207: case SSH_CHANNEL_DYNAMIC:
208: case SSH_CHANNEL_OPENING:
209: case SSH_CHANNEL_OPEN:
210: case SSH_CHANNEL_INPUT_DRAINING:
211: case SSH_CHANNEL_OUTPUT_DRAINING:
212: return (c);
213: }
214: logit("Non-public channel %d, type %d.", id, c->type);
215: return (NULL);
216: }
217:
218: /*
1.55 markus 219: * Register filedescriptors for a channel, used when allocating a channel or
1.52 markus 220: * when the channel consumer/producer is ready, e.g. shell exec'd
1.27 markus 221: */
1.127 itojun 222: static void
1.71 markus 223: channel_register_fds(Channel *c, int rfd, int wfd, int efd,
1.282 dtucker 224: int extusage, int nonblock, int is_tty)
1.1 deraadt 225: {
1.25 markus 226: /* Update the maximum file descriptor value. */
1.84 markus 227: channel_max_fd = MAX(channel_max_fd, rfd);
228: channel_max_fd = MAX(channel_max_fd, wfd);
229: channel_max_fd = MAX(channel_max_fd, efd);
230:
1.298 ! dtucker 231: if (rfd != -1)
! 232: fcntl(rfd, F_SETFD, FD_CLOEXEC);
! 233: if (wfd != -1 && wfd != rfd)
! 234: fcntl(wfd, F_SETFD, FD_CLOEXEC);
! 235: if (efd != -1 && efd != rfd && efd != wfd)
! 236: fcntl(efd, F_SETFD, FD_CLOEXEC);
1.55 markus 237:
1.52 markus 238: c->rfd = rfd;
239: c->wfd = wfd;
240: c->sock = (rfd == wfd) ? rfd : -1;
1.204 djm 241: c->ctl_fd = -1; /* XXX: set elsewhere */
1.52 markus 242: c->efd = efd;
243: c->extended_usage = extusage;
1.71 markus 244:
1.282 dtucker 245: if ((c->isatty = is_tty) != 0)
1.194 markus 246: debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1.91 markus 247:
1.71 markus 248: /* enable nonblocking mode */
249: if (nonblock) {
250: if (rfd != -1)
251: set_nonblock(rfd);
252: if (wfd != -1)
253: set_nonblock(wfd);
254: if (efd != -1)
255: set_nonblock(efd);
256: }
1.52 markus 257: }
258:
259: /*
260: * Allocate a new channel object and set its type and socket. This will cause
261: * remote_name to be freed.
262: */
1.113 markus 263: Channel *
1.52 markus 264: channel_new(char *ctype, int type, int rfd, int wfd, int efd,
1.178 markus 265: u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
1.52 markus 266: {
1.209 avsm 267: int found;
268: u_int i;
1.52 markus 269: Channel *c;
1.25 markus 270:
271: /* Do initial allocation if this is the first call. */
272: if (channels_alloc == 0) {
273: channels_alloc = 10;
1.242 djm 274: channels = xcalloc(channels_alloc, sizeof(Channel *));
1.25 markus 275: for (i = 0; i < channels_alloc; i++)
1.113 markus 276: channels[i] = NULL;
1.25 markus 277: }
278: /* Try to find a free slot where to put the new channel. */
279: for (found = -1, i = 0; i < channels_alloc; i++)
1.113 markus 280: if (channels[i] == NULL) {
1.25 markus 281: /* Found a free slot. */
1.209 avsm 282: found = (int)i;
1.25 markus 283: break;
284: }
1.209 avsm 285: if (found < 0) {
1.27 markus 286: /* There are no free slots. Take last+1 slot and expand the array. */
1.25 markus 287: found = channels_alloc;
1.179 markus 288: if (channels_alloc > 10000)
289: fatal("channel_new: internal error: channels_alloc %d "
290: "too big.", channels_alloc);
1.243 djm 291: channels = xrealloc(channels, channels_alloc + 10,
292: sizeof(Channel *));
1.195 markus 293: channels_alloc += 10;
1.70 markus 294: debug2("channel: expanding %d", channels_alloc);
1.25 markus 295: for (i = found; i < channels_alloc; i++)
1.113 markus 296: channels[i] = NULL;
1.25 markus 297: }
1.113 markus 298: /* Initialize and return new channel. */
1.242 djm 299: c = channels[found] = xcalloc(1, sizeof(Channel));
1.25 markus 300: buffer_init(&c->input);
301: buffer_init(&c->output);
1.41 markus 302: buffer_init(&c->extended);
1.293 djm 303: c->path = NULL;
1.159 markus 304: c->ostate = CHAN_OUTPUT_OPEN;
305: c->istate = CHAN_INPUT_OPEN;
306: c->flags = 0;
1.281 djm 307: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
1.25 markus 308: c->self = found;
309: c->type = type;
1.41 markus 310: c->ctype = ctype;
1.51 markus 311: c->local_window = window;
312: c->local_window_max = window;
313: c->local_consumed = 0;
314: c->local_maxpacket = maxpack;
1.25 markus 315: c->remote_id = -1;
1.190 markus 316: c->remote_name = xstrdup(remote_name);
1.41 markus 317: c->remote_window = 0;
318: c->remote_maxpacket = 0;
1.133 markus 319: c->force_drain = 0;
1.149 markus 320: c->single_connection = 0;
1.131 markus 321: c->detach_user = NULL;
1.225 djm 322: c->detach_close = 0;
1.275 djm 323: c->open_confirm = NULL;
324: c->open_confirm_ctx = NULL;
1.65 markus 325: c->input_filter = NULL;
1.231 reyk 326: c->output_filter = NULL;
1.280 djm 327: c->filter_ctx = NULL;
328: c->filter_cleanup = NULL;
1.275 djm 329: TAILQ_INIT(&c->status_confirms);
1.25 markus 330: debug("channel %d: new [%s]", found, remote_name);
1.113 markus 331: return c;
1.1 deraadt 332: }
1.52 markus 333:
1.132 markus 334: static int
335: channel_find_maxfd(void)
336: {
1.209 avsm 337: u_int i;
338: int max = 0;
1.132 markus 339: Channel *c;
340:
341: for (i = 0; i < channels_alloc; i++) {
342: c = channels[i];
343: if (c != NULL) {
344: max = MAX(max, c->rfd);
345: max = MAX(max, c->wfd);
346: max = MAX(max, c->efd);
347: }
348: }
349: return max;
350: }
351:
352: int
353: channel_close_fd(int *fdp)
354: {
355: int ret = 0, fd = *fdp;
356:
357: if (fd != -1) {
358: ret = close(fd);
359: *fdp = -1;
360: if (fd == channel_max_fd)
361: channel_max_fd = channel_find_maxfd();
362: }
363: return ret;
364: }
365:
1.52 markus 366: /* Close all channel fd/socket. */
1.127 itojun 367: static void
1.52 markus 368: channel_close_fds(Channel *c)
369: {
1.204 djm 370: debug3("channel %d: close_fds r %d w %d e %d c %d",
371: c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
1.118 markus 372:
1.132 markus 373: channel_close_fd(&c->sock);
1.204 djm 374: channel_close_fd(&c->ctl_fd);
1.132 markus 375: channel_close_fd(&c->rfd);
376: channel_close_fd(&c->wfd);
377: channel_close_fd(&c->efd);
1.121 markus 378: }
379:
380: /* Free the channel and close its fd/socket. */
381: void
382: channel_free(Channel *c)
383: {
384: char *s;
1.209 avsm 385: u_int i, n;
1.275 djm 386: struct channel_confirm *cc;
1.121 markus 387:
388: for (n = 0, i = 0; i < channels_alloc; i++)
389: if (channels[i])
390: n++;
1.209 avsm 391: debug("channel %d: free: %s, nchannels %u", c->self,
1.121 markus 392: c->remote_name ? c->remote_name : "???", n);
393:
394: s = channel_open_message();
1.194 markus 395: debug3("channel %d: status: %s", c->self, s);
1.121 markus 396: xfree(s);
397:
398: if (c->sock != -1)
399: shutdown(c->sock, SHUT_RDWR);
1.204 djm 400: if (c->ctl_fd != -1)
401: shutdown(c->ctl_fd, SHUT_RDWR);
1.121 markus 402: channel_close_fds(c);
403: buffer_free(&c->input);
404: buffer_free(&c->output);
405: buffer_free(&c->extended);
406: if (c->remote_name) {
407: xfree(c->remote_name);
408: c->remote_name = NULL;
409: }
1.293 djm 410: if (c->path) {
411: xfree(c->path);
412: c->path = NULL;
413: }
1.275 djm 414: while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
415: if (cc->abandon_cb != NULL)
416: cc->abandon_cb(c, cc->ctx);
417: TAILQ_REMOVE(&c->status_confirms, cc, entry);
418: bzero(cc, sizeof(*cc));
419: xfree(cc);
420: }
1.280 djm 421: if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
422: c->filter_cleanup(c->self, c->filter_ctx);
1.121 markus 423: channels[c->self] = NULL;
424: xfree(c);
425: }
426:
427: void
1.126 markus 428: channel_free_all(void)
1.121 markus 429: {
1.209 avsm 430: u_int i;
1.121 markus 431:
1.126 markus 432: for (i = 0; i < channels_alloc; i++)
433: if (channels[i] != NULL)
434: channel_free(channels[i]);
1.131 markus 435: }
436:
1.121 markus 437: /*
438: * Closes the sockets/fds of all channels. This is used to close extra file
439: * descriptors after a fork.
440: */
441: void
1.142 itojun 442: channel_close_all(void)
1.121 markus 443: {
1.209 avsm 444: u_int i;
1.121 markus 445:
446: for (i = 0; i < channels_alloc; i++)
447: if (channels[i] != NULL)
448: channel_close_fds(channels[i]);
449: }
450:
451: /*
1.131 markus 452: * Stop listening to channels.
453: */
454: void
455: channel_stop_listening(void)
456: {
1.209 avsm 457: u_int i;
1.131 markus 458: Channel *c;
459:
460: for (i = 0; i < channels_alloc; i++) {
461: c = channels[i];
462: if (c != NULL) {
463: switch (c->type) {
464: case SSH_CHANNEL_AUTH_SOCKET:
465: case SSH_CHANNEL_PORT_LISTENER:
466: case SSH_CHANNEL_RPORT_LISTENER:
467: case SSH_CHANNEL_X11_LISTENER:
1.132 markus 468: channel_close_fd(&c->sock);
1.131 markus 469: channel_free(c);
470: break;
471: }
472: }
473: }
474: }
475:
476: /*
1.121 markus 477: * Returns true if no channel has too much buffered data, and false if one or
478: * more channel is overfull.
479: */
480: int
1.142 itojun 481: channel_not_very_much_buffered_data(void)
1.121 markus 482: {
483: u_int i;
484: Channel *c;
485:
486: for (i = 0; i < channels_alloc; i++) {
487: c = channels[i];
488: if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
1.136 markus 489: #if 0
490: if (!compat20 &&
491: buffer_len(&c->input) > packet_get_maxsize()) {
1.185 markus 492: debug2("channel %d: big input buffer %d",
1.121 markus 493: c->self, buffer_len(&c->input));
494: return 0;
495: }
1.136 markus 496: #endif
1.121 markus 497: if (buffer_len(&c->output) > packet_get_maxsize()) {
1.191 markus 498: debug2("channel %d: big output buffer %u > %u",
1.136 markus 499: c->self, buffer_len(&c->output),
500: packet_get_maxsize());
1.121 markus 501: return 0;
502: }
503: }
504: }
505: return 1;
506: }
507:
508: /* Returns true if any channel is still open. */
509: int
1.142 itojun 510: channel_still_open(void)
1.121 markus 511: {
1.209 avsm 512: u_int i;
1.121 markus 513: Channel *c;
514:
515: for (i = 0; i < channels_alloc; i++) {
516: c = channels[i];
517: if (c == NULL)
518: continue;
519: switch (c->type) {
520: case SSH_CHANNEL_X11_LISTENER:
521: case SSH_CHANNEL_PORT_LISTENER:
522: case SSH_CHANNEL_RPORT_LISTENER:
523: case SSH_CHANNEL_CLOSED:
524: case SSH_CHANNEL_AUTH_SOCKET:
525: case SSH_CHANNEL_DYNAMIC:
526: case SSH_CHANNEL_CONNECTING:
527: case SSH_CHANNEL_ZOMBIE:
528: continue;
529: case SSH_CHANNEL_LARVAL:
530: if (!compat20)
531: fatal("cannot happen: SSH_CHANNEL_LARVAL");
532: continue;
533: case SSH_CHANNEL_OPENING:
534: case SSH_CHANNEL_OPEN:
535: case SSH_CHANNEL_X11_OPEN:
536: return 1;
537: case SSH_CHANNEL_INPUT_DRAINING:
538: case SSH_CHANNEL_OUTPUT_DRAINING:
539: if (!compat13)
540: fatal("cannot happen: OUT_DRAIN");
541: return 1;
542: default:
543: fatal("channel_still_open: bad channel type %d", c->type);
544: /* NOTREACHED */
545: }
546: }
547: return 0;
548: }
549:
550: /* Returns the id of an open channel suitable for keepaliving */
551: int
1.142 itojun 552: channel_find_open(void)
1.121 markus 553: {
1.209 avsm 554: u_int i;
1.121 markus 555: Channel *c;
556:
557: for (i = 0; i < channels_alloc; i++) {
558: c = channels[i];
1.206 djm 559: if (c == NULL || c->remote_id < 0)
1.121 markus 560: continue;
561: switch (c->type) {
562: case SSH_CHANNEL_CLOSED:
563: case SSH_CHANNEL_DYNAMIC:
564: case SSH_CHANNEL_X11_LISTENER:
565: case SSH_CHANNEL_PORT_LISTENER:
566: case SSH_CHANNEL_RPORT_LISTENER:
567: case SSH_CHANNEL_OPENING:
568: case SSH_CHANNEL_CONNECTING:
569: case SSH_CHANNEL_ZOMBIE:
570: continue;
571: case SSH_CHANNEL_LARVAL:
572: case SSH_CHANNEL_AUTH_SOCKET:
573: case SSH_CHANNEL_OPEN:
574: case SSH_CHANNEL_X11_OPEN:
575: return i;
576: case SSH_CHANNEL_INPUT_DRAINING:
577: case SSH_CHANNEL_OUTPUT_DRAINING:
578: if (!compat13)
579: fatal("cannot happen: OUT_DRAIN");
580: return i;
581: default:
582: fatal("channel_find_open: bad channel type %d", c->type);
583: /* NOTREACHED */
584: }
585: }
586: return -1;
587: }
588:
589:
590: /*
591: * Returns a message describing the currently open forwarded connections,
592: * suitable for sending to the client. The message contains crlf pairs for
593: * newlines.
594: */
595: char *
1.142 itojun 596: channel_open_message(void)
1.121 markus 597: {
598: Buffer buffer;
599: Channel *c;
600: char buf[1024], *cp;
1.209 avsm 601: u_int i;
1.121 markus 602:
603: buffer_init(&buffer);
604: snprintf(buf, sizeof buf, "The following connections are open:\r\n");
605: buffer_append(&buffer, buf, strlen(buf));
606: for (i = 0; i < channels_alloc; i++) {
607: c = channels[i];
608: if (c == NULL)
609: continue;
610: switch (c->type) {
611: case SSH_CHANNEL_X11_LISTENER:
612: case SSH_CHANNEL_PORT_LISTENER:
613: case SSH_CHANNEL_RPORT_LISTENER:
614: case SSH_CHANNEL_CLOSED:
615: case SSH_CHANNEL_AUTH_SOCKET:
616: case SSH_CHANNEL_ZOMBIE:
617: continue;
618: case SSH_CHANNEL_LARVAL:
619: case SSH_CHANNEL_OPENING:
620: case SSH_CHANNEL_CONNECTING:
621: case SSH_CHANNEL_DYNAMIC:
622: case SSH_CHANNEL_OPEN:
623: case SSH_CHANNEL_X11_OPEN:
624: case SSH_CHANNEL_INPUT_DRAINING:
625: case SSH_CHANNEL_OUTPUT_DRAINING:
1.204 djm 626: snprintf(buf, sizeof buf,
627: " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
1.121 markus 628: c->self, c->remote_name,
629: c->type, c->remote_id,
630: c->istate, buffer_len(&c->input),
631: c->ostate, buffer_len(&c->output),
1.204 djm 632: c->rfd, c->wfd, c->ctl_fd);
1.121 markus 633: buffer_append(&buffer, buf, strlen(buf));
634: continue;
635: default:
636: fatal("channel_open_message: bad channel type %d", c->type);
637: /* NOTREACHED */
638: }
639: }
640: buffer_append(&buffer, "\0", 1);
641: cp = xstrdup(buffer_ptr(&buffer));
642: buffer_free(&buffer);
643: return cp;
644: }
645:
646: void
647: channel_send_open(int id)
648: {
649: Channel *c = channel_lookup(id);
1.180 deraadt 650:
1.121 markus 651: if (c == NULL) {
1.188 itojun 652: logit("channel_send_open: %d: bad id", id);
1.121 markus 653: return;
654: }
1.184 markus 655: debug2("channel %d: send open", id);
1.121 markus 656: packet_start(SSH2_MSG_CHANNEL_OPEN);
657: packet_put_cstring(c->ctype);
658: packet_put_int(c->self);
659: packet_put_int(c->local_window);
660: packet_put_int(c->local_maxpacket);
661: packet_send();
662: }
663:
664: void
1.184 markus 665: channel_request_start(int id, char *service, int wantconfirm)
1.121 markus 666: {
1.184 markus 667: Channel *c = channel_lookup(id);
1.180 deraadt 668:
1.121 markus 669: if (c == NULL) {
1.188 itojun 670: logit("channel_request_start: %d: unknown channel id", id);
1.121 markus 671: return;
672: }
1.204 djm 673: debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
1.121 markus 674: packet_start(SSH2_MSG_CHANNEL_REQUEST);
675: packet_put_int(c->remote_id);
676: packet_put_cstring(service);
677: packet_put_char(wantconfirm);
678: }
1.241 deraadt 679:
1.121 markus 680: void
1.275 djm 681: channel_register_status_confirm(int id, channel_confirm_cb *cb,
682: channel_confirm_abandon_cb *abandon_cb, void *ctx)
683: {
684: struct channel_confirm *cc;
685: Channel *c;
686:
687: if ((c = channel_lookup(id)) == NULL)
688: fatal("channel_register_expect: %d: bad id", id);
689:
690: cc = xmalloc(sizeof(*cc));
691: cc->cb = cb;
692: cc->abandon_cb = abandon_cb;
693: cc->ctx = ctx;
694: TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
695: }
696:
697: void
698: channel_register_open_confirm(int id, channel_callback_fn *fn, void *ctx)
1.121 markus 699: {
700: Channel *c = channel_lookup(id);
1.180 deraadt 701:
1.121 markus 702: if (c == NULL) {
1.287 stevesk 703: logit("channel_register_open_confirm: %d: bad id", id);
1.121 markus 704: return;
705: }
1.275 djm 706: c->open_confirm = fn;
707: c->open_confirm_ctx = ctx;
1.121 markus 708: }
1.241 deraadt 709:
1.121 markus 710: void
1.225 djm 711: channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
1.121 markus 712: {
1.229 markus 713: Channel *c = channel_by_id(id);
1.180 deraadt 714:
1.121 markus 715: if (c == NULL) {
1.188 itojun 716: logit("channel_register_cleanup: %d: bad id", id);
1.121 markus 717: return;
718: }
1.131 markus 719: c->detach_user = fn;
1.225 djm 720: c->detach_close = do_close;
1.121 markus 721: }
1.241 deraadt 722:
1.121 markus 723: void
724: channel_cancel_cleanup(int id)
725: {
1.229 markus 726: Channel *c = channel_by_id(id);
1.180 deraadt 727:
1.121 markus 728: if (c == NULL) {
1.188 itojun 729: logit("channel_cancel_cleanup: %d: bad id", id);
1.121 markus 730: return;
1.52 markus 731: }
1.131 markus 732: c->detach_user = NULL;
1.225 djm 733: c->detach_close = 0;
1.121 markus 734: }
1.241 deraadt 735:
1.121 markus 736: void
1.231 reyk 737: channel_register_filter(int id, channel_infilter_fn *ifn,
1.280 djm 738: channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
1.121 markus 739: {
740: Channel *c = channel_lookup(id);
1.180 deraadt 741:
1.121 markus 742: if (c == NULL) {
1.188 itojun 743: logit("channel_register_filter: %d: bad id", id);
1.121 markus 744: return;
1.52 markus 745: }
1.231 reyk 746: c->input_filter = ifn;
747: c->output_filter = ofn;
1.279 djm 748: c->filter_ctx = ctx;
1.280 djm 749: c->filter_cleanup = cfn;
1.52 markus 750: }
751:
1.49 markus 752: void
1.121 markus 753: channel_set_fds(int id, int rfd, int wfd, int efd,
1.282 dtucker 754: int extusage, int nonblock, int is_tty, u_int window_max)
1.1 deraadt 755: {
1.121 markus 756: Channel *c = channel_lookup(id);
1.180 deraadt 757:
1.121 markus 758: if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
759: fatal("channel_activate for non-larval channel %d.", id);
1.282 dtucker 760: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
1.121 markus 761: c->type = SSH_CHANNEL_OPEN;
1.168 markus 762: c->local_window = c->local_window_max = window_max;
1.121 markus 763: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
764: packet_put_int(c->remote_id);
765: packet_put_int(c->local_window);
766: packet_send();
1.1 deraadt 767: }
768:
1.27 markus 769: /*
1.41 markus 770: * 'channel_pre*' are called just before select() to add any bits relevant to
771: * channels in the select bitmasks.
772: */
773: /*
774: * 'channel_post*': perform any appropriate operations for channels which
775: * have events pending.
1.27 markus 776: */
1.237 deraadt 777: typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
1.41 markus 778: chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
779: chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
780:
1.259 stevesk 781: /* ARGSUSED */
1.127 itojun 782: static void
1.237 deraadt 783: channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 784: {
785: FD_SET(c->sock, readset);
786: }
787:
1.259 stevesk 788: /* ARGSUSED */
1.127 itojun 789: static void
1.237 deraadt 790: channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
1.75 markus 791: {
792: debug3("channel %d: waiting for connection", c->self);
793: FD_SET(c->sock, writeset);
794: }
795:
1.127 itojun 796: static void
1.237 deraadt 797: channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 798: {
799: if (buffer_len(&c->input) < packet_get_maxsize())
800: FD_SET(c->sock, readset);
801: if (buffer_len(&c->output) > 0)
802: FD_SET(c->sock, writeset);
803: }
1.1 deraadt 804:
1.127 itojun 805: static void
1.237 deraadt 806: channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 807: {
1.157 markus 808: u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
1.41 markus 809:
1.44 markus 810: if (c->istate == CHAN_INPUT_OPEN &&
1.157 markus 811: limit > 0 &&
1.250 djm 812: buffer_len(&c->input) < limit &&
813: buffer_check_alloc(&c->input, CHAN_RBUF))
1.44 markus 814: FD_SET(c->rfd, readset);
815: if (c->ostate == CHAN_OUTPUT_OPEN ||
816: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
817: if (buffer_len(&c->output) > 0) {
818: FD_SET(c->wfd, writeset);
819: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.172 markus 820: if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.223 djm 821: debug2("channel %d: obuf_empty delayed efd %d/(%d)",
822: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 823: else
824: chan_obuf_empty(c);
1.44 markus 825: }
826: }
827: /** XXX check close conditions, too */
1.277 markus 828: if (compat20 && c->efd != -1 &&
829: !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
1.44 markus 830: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
831: buffer_len(&c->extended) > 0)
832: FD_SET(c->efd, writeset);
1.172 markus 833: else if (!(c->flags & CHAN_EOF_SENT) &&
834: c->extended_usage == CHAN_EXTENDED_READ &&
1.44 markus 835: buffer_len(&c->extended) < c->remote_window)
836: FD_SET(c->efd, readset);
837: }
1.204 djm 838: /* XXX: What about efd? races? */
1.208 deraadt 839: if (compat20 && c->ctl_fd != -1 &&
1.204 djm 840: c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
841: FD_SET(c->ctl_fd, readset);
1.44 markus 842: }
843:
1.259 stevesk 844: /* ARGSUSED */
1.127 itojun 845: static void
1.237 deraadt 846: channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 847: {
848: if (buffer_len(&c->input) == 0) {
849: packet_start(SSH_MSG_CHANNEL_CLOSE);
850: packet_put_int(c->remote_id);
851: packet_send();
852: c->type = SSH_CHANNEL_CLOSED;
1.194 markus 853: debug2("channel %d: closing after input drain.", c->self);
1.41 markus 854: }
855: }
856:
1.259 stevesk 857: /* ARGSUSED */
1.127 itojun 858: static void
1.237 deraadt 859: channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 860: {
861: if (buffer_len(&c->output) == 0)
1.113 markus 862: chan_mark_dead(c);
1.49 markus 863: else
1.41 markus 864: FD_SET(c->sock, writeset);
865: }
866:
867: /*
868: * This is a special state for X11 authentication spoofing. An opened X11
869: * connection (when authentication spoofing is being done) remains in this
870: * state until the first packet has been completely read. The authentication
871: * data in that packet is then substituted by the real data if it matches the
872: * fake data, and the channel is put into normal mode.
1.51 markus 873: * XXX All this happens at the client side.
1.121 markus 874: * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41 markus 875: */
1.127 itojun 876: static int
1.121 markus 877: x11_open_helper(Buffer *b)
1.1 deraadt 878: {
1.77 markus 879: u_char *ucp;
880: u_int proto_len, data_len;
1.25 markus 881:
1.41 markus 882: /* Check if the fixed size part of the packet is in buffer. */
1.121 markus 883: if (buffer_len(b) < 12)
1.41 markus 884: return 0;
885:
886: /* Parse the lengths of variable-length fields. */
1.155 stevesk 887: ucp = buffer_ptr(b);
1.41 markus 888: if (ucp[0] == 0x42) { /* Byte order MSB first. */
889: proto_len = 256 * ucp[6] + ucp[7];
890: data_len = 256 * ucp[8] + ucp[9];
891: } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
892: proto_len = ucp[6] + 256 * ucp[7];
893: data_len = ucp[8] + 256 * ucp[9];
894: } else {
1.194 markus 895: debug2("Initial X11 packet contains bad byte order byte: 0x%x",
1.148 deraadt 896: ucp[0]);
1.41 markus 897: return -1;
898: }
899:
900: /* Check if the whole packet is in buffer. */
1.121 markus 901: if (buffer_len(b) <
1.41 markus 902: 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
903: return 0;
904:
905: /* Check if authentication protocol matches. */
906: if (proto_len != strlen(x11_saved_proto) ||
907: memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
1.194 markus 908: debug2("X11 connection uses different authentication protocol.");
1.41 markus 909: return -1;
910: }
911: /* Check if authentication data matches our fake data. */
912: if (data_len != x11_fake_data_len ||
913: memcmp(ucp + 12 + ((proto_len + 3) & ~3),
914: x11_fake_data, x11_fake_data_len) != 0) {
1.194 markus 915: debug2("X11 auth data does not match fake data.");
1.41 markus 916: return -1;
917: }
918: /* Check fake data length */
919: if (x11_fake_data_len != x11_saved_data_len) {
920: error("X11 fake_data_len %d != saved_data_len %d",
921: x11_fake_data_len, x11_saved_data_len);
922: return -1;
923: }
924: /*
925: * Received authentication protocol and data match
926: * our fake data. Substitute the fake data with real
927: * data.
928: */
929: memcpy(ucp + 12 + ((proto_len + 3) & ~3),
930: x11_saved_data, x11_saved_data_len);
931: return 1;
932: }
933:
1.127 itojun 934: static void
1.237 deraadt 935: channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 936: {
1.121 markus 937: int ret = x11_open_helper(&c->output);
1.180 deraadt 938:
1.41 markus 939: if (ret == 1) {
940: /* Start normal processing for the channel. */
941: c->type = SSH_CHANNEL_OPEN;
1.47 markus 942: channel_pre_open_13(c, readset, writeset);
1.41 markus 943: } else if (ret == -1) {
944: /*
945: * We have received an X11 connection that has bad
946: * authentication information.
947: */
1.188 itojun 948: logit("X11 connection rejected because of wrong authentication.");
1.41 markus 949: buffer_clear(&c->input);
950: buffer_clear(&c->output);
1.132 markus 951: channel_close_fd(&c->sock);
1.41 markus 952: c->sock = -1;
953: c->type = SSH_CHANNEL_CLOSED;
954: packet_start(SSH_MSG_CHANNEL_CLOSE);
955: packet_put_int(c->remote_id);
956: packet_send();
957: }
958: }
1.25 markus 959:
1.127 itojun 960: static void
1.237 deraadt 961: channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 962: {
1.121 markus 963: int ret = x11_open_helper(&c->output);
1.133 markus 964:
965: /* c->force_drain = 1; */
966:
1.41 markus 967: if (ret == 1) {
968: c->type = SSH_CHANNEL_OPEN;
1.157 markus 969: channel_pre_open(c, readset, writeset);
1.41 markus 970: } else if (ret == -1) {
1.188 itojun 971: logit("X11 connection rejected because of wrong authentication.");
1.194 markus 972: debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
1.156 markus 973: chan_read_failed(c);
974: buffer_clear(&c->input);
975: chan_ibuf_empty(c);
976: buffer_clear(&c->output);
977: /* for proto v1, the peer will send an IEOF */
978: if (compat20)
979: chan_write_failed(c);
980: else
981: c->type = SSH_CHANNEL_OPEN;
1.194 markus 982: debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
1.41 markus 983: }
984: }
1.25 markus 985:
1.104 markus 986: /* try to decode a socks4 header */
1.259 stevesk 987: /* ARGSUSED */
1.127 itojun 988: static int
1.237 deraadt 989: channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
1.103 markus 990: {
1.181 markus 991: char *p, *host;
1.292 djm 992: u_int len, have, i, found, need;
1.148 deraadt 993: char username[256];
1.103 markus 994: struct {
995: u_int8_t version;
996: u_int8_t command;
997: u_int16_t dest_port;
1.104 markus 998: struct in_addr dest_addr;
1.103 markus 999: } s4_req, s4_rsp;
1000:
1.104 markus 1001: debug2("channel %d: decode socks4", c->self);
1.109 markus 1002:
1003: have = buffer_len(&c->input);
1004: len = sizeof(s4_req);
1005: if (have < len)
1006: return 0;
1007: p = buffer_ptr(&c->input);
1.292 djm 1008:
1009: need = 1;
1010: /* SOCKS4A uses an invalid IP address 0.0.0.x */
1011: if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1012: debug2("channel %d: socks4a request", c->self);
1013: /* ... and needs an extra string (the hostname) */
1014: need = 2;
1015: }
1016: /* Check for terminating NUL on the string(s) */
1.109 markus 1017: for (found = 0, i = len; i < have; i++) {
1018: if (p[i] == '\0') {
1.292 djm 1019: found++;
1020: if (found == need)
1021: break;
1.109 markus 1022: }
1023: if (i > 1024) {
1024: /* the peer is probably sending garbage */
1025: debug("channel %d: decode socks4: too long",
1026: c->self);
1027: return -1;
1028: }
1029: }
1.292 djm 1030: if (found < need)
1.109 markus 1031: return 0;
1.103 markus 1032: buffer_get(&c->input, (char *)&s4_req.version, 1);
1033: buffer_get(&c->input, (char *)&s4_req.command, 1);
1034: buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
1.104 markus 1035: buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
1.109 markus 1036: have = buffer_len(&c->input);
1.103 markus 1037: p = buffer_ptr(&c->input);
1038: len = strlen(p);
1.106 markus 1039: debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.292 djm 1040: len++; /* trailing '\0' */
1.103 markus 1041: if (len > have)
1.104 markus 1042: fatal("channel %d: decode socks4: len %d > have %d",
1.103 markus 1043: c->self, len, have);
1044: strlcpy(username, p, sizeof(username));
1045: buffer_consume(&c->input, len);
1046:
1.293 djm 1047: if (c->path != NULL) {
1048: xfree(c->path);
1049: c->path = NULL;
1050: }
1.292 djm 1051: if (need == 1) { /* SOCKS4: one string */
1052: host = inet_ntoa(s4_req.dest_addr);
1.293 djm 1053: c->path = xstrdup(host);
1.292 djm 1054: } else { /* SOCKS4A: two strings */
1055: have = buffer_len(&c->input);
1056: p = buffer_ptr(&c->input);
1057: len = strlen(p);
1058: debug2("channel %d: decode socks4a: host %s/%d",
1059: c->self, p, len);
1060: len++; /* trailing '\0' */
1061: if (len > have)
1062: fatal("channel %d: decode socks4a: len %d > have %d",
1063: c->self, len, have);
1.293 djm 1064: if (len > NI_MAXHOST) {
1.292 djm 1065: error("channel %d: hostname \"%.100s\" too long",
1066: c->self, p);
1067: return -1;
1068: }
1.293 djm 1069: c->path = xstrdup(p);
1.292 djm 1070: buffer_consume(&c->input, len);
1071: }
1.103 markus 1072: c->host_port = ntohs(s4_req.dest_port);
1.148 deraadt 1073:
1.194 markus 1074: debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1.292 djm 1075: c->self, c->path, c->host_port, s4_req.command);
1.103 markus 1076:
1.104 markus 1077: if (s4_req.command != 1) {
1.292 djm 1078: debug("channel %d: cannot handle: %s cn %d",
1079: c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
1.104 markus 1080: return -1;
1.103 markus 1081: }
1.104 markus 1082: s4_rsp.version = 0; /* vn: 0 for reply */
1083: s4_rsp.command = 90; /* cd: req granted */
1.103 markus 1084: s4_rsp.dest_port = 0; /* ignored */
1.104 markus 1085: s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1.246 deraadt 1086: buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
1.104 markus 1087: return 1;
1088: }
1089:
1.193 markus 1090: /* try to decode a socks5 header */
1091: #define SSH_SOCKS5_AUTHDONE 0x1000
1092: #define SSH_SOCKS5_NOAUTH 0x00
1093: #define SSH_SOCKS5_IPV4 0x01
1094: #define SSH_SOCKS5_DOMAIN 0x03
1095: #define SSH_SOCKS5_IPV6 0x04
1096: #define SSH_SOCKS5_CONNECT 0x01
1097: #define SSH_SOCKS5_SUCCESS 0x00
1098:
1.259 stevesk 1099: /* ARGSUSED */
1.193 markus 1100: static int
1.237 deraadt 1101: channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
1.193 markus 1102: {
1103: struct {
1104: u_int8_t version;
1105: u_int8_t command;
1106: u_int8_t reserved;
1107: u_int8_t atyp;
1108: } s5_req, s5_rsp;
1109: u_int16_t dest_port;
1.293 djm 1110: u_char *p, dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1.252 djm 1111: u_int have, need, i, found, nmethods, addrlen, af;
1.193 markus 1112:
1113: debug2("channel %d: decode socks5", c->self);
1114: p = buffer_ptr(&c->input);
1115: if (p[0] != 0x05)
1116: return -1;
1117: have = buffer_len(&c->input);
1118: if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1119: /* format: ver | nmethods | methods */
1.198 djm 1120: if (have < 2)
1.193 markus 1121: return 0;
1122: nmethods = p[1];
1123: if (have < nmethods + 2)
1124: return 0;
1125: /* look for method: "NO AUTHENTICATION REQUIRED" */
1.268 stevesk 1126: for (found = 0, i = 2; i < nmethods + 2; i++) {
1.264 stevesk 1127: if (p[i] == SSH_SOCKS5_NOAUTH) {
1.193 markus 1128: found = 1;
1129: break;
1130: }
1131: }
1132: if (!found) {
1133: debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1134: c->self);
1135: return -1;
1136: }
1137: buffer_consume(&c->input, nmethods + 2);
1138: buffer_put_char(&c->output, 0x05); /* version */
1139: buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1140: FD_SET(c->sock, writeset);
1141: c->flags |= SSH_SOCKS5_AUTHDONE;
1142: debug2("channel %d: socks5 auth done", c->self);
1143: return 0; /* need more */
1144: }
1145: debug2("channel %d: socks5 post auth", c->self);
1146: if (have < sizeof(s5_req)+1)
1147: return 0; /* need more */
1.247 deraadt 1148: memcpy(&s5_req, p, sizeof(s5_req));
1.193 markus 1149: if (s5_req.version != 0x05 ||
1150: s5_req.command != SSH_SOCKS5_CONNECT ||
1151: s5_req.reserved != 0x00) {
1.194 markus 1152: debug2("channel %d: only socks5 connect supported", c->self);
1.193 markus 1153: return -1;
1154: }
1.213 deraadt 1155: switch (s5_req.atyp){
1.193 markus 1156: case SSH_SOCKS5_IPV4:
1157: addrlen = 4;
1158: af = AF_INET;
1159: break;
1160: case SSH_SOCKS5_DOMAIN:
1161: addrlen = p[sizeof(s5_req)];
1162: af = -1;
1163: break;
1164: case SSH_SOCKS5_IPV6:
1165: addrlen = 16;
1166: af = AF_INET6;
1167: break;
1168: default:
1.194 markus 1169: debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1.193 markus 1170: return -1;
1171: }
1.252 djm 1172: need = sizeof(s5_req) + addrlen + 2;
1173: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1174: need++;
1175: if (have < need)
1.193 markus 1176: return 0;
1177: buffer_consume(&c->input, sizeof(s5_req));
1178: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1179: buffer_consume(&c->input, 1); /* host string length */
1180: buffer_get(&c->input, (char *)&dest_addr, addrlen);
1181: buffer_get(&c->input, (char *)&dest_port, 2);
1182: dest_addr[addrlen] = '\0';
1.293 djm 1183: if (c->path != NULL) {
1184: xfree(c->path);
1185: c->path = NULL;
1186: }
1187: if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1.294 djm 1188: if (addrlen >= NI_MAXHOST) {
1.293 djm 1189: error("channel %d: dynamic request: socks5 hostname "
1190: "\"%.100s\" too long", c->self, dest_addr);
1191: return -1;
1192: }
1193: c->path = xstrdup(dest_addr);
1194: } else {
1195: if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1196: return -1;
1197: c->path = xstrdup(ntop);
1198: }
1.193 markus 1199: c->host_port = ntohs(dest_port);
1.198 djm 1200:
1.194 markus 1201: debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1.193 markus 1202: c->self, c->path, c->host_port, s5_req.command);
1203:
1204: s5_rsp.version = 0x05;
1205: s5_rsp.command = SSH_SOCKS5_SUCCESS;
1206: s5_rsp.reserved = 0; /* ignored */
1207: s5_rsp.atyp = SSH_SOCKS5_IPV4;
1208: ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1209: dest_port = 0; /* ignored */
1210:
1.246 deraadt 1211: buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1212: buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1213: buffer_append(&c->output, &dest_port, sizeof(dest_port));
1.193 markus 1214: return 1;
1215: }
1216:
1.104 markus 1217: /* dynamic port forwarding */
1.127 itojun 1218: static void
1.237 deraadt 1219: channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
1.104 markus 1220: {
1221: u_char *p;
1.217 djm 1222: u_int have;
1223: int ret;
1.103 markus 1224:
1.104 markus 1225: have = buffer_len(&c->input);
1.137 markus 1226: c->delayed = 0;
1.104 markus 1227: debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.105 markus 1228: /* buffer_dump(&c->input); */
1.104 markus 1229: /* check if the fixed size part of the packet is in buffer. */
1.193 markus 1230: if (have < 3) {
1.104 markus 1231: /* need more */
1232: FD_SET(c->sock, readset);
1233: return;
1234: }
1235: /* try to guess the protocol */
1236: p = buffer_ptr(&c->input);
1237: switch (p[0]) {
1238: case 0x04:
1239: ret = channel_decode_socks4(c, readset, writeset);
1.193 markus 1240: break;
1241: case 0x05:
1242: ret = channel_decode_socks5(c, readset, writeset);
1.104 markus 1243: break;
1244: default:
1245: ret = -1;
1246: break;
1247: }
1248: if (ret < 0) {
1.113 markus 1249: chan_mark_dead(c);
1.104 markus 1250: } else if (ret == 0) {
1251: debug2("channel %d: pre_dynamic: need more", c->self);
1252: /* need more */
1253: FD_SET(c->sock, readset);
1254: } else {
1255: /* switch to the next state */
1256: c->type = SSH_CHANNEL_OPENING;
1257: port_open_helper(c, "direct-tcpip");
1258: }
1.103 markus 1259: }
1260:
1.41 markus 1261: /* This is our fake X11 server socket. */
1.259 stevesk 1262: /* ARGSUSED */
1.127 itojun 1263: static void
1.237 deraadt 1264: channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1265: {
1.113 markus 1266: Channel *nc;
1.285 djm 1267: struct sockaddr_storage addr;
1.162 stevesk 1268: int newsock;
1.41 markus 1269: socklen_t addrlen;
1.85 markus 1270: char buf[16384], *remote_ipaddr;
1.51 markus 1271: int remote_port;
1.25 markus 1272:
1.41 markus 1273: if (FD_ISSET(c->sock, readset)) {
1274: debug("X11 connection requested.");
1275: addrlen = sizeof(addr);
1.285 djm 1276: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.149 markus 1277: if (c->single_connection) {
1.194 markus 1278: debug2("single_connection: closing X11 listener.");
1.149 markus 1279: channel_close_fd(&c->sock);
1280: chan_mark_dead(c);
1281: }
1.41 markus 1282: if (newsock < 0) {
1283: error("accept: %.100s", strerror(errno));
1284: return;
1285: }
1.162 stevesk 1286: set_nodelay(newsock);
1.85 markus 1287: remote_ipaddr = get_peer_ipaddr(newsock);
1.51 markus 1288: remote_port = get_peer_port(newsock);
1.41 markus 1289: snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1.85 markus 1290: remote_ipaddr, remote_port);
1.51 markus 1291:
1.113 markus 1292: nc = channel_new("accepted x11 socket",
1.51 markus 1293: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1.190 markus 1294: c->local_window_max, c->local_maxpacket, 0, buf, 1);
1.51 markus 1295: if (compat20) {
1296: packet_start(SSH2_MSG_CHANNEL_OPEN);
1297: packet_put_cstring("x11");
1.113 markus 1298: packet_put_int(nc->self);
1.149 markus 1299: packet_put_int(nc->local_window_max);
1300: packet_put_int(nc->local_maxpacket);
1.85 markus 1301: /* originator ipaddr and port */
1302: packet_put_cstring(remote_ipaddr);
1.57 markus 1303: if (datafellows & SSH_BUG_X11FWD) {
1.194 markus 1304: debug2("ssh2 x11 bug compat mode");
1.57 markus 1305: } else {
1306: packet_put_int(remote_port);
1307: }
1.51 markus 1308: packet_send();
1309: } else {
1310: packet_start(SSH_SMSG_X11_OPEN);
1.113 markus 1311: packet_put_int(nc->self);
1.121 markus 1312: if (packet_get_protocol_flags() &
1313: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.125 markus 1314: packet_put_cstring(buf);
1.51 markus 1315: packet_send();
1316: }
1.85 markus 1317: xfree(remote_ipaddr);
1.41 markus 1318: }
1319: }
1.25 markus 1320:
1.127 itojun 1321: static void
1.103 markus 1322: port_open_helper(Channel *c, char *rtype)
1323: {
1324: int direct;
1325: char buf[1024];
1326: char *remote_ipaddr = get_peer_ipaddr(c->sock);
1.216 markus 1327: int remote_port = get_peer_port(c->sock);
1.103 markus 1328:
1329: direct = (strcmp(rtype, "direct-tcpip") == 0);
1330:
1331: snprintf(buf, sizeof buf,
1332: "%s: listening port %d for %.100s port %d, "
1333: "connect from %.200s port %d",
1334: rtype, c->listening_port, c->path, c->host_port,
1335: remote_ipaddr, remote_port);
1336:
1337: xfree(c->remote_name);
1338: c->remote_name = xstrdup(buf);
1339:
1340: if (compat20) {
1341: packet_start(SSH2_MSG_CHANNEL_OPEN);
1342: packet_put_cstring(rtype);
1343: packet_put_int(c->self);
1344: packet_put_int(c->local_window_max);
1345: packet_put_int(c->local_maxpacket);
1346: if (direct) {
1347: /* target host, port */
1348: packet_put_cstring(c->path);
1349: packet_put_int(c->host_port);
1350: } else {
1351: /* listen address, port */
1352: packet_put_cstring(c->path);
1353: packet_put_int(c->listening_port);
1354: }
1355: /* originator host and port */
1356: packet_put_cstring(remote_ipaddr);
1.216 markus 1357: packet_put_int((u_int)remote_port);
1.103 markus 1358: packet_send();
1359: } else {
1360: packet_start(SSH_MSG_PORT_OPEN);
1361: packet_put_int(c->self);
1362: packet_put_cstring(c->path);
1363: packet_put_int(c->host_port);
1.121 markus 1364: if (packet_get_protocol_flags() &
1365: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.103 markus 1366: packet_put_cstring(c->remote_name);
1367: packet_send();
1368: }
1369: xfree(remote_ipaddr);
1370: }
1371:
1.226 djm 1372: static void
1373: channel_set_reuseaddr(int fd)
1374: {
1375: int on = 1;
1376:
1377: /*
1378: * Set socket options.
1379: * Allow local port reuse in TIME_WAIT.
1380: */
1381: if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1382: error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1383: }
1384:
1.41 markus 1385: /*
1386: * This socket is listening for connections to a forwarded TCP/IP port.
1387: */
1.259 stevesk 1388: /* ARGSUSED */
1.127 itojun 1389: static void
1.237 deraadt 1390: channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1391: {
1.103 markus 1392: Channel *nc;
1.285 djm 1393: struct sockaddr_storage addr;
1.113 markus 1394: int newsock, nextstate;
1.41 markus 1395: socklen_t addrlen;
1.103 markus 1396: char *rtype;
1.73 markus 1397:
1.41 markus 1398: if (FD_ISSET(c->sock, readset)) {
1399: debug("Connection to port %d forwarding "
1400: "to %.100s port %d requested.",
1401: c->listening_port, c->path, c->host_port);
1.103 markus 1402:
1.137 markus 1403: if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1404: nextstate = SSH_CHANNEL_OPENING;
1405: rtype = "forwarded-tcpip";
1406: } else {
1407: if (c->host_port == 0) {
1408: nextstate = SSH_CHANNEL_DYNAMIC;
1.138 markus 1409: rtype = "dynamic-tcpip";
1.137 markus 1410: } else {
1411: nextstate = SSH_CHANNEL_OPENING;
1412: rtype = "direct-tcpip";
1413: }
1414: }
1.103 markus 1415:
1.41 markus 1416: addrlen = sizeof(addr);
1.285 djm 1417: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.41 markus 1418: if (newsock < 0) {
1419: error("accept: %.100s", strerror(errno));
1420: return;
1421: }
1.169 stevesk 1422: set_nodelay(newsock);
1.190 markus 1423: nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1424: c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1.103 markus 1425: nc->listening_port = c->listening_port;
1426: nc->host_port = c->host_port;
1.293 djm 1427: if (c->path != NULL)
1428: nc->path = xstrdup(c->path);
1.103 markus 1429:
1.137 markus 1430: if (nextstate == SSH_CHANNEL_DYNAMIC) {
1431: /*
1432: * do not call the channel_post handler until
1433: * this flag has been reset by a pre-handler.
1434: * otherwise the FD_ISSET calls might overflow
1435: */
1436: nc->delayed = 1;
1437: } else {
1.103 markus 1438: port_open_helper(nc, rtype);
1.137 markus 1439: }
1.41 markus 1440: }
1441: }
1.25 markus 1442:
1.41 markus 1443: /*
1444: * This is the authentication agent socket listening for connections from
1445: * clients.
1446: */
1.259 stevesk 1447: /* ARGSUSED */
1.127 itojun 1448: static void
1.237 deraadt 1449: channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1450: {
1.113 markus 1451: Channel *nc;
1452: int newsock;
1.285 djm 1453: struct sockaddr_storage addr;
1.41 markus 1454: socklen_t addrlen;
1.25 markus 1455:
1.41 markus 1456: if (FD_ISSET(c->sock, readset)) {
1457: addrlen = sizeof(addr);
1.285 djm 1458: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.41 markus 1459: if (newsock < 0) {
1460: error("accept from auth socket: %.100s", strerror(errno));
1461: return;
1462: }
1.113 markus 1463: nc = channel_new("accepted auth socket",
1.73 markus 1464: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1465: c->local_window_max, c->local_maxpacket,
1.190 markus 1466: 0, "accepted auth socket", 1);
1.73 markus 1467: if (compat20) {
1468: packet_start(SSH2_MSG_CHANNEL_OPEN);
1469: packet_put_cstring("auth-agent@openssh.com");
1.113 markus 1470: packet_put_int(nc->self);
1.73 markus 1471: packet_put_int(c->local_window_max);
1472: packet_put_int(c->local_maxpacket);
1473: } else {
1474: packet_start(SSH_SMSG_AGENT_OPEN);
1.113 markus 1475: packet_put_int(nc->self);
1.73 markus 1476: }
1.41 markus 1477: packet_send();
1478: }
1479: }
1.25 markus 1480:
1.259 stevesk 1481: /* ARGSUSED */
1.127 itojun 1482: static void
1.237 deraadt 1483: channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
1.75 markus 1484: {
1.276 djm 1485: int err = 0, sock;
1.129 stevesk 1486: socklen_t sz = sizeof(err);
1.114 markus 1487:
1.75 markus 1488: if (FD_ISSET(c->sock, writeset)) {
1.170 stevesk 1489: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1.114 markus 1490: err = errno;
1491: error("getsockopt SO_ERROR failed");
1492: }
1493: if (err == 0) {
1.276 djm 1494: debug("channel %d: connected to %s port %d",
1495: c->self, c->connect_ctx.host, c->connect_ctx.port);
1496: channel_connect_ctx_free(&c->connect_ctx);
1.114 markus 1497: c->type = SSH_CHANNEL_OPEN;
1498: if (compat20) {
1499: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1500: packet_put_int(c->remote_id);
1501: packet_put_int(c->self);
1502: packet_put_int(c->local_window);
1503: packet_put_int(c->local_maxpacket);
1504: } else {
1505: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1506: packet_put_int(c->remote_id);
1507: packet_put_int(c->self);
1508: }
1.75 markus 1509: } else {
1.276 djm 1510: debug("channel %d: connection failed: %s",
1.114 markus 1511: c->self, strerror(err));
1.276 djm 1512: /* Try next address, if any */
1513: if ((sock = connect_next(&c->connect_ctx)) > 0) {
1514: close(c->sock);
1515: c->sock = c->rfd = c->wfd = sock;
1516: channel_max_fd = channel_find_maxfd();
1517: return;
1518: }
1519: /* Exhausted all addresses */
1520: error("connect_to %.100s port %d: failed.",
1521: c->connect_ctx.host, c->connect_ctx.port);
1522: channel_connect_ctx_free(&c->connect_ctx);
1.114 markus 1523: if (compat20) {
1524: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1525: packet_put_int(c->remote_id);
1526: packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1527: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1528: packet_put_cstring(strerror(err));
1529: packet_put_cstring("");
1530: }
1.75 markus 1531: } else {
1.114 markus 1532: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1533: packet_put_int(c->remote_id);
1.75 markus 1534: }
1.114 markus 1535: chan_mark_dead(c);
1.75 markus 1536: }
1.114 markus 1537: packet_send();
1.75 markus 1538: }
1539: }
1540:
1.259 stevesk 1541: /* ARGSUSED */
1.127 itojun 1542: static int
1.237 deraadt 1543: channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1544: {
1.214 markus 1545: char buf[CHAN_RBUF];
1.41 markus 1546: int len;
1.25 markus 1547:
1.118 markus 1548: if (c->rfd != -1 &&
1.41 markus 1549: FD_ISSET(c->rfd, readset)) {
1550: len = read(c->rfd, buf, sizeof(buf));
1.53 markus 1551: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1552: return 1;
1.41 markus 1553: if (len <= 0) {
1.194 markus 1554: debug2("channel %d: read<=0 rfd %d len %d",
1.41 markus 1555: c->self, c->rfd, len);
1.104 markus 1556: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1557: debug2("channel %d: not open", c->self);
1.113 markus 1558: chan_mark_dead(c);
1.103 markus 1559: return -1;
1.104 markus 1560: } else if (compat13) {
1.158 markus 1561: buffer_clear(&c->output);
1.41 markus 1562: c->type = SSH_CHANNEL_INPUT_DRAINING;
1.194 markus 1563: debug2("channel %d: input draining.", c->self);
1.25 markus 1564: } else {
1.41 markus 1565: chan_read_failed(c);
1.25 markus 1566: }
1.41 markus 1567: return -1;
1568: }
1.143 deraadt 1569: if (c->input_filter != NULL) {
1.66 markus 1570: if (c->input_filter(c, buf, len) == -1) {
1.194 markus 1571: debug2("channel %d: filter stops", c->self);
1.65 markus 1572: chan_read_failed(c);
1573: }
1.228 reyk 1574: } else if (c->datagram) {
1575: buffer_put_string(&c->input, buf, len);
1.65 markus 1576: } else {
1577: buffer_append(&c->input, buf, len);
1578: }
1.41 markus 1579: }
1580: return 1;
1581: }
1.241 deraadt 1582:
1.259 stevesk 1583: /* ARGSUSED */
1.127 itojun 1584: static int
1.237 deraadt 1585: channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1586: {
1.95 markus 1587: struct termios tio;
1.231 reyk 1588: u_char *data = NULL, *buf;
1.134 markus 1589: u_int dlen;
1.41 markus 1590: int len;
1.25 markus 1591:
1.41 markus 1592: /* Send buffered output data to the socket. */
1.118 markus 1593: if (c->wfd != -1 &&
1.41 markus 1594: FD_ISSET(c->wfd, writeset) &&
1595: buffer_len(&c->output) > 0) {
1.231 reyk 1596: if (c->output_filter != NULL) {
1597: if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1598: debug2("channel %d: filter stops", c->self);
1.232 reyk 1599: if (c->type != SSH_CHANNEL_OPEN)
1600: chan_mark_dead(c);
1601: else
1602: chan_write_failed(c);
1603: return -1;
1.231 reyk 1604: }
1605: } else if (c->datagram) {
1606: buf = data = buffer_get_string(&c->output, &dlen);
1607: } else {
1608: buf = data = buffer_ptr(&c->output);
1609: dlen = buffer_len(&c->output);
1610: }
1611:
1.228 reyk 1612: if (c->datagram) {
1613: /* ignore truncated writes, datagrams might get lost */
1614: c->local_consumed += dlen + 4;
1.231 reyk 1615: len = write(c->wfd, buf, dlen);
1.228 reyk 1616: xfree(data);
1617: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1618: return 1;
1619: if (len <= 0) {
1620: if (c->type != SSH_CHANNEL_OPEN)
1621: chan_mark_dead(c);
1622: else
1623: chan_write_failed(c);
1624: return -1;
1625: }
1626: return 1;
1627: }
1.231 reyk 1628:
1629: len = write(c->wfd, buf, dlen);
1.53 markus 1630: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1631: return 1;
1.41 markus 1632: if (len <= 0) {
1.104 markus 1633: if (c->type != SSH_CHANNEL_OPEN) {
1.194 markus 1634: debug2("channel %d: not open", c->self);
1.113 markus 1635: chan_mark_dead(c);
1.104 markus 1636: return -1;
1637: } else if (compat13) {
1.158 markus 1638: buffer_clear(&c->output);
1.194 markus 1639: debug2("channel %d: input draining.", c->self);
1.41 markus 1640: c->type = SSH_CHANNEL_INPUT_DRAINING;
1641: } else {
1642: chan_write_failed(c);
1643: }
1644: return -1;
1.91 markus 1645: }
1.231 reyk 1646: if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
1.91 markus 1647: if (tcgetattr(c->wfd, &tio) == 0 &&
1648: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1649: /*
1650: * Simulate echo to reduce the impact of
1.96 markus 1651: * traffic analysis. We need to match the
1.95 markus 1652: * size of a SSH2_MSG_CHANNEL_DATA message
1.231 reyk 1653: * (4 byte channel id + buf)
1.91 markus 1654: */
1.95 markus 1655: packet_send_ignore(4 + len);
1.91 markus 1656: packet_send();
1657: }
1.25 markus 1658: }
1.41 markus 1659: buffer_consume(&c->output, len);
1.44 markus 1660: if (compat20 && len > 0) {
1661: c->local_consumed += len;
1662: }
1663: }
1664: return 1;
1665: }
1.241 deraadt 1666:
1.127 itojun 1667: static int
1.237 deraadt 1668: channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
1.44 markus 1669: {
1.214 markus 1670: char buf[CHAN_RBUF];
1.44 markus 1671: int len;
1672:
1.45 markus 1673: /** XXX handle drain efd, too */
1.44 markus 1674: if (c->efd != -1) {
1675: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1676: FD_ISSET(c->efd, writeset) &&
1677: buffer_len(&c->extended) > 0) {
1678: len = write(c->efd, buffer_ptr(&c->extended),
1679: buffer_len(&c->extended));
1.70 markus 1680: debug2("channel %d: written %d to efd %d",
1.44 markus 1681: c->self, len, c->efd);
1.93 markus 1682: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1683: return 1;
1684: if (len <= 0) {
1685: debug2("channel %d: closing write-efd %d",
1686: c->self, c->efd);
1.132 markus 1687: channel_close_fd(&c->efd);
1.93 markus 1688: } else {
1.44 markus 1689: buffer_consume(&c->extended, len);
1690: c->local_consumed += len;
1691: }
1692: } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1693: FD_ISSET(c->efd, readset)) {
1694: len = read(c->efd, buf, sizeof(buf));
1.70 markus 1695: debug2("channel %d: read %d from efd %d",
1.148 deraadt 1696: c->self, len, c->efd);
1.93 markus 1697: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1698: return 1;
1699: if (len <= 0) {
1700: debug2("channel %d: closing read-efd %d",
1.45 markus 1701: c->self, c->efd);
1.132 markus 1702: channel_close_fd(&c->efd);
1.93 markus 1703: } else {
1.44 markus 1704: buffer_append(&c->extended, buf, len);
1.93 markus 1705: }
1.44 markus 1706: }
1707: }
1708: return 1;
1709: }
1.241 deraadt 1710:
1.259 stevesk 1711: /* ARGSUSED */
1.127 itojun 1712: static int
1.237 deraadt 1713: channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
1.204 djm 1714: {
1715: char buf[16];
1716: int len;
1717:
1718: /* Monitor control fd to detect if the slave client exits */
1719: if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1720: len = read(c->ctl_fd, buf, sizeof(buf));
1721: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1722: return 1;
1723: if (len <= 0) {
1724: debug2("channel %d: ctl read<=0", c->self);
1725: if (c->type != SSH_CHANNEL_OPEN) {
1726: debug2("channel %d: not open", c->self);
1727: chan_mark_dead(c);
1728: return -1;
1729: } else {
1730: chan_read_failed(c);
1731: chan_write_failed(c);
1732: }
1733: return -1;
1734: } else
1735: fatal("%s: unexpected data on ctl fd", __func__);
1736: }
1737: return 1;
1738: }
1.241 deraadt 1739:
1.204 djm 1740: static int
1.93 markus 1741: channel_check_window(Channel *c)
1.44 markus 1742: {
1.104 markus 1743: if (c->type == SSH_CHANNEL_OPEN &&
1744: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.270 dtucker 1745: ((c->local_window_max - c->local_window >
1.269 markus 1746: c->local_maxpacket*3) ||
1747: c->local_window < c->local_window_max/2) &&
1.44 markus 1748: c->local_consumed > 0) {
1749: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1750: packet_put_int(c->remote_id);
1751: packet_put_int(c->local_consumed);
1752: packet_send();
1.70 markus 1753: debug2("channel %d: window %d sent adjust %d",
1.44 markus 1754: c->self, c->local_window,
1755: c->local_consumed);
1756: c->local_window += c->local_consumed;
1757: c->local_consumed = 0;
1.1 deraadt 1758: }
1.41 markus 1759: return 1;
1.1 deraadt 1760: }
1761:
1.127 itojun 1762: static void
1.237 deraadt 1763: channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
1.41 markus 1764: {
1.137 markus 1765: if (c->delayed)
1766: return;
1.41 markus 1767: channel_handle_rfd(c, readset, writeset);
1768: channel_handle_wfd(c, readset, writeset);
1.157 markus 1769: if (!compat20)
1.137 markus 1770: return;
1.44 markus 1771: channel_handle_efd(c, readset, writeset);
1.204 djm 1772: channel_handle_ctl(c, readset, writeset);
1.93 markus 1773: channel_check_window(c);
1.44 markus 1774: }
1775:
1.259 stevesk 1776: /* ARGSUSED */
1.127 itojun 1777: static void
1.237 deraadt 1778: channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
1.1 deraadt 1779: {
1.41 markus 1780: int len;
1.180 deraadt 1781:
1.41 markus 1782: /* Send buffered output data to the socket. */
1783: if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1784: len = write(c->sock, buffer_ptr(&c->output),
1785: buffer_len(&c->output));
1786: if (len <= 0)
1.158 markus 1787: buffer_clear(&c->output);
1.41 markus 1788: else
1789: buffer_consume(&c->output, len);
1790: }
1791: }
1.25 markus 1792:
1.127 itojun 1793: static void
1.44 markus 1794: channel_handler_init_20(void)
1795: {
1.157 markus 1796: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1797: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.44 markus 1798: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1.73 markus 1799: channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1.51 markus 1800: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1.73 markus 1801: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1802: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1803: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.44 markus 1804:
1.157 markus 1805: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.44 markus 1806: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1.73 markus 1807: channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1.51 markus 1808: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1.73 markus 1809: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.75 markus 1810: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1811: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.44 markus 1812: }
1813:
1.127 itojun 1814: static void
1.41 markus 1815: channel_handler_init_13(void)
1816: {
1817: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1818: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1819: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1820: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1821: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1822: channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1823: channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1.75 markus 1824: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1825: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1826:
1.157 markus 1827: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.41 markus 1828: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1829: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1830: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1831: channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1.75 markus 1832: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1833: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1834: }
1.25 markus 1835:
1.127 itojun 1836: static void
1.41 markus 1837: channel_handler_init_15(void)
1838: {
1.157 markus 1839: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1.51 markus 1840: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.41 markus 1841: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1842: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1843: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1844: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1845: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1846:
1.41 markus 1847: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1848: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1849: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.157 markus 1850: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1.75 markus 1851: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.157 markus 1852: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.41 markus 1853: }
1.27 markus 1854:
1.127 itojun 1855: static void
1.41 markus 1856: channel_handler_init(void)
1857: {
1858: int i;
1.180 deraadt 1859:
1.148 deraadt 1860: for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1.41 markus 1861: channel_pre[i] = NULL;
1862: channel_post[i] = NULL;
1863: }
1.44 markus 1864: if (compat20)
1865: channel_handler_init_20();
1866: else if (compat13)
1.41 markus 1867: channel_handler_init_13();
1868: else
1869: channel_handler_init_15();
1870: }
1.25 markus 1871:
1.140 markus 1872: /* gc dead channels */
1873: static void
1874: channel_garbage_collect(Channel *c)
1875: {
1876: if (c == NULL)
1877: return;
1878: if (c->detach_user != NULL) {
1.225 djm 1879: if (!chan_is_dead(c, c->detach_close))
1.140 markus 1880: return;
1.194 markus 1881: debug2("channel %d: gc: notify user", c->self);
1.140 markus 1882: c->detach_user(c->self, NULL);
1883: /* if we still have a callback */
1884: if (c->detach_user != NULL)
1885: return;
1.194 markus 1886: debug2("channel %d: gc: user detached", c->self);
1.140 markus 1887: }
1888: if (!chan_is_dead(c, 1))
1889: return;
1.194 markus 1890: debug2("channel %d: garbage collecting", c->self);
1.140 markus 1891: channel_free(c);
1892: }
1893:
1.127 itojun 1894: static void
1.237 deraadt 1895: channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
1.41 markus 1896: {
1897: static int did_init = 0;
1.209 avsm 1898: u_int i;
1.41 markus 1899: Channel *c;
1.25 markus 1900:
1.41 markus 1901: if (!did_init) {
1902: channel_handler_init();
1903: did_init = 1;
1904: }
1905: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1906: c = channels[i];
1907: if (c == NULL)
1.41 markus 1908: continue;
1.118 markus 1909: if (ftab[c->type] != NULL)
1910: (*ftab[c->type])(c, readset, writeset);
1.140 markus 1911: channel_garbage_collect(c);
1.1 deraadt 1912: }
1913: }
1914:
1.121 markus 1915: /*
1916: * Allocate/update select bitmasks and add any bits relevant to channels in
1917: * select bitmasks.
1918: */
1.49 markus 1919: void
1.100 markus 1920: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1.209 avsm 1921: u_int *nallocp, int rekeying)
1.41 markus 1922: {
1.243 djm 1923: u_int n, sz, nfdset;
1.84 markus 1924:
1925: n = MAX(*maxfdp, channel_max_fd);
1926:
1.243 djm 1927: nfdset = howmany(n+1, NFDBITS);
1928: /* Explicitly test here, because xrealloc isn't always called */
1929: if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1930: fatal("channel_prepare_select: max_fd (%d) is too large", n);
1931: sz = nfdset * sizeof(fd_mask);
1932:
1.132 markus 1933: /* perhaps check sz < nalloc/2 and shrink? */
1934: if (*readsetp == NULL || sz > *nallocp) {
1.243 djm 1935: *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1936: *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
1.132 markus 1937: *nallocp = sz;
1.84 markus 1938: }
1.132 markus 1939: *maxfdp = n;
1.84 markus 1940: memset(*readsetp, 0, sz);
1941: memset(*writesetp, 0, sz);
1942:
1.100 markus 1943: if (!rekeying)
1944: channel_handler(channel_pre, *readsetp, *writesetp);
1.41 markus 1945: }
1946:
1.121 markus 1947: /*
1948: * After select, perform any appropriate operations for channels which have
1949: * events pending.
1950: */
1.49 markus 1951: void
1.237 deraadt 1952: channel_after_select(fd_set *readset, fd_set *writeset)
1.41 markus 1953: {
1954: channel_handler(channel_post, readset, writeset);
1955: }
1956:
1.121 markus 1957:
1.84 markus 1958: /* If there is data to send to the connection, enqueue some of it now. */
1.49 markus 1959: void
1.142 itojun 1960: channel_output_poll(void)
1.1 deraadt 1961: {
1.41 markus 1962: Channel *c;
1.209 avsm 1963: u_int i, len;
1.1 deraadt 1964:
1.25 markus 1965: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1966: c = channels[i];
1967: if (c == NULL)
1968: continue;
1.37 markus 1969:
1.121 markus 1970: /*
1971: * We are only interested in channels that can have buffered
1972: * incoming data.
1973: */
1.37 markus 1974: if (compat13) {
1.41 markus 1975: if (c->type != SSH_CHANNEL_OPEN &&
1976: c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37 markus 1977: continue;
1978: } else {
1.41 markus 1979: if (c->type != SSH_CHANNEL_OPEN)
1.37 markus 1980: continue;
1981: }
1.46 markus 1982: if (compat20 &&
1983: (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 1984: /* XXX is this true? */
1.140 markus 1985: debug3("channel %d: will not send data after close", c->self);
1.44 markus 1986: continue;
1987: }
1.25 markus 1988:
1989: /* Get the amount of buffered data for this channel. */
1.93 markus 1990: if ((c->istate == CHAN_INPUT_OPEN ||
1991: c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1992: (len = buffer_len(&c->input)) > 0) {
1.228 reyk 1993: if (c->datagram) {
1994: if (len > 0) {
1995: u_char *data;
1996: u_int dlen;
1997:
1998: data = buffer_get_string(&c->input,
1999: &dlen);
2000: packet_start(SSH2_MSG_CHANNEL_DATA);
2001: packet_put_int(c->remote_id);
2002: packet_put_string(data, dlen);
2003: packet_send();
2004: c->remote_window -= dlen + 4;
2005: xfree(data);
2006: }
2007: continue;
2008: }
1.121 markus 2009: /*
2010: * Send some data for the other side over the secure
2011: * connection.
2012: */
1.44 markus 2013: if (compat20) {
2014: if (len > c->remote_window)
2015: len = c->remote_window;
2016: if (len > c->remote_maxpacket)
2017: len = c->remote_maxpacket;
1.25 markus 2018: } else {
1.44 markus 2019: if (packet_is_interactive()) {
2020: if (len > 1024)
2021: len = 512;
2022: } else {
2023: /* Keep the packets at reasonable size. */
2024: if (len > packet_get_maxsize()/2)
2025: len = packet_get_maxsize()/2;
2026: }
1.25 markus 2027: }
1.41 markus 2028: if (len > 0) {
1.44 markus 2029: packet_start(compat20 ?
2030: SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41 markus 2031: packet_put_int(c->remote_id);
2032: packet_put_string(buffer_ptr(&c->input), len);
2033: packet_send();
2034: buffer_consume(&c->input, len);
2035: c->remote_window -= len;
2036: }
2037: } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25 markus 2038: if (compat13)
2039: fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27 markus 2040: /*
2041: * input-buffer is empty and read-socket shutdown:
1.172 markus 2042: * tell peer, that we will not send more data: send IEOF.
2043: * hack for extended data: delay EOF if EFD still in use.
1.27 markus 2044: */
1.172 markus 2045: if (CHANNEL_EFD_INPUT_ACTIVE(c))
1.223 djm 2046: debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2047: c->self, c->efd, buffer_len(&c->extended));
1.172 markus 2048: else
2049: chan_ibuf_empty(c);
1.25 markus 2050: }
1.44 markus 2051: /* Send extended data, i.e. stderr */
2052: if (compat20 &&
1.172 markus 2053: !(c->flags & CHAN_EOF_SENT) &&
1.44 markus 2054: c->remote_window > 0 &&
2055: (len = buffer_len(&c->extended)) > 0 &&
2056: c->extended_usage == CHAN_EXTENDED_READ) {
1.178 markus 2057: debug2("channel %d: rwin %u elen %u euse %d",
1.93 markus 2058: c->self, c->remote_window, buffer_len(&c->extended),
2059: c->extended_usage);
1.44 markus 2060: if (len > c->remote_window)
2061: len = c->remote_window;
2062: if (len > c->remote_maxpacket)
2063: len = c->remote_maxpacket;
2064: packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2065: packet_put_int(c->remote_id);
2066: packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2067: packet_put_string(buffer_ptr(&c->extended), len);
2068: packet_send();
2069: buffer_consume(&c->extended, len);
2070: c->remote_window -= len;
1.93 markus 2071: debug2("channel %d: sent ext data %d", c->self, len);
1.44 markus 2072: }
1.25 markus 2073: }
1.1 deraadt 2074: }
2075:
1.121 markus 2076:
2077: /* -- protocol input */
1.249 djm 2078:
2079: /* ARGSUSED */
1.49 markus 2080: void
1.154 markus 2081: channel_input_data(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2082: {
1.37 markus 2083: int id;
1.25 markus 2084: char *data;
1.77 markus 2085: u_int data_len;
1.41 markus 2086: Channel *c;
1.25 markus 2087:
2088: /* Get the channel number and verify it. */
1.37 markus 2089: id = packet_get_int();
1.41 markus 2090: c = channel_lookup(id);
2091: if (c == NULL)
1.37 markus 2092: packet_disconnect("Received data for nonexistent channel %d.", id);
1.25 markus 2093:
2094: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 2095: if (c->type != SSH_CHANNEL_OPEN &&
2096: c->type != SSH_CHANNEL_X11_OPEN)
1.37 markus 2097: return;
2098:
1.25 markus 2099: /* Get the data. */
1.274 markus 2100: data = packet_get_string_ptr(&data_len);
1.200 markus 2101:
2102: /*
2103: * Ignore data for protocol > 1.3 if output end is no longer open.
2104: * For protocol 2 the sending side is reducing its window as it sends
2105: * data, so we must 'fake' consumption of the data in order to ensure
2106: * that window updates are sent back. Otherwise the connection might
2107: * deadlock.
2108: */
2109: if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2110: if (compat20) {
2111: c->local_window -= data_len;
2112: c->local_consumed += data_len;
2113: }
2114: return;
2115: }
1.41 markus 2116:
1.143 deraadt 2117: if (compat20) {
1.44 markus 2118: if (data_len > c->local_maxpacket) {
1.188 itojun 2119: logit("channel %d: rcvd big packet %d, maxpack %d",
1.44 markus 2120: c->self, data_len, c->local_maxpacket);
2121: }
2122: if (data_len > c->local_window) {
1.188 itojun 2123: logit("channel %d: rcvd too much data %d, win %d",
1.44 markus 2124: c->self, data_len, c->local_window);
2125: return;
2126: }
2127: c->local_window -= data_len;
2128: }
1.228 reyk 2129: if (c->datagram)
2130: buffer_put_string(&c->output, data, data_len);
2131: else
2132: buffer_append(&c->output, data, data_len);
1.274 markus 2133: packet_check_eom();
1.1 deraadt 2134: }
1.121 markus 2135:
1.249 djm 2136: /* ARGSUSED */
1.49 markus 2137: void
1.154 markus 2138: channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1.44 markus 2139: {
2140: int id;
2141: char *data;
1.177 markus 2142: u_int data_len, tcode;
1.44 markus 2143: Channel *c;
2144:
2145: /* Get the channel number and verify it. */
2146: id = packet_get_int();
2147: c = channel_lookup(id);
2148:
2149: if (c == NULL)
2150: packet_disconnect("Received extended_data for bad channel %d.", id);
2151: if (c->type != SSH_CHANNEL_OPEN) {
1.188 itojun 2152: logit("channel %d: ext data for non open", id);
1.44 markus 2153: return;
1.172 markus 2154: }
2155: if (c->flags & CHAN_EOF_RCVD) {
2156: if (datafellows & SSH_BUG_EXTEOF)
2157: debug("channel %d: accepting ext data after eof", id);
2158: else
2159: packet_disconnect("Received extended_data after EOF "
2160: "on channel %d.", id);
1.44 markus 2161: }
2162: tcode = packet_get_int();
2163: if (c->efd == -1 ||
2164: c->extended_usage != CHAN_EXTENDED_WRITE ||
2165: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 2166: logit("channel %d: bad ext data", c->self);
1.44 markus 2167: return;
2168: }
2169: data = packet_get_string(&data_len);
1.152 markus 2170: packet_check_eom();
1.44 markus 2171: if (data_len > c->local_window) {
1.188 itojun 2172: logit("channel %d: rcvd too much extended_data %d, win %d",
1.44 markus 2173: c->self, data_len, c->local_window);
2174: xfree(data);
2175: return;
2176: }
1.70 markus 2177: debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44 markus 2178: c->local_window -= data_len;
2179: buffer_append(&c->extended, data, data_len);
2180: xfree(data);
2181: }
2182:
1.249 djm 2183: /* ARGSUSED */
1.49 markus 2184: void
1.154 markus 2185: channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1.41 markus 2186: {
2187: int id;
2188: Channel *c;
2189:
2190: id = packet_get_int();
1.152 markus 2191: packet_check_eom();
1.41 markus 2192: c = channel_lookup(id);
2193: if (c == NULL)
2194: packet_disconnect("Received ieof for nonexistent channel %d.", id);
2195: chan_rcvd_ieof(c);
1.133 markus 2196:
2197: /* XXX force input close */
1.156 markus 2198: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 2199: debug("channel %d: FORCE input drain", c->self);
2200: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.161 markus 2201: if (buffer_len(&c->input) == 0)
2202: chan_ibuf_empty(c);
1.133 markus 2203: }
2204:
1.41 markus 2205: }
1.1 deraadt 2206:
1.249 djm 2207: /* ARGSUSED */
1.49 markus 2208: void
1.154 markus 2209: channel_input_close(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2210: {
1.41 markus 2211: int id;
2212: Channel *c;
1.1 deraadt 2213:
1.41 markus 2214: id = packet_get_int();
1.152 markus 2215: packet_check_eom();
1.41 markus 2216: c = channel_lookup(id);
2217: if (c == NULL)
2218: packet_disconnect("Received close for nonexistent channel %d.", id);
1.27 markus 2219:
2220: /*
2221: * Send a confirmation that we have closed the channel and no more
2222: * data is coming for it.
2223: */
1.25 markus 2224: packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41 markus 2225: packet_put_int(c->remote_id);
1.25 markus 2226: packet_send();
2227:
1.27 markus 2228: /*
2229: * If the channel is in closed state, we have sent a close request,
2230: * and the other side will eventually respond with a confirmation.
2231: * Thus, we cannot free the channel here, because then there would be
2232: * no-one to receive the confirmation. The channel gets freed when
2233: * the confirmation arrives.
2234: */
1.41 markus 2235: if (c->type != SSH_CHANNEL_CLOSED) {
1.27 markus 2236: /*
2237: * Not a closed channel - mark it as draining, which will
2238: * cause it to be freed later.
2239: */
1.158 markus 2240: buffer_clear(&c->input);
1.41 markus 2241: c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25 markus 2242: }
1.1 deraadt 2243: }
2244:
1.41 markus 2245: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.249 djm 2246: /* ARGSUSED */
1.49 markus 2247: void
1.154 markus 2248: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41 markus 2249: {
2250: int id = packet_get_int();
2251: Channel *c = channel_lookup(id);
1.151 markus 2252:
1.152 markus 2253: packet_check_eom();
1.41 markus 2254: if (c == NULL)
2255: packet_disconnect("Received oclose for nonexistent channel %d.", id);
2256: chan_rcvd_oclose(c);
2257: }
1.1 deraadt 2258:
1.249 djm 2259: /* ARGSUSED */
1.49 markus 2260: void
1.154 markus 2261: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2262: {
1.41 markus 2263: int id = packet_get_int();
2264: Channel *c = channel_lookup(id);
1.1 deraadt 2265:
1.152 markus 2266: packet_check_eom();
1.41 markus 2267: if (c == NULL)
2268: packet_disconnect("Received close confirmation for "
2269: "out-of-range channel %d.", id);
2270: if (c->type != SSH_CHANNEL_CLOSED)
2271: packet_disconnect("Received close confirmation for "
2272: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 2273: channel_free(c);
1.1 deraadt 2274: }
2275:
1.249 djm 2276: /* ARGSUSED */
1.49 markus 2277: void
1.154 markus 2278: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2279: {
1.41 markus 2280: int id, remote_id;
2281: Channel *c;
1.1 deraadt 2282:
1.41 markus 2283: id = packet_get_int();
2284: c = channel_lookup(id);
1.25 markus 2285:
1.41 markus 2286: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2287: packet_disconnect("Received open confirmation for "
2288: "non-opening channel %d.", id);
2289: remote_id = packet_get_int();
1.27 markus 2290: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 2291: c->remote_id = remote_id;
2292: c->type = SSH_CHANNEL_OPEN;
1.44 markus 2293:
2294: if (compat20) {
2295: c->remote_window = packet_get_int();
2296: c->remote_maxpacket = packet_get_int();
1.275 djm 2297: if (c->open_confirm) {
1.70 markus 2298: debug2("callback start");
1.275 djm 2299: c->open_confirm(c->self, c->open_confirm_ctx);
1.70 markus 2300: debug2("callback done");
1.44 markus 2301: }
1.194 markus 2302: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
1.44 markus 2303: c->remote_window, c->remote_maxpacket);
2304: }
1.152 markus 2305: packet_check_eom();
1.1 deraadt 2306: }
2307:
1.127 itojun 2308: static char *
1.114 markus 2309: reason2txt(int reason)
2310: {
1.143 deraadt 2311: switch (reason) {
1.114 markus 2312: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2313: return "administratively prohibited";
2314: case SSH2_OPEN_CONNECT_FAILED:
2315: return "connect failed";
2316: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2317: return "unknown channel type";
2318: case SSH2_OPEN_RESOURCE_SHORTAGE:
2319: return "resource shortage";
2320: }
1.117 stevesk 2321: return "unknown reason";
1.114 markus 2322: }
2323:
1.249 djm 2324: /* ARGSUSED */
1.49 markus 2325: void
1.154 markus 2326: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2327: {
1.86 markus 2328: int id, reason;
2329: char *msg = NULL, *lang = NULL;
1.41 markus 2330: Channel *c;
2331:
2332: id = packet_get_int();
2333: c = channel_lookup(id);
1.25 markus 2334:
1.41 markus 2335: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2336: packet_disconnect("Received open failure for "
2337: "non-opening channel %d.", id);
1.44 markus 2338: if (compat20) {
1.86 markus 2339: reason = packet_get_int();
1.110 markus 2340: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86 markus 2341: msg = packet_get_string(NULL);
2342: lang = packet_get_string(NULL);
2343: }
1.188 itojun 2344: logit("channel %d: open failed: %s%s%s", id,
1.114 markus 2345: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86 markus 2346: if (msg != NULL)
2347: xfree(msg);
2348: if (lang != NULL)
2349: xfree(lang);
1.44 markus 2350: }
1.152 markus 2351: packet_check_eom();
1.291 djm 2352: /* Schedule the channel for cleanup/deletion. */
2353: chan_mark_dead(c);
1.44 markus 2354: }
2355:
1.249 djm 2356: /* ARGSUSED */
1.121 markus 2357: void
1.154 markus 2358: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121 markus 2359: {
2360: Channel *c;
1.178 markus 2361: int id;
2362: u_int adjust;
1.121 markus 2363:
2364: if (!compat20)
2365: return;
2366:
2367: /* Get the channel number and verify it. */
2368: id = packet_get_int();
2369: c = channel_lookup(id);
1.1 deraadt 2370:
1.229 markus 2371: if (c == NULL) {
2372: logit("Received window adjust for non-open channel %d.", id);
1.121 markus 2373: return;
2374: }
2375: adjust = packet_get_int();
1.152 markus 2376: packet_check_eom();
1.178 markus 2377: debug2("channel %d: rcvd adjust %u", id, adjust);
1.121 markus 2378: c->remote_window += adjust;
2379: }
1.1 deraadt 2380:
1.249 djm 2381: /* ARGSUSED */
1.121 markus 2382: void
1.154 markus 2383: channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2384: {
1.121 markus 2385: Channel *c = NULL;
2386: u_short host_port;
2387: char *host, *originator_string;
1.276 djm 2388: int remote_id;
1.121 markus 2389:
2390: remote_id = packet_get_int();
2391: host = packet_get_string(NULL);
2392: host_port = packet_get_int();
1.25 markus 2393:
1.121 markus 2394: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2395: originator_string = packet_get_string(NULL);
2396: } else {
2397: originator_string = xstrdup("unknown (remote did not supply name)");
2398: }
1.152 markus 2399: packet_check_eom();
1.276 djm 2400: c = channel_connect_to(host, host_port,
2401: "connected socket", originator_string);
1.190 markus 2402: xfree(originator_string);
1.276 djm 2403: xfree(host);
1.121 markus 2404: if (c == NULL) {
2405: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2406: packet_put_int(remote_id);
2407: packet_send();
1.276 djm 2408: } else
2409: c->remote_id = remote_id;
1.1 deraadt 2410: }
2411:
1.275 djm 2412: /* ARGSUSED */
2413: void
2414: channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2415: {
2416: Channel *c;
2417: struct channel_confirm *cc;
1.289 markus 2418: int id;
1.275 djm 2419:
2420: /* Reset keepalive timeout */
1.296 andreas 2421: packet_set_alive_timeouts(0);
1.275 djm 2422:
1.289 markus 2423: id = packet_get_int();
1.275 djm 2424: packet_check_eom();
2425:
1.289 markus 2426: debug2("channel_input_status_confirm: type %d id %d", type, id);
1.275 djm 2427:
1.289 markus 2428: if ((c = channel_lookup(id)) == NULL) {
2429: logit("channel_input_status_confirm: %d: unknown", id);
1.275 djm 2430: return;
2431: }
2432: ;
2433: if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
2434: return;
2435: cc->cb(type, c, cc->ctx);
2436: TAILQ_REMOVE(&c->status_confirms, cc, entry);
2437: bzero(cc, sizeof(*cc));
2438: xfree(cc);
2439: }
1.121 markus 2440:
2441: /* -- tcp forwarding */
1.135 markus 2442:
2443: void
2444: channel_set_af(int af)
2445: {
2446: IPv4or6 = af;
2447: }
1.121 markus 2448:
1.297 reyk 2449: void
2450: channel_set_rdomain(int rdomain)
2451: {
2452: channel_rdomain = rdomain;
2453: }
2454:
1.160 markus 2455: static int
1.295 djm 2456: channel_setup_fwd_listener(int type, const char *listen_addr,
2457: u_short listen_port, int *allocated_listen_port,
1.160 markus 2458: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
1.25 markus 2459: {
1.113 markus 2460: Channel *c;
1.226 djm 2461: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 2462: struct addrinfo hints, *ai, *aitop;
1.212 djm 2463: const char *host, *addr;
1.35 markus 2464: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.295 djm 2465: in_port_t *lport_p;
1.25 markus 2466:
1.160 markus 2467: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2468: listen_addr : host_to_connect;
1.212 djm 2469: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 2470:
1.160 markus 2471: if (host == NULL) {
2472: error("No forward host name.");
1.218 markus 2473: return 0;
1.73 markus 2474: }
1.294 djm 2475: if (strlen(host) >= NI_MAXHOST) {
1.87 markus 2476: error("Forward host name too long.");
1.218 markus 2477: return 0;
1.87 markus 2478: }
1.25 markus 2479:
1.28 markus 2480: /*
1.212 djm 2481: * Determine whether or not a port forward listens to loopback,
1.213 deraadt 2482: * specified address or wildcard. On the client, a specified bind
2483: * address will always override gateway_ports. On the server, a
2484: * gateway_ports of 1 (``yes'') will override the client's
2485: * specification and force a wildcard bind, whereas a value of 2
2486: * (``clientspecified'') will bind to whatever address the client
1.212 djm 2487: * asked for.
2488: *
2489: * Special-case listen_addrs are:
2490: *
2491: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2492: * "" (empty string), "*" -> wildcard v4/v6
2493: * "localhost" -> loopback v4/v6
2494: */
2495: addr = NULL;
2496: if (listen_addr == NULL) {
2497: /* No address specified: default to gateway_ports setting */
2498: if (gateway_ports)
2499: wildcard = 1;
2500: } else if (gateway_ports || is_client) {
2501: if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
1.272 djm 2502: strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
1.212 djm 2503: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2504: (!is_client && gateway_ports == 1))
2505: wildcard = 1;
2506: else if (strcmp(listen_addr, "localhost") != 0)
2507: addr = listen_addr;
2508: }
2509:
2510: debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2511: type, wildcard, (addr == NULL) ? "NULL" : addr);
2512:
2513: /*
1.35 markus 2514: * getaddrinfo returns a loopback address if the hostname is
2515: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2516: */
1.35 markus 2517: memset(&hints, 0, sizeof(hints));
2518: hints.ai_family = IPv4or6;
1.212 djm 2519: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 2520: hints.ai_socktype = SOCK_STREAM;
1.73 markus 2521: snprintf(strport, sizeof strport, "%d", listen_port);
1.212 djm 2522: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2523: if (addr == NULL) {
2524: /* This really shouldn't happen */
2525: packet_disconnect("getaddrinfo: fatal error: %s",
1.271 dtucker 2526: ssh_gai_strerror(r));
1.212 djm 2527: } else {
1.218 markus 2528: error("channel_setup_fwd_listener: "
1.271 dtucker 2529: "getaddrinfo(%.64s): %s", addr,
2530: ssh_gai_strerror(r));
1.212 djm 2531: }
1.218 markus 2532: return 0;
1.212 djm 2533: }
1.295 djm 2534: if (allocated_listen_port != NULL)
2535: *allocated_listen_port = 0;
1.35 markus 2536: for (ai = aitop; ai; ai = ai->ai_next) {
1.295 djm 2537: switch (ai->ai_family) {
2538: case AF_INET:
2539: lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2540: sin_port;
2541: break;
2542: case AF_INET6:
2543: lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2544: sin6_port;
2545: break;
2546: default:
1.35 markus 2547: continue;
1.295 djm 2548: }
2549: /*
2550: * If allocating a port for -R forwards, then use the
2551: * same port for all address families.
2552: */
2553: if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 &&
2554: allocated_listen_port != NULL && *allocated_listen_port > 0)
2555: *lport_p = htons(*allocated_listen_port);
2556:
1.35 markus 2557: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2558: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.160 markus 2559: error("channel_setup_fwd_listener: getnameinfo failed");
1.35 markus 2560: continue;
2561: }
2562: /* Create a port to listen for the host. */
1.297 reyk 2563: sock = socket_rdomain(ai->ai_family, ai->ai_socktype,
2564: ai->ai_protocol, channel_rdomain);
1.35 markus 2565: if (sock < 0) {
2566: /* this is no error since kernel may not support ipv6 */
2567: verbose("socket: %.100s", strerror(errno));
2568: continue;
2569: }
1.226 djm 2570:
2571: channel_set_reuseaddr(sock);
1.182 stevesk 2572:
1.295 djm 2573: debug("Local forwarding listening on %s port %s.",
2574: ntop, strport);
1.35 markus 2575:
2576: /* Bind the socket to the address. */
2577: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2578: /* address can be in use ipv6 address is already bound */
2579: verbose("bind: %.100s", strerror(errno));
2580: close(sock);
2581: continue;
2582: }
2583: /* Start listening for connections on the socket. */
1.199 markus 2584: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2585: error("listen: %.100s", strerror(errno));
2586: close(sock);
2587: continue;
2588: }
1.295 djm 2589:
2590: /*
2591: * listen_port == 0 requests a dynamically allocated port -
2592: * record what we got.
2593: */
2594: if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 &&
2595: allocated_listen_port != NULL &&
2596: *allocated_listen_port == 0) {
2597: *allocated_listen_port = get_sock_port(sock, 1);
2598: debug("Allocated listen port %d",
2599: *allocated_listen_port);
2600: }
2601:
1.35 markus 2602: /* Allocate a channel number for the socket. */
1.121 markus 2603: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2604: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 2605: 0, "port listener", 1);
1.293 djm 2606: c->path = xstrdup(host);
1.113 markus 2607: c->host_port = port_to_connect;
2608: c->listening_port = listen_port;
1.35 markus 2609: success = 1;
2610: }
2611: if (success == 0)
1.160 markus 2612: error("channel_setup_fwd_listener: cannot listen to port: %d",
1.87 markus 2613: listen_port);
1.35 markus 2614: freeaddrinfo(aitop);
1.87 markus 2615: return success;
1.25 markus 2616: }
1.1 deraadt 2617:
1.202 djm 2618: int
2619: channel_cancel_rport_listener(const char *host, u_short port)
2620: {
1.209 avsm 2621: u_int i;
2622: int found = 0;
1.202 djm 2623:
1.213 deraadt 2624: for (i = 0; i < channels_alloc; i++) {
1.202 djm 2625: Channel *c = channels[i];
2626:
2627: if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
1.293 djm 2628: strcmp(c->path, host) == 0 && c->listening_port == port) {
1.210 djm 2629: debug2("%s: close channel %d", __func__, i);
1.202 djm 2630: channel_free(c);
2631: found = 1;
2632: }
2633: }
2634:
2635: return (found);
2636: }
2637:
1.160 markus 2638: /* protocol local port fwd, used by ssh (and sshd in v1) */
2639: int
1.212 djm 2640: channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
1.160 markus 2641: const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2642: {
2643: return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
1.295 djm 2644: listen_host, listen_port, NULL, host_to_connect, port_to_connect,
1.212 djm 2645: gateway_ports);
1.160 markus 2646: }
2647:
2648: /* protocol v2 remote port fwd, used by sshd */
2649: int
2650: channel_setup_remote_fwd_listener(const char *listen_address,
1.295 djm 2651: u_short listen_port, int *allocated_listen_port, int gateway_ports)
1.160 markus 2652: {
2653: return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
1.295 djm 2654: listen_address, listen_port, allocated_listen_port,
2655: NULL, 0, gateway_ports);
1.160 markus 2656: }
2657:
1.27 markus 2658: /*
2659: * Initiate forwarding of connections to port "port" on remote host through
2660: * the secure channel to host:port from local side.
2661: */
1.1 deraadt 2662:
1.253 markus 2663: int
1.212 djm 2664: channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
1.73 markus 2665: const char *host_to_connect, u_short port_to_connect)
1.25 markus 2666: {
1.153 markus 2667: int type, success = 0;
1.73 markus 2668:
1.25 markus 2669: /* Record locally that connection to this host/port is permitted. */
2670: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2671: fatal("channel_request_remote_forwarding: too many forwards");
2672:
2673: /* Send the forward request to the remote side. */
1.44 markus 2674: if (compat20) {
1.212 djm 2675: const char *address_to_bind;
1.267 djm 2676: if (listen_host == NULL) {
2677: if (datafellows & SSH_BUG_RFWD_ADDR)
2678: address_to_bind = "127.0.0.1";
2679: else
2680: address_to_bind = "localhost";
2681: } else if (*listen_host == '\0' ||
2682: strcmp(listen_host, "*") == 0) {
2683: if (datafellows & SSH_BUG_RFWD_ADDR)
2684: address_to_bind = "0.0.0.0";
2685: else
2686: address_to_bind = "";
2687: } else
1.212 djm 2688: address_to_bind = listen_host;
2689:
1.44 markus 2690: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2691: packet_put_cstring("tcpip-forward");
1.173 markus 2692: packet_put_char(1); /* boolean: want reply */
1.44 markus 2693: packet_put_cstring(address_to_bind);
2694: packet_put_int(listen_port);
1.73 markus 2695: packet_send();
2696: packet_write_wait();
2697: /* Assume that server accepts the request */
2698: success = 1;
1.44 markus 2699: } else {
2700: packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50 markus 2701: packet_put_int(listen_port);
2702: packet_put_cstring(host_to_connect);
1.44 markus 2703: packet_put_int(port_to_connect);
2704: packet_send();
2705: packet_write_wait();
1.73 markus 2706:
2707: /* Wait for response from the remote side. */
1.153 markus 2708: type = packet_read();
1.73 markus 2709: switch (type) {
2710: case SSH_SMSG_SUCCESS:
2711: success = 1;
2712: break;
2713: case SSH_SMSG_FAILURE:
2714: break;
2715: default:
2716: /* Unknown packet */
2717: packet_disconnect("Protocol error for port forward request:"
2718: "received packet type %d.", type);
2719: }
2720: }
2721: if (success) {
2722: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2723: permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2724: permitted_opens[num_permitted_opens].listen_port = listen_port;
2725: num_permitted_opens++;
1.44 markus 2726: }
1.253 markus 2727: return (success ? 0 : -1);
1.1 deraadt 2728: }
2729:
1.27 markus 2730: /*
1.208 deraadt 2731: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 2732: * local side.
2733: */
2734: void
1.212 djm 2735: channel_request_rforward_cancel(const char *host, u_short port)
1.202 djm 2736: {
2737: int i;
2738:
2739: if (!compat20)
2740: return;
2741:
2742: for (i = 0; i < num_permitted_opens; i++) {
1.208 deraadt 2743: if (permitted_opens[i].host_to_connect != NULL &&
1.202 djm 2744: permitted_opens[i].listen_port == port)
2745: break;
2746: }
2747: if (i >= num_permitted_opens) {
2748: debug("%s: requested forward not found", __func__);
2749: return;
2750: }
2751: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2752: packet_put_cstring("cancel-tcpip-forward");
2753: packet_put_char(0);
1.212 djm 2754: packet_put_cstring(host == NULL ? "" : host);
1.202 djm 2755: packet_put_int(port);
2756: packet_send();
2757:
2758: permitted_opens[i].listen_port = 0;
2759: permitted_opens[i].port_to_connect = 0;
1.227 stevesk 2760: xfree(permitted_opens[i].host_to_connect);
1.202 djm 2761: permitted_opens[i].host_to_connect = NULL;
2762: }
2763:
2764: /*
1.27 markus 2765: * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2766: * listening for the port, and sends back a success reply (or disconnect
1.253 markus 2767: * message if there was an error).
1.27 markus 2768: */
1.253 markus 2769: int
1.56 markus 2770: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1 deraadt 2771: {
1.31 markus 2772: u_short port, host_port;
1.253 markus 2773: int success = 0;
1.25 markus 2774: char *hostname;
1.1 deraadt 2775:
1.25 markus 2776: /* Get arguments from the packet. */
2777: port = packet_get_int();
2778: hostname = packet_get_string(NULL);
2779: host_port = packet_get_int();
2780:
1.27 markus 2781: /*
2782: * Check that an unprivileged user is not trying to forward a
2783: * privileged port.
2784: */
1.25 markus 2785: if (port < IPPORT_RESERVED && !is_root)
1.192 markus 2786: packet_disconnect(
2787: "Requested forwarding of port %d but user is not root.",
2788: port);
2789: if (host_port == 0)
2790: packet_disconnect("Dynamic forwarding denied.");
2791:
1.73 markus 2792: /* Initiate forwarding */
1.253 markus 2793: success = channel_setup_local_fwd_listener(NULL, port, hostname,
1.212 djm 2794: host_port, gateway_ports);
1.25 markus 2795:
2796: /* Free the argument string. */
2797: xfree(hostname);
1.253 markus 2798:
2799: return (success ? 0 : -1);
1.1 deraadt 2800: }
2801:
1.99 markus 2802: /*
2803: * Permits opening to any host/port if permitted_opens[] is empty. This is
2804: * usually called by the server, because the user could connect to any port
2805: * anyway, and the server has no way to know but to trust the client anyway.
2806: */
2807: void
1.142 itojun 2808: channel_permit_all_opens(void)
1.99 markus 2809: {
2810: if (num_permitted_opens == 0)
2811: all_opens_permitted = 1;
2812: }
2813:
1.101 markus 2814: void
1.99 markus 2815: channel_add_permitted_opens(char *host, int port)
2816: {
2817: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1.253 markus 2818: fatal("channel_add_permitted_opens: too many forwards");
1.99 markus 2819: debug("allow port forwarding to host %s port %d", host, port);
2820:
2821: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2822: permitted_opens[num_permitted_opens].port_to_connect = port;
2823: num_permitted_opens++;
2824:
2825: all_opens_permitted = 0;
2826: }
2827:
1.258 dtucker 2828: int
1.257 dtucker 2829: channel_add_adm_permitted_opens(char *host, int port)
2830: {
2831: if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2832: fatal("channel_add_adm_permitted_opens: too many forwards");
1.258 dtucker 2833: debug("config allows port forwarding to host %s port %d", host, port);
1.257 dtucker 2834:
2835: permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2836: = xstrdup(host);
2837: permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
1.258 dtucker 2838: return ++num_adm_permitted_opens;
1.257 dtucker 2839: }
2840:
2841: void
1.99 markus 2842: channel_clear_permitted_opens(void)
2843: {
2844: int i;
2845:
2846: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 2847: if (permitted_opens[i].host_to_connect != NULL)
2848: xfree(permitted_opens[i].host_to_connect);
1.99 markus 2849: num_permitted_opens = 0;
1.257 dtucker 2850: }
2851:
2852: void
2853: channel_clear_adm_permitted_opens(void)
2854: {
2855: int i;
1.99 markus 2856:
1.257 dtucker 2857: for (i = 0; i < num_adm_permitted_opens; i++)
2858: if (permitted_adm_opens[i].host_to_connect != NULL)
2859: xfree(permitted_adm_opens[i].host_to_connect);
2860: num_adm_permitted_opens = 0;
1.278 dtucker 2861: }
2862:
2863: void
2864: channel_print_adm_permitted_opens(void)
2865: {
1.286 djm 2866: int i;
1.278 dtucker 2867:
1.290 stevesk 2868: printf("permitopen");
1.288 stevesk 2869: if (num_adm_permitted_opens == 0) {
1.290 stevesk 2870: printf(" any\n");
1.288 stevesk 2871: return;
2872: }
1.278 dtucker 2873: for (i = 0; i < num_adm_permitted_opens; i++)
2874: if (permitted_adm_opens[i].host_to_connect != NULL)
2875: printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
2876: permitted_adm_opens[i].port_to_connect);
1.290 stevesk 2877: printf("\n");
1.99 markus 2878: }
2879:
1.276 djm 2880: /* Try to start non-blocking connect to next host in cctx list */
1.127 itojun 2881: static int
1.276 djm 2882: connect_next(struct channel_connect *cctx)
1.41 markus 2883: {
1.276 djm 2884: int sock, saved_errno;
1.41 markus 2885: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2886:
1.276 djm 2887: for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
2888: if (cctx->ai->ai_family != AF_INET &&
2889: cctx->ai->ai_family != AF_INET6)
1.41 markus 2890: continue;
1.276 djm 2891: if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
2892: ntop, sizeof(ntop), strport, sizeof(strport),
2893: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2894: error("connect_next: getnameinfo failed");
1.41 markus 2895: continue;
2896: }
1.297 reyk 2897: if ((sock = socket_rdomain(cctx->ai->ai_family,
2898: cctx->ai->ai_socktype, cctx->ai->ai_protocol,
2899: channel_rdomain)) == -1) {
1.276 djm 2900: if (cctx->ai->ai_next == NULL)
1.186 djm 2901: error("socket: %.100s", strerror(errno));
2902: else
2903: verbose("socket: %.100s", strerror(errno));
1.41 markus 2904: continue;
2905: }
1.205 djm 2906: if (set_nonblock(sock) == -1)
2907: fatal("%s: set_nonblock(%d)", __func__, sock);
1.276 djm 2908: if (connect(sock, cctx->ai->ai_addr,
2909: cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
2910: debug("connect_next: host %.100s ([%.100s]:%s): "
2911: "%.100s", cctx->host, ntop, strport,
1.41 markus 2912: strerror(errno));
1.276 djm 2913: saved_errno = errno;
1.41 markus 2914: close(sock);
1.276 djm 2915: errno = saved_errno;
1.89 stevesk 2916: continue; /* fail -- try next */
1.41 markus 2917: }
1.276 djm 2918: debug("connect_next: host %.100s ([%.100s]:%s) "
2919: "in progress, fd=%d", cctx->host, ntop, strport, sock);
2920: cctx->ai = cctx->ai->ai_next;
2921: set_nodelay(sock);
2922: return sock;
2923: }
2924: return -1;
2925: }
1.41 markus 2926:
1.276 djm 2927: static void
2928: channel_connect_ctx_free(struct channel_connect *cctx)
2929: {
2930: xfree(cctx->host);
2931: if (cctx->aitop)
2932: freeaddrinfo(cctx->aitop);
2933: bzero(cctx, sizeof(*cctx));
2934: cctx->host = NULL;
2935: cctx->ai = cctx->aitop = NULL;
2936: }
2937:
2938: /* Return CONNECTING channel to remote host, port */
2939: static Channel *
2940: connect_to(const char *host, u_short port, char *ctype, char *rname)
2941: {
2942: struct addrinfo hints;
2943: int gaierr;
2944: int sock = -1;
2945: char strport[NI_MAXSERV];
2946: struct channel_connect cctx;
2947: Channel *c;
2948:
1.284 djm 2949: memset(&cctx, 0, sizeof(cctx));
1.276 djm 2950: memset(&hints, 0, sizeof(hints));
2951: hints.ai_family = IPv4or6;
2952: hints.ai_socktype = SOCK_STREAM;
2953: snprintf(strport, sizeof strport, "%d", port);
2954: if ((gaierr = getaddrinfo(host, strport, &hints, &cctx.aitop)) != 0) {
2955: error("connect_to %.100s: unknown host (%s)", host,
2956: ssh_gai_strerror(gaierr));
2957: return NULL;
1.41 markus 2958: }
1.276 djm 2959:
2960: cctx.host = xstrdup(host);
2961: cctx.port = port;
2962: cctx.ai = cctx.aitop;
2963:
2964: if ((sock = connect_next(&cctx)) == -1) {
2965: error("connect to %.100s port %d failed: %s",
2966: host, port, strerror(errno));
2967: channel_connect_ctx_free(&cctx);
2968: return NULL;
1.41 markus 2969: }
1.276 djm 2970: c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
2971: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
2972: c->connect_ctx = cctx;
2973: return c;
1.41 markus 2974: }
1.99 markus 2975:
1.276 djm 2976: Channel *
2977: channel_connect_by_listen_address(u_short listen_port, char *ctype, char *rname)
1.73 markus 2978: {
2979: int i;
1.99 markus 2980:
1.276 djm 2981: for (i = 0; i < num_permitted_opens; i++) {
1.202 djm 2982: if (permitted_opens[i].host_to_connect != NULL &&
1.276 djm 2983: permitted_opens[i].listen_port == listen_port) {
1.99 markus 2984: return connect_to(
1.73 markus 2985: permitted_opens[i].host_to_connect,
1.276 djm 2986: permitted_opens[i].port_to_connect, ctype, rname);
2987: }
2988: }
1.74 markus 2989: error("WARNING: Server requests forwarding for unknown listen_port %d",
2990: listen_port);
1.276 djm 2991: return NULL;
1.73 markus 2992: }
2993:
1.99 markus 2994: /* Check if connecting to that port is permitted and connect. */
1.276 djm 2995: Channel *
2996: channel_connect_to(const char *host, u_short port, char *ctype, char *rname)
1.99 markus 2997: {
1.257 dtucker 2998: int i, permit, permit_adm = 1;
1.99 markus 2999:
3000: permit = all_opens_permitted;
3001: if (!permit) {
3002: for (i = 0; i < num_permitted_opens; i++)
1.202 djm 3003: if (permitted_opens[i].host_to_connect != NULL &&
3004: permitted_opens[i].port_to_connect == port &&
1.99 markus 3005: strcmp(permitted_opens[i].host_to_connect, host) == 0)
3006: permit = 1;
1.257 dtucker 3007: }
1.99 markus 3008:
1.257 dtucker 3009: if (num_adm_permitted_opens > 0) {
3010: permit_adm = 0;
3011: for (i = 0; i < num_adm_permitted_opens; i++)
3012: if (permitted_adm_opens[i].host_to_connect != NULL &&
3013: permitted_adm_opens[i].port_to_connect == port &&
3014: strcmp(permitted_adm_opens[i].host_to_connect, host)
3015: == 0)
3016: permit_adm = 1;
1.99 markus 3017: }
1.257 dtucker 3018:
3019: if (!permit || !permit_adm) {
1.188 itojun 3020: logit("Received request to connect to host %.100s port %d, "
1.99 markus 3021: "but the request was denied.", host, port);
1.276 djm 3022: return NULL;
1.99 markus 3023: }
1.276 djm 3024: return connect_to(host, port, ctype, rname);
1.204 djm 3025: }
3026:
3027: void
3028: channel_send_window_changes(void)
3029: {
1.209 avsm 3030: u_int i;
1.204 djm 3031: struct winsize ws;
3032:
3033: for (i = 0; i < channels_alloc; i++) {
1.213 deraadt 3034: if (channels[i] == NULL || !channels[i]->client_tty ||
1.204 djm 3035: channels[i]->type != SSH_CHANNEL_OPEN)
3036: continue;
3037: if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3038: continue;
3039: channel_request_start(i, "window-change", 0);
1.238 deraadt 3040: packet_put_int((u_int)ws.ws_col);
3041: packet_put_int((u_int)ws.ws_row);
3042: packet_put_int((u_int)ws.ws_xpixel);
3043: packet_put_int((u_int)ws.ws_ypixel);
1.204 djm 3044: packet_send();
3045: }
1.99 markus 3046: }
3047:
1.121 markus 3048: /* -- X11 forwarding */
1.1 deraadt 3049:
1.27 markus 3050: /*
3051: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 3052: * Returns 0 and a suitable display number for the DISPLAY variable
3053: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 3054: */
1.141 stevesk 3055: int
1.163 stevesk 3056: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.222 djm 3057: int single_connection, u_int *display_numberp, int **chanids)
1.1 deraadt 3058: {
1.149 markus 3059: Channel *nc = NULL;
1.31 markus 3060: int display_number, sock;
3061: u_short port;
1.35 markus 3062: struct addrinfo hints, *ai, *aitop;
3063: char strport[NI_MAXSERV];
3064: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 3065:
1.224 markus 3066: if (chanids == NULL)
3067: return -1;
3068:
1.33 markus 3069: for (display_number = x11_display_offset;
1.148 deraadt 3070: display_number < MAX_DISPLAYS;
3071: display_number++) {
1.25 markus 3072: port = 6000 + display_number;
1.35 markus 3073: memset(&hints, 0, sizeof(hints));
3074: hints.ai_family = IPv4or6;
1.163 stevesk 3075: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 3076: hints.ai_socktype = SOCK_STREAM;
3077: snprintf(strport, sizeof strport, "%d", port);
3078: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1.271 dtucker 3079: error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
1.141 stevesk 3080: return -1;
1.25 markus 3081: }
1.35 markus 3082: for (ai = aitop; ai; ai = ai->ai_next) {
3083: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3084: continue;
1.297 reyk 3085: sock = socket_rdomain(ai->ai_family, ai->ai_socktype,
3086: ai->ai_protocol, channel_rdomain);
1.35 markus 3087: if (sock < 0) {
3088: error("socket: %.100s", strerror(errno));
1.203 markus 3089: freeaddrinfo(aitop);
1.141 stevesk 3090: return -1;
1.35 markus 3091: }
1.226 djm 3092: channel_set_reuseaddr(sock);
1.35 markus 3093: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 3094: debug2("bind port %d: %.100s", port, strerror(errno));
1.35 markus 3095: close(sock);
1.183 itojun 3096:
1.35 markus 3097: for (n = 0; n < num_socks; n++) {
3098: close(socks[n]);
3099: }
3100: num_socks = 0;
3101: break;
3102: }
3103: socks[num_socks++] = sock;
3104: if (num_socks == NUM_SOCKS)
3105: break;
1.25 markus 3106: }
1.83 stevesk 3107: freeaddrinfo(aitop);
1.35 markus 3108: if (num_socks > 0)
3109: break;
1.25 markus 3110: }
3111: if (display_number >= MAX_DISPLAYS) {
3112: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 3113: return -1;
1.25 markus 3114: }
3115: /* Start listening for connections on the socket. */
1.35 markus 3116: for (n = 0; n < num_socks; n++) {
3117: sock = socks[n];
1.199 markus 3118: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 3119: error("listen: %.100s", strerror(errno));
3120: close(sock);
1.141 stevesk 3121: return -1;
1.35 markus 3122: }
1.25 markus 3123: }
1.35 markus 3124:
3125: /* Allocate a channel for each socket. */
1.242 djm 3126: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 3127: for (n = 0; n < num_socks; n++) {
3128: sock = socks[n];
1.149 markus 3129: nc = channel_new("x11 listener",
1.51 markus 3130: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3131: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 3132: 0, "X11 inet listener", 1);
1.167 markus 3133: nc->single_connection = single_connection;
1.224 markus 3134: (*chanids)[n] = nc->self;
1.35 markus 3135: }
1.224 markus 3136: (*chanids)[n] = -1;
1.1 deraadt 3137:
1.141 stevesk 3138: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 3139: *display_numberp = display_number;
3140: return (0);
1.1 deraadt 3141: }
3142:
1.127 itojun 3143: static int
1.77 markus 3144: connect_local_xsocket(u_int dnr)
1.1 deraadt 3145: {
1.25 markus 3146: int sock;
3147: struct sockaddr_un addr;
3148:
1.147 stevesk 3149: sock = socket(AF_UNIX, SOCK_STREAM, 0);
3150: if (sock < 0)
3151: error("socket: %.100s", strerror(errno));
3152: memset(&addr, 0, sizeof(addr));
3153: addr.sun_family = AF_UNIX;
3154: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 3155: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 3156: return sock;
3157: close(sock);
1.25 markus 3158: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
3159: return -1;
1.1 deraadt 3160: }
3161:
1.51 markus 3162: int
3163: x11_connect_display(void)
1.1 deraadt 3164: {
1.248 deraadt 3165: u_int display_number;
1.25 markus 3166: const char *display;
1.51 markus 3167: char buf[1024], *cp;
1.35 markus 3168: struct addrinfo hints, *ai, *aitop;
3169: char strport[NI_MAXSERV];
1.248 deraadt 3170: int gaierr, sock = 0;
1.25 markus 3171:
3172: /* Try to open a socket for the local X server. */
3173: display = getenv("DISPLAY");
3174: if (!display) {
3175: error("DISPLAY not set.");
1.51 markus 3176: return -1;
1.25 markus 3177: }
1.27 markus 3178: /*
3179: * Now we decode the value of the DISPLAY variable and make a
3180: * connection to the real X server.
3181: */
3182:
3183: /*
3184: * Check if it is a unix domain socket. Unix domain displays are in
3185: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
3186: */
1.25 markus 3187: if (strncmp(display, "unix:", 5) == 0 ||
3188: display[0] == ':') {
3189: /* Connect to the unix domain socket. */
1.248 deraadt 3190: if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
1.25 markus 3191: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 3192: display);
1.51 markus 3193: return -1;
1.25 markus 3194: }
3195: /* Create a socket. */
3196: sock = connect_local_xsocket(display_number);
3197: if (sock < 0)
1.51 markus 3198: return -1;
1.25 markus 3199:
3200: /* OK, we now have a connection to the display. */
1.51 markus 3201: return sock;
1.25 markus 3202: }
1.27 markus 3203: /*
3204: * Connect to an inet socket. The DISPLAY value is supposedly
3205: * hostname:d[.s], where hostname may also be numeric IP address.
3206: */
1.145 stevesk 3207: strlcpy(buf, display, sizeof(buf));
1.25 markus 3208: cp = strchr(buf, ':');
3209: if (!cp) {
3210: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 3211: return -1;
1.25 markus 3212: }
3213: *cp = 0;
1.27 markus 3214: /* buf now contains the host name. But first we parse the display number. */
1.248 deraadt 3215: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 3216: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 3217: display);
1.51 markus 3218: return -1;
1.25 markus 3219: }
1.35 markus 3220:
3221: /* Look up the host address */
3222: memset(&hints, 0, sizeof(hints));
3223: hints.ai_family = IPv4or6;
3224: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 3225: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 3226: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1.271 dtucker 3227: error("%.100s: unknown host. (%s)", buf,
3228: ssh_gai_strerror(gaierr));
1.51 markus 3229: return -1;
1.25 markus 3230: }
1.35 markus 3231: for (ai = aitop; ai; ai = ai->ai_next) {
3232: /* Create a socket. */
1.297 reyk 3233: sock = socket_rdomain(ai->ai_family, ai->ai_socktype,
3234: ai->ai_protocol, channel_rdomain);
1.35 markus 3235: if (sock < 0) {
1.194 markus 3236: debug2("socket: %.100s", strerror(errno));
1.41 markus 3237: continue;
3238: }
3239: /* Connect it to the display. */
3240: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.248 deraadt 3241: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 3242: 6000 + display_number, strerror(errno));
3243: close(sock);
3244: continue;
3245: }
3246: /* Success */
3247: break;
1.35 markus 3248: }
3249: freeaddrinfo(aitop);
3250: if (!ai) {
1.248 deraadt 3251: error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
1.35 markus 3252: strerror(errno));
1.51 markus 3253: return -1;
1.25 markus 3254: }
1.162 stevesk 3255: set_nodelay(sock);
1.51 markus 3256: return sock;
3257: }
3258:
3259: /*
3260: * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3261: * the remote channel number. We should do whatever we want, and respond
3262: * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3263: */
3264:
1.259 stevesk 3265: /* ARGSUSED */
1.51 markus 3266: void
1.154 markus 3267: x11_input_open(int type, u_int32_t seq, void *ctxt)
1.51 markus 3268: {
1.113 markus 3269: Channel *c = NULL;
3270: int remote_id, sock = 0;
1.51 markus 3271: char *remote_host;
1.25 markus 3272:
1.113 markus 3273: debug("Received X11 open request.");
1.51 markus 3274:
1.113 markus 3275: remote_id = packet_get_int();
1.121 markus 3276:
3277: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113 markus 3278: remote_host = packet_get_string(NULL);
1.51 markus 3279: } else {
3280: remote_host = xstrdup("unknown (remote did not supply name)");
3281: }
1.152 markus 3282: packet_check_eom();
1.25 markus 3283:
1.51 markus 3284: /* Obtain a connection to the real X display. */
3285: sock = x11_connect_display();
1.113 markus 3286: if (sock != -1) {
3287: /* Allocate a channel for this connection. */
3288: c = channel_new("connected x11 socket",
3289: SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3290: remote_host, 1);
1.167 markus 3291: c->remote_id = remote_id;
3292: c->force_drain = 1;
1.113 markus 3293: }
1.190 markus 3294: xfree(remote_host);
1.113 markus 3295: if (c == NULL) {
1.51 markus 3296: /* Send refusal to the remote host. */
3297: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 3298: packet_put_int(remote_id);
1.51 markus 3299: } else {
3300: /* Send a confirmation to the remote host. */
3301: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113 markus 3302: packet_put_int(remote_id);
3303: packet_put_int(c->self);
1.51 markus 3304: }
1.113 markus 3305: packet_send();
1.72 markus 3306: }
3307:
3308: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
1.259 stevesk 3309: /* ARGSUSED */
1.72 markus 3310: void
1.154 markus 3311: deny_input_open(int type, u_int32_t seq, void *ctxt)
1.72 markus 3312: {
3313: int rchan = packet_get_int();
1.180 deraadt 3314:
1.143 deraadt 3315: switch (type) {
1.72 markus 3316: case SSH_SMSG_AGENT_OPEN:
3317: error("Warning: ssh server tried agent forwarding.");
3318: break;
3319: case SSH_SMSG_X11_OPEN:
3320: error("Warning: ssh server tried X11 forwarding.");
3321: break;
3322: default:
1.154 markus 3323: error("deny_input_open: type %d", type);
1.72 markus 3324: break;
3325: }
1.230 stevesk 3326: error("Warning: this is probably a break-in attempt by a malicious server.");
1.72 markus 3327: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3328: packet_put_int(rchan);
3329: packet_send();
1.1 deraadt 3330: }
3331:
1.27 markus 3332: /*
3333: * Requests forwarding of X11 connections, generates fake authentication
3334: * data, and enables authentication spoofing.
1.121 markus 3335: * This should be called in the client only.
1.27 markus 3336: */
1.49 markus 3337: void
1.215 djm 3338: x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
1.51 markus 3339: const char *proto, const char *data)
1.1 deraadt 3340: {
1.77 markus 3341: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 3342: u_int i, value;
1.25 markus 3343: char *new_data;
3344: int screen_number;
3345: const char *cp;
1.207 avsm 3346: u_int32_t rnd = 0;
1.25 markus 3347:
1.220 markus 3348: if (x11_saved_display == NULL)
3349: x11_saved_display = xstrdup(disp);
3350: else if (strcmp(disp, x11_saved_display) != 0) {
1.219 djm 3351: error("x11_request_forwarding_with_spoofing: different "
3352: "$DISPLAY already forwarded");
3353: return;
3354: }
3355:
1.266 djm 3356: cp = strchr(disp, ':');
1.25 markus 3357: if (cp)
3358: cp = strchr(cp, '.');
3359: if (cp)
1.245 deraadt 3360: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 3361: else
3362: screen_number = 0;
3363:
1.219 djm 3364: if (x11_saved_proto == NULL) {
3365: /* Save protocol name. */
3366: x11_saved_proto = xstrdup(proto);
3367: /*
1.221 djm 3368: * Extract real authentication data and generate fake data
1.219 djm 3369: * of the same length.
3370: */
3371: x11_saved_data = xmalloc(data_len);
3372: x11_fake_data = xmalloc(data_len);
3373: for (i = 0; i < data_len; i++) {
3374: if (sscanf(data + 2 * i, "%2x", &value) != 1)
3375: fatal("x11_request_forwarding: bad "
3376: "authentication data: %.100s", data);
3377: if (i % 4 == 0)
3378: rnd = arc4random();
3379: x11_saved_data[i] = value;
3380: x11_fake_data[i] = rnd & 0xff;
3381: rnd >>= 8;
3382: }
3383: x11_saved_data_len = data_len;
3384: x11_fake_data_len = data_len;
1.25 markus 3385: }
3386:
3387: /* Convert the fake data into hex. */
1.219 djm 3388: new_data = tohex(x11_fake_data, data_len);
1.25 markus 3389:
3390: /* Send the request packet. */
1.51 markus 3391: if (compat20) {
3392: channel_request_start(client_session_id, "x11-req", 0);
3393: packet_put_char(0); /* XXX bool single connection */
3394: } else {
3395: packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3396: }
3397: packet_put_cstring(proto);
3398: packet_put_cstring(new_data);
1.25 markus 3399: packet_put_int(screen_number);
3400: packet_send();
3401: packet_write_wait();
3402: xfree(new_data);
1.1 deraadt 3403: }
3404:
1.121 markus 3405:
3406: /* -- agent forwarding */
3407:
1.1 deraadt 3408: /* Sends a message to the server to request authentication fd forwarding. */
3409:
1.49 markus 3410: void
1.142 itojun 3411: auth_request_forwarding(void)
1.1 deraadt 3412: {
1.25 markus 3413: packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3414: packet_send();
3415: packet_write_wait();
1.1 deraadt 3416: }