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