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