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