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