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