]>
Commit | Line | Data |
---|---|---|
d3221cca | 1 | /* $OpenBSD: serverloop.c,v 1.138 2006/07/09 15:15:11 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
5260325f | 6 | * Server main loop for handling the interactive session. |
bcbf86ec | 7 | * |
8 | * As far as I am concerned, the code I have written for this software | |
9 | * can be used freely for any purpose. Any derived versions of this | |
10 | * software must be clearly marked as such, and if the derived work is | |
11 | * incompatible with the protocol description in the RFC file, it must be | |
12 | * called by a name other than "ssh" or "Secure Shell". | |
13 | * | |
e78a59f5 | 14 | * SSH2 support by Markus Friedl. |
a96070d4 | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
bcbf86ec | 16 | * |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
e78a59f5 | 36 | */ |
8efc0c15 | 37 | |
38 | #include "includes.h" | |
aa2eae64 | 39 | |
40 | #include <sys/types.h> | |
41 | #include <sys/wait.h> | |
9794d008 | 42 | #include <sys/socket.h> |
43 | ||
44 | #include <netinet/in.h> | |
1d3e026f | 45 | |
d3221cca | 46 | #include <fcntl.h> |
b1842393 | 47 | #include <pwd.h> |
ada68823 | 48 | #include <signal.h> |
1d3e026f | 49 | #include <termios.h> |
a22aff1f | 50 | |
8efc0c15 | 51 | #include "xmalloc.h" |
8efc0c15 | 52 | #include "packet.h" |
53 | #include "buffer.h" | |
42f11eb2 | 54 | #include "log.h" |
8efc0c15 | 55 | #include "servconf.h" |
00e842d8 | 56 | #include "canohost.h" |
1729c161 | 57 | #include "sshpty.h" |
a5efa1bb | 58 | #include "channels.h" |
7368a6c8 | 59 | #include "compat.h" |
42f11eb2 | 60 | #include "ssh1.h" |
e78a59f5 | 61 | #include "ssh2.h" |
59c97189 | 62 | #include "auth.h" |
e78a59f5 | 63 | #include "session.h" |
7368a6c8 | 64 | #include "dispatch.h" |
38c295d6 | 65 | #include "auth-options.h" |
42f11eb2 | 66 | #include "serverloop.h" |
67 | #include "misc.h" | |
7a37c112 | 68 | #include "kex.h" |
8efc0c15 | 69 | |
33de75a3 | 70 | extern ServerOptions options; |
71 | ||
7a37c112 | 72 | /* XXX */ |
73 | extern Kex *xxx_kex; | |
2362db19 | 74 | extern Authctxt *the_authctxt; |
0bbbf2a4 | 75 | extern int use_privsep; |
7a37c112 | 76 | |
8efc0c15 | 77 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
78 | static Buffer stdout_buffer; /* Buffer for stdout data. */ | |
79 | static Buffer stderr_buffer; /* Buffer for stderr data. */ | |
80 | static int fdin; /* Descriptor for stdin (for writing) */ | |
81 | static int fdout; /* Descriptor for stdout (for reading); | |
82 | May be same number as fdin. */ | |
83 | static int fderr; /* Descriptor for stderr. May be -1. */ | |
84 | static long stdin_bytes = 0; /* Number of bytes written to stdin. */ | |
85 | static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */ | |
86 | static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */ | |
87 | static long fdout_bytes = 0; /* Number of stdout bytes read from program. */ | |
88 | static int stdin_eof = 0; /* EOF message received from client. */ | |
89 | static int fdout_eof = 0; /* EOF encountered reading from fdout. */ | |
90 | static int fderr_eof = 0; /* EOF encountered readung from fderr. */ | |
e68225b2 | 91 | static int fdin_is_tty = 0; /* fdin points to a tty. */ |
8efc0c15 | 92 | static int connection_in; /* Connection to client (input). */ |
93 | static int connection_out; /* Connection to client (output). */ | |
cc94bd38 | 94 | static int connection_closed = 0; /* Connection to client closed. */ |
95 | static u_int buffer_high; /* "Soft" max buffer size. */ | |
72176c0e | 96 | static int client_alive_timeouts = 0; |
8efc0c15 | 97 | |
aa3378df | 98 | /* |
99 | * This SIGCHLD kludge is used to detect when the child exits. The server | |
100 | * will exit after that, as soon as forwarded connections have terminated. | |
101 | */ | |
396c147e | 102 | |
7a934d1b | 103 | static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ |
8efc0c15 | 104 | |
0bbbf2a4 | 105 | /* Cleanup on signals (!use_privsep case only) */ |
106 | static volatile sig_atomic_t received_sigterm = 0; | |
107 | ||
396c147e | 108 | /* prototypes */ |
109 | static void server_init_dispatch(void); | |
7368a6c8 | 110 | |
a10be357 | 111 | /* |
112 | * we write to this pipe if a SIGCHLD is caught in order to avoid | |
113 | * the race between select() and child_terminated | |
114 | */ | |
115 | static int notify_pipe[2]; | |
116 | static void | |
117 | notify_setup(void) | |
118 | { | |
119 | if (pipe(notify_pipe) < 0) { | |
120 | error("pipe(notify_pipe) failed %s", strerror(errno)); | |
121 | } else if ((fcntl(notify_pipe[0], F_SETFD, 1) == -1) || | |
122 | (fcntl(notify_pipe[1], F_SETFD, 1) == -1)) { | |
123 | error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); | |
124 | close(notify_pipe[0]); | |
125 | close(notify_pipe[1]); | |
126 | } else { | |
127 | set_nonblock(notify_pipe[0]); | |
128 | set_nonblock(notify_pipe[1]); | |
129 | return; | |
130 | } | |
131 | notify_pipe[0] = -1; /* read end */ | |
132 | notify_pipe[1] = -1; /* write end */ | |
133 | } | |
134 | static void | |
135 | notify_parent(void) | |
136 | { | |
137 | if (notify_pipe[1] != -1) | |
138 | write(notify_pipe[1], "", 1); | |
139 | } | |
140 | static void | |
141 | notify_prepare(fd_set *readset) | |
142 | { | |
143 | if (notify_pipe[0] != -1) | |
144 | FD_SET(notify_pipe[0], readset); | |
145 | } | |
146 | static void | |
147 | notify_done(fd_set *readset) | |
148 | { | |
149 | char c; | |
150 | ||
151 | if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) | |
152 | while (read(notify_pipe[0], &c, 1) != -1) | |
153 | debug2("notify_done: reading"); | |
154 | } | |
155 | ||
3efa8ea9 | 156 | /*ARGSUSED*/ |
396c147e | 157 | static void |
5260325f | 158 | sigchld_handler(int sig) |
e78a59f5 | 159 | { |
160 | int save_errno = errno; | |
e78a59f5 | 161 | child_terminated = 1; |
ef51930f | 162 | #ifndef _UNICOS |
c9130033 | 163 | mysignal(SIGCHLD, sigchld_handler); |
ef51930f | 164 | #endif |
a10be357 | 165 | notify_parent(); |
e78a59f5 | 166 | errno = save_errno; |
167 | } | |
8efc0c15 | 168 | |
3efa8ea9 | 169 | /*ARGSUSED*/ |
0bbbf2a4 | 170 | static void |
171 | sigterm_handler(int sig) | |
172 | { | |
173 | received_sigterm = sig; | |
174 | } | |
175 | ||
5260325f | 176 | /* |
177 | * Make packets from buffered stderr data, and buffer it for sending | |
178 | * to the client. | |
179 | */ | |
396c147e | 180 | static void |
5ca51e19 | 181 | make_packets_from_stderr_data(void) |
8efc0c15 | 182 | { |
a27002e5 | 183 | u_int len; |
5260325f | 184 | |
185 | /* Send buffered stderr data to the client. */ | |
186 | while (buffer_len(&stderr_buffer) > 0 && | |
a408af76 | 187 | packet_not_very_much_data_to_write()) { |
5260325f | 188 | len = buffer_len(&stderr_buffer); |
189 | if (packet_is_interactive()) { | |
190 | if (len > 512) | |
191 | len = 512; | |
192 | } else { | |
193 | /* Keep the packets at reasonable size. */ | |
194 | if (len > packet_get_maxsize()) | |
195 | len = packet_get_maxsize(); | |
196 | } | |
197 | packet_start(SSH_SMSG_STDERR_DATA); | |
198 | packet_put_string(buffer_ptr(&stderr_buffer), len); | |
199 | packet_send(); | |
200 | buffer_consume(&stderr_buffer, len); | |
201 | stderr_bytes += len; | |
8efc0c15 | 202 | } |
8efc0c15 | 203 | } |
204 | ||
5260325f | 205 | /* |
206 | * Make packets from buffered stdout data, and buffer it for sending to the | |
207 | * client. | |
208 | */ | |
396c147e | 209 | static void |
5ca51e19 | 210 | make_packets_from_stdout_data(void) |
8efc0c15 | 211 | { |
a27002e5 | 212 | u_int len; |
5260325f | 213 | |
214 | /* Send buffered stdout data to the client. */ | |
215 | while (buffer_len(&stdout_buffer) > 0 && | |
a408af76 | 216 | packet_not_very_much_data_to_write()) { |
5260325f | 217 | len = buffer_len(&stdout_buffer); |
218 | if (packet_is_interactive()) { | |
219 | if (len > 512) | |
220 | len = 512; | |
221 | } else { | |
222 | /* Keep the packets at reasonable size. */ | |
223 | if (len > packet_get_maxsize()) | |
2b87da3b | 224 | len = packet_get_maxsize(); |
5260325f | 225 | } |
226 | packet_start(SSH_SMSG_STDOUT_DATA); | |
227 | packet_put_string(buffer_ptr(&stdout_buffer), len); | |
228 | packet_send(); | |
229 | buffer_consume(&stdout_buffer, len); | |
230 | stdout_bytes += len; | |
8efc0c15 | 231 | } |
8efc0c15 | 232 | } |
233 | ||
72176c0e | 234 | static void |
235 | client_alive_check(void) | |
236 | { | |
95ae2076 | 237 | int channel_id; |
99416ceb | 238 | |
72176c0e | 239 | /* timeout, check to see how many we have had */ |
240 | if (++client_alive_timeouts > options.client_alive_count_max) | |
241 | packet_disconnect("Timeout, your session not responding."); | |
242 | ||
72176c0e | 243 | /* |
95ae2076 | 244 | * send a bogus global/channel request with "wantreply", |
72176c0e | 245 | * we should get back a failure |
246 | */ | |
95ae2076 | 247 | if ((channel_id = channel_find_open()) == -1) { |
248 | packet_start(SSH2_MSG_GLOBAL_REQUEST); | |
249 | packet_put_cstring("keepalive@openssh.com"); | |
250 | packet_put_char(1); /* boolean: want reply */ | |
251 | } else { | |
252 | channel_request_start(channel_id, "keepalive@openssh.com", 1); | |
253 | } | |
72176c0e | 254 | packet_send(); |
255 | } | |
256 | ||
5260325f | 257 | /* |
258 | * Sleep in select() until we can do something. This will initialize the | |
259 | * select masks. Upon return, the masks will indicate which descriptors | |
260 | * have data or can accept data. Optionally, a maximum time can be specified | |
261 | * for the duration of the wait (0 = infinite). | |
262 | */ | |
396c147e | 263 | static void |
39929cdb | 264 | wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
9a995072 | 265 | u_int *nallocp, u_int max_time_milliseconds) |
8efc0c15 | 266 | { |
5260325f | 267 | struct timeval tv, *tvp; |
268 | int ret; | |
3ffc6336 | 269 | int client_alive_scheduled = 0; |
270 | ||
271 | /* | |
184eed6a | 272 | * if using client_alive, set the max timeout accordingly, |
3ffc6336 | 273 | * and indicate that this particular timeout was for client |
274 | * alive by setting the client_alive_scheduled flag. | |
275 | * | |
276 | * this could be randomized somewhat to make traffic | |
184eed6a | 277 | * analysis more difficult, but we're not doing it yet. |
3ffc6336 | 278 | */ |
2c5b1791 | 279 | if (compat20 && |
280 | max_time_milliseconds == 0 && options.client_alive_interval) { | |
39aefe7b | 281 | client_alive_scheduled = 1; |
3ffc6336 | 282 | max_time_milliseconds = options.client_alive_interval * 1000; |
2c5b1791 | 283 | } |
8efc0c15 | 284 | |
39929cdb | 285 | /* Allocate and update select() masks for channel descriptors. */ |
489aa2e9 | 286 | channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 0); |
5260325f | 287 | |
e78a59f5 | 288 | if (compat20) { |
0c139bd1 | 289 | #if 0 |
1d1ffb87 | 290 | /* wrong: bad condition XXX */ |
e78a59f5 | 291 | if (channel_not_very_much_buffered_data()) |
0c139bd1 | 292 | #endif |
293 | FD_SET(connection_in, *readsetp); | |
e78a59f5 | 294 | } else { |
71276795 | 295 | /* |
296 | * Read packets from the client unless we have too much | |
297 | * buffered stdin or channel data. | |
298 | */ | |
299 | if (buffer_len(&stdin_buffer) < buffer_high && | |
e78a59f5 | 300 | channel_not_very_much_buffered_data()) |
39929cdb | 301 | FD_SET(connection_in, *readsetp); |
71276795 | 302 | /* |
303 | * If there is not too much data already buffered going to | |
304 | * the client, try to get some more data from the program. | |
305 | */ | |
306 | if (packet_not_very_much_data_to_write()) { | |
307 | if (!fdout_eof) | |
39929cdb | 308 | FD_SET(fdout, *readsetp); |
71276795 | 309 | if (!fderr_eof) |
39929cdb | 310 | FD_SET(fderr, *readsetp); |
71276795 | 311 | } |
312 | /* | |
313 | * If we have buffered data, try to write some of that data | |
314 | * to the program. | |
315 | */ | |
316 | if (fdin != -1 && buffer_len(&stdin_buffer) > 0) | |
39929cdb | 317 | FD_SET(fdin, *writesetp); |
e78a59f5 | 318 | } |
a10be357 | 319 | notify_prepare(*readsetp); |
5260325f | 320 | |
aa3378df | 321 | /* |
322 | * If we have buffered packet data going to the client, mark that | |
323 | * descriptor. | |
324 | */ | |
5260325f | 325 | if (packet_have_data_to_write()) |
39929cdb | 326 | FD_SET(connection_out, *writesetp); |
5260325f | 327 | |
aa3378df | 328 | /* |
329 | * If child has terminated and there is enough buffer space to read | |
330 | * from it, then read as much as is available and exit. | |
331 | */ | |
5260325f | 332 | if (child_terminated && packet_not_very_much_data_to_write()) |
3ffc6336 | 333 | if (max_time_milliseconds == 0 || client_alive_scheduled) |
5260325f | 334 | max_time_milliseconds = 100; |
335 | ||
336 | if (max_time_milliseconds == 0) | |
337 | tvp = NULL; | |
338 | else { | |
339 | tv.tv_sec = max_time_milliseconds / 1000; | |
340 | tv.tv_usec = 1000 * (max_time_milliseconds % 1000); | |
341 | tvp = &tv; | |
342 | } | |
8efc0c15 | 343 | |
5260325f | 344 | /* Wait for something to happen, or the timeout to expire. */ |
39929cdb | 345 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
8efc0c15 | 346 | |
3ffc6336 | 347 | if (ret == -1) { |
b3ad8fe6 | 348 | memset(*readsetp, 0, *nallocp); |
349 | memset(*writesetp, 0, *nallocp); | |
5260325f | 350 | if (errno != EINTR) |
351 | error("select: %.100s", strerror(errno)); | |
418e724c | 352 | } else if (ret == 0 && client_alive_scheduled) |
72176c0e | 353 | client_alive_check(); |
a10be357 | 354 | |
355 | notify_done(*readsetp); | |
8efc0c15 | 356 | } |
357 | ||
5260325f | 358 | /* |
359 | * Processes input from the client and the program. Input data is stored | |
360 | * in buffers and processed later. | |
361 | */ | |
396c147e | 362 | static void |
b476adfa | 363 | process_input(fd_set *readset) |
5260325f | 364 | { |
365 | int len; | |
366 | char buf[16384]; | |
367 | ||
368 | /* Read and buffer any input data from the client. */ | |
369 | if (FD_ISSET(connection_in, readset)) { | |
370 | len = read(connection_in, buf, sizeof(buf)); | |
371 | if (len == 0) { | |
00e842d8 | 372 | verbose("Connection closed by %.100s", |
373 | get_remote_ipaddr()); | |
cc94bd38 | 374 | connection_closed = 1; |
375 | if (compat20) | |
376 | return; | |
2362db19 | 377 | cleanup_exit(255); |
0e73cc53 | 378 | } else if (len < 0) { |
379 | if (errno != EINTR && errno != EAGAIN) { | |
00e842d8 | 380 | verbose("Read error from remote host " |
381 | "%.100s: %.100s", | |
382 | get_remote_ipaddr(), strerror(errno)); | |
2362db19 | 383 | cleanup_exit(255); |
0e73cc53 | 384 | } |
385 | } else { | |
386 | /* Buffer any received data. */ | |
387 | packet_process_incoming(buf, len); | |
5260325f | 388 | } |
5260325f | 389 | } |
e78a59f5 | 390 | if (compat20) |
391 | return; | |
392 | ||
5260325f | 393 | /* Read and buffer any available stdout data from the program. */ |
394 | if (!fdout_eof && FD_ISSET(fdout, readset)) { | |
ea8c44d9 | 395 | errno = 0; |
5260325f | 396 | len = read(fdout, buf, sizeof(buf)); |
0e73cc53 | 397 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
398 | /* do nothing */ | |
24a87055 | 399 | #ifndef PTY_ZEROREAD |
0e73cc53 | 400 | } else if (len <= 0) { |
ea8c44d9 | 401 | #else |
9c04d67d | 402 | } else if ((!isatty(fdout) && len <= 0) || |
403 | (isatty(fdout) && (len < 0 || (len == 0 && errno != 0)))) { | |
ea8c44d9 | 404 | #endif |
5260325f | 405 | fdout_eof = 1; |
0e73cc53 | 406 | } else { |
5260325f | 407 | buffer_append(&stdout_buffer, buf, len); |
408 | fdout_bytes += len; | |
409 | } | |
410 | } | |
411 | /* Read and buffer any available stderr data from the program. */ | |
412 | if (!fderr_eof && FD_ISSET(fderr, readset)) { | |
ea8c44d9 | 413 | errno = 0; |
5260325f | 414 | len = read(fderr, buf, sizeof(buf)); |
0e73cc53 | 415 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
416 | /* do nothing */ | |
24a87055 | 417 | #ifndef PTY_ZEROREAD |
0e73cc53 | 418 | } else if (len <= 0) { |
ea8c44d9 | 419 | #else |
9c04d67d | 420 | } else if ((!isatty(fderr) && len <= 0) || |
421 | (isatty(fderr) && (len < 0 || (len == 0 && errno != 0)))) { | |
ea8c44d9 | 422 | #endif |
5260325f | 423 | fderr_eof = 1; |
0e73cc53 | 424 | } else { |
5260325f | 425 | buffer_append(&stderr_buffer, buf, len); |
0e73cc53 | 426 | } |
5260325f | 427 | } |
428 | } | |
8efc0c15 | 429 | |
5260325f | 430 | /* |
431 | * Sends data from internal buffers to client program stdin. | |
432 | */ | |
396c147e | 433 | static void |
b476adfa | 434 | process_output(fd_set *writeset) |
8efc0c15 | 435 | { |
4ef922e3 | 436 | struct termios tio; |
780a9951 | 437 | u_char *data; |
438 | u_int dlen; | |
5260325f | 439 | int len; |
440 | ||
441 | /* Write buffered data to program stdin. */ | |
e78a59f5 | 442 | if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { |
780a9951 | 443 | data = buffer_ptr(&stdin_buffer); |
444 | dlen = buffer_len(&stdin_buffer); | |
445 | len = write(fdin, data, dlen); | |
0e73cc53 | 446 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
447 | /* do nothing */ | |
448 | } else if (len <= 0) { | |
7368a6c8 | 449 | if (fdin != fdout) |
5260325f | 450 | close(fdin); |
451 | else | |
452 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ | |
5260325f | 453 | fdin = -1; |
454 | } else { | |
4ef922e3 | 455 | /* Successful write. */ |
780a9951 | 456 | if (fdin_is_tty && dlen >= 1 && data[0] != '\r' && |
457 | tcgetattr(fdin, &tio) == 0 && | |
0ae4fe1d | 458 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
62da27dd | 459 | /* |
460 | * Simulate echo to reduce the impact of | |
461 | * traffic analysis | |
462 | */ | |
95ce5599 | 463 | packet_send_ignore(len); |
4ef922e3 | 464 | packet_send(); |
465 | } | |
466 | /* Consume the data from the buffer. */ | |
5260325f | 467 | buffer_consume(&stdin_buffer, len); |
468 | /* Update the count of bytes written to the program. */ | |
469 | stdin_bytes += len; | |
470 | } | |
8efc0c15 | 471 | } |
5260325f | 472 | /* Send any buffered packet data to the client. */ |
473 | if (FD_ISSET(connection_out, writeset)) | |
474 | packet_write_poll(); | |
8efc0c15 | 475 | } |
476 | ||
5260325f | 477 | /* |
478 | * Wait until all buffered output has been sent to the client. | |
479 | * This is used when the program terminates. | |
480 | */ | |
396c147e | 481 | static void |
5ca51e19 | 482 | drain_output(void) |
8efc0c15 | 483 | { |
5260325f | 484 | /* Send any buffered stdout data to the client. */ |
485 | if (buffer_len(&stdout_buffer) > 0) { | |
486 | packet_start(SSH_SMSG_STDOUT_DATA); | |
487 | packet_put_string(buffer_ptr(&stdout_buffer), | |
488 | buffer_len(&stdout_buffer)); | |
489 | packet_send(); | |
490 | /* Update the count of sent bytes. */ | |
491 | stdout_bytes += buffer_len(&stdout_buffer); | |
492 | } | |
493 | /* Send any buffered stderr data to the client. */ | |
494 | if (buffer_len(&stderr_buffer) > 0) { | |
495 | packet_start(SSH_SMSG_STDERR_DATA); | |
496 | packet_put_string(buffer_ptr(&stderr_buffer), | |
497 | buffer_len(&stderr_buffer)); | |
498 | packet_send(); | |
499 | /* Update the count of sent bytes. */ | |
500 | stderr_bytes += buffer_len(&stderr_buffer); | |
501 | } | |
502 | /* Wait until all buffered data has been written to the client. */ | |
503 | packet_write_wait(); | |
8efc0c15 | 504 | } |
505 | ||
396c147e | 506 | static void |
5ca51e19 | 507 | process_buffered_input_packets(void) |
7368a6c8 | 508 | { |
7a37c112 | 509 | dispatch_run(DISPATCH_NONBLOCK, NULL, compat20 ? xxx_kex : NULL); |
7368a6c8 | 510 | } |
511 | ||
5260325f | 512 | /* |
513 | * Performs the interactive session. This handles data transmission between | |
514 | * the client and the program. Note that the notion of stdin, stdout, and | |
515 | * stderr in this function is sort of reversed: this function writes to | |
516 | * stdin (of the child program), and reads from stdout and stderr (of the | |
517 | * child program). | |
518 | */ | |
6ae2364d | 519 | void |
9da5c3c9 | 520 | server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) |
8efc0c15 | 521 | { |
39929cdb | 522 | fd_set *readset = NULL, *writeset = NULL; |
9a995072 | 523 | int max_fd = 0; |
524 | u_int nalloc = 0; | |
9da5c3c9 | 525 | int wait_status; /* Status returned by wait(). */ |
526 | pid_t wait_pid; /* pid returned by wait(). */ | |
5260325f | 527 | int waiting_termination = 0; /* Have displayed waiting close message. */ |
1e3b8b07 | 528 | u_int max_time_milliseconds; |
529 | u_int previous_stdout_buffer_bytes; | |
530 | u_int stdout_buffer_bytes; | |
5260325f | 531 | int type; |
532 | ||
533 | debug("Entering interactive session."); | |
534 | ||
535 | /* Initialize the SIGCHLD kludge. */ | |
5260325f | 536 | child_terminated = 0; |
42ad0eec | 537 | mysignal(SIGCHLD, sigchld_handler); |
5260325f | 538 | |
0bbbf2a4 | 539 | if (!use_privsep) { |
540 | signal(SIGTERM, sigterm_handler); | |
541 | signal(SIGINT, sigterm_handler); | |
542 | signal(SIGQUIT, sigterm_handler); | |
543 | } | |
544 | ||
5260325f | 545 | /* Initialize our global variables. */ |
546 | fdin = fdin_arg; | |
547 | fdout = fdout_arg; | |
548 | fderr = fderr_arg; | |
0e73cc53 | 549 | |
550 | /* nonblocking IO */ | |
551 | set_nonblock(fdin); | |
552 | set_nonblock(fdout); | |
ff873e98 | 553 | /* we don't have stderr for interactive terminal sessions, see below */ |
554 | if (fderr != -1) | |
555 | set_nonblock(fderr); | |
0e73cc53 | 556 | |
e68225b2 | 557 | if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin)) |
558 | fdin_is_tty = 1; | |
559 | ||
5260325f | 560 | connection_in = packet_get_connection_in(); |
561 | connection_out = packet_get_connection_out(); | |
562 | ||
a10be357 | 563 | notify_setup(); |
564 | ||
5260325f | 565 | previous_stdout_buffer_bytes = 0; |
566 | ||
567 | /* Set approximate I/O buffer size. */ | |
568 | if (packet_is_interactive()) | |
569 | buffer_high = 4096; | |
570 | else | |
571 | buffer_high = 64 * 1024; | |
572 | ||
489aa2e9 | 573 | #if 0 |
5260325f | 574 | /* Initialize max_fd to the maximum of the known file descriptors. */ |
489aa2e9 | 575 | max_fd = MAX(connection_in, connection_out); |
576 | max_fd = MAX(max_fd, fdin); | |
577 | max_fd = MAX(max_fd, fdout); | |
39929cdb | 578 | if (fderr != -1) |
579 | max_fd = MAX(max_fd, fderr); | |
489aa2e9 | 580 | #endif |
5260325f | 581 | |
582 | /* Initialize Initialize buffers. */ | |
583 | buffer_init(&stdin_buffer); | |
584 | buffer_init(&stdout_buffer); | |
585 | buffer_init(&stderr_buffer); | |
586 | ||
aa3378df | 587 | /* |
588 | * If we have no separate fderr (which is the case when we have a pty | |
589 | * - there we cannot make difference between data sent to stdout and | |
590 | * stderr), indicate that we have seen an EOF from stderr. This way | |
7b9b0103 | 591 | * we don't need to check the descriptor everywhere. |
aa3378df | 592 | */ |
5260325f | 593 | if (fderr == -1) |
594 | fderr_eof = 1; | |
595 | ||
7368a6c8 | 596 | server_init_dispatch(); |
597 | ||
5260325f | 598 | /* Main loop of the server for the interactive session mode. */ |
599 | for (;;) { | |
5260325f | 600 | |
601 | /* Process buffered packets from the client. */ | |
602 | process_buffered_input_packets(); | |
603 | ||
aa3378df | 604 | /* |
605 | * If we have received eof, and there is no more pending | |
606 | * input data, cause a real eof by closing fdin. | |
607 | */ | |
5260325f | 608 | if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { |
7368a6c8 | 609 | if (fdin != fdout) |
5260325f | 610 | close(fdin); |
611 | else | |
612 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ | |
5260325f | 613 | fdin = -1; |
614 | } | |
aa3378df | 615 | /* Make packets from buffered stderr data to send to the client. */ |
5260325f | 616 | make_packets_from_stderr_data(); |
617 | ||
aa3378df | 618 | /* |
619 | * Make packets from buffered stdout data to send to the | |
620 | * client. If there is very little to send, this arranges to | |
621 | * not send them now, but to wait a short while to see if we | |
622 | * are getting more data. This is necessary, as some systems | |
623 | * wake up readers from a pty after each separate character. | |
624 | */ | |
5260325f | 625 | max_time_milliseconds = 0; |
626 | stdout_buffer_bytes = buffer_len(&stdout_buffer); | |
627 | if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && | |
628 | stdout_buffer_bytes != previous_stdout_buffer_bytes) { | |
629 | /* try again after a while */ | |
630 | max_time_milliseconds = 10; | |
631 | } else { | |
632 | /* Send it now. */ | |
633 | make_packets_from_stdout_data(); | |
634 | } | |
635 | previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); | |
636 | ||
637 | /* Send channel data to the client. */ | |
638 | if (packet_not_very_much_data_to_write()) | |
639 | channel_output_poll(); | |
640 | ||
aa3378df | 641 | /* |
642 | * Bail out of the loop if the program has closed its output | |
643 | * descriptors, and we have no more data to send to the | |
644 | * client, and there is no pending buffered data. | |
645 | */ | |
7062c40f | 646 | if (fdout_eof && fderr_eof && !packet_have_data_to_write() && |
647 | buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { | |
5260325f | 648 | if (!channel_still_open()) |
7368a6c8 | 649 | break; |
5260325f | 650 | if (!waiting_termination) { |
651 | const char *s = "Waiting for forwarded connections to terminate...\r\n"; | |
652 | char *cp; | |
653 | waiting_termination = 1; | |
654 | buffer_append(&stderr_buffer, s, strlen(s)); | |
655 | ||
656 | /* Display list of open channels. */ | |
657 | cp = channel_open_message(); | |
658 | buffer_append(&stderr_buffer, cp, strlen(cp)); | |
659 | xfree(cp); | |
660 | } | |
661 | } | |
489aa2e9 | 662 | max_fd = MAX(connection_in, connection_out); |
663 | max_fd = MAX(max_fd, fdin); | |
664 | max_fd = MAX(max_fd, fdout); | |
665 | max_fd = MAX(max_fd, fderr); | |
a10be357 | 666 | max_fd = MAX(max_fd, notify_pipe[0]); |
489aa2e9 | 667 | |
5260325f | 668 | /* Sleep in select() until we can do something. */ |
39929cdb | 669 | wait_until_can_do_something(&readset, &writeset, &max_fd, |
489aa2e9 | 670 | &nalloc, max_time_milliseconds); |
5260325f | 671 | |
0bbbf2a4 | 672 | if (received_sigterm) { |
673 | logit("Exiting on signal %d", received_sigterm); | |
674 | /* Clean up sessions, utmp, etc. */ | |
675 | cleanup_exit(255); | |
676 | } | |
677 | ||
5260325f | 678 | /* Process any channel events. */ |
39929cdb | 679 | channel_after_select(readset, writeset); |
5260325f | 680 | |
681 | /* Process input from the client and from program stdout/stderr. */ | |
39929cdb | 682 | process_input(readset); |
5260325f | 683 | |
684 | /* Process output to the client and to program stdin. */ | |
39929cdb | 685 | process_output(writeset); |
8efc0c15 | 686 | } |
39929cdb | 687 | if (readset) |
688 | xfree(readset); | |
689 | if (writeset) | |
690 | xfree(writeset); | |
8efc0c15 | 691 | |
5260325f | 692 | /* Cleanup and termination code. */ |
8efc0c15 | 693 | |
5260325f | 694 | /* Wait until all output has been sent to the client. */ |
695 | drain_output(); | |
8efc0c15 | 696 | |
5260325f | 697 | debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", |
184eed6a | 698 | stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); |
8efc0c15 | 699 | |
5260325f | 700 | /* Free and clear the buffers. */ |
701 | buffer_free(&stdin_buffer); | |
702 | buffer_free(&stdout_buffer); | |
703 | buffer_free(&stderr_buffer); | |
8efc0c15 | 704 | |
5260325f | 705 | /* Close the file descriptors. */ |
706 | if (fdout != -1) | |
707 | close(fdout); | |
708 | fdout = -1; | |
709 | fdout_eof = 1; | |
710 | if (fderr != -1) | |
711 | close(fderr); | |
712 | fderr = -1; | |
713 | fderr_eof = 1; | |
714 | if (fdin != -1) | |
715 | close(fdin); | |
716 | fdin = -1; | |
717 | ||
d6746a0b | 718 | channel_free_all(); |
5260325f | 719 | |
5260325f | 720 | /* We no longer want our SIGCHLD handler to be called. */ |
42ad0eec | 721 | mysignal(SIGCHLD, SIG_DFL); |
5260325f | 722 | |
8c38e88b | 723 | while ((wait_pid = waitpid(-1, &wait_status, 0)) < 0) |
724 | if (errno != EINTR) | |
725 | packet_disconnect("wait: %.100s", strerror(errno)); | |
726 | if (wait_pid != pid) | |
c457707e | 727 | error("Strange, wait returned pid %ld, expected %ld", |
728 | (long)wait_pid, (long)pid); | |
c9130033 | 729 | |
5260325f | 730 | /* Check if it exited normally. */ |
731 | if (WIFEXITED(wait_status)) { | |
732 | /* Yes, normal exit. Get exit status and send it to the client. */ | |
733 | debug("Command exited with status %d.", WEXITSTATUS(wait_status)); | |
734 | packet_start(SSH_SMSG_EXITSTATUS); | |
735 | packet_put_int(WEXITSTATUS(wait_status)); | |
736 | packet_send(); | |
737 | packet_write_wait(); | |
738 | ||
aa3378df | 739 | /* |
740 | * Wait for exit confirmation. Note that there might be | |
741 | * other packets coming before it; however, the program has | |
742 | * already died so we just ignore them. The client is | |
743 | * supposed to respond with the confirmation when it receives | |
744 | * the exit status. | |
745 | */ | |
5260325f | 746 | do { |
54a5250f | 747 | type = packet_read(); |
5260325f | 748 | } |
749 | while (type != SSH_CMSG_EXIT_CONFIRMATION); | |
750 | ||
751 | debug("Received exit confirmation."); | |
752 | return; | |
753 | } | |
754 | /* Check if the program terminated due to a signal. */ | |
755 | if (WIFSIGNALED(wait_status)) | |
756 | packet_disconnect("Command terminated on signal %d.", | |
757 | WTERMSIG(wait_status)); | |
758 | ||
759 | /* Some weird exit cause. Just exit. */ | |
760 | packet_disconnect("wait returned status %04x.", wait_status); | |
761 | /* NOTREACHED */ | |
762 | } | |
7368a6c8 | 763 | |
418e724c | 764 | static void |
765 | collect_children(void) | |
766 | { | |
767 | pid_t pid; | |
768 | sigset_t oset, nset; | |
769 | int status; | |
770 | ||
771 | /* block SIGCHLD while we check for dead children */ | |
772 | sigemptyset(&nset); | |
773 | sigaddset(&nset, SIGCHLD); | |
774 | sigprocmask(SIG_BLOCK, &nset, &oset); | |
775 | if (child_terminated) { | |
140000b4 | 776 | debug("Received SIGCHLD."); |
8c38e88b | 777 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
778 | (pid < 0 && errno == EINTR)) | |
779 | if (pid > 0) | |
780 | session_close_by_pid(pid, status); | |
418e724c | 781 | child_terminated = 0; |
782 | } | |
783 | sigprocmask(SIG_SETMASK, &oset, NULL); | |
784 | } | |
785 | ||
6ae2364d | 786 | void |
57156994 | 787 | server_loop2(Authctxt *authctxt) |
e78a59f5 | 788 | { |
39929cdb | 789 | fd_set *readset = NULL, *writeset = NULL; |
418e724c | 790 | int rekeying = 0, max_fd, nalloc = 0; |
e78a59f5 | 791 | |
792 | debug("Entering interactive session for SSH2."); | |
793 | ||
c9130033 | 794 | mysignal(SIGCHLD, sigchld_handler); |
e78a59f5 | 795 | child_terminated = 0; |
796 | connection_in = packet_get_connection_in(); | |
797 | connection_out = packet_get_connection_out(); | |
39929cdb | 798 | |
0bbbf2a4 | 799 | if (!use_privsep) { |
800 | signal(SIGTERM, sigterm_handler); | |
801 | signal(SIGINT, sigterm_handler); | |
802 | signal(SIGQUIT, sigterm_handler); | |
803 | } | |
804 | ||
a10be357 | 805 | notify_setup(); |
806 | ||
39929cdb | 807 | max_fd = MAX(connection_in, connection_out); |
a10be357 | 808 | max_fd = MAX(max_fd, notify_pipe[0]); |
809 | ||
e78a59f5 | 810 | server_init_dispatch(); |
811 | ||
812 | for (;;) { | |
813 | process_buffered_input_packets(); | |
bbb4cc1b | 814 | |
cd332296 | 815 | rekeying = (xxx_kex != NULL && !xxx_kex->done); |
bbb4cc1b | 816 | |
bbb4cc1b | 817 | if (!rekeying && packet_not_very_much_data_to_write()) |
e78a59f5 | 818 | channel_output_poll(); |
bbb4cc1b | 819 | wait_until_can_do_something(&readset, &writeset, &max_fd, |
489aa2e9 | 820 | &nalloc, 0); |
d5f24f94 | 821 | |
0bbbf2a4 | 822 | if (received_sigterm) { |
823 | logit("Exiting on signal %d", received_sigterm); | |
824 | /* Clean up sessions, utmp, etc. */ | |
825 | cleanup_exit(255); | |
826 | } | |
827 | ||
418e724c | 828 | collect_children(); |
ffd7b36b | 829 | if (!rekeying) { |
bbb4cc1b | 830 | channel_after_select(readset, writeset); |
ffd7b36b | 831 | if (packet_need_rekeying()) { |
832 | debug("need rekeying"); | |
833 | xxx_kex->done = 0; | |
834 | kex_send_kexinit(xxx_kex); | |
835 | } | |
836 | } | |
39929cdb | 837 | process_input(readset); |
cc94bd38 | 838 | if (connection_closed) |
839 | break; | |
39929cdb | 840 | process_output(writeset); |
e78a59f5 | 841 | } |
418e724c | 842 | collect_children(); |
843 | ||
39929cdb | 844 | if (readset) |
845 | xfree(readset); | |
846 | if (writeset) | |
847 | xfree(writeset); | |
848 | ||
d5f24f94 | 849 | /* free all channels, no more reads and writes */ |
850 | channel_free_all(); | |
5b5d170c | 851 | |
418e724c | 852 | /* free remaining sessions, e.g. remove wtmp entries */ |
1853d1ef | 853 | session_destroy_all(NULL); |
e78a59f5 | 854 | } |
855 | ||
396c147e | 856 | static void |
95ae2076 | 857 | server_input_keep_alive(int type, u_int32_t seq, void *ctxt) |
3ffc6336 | 858 | { |
95ae2076 | 859 | debug("Got %d/%u for keepalive", type, seq); |
184eed6a | 860 | /* |
39aefe7b | 861 | * reset timeout, since we got a sane answer from the client. |
3ffc6336 | 862 | * even if this was generated by something other than |
863 | * the bogus CHANNEL_REQUEST we send for keepalives. | |
864 | */ | |
184eed6a | 865 | client_alive_timeouts = 0; |
3ffc6336 | 866 | } |
867 | ||
396c147e | 868 | static void |
7819b5c3 | 869 | server_input_stdin_data(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 870 | { |
871 | char *data; | |
1e3b8b07 | 872 | u_int data_len; |
7368a6c8 | 873 | |
874 | /* Stdin data from the client. Append it to the buffer. */ | |
875 | /* Ignore any data if the client has closed stdin. */ | |
876 | if (fdin == -1) | |
877 | return; | |
878 | data = packet_get_string(&data_len); | |
95500969 | 879 | packet_check_eom(); |
7368a6c8 | 880 | buffer_append(&stdin_buffer, data, data_len); |
881 | memset(data, 0, data_len); | |
882 | xfree(data); | |
883 | } | |
884 | ||
396c147e | 885 | static void |
7819b5c3 | 886 | server_input_eof(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 887 | { |
888 | /* | |
889 | * Eof from the client. The stdin descriptor to the | |
890 | * program will be closed when all buffered data has | |
891 | * drained. | |
892 | */ | |
893 | debug("EOF received for stdin."); | |
95500969 | 894 | packet_check_eom(); |
7368a6c8 | 895 | stdin_eof = 1; |
896 | } | |
897 | ||
396c147e | 898 | static void |
7819b5c3 | 899 | server_input_window_size(int type, u_int32_t seq, void *ctxt) |
7368a6c8 | 900 | { |
148de80c | 901 | u_int row = packet_get_int(); |
902 | u_int col = packet_get_int(); | |
903 | u_int xpixel = packet_get_int(); | |
904 | u_int ypixel = packet_get_int(); | |
7368a6c8 | 905 | |
906 | debug("Window change received."); | |
95500969 | 907 | packet_check_eom(); |
7368a6c8 | 908 | if (fdin != -1) |
909 | pty_change_window_size(fdin, row, col, xpixel, ypixel); | |
910 | } | |
911 | ||
396c147e | 912 | static Channel * |
43f7a4b8 | 913 | server_request_direct_tcpip(void) |
e78a59f5 | 914 | { |
719fc62f | 915 | Channel *c; |
916 | int sock; | |
6ae2364d | 917 | char *target, *originator; |
918 | int target_port, originator_port; | |
e78a59f5 | 919 | |
6ae2364d | 920 | target = packet_get_string(NULL); |
921 | target_port = packet_get_int(); | |
e78a59f5 | 922 | originator = packet_get_string(NULL); |
923 | originator_port = packet_get_int(); | |
95500969 | 924 | packet_check_eom(); |
71276795 | 925 | |
fa08c86b | 926 | debug("server_request_direct_tcpip: originator %s port %d, target %s port %d", |
4e2e5cfd | 927 | originator, originator_port, target, target_port); |
38c295d6 | 928 | |
e78a59f5 | 929 | /* XXX check permission */ |
6ae2364d | 930 | sock = channel_connect_to(target, target_port); |
931 | xfree(target); | |
e78a59f5 | 932 | xfree(originator); |
933 | if (sock < 0) | |
fa08c86b | 934 | return NULL; |
43f7a4b8 | 935 | c = channel_new("direct-tcpip", SSH_CHANNEL_CONNECTING, |
bcbf86ec | 936 | sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT, |
0d942eff | 937 | CHAN_TCP_PACKET_DEFAULT, 0, "direct-tcpip", 1); |
719fc62f | 938 | return c; |
fa08c86b | 939 | } |
940 | ||
d20f3c9e | 941 | static Channel * |
942 | server_request_tun(void) | |
943 | { | |
944 | Channel *c = NULL; | |
a4f24bf8 | 945 | int mode, tun; |
946 | int sock; | |
d20f3c9e | 947 | |
a4f24bf8 | 948 | mode = packet_get_int(); |
949 | switch (mode) { | |
950 | case SSH_TUNMODE_POINTOPOINT: | |
951 | case SSH_TUNMODE_ETHERNET: | |
952 | break; | |
953 | default: | |
954 | packet_send_debug("Unsupported tunnel device mode."); | |
955 | return NULL; | |
956 | } | |
957 | if ((options.permit_tun & mode) == 0) { | |
958 | packet_send_debug("Server has rejected tunnel device " | |
959 | "forwarding"); | |
d20f3c9e | 960 | return NULL; |
961 | } | |
962 | ||
963 | tun = packet_get_int(); | |
a2b1748a | 964 | if (forced_tun_device != -1) { |
3efa8ea9 | 965 | if (tun != SSH_TUNID_ANY && forced_tun_device != tun) |
d20f3c9e | 966 | goto done; |
967 | tun = forced_tun_device; | |
968 | } | |
a4f24bf8 | 969 | sock = tun_open(tun, mode); |
d20f3c9e | 970 | if (sock < 0) |
971 | goto done; | |
972 | c = channel_new("tun", SSH_CHANNEL_OPEN, sock, sock, -1, | |
973 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); | |
974 | c->datagram = 1; | |
e914f53a | 975 | #if defined(SSH_TUN_FILTER) |
976 | if (mode == SSH_TUNMODE_POINTOPOINT) | |
977 | channel_register_filter(c->self, sys_tun_infilter, | |
978 | sys_tun_outfilter); | |
979 | #endif | |
d20f3c9e | 980 | |
981 | done: | |
982 | if (c == NULL) | |
983 | packet_send_debug("Failed to open the tunnel device."); | |
984 | return c; | |
985 | } | |
986 | ||
396c147e | 987 | static Channel * |
43f7a4b8 | 988 | server_request_session(void) |
fa08c86b | 989 | { |
719fc62f | 990 | Channel *c; |
fa08c86b | 991 | |
992 | debug("input_session_request"); | |
95500969 | 993 | packet_check_eom(); |
fa08c86b | 994 | /* |
995 | * A server session has no fd to read or write until a | |
996 | * CHANNEL_REQUEST for a shell is made, so we set the type to | |
997 | * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all | |
998 | * CHANNEL_REQUEST messages is registered. | |
999 | */ | |
43f7a4b8 | 1000 | c = channel_new("session", SSH_CHANNEL_LARVAL, |
719fc62f | 1001 | -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, |
0d942eff | 1002 | 0, "server-session", 1); |
2362db19 | 1003 | if (session_open(the_authctxt, c->self) != 1) { |
719fc62f | 1004 | debug("session open failed, free channel %d", c->self); |
1005 | channel_free(c); | |
1006 | return NULL; | |
fa08c86b | 1007 | } |
8cd0437d | 1008 | channel_register_cleanup(c->self, session_close_by_channel, 0); |
719fc62f | 1009 | return c; |
e78a59f5 | 1010 | } |
1011 | ||
396c147e | 1012 | static void |
7819b5c3 | 1013 | server_input_channel_open(int type, u_int32_t seq, void *ctxt) |
e78a59f5 | 1014 | { |
1015 | Channel *c = NULL; | |
1016 | char *ctype; | |
e78a59f5 | 1017 | int rchan; |
f91d9a89 | 1018 | u_int rmaxpack, rwindow, len; |
e78a59f5 | 1019 | |
1020 | ctype = packet_get_string(&len); | |
1021 | rchan = packet_get_int(); | |
1022 | rwindow = packet_get_int(); | |
1023 | rmaxpack = packet_get_int(); | |
1024 | ||
188adeb2 | 1025 | debug("server_input_channel_open: ctype %s rchan %d win %d max %d", |
e78a59f5 | 1026 | ctype, rchan, rwindow, rmaxpack); |
1027 | ||
1028 | if (strcmp(ctype, "session") == 0) { | |
43f7a4b8 | 1029 | c = server_request_session(); |
e78a59f5 | 1030 | } else if (strcmp(ctype, "direct-tcpip") == 0) { |
43f7a4b8 | 1031 | c = server_request_direct_tcpip(); |
d20f3c9e | 1032 | } else if (strcmp(ctype, "tun@openssh.com") == 0) { |
1033 | c = server_request_tun(); | |
e78a59f5 | 1034 | } |
1035 | if (c != NULL) { | |
fa08c86b | 1036 | debug("server_input_channel_open: confirm %s", ctype); |
e78a59f5 | 1037 | c->remote_id = rchan; |
1038 | c->remote_window = rwindow; | |
1039 | c->remote_maxpacket = rmaxpack; | |
d18e0850 | 1040 | if (c->type != SSH_CHANNEL_CONNECTING) { |
1041 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); | |
1042 | packet_put_int(c->remote_id); | |
1043 | packet_put_int(c->self); | |
1044 | packet_put_int(c->local_window); | |
1045 | packet_put_int(c->local_maxpacket); | |
1046 | packet_send(); | |
1047 | } | |
e78a59f5 | 1048 | } else { |
fa08c86b | 1049 | debug("server_input_channel_open: failure %s", ctype); |
e78a59f5 | 1050 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
1051 | packet_put_int(rchan); | |
1052 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); | |
fbe90f7b | 1053 | if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
d18e0850 | 1054 | packet_put_cstring("open failed"); |
fbe90f7b | 1055 | packet_put_cstring(""); |
1056 | } | |
e78a59f5 | 1057 | packet_send(); |
1058 | } | |
1059 | xfree(ctype); | |
1060 | } | |
1061 | ||
396c147e | 1062 | static void |
7819b5c3 | 1063 | server_input_global_request(int type, u_int32_t seq, void *ctxt) |
fa08c86b | 1064 | { |
1065 | char *rtype; | |
1066 | int want_reply; | |
1067 | int success = 0; | |
1068 | ||
1069 | rtype = packet_get_string(NULL); | |
1070 | want_reply = packet_get_char(); | |
1071 | debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); | |
2b87da3b | 1072 | |
01794848 | 1073 | /* -R style forwarding */ |
fa08c86b | 1074 | if (strcmp(rtype, "tcpip-forward") == 0) { |
1075 | struct passwd *pw; | |
1076 | char *listen_address; | |
1077 | u_short listen_port; | |
1078 | ||
2362db19 | 1079 | pw = the_authctxt->pw; |
95077f48 | 1080 | if (pw == NULL || !the_authctxt->valid) |
fdaef11e | 1081 | fatal("server_input_global_request: no/invalid user"); |
b08a39ff | 1082 | listen_address = packet_get_string(NULL); |
fa08c86b | 1083 | listen_port = (u_short)packet_get_int(); |
1084 | debug("server_input_global_request: tcpip-forward listen %s port %d", | |
1085 | listen_address, listen_port); | |
1086 | ||
1087 | /* check permissions */ | |
1088 | if (!options.allow_tcp_forwarding || | |
e6f15ed1 | 1089 | no_port_forwarding_flag |
1090 | #ifndef NO_IPPORT_RESERVED_CONCEPT | |
1091 | || (listen_port < IPPORT_RESERVED && pw->pw_uid != 0) | |
1092 | #endif | |
98c044d0 | 1093 | ) { |
fa08c86b | 1094 | success = 0; |
1095 | packet_send_debug("Server has disabled port forwarding."); | |
1096 | } else { | |
1097 | /* Start listening on the port */ | |
5641aefa | 1098 | success = channel_setup_remote_fwd_listener( |
1099 | listen_address, listen_port, options.gateway_ports); | |
fa08c86b | 1100 | } |
1101 | xfree(listen_address); | |
d740ec16 | 1102 | } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { |
1103 | char *cancel_address; | |
1104 | u_short cancel_port; | |
1105 | ||
1106 | cancel_address = packet_get_string(NULL); | |
1107 | cancel_port = (u_short)packet_get_int(); | |
1108 | debug("%s: cancel-tcpip-forward addr %s port %d", __func__, | |
1109 | cancel_address, cancel_port); | |
1110 | ||
1111 | success = channel_cancel_rport_listener(cancel_address, | |
1112 | cancel_port); | |
4113a1d7 | 1113 | xfree(cancel_address); |
fa08c86b | 1114 | } |
1115 | if (want_reply) { | |
1116 | packet_start(success ? | |
1117 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); | |
1118 | packet_send(); | |
1119 | packet_write_wait(); | |
1120 | } | |
1121 | xfree(rtype); | |
1122 | } | |
a7137f6b | 1123 | |
7899c98f | 1124 | static void |
1125 | server_input_channel_req(int type, u_int32_t seq, void *ctxt) | |
1126 | { | |
1127 | Channel *c; | |
1128 | int id, reply, success = 0; | |
1129 | char *rtype; | |
1130 | ||
1131 | id = packet_get_int(); | |
1132 | rtype = packet_get_string(NULL); | |
1133 | reply = packet_get_char(); | |
1134 | ||
1135 | debug("server_input_channel_req: channel %d request %s reply %d", | |
1136 | id, rtype, reply); | |
1137 | ||
1138 | if ((c = channel_lookup(id)) == NULL) | |
1139 | packet_disconnect("server_input_channel_req: " | |
1140 | "unknown channel %d", id); | |
1141 | if (c->type == SSH_CHANNEL_LARVAL || c->type == SSH_CHANNEL_OPEN) | |
1142 | success = session_input_channel_req(c, rtype); | |
1143 | if (reply) { | |
1144 | packet_start(success ? | |
1145 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); | |
1146 | packet_put_int(c->remote_id); | |
1147 | packet_send(); | |
1148 | } | |
1149 | xfree(rtype); | |
1150 | } | |
fa08c86b | 1151 | |
396c147e | 1152 | static void |
5ca51e19 | 1153 | server_init_dispatch_20(void) |
e78a59f5 | 1154 | { |
1155 | debug("server_init_dispatch_20"); | |
1156 | dispatch_init(&dispatch_protocol_error); | |
1157 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); | |
1158 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); | |
1159 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); | |
1160 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); | |
1161 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); | |
1162 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); | |
1163 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
7899c98f | 1164 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); |
e78a59f5 | 1165 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
fa08c86b | 1166 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
3ffc6336 | 1167 | /* client_alive */ |
95ae2076 | 1168 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); |
1169 | dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); | |
1170 | dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); | |
7a37c112 | 1171 | /* rekeying */ |
1172 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); | |
e78a59f5 | 1173 | } |
396c147e | 1174 | static void |
5ca51e19 | 1175 | server_init_dispatch_13(void) |
7368a6c8 | 1176 | { |
1177 | debug("server_init_dispatch_13"); | |
1178 | dispatch_init(NULL); | |
1179 | dispatch_set(SSH_CMSG_EOF, &server_input_eof); | |
1180 | dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); | |
1181 | dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); | |
1182 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); | |
1183 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); | |
1184 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); | |
1185 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); | |
1186 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
1187 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); | |
1188 | } | |
396c147e | 1189 | static void |
5ca51e19 | 1190 | server_init_dispatch_15(void) |
7368a6c8 | 1191 | { |
1192 | server_init_dispatch_13(); | |
1193 | debug("server_init_dispatch_15"); | |
1194 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); | |
1195 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); | |
1196 | } | |
396c147e | 1197 | static void |
5ca51e19 | 1198 | server_init_dispatch(void) |
7368a6c8 | 1199 | { |
e78a59f5 | 1200 | if (compat20) |
1201 | server_init_dispatch_20(); | |
1202 | else if (compat13) | |
7368a6c8 | 1203 | server_init_dispatch_13(); |
1204 | else | |
1205 | server_init_dispatch_15(); | |
1206 | } |