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