Annotation of src/usr.bin/ssh/channels.c, Revision 1.359
1.359 ! djm 1: /* $OpenBSD: channels.c,v 1.358 2017/04/30 23:13:25 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.41 markus 2596: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.249 djm 2597: /* ARGSUSED */
1.339 markus 2598: int
1.154 markus 2599: channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1.41 markus 2600: {
2601: int id = packet_get_int();
2602: Channel *c = channel_lookup(id);
1.151 markus 2603:
1.41 markus 2604: if (c == NULL)
2605: packet_disconnect("Received oclose for nonexistent channel %d.", id);
1.354 markus 2606: if (channel_proxy_upstream(c, type, seq, ctxt))
2607: return 0;
2608: packet_check_eom();
1.41 markus 2609: chan_rcvd_oclose(c);
1.339 markus 2610: return 0;
1.41 markus 2611: }
1.1 deraadt 2612:
1.249 djm 2613: /* ARGSUSED */
1.339 markus 2614: int
1.154 markus 2615: channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2616: {
1.41 markus 2617: int id = packet_get_int();
2618: Channel *c = channel_lookup(id);
1.1 deraadt 2619:
1.41 markus 2620: if (c == NULL)
2621: packet_disconnect("Received close confirmation for "
2622: "out-of-range channel %d.", id);
1.354 markus 2623: if (channel_proxy_upstream(c, type, seq, ctxt))
2624: return 0;
2625: packet_check_eom();
1.323 dtucker 2626: if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
1.41 markus 2627: packet_disconnect("Received close confirmation for "
2628: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 2629: channel_free(c);
1.339 markus 2630: return 0;
1.1 deraadt 2631: }
2632:
1.249 djm 2633: /* ARGSUSED */
1.339 markus 2634: int
1.154 markus 2635: channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2636: {
1.41 markus 2637: int id, remote_id;
2638: Channel *c;
1.1 deraadt 2639:
1.41 markus 2640: id = packet_get_int();
2641: c = channel_lookup(id);
1.25 markus 2642:
1.354 markus 2643: if (c==NULL)
2644: packet_disconnect("Received open confirmation for "
2645: "unknown channel %d.", id);
2646: if (channel_proxy_upstream(c, type, seq, ctxt))
2647: return 0;
2648: if (c->type != SSH_CHANNEL_OPENING)
1.41 markus 2649: packet_disconnect("Received open confirmation for "
2650: "non-opening channel %d.", id);
2651: remote_id = packet_get_int();
1.27 markus 2652: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 2653: c->remote_id = remote_id;
2654: c->type = SSH_CHANNEL_OPEN;
1.44 markus 2655:
1.358 djm 2656: c->remote_window = packet_get_int();
2657: c->remote_maxpacket = packet_get_int();
2658: if (c->open_confirm) {
2659: debug2("callback start");
2660: c->open_confirm(c->self, 1, c->open_confirm_ctx);
2661: debug2("callback done");
1.44 markus 2662: }
1.358 djm 2663: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2664: c->remote_window, c->remote_maxpacket);
1.152 markus 2665: packet_check_eom();
1.339 markus 2666: return 0;
1.1 deraadt 2667: }
2668:
1.127 itojun 2669: static char *
1.114 markus 2670: reason2txt(int reason)
2671: {
1.143 deraadt 2672: switch (reason) {
1.114 markus 2673: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2674: return "administratively prohibited";
2675: case SSH2_OPEN_CONNECT_FAILED:
2676: return "connect failed";
2677: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2678: return "unknown channel type";
2679: case SSH2_OPEN_RESOURCE_SHORTAGE:
2680: return "resource shortage";
2681: }
1.117 stevesk 2682: return "unknown reason";
1.114 markus 2683: }
2684:
1.249 djm 2685: /* ARGSUSED */
1.339 markus 2686: int
1.154 markus 2687: channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1.1 deraadt 2688: {
1.86 markus 2689: int id, reason;
2690: char *msg = NULL, *lang = NULL;
1.41 markus 2691: Channel *c;
2692:
2693: id = packet_get_int();
2694: c = channel_lookup(id);
1.25 markus 2695:
1.354 markus 2696: if (c==NULL)
2697: packet_disconnect("Received open failure for "
2698: "unknown channel %d.", id);
2699: if (channel_proxy_upstream(c, type, seq, ctxt))
2700: return 0;
2701: if (c->type != SSH_CHANNEL_OPENING)
1.41 markus 2702: packet_disconnect("Received open failure for "
2703: "non-opening channel %d.", id);
1.358 djm 2704: reason = packet_get_int();
2705: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2706: msg = packet_get_string(NULL);
2707: lang = packet_get_string(NULL);
2708: }
2709: logit("channel %d: open failed: %s%s%s", id,
2710: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2711: free(msg);
2712: free(lang);
2713: if (c->open_confirm) {
2714: debug2("callback start");
2715: c->open_confirm(c->self, 0, c->open_confirm_ctx);
2716: debug2("callback done");
1.44 markus 2717: }
1.152 markus 2718: packet_check_eom();
1.291 djm 2719: /* Schedule the channel for cleanup/deletion. */
2720: chan_mark_dead(c);
1.339 markus 2721: return 0;
1.44 markus 2722: }
2723:
1.249 djm 2724: /* ARGSUSED */
1.339 markus 2725: int
1.154 markus 2726: channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1.121 markus 2727: {
2728: Channel *c;
1.178 markus 2729: int id;
1.346 djm 2730: u_int adjust, tmp;
1.121 markus 2731:
2732: /* Get the channel number and verify it. */
2733: id = packet_get_int();
2734: c = channel_lookup(id);
1.1 deraadt 2735:
1.229 markus 2736: if (c == NULL) {
2737: logit("Received window adjust for non-open channel %d.", id);
1.339 markus 2738: return 0;
1.121 markus 2739: }
1.354 markus 2740: if (channel_proxy_upstream(c, type, seq, ctxt))
2741: return 0;
1.121 markus 2742: adjust = packet_get_int();
1.152 markus 2743: packet_check_eom();
1.178 markus 2744: debug2("channel %d: rcvd adjust %u", id, adjust);
1.346 djm 2745: if ((tmp = c->remote_window + adjust) < c->remote_window)
2746: fatal("channel %d: adjust %u overflows remote window %u",
2747: id, adjust, c->remote_window);
2748: c->remote_window = tmp;
1.339 markus 2749: return 0;
1.121 markus 2750: }
1.1 deraadt 2751:
1.249 djm 2752: /* ARGSUSED */
1.339 markus 2753: int
1.275 djm 2754: channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2755: {
2756: Channel *c;
2757: struct channel_confirm *cc;
1.289 markus 2758: int id;
1.275 djm 2759:
2760: /* Reset keepalive timeout */
1.296 andreas 2761: packet_set_alive_timeouts(0);
1.275 djm 2762:
1.289 markus 2763: id = packet_get_int();
2764: debug2("channel_input_status_confirm: type %d id %d", type, id);
1.275 djm 2765:
1.289 markus 2766: if ((c = channel_lookup(id)) == NULL) {
2767: logit("channel_input_status_confirm: %d: unknown", id);
1.339 markus 2768: return 0;
1.275 djm 2769: }
1.354 markus 2770: if (channel_proxy_upstream(c, type, seq, ctxt))
2771: return 0;
2772: packet_check_eom();
1.275 djm 2773: if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
1.339 markus 2774: return 0;
1.275 djm 2775: cc->cb(type, c, cc->ctx);
2776: TAILQ_REMOVE(&c->status_confirms, cc, entry);
1.329 tedu 2777: explicit_bzero(cc, sizeof(*cc));
1.321 djm 2778: free(cc);
1.339 markus 2779: return 0;
1.275 djm 2780: }
1.121 markus 2781:
2782: /* -- tcp forwarding */
1.135 markus 2783:
2784: void
2785: channel_set_af(int af)
2786: {
2787: IPv4or6 = af;
2788: }
1.121 markus 2789:
1.312 djm 2790:
2791: /*
2792: * Determine whether or not a port forward listens to loopback, the
2793: * specified address or wildcard. On the client, a specified bind
2794: * address will always override gateway_ports. On the server, a
2795: * gateway_ports of 1 (``yes'') will override the client's specification
2796: * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2797: * will bind to whatever address the client asked for.
2798: *
2799: * Special-case listen_addrs are:
2800: *
2801: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2802: * "" (empty string), "*" -> wildcard v4/v6
2803: * "localhost" -> loopback v4/v6
1.334 djm 2804: * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
1.312 djm 2805: */
2806: static const char *
2807: channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
1.336 millert 2808: int is_client, struct ForwardOptions *fwd_opts)
1.312 djm 2809: {
2810: const char *addr = NULL;
2811: int wildcard = 0;
2812:
2813: if (listen_addr == NULL) {
2814: /* No address specified: default to gateway_ports setting */
1.336 millert 2815: if (fwd_opts->gateway_ports)
1.312 djm 2816: wildcard = 1;
1.336 millert 2817: } else if (fwd_opts->gateway_ports || is_client) {
1.312 djm 2818: if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2819: strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2820: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
1.336 millert 2821: (!is_client && fwd_opts->gateway_ports == 1)) {
1.312 djm 2822: wildcard = 1;
1.326 djm 2823: /*
2824: * Notify client if they requested a specific listen
2825: * address and it was overridden.
2826: */
2827: if (*listen_addr != '\0' &&
2828: strcmp(listen_addr, "0.0.0.0") != 0 &&
2829: strcmp(listen_addr, "*") != 0) {
2830: packet_send_debug("Forwarding listen address "
2831: "\"%s\" overridden by server "
2832: "GatewayPorts", listen_addr);
2833: }
1.334 djm 2834: } else if (strcmp(listen_addr, "localhost") != 0 ||
2835: strcmp(listen_addr, "127.0.0.1") == 0 ||
2836: strcmp(listen_addr, "::1") == 0) {
2837: /* Accept localhost address when GatewayPorts=yes */
2838: addr = listen_addr;
1.326 djm 2839: }
1.334 djm 2840: } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2841: strcmp(listen_addr, "::1") == 0) {
2842: /*
2843: * If a specific IPv4/IPv6 localhost address has been
2844: * requested then accept it even if gateway_ports is in
2845: * effect. This allows the client to prefer IPv4 or IPv6.
2846: */
2847: addr = listen_addr;
1.312 djm 2848: }
2849: if (wildcardp != NULL)
2850: *wildcardp = wildcard;
2851: return addr;
2852: }
2853:
1.160 markus 2854: static int
1.336 millert 2855: channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2856: int *allocated_listen_port, struct ForwardOptions *fwd_opts)
1.25 markus 2857: {
1.113 markus 2858: Channel *c;
1.226 djm 2859: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 2860: struct addrinfo hints, *ai, *aitop;
1.212 djm 2861: const char *host, *addr;
1.35 markus 2862: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.295 djm 2863: in_port_t *lport_p;
1.25 markus 2864:
1.212 djm 2865: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 2866:
1.344 millert 2867: if (is_client && fwd->connect_path != NULL) {
2868: host = fwd->connect_path;
2869: } else {
2870: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2871: fwd->listen_host : fwd->connect_host;
2872: if (host == NULL) {
2873: error("No forward host name.");
2874: return 0;
2875: }
2876: if (strlen(host) >= NI_MAXHOST) {
2877: error("Forward host name too long.");
2878: return 0;
2879: }
1.87 markus 2880: }
1.25 markus 2881:
1.312 djm 2882: /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
1.336 millert 2883: addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2884: is_client, fwd_opts);
2885: debug3("%s: type %d wildcard %d addr %s", __func__,
1.212 djm 2886: type, wildcard, (addr == NULL) ? "NULL" : addr);
2887:
2888: /*
1.35 markus 2889: * getaddrinfo returns a loopback address if the hostname is
2890: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2891: */
1.35 markus 2892: memset(&hints, 0, sizeof(hints));
2893: hints.ai_family = IPv4or6;
1.212 djm 2894: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 2895: hints.ai_socktype = SOCK_STREAM;
1.336 millert 2896: snprintf(strport, sizeof strport, "%d", fwd->listen_port);
1.212 djm 2897: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2898: if (addr == NULL) {
2899: /* This really shouldn't happen */
2900: packet_disconnect("getaddrinfo: fatal error: %s",
1.271 dtucker 2901: ssh_gai_strerror(r));
1.212 djm 2902: } else {
1.336 millert 2903: error("%s: getaddrinfo(%.64s): %s", __func__, addr,
1.271 dtucker 2904: ssh_gai_strerror(r));
1.212 djm 2905: }
1.218 markus 2906: return 0;
1.212 djm 2907: }
1.295 djm 2908: if (allocated_listen_port != NULL)
2909: *allocated_listen_port = 0;
1.35 markus 2910: for (ai = aitop; ai; ai = ai->ai_next) {
1.295 djm 2911: switch (ai->ai_family) {
2912: case AF_INET:
2913: lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2914: sin_port;
2915: break;
2916: case AF_INET6:
2917: lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2918: sin6_port;
2919: break;
2920: default:
1.35 markus 2921: continue;
1.295 djm 2922: }
2923: /*
2924: * If allocating a port for -R forwards, then use the
2925: * same port for all address families.
2926: */
1.336 millert 2927: if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
1.295 djm 2928: allocated_listen_port != NULL && *allocated_listen_port > 0)
2929: *lport_p = htons(*allocated_listen_port);
2930:
1.35 markus 2931: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2932: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.336 millert 2933: error("%s: getnameinfo failed", __func__);
1.35 markus 2934: continue;
2935: }
2936: /* Create a port to listen for the host. */
1.300 dtucker 2937: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 2938: if (sock < 0) {
2939: /* this is no error since kernel may not support ipv6 */
2940: verbose("socket: %.100s", strerror(errno));
2941: continue;
2942: }
1.226 djm 2943:
2944: channel_set_reuseaddr(sock);
1.182 stevesk 2945:
1.295 djm 2946: debug("Local forwarding listening on %s port %s.",
2947: ntop, strport);
1.35 markus 2948:
2949: /* Bind the socket to the address. */
2950: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2951: /* address can be in use ipv6 address is already bound */
2952: verbose("bind: %.100s", strerror(errno));
2953: close(sock);
2954: continue;
2955: }
2956: /* Start listening for connections on the socket. */
1.199 markus 2957: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 2958: error("listen: %.100s", strerror(errno));
2959: close(sock);
2960: continue;
2961: }
1.295 djm 2962:
2963: /*
1.336 millert 2964: * fwd->listen_port == 0 requests a dynamically allocated port -
1.295 djm 2965: * record what we got.
2966: */
1.336 millert 2967: if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
1.295 djm 2968: allocated_listen_port != NULL &&
2969: *allocated_listen_port == 0) {
1.350 djm 2970: *allocated_listen_port = get_local_port(sock);
1.295 djm 2971: debug("Allocated listen port %d",
2972: *allocated_listen_port);
2973: }
2974:
1.35 markus 2975: /* Allocate a channel number for the socket. */
1.121 markus 2976: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2977: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 2978: 0, "port listener", 1);
1.293 djm 2979: c->path = xstrdup(host);
1.336 millert 2980: c->host_port = fwd->connect_port;
1.312 djm 2981: c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
1.336 millert 2982: if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
1.315 markus 2983: !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2984: c->listening_port = *allocated_listen_port;
2985: else
1.336 millert 2986: c->listening_port = fwd->listen_port;
1.35 markus 2987: success = 1;
2988: }
2989: if (success == 0)
1.336 millert 2990: error("%s: cannot listen to port: %d", __func__,
2991: fwd->listen_port);
1.35 markus 2992: freeaddrinfo(aitop);
1.87 markus 2993: return success;
1.25 markus 2994: }
1.1 deraadt 2995:
1.336 millert 2996: static int
2997: channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
2998: struct ForwardOptions *fwd_opts)
2999: {
3000: struct sockaddr_un sunaddr;
3001: const char *path;
3002: Channel *c;
3003: int port, sock;
3004: mode_t omask;
3005:
3006: switch (type) {
3007: case SSH_CHANNEL_UNIX_LISTENER:
3008: if (fwd->connect_path != NULL) {
3009: if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3010: error("Local connecting path too long: %s",
3011: fwd->connect_path);
3012: return 0;
3013: }
3014: path = fwd->connect_path;
3015: port = PORT_STREAMLOCAL;
3016: } else {
3017: if (fwd->connect_host == NULL) {
3018: error("No forward host name.");
3019: return 0;
3020: }
3021: if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3022: error("Forward host name too long.");
3023: return 0;
3024: }
3025: path = fwd->connect_host;
3026: port = fwd->connect_port;
3027: }
3028: break;
3029: case SSH_CHANNEL_RUNIX_LISTENER:
3030: path = fwd->listen_path;
3031: port = PORT_STREAMLOCAL;
3032: break;
3033: default:
3034: error("%s: unexpected channel type %d", __func__, type);
3035: return 0;
3036: }
3037:
3038: if (fwd->listen_path == NULL) {
3039: error("No forward path name.");
3040: return 0;
3041: }
3042: if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3043: error("Local listening path too long: %s", fwd->listen_path);
3044: return 0;
3045: }
3046:
3047: debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3048:
3049: /* Start a Unix domain listener. */
3050: omask = umask(fwd_opts->streamlocal_bind_mask);
3051: sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3052: fwd_opts->streamlocal_bind_unlink);
3053: umask(omask);
3054: if (sock < 0)
3055: return 0;
3056:
3057: debug("Local forwarding listening on path %s.", fwd->listen_path);
3058:
3059: /* Allocate a channel number for the socket. */
3060: c = channel_new("unix listener", type, sock, sock, -1,
3061: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3062: 0, "unix listener", 1);
3063: c->path = xstrdup(path);
3064: c->host_port = port;
3065: c->listening_port = PORT_STREAMLOCAL;
3066: c->listening_addr = xstrdup(fwd->listen_path);
3067: return 1;
3068: }
3069:
3070: static int
3071: channel_cancel_rport_listener_tcpip(const char *host, u_short port)
1.202 djm 3072: {
1.209 avsm 3073: u_int i;
3074: int found = 0;
1.202 djm 3075:
1.213 deraadt 3076: for (i = 0; i < channels_alloc; i++) {
1.202 djm 3077: Channel *c = channels[i];
1.312 djm 3078: if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3079: continue;
3080: if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3081: debug2("%s: close channel %d", __func__, i);
3082: channel_free(c);
3083: found = 1;
3084: }
3085: }
3086:
3087: return (found);
3088: }
3089:
1.336 millert 3090: static int
3091: channel_cancel_rport_listener_streamlocal(const char *path)
3092: {
3093: u_int i;
3094: int found = 0;
3095:
3096: for (i = 0; i < channels_alloc; i++) {
3097: Channel *c = channels[i];
3098: if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3099: continue;
3100: if (c->path == NULL)
3101: continue;
3102: if (strcmp(c->path, path) == 0) {
3103: debug2("%s: close channel %d", __func__, i);
3104: channel_free(c);
3105: found = 1;
3106: }
3107: }
3108:
3109: return (found);
3110: }
3111:
1.312 djm 3112: int
1.336 millert 3113: channel_cancel_rport_listener(struct Forward *fwd)
3114: {
3115: if (fwd->listen_path != NULL)
3116: return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3117: else
3118: return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3119: }
3120:
3121: static int
3122: channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3123: int cport, struct ForwardOptions *fwd_opts)
1.312 djm 3124: {
3125: u_int i;
3126: int found = 0;
1.336 millert 3127: const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
1.202 djm 3128:
1.312 djm 3129: for (i = 0; i < channels_alloc; i++) {
3130: Channel *c = channels[i];
3131: if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3132: continue;
1.313 markus 3133: if (c->listening_port != lport)
1.312 djm 3134: continue;
1.313 markus 3135: if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3136: /* skip dynamic forwardings */
3137: if (c->host_port == 0)
3138: continue;
3139: } else {
3140: if (c->host_port != cport)
3141: continue;
3142: }
1.312 djm 3143: if ((c->listening_addr == NULL && addr != NULL) ||
3144: (c->listening_addr != NULL && addr == NULL))
3145: continue;
3146: if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
1.210 djm 3147: debug2("%s: close channel %d", __func__, i);
1.202 djm 3148: channel_free(c);
3149: found = 1;
3150: }
3151: }
3152:
3153: return (found);
3154: }
3155:
1.336 millert 3156: static int
3157: channel_cancel_lport_listener_streamlocal(const char *path)
3158: {
3159: u_int i;
3160: int found = 0;
3161:
3162: if (path == NULL) {
3163: error("%s: no path specified.", __func__);
3164: return 0;
3165: }
3166:
3167: for (i = 0; i < channels_alloc; i++) {
3168: Channel *c = channels[i];
3169: if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3170: continue;
3171: if (c->listening_addr == NULL)
3172: continue;
3173: if (strcmp(c->listening_addr, path) == 0) {
3174: debug2("%s: close channel %d", __func__, i);
3175: channel_free(c);
3176: found = 1;
3177: }
3178: }
3179:
3180: return (found);
3181: }
3182:
3183: int
3184: channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3185: {
3186: if (fwd->listen_path != NULL)
3187: return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3188: else
3189: return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3190: }
3191:
1.160 markus 3192: /* protocol local port fwd, used by ssh (and sshd in v1) */
3193: int
1.336 millert 3194: channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
1.160 markus 3195: {
1.336 millert 3196: if (fwd->listen_path != NULL) {
3197: return channel_setup_fwd_listener_streamlocal(
3198: SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3199: } else {
3200: return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3201: fwd, NULL, fwd_opts);
3202: }
1.160 markus 3203: }
3204:
3205: /* protocol v2 remote port fwd, used by sshd */
3206: int
1.336 millert 3207: channel_setup_remote_fwd_listener(struct Forward *fwd,
3208: int *allocated_listen_port, struct ForwardOptions *fwd_opts)
1.160 markus 3209: {
1.336 millert 3210: if (fwd->listen_path != NULL) {
3211: return channel_setup_fwd_listener_streamlocal(
3212: SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3213: } else {
3214: return channel_setup_fwd_listener_tcpip(
3215: SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3216: fwd_opts);
3217: }
1.160 markus 3218: }
3219:
1.27 markus 3220: /*
1.312 djm 3221: * Translate the requested rfwd listen host to something usable for
3222: * this server.
3223: */
3224: static const char *
3225: channel_rfwd_bind_host(const char *listen_host)
3226: {
3227: if (listen_host == NULL) {
3228: if (datafellows & SSH_BUG_RFWD_ADDR)
3229: return "127.0.0.1";
3230: else
3231: return "localhost";
3232: } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3233: if (datafellows & SSH_BUG_RFWD_ADDR)
3234: return "0.0.0.0";
3235: else
3236: return "";
3237: } else
3238: return listen_host;
3239: }
3240:
3241: /*
1.27 markus 3242: * Initiate forwarding of connections to port "port" on remote host through
3243: * the secure channel to host:port from local side.
1.315 markus 3244: * Returns handle (index) for updating the dynamic listen port with
3245: * channel_update_permitted_opens().
1.27 markus 3246: */
1.253 markus 3247: int
1.336 millert 3248: channel_request_remote_forwarding(struct Forward *fwd)
1.25 markus 3249: {
1.358 djm 3250: int success = 0, idx = -1;
1.73 markus 3251:
1.25 markus 3252: /* Send the forward request to the remote side. */
1.358 djm 3253: packet_start(SSH2_MSG_GLOBAL_REQUEST);
3254: if (fwd->listen_path != NULL) {
3255: packet_put_cstring("streamlocal-forward@openssh.com");
3256: packet_put_char(1); /* boolean: want reply */
3257: packet_put_cstring(fwd->listen_path);
1.336 millert 3258: } else {
1.358 djm 3259: packet_put_cstring("tcpip-forward");
3260: packet_put_char(1); /* boolean: want reply */
3261: packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3262: packet_put_int(fwd->listen_port);
1.73 markus 3263: }
1.358 djm 3264: packet_send();
3265: packet_write_wait();
3266: /* Assume that server accepts the request */
3267: success = 1;
1.73 markus 3268: if (success) {
1.305 djm 3269: /* Record that connection to this host/port is permitted. */
1.342 deraadt 3270: permitted_opens = xreallocarray(permitted_opens,
1.305 djm 3271: num_permitted_opens + 1, sizeof(*permitted_opens));
1.315 markus 3272: idx = num_permitted_opens++;
1.336 millert 3273: if (fwd->connect_path != NULL) {
3274: permitted_opens[idx].host_to_connect =
3275: xstrdup(fwd->connect_path);
3276: permitted_opens[idx].port_to_connect =
3277: PORT_STREAMLOCAL;
3278: } else {
3279: permitted_opens[idx].host_to_connect =
3280: xstrdup(fwd->connect_host);
3281: permitted_opens[idx].port_to_connect =
3282: fwd->connect_port;
3283: }
3284: if (fwd->listen_path != NULL) {
3285: permitted_opens[idx].listen_host = NULL;
3286: permitted_opens[idx].listen_path =
3287: xstrdup(fwd->listen_path);
3288: permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3289: } else {
3290: permitted_opens[idx].listen_host =
3291: fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3292: permitted_opens[idx].listen_path = NULL;
3293: permitted_opens[idx].listen_port = fwd->listen_port;
3294: }
1.354 markus 3295: permitted_opens[idx].downstream = NULL;
1.44 markus 3296: }
1.315 markus 3297: return (idx);
1.1 deraadt 3298: }
3299:
1.333 markus 3300: static int
3301: open_match(ForwardPermission *allowed_open, const char *requestedhost,
1.336 millert 3302: int requestedport)
1.333 markus 3303: {
3304: if (allowed_open->host_to_connect == NULL)
3305: return 0;
3306: if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3307: allowed_open->port_to_connect != requestedport)
3308: return 0;
1.351 dtucker 3309: if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3310: strcmp(allowed_open->host_to_connect, requestedhost) != 0)
1.333 markus 3311: return 0;
3312: return 1;
3313: }
3314:
3315: /*
1.336 millert 3316: * Note that in the listen host/port case
1.333 markus 3317: * we don't support FWD_PERMIT_ANY_PORT and
3318: * need to translate between the configured-host (listen_host)
3319: * and what we've sent to the remote server (channel_rfwd_bind_host)
3320: */
3321: static int
1.336 millert 3322: open_listen_match_tcpip(ForwardPermission *allowed_open,
3323: const char *requestedhost, u_short requestedport, int translate)
1.333 markus 3324: {
3325: const char *allowed_host;
3326:
3327: if (allowed_open->host_to_connect == NULL)
3328: return 0;
3329: if (allowed_open->listen_port != requestedport)
3330: return 0;
1.335 djm 3331: if (!translate && allowed_open->listen_host == NULL &&
3332: requestedhost == NULL)
3333: return 1;
1.333 markus 3334: allowed_host = translate ?
3335: channel_rfwd_bind_host(allowed_open->listen_host) :
3336: allowed_open->listen_host;
3337: if (allowed_host == NULL ||
3338: strcmp(allowed_host, requestedhost) != 0)
3339: return 0;
3340: return 1;
3341: }
3342:
1.336 millert 3343: static int
3344: open_listen_match_streamlocal(ForwardPermission *allowed_open,
3345: const char *requestedpath)
3346: {
3347: if (allowed_open->host_to_connect == NULL)
3348: return 0;
3349: if (allowed_open->listen_port != PORT_STREAMLOCAL)
3350: return 0;
3351: if (allowed_open->listen_path == NULL ||
3352: strcmp(allowed_open->listen_path, requestedpath) != 0)
3353: return 0;
3354: return 1;
3355: }
3356:
1.27 markus 3357: /*
1.208 deraadt 3358: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 3359: * local side.
3360: */
1.336 millert 3361: static int
3362: channel_request_rforward_cancel_tcpip(const char *host, u_short port)
1.202 djm 3363: {
3364: int i;
3365:
3366: for (i = 0; i < num_permitted_opens; i++) {
1.336 millert 3367: if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
1.202 djm 3368: break;
3369: }
3370: if (i >= num_permitted_opens) {
3371: debug("%s: requested forward not found", __func__);
1.312 djm 3372: return -1;
1.202 djm 3373: }
3374: packet_start(SSH2_MSG_GLOBAL_REQUEST);
3375: packet_put_cstring("cancel-tcpip-forward");
3376: packet_put_char(0);
1.312 djm 3377: packet_put_cstring(channel_rfwd_bind_host(host));
1.202 djm 3378: packet_put_int(port);
3379: packet_send();
3380:
1.336 millert 3381: permitted_opens[i].listen_port = 0;
1.333 markus 3382: permitted_opens[i].port_to_connect = 0;
1.336 millert 3383: free(permitted_opens[i].host_to_connect);
3384: permitted_opens[i].host_to_connect = NULL;
3385: free(permitted_opens[i].listen_host);
3386: permitted_opens[i].listen_host = NULL;
3387: permitted_opens[i].listen_path = NULL;
1.354 markus 3388: permitted_opens[i].downstream = NULL;
1.336 millert 3389:
3390: return 0;
3391: }
3392:
3393: /*
3394: * Request cancellation of remote forwarding of Unix domain socket
3395: * path from local side.
3396: */
3397: static int
3398: channel_request_rforward_cancel_streamlocal(const char *path)
3399: {
3400: int i;
3401:
3402: for (i = 0; i < num_permitted_opens; i++) {
3403: if (open_listen_match_streamlocal(&permitted_opens[i], path))
3404: break;
3405: }
3406: if (i >= num_permitted_opens) {
3407: debug("%s: requested forward not found", __func__);
3408: return -1;
3409: }
3410: packet_start(SSH2_MSG_GLOBAL_REQUEST);
3411: packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3412: packet_put_char(0);
3413: packet_put_cstring(path);
3414: packet_send();
3415:
1.202 djm 3416: permitted_opens[i].listen_port = 0;
1.336 millert 3417: permitted_opens[i].port_to_connect = 0;
1.321 djm 3418: free(permitted_opens[i].host_to_connect);
1.202 djm 3419: permitted_opens[i].host_to_connect = NULL;
1.333 markus 3420: permitted_opens[i].listen_host = NULL;
1.336 millert 3421: free(permitted_opens[i].listen_path);
3422: permitted_opens[i].listen_path = NULL;
1.354 markus 3423: permitted_opens[i].downstream = NULL;
1.312 djm 3424:
3425: return 0;
1.202 djm 3426: }
3427:
3428: /*
1.336 millert 3429: * Request cancellation of remote forwarding of a connection from local side.
3430: */
3431: int
3432: channel_request_rforward_cancel(struct Forward *fwd)
3433: {
3434: if (fwd->listen_path != NULL) {
3435: return (channel_request_rforward_cancel_streamlocal(
3436: fwd->listen_path));
3437: } else {
3438: return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3439: fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3440: }
1.1 deraadt 3441: }
3442:
1.99 markus 3443: /*
3444: * Permits opening to any host/port if permitted_opens[] is empty. This is
3445: * usually called by the server, because the user could connect to any port
3446: * anyway, and the server has no way to know but to trust the client anyway.
3447: */
3448: void
1.142 itojun 3449: channel_permit_all_opens(void)
1.99 markus 3450: {
3451: if (num_permitted_opens == 0)
3452: all_opens_permitted = 1;
3453: }
3454:
1.101 markus 3455: void
1.99 markus 3456: channel_add_permitted_opens(char *host, int port)
3457: {
3458: debug("allow port forwarding to host %s port %d", host, port);
3459:
1.342 deraadt 3460: permitted_opens = xreallocarray(permitted_opens,
1.305 djm 3461: num_permitted_opens + 1, sizeof(*permitted_opens));
1.99 markus 3462: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3463: permitted_opens[num_permitted_opens].port_to_connect = port;
1.333 markus 3464: permitted_opens[num_permitted_opens].listen_host = NULL;
1.336 millert 3465: permitted_opens[num_permitted_opens].listen_path = NULL;
1.333 markus 3466: permitted_opens[num_permitted_opens].listen_port = 0;
1.354 markus 3467: permitted_opens[num_permitted_opens].downstream = NULL;
1.99 markus 3468: num_permitted_opens++;
3469:
3470: all_opens_permitted = 0;
1.315 markus 3471: }
3472:
3473: /*
3474: * Update the listen port for a dynamic remote forward, after
3475: * the actual 'newport' has been allocated. If 'newport' < 0 is
3476: * passed then they entry will be invalidated.
3477: */
3478: void
3479: channel_update_permitted_opens(int idx, int newport)
3480: {
3481: if (idx < 0 || idx >= num_permitted_opens) {
3482: debug("channel_update_permitted_opens: index out of range:"
3483: " %d num_permitted_opens %d", idx, num_permitted_opens);
3484: return;
3485: }
3486: debug("%s allowed port %d for forwarding to host %s port %d",
3487: newport > 0 ? "Updating" : "Removing",
3488: newport,
3489: permitted_opens[idx].host_to_connect,
3490: permitted_opens[idx].port_to_connect);
3491: if (newport >= 0) {
3492: permitted_opens[idx].listen_port =
3493: (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3494: } else {
3495: permitted_opens[idx].listen_port = 0;
3496: permitted_opens[idx].port_to_connect = 0;
1.321 djm 3497: free(permitted_opens[idx].host_to_connect);
1.315 markus 3498: permitted_opens[idx].host_to_connect = NULL;
1.333 markus 3499: free(permitted_opens[idx].listen_host);
3500: permitted_opens[idx].listen_host = NULL;
1.336 millert 3501: free(permitted_opens[idx].listen_path);
3502: permitted_opens[idx].listen_path = NULL;
1.315 markus 3503: }
1.99 markus 3504: }
3505:
1.258 dtucker 3506: int
1.257 dtucker 3507: channel_add_adm_permitted_opens(char *host, int port)
3508: {
1.258 dtucker 3509: debug("config allows port forwarding to host %s port %d", host, port);
1.257 dtucker 3510:
1.342 deraadt 3511: permitted_adm_opens = xreallocarray(permitted_adm_opens,
1.305 djm 3512: num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
1.257 dtucker 3513: permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3514: = xstrdup(host);
3515: permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
1.333 markus 3516: permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
1.336 millert 3517: permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
1.333 markus 3518: permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
1.258 dtucker 3519: return ++num_adm_permitted_opens;
1.257 dtucker 3520: }
3521:
3522: void
1.316 dtucker 3523: channel_disable_adm_local_opens(void)
3524: {
1.319 djm 3525: channel_clear_adm_permitted_opens();
1.343 dtucker 3526: permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
1.319 djm 3527: permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3528: num_adm_permitted_opens = 1;
1.316 dtucker 3529: }
3530:
3531: void
1.99 markus 3532: channel_clear_permitted_opens(void)
3533: {
3534: int i;
3535:
1.333 markus 3536: for (i = 0; i < num_permitted_opens; i++) {
1.321 djm 3537: free(permitted_opens[i].host_to_connect);
1.333 markus 3538: free(permitted_opens[i].listen_host);
1.336 millert 3539: free(permitted_opens[i].listen_path);
1.333 markus 3540: }
1.321 djm 3541: free(permitted_opens);
3542: permitted_opens = NULL;
1.99 markus 3543: num_permitted_opens = 0;
1.257 dtucker 3544: }
3545:
3546: void
3547: channel_clear_adm_permitted_opens(void)
3548: {
3549: int i;
1.99 markus 3550:
1.333 markus 3551: for (i = 0; i < num_adm_permitted_opens; i++) {
1.321 djm 3552: free(permitted_adm_opens[i].host_to_connect);
1.333 markus 3553: free(permitted_adm_opens[i].listen_host);
1.336 millert 3554: free(permitted_adm_opens[i].listen_path);
1.333 markus 3555: }
1.321 djm 3556: free(permitted_adm_opens);
3557: permitted_adm_opens = NULL;
1.257 dtucker 3558: num_adm_permitted_opens = 0;
1.278 dtucker 3559: }
3560:
3561: void
3562: channel_print_adm_permitted_opens(void)
3563: {
1.286 djm 3564: int i;
1.278 dtucker 3565:
1.290 stevesk 3566: printf("permitopen");
1.288 stevesk 3567: if (num_adm_permitted_opens == 0) {
1.290 stevesk 3568: printf(" any\n");
1.288 stevesk 3569: return;
3570: }
1.278 dtucker 3571: for (i = 0; i < num_adm_permitted_opens; i++)
1.316 dtucker 3572: if (permitted_adm_opens[i].host_to_connect == NULL)
3573: printf(" none");
3574: else
1.278 dtucker 3575: printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3576: permitted_adm_opens[i].port_to_connect);
1.290 stevesk 3577: printf("\n");
1.99 markus 3578: }
3579:
1.314 dtucker 3580: /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3581: int
3582: permitopen_port(const char *p)
3583: {
3584: int port;
3585:
3586: if (strcmp(p, "*") == 0)
3587: return FWD_PERMIT_ANY_PORT;
3588: if ((port = a2port(p)) > 0)
3589: return port;
3590: return -1;
3591: }
3592:
1.276 djm 3593: /* Try to start non-blocking connect to next host in cctx list */
1.127 itojun 3594: static int
1.276 djm 3595: connect_next(struct channel_connect *cctx)
1.41 markus 3596: {
1.276 djm 3597: int sock, saved_errno;
1.336 millert 3598: struct sockaddr_un *sunaddr;
1.352 deraadt 3599: char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
1.41 markus 3600:
1.276 djm 3601: for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
1.336 millert 3602: switch (cctx->ai->ai_family) {
3603: case AF_UNIX:
3604: /* unix:pathname instead of host:port */
3605: sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3606: strlcpy(ntop, "unix", sizeof(ntop));
3607: strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3608: break;
3609: case AF_INET:
3610: case AF_INET6:
3611: if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3612: ntop, sizeof(ntop), strport, sizeof(strport),
3613: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3614: error("connect_next: getnameinfo failed");
3615: continue;
3616: }
3617: break;
3618: default:
1.41 markus 3619: continue;
3620: }
1.300 dtucker 3621: if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3622: cctx->ai->ai_protocol)) == -1) {
1.276 djm 3623: if (cctx->ai->ai_next == NULL)
1.186 djm 3624: error("socket: %.100s", strerror(errno));
3625: else
3626: verbose("socket: %.100s", strerror(errno));
1.41 markus 3627: continue;
3628: }
1.205 djm 3629: if (set_nonblock(sock) == -1)
3630: fatal("%s: set_nonblock(%d)", __func__, sock);
1.276 djm 3631: if (connect(sock, cctx->ai->ai_addr,
3632: cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3633: debug("connect_next: host %.100s ([%.100s]:%s): "
3634: "%.100s", cctx->host, ntop, strport,
1.41 markus 3635: strerror(errno));
1.276 djm 3636: saved_errno = errno;
1.41 markus 3637: close(sock);
1.276 djm 3638: errno = saved_errno;
1.89 stevesk 3639: continue; /* fail -- try next */
1.41 markus 3640: }
1.336 millert 3641: if (cctx->ai->ai_family != AF_UNIX)
3642: set_nodelay(sock);
1.276 djm 3643: debug("connect_next: host %.100s ([%.100s]:%s) "
3644: "in progress, fd=%d", cctx->host, ntop, strport, sock);
3645: cctx->ai = cctx->ai->ai_next;
3646: return sock;
3647: }
3648: return -1;
3649: }
1.41 markus 3650:
1.276 djm 3651: static void
3652: channel_connect_ctx_free(struct channel_connect *cctx)
3653: {
1.321 djm 3654: free(cctx->host);
1.336 millert 3655: if (cctx->aitop) {
3656: if (cctx->aitop->ai_family == AF_UNIX)
3657: free(cctx->aitop);
3658: else
3659: freeaddrinfo(cctx->aitop);
3660: }
1.329 tedu 3661: memset(cctx, 0, sizeof(*cctx));
1.276 djm 3662: }
3663:
1.357 dtucker 3664: /*
3665: * Return CONNECTING channel to remote host:port or local socket path,
3666: * passing back the failure reason if appropriate.
3667: */
1.276 djm 3668: static Channel *
1.357 dtucker 3669: connect_to_reason(const char *name, int port, char *ctype, char *rname,
3670: int *reason, const char **errmsg)
1.276 djm 3671: {
3672: struct addrinfo hints;
3673: int gaierr;
3674: int sock = -1;
3675: char strport[NI_MAXSERV];
3676: struct channel_connect cctx;
3677: Channel *c;
3678:
1.284 djm 3679: memset(&cctx, 0, sizeof(cctx));
1.336 millert 3680:
3681: if (port == PORT_STREAMLOCAL) {
3682: struct sockaddr_un *sunaddr;
3683: struct addrinfo *ai;
3684:
3685: if (strlen(name) > sizeof(sunaddr->sun_path)) {
3686: error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3687: return (NULL);
3688: }
3689:
3690: /*
3691: * Fake up a struct addrinfo for AF_UNIX connections.
3692: * channel_connect_ctx_free() must check ai_family
3693: * and use free() not freeaddirinfo() for AF_UNIX.
3694: */
3695: ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3696: memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3697: ai->ai_addr = (struct sockaddr *)(ai + 1);
3698: ai->ai_addrlen = sizeof(*sunaddr);
3699: ai->ai_family = AF_UNIX;
3700: ai->ai_socktype = SOCK_STREAM;
3701: ai->ai_protocol = PF_UNSPEC;
3702: sunaddr = (struct sockaddr_un *)ai->ai_addr;
3703: sunaddr->sun_family = AF_UNIX;
3704: strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3705: cctx.aitop = ai;
3706: } else {
3707: memset(&hints, 0, sizeof(hints));
3708: hints.ai_family = IPv4or6;
3709: hints.ai_socktype = SOCK_STREAM;
3710: snprintf(strport, sizeof strport, "%d", port);
1.357 dtucker 3711: if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3712: != 0) {
3713: if (errmsg != NULL)
3714: *errmsg = ssh_gai_strerror(gaierr);
3715: if (reason != NULL)
3716: *reason = SSH2_OPEN_CONNECT_FAILED;
1.336 millert 3717: error("connect_to %.100s: unknown host (%s)", name,
3718: ssh_gai_strerror(gaierr));
3719: return NULL;
3720: }
1.41 markus 3721: }
1.276 djm 3722:
1.336 millert 3723: cctx.host = xstrdup(name);
1.276 djm 3724: cctx.port = port;
3725: cctx.ai = cctx.aitop;
3726:
3727: if ((sock = connect_next(&cctx)) == -1) {
3728: error("connect to %.100s port %d failed: %s",
1.336 millert 3729: name, port, strerror(errno));
1.276 djm 3730: channel_connect_ctx_free(&cctx);
3731: return NULL;
1.41 markus 3732: }
1.276 djm 3733: c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3734: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3735: c->connect_ctx = cctx;
3736: return c;
1.41 markus 3737: }
1.99 markus 3738:
1.357 dtucker 3739: /* Return CONNECTING channel to remote host:port or local socket path */
3740: static Channel *
3741: connect_to(const char *name, int port, char *ctype, char *rname)
3742: {
3743: return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3744: }
3745:
1.354 markus 3746: /*
3747: * returns either the newly connected channel or the downstream channel
3748: * that needs to deal with this connection.
3749: */
1.276 djm 3750: Channel *
1.333 markus 3751: channel_connect_by_listen_address(const char *listen_host,
3752: u_short listen_port, char *ctype, char *rname)
1.73 markus 3753: {
3754: int i;
1.99 markus 3755:
1.276 djm 3756: for (i = 0; i < num_permitted_opens; i++) {
1.336 millert 3757: if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
1.333 markus 3758: listen_port, 1)) {
1.354 markus 3759: if (permitted_opens[i].downstream)
3760: return permitted_opens[i].downstream;
1.99 markus 3761: return connect_to(
1.73 markus 3762: permitted_opens[i].host_to_connect,
1.276 djm 3763: permitted_opens[i].port_to_connect, ctype, rname);
3764: }
3765: }
1.74 markus 3766: error("WARNING: Server requests forwarding for unknown listen_port %d",
3767: listen_port);
1.276 djm 3768: return NULL;
1.73 markus 3769: }
3770:
1.336 millert 3771: Channel *
3772: channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3773: {
3774: int i;
3775:
3776: for (i = 0; i < num_permitted_opens; i++) {
3777: if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3778: return connect_to(
3779: permitted_opens[i].host_to_connect,
3780: permitted_opens[i].port_to_connect, ctype, rname);
3781: }
3782: }
3783: error("WARNING: Server requests forwarding for unknown path %.100s",
3784: path);
3785: return NULL;
3786: }
3787:
1.99 markus 3788: /* Check if connecting to that port is permitted and connect. */
1.276 djm 3789: Channel *
1.357 dtucker 3790: channel_connect_to_port(const char *host, u_short port, char *ctype,
3791: char *rname, int *reason, const char **errmsg)
1.99 markus 3792: {
1.257 dtucker 3793: int i, permit, permit_adm = 1;
1.99 markus 3794:
3795: permit = all_opens_permitted;
3796: if (!permit) {
3797: for (i = 0; i < num_permitted_opens; i++)
1.333 markus 3798: if (open_match(&permitted_opens[i], host, port)) {
1.99 markus 3799: permit = 1;
1.333 markus 3800: break;
3801: }
1.257 dtucker 3802: }
1.99 markus 3803:
1.257 dtucker 3804: if (num_adm_permitted_opens > 0) {
3805: permit_adm = 0;
3806: for (i = 0; i < num_adm_permitted_opens; i++)
1.333 markus 3807: if (open_match(&permitted_adm_opens[i], host, port)) {
1.257 dtucker 3808: permit_adm = 1;
1.333 markus 3809: break;
3810: }
1.99 markus 3811: }
1.257 dtucker 3812:
3813: if (!permit || !permit_adm) {
1.188 itojun 3814: logit("Received request to connect to host %.100s port %d, "
1.99 markus 3815: "but the request was denied.", host, port);
1.357 dtucker 3816: if (reason != NULL)
3817: *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
1.276 djm 3818: return NULL;
1.99 markus 3819: }
1.357 dtucker 3820: return connect_to_reason(host, port, ctype, rname, reason, errmsg);
1.336 millert 3821: }
3822:
3823: /* Check if connecting to that path is permitted and connect. */
3824: Channel *
3825: channel_connect_to_path(const char *path, char *ctype, char *rname)
3826: {
3827: int i, permit, permit_adm = 1;
3828:
3829: permit = all_opens_permitted;
3830: if (!permit) {
3831: for (i = 0; i < num_permitted_opens; i++)
3832: if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3833: permit = 1;
3834: break;
3835: }
3836: }
3837:
3838: if (num_adm_permitted_opens > 0) {
3839: permit_adm = 0;
3840: for (i = 0; i < num_adm_permitted_opens; i++)
3841: if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3842: permit_adm = 1;
3843: break;
3844: }
3845: }
3846:
3847: if (!permit || !permit_adm) {
3848: logit("Received request to connect to path %.100s, "
3849: "but the request was denied.", path);
3850: return NULL;
3851: }
3852: return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
1.204 djm 3853: }
3854:
3855: void
3856: channel_send_window_changes(void)
3857: {
1.209 avsm 3858: u_int i;
1.204 djm 3859: struct winsize ws;
3860:
3861: for (i = 0; i < channels_alloc; i++) {
1.213 deraadt 3862: if (channels[i] == NULL || !channels[i]->client_tty ||
1.204 djm 3863: channels[i]->type != SSH_CHANNEL_OPEN)
3864: continue;
3865: if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3866: continue;
3867: channel_request_start(i, "window-change", 0);
1.238 deraadt 3868: packet_put_int((u_int)ws.ws_col);
3869: packet_put_int((u_int)ws.ws_row);
3870: packet_put_int((u_int)ws.ws_xpixel);
3871: packet_put_int((u_int)ws.ws_ypixel);
1.204 djm 3872: packet_send();
3873: }
1.99 markus 3874: }
3875:
1.121 markus 3876: /* -- X11 forwarding */
1.1 deraadt 3877:
1.27 markus 3878: /*
3879: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 3880: * Returns 0 and a suitable display number for the DISPLAY variable
3881: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 3882: */
1.141 stevesk 3883: int
1.163 stevesk 3884: x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
1.222 djm 3885: int single_connection, u_int *display_numberp, int **chanids)
1.1 deraadt 3886: {
1.149 markus 3887: Channel *nc = NULL;
1.31 markus 3888: int display_number, sock;
3889: u_short port;
1.35 markus 3890: struct addrinfo hints, *ai, *aitop;
3891: char strport[NI_MAXSERV];
3892: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 3893:
1.224 markus 3894: if (chanids == NULL)
3895: return -1;
3896:
1.33 markus 3897: for (display_number = x11_display_offset;
1.148 deraadt 3898: display_number < MAX_DISPLAYS;
3899: display_number++) {
1.25 markus 3900: port = 6000 + display_number;
1.35 markus 3901: memset(&hints, 0, sizeof(hints));
3902: hints.ai_family = IPv4or6;
1.163 stevesk 3903: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 3904: hints.ai_socktype = SOCK_STREAM;
3905: snprintf(strport, sizeof strport, "%d", port);
3906: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1.271 dtucker 3907: error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
1.141 stevesk 3908: return -1;
1.25 markus 3909: }
1.35 markus 3910: for (ai = aitop; ai; ai = ai->ai_next) {
3911: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3912: continue;
1.300 dtucker 3913: sock = socket(ai->ai_family, ai->ai_socktype,
3914: ai->ai_protocol);
1.35 markus 3915: if (sock < 0) {
3916: error("socket: %.100s", strerror(errno));
1.203 markus 3917: freeaddrinfo(aitop);
1.141 stevesk 3918: return -1;
1.35 markus 3919: }
1.226 djm 3920: channel_set_reuseaddr(sock);
1.35 markus 3921: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.194 markus 3922: debug2("bind port %d: %.100s", port, strerror(errno));
1.35 markus 3923: close(sock);
1.183 itojun 3924:
1.35 markus 3925: for (n = 0; n < num_socks; n++) {
3926: close(socks[n]);
3927: }
3928: num_socks = 0;
3929: break;
3930: }
3931: socks[num_socks++] = sock;
3932: if (num_socks == NUM_SOCKS)
3933: break;
1.25 markus 3934: }
1.83 stevesk 3935: freeaddrinfo(aitop);
1.35 markus 3936: if (num_socks > 0)
3937: break;
1.25 markus 3938: }
3939: if (display_number >= MAX_DISPLAYS) {
3940: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 3941: return -1;
1.25 markus 3942: }
3943: /* Start listening for connections on the socket. */
1.35 markus 3944: for (n = 0; n < num_socks; n++) {
3945: sock = socks[n];
1.199 markus 3946: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
1.35 markus 3947: error("listen: %.100s", strerror(errno));
3948: close(sock);
1.141 stevesk 3949: return -1;
1.35 markus 3950: }
1.25 markus 3951: }
1.35 markus 3952:
3953: /* Allocate a channel for each socket. */
1.242 djm 3954: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 3955: for (n = 0; n < num_socks; n++) {
3956: sock = socks[n];
1.149 markus 3957: nc = channel_new("x11 listener",
1.51 markus 3958: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3959: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 3960: 0, "X11 inet listener", 1);
1.167 markus 3961: nc->single_connection = single_connection;
1.224 markus 3962: (*chanids)[n] = nc->self;
1.35 markus 3963: }
1.224 markus 3964: (*chanids)[n] = -1;
1.1 deraadt 3965:
1.141 stevesk 3966: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 3967: *display_numberp = display_number;
3968: return (0);
1.1 deraadt 3969: }
3970:
1.127 itojun 3971: static int
1.77 markus 3972: connect_local_xsocket(u_int dnr)
1.1 deraadt 3973: {
1.25 markus 3974: int sock;
3975: struct sockaddr_un addr;
3976:
1.147 stevesk 3977: sock = socket(AF_UNIX, SOCK_STREAM, 0);
3978: if (sock < 0)
3979: error("socket: %.100s", strerror(errno));
3980: memset(&addr, 0, sizeof(addr));
3981: addr.sun_family = AF_UNIX;
3982: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 3983: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 3984: return sock;
3985: close(sock);
1.25 markus 3986: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
3987: return -1;
1.1 deraadt 3988: }
3989:
1.51 markus 3990: int
3991: x11_connect_display(void)
1.1 deraadt 3992: {
1.248 deraadt 3993: u_int display_number;
1.25 markus 3994: const char *display;
1.51 markus 3995: char buf[1024], *cp;
1.35 markus 3996: struct addrinfo hints, *ai, *aitop;
3997: char strport[NI_MAXSERV];
1.248 deraadt 3998: int gaierr, sock = 0;
1.25 markus 3999:
4000: /* Try to open a socket for the local X server. */
4001: display = getenv("DISPLAY");
4002: if (!display) {
4003: error("DISPLAY not set.");
1.51 markus 4004: return -1;
1.25 markus 4005: }
1.27 markus 4006: /*
4007: * Now we decode the value of the DISPLAY variable and make a
4008: * connection to the real X server.
4009: */
4010:
4011: /*
4012: * Check if it is a unix domain socket. Unix domain displays are in
4013: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4014: */
1.25 markus 4015: if (strncmp(display, "unix:", 5) == 0 ||
4016: display[0] == ':') {
4017: /* Connect to the unix domain socket. */
1.248 deraadt 4018: if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
1.25 markus 4019: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 4020: display);
1.51 markus 4021: return -1;
1.25 markus 4022: }
4023: /* Create a socket. */
4024: sock = connect_local_xsocket(display_number);
4025: if (sock < 0)
1.51 markus 4026: return -1;
1.25 markus 4027:
4028: /* OK, we now have a connection to the display. */
1.51 markus 4029: return sock;
1.25 markus 4030: }
1.27 markus 4031: /*
4032: * Connect to an inet socket. The DISPLAY value is supposedly
4033: * hostname:d[.s], where hostname may also be numeric IP address.
4034: */
1.145 stevesk 4035: strlcpy(buf, display, sizeof(buf));
1.25 markus 4036: cp = strchr(buf, ':');
4037: if (!cp) {
4038: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 4039: return -1;
1.25 markus 4040: }
4041: *cp = 0;
1.27 markus 4042: /* buf now contains the host name. But first we parse the display number. */
1.248 deraadt 4043: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 4044: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 4045: display);
1.51 markus 4046: return -1;
1.25 markus 4047: }
1.35 markus 4048:
4049: /* Look up the host address */
4050: memset(&hints, 0, sizeof(hints));
4051: hints.ai_family = IPv4or6;
4052: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 4053: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 4054: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1.271 dtucker 4055: error("%.100s: unknown host. (%s)", buf,
4056: ssh_gai_strerror(gaierr));
1.51 markus 4057: return -1;
1.25 markus 4058: }
1.35 markus 4059: for (ai = aitop; ai; ai = ai->ai_next) {
4060: /* Create a socket. */
1.300 dtucker 4061: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.35 markus 4062: if (sock < 0) {
1.194 markus 4063: debug2("socket: %.100s", strerror(errno));
1.41 markus 4064: continue;
4065: }
4066: /* Connect it to the display. */
4067: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1.248 deraadt 4068: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 4069: 6000 + display_number, strerror(errno));
4070: close(sock);
4071: continue;
4072: }
4073: /* Success */
4074: break;
1.35 markus 4075: }
4076: freeaddrinfo(aitop);
4077: if (!ai) {
1.248 deraadt 4078: error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
1.35 markus 4079: strerror(errno));
1.51 markus 4080: return -1;
1.25 markus 4081: }
1.162 stevesk 4082: set_nodelay(sock);
1.51 markus 4083: return sock;
4084: }
4085:
4086: /*
1.27 markus 4087: * Requests forwarding of X11 connections, generates fake authentication
4088: * data, and enables authentication spoofing.
1.121 markus 4089: * This should be called in the client only.
1.27 markus 4090: */
1.49 markus 4091: void
1.215 djm 4092: x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
1.311 djm 4093: const char *proto, const char *data, int want_reply)
1.1 deraadt 4094: {
1.77 markus 4095: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 4096: u_int i, value;
1.25 markus 4097: char *new_data;
4098: int screen_number;
4099: const char *cp;
4100:
1.220 markus 4101: if (x11_saved_display == NULL)
4102: x11_saved_display = xstrdup(disp);
4103: else if (strcmp(disp, x11_saved_display) != 0) {
1.219 djm 4104: error("x11_request_forwarding_with_spoofing: different "
4105: "$DISPLAY already forwarded");
4106: return;
4107: }
4108:
1.266 djm 4109: cp = strchr(disp, ':');
1.25 markus 4110: if (cp)
4111: cp = strchr(cp, '.');
4112: if (cp)
1.245 deraadt 4113: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 4114: else
4115: screen_number = 0;
4116:
1.219 djm 4117: if (x11_saved_proto == NULL) {
4118: /* Save protocol name. */
4119: x11_saved_proto = xstrdup(proto);
1.353 natano 4120:
4121: /* Extract real authentication data. */
1.219 djm 4122: x11_saved_data = xmalloc(data_len);
4123: for (i = 0; i < data_len; i++) {
4124: if (sscanf(data + 2 * i, "%2x", &value) != 1)
4125: fatal("x11_request_forwarding: bad "
4126: "authentication data: %.100s", data);
4127: x11_saved_data[i] = value;
4128: }
4129: x11_saved_data_len = data_len;
1.353 natano 4130:
4131: /* Generate fake data of the same length. */
4132: x11_fake_data = xmalloc(data_len);
4133: arc4random_buf(x11_fake_data, data_len);
1.219 djm 4134: x11_fake_data_len = data_len;
1.25 markus 4135: }
4136:
4137: /* Convert the fake data into hex. */
1.219 djm 4138: new_data = tohex(x11_fake_data, data_len);
1.25 markus 4139:
4140: /* Send the request packet. */
1.358 djm 4141: channel_request_start(client_session_id, "x11-req", want_reply);
4142: packet_put_char(0); /* XXX bool single connection */
1.51 markus 4143: packet_put_cstring(proto);
4144: packet_put_cstring(new_data);
1.25 markus 4145: packet_put_int(screen_number);
4146: packet_send();
4147: packet_write_wait();
1.321 djm 4148: free(new_data);
1.1 deraadt 4149: }