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