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