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