Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * channels.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Fri Mar 24 16:35:24 1995 ylo |
| 11 | * |
| 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. |
| 15 | * |
| 16 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
| 18 | #include "includes.h" |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 19 | RCSID("$Id: channels.c,v 1.18 2000/03/11 09:45:41 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | |
| 21 | #include "ssh.h" |
| 22 | #include "packet.h" |
| 23 | #include "xmalloc.h" |
| 24 | #include "buffer.h" |
| 25 | #include "authfd.h" |
| 26 | #include "uidswap.h" |
Damien Miller | 6d7b2cd | 1999-11-12 15:19:27 +1100 | [diff] [blame] | 27 | #include "readconf.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | #include "servconf.h" |
| 29 | |
| 30 | #include "channels.h" |
| 31 | #include "nchan.h" |
| 32 | #include "compat.h" |
| 33 | |
| 34 | /* Maximum number of fake X11 displays to try. */ |
| 35 | #define MAX_DISPLAYS 1000 |
| 36 | |
| 37 | /* Max len of agent socket */ |
| 38 | #define MAX_SOCKET_NAME 100 |
| 39 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 40 | /* |
| 41 | * Pointer to an array containing all allocated channels. The array is |
| 42 | * dynamically extended as needed. |
| 43 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | static Channel *channels = NULL; |
| 45 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 46 | /* |
| 47 | * Size of the channel array. All slots of the array must always be |
| 48 | * initialized (at least the type field); unused slots are marked with type |
| 49 | * SSH_CHANNEL_FREE. |
| 50 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | static int channels_alloc = 0; |
| 52 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 53 | /* |
| 54 | * Maximum file descriptor value used in any of the channels. This is |
| 55 | * updated in channel_allocate. |
| 56 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | static int channel_max_fd_value = 0; |
| 58 | |
| 59 | /* Name and directory of socket for authentication agent forwarding. */ |
| 60 | static char *channel_forwarded_auth_socket_name = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | static char *channel_forwarded_auth_socket_dir = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | |
| 63 | /* Saved X11 authentication protocol name. */ |
| 64 | char *x11_saved_proto = NULL; |
| 65 | |
| 66 | /* Saved X11 authentication data. This is the real data. */ |
| 67 | char *x11_saved_data = NULL; |
| 68 | unsigned int x11_saved_data_len = 0; |
| 69 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 70 | /* |
| 71 | * Fake X11 authentication data. This is what the server will be sending us; |
| 72 | * we should replace any occurrences of this by the real data. |
| 73 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | char *x11_fake_data = NULL; |
| 75 | unsigned int x11_fake_data_len; |
| 76 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 77 | /* |
| 78 | * Data structure for storing which hosts are permitted for forward requests. |
| 79 | * The local sides of any remote forwards are stored in this array to prevent |
| 80 | * a corrupt remote server from accessing arbitrary TCP/IP ports on our local |
| 81 | * network (which might be behind a firewall). |
| 82 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 83 | typedef struct { |
| 84 | char *host; /* Host name. */ |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 85 | u_short port; /* Port number. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | } ForwardPermission; |
| 87 | |
| 88 | /* List of all permitted host/port pairs to connect. */ |
| 89 | static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION]; |
| 90 | /* Number of permitted host/port pairs in the array. */ |
| 91 | static int num_permitted_opens = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 92 | /* |
| 93 | * If this is true, all opens are permitted. This is the case on the server |
| 94 | * on which we have to trust the client anyway, and the user could do |
| 95 | * anything after logging in anyway. |
| 96 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | static int all_opens_permitted = 0; |
| 98 | |
| 99 | /* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */ |
| 100 | static int have_hostname_in_open = 0; |
| 101 | |
| 102 | /* Sets specific protocol options. */ |
| 103 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 104 | void |
| 105 | channel_set_options(int hostname_in_open) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 106 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 107 | have_hostname_in_open = hostname_in_open; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 108 | } |
| 109 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 110 | /* |
| 111 | * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually |
| 112 | * called by the server, because the user could connect to any port anyway, |
| 113 | * and the server has no way to know but to trust the client anyway. |
| 114 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | void |
| 117 | channel_permit_all_opens() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | all_opens_permitted = 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | } |
| 121 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 122 | /* |
| 123 | * Allocate a new channel object and set its type and socket. This will cause |
| 124 | * remote_name to be freed. |
| 125 | */ |
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 | int |
| 128 | channel_allocate(int type, int sock, char *remote_name) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | int i, found; |
| 131 | Channel *c; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | /* Update the maximum file descriptor value. */ |
| 134 | if (sock > channel_max_fd_value) |
| 135 | channel_max_fd_value = sock; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 136 | /* XXX set close-on-exec -markus */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 137 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | /* Do initial allocation if this is the first call. */ |
| 139 | if (channels_alloc == 0) { |
| 140 | channels_alloc = 10; |
| 141 | channels = xmalloc(channels_alloc * sizeof(Channel)); |
| 142 | for (i = 0; i < channels_alloc; i++) |
| 143 | channels[i].type = SSH_CHANNEL_FREE; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 144 | /* |
| 145 | * Kludge: arrange a call to channel_stop_listening if we |
| 146 | * terminate with fatal(). |
| 147 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL); |
| 149 | } |
| 150 | /* Try to find a free slot where to put the new channel. */ |
| 151 | for (found = -1, i = 0; i < channels_alloc; i++) |
| 152 | if (channels[i].type == SSH_CHANNEL_FREE) { |
| 153 | /* Found a free slot. */ |
| 154 | found = i; |
| 155 | break; |
| 156 | } |
| 157 | if (found == -1) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 158 | /* 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] | 159 | found = channels_alloc; |
| 160 | channels_alloc += 10; |
| 161 | debug("channel: expanding %d", channels_alloc); |
| 162 | channels = xrealloc(channels, channels_alloc * sizeof(Channel)); |
| 163 | for (i = found; i < channels_alloc; i++) |
| 164 | channels[i].type = SSH_CHANNEL_FREE; |
| 165 | } |
| 166 | /* Initialize and return new channel number. */ |
| 167 | c = &channels[found]; |
| 168 | buffer_init(&c->input); |
| 169 | buffer_init(&c->output); |
| 170 | chan_init_iostates(c); |
| 171 | c->self = found; |
| 172 | c->type = type; |
| 173 | c->sock = sock; |
| 174 | c->remote_id = -1; |
| 175 | c->remote_name = remote_name; |
| 176 | debug("channel %d: new [%s]", found, remote_name); |
| 177 | return found; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Free the channel and close its socket. */ |
| 181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | void |
| 183 | channel_free(int channel) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | if (channel < 0 || channel >= channels_alloc || |
| 186 | channels[channel].type == SSH_CHANNEL_FREE) |
| 187 | packet_disconnect("channel free: bad local channel %d", channel); |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 188 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | if (compat13) |
| 190 | shutdown(channels[channel].sock, SHUT_RDWR); |
| 191 | close(channels[channel].sock); |
| 192 | buffer_free(&channels[channel].input); |
| 193 | buffer_free(&channels[channel].output); |
| 194 | channels[channel].type = SSH_CHANNEL_FREE; |
| 195 | if (channels[channel].remote_name) { |
| 196 | xfree(channels[channel].remote_name); |
| 197 | channels[channel].remote_name = NULL; |
| 198 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | } |
| 200 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 201 | /* |
| 202 | * This is called just before select() to add any bits relevant to channels |
| 203 | * in the select bitmasks. |
| 204 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 205 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 206 | void |
| 207 | channel_prepare_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | int i; |
| 210 | Channel *ch; |
| 211 | unsigned char *ucp; |
| 212 | unsigned int proto_len, data_len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | for (i = 0; i < channels_alloc; i++) { |
| 215 | ch = &channels[i]; |
| 216 | redo: |
| 217 | switch (ch->type) { |
| 218 | case SSH_CHANNEL_X11_LISTENER: |
| 219 | case SSH_CHANNEL_PORT_LISTENER: |
| 220 | case SSH_CHANNEL_AUTH_SOCKET: |
| 221 | FD_SET(ch->sock, readset); |
| 222 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 223 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | case SSH_CHANNEL_OPEN: |
| 225 | if (compat13) { |
| 226 | if (buffer_len(&ch->input) < packet_get_maxsize()) |
| 227 | FD_SET(ch->sock, readset); |
| 228 | if (buffer_len(&ch->output) > 0) |
| 229 | FD_SET(ch->sock, writeset); |
| 230 | break; |
| 231 | } |
| 232 | /* test whether sockets are 'alive' for read/write */ |
| 233 | if (ch->istate == CHAN_INPUT_OPEN) |
| 234 | if (buffer_len(&ch->input) < packet_get_maxsize()) |
| 235 | FD_SET(ch->sock, readset); |
| 236 | if (ch->ostate == CHAN_OUTPUT_OPEN || |
| 237 | ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 238 | if (buffer_len(&ch->output) > 0) { |
| 239 | FD_SET(ch->sock, writeset); |
| 240 | } else if (ch->ostate == CHAN_OUTPUT_WAIT_DRAIN) { |
| 241 | chan_obuf_empty(ch); |
| 242 | } |
| 243 | } |
| 244 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 245 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 246 | case SSH_CHANNEL_INPUT_DRAINING: |
| 247 | if (!compat13) |
| 248 | fatal("cannot happen: IN_DRAIN"); |
| 249 | if (buffer_len(&ch->input) == 0) { |
| 250 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 251 | packet_put_int(ch->remote_id); |
| 252 | packet_send(); |
| 253 | ch->type = SSH_CHANNEL_CLOSED; |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 254 | debug("Closing channel %d after input drain.", ch->self); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 260 | if (!compat13) |
| 261 | fatal("cannot happen: OUT_DRAIN"); |
| 262 | if (buffer_len(&ch->output) == 0) { |
| 263 | channel_free(i); |
| 264 | break; |
| 265 | } |
| 266 | FD_SET(ch->sock, writeset); |
| 267 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 268 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 269 | case SSH_CHANNEL_X11_OPEN: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 270 | /* |
| 271 | * This is a special state for X11 authentication |
| 272 | * spoofing. An opened X11 connection (when |
| 273 | * authentication spoofing is being done) remains in |
| 274 | * this state until the first packet has been |
| 275 | * completely read. The authentication data in that |
| 276 | * packet is then substituted by the real data if it |
| 277 | * matches the fake data, and the channel is put into |
| 278 | * normal mode. |
| 279 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 280 | /* Check if the fixed size part of the packet is in buffer. */ |
| 281 | if (buffer_len(&ch->output) < 12) |
| 282 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 283 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 284 | /* Parse the lengths of variable-length fields. */ |
| 285 | ucp = (unsigned char *) buffer_ptr(&ch->output); |
| 286 | if (ucp[0] == 0x42) { /* Byte order MSB first. */ |
| 287 | proto_len = 256 * ucp[6] + ucp[7]; |
| 288 | data_len = 256 * ucp[8] + ucp[9]; |
| 289 | } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */ |
| 290 | proto_len = ucp[6] + 256 * ucp[7]; |
| 291 | data_len = ucp[8] + 256 * ucp[9]; |
| 292 | } else { |
| 293 | debug("Initial X11 packet contains bad byte order byte: 0x%x", |
| 294 | ucp[0]); |
| 295 | ch->type = SSH_CHANNEL_OPEN; |
| 296 | goto reject; |
| 297 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 298 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 299 | /* Check if the whole packet is in buffer. */ |
| 300 | if (buffer_len(&ch->output) < |
| 301 | 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3)) |
| 302 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 303 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 304 | /* Check if authentication protocol matches. */ |
| 305 | if (proto_len != strlen(x11_saved_proto) || |
| 306 | memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) { |
| 307 | debug("X11 connection uses different authentication protocol."); |
| 308 | ch->type = SSH_CHANNEL_OPEN; |
| 309 | goto reject; |
| 310 | } |
| 311 | /* Check if authentication data matches our fake data. */ |
| 312 | if (data_len != x11_fake_data_len || |
| 313 | memcmp(ucp + 12 + ((proto_len + 3) & ~3), |
| 314 | x11_fake_data, x11_fake_data_len) != 0) { |
| 315 | debug("X11 auth data does not match fake data."); |
| 316 | ch->type = SSH_CHANNEL_OPEN; |
| 317 | goto reject; |
| 318 | } |
| 319 | /* Check fake data length */ |
| 320 | if (x11_fake_data_len != x11_saved_data_len) { |
| 321 | error("X11 fake_data_len %d != saved_data_len %d", |
| 322 | x11_fake_data_len, x11_saved_data_len); |
| 323 | ch->type = SSH_CHANNEL_OPEN; |
| 324 | goto reject; |
| 325 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 326 | /* |
| 327 | * Received authentication protocol and data match |
| 328 | * our fake data. Substitute the fake data with real |
| 329 | * data. |
| 330 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 331 | memcpy(ucp + 12 + ((proto_len + 3) & ~3), |
| 332 | x11_saved_data, x11_saved_data_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 333 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 334 | /* Start normal processing for the channel. */ |
| 335 | ch->type = SSH_CHANNEL_OPEN; |
| 336 | goto redo; |
Damien Miller | fd7c911 | 1999-11-08 16:15:55 +1100 | [diff] [blame] | 337 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | reject: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 339 | /* |
| 340 | * We have received an X11 connection that has bad |
| 341 | * authentication information. |
| 342 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | log("X11 connection rejected because of wrong authentication.\r\n"); |
| 344 | buffer_clear(&ch->input); |
| 345 | buffer_clear(&ch->output); |
| 346 | if (compat13) { |
| 347 | close(ch->sock); |
| 348 | ch->sock = -1; |
| 349 | ch->type = SSH_CHANNEL_CLOSED; |
| 350 | packet_start(SSH_MSG_CHANNEL_CLOSE); |
| 351 | packet_put_int(ch->remote_id); |
| 352 | packet_send(); |
| 353 | } else { |
| 354 | debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate); |
| 355 | chan_read_failed(ch); |
| 356 | chan_write_failed(ch); |
| 357 | debug("X11 rejected %d i%d/o%d", ch->self, ch->istate, ch->ostate); |
| 358 | } |
| 359 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 360 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | case SSH_CHANNEL_FREE: |
| 362 | default: |
| 363 | continue; |
| 364 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 365 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 366 | } |
| 367 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 368 | /* |
| 369 | * After select, perform any appropriate operations for channels which have |
| 370 | * events pending. |
| 371 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 372 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | void |
| 374 | channel_after_select(fd_set * readset, fd_set * writeset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 375 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 376 | struct sockaddr addr; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 377 | int newsock, i, newch, len; |
| 378 | socklen_t addrlen; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | Channel *ch; |
| 380 | char buf[16384], *remote_hostname; |
| 381 | |
| 382 | /* Loop over all channels... */ |
| 383 | for (i = 0; i < channels_alloc; i++) { |
| 384 | ch = &channels[i]; |
| 385 | switch (ch->type) { |
| 386 | case SSH_CHANNEL_X11_LISTENER: |
| 387 | /* This is our fake X11 server socket. */ |
| 388 | if (FD_ISSET(ch->sock, readset)) { |
| 389 | debug("X11 connection requested."); |
| 390 | addrlen = sizeof(addr); |
| 391 | newsock = accept(ch->sock, &addr, &addrlen); |
| 392 | if (newsock < 0) { |
| 393 | error("accept: %.100s", strerror(errno)); |
| 394 | break; |
| 395 | } |
| 396 | remote_hostname = get_remote_hostname(newsock); |
| 397 | snprintf(buf, sizeof buf, "X11 connection from %.200s port %d", |
| 398 | remote_hostname, get_peer_port(newsock)); |
| 399 | xfree(remote_hostname); |
| 400 | newch = channel_allocate(SSH_CHANNEL_OPENING, newsock, |
| 401 | xstrdup(buf)); |
| 402 | packet_start(SSH_SMSG_X11_OPEN); |
| 403 | packet_put_int(newch); |
| 404 | if (have_hostname_in_open) |
| 405 | packet_put_string(buf, strlen(buf)); |
| 406 | packet_send(); |
| 407 | } |
| 408 | break; |
| 409 | |
| 410 | case SSH_CHANNEL_PORT_LISTENER: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 411 | /* |
| 412 | * This socket is listening for connections to a |
| 413 | * forwarded TCP/IP port. |
| 414 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 415 | if (FD_ISSET(ch->sock, readset)) { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 416 | debug("Connection to port %d forwarding to %.100s port %d requested.", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 417 | ch->listening_port, ch->path, ch->host_port); |
| 418 | addrlen = sizeof(addr); |
| 419 | newsock = accept(ch->sock, &addr, &addrlen); |
| 420 | if (newsock < 0) { |
| 421 | error("accept: %.100s", strerror(errno)); |
| 422 | break; |
| 423 | } |
| 424 | remote_hostname = get_remote_hostname(newsock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 425 | snprintf(buf, sizeof buf, "listen port %d for %.100s port %d, connect from %.200s port %d", |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 426 | ch->listening_port, ch->path, ch->host_port, |
| 427 | remote_hostname, get_peer_port(newsock)); |
| 428 | xfree(remote_hostname); |
| 429 | newch = channel_allocate(SSH_CHANNEL_OPENING, newsock, |
| 430 | xstrdup(buf)); |
| 431 | packet_start(SSH_MSG_PORT_OPEN); |
| 432 | packet_put_int(newch); |
| 433 | packet_put_string(ch->path, strlen(ch->path)); |
| 434 | packet_put_int(ch->host_port); |
| 435 | if (have_hostname_in_open) |
| 436 | packet_put_string(buf, strlen(buf)); |
| 437 | packet_send(); |
| 438 | } |
| 439 | break; |
| 440 | |
| 441 | case SSH_CHANNEL_AUTH_SOCKET: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 442 | /* |
| 443 | * This is the authentication agent socket listening |
| 444 | * for connections from clients. |
| 445 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 446 | if (FD_ISSET(ch->sock, readset)) { |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 447 | addrlen = sizeof(addr); |
| 448 | newsock = accept(ch->sock, &addr, &addrlen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 449 | if (newsock < 0) { |
| 450 | error("accept from auth socket: %.100s", strerror(errno)); |
| 451 | break; |
| 452 | } |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 453 | newch = channel_allocate(SSH_CHANNEL_OPENING, newsock, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 454 | xstrdup("accepted auth socket")); |
| 455 | packet_start(SSH_SMSG_AGENT_OPEN); |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 456 | packet_put_int(newch); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 457 | packet_send(); |
| 458 | } |
| 459 | break; |
| 460 | |
| 461 | case SSH_CHANNEL_OPEN: |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 462 | /* |
| 463 | * This is an open two-way communication channel. It |
| 464 | * is not of interest to us at this point what kind |
| 465 | * of data is being transmitted. |
| 466 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 467 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 468 | /* |
| 469 | * Read available incoming data and append it to |
| 470 | * buffer; shutdown socket, if read or write failes |
| 471 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 472 | if (FD_ISSET(ch->sock, readset)) { |
| 473 | len = read(ch->sock, buf, sizeof(buf)); |
| 474 | if (len <= 0) { |
| 475 | if (compat13) { |
| 476 | buffer_consume(&ch->output, buffer_len(&ch->output)); |
| 477 | ch->type = SSH_CHANNEL_INPUT_DRAINING; |
| 478 | debug("Channel %d status set to input draining.", i); |
| 479 | } else { |
| 480 | chan_read_failed(ch); |
| 481 | } |
| 482 | break; |
| 483 | } |
| 484 | buffer_append(&ch->input, buf, len); |
| 485 | } |
| 486 | /* Send buffered output data to the socket. */ |
| 487 | if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) { |
| 488 | len = write(ch->sock, buffer_ptr(&ch->output), |
| 489 | buffer_len(&ch->output)); |
| 490 | if (len <= 0) { |
| 491 | if (compat13) { |
| 492 | buffer_consume(&ch->output, buffer_len(&ch->output)); |
| 493 | debug("Channel %d status set to input draining.", i); |
| 494 | ch->type = SSH_CHANNEL_INPUT_DRAINING; |
| 495 | } else { |
| 496 | chan_write_failed(ch); |
| 497 | } |
| 498 | break; |
| 499 | } |
| 500 | buffer_consume(&ch->output, len); |
| 501 | } |
| 502 | break; |
| 503 | |
| 504 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 505 | if (!compat13) |
| 506 | fatal("cannot happen: OUT_DRAIN"); |
| 507 | /* Send buffered output data to the socket. */ |
| 508 | if (FD_ISSET(ch->sock, writeset) && buffer_len(&ch->output) > 0) { |
| 509 | len = write(ch->sock, buffer_ptr(&ch->output), |
| 510 | buffer_len(&ch->output)); |
| 511 | if (len <= 0) |
| 512 | buffer_consume(&ch->output, buffer_len(&ch->output)); |
| 513 | else |
| 514 | buffer_consume(&ch->output, len); |
| 515 | } |
| 516 | break; |
| 517 | |
| 518 | case SSH_CHANNEL_X11_OPEN: |
| 519 | case SSH_CHANNEL_FREE: |
| 520 | default: |
| 521 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 522 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 523 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* If there is data to send to the connection, send some of it now. */ |
| 527 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 528 | void |
| 529 | channel_output_poll() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 530 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 531 | int len, i; |
| 532 | Channel *ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 533 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 534 | for (i = 0; i < channels_alloc; i++) { |
| 535 | ch = &channels[i]; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 536 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 537 | /* We are only interested in channels that can have buffered incoming data. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 538 | if (compat13) { |
| 539 | if (ch->type != SSH_CHANNEL_OPEN && |
| 540 | ch->type != SSH_CHANNEL_INPUT_DRAINING) |
| 541 | continue; |
| 542 | } else { |
| 543 | if (ch->type != SSH_CHANNEL_OPEN) |
| 544 | continue; |
| 545 | if (ch->istate != CHAN_INPUT_OPEN && |
| 546 | ch->istate != CHAN_INPUT_WAIT_DRAIN) |
| 547 | continue; |
| 548 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 549 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | /* Get the amount of buffered data for this channel. */ |
| 551 | len = buffer_len(&ch->input); |
| 552 | if (len > 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 553 | /* Send some data for the other side over the secure connection. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 554 | if (packet_is_interactive()) { |
| 555 | if (len > 1024) |
| 556 | len = 512; |
| 557 | } else { |
| 558 | /* Keep the packets at reasonable size. */ |
Damien Miller | 68e45de | 1999-12-27 23:54:55 +1100 | [diff] [blame] | 559 | if (len > packet_get_maxsize()/2) |
| 560 | len = packet_get_maxsize()/2; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 561 | } |
| 562 | packet_start(SSH_MSG_CHANNEL_DATA); |
| 563 | packet_put_int(ch->remote_id); |
| 564 | packet_put_string(buffer_ptr(&ch->input), len); |
| 565 | packet_send(); |
| 566 | buffer_consume(&ch->input, len); |
| 567 | } else if (ch->istate == CHAN_INPUT_WAIT_DRAIN) { |
| 568 | if (compat13) |
| 569 | fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 570 | /* |
| 571 | * input-buffer is empty and read-socket shutdown: |
| 572 | * tell peer, that we will not send more data: send IEOF |
| 573 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 574 | chan_ibuf_empty(ch); |
| 575 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 576 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 577 | } |
| 578 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 579 | /* |
| 580 | * This is called when a packet of type CHANNEL_DATA has just been received. |
| 581 | * The message type has already been consumed, but channel number and data is |
| 582 | * still there. |
| 583 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 584 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 585 | void |
| 586 | channel_input_data(int payload_len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 587 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 588 | int id; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 589 | char *data; |
| 590 | unsigned int data_len; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 591 | Channel *ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 592 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 593 | /* Get the channel number and verify it. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 594 | id = packet_get_int(); |
| 595 | if (id < 0 || id >= channels_alloc) |
| 596 | packet_disconnect("Received data for nonexistent channel %d.", id); |
| 597 | ch = &channels[id]; |
| 598 | |
| 599 | if (ch->type == SSH_CHANNEL_FREE) |
| 600 | packet_disconnect("Received data for free channel %d.", ch->self); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 601 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 602 | /* Ignore any data for non-open channels (might happen on close) */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 603 | if (ch->type != SSH_CHANNEL_OPEN && |
| 604 | ch->type != SSH_CHANNEL_X11_OPEN) |
| 605 | return; |
| 606 | |
| 607 | /* same for protocol 1.5 if output end is no longer open */ |
| 608 | if (!compat13 && ch->ostate != CHAN_OUTPUT_OPEN) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 609 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 610 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 611 | /* Get the data. */ |
| 612 | data = packet_get_string(&data_len); |
| 613 | packet_integrity_check(payload_len, 4 + 4 + data_len, SSH_MSG_CHANNEL_DATA); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 614 | buffer_append(&ch->output, data, data_len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 615 | xfree(data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 616 | } |
| 617 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 618 | /* |
| 619 | * Returns true if no channel has too much buffered data, and false if one or |
| 620 | * more channel is overfull. |
| 621 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 622 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 623 | int |
| 624 | channel_not_very_much_buffered_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 625 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 626 | unsigned int i; |
| 627 | Channel *ch; |
| 628 | |
| 629 | for (i = 0; i < channels_alloc; i++) { |
| 630 | ch = &channels[i]; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 631 | if (ch->type == SSH_CHANNEL_OPEN) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 632 | if (buffer_len(&ch->input) > packet_get_maxsize()) |
| 633 | return 0; |
| 634 | if (buffer_len(&ch->output) > packet_get_maxsize()) |
| 635 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 636 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 637 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 638 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* This is called after receiving CHANNEL_CLOSE/IEOF. */ |
| 642 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 643 | void |
| 644 | channel_input_close() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 645 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 646 | int channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 647 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 648 | /* Get the channel number and verify it. */ |
| 649 | channel = packet_get_int(); |
| 650 | if (channel < 0 || channel >= channels_alloc || |
| 651 | channels[channel].type == SSH_CHANNEL_FREE) |
| 652 | packet_disconnect("Received data for nonexistent channel %d.", channel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 653 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 654 | if (!compat13) { |
| 655 | /* proto version 1.5 overloads CLOSE with IEOF */ |
| 656 | chan_rcvd_ieof(&channels[channel]); |
| 657 | return; |
| 658 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * Send a confirmation that we have closed the channel and no more |
| 662 | * data is coming for it. |
| 663 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 664 | packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION); |
| 665 | packet_put_int(channels[channel].remote_id); |
| 666 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 667 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 668 | /* |
| 669 | * If the channel is in closed state, we have sent a close request, |
| 670 | * and the other side will eventually respond with a confirmation. |
| 671 | * Thus, we cannot free the channel here, because then there would be |
| 672 | * no-one to receive the confirmation. The channel gets freed when |
| 673 | * the confirmation arrives. |
| 674 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 675 | if (channels[channel].type != SSH_CHANNEL_CLOSED) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 676 | /* |
| 677 | * Not a closed channel - mark it as draining, which will |
| 678 | * cause it to be freed later. |
| 679 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 680 | buffer_consume(&channels[channel].input, |
| 681 | buffer_len(&channels[channel].input)); |
| 682 | channels[channel].type = SSH_CHANNEL_OUTPUT_DRAINING; |
| 683 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* This is called after receiving CHANNEL_CLOSE_CONFIRMATION/OCLOSE. */ |
| 687 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 688 | void |
| 689 | channel_input_close_confirmation() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 690 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 691 | int channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 692 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 693 | /* Get the channel number and verify it. */ |
| 694 | channel = packet_get_int(); |
| 695 | if (channel < 0 || channel >= channels_alloc) |
| 696 | packet_disconnect("Received close confirmation for out-of-range channel %d.", |
| 697 | channel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 698 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 699 | if (!compat13) { |
| 700 | /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */ |
| 701 | chan_rcvd_oclose(&channels[channel]); |
| 702 | return; |
| 703 | } |
| 704 | if (channels[channel].type != SSH_CHANNEL_CLOSED) |
| 705 | packet_disconnect("Received close confirmation for non-closed channel %d (type %d).", |
| 706 | channel, channels[channel].type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 707 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 708 | /* Free the channel. */ |
| 709 | channel_free(channel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* This is called after receiving CHANNEL_OPEN_CONFIRMATION. */ |
| 713 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 714 | void |
| 715 | channel_input_open_confirmation() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 716 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 717 | int channel, remote_channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 718 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 719 | /* Get the channel number and verify it. */ |
| 720 | channel = packet_get_int(); |
| 721 | if (channel < 0 || channel >= channels_alloc || |
| 722 | channels[channel].type != SSH_CHANNEL_OPENING) |
| 723 | packet_disconnect("Received open confirmation for non-opening channel %d.", |
| 724 | channel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 725 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 726 | /* Get remote side's id for this channel. */ |
| 727 | remote_channel = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 728 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 729 | /* Record the remote channel number and mark that the channel is now open. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 730 | channels[channel].remote_id = remote_channel; |
| 731 | channels[channel].type = SSH_CHANNEL_OPEN; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* This is called after receiving CHANNEL_OPEN_FAILURE from the other side. */ |
| 735 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 736 | void |
| 737 | channel_input_open_failure() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 738 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 739 | int channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 740 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 741 | /* Get the channel number and verify it. */ |
| 742 | channel = packet_get_int(); |
| 743 | if (channel < 0 || channel >= channels_alloc || |
| 744 | channels[channel].type != SSH_CHANNEL_OPENING) |
| 745 | packet_disconnect("Received open failure for non-opening channel %d.", |
| 746 | channel); |
| 747 | |
| 748 | /* Free the channel. This will also close the socket. */ |
| 749 | channel_free(channel); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 750 | } |
| 751 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 752 | /* |
| 753 | * Stops listening for channels, and removes any unix domain sockets that we |
| 754 | * might have. |
| 755 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 756 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 757 | void |
| 758 | channel_stop_listening() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 759 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 760 | int i; |
| 761 | for (i = 0; i < channels_alloc; i++) { |
| 762 | switch (channels[i].type) { |
| 763 | case SSH_CHANNEL_AUTH_SOCKET: |
| 764 | close(channels[i].sock); |
| 765 | remove(channels[i].path); |
| 766 | channel_free(i); |
| 767 | break; |
| 768 | case SSH_CHANNEL_PORT_LISTENER: |
| 769 | case SSH_CHANNEL_X11_LISTENER: |
| 770 | close(channels[i].sock); |
| 771 | channel_free(i); |
| 772 | break; |
| 773 | default: |
| 774 | break; |
| 775 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 776 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 777 | } |
| 778 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 779 | /* |
| 780 | * Closes the sockets of all channels. This is used to close extra file |
| 781 | * descriptors after a fork. |
| 782 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 783 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 784 | void |
| 785 | channel_close_all() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 786 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 787 | int i; |
| 788 | for (i = 0; i < channels_alloc; i++) { |
| 789 | if (channels[i].type != SSH_CHANNEL_FREE) |
| 790 | close(channels[i].sock); |
| 791 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Returns the maximum file descriptor number used by the channels. */ |
| 795 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 796 | int |
| 797 | channel_max_fd() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 798 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 799 | return channel_max_fd_value; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* Returns true if any channel is still open. */ |
| 803 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 804 | int |
| 805 | channel_still_open() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 806 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 807 | unsigned int i; |
| 808 | for (i = 0; i < channels_alloc; i++) |
| 809 | switch (channels[i].type) { |
| 810 | case SSH_CHANNEL_FREE: |
| 811 | case SSH_CHANNEL_X11_LISTENER: |
| 812 | case SSH_CHANNEL_PORT_LISTENER: |
| 813 | case SSH_CHANNEL_CLOSED: |
| 814 | case SSH_CHANNEL_AUTH_SOCKET: |
| 815 | continue; |
| 816 | case SSH_CHANNEL_OPENING: |
| 817 | case SSH_CHANNEL_OPEN: |
| 818 | case SSH_CHANNEL_X11_OPEN: |
| 819 | return 1; |
| 820 | case SSH_CHANNEL_INPUT_DRAINING: |
| 821 | case SSH_CHANNEL_OUTPUT_DRAINING: |
| 822 | if (!compat13) |
| 823 | fatal("cannot happen: OUT_DRAIN"); |
| 824 | return 1; |
| 825 | default: |
| 826 | fatal("channel_still_open: bad channel type %d", channels[i].type); |
| 827 | /* NOTREACHED */ |
| 828 | } |
| 829 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 830 | } |
| 831 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 832 | /* |
| 833 | * Returns a message describing the currently open forwarded connections, |
| 834 | * suitable for sending to the client. The message contains crlf pairs for |
| 835 | * newlines. |
| 836 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 837 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 838 | char * |
| 839 | channel_open_message() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 840 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 841 | Buffer buffer; |
| 842 | int i; |
| 843 | char buf[512], *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 844 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 845 | buffer_init(&buffer); |
| 846 | snprintf(buf, sizeof buf, "The following connections are open:\r\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 847 | buffer_append(&buffer, buf, strlen(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 848 | for (i = 0; i < channels_alloc; i++) { |
| 849 | Channel *c = &channels[i]; |
| 850 | switch (c->type) { |
| 851 | case SSH_CHANNEL_FREE: |
| 852 | case SSH_CHANNEL_X11_LISTENER: |
| 853 | case SSH_CHANNEL_PORT_LISTENER: |
| 854 | case SSH_CHANNEL_CLOSED: |
| 855 | case SSH_CHANNEL_AUTH_SOCKET: |
| 856 | continue; |
| 857 | case SSH_CHANNEL_OPENING: |
| 858 | case SSH_CHANNEL_OPEN: |
| 859 | case SSH_CHANNEL_X11_OPEN: |
| 860 | case SSH_CHANNEL_INPUT_DRAINING: |
| 861 | case SSH_CHANNEL_OUTPUT_DRAINING: |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 862 | snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d)\r\n", |
| 863 | c->self, c->remote_name, |
| 864 | c->type, c->remote_id, |
| 865 | c->istate, buffer_len(&c->input), |
| 866 | c->ostate, buffer_len(&c->output)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 867 | buffer_append(&buffer, buf, strlen(buf)); |
| 868 | continue; |
| 869 | default: |
| 870 | fatal("channel_still_open: bad channel type %d", c->type); |
| 871 | /* NOTREACHED */ |
| 872 | } |
| 873 | } |
| 874 | buffer_append(&buffer, "\0", 1); |
| 875 | cp = xstrdup(buffer_ptr(&buffer)); |
| 876 | buffer_free(&buffer); |
| 877 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 878 | } |
| 879 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 880 | /* |
| 881 | * Initiate forwarding of connections to local port "port" through the secure |
| 882 | * channel to host:port from remote side. |
| 883 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 884 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 885 | void |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 886 | channel_request_local_forwarding(u_short port, const char *host, |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 887 | u_short host_port, int gateway_ports) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 888 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 889 | int success, ch, sock, on = 1; |
| 890 | struct addrinfo hints, *ai, *aitop; |
| 891 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 892 | struct linger linger; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 893 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 894 | if (strlen(host) > sizeof(channels[0].path) - 1) |
| 895 | packet_disconnect("Forward host name too long."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 896 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 897 | /* |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 898 | * getaddrinfo returns a loopback address if the hostname is |
| 899 | * set to NULL and hints.ai_flags is not AI_PASSIVE |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 900 | */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 901 | memset(&hints, 0, sizeof(hints)); |
| 902 | hints.ai_family = IPv4or6; |
| 903 | hints.ai_flags = gateway_ports ? AI_PASSIVE : 0; |
| 904 | hints.ai_socktype = SOCK_STREAM; |
| 905 | snprintf(strport, sizeof strport, "%d", port); |
| 906 | if (getaddrinfo(NULL, strport, &hints, &aitop) != 0) |
| 907 | packet_disconnect("getaddrinfo: fatal error"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 908 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 909 | success = 0; |
| 910 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 911 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 912 | continue; |
| 913 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 914 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 915 | error("channel_request_local_forwarding: getnameinfo failed"); |
| 916 | continue; |
| 917 | } |
| 918 | /* Create a port to listen for the host. */ |
| 919 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 920 | if (sock < 0) { |
| 921 | /* this is no error since kernel may not support ipv6 */ |
| 922 | verbose("socket: %.100s", strerror(errno)); |
| 923 | continue; |
| 924 | } |
| 925 | /* |
| 926 | * Set socket options. We would like the socket to disappear |
| 927 | * as soon as it has been closed for whatever reason. |
| 928 | */ |
| 929 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); |
| 930 | linger.l_onoff = 1; |
| 931 | linger.l_linger = 5; |
| 932 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger)); |
| 933 | debug("Local forwarding listening on %s port %s.", ntop, strport); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 934 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 935 | /* Bind the socket to the address. */ |
| 936 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 937 | /* address can be in use ipv6 address is already bound */ |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 938 | if (!ai->ai_next) |
| 939 | error("bind: %.100s", strerror(errno)); |
| 940 | else |
| 941 | verbose("bind: %.100s", strerror(errno)); |
| 942 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 943 | close(sock); |
| 944 | continue; |
| 945 | } |
| 946 | /* Start listening for connections on the socket. */ |
| 947 | if (listen(sock, 5) < 0) { |
| 948 | error("listen: %.100s", strerror(errno)); |
| 949 | close(sock); |
| 950 | continue; |
| 951 | } |
| 952 | /* Allocate a channel number for the socket. */ |
| 953 | ch = channel_allocate(SSH_CHANNEL_PORT_LISTENER, sock, |
| 954 | xstrdup("port listener")); |
| 955 | strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); |
| 956 | channels[ch].host_port = host_port; |
| 957 | channels[ch].listening_port = port; |
| 958 | success = 1; |
| 959 | } |
| 960 | if (success == 0) |
| 961 | packet_disconnect("cannot listen port: %d", port); |
| 962 | freeaddrinfo(aitop); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 963 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 964 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 965 | /* |
| 966 | * Initiate forwarding of connections to port "port" on remote host through |
| 967 | * the secure channel to host:port from local side. |
| 968 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 969 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 970 | void |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 971 | channel_request_remote_forwarding(u_short port, const char *host, |
| 972 | u_short remote_port) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 973 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 974 | int payload_len; |
| 975 | /* Record locally that connection to this host/port is permitted. */ |
| 976 | if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 977 | fatal("channel_request_remote_forwarding: too many forwards"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 978 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 979 | permitted_opens[num_permitted_opens].host = xstrdup(host); |
| 980 | permitted_opens[num_permitted_opens].port = remote_port; |
| 981 | num_permitted_opens++; |
| 982 | |
| 983 | /* Send the forward request to the remote side. */ |
| 984 | packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
| 985 | packet_put_int(port); |
| 986 | packet_put_string(host, strlen(host)); |
| 987 | packet_put_int(remote_port); |
| 988 | packet_send(); |
| 989 | packet_write_wait(); |
| 990 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 991 | /* |
| 992 | * Wait for response from the remote side. It will send a disconnect |
| 993 | * message on failure, and we will never see it here. |
| 994 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 995 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 996 | } |
| 997 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 998 | /* |
| 999 | * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates |
| 1000 | * listening for the port, and sends back a success reply (or disconnect |
| 1001 | * message if there was an error). This never returns if there was an error. |
| 1002 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1003 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1004 | void |
| 1005 | channel_input_port_forward_request(int is_root) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1006 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1007 | u_short port, host_port; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1008 | char *hostname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1009 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1010 | /* Get arguments from the packet. */ |
| 1011 | port = packet_get_int(); |
| 1012 | hostname = packet_get_string(NULL); |
| 1013 | host_port = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1014 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1015 | /* |
| 1016 | * Check that an unprivileged user is not trying to forward a |
| 1017 | * privileged port. |
| 1018 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1019 | if (port < IPPORT_RESERVED && !is_root) |
| 1020 | packet_disconnect("Requested forwarding of port %d but user is not root.", |
| 1021 | port); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1022 | /* |
| 1023 | * Initiate forwarding, |
| 1024 | * bind port to localhost only (gateway ports == 0). |
| 1025 | */ |
| 1026 | channel_request_local_forwarding(port, hostname, host_port, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1027 | |
| 1028 | /* Free the argument string. */ |
| 1029 | xfree(hostname); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1030 | } |
| 1031 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1032 | /* |
| 1033 | * This is called after receiving PORT_OPEN message. This attempts to |
| 1034 | * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION |
| 1035 | * or CHANNEL_OPEN_FAILURE. |
| 1036 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1037 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1038 | void |
| 1039 | channel_input_port_open(int payload_len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1040 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1041 | int remote_channel, sock = 0, newch, i; |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1042 | u_short host_port; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1043 | char *host, *originator_string; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1044 | int host_len, originator_len; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1045 | struct addrinfo hints, *ai, *aitop; |
| 1046 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
| 1047 | int gaierr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1048 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1049 | /* Get remote channel number. */ |
| 1050 | remote_channel = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1051 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1052 | /* Get host name to connect to. */ |
| 1053 | host = packet_get_string(&host_len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1054 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1055 | /* Get port to connect to. */ |
| 1056 | host_port = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1057 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1058 | /* Get remote originator name. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1059 | if (have_hostname_in_open) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1060 | originator_string = packet_get_string(&originator_len); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1061 | originator_len += 4; /* size of packet_int */ |
| 1062 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1063 | originator_string = xstrdup("unknown (remote did not supply name)"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1064 | originator_len = 0; /* no originator supplied */ |
| 1065 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1066 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1067 | packet_integrity_check(payload_len, |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1068 | 4 + 4 + host_len + 4 + originator_len, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1069 | SSH_MSG_PORT_OPEN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1070 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1071 | /* Check if opening that port is permitted. */ |
| 1072 | if (!all_opens_permitted) { |
| 1073 | /* Go trough all permitted ports. */ |
| 1074 | for (i = 0; i < num_permitted_opens; i++) |
| 1075 | if (permitted_opens[i].port == host_port && |
| 1076 | strcmp(permitted_opens[i].host, host) == 0) |
| 1077 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1078 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1079 | /* Check if we found the requested port among those permitted. */ |
| 1080 | if (i >= num_permitted_opens) { |
| 1081 | /* The port is not permitted. */ |
| 1082 | log("Received request to connect to %.100s:%d, but the request was denied.", |
| 1083 | host, host_port); |
| 1084 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1085 | packet_put_int(remote_channel); |
| 1086 | packet_send(); |
| 1087 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1088 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1089 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1090 | memset(&hints, 0, sizeof(hints)); |
| 1091 | hints.ai_family = IPv4or6; |
| 1092 | hints.ai_socktype = SOCK_STREAM; |
| 1093 | snprintf(strport, sizeof strport, "%d", host_port); |
| 1094 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) { |
| 1095 | error("%.100s: unknown host (%s)", host, gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1096 | goto fail; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1097 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1098 | |
| 1099 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1100 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1101 | continue; |
| 1102 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
| 1103 | strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
| 1104 | error("channel_input_port_open: getnameinfo failed"); |
| 1105 | continue; |
| 1106 | } |
| 1107 | /* Create the socket. */ |
| 1108 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1109 | if (sock < 0) { |
| 1110 | error("socket: %.100s", strerror(errno)); |
| 1111 | continue; |
| 1112 | } |
| 1113 | /* Connect to the host/port. */ |
| 1114 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1115 | error("connect %.100s port %s: %.100s", ntop, strport, |
| 1116 | strerror(errno)); |
| 1117 | close(sock); |
| 1118 | continue; /* fail -- try next */ |
| 1119 | } |
| 1120 | break; /* success */ |
| 1121 | |
| 1122 | } |
| 1123 | freeaddrinfo(aitop); |
| 1124 | |
| 1125 | if (!ai) { |
| 1126 | error("connect %.100s port %d: failed.", host, host_port); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1127 | goto fail; |
| 1128 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1129 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1130 | /* Successful connection. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1131 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1132 | /* Allocate a channel for this connection. */ |
| 1133 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string); |
| 1134 | channels[newch].remote_id = remote_channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1135 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1136 | /* Send a confirmation to the remote host. */ |
| 1137 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1138 | packet_put_int(remote_channel); |
| 1139 | packet_put_int(newch); |
| 1140 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1141 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1142 | /* Free the argument string. */ |
| 1143 | xfree(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1144 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1145 | return; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1146 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1147 | fail: |
| 1148 | /* Free the argument string. */ |
| 1149 | xfree(host); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1150 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1151 | /* Send refusal to the remote host. */ |
| 1152 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1153 | packet_put_int(remote_channel); |
| 1154 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1155 | } |
| 1156 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1157 | /* |
| 1158 | * Creates an internet domain socket for listening for X11 connections. |
| 1159 | * Returns a suitable value for the DISPLAY variable, or NULL if an error |
| 1160 | * occurs. |
| 1161 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1162 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1163 | #define NUM_SOCKS 10 |
| 1164 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1165 | char * |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1166 | x11_create_display_inet(int screen_number, int x11_display_offset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1167 | { |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 1168 | int display_number, sock; |
| 1169 | u_short port; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1170 | struct addrinfo hints, *ai, *aitop; |
| 1171 | char strport[NI_MAXSERV]; |
| 1172 | int gaierr, n, num_socks = 0, socks[NUM_SOCKS]; |
| 1173 | char display[512]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1174 | char hostname[MAXHOSTNAMELEN]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1175 | |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 1176 | for (display_number = x11_display_offset; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1177 | display_number < MAX_DISPLAYS; |
| 1178 | display_number++) { |
| 1179 | port = 6000 + display_number; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1180 | memset(&hints, 0, sizeof(hints)); |
| 1181 | hints.ai_family = IPv4or6; |
| 1182 | hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */ |
| 1183 | hints.ai_socktype = SOCK_STREAM; |
| 1184 | snprintf(strport, sizeof strport, "%d", port); |
| 1185 | if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) { |
| 1186 | error("getaddrinfo: %.100s", gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1187 | return NULL; |
| 1188 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1189 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1190 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) |
| 1191 | continue; |
| 1192 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1193 | if (sock < 0) { |
Damien Miller | e219273 | 2000-01-17 13:22:55 +1100 | [diff] [blame] | 1194 | if (errno != EINVAL) { |
| 1195 | error("socket: %.100s", strerror(errno)); |
| 1196 | return NULL; |
| 1197 | } else { |
| 1198 | debug("Socket family %d not supported [X11 disp create]", ai->ai_family); |
| 1199 | continue; |
| 1200 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1201 | } |
| 1202 | if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1203 | debug("bind port %d: %.100s", port, strerror(errno)); |
| 1204 | shutdown(sock, SHUT_RDWR); |
| 1205 | close(sock); |
Damien Miller | 3c7eeb2 | 2000-03-03 22:35:33 +1100 | [diff] [blame] | 1206 | |
| 1207 | if (ai->ai_next) |
| 1208 | continue; |
| 1209 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1210 | for (n = 0; n < num_socks; n++) { |
| 1211 | shutdown(socks[n], SHUT_RDWR); |
| 1212 | close(socks[n]); |
| 1213 | } |
| 1214 | num_socks = 0; |
| 1215 | break; |
| 1216 | } |
| 1217 | socks[num_socks++] = sock; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1218 | #ifndef DONT_TRY_OTHER_AF |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1219 | if (num_socks == NUM_SOCKS) |
| 1220 | break; |
Damien Miller | 7bcb089 | 2000-03-11 20:45:40 +1100 | [diff] [blame] | 1221 | #else |
| 1222 | break; |
| 1223 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1224 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1225 | if (num_socks > 0) |
| 1226 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1227 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1228 | if (display_number >= MAX_DISPLAYS) { |
| 1229 | error("Failed to allocate internet-domain X11 display socket."); |
| 1230 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1231 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1232 | /* Start listening for connections on the socket. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1233 | for (n = 0; n < num_socks; n++) { |
| 1234 | sock = socks[n]; |
| 1235 | if (listen(sock, 5) < 0) { |
| 1236 | error("listen: %.100s", strerror(errno)); |
| 1237 | shutdown(sock, SHUT_RDWR); |
| 1238 | close(sock); |
| 1239 | return NULL; |
| 1240 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1241 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1242 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1243 | /* Set up a suitable value for the DISPLAY variable. */ |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1244 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1245 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 1246 | fatal("gethostname: %.100s", strerror(errno)); |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1247 | |
| 1248 | #ifdef IPADDR_IN_DISPLAY |
| 1249 | /* |
| 1250 | * HPUX detects the local hostname in the DISPLAY variable and tries |
| 1251 | * to set up a shared memory connection to the server, which it |
| 1252 | * incorrectly supposes to be local. |
| 1253 | * |
| 1254 | * The workaround - as used in later $$H and other programs - is |
| 1255 | * is to set display to the host's IP address. |
| 1256 | */ |
| 1257 | { |
| 1258 | struct hostent *he; |
| 1259 | struct in_addr my_addr; |
| 1260 | |
| 1261 | he = gethostbyname(hostname); |
| 1262 | if (he == NULL) { |
| 1263 | error("[X11-broken-fwd-hostname-workaround] Could not get " |
| 1264 | "IP address for hostname %s.", hostname); |
| 1265 | |
| 1266 | packet_send_debug("[X11-broken-fwd-hostname-workaround]" |
| 1267 | "Could not get IP address for hostname %s.", hostname); |
| 1268 | |
| 1269 | shutdown(sock, SHUT_RDWR); |
| 1270 | close(sock); |
| 1271 | |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | |
| 1275 | memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 1276 | |
| 1277 | /* Set DISPLAY to <ip address>:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1278 | snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr), |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1279 | display_number, screen_number); |
| 1280 | } |
| 1281 | #else /* IPADDR_IN_DISPLAY */ |
| 1282 | /* Just set DISPLAY to hostname:screen.display */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1283 | snprintf(display, sizeof display, "%.400s:%d.%d", hostname, |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 1284 | display_number, screen_number); |
| 1285 | #endif /* IPADDR_IN_DISPLAY */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1286 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1287 | /* Allocate a channel for each socket. */ |
| 1288 | for (n = 0; n < num_socks; n++) { |
| 1289 | sock = socks[n]; |
| 1290 | (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock, |
| 1291 | xstrdup("X11 inet listener")); |
| 1292 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1293 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1294 | /* Return a suitable value for the DISPLAY environment variable. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1295 | return xstrdup(display); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | #ifndef X_UNIX_PATH |
| 1299 | #define X_UNIX_PATH "/tmp/.X11-unix/X" |
| 1300 | #endif |
| 1301 | |
| 1302 | static |
| 1303 | int |
Damien Miller | 81b25cf | 1999-12-07 16:47:28 +1100 | [diff] [blame] | 1304 | connect_local_xsocket(unsigned int dnr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1305 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1306 | static const char *const x_sockets[] = { |
| 1307 | X_UNIX_PATH "%u", |
| 1308 | "/var/X/.X11-unix/X" "%u", |
| 1309 | "/usr/spool/sockets/X11/" "%u", |
| 1310 | NULL |
| 1311 | }; |
| 1312 | int sock; |
| 1313 | struct sockaddr_un addr; |
| 1314 | const char *const * path; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1315 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1316 | for (path = x_sockets; *path; ++path) { |
| 1317 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1318 | if (sock < 0) |
| 1319 | error("socket: %.100s", strerror(errno)); |
| 1320 | memset(&addr, 0, sizeof(addr)); |
| 1321 | addr.sun_family = AF_UNIX; |
| 1322 | snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr); |
| 1323 | if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0) |
| 1324 | return sock; |
| 1325 | close(sock); |
| 1326 | } |
| 1327 | error("connect %.100s: %.100s", addr.sun_path, strerror(errno)); |
| 1328 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1332 | /* |
| 1333 | * This is called when SSH_SMSG_X11_OPEN is received. The packet contains |
| 1334 | * the remote channel number. We should do whatever we want, and respond |
| 1335 | * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. |
| 1336 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1337 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1338 | void |
| 1339 | x11_input_open(int payload_len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1340 | { |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1341 | int remote_channel, display_number, sock = 0, newch; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1342 | const char *display; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1343 | char buf[1024], *cp, *remote_host; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1344 | int remote_len; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1345 | struct addrinfo hints, *ai, *aitop; |
| 1346 | char strport[NI_MAXSERV]; |
| 1347 | int gaierr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1348 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1349 | /* Get remote channel number. */ |
| 1350 | remote_channel = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1351 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1352 | /* Get remote originator name. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1353 | if (have_hostname_in_open) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1354 | remote_host = packet_get_string(&remote_len); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1355 | remote_len += 4; |
| 1356 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1357 | remote_host = xstrdup("unknown (remote did not supply name)"); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1358 | remote_len = 0; |
| 1359 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1360 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1361 | debug("Received X11 open request."); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1362 | packet_integrity_check(payload_len, 4 + remote_len, SSH_SMSG_X11_OPEN); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1363 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1364 | /* Try to open a socket for the local X server. */ |
| 1365 | display = getenv("DISPLAY"); |
| 1366 | if (!display) { |
| 1367 | error("DISPLAY not set."); |
| 1368 | goto fail; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1369 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1370 | /* |
| 1371 | * Now we decode the value of the DISPLAY variable and make a |
| 1372 | * connection to the real X server. |
| 1373 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1374 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1375 | /* |
| 1376 | * Check if it is a unix domain socket. Unix domain displays are in |
| 1377 | * one of the following formats: unix:d[.s], :d[.s], ::d[.s] |
| 1378 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1379 | if (strncmp(display, "unix:", 5) == 0 || |
| 1380 | display[0] == ':') { |
| 1381 | /* Connect to the unix domain socket. */ |
| 1382 | if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) { |
| 1383 | error("Could not parse display number from DISPLAY: %.100s", |
| 1384 | display); |
| 1385 | goto fail; |
| 1386 | } |
| 1387 | /* Create a socket. */ |
| 1388 | sock = connect_local_xsocket(display_number); |
| 1389 | if (sock < 0) |
| 1390 | goto fail; |
| 1391 | |
| 1392 | /* OK, we now have a connection to the display. */ |
| 1393 | goto success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1394 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1395 | /* |
| 1396 | * Connect to an inet socket. The DISPLAY value is supposedly |
| 1397 | * hostname:d[.s], where hostname may also be numeric IP address. |
| 1398 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1399 | strncpy(buf, display, sizeof(buf)); |
| 1400 | buf[sizeof(buf) - 1] = 0; |
| 1401 | cp = strchr(buf, ':'); |
| 1402 | if (!cp) { |
| 1403 | error("Could not find ':' in DISPLAY: %.100s", display); |
| 1404 | goto fail; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1405 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1406 | *cp = 0; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1407 | /* 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] | 1408 | if (sscanf(cp + 1, "%d", &display_number) != 1) { |
| 1409 | error("Could not parse display number from DISPLAY: %.100s", |
| 1410 | display); |
| 1411 | goto fail; |
| 1412 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1413 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1414 | /* Look up the host address */ |
| 1415 | memset(&hints, 0, sizeof(hints)); |
| 1416 | hints.ai_family = IPv4or6; |
| 1417 | hints.ai_socktype = SOCK_STREAM; |
| 1418 | snprintf(strport, sizeof strport, "%d", 6000 + display_number); |
| 1419 | if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) { |
| 1420 | error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1421 | goto fail; |
| 1422 | } |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1423 | for (ai = aitop; ai; ai = ai->ai_next) { |
| 1424 | /* Create a socket. */ |
| 1425 | sock = socket(ai->ai_family, SOCK_STREAM, 0); |
| 1426 | if (sock < 0) { |
| 1427 | debug("socket: %.100s", strerror(errno)); |
| 1428 | continue; |
| 1429 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1430 | /* Connect it to the display. */ |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1431 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 1432 | debug("connect %.100s port %d: %.100s", buf, 6000 + display_number, |
| 1433 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1434 | close(sock); |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 1435 | continue; |
| 1436 | } |
| 1437 | /* Success */ |
| 1438 | break; |
| 1439 | |
| 1440 | } /* (ai = aitop, ai; ai = ai->ai_next) */ |
| 1441 | freeaddrinfo(aitop); |
| 1442 | if (!ai) { |
| 1443 | error("connect %.100s port %d: %.100s", buf, 6000 + display_number, |
| 1444 | strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1445 | goto fail; |
| 1446 | } |
| 1447 | success: |
| 1448 | /* We have successfully obtained a connection to the real X display. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1449 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1450 | /* Allocate a channel for this connection. */ |
| 1451 | if (x11_saved_proto == NULL) |
| 1452 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host); |
| 1453 | else |
| 1454 | newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host); |
| 1455 | channels[newch].remote_id = remote_channel; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1456 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1457 | /* Send a confirmation to the remote host. */ |
| 1458 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1459 | packet_put_int(remote_channel); |
| 1460 | packet_put_int(newch); |
| 1461 | packet_send(); |
| 1462 | |
| 1463 | return; |
| 1464 | |
| 1465 | fail: |
| 1466 | /* Send refusal to the remote host. */ |
| 1467 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1468 | packet_put_int(remote_channel); |
| 1469 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1470 | } |
| 1471 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1472 | /* |
| 1473 | * Requests forwarding of X11 connections, generates fake authentication |
| 1474 | * data, and enables authentication spoofing. |
| 1475 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1476 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1477 | void |
| 1478 | x11_request_forwarding_with_spoofing(const char *proto, const char *data) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1479 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1480 | unsigned int data_len = (unsigned int) strlen(data) / 2; |
| 1481 | unsigned int i, value; |
| 1482 | char *new_data; |
| 1483 | int screen_number; |
| 1484 | const char *cp; |
| 1485 | u_int32_t rand = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1486 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1487 | cp = getenv("DISPLAY"); |
| 1488 | if (cp) |
| 1489 | cp = strchr(cp, ':'); |
| 1490 | if (cp) |
| 1491 | cp = strchr(cp, '.'); |
| 1492 | if (cp) |
| 1493 | screen_number = atoi(cp + 1); |
| 1494 | else |
| 1495 | screen_number = 0; |
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 | /* Save protocol name. */ |
| 1498 | x11_saved_proto = xstrdup(proto); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1499 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1500 | /* |
| 1501 | * Extract real authentication data and generate fake data of the |
| 1502 | * same length. |
| 1503 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1504 | x11_saved_data = xmalloc(data_len); |
| 1505 | x11_fake_data = xmalloc(data_len); |
| 1506 | for (i = 0; i < data_len; i++) { |
| 1507 | if (sscanf(data + 2 * i, "%2x", &value) != 1) |
| 1508 | fatal("x11_request_forwarding: bad authentication data: %.100s", data); |
| 1509 | if (i % 4 == 0) |
| 1510 | rand = arc4random(); |
| 1511 | x11_saved_data[i] = value; |
| 1512 | x11_fake_data[i] = rand & 0xff; |
| 1513 | rand >>= 8; |
| 1514 | } |
| 1515 | x11_saved_data_len = data_len; |
| 1516 | x11_fake_data_len = data_len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1517 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1518 | /* Convert the fake data into hex. */ |
| 1519 | new_data = xmalloc(2 * data_len + 1); |
| 1520 | for (i = 0; i < data_len; i++) |
| 1521 | sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1522 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1523 | /* Send the request packet. */ |
| 1524 | packet_start(SSH_CMSG_X11_REQUEST_FORWARDING); |
| 1525 | packet_put_string(proto, strlen(proto)); |
| 1526 | packet_put_string(new_data, strlen(new_data)); |
| 1527 | packet_put_int(screen_number); |
| 1528 | packet_send(); |
| 1529 | packet_write_wait(); |
| 1530 | xfree(new_data); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* Sends a message to the server to request authentication fd forwarding. */ |
| 1534 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1535 | void |
| 1536 | auth_request_forwarding() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1537 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1538 | packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING); |
| 1539 | packet_send(); |
| 1540 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1541 | } |
| 1542 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1543 | /* |
| 1544 | * Returns the name of the forwarded authentication socket. Returns NULL if |
| 1545 | * there is no forwarded authentication socket. The returned value points to |
| 1546 | * a static buffer. |
| 1547 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1548 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1549 | char * |
| 1550 | auth_get_socket_name() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1551 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1552 | return channel_forwarded_auth_socket_name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /* removes the agent forwarding socket */ |
| 1556 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1557 | void |
| 1558 | cleanup_socket(void) |
| 1559 | { |
| 1560 | remove(channel_forwarded_auth_socket_name); |
| 1561 | rmdir(channel_forwarded_auth_socket_dir); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1562 | } |
| 1563 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1564 | /* |
| 1565 | * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server. |
| 1566 | * This starts forwarding authentication requests. |
| 1567 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1568 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1569 | void |
| 1570 | auth_input_request_forwarding(struct passwd * pw) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1571 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1572 | int sock, newch; |
| 1573 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1574 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1575 | if (auth_get_socket_name() != NULL) |
| 1576 | fatal("Protocol error: authentication forwarding requested twice."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1577 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1578 | /* Temporarily drop privileged uid for mkdir/bind. */ |
| 1579 | temporarily_use_uid(pw->pw_uid); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1580 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1581 | /* Allocate a buffer for the socket name, and format the name. */ |
| 1582 | channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME); |
| 1583 | channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME); |
| 1584 | strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1585 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1586 | /* Create private directory for socket */ |
| 1587 | if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) |
| 1588 | packet_disconnect("mkdtemp: %.100s", strerror(errno)); |
| 1589 | snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d", |
| 1590 | channel_forwarded_auth_socket_dir, (int) getpid()); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1591 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1592 | if (atexit(cleanup_socket) < 0) { |
| 1593 | int saved = errno; |
| 1594 | cleanup_socket(); |
| 1595 | packet_disconnect("socket: %.100s", strerror(saved)); |
| 1596 | } |
| 1597 | /* Create the socket. */ |
| 1598 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1599 | if (sock < 0) |
| 1600 | packet_disconnect("socket: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1601 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1602 | /* Bind it to the name. */ |
| 1603 | memset(&sunaddr, 0, sizeof(sunaddr)); |
| 1604 | sunaddr.sun_family = AF_UNIX; |
| 1605 | strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name, |
| 1606 | sizeof(sunaddr.sun_path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1607 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1608 | if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) |
| 1609 | packet_disconnect("bind: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1610 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1611 | /* Restore the privileged uid. */ |
| 1612 | restore_uid(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1613 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1614 | /* Start listening on the socket. */ |
| 1615 | if (listen(sock, 5) < 0) |
| 1616 | packet_disconnect("listen: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1617 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1618 | /* Allocate a channel for the authentication agent socket. */ |
| 1619 | newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock, |
| 1620 | xstrdup("auth socket")); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 1621 | strlcpy(channels[newch].path, channel_forwarded_auth_socket_name, |
| 1622 | sizeof(channels[newch].path)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* This is called to process an SSH_SMSG_AGENT_OPEN message. */ |
| 1626 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1627 | void |
| 1628 | auth_input_open_request() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1629 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1630 | int remch, sock, newch; |
| 1631 | char *dummyname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1632 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1633 | /* Read the remote channel number from the message. */ |
| 1634 | remch = packet_get_int(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1635 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1636 | /* |
| 1637 | * Get a connection to the local authentication agent (this may again |
| 1638 | * get forwarded). |
| 1639 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1640 | sock = ssh_get_authentication_socket(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1641 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1642 | /* |
| 1643 | * If we could not connect the agent, send an error message back to |
| 1644 | * the server. This should never happen unless the agent dies, |
| 1645 | * because authentication forwarding is only enabled if we have an |
| 1646 | * agent. |
| 1647 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1648 | if (sock < 0) { |
| 1649 | packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); |
| 1650 | packet_put_int(remch); |
| 1651 | packet_send(); |
| 1652 | return; |
| 1653 | } |
| 1654 | debug("Forwarding authentication connection."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1655 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1656 | /* |
| 1657 | * Dummy host name. This will be freed when the channel is freed; it |
| 1658 | * will still be valid in the packet_put_string below since the |
| 1659 | * channel cannot yet be freed at that point. |
| 1660 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1661 | dummyname = xstrdup("authentication agent connection"); |
| 1662 | |
| 1663 | newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname); |
| 1664 | channels[newch].remote_id = remch; |
| 1665 | |
| 1666 | /* Send a confirmation to the remote host. */ |
| 1667 | packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1668 | packet_put_int(remch); |
| 1669 | packet_put_int(newch); |
| 1670 | packet_send(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1671 | } |