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