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