Annotation of src/usr.bin/ssh/channels.c, Revision 1.437
1.437 ! djm 1: /* $OpenBSD: channels.c,v 1.436 2024/01/09 22:19:00 djm Exp $ */
1.1 deraadt 2: /*
1.26 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * This file contains functions for generic socket connection forwarding.
7: * There is also code for initiating connection forwarding for X11 connections,
8: * arbitrary tcp/ip connections, and the authentication agent connection.
1.49 markus 9: *
1.67 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
1.44 markus 16: * SSH2 support added by Markus Friedl.
1.156 markus 17: * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
1.67 deraadt 18: * Copyright (c) 1999 Dug Song. All rights reserved.
19: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20: *
21: * Redistribution and use in source and binary forms, with or without
22: * modification, are permitted provided that the following conditions
23: * are met:
24: * 1. Redistributions of source code must retain the above copyright
25: * notice, this list of conditions and the following disclaimer.
26: * 2. Redistributions in binary form must reproduce the above copyright
27: * notice, this list of conditions and the following disclaimer in the
28: * documentation and/or other materials provided with the distribution.
29: *
30: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.26 deraadt 40: */
1.1 deraadt 41:
1.265 deraadt 42: #include <sys/types.h>
1.336 millert 43: #include <sys/stat.h>
1.234 stevesk 44: #include <sys/ioctl.h>
1.235 stevesk 45: #include <sys/un.h>
1.251 stevesk 46: #include <sys/socket.h>
1.261 stevesk 47: #include <sys/time.h>
1.275 djm 48: #include <sys/queue.h>
1.251 stevesk 49:
50: #include <netinet/in.h>
51: #include <arpa/inet.h>
1.233 stevesk 52:
1.254 stevesk 53: #include <errno.h>
1.298 dtucker 54: #include <fcntl.h>
1.367 djm 55: #include <limits.h>
1.255 stevesk 56: #include <netdb.h>
1.411 djm 57: #include <poll.h>
1.367 djm 58: #include <stdarg.h>
1.341 millert 59: #include <stdint.h>
1.263 stevesk 60: #include <stdio.h>
1.262 stevesk 61: #include <stdlib.h>
1.260 stevesk 62: #include <string.h>
1.233 stevesk 63: #include <termios.h>
1.256 stevesk 64: #include <unistd.h>
1.1 deraadt 65:
1.265 deraadt 66: #include "xmalloc.h"
1.1 deraadt 67: #include "ssh.h"
1.82 markus 68: #include "ssh2.h"
1.354 markus 69: #include "ssherr.h"
1.367 djm 70: #include "sshbuf.h"
1.1 deraadt 71: #include "packet.h"
1.82 markus 72: #include "log.h"
73: #include "misc.h"
1.14 markus 74: #include "channels.h"
75: #include "compat.h"
1.82 markus 76: #include "canohost.h"
1.383 markus 77: #include "sshkey.h"
1.64 markus 78: #include "authfd.h"
1.147 stevesk 79: #include "pathnames.h"
1.381 djm 80: #include "match.h"
1.388 djm 81:
1.411 djm 82: /* XXX remove once we're satisfied there's no lurking bugs */
83: /* #define DEBUG_CHANNEL_POLL 1 */
84:
1.367 djm 85: /* -- agent forwarding */
86: #define NUM_SOCKS 10
1.12 markus 87:
1.367 djm 88: /* -- tcp forwarding */
89: /* special-case port number meaning allow any port */
90: #define FWD_PERMIT_ANY_PORT 0
1.1 deraadt 91:
1.367 djm 92: /* special-case wildcard meaning allow any host */
93: #define FWD_PERMIT_ANY_HOST "*"
1.1 deraadt 94:
1.367 djm 95: /* -- X11 forwarding */
96: /* Maximum number of fake X11 displays to try. */
97: #define MAX_DISPLAYS 1000
1.1 deraadt 98:
1.411 djm 99: /* Per-channel callback for pre/post IO actions */
1.410 djm 100: typedef void chan_fn(struct ssh *, Channel *c);
1.381 djm 101:
1.27 markus 102: /*
103: * Data structure for storing which hosts are permitted for forward requests.
104: * The local sides of any remote forwards are stored in this array to prevent
105: * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
106: * network (which might be behind a firewall).
107: */
1.336 millert 108: /* XXX: streamlocal wants a path instead of host:port */
109: /* Overload host_to_connect; we could just make this match Forward */
110: /* XXX - can we use listen_host instead of listen_path? */
1.381 djm 111: struct permission {
1.41 markus 112: char *host_to_connect; /* Connect to 'host'. */
1.336 millert 113: int port_to_connect; /* Connect to 'port'. */
1.333 markus 114: char *listen_host; /* Remote side should listen address. */
1.336 millert 115: char *listen_path; /* Remote side should listen path. */
116: int listen_port; /* Remote side should listen port. */
1.354 markus 117: Channel *downstream; /* Downstream mux*/
1.381 djm 118: };
119:
120: /*
121: * Stores the forwarding permission state for a single direction (local or
122: * remote).
123: */
124: struct permission_set {
125: /*
126: * List of all local permitted host/port pairs to allow for the
127: * user.
128: */
129: u_int num_permitted_user;
130: struct permission *permitted_user;
1.1 deraadt 131:
1.381 djm 132: /*
133: * List of all permitted host/port pairs to allow for the admin.
134: */
135: u_int num_permitted_admin;
136: struct permission *permitted_admin;
137:
138: /*
139: * If this is true, all opens/listens are permitted. This is the
140: * case on the server on which we have to trust the client anyway,
141: * and the user could do anything after logging in.
142: */
143: int all_permitted;
144: };
1.121 markus 145:
1.426 djm 146: /* Used to record timeouts per channel type */
147: struct ssh_channel_timeout {
148: char *type_pattern;
1.431 millert 149: int timeout_secs;
1.426 djm 150: };
151:
1.367 djm 152: /* Master structure for channels state */
153: struct ssh_channels {
154: /*
155: * Pointer to an array containing all allocated channels. The array
156: * is dynamically extended as needed.
157: */
158: Channel **channels;
1.257 dtucker 159:
1.367 djm 160: /*
161: * Size of the channel array. All slots of the array must always be
162: * initialized (at least the type field); unused slots set to NULL
163: */
164: u_int channels_alloc;
1.257 dtucker 165:
1.367 djm 166: /*
1.411 djm 167: * 'channel_pre*' are called just before IO to add any bits
168: * relevant to channels in the c->io_want bitmasks.
1.367 djm 169: *
170: * 'channel_post*': perform any appropriate operations for
1.411 djm 171: * channels which have c->io_ready events pending.
1.367 djm 172: */
173: chan_fn **channel_pre;
174: chan_fn **channel_post;
1.314 dtucker 175:
1.367 djm 176: /* -- tcp forwarding */
1.381 djm 177: struct permission_set local_perms;
178: struct permission_set remote_perms;
1.219 djm 179:
1.367 djm 180: /* -- X11 forwarding */
1.121 markus 181:
1.367 djm 182: /* Saved X11 local (client) display. */
183: char *x11_saved_display;
1.121 markus 184:
1.367 djm 185: /* Saved X11 authentication protocol name. */
186: char *x11_saved_proto;
1.347 djm 187:
1.367 djm 188: /* Saved X11 authentication data. This is the real data. */
189: char *x11_saved_data;
190: u_int x11_saved_data_len;
1.121 markus 191:
1.367 djm 192: /* Deadline after which all X11 connections are refused */
1.428 dtucker 193: time_t x11_refuse_time;
1.121 markus 194:
1.367 djm 195: /*
196: * Fake X11 authentication data. This is what the server will be
197: * sending us; we should replace any occurrences of this by the
198: * real data.
199: */
200: u_char *x11_fake_data;
201: u_int x11_fake_data_len;
1.121 markus 202:
1.367 djm 203: /* AF_UNSPEC or AF_INET or AF_INET6 */
204: int IPv4or6;
1.426 djm 205:
206: /* Channel timeouts by type */
207: struct ssh_channel_timeout *timeouts;
208: size_t ntimeouts;
1.436 djm 209: /* Global timeout for all OPEN channels */
210: int global_deadline;
211: time_t lastused;
1.367 djm 212: };
1.1 deraadt 213:
1.121 markus 214: /* helper */
1.367 djm 215: static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
1.354 markus 216: static const char *channel_rfwd_bind_host(const char *listen_host);
1.103 markus 217:
1.276 djm 218: /* non-blocking connect helpers */
219: static int connect_next(struct channel_connect *);
220: static void channel_connect_ctx_free(struct channel_connect *);
1.372 markus 221: static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *);
222: static int rdynamic_connect_finish(struct ssh *, Channel *);
1.276 djm 223:
1.367 djm 224: /* Setup helper */
225: static void channel_handler_init(struct ssh_channels *sc);
226:
1.121 markus 227: /* -- channel core */
1.41 markus 228:
1.367 djm 229: void
230: channel_init_channels(struct ssh *ssh)
231: {
232: struct ssh_channels *sc;
233:
1.387 djm 234: if ((sc = calloc(1, sizeof(*sc))) == NULL)
1.403 djm 235: fatal_f("allocation failed");
1.367 djm 236: sc->channels_alloc = 10;
237: sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
238: sc->IPv4or6 = AF_UNSPEC;
239: channel_handler_init(sc);
240:
241: ssh->chanctxt = sc;
242: }
243:
1.41 markus 244: Channel *
1.367 djm 245: channel_by_id(struct ssh *ssh, int id)
1.41 markus 246: {
247: Channel *c;
1.113 markus 248:
1.367 djm 249: if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
1.403 djm 250: logit_f("%d: bad id", id);
1.41 markus 251: return NULL;
252: }
1.367 djm 253: c = ssh->chanctxt->channels[id];
1.113 markus 254: if (c == NULL) {
1.403 djm 255: logit_f("%d: bad id: channel free", id);
1.41 markus 256: return NULL;
257: }
258: return c;
1.55 markus 259: }
260:
1.354 markus 261: Channel *
1.368 djm 262: channel_by_remote_id(struct ssh *ssh, u_int remote_id)
1.354 markus 263: {
264: Channel *c;
265: u_int i;
266:
1.367 djm 267: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
268: c = ssh->chanctxt->channels[i];
1.368 djm 269: if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
1.354 markus 270: return c;
271: }
272: return NULL;
273: }
274:
1.27 markus 275: /*
1.229 markus 276: * Returns the channel if it is allowed to receive protocol messages.
277: * Private channels, like listening sockets, may not receive messages.
278: */
279: Channel *
1.367 djm 280: channel_lookup(struct ssh *ssh, int id)
1.229 markus 281: {
282: Channel *c;
283:
1.367 djm 284: if ((c = channel_by_id(ssh, id)) == NULL)
285: return NULL;
1.229 markus 286:
1.237 deraadt 287: switch (c->type) {
1.229 markus 288: case SSH_CHANNEL_X11_OPEN:
289: case SSH_CHANNEL_LARVAL:
290: case SSH_CHANNEL_CONNECTING:
291: case SSH_CHANNEL_DYNAMIC:
1.372 markus 292: case SSH_CHANNEL_RDYNAMIC_OPEN:
293: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.229 markus 294: case SSH_CHANNEL_OPENING:
295: case SSH_CHANNEL_OPEN:
1.323 dtucker 296: case SSH_CHANNEL_ABANDONED:
1.354 markus 297: case SSH_CHANNEL_MUX_PROXY:
1.367 djm 298: return c;
1.229 markus 299: }
300: logit("Non-public channel %d, type %d.", id, c->type);
1.367 djm 301: return NULL;
1.229 markus 302: }
303:
304: /*
1.426 djm 305: * Add a timeout for open channels whose c->ctype (or c->xctype if it is set)
306: * match type_pattern.
307: */
308: void
309: channel_add_timeout(struct ssh *ssh, const char *type_pattern,
1.431 millert 310: int timeout_secs)
1.426 djm 311: {
312: struct ssh_channels *sc = ssh->chanctxt;
313:
1.436 djm 314: if (strcmp(type_pattern, "global") == 0) {
315: debug2_f("global channel timeout %d seconds", timeout_secs);
316: sc->global_deadline = timeout_secs;
317: return;
318: }
1.431 millert 319: debug2_f("channel type \"%s\" timeout %d seconds",
1.426 djm 320: type_pattern, timeout_secs);
321: sc->timeouts = xrecallocarray(sc->timeouts, sc->ntimeouts,
322: sc->ntimeouts + 1, sizeof(*sc->timeouts));
323: sc->timeouts[sc->ntimeouts].type_pattern = xstrdup(type_pattern);
324: sc->timeouts[sc->ntimeouts].timeout_secs = timeout_secs;
325: sc->ntimeouts++;
326: }
327:
328: /* Clears all previously-added channel timeouts */
329: void
330: channel_clear_timeouts(struct ssh *ssh)
331: {
332: struct ssh_channels *sc = ssh->chanctxt;
333: size_t i;
334:
335: debug3_f("clearing");
336: for (i = 0; i < sc->ntimeouts; i++)
337: free(sc->timeouts[i].type_pattern);
338: free(sc->timeouts);
339: sc->timeouts = NULL;
340: sc->ntimeouts = 0;
341: }
342:
1.431 millert 343: static int
1.426 djm 344: lookup_timeout(struct ssh *ssh, const char *type)
345: {
346: struct ssh_channels *sc = ssh->chanctxt;
347: size_t i;
348:
349: for (i = 0; i < sc->ntimeouts; i++) {
350: if (match_pattern(type, sc->timeouts[i].type_pattern))
351: return sc->timeouts[i].timeout_secs;
352: }
353:
354: return 0;
355: }
356:
357: /*
1.425 djm 358: * Sets "extended type" of a channel; used by session layer to add additional
359: * information about channel types (e.g. shell, login, subsystem) that can then
360: * be used to select timeouts.
1.426 djm 361: * Will reset c->inactive_deadline as a side-effect.
1.425 djm 362: */
363: void
364: channel_set_xtype(struct ssh *ssh, int id, const char *xctype)
365: {
366: Channel *c;
367:
368: if ((c = channel_by_id(ssh, id)) == NULL)
369: fatal_f("missing channel %d", id);
370: if (c->xctype != NULL)
371: free(c->xctype);
372: c->xctype = xstrdup(xctype);
1.426 djm 373: /* Type has changed, so look up inactivity deadline again */
374: c->inactive_deadline = lookup_timeout(ssh, c->xctype);
375: debug2_f("labeled channel %d as %s (inactive timeout %u)", id, xctype,
376: c->inactive_deadline);
1.425 djm 377: }
378:
379: /*
1.436 djm 380: * update "last used" time on a channel.
381: * NB. nothing else should update lastused except to clear it.
382: */
383: static void
384: channel_set_used_time(struct ssh *ssh, Channel *c)
385: {
386: ssh->chanctxt->lastused = monotime();
387: if (c != NULL)
388: c->lastused = ssh->chanctxt->lastused;
389: }
390:
391: /*
392: * Get the time at which a channel is due to time out for inactivity.
393: * Returns 0 if the channel is not due to time out ever.
394: */
395: static time_t
396: channel_get_expiry(struct ssh *ssh, Channel *c)
397: {
398: struct ssh_channels *sc = ssh->chanctxt;
399: time_t expiry = 0, channel_expiry;
400:
401: if (sc->lastused != 0 && sc->global_deadline != 0)
402: expiry = sc->lastused + sc->global_deadline;
403: if (c->lastused != 0 && c->inactive_deadline != 0) {
404: channel_expiry = c->lastused + c->inactive_deadline;
405: if (expiry == 0 || channel_expiry < expiry)
406: expiry = channel_expiry;
407: }
408: return expiry;
409: }
410:
411: /*
1.55 markus 412: * Register filedescriptors for a channel, used when allocating a channel or
1.52 markus 413: * when the channel consumer/producer is ready, e.g. shell exec'd
1.27 markus 414: */
1.127 itojun 415: static void
1.367 djm 416: channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
1.282 dtucker 417: int extusage, int nonblock, int is_tty)
1.1 deraadt 418: {
1.418 markus 419: int val;
420:
1.298 dtucker 421: if (rfd != -1)
1.430 dtucker 422: (void)fcntl(rfd, F_SETFD, FD_CLOEXEC);
1.298 dtucker 423: if (wfd != -1 && wfd != rfd)
1.430 dtucker 424: (void)fcntl(wfd, F_SETFD, FD_CLOEXEC);
1.298 dtucker 425: if (efd != -1 && efd != rfd && efd != wfd)
1.430 dtucker 426: (void)fcntl(efd, F_SETFD, FD_CLOEXEC);
1.55 markus 427:
1.52 markus 428: c->rfd = rfd;
429: c->wfd = wfd;
430: c->sock = (rfd == wfd) ? rfd : -1;
431: c->efd = efd;
432: c->extended_usage = extusage;
1.71 markus 433:
1.282 dtucker 434: if ((c->isatty = is_tty) != 0)
1.194 markus 435: debug2("channel %d: rfd %d isatty", c->self, c->rfd);
1.91 markus 436:
1.71 markus 437: /* enable nonblocking mode */
1.407 djm 438: c->restore_block = 0;
439: if (nonblock == CHANNEL_NONBLOCK_STDIO) {
440: /*
441: * Special handling for stdio file descriptors: do not set
442: * non-blocking mode if they are TTYs. Otherwise prepare to
443: * restore their blocking state on exit to avoid interfering
444: * with other programs that follow.
445: */
1.418 markus 446: if (rfd != -1 && !isatty(rfd) &&
447: (val = fcntl(rfd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.427 djm 448: c->restore_flags[0] = val;
1.407 djm 449: c->restore_block |= CHANNEL_RESTORE_RFD;
450: set_nonblock(rfd);
451: }
1.418 markus 452: if (wfd != -1 && !isatty(wfd) &&
453: (val = fcntl(wfd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.427 djm 454: c->restore_flags[1] = val;
1.407 djm 455: c->restore_block |= CHANNEL_RESTORE_WFD;
456: set_nonblock(wfd);
457: }
1.418 markus 458: if (efd != -1 && !isatty(efd) &&
459: (val = fcntl(efd, F_GETFL)) != -1 && !(val & O_NONBLOCK)) {
1.427 djm 460: c->restore_flags[2] = val;
1.407 djm 461: c->restore_block |= CHANNEL_RESTORE_EFD;
462: set_nonblock(efd);
463: }
464: } else if (nonblock) {
1.71 markus 465: if (rfd != -1)
466: set_nonblock(rfd);
467: if (wfd != -1)
468: set_nonblock(wfd);
469: if (efd != -1)
470: set_nonblock(efd);
471: }
1.436 djm 472: /* channel might be entering a larval state, so reset global timeout */
473: channel_set_used_time(ssh, NULL);
1.52 markus 474: }
475:
476: /*
1.419 djm 477: * Allocate a new channel object and set its type and socket.
1.52 markus 478: */
1.113 markus 479: Channel *
1.367 djm 480: channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
1.419 djm 481: u_int window, u_int maxpack, int extusage, const char *remote_name,
482: int nonblock)
1.52 markus 483: {
1.367 djm 484: struct ssh_channels *sc = ssh->chanctxt;
485: u_int i, found;
1.52 markus 486: Channel *c;
1.402 djm 487: int r;
1.25 markus 488:
489: /* Try to find a free slot where to put the new channel. */
1.367 djm 490: for (i = 0; i < sc->channels_alloc; i++) {
491: if (sc->channels[i] == NULL) {
1.25 markus 492: /* Found a free slot. */
1.367 djm 493: found = i;
1.25 markus 494: break;
495: }
1.367 djm 496: }
497: if (i >= sc->channels_alloc) {
498: /*
499: * There are no free slots. Take last+1 slot and expand
500: * the array.
501: */
502: found = sc->channels_alloc;
503: if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
1.403 djm 504: fatal_f("internal error: channels_alloc %d too big",
505: sc->channels_alloc);
1.367 djm 506: sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
507: sc->channels_alloc + 10, sizeof(*sc->channels));
508: sc->channels_alloc += 10;
509: debug2("channel: expanding %d", sc->channels_alloc);
1.25 markus 510: }
1.113 markus 511: /* Initialize and return new channel. */
1.367 djm 512: c = sc->channels[found] = xcalloc(1, sizeof(Channel));
513: if ((c->input = sshbuf_new()) == NULL ||
514: (c->output = sshbuf_new()) == NULL ||
515: (c->extended = sshbuf_new()) == NULL)
1.403 djm 516: fatal_f("sshbuf_new failed");
1.402 djm 517: if ((r = sshbuf_set_max_size(c->input, CHAN_INPUT_MAX)) != 0)
1.403 djm 518: fatal_fr(r, "sshbuf_set_max_size");
1.159 markus 519: c->ostate = CHAN_OUTPUT_OPEN;
520: c->istate = CHAN_INPUT_OPEN;
1.367 djm 521: channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
1.25 markus 522: c->self = found;
523: c->type = type;
1.41 markus 524: c->ctype = ctype;
1.51 markus 525: c->local_window = window;
526: c->local_window_max = window;
527: c->local_maxpacket = maxpack;
1.190 markus 528: c->remote_name = xstrdup(remote_name);
1.302 djm 529: c->ctl_chan = -1;
1.299 markus 530: c->delayed = 1; /* prevent call to channel_post handler */
1.426 djm 531: c->inactive_deadline = lookup_timeout(ssh, c->ctype);
1.275 djm 532: TAILQ_INIT(&c->status_confirms);
1.426 djm 533: debug("channel %d: new %s [%s] (inactive timeout: %u)",
534: found, c->ctype, remote_name, c->inactive_deadline);
1.113 markus 535: return c;
1.1 deraadt 536: }
1.52 markus 537:
1.132 markus 538: int
1.407 djm 539: channel_close_fd(struct ssh *ssh, Channel *c, int *fdp)
1.132 markus 540: {
1.407 djm 541: int ret, fd = *fdp;
1.132 markus 542:
1.407 djm 543: if (fd == -1)
544: return 0;
545:
1.427 djm 546: /* restore blocking */
547: if (*fdp == c->rfd &&
548: (c->restore_block & CHANNEL_RESTORE_RFD) != 0)
549: (void)fcntl(*fdp, F_SETFL, c->restore_flags[0]);
550: else if (*fdp == c->wfd &&
551: (c->restore_block & CHANNEL_RESTORE_WFD) != 0)
552: (void)fcntl(*fdp, F_SETFL, c->restore_flags[1]);
553: else if (*fdp == c->efd &&
554: (c->restore_block & CHANNEL_RESTORE_EFD) != 0)
555: (void)fcntl(*fdp, F_SETFL, c->restore_flags[2]);
1.407 djm 556:
1.411 djm 557: if (*fdp == c->rfd) {
558: c->io_want &= ~SSH_CHAN_IO_RFD;
559: c->io_ready &= ~SSH_CHAN_IO_RFD;
560: c->rfd = -1;
1.415 djm 561: c->pfds[0] = -1;
1.411 djm 562: }
563: if (*fdp == c->wfd) {
564: c->io_want &= ~SSH_CHAN_IO_WFD;
565: c->io_ready &= ~SSH_CHAN_IO_WFD;
566: c->wfd = -1;
1.415 djm 567: c->pfds[1] = -1;
1.411 djm 568: }
569: if (*fdp == c->efd) {
570: c->io_want &= ~SSH_CHAN_IO_EFD;
571: c->io_ready &= ~SSH_CHAN_IO_EFD;
572: c->efd = -1;
1.415 djm 573: c->pfds[2] = -1;
1.411 djm 574: }
575: if (*fdp == c->sock) {
576: c->io_want &= ~SSH_CHAN_IO_SOCK;
577: c->io_ready &= ~SSH_CHAN_IO_SOCK;
578: c->sock = -1;
1.415 djm 579: c->pfds[3] = -1;
1.411 djm 580: }
581:
1.407 djm 582: ret = close(fd);
1.411 djm 583: *fdp = -1; /* probably redundant */
1.132 markus 584: return ret;
585: }
586:
1.52 markus 587: /* Close all channel fd/socket. */
1.127 itojun 588: static void
1.367 djm 589: channel_close_fds(struct ssh *ssh, Channel *c)
590: {
1.379 tb 591: int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
592:
1.407 djm 593: channel_close_fd(ssh, c, &c->sock);
1.379 tb 594: if (rfd != sock)
1.407 djm 595: channel_close_fd(ssh, c, &c->rfd);
1.379 tb 596: if (wfd != sock && wfd != rfd)
1.407 djm 597: channel_close_fd(ssh, c, &c->wfd);
1.379 tb 598: if (efd != sock && efd != rfd && efd != wfd)
1.407 djm 599: channel_close_fd(ssh, c, &c->efd);
1.367 djm 600: }
601:
602: static void
1.381 djm 603: fwd_perm_clear(struct permission *perm)
1.52 markus 604: {
1.381 djm 605: free(perm->host_to_connect);
606: free(perm->listen_host);
607: free(perm->listen_path);
1.398 dtucker 608: memset(perm, 0, sizeof(*perm));
1.367 djm 609: }
610:
1.381 djm 611: /* Returns an printable name for the specified forwarding permission list */
612: static const char *
613: fwd_ident(int who, int where)
614: {
615: if (who == FORWARD_ADM) {
616: if (where == FORWARD_LOCAL)
617: return "admin local";
618: else if (where == FORWARD_REMOTE)
619: return "admin remote";
620: } else if (who == FORWARD_USER) {
621: if (where == FORWARD_LOCAL)
622: return "user local";
623: else if (where == FORWARD_REMOTE)
624: return "user remote";
625: }
626: fatal("Unknown forward permission list %d/%d", who, where);
627: }
628:
629: /* Returns the forwarding permission list for the specified direction */
630: static struct permission_set *
631: permission_set_get(struct ssh *ssh, int where)
632: {
633: struct ssh_channels *sc = ssh->chanctxt;
1.367 djm 634:
1.381 djm 635: switch (where) {
636: case FORWARD_LOCAL:
637: return &sc->local_perms;
638: break;
639: case FORWARD_REMOTE:
640: return &sc->remote_perms;
641: break;
642: default:
1.403 djm 643: fatal_f("invalid forwarding direction %d", where);
1.381 djm 644: }
645: }
646:
1.392 dtucker 647: /* Returns pointers to the specified forwarding list and its element count */
1.381 djm 648: static void
649: permission_set_get_array(struct ssh *ssh, int who, int where,
650: struct permission ***permpp, u_int **npermpp)
651: {
652: struct permission_set *pset = permission_set_get(ssh, where);
653:
654: switch (who) {
655: case FORWARD_USER:
656: *permpp = &pset->permitted_user;
657: *npermpp = &pset->num_permitted_user;
658: break;
659: case FORWARD_ADM:
660: *permpp = &pset->permitted_admin;
661: *npermpp = &pset->num_permitted_admin;
662: break;
663: default:
1.403 djm 664: fatal_f("invalid forwarding client %d", who);
1.381 djm 665: }
666: }
667:
1.409 jsg 668: /* Adds an entry to the specified forwarding list */
1.367 djm 669: static int
1.381 djm 670: permission_set_add(struct ssh *ssh, int who, int where,
1.367 djm 671: const char *host_to_connect, int port_to_connect,
672: const char *listen_host, const char *listen_path, int listen_port,
673: Channel *downstream)
674: {
1.381 djm 675: struct permission **permp;
676: u_int n, *npermp;
1.367 djm 677:
1.381 djm 678: permission_set_get_array(ssh, who, where, &permp, &npermp);
1.367 djm 679:
1.381 djm 680: if (*npermp >= INT_MAX)
1.403 djm 681: fatal_f("%s overflow", fwd_ident(who, where));
1.367 djm 682:
1.381 djm 683: *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp));
684: n = (*npermp)++;
1.367 djm 685: #define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
1.381 djm 686: (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect);
687: (*permp)[n].port_to_connect = port_to_connect;
688: (*permp)[n].listen_host = MAYBE_DUP(listen_host);
689: (*permp)[n].listen_path = MAYBE_DUP(listen_path);
690: (*permp)[n].listen_port = listen_port;
691: (*permp)[n].downstream = downstream;
1.367 djm 692: #undef MAYBE_DUP
693: return (int)n;
694: }
695:
696: static void
697: mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
698: {
699: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 700: struct permission_set *pset = &sc->local_perms;
701: struct permission *perm;
1.367 djm 702: int r;
703: u_int i;
704:
1.381 djm 705: for (i = 0; i < pset->num_permitted_user; i++) {
706: perm = &pset->permitted_user[i];
707: if (perm->downstream != c)
1.367 djm 708: continue;
709:
710: /* cancel on the server, since mux client is gone */
711: debug("channel %d: cleanup remote forward for %s:%u",
1.381 djm 712: c->self, perm->listen_host, perm->listen_port);
1.367 djm 713: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
714: (r = sshpkt_put_cstring(ssh,
715: "cancel-tcpip-forward")) != 0 ||
716: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
717: (r = sshpkt_put_cstring(ssh,
1.381 djm 718: channel_rfwd_bind_host(perm->listen_host))) != 0 ||
719: (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 ||
1.367 djm 720: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 721: fatal_fr(r, "channel %i", c->self);
1.367 djm 722: }
1.381 djm 723: fwd_perm_clear(perm); /* unregister */
1.367 djm 724: }
1.121 markus 725: }
726:
727: /* Free the channel and close its fd/socket. */
728: void
1.367 djm 729: channel_free(struct ssh *ssh, Channel *c)
1.121 markus 730: {
1.367 djm 731: struct ssh_channels *sc = ssh->chanctxt;
1.121 markus 732: char *s;
1.209 avsm 733: u_int i, n;
1.354 markus 734: Channel *other;
1.275 djm 735: struct channel_confirm *cc;
1.121 markus 736:
1.367 djm 737: for (n = 0, i = 0; i < sc->channels_alloc; i++) {
738: if ((other = sc->channels[i]) == NULL)
739: continue;
740: n++;
741: /* detach from mux client and prepare for closing */
742: if (c->type == SSH_CHANNEL_MUX_CLIENT &&
743: other->type == SSH_CHANNEL_MUX_PROXY &&
744: other->mux_ctx == c) {
745: other->mux_ctx = NULL;
746: other->type = SSH_CHANNEL_OPEN;
747: other->istate = CHAN_INPUT_CLOSED;
748: other->ostate = CHAN_OUTPUT_CLOSED;
1.354 markus 749: }
750: }
1.209 avsm 751: debug("channel %d: free: %s, nchannels %u", c->self,
1.121 markus 752: c->remote_name ? c->remote_name : "???", n);
753:
1.408 mbuhl 754: if (c->type == SSH_CHANNEL_MUX_CLIENT) {
1.367 djm 755: mux_remove_remote_forwardings(ssh, c);
1.408 mbuhl 756: free(c->mux_ctx);
757: c->mux_ctx = NULL;
758: } else if (c->type == SSH_CHANNEL_MUX_LISTENER) {
1.401 djm 759: free(c->mux_ctx);
760: c->mux_ctx = NULL;
761: }
1.354 markus 762:
1.384 markus 763: if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) {
764: s = channel_open_message(ssh);
765: debug3("channel %d: status: %s", c->self, s);
766: free(s);
767: }
1.121 markus 768:
1.367 djm 769: channel_close_fds(ssh, c);
770: sshbuf_free(c->input);
771: sshbuf_free(c->output);
772: sshbuf_free(c->extended);
773: c->input = c->output = c->extended = NULL;
1.321 djm 774: free(c->remote_name);
775: c->remote_name = NULL;
776: free(c->path);
777: c->path = NULL;
778: free(c->listening_addr);
779: c->listening_addr = NULL;
1.425 djm 780: free(c->xctype);
781: c->xctype = NULL;
1.275 djm 782: while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
783: if (cc->abandon_cb != NULL)
1.367 djm 784: cc->abandon_cb(ssh, c, cc->ctx);
1.275 djm 785: TAILQ_REMOVE(&c->status_confirms, cc, entry);
1.396 jsg 786: freezero(cc, sizeof(*cc));
1.275 djm 787: }
1.280 djm 788: if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
1.367 djm 789: c->filter_cleanup(ssh, c->self, c->filter_ctx);
790: sc->channels[c->self] = NULL;
1.396 jsg 791: freezero(c, sizeof(*c));
1.121 markus 792: }
793:
794: void
1.367 djm 795: channel_free_all(struct ssh *ssh)
1.121 markus 796: {
1.209 avsm 797: u_int i;
1.390 dtucker 798: struct ssh_channels *sc = ssh->chanctxt;
1.121 markus 799:
1.390 dtucker 800: for (i = 0; i < sc->channels_alloc; i++)
801: if (sc->channels[i] != NULL)
802: channel_free(ssh, sc->channels[i]);
803:
804: free(sc->channels);
805: sc->channels = NULL;
806: sc->channels_alloc = 0;
807:
808: free(sc->x11_saved_display);
809: sc->x11_saved_display = NULL;
810:
811: free(sc->x11_saved_proto);
812: sc->x11_saved_proto = NULL;
813:
814: free(sc->x11_saved_data);
815: sc->x11_saved_data = NULL;
816: sc->x11_saved_data_len = 0;
817:
818: free(sc->x11_fake_data);
819: sc->x11_fake_data = NULL;
820: sc->x11_fake_data_len = 0;
1.131 markus 821: }
822:
1.121 markus 823: /*
824: * Closes the sockets/fds of all channels. This is used to close extra file
825: * descriptors after a fork.
826: */
827: void
1.367 djm 828: channel_close_all(struct ssh *ssh)
1.121 markus 829: {
1.209 avsm 830: u_int i;
1.121 markus 831:
1.367 djm 832: for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
833: if (ssh->chanctxt->channels[i] != NULL)
834: channel_close_fds(ssh, ssh->chanctxt->channels[i]);
1.121 markus 835: }
836:
837: /*
1.131 markus 838: * Stop listening to channels.
839: */
840: void
1.367 djm 841: channel_stop_listening(struct ssh *ssh)
1.131 markus 842: {
1.209 avsm 843: u_int i;
1.131 markus 844: Channel *c;
845:
1.367 djm 846: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
847: c = ssh->chanctxt->channels[i];
1.131 markus 848: if (c != NULL) {
849: switch (c->type) {
850: case SSH_CHANNEL_AUTH_SOCKET:
851: case SSH_CHANNEL_PORT_LISTENER:
852: case SSH_CHANNEL_RPORT_LISTENER:
853: case SSH_CHANNEL_X11_LISTENER:
1.336 millert 854: case SSH_CHANNEL_UNIX_LISTENER:
855: case SSH_CHANNEL_RUNIX_LISTENER:
1.407 djm 856: channel_close_fd(ssh, c, &c->sock);
1.367 djm 857: channel_free(ssh, c);
1.131 markus 858: break;
859: }
860: }
861: }
862: }
863:
864: /*
1.121 markus 865: * Returns true if no channel has too much buffered data, and false if one or
866: * more channel is overfull.
867: */
868: int
1.367 djm 869: channel_not_very_much_buffered_data(struct ssh *ssh)
1.121 markus 870: {
871: u_int i;
1.367 djm 872: u_int maxsize = ssh_packet_get_maxsize(ssh);
1.121 markus 873: Channel *c;
874:
1.367 djm 875: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
876: c = ssh->chanctxt->channels[i];
877: if (c == NULL || c->type != SSH_CHANNEL_OPEN)
878: continue;
879: if (sshbuf_len(c->output) > maxsize) {
880: debug2("channel %d: big output buffer %zu > %u",
881: c->self, sshbuf_len(c->output), maxsize);
882: return 0;
1.121 markus 883: }
884: }
885: return 1;
886: }
887:
888: /* Returns true if any channel is still open. */
889: int
1.367 djm 890: channel_still_open(struct ssh *ssh)
1.121 markus 891: {
1.209 avsm 892: u_int i;
1.121 markus 893: Channel *c;
894:
1.367 djm 895: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
896: c = ssh->chanctxt->channels[i];
1.121 markus 897: if (c == NULL)
898: continue;
899: switch (c->type) {
900: case SSH_CHANNEL_X11_LISTENER:
901: case SSH_CHANNEL_PORT_LISTENER:
902: case SSH_CHANNEL_RPORT_LISTENER:
1.302 djm 903: case SSH_CHANNEL_MUX_LISTENER:
1.121 markus 904: case SSH_CHANNEL_CLOSED:
905: case SSH_CHANNEL_AUTH_SOCKET:
906: case SSH_CHANNEL_DYNAMIC:
1.372 markus 907: case SSH_CHANNEL_RDYNAMIC_OPEN:
1.121 markus 908: case SSH_CHANNEL_CONNECTING:
909: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 910: case SSH_CHANNEL_ABANDONED:
1.336 millert 911: case SSH_CHANNEL_UNIX_LISTENER:
912: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 913: continue;
914: case SSH_CHANNEL_LARVAL:
915: continue;
916: case SSH_CHANNEL_OPENING:
917: case SSH_CHANNEL_OPEN:
1.372 markus 918: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 919: case SSH_CHANNEL_X11_OPEN:
1.302 djm 920: case SSH_CHANNEL_MUX_CLIENT:
1.354 markus 921: case SSH_CHANNEL_MUX_PROXY:
1.121 markus 922: return 1;
923: default:
1.403 djm 924: fatal_f("bad channel type %d", c->type);
1.121 markus 925: /* NOTREACHED */
926: }
1.434 djm 927: }
928: return 0;
929: }
930:
931: /* Returns true if a channel with a TTY is open. */
932: int
933: channel_tty_open(struct ssh *ssh)
934: {
935: u_int i;
936: Channel *c;
937:
938: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
939: c = ssh->chanctxt->channels[i];
940: if (c == NULL || c->type != SSH_CHANNEL_OPEN)
941: continue;
942: if (c->client_tty)
943: return 1;
1.121 markus 944: }
945: return 0;
946: }
947:
948: /* Returns the id of an open channel suitable for keepaliving */
949: int
1.367 djm 950: channel_find_open(struct ssh *ssh)
1.121 markus 951: {
1.209 avsm 952: u_int i;
1.121 markus 953: Channel *c;
954:
1.367 djm 955: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
956: c = ssh->chanctxt->channels[i];
1.368 djm 957: if (c == NULL || !c->have_remote_id)
1.121 markus 958: continue;
959: switch (c->type) {
960: case SSH_CHANNEL_CLOSED:
961: case SSH_CHANNEL_DYNAMIC:
1.372 markus 962: case SSH_CHANNEL_RDYNAMIC_OPEN:
963: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 964: case SSH_CHANNEL_X11_LISTENER:
965: case SSH_CHANNEL_PORT_LISTENER:
966: case SSH_CHANNEL_RPORT_LISTENER:
1.302 djm 967: case SSH_CHANNEL_MUX_LISTENER:
968: case SSH_CHANNEL_MUX_CLIENT:
1.354 markus 969: case SSH_CHANNEL_MUX_PROXY:
1.121 markus 970: case SSH_CHANNEL_OPENING:
971: case SSH_CHANNEL_CONNECTING:
972: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 973: case SSH_CHANNEL_ABANDONED:
1.336 millert 974: case SSH_CHANNEL_UNIX_LISTENER:
975: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 976: continue;
977: case SSH_CHANNEL_LARVAL:
978: case SSH_CHANNEL_AUTH_SOCKET:
979: case SSH_CHANNEL_OPEN:
980: case SSH_CHANNEL_X11_OPEN:
981: return i;
982: default:
1.403 djm 983: fatal_f("bad channel type %d", c->type);
1.121 markus 984: /* NOTREACHED */
985: }
986: }
987: return -1;
988: }
989:
1.385 djm 990: /* Returns the state of the channel's extended usage flag */
991: const char *
992: channel_format_extended_usage(const Channel *c)
993: {
994: if (c->efd == -1)
995: return "closed";
996:
997: switch (c->extended_usage) {
998: case CHAN_EXTENDED_WRITE:
999: return "write";
1000: case CHAN_EXTENDED_READ:
1001: return "read";
1002: case CHAN_EXTENDED_IGNORE:
1003: return "ignore";
1004: default:
1005: return "UNKNOWN";
1006: }
1007: }
1008:
1.386 djm 1009: static char *
1010: channel_format_status(const Channel *c)
1011: {
1012: char *ret = NULL;
1013:
1.425 djm 1014: xasprintf(&ret, "t%d [%s] %s%u i%u/%zu o%u/%zu e[%s]/%zu "
1.411 djm 1015: "fd %d/%d/%d sock %d cc %d io 0x%02x/0x%02x",
1.425 djm 1016: c->type, c->xctype != NULL ? c->xctype : c->ctype,
1.386 djm 1017: c->have_remote_id ? "r" : "nr", c->remote_id,
1018: c->istate, sshbuf_len(c->input),
1019: c->ostate, sshbuf_len(c->output),
1020: channel_format_extended_usage(c), sshbuf_len(c->extended),
1.411 djm 1021: c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan,
1022: c->io_want, c->io_ready);
1.386 djm 1023: return ret;
1024: }
1025:
1.121 markus 1026: /*
1027: * Returns a message describing the currently open forwarded connections,
1028: * suitable for sending to the client. The message contains crlf pairs for
1029: * newlines.
1030: */
1031: char *
1.367 djm 1032: channel_open_message(struct ssh *ssh)
1.121 markus 1033: {
1.367 djm 1034: struct sshbuf *buf;
1.121 markus 1035: Channel *c;
1.209 avsm 1036: u_int i;
1.367 djm 1037: int r;
1.386 djm 1038: char *cp, *ret;
1.121 markus 1039:
1.367 djm 1040: if ((buf = sshbuf_new()) == NULL)
1.403 djm 1041: fatal_f("sshbuf_new");
1.367 djm 1042: if ((r = sshbuf_putf(buf,
1043: "The following connections are open:\r\n")) != 0)
1.403 djm 1044: fatal_fr(r, "sshbuf_putf");
1.367 djm 1045: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
1046: c = ssh->chanctxt->channels[i];
1.121 markus 1047: if (c == NULL)
1048: continue;
1049: switch (c->type) {
1050: case SSH_CHANNEL_X11_LISTENER:
1051: case SSH_CHANNEL_PORT_LISTENER:
1052: case SSH_CHANNEL_RPORT_LISTENER:
1053: case SSH_CHANNEL_CLOSED:
1054: case SSH_CHANNEL_AUTH_SOCKET:
1055: case SSH_CHANNEL_ZOMBIE:
1.323 dtucker 1056: case SSH_CHANNEL_ABANDONED:
1.302 djm 1057: case SSH_CHANNEL_MUX_LISTENER:
1.336 millert 1058: case SSH_CHANNEL_UNIX_LISTENER:
1059: case SSH_CHANNEL_RUNIX_LISTENER:
1.121 markus 1060: continue;
1061: case SSH_CHANNEL_LARVAL:
1062: case SSH_CHANNEL_OPENING:
1063: case SSH_CHANNEL_CONNECTING:
1064: case SSH_CHANNEL_DYNAMIC:
1.372 markus 1065: case SSH_CHANNEL_RDYNAMIC_OPEN:
1066: case SSH_CHANNEL_RDYNAMIC_FINISH:
1.121 markus 1067: case SSH_CHANNEL_OPEN:
1068: case SSH_CHANNEL_X11_OPEN:
1.354 markus 1069: case SSH_CHANNEL_MUX_PROXY:
1070: case SSH_CHANNEL_MUX_CLIENT:
1.386 djm 1071: cp = channel_format_status(c);
1072: if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n",
1073: c->self, c->remote_name, cp)) != 0) {
1074: free(cp);
1.403 djm 1075: fatal_fr(r, "sshbuf_putf");
1.386 djm 1076: }
1077: free(cp);
1.121 markus 1078: continue;
1079: default:
1.403 djm 1080: fatal_f("bad channel type %d", c->type);
1.121 markus 1081: /* NOTREACHED */
1082: }
1083: }
1.367 djm 1084: if ((ret = sshbuf_dup_string(buf)) == NULL)
1.403 djm 1085: fatal_f("sshbuf_dup_string");
1.367 djm 1086: sshbuf_free(buf);
1087: return ret;
1088: }
1089:
1090: static void
1091: open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
1092: {
1093: int r;
1094:
1095: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1096: (r = sshpkt_put_cstring(ssh, type)) != 0 ||
1097: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1098: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1099: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
1.403 djm 1100: fatal_r(r, "%s: channel %i: open", where, c->self);
1.367 djm 1101: }
1.121 markus 1102: }
1103:
1104: void
1.367 djm 1105: channel_send_open(struct ssh *ssh, int id)
1.121 markus 1106: {
1.367 djm 1107: Channel *c = channel_lookup(ssh, id);
1108: int r;
1.180 deraadt 1109:
1.121 markus 1110: if (c == NULL) {
1.188 itojun 1111: logit("channel_send_open: %d: bad id", id);
1.121 markus 1112: return;
1113: }
1.184 markus 1114: debug2("channel %d: send open", id);
1.367 djm 1115: open_preamble(ssh, __func__, c, c->ctype);
1116: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1117: fatal_fr(r, "channel %i", c->self);
1.121 markus 1118: }
1119:
1120: void
1.367 djm 1121: channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
1.121 markus 1122: {
1.367 djm 1123: Channel *c = channel_lookup(ssh, id);
1124: int r;
1.180 deraadt 1125:
1.121 markus 1126: if (c == NULL) {
1.403 djm 1127: logit_f("%d: unknown channel id", id);
1.121 markus 1128: return;
1129: }
1.368 djm 1130: if (!c->have_remote_id)
1.403 djm 1131: fatal_f("channel %d: no remote id", c->self);
1.368 djm 1132:
1.204 djm 1133: debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
1.367 djm 1134: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
1135: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1136: (r = sshpkt_put_cstring(ssh, service)) != 0 ||
1137: (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
1.403 djm 1138: fatal_fr(r, "channel %i", c->self);
1.367 djm 1139: }
1.121 markus 1140: }
1.241 deraadt 1141:
1.121 markus 1142: void
1.367 djm 1143: channel_register_status_confirm(struct ssh *ssh, int id,
1144: channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
1.275 djm 1145: {
1146: struct channel_confirm *cc;
1147: Channel *c;
1148:
1.367 djm 1149: if ((c = channel_lookup(ssh, id)) == NULL)
1.403 djm 1150: fatal_f("%d: bad id", id);
1.275 djm 1151:
1.327 djm 1152: cc = xcalloc(1, sizeof(*cc));
1.275 djm 1153: cc->cb = cb;
1154: cc->abandon_cb = abandon_cb;
1155: cc->ctx = ctx;
1156: TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
1157: }
1158:
1159: void
1.367 djm 1160: channel_register_open_confirm(struct ssh *ssh, int id,
1161: channel_open_fn *fn, void *ctx)
1.121 markus 1162: {
1.367 djm 1163: Channel *c = channel_lookup(ssh, id);
1.180 deraadt 1164:
1.121 markus 1165: if (c == NULL) {
1.403 djm 1166: logit_f("%d: bad id", id);
1.121 markus 1167: return;
1168: }
1.275 djm 1169: c->open_confirm = fn;
1170: c->open_confirm_ctx = ctx;
1.121 markus 1171: }
1.241 deraadt 1172:
1.121 markus 1173: void
1.367 djm 1174: channel_register_cleanup(struct ssh *ssh, int id,
1175: channel_callback_fn *fn, int do_close)
1.121 markus 1176: {
1.367 djm 1177: Channel *c = channel_by_id(ssh, id);
1.180 deraadt 1178:
1.121 markus 1179: if (c == NULL) {
1.403 djm 1180: logit_f("%d: bad id", id);
1.121 markus 1181: return;
1182: }
1.131 markus 1183: c->detach_user = fn;
1.225 djm 1184: c->detach_close = do_close;
1.121 markus 1185: }
1.241 deraadt 1186:
1.121 markus 1187: void
1.367 djm 1188: channel_cancel_cleanup(struct ssh *ssh, int id)
1.121 markus 1189: {
1.367 djm 1190: Channel *c = channel_by_id(ssh, id);
1.180 deraadt 1191:
1.121 markus 1192: if (c == NULL) {
1.403 djm 1193: logit_f("%d: bad id", id);
1.121 markus 1194: return;
1.52 markus 1195: }
1.131 markus 1196: c->detach_user = NULL;
1.225 djm 1197: c->detach_close = 0;
1.121 markus 1198: }
1.241 deraadt 1199:
1.121 markus 1200: void
1.367 djm 1201: channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
1.280 djm 1202: channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
1.121 markus 1203: {
1.367 djm 1204: Channel *c = channel_lookup(ssh, id);
1.180 deraadt 1205:
1.121 markus 1206: if (c == NULL) {
1.403 djm 1207: logit_f("%d: bad id", id);
1.121 markus 1208: return;
1.52 markus 1209: }
1.231 reyk 1210: c->input_filter = ifn;
1211: c->output_filter = ofn;
1.279 djm 1212: c->filter_ctx = ctx;
1.280 djm 1213: c->filter_cleanup = cfn;
1.52 markus 1214: }
1215:
1.49 markus 1216: void
1.367 djm 1217: channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
1.282 dtucker 1218: int extusage, int nonblock, int is_tty, u_int window_max)
1.1 deraadt 1219: {
1.367 djm 1220: Channel *c = channel_lookup(ssh, id);
1221: int r;
1.180 deraadt 1222:
1.121 markus 1223: if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
1224: fatal("channel_activate for non-larval channel %d.", id);
1.368 djm 1225: if (!c->have_remote_id)
1.403 djm 1226: fatal_f("channel %d: no remote id", c->self);
1.368 djm 1227:
1.367 djm 1228: channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
1.121 markus 1229: c->type = SSH_CHANNEL_OPEN;
1.436 djm 1230: channel_set_used_time(ssh, c);
1.168 markus 1231: c->local_window = c->local_window_max = window_max;
1.367 djm 1232:
1233: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1234: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1235: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1236: (r = sshpkt_send(ssh)) != 0)
1.403 djm 1237: fatal_fr(r, "channel %i", c->self);
1.1 deraadt 1238: }
1239:
1.127 itojun 1240: static void
1.410 djm 1241: channel_pre_listener(struct ssh *ssh, Channel *c)
1.41 markus 1242: {
1.410 djm 1243: c->io_want = SSH_CHAN_IO_SOCK_R;
1.41 markus 1244: }
1245:
1.127 itojun 1246: static void
1.410 djm 1247: channel_pre_connecting(struct ssh *ssh, Channel *c)
1.75 markus 1248: {
1249: debug3("channel %d: waiting for connection", c->self);
1.410 djm 1250: c->io_want = SSH_CHAN_IO_SOCK_W;
1.75 markus 1251: }
1252:
1.127 itojun 1253: static void
1.410 djm 1254: channel_pre_open(struct ssh *ssh, Channel *c)
1.41 markus 1255: {
1.410 djm 1256: c->io_want = 0;
1.44 markus 1257: if (c->istate == CHAN_INPUT_OPEN &&
1.358 djm 1258: c->remote_window > 0 &&
1.367 djm 1259: sshbuf_len(c->input) < c->remote_window &&
1260: sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
1.410 djm 1261: c->io_want |= SSH_CHAN_IO_RFD;
1.44 markus 1262: if (c->ostate == CHAN_OUTPUT_OPEN ||
1263: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.367 djm 1264: if (sshbuf_len(c->output) > 0) {
1.410 djm 1265: c->io_want |= SSH_CHAN_IO_WFD;
1.44 markus 1266: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.172 markus 1267: if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
1.367 djm 1268: debug2("channel %d: "
1269: "obuf_empty delayed efd %d/(%zu)", c->self,
1270: c->efd, sshbuf_len(c->extended));
1.172 markus 1271: else
1.367 djm 1272: chan_obuf_empty(ssh, c);
1.44 markus 1273: }
1274: }
1275: /** XXX check close conditions, too */
1.358 djm 1276: if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1277: c->ostate == CHAN_OUTPUT_CLOSED)) {
1.44 markus 1278: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1.367 djm 1279: sshbuf_len(c->extended) > 0)
1.410 djm 1280: c->io_want |= SSH_CHAN_IO_EFD_W;
1.306 djm 1281: else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1282: (c->extended_usage == CHAN_EXTENDED_READ ||
1283: c->extended_usage == CHAN_EXTENDED_IGNORE) &&
1.367 djm 1284: sshbuf_len(c->extended) < c->remote_window)
1.410 djm 1285: c->io_want |= SSH_CHAN_IO_EFD_R;
1.44 markus 1286: }
1.204 djm 1287: /* XXX: What about efd? races? */
1.44 markus 1288: }
1289:
1.41 markus 1290: /*
1291: * This is a special state for X11 authentication spoofing. An opened X11
1292: * connection (when authentication spoofing is being done) remains in this
1293: * state until the first packet has been completely read. The authentication
1294: * data in that packet is then substituted by the real data if it matches the
1295: * fake data, and the channel is put into normal mode.
1.51 markus 1296: * XXX All this happens at the client side.
1.121 markus 1297: * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41 markus 1298: */
1.127 itojun 1299: static int
1.367 djm 1300: x11_open_helper(struct ssh *ssh, struct sshbuf *b)
1.1 deraadt 1301: {
1.367 djm 1302: struct ssh_channels *sc = ssh->chanctxt;
1.77 markus 1303: u_char *ucp;
1304: u_int proto_len, data_len;
1.25 markus 1305:
1.347 djm 1306: /* Is this being called after the refusal deadline? */
1.367 djm 1307: if (sc->x11_refuse_time != 0 &&
1.428 dtucker 1308: monotime() >= sc->x11_refuse_time) {
1.347 djm 1309: verbose("Rejected X11 connection after ForwardX11Timeout "
1310: "expired");
1311: return -1;
1312: }
1313:
1.41 markus 1314: /* Check if the fixed size part of the packet is in buffer. */
1.367 djm 1315: if (sshbuf_len(b) < 12)
1.41 markus 1316: return 0;
1317:
1318: /* Parse the lengths of variable-length fields. */
1.367 djm 1319: ucp = sshbuf_mutable_ptr(b);
1.41 markus 1320: if (ucp[0] == 0x42) { /* Byte order MSB first. */
1321: proto_len = 256 * ucp[6] + ucp[7];
1322: data_len = 256 * ucp[8] + ucp[9];
1323: } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1324: proto_len = ucp[6] + 256 * ucp[7];
1325: data_len = ucp[8] + 256 * ucp[9];
1326: } else {
1.194 markus 1327: debug2("Initial X11 packet contains bad byte order byte: 0x%x",
1.148 deraadt 1328: ucp[0]);
1.41 markus 1329: return -1;
1330: }
1331:
1332: /* Check if the whole packet is in buffer. */
1.367 djm 1333: if (sshbuf_len(b) <
1.41 markus 1334: 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1335: return 0;
1336:
1337: /* Check if authentication protocol matches. */
1.367 djm 1338: if (proto_len != strlen(sc->x11_saved_proto) ||
1339: memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
1.194 markus 1340: debug2("X11 connection uses different authentication protocol.");
1.41 markus 1341: return -1;
1342: }
1343: /* Check if authentication data matches our fake data. */
1.367 djm 1344: if (data_len != sc->x11_fake_data_len ||
1.308 djm 1345: timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
1.367 djm 1346: sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
1.194 markus 1347: debug2("X11 auth data does not match fake data.");
1.41 markus 1348: return -1;
1349: }
1350: /* Check fake data length */
1.367 djm 1351: if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
1.41 markus 1352: error("X11 fake_data_len %d != saved_data_len %d",
1.367 djm 1353: sc->x11_fake_data_len, sc->x11_saved_data_len);
1.41 markus 1354: return -1;
1355: }
1356: /*
1357: * Received authentication protocol and data match
1358: * our fake data. Substitute the fake data with real
1359: * data.
1360: */
1361: memcpy(ucp + 12 + ((proto_len + 3) & ~3),
1.367 djm 1362: sc->x11_saved_data, sc->x11_saved_data_len);
1.41 markus 1363: return 1;
1364: }
1365:
1.423 djm 1366: void
1367: channel_force_close(struct ssh *ssh, Channel *c, int abandon)
1368: {
1369: debug3_f("channel %d: forcibly closing", c->self);
1370: if (c->istate == CHAN_INPUT_OPEN)
1371: chan_read_failed(ssh, c);
1372: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1373: sshbuf_reset(c->input);
1374: chan_ibuf_empty(ssh, c);
1375: }
1376: if (c->ostate == CHAN_OUTPUT_OPEN ||
1377: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1378: sshbuf_reset(c->output);
1379: chan_write_failed(ssh, c);
1380: }
1381: if (c->detach_user)
1382: c->detach_user(ssh, c->self, 1, NULL);
1383: if (c->efd != -1)
1384: channel_close_fd(ssh, c, &c->efd);
1385: if (abandon)
1386: c->type = SSH_CHANNEL_ABANDONED;
1.426 djm 1387: /* exempt from inactivity timeouts */
1388: c->inactive_deadline = 0;
1389: c->lastused = 0;
1.423 djm 1390: }
1391:
1.127 itojun 1392: static void
1.410 djm 1393: channel_pre_x11_open(struct ssh *ssh, Channel *c)
1.41 markus 1394: {
1.367 djm 1395: int ret = x11_open_helper(ssh, c->output);
1.133 markus 1396:
1397: /* c->force_drain = 1; */
1398:
1.41 markus 1399: if (ret == 1) {
1400: c->type = SSH_CHANNEL_OPEN;
1.436 djm 1401: channel_set_used_time(ssh, c);
1.410 djm 1402: channel_pre_open(ssh, c);
1.41 markus 1403: } else if (ret == -1) {
1.423 djm 1404: logit("X11 connection rejected because of wrong "
1405: "authentication.");
1.367 djm 1406: debug2("X11 rejected %d i%d/o%d",
1407: c->self, c->istate, c->ostate);
1.423 djm 1408: channel_force_close(ssh, c, 0);
1.41 markus 1409: }
1410: }
1.25 markus 1411:
1.302 djm 1412: static void
1.410 djm 1413: channel_pre_mux_client(struct ssh *ssh, Channel *c)
1.302 djm 1414: {
1.410 djm 1415: c->io_want = 0;
1.304 djm 1416: if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
1.367 djm 1417: sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
1.410 djm 1418: c->io_want |= SSH_CHAN_IO_RFD;
1.302 djm 1419: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1420: /* clear buffer immediately (discard any partial packet) */
1.367 djm 1421: sshbuf_reset(c->input);
1422: chan_ibuf_empty(ssh, c);
1.302 djm 1423: /* Start output drain. XXX just kill chan? */
1.367 djm 1424: chan_rcvd_oclose(ssh, c);
1.302 djm 1425: }
1426: if (c->ostate == CHAN_OUTPUT_OPEN ||
1427: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1.367 djm 1428: if (sshbuf_len(c->output) > 0)
1.410 djm 1429: c->io_want |= SSH_CHAN_IO_WFD;
1.302 djm 1430: else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1.367 djm 1431: chan_obuf_empty(ssh, c);
1.302 djm 1432: }
1433: }
1434:
1.104 markus 1435: /* try to decode a socks4 header */
1.127 itojun 1436: static int
1.372 markus 1437: channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
1.103 markus 1438: {
1.367 djm 1439: const u_char *p;
1440: char *host;
1.292 djm 1441: u_int len, have, i, found, need;
1.148 deraadt 1442: char username[256];
1.103 markus 1443: struct {
1444: u_int8_t version;
1445: u_int8_t command;
1446: u_int16_t dest_port;
1.104 markus 1447: struct in_addr dest_addr;
1.103 markus 1448: } s4_req, s4_rsp;
1.367 djm 1449: int r;
1.103 markus 1450:
1.104 markus 1451: debug2("channel %d: decode socks4", c->self);
1.109 markus 1452:
1.372 markus 1453: have = sshbuf_len(input);
1.109 markus 1454: len = sizeof(s4_req);
1455: if (have < len)
1456: return 0;
1.372 markus 1457: p = sshbuf_ptr(input);
1.292 djm 1458:
1459: need = 1;
1460: /* SOCKS4A uses an invalid IP address 0.0.0.x */
1461: if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1462: debug2("channel %d: socks4a request", c->self);
1463: /* ... and needs an extra string (the hostname) */
1464: need = 2;
1465: }
1466: /* Check for terminating NUL on the string(s) */
1.109 markus 1467: for (found = 0, i = len; i < have; i++) {
1468: if (p[i] == '\0') {
1.292 djm 1469: found++;
1470: if (found == need)
1471: break;
1.109 markus 1472: }
1473: if (i > 1024) {
1474: /* the peer is probably sending garbage */
1475: debug("channel %d: decode socks4: too long",
1476: c->self);
1477: return -1;
1478: }
1479: }
1.292 djm 1480: if (found < need)
1.109 markus 1481: return 0;
1.372 markus 1482: if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1483: (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1484: (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1485: (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
1.403 djm 1486: debug_r(r, "channels %d: decode socks4", c->self);
1.367 djm 1487: return -1;
1488: }
1.372 markus 1489: have = sshbuf_len(input);
1490: p = sshbuf_ptr(input);
1.364 djm 1491: if (memchr(p, '\0', have) == NULL) {
1.403 djm 1492: error("channel %d: decode socks4: unterminated user", c->self);
1.364 djm 1493: return -1;
1494: }
1.103 markus 1495: len = strlen(p);
1.106 markus 1496: debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.367 djm 1497: len++; /* trailing '\0' */
1.103 markus 1498: strlcpy(username, p, sizeof(username));
1.403 djm 1499: if ((r = sshbuf_consume(input, len)) != 0)
1500: fatal_fr(r, "channel %d: consume", c->self);
1.321 djm 1501: free(c->path);
1502: c->path = NULL;
1.292 djm 1503: if (need == 1) { /* SOCKS4: one string */
1504: host = inet_ntoa(s4_req.dest_addr);
1.293 djm 1505: c->path = xstrdup(host);
1.292 djm 1506: } else { /* SOCKS4A: two strings */
1.372 markus 1507: have = sshbuf_len(input);
1508: p = sshbuf_ptr(input);
1.364 djm 1509: if (memchr(p, '\0', have) == NULL) {
1510: error("channel %d: decode socks4a: host not nul "
1511: "terminated", c->self);
1512: return -1;
1513: }
1.292 djm 1514: len = strlen(p);
1515: debug2("channel %d: decode socks4a: host %s/%d",
1516: c->self, p, len);
1517: len++; /* trailing '\0' */
1.293 djm 1518: if (len > NI_MAXHOST) {
1.292 djm 1519: error("channel %d: hostname \"%.100s\" too long",
1520: c->self, p);
1521: return -1;
1522: }
1.293 djm 1523: c->path = xstrdup(p);
1.403 djm 1524: if ((r = sshbuf_consume(input, len)) != 0)
1525: fatal_fr(r, "channel %d: consume", c->self);
1.292 djm 1526: }
1.103 markus 1527: c->host_port = ntohs(s4_req.dest_port);
1.148 deraadt 1528:
1.194 markus 1529: debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1.292 djm 1530: c->self, c->path, c->host_port, s4_req.command);
1.103 markus 1531:
1.104 markus 1532: if (s4_req.command != 1) {
1.292 djm 1533: debug("channel %d: cannot handle: %s cn %d",
1534: c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
1.104 markus 1535: return -1;
1.103 markus 1536: }
1.104 markus 1537: s4_rsp.version = 0; /* vn: 0 for reply */
1538: s4_rsp.command = 90; /* cd: req granted */
1.103 markus 1539: s4_rsp.dest_port = 0; /* ignored */
1.104 markus 1540: s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1.403 djm 1541: if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0)
1542: fatal_fr(r, "channel %d: append reply", c->self);
1.104 markus 1543: return 1;
1544: }
1545:
1.193 markus 1546: /* try to decode a socks5 header */
1547: #define SSH_SOCKS5_AUTHDONE 0x1000
1548: #define SSH_SOCKS5_NOAUTH 0x00
1549: #define SSH_SOCKS5_IPV4 0x01
1550: #define SSH_SOCKS5_DOMAIN 0x03
1551: #define SSH_SOCKS5_IPV6 0x04
1552: #define SSH_SOCKS5_CONNECT 0x01
1553: #define SSH_SOCKS5_SUCCESS 0x00
1554:
1555: static int
1.372 markus 1556: channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
1.193 markus 1557: {
1.367 djm 1558: /* XXX use get/put_u8 instead of trusting struct padding */
1.193 markus 1559: struct {
1560: u_int8_t version;
1561: u_int8_t command;
1562: u_int8_t reserved;
1563: u_int8_t atyp;
1564: } s5_req, s5_rsp;
1565: u_int16_t dest_port;
1.324 djm 1566: char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1.367 djm 1567: const u_char *p;
1.252 djm 1568: u_int have, need, i, found, nmethods, addrlen, af;
1.367 djm 1569: int r;
1.193 markus 1570:
1571: debug2("channel %d: decode socks5", c->self);
1.372 markus 1572: p = sshbuf_ptr(input);
1.193 markus 1573: if (p[0] != 0x05)
1574: return -1;
1.372 markus 1575: have = sshbuf_len(input);
1.193 markus 1576: if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1577: /* format: ver | nmethods | methods */
1.198 djm 1578: if (have < 2)
1.193 markus 1579: return 0;
1580: nmethods = p[1];
1581: if (have < nmethods + 2)
1582: return 0;
1583: /* look for method: "NO AUTHENTICATION REQUIRED" */
1.268 stevesk 1584: for (found = 0, i = 2; i < nmethods + 2; i++) {
1.264 stevesk 1585: if (p[i] == SSH_SOCKS5_NOAUTH) {
1.193 markus 1586: found = 1;
1587: break;
1588: }
1589: }
1590: if (!found) {
1591: debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1592: c->self);
1593: return -1;
1594: }
1.403 djm 1595: if ((r = sshbuf_consume(input, nmethods + 2)) != 0)
1596: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1597: /* version, method */
1.372 markus 1598: if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1.403 djm 1599: (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0)
1600: fatal_fr(r, "channel %d: append reply", c->self);
1.193 markus 1601: c->flags |= SSH_SOCKS5_AUTHDONE;
1602: debug2("channel %d: socks5 auth done", c->self);
1603: return 0; /* need more */
1604: }
1605: debug2("channel %d: socks5 post auth", c->self);
1606: if (have < sizeof(s5_req)+1)
1607: return 0; /* need more */
1.247 deraadt 1608: memcpy(&s5_req, p, sizeof(s5_req));
1.193 markus 1609: if (s5_req.version != 0x05 ||
1610: s5_req.command != SSH_SOCKS5_CONNECT ||
1611: s5_req.reserved != 0x00) {
1.194 markus 1612: debug2("channel %d: only socks5 connect supported", c->self);
1.193 markus 1613: return -1;
1614: }
1.213 deraadt 1615: switch (s5_req.atyp){
1.193 markus 1616: case SSH_SOCKS5_IPV4:
1617: addrlen = 4;
1618: af = AF_INET;
1619: break;
1620: case SSH_SOCKS5_DOMAIN:
1621: addrlen = p[sizeof(s5_req)];
1622: af = -1;
1623: break;
1624: case SSH_SOCKS5_IPV6:
1625: addrlen = 16;
1626: af = AF_INET6;
1627: break;
1628: default:
1.194 markus 1629: debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1.193 markus 1630: return -1;
1631: }
1.252 djm 1632: need = sizeof(s5_req) + addrlen + 2;
1633: if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1634: need++;
1635: if (have < need)
1.193 markus 1636: return 0;
1.403 djm 1637: if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0)
1638: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1639: if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1640: /* host string length */
1.403 djm 1641: if ((r = sshbuf_consume(input, 1)) != 0)
1642: fatal_fr(r, "channel %d: consume", c->self);
1.367 djm 1643: }
1.372 markus 1644: if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1645: (r = sshbuf_get(input, &dest_port, 2)) != 0) {
1.403 djm 1646: debug_r(r, "channel %d: parse addr/port", c->self);
1.367 djm 1647: return -1;
1648: }
1.193 markus 1649: dest_addr[addrlen] = '\0';
1.321 djm 1650: free(c->path);
1651: c->path = NULL;
1.293 djm 1652: if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1.294 djm 1653: if (addrlen >= NI_MAXHOST) {
1.293 djm 1654: error("channel %d: dynamic request: socks5 hostname "
1655: "\"%.100s\" too long", c->self, dest_addr);
1656: return -1;
1657: }
1658: c->path = xstrdup(dest_addr);
1659: } else {
1660: if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1661: return -1;
1662: c->path = xstrdup(ntop);
1663: }
1.193 markus 1664: c->host_port = ntohs(dest_port);
1.198 djm 1665:
1.194 markus 1666: debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1.193 markus 1667: c->self, c->path, c->host_port, s5_req.command);
1668:
1669: s5_rsp.version = 0x05;
1670: s5_rsp.command = SSH_SOCKS5_SUCCESS;
1671: s5_rsp.reserved = 0; /* ignored */
1672: s5_rsp.atyp = SSH_SOCKS5_IPV4;
1673: dest_port = 0; /* ignored */
1674:
1.372 markus 1675: if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1676: (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1677: (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
1.403 djm 1678: fatal_fr(r, "channel %d: append reply", c->self);
1.193 markus 1679: return 1;
1.301 dtucker 1680: }
1681:
1682: Channel *
1.367 djm 1683: channel_connect_stdio_fwd(struct ssh *ssh,
1.432 dlg 1684: const char *host_to_connect, int port_to_connect,
1.407 djm 1685: int in, int out, int nonblock)
1.301 dtucker 1686: {
1687: Channel *c;
1688:
1.403 djm 1689: debug_f("%s:%d", host_to_connect, port_to_connect);
1.301 dtucker 1690:
1.367 djm 1691: c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
1.301 dtucker 1692: -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.407 djm 1693: 0, "stdio-forward", nonblock);
1.301 dtucker 1694:
1695: c->path = xstrdup(host_to_connect);
1696: c->host_port = port_to_connect;
1697: c->listening_port = 0;
1698: c->force_drain = 1;
1699:
1.367 djm 1700: channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1.432 dlg 1701: port_open_helper(ssh, c, port_to_connect == PORT_STREAMLOCAL ?
1702: "direct-streamlocal@openssh.com" : "direct-tcpip");
1.301 dtucker 1703:
1704: return c;
1.193 markus 1705: }
1706:
1.104 markus 1707: /* dynamic port forwarding */
1.127 itojun 1708: static void
1.410 djm 1709: channel_pre_dynamic(struct ssh *ssh, Channel *c)
1.104 markus 1710: {
1.367 djm 1711: const u_char *p;
1.217 djm 1712: u_int have;
1713: int ret;
1.103 markus 1714:
1.410 djm 1715: c->io_want = 0;
1.367 djm 1716: have = sshbuf_len(c->input);
1.104 markus 1717: debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.367 djm 1718: /* sshbuf_dump(c->input, stderr); */
1.104 markus 1719: /* check if the fixed size part of the packet is in buffer. */
1.193 markus 1720: if (have < 3) {
1.104 markus 1721: /* need more */
1.410 djm 1722: c->io_want |= SSH_CHAN_IO_RFD;
1.104 markus 1723: return;
1724: }
1725: /* try to guess the protocol */
1.367 djm 1726: p = sshbuf_ptr(c->input);
1727: /* XXX sshbuf_peek_u8? */
1.104 markus 1728: switch (p[0]) {
1729: case 0x04:
1.372 markus 1730: ret = channel_decode_socks4(c, c->input, c->output);
1.193 markus 1731: break;
1732: case 0x05:
1.372 markus 1733: ret = channel_decode_socks5(c, c->input, c->output);
1.104 markus 1734: break;
1735: default:
1736: ret = -1;
1737: break;
1738: }
1739: if (ret < 0) {
1.367 djm 1740: chan_mark_dead(ssh, c);
1.104 markus 1741: } else if (ret == 0) {
1742: debug2("channel %d: pre_dynamic: need more", c->self);
1743: /* need more */
1.410 djm 1744: c->io_want |= SSH_CHAN_IO_RFD;
1.372 markus 1745: if (sshbuf_len(c->output))
1.410 djm 1746: c->io_want |= SSH_CHAN_IO_WFD;
1.104 markus 1747: } else {
1748: /* switch to the next state */
1749: c->type = SSH_CHANNEL_OPENING;
1.367 djm 1750: port_open_helper(ssh, c, "direct-tcpip");
1.104 markus 1751: }
1.103 markus 1752: }
1753:
1.372 markus 1754: /* simulate read-error */
1755: static void
1756: rdynamic_close(struct ssh *ssh, Channel *c)
1757: {
1758: c->type = SSH_CHANNEL_OPEN;
1.423 djm 1759: channel_force_close(ssh, c, 0);
1.372 markus 1760: }
1761:
1762: /* reverse dynamic port forwarding */
1763: static void
1.411 djm 1764: channel_before_prepare_io_rdynamic(struct ssh *ssh, Channel *c)
1.372 markus 1765: {
1766: const u_char *p;
1767: u_int have, len;
1768: int r, ret;
1769:
1770: have = sshbuf_len(c->output);
1771: debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1772: /* sshbuf_dump(c->output, stderr); */
1773: /* EOF received */
1774: if (c->flags & CHAN_EOF_RCVD) {
1.403 djm 1775: if ((r = sshbuf_consume(c->output, have)) != 0)
1776: fatal_fr(r, "channel %d: consume", c->self);
1.372 markus 1777: rdynamic_close(ssh, c);
1778: return;
1779: }
1780: /* check if the fixed size part of the packet is in buffer. */
1781: if (have < 3)
1782: return;
1783: /* try to guess the protocol */
1784: p = sshbuf_ptr(c->output);
1785: switch (p[0]) {
1786: case 0x04:
1787: /* switch input/output for reverse forwarding */
1788: ret = channel_decode_socks4(c, c->output, c->input);
1789: break;
1790: case 0x05:
1791: ret = channel_decode_socks5(c, c->output, c->input);
1792: break;
1793: default:
1794: ret = -1;
1795: break;
1796: }
1797: if (ret < 0) {
1798: rdynamic_close(ssh, c);
1799: } else if (ret == 0) {
1800: debug2("channel %d: pre_rdynamic: need more", c->self);
1801: /* send socks request to peer */
1802: len = sshbuf_len(c->input);
1803: if (len > 0 && len < c->remote_window) {
1804: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1805: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1806: (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1807: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 1808: fatal_fr(r, "channel %i: rdynamic", c->self);
1.372 markus 1809: }
1.403 djm 1810: if ((r = sshbuf_consume(c->input, len)) != 0)
1811: fatal_fr(r, "channel %d: consume", c->self);
1.372 markus 1812: c->remote_window -= len;
1813: }
1814: } else if (rdynamic_connect_finish(ssh, c) < 0) {
1815: /* the connect failed */
1816: rdynamic_close(ssh, c);
1817: }
1818: }
1819:
1.41 markus 1820: /* This is our fake X11 server socket. */
1.127 itojun 1821: static void
1.410 djm 1822: channel_post_x11_listener(struct ssh *ssh, Channel *c)
1.41 markus 1823: {
1.113 markus 1824: Channel *nc;
1.285 djm 1825: struct sockaddr_storage addr;
1.367 djm 1826: int r, newsock, oerrno, remote_port;
1.41 markus 1827: socklen_t addrlen;
1.85 markus 1828: char buf[16384], *remote_ipaddr;
1.25 markus 1829:
1.410 djm 1830: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 1831: return;
1832:
1833: debug("X11 connection requested.");
1834: addrlen = sizeof(addr);
1835: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1836: if (c->single_connection) {
1837: oerrno = errno;
1838: debug2("single_connection: closing X11 listener.");
1.407 djm 1839: channel_close_fd(ssh, c, &c->sock);
1.367 djm 1840: chan_mark_dead(ssh, c);
1841: errno = oerrno;
1842: }
1.393 deraadt 1843: if (newsock == -1) {
1.367 djm 1844: if (errno != EINTR && errno != EWOULDBLOCK &&
1845: errno != ECONNABORTED)
1846: error("accept: %.100s", strerror(errno));
1847: if (errno == EMFILE || errno == ENFILE)
1848: c->notbefore = monotime() + 1;
1849: return;
1850: }
1851: set_nodelay(newsock);
1852: remote_ipaddr = get_peer_ipaddr(newsock);
1853: remote_port = get_peer_port(newsock);
1854: snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1855: remote_ipaddr, remote_port);
1856:
1.424 djm 1857: nc = channel_new(ssh, "x11-connection",
1.367 djm 1858: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1859: c->local_window_max, c->local_maxpacket, 0, buf, 1);
1860: open_preamble(ssh, __func__, nc, "x11");
1.378 djm 1861: if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1862: (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
1.403 djm 1863: fatal_fr(r, "channel %i: reply", c->self);
1.41 markus 1864: }
1.367 djm 1865: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1866: fatal_fr(r, "channel %i: send", c->self);
1.367 djm 1867: free(remote_ipaddr);
1.41 markus 1868: }
1.25 markus 1869:
1.127 itojun 1870: static void
1.367 djm 1871: port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
1.103 markus 1872: {
1.328 djm 1873: char *local_ipaddr = get_local_ipaddr(c->sock);
1.350 djm 1874: int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
1.103 markus 1875: char *remote_ipaddr = get_peer_ipaddr(c->sock);
1.216 markus 1876: int remote_port = get_peer_port(c->sock);
1.367 djm 1877: int r;
1.303 djm 1878:
1879: if (remote_port == -1) {
1880: /* Fake addr/port to appease peers that validate it (Tectia) */
1.321 djm 1881: free(remote_ipaddr);
1.303 djm 1882: remote_ipaddr = xstrdup("127.0.0.1");
1883: remote_port = 65535;
1884: }
1.103 markus 1885:
1.367 djm 1886: free(c->remote_name);
1887: xasprintf(&c->remote_name,
1.103 markus 1888: "%s: listening port %d for %.100s port %d, "
1.328 djm 1889: "connect from %.200s port %d to %.100s port %d",
1.103 markus 1890: rtype, c->listening_port, c->path, c->host_port,
1.328 djm 1891: remote_ipaddr, remote_port, local_ipaddr, local_port);
1.103 markus 1892:
1.367 djm 1893: open_preamble(ssh, __func__, c, rtype);
1.358 djm 1894: if (strcmp(rtype, "direct-tcpip") == 0) {
1895: /* target host, port */
1.367 djm 1896: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1.403 djm 1897: (r = sshpkt_put_u32(ssh, c->host_port)) != 0)
1898: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1899: } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1900: /* target path */
1.403 djm 1901: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1902: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1903: } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1904: /* listen path */
1.403 djm 1905: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1906: fatal_fr(r, "channel %i: reply", c->self);
1.103 markus 1907: } else {
1.358 djm 1908: /* listen address, port */
1.367 djm 1909: if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1.403 djm 1910: (r = sshpkt_put_u32(ssh, local_port)) != 0)
1911: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1912: }
1913: if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1914: /* reserved for future owner/mode info */
1.403 djm 1915: if ((r = sshpkt_put_cstring(ssh, "")) != 0)
1916: fatal_fr(r, "channel %i: reply", c->self);
1.358 djm 1917: } else {
1918: /* originator host and port */
1.367 djm 1919: if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1.403 djm 1920: (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0)
1921: fatal_fr(r, "channel %i: reply", c->self);
1.103 markus 1922: }
1.367 djm 1923: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 1924: fatal_fr(r, "channel %i: send", c->self);
1.321 djm 1925: free(remote_ipaddr);
1.328 djm 1926: free(local_ipaddr);
1.103 markus 1927: }
1928:
1.347 djm 1929: void
1.428 dtucker 1930: channel_set_x11_refuse_time(struct ssh *ssh, time_t refuse_time)
1.347 djm 1931: {
1.367 djm 1932: ssh->chanctxt->x11_refuse_time = refuse_time;
1.226 djm 1933: }
1934:
1.41 markus 1935: /*
1936: * This socket is listening for connections to a forwarded TCP/IP port.
1937: */
1.127 itojun 1938: static void
1.410 djm 1939: channel_post_port_listener(struct ssh *ssh, Channel *c)
1.41 markus 1940: {
1.103 markus 1941: Channel *nc;
1.285 djm 1942: struct sockaddr_storage addr;
1.113 markus 1943: int newsock, nextstate;
1.41 markus 1944: socklen_t addrlen;
1.103 markus 1945: char *rtype;
1.73 markus 1946:
1.410 djm 1947: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 1948: return;
1949:
1950: debug("Connection to port %d forwarding to %.100s port %d requested.",
1951: c->listening_port, c->path, c->host_port);
1.103 markus 1952:
1.367 djm 1953: if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1954: nextstate = SSH_CHANNEL_OPENING;
1955: rtype = "forwarded-tcpip";
1956: } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1957: nextstate = SSH_CHANNEL_OPENING;
1958: rtype = "forwarded-streamlocal@openssh.com";
1959: } else if (c->host_port == PORT_STREAMLOCAL) {
1960: nextstate = SSH_CHANNEL_OPENING;
1961: rtype = "direct-streamlocal@openssh.com";
1962: } else if (c->host_port == 0) {
1963: nextstate = SSH_CHANNEL_DYNAMIC;
1964: rtype = "dynamic-tcpip";
1965: } else {
1966: nextstate = SSH_CHANNEL_OPENING;
1967: rtype = "direct-tcpip";
1968: }
1.103 markus 1969:
1.367 djm 1970: addrlen = sizeof(addr);
1971: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.393 deraadt 1972: if (newsock == -1) {
1.367 djm 1973: if (errno != EINTR && errno != EWOULDBLOCK &&
1974: errno != ECONNABORTED)
1975: error("accept: %.100s", strerror(errno));
1976: if (errno == EMFILE || errno == ENFILE)
1977: c->notbefore = monotime() + 1;
1978: return;
1.41 markus 1979: }
1.367 djm 1980: if (c->host_port != PORT_STREAMLOCAL)
1981: set_nodelay(newsock);
1982: nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1983: c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1984: nc->listening_port = c->listening_port;
1985: nc->host_port = c->host_port;
1986: if (c->path != NULL)
1987: nc->path = xstrdup(c->path);
1988:
1989: if (nextstate != SSH_CHANNEL_DYNAMIC)
1990: port_open_helper(ssh, nc, rtype);
1.41 markus 1991: }
1.25 markus 1992:
1.41 markus 1993: /*
1994: * This is the authentication agent socket listening for connections from
1995: * clients.
1996: */
1.127 itojun 1997: static void
1.410 djm 1998: channel_post_auth_listener(struct ssh *ssh, Channel *c)
1.41 markus 1999: {
1.113 markus 2000: Channel *nc;
1.367 djm 2001: int r, newsock;
1.285 djm 2002: struct sockaddr_storage addr;
1.41 markus 2003: socklen_t addrlen;
1.25 markus 2004:
1.410 djm 2005: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.367 djm 2006: return;
2007:
2008: addrlen = sizeof(addr);
2009: newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1.393 deraadt 2010: if (newsock == -1) {
1.367 djm 2011: error("accept from auth socket: %.100s", strerror(errno));
2012: if (errno == EMFILE || errno == ENFILE)
2013: c->notbefore = monotime() + 1;
2014: return;
1.41 markus 2015: }
1.424 djm 2016: nc = channel_new(ssh, "agent-connection",
1.367 djm 2017: SSH_CHANNEL_OPENING, newsock, newsock, -1,
2018: c->local_window_max, c->local_maxpacket,
2019: 0, "accepted auth socket", 1);
2020: open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
2021: if ((r = sshpkt_send(ssh)) != 0)
1.403 djm 2022: fatal_fr(r, "channel %i", c->self);
1.41 markus 2023: }
1.25 markus 2024:
1.127 itojun 2025: static void
1.410 djm 2026: channel_post_connecting(struct ssh *ssh, Channel *c)
1.75 markus 2027: {
1.372 markus 2028: int err = 0, sock, isopen, r;
1.129 stevesk 2029: socklen_t sz = sizeof(err);
1.114 markus 2030:
1.410 djm 2031: if ((c->io_ready & SSH_CHAN_IO_SOCK_W) == 0)
1.367 djm 2032: return;
1.368 djm 2033: if (!c->have_remote_id)
1.403 djm 2034: fatal_f("channel %d: no remote id", c->self);
1.372 markus 2035: /* for rdynamic the OPEN_CONFIRMATION has been sent already */
2036: isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
1.429 djm 2037:
1.393 deraadt 2038: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) == -1) {
1.367 djm 2039: err = errno;
2040: error("getsockopt SO_ERROR failed");
2041: }
1.429 djm 2042:
1.367 djm 2043: if (err == 0) {
1.429 djm 2044: /* Non-blocking connection completed */
1.367 djm 2045: debug("channel %d: connected to %s port %d",
2046: c->self, c->connect_ctx.host, c->connect_ctx.port);
2047: channel_connect_ctx_free(&c->connect_ctx);
2048: c->type = SSH_CHANNEL_OPEN;
1.436 djm 2049: channel_set_used_time(ssh, c);
1.372 markus 2050: if (isopen) {
2051: /* no message necessary */
2052: } else {
2053: if ((r = sshpkt_start(ssh,
2054: SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
2055: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2056: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
2057: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1.403 djm 2058: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
2059: (r = sshpkt_send(ssh)) != 0)
2060: fatal_fr(r, "channel %i open confirm", c->self);
1.367 djm 2061: }
1.429 djm 2062: return;
2063: }
2064: if (err == EINTR || err == EAGAIN || err == EINPROGRESS)
2065: return;
2066:
2067: /* Non-blocking connection failed */
2068: debug("channel %d: connection failed: %s", c->self, strerror(err));
2069:
2070: /* Try next address, if any */
2071: if ((sock = connect_next(&c->connect_ctx)) == -1) {
2072: /* Exhausted all addresses for this destination */
1.367 djm 2073: error("connect_to %.100s port %d: failed.",
2074: c->connect_ctx.host, c->connect_ctx.port);
2075: channel_connect_ctx_free(&c->connect_ctx);
1.372 markus 2076: if (isopen) {
2077: rdynamic_close(ssh, c);
2078: } else {
2079: if ((r = sshpkt_start(ssh,
2080: SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
2081: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1.378 djm 2082: (r = sshpkt_put_u32(ssh,
2083: SSH2_OPEN_CONNECT_FAILED)) != 0 ||
2084: (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1.403 djm 2085: (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2086: (r = sshpkt_send(ssh)) != 0)
2087: fatal_fr(r, "channel %i: failure", c->self);
1.372 markus 2088: chan_mark_dead(ssh, c);
1.75 markus 2089: }
2090: }
1.429 djm 2091:
2092: /* New non-blocking connection in progress */
2093: close(c->sock);
2094: c->sock = c->rfd = c->wfd = sock;
1.75 markus 2095: }
2096:
1.127 itojun 2097: static int
1.410 djm 2098: channel_handle_rfd(struct ssh *ssh, Channel *c)
1.41 markus 2099: {
1.214 markus 2100: char buf[CHAN_RBUF];
1.367 djm 2101: ssize_t len;
2102: int r;
1.426 djm 2103: size_t nr = 0, have, avail, maxlen = CHANNEL_MAX_READ;
1.25 markus 2104:
1.410 djm 2105: if ((c->io_ready & SSH_CHAN_IO_RFD) == 0)
1.412 djm 2106: return 1; /* Shouldn't happen */
2107: if ((avail = sshbuf_avail(c->input)) == 0)
2108: return 1; /* Shouldn't happen */
2109:
2110: /*
2111: * For "simple" channels (i.e. not datagram or filtered), we can
2112: * read directly to the channel buffer.
2113: */
2114: if (c->input_filter == NULL && !c->datagram) {
2115: /* Only OPEN channels have valid rwin */
2116: if (c->type == SSH_CHANNEL_OPEN) {
2117: if ((have = sshbuf_len(c->input)) >= c->remote_window)
2118: return 1; /* shouldn't happen */
2119: if (maxlen > c->remote_window - have)
2120: maxlen = c->remote_window - have;
2121: }
2122: if (maxlen > avail)
2123: maxlen = avail;
1.426 djm 2124: if ((r = sshbuf_read(c->rfd, c->input, maxlen, &nr)) != 0) {
1.413 djm 2125: if (errno == EINTR || errno == EAGAIN)
2126: return 1;
1.412 djm 2127: debug2("channel %d: read failed rfd %d maxlen %zu: %s",
2128: c->self, c->rfd, maxlen, ssh_err(r));
2129: goto rfail;
2130: }
1.426 djm 2131: if (nr != 0)
1.436 djm 2132: channel_set_used_time(ssh, c);
1.367 djm 2133: return 1;
1.412 djm 2134: }
1.367 djm 2135:
2136: len = read(c->rfd, buf, sizeof(buf));
1.393 deraadt 2137: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2138: return 1;
2139: if (len <= 0) {
1.412 djm 2140: debug2("channel %d: read<=0 rfd %d len %zd: %s",
2141: c->self, c->rfd, len,
2142: len == 0 ? "closed" : strerror(errno));
2143: rfail:
1.367 djm 2144: if (c->type != SSH_CHANNEL_OPEN) {
2145: debug2("channel %d: not open", c->self);
2146: chan_mark_dead(ssh, c);
1.41 markus 2147: return -1;
1.65 markus 2148: } else {
1.367 djm 2149: chan_read_failed(ssh, c);
1.65 markus 2150: }
1.367 djm 2151: return -1;
2152: }
1.436 djm 2153: channel_set_used_time(ssh, c);
1.367 djm 2154: if (c->input_filter != NULL) {
2155: if (c->input_filter(ssh, c, buf, len) == -1) {
2156: debug2("channel %d: filter stops", c->self);
2157: chan_read_failed(ssh, c);
2158: }
2159: } else if (c->datagram) {
2160: if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1.403 djm 2161: fatal_fr(r, "channel %i: put datagram", c->self);
1.412 djm 2162: }
1.41 markus 2163: return 1;
2164: }
1.241 deraadt 2165:
1.127 itojun 2166: static int
1.410 djm 2167: channel_handle_wfd(struct ssh *ssh, Channel *c)
1.41 markus 2168: {
1.95 markus 2169: struct termios tio;
1.367 djm 2170: u_char *data = NULL, *buf; /* XXX const; need filter API change */
2171: size_t dlen, olen = 0;
2172: int r, len;
2173:
1.410 djm 2174: if ((c->io_ready & SSH_CHAN_IO_WFD) == 0)
2175: return 1;
2176: if (sshbuf_len(c->output) == 0)
1.367 djm 2177: return 1;
1.25 markus 2178:
1.41 markus 2179: /* Send buffered output data to the socket. */
1.367 djm 2180: olen = sshbuf_len(c->output);
2181: if (c->output_filter != NULL) {
2182: if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
2183: debug2("channel %d: filter stops", c->self);
2184: if (c->type != SSH_CHANNEL_OPEN)
2185: chan_mark_dead(ssh, c);
2186: else
2187: chan_write_failed(ssh, c);
2188: return -1;
1.231 reyk 2189: }
1.367 djm 2190: } else if (c->datagram) {
2191: if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
1.403 djm 2192: fatal_fr(r, "channel %i: get datagram", c->self);
1.369 djm 2193: buf = data;
1.367 djm 2194: } else {
2195: buf = data = sshbuf_mutable_ptr(c->output);
2196: dlen = sshbuf_len(c->output);
2197: }
1.231 reyk 2198:
1.367 djm 2199: if (c->datagram) {
2200: /* ignore truncated writes, datagrams might get lost */
1.374 djm 2201: len = write(c->wfd, buf, dlen);
1.367 djm 2202: free(data);
1.393 deraadt 2203: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.53 markus 2204: return 1;
1.367 djm 2205: if (len <= 0)
2206: goto write_fail;
2207: goto out;
2208: }
2209:
2210: len = write(c->wfd, buf, dlen);
1.393 deraadt 2211: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2212: return 1;
2213: if (len <= 0) {
2214: write_fail:
2215: if (c->type != SSH_CHANNEL_OPEN) {
2216: debug2("channel %d: not open", c->self);
2217: chan_mark_dead(ssh, c);
1.41 markus 2218: return -1;
1.367 djm 2219: } else {
2220: chan_write_failed(ssh, c);
1.91 markus 2221: }
1.367 djm 2222: return -1;
2223: }
1.436 djm 2224: channel_set_used_time(ssh, c);
1.367 djm 2225: if (c->isatty && dlen >= 1 && buf[0] != '\r') {
2226: if (tcgetattr(c->wfd, &tio) == 0 &&
2227: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
2228: /*
2229: * Simulate echo to reduce the impact of
2230: * traffic analysis. We need to match the
2231: * size of a SSH2_MSG_CHANNEL_DATA message
2232: * (4 byte channel id + buf)
2233: */
2234: if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2235: (r = sshpkt_send(ssh)) != 0)
1.403 djm 2236: fatal_fr(r, "channel %i: ignore", c->self);
1.25 markus 2237: }
1.367 djm 2238: }
1.403 djm 2239: if ((r = sshbuf_consume(c->output, len)) != 0)
2240: fatal_fr(r, "channel %i: consume", c->self);
1.309 djm 2241: out:
1.367 djm 2242: c->local_consumed += olen - sshbuf_len(c->output);
2243:
2244: return 1;
2245: }
2246:
2247: static int
1.410 djm 2248: channel_handle_efd_write(struct ssh *ssh, Channel *c)
1.367 djm 2249: {
2250: int r;
2251: ssize_t len;
2252:
1.410 djm 2253: if ((c->io_ready & SSH_CHAN_IO_EFD_W) == 0)
2254: return 1;
2255: if (sshbuf_len(c->extended) == 0)
1.367 djm 2256: return 1;
2257:
2258: len = write(c->efd, sshbuf_ptr(c->extended),
2259: sshbuf_len(c->extended));
2260: debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
1.393 deraadt 2261: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2262: return 1;
2263: if (len <= 0) {
2264: debug2("channel %d: closing write-efd %d", c->self, c->efd);
1.407 djm 2265: channel_close_fd(ssh, c, &c->efd);
1.367 djm 2266: } else {
1.403 djm 2267: if ((r = sshbuf_consume(c->extended, len)) != 0)
2268: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2269: c->local_consumed += len;
1.436 djm 2270: channel_set_used_time(ssh, c);
1.367 djm 2271: }
1.44 markus 2272: return 1;
2273: }
1.241 deraadt 2274:
1.127 itojun 2275: static int
1.410 djm 2276: channel_handle_efd_read(struct ssh *ssh, Channel *c)
1.44 markus 2277: {
1.214 markus 2278: char buf[CHAN_RBUF];
1.367 djm 2279: int r;
2280: ssize_t len;
2281:
1.410 djm 2282: if ((c->io_ready & SSH_CHAN_IO_EFD_R) == 0)
1.367 djm 2283: return 1;
1.44 markus 2284:
1.367 djm 2285: len = read(c->efd, buf, sizeof(buf));
2286: debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
1.393 deraadt 2287: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2288: return 1;
2289: if (len <= 0) {
1.403 djm 2290: debug2("channel %d: closing read-efd %d", c->self, c->efd);
1.407 djm 2291: channel_close_fd(ssh, c, &c->efd);
1.426 djm 2292: return 1;
2293: }
1.436 djm 2294: channel_set_used_time(ssh, c);
1.426 djm 2295: if (c->extended_usage == CHAN_EXTENDED_IGNORE)
1.403 djm 2296: debug3("channel %d: discard efd", c->self);
2297: else if ((r = sshbuf_put(c->extended, buf, len)) != 0)
2298: fatal_fr(r, "channel %i: append", c->self);
1.44 markus 2299: return 1;
2300: }
1.241 deraadt 2301:
1.127 itojun 2302: static int
1.410 djm 2303: channel_handle_efd(struct ssh *ssh, Channel *c)
1.44 markus 2304: {
1.367 djm 2305: if (c->efd == -1)
2306: return 1;
2307:
2308: /** XXX handle drain efd, too */
2309:
2310: if (c->extended_usage == CHAN_EXTENDED_WRITE)
1.410 djm 2311: return channel_handle_efd_write(ssh, c);
1.367 djm 2312: else if (c->extended_usage == CHAN_EXTENDED_READ ||
2313: c->extended_usage == CHAN_EXTENDED_IGNORE)
1.410 djm 2314: return channel_handle_efd_read(ssh, c);
1.367 djm 2315:
2316: return 1;
2317: }
2318:
2319: static int
2320: channel_check_window(struct ssh *ssh, Channel *c)
2321: {
2322: int r;
2323:
1.104 markus 2324: if (c->type == SSH_CHANNEL_OPEN &&
2325: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.270 dtucker 2326: ((c->local_window_max - c->local_window >
1.269 markus 2327: c->local_maxpacket*3) ||
2328: c->local_window < c->local_window_max/2) &&
1.44 markus 2329: c->local_consumed > 0) {
1.368 djm 2330: if (!c->have_remote_id)
1.403 djm 2331: fatal_f("channel %d: no remote id", c->self);
1.367 djm 2332: if ((r = sshpkt_start(ssh,
2333: SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2334: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2335: (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2336: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 2337: fatal_fr(r, "channel %i", c->self);
1.367 djm 2338: }
1.403 djm 2339: debug2("channel %d: window %d sent adjust %d", c->self,
2340: c->local_window, c->local_consumed);
1.44 markus 2341: c->local_window += c->local_consumed;
2342: c->local_consumed = 0;
1.1 deraadt 2343: }
1.41 markus 2344: return 1;
1.1 deraadt 2345: }
2346:
1.127 itojun 2347: static void
1.410 djm 2348: channel_post_open(struct ssh *ssh, Channel *c)
1.41 markus 2349: {
1.410 djm 2350: channel_handle_rfd(ssh, c);
2351: channel_handle_wfd(ssh, c);
2352: channel_handle_efd(ssh, c);
1.367 djm 2353: channel_check_window(ssh, c);
1.44 markus 2354: }
2355:
1.302 djm 2356: static u_int
1.367 djm 2357: read_mux(struct ssh *ssh, Channel *c, u_int need)
1.302 djm 2358: {
2359: char buf[CHAN_RBUF];
1.367 djm 2360: ssize_t len;
1.302 djm 2361: u_int rlen;
1.367 djm 2362: int r;
1.302 djm 2363:
1.367 djm 2364: if (sshbuf_len(c->input) < need) {
2365: rlen = need - sshbuf_len(c->input);
1.352 deraadt 2366: len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
1.393 deraadt 2367: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2368: return sshbuf_len(c->input);
1.302 djm 2369: if (len <= 0) {
1.367 djm 2370: debug2("channel %d: ctl read<=0 rfd %d len %zd",
1.349 naddy 2371: c->self, c->rfd, len);
1.367 djm 2372: chan_read_failed(ssh, c);
1.349 naddy 2373: return 0;
1.403 djm 2374: } else if ((r = sshbuf_put(c->input, buf, len)) != 0)
2375: fatal_fr(r, "channel %i: append", c->self);
1.302 djm 2376: }
1.367 djm 2377: return sshbuf_len(c->input);
1.302 djm 2378: }
2379:
2380: static void
1.410 djm 2381: channel_post_mux_client_read(struct ssh *ssh, Channel *c)
1.302 djm 2382: {
2383: u_int need;
2384:
1.410 djm 2385: if ((c->io_ready & SSH_CHAN_IO_RFD) == 0)
1.367 djm 2386: return;
2387: if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2388: return;
2389: if (c->mux_pause)
2390: return;
2391:
2392: /*
2393: * Don't not read past the precise end of packets to
2394: * avoid disrupting fd passing.
2395: */
2396: if (read_mux(ssh, c, 4) < 4) /* read header */
2397: return;
2398: /* XXX sshbuf_peek_u32 */
2399: need = PEEK_U32(sshbuf_ptr(c->input));
1.302 djm 2400: #define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1.367 djm 2401: if (need > CHANNEL_MUX_MAX_PACKET) {
2402: debug2("channel %d: packet too big %u > %u",
2403: c->self, CHANNEL_MUX_MAX_PACKET, need);
2404: chan_rcvd_oclose(ssh, c);
2405: return;
2406: }
2407: if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2408: return;
2409: if (c->mux_rcb(ssh, c) != 0) {
2410: debug("channel %d: mux_rcb failed", c->self);
2411: chan_mark_dead(ssh, c);
2412: return;
1.302 djm 2413: }
1.367 djm 2414: }
2415:
2416: static void
1.410 djm 2417: channel_post_mux_client_write(struct ssh *ssh, Channel *c)
1.367 djm 2418: {
2419: ssize_t len;
2420: int r;
1.302 djm 2421:
1.410 djm 2422: if ((c->io_ready & SSH_CHAN_IO_WFD) == 0)
2423: return;
2424: if (sshbuf_len(c->output) == 0)
1.367 djm 2425: return;
2426:
2427: len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
1.393 deraadt 2428: if (len == -1 && (errno == EINTR || errno == EAGAIN))
1.367 djm 2429: return;
2430: if (len <= 0) {
2431: chan_mark_dead(ssh, c);
2432: return;
1.302 djm 2433: }
1.367 djm 2434: if ((r = sshbuf_consume(c->output, len)) != 0)
1.403 djm 2435: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2436: }
2437:
2438: static void
1.410 djm 2439: channel_post_mux_client(struct ssh *ssh, Channel *c)
1.367 djm 2440: {
1.410 djm 2441: channel_post_mux_client_read(ssh, c);
2442: channel_post_mux_client_write(ssh, c);
1.302 djm 2443: }
2444:
2445: static void
1.410 djm 2446: channel_post_mux_listener(struct ssh *ssh, Channel *c)
1.302 djm 2447: {
2448: Channel *nc;
2449: struct sockaddr_storage addr;
2450: socklen_t addrlen;
2451: int newsock;
2452: uid_t euid;
2453: gid_t egid;
2454:
1.410 djm 2455: if ((c->io_ready & SSH_CHAN_IO_SOCK_R) == 0)
1.302 djm 2456: return;
2457:
2458: debug("multiplexing control connection");
2459:
2460: /*
2461: * Accept connection on control socket
2462: */
2463: memset(&addr, 0, sizeof(addr));
2464: addrlen = sizeof(addr);
2465: if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2466: &addrlen)) == -1) {
1.403 djm 2467: error_f("accept: %s", strerror(errno));
1.317 djm 2468: if (errno == EMFILE || errno == ENFILE)
1.322 dtucker 2469: c->notbefore = monotime() + 1;
1.302 djm 2470: return;
2471: }
2472:
1.393 deraadt 2473: if (getpeereid(newsock, &euid, &egid) == -1) {
1.403 djm 2474: error_f("getpeereid failed: %s", strerror(errno));
1.302 djm 2475: close(newsock);
2476: return;
2477: }
2478: if ((euid != 0) && (getuid() != euid)) {
2479: error("multiplex uid mismatch: peer euid %u != uid %u",
2480: (u_int)euid, (u_int)getuid());
2481: close(newsock);
2482: return;
2483: }
1.424 djm 2484: nc = channel_new(ssh, "mux-control", SSH_CHANNEL_MUX_CLIENT,
1.302 djm 2485: newsock, newsock, -1, c->local_window_max,
2486: c->local_maxpacket, 0, "mux-control", 1);
2487: nc->mux_rcb = c->mux_rcb;
1.403 djm 2488: debug3_f("new mux channel %d fd %d", nc->self, nc->sock);
1.302 djm 2489: /* establish state */
1.367 djm 2490: nc->mux_rcb(ssh, nc);
1.302 djm 2491: /* mux state transitions must not elicit protocol messages */
2492: nc->flags |= CHAN_LOCAL;
2493: }
2494:
1.127 itojun 2495: static void
1.367 djm 2496: channel_handler_init(struct ssh_channels *sc)
1.1 deraadt 2497: {
1.367 djm 2498: chan_fn **pre, **post;
2499:
2500: if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
1.406 djm 2501: (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
1.403 djm 2502: fatal_f("allocation failed");
1.367 djm 2503:
2504: pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2505: pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2506: pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2507: pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2508: pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2509: pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2510: pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2511: pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2512: pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2513: pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.372 markus 2514: pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
1.367 djm 2515: pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2516: pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2517:
2518: post[SSH_CHANNEL_OPEN] = &channel_post_open;
2519: post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2520: post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2521: post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2522: post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2523: post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2524: post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2525: post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2526: post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1.372 markus 2527: post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
1.367 djm 2528: post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2529: post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
1.180 deraadt 2530:
1.367 djm 2531: sc->channel_pre = pre;
2532: sc->channel_post = post;
1.44 markus 2533: }
2534:
1.140 markus 2535: /* gc dead channels */
2536: static void
1.367 djm 2537: channel_garbage_collect(struct ssh *ssh, Channel *c)
1.140 markus 2538: {
2539: if (c == NULL)
2540: return;
2541: if (c->detach_user != NULL) {
1.367 djm 2542: if (!chan_is_dead(ssh, c, c->detach_close))
1.140 markus 2543: return;
1.385 djm 2544:
1.194 markus 2545: debug2("channel %d: gc: notify user", c->self);
1.423 djm 2546: c->detach_user(ssh, c->self, 0, NULL);
1.140 markus 2547: /* if we still have a callback */
2548: if (c->detach_user != NULL)
2549: return;
1.194 markus 2550: debug2("channel %d: gc: user detached", c->self);
1.140 markus 2551: }
1.367 djm 2552: if (!chan_is_dead(ssh, c, 1))
1.140 markus 2553: return;
1.194 markus 2554: debug2("channel %d: garbage collecting", c->self);
1.367 djm 2555: channel_free(ssh, c);
1.140 markus 2556: }
2557:
1.367 djm 2558: enum channel_table { CHAN_PRE, CHAN_POST };
2559:
1.127 itojun 2560: static void
1.422 djm 2561: channel_handler(struct ssh *ssh, int table, struct timespec *timeout)
1.41 markus 2562: {
1.367 djm 2563: struct ssh_channels *sc = ssh->chanctxt;
2564: chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
1.299 markus 2565: u_int i, oalloc;
1.41 markus 2566: Channel *c;
1.317 djm 2567: time_t now;
1.25 markus 2568:
1.322 dtucker 2569: now = monotime();
1.367 djm 2570: for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2571: c = sc->channels[i];
1.113 markus 2572: if (c == NULL)
1.41 markus 2573: continue;
1.417 djm 2574: /* Try to keep IO going while rekeying */
2575: if (ssh_packet_is_rekeying(ssh) && c->type != SSH_CHANNEL_OPEN)
2576: continue;
1.299 markus 2577: if (c->delayed) {
1.367 djm 2578: if (table == CHAN_PRE)
1.299 markus 2579: c->delayed = 0;
2580: else
2581: continue;
2582: }
1.317 djm 2583: if (ftab[c->type] != NULL) {
1.436 djm 2584: if (table == CHAN_PRE && c->type == SSH_CHANNEL_OPEN &&
2585: channel_get_expiry(ssh, c) != 0 &&
2586: now >= channel_get_expiry(ssh, c)) {
1.426 djm 2587: /* channel closed for inactivity */
2588: verbose("channel %d: closing after %u seconds "
2589: "of inactivity", c->self,
2590: c->inactive_deadline);
2591: channel_force_close(ssh, c, 1);
2592: } else if (c->notbefore <= now) {
2593: /* Run handlers that are not paused. */
1.410 djm 2594: (*ftab[c->type])(ssh, c);
1.426 djm 2595: /* inactivity timeouts must interrupt poll() */
2596: if (timeout != NULL &&
2597: c->type == SSH_CHANNEL_OPEN &&
1.436 djm 2598: channel_get_expiry(ssh, c) != 0) {
1.426 djm 2599: ptimeout_deadline_monotime(timeout,
1.436 djm 2600: channel_get_expiry(ssh, c));
1.426 djm 2601: }
2602: } else if (timeout != NULL) {
1.317 djm 2603: /*
1.426 djm 2604: * Arrange for poll() wakeup when channel pause
1.422 djm 2605: * timer expires.
1.317 djm 2606: */
1.422 djm 2607: ptimeout_deadline_monotime(timeout,
2608: c->notbefore);
1.317 djm 2609: }
2610: }
1.367 djm 2611: channel_garbage_collect(ssh, c);
1.1 deraadt 2612: }
2613: }
2614:
1.121 markus 2615: /*
1.411 djm 2616: * Create sockets before preparing IO.
1.372 markus 2617: * This is necessary for things that need to happen after reading
1.411 djm 2618: * the network-input but need to be completed before IO event setup, e.g.
2619: * because they may create new channels.
1.372 markus 2620: */
2621: static void
1.411 djm 2622: channel_before_prepare_io(struct ssh *ssh)
1.372 markus 2623: {
2624: struct ssh_channels *sc = ssh->chanctxt;
2625: Channel *c;
2626: u_int i, oalloc;
2627:
2628: for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2629: c = sc->channels[i];
2630: if (c == NULL)
2631: continue;
2632: if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
1.411 djm 2633: channel_before_prepare_io_rdynamic(ssh, c);
2634: }
2635: }
2636:
2637: static void
2638: dump_channel_poll(const char *func, const char *what, Channel *c,
2639: u_int pollfd_offset, struct pollfd *pfd)
2640: {
2641: #ifdef DEBUG_CHANNEL_POLL
1.415 djm 2642: debug3("%s: channel %d: %s r%d w%d e%d s%d c->pfds [ %d %d %d %d ] "
2643: "io_want 0x%02x io_ready 0x%02x pfd[%u].fd=%d "
2644: "pfd.ev 0x%02x pfd.rev 0x%02x", func, c->self, what,
2645: c->rfd, c->wfd, c->efd, c->sock,
2646: c->pfds[0], c->pfds[1], c->pfds[2], c->pfds[3],
2647: c->io_want, c->io_ready,
2648: pollfd_offset, pfd->fd, pfd->events, pfd->revents);
1.411 djm 2649: #endif
2650: }
2651:
2652: /* Prepare pollfd entries for a single channel */
2653: static void
2654: channel_prepare_pollfd(Channel *c, u_int *next_pollfd,
2655: struct pollfd *pfd, u_int npfd)
2656: {
1.415 djm 2657: u_int ev, p = *next_pollfd;
1.411 djm 2658:
2659: if (c == NULL)
2660: return;
2661: if (p + 4 > npfd) {
2662: /* Shouldn't happen */
2663: fatal_f("channel %d: bad pfd offset %u (max %u)",
2664: c->self, p, npfd);
2665: }
1.415 djm 2666: c->pfds[0] = c->pfds[1] = c->pfds[2] = c->pfds[3] = -1;
1.411 djm 2667: /*
2668: * prepare c->rfd
2669: *
2670: * This is a special case, since c->rfd might be the same as
2671: * c->wfd, c->efd and/or c->sock. Handle those here if they want
2672: * IO too.
2673: */
2674: if (c->rfd != -1) {
1.415 djm 2675: ev = 0;
1.411 djm 2676: if ((c->io_want & SSH_CHAN_IO_RFD) != 0)
1.415 djm 2677: ev |= POLLIN;
1.411 djm 2678: /* rfd == wfd */
1.415 djm 2679: if (c->wfd == c->rfd) {
2680: if ((c->io_want & SSH_CHAN_IO_WFD) != 0)
2681: ev |= POLLOUT;
2682: }
1.411 djm 2683: /* rfd == efd */
1.415 djm 2684: if (c->efd == c->rfd) {
2685: if ((c->io_want & SSH_CHAN_IO_EFD_R) != 0)
2686: ev |= POLLIN;
2687: if ((c->io_want & SSH_CHAN_IO_EFD_W) != 0)
2688: ev |= POLLOUT;
2689: }
1.411 djm 2690: /* rfd == sock */
1.415 djm 2691: if (c->sock == c->rfd) {
2692: if ((c->io_want & SSH_CHAN_IO_SOCK_R) != 0)
2693: ev |= POLLIN;
2694: if ((c->io_want & SSH_CHAN_IO_SOCK_W) != 0)
2695: ev |= POLLOUT;
2696: }
2697: /* Pack a pfd entry if any event armed for this fd */
2698: if (ev != 0) {
2699: c->pfds[0] = p;
2700: pfd[p].fd = c->rfd;
2701: pfd[p].events = ev;
2702: dump_channel_poll(__func__, "rfd", c, p, &pfd[p]);
2703: p++;
2704: }
1.411 djm 2705: }
1.415 djm 2706: /* prepare c->wfd if wanting IO and not already handled above */
1.411 djm 2707: if (c->wfd != -1 && c->rfd != c->wfd) {
1.415 djm 2708: ev = 0;
2709: if ((c->io_want & SSH_CHAN_IO_WFD))
2710: ev |= POLLOUT;
2711: /* Pack a pfd entry if any event armed for this fd */
2712: if (ev != 0) {
2713: c->pfds[1] = p;
2714: pfd[p].fd = c->wfd;
2715: pfd[p].events = ev;
2716: dump_channel_poll(__func__, "wfd", c, p, &pfd[p]);
2717: p++;
2718: }
1.411 djm 2719: }
1.415 djm 2720: /* prepare c->efd if wanting IO and not already handled above */
1.411 djm 2721: if (c->efd != -1 && c->rfd != c->efd) {
1.415 djm 2722: ev = 0;
1.411 djm 2723: if ((c->io_want & SSH_CHAN_IO_EFD_R) != 0)
1.415 djm 2724: ev |= POLLIN;
1.411 djm 2725: if ((c->io_want & SSH_CHAN_IO_EFD_W) != 0)
1.415 djm 2726: ev |= POLLOUT;
2727: /* Pack a pfd entry if any event armed for this fd */
2728: if (ev != 0) {
2729: c->pfds[2] = p;
2730: pfd[p].fd = c->efd;
2731: pfd[p].events = ev;
2732: dump_channel_poll(__func__, "efd", c, p, &pfd[p]);
2733: p++;
2734: }
1.411 djm 2735: }
1.415 djm 2736: /* prepare c->sock if wanting IO and not already handled above */
1.411 djm 2737: if (c->sock != -1 && c->rfd != c->sock) {
1.415 djm 2738: ev = 0;
1.411 djm 2739: if ((c->io_want & SSH_CHAN_IO_SOCK_R) != 0)
1.415 djm 2740: ev |= POLLIN;
1.411 djm 2741: if ((c->io_want & SSH_CHAN_IO_SOCK_W) != 0)
1.415 djm 2742: ev |= POLLOUT;
2743: /* Pack a pfd entry if any event armed for this fd */
2744: if (ev != 0) {
2745: c->pfds[3] = p;
2746: pfd[p].fd = c->sock;
2747: pfd[p].events = 0;
2748: dump_channel_poll(__func__, "sock", c, p, &pfd[p]);
2749: p++;
2750: }
1.372 markus 2751: }
1.411 djm 2752: *next_pollfd = p;
1.372 markus 2753: }
2754:
1.411 djm 2755: /* * Allocate/prepare poll structure */
1.49 markus 2756: void
1.411 djm 2757: channel_prepare_poll(struct ssh *ssh, struct pollfd **pfdp, u_int *npfd_allocp,
1.422 djm 2758: u_int *npfd_activep, u_int npfd_reserved, struct timespec *timeout)
1.41 markus 2759: {
1.410 djm 2760: struct ssh_channels *sc = ssh->chanctxt;
1.411 djm 2761: u_int i, oalloc, p, npfd = npfd_reserved;
2762:
2763: channel_before_prepare_io(ssh); /* might create a new channel */
1.416 djm 2764: /* clear out I/O flags from last poll */
2765: for (i = 0; i < sc->channels_alloc; i++) {
2766: if (sc->channels[i] == NULL)
2767: continue;
2768: sc->channels[i]->io_want = sc->channels[i]->io_ready = 0;
2769: }
1.411 djm 2770: /* Allocate 4x pollfd for each channel (rfd, wfd, efd, sock) */
2771: if (sc->channels_alloc >= (INT_MAX / 4) - npfd_reserved)
2772: fatal_f("too many channels"); /* shouldn't happen */
1.417 djm 2773: npfd += sc->channels_alloc * 4;
1.411 djm 2774: if (npfd > *npfd_allocp) {
2775: *pfdp = xrecallocarray(*pfdp, *npfd_allocp,
2776: npfd, sizeof(**pfdp));
2777: *npfd_allocp = npfd;
2778: }
2779: *npfd_activep = npfd_reserved;
2780: oalloc = sc->channels_alloc;
1.372 markus 2781:
1.422 djm 2782: channel_handler(ssh, CHAN_PRE, timeout);
1.84 markus 2783:
1.411 djm 2784: if (oalloc != sc->channels_alloc) {
2785: /* shouldn't happen */
2786: fatal_f("channels_alloc changed during CHAN_PRE "
2787: "(was %u, now %u)", oalloc, sc->channels_alloc);
2788: }
1.84 markus 2789:
1.411 djm 2790: /* Prepare pollfd */
2791: p = npfd_reserved;
2792: for (i = 0; i < sc->channels_alloc; i++)
2793: channel_prepare_pollfd(sc->channels[i], &p, *pfdp, npfd);
2794: *npfd_activep = p;
2795: }
2796:
2797: static void
1.415 djm 2798: fd_ready(Channel *c, int p, struct pollfd *pfds, u_int npfd, int fd,
1.411 djm 2799: const char *what, u_int revents_mask, u_int ready)
2800: {
2801: struct pollfd *pfd = &pfds[p];
1.410 djm 2802:
1.411 djm 2803: if (fd == -1)
2804: return;
1.415 djm 2805: if (p == -1 || (u_int)p >= npfd)
2806: fatal_f("channel %d: bad pfd %d (max %u)", c->self, p, npfd);
1.411 djm 2807: dump_channel_poll(__func__, what, c, p, pfd);
2808: if (pfd->fd != fd) {
2809: fatal("channel %d: inconsistent %s fd=%d pollfd[%u].fd %d "
2810: "r%d w%d e%d s%d", c->self, what, fd, p, pfd->fd,
2811: c->rfd, c->wfd, c->efd, c->sock);
2812: }
2813: if ((pfd->revents & POLLNVAL) != 0) {
2814: fatal("channel %d: invalid %s pollfd[%u].fd %d r%d w%d e%d s%d",
2815: c->self, what, p, pfd->fd, c->rfd, c->wfd, c->efd, c->sock);
1.410 djm 2816: }
1.411 djm 2817: if ((pfd->revents & (revents_mask|POLLHUP|POLLERR)) != 0)
2818: c->io_ready |= ready & c->io_want;
1.41 markus 2819: }
2820:
1.121 markus 2821: /*
1.411 djm 2822: * After poll, perform any appropriate operations for channels which have
1.121 markus 2823: * events pending.
2824: */
1.49 markus 2825: void
1.411 djm 2826: channel_after_poll(struct ssh *ssh, struct pollfd *pfd, u_int npfd)
1.41 markus 2827: {
1.410 djm 2828: struct ssh_channels *sc = ssh->chanctxt;
1.415 djm 2829: u_int i;
2830: int p;
1.410 djm 2831: Channel *c;
2832:
1.411 djm 2833: #ifdef DEBUG_CHANNEL_POLL
1.415 djm 2834: for (p = 0; p < (int)npfd; p++) {
1.411 djm 2835: if (pfd[p].revents == 0)
2836: continue;
2837: debug_f("pfd[%u].fd %d rev 0x%04x",
2838: p, pfd[p].fd, pfd[p].revents);
2839: }
2840: #endif
2841:
2842: /* Convert pollfd into c->io_ready */
2843: for (i = 0; i < sc->channels_alloc; i++) {
1.410 djm 2844: c = sc->channels[i];
1.415 djm 2845: if (c == NULL)
1.410 djm 2846: continue;
1.411 djm 2847: /* if rfd is shared with efd/sock then wfd should be too */
2848: if (c->rfd != -1 && c->wfd != -1 && c->rfd != c->wfd &&
2849: (c->rfd == c->efd || c->rfd == c->sock)) {
2850: /* Shouldn't happen */
2851: fatal_f("channel %d: unexpected fds r%d w%d e%d s%d",
2852: c->self, c->rfd, c->wfd, c->efd, c->sock);
2853: }
1.410 djm 2854: c->io_ready = 0;
1.411 djm 2855: /* rfd, potentially shared with wfd, efd and sock */
1.415 djm 2856: if (c->rfd != -1 && (p = c->pfds[0]) != -1) {
2857: fd_ready(c, p, pfd, npfd, c->rfd,
2858: "rfd", POLLIN, SSH_CHAN_IO_RFD);
1.411 djm 2859: if (c->rfd == c->wfd) {
1.415 djm 2860: fd_ready(c, p, pfd, npfd, c->wfd,
2861: "wfd/r", POLLOUT, SSH_CHAN_IO_WFD);
1.411 djm 2862: }
2863: if (c->rfd == c->efd) {
1.415 djm 2864: fd_ready(c, p, pfd, npfd, c->efd,
2865: "efdr/r", POLLIN, SSH_CHAN_IO_EFD_R);
2866: fd_ready(c, p, pfd, npfd, c->efd,
2867: "efdw/r", POLLOUT, SSH_CHAN_IO_EFD_W);
1.411 djm 2868: }
2869: if (c->rfd == c->sock) {
1.415 djm 2870: fd_ready(c, p, pfd, npfd, c->sock,
2871: "sockr/r", POLLIN, SSH_CHAN_IO_SOCK_R);
2872: fd_ready(c, p, pfd, npfd, c->sock,
2873: "sockw/r", POLLOUT, SSH_CHAN_IO_SOCK_W);
1.411 djm 2874: }
1.415 djm 2875: dump_channel_poll(__func__, "rfd", c, p, pfd);
1.411 djm 2876: }
2877: /* wfd */
1.415 djm 2878: if (c->wfd != -1 && c->wfd != c->rfd &&
2879: (p = c->pfds[1]) != -1) {
2880: fd_ready(c, p, pfd, npfd, c->wfd,
2881: "wfd", POLLOUT, SSH_CHAN_IO_WFD);
2882: dump_channel_poll(__func__, "wfd", c, p, pfd);
1.411 djm 2883: }
2884: /* efd */
1.415 djm 2885: if (c->efd != -1 && c->efd != c->rfd &&
2886: (p = c->pfds[2]) != -1) {
2887: fd_ready(c, p, pfd, npfd, c->efd,
2888: "efdr", POLLIN, SSH_CHAN_IO_EFD_R);
2889: fd_ready(c, p, pfd, npfd, c->efd,
2890: "efdw", POLLOUT, SSH_CHAN_IO_EFD_W);
2891: dump_channel_poll(__func__, "efd", c, p, pfd);
1.411 djm 2892: }
2893: /* sock */
1.415 djm 2894: if (c->sock != -1 && c->sock != c->rfd &&
2895: (p = c->pfds[3]) != -1) {
2896: fd_ready(c, p, pfd, npfd, c->sock,
2897: "sockr", POLLIN, SSH_CHAN_IO_SOCK_R);
2898: fd_ready(c, p, pfd, npfd, c->sock,
2899: "sockw", POLLOUT, SSH_CHAN_IO_SOCK_W);
2900: dump_channel_poll(__func__, "sock", c, p, pfd);
1.411 djm 2901: }
1.410 djm 2902: }
2903: channel_handler(ssh, CHAN_POST, NULL);
1.367 djm 2904: }
2905:
2906: /*
2907: * Enqueue data for channels with open or draining c->input.
1.433 djm 2908: * Returns non-zero if a packet was enqueued.
1.367 djm 2909: */
1.433 djm 2910: static int
1.367 djm 2911: channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2912: {
1.373 djm 2913: size_t len, plen;
2914: const u_char *pkt;
1.367 djm 2915: int r;
2916:
2917: if ((len = sshbuf_len(c->input)) == 0) {
2918: if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2919: /*
2920: * input-buffer is empty and read-socket shutdown:
2921: * tell peer, that we will not send more data:
2922: * send IEOF.
2923: * hack for extended data: delay EOF if EFD still
2924: * in use.
2925: */
2926: if (CHANNEL_EFD_INPUT_ACTIVE(c))
2927: debug2("channel %d: "
2928: "ibuf_empty delayed efd %d/(%zu)",
2929: c->self, c->efd, sshbuf_len(c->extended));
2930: else
2931: chan_ibuf_empty(ssh, c);
2932: }
1.433 djm 2933: return 0;
1.367 djm 2934: }
2935:
1.368 djm 2936: if (!c->have_remote_id)
1.403 djm 2937: fatal_f("channel %d: no remote id", c->self);
1.368 djm 2938:
1.367 djm 2939: if (c->datagram) {
2940: /* Check datagram will fit; drop if not */
1.373 djm 2941: if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
1.403 djm 2942: fatal_fr(r, "channel %i: get datagram", c->self);
1.367 djm 2943: /*
2944: * XXX this does tail-drop on the datagram queue which is
2945: * usually suboptimal compared to head-drop. Better to have
2946: * backpressure at read time? (i.e. read + discard)
2947: */
1.373 djm 2948: if (plen > c->remote_window || plen > c->remote_maxpacket) {
1.367 djm 2949: debug("channel %d: datagram too big", c->self);
1.433 djm 2950: return 0;
1.367 djm 2951: }
2952: /* Enqueue it */
2953: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2954: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1.373 djm 2955: (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
1.403 djm 2956: (r = sshpkt_send(ssh)) != 0)
2957: fatal_fr(r, "channel %i: send datagram", c->self);
1.373 djm 2958: c->remote_window -= plen;
1.433 djm 2959: return 1;
1.367 djm 2960: }
2961:
2962: /* Enqueue packet for buffered data. */
2963: if (len > c->remote_window)
2964: len = c->remote_window;
2965: if (len > c->remote_maxpacket)
2966: len = c->remote_maxpacket;
2967: if (len == 0)
1.433 djm 2968: return 0;
1.367 djm 2969: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2970: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2971: (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
1.403 djm 2972: (r = sshpkt_send(ssh)) != 0)
2973: fatal_fr(r, "channel %i: send data", c->self);
1.367 djm 2974: if ((r = sshbuf_consume(c->input, len)) != 0)
1.403 djm 2975: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 2976: c->remote_window -= len;
1.433 djm 2977: return 1;
1.41 markus 2978: }
2979:
1.367 djm 2980: /*
2981: * Enqueue data for channels with open c->extended in read mode.
1.433 djm 2982: * Returns non-zero if a packet was enqueued.
1.367 djm 2983: */
1.433 djm 2984: static int
1.367 djm 2985: channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2986: {
2987: size_t len;
2988: int r;
2989:
2990: if ((len = sshbuf_len(c->extended)) == 0)
1.433 djm 2991: return 0;
1.367 djm 2992:
2993: debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2994: c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2995: if (len > c->remote_window)
2996: len = c->remote_window;
2997: if (len > c->remote_maxpacket)
2998: len = c->remote_maxpacket;
2999: if (len == 0)
1.433 djm 3000: return 0;
1.368 djm 3001: if (!c->have_remote_id)
1.403 djm 3002: fatal_f("channel %d: no remote id", c->self);
1.367 djm 3003: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
3004: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
3005: (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
3006: (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
1.403 djm 3007: (r = sshpkt_send(ssh)) != 0)
3008: fatal_fr(r, "channel %i: data", c->self);
1.367 djm 3009: if ((r = sshbuf_consume(c->extended, len)) != 0)
1.403 djm 3010: fatal_fr(r, "channel %i: consume", c->self);
1.367 djm 3011: c->remote_window -= len;
3012: debug2("channel %d: sent ext data %zu", c->self, len);
1.433 djm 3013: return 1;
1.367 djm 3014: }
1.121 markus 3015:
1.433 djm 3016: /*
3017: * If there is data to send to the connection, enqueue some of it now.
3018: * Returns non-zero if data was enqueued.
3019: */
3020: int
1.367 djm 3021: channel_output_poll(struct ssh *ssh)
1.1 deraadt 3022: {
1.367 djm 3023: struct ssh_channels *sc = ssh->chanctxt;
1.41 markus 3024: Channel *c;
1.367 djm 3025: u_int i;
1.433 djm 3026: int ret = 0;
1.1 deraadt 3027:
1.367 djm 3028: for (i = 0; i < sc->channels_alloc; i++) {
3029: c = sc->channels[i];
1.113 markus 3030: if (c == NULL)
3031: continue;
1.37 markus 3032:
1.121 markus 3033: /*
3034: * We are only interested in channels that can have buffered
3035: * incoming data.
3036: */
1.358 djm 3037: if (c->type != SSH_CHANNEL_OPEN)
3038: continue;
3039: if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 3040: /* XXX is this true? */
1.367 djm 3041: debug3("channel %d: will not send data after close",
3042: c->self);
1.44 markus 3043: continue;
3044: }
1.25 markus 3045:
3046: /* Get the amount of buffered data for this channel. */
1.367 djm 3047: if (c->istate == CHAN_INPUT_OPEN ||
3048: c->istate == CHAN_INPUT_WAIT_DRAIN)
1.433 djm 3049: ret |= channel_output_poll_input_open(ssh, c);
1.44 markus 3050: /* Send extended data, i.e. stderr */
1.358 djm 3051: if (!(c->flags & CHAN_EOF_SENT) &&
1.367 djm 3052: c->extended_usage == CHAN_EXTENDED_READ)
1.433 djm 3053: ret |= channel_output_poll_extended_read(ssh, c);
1.25 markus 3054: }
1.433 djm 3055: return ret;
1.1 deraadt 3056: }
3057:
1.354 markus 3058: /* -- mux proxy support */
3059:
3060: /*
3061: * When multiplexing channel messages for mux clients we have to deal
3062: * with downstream messages from the mux client and upstream messages
3063: * from the ssh server:
3064: * 1) Handling downstream messages is straightforward and happens
3065: * in channel_proxy_downstream():
3066: * - We forward all messages (mostly) unmodified to the server.
3067: * - However, in order to route messages from upstream to the correct
3068: * downstream client, we have to replace the channel IDs used by the
3069: * mux clients with a unique channel ID because the mux clients might
3070: * use conflicting channel IDs.
3071: * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
3072: * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
3073: * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
3074: * with the newly allocated channel ID.
3075: * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
1.380 djm 3076: * channels and processed by channel_proxy_upstream(). The local channel ID
1.354 markus 3077: * is then translated back to the original mux client ID.
3078: * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
3079: * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
3080: * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
3081: * downstream mux client are removed.
3082: * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
3083: * requires more work, because they are not addressed to a specific
3084: * channel. E.g. client_request_forwarded_tcpip() needs to figure
3085: * out whether the request is addressed to the local client or a
3086: * specific downstream client based on the listen-address/port.
1.380 djm 3087: * 6) Agent and X11-Forwarding have a similar problem and are currently
1.354 markus 3088: * not supported as the matching session/channel cannot be identified
3089: * easily.
3090: */
3091:
3092: /*
3093: * receive packets from downstream mux clients:
3094: * channel callback fired on read from mux client, creates
3095: * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
3096: * on channel creation.
3097: */
3098: int
1.367 djm 3099: channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
1.354 markus 3100: {
3101: Channel *c = NULL;
3102: struct sshbuf *original = NULL, *modified = NULL;
3103: const u_char *cp;
3104: char *ctype = NULL, *listen_host = NULL;
3105: u_char type;
3106: size_t have;
1.370 djm 3107: int ret = -1, r;
1.355 djm 3108: u_int id, remote_id, listen_port;
1.354 markus 3109:
1.367 djm 3110: /* sshbuf_dump(downstream->input, stderr); */
3111: if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
1.354 markus 3112: != 0) {
1.403 djm 3113: error_fr(r, "parse");
1.354 markus 3114: return -1;
3115: }
3116: if (have < 2) {
1.403 djm 3117: error_f("short message");
1.354 markus 3118: return -1;
3119: }
3120: type = cp[1];
3121: /* skip padlen + type */
3122: cp += 2;
3123: have -= 2;
3124: if (ssh_packet_log_type(type))
1.403 djm 3125: debug3_f("channel %u: down->up: type %u",
1.354 markus 3126: downstream->self, type);
3127:
3128: switch (type) {
3129: case SSH2_MSG_CHANNEL_OPEN:
3130: if ((original = sshbuf_from(cp, have)) == NULL ||
3131: (modified = sshbuf_new()) == NULL) {
1.403 djm 3132: error_f("alloc");
1.354 markus 3133: goto out;
3134: }
3135: if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
3136: (r = sshbuf_get_u32(original, &id)) != 0) {
1.403 djm 3137: error_fr(r, "parse");
1.354 markus 3138: goto out;
3139: }
1.424 djm 3140: c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
1.406 djm 3141: -1, -1, -1, 0, 0, 0, ctype, 1);
1.354 markus 3142: c->mux_ctx = downstream; /* point to mux client */
3143: c->mux_downstream_id = id; /* original downstream id */
3144: if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
3145: (r = sshbuf_put_u32(modified, c->self)) != 0 ||
3146: (r = sshbuf_putb(modified, original)) != 0) {
1.403 djm 3147: error_fr(r, "compose");
1.367 djm 3148: channel_free(ssh, c);
1.354 markus 3149: goto out;
3150: }
3151: break;
3152: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
3153: /*
3154: * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
3155: * need to parse 'remote_id' instead of 'ctype'.
3156: */
3157: if ((original = sshbuf_from(cp, have)) == NULL ||
3158: (modified = sshbuf_new()) == NULL) {
1.403 djm 3159: error_f("alloc");
1.354 markus 3160: goto out;
3161: }
3162: if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
3163: (r = sshbuf_get_u32(original, &id)) != 0) {
1.403 djm 3164: error_fr(r, "parse");
1.354 markus 3165: goto out;
3166: }
1.424 djm 3167: c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
1.406 djm 3168: -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
1.354 markus 3169: c->mux_ctx = downstream; /* point to mux client */
3170: c->mux_downstream_id = id;
3171: c->remote_id = remote_id;
1.368 djm 3172: c->have_remote_id = 1;
1.354 markus 3173: if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
3174: (r = sshbuf_put_u32(modified, c->self)) != 0 ||
3175: (r = sshbuf_putb(modified, original)) != 0) {
1.403 djm 3176: error_fr(r, "compose");
1.367 djm 3177: channel_free(ssh, c);
1.354 markus 3178: goto out;
3179: }
3180: break;
3181: case SSH2_MSG_GLOBAL_REQUEST:
3182: if ((original = sshbuf_from(cp, have)) == NULL) {
1.403 djm 3183: error_f("alloc");
1.354 markus 3184: goto out;
3185: }
3186: if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
1.403 djm 3187: error_fr(r, "parse");
1.354 markus 3188: goto out;
3189: }
3190: if (strcmp(ctype, "tcpip-forward") != 0) {
1.403 djm 3191: error_f("unsupported request %s", ctype);
1.354 markus 3192: goto out;
3193: }
3194: if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
3195: (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
3196: (r = sshbuf_get_u32(original, &listen_port)) != 0) {
1.403 djm 3197: error_fr(r, "parse");
1.354 markus 3198: goto out;
3199: }
1.355 djm 3200: if (listen_port > 65535) {
1.403 djm 3201: error_f("tcpip-forward for %s: bad port %u",
3202: listen_host, listen_port);
1.355 djm 3203: goto out;
3204: }
1.354 markus 3205: /* Record that connection to this host/port is permitted. */
1.437 ! djm 3206: permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>",
! 3207: -1, listen_host, NULL, (int)listen_port, downstream);
1.354 markus 3208: break;
3209: case SSH2_MSG_CHANNEL_CLOSE:
3210: if (have < 4)
3211: break;
3212: remote_id = PEEK_U32(cp);
1.367 djm 3213: if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
1.354 markus 3214: if (c->flags & CHAN_CLOSE_RCVD)
1.367 djm 3215: channel_free(ssh, c);
1.354 markus 3216: else
3217: c->flags |= CHAN_CLOSE_SENT;
3218: }
3219: break;
3220: }
3221: if (modified) {
3222: if ((r = sshpkt_start(ssh, type)) != 0 ||
3223: (r = sshpkt_putb(ssh, modified)) != 0 ||
3224: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 3225: error_fr(r, "send");
1.354 markus 3226: goto out;
3227: }
3228: } else {
3229: if ((r = sshpkt_start(ssh, type)) != 0 ||
3230: (r = sshpkt_put(ssh, cp, have)) != 0 ||
3231: (r = sshpkt_send(ssh)) != 0) {
1.403 djm 3232: error_fr(r, "send");
1.354 markus 3233: goto out;
3234: }
3235: }
3236: ret = 0;
3237: out:
3238: free(ctype);
3239: free(listen_host);
3240: sshbuf_free(original);
3241: sshbuf_free(modified);
3242: return ret;
3243: }
3244:
3245: /*
3246: * receive packets from upstream server and de-multiplex packets
3247: * to correct downstream:
3248: * implemented as a helper for channel input handlers,
3249: * replaces local (proxy) channel ID with downstream channel ID.
3250: */
3251: int
1.363 markus 3252: channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
1.354 markus 3253: {
3254: struct sshbuf *b = NULL;
3255: Channel *downstream;
3256: const u_char *cp = NULL;
3257: size_t len;
3258: int r;
3259:
3260: /*
3261: * When receiving packets from the peer we need to check whether we
3262: * need to forward the packets to the mux client. In this case we
1.380 djm 3263: * restore the original channel id and keep track of CLOSE messages,
1.354 markus 3264: * so we can cleanup the channel.
3265: */
3266: if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
3267: return 0;
3268: if ((downstream = c->mux_ctx) == NULL)
3269: return 0;
3270: switch (type) {
3271: case SSH2_MSG_CHANNEL_CLOSE:
3272: case SSH2_MSG_CHANNEL_DATA:
3273: case SSH2_MSG_CHANNEL_EOF:
3274: case SSH2_MSG_CHANNEL_EXTENDED_DATA:
3275: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
3276: case SSH2_MSG_CHANNEL_OPEN_FAILURE:
3277: case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
3278: case SSH2_MSG_CHANNEL_SUCCESS:
3279: case SSH2_MSG_CHANNEL_FAILURE:
3280: case SSH2_MSG_CHANNEL_REQUEST:
3281: break;
3282: default:
1.403 djm 3283: debug2_f("channel %u: unsupported type %u", c->self, type);
1.354 markus 3284: return 0;
3285: }
3286: if ((b = sshbuf_new()) == NULL) {
1.403 djm 3287: error_f("alloc reply");
1.354 markus 3288: goto out;
3289: }
3290: /* get remaining payload (after id) */
3291: cp = sshpkt_ptr(ssh, &len);
3292: if (cp == NULL) {
1.403 djm 3293: error_f("no packet");
1.354 markus 3294: goto out;
3295: }
3296: /* translate id and send to muxclient */
3297: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
3298: (r = sshbuf_put_u8(b, type)) != 0 ||
3299: (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
3300: (r = sshbuf_put(b, cp, len)) != 0 ||
1.367 djm 3301: (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
1.403 djm 3302: error_fr(r, "compose muxclient");
1.354 markus 3303: goto out;
3304: }
3305: /* sshbuf_dump(b, stderr); */
3306: if (ssh_packet_log_type(type))
1.403 djm 3307: debug3_f("channel %u: up->down: type %u", c->self, type);
1.354 markus 3308: out:
3309: /* update state */
3310: switch (type) {
3311: case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
3312: /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
1.368 djm 3313: if (cp && len > 4) {
1.354 markus 3314: c->remote_id = PEEK_U32(cp);
1.368 djm 3315: c->have_remote_id = 1;
3316: }
1.354 markus 3317: break;
3318: case SSH2_MSG_CHANNEL_CLOSE:
3319: if (c->flags & CHAN_CLOSE_SENT)
1.367 djm 3320: channel_free(ssh, c);
1.354 markus 3321: else
3322: c->flags |= CHAN_CLOSE_RCVD;
3323: break;
3324: }
3325: sshbuf_free(b);
3326: return 1;
3327: }
1.121 markus 3328:
3329: /* -- protocol input */
1.249 djm 3330:
1.367 djm 3331: /* Parse a channel ID from the current packet */
3332: static int
3333: channel_parse_id(struct ssh *ssh, const char *where, const char *what)
3334: {
3335: u_int32_t id;
3336: int r;
3337:
3338: if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
1.403 djm 3339: error_r(r, "%s: parse id", where);
1.367 djm 3340: ssh_packet_disconnect(ssh, "Invalid %s message", what);
3341: }
3342: if (id > INT_MAX) {
1.403 djm 3343: error_r(r, "%s: bad channel id %u", where, id);
1.367 djm 3344: ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
3345: }
3346: return (int)id;
3347: }
3348:
3349: /* Lookup a channel from an ID in the current packet */
3350: static Channel *
3351: channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
3352: {
3353: int id = channel_parse_id(ssh, where, what);
3354: Channel *c;
3355:
3356: if ((c = channel_lookup(ssh, id)) == NULL) {
3357: ssh_packet_disconnect(ssh,
3358: "%s packet referred to nonexistent channel %d", what, id);
3359: }
3360: return c;
3361: }
3362:
1.339 markus 3363: int
1.363 markus 3364: channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3365: {
1.332 djm 3366: const u_char *data;
1.367 djm 3367: size_t data_len, win_len;
3368: Channel *c = channel_from_packet_id(ssh, __func__, "data");
3369: int r;
1.25 markus 3370:
1.363 markus 3371: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3372: return 0;
1.25 markus 3373:
3374: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 3375: if (c->type != SSH_CHANNEL_OPEN &&
1.372 markus 3376: c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
3377: c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
1.41 markus 3378: c->type != SSH_CHANNEL_X11_OPEN)
1.339 markus 3379: return 0;
1.37 markus 3380:
1.25 markus 3381: /* Get the data. */
1.389 djm 3382: if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3383: (r = sshpkt_get_end(ssh)) != 0)
1.403 djm 3384: fatal_fr(r, "channel %i: get data", c->self);
1.367 djm 3385:
1.309 djm 3386: win_len = data_len;
3387: if (c->datagram)
3388: win_len += 4; /* string length header */
1.200 markus 3389:
3390: /*
1.367 djm 3391: * The sending side reduces its window as it sends data, so we
3392: * must 'fake' consumption of the data in order to ensure that window
3393: * updates are sent back. Otherwise the connection might deadlock.
1.200 markus 3394: */
1.358 djm 3395: if (c->ostate != CHAN_OUTPUT_OPEN) {
3396: c->local_window -= win_len;
3397: c->local_consumed += win_len;
1.339 markus 3398: return 0;
1.200 markus 3399: }
1.41 markus 3400:
1.358 djm 3401: if (win_len > c->local_maxpacket) {
1.367 djm 3402: logit("channel %d: rcvd big packet %zu, maxpack %u",
1.358 djm 3403: c->self, win_len, c->local_maxpacket);
1.367 djm 3404: return 0;
1.358 djm 3405: }
3406: if (win_len > c->local_window) {
1.435 djm 3407: c->local_window_exceeded += win_len - c->local_window;
3408: logit("channel %d: rcvd too much data %zu, win %u/%u "
3409: "(excess %u)", c->self, win_len, c->local_window,
3410: c->local_window_max, c->local_window_exceeded);
3411: c->local_window = 0;
3412: /* Allow 10% grace before bringing the hammer down */
3413: if (c->local_window_exceeded > (c->local_window_max / 10)) {
3414: ssh_packet_disconnect(ssh, "channel %d: peer ignored "
3415: "channel window", c->self);
3416: }
3417: } else {
3418: c->local_window -= win_len;
3419: c->local_window_exceeded = 0;
1.44 markus 3420: }
1.358 djm 3421:
1.367 djm 3422: if (c->datagram) {
3423: if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
1.403 djm 3424: fatal_fr(r, "channel %i: append datagram", c->self);
1.367 djm 3425: } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
1.403 djm 3426: fatal_fr(r, "channel %i: append data", c->self);
1.367 djm 3427:
1.339 markus 3428: return 0;
1.1 deraadt 3429: }
1.121 markus 3430:
1.339 markus 3431: int
1.363 markus 3432: channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
1.44 markus 3433: {
1.367 djm 3434: const u_char *data;
3435: size_t data_len;
3436: u_int32_t tcode;
3437: Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3438: int r;
1.44 markus 3439:
1.363 markus 3440: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3441: return 0;
1.44 markus 3442: if (c->type != SSH_CHANNEL_OPEN) {
1.367 djm 3443: logit("channel %d: ext data for non open", c->self);
1.339 markus 3444: return 0;
1.172 markus 3445: }
3446: if (c->flags & CHAN_EOF_RCVD) {
1.404 djm 3447: if (ssh->compat & SSH_BUG_EXTEOF)
1.367 djm 3448: debug("channel %d: accepting ext data after eof",
3449: c->self);
1.172 markus 3450: else
1.367 djm 3451: ssh_packet_disconnect(ssh, "Received extended_data "
3452: "after EOF on channel %d.", c->self);
3453: }
3454:
3455: if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
1.403 djm 3456: error_fr(r, "parse tcode");
1.367 djm 3457: ssh_packet_disconnect(ssh, "Invalid extended_data message");
1.44 markus 3458: }
3459: if (c->efd == -1 ||
3460: c->extended_usage != CHAN_EXTENDED_WRITE ||
3461: tcode != SSH2_EXTENDED_DATA_STDERR) {
1.188 itojun 3462: logit("channel %d: bad ext data", c->self);
1.339 markus 3463: return 0;
1.44 markus 3464: }
1.389 djm 3465: if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3466: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3467: error_fr(r, "parse data");
1.367 djm 3468: ssh_packet_disconnect(ssh, "Invalid extended_data message");
3469: }
3470:
1.44 markus 3471: if (data_len > c->local_window) {
1.367 djm 3472: logit("channel %d: rcvd too much extended_data %zu, win %u",
1.44 markus 3473: c->self, data_len, c->local_window);
1.339 markus 3474: return 0;
1.44 markus 3475: }
1.367 djm 3476: debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3477: /* XXX sshpkt_getb? */
3478: if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
1.403 djm 3479: error_fr(r, "append");
1.44 markus 3480: c->local_window -= data_len;
1.339 markus 3481: return 0;
1.44 markus 3482: }
3483:
1.339 markus 3484: int
1.363 markus 3485: channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
1.41 markus 3486: {
1.367 djm 3487: Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
1.389 djm 3488: int r;
1.367 djm 3489:
1.389 djm 3490: if ((r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3491: error_fr(r, "parse data");
1.389 djm 3492: ssh_packet_disconnect(ssh, "Invalid ieof message");
3493: }
1.41 markus 3494:
1.363 markus 3495: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3496: return 0;
1.367 djm 3497: chan_rcvd_ieof(ssh, c);
1.133 markus 3498:
3499: /* XXX force input close */
1.156 markus 3500: if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1.133 markus 3501: debug("channel %d: FORCE input drain", c->self);
3502: c->istate = CHAN_INPUT_WAIT_DRAIN;
1.367 djm 3503: if (sshbuf_len(c->input) == 0)
3504: chan_ibuf_empty(ssh, c);
1.133 markus 3505: }
1.339 markus 3506: return 0;
1.41 markus 3507: }
1.1 deraadt 3508:
1.339 markus 3509: int
1.363 markus 3510: channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
1.41 markus 3511: {
1.367 djm 3512: Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
1.389 djm 3513: int r;
1.151 markus 3514:
1.363 markus 3515: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3516: return 0;
1.389 djm 3517: if ((r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3518: error_fr(r, "parse data");
1.389 djm 3519: ssh_packet_disconnect(ssh, "Invalid oclose message");
3520: }
1.367 djm 3521: chan_rcvd_oclose(ssh, c);
1.339 markus 3522: return 0;
1.1 deraadt 3523: }
3524:
1.339 markus 3525: int
1.363 markus 3526: channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3527: {
1.367 djm 3528: Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3529: u_int32_t remote_window, remote_maxpacket;
3530: int r;
1.25 markus 3531:
1.363 markus 3532: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3533: return 0;
3534: if (c->type != SSH_CHANNEL_OPENING)
1.389 djm 3535: ssh_packet_disconnect(ssh, "Received open confirmation for "
1.367 djm 3536: "non-opening channel %d.", c->self);
3537: /*
3538: * Record the remote channel number and mark that the channel
3539: * is now open.
3540: */
1.368 djm 3541: if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3542: (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
1.389 djm 3543: (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 ||
3544: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3545: error_fr(r, "window/maxpacket");
1.389 djm 3546: ssh_packet_disconnect(ssh, "Invalid open confirmation message");
1.367 djm 3547: }
3548:
1.368 djm 3549: c->have_remote_id = 1;
1.367 djm 3550: c->remote_window = remote_window;
3551: c->remote_maxpacket = remote_maxpacket;
1.41 markus 3552: c->type = SSH_CHANNEL_OPEN;
1.358 djm 3553: if (c->open_confirm) {
1.403 djm 3554: debug2_f("channel %d: callback start", c->self);
1.367 djm 3555: c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
1.403 djm 3556: debug2_f("channel %d: callback done", c->self);
1.44 markus 3557: }
1.436 djm 3558: channel_set_used_time(ssh, c);
1.358 djm 3559: debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3560: c->remote_window, c->remote_maxpacket);
1.339 markus 3561: return 0;
1.1 deraadt 3562: }
3563:
1.127 itojun 3564: static char *
1.114 markus 3565: reason2txt(int reason)
3566: {
1.143 deraadt 3567: switch (reason) {
1.114 markus 3568: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3569: return "administratively prohibited";
3570: case SSH2_OPEN_CONNECT_FAILED:
3571: return "connect failed";
3572: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3573: return "unknown channel type";
3574: case SSH2_OPEN_RESOURCE_SHORTAGE:
3575: return "resource shortage";
3576: }
1.117 stevesk 3577: return "unknown reason";
1.114 markus 3578: }
3579:
1.339 markus 3580: int
1.363 markus 3581: channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
1.1 deraadt 3582: {
1.367 djm 3583: Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3584: u_int32_t reason;
3585: char *msg = NULL;
3586: int r;
1.25 markus 3587:
1.363 markus 3588: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3589: return 0;
3590: if (c->type != SSH_CHANNEL_OPENING)
1.389 djm 3591: ssh_packet_disconnect(ssh, "Received open failure for "
1.367 djm 3592: "non-opening channel %d.", c->self);
3593: if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
1.403 djm 3594: error_fr(r, "parse reason");
1.389 djm 3595: ssh_packet_disconnect(ssh, "Invalid open failure message");
1.367 djm 3596: }
1.378 djm 3597: /* skip language */
3598: if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
1.389 djm 3599: (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 ||
3600: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3601: error_fr(r, "parse msg/lang");
1.389 djm 3602: ssh_packet_disconnect(ssh, "Invalid open failure message");
1.358 djm 3603: }
1.367 djm 3604: logit("channel %d: open failed: %s%s%s", c->self,
1.358 djm 3605: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3606: free(msg);
3607: if (c->open_confirm) {
1.403 djm 3608: debug2_f("channel %d: callback start", c->self);
1.367 djm 3609: c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
1.403 djm 3610: debug2_f("channel %d: callback done", c->self);
1.44 markus 3611: }
1.291 djm 3612: /* Schedule the channel for cleanup/deletion. */
1.367 djm 3613: chan_mark_dead(ssh, c);
1.339 markus 3614: return 0;
1.44 markus 3615: }
3616:
1.339 markus 3617: int
1.363 markus 3618: channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
1.121 markus 3619: {
1.367 djm 3620: int id = channel_parse_id(ssh, __func__, "window adjust");
1.121 markus 3621: Channel *c;
1.367 djm 3622: u_int32_t adjust;
3623: u_int new_rwin;
3624: int r;
1.1 deraadt 3625:
1.367 djm 3626: if ((c = channel_lookup(ssh, id)) == NULL) {
1.229 markus 3627: logit("Received window adjust for non-open channel %d.", id);
1.339 markus 3628: return 0;
1.372 markus 3629: }
1.367 djm 3630:
1.363 markus 3631: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3632: return 0;
1.389 djm 3633: if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 ||
3634: (r = sshpkt_get_end(ssh)) != 0) {
1.403 djm 3635: error_fr(r, "parse adjust");
1.389 djm 3636: ssh_packet_disconnect(ssh, "Invalid window adjust message");
1.367 djm 3637: }
3638: debug2("channel %d: rcvd adjust %u", c->self, adjust);
3639: if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
1.346 djm 3640: fatal("channel %d: adjust %u overflows remote window %u",
1.367 djm 3641: c->self, adjust, c->remote_window);
3642: }
3643: c->remote_window = new_rwin;
1.339 markus 3644: return 0;
1.121 markus 3645: }
1.1 deraadt 3646:
1.339 markus 3647: int
1.363 markus 3648: channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
1.275 djm 3649: {
1.367 djm 3650: int id = channel_parse_id(ssh, __func__, "status confirm");
1.275 djm 3651: Channel *c;
3652: struct channel_confirm *cc;
3653:
3654: /* Reset keepalive timeout */
1.389 djm 3655: ssh_packet_set_alive_timeouts(ssh, 0);
1.275 djm 3656:
1.403 djm 3657: debug2_f("type %d id %d", type, id);
1.275 djm 3658:
1.367 djm 3659: if ((c = channel_lookup(ssh, id)) == NULL) {
1.403 djm 3660: logit_f("%d: unknown", id);
1.339 markus 3661: return 0;
1.367 djm 3662: }
1.363 markus 3663: if (channel_proxy_upstream(c, type, seq, ssh))
1.354 markus 3664: return 0;
1.394 dtucker 3665: if (sshpkt_get_end(ssh) != 0)
1.389 djm 3666: ssh_packet_disconnect(ssh, "Invalid status confirm message");
1.275 djm 3667: if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
1.339 markus 3668: return 0;
1.367 djm 3669: cc->cb(ssh, type, c, cc->ctx);
1.275 djm 3670: TAILQ_REMOVE(&c->status_confirms, cc, entry);
1.396 jsg 3671: freezero(cc, sizeof(*cc));
1.339 markus 3672: return 0;
1.275 djm 3673: }
1.121 markus 3674:
3675: /* -- tcp forwarding */
1.135 markus 3676:
3677: void
1.367 djm 3678: channel_set_af(struct ssh *ssh, int af)
1.135 markus 3679: {
1.367 djm 3680: ssh->chanctxt->IPv4or6 = af;
1.135 markus 3681: }
1.121 markus 3682:
1.312 djm 3683:
3684: /*
3685: * Determine whether or not a port forward listens to loopback, the
3686: * specified address or wildcard. On the client, a specified bind
3687: * address will always override gateway_ports. On the server, a
3688: * gateway_ports of 1 (``yes'') will override the client's specification
3689: * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3690: * will bind to whatever address the client asked for.
3691: *
3692: * Special-case listen_addrs are:
3693: *
3694: * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3695: * "" (empty string), "*" -> wildcard v4/v6
3696: * "localhost" -> loopback v4/v6
1.334 djm 3697: * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
1.312 djm 3698: */
3699: static const char *
1.389 djm 3700: channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp,
1.336 millert 3701: int is_client, struct ForwardOptions *fwd_opts)
1.312 djm 3702: {
3703: const char *addr = NULL;
3704: int wildcard = 0;
3705:
3706: if (listen_addr == NULL) {
3707: /* No address specified: default to gateway_ports setting */
1.336 millert 3708: if (fwd_opts->gateway_ports)
1.312 djm 3709: wildcard = 1;
1.336 millert 3710: } else if (fwd_opts->gateway_ports || is_client) {
1.404 djm 3711: if (((ssh->compat & SSH_OLD_FORWARD_ADDR) &&
1.312 djm 3712: strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3713: *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
1.336 millert 3714: (!is_client && fwd_opts->gateway_ports == 1)) {
1.312 djm 3715: wildcard = 1;
1.326 djm 3716: /*
3717: * Notify client if they requested a specific listen
3718: * address and it was overridden.
3719: */
3720: if (*listen_addr != '\0' &&
3721: strcmp(listen_addr, "0.0.0.0") != 0 &&
3722: strcmp(listen_addr, "*") != 0) {
1.389 djm 3723: ssh_packet_send_debug(ssh,
3724: "Forwarding listen address "
1.326 djm 3725: "\"%s\" overridden by server "
3726: "GatewayPorts", listen_addr);
3727: }
1.334 djm 3728: } else if (strcmp(listen_addr, "localhost") != 0 ||
3729: strcmp(listen_addr, "127.0.0.1") == 0 ||
3730: strcmp(listen_addr, "::1") == 0) {
1.395 djm 3731: /*
3732: * Accept explicit localhost address when
3733: * GatewayPorts=yes. The "localhost" hostname is
3734: * deliberately skipped here so it will listen on all
3735: * available local address families.
3736: */
1.334 djm 3737: addr = listen_addr;
1.326 djm 3738: }
1.334 djm 3739: } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3740: strcmp(listen_addr, "::1") == 0) {
3741: /*
3742: * If a specific IPv4/IPv6 localhost address has been
3743: * requested then accept it even if gateway_ports is in
3744: * effect. This allows the client to prefer IPv4 or IPv6.
3745: */
3746: addr = listen_addr;
1.312 djm 3747: }
3748: if (wildcardp != NULL)
3749: *wildcardp = wildcard;
3750: return addr;
3751: }
3752:
1.160 markus 3753: static int
1.367 djm 3754: channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3755: struct Forward *fwd, int *allocated_listen_port,
3756: struct ForwardOptions *fwd_opts)
1.25 markus 3757: {
1.113 markus 3758: Channel *c;
1.226 djm 3759: int sock, r, success = 0, wildcard = 0, is_client;
1.35 markus 3760: struct addrinfo hints, *ai, *aitop;
1.212 djm 3761: const char *host, *addr;
1.35 markus 3762: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.295 djm 3763: in_port_t *lport_p;
1.25 markus 3764:
1.212 djm 3765: is_client = (type == SSH_CHANNEL_PORT_LISTENER);
1.87 markus 3766:
1.344 millert 3767: if (is_client && fwd->connect_path != NULL) {
3768: host = fwd->connect_path;
3769: } else {
3770: host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3771: fwd->listen_host : fwd->connect_host;
3772: if (host == NULL) {
3773: error("No forward host name.");
3774: return 0;
3775: }
3776: if (strlen(host) >= NI_MAXHOST) {
3777: error("Forward host name too long.");
3778: return 0;
3779: }
1.87 markus 3780: }
1.25 markus 3781:
1.312 djm 3782: /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
1.389 djm 3783: addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard,
1.336 millert 3784: is_client, fwd_opts);
1.403 djm 3785: debug3_f("type %d wildcard %d addr %s", type, wildcard,
3786: (addr == NULL) ? "NULL" : addr);
1.212 djm 3787:
3788: /*
1.35 markus 3789: * getaddrinfo returns a loopback address if the hostname is
3790: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 3791: */
1.35 markus 3792: memset(&hints, 0, sizeof(hints));
1.367 djm 3793: hints.ai_family = ssh->chanctxt->IPv4or6;
1.212 djm 3794: hints.ai_flags = wildcard ? AI_PASSIVE : 0;
1.35 markus 3795: hints.ai_socktype = SOCK_STREAM;
1.336 millert 3796: snprintf(strport, sizeof strport, "%d", fwd->listen_port);
1.212 djm 3797: if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3798: if (addr == NULL) {
3799: /* This really shouldn't happen */
1.389 djm 3800: ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s",
1.271 dtucker 3801: ssh_gai_strerror(r));
1.212 djm 3802: } else {
1.403 djm 3803: error_f("getaddrinfo(%.64s): %s", addr,
1.271 dtucker 3804: ssh_gai_strerror(r));
1.212 djm 3805: }
1.218 markus 3806: return 0;
1.212 djm 3807: }
1.295 djm 3808: if (allocated_listen_port != NULL)
3809: *allocated_listen_port = 0;
1.35 markus 3810: for (ai = aitop; ai; ai = ai->ai_next) {
1.295 djm 3811: switch (ai->ai_family) {
3812: case AF_INET:
3813: lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3814: sin_port;
3815: break;
3816: case AF_INET6:
3817: lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3818: sin6_port;
3819: break;
3820: default:
1.35 markus 3821: continue;
1.295 djm 3822: }
3823: /*
3824: * If allocating a port for -R forwards, then use the
3825: * same port for all address families.
3826: */
1.367 djm 3827: if (type == SSH_CHANNEL_RPORT_LISTENER &&
3828: fwd->listen_port == 0 && allocated_listen_port != NULL &&
3829: *allocated_listen_port > 0)
1.295 djm 3830: *lport_p = htons(*allocated_listen_port);
3831:
1.35 markus 3832: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1.367 djm 3833: strport, sizeof(strport),
3834: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.403 djm 3835: error_f("getnameinfo failed");
1.35 markus 3836: continue;
3837: }
3838: /* Create a port to listen for the host. */
1.300 dtucker 3839: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.393 deraadt 3840: if (sock == -1) {
1.35 markus 3841: /* this is no error since kernel may not support ipv6 */
1.377 djm 3842: verbose("socket [%s]:%s: %.100s", ntop, strport,
3843: strerror(errno));
1.35 markus 3844: continue;
3845: }
1.226 djm 3846:
1.376 djm 3847: set_reuseaddr(sock);
1.182 stevesk 3848:
1.295 djm 3849: debug("Local forwarding listening on %s port %s.",
3850: ntop, strport);
1.35 markus 3851:
3852: /* Bind the socket to the address. */
1.393 deraadt 3853: if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.367 djm 3854: /*
3855: * address can be in if use ipv6 address is
3856: * already bound
3857: */
1.377 djm 3858: verbose("bind [%s]:%s: %.100s",
3859: ntop, strport, strerror(errno));
1.35 markus 3860: close(sock);
3861: continue;
3862: }
3863: /* Start listening for connections on the socket. */
1.393 deraadt 3864: if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
1.377 djm 3865: error("listen [%s]:%s: %.100s", ntop, strport,
3866: strerror(errno));
1.35 markus 3867: close(sock);
3868: continue;
3869: }
1.295 djm 3870:
3871: /*
1.336 millert 3872: * fwd->listen_port == 0 requests a dynamically allocated port -
1.295 djm 3873: * record what we got.
3874: */
1.367 djm 3875: if (type == SSH_CHANNEL_RPORT_LISTENER &&
3876: fwd->listen_port == 0 &&
1.295 djm 3877: allocated_listen_port != NULL &&
3878: *allocated_listen_port == 0) {
1.350 djm 3879: *allocated_listen_port = get_local_port(sock);
1.295 djm 3880: debug("Allocated listen port %d",
3881: *allocated_listen_port);
3882: }
3883:
1.35 markus 3884: /* Allocate a channel number for the socket. */
1.424 djm 3885: c = channel_new(ssh, "port-listener", type, sock, sock, -1,
1.51 markus 3886: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.190 markus 3887: 0, "port listener", 1);
1.293 djm 3888: c->path = xstrdup(host);
1.336 millert 3889: c->host_port = fwd->connect_port;
1.312 djm 3890: c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
1.336 millert 3891: if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
1.404 djm 3892: !(ssh->compat & SSH_BUG_DYNAMIC_RPORT))
1.315 markus 3893: c->listening_port = *allocated_listen_port;
3894: else
1.336 millert 3895: c->listening_port = fwd->listen_port;
1.35 markus 3896: success = 1;
3897: }
3898: if (success == 0)
1.403 djm 3899: error_f("cannot listen to port: %d", fwd->listen_port);
1.35 markus 3900: freeaddrinfo(aitop);
1.87 markus 3901: return success;
1.25 markus 3902: }
1.1 deraadt 3903:
1.336 millert 3904: static int
1.367 djm 3905: channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3906: struct Forward *fwd, struct ForwardOptions *fwd_opts)
1.336 millert 3907: {
3908: struct sockaddr_un sunaddr;
3909: const char *path;
3910: Channel *c;
3911: int port, sock;
3912: mode_t omask;
3913:
3914: switch (type) {
3915: case SSH_CHANNEL_UNIX_LISTENER:
3916: if (fwd->connect_path != NULL) {
3917: if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3918: error("Local connecting path too long: %s",
3919: fwd->connect_path);
3920: return 0;
3921: }
3922: path = fwd->connect_path;
3923: port = PORT_STREAMLOCAL;
3924: } else {
3925: if (fwd->connect_host == NULL) {
3926: error("No forward host name.");
3927: return 0;
3928: }
3929: if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3930: error("Forward host name too long.");
3931: return 0;
3932: }
3933: path = fwd->connect_host;
3934: port = fwd->connect_port;
3935: }
3936: break;
3937: case SSH_CHANNEL_RUNIX_LISTENER:
3938: path = fwd->listen_path;
3939: port = PORT_STREAMLOCAL;
3940: break;
3941: default:
1.403 djm 3942: error_f("unexpected channel type %d", type);
1.336 millert 3943: return 0;
3944: }
3945:
3946: if (fwd->listen_path == NULL) {
3947: error("No forward path name.");
3948: return 0;
3949: }
3950: if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3951: error("Local listening path too long: %s", fwd->listen_path);
3952: return 0;
3953: }
3954:
1.403 djm 3955: debug3_f("type %d path %s", type, fwd->listen_path);
1.336 millert 3956:
3957: /* Start a Unix domain listener. */
3958: omask = umask(fwd_opts->streamlocal_bind_mask);
3959: sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3960: fwd_opts->streamlocal_bind_unlink);
3961: umask(omask);
3962: if (sock < 0)
3963: return 0;
3964:
3965: debug("Local forwarding listening on path %s.", fwd->listen_path);
3966:
3967: /* Allocate a channel number for the socket. */
1.424 djm 3968: c = channel_new(ssh, "unix-listener", type, sock, sock, -1,
1.336 millert 3969: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3970: 0, "unix listener", 1);
3971: c->path = xstrdup(path);
3972: c->host_port = port;
3973: c->listening_port = PORT_STREAMLOCAL;
3974: c->listening_addr = xstrdup(fwd->listen_path);
3975: return 1;
3976: }
3977:
3978: static int
1.367 djm 3979: channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3980: const char *host, u_short port)
1.202 djm 3981: {
1.209 avsm 3982: u_int i;
3983: int found = 0;
1.202 djm 3984:
1.367 djm 3985: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3986: Channel *c = ssh->chanctxt->channels[i];
1.312 djm 3987: if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3988: continue;
3989: if (strcmp(c->path, host) == 0 && c->listening_port == port) {
1.403 djm 3990: debug2_f("close channel %d", i);
1.367 djm 3991: channel_free(ssh, c);
1.312 djm 3992: found = 1;
3993: }
3994: }
3995:
1.367 djm 3996: return found;
1.312 djm 3997: }
3998:
1.336 millert 3999: static int
1.367 djm 4000: channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 4001: {
4002: u_int i;
4003: int found = 0;
4004:
1.367 djm 4005: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4006: Channel *c = ssh->chanctxt->channels[i];
1.336 millert 4007: if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
4008: continue;
4009: if (c->path == NULL)
4010: continue;
4011: if (strcmp(c->path, path) == 0) {
1.403 djm 4012: debug2_f("close channel %d", i);
1.367 djm 4013: channel_free(ssh, c);
1.336 millert 4014: found = 1;
4015: }
4016: }
4017:
1.367 djm 4018: return found;
1.336 millert 4019: }
4020:
1.312 djm 4021: int
1.367 djm 4022: channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
1.336 millert 4023: {
1.367 djm 4024: if (fwd->listen_path != NULL) {
4025: return channel_cancel_rport_listener_streamlocal(ssh,
4026: fwd->listen_path);
4027: } else {
4028: return channel_cancel_rport_listener_tcpip(ssh,
4029: fwd->listen_host, fwd->listen_port);
4030: }
1.336 millert 4031: }
4032:
4033: static int
1.367 djm 4034: channel_cancel_lport_listener_tcpip(struct ssh *ssh,
4035: const char *lhost, u_short lport, int cport,
4036: struct ForwardOptions *fwd_opts)
1.312 djm 4037: {
4038: u_int i;
4039: int found = 0;
1.389 djm 4040: const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts);
1.202 djm 4041:
1.367 djm 4042: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4043: Channel *c = ssh->chanctxt->channels[i];
1.312 djm 4044: if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
4045: continue;
1.313 markus 4046: if (c->listening_port != lport)
1.312 djm 4047: continue;
1.313 markus 4048: if (cport == CHANNEL_CANCEL_PORT_STATIC) {
4049: /* skip dynamic forwardings */
4050: if (c->host_port == 0)
4051: continue;
4052: } else {
4053: if (c->host_port != cport)
4054: continue;
4055: }
1.312 djm 4056: if ((c->listening_addr == NULL && addr != NULL) ||
4057: (c->listening_addr != NULL && addr == NULL))
4058: continue;
4059: if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
1.403 djm 4060: debug2_f("close channel %d", i);
1.367 djm 4061: channel_free(ssh, c);
1.202 djm 4062: found = 1;
4063: }
4064: }
4065:
1.367 djm 4066: return found;
1.202 djm 4067: }
4068:
1.336 millert 4069: static int
1.367 djm 4070: channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 4071: {
4072: u_int i;
4073: int found = 0;
4074:
4075: if (path == NULL) {
1.403 djm 4076: error_f("no path specified.");
1.336 millert 4077: return 0;
4078: }
4079:
1.367 djm 4080: for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4081: Channel *c = ssh->chanctxt->channels[i];
1.336 millert 4082: if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
4083: continue;
4084: if (c->listening_addr == NULL)
4085: continue;
4086: if (strcmp(c->listening_addr, path) == 0) {
1.403 djm 4087: debug2_f("close channel %d", i);
1.367 djm 4088: channel_free(ssh, c);
1.336 millert 4089: found = 1;
4090: }
4091: }
4092:
1.367 djm 4093: return found;
1.336 millert 4094: }
4095:
4096: int
1.367 djm 4097: channel_cancel_lport_listener(struct ssh *ssh,
4098: struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
1.336 millert 4099: {
1.367 djm 4100: if (fwd->listen_path != NULL) {
4101: return channel_cancel_lport_listener_streamlocal(ssh,
4102: fwd->listen_path);
4103: } else {
4104: return channel_cancel_lport_listener_tcpip(ssh,
4105: fwd->listen_host, fwd->listen_port, cport, fwd_opts);
4106: }
1.336 millert 4107: }
4108:
1.362 markus 4109: /* protocol local port fwd, used by ssh */
1.160 markus 4110: int
1.367 djm 4111: channel_setup_local_fwd_listener(struct ssh *ssh,
4112: struct Forward *fwd, struct ForwardOptions *fwd_opts)
1.160 markus 4113: {
1.336 millert 4114: if (fwd->listen_path != NULL) {
1.367 djm 4115: return channel_setup_fwd_listener_streamlocal(ssh,
1.336 millert 4116: SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
4117: } else {
1.367 djm 4118: return channel_setup_fwd_listener_tcpip(ssh,
4119: SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
1.336 millert 4120: }
1.160 markus 4121: }
4122:
1.381 djm 4123: /* Matches a remote forwarding permission against a requested forwarding */
4124: static int
4125: remote_open_match(struct permission *allowed_open, struct Forward *fwd)
4126: {
4127: int ret;
4128: char *lhost;
4129:
4130: /* XXX add ACLs for streamlocal */
4131: if (fwd->listen_path != NULL)
4132: return 1;
4133:
4134: if (fwd->listen_host == NULL || allowed_open->listen_host == NULL)
4135: return 0;
4136:
4137: if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT &&
4138: allowed_open->listen_port != fwd->listen_port)
4139: return 0;
4140:
4141: /* Match hostnames case-insensitively */
4142: lhost = xstrdup(fwd->listen_host);
4143: lowercase(lhost);
4144: ret = match_pattern(lhost, allowed_open->listen_host);
4145: free(lhost);
4146:
4147: return ret;
4148: }
4149:
4150: /* Checks whether a requested remote forwarding is permitted */
4151: static int
4152: check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
4153: {
4154: struct ssh_channels *sc = ssh->chanctxt;
4155: struct permission_set *pset = &sc->remote_perms;
4156: u_int i, permit, permit_adm = 1;
4157: struct permission *perm;
4158:
4159: /* XXX apply GatewayPorts override before checking? */
4160:
4161: permit = pset->all_permitted;
4162: if (!permit) {
4163: for (i = 0; i < pset->num_permitted_user; i++) {
4164: perm = &pset->permitted_user[i];
4165: if (remote_open_match(perm, fwd)) {
4166: permit = 1;
4167: break;
4168: }
4169: }
4170: }
4171:
4172: if (pset->num_permitted_admin > 0) {
4173: permit_adm = 0;
4174: for (i = 0; i < pset->num_permitted_admin; i++) {
4175: perm = &pset->permitted_admin[i];
4176: if (remote_open_match(perm, fwd)) {
4177: permit_adm = 1;
4178: break;
4179: }
4180: }
4181: }
4182:
4183: return permit && permit_adm;
4184: }
4185:
1.160 markus 4186: /* protocol v2 remote port fwd, used by sshd */
4187: int
1.367 djm 4188: channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
1.336 millert 4189: int *allocated_listen_port, struct ForwardOptions *fwd_opts)
1.160 markus 4190: {
1.381 djm 4191: if (!check_rfwd_permission(ssh, fwd)) {
1.389 djm 4192: ssh_packet_send_debug(ssh, "port forwarding refused");
1.391 florian 4193: if (fwd->listen_path != NULL)
4194: /* XXX always allowed, see remote_open_match() */
4195: logit("Received request from %.100s port %d to "
4196: "remote forward to path \"%.100s\", "
4197: "but the request was denied.",
4198: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4199: fwd->listen_path);
4200: else if(fwd->listen_host != NULL)
4201: logit("Received request from %.100s port %d to "
4202: "remote forward to host %.100s port %d, "
4203: "but the request was denied.",
4204: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4205: fwd->listen_host, fwd->listen_port );
4206: else
4207: logit("Received request from %.100s port %d to remote "
4208: "forward, but the request was denied.",
4209: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.381 djm 4210: return 0;
4211: }
1.336 millert 4212: if (fwd->listen_path != NULL) {
1.367 djm 4213: return channel_setup_fwd_listener_streamlocal(ssh,
1.336 millert 4214: SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
4215: } else {
1.367 djm 4216: return channel_setup_fwd_listener_tcpip(ssh,
1.336 millert 4217: SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
4218: fwd_opts);
4219: }
1.160 markus 4220: }
4221:
1.27 markus 4222: /*
1.312 djm 4223: * Translate the requested rfwd listen host to something usable for
4224: * this server.
4225: */
4226: static const char *
4227: channel_rfwd_bind_host(const char *listen_host)
4228: {
4229: if (listen_host == NULL) {
1.378 djm 4230: return "localhost";
1.312 djm 4231: } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
1.378 djm 4232: return "";
1.312 djm 4233: } else
4234: return listen_host;
4235: }
4236:
4237: /*
1.27 markus 4238: * Initiate forwarding of connections to port "port" on remote host through
4239: * the secure channel to host:port from local side.
1.315 markus 4240: * Returns handle (index) for updating the dynamic listen port with
1.381 djm 4241: * channel_update_permission().
1.27 markus 4242: */
1.253 markus 4243: int
1.367 djm 4244: channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
1.25 markus 4245: {
1.367 djm 4246: int r, success = 0, idx = -1;
1.421 mbuhl 4247: const char *host_to_connect, *listen_host, *listen_path;
1.367 djm 4248: int port_to_connect, listen_port;
1.73 markus 4249:
1.25 markus 4250: /* Send the forward request to the remote side. */
1.358 djm 4251: if (fwd->listen_path != NULL) {
1.367 djm 4252: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4253: (r = sshpkt_put_cstring(ssh,
4254: "streamlocal-forward@openssh.com")) != 0 ||
4255: (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4256: (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
4257: (r = sshpkt_send(ssh)) != 0 ||
4258: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 4259: fatal_fr(r, "request streamlocal");
1.336 millert 4260: } else {
1.367 djm 4261: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4262: (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
4263: (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4264: (r = sshpkt_put_cstring(ssh,
4265: channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
4266: (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
4267: (r = sshpkt_send(ssh)) != 0 ||
4268: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 4269: fatal_fr(r, "request tcpip-forward");
1.73 markus 4270: }
1.358 djm 4271: /* Assume that server accepts the request */
4272: success = 1;
1.73 markus 4273: if (success) {
1.305 djm 4274: /* Record that connection to this host/port is permitted. */
1.367 djm 4275: host_to_connect = listen_host = listen_path = NULL;
4276: port_to_connect = listen_port = 0;
1.336 millert 4277: if (fwd->connect_path != NULL) {
1.421 mbuhl 4278: host_to_connect = fwd->connect_path;
1.367 djm 4279: port_to_connect = PORT_STREAMLOCAL;
1.336 millert 4280: } else {
1.421 mbuhl 4281: host_to_connect = fwd->connect_host;
1.367 djm 4282: port_to_connect = fwd->connect_port;
1.336 millert 4283: }
4284: if (fwd->listen_path != NULL) {
1.421 mbuhl 4285: listen_path = fwd->listen_path;
1.367 djm 4286: listen_port = PORT_STREAMLOCAL;
1.336 millert 4287: } else {
1.421 mbuhl 4288: listen_host = fwd->listen_host;
1.367 djm 4289: listen_port = fwd->listen_port;
4290: }
1.381 djm 4291: idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
1.367 djm 4292: host_to_connect, port_to_connect,
4293: listen_host, listen_path, listen_port, NULL);
1.44 markus 4294: }
1.367 djm 4295: return idx;
1.1 deraadt 4296: }
4297:
1.333 markus 4298: static int
1.381 djm 4299: open_match(struct permission *allowed_open, const char *requestedhost,
1.336 millert 4300: int requestedport)
1.333 markus 4301: {
4302: if (allowed_open->host_to_connect == NULL)
4303: return 0;
4304: if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
4305: allowed_open->port_to_connect != requestedport)
4306: return 0;
1.351 dtucker 4307: if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
4308: strcmp(allowed_open->host_to_connect, requestedhost) != 0)
1.333 markus 4309: return 0;
4310: return 1;
4311: }
4312:
4313: /*
1.336 millert 4314: * Note that in the listen host/port case
1.333 markus 4315: * we don't support FWD_PERMIT_ANY_PORT and
4316: * need to translate between the configured-host (listen_host)
4317: * and what we've sent to the remote server (channel_rfwd_bind_host)
4318: */
4319: static int
1.381 djm 4320: open_listen_match_tcpip(struct permission *allowed_open,
1.336 millert 4321: const char *requestedhost, u_short requestedport, int translate)
1.333 markus 4322: {
4323: const char *allowed_host;
4324:
4325: if (allowed_open->host_to_connect == NULL)
4326: return 0;
4327: if (allowed_open->listen_port != requestedport)
4328: return 0;
1.335 djm 4329: if (!translate && allowed_open->listen_host == NULL &&
4330: requestedhost == NULL)
4331: return 1;
1.333 markus 4332: allowed_host = translate ?
4333: channel_rfwd_bind_host(allowed_open->listen_host) :
4334: allowed_open->listen_host;
1.382 djm 4335: if (allowed_host == NULL || requestedhost == NULL ||
1.333 markus 4336: strcmp(allowed_host, requestedhost) != 0)
4337: return 0;
4338: return 1;
4339: }
4340:
1.336 millert 4341: static int
1.381 djm 4342: open_listen_match_streamlocal(struct permission *allowed_open,
1.336 millert 4343: const char *requestedpath)
4344: {
4345: if (allowed_open->host_to_connect == NULL)
4346: return 0;
4347: if (allowed_open->listen_port != PORT_STREAMLOCAL)
4348: return 0;
4349: if (allowed_open->listen_path == NULL ||
4350: strcmp(allowed_open->listen_path, requestedpath) != 0)
4351: return 0;
4352: return 1;
4353: }
4354:
1.27 markus 4355: /*
1.208 deraadt 4356: * Request cancellation of remote forwarding of connection host:port from
1.202 djm 4357: * local side.
4358: */
1.336 millert 4359: static int
1.367 djm 4360: channel_request_rforward_cancel_tcpip(struct ssh *ssh,
4361: const char *host, u_short port)
1.202 djm 4362: {
1.367 djm 4363: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4364: struct permission_set *pset = &sc->local_perms;
1.367 djm 4365: int r;
4366: u_int i;
1.397 markus 4367: struct permission *perm = NULL;
1.202 djm 4368:
1.381 djm 4369: for (i = 0; i < pset->num_permitted_user; i++) {
4370: perm = &pset->permitted_user[i];
4371: if (open_listen_match_tcpip(perm, host, port, 0))
1.202 djm 4372: break;
1.381 djm 4373: perm = NULL;
1.202 djm 4374: }
1.381 djm 4375: if (perm == NULL) {
1.403 djm 4376: debug_f("requested forward not found");
1.312 djm 4377: return -1;
1.202 djm 4378: }
1.367 djm 4379: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4380: (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
4381: (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4382: (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
4383: (r = sshpkt_put_u32(ssh, port)) != 0 ||
4384: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4385: fatal_fr(r, "send cancel");
1.367 djm 4386:
1.381 djm 4387: fwd_perm_clear(perm); /* unregister */
1.336 millert 4388:
4389: return 0;
4390: }
4391:
4392: /*
4393: * Request cancellation of remote forwarding of Unix domain socket
4394: * path from local side.
4395: */
4396: static int
1.367 djm 4397: channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
1.336 millert 4398: {
1.367 djm 4399: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4400: struct permission_set *pset = &sc->local_perms;
1.367 djm 4401: int r;
4402: u_int i;
1.397 markus 4403: struct permission *perm = NULL;
1.336 millert 4404:
1.381 djm 4405: for (i = 0; i < pset->num_permitted_user; i++) {
4406: perm = &pset->permitted_user[i];
4407: if (open_listen_match_streamlocal(perm, path))
1.336 millert 4408: break;
1.381 djm 4409: perm = NULL;
1.336 millert 4410: }
1.381 djm 4411: if (perm == NULL) {
1.403 djm 4412: debug_f("requested forward not found");
1.336 millert 4413: return -1;
4414: }
1.367 djm 4415: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4416: (r = sshpkt_put_cstring(ssh,
4417: "cancel-streamlocal-forward@openssh.com")) != 0 ||
4418: (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4419: (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4420: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4421: fatal_fr(r, "send cancel");
1.367 djm 4422:
1.381 djm 4423: fwd_perm_clear(perm); /* unregister */
1.312 djm 4424:
4425: return 0;
1.202 djm 4426: }
4427:
4428: /*
1.336 millert 4429: * Request cancellation of remote forwarding of a connection from local side.
4430: */
4431: int
1.367 djm 4432: channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
1.336 millert 4433: {
4434: if (fwd->listen_path != NULL) {
1.367 djm 4435: return channel_request_rforward_cancel_streamlocal(ssh,
4436: fwd->listen_path);
1.336 millert 4437: } else {
1.367 djm 4438: return channel_request_rforward_cancel_tcpip(ssh,
4439: fwd->listen_host,
4440: fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
1.336 millert 4441: }
1.1 deraadt 4442: }
4443:
1.99 markus 4444: /*
1.381 djm 4445: * Permits opening to any host/port if permitted_user[] is empty. This is
1.99 markus 4446: * usually called by the server, because the user could connect to any port
4447: * anyway, and the server has no way to know but to trust the client anyway.
4448: */
4449: void
1.381 djm 4450: channel_permit_all(struct ssh *ssh, int where)
4451: {
4452: struct permission_set *pset = permission_set_get(ssh, where);
4453:
4454: if (pset->num_permitted_user == 0)
4455: pset->all_permitted = 1;
4456: }
4457:
4458: /*
4459: * Permit the specified host/port for forwarding.
4460: */
4461: void
4462: channel_add_permission(struct ssh *ssh, int who, int where,
4463: char *host, int port)
4464: {
4465: int local = where == FORWARD_LOCAL;
4466: struct permission_set *pset = permission_set_get(ssh, where);
4467:
4468: debug("allow %s forwarding to host %s port %d",
4469: fwd_ident(who, where), host, port);
4470: /*
4471: * Remote forwards set listen_host/port, local forwards set
4472: * host/port_to_connect.
4473: */
4474: permission_set_add(ssh, who, where,
4475: local ? host : 0, local ? port : 0,
4476: local ? NULL : host, NULL, local ? 0 : port, NULL);
4477: pset->all_permitted = 0;
4478: }
4479:
4480: /*
4481: * Administratively disable forwarding.
4482: */
4483: void
4484: channel_disable_admin(struct ssh *ssh, int where)
1.99 markus 4485: {
1.381 djm 4486: channel_clear_permission(ssh, FORWARD_ADM, where);
4487: permission_set_add(ssh, FORWARD_ADM, where,
4488: NULL, 0, NULL, NULL, 0, NULL);
1.99 markus 4489: }
4490:
1.381 djm 4491: /*
4492: * Clear a list of permitted opens.
4493: */
1.101 markus 4494: void
1.381 djm 4495: channel_clear_permission(struct ssh *ssh, int who, int where)
1.99 markus 4496: {
1.381 djm 4497: struct permission **permp;
4498: u_int *npermp;
1.367 djm 4499:
1.381 djm 4500: permission_set_get_array(ssh, who, where, &permp, &npermp);
4501: *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp));
4502: *npermp = 0;
1.315 markus 4503: }
4504:
4505: /*
4506: * Update the listen port for a dynamic remote forward, after
4507: * the actual 'newport' has been allocated. If 'newport' < 0 is
4508: * passed then they entry will be invalidated.
4509: */
4510: void
1.381 djm 4511: channel_update_permission(struct ssh *ssh, int idx, int newport)
1.315 markus 4512: {
1.381 djm 4513: struct permission_set *pset = &ssh->chanctxt->local_perms;
1.367 djm 4514:
1.381 djm 4515: if (idx < 0 || (u_int)idx >= pset->num_permitted_user) {
1.403 djm 4516: debug_f("index out of range: %d num_permitted_user %d",
4517: idx, pset->num_permitted_user);
1.315 markus 4518: return;
4519: }
4520: debug("%s allowed port %d for forwarding to host %s port %d",
4521: newport > 0 ? "Updating" : "Removing",
4522: newport,
1.381 djm 4523: pset->permitted_user[idx].host_to_connect,
4524: pset->permitted_user[idx].port_to_connect);
1.367 djm 4525: if (newport <= 0)
1.381 djm 4526: fwd_perm_clear(&pset->permitted_user[idx]);
1.367 djm 4527: else {
1.381 djm 4528: pset->permitted_user[idx].listen_port =
1.404 djm 4529: (ssh->compat & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
1.315 markus 4530: }
1.99 markus 4531: }
4532:
1.314 dtucker 4533: /* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
4534: int
4535: permitopen_port(const char *p)
4536: {
4537: int port;
4538:
4539: if (strcmp(p, "*") == 0)
4540: return FWD_PERMIT_ANY_PORT;
4541: if ((port = a2port(p)) > 0)
4542: return port;
4543: return -1;
4544: }
4545:
1.276 djm 4546: /* Try to start non-blocking connect to next host in cctx list */
1.127 itojun 4547: static int
1.276 djm 4548: connect_next(struct channel_connect *cctx)
1.41 markus 4549: {
1.276 djm 4550: int sock, saved_errno;
1.336 millert 4551: struct sockaddr_un *sunaddr;
1.367 djm 4552: char ntop[NI_MAXHOST];
4553: char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
1.41 markus 4554:
1.276 djm 4555: for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
1.336 millert 4556: switch (cctx->ai->ai_family) {
4557: case AF_UNIX:
4558: /* unix:pathname instead of host:port */
4559: sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4560: strlcpy(ntop, "unix", sizeof(ntop));
4561: strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4562: break;
4563: case AF_INET:
4564: case AF_INET6:
4565: if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4566: ntop, sizeof(ntop), strport, sizeof(strport),
4567: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.420 djm 4568: error_f("getnameinfo failed");
1.336 millert 4569: continue;
4570: }
4571: break;
4572: default:
1.41 markus 4573: continue;
4574: }
1.420 djm 4575: debug_f("start for host %.100s ([%.100s]:%s)",
4576: cctx->host, ntop, strport);
1.300 dtucker 4577: if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4578: cctx->ai->ai_protocol)) == -1) {
1.276 djm 4579: if (cctx->ai->ai_next == NULL)
1.186 djm 4580: error("socket: %.100s", strerror(errno));
4581: else
4582: verbose("socket: %.100s", strerror(errno));
1.41 markus 4583: continue;
4584: }
1.205 djm 4585: if (set_nonblock(sock) == -1)
1.403 djm 4586: fatal_f("set_nonblock(%d)", sock);
1.276 djm 4587: if (connect(sock, cctx->ai->ai_addr,
4588: cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
1.420 djm 4589: debug_f("host %.100s ([%.100s]:%s): %.100s",
4590: cctx->host, ntop, strport, strerror(errno));
1.276 djm 4591: saved_errno = errno;
1.41 markus 4592: close(sock);
1.276 djm 4593: errno = saved_errno;
1.89 stevesk 4594: continue; /* fail -- try next */
1.41 markus 4595: }
1.336 millert 4596: if (cctx->ai->ai_family != AF_UNIX)
4597: set_nodelay(sock);
1.420 djm 4598: debug_f("connect host %.100s ([%.100s]:%s) in progress, fd=%d",
4599: cctx->host, ntop, strport, sock);
1.276 djm 4600: cctx->ai = cctx->ai->ai_next;
4601: return sock;
4602: }
4603: return -1;
4604: }
1.41 markus 4605:
1.276 djm 4606: static void
4607: channel_connect_ctx_free(struct channel_connect *cctx)
4608: {
1.321 djm 4609: free(cctx->host);
1.336 millert 4610: if (cctx->aitop) {
4611: if (cctx->aitop->ai_family == AF_UNIX)
4612: free(cctx->aitop);
4613: else
4614: freeaddrinfo(cctx->aitop);
4615: }
1.329 tedu 4616: memset(cctx, 0, sizeof(*cctx));
1.276 djm 4617: }
4618:
1.357 dtucker 4619: /*
1.372 markus 4620: * Return connecting socket to remote host:port or local socket path,
1.357 dtucker 4621: * passing back the failure reason if appropriate.
4622: */
1.372 markus 4623: static int
4624: connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4625: char *ctype, char *rname, struct channel_connect *cctx,
4626: int *reason, const char **errmsg)
1.276 djm 4627: {
4628: struct addrinfo hints;
4629: int gaierr;
4630: int sock = -1;
4631: char strport[NI_MAXSERV];
1.336 millert 4632:
4633: if (port == PORT_STREAMLOCAL) {
4634: struct sockaddr_un *sunaddr;
4635: struct addrinfo *ai;
4636:
4637: if (strlen(name) > sizeof(sunaddr->sun_path)) {
4638: error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
1.372 markus 4639: return -1;
1.336 millert 4640: }
4641:
4642: /*
4643: * Fake up a struct addrinfo for AF_UNIX connections.
4644: * channel_connect_ctx_free() must check ai_family
4645: * and use free() not freeaddirinfo() for AF_UNIX.
4646: */
4647: ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4648: memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4649: ai->ai_addr = (struct sockaddr *)(ai + 1);
4650: ai->ai_addrlen = sizeof(*sunaddr);
4651: ai->ai_family = AF_UNIX;
1.372 markus 4652: ai->ai_socktype = socktype;
1.336 millert 4653: ai->ai_protocol = PF_UNSPEC;
4654: sunaddr = (struct sockaddr_un *)ai->ai_addr;
4655: sunaddr->sun_family = AF_UNIX;
4656: strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
1.372 markus 4657: cctx->aitop = ai;
1.336 millert 4658: } else {
4659: memset(&hints, 0, sizeof(hints));
1.367 djm 4660: hints.ai_family = ssh->chanctxt->IPv4or6;
1.372 markus 4661: hints.ai_socktype = socktype;
1.336 millert 4662: snprintf(strport, sizeof strport, "%d", port);
1.372 markus 4663: if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
1.357 dtucker 4664: != 0) {
4665: if (errmsg != NULL)
4666: *errmsg = ssh_gai_strerror(gaierr);
4667: if (reason != NULL)
4668: *reason = SSH2_OPEN_CONNECT_FAILED;
1.336 millert 4669: error("connect_to %.100s: unknown host (%s)", name,
4670: ssh_gai_strerror(gaierr));
1.372 markus 4671: return -1;
1.336 millert 4672: }
1.41 markus 4673: }
1.276 djm 4674:
1.372 markus 4675: cctx->host = xstrdup(name);
4676: cctx->port = port;
4677: cctx->ai = cctx->aitop;
1.276 djm 4678:
1.372 markus 4679: if ((sock = connect_next(cctx)) == -1) {
1.276 djm 4680: error("connect to %.100s port %d failed: %s",
1.336 millert 4681: name, port, strerror(errno));
1.372 markus 4682: return -1;
4683: }
4684:
4685: return sock;
4686: }
4687:
4688: /* Return CONNECTING channel to remote host:port or local socket path */
4689: static Channel *
4690: connect_to(struct ssh *ssh, const char *host, int port,
4691: char *ctype, char *rname)
4692: {
4693: struct channel_connect cctx;
4694: Channel *c;
4695: int sock;
4696:
4697: memset(&cctx, 0, sizeof(cctx));
4698: sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4699: &cctx, NULL, NULL);
4700: if (sock == -1) {
1.276 djm 4701: channel_connect_ctx_free(&cctx);
4702: return NULL;
1.41 markus 4703: }
1.367 djm 4704: c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
1.276 djm 4705: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
1.372 markus 4706: c->host_port = port;
4707: c->path = xstrdup(host);
1.276 djm 4708: c->connect_ctx = cctx;
1.372 markus 4709:
1.276 djm 4710: return c;
1.41 markus 4711: }
1.99 markus 4712:
1.354 markus 4713: /*
4714: * returns either the newly connected channel or the downstream channel
4715: * that needs to deal with this connection.
4716: */
1.276 djm 4717: Channel *
1.367 djm 4718: channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
1.333 markus 4719: u_short listen_port, char *ctype, char *rname)
1.73 markus 4720: {
1.367 djm 4721: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4722: struct permission_set *pset = &sc->local_perms;
1.367 djm 4723: u_int i;
1.381 djm 4724: struct permission *perm;
1.99 markus 4725:
1.381 djm 4726: for (i = 0; i < pset->num_permitted_user; i++) {
4727: perm = &pset->permitted_user[i];
4728: if (open_listen_match_tcpip(perm,
4729: listen_host, listen_port, 1)) {
4730: if (perm->downstream)
4731: return perm->downstream;
4732: if (perm->port_to_connect == 0)
1.372 markus 4733: return rdynamic_connect_prepare(ssh,
4734: ctype, rname);
1.367 djm 4735: return connect_to(ssh,
1.381 djm 4736: perm->host_to_connect, perm->port_to_connect,
1.367 djm 4737: ctype, rname);
1.276 djm 4738: }
4739: }
1.74 markus 4740: error("WARNING: Server requests forwarding for unknown listen_port %d",
4741: listen_port);
1.276 djm 4742: return NULL;
1.73 markus 4743: }
4744:
1.336 millert 4745: Channel *
1.367 djm 4746: channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4747: char *ctype, char *rname)
1.336 millert 4748: {
1.367 djm 4749: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4750: struct permission_set *pset = &sc->local_perms;
1.367 djm 4751: u_int i;
1.381 djm 4752: struct permission *perm;
1.336 millert 4753:
1.381 djm 4754: for (i = 0; i < pset->num_permitted_user; i++) {
4755: perm = &pset->permitted_user[i];
4756: if (open_listen_match_streamlocal(perm, path)) {
1.367 djm 4757: return connect_to(ssh,
1.381 djm 4758: perm->host_to_connect, perm->port_to_connect,
1.367 djm 4759: ctype, rname);
1.336 millert 4760: }
4761: }
4762: error("WARNING: Server requests forwarding for unknown path %.100s",
4763: path);
4764: return NULL;
4765: }
4766:
1.99 markus 4767: /* Check if connecting to that port is permitted and connect. */
1.276 djm 4768: Channel *
1.367 djm 4769: channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4770: char *ctype, char *rname, int *reason, const char **errmsg)
1.99 markus 4771: {
1.367 djm 4772: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4773: struct permission_set *pset = &sc->local_perms;
1.372 markus 4774: struct channel_connect cctx;
4775: Channel *c;
1.367 djm 4776: u_int i, permit, permit_adm = 1;
1.372 markus 4777: int sock;
1.381 djm 4778: struct permission *perm;
1.99 markus 4779:
1.381 djm 4780: permit = pset->all_permitted;
1.99 markus 4781: if (!permit) {
1.381 djm 4782: for (i = 0; i < pset->num_permitted_user; i++) {
4783: perm = &pset->permitted_user[i];
4784: if (open_match(perm, host, port)) {
1.99 markus 4785: permit = 1;
1.333 markus 4786: break;
4787: }
1.367 djm 4788: }
1.257 dtucker 4789: }
1.99 markus 4790:
1.381 djm 4791: if (pset->num_permitted_admin > 0) {
1.257 dtucker 4792: permit_adm = 0;
1.381 djm 4793: for (i = 0; i < pset->num_permitted_admin; i++) {
4794: perm = &pset->permitted_admin[i];
4795: if (open_match(perm, host, port)) {
1.257 dtucker 4796: permit_adm = 1;
1.333 markus 4797: break;
4798: }
1.367 djm 4799: }
1.99 markus 4800: }
1.257 dtucker 4801:
4802: if (!permit || !permit_adm) {
1.391 florian 4803: logit("Received request from %.100s port %d to connect to "
4804: "host %.100s port %d, but the request was denied.",
4805: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), host, port);
1.357 dtucker 4806: if (reason != NULL)
4807: *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
1.276 djm 4808: return NULL;
1.99 markus 4809: }
1.372 markus 4810:
4811: memset(&cctx, 0, sizeof(cctx));
4812: sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4813: &cctx, reason, errmsg);
4814: if (sock == -1) {
4815: channel_connect_ctx_free(&cctx);
4816: return NULL;
4817: }
4818:
4819: c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4820: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4821: c->host_port = port;
4822: c->path = xstrdup(host);
4823: c->connect_ctx = cctx;
4824:
4825: return c;
1.336 millert 4826: }
4827:
4828: /* Check if connecting to that path is permitted and connect. */
4829: Channel *
1.367 djm 4830: channel_connect_to_path(struct ssh *ssh, const char *path,
4831: char *ctype, char *rname)
1.336 millert 4832: {
1.367 djm 4833: struct ssh_channels *sc = ssh->chanctxt;
1.381 djm 4834: struct permission_set *pset = &sc->local_perms;
1.367 djm 4835: u_int i, permit, permit_adm = 1;
1.381 djm 4836: struct permission *perm;
1.336 millert 4837:
1.381 djm 4838: permit = pset->all_permitted;
1.336 millert 4839: if (!permit) {
1.381 djm 4840: for (i = 0; i < pset->num_permitted_user; i++) {
4841: perm = &pset->permitted_user[i];
4842: if (open_match(perm, path, PORT_STREAMLOCAL)) {
1.336 millert 4843: permit = 1;
4844: break;
4845: }
1.367 djm 4846: }
1.336 millert 4847: }
4848:
1.381 djm 4849: if (pset->num_permitted_admin > 0) {
1.336 millert 4850: permit_adm = 0;
1.381 djm 4851: for (i = 0; i < pset->num_permitted_admin; i++) {
4852: perm = &pset->permitted_admin[i];
4853: if (open_match(perm, path, PORT_STREAMLOCAL)) {
1.336 millert 4854: permit_adm = 1;
4855: break;
4856: }
1.367 djm 4857: }
1.336 millert 4858: }
4859:
4860: if (!permit || !permit_adm) {
4861: logit("Received request to connect to path %.100s, "
4862: "but the request was denied.", path);
4863: return NULL;
4864: }
1.367 djm 4865: return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
1.204 djm 4866: }
4867:
4868: void
1.367 djm 4869: channel_send_window_changes(struct ssh *ssh)
1.204 djm 4870: {
1.367 djm 4871: struct ssh_channels *sc = ssh->chanctxt;
4872: struct winsize ws;
4873: int r;
1.209 avsm 4874: u_int i;
1.204 djm 4875:
1.367 djm 4876: for (i = 0; i < sc->channels_alloc; i++) {
4877: if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4878: sc->channels[i]->type != SSH_CHANNEL_OPEN)
1.204 djm 4879: continue;
1.393 deraadt 4880: if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) == -1)
1.204 djm 4881: continue;
1.367 djm 4882: channel_request_start(ssh, i, "window-change", 0);
4883: if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4884: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4885: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4886: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4887: (r = sshpkt_send(ssh)) != 0)
1.403 djm 4888: fatal_fr(r, "channel %u; send window-change", i);
1.204 djm 4889: }
1.372 markus 4890: }
4891:
4892: /* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4893: static Channel *
4894: rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4895: {
4896: Channel *c;
4897: int r;
4898:
4899: c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4900: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4901: c->host_port = 0;
4902: c->path = NULL;
4903:
4904: /*
4905: * We need to open the channel before we have a FD,
4906: * so that we can get SOCKS header from peer.
4907: */
4908: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4909: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4910: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4911: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1.403 djm 4912: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0)
4913: fatal_fr(r, "channel %i; confirm", c->self);
1.372 markus 4914: return c;
4915: }
4916:
4917: /* Return CONNECTING socket to remote host:port or local socket path */
4918: static int
4919: rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4920: {
1.405 markus 4921: struct ssh_channels *sc = ssh->chanctxt;
4922: struct permission_set *pset = &sc->local_perms;
4923: struct permission *perm;
1.372 markus 4924: struct channel_connect cctx;
1.405 markus 4925: u_int i, permit_adm = 1;
1.372 markus 4926: int sock;
1.405 markus 4927:
4928: if (pset->num_permitted_admin > 0) {
4929: permit_adm = 0;
4930: for (i = 0; i < pset->num_permitted_admin; i++) {
4931: perm = &pset->permitted_admin[i];
4932: if (open_match(perm, c->path, c->host_port)) {
4933: permit_adm = 1;
4934: break;
4935: }
4936: }
4937: }
4938: if (!permit_adm) {
4939: debug_f("requested forward not permitted");
4940: return -1;
4941: }
1.372 markus 4942:
4943: memset(&cctx, 0, sizeof(cctx));
4944: sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4945: NULL, &cctx, NULL, NULL);
4946: if (sock == -1)
4947: channel_connect_ctx_free(&cctx);
4948: else {
4949: /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4950: c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4951: c->connect_ctx = cctx;
4952: channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4953: }
4954: return sock;
1.99 markus 4955: }
4956:
1.121 markus 4957: /* -- X11 forwarding */
1.1 deraadt 4958:
1.27 markus 4959: /*
4960: * Creates an internet domain socket for listening for X11 connections.
1.176 deraadt 4961: * Returns 0 and a suitable display number for the DISPLAY variable
4962: * stored in display_numberp , or -1 if an error occurs.
1.27 markus 4963: */
1.141 stevesk 4964: int
1.367 djm 4965: x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4966: int x11_use_localhost, int single_connection,
4967: u_int *display_numberp, int **chanids)
1.1 deraadt 4968: {
1.149 markus 4969: Channel *nc = NULL;
1.31 markus 4970: int display_number, sock;
4971: u_short port;
1.35 markus 4972: struct addrinfo hints, *ai, *aitop;
4973: char strport[NI_MAXSERV];
4974: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1.25 markus 4975:
1.224 markus 4976: if (chanids == NULL)
4977: return -1;
4978:
1.33 markus 4979: for (display_number = x11_display_offset;
1.148 deraadt 4980: display_number < MAX_DISPLAYS;
4981: display_number++) {
1.25 markus 4982: port = 6000 + display_number;
1.35 markus 4983: memset(&hints, 0, sizeof(hints));
1.367 djm 4984: hints.ai_family = ssh->chanctxt->IPv4or6;
1.163 stevesk 4985: hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
1.35 markus 4986: hints.ai_socktype = SOCK_STREAM;
4987: snprintf(strport, sizeof strport, "%d", port);
1.367 djm 4988: if ((gaierr = getaddrinfo(NULL, strport,
4989: &hints, &aitop)) != 0) {
1.271 dtucker 4990: error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
1.141 stevesk 4991: return -1;
1.25 markus 4992: }
1.35 markus 4993: for (ai = aitop; ai; ai = ai->ai_next) {
1.367 djm 4994: if (ai->ai_family != AF_INET &&
4995: ai->ai_family != AF_INET6)
1.35 markus 4996: continue;
1.300 dtucker 4997: sock = socket(ai->ai_family, ai->ai_socktype,
4998: ai->ai_protocol);
1.393 deraadt 4999: if (sock == -1) {
1.35 markus 5000: error("socket: %.100s", strerror(errno));
1.203 markus 5001: freeaddrinfo(aitop);
1.141 stevesk 5002: return -1;
1.35 markus 5003: }
1.376 djm 5004: set_reuseaddr(sock);
1.393 deraadt 5005: if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.403 djm 5006: debug2_f("bind port %d: %.100s", port,
5007: strerror(errno));
1.35 markus 5008: close(sock);
1.367 djm 5009: for (n = 0; n < num_socks; n++)
1.35 markus 5010: close(socks[n]);
5011: num_socks = 0;
5012: break;
5013: }
5014: socks[num_socks++] = sock;
5015: if (num_socks == NUM_SOCKS)
5016: break;
1.25 markus 5017: }
1.83 stevesk 5018: freeaddrinfo(aitop);
1.35 markus 5019: if (num_socks > 0)
5020: break;
1.25 markus 5021: }
5022: if (display_number >= MAX_DISPLAYS) {
5023: error("Failed to allocate internet-domain X11 display socket.");
1.141 stevesk 5024: return -1;
1.25 markus 5025: }
5026: /* Start listening for connections on the socket. */
1.35 markus 5027: for (n = 0; n < num_socks; n++) {
5028: sock = socks[n];
1.393 deraadt 5029: if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
1.35 markus 5030: error("listen: %.100s", strerror(errno));
5031: close(sock);
1.141 stevesk 5032: return -1;
1.35 markus 5033: }
1.25 markus 5034: }
1.35 markus 5035:
5036: /* Allocate a channel for each socket. */
1.242 djm 5037: *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
1.35 markus 5038: for (n = 0; n < num_socks; n++) {
5039: sock = socks[n];
1.424 djm 5040: nc = channel_new(ssh, "x11-listener",
1.51 markus 5041: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
5042: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.190 markus 5043: 0, "X11 inet listener", 1);
1.167 markus 5044: nc->single_connection = single_connection;
1.224 markus 5045: (*chanids)[n] = nc->self;
1.35 markus 5046: }
1.224 markus 5047: (*chanids)[n] = -1;
1.1 deraadt 5048:
1.141 stevesk 5049: /* Return the display number for the DISPLAY environment variable. */
1.176 deraadt 5050: *display_numberp = display_number;
1.367 djm 5051: return 0;
1.1 deraadt 5052: }
5053:
1.127 itojun 5054: static int
1.77 markus 5055: connect_local_xsocket(u_int dnr)
1.1 deraadt 5056: {
1.25 markus 5057: int sock;
5058: struct sockaddr_un addr;
5059:
1.147 stevesk 5060: sock = socket(AF_UNIX, SOCK_STREAM, 0);
1.393 deraadt 5061: if (sock == -1)
1.147 stevesk 5062: error("socket: %.100s", strerror(errno));
5063: memset(&addr, 0, sizeof(addr));
5064: addr.sun_family = AF_UNIX;
5065: snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
1.239 deraadt 5066: if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
1.147 stevesk 5067: return sock;
5068: close(sock);
1.25 markus 5069: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
5070: return -1;
1.1 deraadt 5071: }
5072:
1.51 markus 5073: int
1.367 djm 5074: x11_connect_display(struct ssh *ssh)
1.1 deraadt 5075: {
1.248 deraadt 5076: u_int display_number;
1.25 markus 5077: const char *display;
1.51 markus 5078: char buf[1024], *cp;
1.35 markus 5079: struct addrinfo hints, *ai, *aitop;
5080: char strport[NI_MAXSERV];
1.248 deraadt 5081: int gaierr, sock = 0;
1.25 markus 5082:
5083: /* Try to open a socket for the local X server. */
5084: display = getenv("DISPLAY");
5085: if (!display) {
5086: error("DISPLAY not set.");
1.51 markus 5087: return -1;
1.25 markus 5088: }
1.27 markus 5089: /*
5090: * Now we decode the value of the DISPLAY variable and make a
5091: * connection to the real X server.
5092: */
5093:
5094: /*
5095: * Check if it is a unix domain socket. Unix domain displays are in
5096: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
5097: */
1.25 markus 5098: if (strncmp(display, "unix:", 5) == 0 ||
5099: display[0] == ':') {
5100: /* Connect to the unix domain socket. */
1.367 djm 5101: if (sscanf(strrchr(display, ':') + 1, "%u",
5102: &display_number) != 1) {
5103: error("Could not parse display number from DISPLAY: "
5104: "%.100s", display);
1.51 markus 5105: return -1;
1.25 markus 5106: }
5107: /* Create a socket. */
5108: sock = connect_local_xsocket(display_number);
5109: if (sock < 0)
1.51 markus 5110: return -1;
1.25 markus 5111:
5112: /* OK, we now have a connection to the display. */
1.51 markus 5113: return sock;
1.25 markus 5114: }
1.27 markus 5115: /*
5116: * Connect to an inet socket. The DISPLAY value is supposedly
5117: * hostname:d[.s], where hostname may also be numeric IP address.
5118: */
1.145 stevesk 5119: strlcpy(buf, display, sizeof(buf));
1.25 markus 5120: cp = strchr(buf, ':');
5121: if (!cp) {
5122: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 5123: return -1;
1.25 markus 5124: }
5125: *cp = 0;
1.367 djm 5126: /*
5127: * buf now contains the host name. But first we parse the
5128: * display number.
5129: */
1.248 deraadt 5130: if (sscanf(cp + 1, "%u", &display_number) != 1) {
1.25 markus 5131: error("Could not parse display number from DISPLAY: %.100s",
1.148 deraadt 5132: display);
1.51 markus 5133: return -1;
1.25 markus 5134: }
1.35 markus 5135:
5136: /* Look up the host address */
5137: memset(&hints, 0, sizeof(hints));
1.367 djm 5138: hints.ai_family = ssh->chanctxt->IPv4or6;
1.35 markus 5139: hints.ai_socktype = SOCK_STREAM;
1.248 deraadt 5140: snprintf(strport, sizeof strport, "%u", 6000 + display_number);
1.35 markus 5141: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1.271 dtucker 5142: error("%.100s: unknown host. (%s)", buf,
5143: ssh_gai_strerror(gaierr));
1.51 markus 5144: return -1;
1.25 markus 5145: }
1.35 markus 5146: for (ai = aitop; ai; ai = ai->ai_next) {
5147: /* Create a socket. */
1.300 dtucker 5148: sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.393 deraadt 5149: if (sock == -1) {
1.194 markus 5150: debug2("socket: %.100s", strerror(errno));
1.41 markus 5151: continue;
5152: }
5153: /* Connect it to the display. */
1.393 deraadt 5154: if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
1.248 deraadt 5155: debug2("connect %.100s port %u: %.100s", buf,
1.41 markus 5156: 6000 + display_number, strerror(errno));
5157: close(sock);
5158: continue;
5159: }
5160: /* Success */
5161: break;
1.35 markus 5162: }
5163: freeaddrinfo(aitop);
5164: if (!ai) {
1.367 djm 5165: error("connect %.100s port %u: %.100s", buf,
5166: 6000 + display_number, strerror(errno));
1.51 markus 5167: return -1;
1.25 markus 5168: }
1.162 stevesk 5169: set_nodelay(sock);
1.51 markus 5170: return sock;
5171: }
5172:
5173: /*
1.27 markus 5174: * Requests forwarding of X11 connections, generates fake authentication
5175: * data, and enables authentication spoofing.
1.121 markus 5176: * This should be called in the client only.
1.27 markus 5177: */
1.49 markus 5178: void
1.367 djm 5179: x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
5180: const char *disp, const char *proto, const char *data, int want_reply)
1.1 deraadt 5181: {
1.367 djm 5182: struct ssh_channels *sc = ssh->chanctxt;
1.77 markus 5183: u_int data_len = (u_int) strlen(data) / 2;
1.219 djm 5184: u_int i, value;
1.367 djm 5185: const char *cp;
1.25 markus 5186: char *new_data;
1.367 djm 5187: int r, screen_number;
1.25 markus 5188:
1.367 djm 5189: if (sc->x11_saved_display == NULL)
5190: sc->x11_saved_display = xstrdup(disp);
5191: else if (strcmp(disp, sc->x11_saved_display) != 0) {
1.219 djm 5192: error("x11_request_forwarding_with_spoofing: different "
5193: "$DISPLAY already forwarded");
5194: return;
5195: }
5196:
1.266 djm 5197: cp = strchr(disp, ':');
1.25 markus 5198: if (cp)
5199: cp = strchr(cp, '.');
5200: if (cp)
1.245 deraadt 5201: screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
1.25 markus 5202: else
5203: screen_number = 0;
5204:
1.367 djm 5205: if (sc->x11_saved_proto == NULL) {
1.219 djm 5206: /* Save protocol name. */
1.367 djm 5207: sc->x11_saved_proto = xstrdup(proto);
1.353 natano 5208:
5209: /* Extract real authentication data. */
1.367 djm 5210: sc->x11_saved_data = xmalloc(data_len);
1.219 djm 5211: for (i = 0; i < data_len; i++) {
1.403 djm 5212: if (sscanf(data + 2 * i, "%2x", &value) != 1) {
1.219 djm 5213: fatal("x11_request_forwarding: bad "
5214: "authentication data: %.100s", data);
1.403 djm 5215: }
1.367 djm 5216: sc->x11_saved_data[i] = value;
1.219 djm 5217: }
1.367 djm 5218: sc->x11_saved_data_len = data_len;
1.353 natano 5219:
5220: /* Generate fake data of the same length. */
1.367 djm 5221: sc->x11_fake_data = xmalloc(data_len);
5222: arc4random_buf(sc->x11_fake_data, data_len);
5223: sc->x11_fake_data_len = data_len;
1.25 markus 5224: }
5225:
5226: /* Convert the fake data into hex. */
1.367 djm 5227: new_data = tohex(sc->x11_fake_data, data_len);
1.25 markus 5228:
5229: /* Send the request packet. */
1.367 djm 5230: channel_request_start(ssh, client_session_id, "x11-req", want_reply);
5231: if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
5232: (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
5233: (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
5234: (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
5235: (r = sshpkt_send(ssh)) != 0 ||
5236: (r = ssh_packet_write_wait(ssh)) != 0)
1.403 djm 5237: fatal_fr(r, "send x11-req");
1.321 djm 5238: free(new_data);
1.1 deraadt 5239: }