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