Annotation of src/usr.bin/ssh/channels.c, Revision 1.126
1.1 deraadt 1: /*
1.26 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * This file contains functions for generic socket connection forwarding.
6: * There is also code for initiating connection forwarding for X11 connections,
7: * arbitrary tcp/ip connections, and the authentication agent connection.
1.49 markus 8: *
1.67 deraadt 9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
14: *
15: *
1.44 markus 16: * SSH2 support added by Markus Friedl.
1.67 deraadt 17: * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
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:
42: #include "includes.h"
1.126 ! markus 43: RCSID("$OpenBSD: channels.c,v 1.125 2001/06/07 20:23:04 markus Exp $");
1.1 deraadt 44:
45: #include "ssh.h"
1.82 markus 46: #include "ssh1.h"
47: #include "ssh2.h"
1.1 deraadt 48: #include "packet.h"
49: #include "xmalloc.h"
50: #include "uidswap.h"
1.82 markus 51: #include "log.h"
52: #include "misc.h"
1.14 markus 53: #include "channels.h"
54: #include "compat.h"
1.82 markus 55: #include "canohost.h"
1.64 markus 56: #include "key.h"
57: #include "authfd.h"
1.44 markus 58:
1.1 deraadt 59:
1.121 markus 60: /* -- channel core */
1.12 markus 61:
1.27 markus 62: /*
63: * Pointer to an array containing all allocated channels. The array is
64: * dynamically extended as needed.
65: */
1.113 markus 66: static Channel **channels = NULL;
1.1 deraadt 67:
1.27 markus 68: /*
69: * Size of the channel array. All slots of the array must always be
1.113 markus 70: * initialized (at least the type field); unused slots set to NULL
1.27 markus 71: */
1.1 deraadt 72: static int channels_alloc = 0;
73:
1.27 markus 74: /*
75: * Maximum file descriptor value used in any of the channels. This is
1.113 markus 76: * updated in channel_new.
1.27 markus 77: */
1.84 markus 78: static int channel_max_fd = 0;
1.1 deraadt 79:
80:
1.121 markus 81: /* -- tcp forwarding */
1.1 deraadt 82:
1.27 markus 83: /*
84: * Data structure for storing which hosts are permitted for forward requests.
85: * The local sides of any remote forwards are stored in this array to prevent
86: * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
87: * network (which might be behind a firewall).
88: */
1.25 markus 89: typedef struct {
1.41 markus 90: char *host_to_connect; /* Connect to 'host'. */
91: u_short port_to_connect; /* Connect to 'port'. */
92: u_short listen_port; /* Remote side should listen port number. */
1.1 deraadt 93: } ForwardPermission;
94:
95: /* List of all permitted host/port pairs to connect. */
96: static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
1.121 markus 97:
1.1 deraadt 98: /* Number of permitted host/port pairs in the array. */
99: static int num_permitted_opens = 0;
1.27 markus 100: /*
101: * If this is true, all opens are permitted. This is the case on the server
102: * on which we have to trust the client anyway, and the user could do
103: * anything after logging in anyway.
104: */
1.1 deraadt 105: static int all_opens_permitted = 0;
106:
1.121 markus 107:
108: /* -- X11 forwarding */
109:
110: /* Maximum number of fake X11 displays to try. */
111: #define MAX_DISPLAYS 1000
112:
113: /* Saved X11 authentication protocol name. */
114: static char *x11_saved_proto = NULL;
115:
116: /* Saved X11 authentication data. This is the real data. */
117: static char *x11_saved_data = NULL;
118: static u_int x11_saved_data_len = 0;
119:
120: /*
121: * Fake X11 authentication data. This is what the server will be sending us;
122: * we should replace any occurrences of this by the real data.
123: */
124: static char *x11_fake_data = NULL;
125: static u_int x11_fake_data_len;
126:
127:
128: /* -- agent forwarding */
129:
130: #define NUM_SOCKS 10
131:
132: /* Name and directory of socket for authentication agent forwarding. */
133: static char *auth_sock_name = NULL;
134: static char *auth_sock_dir = NULL;
135:
1.82 markus 136:
137: /* AF_UNSPEC or AF_INET or AF_INET6 */
138: extern int IPv4or6;
1.1 deraadt 139:
1.121 markus 140: /* helper */
141: void port_open_helper(Channel *c, char *rtype);
1.103 markus 142:
1.121 markus 143: /* -- channel core */
1.41 markus 144:
145: Channel *
146: channel_lookup(int id)
147: {
148: Channel *c;
1.113 markus 149:
1.63 provos 150: if (id < 0 || id > channels_alloc) {
1.41 markus 151: log("channel_lookup: %d: bad id", id);
152: return NULL;
153: }
1.113 markus 154: c = channels[id];
155: if (c == NULL) {
1.41 markus 156: log("channel_lookup: %d: bad id: channel free", id);
157: return NULL;
158: }
159: return c;
1.55 markus 160: }
161:
1.27 markus 162: /*
1.55 markus 163: * Register filedescriptors for a channel, used when allocating a channel or
1.52 markus 164: * when the channel consumer/producer is ready, e.g. shell exec'd
1.27 markus 165: */
1.1 deraadt 166:
1.52 markus 167: void
1.71 markus 168: channel_register_fds(Channel *c, int rfd, int wfd, int efd,
169: int extusage, int nonblock)
1.1 deraadt 170: {
1.25 markus 171: /* Update the maximum file descriptor value. */
1.84 markus 172: channel_max_fd = MAX(channel_max_fd, rfd);
173: channel_max_fd = MAX(channel_max_fd, wfd);
174: channel_max_fd = MAX(channel_max_fd, efd);
175:
1.27 markus 176: /* XXX set close-on-exec -markus */
1.55 markus 177:
1.52 markus 178: c->rfd = rfd;
179: c->wfd = wfd;
180: c->sock = (rfd == wfd) ? rfd : -1;
181: c->efd = efd;
182: c->extended_usage = extusage;
1.71 markus 183:
1.91 markus 184: /* XXX ugly hack: nonblock is only set by the server */
185: if (nonblock && isatty(c->rfd)) {
1.94 markus 186: debug("channel %d: rfd %d isatty", c->self, c->rfd);
1.91 markus 187: c->isatty = 1;
188: if (!isatty(c->wfd)) {
1.94 markus 189: error("channel %d: wfd %d is not a tty?",
1.91 markus 190: c->self, c->wfd);
191: }
192: } else {
193: c->isatty = 0;
194: }
195:
1.71 markus 196: /* enable nonblocking mode */
197: if (nonblock) {
198: if (rfd != -1)
199: set_nonblock(rfd);
200: if (wfd != -1)
201: set_nonblock(wfd);
202: if (efd != -1)
203: set_nonblock(efd);
204: }
1.52 markus 205: }
206:
207: /*
208: * Allocate a new channel object and set its type and socket. This will cause
209: * remote_name to be freed.
210: */
211:
1.113 markus 212: Channel *
1.52 markus 213: channel_new(char *ctype, int type, int rfd, int wfd, int efd,
1.71 markus 214: int window, int maxpack, int extusage, char *remote_name, int nonblock)
1.52 markus 215: {
216: int i, found;
217: Channel *c;
1.25 markus 218:
219: /* Do initial allocation if this is the first call. */
220: if (channels_alloc == 0) {
1.44 markus 221: chan_init();
1.25 markus 222: channels_alloc = 10;
1.113 markus 223: channels = xmalloc(channels_alloc * sizeof(Channel *));
1.25 markus 224: for (i = 0; i < channels_alloc; i++)
1.113 markus 225: channels[i] = NULL;
1.126 ! markus 226: fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
1.25 markus 227: }
228: /* Try to find a free slot where to put the new channel. */
229: for (found = -1, i = 0; i < channels_alloc; i++)
1.113 markus 230: if (channels[i] == NULL) {
1.25 markus 231: /* Found a free slot. */
232: found = i;
233: break;
234: }
235: if (found == -1) {
1.27 markus 236: /* There are no free slots. Take last+1 slot and expand the array. */
1.25 markus 237: found = channels_alloc;
238: channels_alloc += 10;
1.70 markus 239: debug2("channel: expanding %d", channels_alloc);
1.113 markus 240: channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
1.25 markus 241: for (i = found; i < channels_alloc; i++)
1.113 markus 242: channels[i] = NULL;
1.25 markus 243: }
1.113 markus 244: /* Initialize and return new channel. */
245: c = channels[found] = xmalloc(sizeof(Channel));
1.25 markus 246: buffer_init(&c->input);
247: buffer_init(&c->output);
1.41 markus 248: buffer_init(&c->extended);
1.25 markus 249: chan_init_iostates(c);
1.71 markus 250: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
1.25 markus 251: c->self = found;
252: c->type = type;
1.41 markus 253: c->ctype = ctype;
1.51 markus 254: c->local_window = window;
255: c->local_window_max = window;
256: c->local_consumed = 0;
257: c->local_maxpacket = maxpack;
1.25 markus 258: c->remote_id = -1;
259: c->remote_name = remote_name;
1.41 markus 260: c->remote_window = 0;
261: c->remote_maxpacket = 0;
262: c->cb_fn = NULL;
263: c->cb_arg = NULL;
264: c->cb_event = 0;
265: c->dettach_user = NULL;
1.65 markus 266: c->input_filter = NULL;
1.25 markus 267: debug("channel %d: new [%s]", found, remote_name);
1.113 markus 268: return c;
1.1 deraadt 269: }
1.52 markus 270:
271: /* Close all channel fd/socket. */
272:
273: void
274: channel_close_fds(Channel *c)
275: {
1.118 markus 276: debug3("channel_close_fds: channel %d: r %d w %d e %d",
277: c->self, c->rfd, c->wfd, c->efd);
278:
1.52 markus 279: if (c->sock != -1) {
280: close(c->sock);
281: c->sock = -1;
282: }
283: if (c->rfd != -1) {
284: close(c->rfd);
1.121 markus 285: c->rfd = -1;
286: }
287: if (c->wfd != -1) {
288: close(c->wfd);
289: c->wfd = -1;
290: }
291: if (c->efd != -1) {
292: close(c->efd);
293: c->efd = -1;
294: }
295: }
296:
297: /* Free the channel and close its fd/socket. */
298:
299: void
300: channel_free(Channel *c)
301: {
302: char *s;
303: int i, n;
304:
305: for (n = 0, i = 0; i < channels_alloc; i++)
306: if (channels[i])
307: n++;
308: debug("channel_free: channel %d: %s, nchannels %d", c->self,
309: c->remote_name ? c->remote_name : "???", n);
310:
311: s = channel_open_message();
312: debug3("channel_free: status: %s", s);
313: xfree(s);
314:
315: if (c->dettach_user != NULL) {
316: debug("channel_free: channel %d: dettaching channel user", c->self);
317: c->dettach_user(c->self, NULL);
318: }
319: if (c->sock != -1)
320: shutdown(c->sock, SHUT_RDWR);
321: channel_close_fds(c);
322: buffer_free(&c->input);
323: buffer_free(&c->output);
324: buffer_free(&c->extended);
325: if (c->remote_name) {
326: xfree(c->remote_name);
327: c->remote_name = NULL;
328: }
329: channels[c->self] = NULL;
330: xfree(c);
331: }
332:
333: void
1.126 ! markus 334: channel_free_all(void)
1.121 markus 335: {
336: int i;
337:
1.126 ! markus 338: for (i = 0; i < channels_alloc; i++)
! 339: if (channels[i] != NULL)
! 340: channel_free(channels[i]);
1.121 markus 341: }
342:
343: /*
344: * Closes the sockets/fds of all channels. This is used to close extra file
345: * descriptors after a fork.
346: */
347:
348: void
349: channel_close_all()
350: {
351: int i;
352:
353: for (i = 0; i < channels_alloc; i++)
354: if (channels[i] != NULL)
355: channel_close_fds(channels[i]);
356: }
357:
358: /*
359: * Returns true if no channel has too much buffered data, and false if one or
360: * more channel is overfull.
361: */
362:
363: int
364: channel_not_very_much_buffered_data()
365: {
366: u_int i;
367: Channel *c;
368:
369: for (i = 0; i < channels_alloc; i++) {
370: c = channels[i];
371: if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
372: if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
373: debug("channel %d: big input buffer %d",
374: c->self, buffer_len(&c->input));
375: return 0;
376: }
377: if (buffer_len(&c->output) > packet_get_maxsize()) {
378: debug("channel %d: big output buffer %d",
379: c->self, buffer_len(&c->output));
380: return 0;
381: }
382: }
383: }
384: return 1;
385: }
386:
387: /* Returns true if any channel is still open. */
388:
389: int
390: channel_still_open()
391: {
392: int i;
393: Channel *c;
394:
395: for (i = 0; i < channels_alloc; i++) {
396: c = channels[i];
397: if (c == NULL)
398: continue;
399: switch (c->type) {
400: case SSH_CHANNEL_X11_LISTENER:
401: case SSH_CHANNEL_PORT_LISTENER:
402: case SSH_CHANNEL_RPORT_LISTENER:
403: case SSH_CHANNEL_CLOSED:
404: case SSH_CHANNEL_AUTH_SOCKET:
405: case SSH_CHANNEL_DYNAMIC:
406: case SSH_CHANNEL_CONNECTING:
407: case SSH_CHANNEL_ZOMBIE:
408: continue;
409: case SSH_CHANNEL_LARVAL:
410: if (!compat20)
411: fatal("cannot happen: SSH_CHANNEL_LARVAL");
412: continue;
413: case SSH_CHANNEL_OPENING:
414: case SSH_CHANNEL_OPEN:
415: case SSH_CHANNEL_X11_OPEN:
416: return 1;
417: case SSH_CHANNEL_INPUT_DRAINING:
418: case SSH_CHANNEL_OUTPUT_DRAINING:
419: if (!compat13)
420: fatal("cannot happen: OUT_DRAIN");
421: return 1;
422: default:
423: fatal("channel_still_open: bad channel type %d", c->type);
424: /* NOTREACHED */
425: }
426: }
427: return 0;
428: }
429:
430: /* Returns the id of an open channel suitable for keepaliving */
431:
432: int
433: channel_find_open()
434: {
435: int i;
436: Channel *c;
437:
438: for (i = 0; i < channels_alloc; i++) {
439: c = channels[i];
440: if (c == NULL)
441: continue;
442: switch (c->type) {
443: case SSH_CHANNEL_CLOSED:
444: case SSH_CHANNEL_DYNAMIC:
445: case SSH_CHANNEL_X11_LISTENER:
446: case SSH_CHANNEL_PORT_LISTENER:
447: case SSH_CHANNEL_RPORT_LISTENER:
448: case SSH_CHANNEL_OPENING:
449: case SSH_CHANNEL_CONNECTING:
450: case SSH_CHANNEL_ZOMBIE:
451: continue;
452: case SSH_CHANNEL_LARVAL:
453: case SSH_CHANNEL_AUTH_SOCKET:
454: case SSH_CHANNEL_OPEN:
455: case SSH_CHANNEL_X11_OPEN:
456: return i;
457: case SSH_CHANNEL_INPUT_DRAINING:
458: case SSH_CHANNEL_OUTPUT_DRAINING:
459: if (!compat13)
460: fatal("cannot happen: OUT_DRAIN");
461: return i;
462: default:
463: fatal("channel_find_open: bad channel type %d", c->type);
464: /* NOTREACHED */
465: }
466: }
467: return -1;
468: }
469:
470:
471: /*
472: * Returns a message describing the currently open forwarded connections,
473: * suitable for sending to the client. The message contains crlf pairs for
474: * newlines.
475: */
476:
477: char *
478: channel_open_message()
479: {
480: Buffer buffer;
481: Channel *c;
482: char buf[1024], *cp;
483: int i;
484:
485: buffer_init(&buffer);
486: snprintf(buf, sizeof buf, "The following connections are open:\r\n");
487: buffer_append(&buffer, buf, strlen(buf));
488: for (i = 0; i < channels_alloc; i++) {
489: c = channels[i];
490: if (c == NULL)
491: continue;
492: switch (c->type) {
493: case SSH_CHANNEL_X11_LISTENER:
494: case SSH_CHANNEL_PORT_LISTENER:
495: case SSH_CHANNEL_RPORT_LISTENER:
496: case SSH_CHANNEL_CLOSED:
497: case SSH_CHANNEL_AUTH_SOCKET:
498: case SSH_CHANNEL_ZOMBIE:
499: continue;
500: case SSH_CHANNEL_LARVAL:
501: case SSH_CHANNEL_OPENING:
502: case SSH_CHANNEL_CONNECTING:
503: case SSH_CHANNEL_DYNAMIC:
504: case SSH_CHANNEL_OPEN:
505: case SSH_CHANNEL_X11_OPEN:
506: case SSH_CHANNEL_INPUT_DRAINING:
507: case SSH_CHANNEL_OUTPUT_DRAINING:
508: snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
509: c->self, c->remote_name,
510: c->type, c->remote_id,
511: c->istate, buffer_len(&c->input),
512: c->ostate, buffer_len(&c->output),
513: c->rfd, c->wfd);
514: buffer_append(&buffer, buf, strlen(buf));
515: continue;
516: default:
517: fatal("channel_open_message: bad channel type %d", c->type);
518: /* NOTREACHED */
519: }
520: }
521: buffer_append(&buffer, "\0", 1);
522: cp = xstrdup(buffer_ptr(&buffer));
523: buffer_free(&buffer);
524: return cp;
525: }
526:
527: void
528: channel_send_open(int id)
529: {
530: Channel *c = channel_lookup(id);
531: if (c == NULL) {
532: log("channel_send_open: %d: bad id", id);
533: return;
534: }
535: debug("send channel open %d", id);
536: packet_start(SSH2_MSG_CHANNEL_OPEN);
537: packet_put_cstring(c->ctype);
538: packet_put_int(c->self);
539: packet_put_int(c->local_window);
540: packet_put_int(c->local_maxpacket);
541: packet_send();
542: }
543:
544: void
545: channel_request(int id, char *service, int wantconfirm)
546: {
547: channel_request_start(id, service, wantconfirm);
548: packet_send();
549: debug("channel request %d: %s", id, service) ;
550: }
551: void
552: channel_request_start(int id, char *service, int wantconfirm)
553: {
554: Channel *c = channel_lookup(id);
555: if (c == NULL) {
556: log("channel_request: %d: bad id", id);
557: return;
558: }
559: packet_start(SSH2_MSG_CHANNEL_REQUEST);
560: packet_put_int(c->remote_id);
561: packet_put_cstring(service);
562: packet_put_char(wantconfirm);
563: }
564: void
565: channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
566: {
567: Channel *c = channel_lookup(id);
568: if (c == NULL) {
569: log("channel_register_callback: %d: bad id", id);
570: return;
571: }
572: c->cb_event = mtype;
573: c->cb_fn = fn;
574: c->cb_arg = arg;
575: }
576: void
577: channel_register_cleanup(int id, channel_callback_fn *fn)
578: {
579: Channel *c = channel_lookup(id);
580: if (c == NULL) {
581: log("channel_register_cleanup: %d: bad id", id);
582: return;
583: }
584: c->dettach_user = fn;
585: }
586: void
587: channel_cancel_cleanup(int id)
588: {
589: Channel *c = channel_lookup(id);
590: if (c == NULL) {
591: log("channel_cancel_cleanup: %d: bad id", id);
592: return;
1.52 markus 593: }
1.121 markus 594: c->dettach_user = NULL;
595: }
596: void
597: channel_register_filter(int id, channel_filter_fn *fn)
598: {
599: Channel *c = channel_lookup(id);
600: if (c == NULL) {
601: log("channel_register_filter: %d: bad id", id);
602: return;
1.52 markus 603: }
1.121 markus 604: c->input_filter = fn;
1.52 markus 605: }
606:
1.49 markus 607: void
1.121 markus 608: channel_set_fds(int id, int rfd, int wfd, int efd,
609: int extusage, int nonblock)
1.1 deraadt 610: {
1.121 markus 611: Channel *c = channel_lookup(id);
612: if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
613: fatal("channel_activate for non-larval channel %d.", id);
614: channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
615: c->type = SSH_CHANNEL_OPEN;
616: /* XXX window size? */
617: c->local_window = c->local_window_max = c->local_maxpacket * 2;
618: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
619: packet_put_int(c->remote_id);
620: packet_put_int(c->local_window);
621: packet_send();
1.1 deraadt 622: }
623:
1.27 markus 624: /*
1.41 markus 625: * 'channel_pre*' are called just before select() to add any bits relevant to
626: * channels in the select bitmasks.
627: */
628: /*
629: * 'channel_post*': perform any appropriate operations for channels which
630: * have events pending.
1.27 markus 631: */
1.41 markus 632: typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
633: chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
634: chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
635:
636: void
637: channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
638: {
639: FD_SET(c->sock, readset);
640: }
641:
642: void
1.75 markus 643: channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
644: {
645: debug3("channel %d: waiting for connection", c->self);
646: FD_SET(c->sock, writeset);
647: }
648:
649: void
1.41 markus 650: channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
651: {
652: if (buffer_len(&c->input) < packet_get_maxsize())
653: FD_SET(c->sock, readset);
654: if (buffer_len(&c->output) > 0)
655: FD_SET(c->sock, writeset);
656: }
1.1 deraadt 657:
1.41 markus 658: void
659: channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
660: {
661: /* test whether sockets are 'alive' for read/write */
662: if (c->istate == CHAN_INPUT_OPEN)
663: if (buffer_len(&c->input) < packet_get_maxsize())
664: FD_SET(c->sock, readset);
665: if (c->ostate == CHAN_OUTPUT_OPEN ||
666: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
667: if (buffer_len(&c->output) > 0) {
668: FD_SET(c->sock, writeset);
669: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
670: chan_obuf_empty(c);
671: }
672: }
673: }
674:
675: void
1.44 markus 676: channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
677: {
678: if (c->istate == CHAN_INPUT_OPEN &&
679: c->remote_window > 0 &&
680: buffer_len(&c->input) < c->remote_window)
681: FD_SET(c->rfd, readset);
682: if (c->ostate == CHAN_OUTPUT_OPEN ||
683: c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
684: if (buffer_len(&c->output) > 0) {
685: FD_SET(c->wfd, writeset);
686: } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
687: chan_obuf_empty(c);
688: }
689: }
690: /** XXX check close conditions, too */
691: if (c->efd != -1) {
692: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
693: buffer_len(&c->extended) > 0)
694: FD_SET(c->efd, writeset);
695: else if (c->extended_usage == CHAN_EXTENDED_READ &&
696: buffer_len(&c->extended) < c->remote_window)
697: FD_SET(c->efd, readset);
698: }
699: }
700:
701: void
1.41 markus 702: channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
703: {
704: if (buffer_len(&c->input) == 0) {
705: packet_start(SSH_MSG_CHANNEL_CLOSE);
706: packet_put_int(c->remote_id);
707: packet_send();
708: c->type = SSH_CHANNEL_CLOSED;
1.105 markus 709: debug("channel %d: closing after input drain.", c->self);
1.41 markus 710: }
711: }
712:
713: void
714: channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
715: {
716: if (buffer_len(&c->output) == 0)
1.113 markus 717: chan_mark_dead(c);
1.49 markus 718: else
1.41 markus 719: FD_SET(c->sock, writeset);
720: }
721:
722: /*
723: * This is a special state for X11 authentication spoofing. An opened X11
724: * connection (when authentication spoofing is being done) remains in this
725: * state until the first packet has been completely read. The authentication
726: * data in that packet is then substituted by the real data if it matches the
727: * fake data, and the channel is put into normal mode.
1.51 markus 728: * XXX All this happens at the client side.
1.121 markus 729: * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
1.41 markus 730: */
731: int
1.121 markus 732: x11_open_helper(Buffer *b)
1.1 deraadt 733: {
1.77 markus 734: u_char *ucp;
735: u_int proto_len, data_len;
1.25 markus 736:
1.41 markus 737: /* Check if the fixed size part of the packet is in buffer. */
1.121 markus 738: if (buffer_len(b) < 12)
1.41 markus 739: return 0;
740:
741: /* Parse the lengths of variable-length fields. */
1.121 markus 742: ucp = (u_char *) buffer_ptr(b);
1.41 markus 743: if (ucp[0] == 0x42) { /* Byte order MSB first. */
744: proto_len = 256 * ucp[6] + ucp[7];
745: data_len = 256 * ucp[8] + ucp[9];
746: } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
747: proto_len = ucp[6] + 256 * ucp[7];
748: data_len = ucp[8] + 256 * ucp[9];
749: } else {
750: debug("Initial X11 packet contains bad byte order byte: 0x%x",
751: ucp[0]);
752: return -1;
753: }
754:
755: /* Check if the whole packet is in buffer. */
1.121 markus 756: if (buffer_len(b) <
1.41 markus 757: 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
758: return 0;
759:
760: /* Check if authentication protocol matches. */
761: if (proto_len != strlen(x11_saved_proto) ||
762: memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
763: debug("X11 connection uses different authentication protocol.");
764: return -1;
765: }
766: /* Check if authentication data matches our fake data. */
767: if (data_len != x11_fake_data_len ||
768: memcmp(ucp + 12 + ((proto_len + 3) & ~3),
769: x11_fake_data, x11_fake_data_len) != 0) {
770: debug("X11 auth data does not match fake data.");
771: return -1;
772: }
773: /* Check fake data length */
774: if (x11_fake_data_len != x11_saved_data_len) {
775: error("X11 fake_data_len %d != saved_data_len %d",
776: x11_fake_data_len, x11_saved_data_len);
777: return -1;
778: }
779: /*
780: * Received authentication protocol and data match
781: * our fake data. Substitute the fake data with real
782: * data.
783: */
784: memcpy(ucp + 12 + ((proto_len + 3) & ~3),
785: x11_saved_data, x11_saved_data_len);
786: return 1;
787: }
788:
789: void
790: channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
791: {
1.121 markus 792: int ret = x11_open_helper(&c->output);
1.41 markus 793: if (ret == 1) {
794: /* Start normal processing for the channel. */
795: c->type = SSH_CHANNEL_OPEN;
1.47 markus 796: channel_pre_open_13(c, readset, writeset);
1.41 markus 797: } else if (ret == -1) {
798: /*
799: * We have received an X11 connection that has bad
800: * authentication information.
801: */
1.98 millert 802: log("X11 connection rejected because of wrong authentication.");
1.41 markus 803: buffer_clear(&c->input);
804: buffer_clear(&c->output);
805: close(c->sock);
806: c->sock = -1;
807: c->type = SSH_CHANNEL_CLOSED;
808: packet_start(SSH_MSG_CHANNEL_CLOSE);
809: packet_put_int(c->remote_id);
810: packet_send();
811: }
812: }
1.25 markus 813:
1.41 markus 814: void
1.51 markus 815: channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
1.41 markus 816: {
1.121 markus 817: int ret = x11_open_helper(&c->output);
1.41 markus 818: if (ret == 1) {
819: c->type = SSH_CHANNEL_OPEN;
1.57 markus 820: if (compat20)
821: channel_pre_open_20(c, readset, writeset);
822: else
823: channel_pre_open_15(c, readset, writeset);
1.41 markus 824: } else if (ret == -1) {
825: debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
1.51 markus 826: chan_read_failed(c); /** force close? */
1.41 markus 827: chan_write_failed(c);
828: debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
829: }
830: }
1.25 markus 831:
1.104 markus 832: /* try to decode a socks4 header */
833: int
834: channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
1.103 markus 835: {
836: u_char *p, *host;
1.109 markus 837: int len, have, i, found;
1.104 markus 838: char username[256];
1.103 markus 839: struct {
840: u_int8_t version;
841: u_int8_t command;
842: u_int16_t dest_port;
1.104 markus 843: struct in_addr dest_addr;
1.103 markus 844: } s4_req, s4_rsp;
845:
1.104 markus 846: debug2("channel %d: decode socks4", c->self);
1.109 markus 847:
848: have = buffer_len(&c->input);
849: len = sizeof(s4_req);
850: if (have < len)
851: return 0;
852: p = buffer_ptr(&c->input);
853: for (found = 0, i = len; i < have; i++) {
854: if (p[i] == '\0') {
855: found = 1;
856: break;
857: }
858: if (i > 1024) {
859: /* the peer is probably sending garbage */
860: debug("channel %d: decode socks4: too long",
861: c->self);
862: return -1;
863: }
864: }
865: if (!found)
866: return 0;
1.103 markus 867: buffer_get(&c->input, (char *)&s4_req.version, 1);
868: buffer_get(&c->input, (char *)&s4_req.command, 1);
869: buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
1.104 markus 870: buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
1.109 markus 871: have = buffer_len(&c->input);
1.103 markus 872: p = buffer_ptr(&c->input);
873: len = strlen(p);
1.106 markus 874: debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1.103 markus 875: if (len > have)
1.104 markus 876: fatal("channel %d: decode socks4: len %d > have %d",
1.103 markus 877: c->self, len, have);
878: strlcpy(username, p, sizeof(username));
879: buffer_consume(&c->input, len);
880: buffer_consume(&c->input, 1); /* trailing '\0' */
881:
1.104 markus 882: host = inet_ntoa(s4_req.dest_addr);
1.103 markus 883: strlcpy(c->path, host, sizeof(c->path));
884: c->host_port = ntohs(s4_req.dest_port);
885:
1.106 markus 886: debug("channel %d: dynamic request: socks4 host %s port %u command %u",
887: c->self, host, c->host_port, s4_req.command);
1.103 markus 888:
1.104 markus 889: if (s4_req.command != 1) {
890: debug("channel %d: cannot handle: socks4 cn %d",
891: c->self, s4_req.command);
892: return -1;
1.103 markus 893: }
1.104 markus 894: s4_rsp.version = 0; /* vn: 0 for reply */
895: s4_rsp.command = 90; /* cd: req granted */
1.103 markus 896: s4_rsp.dest_port = 0; /* ignored */
1.104 markus 897: s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1.103 markus 898: buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
1.104 markus 899: return 1;
900: }
901:
902: /* dynamic port forwarding */
903: void
904: channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
905: {
906: u_char *p;
907: int have, ret;
1.103 markus 908:
1.104 markus 909: have = buffer_len(&c->input);
910:
911: debug2("channel %d: pre_dynamic: have %d", c->self, have);
1.105 markus 912: /* buffer_dump(&c->input); */
1.104 markus 913: /* check if the fixed size part of the packet is in buffer. */
914: if (have < 4) {
915: /* need more */
916: FD_SET(c->sock, readset);
917: return;
918: }
919: /* try to guess the protocol */
920: p = buffer_ptr(&c->input);
921: switch (p[0]) {
922: case 0x04:
923: ret = channel_decode_socks4(c, readset, writeset);
924: break;
925: default:
926: ret = -1;
927: break;
928: }
929: if (ret < 0) {
1.113 markus 930: chan_mark_dead(c);
1.104 markus 931: } else if (ret == 0) {
932: debug2("channel %d: pre_dynamic: need more", c->self);
933: /* need more */
934: FD_SET(c->sock, readset);
935: } else {
936: /* switch to the next state */
937: c->type = SSH_CHANNEL_OPENING;
938: port_open_helper(c, "direct-tcpip");
939: }
1.103 markus 940: }
941:
1.41 markus 942: /* This is our fake X11 server socket. */
943: void
944: channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
945: {
1.113 markus 946: Channel *nc;
1.41 markus 947: struct sockaddr addr;
1.113 markus 948: int newsock;
1.41 markus 949: socklen_t addrlen;
1.85 markus 950: char buf[16384], *remote_ipaddr;
1.51 markus 951: int remote_port;
1.25 markus 952:
1.41 markus 953: if (FD_ISSET(c->sock, readset)) {
954: debug("X11 connection requested.");
955: addrlen = sizeof(addr);
956: newsock = accept(c->sock, &addr, &addrlen);
957: if (newsock < 0) {
958: error("accept: %.100s", strerror(errno));
959: return;
960: }
1.85 markus 961: remote_ipaddr = get_peer_ipaddr(newsock);
1.51 markus 962: remote_port = get_peer_port(newsock);
1.41 markus 963: snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1.85 markus 964: remote_ipaddr, remote_port);
1.51 markus 965:
1.113 markus 966: nc = channel_new("accepted x11 socket",
1.51 markus 967: SSH_CHANNEL_OPENING, newsock, newsock, -1,
968: c->local_window_max, c->local_maxpacket,
1.71 markus 969: 0, xstrdup(buf), 1);
1.113 markus 970: if (nc == NULL) {
971: close(newsock);
972: xfree(remote_ipaddr);
973: return;
974: }
1.51 markus 975: if (compat20) {
976: packet_start(SSH2_MSG_CHANNEL_OPEN);
977: packet_put_cstring("x11");
1.113 markus 978: packet_put_int(nc->self);
1.51 markus 979: packet_put_int(c->local_window_max);
980: packet_put_int(c->local_maxpacket);
1.85 markus 981: /* originator ipaddr and port */
982: packet_put_cstring(remote_ipaddr);
1.57 markus 983: if (datafellows & SSH_BUG_X11FWD) {
984: debug("ssh2 x11 bug compat mode");
985: } else {
986: packet_put_int(remote_port);
987: }
1.51 markus 988: packet_send();
989: } else {
990: packet_start(SSH_SMSG_X11_OPEN);
1.113 markus 991: packet_put_int(nc->self);
1.121 markus 992: if (packet_get_protocol_flags() &
993: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.125 markus 994: packet_put_cstring(buf);
1.51 markus 995: packet_send();
996: }
1.85 markus 997: xfree(remote_ipaddr);
1.41 markus 998: }
999: }
1.25 markus 1000:
1.103 markus 1001: void
1002: port_open_helper(Channel *c, char *rtype)
1003: {
1004: int direct;
1005: char buf[1024];
1006: char *remote_ipaddr = get_peer_ipaddr(c->sock);
1007: u_short remote_port = get_peer_port(c->sock);
1008:
1009: direct = (strcmp(rtype, "direct-tcpip") == 0);
1010:
1011: snprintf(buf, sizeof buf,
1012: "%s: listening port %d for %.100s port %d, "
1013: "connect from %.200s port %d",
1014: rtype, c->listening_port, c->path, c->host_port,
1015: remote_ipaddr, remote_port);
1016:
1017: xfree(c->remote_name);
1018: c->remote_name = xstrdup(buf);
1019:
1020: if (compat20) {
1021: packet_start(SSH2_MSG_CHANNEL_OPEN);
1022: packet_put_cstring(rtype);
1023: packet_put_int(c->self);
1024: packet_put_int(c->local_window_max);
1025: packet_put_int(c->local_maxpacket);
1026: if (direct) {
1027: /* target host, port */
1028: packet_put_cstring(c->path);
1029: packet_put_int(c->host_port);
1030: } else {
1031: /* listen address, port */
1032: packet_put_cstring(c->path);
1033: packet_put_int(c->listening_port);
1034: }
1035: /* originator host and port */
1036: packet_put_cstring(remote_ipaddr);
1037: packet_put_int(remote_port);
1038: packet_send();
1039: } else {
1040: packet_start(SSH_MSG_PORT_OPEN);
1041: packet_put_int(c->self);
1042: packet_put_cstring(c->path);
1043: packet_put_int(c->host_port);
1.121 markus 1044: if (packet_get_protocol_flags() &
1045: SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1.103 markus 1046: packet_put_cstring(c->remote_name);
1047: packet_send();
1048: }
1049: xfree(remote_ipaddr);
1050: }
1051:
1.41 markus 1052: /*
1053: * This socket is listening for connections to a forwarded TCP/IP port.
1054: */
1055: void
1056: channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1057: {
1.103 markus 1058: Channel *nc;
1.41 markus 1059: struct sockaddr addr;
1.113 markus 1060: int newsock, nextstate;
1.41 markus 1061: socklen_t addrlen;
1.103 markus 1062: char *rtype;
1.73 markus 1063:
1.41 markus 1064: if (FD_ISSET(c->sock, readset)) {
1065: debug("Connection to port %d forwarding "
1066: "to %.100s port %d requested.",
1067: c->listening_port, c->path, c->host_port);
1.103 markus 1068:
1069: rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
1070: "forwarded-tcpip" : "direct-tcpip";
1.115 markus 1071: nextstate = (c->host_port == 0 &&
1072: c->type != SSH_CHANNEL_RPORT_LISTENER) ?
1073: SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
1.103 markus 1074:
1.41 markus 1075: addrlen = sizeof(addr);
1076: newsock = accept(c->sock, &addr, &addrlen);
1077: if (newsock < 0) {
1078: error("accept: %.100s", strerror(errno));
1079: return;
1080: }
1.113 markus 1081: nc = channel_new(rtype,
1.103 markus 1082: nextstate, newsock, newsock, -1,
1.41 markus 1083: c->local_window_max, c->local_maxpacket,
1.103 markus 1084: 0, xstrdup(rtype), 1);
1085: if (nc == NULL) {
1.113 markus 1086: error("channel_post_port_listener: no new channel:");
1087: close(newsock);
1.103 markus 1088: return;
1.41 markus 1089: }
1.103 markus 1090: nc->listening_port = c->listening_port;
1091: nc->host_port = c->host_port;
1092: strlcpy(nc->path, c->path, sizeof(nc->path));
1093:
1094: if (nextstate != SSH_CHANNEL_DYNAMIC)
1095: port_open_helper(nc, rtype);
1.41 markus 1096: }
1097: }
1.25 markus 1098:
1.41 markus 1099: /*
1100: * This is the authentication agent socket listening for connections from
1101: * clients.
1102: */
1103: void
1104: channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1105: {
1.113 markus 1106: Channel *nc;
1.121 markus 1107: char *name;
1.113 markus 1108: int newsock;
1.41 markus 1109: struct sockaddr addr;
1110: socklen_t addrlen;
1.25 markus 1111:
1.41 markus 1112: if (FD_ISSET(c->sock, readset)) {
1113: addrlen = sizeof(addr);
1114: newsock = accept(c->sock, &addr, &addrlen);
1115: if (newsock < 0) {
1116: error("accept from auth socket: %.100s", strerror(errno));
1117: return;
1118: }
1.121 markus 1119: name = xstrdup("accepted auth socket");
1.113 markus 1120: nc = channel_new("accepted auth socket",
1.73 markus 1121: SSH_CHANNEL_OPENING, newsock, newsock, -1,
1122: c->local_window_max, c->local_maxpacket,
1.121 markus 1123: 0, name, 1);
1.113 markus 1124: if (nc == NULL) {
1125: error("channel_post_auth_listener: channel_new failed");
1.121 markus 1126: xfree(name);
1.113 markus 1127: close(newsock);
1128: }
1.73 markus 1129: if (compat20) {
1130: packet_start(SSH2_MSG_CHANNEL_OPEN);
1131: packet_put_cstring("auth-agent@openssh.com");
1.113 markus 1132: packet_put_int(nc->self);
1.73 markus 1133: packet_put_int(c->local_window_max);
1134: packet_put_int(c->local_maxpacket);
1135: } else {
1136: packet_start(SSH_SMSG_AGENT_OPEN);
1.113 markus 1137: packet_put_int(nc->self);
1.73 markus 1138: }
1.41 markus 1139: packet_send();
1140: }
1141: }
1.25 markus 1142:
1.75 markus 1143: void
1144: channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1145: {
1.114 markus 1146: int err = 0;
1147: int sz = sizeof(err);
1148:
1.75 markus 1149: if (FD_ISSET(c->sock, writeset)) {
1.114 markus 1150: if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1151: &sz) < 0) {
1152: err = errno;
1153: error("getsockopt SO_ERROR failed");
1154: }
1155: if (err == 0) {
1156: debug("channel %d: connected", c->self);
1157: c->type = SSH_CHANNEL_OPEN;
1158: if (compat20) {
1159: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1160: packet_put_int(c->remote_id);
1161: packet_put_int(c->self);
1162: packet_put_int(c->local_window);
1163: packet_put_int(c->local_maxpacket);
1164: } else {
1165: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1166: packet_put_int(c->remote_id);
1167: packet_put_int(c->self);
1168: }
1.75 markus 1169: } else {
1.114 markus 1170: debug("channel %d: not connected: %s",
1171: c->self, strerror(err));
1172: if (compat20) {
1173: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1174: packet_put_int(c->remote_id);
1175: packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1176: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1177: packet_put_cstring(strerror(err));
1178: packet_put_cstring("");
1179: }
1.75 markus 1180: } else {
1.114 markus 1181: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1182: packet_put_int(c->remote_id);
1.75 markus 1183: }
1.114 markus 1184: chan_mark_dead(c);
1.75 markus 1185: }
1.114 markus 1186: packet_send();
1.75 markus 1187: }
1188: }
1189:
1.41 markus 1190: int
1191: channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1192: {
1193: char buf[16*1024];
1194: int len;
1.25 markus 1195:
1.118 markus 1196: if (c->rfd != -1 &&
1.41 markus 1197: FD_ISSET(c->rfd, readset)) {
1198: len = read(c->rfd, buf, sizeof(buf));
1.53 markus 1199: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1200: return 1;
1.41 markus 1201: if (len <= 0) {
1.51 markus 1202: debug("channel %d: read<=0 rfd %d len %d",
1.41 markus 1203: c->self, c->rfd, len);
1.104 markus 1204: if (c->type != SSH_CHANNEL_OPEN) {
1205: debug("channel %d: not open", c->self);
1.113 markus 1206: chan_mark_dead(c);
1.103 markus 1207: return -1;
1.104 markus 1208: } else if (compat13) {
1.41 markus 1209: buffer_consume(&c->output, buffer_len(&c->output));
1210: c->type = SSH_CHANNEL_INPUT_DRAINING;
1.121 markus 1211: debug("channel %d: input draining.", c->self);
1.25 markus 1212: } else {
1.41 markus 1213: chan_read_failed(c);
1.25 markus 1214: }
1.41 markus 1215: return -1;
1216: }
1.65 markus 1217: if(c->input_filter != NULL) {
1.66 markus 1218: if (c->input_filter(c, buf, len) == -1) {
1.105 markus 1219: debug("channel %d: filter stops", c->self);
1.65 markus 1220: chan_read_failed(c);
1221: }
1222: } else {
1223: buffer_append(&c->input, buf, len);
1224: }
1.41 markus 1225: }
1226: return 1;
1227: }
1228: int
1229: channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1230: {
1.95 markus 1231: struct termios tio;
1.41 markus 1232: int len;
1.25 markus 1233:
1.41 markus 1234: /* Send buffered output data to the socket. */
1.118 markus 1235: if (c->wfd != -1 &&
1.41 markus 1236: FD_ISSET(c->wfd, writeset) &&
1237: buffer_len(&c->output) > 0) {
1238: len = write(c->wfd, buffer_ptr(&c->output),
1.53 markus 1239: buffer_len(&c->output));
1240: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1241: return 1;
1.41 markus 1242: if (len <= 0) {
1.104 markus 1243: if (c->type != SSH_CHANNEL_OPEN) {
1244: debug("channel %d: not open", c->self);
1.113 markus 1245: chan_mark_dead(c);
1.104 markus 1246: return -1;
1247: } else if (compat13) {
1.41 markus 1248: buffer_consume(&c->output, buffer_len(&c->output));
1.121 markus 1249: debug("channel %d: input draining.", c->self);
1.41 markus 1250: c->type = SSH_CHANNEL_INPUT_DRAINING;
1251: } else {
1252: chan_write_failed(c);
1253: }
1254: return -1;
1.91 markus 1255: }
1256: if (compat20 && c->isatty) {
1257: if (tcgetattr(c->wfd, &tio) == 0 &&
1258: !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1259: /*
1260: * Simulate echo to reduce the impact of
1.96 markus 1261: * traffic analysis. We need to match the
1.95 markus 1262: * size of a SSH2_MSG_CHANNEL_DATA message
1263: * (4 byte channel id + data)
1.91 markus 1264: */
1.95 markus 1265: packet_send_ignore(4 + len);
1.91 markus 1266: packet_send();
1267: }
1.25 markus 1268: }
1.41 markus 1269: buffer_consume(&c->output, len);
1.44 markus 1270: if (compat20 && len > 0) {
1271: c->local_consumed += len;
1272: }
1273: }
1274: return 1;
1275: }
1276: int
1277: channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1278: {
1279: char buf[16*1024];
1280: int len;
1281:
1.45 markus 1282: /** XXX handle drain efd, too */
1.44 markus 1283: if (c->efd != -1) {
1284: if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1285: FD_ISSET(c->efd, writeset) &&
1286: buffer_len(&c->extended) > 0) {
1287: len = write(c->efd, buffer_ptr(&c->extended),
1288: buffer_len(&c->extended));
1.70 markus 1289: debug2("channel %d: written %d to efd %d",
1.44 markus 1290: c->self, len, c->efd);
1.93 markus 1291: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1292: return 1;
1293: if (len <= 0) {
1294: debug2("channel %d: closing write-efd %d",
1295: c->self, c->efd);
1296: close(c->efd);
1297: c->efd = -1;
1298: } else {
1.44 markus 1299: buffer_consume(&c->extended, len);
1300: c->local_consumed += len;
1301: }
1302: } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1303: FD_ISSET(c->efd, readset)) {
1304: len = read(c->efd, buf, sizeof(buf));
1.70 markus 1305: debug2("channel %d: read %d from efd %d",
1.44 markus 1306: c->self, len, c->efd);
1.93 markus 1307: if (len < 0 && (errno == EINTR || errno == EAGAIN))
1308: return 1;
1309: if (len <= 0) {
1310: debug2("channel %d: closing read-efd %d",
1.45 markus 1311: c->self, c->efd);
1312: close(c->efd);
1313: c->efd = -1;
1.93 markus 1314: } else {
1.44 markus 1315: buffer_append(&c->extended, buf, len);
1.93 markus 1316: }
1.44 markus 1317: }
1318: }
1319: return 1;
1320: }
1321: int
1.93 markus 1322: channel_check_window(Channel *c)
1.44 markus 1323: {
1.104 markus 1324: if (c->type == SSH_CHANNEL_OPEN &&
1325: !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1.44 markus 1326: c->local_window < c->local_window_max/2 &&
1327: c->local_consumed > 0) {
1328: packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1329: packet_put_int(c->remote_id);
1330: packet_put_int(c->local_consumed);
1331: packet_send();
1.70 markus 1332: debug2("channel %d: window %d sent adjust %d",
1.44 markus 1333: c->self, c->local_window,
1334: c->local_consumed);
1335: c->local_window += c->local_consumed;
1336: c->local_consumed = 0;
1.1 deraadt 1337: }
1.41 markus 1338: return 1;
1.1 deraadt 1339: }
1340:
1.41 markus 1341: void
1342: channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1343: {
1344: channel_handle_rfd(c, readset, writeset);
1345: channel_handle_wfd(c, readset, writeset);
1346: }
1.1 deraadt 1347:
1.41 markus 1348: void
1.44 markus 1349: channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1350: {
1351: channel_handle_rfd(c, readset, writeset);
1352: channel_handle_wfd(c, readset, writeset);
1353: channel_handle_efd(c, readset, writeset);
1.93 markus 1354:
1355: channel_check_window(c);
1.44 markus 1356: }
1357:
1358: void
1.41 markus 1359: channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1.1 deraadt 1360: {
1.41 markus 1361: int len;
1362: /* Send buffered output data to the socket. */
1363: if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1364: len = write(c->sock, buffer_ptr(&c->output),
1365: buffer_len(&c->output));
1366: if (len <= 0)
1367: buffer_consume(&c->output, buffer_len(&c->output));
1368: else
1369: buffer_consume(&c->output, len);
1370: }
1371: }
1.25 markus 1372:
1.41 markus 1373: void
1.44 markus 1374: channel_handler_init_20(void)
1375: {
1376: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
1.51 markus 1377: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.44 markus 1378: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1.73 markus 1379: channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1.51 markus 1380: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1.73 markus 1381: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1382: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1383: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.44 markus 1384:
1385: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1386: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1.73 markus 1387: channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1.51 markus 1388: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1.73 markus 1389: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1.75 markus 1390: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.104 markus 1391: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
1.44 markus 1392: }
1393:
1394: void
1.41 markus 1395: channel_handler_init_13(void)
1396: {
1397: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1398: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1399: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1400: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1401: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1402: channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1403: channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1.75 markus 1404: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1405: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1406:
1.41 markus 1407: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1408: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1409: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1410: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1411: channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1.75 markus 1412: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.104 markus 1413: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
1.41 markus 1414: }
1.25 markus 1415:
1.41 markus 1416: void
1417: channel_handler_init_15(void)
1418: {
1419: channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
1.51 markus 1420: channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1.41 markus 1421: channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1422: channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1423: channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1.75 markus 1424: channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1.103 markus 1425: channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1.25 markus 1426:
1.41 markus 1427: channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1428: channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1429: channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1430: channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1.75 markus 1431: channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1.104 markus 1432: channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
1.41 markus 1433: }
1.27 markus 1434:
1.41 markus 1435: void
1436: channel_handler_init(void)
1437: {
1438: int i;
1439: for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1440: channel_pre[i] = NULL;
1441: channel_post[i] = NULL;
1442: }
1.44 markus 1443: if (compat20)
1444: channel_handler_init_20();
1445: else if (compat13)
1.41 markus 1446: channel_handler_init_13();
1447: else
1448: channel_handler_init_15();
1449: }
1.25 markus 1450:
1.49 markus 1451: void
1.41 markus 1452: channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1453: {
1454: static int did_init = 0;
1455: int i;
1456: Channel *c;
1.25 markus 1457:
1.41 markus 1458: if (!did_init) {
1459: channel_handler_init();
1460: did_init = 1;
1461: }
1462: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1463: c = channels[i];
1464: if (c == NULL)
1.41 markus 1465: continue;
1.118 markus 1466: if (ftab[c->type] != NULL)
1467: (*ftab[c->type])(c, readset, writeset);
1.93 markus 1468: if (chan_is_dead(c)) {
1469: /*
1470: * we have to remove the fd's from the select mask
1471: * before the channels are free'd and the fd's are
1472: * closed
1473: */
1474: if (c->wfd != -1)
1475: FD_CLR(c->wfd, writeset);
1476: if (c->rfd != -1)
1477: FD_CLR(c->rfd, readset);
1478: if (c->efd != -1) {
1479: if (c->extended_usage == CHAN_EXTENDED_READ)
1480: FD_CLR(c->efd, readset);
1481: if (c->extended_usage == CHAN_EXTENDED_WRITE)
1482: FD_CLR(c->efd, writeset);
1483: }
1.113 markus 1484: channel_free(c);
1.93 markus 1485: }
1.1 deraadt 1486: }
1487: }
1488:
1.121 markus 1489: /*
1490: * Allocate/update select bitmasks and add any bits relevant to channels in
1491: * select bitmasks.
1492: */
1.49 markus 1493: void
1.100 markus 1494: channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1495: int rekeying)
1.41 markus 1496: {
1.84 markus 1497: int n;
1498: u_int sz;
1499:
1500: n = MAX(*maxfdp, channel_max_fd);
1501:
1502: sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1503: if (*readsetp == NULL || n > *maxfdp) {
1504: if (*readsetp)
1505: xfree(*readsetp);
1506: if (*writesetp)
1507: xfree(*writesetp);
1508: *readsetp = xmalloc(sz);
1509: *writesetp = xmalloc(sz);
1510: *maxfdp = n;
1511: }
1512: memset(*readsetp, 0, sz);
1513: memset(*writesetp, 0, sz);
1514:
1.100 markus 1515: if (!rekeying)
1516: channel_handler(channel_pre, *readsetp, *writesetp);
1.41 markus 1517: }
1518:
1.121 markus 1519: /*
1520: * After select, perform any appropriate operations for channels which have
1521: * events pending.
1522: */
1.49 markus 1523: void
1.41 markus 1524: channel_after_select(fd_set * readset, fd_set * writeset)
1525: {
1526: channel_handler(channel_post, readset, writeset);
1527: }
1528:
1.121 markus 1529:
1.84 markus 1530: /* If there is data to send to the connection, enqueue some of it now. */
1.1 deraadt 1531:
1.49 markus 1532: void
1.25 markus 1533: channel_output_poll()
1.1 deraadt 1534: {
1.25 markus 1535: int len, i;
1.41 markus 1536: Channel *c;
1.1 deraadt 1537:
1.25 markus 1538: for (i = 0; i < channels_alloc; i++) {
1.113 markus 1539: c = channels[i];
1540: if (c == NULL)
1541: continue;
1.37 markus 1542:
1.121 markus 1543: /*
1544: * We are only interested in channels that can have buffered
1545: * incoming data.
1546: */
1.37 markus 1547: if (compat13) {
1.41 markus 1548: if (c->type != SSH_CHANNEL_OPEN &&
1549: c->type != SSH_CHANNEL_INPUT_DRAINING)
1.37 markus 1550: continue;
1551: } else {
1.41 markus 1552: if (c->type != SSH_CHANNEL_OPEN)
1.37 markus 1553: continue;
1554: }
1.46 markus 1555: if (compat20 &&
1556: (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1.93 markus 1557: /* XXX is this true? */
1.97 markus 1558: debug2("channel %d: no data after CLOSE", c->self);
1.44 markus 1559: continue;
1560: }
1.25 markus 1561:
1562: /* Get the amount of buffered data for this channel. */
1.93 markus 1563: if ((c->istate == CHAN_INPUT_OPEN ||
1564: c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1565: (len = buffer_len(&c->input)) > 0) {
1.121 markus 1566: /*
1567: * Send some data for the other side over the secure
1568: * connection.
1569: */
1.44 markus 1570: if (compat20) {
1571: if (len > c->remote_window)
1572: len = c->remote_window;
1573: if (len > c->remote_maxpacket)
1574: len = c->remote_maxpacket;
1.25 markus 1575: } else {
1.44 markus 1576: if (packet_is_interactive()) {
1577: if (len > 1024)
1578: len = 512;
1579: } else {
1580: /* Keep the packets at reasonable size. */
1581: if (len > packet_get_maxsize()/2)
1582: len = packet_get_maxsize()/2;
1583: }
1.25 markus 1584: }
1.41 markus 1585: if (len > 0) {
1.44 markus 1586: packet_start(compat20 ?
1587: SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1.41 markus 1588: packet_put_int(c->remote_id);
1589: packet_put_string(buffer_ptr(&c->input), len);
1590: packet_send();
1591: buffer_consume(&c->input, len);
1592: c->remote_window -= len;
1593: }
1594: } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1.25 markus 1595: if (compat13)
1596: fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1.27 markus 1597: /*
1598: * input-buffer is empty and read-socket shutdown:
1599: * tell peer, that we will not send more data: send IEOF
1600: */
1.41 markus 1601: chan_ibuf_empty(c);
1.25 markus 1602: }
1.44 markus 1603: /* Send extended data, i.e. stderr */
1604: if (compat20 &&
1605: c->remote_window > 0 &&
1606: (len = buffer_len(&c->extended)) > 0 &&
1607: c->extended_usage == CHAN_EXTENDED_READ) {
1.93 markus 1608: debug2("channel %d: rwin %d elen %d euse %d",
1609: c->self, c->remote_window, buffer_len(&c->extended),
1610: c->extended_usage);
1.44 markus 1611: if (len > c->remote_window)
1612: len = c->remote_window;
1613: if (len > c->remote_maxpacket)
1614: len = c->remote_maxpacket;
1615: packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1616: packet_put_int(c->remote_id);
1617: packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1618: packet_put_string(buffer_ptr(&c->extended), len);
1619: packet_send();
1620: buffer_consume(&c->extended, len);
1621: c->remote_window -= len;
1.93 markus 1622: debug2("channel %d: sent ext data %d", c->self, len);
1.44 markus 1623: }
1.25 markus 1624: }
1.1 deraadt 1625: }
1626:
1.121 markus 1627:
1628: /* -- protocol input */
1.1 deraadt 1629:
1.49 markus 1630: void
1.69 markus 1631: channel_input_data(int type, int plen, void *ctxt)
1.1 deraadt 1632: {
1.37 markus 1633: int id;
1.25 markus 1634: char *data;
1.77 markus 1635: u_int data_len;
1.41 markus 1636: Channel *c;
1.25 markus 1637:
1638: /* Get the channel number and verify it. */
1.37 markus 1639: id = packet_get_int();
1.41 markus 1640: c = channel_lookup(id);
1641: if (c == NULL)
1.37 markus 1642: packet_disconnect("Received data for nonexistent channel %d.", id);
1.25 markus 1643:
1644: /* Ignore any data for non-open channels (might happen on close) */
1.41 markus 1645: if (c->type != SSH_CHANNEL_OPEN &&
1646: c->type != SSH_CHANNEL_X11_OPEN)
1.37 markus 1647: return;
1648:
1649: /* same for protocol 1.5 if output end is no longer open */
1.41 markus 1650: if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
1.25 markus 1651: return;
1652:
1653: /* Get the data. */
1654: data = packet_get_string(&data_len);
1.48 markus 1655: packet_done();
1.41 markus 1656:
1.44 markus 1657: if (compat20){
1658: if (data_len > c->local_maxpacket) {
1659: log("channel %d: rcvd big packet %d, maxpack %d",
1660: c->self, data_len, c->local_maxpacket);
1661: }
1662: if (data_len > c->local_window) {
1663: log("channel %d: rcvd too much data %d, win %d",
1664: c->self, data_len, c->local_window);
1665: xfree(data);
1666: return;
1667: }
1668: c->local_window -= data_len;
1669: }else{
1670: packet_integrity_check(plen, 4 + 4 + data_len, type);
1671: }
1.41 markus 1672: buffer_append(&c->output, data, data_len);
1.25 markus 1673: xfree(data);
1.1 deraadt 1674: }
1.121 markus 1675:
1.49 markus 1676: void
1.69 markus 1677: channel_input_extended_data(int type, int plen, void *ctxt)
1.44 markus 1678: {
1679: int id;
1680: int tcode;
1681: char *data;
1.77 markus 1682: u_int data_len;
1.44 markus 1683: Channel *c;
1684:
1685: /* Get the channel number and verify it. */
1686: id = packet_get_int();
1687: c = channel_lookup(id);
1688:
1689: if (c == NULL)
1690: packet_disconnect("Received extended_data for bad channel %d.", id);
1691: if (c->type != SSH_CHANNEL_OPEN) {
1692: log("channel %d: ext data for non open", id);
1693: return;
1694: }
1695: tcode = packet_get_int();
1696: if (c->efd == -1 ||
1697: c->extended_usage != CHAN_EXTENDED_WRITE ||
1698: tcode != SSH2_EXTENDED_DATA_STDERR) {
1699: log("channel %d: bad ext data", c->self);
1700: return;
1701: }
1702: data = packet_get_string(&data_len);
1.48 markus 1703: packet_done();
1.44 markus 1704: if (data_len > c->local_window) {
1705: log("channel %d: rcvd too much extended_data %d, win %d",
1706: c->self, data_len, c->local_window);
1707: xfree(data);
1708: return;
1709: }
1.70 markus 1710: debug2("channel %d: rcvd ext data %d", c->self, data_len);
1.44 markus 1711: c->local_window -= data_len;
1712: buffer_append(&c->extended, data, data_len);
1713: xfree(data);
1714: }
1715:
1.49 markus 1716: void
1.69 markus 1717: channel_input_ieof(int type, int plen, void *ctxt)
1.41 markus 1718: {
1719: int id;
1720: Channel *c;
1721:
1722: packet_integrity_check(plen, 4, type);
1723:
1724: id = packet_get_int();
1725: c = channel_lookup(id);
1726: if (c == NULL)
1727: packet_disconnect("Received ieof for nonexistent channel %d.", id);
1728: chan_rcvd_ieof(c);
1729: }
1.1 deraadt 1730:
1.49 markus 1731: void
1.69 markus 1732: channel_input_close(int type, int plen, void *ctxt)
1.1 deraadt 1733: {
1.41 markus 1734: int id;
1735: Channel *c;
1.1 deraadt 1736:
1.41 markus 1737: packet_integrity_check(plen, 4, type);
1738:
1739: id = packet_get_int();
1740: c = channel_lookup(id);
1741: if (c == NULL)
1742: packet_disconnect("Received close for nonexistent channel %d.", id);
1.27 markus 1743:
1744: /*
1745: * Send a confirmation that we have closed the channel and no more
1746: * data is coming for it.
1747: */
1.25 markus 1748: packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1.41 markus 1749: packet_put_int(c->remote_id);
1.25 markus 1750: packet_send();
1751:
1.27 markus 1752: /*
1753: * If the channel is in closed state, we have sent a close request,
1754: * and the other side will eventually respond with a confirmation.
1755: * Thus, we cannot free the channel here, because then there would be
1756: * no-one to receive the confirmation. The channel gets freed when
1757: * the confirmation arrives.
1758: */
1.41 markus 1759: if (c->type != SSH_CHANNEL_CLOSED) {
1.27 markus 1760: /*
1761: * Not a closed channel - mark it as draining, which will
1762: * cause it to be freed later.
1763: */
1.41 markus 1764: buffer_consume(&c->input, buffer_len(&c->input));
1765: c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1.25 markus 1766: }
1.1 deraadt 1767: }
1768:
1.41 markus 1769: /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1.49 markus 1770: void
1.69 markus 1771: channel_input_oclose(int type, int plen, void *ctxt)
1.41 markus 1772: {
1773: int id = packet_get_int();
1774: Channel *c = channel_lookup(id);
1775: packet_integrity_check(plen, 4, type);
1776: if (c == NULL)
1777: packet_disconnect("Received oclose for nonexistent channel %d.", id);
1778: chan_rcvd_oclose(c);
1779: }
1.1 deraadt 1780:
1.49 markus 1781: void
1.69 markus 1782: channel_input_close_confirmation(int type, int plen, void *ctxt)
1.1 deraadt 1783: {
1.41 markus 1784: int id = packet_get_int();
1785: Channel *c = channel_lookup(id);
1.1 deraadt 1786:
1.48 markus 1787: packet_done();
1.41 markus 1788: if (c == NULL)
1789: packet_disconnect("Received close confirmation for "
1790: "out-of-range channel %d.", id);
1791: if (c->type != SSH_CHANNEL_CLOSED)
1792: packet_disconnect("Received close confirmation for "
1793: "non-closed channel %d (type %d).", id, c->type);
1.113 markus 1794: channel_free(c);
1.1 deraadt 1795: }
1796:
1.49 markus 1797: void
1.69 markus 1798: channel_input_open_confirmation(int type, int plen, void *ctxt)
1.1 deraadt 1799: {
1.41 markus 1800: int id, remote_id;
1801: Channel *c;
1.1 deraadt 1802:
1.44 markus 1803: if (!compat20)
1804: packet_integrity_check(plen, 4 + 4, type);
1.25 markus 1805:
1.41 markus 1806: id = packet_get_int();
1807: c = channel_lookup(id);
1.25 markus 1808:
1.41 markus 1809: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1810: packet_disconnect("Received open confirmation for "
1811: "non-opening channel %d.", id);
1812: remote_id = packet_get_int();
1.27 markus 1813: /* Record the remote channel number and mark that the channel is now open. */
1.41 markus 1814: c->remote_id = remote_id;
1815: c->type = SSH_CHANNEL_OPEN;
1.44 markus 1816:
1817: if (compat20) {
1818: c->remote_window = packet_get_int();
1819: c->remote_maxpacket = packet_get_int();
1.48 markus 1820: packet_done();
1.44 markus 1821: if (c->cb_fn != NULL && c->cb_event == type) {
1.70 markus 1822: debug2("callback start");
1.44 markus 1823: c->cb_fn(c->self, c->cb_arg);
1.70 markus 1824: debug2("callback done");
1.44 markus 1825: }
1826: debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1827: c->remote_window, c->remote_maxpacket);
1828: }
1.1 deraadt 1829: }
1830:
1.114 markus 1831: char *
1832: reason2txt(int reason)
1833: {
1834: switch(reason) {
1835: case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1836: return "administratively prohibited";
1837: case SSH2_OPEN_CONNECT_FAILED:
1838: return "connect failed";
1839: case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1840: return "unknown channel type";
1841: case SSH2_OPEN_RESOURCE_SHORTAGE:
1842: return "resource shortage";
1843: }
1.117 stevesk 1844: return "unknown reason";
1.114 markus 1845: }
1846:
1.49 markus 1847: void
1.69 markus 1848: channel_input_open_failure(int type, int plen, void *ctxt)
1.1 deraadt 1849: {
1.86 markus 1850: int id, reason;
1851: char *msg = NULL, *lang = NULL;
1.41 markus 1852: Channel *c;
1853:
1.44 markus 1854: if (!compat20)
1855: packet_integrity_check(plen, 4, type);
1.41 markus 1856:
1857: id = packet_get_int();
1858: c = channel_lookup(id);
1.25 markus 1859:
1.41 markus 1860: if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1861: packet_disconnect("Received open failure for "
1862: "non-opening channel %d.", id);
1.44 markus 1863: if (compat20) {
1.86 markus 1864: reason = packet_get_int();
1.110 markus 1865: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.86 markus 1866: msg = packet_get_string(NULL);
1867: lang = packet_get_string(NULL);
1868: }
1.48 markus 1869: packet_done();
1.114 markus 1870: log("channel %d: open failed: %s%s%s", id,
1871: reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1.86 markus 1872: if (msg != NULL)
1873: xfree(msg);
1874: if (lang != NULL)
1875: xfree(lang);
1.44 markus 1876: }
1.25 markus 1877: /* Free the channel. This will also close the socket. */
1.113 markus 1878: channel_free(c);
1.1 deraadt 1879: }
1880:
1.44 markus 1881: void
1.69 markus 1882: channel_input_channel_request(int type, int plen, void *ctxt)
1.44 markus 1883: {
1884: int id;
1885: Channel *c;
1886:
1887: id = packet_get_int();
1888: c = channel_lookup(id);
1889:
1890: if (c == NULL ||
1891: (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1892: packet_disconnect("Received request for "
1893: "non-open channel %d.", id);
1894: if (c->cb_fn != NULL && c->cb_event == type) {
1.70 markus 1895: debug2("callback start");
1.44 markus 1896: c->cb_fn(c->self, c->cb_arg);
1.70 markus 1897: debug2("callback done");
1.44 markus 1898: } else {
1899: char *service = packet_get_string(NULL);
1.94 markus 1900: debug("channel %d: rcvd request for %s", c->self, service);
1.70 markus 1901: debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1.44 markus 1902: xfree(service);
1903: }
1904: }
1905:
1.121 markus 1906: void
1907: channel_input_window_adjust(int type, int plen, void *ctxt)
1908: {
1909: Channel *c;
1910: int id, adjust;
1911:
1912: if (!compat20)
1913: return;
1914:
1915: /* Get the channel number and verify it. */
1916: id = packet_get_int();
1917: c = channel_lookup(id);
1.1 deraadt 1918:
1.121 markus 1919: if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1920: log("Received window adjust for "
1921: "non-open channel %d.", id);
1922: return;
1923: }
1924: adjust = packet_get_int();
1925: packet_done();
1926: debug2("channel %d: rcvd adjust %d", id, adjust);
1927: c->remote_window += adjust;
1928: }
1.1 deraadt 1929:
1.121 markus 1930: void
1931: channel_input_port_open(int type, int plen, void *ctxt)
1.1 deraadt 1932: {
1.121 markus 1933: Channel *c = NULL;
1934: u_short host_port;
1935: char *host, *originator_string;
1936: int remote_id, sock = -1;
1937:
1938: remote_id = packet_get_int();
1939: host = packet_get_string(NULL);
1940: host_port = packet_get_int();
1.25 markus 1941:
1.121 markus 1942: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1943: originator_string = packet_get_string(NULL);
1944: } else {
1945: originator_string = xstrdup("unknown (remote did not supply name)");
1946: }
1947: packet_done();
1948: sock = channel_connect_to(host, host_port);
1949: if (sock != -1) {
1950: c = channel_new("connected socket",
1951: SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
1952: originator_string, 1);
1953: if (c == NULL) {
1954: error("channel_input_port_open: channel_new failed");
1955: close(sock);
1956: } else {
1957: c->remote_id = remote_id;
1.25 markus 1958: }
1959: }
1.121 markus 1960: if (c == NULL) {
1961: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1962: packet_put_int(remote_id);
1963: packet_send();
1964: }
1965: xfree(host);
1.1 deraadt 1966: }
1967:
1.121 markus 1968:
1969: /* -- tcp forwarding */
1970:
1.27 markus 1971: /*
1972: * Initiate forwarding of connections to local port "port" through the secure
1973: * channel to host:port from remote side.
1974: */
1.87 markus 1975: int
1.73 markus 1976: channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1977: u_short port_to_connect, int gateway_ports)
1978: {
1.87 markus 1979: return channel_request_forwarding(
1.73 markus 1980: NULL, listen_port,
1981: host_to_connect, port_to_connect,
1982: gateway_ports, /*remote_fwd*/ 0);
1983: }
1.1 deraadt 1984:
1.73 markus 1985: /*
1986: * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1987: * (SSH_CHANNEL_RPORT_LISTENER).
1988: */
1.87 markus 1989: int
1.73 markus 1990: channel_request_forwarding(
1991: const char *listen_address, u_short listen_port,
1992: const char *host_to_connect, u_short port_to_connect,
1993: int gateway_ports, int remote_fwd)
1.25 markus 1994: {
1.113 markus 1995: Channel *c;
1.121 markus 1996: int success, sock, on = 1, type;
1.35 markus 1997: struct addrinfo hints, *ai, *aitop;
1998: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1.73 markus 1999: const char *host;
1.28 markus 2000: struct linger linger;
1.25 markus 2001:
1.87 markus 2002: success = 0;
2003:
1.73 markus 2004: if (remote_fwd) {
2005: host = listen_address;
1.121 markus 2006: type = SSH_CHANNEL_RPORT_LISTENER;
1.73 markus 2007: } else {
2008: host = host_to_connect;
1.121 markus 2009: type = SSH_CHANNEL_PORT_LISTENER;
1.73 markus 2010: }
2011:
1.113 markus 2012: if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
1.87 markus 2013: error("Forward host name too long.");
2014: return success;
2015: }
1.25 markus 2016:
1.73 markus 2017: /* XXX listen_address is currently ignored */
1.28 markus 2018: /*
1.35 markus 2019: * getaddrinfo returns a loopback address if the hostname is
2020: * set to NULL and hints.ai_flags is not AI_PASSIVE
1.28 markus 2021: */
1.35 markus 2022: memset(&hints, 0, sizeof(hints));
2023: hints.ai_family = IPv4or6;
2024: hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2025: hints.ai_socktype = SOCK_STREAM;
1.73 markus 2026: snprintf(strport, sizeof strport, "%d", listen_port);
1.35 markus 2027: if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2028: packet_disconnect("getaddrinfo: fatal error");
2029:
2030: for (ai = aitop; ai; ai = ai->ai_next) {
2031: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2032: continue;
2033: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2034: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.73 markus 2035: error("channel_request_forwarding: getnameinfo failed");
1.35 markus 2036: continue;
2037: }
2038: /* Create a port to listen for the host. */
2039: sock = socket(ai->ai_family, SOCK_STREAM, 0);
2040: if (sock < 0) {
2041: /* this is no error since kernel may not support ipv6 */
2042: verbose("socket: %.100s", strerror(errno));
2043: continue;
2044: }
2045: /*
2046: * Set socket options. We would like the socket to disappear
2047: * as soon as it has been closed for whatever reason.
2048: */
2049: setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2050: linger.l_onoff = 1;
2051: linger.l_linger = 5;
2052: setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2053: debug("Local forwarding listening on %s port %s.", ntop, strport);
2054:
2055: /* Bind the socket to the address. */
2056: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2057: /* address can be in use ipv6 address is already bound */
2058: verbose("bind: %.100s", strerror(errno));
2059: close(sock);
2060: continue;
2061: }
2062: /* Start listening for connections on the socket. */
2063: if (listen(sock, 5) < 0) {
2064: error("listen: %.100s", strerror(errno));
2065: close(sock);
2066: continue;
2067: }
2068: /* Allocate a channel number for the socket. */
1.121 markus 2069: c = channel_new("port listener", type, sock, sock, -1,
1.51 markus 2070: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1.71 markus 2071: 0, xstrdup("port listener"), 1);
1.113 markus 2072: if (c == NULL) {
2073: error("channel_request_forwarding: channel_new failed");
2074: close(sock);
2075: continue;
2076: }
2077: strlcpy(c->path, host, sizeof(c->path));
2078: c->host_port = port_to_connect;
2079: c->listening_port = listen_port;
1.35 markus 2080: success = 1;
2081: }
2082: if (success == 0)
1.87 markus 2083: error("channel_request_forwarding: cannot listen to port: %d",
2084: listen_port);
1.35 markus 2085: freeaddrinfo(aitop);
1.87 markus 2086: return success;
1.25 markus 2087: }
1.1 deraadt 2088:
1.27 markus 2089: /*
2090: * Initiate forwarding of connections to port "port" on remote host through
2091: * the secure channel to host:port from local side.
2092: */
1.1 deraadt 2093:
1.49 markus 2094: void
1.73 markus 2095: channel_request_remote_forwarding(u_short listen_port,
2096: const char *host_to_connect, u_short port_to_connect)
1.25 markus 2097: {
1.73 markus 2098: int payload_len, type, success = 0;
2099:
1.25 markus 2100: /* Record locally that connection to this host/port is permitted. */
2101: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2102: fatal("channel_request_remote_forwarding: too many forwards");
2103:
2104: /* Send the forward request to the remote side. */
1.44 markus 2105: if (compat20) {
2106: const char *address_to_bind = "0.0.0.0";
2107: packet_start(SSH2_MSG_GLOBAL_REQUEST);
2108: packet_put_cstring("tcpip-forward");
2109: packet_put_char(0); /* boolean: want reply */
2110: packet_put_cstring(address_to_bind);
2111: packet_put_int(listen_port);
1.73 markus 2112: packet_send();
2113: packet_write_wait();
2114: /* Assume that server accepts the request */
2115: success = 1;
1.44 markus 2116: } else {
2117: packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1.50 markus 2118: packet_put_int(listen_port);
2119: packet_put_cstring(host_to_connect);
1.44 markus 2120: packet_put_int(port_to_connect);
2121: packet_send();
2122: packet_write_wait();
1.73 markus 2123:
2124: /* Wait for response from the remote side. */
2125: type = packet_read(&payload_len);
2126: switch (type) {
2127: case SSH_SMSG_SUCCESS:
2128: success = 1;
2129: break;
2130: case SSH_SMSG_FAILURE:
2131: log("Warning: Server denied remote port forwarding.");
2132: break;
2133: default:
2134: /* Unknown packet */
2135: packet_disconnect("Protocol error for port forward request:"
2136: "received packet type %d.", type);
2137: }
2138: }
2139: if (success) {
2140: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2141: permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2142: permitted_opens[num_permitted_opens].listen_port = listen_port;
2143: num_permitted_opens++;
1.44 markus 2144: }
1.1 deraadt 2145: }
2146:
1.27 markus 2147: /*
2148: * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2149: * listening for the port, and sends back a success reply (or disconnect
2150: * message if there was an error). This never returns if there was an error.
2151: */
1.1 deraadt 2152:
1.49 markus 2153: void
1.56 markus 2154: channel_input_port_forward_request(int is_root, int gateway_ports)
1.1 deraadt 2155: {
1.31 markus 2156: u_short port, host_port;
1.25 markus 2157: char *hostname;
1.1 deraadt 2158:
1.25 markus 2159: /* Get arguments from the packet. */
2160: port = packet_get_int();
2161: hostname = packet_get_string(NULL);
2162: host_port = packet_get_int();
2163:
1.27 markus 2164: /*
2165: * Check that an unprivileged user is not trying to forward a
2166: * privileged port.
2167: */
1.25 markus 2168: if (port < IPPORT_RESERVED && !is_root)
2169: packet_disconnect("Requested forwarding of port %d but user is not root.",
2170: port);
1.73 markus 2171: /* Initiate forwarding */
1.56 markus 2172: channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
1.25 markus 2173:
2174: /* Free the argument string. */
2175: xfree(hostname);
1.1 deraadt 2176: }
2177:
1.99 markus 2178: /*
2179: * Permits opening to any host/port if permitted_opens[] is empty. This is
2180: * usually called by the server, because the user could connect to any port
2181: * anyway, and the server has no way to know but to trust the client anyway.
2182: */
2183: void
2184: channel_permit_all_opens()
2185: {
2186: if (num_permitted_opens == 0)
2187: all_opens_permitted = 1;
2188: }
2189:
1.101 markus 2190: void
1.99 markus 2191: channel_add_permitted_opens(char *host, int port)
2192: {
2193: if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2194: fatal("channel_request_remote_forwarding: too many forwards");
2195: debug("allow port forwarding to host %s port %d", host, port);
2196:
2197: permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2198: permitted_opens[num_permitted_opens].port_to_connect = port;
2199: num_permitted_opens++;
2200:
2201: all_opens_permitted = 0;
2202: }
2203:
1.101 markus 2204: void
1.99 markus 2205: channel_clear_permitted_opens(void)
2206: {
2207: int i;
2208:
2209: for (i = 0; i < num_permitted_opens; i++)
2210: xfree(permitted_opens[i].host_to_connect);
2211: num_permitted_opens = 0;
2212:
2213: }
2214:
2215:
2216: /* return socket to remote host, port */
1.41 markus 2217: int
1.99 markus 2218: connect_to(const char *host, u_short port)
1.41 markus 2219: {
2220: struct addrinfo hints, *ai, *aitop;
2221: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2222: int gaierr;
2223: int sock = -1;
2224:
2225: memset(&hints, 0, sizeof(hints));
2226: hints.ai_family = IPv4or6;
2227: hints.ai_socktype = SOCK_STREAM;
1.99 markus 2228: snprintf(strport, sizeof strport, "%d", port);
1.41 markus 2229: if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1.99 markus 2230: error("connect_to %.100s: unknown host (%s)", host,
2231: gai_strerror(gaierr));
1.41 markus 2232: return -1;
2233: }
2234: for (ai = aitop; ai; ai = ai->ai_next) {
2235: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2236: continue;
2237: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2238: strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1.99 markus 2239: error("connect_to: getnameinfo failed");
1.41 markus 2240: continue;
2241: }
2242: sock = socket(ai->ai_family, SOCK_STREAM, 0);
2243: if (sock < 0) {
2244: error("socket: %.100s", strerror(errno));
2245: continue;
2246: }
1.80 markus 2247: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
1.75 markus 2248: fatal("connect_to: F_SETFL: %s", strerror(errno));
2249: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2250: errno != EINPROGRESS) {
1.99 markus 2251: error("connect_to %.100s port %s: %.100s", ntop, strport,
1.41 markus 2252: strerror(errno));
2253: close(sock);
1.89 stevesk 2254: continue; /* fail -- try next */
1.41 markus 2255: }
2256: break; /* success */
2257:
2258: }
2259: freeaddrinfo(aitop);
2260: if (!ai) {
1.99 markus 2261: error("connect_to %.100s port %d: failed.", host, port);
1.41 markus 2262: return -1;
2263: }
2264: /* success */
2265: return sock;
2266: }
1.99 markus 2267:
1.73 markus 2268: int
2269: channel_connect_by_listen_adress(u_short listen_port)
2270: {
2271: int i;
1.99 markus 2272:
1.73 markus 2273: for (i = 0; i < num_permitted_opens; i++)
2274: if (permitted_opens[i].listen_port == listen_port)
1.99 markus 2275: return connect_to(
1.73 markus 2276: permitted_opens[i].host_to_connect,
2277: permitted_opens[i].port_to_connect);
1.74 markus 2278: error("WARNING: Server requests forwarding for unknown listen_port %d",
2279: listen_port);
1.73 markus 2280: return -1;
2281: }
2282:
1.99 markus 2283: /* Check if connecting to that port is permitted and connect. */
2284: int
2285: channel_connect_to(const char *host, u_short port)
2286: {
2287: int i, permit;
2288:
2289: permit = all_opens_permitted;
2290: if (!permit) {
2291: for (i = 0; i < num_permitted_opens; i++)
2292: if (permitted_opens[i].port_to_connect == port &&
2293: strcmp(permitted_opens[i].host_to_connect, host) == 0)
2294: permit = 1;
2295:
2296: }
2297: if (!permit) {
2298: log("Received request to connect to host %.100s port %d, "
2299: "but the request was denied.", host, port);
2300: return -1;
2301: }
2302: return connect_to(host, port);
2303: }
2304:
1.121 markus 2305: /* -- X11 forwarding */
1.1 deraadt 2306:
1.27 markus 2307: /*
2308: * Creates an internet domain socket for listening for X11 connections.
2309: * Returns a suitable value for the DISPLAY variable, or NULL if an error
2310: * occurs.
2311: */
1.25 markus 2312: char *
1.33 markus 2313: x11_create_display_inet(int screen_number, int x11_display_offset)
1.1 deraadt 2314: {
1.31 markus 2315: int display_number, sock;
2316: u_short port;
1.35 markus 2317: struct addrinfo hints, *ai, *aitop;
2318: char strport[NI_MAXSERV];
2319: int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2320: char display[512];
1.25 markus 2321: char hostname[MAXHOSTNAMELEN];
2322:
1.33 markus 2323: for (display_number = x11_display_offset;
1.25 markus 2324: display_number < MAX_DISPLAYS;
2325: display_number++) {
2326: port = 6000 + display_number;
1.35 markus 2327: memset(&hints, 0, sizeof(hints));
2328: hints.ai_family = IPv4or6;
1.36 markus 2329: hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1.35 markus 2330: hints.ai_socktype = SOCK_STREAM;
2331: snprintf(strport, sizeof strport, "%d", port);
2332: if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2333: error("getaddrinfo: %.100s", gai_strerror(gaierr));
1.25 markus 2334: return NULL;
2335: }
1.35 markus 2336: for (ai = aitop; ai; ai = ai->ai_next) {
2337: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2338: continue;
2339: sock = socket(ai->ai_family, SOCK_STREAM, 0);
2340: if (sock < 0) {
2341: error("socket: %.100s", strerror(errno));
2342: return NULL;
2343: }
2344: if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2345: debug("bind port %d: %.100s", port, strerror(errno));
2346: shutdown(sock, SHUT_RDWR);
2347: close(sock);
2348: for (n = 0; n < num_socks; n++) {
2349: shutdown(socks[n], SHUT_RDWR);
2350: close(socks[n]);
2351: }
2352: num_socks = 0;
2353: break;
2354: }
2355: socks[num_socks++] = sock;
2356: if (num_socks == NUM_SOCKS)
2357: break;
1.25 markus 2358: }
1.83 stevesk 2359: freeaddrinfo(aitop);
1.35 markus 2360: if (num_socks > 0)
2361: break;
1.25 markus 2362: }
2363: if (display_number >= MAX_DISPLAYS) {
2364: error("Failed to allocate internet-domain X11 display socket.");
2365: return NULL;
2366: }
2367: /* Start listening for connections on the socket. */
1.35 markus 2368: for (n = 0; n < num_socks; n++) {
2369: sock = socks[n];
2370: if (listen(sock, 5) < 0) {
2371: error("listen: %.100s", strerror(errno));
2372: shutdown(sock, SHUT_RDWR);
2373: close(sock);
2374: return NULL;
2375: }
1.25 markus 2376: }
1.35 markus 2377:
1.25 markus 2378: /* Set up a suitable value for the DISPLAY variable. */
2379: if (gethostname(hostname, sizeof(hostname)) < 0)
2380: fatal("gethostname: %.100s", strerror(errno));
1.35 markus 2381: snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1.25 markus 2382: display_number, screen_number);
2383:
1.35 markus 2384: /* Allocate a channel for each socket. */
2385: for (n = 0; n < num_socks; n++) {
2386: sock = socks[n];
1.51 markus 2387: (void) channel_new("x11 listener",
2388: SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2389: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.71 markus 2390: 0, xstrdup("X11 inet listener"), 1);
1.35 markus 2391: }
1.1 deraadt 2392:
1.25 markus 2393: /* Return a suitable value for the DISPLAY environment variable. */
1.35 markus 2394: return xstrdup(display);
1.1 deraadt 2395: }
2396:
2397: #ifndef X_UNIX_PATH
2398: #define X_UNIX_PATH "/tmp/.X11-unix/X"
2399: #endif
2400:
2401: static
2402: int
1.77 markus 2403: connect_local_xsocket(u_int dnr)
1.1 deraadt 2404: {
1.25 markus 2405: static const char *const x_sockets[] = {
2406: X_UNIX_PATH "%u",
2407: "/var/X/.X11-unix/X" "%u",
2408: "/usr/spool/sockets/X11/" "%u",
2409: NULL
2410: };
2411: int sock;
2412: struct sockaddr_un addr;
2413: const char *const * path;
2414:
2415: for (path = x_sockets; *path; ++path) {
2416: sock = socket(AF_UNIX, SOCK_STREAM, 0);
2417: if (sock < 0)
2418: error("socket: %.100s", strerror(errno));
2419: memset(&addr, 0, sizeof(addr));
2420: addr.sun_family = AF_UNIX;
2421: snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2422: if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2423: return sock;
2424: close(sock);
2425: }
2426: error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2427: return -1;
1.1 deraadt 2428: }
2429:
1.51 markus 2430: int
2431: x11_connect_display(void)
1.1 deraadt 2432: {
1.51 markus 2433: int display_number, sock = 0;
1.25 markus 2434: const char *display;
1.51 markus 2435: char buf[1024], *cp;
1.35 markus 2436: struct addrinfo hints, *ai, *aitop;
2437: char strport[NI_MAXSERV];
2438: int gaierr;
1.25 markus 2439:
2440: /* Try to open a socket for the local X server. */
2441: display = getenv("DISPLAY");
2442: if (!display) {
2443: error("DISPLAY not set.");
1.51 markus 2444: return -1;
1.25 markus 2445: }
1.27 markus 2446: /*
2447: * Now we decode the value of the DISPLAY variable and make a
2448: * connection to the real X server.
2449: */
2450:
2451: /*
2452: * Check if it is a unix domain socket. Unix domain displays are in
2453: * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2454: */
1.25 markus 2455: if (strncmp(display, "unix:", 5) == 0 ||
2456: display[0] == ':') {
2457: /* Connect to the unix domain socket. */
2458: if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2459: error("Could not parse display number from DISPLAY: %.100s",
2460: display);
1.51 markus 2461: return -1;
1.25 markus 2462: }
2463: /* Create a socket. */
2464: sock = connect_local_xsocket(display_number);
2465: if (sock < 0)
1.51 markus 2466: return -1;
1.25 markus 2467:
2468: /* OK, we now have a connection to the display. */
1.51 markus 2469: return sock;
1.25 markus 2470: }
1.27 markus 2471: /*
2472: * Connect to an inet socket. The DISPLAY value is supposedly
2473: * hostname:d[.s], where hostname may also be numeric IP address.
2474: */
1.25 markus 2475: strncpy(buf, display, sizeof(buf));
2476: buf[sizeof(buf) - 1] = 0;
2477: cp = strchr(buf, ':');
2478: if (!cp) {
2479: error("Could not find ':' in DISPLAY: %.100s", display);
1.51 markus 2480: return -1;
1.25 markus 2481: }
2482: *cp = 0;
1.27 markus 2483: /* buf now contains the host name. But first we parse the display number. */
1.25 markus 2484: if (sscanf(cp + 1, "%d", &display_number) != 1) {
2485: error("Could not parse display number from DISPLAY: %.100s",
2486: display);
1.51 markus 2487: return -1;
1.25 markus 2488: }
1.35 markus 2489:
2490: /* Look up the host address */
2491: memset(&hints, 0, sizeof(hints));
2492: hints.ai_family = IPv4or6;
2493: hints.ai_socktype = SOCK_STREAM;
2494: snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2495: if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2496: error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1.51 markus 2497: return -1;
1.25 markus 2498: }
1.35 markus 2499: for (ai = aitop; ai; ai = ai->ai_next) {
2500: /* Create a socket. */
2501: sock = socket(ai->ai_family, SOCK_STREAM, 0);
2502: if (sock < 0) {
2503: debug("socket: %.100s", strerror(errno));
1.41 markus 2504: continue;
2505: }
2506: /* Connect it to the display. */
2507: if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2508: debug("connect %.100s port %d: %.100s", buf,
2509: 6000 + display_number, strerror(errno));
2510: close(sock);
2511: continue;
2512: }
2513: /* Success */
2514: break;
1.35 markus 2515: }
2516: freeaddrinfo(aitop);
2517: if (!ai) {
1.49 markus 2518: error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1.35 markus 2519: strerror(errno));
1.51 markus 2520: return -1;
1.25 markus 2521: }
1.51 markus 2522: return sock;
2523: }
2524:
2525: /*
2526: * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2527: * the remote channel number. We should do whatever we want, and respond
2528: * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2529: */
2530:
2531: void
1.69 markus 2532: x11_input_open(int type, int plen, void *ctxt)
1.51 markus 2533: {
1.113 markus 2534: Channel *c = NULL;
2535: int remote_id, sock = 0;
1.51 markus 2536: char *remote_host;
1.25 markus 2537:
1.113 markus 2538: debug("Received X11 open request.");
1.51 markus 2539:
1.113 markus 2540: remote_id = packet_get_int();
1.121 markus 2541:
2542: if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1.113 markus 2543: remote_host = packet_get_string(NULL);
1.51 markus 2544: } else {
2545: remote_host = xstrdup("unknown (remote did not supply name)");
2546: }
1.113 markus 2547: packet_done();
1.25 markus 2548:
1.51 markus 2549: /* Obtain a connection to the real X display. */
2550: sock = x11_connect_display();
1.113 markus 2551: if (sock != -1) {
2552: /* Allocate a channel for this connection. */
2553: c = channel_new("connected x11 socket",
2554: SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2555: remote_host, 1);
2556: if (c == NULL) {
2557: error("x11_input_open: channel_new failed");
2558: close(sock);
2559: } else {
2560: c->remote_id = remote_id;
2561: }
2562: }
2563: if (c == NULL) {
1.51 markus 2564: /* Send refusal to the remote host. */
2565: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 2566: packet_put_int(remote_id);
1.51 markus 2567: } else {
2568: /* Send a confirmation to the remote host. */
2569: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1.113 markus 2570: packet_put_int(remote_id);
2571: packet_put_int(c->self);
1.51 markus 2572: }
1.113 markus 2573: packet_send();
1.72 markus 2574: }
2575:
2576: /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2577: void
2578: deny_input_open(int type, int plen, void *ctxt)
2579: {
2580: int rchan = packet_get_int();
2581: switch(type){
2582: case SSH_SMSG_AGENT_OPEN:
2583: error("Warning: ssh server tried agent forwarding.");
2584: break;
2585: case SSH_SMSG_X11_OPEN:
2586: error("Warning: ssh server tried X11 forwarding.");
2587: break;
2588: default:
2589: error("deny_input_open: type %d plen %d", type, plen);
2590: break;
2591: }
2592: error("Warning: this is probably a break in attempt by a malicious server.");
2593: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2594: packet_put_int(rchan);
2595: packet_send();
1.1 deraadt 2596: }
2597:
1.27 markus 2598: /*
2599: * Requests forwarding of X11 connections, generates fake authentication
2600: * data, and enables authentication spoofing.
1.121 markus 2601: * This should be called in the client only.
1.27 markus 2602: */
1.49 markus 2603: void
1.51 markus 2604: x11_request_forwarding_with_spoofing(int client_session_id,
2605: const char *proto, const char *data)
1.1 deraadt 2606: {
1.77 markus 2607: u_int data_len = (u_int) strlen(data) / 2;
1.90 markus 2608: u_int i, value, len;
1.25 markus 2609: char *new_data;
2610: int screen_number;
2611: const char *cp;
2612: u_int32_t rand = 0;
2613:
2614: cp = getenv("DISPLAY");
2615: if (cp)
2616: cp = strchr(cp, ':');
2617: if (cp)
2618: cp = strchr(cp, '.');
2619: if (cp)
2620: screen_number = atoi(cp + 1);
2621: else
2622: screen_number = 0;
2623:
2624: /* Save protocol name. */
2625: x11_saved_proto = xstrdup(proto);
2626:
1.27 markus 2627: /*
2628: * Extract real authentication data and generate fake data of the
2629: * same length.
2630: */
1.25 markus 2631: x11_saved_data = xmalloc(data_len);
2632: x11_fake_data = xmalloc(data_len);
2633: for (i = 0; i < data_len; i++) {
2634: if (sscanf(data + 2 * i, "%2x", &value) != 1)
2635: fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2636: if (i % 4 == 0)
2637: rand = arc4random();
2638: x11_saved_data[i] = value;
2639: x11_fake_data[i] = rand & 0xff;
2640: rand >>= 8;
2641: }
2642: x11_saved_data_len = data_len;
2643: x11_fake_data_len = data_len;
2644:
2645: /* Convert the fake data into hex. */
1.90 markus 2646: len = 2 * data_len + 1;
2647: new_data = xmalloc(len);
1.25 markus 2648: for (i = 0; i < data_len; i++)
1.90 markus 2649: snprintf(new_data + 2 * i, len - 2 * i,
2650: "%02x", (u_char) x11_fake_data[i]);
1.25 markus 2651:
2652: /* Send the request packet. */
1.51 markus 2653: if (compat20) {
2654: channel_request_start(client_session_id, "x11-req", 0);
2655: packet_put_char(0); /* XXX bool single connection */
2656: } else {
2657: packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2658: }
2659: packet_put_cstring(proto);
2660: packet_put_cstring(new_data);
1.25 markus 2661: packet_put_int(screen_number);
2662: packet_send();
2663: packet_write_wait();
2664: xfree(new_data);
1.1 deraadt 2665: }
2666:
1.121 markus 2667:
2668: /* -- agent forwarding */
2669:
1.1 deraadt 2670: /* Sends a message to the server to request authentication fd forwarding. */
2671:
1.49 markus 2672: void
1.25 markus 2673: auth_request_forwarding()
1.1 deraadt 2674: {
1.25 markus 2675: packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2676: packet_send();
2677: packet_write_wait();
1.1 deraadt 2678: }
2679:
1.27 markus 2680: /*
2681: * Returns the name of the forwarded authentication socket. Returns NULL if
2682: * there is no forwarded authentication socket. The returned value points to
2683: * a static buffer.
2684: */
1.1 deraadt 2685:
1.25 markus 2686: char *
2687: auth_get_socket_name()
1.1 deraadt 2688: {
1.121 markus 2689: return auth_sock_name;
1.1 deraadt 2690: }
2691:
1.12 markus 2692: /* removes the agent forwarding socket */
2693:
1.49 markus 2694: void
1.123 markus 2695: auth_sock_cleanup_proc(void *_pw)
1.25 markus 2696: {
1.123 markus 2697: struct passwd *pw = _pw;
2698:
1.122 markus 2699: if (auth_sock_name) {
1.123 markus 2700: temporarily_use_uid(pw);
1.122 markus 2701: unlink(auth_sock_name);
2702: rmdir(auth_sock_dir);
2703: auth_sock_name = NULL;
1.123 markus 2704: restore_uid();
1.122 markus 2705: }
1.12 markus 2706: }
2707:
1.27 markus 2708: /*
1.59 markus 2709: * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
1.27 markus 2710: * This starts forwarding authentication requests.
2711: */
1.1 deraadt 2712:
1.59 markus 2713: int
1.25 markus 2714: auth_input_request_forwarding(struct passwd * pw)
1.1 deraadt 2715: {
1.113 markus 2716: Channel *nc;
2717: int sock;
1.25 markus 2718: struct sockaddr_un sunaddr;
2719:
1.121 markus 2720: if (auth_get_socket_name() != NULL) {
2721: error("authentication forwarding requested twice.");
2722: return 0;
2723: }
1.25 markus 2724:
2725: /* Temporarily drop privileged uid for mkdir/bind. */
1.102 markus 2726: temporarily_use_uid(pw);
1.25 markus 2727:
2728: /* Allocate a buffer for the socket name, and format the name. */
1.121 markus 2729: auth_sock_name = xmalloc(MAXPATHLEN);
2730: auth_sock_dir = xmalloc(MAXPATHLEN);
2731: strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1.25 markus 2732:
2733: /* Create private directory for socket */
1.121 markus 2734: if (mkdtemp(auth_sock_dir) == NULL) {
2735: packet_send_debug("Agent forwarding disabled: "
2736: "mkdtemp() failed: %.100s", strerror(errno));
1.59 markus 2737: restore_uid();
1.121 markus 2738: xfree(auth_sock_name);
2739: xfree(auth_sock_dir);
2740: auth_sock_name = NULL;
2741: auth_sock_dir = NULL;
1.59 markus 2742: return 0;
2743: }
1.121 markus 2744: snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2745: auth_sock_dir, (int) getpid());
1.25 markus 2746:
1.122 markus 2747: /* delete agent socket on fatal() */
1.123 markus 2748: fatal_add_cleanup(auth_sock_cleanup_proc, pw);
1.122 markus 2749:
1.25 markus 2750: /* Create the socket. */
2751: sock = socket(AF_UNIX, SOCK_STREAM, 0);
2752: if (sock < 0)
2753: packet_disconnect("socket: %.100s", strerror(errno));
2754:
2755: /* Bind it to the name. */
2756: memset(&sunaddr, 0, sizeof(sunaddr));
2757: sunaddr.sun_family = AF_UNIX;
1.121 markus 2758: strncpy(sunaddr.sun_path, auth_sock_name,
1.25 markus 2759: sizeof(sunaddr.sun_path));
2760:
2761: if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2762: packet_disconnect("bind: %.100s", strerror(errno));
2763:
2764: /* Restore the privileged uid. */
2765: restore_uid();
2766:
2767: /* Start listening on the socket. */
2768: if (listen(sock, 5) < 0)
2769: packet_disconnect("listen: %.100s", strerror(errno));
2770:
2771: /* Allocate a channel for the authentication agent socket. */
1.113 markus 2772: nc = channel_new("auth socket",
1.73 markus 2773: SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2774: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2775: 0, xstrdup("auth socket"), 1);
1.113 markus 2776: if (nc == NULL) {
2777: error("auth_input_request_forwarding: channel_new failed");
1.123 markus 2778: auth_sock_cleanup_proc(pw);
1.124 markus 2779: fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
1.113 markus 2780: close(sock);
2781: return 0;
2782: }
1.121 markus 2783: strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
1.59 markus 2784: return 1;
1.1 deraadt 2785: }
2786:
2787: /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2788:
1.49 markus 2789: void
1.69 markus 2790: auth_input_open_request(int type, int plen, void *ctxt)
1.1 deraadt 2791: {
1.113 markus 2792: Channel *c = NULL;
2793: int remote_id, sock;
1.121 markus 2794: char *name;
1.41 markus 2795:
2796: packet_integrity_check(plen, 4, type);
1.25 markus 2797:
2798: /* Read the remote channel number from the message. */
1.113 markus 2799: remote_id = packet_get_int();
1.25 markus 2800:
1.27 markus 2801: /*
2802: * Get a connection to the local authentication agent (this may again
2803: * get forwarded).
2804: */
1.25 markus 2805: sock = ssh_get_authentication_socket();
2806:
1.27 markus 2807: /*
2808: * If we could not connect the agent, send an error message back to
2809: * the server. This should never happen unless the agent dies,
2810: * because authentication forwarding is only enabled if we have an
2811: * agent.
2812: */
1.113 markus 2813: if (sock >= 0) {
1.121 markus 2814: name = xstrdup("authentication agent connection");
2815: c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2816: -1, 0, 0, 0, name, 1);
1.113 markus 2817: if (c == NULL) {
2818: error("auth_input_open_request: channel_new failed");
1.121 markus 2819: xfree(name);
1.113 markus 2820: close(sock);
2821: } else {
2822: c->remote_id = remote_id;
2823: }
2824: }
2825: if (c == NULL) {
1.25 markus 2826: packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1.113 markus 2827: packet_put_int(remote_id);
2828: } else {
2829: /* Send a confirmation to the remote host. */
2830: debug("Forwarding authentication connection.");
2831: packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2832: packet_put_int(remote_id);
2833: packet_put_int(c->self);
1.25 markus 2834: }
2835: packet_send();
1.1 deraadt 2836: }