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