Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 1 | /* $OpenBSD: channels.c,v 1.260 2006/07/22 20:48:22 stevesk Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * This file contains functions for generic socket connection forwarding. |
| 7 | * There is also code for initiating connection forwarding for X11 connections, |
| 8 | * arbitrary tcp/ip connections, and the authentication agent connection. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 10 | * As far as I am concerned, the code I have written for this software |
| 11 | * can be used freely for any purpose. Any derived versions of this |
| 12 | * software must be clearly marked as such, and if the derived work is |
| 13 | * incompatible with the protocol description in the RFC file, it must be |
| 14 | * called by a name other than "ssh" or "Secure Shell". |
| 15 | * |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 16 | * SSH2 support added by Markus Friedl. |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 17 | * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
| 42 | #include "includes.h" |
Damien Miller | 17e91c0 | 2006-03-15 11:28:34 +1100 | [diff] [blame] | 43 | |
| 44 | #include <sys/ioctl.h> |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | #include <sys/un.h> |
Damien Miller | efc04e7 | 2006-07-10 20:26:27 +1000 | [diff] [blame] | 47 | #include <sys/socket.h> |
| 48 | |
| 49 | #include <netinet/in.h> |
| 50 | #include <arpa/inet.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 51 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 52 | #include <errno.h> |
Damien Miller | be43ebf | 2006-07-24 13:51:51 +1000 | [diff] [blame] | 53 | #if defined(HAVE_NETDB_H) |
| 54 | # include <netdb.h> |
| 55 | #endif |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 56 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 57 | #include <termios.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 58 | #include <unistd.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | |
| 60 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 61 | #include "ssh1.h" |
| 62 | #include "ssh2.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | #include "packet.h" |
| 64 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 65 | #include "log.h" |
| 66 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | #include "channels.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 69 | #include "canohost.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 70 | #include "key.h" |
| 71 | #include "authfd.h" |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 72 | #include "pathnames.h" |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 73 | #include "bufaux.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 75 | /* -- channel core */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 77 | /* |
| 78 | * Pointer to an array containing all allocated channels. The array is |
| 79 | * dynamically extended as needed. |
| 80 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 81 | static Channel **channels = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 83 | /* |
| 84 | * Size of the channel array. All slots of the array must always be |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 85 | * initialized (at least the type field); unused slots set to NULL |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 86 | */ |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 87 | static u_int channels_alloc = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 89 | /* |
| 90 | * Maximum file descriptor value used in any of the channels. This is |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 91 | * updated in channel_new. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 92 | */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 93 | static int channel_max_fd = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 96 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 98 | /* |
| 99 | * Data structure for storing which hosts are permitted for forward requests. |
| 100 | * The local sides of any remote forwards are stored in this array to prevent |
| 101 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 102 | * network (which might be behind a firewall). |
| 103 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | typedef struct { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 105 | char *host_to_connect; /* Connect to 'host'. */ |
| 106 | u_short port_to_connect; /* Connect to 'port'. */ |
| 107 | u_short listen_port; /* Remote side should listen port number. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 108 | } ForwardPermission; |
| 109 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 110 | /* List of all permitted host/port pairs to connect by the user. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION]; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 112 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 113 | /* List of all permitted host/port pairs to connect by the admin. */ |
| 114 | static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION]; |
| 115 | |
| 116 | /* Number of permitted host/port pairs in the array permitted by the user. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | static int num_permitted_opens = 0; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 118 | |
| 119 | /* Number of permitted host/port pair in the array permitted by the admin. */ |
| 120 | static int num_adm_permitted_opens = 0; |
| 121 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 122 | /* |
| 123 | * If this is true, all opens are permitted. This is the case on the server |
| 124 | * on which we have to trust the client anyway, and the user could do |
| 125 | * anything after logging in anyway. |
| 126 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 127 | static int all_opens_permitted = 0; |
| 128 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 129 | |
| 130 | /* -- X11 forwarding */ |
| 131 | |
| 132 | /* Maximum number of fake X11 displays to try. */ |
| 133 | #define MAX_DISPLAYS 1000 |
| 134 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 135 | /* Saved X11 local (client) display. */ |
| 136 | static char *x11_saved_display = NULL; |
| 137 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 138 | /* Saved X11 authentication protocol name. */ |
| 139 | static char *x11_saved_proto = NULL; |
| 140 | |
| 141 | /* Saved X11 authentication data. This is the real data. */ |
| 142 | static char *x11_saved_data = NULL; |
| 143 | static u_int x11_saved_data_len = 0; |
| 144 | |
| 145 | /* |
| 146 | * Fake X11 authentication data. This is what the server will be sending us; |
| 147 | * we should replace any occurrences of this by the real data. |
| 148 | */ |
Damien Miller | 4ae97f1 | 2006-03-26 14:08:10 +1100 | [diff] [blame] | 149 | static u_char *x11_fake_data = NULL; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 150 | static u_int x11_fake_data_len; |
| 151 | |
| 152 | |
| 153 | /* -- agent forwarding */ |
| 154 | |
| 155 | #define NUM_SOCKS 10 |
| 156 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 157 | /* AF_UNSPEC or AF_INET or AF_INET6 */ |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 158 | static int IPv4or6 = AF_UNSPEC; |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 159 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 160 | /* helper */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 161 | static void port_open_helper(Channel *c, char *rtype); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 162 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 163 | /* -- channel core */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 164 | |
| 165 | Channel * |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 166 | channel_by_id(int id) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 167 | { |
| 168 | Channel *c; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 169 | |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 170 | if (id < 0 || (u_int)id >= channels_alloc) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 171 | logit("channel_by_id: %d: bad id", id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 174 | c = channels[id]; |
| 175 | if (c == NULL) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 176 | logit("channel_by_id: %d: bad id: channel free", id); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | return c; |
| 180 | } |
| 181 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 182 | /* |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 183 | * Returns the channel if it is allowed to receive protocol messages. |
| 184 | * Private channels, like listening sockets, may not receive messages. |
| 185 | */ |
| 186 | Channel * |
| 187 | channel_lookup(int id) |
| 188 | { |
| 189 | Channel *c; |
| 190 | |
| 191 | if ((c = channel_by_id(id)) == NULL) |
| 192 | return (NULL); |
| 193 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 194 | switch (c->type) { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 195 | case SSH_CHANNEL_X11_OPEN: |
| 196 | case SSH_CHANNEL_LARVAL: |
| 197 | case SSH_CHANNEL_CONNECTING: |
| 198 | case SSH_CHANNEL_DYNAMIC: |
| 199 | case SSH_CHANNEL_OPENING: |
| 200 | case SSH_CHANNEL_OPEN: |
| 201 | case SSH_CHANNEL_INPUT_DRAINING: |
| 202 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 203 | return (c); |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 204 | } |
| 205 | logit("Non-public channel %d, type %d.", id, c->type); |
| 206 | return (NULL); |
| 207 | } |
| 208 | |
| 209 | /* |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 210 | * Register filedescriptors for a channel, used when allocating a channel or |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 211 | * when the channel consumer/producer is ready, e.g. shell exec'd |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 212 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 213 | static void |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 214 | channel_register_fds(Channel *c, int rfd, int wfd, int efd, |
| 215 | int extusage, int nonblock) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 217 | /* Update the maximum file descriptor value. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 218 | channel_max_fd = MAX(channel_max_fd, rfd); |
| 219 | channel_max_fd = MAX(channel_max_fd, wfd); |
| 220 | channel_max_fd = MAX(channel_max_fd, efd); |
| 221 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 222 | /* XXX set close-on-exec -markus */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 223 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 224 | c->rfd = rfd; |
| 225 | c->wfd = wfd; |
| 226 | c->sock = (rfd == wfd) ? rfd : -1; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 227 | c->ctl_fd = -1; /* XXX: set elsewhere */ |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 228 | c->efd = efd; |
| 229 | c->extended_usage = extusage; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 230 | |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 231 | /* XXX ugly hack: nonblock is only set by the server */ |
| 232 | if (nonblock && isatty(c->rfd)) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 233 | debug2("channel %d: rfd %d isatty", c->self, c->rfd); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 234 | c->isatty = 1; |
| 235 | if (!isatty(c->wfd)) { |
Ben Lindstrom | cc74df7 | 2001-03-05 06:20:14 +0000 | [diff] [blame] | 236 | error("channel %d: wfd %d is not a tty?", |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 237 | c->self, c->wfd); |
| 238 | } |
| 239 | } else { |
| 240 | c->isatty = 0; |
| 241 | } |
Ben Lindstrom | beb5f33 | 2002-07-22 15:28:53 +0000 | [diff] [blame] | 242 | c->wfd_isatty = isatty(c->wfd); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 243 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 244 | /* enable nonblocking mode */ |
| 245 | if (nonblock) { |
| 246 | if (rfd != -1) |
| 247 | set_nonblock(rfd); |
| 248 | if (wfd != -1) |
| 249 | set_nonblock(wfd); |
| 250 | if (efd != -1) |
| 251 | set_nonblock(efd); |
| 252 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Allocate a new channel object and set its type and socket. This will cause |
| 257 | * remote_name to be freed. |
| 258 | */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 259 | Channel * |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 260 | channel_new(char *ctype, int type, int rfd, int wfd, int efd, |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 261 | u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock) |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 262 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 263 | int found; |
| 264 | u_int i; |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 265 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 266 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 267 | /* Do initial allocation if this is the first call. */ |
| 268 | if (channels_alloc == 0) { |
| 269 | channels_alloc = 10; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 270 | channels = xcalloc(channels_alloc, sizeof(Channel *)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | for (i = 0; i < channels_alloc; i++) |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 272 | channels[i] = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | } |
| 274 | /* Try to find a free slot where to put the new channel. */ |
| 275 | for (found = -1, i = 0; i < channels_alloc; i++) |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 276 | if (channels[i] == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | /* Found a free slot. */ |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 278 | found = (int)i; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 279 | break; |
| 280 | } |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 281 | if (found < 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 282 | /* There are no free slots. Take last+1 slot and expand the array. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 283 | found = channels_alloc; |
Damien Miller | 9403aa2 | 2002-06-26 19:14:43 +1000 | [diff] [blame] | 284 | if (channels_alloc > 10000) |
| 285 | fatal("channel_new: internal error: channels_alloc %d " |
| 286 | "too big.", channels_alloc); |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 287 | channels = xrealloc(channels, channels_alloc + 10, |
| 288 | sizeof(Channel *)); |
Damien Miller | 5efcecc | 2003-09-17 07:31:14 +1000 | [diff] [blame] | 289 | channels_alloc += 10; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 290 | debug2("channel: expanding %d", channels_alloc); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | for (i = found; i < channels_alloc; i++) |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 292 | channels[i] = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 293 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 294 | /* Initialize and return new channel. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 295 | c = channels[found] = xcalloc(1, sizeof(Channel)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 296 | buffer_init(&c->input); |
| 297 | buffer_init(&c->output); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 298 | buffer_init(&c->extended); |
Damien Miller | 5144df9 | 2002-01-22 23:28:45 +1100 | [diff] [blame] | 299 | c->ostate = CHAN_OUTPUT_OPEN; |
| 300 | c->istate = CHAN_INPUT_OPEN; |
| 301 | c->flags = 0; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 302 | channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | c->self = found; |
| 304 | c->type = type; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 305 | c->ctype = ctype; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 306 | c->local_window = window; |
| 307 | c->local_window_max = window; |
| 308 | c->local_consumed = 0; |
| 309 | c->local_maxpacket = maxpack; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 310 | c->remote_id = -1; |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 311 | c->remote_name = xstrdup(remote_name); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 312 | c->remote_window = 0; |
| 313 | c->remote_maxpacket = 0; |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 314 | c->force_drain = 0; |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 315 | c->single_connection = 0; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 316 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 317 | c->detach_close = 0; |
Damien Miller | 67f0bc0 | 2002-02-05 12:23:08 +1100 | [diff] [blame] | 318 | c->confirm = NULL; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 319 | c->confirm_ctx = NULL; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 320 | c->input_filter = NULL; |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 321 | c->output_filter = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 322 | debug("channel %d: new [%s]", found, remote_name); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 323 | return c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 324 | } |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 325 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 326 | static int |
| 327 | channel_find_maxfd(void) |
| 328 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 329 | u_int i; |
| 330 | int max = 0; |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 331 | Channel *c; |
| 332 | |
| 333 | for (i = 0; i < channels_alloc; i++) { |
| 334 | c = channels[i]; |
| 335 | if (c != NULL) { |
| 336 | max = MAX(max, c->rfd); |
| 337 | max = MAX(max, c->wfd); |
| 338 | max = MAX(max, c->efd); |
| 339 | } |
| 340 | } |
| 341 | return max; |
| 342 | } |
| 343 | |
| 344 | int |
| 345 | channel_close_fd(int *fdp) |
| 346 | { |
| 347 | int ret = 0, fd = *fdp; |
| 348 | |
| 349 | if (fd != -1) { |
| 350 | ret = close(fd); |
| 351 | *fdp = -1; |
| 352 | if (fd == channel_max_fd) |
| 353 | channel_max_fd = channel_find_maxfd(); |
| 354 | } |
| 355 | return ret; |
| 356 | } |
| 357 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 358 | /* Close all channel fd/socket. */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 359 | static void |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 360 | channel_close_fds(Channel *c) |
| 361 | { |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 362 | debug3("channel %d: close_fds r %d w %d e %d c %d", |
| 363 | c->self, c->rfd, c->wfd, c->efd, c->ctl_fd); |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 364 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 365 | channel_close_fd(&c->sock); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 366 | channel_close_fd(&c->ctl_fd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 367 | channel_close_fd(&c->rfd); |
| 368 | channel_close_fd(&c->wfd); |
| 369 | channel_close_fd(&c->efd); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Free the channel and close its fd/socket. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 373 | void |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 374 | channel_free(Channel *c) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 375 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 376 | char *s; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 377 | u_int i, n; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 378 | |
| 379 | for (n = 0, i = 0; i < channels_alloc; i++) |
| 380 | if (channels[i]) |
| 381 | n++; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 382 | debug("channel %d: free: %s, nchannels %u", c->self, |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 383 | c->remote_name ? c->remote_name : "???", n); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 384 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 385 | s = channel_open_message(); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 386 | debug3("channel %d: status: %s", c->self, s); |
Ben Lindstrom | 6c3ae2b | 2000-12-30 03:25:14 +0000 | [diff] [blame] | 387 | xfree(s); |
| 388 | |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 389 | if (c->sock != -1) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 390 | shutdown(c->sock, SHUT_RDWR); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 391 | if (c->ctl_fd != -1) |
| 392 | shutdown(c->ctl_fd, SHUT_RDWR); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 393 | channel_close_fds(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 394 | buffer_free(&c->input); |
| 395 | buffer_free(&c->output); |
| 396 | buffer_free(&c->extended); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 397 | if (c->remote_name) { |
| 398 | xfree(c->remote_name); |
| 399 | c->remote_name = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 401 | channels[c->self] = NULL; |
| 402 | xfree(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 403 | } |
| 404 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 405 | void |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 406 | channel_free_all(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 407 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 408 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 409 | |
Ben Lindstrom | 601e436 | 2001-06-21 03:19:23 +0000 | [diff] [blame] | 410 | for (i = 0; i < channels_alloc; i++) |
| 411 | if (channels[i] != NULL) |
| 412 | channel_free(channels[i]); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Closes the sockets/fds of all channels. This is used to close extra file |
| 417 | * descriptors after a fork. |
| 418 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 419 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 420 | channel_close_all(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 421 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 422 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 423 | |
| 424 | for (i = 0; i < channels_alloc; i++) |
| 425 | if (channels[i] != NULL) |
| 426 | channel_close_fds(channels[i]); |
| 427 | } |
| 428 | |
| 429 | /* |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 430 | * Stop listening to channels. |
| 431 | */ |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 432 | void |
| 433 | channel_stop_listening(void) |
| 434 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 435 | u_int i; |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 436 | Channel *c; |
| 437 | |
| 438 | for (i = 0; i < channels_alloc; i++) { |
| 439 | c = channels[i]; |
| 440 | if (c != NULL) { |
| 441 | switch (c->type) { |
| 442 | case SSH_CHANNEL_AUTH_SOCKET: |
| 443 | case SSH_CHANNEL_PORT_LISTENER: |
| 444 | case SSH_CHANNEL_RPORT_LISTENER: |
| 445 | case SSH_CHANNEL_X11_LISTENER: |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 446 | channel_close_fd(&c->sock); |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 447 | channel_free(c); |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 455 | * Returns true if no channel has too much buffered data, and false if one or |
| 456 | * more channel is overfull. |
| 457 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 458 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 459 | channel_not_very_much_buffered_data(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 460 | { |
| 461 | u_int i; |
| 462 | Channel *c; |
| 463 | |
| 464 | for (i = 0; i < channels_alloc; i++) { |
| 465 | c = channels[i]; |
| 466 | if (c != NULL && c->type == SSH_CHANNEL_OPEN) { |
Damien Miller | af5f2e6 | 2001-10-10 15:01:16 +1000 | [diff] [blame] | 467 | #if 0 |
| 468 | if (!compat20 && |
| 469 | buffer_len(&c->input) > packet_get_maxsize()) { |
Damien Miller | 275295e | 2003-01-08 14:04:09 +1100 | [diff] [blame] | 470 | debug2("channel %d: big input buffer %d", |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 471 | c->self, buffer_len(&c->input)); |
| 472 | return 0; |
| 473 | } |
Damien Miller | af5f2e6 | 2001-10-10 15:01:16 +1000 | [diff] [blame] | 474 | #endif |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 475 | if (buffer_len(&c->output) > packet_get_maxsize()) { |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 476 | debug2("channel %d: big output buffer %u > %u", |
Damien Miller | af5f2e6 | 2001-10-10 15:01:16 +1000 | [diff] [blame] | 477 | c->self, buffer_len(&c->output), |
| 478 | packet_get_maxsize()); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | /* Returns true if any channel is still open. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 487 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 488 | channel_still_open(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 489 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 490 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 491 | Channel *c; |
| 492 | |
| 493 | for (i = 0; i < channels_alloc; i++) { |
| 494 | c = channels[i]; |
| 495 | if (c == NULL) |
| 496 | continue; |
| 497 | switch (c->type) { |
| 498 | case SSH_CHANNEL_X11_LISTENER: |
| 499 | case SSH_CHANNEL_PORT_LISTENER: |
| 500 | case SSH_CHANNEL_RPORT_LISTENER: |
| 501 | case SSH_CHANNEL_CLOSED: |
| 502 | case SSH_CHANNEL_AUTH_SOCKET: |
| 503 | case SSH_CHANNEL_DYNAMIC: |
| 504 | case SSH_CHANNEL_CONNECTING: |
| 505 | case SSH_CHANNEL_ZOMBIE: |
| 506 | continue; |
| 507 | case SSH_CHANNEL_LARVAL: |
| 508 | if (!compat20) |
| 509 | fatal("cannot happen: SSH_CHANNEL_LARVAL"); |
| 510 | continue; |
| 511 | case SSH_CHANNEL_OPENING: |
| 512 | case SSH_CHANNEL_OPEN: |
| 513 | case SSH_CHANNEL_X11_OPEN: |
| 514 | return 1; |
| 515 | case SSH_CHANNEL_INPUT_DRAINING: |
| 516 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 517 | if (!compat13) |
| 518 | fatal("cannot happen: OUT_DRAIN"); |
| 519 | return 1; |
| 520 | default: |
| 521 | fatal("channel_still_open: bad channel type %d", c->type); |
| 522 | /* NOTREACHED */ |
| 523 | } |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* Returns the id of an open channel suitable for keepaliving */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 529 | int |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 530 | channel_find_open(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 531 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 532 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 533 | Channel *c; |
| 534 | |
| 535 | for (i = 0; i < channels_alloc; i++) { |
| 536 | c = channels[i]; |
Damien Miller | 3bbd878 | 2004-06-18 22:23:22 +1000 | [diff] [blame] | 537 | if (c == NULL || c->remote_id < 0) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 538 | continue; |
| 539 | switch (c->type) { |
| 540 | case SSH_CHANNEL_CLOSED: |
| 541 | case SSH_CHANNEL_DYNAMIC: |
| 542 | case SSH_CHANNEL_X11_LISTENER: |
| 543 | case SSH_CHANNEL_PORT_LISTENER: |
| 544 | case SSH_CHANNEL_RPORT_LISTENER: |
| 545 | case SSH_CHANNEL_OPENING: |
| 546 | case SSH_CHANNEL_CONNECTING: |
| 547 | case SSH_CHANNEL_ZOMBIE: |
| 548 | continue; |
| 549 | case SSH_CHANNEL_LARVAL: |
| 550 | case SSH_CHANNEL_AUTH_SOCKET: |
| 551 | case SSH_CHANNEL_OPEN: |
| 552 | case SSH_CHANNEL_X11_OPEN: |
| 553 | return i; |
| 554 | case SSH_CHANNEL_INPUT_DRAINING: |
| 555 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 556 | if (!compat13) |
| 557 | fatal("cannot happen: OUT_DRAIN"); |
| 558 | return i; |
| 559 | default: |
| 560 | fatal("channel_find_open: bad channel type %d", c->type); |
| 561 | /* NOTREACHED */ |
| 562 | } |
| 563 | } |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | /* |
| 569 | * Returns a message describing the currently open forwarded connections, |
| 570 | * suitable for sending to the client. The message contains crlf pairs for |
| 571 | * newlines. |
| 572 | */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 573 | char * |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 574 | channel_open_message(void) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 575 | { |
| 576 | Buffer buffer; |
| 577 | Channel *c; |
| 578 | char buf[1024], *cp; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 579 | u_int i; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 580 | |
| 581 | buffer_init(&buffer); |
| 582 | snprintf(buf, sizeof buf, "The following connections are open:\r\n"); |
| 583 | buffer_append(&buffer, buf, strlen(buf)); |
| 584 | for (i = 0; i < channels_alloc; i++) { |
| 585 | c = channels[i]; |
| 586 | if (c == NULL) |
| 587 | continue; |
| 588 | switch (c->type) { |
| 589 | case SSH_CHANNEL_X11_LISTENER: |
| 590 | case SSH_CHANNEL_PORT_LISTENER: |
| 591 | case SSH_CHANNEL_RPORT_LISTENER: |
| 592 | case SSH_CHANNEL_CLOSED: |
| 593 | case SSH_CHANNEL_AUTH_SOCKET: |
| 594 | case SSH_CHANNEL_ZOMBIE: |
| 595 | continue; |
| 596 | case SSH_CHANNEL_LARVAL: |
| 597 | case SSH_CHANNEL_OPENING: |
| 598 | case SSH_CHANNEL_CONNECTING: |
| 599 | case SSH_CHANNEL_DYNAMIC: |
| 600 | case SSH_CHANNEL_OPEN: |
| 601 | case SSH_CHANNEL_X11_OPEN: |
| 602 | case SSH_CHANNEL_INPUT_DRAINING: |
| 603 | case SSH_CHANNEL_OUTPUT_DRAINING: |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 604 | snprintf(buf, sizeof buf, |
| 605 | " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n", |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 606 | c->self, c->remote_name, |
| 607 | c->type, c->remote_id, |
| 608 | c->istate, buffer_len(&c->input), |
| 609 | c->ostate, buffer_len(&c->output), |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 610 | c->rfd, c->wfd, c->ctl_fd); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 611 | buffer_append(&buffer, buf, strlen(buf)); |
| 612 | continue; |
| 613 | default: |
| 614 | fatal("channel_open_message: bad channel type %d", c->type); |
| 615 | /* NOTREACHED */ |
| 616 | } |
| 617 | } |
| 618 | buffer_append(&buffer, "\0", 1); |
| 619 | cp = xstrdup(buffer_ptr(&buffer)); |
| 620 | buffer_free(&buffer); |
| 621 | return cp; |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | channel_send_open(int id) |
| 626 | { |
| 627 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 628 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 629 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 630 | logit("channel_send_open: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 631 | return; |
| 632 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 633 | debug2("channel %d: send open", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 634 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 635 | packet_put_cstring(c->ctype); |
| 636 | packet_put_int(c->self); |
| 637 | packet_put_int(c->local_window); |
| 638 | packet_put_int(c->local_maxpacket); |
| 639 | packet_send(); |
| 640 | } |
| 641 | |
| 642 | void |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 643 | channel_request_start(int id, char *service, int wantconfirm) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 644 | { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 645 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 646 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 647 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 648 | logit("channel_request_start: %d: unknown channel id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 649 | return; |
| 650 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 651 | debug2("channel %d: request %s confirm %d", id, service, wantconfirm); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 652 | packet_start(SSH2_MSG_CHANNEL_REQUEST); |
| 653 | packet_put_int(c->remote_id); |
| 654 | packet_put_cstring(service); |
| 655 | packet_put_char(wantconfirm); |
| 656 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 657 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 658 | void |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 659 | channel_register_confirm(int id, channel_callback_fn *fn, void *ctx) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 660 | { |
| 661 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 662 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 663 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 664 | logit("channel_register_comfirm: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
Damien Miller | 67f0bc0 | 2002-02-05 12:23:08 +1100 | [diff] [blame] | 667 | c->confirm = fn; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 668 | c->confirm_ctx = ctx; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 669 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 670 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 671 | void |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 672 | channel_register_cleanup(int id, channel_callback_fn *fn, int do_close) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 673 | { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 674 | Channel *c = channel_by_id(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 675 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 676 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 677 | logit("channel_register_cleanup: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 680 | c->detach_user = fn; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 681 | c->detach_close = do_close; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 682 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 683 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 684 | void |
| 685 | channel_cancel_cleanup(int id) |
| 686 | { |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 687 | Channel *c = channel_by_id(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 688 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 689 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 690 | logit("channel_cancel_cleanup: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 691 | return; |
| 692 | } |
Ben Lindstrom | 809744e | 2001-07-04 05:26:06 +0000 | [diff] [blame] | 693 | c->detach_user = NULL; |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 694 | c->detach_close = 0; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 695 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 696 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 697 | void |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 698 | channel_register_filter(int id, channel_infilter_fn *ifn, |
| 699 | channel_outfilter_fn *ofn) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 700 | { |
| 701 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 702 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 703 | if (c == NULL) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 704 | logit("channel_register_filter: %d: bad id", id); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 705 | return; |
| 706 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 707 | c->input_filter = ifn; |
| 708 | c->output_filter = ofn; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | void |
| 712 | channel_set_fds(int id, int rfd, int wfd, int efd, |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 713 | int extusage, int nonblock, u_int window_max) |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 714 | { |
| 715 | Channel *c = channel_lookup(id); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 716 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 717 | if (c == NULL || c->type != SSH_CHANNEL_LARVAL) |
| 718 | fatal("channel_activate for non-larval channel %d.", id); |
| 719 | channel_register_fds(c, rfd, wfd, efd, extusage, nonblock); |
| 720 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 2aa0c19 | 2002-02-19 15:20:08 +1100 | [diff] [blame] | 721 | c->local_window = c->local_window_max = window_max; |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 722 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 723 | packet_put_int(c->remote_id); |
| 724 | packet_put_int(c->local_window); |
| 725 | packet_send(); |
| 726 | } |
| 727 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 728 | /* |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 729 | * 'channel_pre*' are called just before select() to add any bits relevant to |
| 730 | * channels in the select bitmasks. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 731 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 732 | /* |
| 733 | * 'channel_post*': perform any appropriate operations for channels which |
| 734 | * have events pending. |
| 735 | */ |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 736 | typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 737 | chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE]; |
| 738 | chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE]; |
| 739 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 740 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 741 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 742 | channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 743 | { |
| 744 | FD_SET(c->sock, readset); |
| 745 | } |
| 746 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 747 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 748 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 749 | channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 750 | { |
| 751 | debug3("channel %d: waiting for connection", c->self); |
| 752 | FD_SET(c->sock, writeset); |
| 753 | } |
| 754 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 755 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 756 | channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 757 | { |
| 758 | if (buffer_len(&c->input) < packet_get_maxsize()) |
| 759 | FD_SET(c->sock, readset); |
| 760 | if (buffer_len(&c->output) > 0) |
| 761 | FD_SET(c->sock, writeset); |
| 762 | } |
| 763 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 764 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 765 | channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 766 | { |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 767 | u_int limit = compat20 ? c->remote_window : packet_get_maxsize(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 768 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 769 | if (c->istate == CHAN_INPUT_OPEN && |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 770 | limit > 0 && |
Damien Miller | 499a0d5 | 2006-04-23 12:06:03 +1000 | [diff] [blame] | 771 | buffer_len(&c->input) < limit && |
| 772 | buffer_check_alloc(&c->input, CHAN_RBUF)) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 773 | FD_SET(c->rfd, readset); |
| 774 | if (c->ostate == CHAN_OUTPUT_OPEN || |
| 775 | c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 776 | if (buffer_len(&c->output) > 0) { |
| 777 | FD_SET(c->wfd, writeset); |
| 778 | } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 779 | if (CHANNEL_EFD_OUTPUT_ACTIVE(c)) |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 780 | debug2("channel %d: obuf_empty delayed efd %d/(%d)", |
| 781 | c->self, c->efd, buffer_len(&c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 782 | else |
| 783 | chan_obuf_empty(c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | /** XXX check close conditions, too */ |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 787 | if (compat20 && c->efd != -1) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 788 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
| 789 | buffer_len(&c->extended) > 0) |
| 790 | FD_SET(c->efd, writeset); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 791 | else if (!(c->flags & CHAN_EOF_SENT) && |
| 792 | c->extended_usage == CHAN_EXTENDED_READ && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 793 | buffer_len(&c->extended) < c->remote_window) |
| 794 | FD_SET(c->efd, readset); |
| 795 | } |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 796 | /* XXX: What about efd? races? */ |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 797 | if (compat20 && c->ctl_fd != -1 && |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 798 | c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN) |
| 799 | FD_SET(c->ctl_fd, readset); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 800 | } |
| 801 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 802 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 803 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 804 | channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 805 | { |
| 806 | if (buffer_len(&c->input) == 0) { |
| 807 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 808 | packet_put_int(c->remote_id); |
| 809 | packet_send(); |
| 810 | c->type = SSH_CHANNEL_CLOSED; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 811 | debug2("channel %d: closing after input drain.", c->self); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 815 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 816 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 817 | channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 818 | { |
| 819 | if (buffer_len(&c->output) == 0) |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 820 | chan_mark_dead(c); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 821 | else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 822 | FD_SET(c->sock, writeset); |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * This is a special state for X11 authentication spoofing. An opened X11 |
| 827 | * connection (when authentication spoofing is being done) remains in this |
| 828 | * state until the first packet has been completely read. The authentication |
| 829 | * data in that packet is then substituted by the real data if it matches the |
| 830 | * fake data, and the channel is put into normal mode. |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 831 | * XXX All this happens at the client side. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 832 | * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 833 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 834 | static int |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 835 | x11_open_helper(Buffer *b) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 836 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 837 | u_char *ucp; |
| 838 | u_int proto_len, data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 839 | |
| 840 | /* Check if the fixed size part of the packet is in buffer. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 841 | if (buffer_len(b) < 12) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 842 | return 0; |
| 843 | |
| 844 | /* Parse the lengths of variable-length fields. */ |
Damien Miller | 708d21c | 2002-01-22 23:18:15 +1100 | [diff] [blame] | 845 | ucp = buffer_ptr(b); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 846 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 847 | proto_len = 256 * ucp[6] + ucp[7]; |
| 848 | data_len = 256 * ucp[8] + ucp[9]; |
| 849 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 850 | proto_len = ucp[6] + 256 * ucp[7]; |
| 851 | data_len = ucp[8] + 256 * ucp[9]; |
| 852 | } else { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 853 | debug2("Initial X11 packet contains bad byte order byte: 0x%x", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 854 | ucp[0]); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | /* Check if the whole packet is in buffer. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 859 | if (buffer_len(b) < |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 860 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 861 | return 0; |
| 862 | |
| 863 | /* Check if authentication protocol matches. */ |
| 864 | if (proto_len != strlen(x11_saved_proto) || |
| 865 | memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 866 | debug2("X11 connection uses different authentication protocol."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 867 | return -1; |
| 868 | } |
| 869 | /* Check if authentication data matches our fake data. */ |
| 870 | if (data_len != x11_fake_data_len || |
| 871 | memcmp(ucp + 12 + ((proto_len + 3) & ~3), |
| 872 | x11_fake_data, x11_fake_data_len) != 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 873 | debug2("X11 auth data does not match fake data."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 874 | return -1; |
| 875 | } |
| 876 | /* Check fake data length */ |
| 877 | if (x11_fake_data_len != x11_saved_data_len) { |
| 878 | error("X11 fake_data_len %d != saved_data_len %d", |
| 879 | x11_fake_data_len, x11_saved_data_len); |
| 880 | return -1; |
| 881 | } |
| 882 | /* |
| 883 | * Received authentication protocol and data match |
| 884 | * our fake data. Substitute the fake data with real |
| 885 | * data. |
| 886 | */ |
| 887 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
| 888 | x11_saved_data, x11_saved_data_len); |
| 889 | return 1; |
| 890 | } |
| 891 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 892 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 893 | channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 894 | { |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 895 | int ret = x11_open_helper(&c->output); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 896 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 897 | if (ret == 1) { |
| 898 | /* Start normal processing for the channel. */ |
| 899 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 900 | channel_pre_open_13(c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 901 | } else if (ret == -1) { |
| 902 | /* |
| 903 | * We have received an X11 connection that has bad |
| 904 | * authentication information. |
| 905 | */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 906 | logit("X11 connection rejected because of wrong authentication."); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 907 | buffer_clear(&c->input); |
| 908 | buffer_clear(&c->output); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 909 | channel_close_fd(&c->sock); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 910 | c->sock = -1; |
| 911 | c->type = SSH_CHANNEL_CLOSED; |
| 912 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 913 | packet_put_int(c->remote_id); |
| 914 | packet_send(); |
| 915 | } |
| 916 | } |
| 917 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 918 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 919 | channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 920 | { |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 921 | int ret = x11_open_helper(&c->output); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 922 | |
| 923 | /* c->force_drain = 1; */ |
| 924 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 925 | if (ret == 1) { |
| 926 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 927 | channel_pre_open(c, readset, writeset); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 928 | } else if (ret == -1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 929 | logit("X11 connection rejected because of wrong authentication."); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 930 | debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate); |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 931 | chan_read_failed(c); |
| 932 | buffer_clear(&c->input); |
| 933 | chan_ibuf_empty(c); |
| 934 | buffer_clear(&c->output); |
| 935 | /* for proto v1, the peer will send an IEOF */ |
| 936 | if (compat20) |
| 937 | chan_write_failed(c); |
| 938 | else |
| 939 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 940 | debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 944 | /* try to decode a socks4 header */ |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 945 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 946 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 947 | channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 948 | { |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 949 | char *p, *host; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 950 | u_int len, have, i, found; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 951 | char username[256]; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 952 | struct { |
| 953 | u_int8_t version; |
| 954 | u_int8_t command; |
| 955 | u_int16_t dest_port; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 956 | struct in_addr dest_addr; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 957 | } s4_req, s4_rsp; |
| 958 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 959 | debug2("channel %d: decode socks4", c->self); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 960 | |
| 961 | have = buffer_len(&c->input); |
| 962 | len = sizeof(s4_req); |
| 963 | if (have < len) |
| 964 | return 0; |
| 965 | p = buffer_ptr(&c->input); |
| 966 | for (found = 0, i = len; i < have; i++) { |
| 967 | if (p[i] == '\0') { |
| 968 | found = 1; |
| 969 | break; |
| 970 | } |
| 971 | if (i > 1024) { |
| 972 | /* the peer is probably sending garbage */ |
| 973 | debug("channel %d: decode socks4: too long", |
| 974 | c->self); |
| 975 | return -1; |
| 976 | } |
| 977 | } |
| 978 | if (!found) |
| 979 | return 0; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 980 | buffer_get(&c->input, (char *)&s4_req.version, 1); |
| 981 | buffer_get(&c->input, (char *)&s4_req.command, 1); |
| 982 | buffer_get(&c->input, (char *)&s4_req.dest_port, 2); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 983 | buffer_get(&c->input, (char *)&s4_req.dest_addr, 4); |
Ben Lindstrom | 2b261b9 | 2001-04-17 18:14:34 +0000 | [diff] [blame] | 984 | have = buffer_len(&c->input); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 985 | p = buffer_ptr(&c->input); |
| 986 | len = strlen(p); |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 987 | debug2("channel %d: decode socks4: user %s/%d", c->self, p, len); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 988 | if (len > have) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 989 | fatal("channel %d: decode socks4: len %d > have %d", |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 990 | c->self, len, have); |
| 991 | strlcpy(username, p, sizeof(username)); |
| 992 | buffer_consume(&c->input, len); |
| 993 | buffer_consume(&c->input, 1); /* trailing '\0' */ |
| 994 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 995 | host = inet_ntoa(s4_req.dest_addr); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 996 | strlcpy(c->path, host, sizeof(c->path)); |
| 997 | c->host_port = ntohs(s4_req.dest_port); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 998 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 999 | debug2("channel %d: dynamic request: socks4 host %s port %u command %u", |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1000 | c->self, host, c->host_port, s4_req.command); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1001 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1002 | if (s4_req.command != 1) { |
| 1003 | debug("channel %d: cannot handle: socks4 cn %d", |
| 1004 | c->self, s4_req.command); |
| 1005 | return -1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1006 | } |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1007 | s4_rsp.version = 0; /* vn: 0 for reply */ |
| 1008 | s4_rsp.command = 90; /* cd: req granted */ |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1009 | s4_rsp.dest_port = 0; /* ignored */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1010 | s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */ |
Damien Miller | a0fdce9 | 2006-03-26 14:28:50 +1100 | [diff] [blame] | 1011 | buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp)); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1012 | return 1; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1015 | /* try to decode a socks5 header */ |
| 1016 | #define SSH_SOCKS5_AUTHDONE 0x1000 |
| 1017 | #define SSH_SOCKS5_NOAUTH 0x00 |
| 1018 | #define SSH_SOCKS5_IPV4 0x01 |
| 1019 | #define SSH_SOCKS5_DOMAIN 0x03 |
| 1020 | #define SSH_SOCKS5_IPV6 0x04 |
| 1021 | #define SSH_SOCKS5_CONNECT 0x01 |
| 1022 | #define SSH_SOCKS5_SUCCESS 0x00 |
| 1023 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1024 | /* ARGSUSED */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1025 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1026 | channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1027 | { |
| 1028 | struct { |
| 1029 | u_int8_t version; |
| 1030 | u_int8_t command; |
| 1031 | u_int8_t reserved; |
| 1032 | u_int8_t atyp; |
| 1033 | } s5_req, s5_rsp; |
| 1034 | u_int16_t dest_port; |
| 1035 | u_char *p, dest_addr[255+1]; |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1036 | u_int have, need, i, found, nmethods, addrlen, af; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1037 | |
| 1038 | debug2("channel %d: decode socks5", c->self); |
| 1039 | p = buffer_ptr(&c->input); |
| 1040 | if (p[0] != 0x05) |
| 1041 | return -1; |
| 1042 | have = buffer_len(&c->input); |
| 1043 | if (!(c->flags & SSH_SOCKS5_AUTHDONE)) { |
| 1044 | /* format: ver | nmethods | methods */ |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1045 | if (have < 2) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1046 | return 0; |
| 1047 | nmethods = p[1]; |
| 1048 | if (have < nmethods + 2) |
| 1049 | return 0; |
| 1050 | /* look for method: "NO AUTHENTICATION REQUIRED" */ |
| 1051 | for (found = 0, i = 2 ; i < nmethods + 2; i++) { |
| 1052 | if (p[i] == SSH_SOCKS5_NOAUTH ) { |
| 1053 | found = 1; |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | if (!found) { |
| 1058 | debug("channel %d: method SSH_SOCKS5_NOAUTH not found", |
| 1059 | c->self); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | buffer_consume(&c->input, nmethods + 2); |
| 1063 | buffer_put_char(&c->output, 0x05); /* version */ |
| 1064 | buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */ |
| 1065 | FD_SET(c->sock, writeset); |
| 1066 | c->flags |= SSH_SOCKS5_AUTHDONE; |
| 1067 | debug2("channel %d: socks5 auth done", c->self); |
| 1068 | return 0; /* need more */ |
| 1069 | } |
| 1070 | debug2("channel %d: socks5 post auth", c->self); |
| 1071 | if (have < sizeof(s5_req)+1) |
| 1072 | return 0; /* need more */ |
Damien Miller | e3b21a5 | 2006-03-26 14:29:06 +1100 | [diff] [blame] | 1073 | memcpy(&s5_req, p, sizeof(s5_req)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1074 | if (s5_req.version != 0x05 || |
| 1075 | s5_req.command != SSH_SOCKS5_CONNECT || |
| 1076 | s5_req.reserved != 0x00) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1077 | debug2("channel %d: only socks5 connect supported", c->self); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1078 | return -1; |
| 1079 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1080 | switch (s5_req.atyp){ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1081 | case SSH_SOCKS5_IPV4: |
| 1082 | addrlen = 4; |
| 1083 | af = AF_INET; |
| 1084 | break; |
| 1085 | case SSH_SOCKS5_DOMAIN: |
| 1086 | addrlen = p[sizeof(s5_req)]; |
| 1087 | af = -1; |
| 1088 | break; |
| 1089 | case SSH_SOCKS5_IPV6: |
| 1090 | addrlen = 16; |
| 1091 | af = AF_INET6; |
| 1092 | break; |
| 1093 | default: |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1094 | debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1095 | return -1; |
| 1096 | } |
Damien Miller | 0f07707 | 2006-07-10 22:21:02 +1000 | [diff] [blame] | 1097 | need = sizeof(s5_req) + addrlen + 2; |
| 1098 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1099 | need++; |
| 1100 | if (have < need) |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1101 | return 0; |
| 1102 | buffer_consume(&c->input, sizeof(s5_req)); |
| 1103 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
| 1104 | buffer_consume(&c->input, 1); /* host string length */ |
| 1105 | buffer_get(&c->input, (char *)&dest_addr, addrlen); |
| 1106 | buffer_get(&c->input, (char *)&dest_port, 2); |
| 1107 | dest_addr[addrlen] = '\0'; |
| 1108 | if (s5_req.atyp == SSH_SOCKS5_DOMAIN) |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1109 | strlcpy(c->path, (char *)dest_addr, sizeof(c->path)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1110 | else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL) |
| 1111 | return -1; |
| 1112 | c->host_port = ntohs(dest_port); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1113 | |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1114 | debug2("channel %d: dynamic request: socks5 host %s port %u command %u", |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1115 | c->self, c->path, c->host_port, s5_req.command); |
| 1116 | |
| 1117 | s5_rsp.version = 0x05; |
| 1118 | s5_rsp.command = SSH_SOCKS5_SUCCESS; |
| 1119 | s5_rsp.reserved = 0; /* ignored */ |
| 1120 | s5_rsp.atyp = SSH_SOCKS5_IPV4; |
| 1121 | ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY; |
| 1122 | dest_port = 0; /* ignored */ |
| 1123 | |
Damien Miller | a0fdce9 | 2006-03-26 14:28:50 +1100 | [diff] [blame] | 1124 | buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp)); |
| 1125 | buffer_append(&c->output, &dest_addr, sizeof(struct in_addr)); |
| 1126 | buffer_append(&c->output, &dest_port, sizeof(dest_port)); |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1127 | return 1; |
| 1128 | } |
| 1129 | |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1130 | /* dynamic port forwarding */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1131 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1132 | channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset) |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1133 | { |
| 1134 | u_char *p; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 1135 | u_int have; |
| 1136 | int ret; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1137 | |
| 1138 | have = buffer_len(&c->input); |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1139 | c->delayed = 0; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1140 | debug2("channel %d: pre_dynamic: have %d", c->self, have); |
Ben Lindstrom | c486d88 | 2001-04-11 16:08:34 +0000 | [diff] [blame] | 1141 | /* buffer_dump(&c->input); */ |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1142 | /* check if the fixed size part of the packet is in buffer. */ |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1143 | if (have < 3) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1144 | /* need more */ |
| 1145 | FD_SET(c->sock, readset); |
| 1146 | return; |
| 1147 | } |
| 1148 | /* try to guess the protocol */ |
| 1149 | p = buffer_ptr(&c->input); |
| 1150 | switch (p[0]) { |
Ben Lindstrom | 6fa9d10 | 2001-04-11 23:08:17 +0000 | [diff] [blame] | 1151 | case 0x04: |
| 1152 | ret = channel_decode_socks4(c, readset, writeset); |
| 1153 | break; |
Darren Tucker | 46471c9 | 2003-07-03 13:55:19 +1000 | [diff] [blame] | 1154 | case 0x05: |
| 1155 | ret = channel_decode_socks5(c, readset, writeset); |
| 1156 | break; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1157 | default: |
| 1158 | ret = -1; |
| 1159 | break; |
| 1160 | } |
| 1161 | if (ret < 0) { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1162 | chan_mark_dead(c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1163 | } else if (ret == 0) { |
| 1164 | debug2("channel %d: pre_dynamic: need more", c->self); |
| 1165 | /* need more */ |
| 1166 | FD_SET(c->sock, readset); |
| 1167 | } else { |
| 1168 | /* switch to the next state */ |
| 1169 | c->type = SSH_CHANNEL_OPENING; |
| 1170 | port_open_helper(c, "direct-tcpip"); |
| 1171 | } |
| 1172 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1173 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1174 | /* This is our fake X11 server socket. */ |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1175 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1176 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1177 | channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1178 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1179 | Channel *nc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1180 | struct sockaddr addr; |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 1181 | int newsock; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1182 | socklen_t addrlen; |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1183 | char buf[16384], *remote_ipaddr; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1184 | int remote_port; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1185 | |
| 1186 | if (FD_ISSET(c->sock, readset)) { |
| 1187 | debug("X11 connection requested."); |
| 1188 | addrlen = sizeof(addr); |
| 1189 | newsock = accept(c->sock, &addr, &addrlen); |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 1190 | if (c->single_connection) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1191 | debug2("single_connection: closing X11 listener."); |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 1192 | channel_close_fd(&c->sock); |
| 1193 | chan_mark_dead(c); |
| 1194 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1195 | if (newsock < 0) { |
| 1196 | error("accept: %.100s", strerror(errno)); |
| 1197 | return; |
| 1198 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 1199 | set_nodelay(newsock); |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1200 | remote_ipaddr = get_peer_ipaddr(newsock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1201 | remote_port = get_peer_port(newsock); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1202 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1203 | remote_ipaddr, remote_port); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1204 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1205 | nc = channel_new("accepted x11 socket", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1206 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1207 | c->local_window_max, c->local_maxpacket, 0, buf, 1); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1208 | if (compat20) { |
| 1209 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 1210 | packet_put_cstring("x11"); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1211 | packet_put_int(nc->self); |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 1212 | packet_put_int(nc->local_window_max); |
| 1213 | packet_put_int(nc->local_maxpacket); |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1214 | /* originator ipaddr and port */ |
| 1215 | packet_put_cstring(remote_ipaddr); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1216 | if (datafellows & SSH_BUG_X11FWD) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1217 | debug2("ssh2 x11 bug compat mode"); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1218 | } else { |
| 1219 | packet_put_int(remote_port); |
| 1220 | } |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1221 | packet_send(); |
| 1222 | } else { |
| 1223 | packet_start(SSH_SMSG_X11_OPEN); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1224 | packet_put_int(nc->self); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1225 | if (packet_get_protocol_flags() & |
| 1226 | SSH_PROTOFLAG_HOST_IN_FWD_OPEN) |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 1227 | packet_put_cstring(buf); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1228 | packet_send(); |
| 1229 | } |
Damien Miller | d83ff35 | 2001-01-30 09:19:34 +1100 | [diff] [blame] | 1230 | xfree(remote_ipaddr); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1234 | static void |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1235 | port_open_helper(Channel *c, char *rtype) |
| 1236 | { |
| 1237 | int direct; |
| 1238 | char buf[1024]; |
| 1239 | char *remote_ipaddr = get_peer_ipaddr(c->sock); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1240 | int remote_port = get_peer_port(c->sock); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1241 | |
| 1242 | direct = (strcmp(rtype, "direct-tcpip") == 0); |
| 1243 | |
| 1244 | snprintf(buf, sizeof buf, |
| 1245 | "%s: listening port %d for %.100s port %d, " |
| 1246 | "connect from %.200s port %d", |
| 1247 | rtype, c->listening_port, c->path, c->host_port, |
| 1248 | remote_ipaddr, remote_port); |
| 1249 | |
| 1250 | xfree(c->remote_name); |
| 1251 | c->remote_name = xstrdup(buf); |
| 1252 | |
| 1253 | if (compat20) { |
| 1254 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 1255 | packet_put_cstring(rtype); |
| 1256 | packet_put_int(c->self); |
| 1257 | packet_put_int(c->local_window_max); |
| 1258 | packet_put_int(c->local_maxpacket); |
| 1259 | if (direct) { |
| 1260 | /* target host, port */ |
| 1261 | packet_put_cstring(c->path); |
| 1262 | packet_put_int(c->host_port); |
| 1263 | } else { |
| 1264 | /* listen address, port */ |
| 1265 | packet_put_cstring(c->path); |
| 1266 | packet_put_int(c->listening_port); |
| 1267 | } |
| 1268 | /* originator host and port */ |
| 1269 | packet_put_cstring(remote_ipaddr); |
Damien Miller | 677257f | 2005-06-17 12:55:03 +1000 | [diff] [blame] | 1270 | packet_put_int((u_int)remote_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1271 | packet_send(); |
| 1272 | } else { |
| 1273 | packet_start(SSH_MSG_PORT_OPEN); |
| 1274 | packet_put_int(c->self); |
| 1275 | packet_put_cstring(c->path); |
| 1276 | packet_put_int(c->host_port); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1277 | if (packet_get_protocol_flags() & |
| 1278 | SSH_PROTOFLAG_HOST_IN_FWD_OPEN) |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1279 | packet_put_cstring(c->remote_name); |
| 1280 | packet_send(); |
| 1281 | } |
| 1282 | xfree(remote_ipaddr); |
| 1283 | } |
| 1284 | |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 1285 | static void |
| 1286 | channel_set_reuseaddr(int fd) |
| 1287 | { |
| 1288 | int on = 1; |
| 1289 | |
| 1290 | /* |
| 1291 | * Set socket options. |
| 1292 | * Allow local port reuse in TIME_WAIT. |
| 1293 | */ |
| 1294 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) |
| 1295 | error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno)); |
| 1296 | } |
| 1297 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1298 | /* |
| 1299 | * This socket is listening for connections to a forwarded TCP/IP port. |
| 1300 | */ |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1301 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1302 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1303 | channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1304 | { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1305 | Channel *nc; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1306 | struct sockaddr addr; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1307 | int newsock, nextstate; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1308 | socklen_t addrlen; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1309 | char *rtype; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1310 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1311 | if (FD_ISSET(c->sock, readset)) { |
| 1312 | debug("Connection to port %d forwarding " |
| 1313 | "to %.100s port %d requested.", |
| 1314 | c->listening_port, c->path, c->host_port); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1315 | |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1316 | if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
| 1317 | nextstate = SSH_CHANNEL_OPENING; |
| 1318 | rtype = "forwarded-tcpip"; |
| 1319 | } else { |
| 1320 | if (c->host_port == 0) { |
| 1321 | nextstate = SSH_CHANNEL_DYNAMIC; |
Damien Miller | d3c04b9 | 2001-10-10 15:04:20 +1000 | [diff] [blame] | 1322 | rtype = "dynamic-tcpip"; |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1323 | } else { |
| 1324 | nextstate = SSH_CHANNEL_OPENING; |
| 1325 | rtype = "direct-tcpip"; |
| 1326 | } |
| 1327 | } |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1328 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1329 | addrlen = sizeof(addr); |
| 1330 | newsock = accept(c->sock, &addr, &addrlen); |
| 1331 | if (newsock < 0) { |
| 1332 | error("accept: %.100s", strerror(errno)); |
| 1333 | return; |
| 1334 | } |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 1335 | set_nodelay(newsock); |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1336 | nc = channel_new(rtype, nextstate, newsock, newsock, -1, |
| 1337 | c->local_window_max, c->local_maxpacket, 0, rtype, 1); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1338 | nc->listening_port = c->listening_port; |
| 1339 | nc->host_port = c->host_port; |
| 1340 | strlcpy(nc->path, c->path, sizeof(nc->path)); |
| 1341 | |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1342 | if (nextstate == SSH_CHANNEL_DYNAMIC) { |
| 1343 | /* |
| 1344 | * do not call the channel_post handler until |
| 1345 | * this flag has been reset by a pre-handler. |
| 1346 | * otherwise the FD_ISSET calls might overflow |
| 1347 | */ |
| 1348 | nc->delayed = 1; |
| 1349 | } else { |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1350 | port_open_helper(nc, rtype); |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1351 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * This is the authentication agent socket listening for connections from |
| 1357 | * clients. |
| 1358 | */ |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1359 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1360 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1361 | channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1362 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1363 | Channel *nc; |
| 1364 | int newsock; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1365 | struct sockaddr addr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1366 | socklen_t addrlen; |
| 1367 | |
| 1368 | if (FD_ISSET(c->sock, readset)) { |
| 1369 | addrlen = sizeof(addr); |
| 1370 | newsock = accept(c->sock, &addr, &addrlen); |
| 1371 | if (newsock < 0) { |
| 1372 | error("accept from auth socket: %.100s", strerror(errno)); |
| 1373 | return; |
| 1374 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1375 | nc = channel_new("accepted auth socket", |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1376 | SSH_CHANNEL_OPENING, newsock, newsock, -1, |
| 1377 | c->local_window_max, c->local_maxpacket, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 1378 | 0, "accepted auth socket", 1); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1379 | if (compat20) { |
| 1380 | packet_start(SSH2_MSG_CHANNEL_OPEN); |
| 1381 | packet_put_cstring("auth-agent@openssh.com"); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1382 | packet_put_int(nc->self); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1383 | packet_put_int(c->local_window_max); |
| 1384 | packet_put_int(c->local_maxpacket); |
| 1385 | } else { |
| 1386 | packet_start(SSH_SMSG_AGENT_OPEN); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1387 | packet_put_int(nc->self); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1388 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1389 | packet_send(); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1393 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1394 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1395 | channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset) |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1396 | { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1397 | int err = 0; |
Ben Lindstrom | 1118095 | 2001-07-04 05:13:35 +0000 | [diff] [blame] | 1398 | socklen_t sz = sizeof(err); |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1399 | |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1400 | if (FD_ISSET(c->sock, writeset)) { |
Ben Lindstrom | 733a235 | 2002-03-05 01:31:28 +0000 | [diff] [blame] | 1401 | if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1402 | err = errno; |
| 1403 | error("getsockopt SO_ERROR failed"); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1404 | } |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 1405 | if (err == 0) { |
| 1406 | debug("channel %d: connected", c->self); |
| 1407 | c->type = SSH_CHANNEL_OPEN; |
| 1408 | if (compat20) { |
| 1409 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1410 | packet_put_int(c->remote_id); |
| 1411 | packet_put_int(c->self); |
| 1412 | packet_put_int(c->local_window); |
| 1413 | packet_put_int(c->local_maxpacket); |
| 1414 | } else { |
| 1415 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1416 | packet_put_int(c->remote_id); |
| 1417 | packet_put_int(c->self); |
| 1418 | } |
| 1419 | } else { |
| 1420 | debug("channel %d: not connected: %s", |
| 1421 | c->self, strerror(err)); |
| 1422 | if (compat20) { |
| 1423 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 1424 | packet_put_int(c->remote_id); |
| 1425 | packet_put_int(SSH2_OPEN_CONNECT_FAILED); |
| 1426 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
| 1427 | packet_put_cstring(strerror(err)); |
| 1428 | packet_put_cstring(""); |
| 1429 | } |
| 1430 | } else { |
| 1431 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1432 | packet_put_int(c->remote_id); |
| 1433 | } |
| 1434 | chan_mark_dead(c); |
| 1435 | } |
| 1436 | packet_send(); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1440 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1441 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1442 | channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1443 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1444 | char buf[CHAN_RBUF]; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1445 | int len; |
| 1446 | |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 1447 | if (c->rfd != -1 && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1448 | FD_ISSET(c->rfd, readset)) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1449 | errno = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1450 | len = read(c->rfd, buf, sizeof(buf)); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1451 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1452 | return 1; |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1453 | #ifndef PTY_ZEROREAD |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1454 | if (len <= 0) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1455 | #else |
Darren Tucker | 144e8d6 | 2006-06-25 08:25:25 +1000 | [diff] [blame] | 1456 | if ((!c->isatty && len <= 0) || |
| 1457 | (c->isatty && (len < 0 || (len == 0 && errno != 0)))) { |
Darren Tucker | 9afe115 | 2006-06-23 21:24:12 +1000 | [diff] [blame] | 1458 | #endif |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1459 | debug2("channel %d: read<=0 rfd %d len %d", |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1460 | c->self, c->rfd, len); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1461 | if (c->type != SSH_CHANNEL_OPEN) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1462 | debug2("channel %d: not open", c->self); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1463 | chan_mark_dead(c); |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1464 | return -1; |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1465 | } else if (compat13) { |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 1466 | buffer_clear(&c->output); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1467 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1468 | debug2("channel %d: input draining.", c->self); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1469 | } else { |
| 1470 | chan_read_failed(c); |
| 1471 | } |
| 1472 | return -1; |
| 1473 | } |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 1474 | if (c->input_filter != NULL) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1475 | if (c->input_filter(c, buf, len) == -1) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1476 | debug2("channel %d: filter stops", c->self); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1477 | chan_read_failed(c); |
| 1478 | } |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1479 | } else if (c->datagram) { |
| 1480 | buffer_put_string(&c->input, buf, len); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1481 | } else { |
| 1482 | buffer_append(&c->input, buf, len); |
| 1483 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1484 | } |
| 1485 | return 1; |
| 1486 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1487 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1488 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1489 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1490 | channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1491 | { |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1492 | struct termios tio; |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1493 | u_char *data = NULL, *buf; |
Ben Lindstrom | 6d218f4 | 2001-09-18 05:53:12 +0000 | [diff] [blame] | 1494 | u_int dlen; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1495 | int len; |
| 1496 | |
| 1497 | /* Send buffered output data to the socket. */ |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 1498 | if (c->wfd != -1 && |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1499 | FD_ISSET(c->wfd, writeset) && |
| 1500 | buffer_len(&c->output) > 0) { |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1501 | if (c->output_filter != NULL) { |
| 1502 | if ((buf = c->output_filter(c, &data, &dlen)) == NULL) { |
| 1503 | debug2("channel %d: filter stops", c->self); |
Damien Miller | e204f6a | 2006-01-31 21:47:15 +1100 | [diff] [blame] | 1504 | if (c->type != SSH_CHANNEL_OPEN) |
| 1505 | chan_mark_dead(c); |
| 1506 | else |
| 1507 | chan_write_failed(c); |
| 1508 | return -1; |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1509 | } |
| 1510 | } else if (c->datagram) { |
| 1511 | buf = data = buffer_get_string(&c->output, &dlen); |
| 1512 | } else { |
| 1513 | buf = data = buffer_ptr(&c->output); |
| 1514 | dlen = buffer_len(&c->output); |
| 1515 | } |
| 1516 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1517 | if (c->datagram) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1518 | /* ignore truncated writes, datagrams might get lost */ |
| 1519 | c->local_consumed += dlen + 4; |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1520 | len = write(c->wfd, buf, dlen); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1521 | xfree(data); |
| 1522 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1523 | return 1; |
| 1524 | if (len <= 0) { |
| 1525 | if (c->type != SSH_CHANNEL_OPEN) |
| 1526 | chan_mark_dead(c); |
| 1527 | else |
| 1528 | chan_write_failed(c); |
| 1529 | return -1; |
| 1530 | } |
| 1531 | return 1; |
| 1532 | } |
Ben Lindstrom | 92ea0ea | 2002-07-04 18:11:09 +0000 | [diff] [blame] | 1533 | #ifdef _AIX |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1534 | /* XXX: Later AIX versions can't push as much data to tty */ |
Darren Tucker | 240fdfa | 2003-11-22 14:10:02 +1100 | [diff] [blame] | 1535 | if (compat20 && c->wfd_isatty) |
| 1536 | dlen = MIN(dlen, 8*1024); |
Ben Lindstrom | 92ea0ea | 2002-07-04 18:11:09 +0000 | [diff] [blame] | 1537 | #endif |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1538 | |
| 1539 | len = write(c->wfd, buf, dlen); |
Damien Miller | 6f83b8e | 2000-05-02 09:23:45 +1000 | [diff] [blame] | 1540 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1541 | return 1; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1542 | if (len <= 0) { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1543 | if (c->type != SSH_CHANNEL_OPEN) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1544 | debug2("channel %d: not open", c->self); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1545 | chan_mark_dead(c); |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1546 | return -1; |
| 1547 | } else if (compat13) { |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 1548 | buffer_clear(&c->output); |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1549 | debug2("channel %d: input draining.", c->self); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1550 | c->type = SSH_CHANNEL_INPUT_DRAINING; |
| 1551 | } else { |
| 1552 | chan_write_failed(c); |
| 1553 | } |
| 1554 | return -1; |
| 1555 | } |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1556 | if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') { |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1557 | if (tcgetattr(c->wfd, &tio) == 0 && |
| 1558 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
| 1559 | /* |
| 1560 | * Simulate echo to reduce the impact of |
Ben Lindstrom | b40204b | 2001-03-05 06:29:44 +0000 | [diff] [blame] | 1561 | * traffic analysis. We need to match the |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1562 | * size of a SSH2_MSG_CHANNEL_DATA message |
Damien Miller | 077b238 | 2005-12-31 16:22:32 +1100 | [diff] [blame] | 1563 | * (4 byte channel id + buf) |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1564 | */ |
Ben Lindstrom | e229b25 | 2001-03-05 06:28:06 +0000 | [diff] [blame] | 1565 | packet_send_ignore(4 + len); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1566 | packet_send(); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 1567 | } |
| 1568 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1569 | buffer_consume(&c->output, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1570 | if (compat20 && len > 0) { |
| 1571 | c->local_consumed += len; |
| 1572 | } |
| 1573 | } |
| 1574 | return 1; |
| 1575 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1576 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1577 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1578 | channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1579 | { |
Darren Tucker | 11327cc | 2005-03-14 23:22:25 +1100 | [diff] [blame] | 1580 | char buf[CHAN_RBUF]; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1581 | int len; |
| 1582 | |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1583 | /** XXX handle drain efd, too */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1584 | if (c->efd != -1) { |
| 1585 | if (c->extended_usage == CHAN_EXTENDED_WRITE && |
| 1586 | FD_ISSET(c->efd, writeset) && |
| 1587 | buffer_len(&c->extended) > 0) { |
| 1588 | len = write(c->efd, buffer_ptr(&c->extended), |
| 1589 | buffer_len(&c->extended)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1590 | debug2("channel %d: written %d to efd %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1591 | c->self, len, c->efd); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1592 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1593 | return 1; |
| 1594 | if (len <= 0) { |
| 1595 | debug2("channel %d: closing write-efd %d", |
| 1596 | c->self, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1597 | channel_close_fd(&c->efd); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1598 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1599 | buffer_consume(&c->extended, len); |
| 1600 | c->local_consumed += len; |
| 1601 | } |
| 1602 | } else if (c->extended_usage == CHAN_EXTENDED_READ && |
| 1603 | FD_ISSET(c->efd, readset)) { |
| 1604 | len = read(c->efd, buf, sizeof(buf)); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1605 | debug2("channel %d: read %d from efd %d", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1606 | c->self, len, c->efd); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1607 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1608 | return 1; |
| 1609 | if (len <= 0) { |
| 1610 | debug2("channel %d: closing read-efd %d", |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 1611 | c->self, c->efd); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1612 | channel_close_fd(&c->efd); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1613 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1614 | buffer_append(&c->extended, buf, len); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1615 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1616 | } |
| 1617 | } |
| 1618 | return 1; |
| 1619 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1620 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1621 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1622 | static int |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1623 | channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1624 | { |
| 1625 | char buf[16]; |
| 1626 | int len; |
| 1627 | |
| 1628 | /* Monitor control fd to detect if the slave client exits */ |
| 1629 | if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) { |
| 1630 | len = read(c->ctl_fd, buf, sizeof(buf)); |
| 1631 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
| 1632 | return 1; |
| 1633 | if (len <= 0) { |
| 1634 | debug2("channel %d: ctl read<=0", c->self); |
| 1635 | if (c->type != SSH_CHANNEL_OPEN) { |
| 1636 | debug2("channel %d: not open", c->self); |
| 1637 | chan_mark_dead(c); |
| 1638 | return -1; |
| 1639 | } else { |
| 1640 | chan_read_failed(c); |
| 1641 | chan_write_failed(c); |
| 1642 | } |
| 1643 | return -1; |
| 1644 | } else |
| 1645 | fatal("%s: unexpected data on ctl fd", __func__); |
| 1646 | } |
| 1647 | return 1; |
| 1648 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 1649 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1650 | static int |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1651 | channel_check_window(Channel *c) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1652 | { |
Ben Lindstrom | b392151 | 2001-04-11 15:57:50 +0000 | [diff] [blame] | 1653 | if (c->type == SSH_CHANNEL_OPEN && |
| 1654 | !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1655 | c->local_window < c->local_window_max/2 && |
| 1656 | c->local_consumed > 0) { |
| 1657 | packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 1658 | packet_put_int(c->remote_id); |
| 1659 | packet_put_int(c->local_consumed); |
| 1660 | packet_send(); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 1661 | debug2("channel %d: window %d sent adjust %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1662 | c->self, c->local_window, |
| 1663 | c->local_consumed); |
| 1664 | c->local_window += c->local_consumed; |
| 1665 | c->local_consumed = 0; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1666 | } |
| 1667 | return 1; |
| 1668 | } |
| 1669 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1670 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1671 | channel_post_open(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1672 | { |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1673 | if (c->delayed) |
| 1674 | return; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1675 | channel_handle_rfd(c, readset, writeset); |
| 1676 | channel_handle_wfd(c, readset, writeset); |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1677 | if (!compat20) |
Damien Miller | 4623a75 | 2001-10-10 15:03:58 +1000 | [diff] [blame] | 1678 | return; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1679 | channel_handle_efd(c, readset, writeset); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 1680 | channel_handle_ctl(c, readset, writeset); |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1681 | channel_check_window(c); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1682 | } |
| 1683 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 1684 | /* ARGSUSED */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1685 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1686 | channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1687 | { |
| 1688 | int len; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1689 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1690 | /* Send buffered output data to the socket. */ |
| 1691 | if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) { |
| 1692 | len = write(c->sock, buffer_ptr(&c->output), |
| 1693 | buffer_len(&c->output)); |
| 1694 | if (len <= 0) |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 1695 | buffer_clear(&c->output); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1696 | else |
| 1697 | buffer_consume(&c->output, len); |
| 1698 | } |
| 1699 | } |
| 1700 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1701 | static void |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1702 | channel_handler_init_20(void) |
| 1703 | { |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1704 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1705 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1706 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1707 | channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1708 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1709 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1710 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1711 | channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1712 | |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1713 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1714 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1715 | channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1716 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1717 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1718 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1719 | channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1720 | } |
| 1721 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1722 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1723 | channel_handler_init_13(void) |
| 1724 | { |
| 1725 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13; |
| 1726 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13; |
| 1727 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 1728 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 1729 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
| 1730 | channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining; |
| 1731 | channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1732 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1733 | channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1734 | |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1735 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1736 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 1737 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 1738 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
| 1739 | channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1740 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1741 | channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1742 | } |
| 1743 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1744 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1745 | channel_handler_init_15(void) |
| 1746 | { |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1747 | channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 1748 | channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1749 | channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
| 1750 | channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
| 1751 | channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1752 | channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting; |
Ben Lindstrom | 3bb4f9d | 2001-04-08 18:30:26 +0000 | [diff] [blame] | 1753 | channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1754 | |
| 1755 | channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
| 1756 | channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
| 1757 | channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1758 | channel_post[SSH_CHANNEL_OPEN] = &channel_post_open; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 1759 | channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting; |
Damien Miller | de6987c | 2002-01-22 23:20:40 +1100 | [diff] [blame] | 1760 | channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1761 | } |
| 1762 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1763 | static void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1764 | channel_handler_init(void) |
| 1765 | { |
| 1766 | int i; |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 1767 | |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1768 | for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1769 | channel_pre[i] = NULL; |
| 1770 | channel_post[i] = NULL; |
| 1771 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1772 | if (compat20) |
| 1773 | channel_handler_init_20(); |
| 1774 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1775 | channel_handler_init_13(); |
| 1776 | else |
| 1777 | channel_handler_init_15(); |
| 1778 | } |
| 1779 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1780 | /* gc dead channels */ |
| 1781 | static void |
| 1782 | channel_garbage_collect(Channel *c) |
| 1783 | { |
| 1784 | if (c == NULL) |
| 1785 | return; |
| 1786 | if (c->detach_user != NULL) { |
Damien Miller | 39eda6e | 2005-11-05 14:52:50 +1100 | [diff] [blame] | 1787 | if (!chan_is_dead(c, c->detach_close)) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1788 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1789 | debug2("channel %d: gc: notify user", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1790 | c->detach_user(c->self, NULL); |
| 1791 | /* if we still have a callback */ |
| 1792 | if (c->detach_user != NULL) |
| 1793 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1794 | debug2("channel %d: gc: user detached", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1795 | } |
| 1796 | if (!chan_is_dead(c, 1)) |
| 1797 | return; |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 1798 | debug2("channel %d: garbage collecting", c->self); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1799 | channel_free(c); |
| 1800 | } |
| 1801 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1802 | static void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1803 | channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1804 | { |
| 1805 | static int did_init = 0; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1806 | u_int i; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1807 | Channel *c; |
| 1808 | |
| 1809 | if (!did_init) { |
| 1810 | channel_handler_init(); |
| 1811 | did_init = 1; |
| 1812 | } |
| 1813 | for (i = 0; i < channels_alloc; i++) { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1814 | c = channels[i]; |
| 1815 | if (c == NULL) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1816 | continue; |
Ben Lindstrom | c0dee1a | 2001-06-05 20:52:50 +0000 | [diff] [blame] | 1817 | if (ftab[c->type] != NULL) |
| 1818 | (*ftab[c->type])(c, readset, writeset); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1819 | channel_garbage_collect(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1820 | } |
| 1821 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1822 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1823 | /* |
| 1824 | * Allocate/update select bitmasks and add any bits relevant to channels in |
| 1825 | * select bitmasks. |
| 1826 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1827 | void |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1828 | channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1829 | u_int *nallocp, int rekeying) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1830 | { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 1831 | u_int n, sz, nfdset; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1832 | |
| 1833 | n = MAX(*maxfdp, channel_max_fd); |
| 1834 | |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 1835 | nfdset = howmany(n+1, NFDBITS); |
| 1836 | /* Explicitly test here, because xrealloc isn't always called */ |
| 1837 | if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask)) |
| 1838 | fatal("channel_prepare_select: max_fd (%d) is too large", n); |
| 1839 | sz = nfdset * sizeof(fd_mask); |
| 1840 | |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1841 | /* perhaps check sz < nalloc/2 and shrink? */ |
| 1842 | if (*readsetp == NULL || sz > *nallocp) { |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 1843 | *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask)); |
| 1844 | *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask)); |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1845 | *nallocp = sz; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1846 | } |
Ben Lindstrom | 16d29d5 | 2001-07-18 16:01:46 +0000 | [diff] [blame] | 1847 | *maxfdp = n; |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1848 | memset(*readsetp, 0, sz); |
| 1849 | memset(*writesetp, 0, sz); |
| 1850 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 1851 | if (!rekeying) |
| 1852 | channel_handler(channel_pre, *readsetp, *writesetp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1853 | } |
| 1854 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1855 | /* |
| 1856 | * After select, perform any appropriate operations for channels which have |
| 1857 | * events pending. |
| 1858 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1859 | void |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 1860 | channel_after_select(fd_set *readset, fd_set *writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1861 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1862 | channel_handler(channel_post, readset, writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1863 | } |
| 1864 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1865 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 1866 | /* If there is data to send to the connection, enqueue some of it now. */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1867 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 1868 | channel_output_poll(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1869 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1870 | Channel *c; |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 1871 | u_int i, len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1872 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1873 | for (i = 0; i < channels_alloc; i++) { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 1874 | c = channels[i]; |
| 1875 | if (c == NULL) |
| 1876 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1877 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1878 | /* |
| 1879 | * We are only interested in channels that can have buffered |
| 1880 | * incoming data. |
| 1881 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1882 | if (compat13) { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1883 | if (c->type != SSH_CHANNEL_OPEN && |
| 1884 | c->type != SSH_CHANNEL_INPUT_DRAINING) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1885 | continue; |
| 1886 | } else { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1887 | if (c->type != SSH_CHANNEL_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1888 | continue; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1889 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 1890 | if (compat20 && |
| 1891 | (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) { |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1892 | /* XXX is this true? */ |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 1893 | debug3("channel %d: will not send data after close", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1894 | continue; |
| 1895 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1896 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1897 | /* Get the amount of buffered data for this channel. */ |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1898 | if ((c->istate == CHAN_INPUT_OPEN || |
| 1899 | c->istate == CHAN_INPUT_WAIT_DRAIN) && |
| 1900 | (len = buffer_len(&c->input)) > 0) { |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 1901 | if (c->datagram) { |
| 1902 | if (len > 0) { |
| 1903 | u_char *data; |
| 1904 | u_int dlen; |
| 1905 | |
| 1906 | data = buffer_get_string(&c->input, |
| 1907 | &dlen); |
| 1908 | packet_start(SSH2_MSG_CHANNEL_DATA); |
| 1909 | packet_put_int(c->remote_id); |
| 1910 | packet_put_string(data, dlen); |
| 1911 | packet_send(); |
| 1912 | c->remote_window -= dlen + 4; |
| 1913 | xfree(data); |
| 1914 | } |
| 1915 | continue; |
| 1916 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1917 | /* |
| 1918 | * Send some data for the other side over the secure |
| 1919 | * connection. |
| 1920 | */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1921 | if (compat20) { |
| 1922 | if (len > c->remote_window) |
| 1923 | len = c->remote_window; |
| 1924 | if (len > c->remote_maxpacket) |
| 1925 | len = c->remote_maxpacket; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1926 | } else { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1927 | if (packet_is_interactive()) { |
| 1928 | if (len > 1024) |
| 1929 | len = 512; |
| 1930 | } else { |
| 1931 | /* Keep the packets at reasonable size. */ |
| 1932 | if (len > packet_get_maxsize()/2) |
| 1933 | len = packet_get_maxsize()/2; |
| 1934 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1935 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1936 | if (len > 0) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1937 | packet_start(compat20 ? |
| 1938 | SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1939 | packet_put_int(c->remote_id); |
| 1940 | packet_put_string(buffer_ptr(&c->input), len); |
| 1941 | packet_send(); |
| 1942 | buffer_consume(&c->input, len); |
| 1943 | c->remote_window -= len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1944 | } |
| 1945 | } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1946 | if (compat13) |
| 1947 | fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1948 | /* |
| 1949 | * input-buffer is empty and read-socket shutdown: |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1950 | * tell peer, that we will not send more data: send IEOF. |
| 1951 | * hack for extended data: delay EOF if EFD still in use. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1952 | */ |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1953 | if (CHANNEL_EFD_INPUT_ACTIVE(c)) |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1954 | debug2("channel %d: ibuf_empty delayed efd %d/(%d)", |
| 1955 | c->self, c->efd, buffer_len(&c->extended)); |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1956 | else |
| 1957 | chan_ibuf_empty(c); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1958 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1959 | /* Send extended data, i.e. stderr */ |
| 1960 | if (compat20 && |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 1961 | !(c->flags & CHAN_EOF_SENT) && |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1962 | c->remote_window > 0 && |
| 1963 | (len = buffer_len(&c->extended)) > 0 && |
| 1964 | c->extended_usage == CHAN_EXTENDED_READ) { |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 1965 | debug2("channel %d: rwin %u elen %u euse %d", |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1966 | c->self, c->remote_window, buffer_len(&c->extended), |
| 1967 | c->extended_usage); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1968 | if (len > c->remote_window) |
| 1969 | len = c->remote_window; |
| 1970 | if (len > c->remote_maxpacket) |
| 1971 | len = c->remote_maxpacket; |
| 1972 | packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
| 1973 | packet_put_int(c->remote_id); |
| 1974 | packet_put_int(SSH2_EXTENDED_DATA_STDERR); |
| 1975 | packet_put_string(buffer_ptr(&c->extended), len); |
| 1976 | packet_send(); |
| 1977 | buffer_consume(&c->extended, len); |
| 1978 | c->remote_window -= len; |
Ben Lindstrom | 7fbd455 | 2001-03-05 06:16:11 +0000 | [diff] [blame] | 1979 | debug2("channel %d: sent ext data %d", c->self, len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 1980 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1981 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1982 | } |
| 1983 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 1984 | |
| 1985 | /* -- protocol input */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 1986 | |
| 1987 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1988 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1989 | channel_input_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1990 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1991 | int id; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1992 | char *data; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1993 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1994 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1995 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1996 | /* Get the channel number and verify it. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1997 | id = packet_get_int(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 1998 | c = channel_lookup(id); |
| 1999 | if (c == NULL) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2000 | packet_disconnect("Received data for nonexistent channel %d.", id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2001 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2002 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2003 | if (c->type != SSH_CHANNEL_OPEN && |
| 2004 | c->type != SSH_CHANNEL_X11_OPEN) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2005 | return; |
| 2006 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2007 | /* Get the data. */ |
| 2008 | data = packet_get_string(&data_len); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2009 | |
Damien Miller | a04ad49 | 2004-01-21 11:02:09 +1100 | [diff] [blame] | 2010 | /* |
| 2011 | * Ignore data for protocol > 1.3 if output end is no longer open. |
| 2012 | * For protocol 2 the sending side is reducing its window as it sends |
| 2013 | * data, so we must 'fake' consumption of the data in order to ensure |
| 2014 | * that window updates are sent back. Otherwise the connection might |
| 2015 | * deadlock. |
| 2016 | */ |
| 2017 | if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) { |
| 2018 | if (compat20) { |
| 2019 | c->local_window -= data_len; |
| 2020 | c->local_consumed += data_len; |
| 2021 | } |
| 2022 | xfree(data); |
| 2023 | return; |
| 2024 | } |
| 2025 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 2026 | if (compat20) { |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2027 | if (data_len > c->local_maxpacket) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2028 | logit("channel %d: rcvd big packet %d, maxpack %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2029 | c->self, data_len, c->local_maxpacket); |
| 2030 | } |
| 2031 | if (data_len > c->local_window) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2032 | logit("channel %d: rcvd too much data %d, win %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2033 | c->self, data_len, c->local_window); |
| 2034 | xfree(data); |
| 2035 | return; |
| 2036 | } |
| 2037 | c->local_window -= data_len; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2038 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2039 | packet_check_eom(); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 2040 | if (c->datagram) |
| 2041 | buffer_put_string(&c->output, data, data_len); |
| 2042 | else |
| 2043 | buffer_append(&c->output, data, data_len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2044 | xfree(data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2045 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2046 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2047 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2048 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2049 | channel_input_extended_data(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2050 | { |
| 2051 | int id; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2052 | char *data; |
Ben Lindstrom | daa2179 | 2002-06-25 23:15:30 +0000 | [diff] [blame] | 2053 | u_int data_len, tcode; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2054 | Channel *c; |
| 2055 | |
| 2056 | /* Get the channel number and verify it. */ |
| 2057 | id = packet_get_int(); |
| 2058 | c = channel_lookup(id); |
| 2059 | |
| 2060 | if (c == NULL) |
| 2061 | packet_disconnect("Received extended_data for bad channel %d.", id); |
| 2062 | if (c->type != SSH_CHANNEL_OPEN) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2063 | logit("channel %d: ext data for non open", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
Ben Lindstrom | cf15944 | 2002-03-26 03:26:24 +0000 | [diff] [blame] | 2066 | if (c->flags & CHAN_EOF_RCVD) { |
| 2067 | if (datafellows & SSH_BUG_EXTEOF) |
| 2068 | debug("channel %d: accepting ext data after eof", id); |
| 2069 | else |
| 2070 | packet_disconnect("Received extended_data after EOF " |
| 2071 | "on channel %d.", id); |
| 2072 | } |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2073 | tcode = packet_get_int(); |
| 2074 | if (c->efd == -1 || |
| 2075 | c->extended_usage != CHAN_EXTENDED_WRITE || |
| 2076 | tcode != SSH2_EXTENDED_DATA_STDERR) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2077 | logit("channel %d: bad ext data", c->self); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2078 | return; |
| 2079 | } |
| 2080 | data = packet_get_string(&data_len); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2081 | packet_check_eom(); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2082 | if (data_len > c->local_window) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2083 | logit("channel %d: rcvd too much extended_data %d, win %d", |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2084 | c->self, data_len, c->local_window); |
| 2085 | xfree(data); |
| 2086 | return; |
| 2087 | } |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2088 | debug2("channel %d: rcvd ext data %d", c->self, data_len); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2089 | c->local_window -= data_len; |
| 2090 | buffer_append(&c->extended, data, data_len); |
| 2091 | xfree(data); |
| 2092 | } |
| 2093 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2094 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2095 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2096 | channel_input_ieof(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2097 | { |
| 2098 | int id; |
| 2099 | Channel *c; |
| 2100 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2101 | id = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2102 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2103 | c = channel_lookup(id); |
| 2104 | if (c == NULL) |
| 2105 | packet_disconnect("Received ieof for nonexistent channel %d.", id); |
| 2106 | chan_rcvd_ieof(c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2107 | |
| 2108 | /* XXX force input close */ |
Damien Miller | a90fc08 | 2002-01-22 23:19:38 +1100 | [diff] [blame] | 2109 | if (c->force_drain && c->istate == CHAN_INPUT_OPEN) { |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2110 | debug("channel %d: FORCE input drain", c->self); |
| 2111 | c->istate = CHAN_INPUT_WAIT_DRAIN; |
Damien Miller | 73f1074 | 2002-01-22 23:34:52 +1100 | [diff] [blame] | 2112 | if (buffer_len(&c->input) == 0) |
| 2113 | chan_ibuf_empty(c); |
Ben Lindstrom | 944c4f0 | 2001-09-18 05:51:13 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2116 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2117 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2118 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2119 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2120 | channel_input_close(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2121 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2122 | int id; |
| 2123 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2124 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2125 | id = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2126 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2127 | c = channel_lookup(id); |
| 2128 | if (c == NULL) |
| 2129 | packet_disconnect("Received close for nonexistent channel %d.", id); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2130 | |
| 2131 | /* |
| 2132 | * Send a confirmation that we have closed the channel and no more |
| 2133 | * data is coming for it. |
| 2134 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2135 | packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2136 | packet_put_int(c->remote_id); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2137 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2138 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2139 | /* |
| 2140 | * If the channel is in closed state, we have sent a close request, |
| 2141 | * and the other side will eventually respond with a confirmation. |
| 2142 | * Thus, we cannot free the channel here, because then there would be |
| 2143 | * no-one to receive the confirmation. The channel gets freed when |
| 2144 | * the confirmation arrives. |
| 2145 | */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2146 | if (c->type != SSH_CHANNEL_CLOSED) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2147 | /* |
| 2148 | * Not a closed channel - mark it as draining, which will |
| 2149 | * cause it to be freed later. |
| 2150 | */ |
Damien Miller | 76765c0 | 2002-01-22 23:21:15 +1100 | [diff] [blame] | 2151 | buffer_clear(&c->input); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2152 | c->type = SSH_CHANNEL_OUTPUT_DRAINING; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2153 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2154 | } |
| 2155 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2156 | /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */ |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2157 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2158 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2159 | channel_input_oclose(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2160 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2161 | int id = packet_get_int(); |
| 2162 | Channel *c = channel_lookup(id); |
Damien Miller | 66823cd | 2002-01-22 23:11:38 +1100 | [diff] [blame] | 2163 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2164 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2165 | if (c == NULL) |
| 2166 | packet_disconnect("Received oclose for nonexistent channel %d.", id); |
| 2167 | chan_rcvd_oclose(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2168 | } |
| 2169 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2170 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2171 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2172 | channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2173 | { |
| 2174 | int id = packet_get_int(); |
| 2175 | Channel *c = channel_lookup(id); |
| 2176 | |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2177 | packet_check_eom(); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2178 | if (c == NULL) |
| 2179 | packet_disconnect("Received close confirmation for " |
| 2180 | "out-of-range channel %d.", id); |
| 2181 | if (c->type != SSH_CHANNEL_CLOSED) |
| 2182 | packet_disconnect("Received close confirmation for " |
| 2183 | "non-closed channel %d (type %d).", id, c->type); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2184 | channel_free(c); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2185 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2186 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2187 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2188 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2189 | channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2190 | { |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2191 | int id, remote_id; |
| 2192 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2193 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2194 | id = packet_get_int(); |
| 2195 | c = channel_lookup(id); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2196 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2197 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 2198 | packet_disconnect("Received open confirmation for " |
| 2199 | "non-opening channel %d.", id); |
| 2200 | remote_id = packet_get_int(); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2201 | /* Record the remote channel number and mark that the channel is now open. */ |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2202 | c->remote_id = remote_id; |
| 2203 | c->type = SSH_CHANNEL_OPEN; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2204 | |
| 2205 | if (compat20) { |
| 2206 | c->remote_window = packet_get_int(); |
| 2207 | c->remote_maxpacket = packet_get_int(); |
Damien Miller | 67f0bc0 | 2002-02-05 12:23:08 +1100 | [diff] [blame] | 2208 | if (c->confirm) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2209 | debug2("callback start"); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2210 | c->confirm(c->self, c->confirm_ctx); |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 2211 | debug2("callback done"); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2212 | } |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2213 | debug2("channel %d: open confirm rwindow %u rmax %u", c->self, |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2214 | c->remote_window, c->remote_maxpacket); |
| 2215 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2216 | packet_check_eom(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2217 | } |
| 2218 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2219 | static char * |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2220 | reason2txt(int reason) |
| 2221 | { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 2222 | switch (reason) { |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2223 | case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 2224 | return "administratively prohibited"; |
| 2225 | case SSH2_OPEN_CONNECT_FAILED: |
| 2226 | return "connect failed"; |
| 2227 | case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 2228 | return "unknown channel type"; |
| 2229 | case SSH2_OPEN_RESOURCE_SHORTAGE: |
| 2230 | return "resource shortage"; |
| 2231 | } |
Ben Lindstrom | e259544 | 2001-06-05 20:01:39 +0000 | [diff] [blame] | 2232 | return "unknown reason"; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2235 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2236 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2237 | channel_input_open_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2238 | { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2239 | int id, reason; |
| 2240 | char *msg = NULL, *lang = NULL; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2241 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2242 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2243 | id = packet_get_int(); |
| 2244 | c = channel_lookup(id); |
| 2245 | |
| 2246 | if (c==NULL || c->type != SSH_CHANNEL_OPENING) |
| 2247 | packet_disconnect("Received open failure for " |
| 2248 | "non-opening channel %d.", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2249 | if (compat20) { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2250 | reason = packet_get_int(); |
Ben Lindstrom | f343674 | 2001-04-29 19:52:00 +0000 | [diff] [blame] | 2251 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2252 | msg = packet_get_string(NULL); |
| 2253 | lang = packet_get_string(NULL); |
| 2254 | } |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2255 | logit("channel %d: open failed: %s%s%s", id, |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 2256 | reason2txt(reason), msg ? ": ": "", msg ? msg : ""); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2257 | if (msg != NULL) |
| 2258 | xfree(msg); |
| 2259 | if (lang != NULL) |
| 2260 | xfree(lang); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2261 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2262 | packet_check_eom(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2263 | /* Free the channel. This will also close the socket. */ |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2264 | channel_free(c); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2265 | } |
| 2266 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2267 | /* ARGSUSED */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2268 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2269 | channel_input_window_adjust(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2270 | { |
| 2271 | Channel *c; |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 2272 | int id; |
| 2273 | u_int adjust; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2274 | |
| 2275 | if (!compat20) |
| 2276 | return; |
| 2277 | |
| 2278 | /* Get the channel number and verify it. */ |
| 2279 | id = packet_get_int(); |
| 2280 | c = channel_lookup(id); |
| 2281 | |
Damien Miller | d47c62a | 2005-12-13 19:33:57 +1100 | [diff] [blame] | 2282 | if (c == NULL) { |
| 2283 | logit("Received window adjust for non-open channel %d.", id); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2284 | return; |
| 2285 | } |
| 2286 | adjust = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2287 | packet_check_eom(); |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 2288 | debug2("channel %d: rcvd adjust %u", id, adjust); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2289 | c->remote_window += adjust; |
| 2290 | } |
| 2291 | |
Damien Miller | d79b424 | 2006-03-31 23:11:44 +1100 | [diff] [blame] | 2292 | /* ARGSUSED */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2293 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 2294 | channel_input_port_open(int type, u_int32_t seq, void *ctxt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2295 | { |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2296 | Channel *c = NULL; |
| 2297 | u_short host_port; |
| 2298 | char *host, *originator_string; |
| 2299 | int remote_id, sock = -1; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2300 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2301 | remote_id = packet_get_int(); |
| 2302 | host = packet_get_string(NULL); |
| 2303 | host_port = packet_get_int(); |
| 2304 | |
| 2305 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) { |
| 2306 | originator_string = packet_get_string(NULL); |
| 2307 | } else { |
| 2308 | originator_string = xstrdup("unknown (remote did not supply name)"); |
| 2309 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 2310 | packet_check_eom(); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2311 | sock = channel_connect_to(host, host_port); |
| 2312 | if (sock != -1) { |
| 2313 | c = channel_new("connected socket", |
| 2314 | SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0, |
| 2315 | originator_string, 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 2316 | c->remote_id = remote_id; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2317 | } |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 2318 | xfree(originator_string); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2319 | if (c == NULL) { |
| 2320 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 2321 | packet_put_int(remote_id); |
| 2322 | packet_send(); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2323 | } |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2324 | xfree(host); |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2328 | /* -- tcp forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2329 | |
Ben Lindstrom | 908afed | 2001-10-03 17:34:59 +0000 | [diff] [blame] | 2330 | void |
| 2331 | channel_set_af(int af) |
| 2332 | { |
| 2333 | IPv4or6 = af; |
| 2334 | } |
| 2335 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2336 | static int |
| 2337 | channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port, |
| 2338 | const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2339 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2340 | Channel *c; |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 2341 | int sock, r, success = 0, wildcard = 0, is_client; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2342 | struct addrinfo hints, *ai, *aitop; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2343 | const char *host, *addr; |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2344 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2345 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2346 | host = (type == SSH_CHANNEL_RPORT_LISTENER) ? |
| 2347 | listen_addr : host_to_connect; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2348 | is_client = (type == SSH_CHANNEL_PORT_LISTENER); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2349 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2350 | if (host == NULL) { |
| 2351 | error("No forward host name."); |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 2352 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2353 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2354 | if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) { |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2355 | error("Forward host name too long."); |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 2356 | return 0; |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2357 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2358 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2359 | /* |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2360 | * Determine whether or not a port forward listens to loopback, |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 2361 | * specified address or wildcard. On the client, a specified bind |
| 2362 | * address will always override gateway_ports. On the server, a |
| 2363 | * gateway_ports of 1 (``yes'') will override the client's |
| 2364 | * specification and force a wildcard bind, whereas a value of 2 |
| 2365 | * (``clientspecified'') will bind to whatever address the client |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2366 | * asked for. |
| 2367 | * |
| 2368 | * Special-case listen_addrs are: |
| 2369 | * |
| 2370 | * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR |
| 2371 | * "" (empty string), "*" -> wildcard v4/v6 |
| 2372 | * "localhost" -> loopback v4/v6 |
| 2373 | */ |
| 2374 | addr = NULL; |
| 2375 | if (listen_addr == NULL) { |
| 2376 | /* No address specified: default to gateway_ports setting */ |
| 2377 | if (gateway_ports) |
| 2378 | wildcard = 1; |
| 2379 | } else if (gateway_ports || is_client) { |
| 2380 | if (((datafellows & SSH_OLD_FORWARD_ADDR) && |
| 2381 | strcmp(listen_addr, "0.0.0.0") == 0) || |
| 2382 | *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 || |
| 2383 | (!is_client && gateway_ports == 1)) |
| 2384 | wildcard = 1; |
| 2385 | else if (strcmp(listen_addr, "localhost") != 0) |
| 2386 | addr = listen_addr; |
| 2387 | } |
| 2388 | |
| 2389 | debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s", |
| 2390 | type, wildcard, (addr == NULL) ? "NULL" : addr); |
| 2391 | |
| 2392 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2393 | * getaddrinfo returns a loopback address if the hostname is |
| 2394 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2395 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2396 | memset(&hints, 0, sizeof(hints)); |
| 2397 | hints.ai_family = IPv4or6; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2398 | hints.ai_flags = wildcard ? AI_PASSIVE : 0; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2399 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2400 | snprintf(strport, sizeof strport, "%d", listen_port); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2401 | if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) { |
| 2402 | if (addr == NULL) { |
| 2403 | /* This really shouldn't happen */ |
| 2404 | packet_disconnect("getaddrinfo: fatal error: %s", |
| 2405 | gai_strerror(r)); |
| 2406 | } else { |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 2407 | error("channel_setup_fwd_listener: " |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2408 | "getaddrinfo(%.64s): %s", addr, gai_strerror(r)); |
| 2409 | } |
Damien Miller | a727030 | 2005-07-06 09:36:05 +1000 | [diff] [blame] | 2410 | return 0; |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2411 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2412 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2413 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 2414 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 2415 | continue; |
| 2416 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 2417 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2418 | error("channel_setup_fwd_listener: getnameinfo failed"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2419 | continue; |
| 2420 | } |
| 2421 | /* Create a port to listen for the host. */ |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 2422 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2423 | if (sock < 0) { |
| 2424 | /* this is no error since kernel may not support ipv6 */ |
| 2425 | verbose("socket: %.100s", strerror(errno)); |
| 2426 | continue; |
| 2427 | } |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 2428 | |
| 2429 | channel_set_reuseaddr(sock); |
Damien Miller | e1383ce | 2002-09-19 11:49:37 +1000 | [diff] [blame] | 2430 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2431 | debug("Local forwarding listening on %s port %s.", ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2432 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2433 | /* Bind the socket to the address. */ |
| 2434 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 2435 | /* address can be in use ipv6 address is already bound */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 2436 | if (!ai->ai_next) |
| 2437 | error("bind: %.100s", strerror(errno)); |
| 2438 | else |
| 2439 | verbose("bind: %.100s", strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 2440 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2441 | close(sock); |
| 2442 | continue; |
| 2443 | } |
| 2444 | /* Start listening for connections on the socket. */ |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 2445 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2446 | error("listen: %.100s", strerror(errno)); |
| 2447 | close(sock); |
| 2448 | continue; |
| 2449 | } |
| 2450 | /* Allocate a channel number for the socket. */ |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2451 | c = channel_new("port listener", type, sock, sock, -1, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2452 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 2453 | 0, "port listener", 1); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 2454 | strlcpy(c->path, host, sizeof(c->path)); |
| 2455 | c->host_port = port_to_connect; |
| 2456 | c->listening_port = listen_port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2457 | success = 1; |
| 2458 | } |
| 2459 | if (success == 0) |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2460 | error("channel_setup_fwd_listener: cannot listen to port: %d", |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2461 | listen_port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2462 | freeaddrinfo(aitop); |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 2463 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2464 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2465 | |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2466 | int |
| 2467 | channel_cancel_rport_listener(const char *host, u_short port) |
| 2468 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 2469 | u_int i; |
| 2470 | int found = 0; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2471 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 2472 | for (i = 0; i < channels_alloc; i++) { |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2473 | Channel *c = channels[i]; |
| 2474 | |
| 2475 | if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER && |
| 2476 | strncmp(c->path, host, sizeof(c->path)) == 0 && |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 2477 | c->listening_port == port) { |
Darren Tucker | e6ed839 | 2004-08-29 16:29:44 +1000 | [diff] [blame] | 2478 | debug2("%s: close channel %d", __func__, i); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2479 | channel_free(c); |
| 2480 | found = 1; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | return (found); |
| 2485 | } |
| 2486 | |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2487 | /* protocol local port fwd, used by ssh (and sshd in v1) */ |
| 2488 | int |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2489 | channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port, |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2490 | const char *host_to_connect, u_short port_to_connect, int gateway_ports) |
| 2491 | { |
| 2492 | return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2493 | listen_host, listen_port, host_to_connect, port_to_connect, |
| 2494 | gateway_ports); |
Damien Miller | b16461c | 2002-01-22 23:29:22 +1100 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | /* protocol v2 remote port fwd, used by sshd */ |
| 2498 | int |
| 2499 | channel_setup_remote_fwd_listener(const char *listen_address, |
| 2500 | u_short listen_port, int gateway_ports) |
| 2501 | { |
| 2502 | return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER, |
| 2503 | listen_address, listen_port, NULL, 0, gateway_ports); |
| 2504 | } |
| 2505 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2506 | /* |
| 2507 | * Initiate forwarding of connections to port "port" on remote host through |
| 2508 | * the secure channel to host:port from local side. |
| 2509 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2510 | |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2511 | int |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2512 | channel_request_remote_forwarding(const char *listen_host, u_short listen_port, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2513 | const char *host_to_connect, u_short port_to_connect) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2514 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 2515 | int type, success = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2516 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2517 | /* Record locally that connection to this host/port is permitted. */ |
| 2518 | if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 2519 | fatal("channel_request_remote_forwarding: too many forwards"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2520 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2521 | /* Send the forward request to the remote side. */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2522 | if (compat20) { |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2523 | const char *address_to_bind; |
| 2524 | if (listen_host == NULL) |
| 2525 | address_to_bind = "localhost"; |
| 2526 | else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) |
| 2527 | address_to_bind = ""; |
| 2528 | else |
| 2529 | address_to_bind = listen_host; |
| 2530 | |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2531 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 2532 | packet_put_cstring("tcpip-forward"); |
Damien Miller | 2797f7f | 2002-04-23 21:09:44 +1000 | [diff] [blame] | 2533 | packet_put_char(1); /* boolean: want reply */ |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2534 | packet_put_cstring(address_to_bind); |
| 2535 | packet_put_int(listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2536 | packet_send(); |
| 2537 | packet_write_wait(); |
| 2538 | /* Assume that server accepts the request */ |
| 2539 | success = 1; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2540 | } else { |
| 2541 | packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2542 | packet_put_int(listen_port); |
Damien Miller | 8bb73be | 2000-04-19 16:26:12 +1000 | [diff] [blame] | 2543 | packet_put_cstring(host_to_connect); |
| 2544 | packet_put_int(port_to_connect); |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2545 | packet_send(); |
| 2546 | packet_write_wait(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2547 | |
| 2548 | /* Wait for response from the remote side. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 2549 | type = packet_read(); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2550 | switch (type) { |
| 2551 | case SSH_SMSG_SUCCESS: |
| 2552 | success = 1; |
| 2553 | break; |
| 2554 | case SSH_SMSG_FAILURE: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2555 | break; |
| 2556 | default: |
| 2557 | /* Unknown packet */ |
| 2558 | packet_disconnect("Protocol error for port forward request:" |
| 2559 | "received packet type %d.", type); |
| 2560 | } |
| 2561 | } |
| 2562 | if (success) { |
| 2563 | permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect); |
| 2564 | permitted_opens[num_permitted_opens].port_to_connect = port_to_connect; |
| 2565 | permitted_opens[num_permitted_opens].listen_port = listen_port; |
| 2566 | num_permitted_opens++; |
Damien Miller | 33b1356 | 2000-04-04 14:38:59 +1000 | [diff] [blame] | 2567 | } |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2568 | return (success ? 0 : -1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2569 | } |
| 2570 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2571 | /* |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 2572 | * Request cancellation of remote forwarding of connection host:port from |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2573 | * local side. |
| 2574 | */ |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2575 | void |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2576 | channel_request_rforward_cancel(const char *host, u_short port) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2577 | { |
| 2578 | int i; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2579 | |
| 2580 | if (!compat20) |
| 2581 | return; |
| 2582 | |
| 2583 | for (i = 0; i < num_permitted_opens; i++) { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 2584 | if (permitted_opens[i].host_to_connect != NULL && |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2585 | permitted_opens[i].listen_port == port) |
| 2586 | break; |
| 2587 | } |
| 2588 | if (i >= num_permitted_opens) { |
| 2589 | debug("%s: requested forward not found", __func__); |
| 2590 | return; |
| 2591 | } |
| 2592 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 2593 | packet_put_cstring("cancel-tcpip-forward"); |
| 2594 | packet_put_char(0); |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2595 | packet_put_cstring(host == NULL ? "" : host); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2596 | packet_put_int(port); |
| 2597 | packet_send(); |
| 2598 | |
| 2599 | permitted_opens[i].listen_port = 0; |
| 2600 | permitted_opens[i].port_to_connect = 0; |
Damien Miller | 0a0176e | 2005-11-05 15:07:59 +1100 | [diff] [blame] | 2601 | xfree(permitted_opens[i].host_to_connect); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2602 | permitted_opens[i].host_to_connect = NULL; |
| 2603 | } |
| 2604 | |
| 2605 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2606 | * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates |
| 2607 | * listening for the port, and sends back a success reply (or disconnect |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2608 | * message if there was an error). |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2609 | */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2610 | int |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 2611 | channel_input_port_forward_request(int is_root, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2612 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 2613 | u_short port, host_port; |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2614 | int success = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2615 | char *hostname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2616 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2617 | /* Get arguments from the packet. */ |
| 2618 | port = packet_get_int(); |
| 2619 | hostname = packet_get_string(NULL); |
| 2620 | host_port = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2621 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 2622 | #ifndef HAVE_CYGWIN |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2623 | /* |
| 2624 | * Check that an unprivileged user is not trying to forward a |
| 2625 | * privileged port. |
| 2626 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2627 | if (port < IPPORT_RESERVED && !is_root) |
Darren Tucker | 9189ff8 | 2003-07-03 13:52:04 +1000 | [diff] [blame] | 2628 | packet_disconnect( |
| 2629 | "Requested forwarding of port %d but user is not root.", |
| 2630 | port); |
| 2631 | if (host_port == 0) |
| 2632 | packet_disconnect("Dynamic forwarding denied."); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 2633 | #endif |
Darren Tucker | 9189ff8 | 2003-07-03 13:52:04 +1000 | [diff] [blame] | 2634 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2635 | /* Initiate forwarding */ |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2636 | success = channel_setup_local_fwd_listener(NULL, port, hostname, |
Damien Miller | f91ee4c | 2005-03-01 21:24:33 +1100 | [diff] [blame] | 2637 | host_port, gateway_ports); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2638 | |
| 2639 | /* Free the argument string. */ |
| 2640 | xfree(hostname); |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2641 | |
| 2642 | return (success ? 0 : -1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2643 | } |
| 2644 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2645 | /* |
| 2646 | * Permits opening to any host/port if permitted_opens[] is empty. This is |
| 2647 | * usually called by the server, because the user could connect to any port |
| 2648 | * anyway, and the server has no way to know but to trust the client anyway. |
| 2649 | */ |
| 2650 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 2651 | channel_permit_all_opens(void) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2652 | { |
| 2653 | if (num_permitted_opens == 0) |
| 2654 | all_opens_permitted = 1; |
| 2655 | } |
| 2656 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 2657 | void |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2658 | channel_add_permitted_opens(char *host, int port) |
| 2659 | { |
| 2660 | if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
Darren Tucker | e7d4b19 | 2006-07-12 22:17:10 +1000 | [diff] [blame] | 2661 | fatal("channel_add_permitted_opens: too many forwards"); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2662 | debug("allow port forwarding to host %s port %d", host, port); |
| 2663 | |
| 2664 | permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host); |
| 2665 | permitted_opens[num_permitted_opens].port_to_connect = port; |
| 2666 | num_permitted_opens++; |
| 2667 | |
| 2668 | all_opens_permitted = 0; |
| 2669 | } |
| 2670 | |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 2671 | int |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2672 | channel_add_adm_permitted_opens(char *host, int port) |
| 2673 | { |
| 2674 | if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 2675 | fatal("channel_add_adm_permitted_opens: too many forwards"); |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 2676 | debug("config allows port forwarding to host %s port %d", host, port); |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2677 | |
| 2678 | permitted_adm_opens[num_adm_permitted_opens].host_to_connect |
| 2679 | = xstrdup(host); |
| 2680 | permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port; |
Damien Miller | a765cf4 | 2006-07-24 14:08:13 +1000 | [diff] [blame] | 2681 | return ++num_adm_permitted_opens; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | void |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2685 | channel_clear_permitted_opens(void) |
| 2686 | { |
| 2687 | int i; |
| 2688 | |
| 2689 | for (i = 0; i < num_permitted_opens; i++) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2690 | if (permitted_opens[i].host_to_connect != NULL) |
| 2691 | xfree(permitted_opens[i].host_to_connect); |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2692 | num_permitted_opens = 0; |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2693 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2694 | |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2695 | void |
| 2696 | channel_clear_adm_permitted_opens(void) |
| 2697 | { |
| 2698 | int i; |
| 2699 | |
| 2700 | for (i = 0; i < num_adm_permitted_opens; i++) |
| 2701 | if (permitted_adm_opens[i].host_to_connect != NULL) |
| 2702 | xfree(permitted_adm_opens[i].host_to_connect); |
| 2703 | num_adm_permitted_opens = 0; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2706 | /* return socket to remote host, port */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2707 | static int |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2708 | connect_to(const char *host, u_short port) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2709 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2710 | struct addrinfo hints, *ai, *aitop; |
| 2711 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 2712 | int gaierr; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2713 | int sock = -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2714 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2715 | memset(&hints, 0, sizeof(hints)); |
| 2716 | hints.ai_family = IPv4or6; |
| 2717 | hints.ai_socktype = SOCK_STREAM; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2718 | snprintf(strport, sizeof strport, "%d", port); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2719 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2720 | error("connect_to %.100s: unknown host (%s)", host, |
| 2721 | gai_strerror(gaierr)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2722 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2723 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2724 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 2725 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 2726 | continue; |
| 2727 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 2728 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2729 | error("connect_to: getnameinfo failed"); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2730 | continue; |
| 2731 | } |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 2732 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2733 | if (sock < 0) { |
Damien Miller | b46b9f3 | 2003-01-10 21:45:12 +1100 | [diff] [blame] | 2734 | if (ai->ai_next == NULL) |
| 2735 | error("socket: %.100s", strerror(errno)); |
| 2736 | else |
| 2737 | verbose("socket: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2738 | continue; |
| 2739 | } |
Damien Miller | 232711f | 2004-06-15 10:35:30 +1000 | [diff] [blame] | 2740 | if (set_nonblock(sock) == -1) |
| 2741 | fatal("%s: set_nonblock(%d)", __func__, sock); |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 2742 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 && |
| 2743 | errno != EINPROGRESS) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2744 | error("connect_to %.100s port %s: %.100s", ntop, strport, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2745 | strerror(errno)); |
| 2746 | close(sock); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 2747 | continue; /* fail -- try next */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2748 | } |
| 2749 | break; /* success */ |
| 2750 | |
| 2751 | } |
| 2752 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2753 | if (!ai) { |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2754 | error("connect_to %.100s port %d: failed.", host, port); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2755 | return -1; |
| 2756 | } |
| 2757 | /* success */ |
Ben Lindstrom | 1ebd7a5 | 2002-02-26 18:12:51 +0000 | [diff] [blame] | 2758 | set_nodelay(sock); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2759 | return sock; |
| 2760 | } |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2761 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2762 | int |
Ben Lindstrom | 173e646 | 2001-07-04 05:15:15 +0000 | [diff] [blame] | 2763 | channel_connect_by_listen_address(u_short listen_port) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2764 | { |
| 2765 | int i; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2766 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2767 | for (i = 0; i < num_permitted_opens; i++) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2768 | if (permitted_opens[i].host_to_connect != NULL && |
| 2769 | permitted_opens[i].listen_port == listen_port) |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2770 | return connect_to( |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2771 | permitted_opens[i].host_to_connect, |
| 2772 | permitted_opens[i].port_to_connect); |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 2773 | error("WARNING: Server requests forwarding for unknown listen_port %d", |
| 2774 | listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2775 | return -1; |
| 2776 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 2777 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2778 | /* Check if connecting to that port is permitted and connect. */ |
| 2779 | int |
| 2780 | channel_connect_to(const char *host, u_short port) |
| 2781 | { |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2782 | int i, permit, permit_adm = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2783 | |
| 2784 | permit = all_opens_permitted; |
| 2785 | if (!permit) { |
| 2786 | for (i = 0; i < num_permitted_opens; i++) |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 2787 | if (permitted_opens[i].host_to_connect != NULL && |
| 2788 | permitted_opens[i].port_to_connect == port && |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2789 | strcmp(permitted_opens[i].host_to_connect, host) == 0) |
| 2790 | permit = 1; |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2791 | } |
Damien Miller | 9b439df | 2006-07-24 14:04:00 +1000 | [diff] [blame] | 2792 | |
| 2793 | if (num_adm_permitted_opens > 0) { |
| 2794 | permit_adm = 0; |
| 2795 | for (i = 0; i < num_adm_permitted_opens; i++) |
| 2796 | if (permitted_adm_opens[i].host_to_connect != NULL && |
| 2797 | permitted_adm_opens[i].port_to_connect == port && |
| 2798 | strcmp(permitted_adm_opens[i].host_to_connect, host) |
| 2799 | == 0) |
| 2800 | permit_adm = 1; |
| 2801 | } |
| 2802 | |
| 2803 | if (!permit || !permit_adm) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 2804 | logit("Received request to connect to host %.100s port %d, " |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 2805 | "but the request was denied.", host, port); |
| 2806 | return -1; |
| 2807 | } |
| 2808 | return connect_to(host, port); |
| 2809 | } |
| 2810 | |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2811 | void |
| 2812 | channel_send_window_changes(void) |
| 2813 | { |
Darren Tucker | c7a6fc4 | 2004-08-13 21:18:00 +1000 | [diff] [blame] | 2814 | u_int i; |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2815 | struct winsize ws; |
| 2816 | |
| 2817 | for (i = 0; i < channels_alloc; i++) { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 2818 | if (channels[i] == NULL || !channels[i]->client_tty || |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2819 | channels[i]->type != SSH_CHANNEL_OPEN) |
| 2820 | continue; |
| 2821 | if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0) |
| 2822 | continue; |
| 2823 | channel_request_start(i, "window-change", 0); |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 2824 | packet_put_int((u_int)ws.ws_col); |
| 2825 | packet_put_int((u_int)ws.ws_row); |
| 2826 | packet_put_int((u_int)ws.ws_xpixel); |
| 2827 | packet_put_int((u_int)ws.ws_ypixel); |
Damien Miller | 0e220db | 2004-06-15 10:34:08 +1000 | [diff] [blame] | 2828 | packet_send(); |
| 2829 | } |
| 2830 | } |
| 2831 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 2832 | /* -- X11 forwarding */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2833 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2834 | /* |
| 2835 | * Creates an internet domain socket for listening for X11 connections. |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2836 | * Returns 0 and a suitable display number for the DISPLAY variable |
| 2837 | * stored in display_numberp , or -1 if an error occurs. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2838 | */ |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2839 | int |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 2840 | x11_create_display_inet(int x11_display_offset, int x11_use_localhost, |
Damien Miller | 2b9b045 | 2005-07-17 17:19:24 +1000 | [diff] [blame] | 2841 | int single_connection, u_int *display_numberp, int **chanids) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2842 | { |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 2843 | Channel *nc = NULL; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 2844 | int display_number, sock; |
| 2845 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2846 | struct addrinfo hints, *ai, *aitop; |
| 2847 | char strport[NI_MAXSERV]; |
| 2848 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2849 | |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 2850 | if (chanids == NULL) |
| 2851 | return -1; |
| 2852 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 2853 | for (display_number = x11_display_offset; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 2854 | display_number < MAX_DISPLAYS; |
| 2855 | display_number++) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2856 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2857 | memset(&hints, 0, sizeof(hints)); |
| 2858 | hints.ai_family = IPv4or6; |
Damien Miller | 95c249f | 2002-02-05 12:11:34 +1100 | [diff] [blame] | 2859 | hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2860 | hints.ai_socktype = SOCK_STREAM; |
| 2861 | snprintf(strport, sizeof strport, "%d", port); |
| 2862 | if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) { |
| 2863 | error("getaddrinfo: %.100s", gai_strerror(gaierr)); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2864 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2865 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2866 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 2867 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 2868 | continue; |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 2869 | sock = socket(ai->ai_family, ai->ai_socktype, |
| 2870 | ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2871 | if (sock < 0) { |
Damien Miller | 89d9796 | 2000-10-14 12:37:19 +1100 | [diff] [blame] | 2872 | if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 2873 | error("socket: %.100s", strerror(errno)); |
Damien Miller | 3e4dffb | 2004-06-15 10:27:15 +1000 | [diff] [blame] | 2874 | freeaddrinfo(aitop); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2875 | return -1; |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 2876 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 2877 | debug("x11_create_display_inet: Socket family %d not supported", |
| 2878 | ai->ai_family); |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 2879 | continue; |
| 2880 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2881 | } |
Kevin Steves | df75dd2 | 2002-06-04 20:52:19 +0000 | [diff] [blame] | 2882 | #ifdef IPV6_V6ONLY |
| 2883 | if (ai->ai_family == AF_INET6) { |
| 2884 | int on = 1; |
| 2885 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) |
| 2886 | error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno)); |
| 2887 | } |
| 2888 | #endif |
Damien Miller | 5e7fd07 | 2005-11-05 14:53:39 +1100 | [diff] [blame] | 2889 | channel_set_reuseaddr(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2890 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 2891 | debug2("bind port %d: %.100s", port, strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2892 | close(sock); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 2893 | |
| 2894 | if (ai->ai_next) |
| 2895 | continue; |
| 2896 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2897 | for (n = 0; n < num_socks; n++) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2898 | close(socks[n]); |
| 2899 | } |
| 2900 | num_socks = 0; |
| 2901 | break; |
| 2902 | } |
| 2903 | socks[num_socks++] = sock; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 2904 | #ifndef DONT_TRY_OTHER_AF |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2905 | if (num_socks == NUM_SOCKS) |
| 2906 | break; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 2907 | #else |
Kevin Steves | df75dd2 | 2002-06-04 20:52:19 +0000 | [diff] [blame] | 2908 | if (x11_use_localhost) { |
| 2909 | if (num_socks == NUM_SOCKS) |
| 2910 | break; |
| 2911 | } else { |
| 2912 | break; |
| 2913 | } |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 2914 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2915 | } |
Ben Lindstrom | 87b147f | 2001-01-25 00:41:12 +0000 | [diff] [blame] | 2916 | freeaddrinfo(aitop); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2917 | if (num_socks > 0) |
| 2918 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2919 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2920 | if (display_number >= MAX_DISPLAYS) { |
| 2921 | error("Failed to allocate internet-domain X11 display socket."); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2922 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2923 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2924 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2925 | for (n = 0; n < num_socks; n++) { |
| 2926 | sock = socks[n]; |
Darren Tucker | 3175eb9 | 2003-12-09 19:15:11 +1100 | [diff] [blame] | 2927 | if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2928 | error("listen: %.100s", strerror(errno)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2929 | close(sock); |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2930 | return -1; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2931 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2932 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2933 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2934 | /* Allocate a channel for each socket. */ |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 2935 | *chanids = xcalloc(num_socks + 1, sizeof(**chanids)); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2936 | for (n = 0; n < num_socks; n++) { |
| 2937 | sock = socks[n]; |
Damien Miller | e737856 | 2001-12-21 14:58:35 +1100 | [diff] [blame] | 2938 | nc = channel_new("x11 listener", |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2939 | SSH_CHANNEL_X11_LISTENER, sock, sock, -1, |
| 2940 | CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 2941 | 0, "X11 inet listener", 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 2942 | nc->single_connection = single_connection; |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 2943 | (*chanids)[n] = nc->self; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2944 | } |
Darren Tucker | d3d0fa1 | 2005-10-03 18:03:05 +1000 | [diff] [blame] | 2945 | (*chanids)[n] = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2946 | |
Kevin Steves | 366298c | 2001-12-19 17:58:01 +0000 | [diff] [blame] | 2947 | /* Return the display number for the DISPLAY environment variable. */ |
Ben Lindstrom | a9d2c89 | 2002-06-23 21:48:28 +0000 | [diff] [blame] | 2948 | *display_numberp = display_number; |
| 2949 | return (0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2950 | } |
| 2951 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2952 | static int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 2953 | connect_local_xsocket(u_int dnr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2954 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2955 | int sock; |
| 2956 | struct sockaddr_un addr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2957 | |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 2958 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 2959 | if (sock < 0) |
| 2960 | error("socket: %.100s", strerror(errno)); |
| 2961 | memset(&addr, 0, sizeof(addr)); |
| 2962 | addr.sun_family = AF_UNIX; |
| 2963 | snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr); |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 2964 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) |
Damien Miller | 3afe375 | 2001-12-21 12:39:51 +1100 | [diff] [blame] | 2965 | return sock; |
| 2966 | close(sock); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2967 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 2968 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2969 | } |
| 2970 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2971 | int |
| 2972 | x11_connect_display(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2973 | { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 2974 | u_int display_number; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2975 | const char *display; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2976 | char buf[1024], *cp; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 2977 | struct addrinfo hints, *ai, *aitop; |
| 2978 | char strport[NI_MAXSERV]; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 2979 | int gaierr, sock = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2980 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2981 | /* Try to open a socket for the local X server. */ |
| 2982 | display = getenv("DISPLAY"); |
| 2983 | if (!display) { |
| 2984 | error("DISPLAY not set."); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 2985 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2986 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2987 | /* |
| 2988 | * Now we decode the value of the DISPLAY variable and make a |
| 2989 | * connection to the real X server. |
| 2990 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2991 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2992 | /* |
| 2993 | * Check if it is a unix domain socket. Unix domain displays are in |
| 2994 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 2995 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2996 | if (strncmp(display, "unix:", 5) == 0 || |
| 2997 | display[0] == ':') { |
| 2998 | /* Connect to the unix domain socket. */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 2999 | if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3000 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 3001 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3002 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3003 | } |
| 3004 | /* Create a socket. */ |
| 3005 | sock = connect_local_xsocket(display_number); |
| 3006 | if (sock < 0) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3007 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3008 | |
| 3009 | /* OK, we now have a connection to the display. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3010 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3011 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3012 | /* |
| 3013 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 3014 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 3015 | */ |
Ben Lindstrom | ccd8d07 | 2001-12-07 17:26:48 +0000 | [diff] [blame] | 3016 | strlcpy(buf, display, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3017 | cp = strchr(buf, ':'); |
| 3018 | if (!cp) { |
| 3019 | error("Could not find ':' in DISPLAY: %.100s", display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3020 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3021 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3022 | *cp = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3023 | /* buf now contains the host name. But first we parse the display number. */ |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 3024 | if (sscanf(cp + 1, "%u", &display_number) != 1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3025 | error("Could not parse display number from DISPLAY: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 3026 | display); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3027 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3028 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3029 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3030 | /* Look up the host address */ |
| 3031 | memset(&hints, 0, sizeof(hints)); |
| 3032 | hints.ai_family = IPv4or6; |
| 3033 | hints.ai_socktype = SOCK_STREAM; |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 3034 | snprintf(strport, sizeof strport, "%u", 6000 + display_number); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3035 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
| 3036 | error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3037 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3038 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3039 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 3040 | /* Create a socket. */ |
Damien Miller | 2372ace | 2003-05-14 13:42:23 +1000 | [diff] [blame] | 3041 | sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3042 | if (sock < 0) { |
Damien Miller | fbdeece | 2003-09-02 22:52:31 +1000 | [diff] [blame] | 3043 | debug2("socket: %.100s", strerror(errno)); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3044 | continue; |
| 3045 | } |
| 3046 | /* Connect it to the display. */ |
| 3047 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 3048 | debug2("connect %.100s port %u: %.100s", buf, |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 3049 | 6000 + display_number, strerror(errno)); |
| 3050 | close(sock); |
| 3051 | continue; |
| 3052 | } |
| 3053 | /* Success */ |
| 3054 | break; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3055 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3056 | freeaddrinfo(aitop); |
| 3057 | if (!ai) { |
Damien Miller | 57c4e87 | 2006-03-31 23:11:07 +1100 | [diff] [blame] | 3058 | error("connect %.100s port %u: %.100s", buf, 6000 + display_number, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 3059 | strerror(errno)); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3060 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3061 | } |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 3062 | set_nodelay(sock); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3063 | return sock; |
| 3064 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3065 | |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3066 | /* |
| 3067 | * This is called when SSH_SMSG_X11_OPEN is received. The packet contains |
| 3068 | * the remote channel number. We should do whatever we want, and respond |
| 3069 | * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. |
| 3070 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3071 | |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 3072 | /* ARGSUSED */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3073 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 3074 | x11_input_open(int type, u_int32_t seq, void *ctxt) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3075 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3076 | Channel *c = NULL; |
| 3077 | int remote_id, sock = 0; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3078 | char *remote_host; |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3079 | |
| 3080 | debug("Received X11 open request."); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3081 | |
| 3082 | remote_id = packet_get_int(); |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3083 | |
| 3084 | if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3085 | remote_host = packet_get_string(NULL); |
| 3086 | } else { |
| 3087 | remote_host = xstrdup("unknown (remote did not supply name)"); |
| 3088 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 3089 | packet_check_eom(); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3090 | |
| 3091 | /* Obtain a connection to the real X display. */ |
| 3092 | sock = x11_connect_display(); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3093 | if (sock != -1) { |
| 3094 | /* Allocate a channel for this connection. */ |
| 3095 | c = channel_new("connected x11 socket", |
| 3096 | SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0, |
| 3097 | remote_host, 1); |
Damien Miller | 699d003 | 2002-02-08 22:07:16 +1100 | [diff] [blame] | 3098 | c->remote_id = remote_id; |
| 3099 | c->force_drain = 1; |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3100 | } |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 3101 | xfree(remote_host); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3102 | if (c == NULL) { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3103 | /* Send refusal to the remote host. */ |
| 3104 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3105 | packet_put_int(remote_id); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3106 | } else { |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3107 | /* Send a confirmation to the remote host. */ |
| 3108 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3109 | packet_put_int(remote_id); |
| 3110 | packet_put_int(c->self); |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3111 | } |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 3112 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3113 | } |
| 3114 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3115 | /* dummy protocol handler that denies SSH-1 requests (agent/x11) */ |
Damien Miller | 8473dd8 | 2006-07-24 14:08:32 +1000 | [diff] [blame] | 3116 | /* ARGSUSED */ |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3117 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 3118 | deny_input_open(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3119 | { |
| 3120 | int rchan = packet_get_int(); |
Ben Lindstrom | 8b2eecd | 2002-07-07 22:11:51 +0000 | [diff] [blame] | 3121 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 3122 | switch (type) { |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3123 | case SSH_SMSG_AGENT_OPEN: |
| 3124 | error("Warning: ssh server tried agent forwarding."); |
| 3125 | break; |
| 3126 | case SSH_SMSG_X11_OPEN: |
| 3127 | error("Warning: ssh server tried X11 forwarding."); |
| 3128 | break; |
| 3129 | default: |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 3130 | error("deny_input_open: type %d", type); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3131 | break; |
| 3132 | } |
Damien Miller | 5eb137c | 2005-12-31 16:19:53 +1100 | [diff] [blame] | 3133 | error("Warning: this is probably a break-in attempt by a malicious server."); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 3134 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 3135 | packet_put_int(rchan); |
| 3136 | packet_send(); |
| 3137 | } |
| 3138 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3139 | /* |
| 3140 | * Requests forwarding of X11 connections, generates fake authentication |
| 3141 | * data, and enables authentication spoofing. |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3142 | * This should be called in the client only. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 3143 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 3144 | void |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 3145 | x11_request_forwarding_with_spoofing(int client_session_id, const char *disp, |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3146 | const char *proto, const char *data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3147 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 3148 | u_int data_len = (u_int) strlen(data) / 2; |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 3149 | u_int i, value; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3150 | char *new_data; |
| 3151 | int screen_number; |
| 3152 | const char *cp; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 3153 | u_int32_t rnd = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3154 | |
Damien Miller | f92c079 | 2005-07-06 09:45:26 +1000 | [diff] [blame] | 3155 | if (x11_saved_display == NULL) |
| 3156 | x11_saved_display = xstrdup(disp); |
| 3157 | else if (strcmp(disp, x11_saved_display) != 0) { |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 3158 | error("x11_request_forwarding_with_spoofing: different " |
| 3159 | "$DISPLAY already forwarded"); |
| 3160 | return; |
| 3161 | } |
| 3162 | |
Damien Miller | 17e7ed0 | 2005-06-17 12:54:33 +1000 | [diff] [blame] | 3163 | cp = disp; |
| 3164 | if (disp) |
| 3165 | cp = strchr(disp, ':'); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3166 | if (cp) |
| 3167 | cp = strchr(cp, '.'); |
| 3168 | if (cp) |
Damien Miller | 08d6150 | 2006-03-26 14:28:32 +1100 | [diff] [blame] | 3169 | screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3170 | else |
| 3171 | screen_number = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3172 | |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 3173 | if (x11_saved_proto == NULL) { |
| 3174 | /* Save protocol name. */ |
| 3175 | x11_saved_proto = xstrdup(proto); |
| 3176 | /* |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 3177 | * Extract real authentication data and generate fake data |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 3178 | * of the same length. |
| 3179 | */ |
| 3180 | x11_saved_data = xmalloc(data_len); |
| 3181 | x11_fake_data = xmalloc(data_len); |
| 3182 | for (i = 0; i < data_len; i++) { |
| 3183 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 3184 | fatal("x11_request_forwarding: bad " |
| 3185 | "authentication data: %.100s", data); |
| 3186 | if (i % 4 == 0) |
| 3187 | rnd = arc4random(); |
| 3188 | x11_saved_data[i] = value; |
| 3189 | x11_fake_data[i] = rnd & 0xff; |
| 3190 | rnd >>= 8; |
| 3191 | } |
| 3192 | x11_saved_data_len = data_len; |
| 3193 | x11_fake_data_len = data_len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3194 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3195 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3196 | /* Convert the fake data into hex. */ |
Damien Miller | 1339002 | 2005-07-06 09:44:19 +1000 | [diff] [blame] | 3197 | new_data = tohex(x11_fake_data, data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3198 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3199 | /* Send the request packet. */ |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 3200 | if (compat20) { |
| 3201 | channel_request_start(client_session_id, "x11-req", 0); |
| 3202 | packet_put_char(0); /* XXX bool single connection */ |
| 3203 | } else { |
| 3204 | packet_start(SSH_CMSG_X11_REQUEST_FORWARDING); |
| 3205 | } |
| 3206 | packet_put_cstring(proto); |
| 3207 | packet_put_cstring(new_data); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3208 | packet_put_int(screen_number); |
| 3209 | packet_send(); |
| 3210 | packet_write_wait(); |
| 3211 | xfree(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3212 | } |
| 3213 | |
Ben Lindstrom | e9c9991 | 2001-06-09 00:41:05 +0000 | [diff] [blame] | 3214 | |
| 3215 | /* -- agent forwarding */ |
| 3216 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3217 | /* Sends a message to the server to request authentication fd forwarding. */ |
| 3218 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 3219 | void |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 3220 | auth_request_forwarding(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3221 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3222 | packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING); |
| 3223 | packet_send(); |
| 3224 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 3225 | } |