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