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