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