blob: d1290ff228b1c450422072671da976923c6ca0e2 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Server main loop for handling the interactive session.
Damien Millere4340be2000-09-16 13:29:08 +11006 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Damien Millerefb4afe2000-04-12 18:45:05 +100013 * SSH2 support by Markus Friedl.
Ben Lindstrom92a2e382001-03-05 06:59:27 +000014 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Ben Lindstrom7bb8b492001-03-17 00:47:54 +000038RCSID("$OpenBSD: serverloop.c,v 1.55 2001/03/16 19:06:29 markus Exp $");
Damien Miller69b69aa2000-10-28 14:19:58 +110039
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041#include "packet.h"
42#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000045#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "channels.h"
Damien Millerb38eff82000-04-01 11:09:21 +100047#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "ssh1.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "ssh2.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000050#include "auth.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "session.h"
Damien Millerb38eff82000-04-01 11:09:21 +100052#include "dispatch.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100053#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "serverloop.h"
55#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller50a41ed2000-10-16 12:14:42 +110057extern ServerOptions options;
58
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059static Buffer stdin_buffer; /* Buffer for stdin data. */
60static Buffer stdout_buffer; /* Buffer for stdout data. */
61static Buffer stderr_buffer; /* Buffer for stderr data. */
62static int fdin; /* Descriptor for stdin (for writing) */
63static int fdout; /* Descriptor for stdout (for reading);
64 May be same number as fdin. */
65static int fderr; /* Descriptor for stderr. May be -1. */
66static long stdin_bytes = 0; /* Number of bytes written to stdin. */
67static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */
68static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */
69static long fdout_bytes = 0; /* Number of stdout bytes read from program. */
70static int stdin_eof = 0; /* EOF message received from client. */
71static int fdout_eof = 0; /* EOF encountered reading from fdout. */
72static int fderr_eof = 0; /* EOF encountered readung from fderr. */
Damien Miller225736c2001-02-19 21:51:08 +110073static int fdin_is_tty = 0; /* fdin points to a tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074static int connection_in; /* Connection to client (input). */
75static int connection_out; /* Connection to client (output). */
Ben Lindstrom46c16222000-12-22 01:43:59 +000076static u_int buffer_high;/* "Soft" max buffer size. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller5428f641999-11-25 11:54:57 +110078/*
79 * This SIGCHLD kludge is used to detect when the child exits. The server
80 * will exit after that, as soon as forwarded connections have terminated.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller166fca82000-04-20 07:42:21 +100083static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084static volatile int child_terminated; /* The child has terminated. */
85static volatile int child_wait_status; /* Status from wait(). */
86
Damien Millerb38eff82000-04-01 11:09:21 +100087void server_init_dispatch(void);
88
Damien Miller4af51302000-04-16 11:18:38 +100089void
Damien Miller95def091999-11-25 00:26:21 +110090sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
Damien Miller95def091999-11-25 00:26:21 +110092 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100093 pid_t wait_pid;
94
Damien Miller95def091999-11-25 00:26:21 +110095 debug("Received SIGCHLD.");
96 wait_pid = wait((int *) &child_wait_status);
97 if (wait_pid != -1) {
98 if (wait_pid != child_pid)
99 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
100 wait_pid, child_pid);
101 if (WIFEXITED(child_wait_status) ||
Damien Miller43dc8da2000-11-29 15:55:17 +1100102 WIFSIGNALED(child_wait_status))
Damien Miller95def091999-11-25 00:26:21 +1100103 child_terminated = 1;
104 }
105 signal(SIGCHLD, sigchld_handler);
106 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107}
Damien Miller4af51302000-04-16 11:18:38 +1000108void
Damien Millerefb4afe2000-04-12 18:45:05 +1000109sigchld_handler2(int sig)
110{
111 int save_errno = errno;
112 debug("Received SIGCHLD.");
113 child_terminated = 1;
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000114 mysignal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000115 errno = save_errno;
116}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller95def091999-11-25 00:26:21 +1100118/*
Damien Miller95def091999-11-25 00:26:21 +1100119 * Make packets from buffered stderr data, and buffer it for sending
120 * to the client.
121 */
Damien Miller4af51302000-04-16 11:18:38 +1000122void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000123make_packets_from_stderr_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124{
Damien Miller95def091999-11-25 00:26:21 +1100125 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Send buffered stderr data to the client. */
128 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100129 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100130 len = buffer_len(&stderr_buffer);
131 if (packet_is_interactive()) {
132 if (len > 512)
133 len = 512;
134 } else {
135 /* Keep the packets at reasonable size. */
136 if (len > packet_get_maxsize())
137 len = packet_get_maxsize();
138 }
139 packet_start(SSH_SMSG_STDERR_DATA);
140 packet_put_string(buffer_ptr(&stderr_buffer), len);
141 packet_send();
142 buffer_consume(&stderr_buffer, len);
143 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Miller95def091999-11-25 00:26:21 +1100147/*
148 * Make packets from buffered stdout data, and buffer it for sending to the
149 * client.
150 */
Damien Miller4af51302000-04-16 11:18:38 +1000151void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000152make_packets_from_stdout_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller95def091999-11-25 00:26:21 +1100156 /* Send buffered stdout data to the client. */
157 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100158 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100159 len = buffer_len(&stdout_buffer);
160 if (packet_is_interactive()) {
161 if (len > 512)
162 len = 512;
163 } else {
164 /* Keep the packets at reasonable size. */
165 if (len > packet_get_maxsize())
Kevin Stevesef4eea92001-02-05 12:42:17 +0000166 len = packet_get_maxsize();
Damien Miller95def091999-11-25 00:26:21 +1100167 }
168 packet_start(SSH_SMSG_STDOUT_DATA);
169 packet_put_string(buffer_ptr(&stdout_buffer), len);
170 packet_send();
171 buffer_consume(&stdout_buffer, len);
172 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Miller95def091999-11-25 00:26:21 +1100176/*
177 * Sleep in select() until we can do something. This will initialize the
178 * select masks. Upon return, the masks will indicate which descriptors
179 * have data or can accept data. Optionally, a maximum time can be specified
180 * for the duration of the wait (0 = infinite).
181 */
Damien Miller4af51302000-04-16 11:18:38 +1000182void
Damien Miller5e953212001-01-30 09:14:00 +1100183wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
184 u_int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller95def091999-11-25 00:26:21 +1100186 struct timeval tv, *tvp;
187 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller95def091999-11-25 00:26:21 +1100189 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190retry_select:
191
Damien Miller5e953212001-01-30 09:14:00 +1100192 /* Allocate and update select() masks for channel descriptors. */
193 channel_prepare_select(readsetp, writesetp, maxfdp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Millerefb4afe2000-04-12 18:45:05 +1000195 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000196 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000197 if (channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100198 FD_SET(connection_in, *readsetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000199 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000200 /*
201 * Read packets from the client unless we have too much
202 * buffered stdin or channel data.
203 */
204 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000205 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100206 FD_SET(connection_in, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000207 /*
208 * If there is not too much data already buffered going to
209 * the client, try to get some more data from the program.
210 */
211 if (packet_not_very_much_data_to_write()) {
212 if (!fdout_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100213 FD_SET(fdout, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000214 if (!fderr_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100215 FD_SET(fderr, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000216 }
217 /*
218 * If we have buffered data, try to write some of that data
219 * to the program.
220 */
221 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100222 FD_SET(fdin, *writesetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000223 }
Damien Miller95def091999-11-25 00:26:21 +1100224
Damien Miller5428f641999-11-25 11:54:57 +1100225 /*
226 * If we have buffered packet data going to the client, mark that
227 * descriptor.
228 */
Damien Miller95def091999-11-25 00:26:21 +1100229 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100230 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100231
Damien Miller5428f641999-11-25 11:54:57 +1100232 /*
233 * If child has terminated and there is enough buffer space to read
234 * from it, then read as much as is available and exit.
235 */
Damien Miller95def091999-11-25 00:26:21 +1100236 if (child_terminated && packet_not_very_much_data_to_write())
237 if (max_time_milliseconds == 0)
238 max_time_milliseconds = 100;
239
240 if (max_time_milliseconds == 0)
241 tvp = NULL;
242 else {
243 tv.tv_sec = max_time_milliseconds / 1000;
244 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
245 tvp = &tv;
246 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000247 if (tvp!=NULL)
Ben Lindstromf4c73112001-03-05 05:58:23 +0000248 debug3("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100249
250 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100251 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100252
253 if (ret < 0) {
254 if (errno != EINTR)
255 error("select: %.100s", strerror(errno));
256 else
257 goto retry_select;
258 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259}
260
Damien Miller95def091999-11-25 00:26:21 +1100261/*
262 * Processes input from the client and the program. Input data is stored
263 * in buffers and processed later.
264 */
Damien Miller4af51302000-04-16 11:18:38 +1000265void
Damien Miller95def091999-11-25 00:26:21 +1100266process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267{
Damien Miller95def091999-11-25 00:26:21 +1100268 int len;
269 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller95def091999-11-25 00:26:21 +1100271 /* Read and buffer any input data from the client. */
272 if (FD_ISSET(connection_in, readset)) {
273 len = read(connection_in, buf, sizeof(buf));
274 if (len == 0) {
275 verbose("Connection closed by remote host.");
276 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000277 } else if (len < 0) {
278 if (errno != EINTR && errno != EAGAIN) {
279 verbose("Read error from remote host: %.100s", strerror(errno));
280 fatal_cleanup();
281 }
282 } else {
283 /* Buffer any received data. */
284 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100285 }
Damien Miller95def091999-11-25 00:26:21 +1100286 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000287 if (compat20)
288 return;
289
Damien Miller95def091999-11-25 00:26:21 +1100290 /* Read and buffer any available stdout data from the program. */
291 if (!fdout_eof && FD_ISSET(fdout, readset)) {
292 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000293 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
294 /* do nothing */
295 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100296 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000297 } else {
Damien Miller95def091999-11-25 00:26:21 +1100298 buffer_append(&stdout_buffer, buf, len);
299 fdout_bytes += len;
300 }
301 }
302 /* Read and buffer any available stderr data from the program. */
303 if (!fderr_eof && FD_ISSET(fderr, readset)) {
304 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000305 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
306 /* do nothing */
307 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100308 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000309 } else {
Damien Miller95def091999-11-25 00:26:21 +1100310 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000311 }
Damien Miller95def091999-11-25 00:26:21 +1100312 }
313}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller95def091999-11-25 00:26:21 +1100315/*
316 * Sends data from internal buffers to client program stdin.
317 */
Damien Miller4af51302000-04-16 11:18:38 +1000318void
Damien Miller95def091999-11-25 00:26:21 +1100319process_output(fd_set * writeset)
320{
Ben Lindstromaa630de2001-02-10 23:44:47 +0000321 struct termios tio;
Damien Miller95def091999-11-25 00:26:21 +1100322 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323
Damien Miller95def091999-11-25 00:26:21 +1100324 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000325 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100326 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100327 buffer_len(&stdin_buffer));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000328 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
329 /* do nothing */
330 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100331#ifdef USE_PIPES
332 close(fdin);
333#else
Damien Millerb38eff82000-04-01 11:09:21 +1000334 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100335 close(fdin);
336 else
337 shutdown(fdin, SHUT_WR); /* We will no longer send. */
338#endif
339 fdin = -1;
340 } else {
Ben Lindstromaa630de2001-02-10 23:44:47 +0000341 /* Successful write. */
Damien Miller225736c2001-02-19 21:51:08 +1100342 if (fdin_is_tty && tcgetattr(fdin, &tio) == 0 &&
Damien Miller79438cc2001-02-16 12:34:57 +1100343 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
Kevin Stevesb7f036f2001-02-15 17:27:15 +0000344 /*
345 * Simulate echo to reduce the impact of
346 * traffic analysis
347 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000348 packet_send_ignore(len);
Ben Lindstromaa630de2001-02-10 23:44:47 +0000349 packet_send();
350 }
351 /* Consume the data from the buffer. */
Damien Miller95def091999-11-25 00:26:21 +1100352 buffer_consume(&stdin_buffer, len);
353 /* Update the count of bytes written to the program. */
354 stdin_bytes += len;
355 }
356 }
357 /* Send any buffered packet data to the client. */
358 if (FD_ISSET(connection_out, writeset))
359 packet_write_poll();
360}
361
362/*
363 * Wait until all buffered output has been sent to the client.
364 * This is used when the program terminates.
365 */
Damien Miller4af51302000-04-16 11:18:38 +1000366void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000367drain_output(void)
Damien Miller95def091999-11-25 00:26:21 +1100368{
369 /* Send any buffered stdout data to the client. */
370 if (buffer_len(&stdout_buffer) > 0) {
371 packet_start(SSH_SMSG_STDOUT_DATA);
372 packet_put_string(buffer_ptr(&stdout_buffer),
373 buffer_len(&stdout_buffer));
374 packet_send();
375 /* Update the count of sent bytes. */
376 stdout_bytes += buffer_len(&stdout_buffer);
377 }
378 /* Send any buffered stderr data to the client. */
379 if (buffer_len(&stderr_buffer) > 0) {
380 packet_start(SSH_SMSG_STDERR_DATA);
381 packet_put_string(buffer_ptr(&stderr_buffer),
382 buffer_len(&stderr_buffer));
383 packet_send();
384 /* Update the count of sent bytes. */
385 stderr_bytes += buffer_len(&stderr_buffer);
386 }
387 /* Wait until all buffered data has been written to the client. */
388 packet_write_wait();
389}
390
Damien Miller4af51302000-04-16 11:18:38 +1000391void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000392process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000393{
Damien Miller62cee002000-09-23 17:15:56 +1100394 dispatch_run(DISPATCH_NONBLOCK, NULL, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000395}
396
Damien Miller95def091999-11-25 00:26:21 +1100397/*
398 * Performs the interactive session. This handles data transmission between
399 * the client and the program. Note that the notion of stdin, stdout, and
400 * stderr in this function is sort of reversed: this function writes to
401 * stdin (of the child program), and reads from stdout and stderr (of the
402 * child program).
403 */
Damien Miller4af51302000-04-16 11:18:38 +1000404void
Damien Miller166fca82000-04-20 07:42:21 +1000405server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100406{
Damien Miller5e953212001-01-30 09:14:00 +1100407 fd_set *readset = NULL, *writeset = NULL;
408 int max_fd;
Damien Miller166fca82000-04-20 07:42:21 +1000409 int wait_status; /* Status returned by wait(). */
410 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100411 int waiting_termination = 0; /* Have displayed waiting close message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000412 u_int max_time_milliseconds;
413 u_int previous_stdout_buffer_bytes;
414 u_int stdout_buffer_bytes;
Damien Miller95def091999-11-25 00:26:21 +1100415 int type;
416
417 debug("Entering interactive session.");
418
419 /* Initialize the SIGCHLD kludge. */
420 child_pid = pid;
421 child_terminated = 0;
422 signal(SIGCHLD, sigchld_handler);
423
424 /* Initialize our global variables. */
425 fdin = fdin_arg;
426 fdout = fdout_arg;
427 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000428
429 /* nonblocking IO */
430 set_nonblock(fdin);
431 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000432 /* we don't have stderr for interactive terminal sessions, see below */
433 if (fderr != -1)
434 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000435
Damien Miller225736c2001-02-19 21:51:08 +1100436 if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin))
437 fdin_is_tty = 1;
438
Damien Miller95def091999-11-25 00:26:21 +1100439 connection_in = packet_get_connection_in();
440 connection_out = packet_get_connection_out();
441
442 previous_stdout_buffer_bytes = 0;
443
444 /* Set approximate I/O buffer size. */
445 if (packet_is_interactive())
446 buffer_high = 4096;
447 else
448 buffer_high = 64 * 1024;
449
450 /* Initialize max_fd to the maximum of the known file descriptors. */
Damien Miller5e953212001-01-30 09:14:00 +1100451 max_fd = MAX(fdin, fdout);
452 if (fderr != -1)
453 max_fd = MAX(max_fd, fderr);
454 max_fd = MAX(max_fd, connection_in);
455 max_fd = MAX(max_fd, connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100456
457 /* Initialize Initialize buffers. */
458 buffer_init(&stdin_buffer);
459 buffer_init(&stdout_buffer);
460 buffer_init(&stderr_buffer);
461
Damien Miller5428f641999-11-25 11:54:57 +1100462 /*
463 * If we have no separate fderr (which is the case when we have a pty
464 * - there we cannot make difference between data sent to stdout and
465 * stderr), indicate that we have seen an EOF from stderr. This way
466 * we don\'t need to check the descriptor everywhere.
467 */
Damien Miller95def091999-11-25 00:26:21 +1100468 if (fderr == -1)
469 fderr_eof = 1;
470
Damien Millerb38eff82000-04-01 11:09:21 +1000471 server_init_dispatch();
472
Damien Miller95def091999-11-25 00:26:21 +1100473 /* Main loop of the server for the interactive session mode. */
474 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100475
476 /* Process buffered packets from the client. */
477 process_buffered_input_packets();
478
Damien Miller5428f641999-11-25 11:54:57 +1100479 /*
480 * If we have received eof, and there is no more pending
481 * input data, cause a real eof by closing fdin.
482 */
Damien Miller95def091999-11-25 00:26:21 +1100483 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
484#ifdef USE_PIPES
485 close(fdin);
486#else
Damien Millerb38eff82000-04-01 11:09:21 +1000487 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100488 close(fdin);
489 else
490 shutdown(fdin, SHUT_WR); /* We will no longer send. */
491#endif
492 fdin = -1;
493 }
Damien Miller5428f641999-11-25 11:54:57 +1100494 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100495 make_packets_from_stderr_data();
496
Damien Miller5428f641999-11-25 11:54:57 +1100497 /*
498 * Make packets from buffered stdout data to send to the
499 * client. If there is very little to send, this arranges to
500 * not send them now, but to wait a short while to see if we
501 * are getting more data. This is necessary, as some systems
502 * wake up readers from a pty after each separate character.
503 */
Damien Miller95def091999-11-25 00:26:21 +1100504 max_time_milliseconds = 0;
505 stdout_buffer_bytes = buffer_len(&stdout_buffer);
506 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
507 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
508 /* try again after a while */
509 max_time_milliseconds = 10;
510 } else {
511 /* Send it now. */
512 make_packets_from_stdout_data();
513 }
514 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
515
516 /* Send channel data to the client. */
517 if (packet_not_very_much_data_to_write())
518 channel_output_poll();
519
Damien Miller5428f641999-11-25 11:54:57 +1100520 /*
521 * Bail out of the loop if the program has closed its output
522 * descriptors, and we have no more data to send to the
523 * client, and there is no pending buffered data.
524 */
Damien Miller43dc8da2000-11-29 15:55:17 +1100525 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
526 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100527 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000528 break;
Damien Miller95def091999-11-25 00:26:21 +1100529 if (!waiting_termination) {
530 const char *s = "Waiting for forwarded connections to terminate...\r\n";
531 char *cp;
532 waiting_termination = 1;
533 buffer_append(&stderr_buffer, s, strlen(s));
534
535 /* Display list of open channels. */
536 cp = channel_open_message();
537 buffer_append(&stderr_buffer, cp, strlen(cp));
538 xfree(cp);
539 }
540 }
541 /* Sleep in select() until we can do something. */
Damien Miller5e953212001-01-30 09:14:00 +1100542 wait_until_can_do_something(&readset, &writeset, &max_fd,
543 max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100544
545 /* Process any channel events. */
Damien Miller5e953212001-01-30 09:14:00 +1100546 channel_after_select(readset, writeset);
Damien Miller95def091999-11-25 00:26:21 +1100547
548 /* Process input from the client and from program stdout/stderr. */
Damien Miller5e953212001-01-30 09:14:00 +1100549 process_input(readset);
Damien Miller95def091999-11-25 00:26:21 +1100550
551 /* Process output to the client and to program stdin. */
Damien Miller5e953212001-01-30 09:14:00 +1100552 process_output(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100553 }
Damien Miller5e953212001-01-30 09:14:00 +1100554 if (readset)
555 xfree(readset);
556 if (writeset)
557 xfree(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100558
Damien Miller95def091999-11-25 00:26:21 +1100559 /* Cleanup and termination code. */
560
561 /* Wait until all output has been sent to the client. */
562 drain_output();
563
564 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
565 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
566
567 /* Free and clear the buffers. */
568 buffer_free(&stdin_buffer);
569 buffer_free(&stdout_buffer);
570 buffer_free(&stderr_buffer);
571
572 /* Close the file descriptors. */
573 if (fdout != -1)
574 close(fdout);
575 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100577 if (fderr != -1)
578 close(fderr);
579 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100581 if (fdin != -1)
582 close(fdin);
583 fdin = -1;
584
585 /* Stop listening for channels; this removes unix domain sockets. */
586 channel_stop_listening();
587
588 /* Wait for the child to exit. Get its exit status. */
589 wait_pid = wait(&wait_status);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000590 if (wait_pid == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100591 /*
592 * It is possible that the wait was handled by SIGCHLD
593 * handler. This may result in either: this call
594 * returning with EINTR, or: this call returning ECHILD.
595 */
596 if (child_terminated)
597 wait_status = child_wait_status;
598 else
599 packet_disconnect("wait: %.100s", strerror(errno));
600 } else {
601 /* Check if it matches the process we forked. */
602 if (wait_pid != pid)
603 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100604 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100605 }
606
607 /* We no longer want our SIGCHLD handler to be called. */
608 signal(SIGCHLD, SIG_DFL);
609
610 /* Check if it exited normally. */
611 if (WIFEXITED(wait_status)) {
612 /* Yes, normal exit. Get exit status and send it to the client. */
613 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
614 packet_start(SSH_SMSG_EXITSTATUS);
615 packet_put_int(WEXITSTATUS(wait_status));
616 packet_send();
617 packet_write_wait();
618
Damien Miller5428f641999-11-25 11:54:57 +1100619 /*
620 * Wait for exit confirmation. Note that there might be
621 * other packets coming before it; however, the program has
622 * already died so we just ignore them. The client is
623 * supposed to respond with the confirmation when it receives
624 * the exit status.
625 */
Damien Miller95def091999-11-25 00:26:21 +1100626 do {
627 int plen;
628 type = packet_read(&plen);
629 }
630 while (type != SSH_CMSG_EXIT_CONFIRMATION);
631
632 debug("Received exit confirmation.");
633 return;
634 }
635 /* Check if the program terminated due to a signal. */
636 if (WIFSIGNALED(wait_status))
637 packet_disconnect("Command terminated on signal %d.",
638 WTERMSIG(wait_status));
639
640 /* Some weird exit cause. Just exit. */
641 packet_disconnect("wait returned status %04x.", wait_status);
642 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000643}
Damien Millerb38eff82000-04-01 11:09:21 +1000644
Damien Miller4af51302000-04-16 11:18:38 +1000645void
Damien Millerefb4afe2000-04-12 18:45:05 +1000646server_loop2(void)
647{
Damien Miller5e953212001-01-30 09:14:00 +1100648 fd_set *readset = NULL, *writeset = NULL;
649 int max_fd;
Damien Millerefb4afe2000-04-12 18:45:05 +1000650 int had_channel = 0;
651 int status;
652 pid_t pid;
653
654 debug("Entering interactive session for SSH2.");
655
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000656 mysignal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000657 child_terminated = 0;
658 connection_in = packet_get_connection_in();
659 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100660
661 max_fd = MAX(connection_in, connection_out);
662
Damien Millerefb4afe2000-04-12 18:45:05 +1000663 server_init_dispatch();
664
665 for (;;) {
666 process_buffered_input_packets();
667 if (!had_channel && channel_still_open())
668 had_channel = 1;
669 if (had_channel && !channel_still_open()) {
670 debug("!channel_still_open.");
671 break;
672 }
673 if (packet_not_very_much_data_to_write())
674 channel_output_poll();
Damien Miller5e953212001-01-30 09:14:00 +1100675 wait_until_can_do_something(&readset, &writeset, &max_fd, 0);
Damien Miller43dc8da2000-11-29 15:55:17 +1100676 if (child_terminated) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000677 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
678 session_close_by_pid(pid, status);
679 child_terminated = 0;
680 }
Damien Miller5e953212001-01-30 09:14:00 +1100681 channel_after_select(readset, writeset);
682 process_input(readset);
683 process_output(writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000684 }
Damien Miller5e953212001-01-30 09:14:00 +1100685 if (readset)
686 xfree(readset);
687 if (writeset)
688 xfree(writeset);
689
Damien Millerefb4afe2000-04-12 18:45:05 +1000690 signal(SIGCHLD, SIG_DFL);
691 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
692 session_close_by_pid(pid, status);
693 channel_stop_listening();
694}
695
Damien Millerb38eff82000-04-01 11:09:21 +1000696void
Damien Miller62cee002000-09-23 17:15:56 +1100697server_input_stdin_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000698{
699 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000700 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000701
702 /* Stdin data from the client. Append it to the buffer. */
703 /* Ignore any data if the client has closed stdin. */
704 if (fdin == -1)
705 return;
706 data = packet_get_string(&data_len);
707 packet_integrity_check(plen, (4 + data_len), type);
708 buffer_append(&stdin_buffer, data, data_len);
709 memset(data, 0, data_len);
710 xfree(data);
711}
712
713void
Damien Miller62cee002000-09-23 17:15:56 +1100714server_input_eof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000715{
716 /*
717 * Eof from the client. The stdin descriptor to the
718 * program will be closed when all buffered data has
719 * drained.
720 */
721 debug("EOF received for stdin.");
722 packet_integrity_check(plen, 0, type);
723 stdin_eof = 1;
724}
725
726void
Damien Miller62cee002000-09-23 17:15:56 +1100727server_input_window_size(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000728{
729 int row = packet_get_int();
730 int col = packet_get_int();
731 int xpixel = packet_get_int();
732 int ypixel = packet_get_int();
733
734 debug("Window change received.");
735 packet_integrity_check(plen, 4 * 4, type);
736 if (fdin != -1)
737 pty_change_window_size(fdin, row, col, xpixel, ypixel);
738}
739
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740Channel *
741server_request_direct_tcpip(char *ctype)
Damien Millerefb4afe2000-04-12 18:45:05 +1000742{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100743 int sock, newch;
Damien Miller4af51302000-04-16 11:18:38 +1000744 char *target, *originator;
745 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000746
Damien Miller4af51302000-04-16 11:18:38 +1000747 target = packet_get_string(NULL);
748 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000749 originator = packet_get_string(NULL);
750 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000751 packet_done();
Damien Millerb1715dc2000-05-30 13:44:51 +1000752
Damien Miller0bc1bd82000-11-13 22:57:25 +1100753 debug("server_request_direct_tcpip: originator %s port %d, target %s port %d",
Damien Millerb1715dc2000-05-30 13:44:51 +1000754 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000755
Damien Millerefb4afe2000-04-12 18:45:05 +1000756 /* XXX check permission */
Damien Miller4af51302000-04-16 11:18:38 +1000757 sock = channel_connect_to(target, target_port);
758 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000759 xfree(originator);
760 if (sock < 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100761 return NULL;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000762 newch = channel_new(ctype, SSH_CHANNEL_CONNECTING,
Damien Millere4340be2000-09-16 13:29:08 +1100763 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +1100764 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 return (newch >= 0) ? channel_lookup(newch) : NULL;
766}
767
768Channel *
769server_request_session(char *ctype)
770{
771 int newch;
772
773 debug("input_session_request");
774 packet_done();
775 /*
776 * A server session has no fd to read or write until a
777 * CHANNEL_REQUEST for a shell is made, so we set the type to
778 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
779 * CHANNEL_REQUEST messages is registered.
780 */
781 newch = channel_new(ctype, SSH_CHANNEL_LARVAL,
782 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
783 0, xstrdup("server-session"), 1);
784 if (session_open(newch) == 1) {
785 channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST,
786 session_input_channel_req, (void *)0);
787 channel_register_cleanup(newch, session_close_by_channel);
788 return channel_lookup(newch);
789 } else {
790 debug("session open failed, free channel %d", newch);
791 channel_free(newch);
792 }
793 return NULL;
Damien Millerefb4afe2000-04-12 18:45:05 +1000794}
795
Damien Miller4af51302000-04-16 11:18:38 +1000796void
Damien Miller62cee002000-09-23 17:15:56 +1100797server_input_channel_open(int type, int plen, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000798{
799 Channel *c = NULL;
800 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000801 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000802 int rchan;
803 int rmaxpack;
804 int rwindow;
805
806 ctype = packet_get_string(&len);
807 rchan = packet_get_int();
808 rwindow = packet_get_int();
809 rmaxpack = packet_get_int();
810
Damien Miller62cee002000-09-23 17:15:56 +1100811 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000812 ctype, rchan, rwindow, rmaxpack);
813
814 if (strcmp(ctype, "session") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 c = server_request_session(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000816 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817 c = server_request_direct_tcpip(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000818 }
819 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000821 c->remote_id = rchan;
822 c->remote_window = rwindow;
823 c->remote_maxpacket = rmaxpack;
824
825 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
826 packet_put_int(c->remote_id);
827 packet_put_int(c->self);
828 packet_put_int(c->local_window);
829 packet_put_int(c->local_maxpacket);
830 packet_send();
831 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000833 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
834 packet_put_int(rchan);
835 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
836 packet_put_cstring("bla bla");
837 packet_put_cstring("");
838 packet_send();
839 }
840 xfree(ctype);
841}
842
Kevin Stevesef4eea92001-02-05 12:42:17 +0000843void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100844server_input_global_request(int type, int plen, void *ctxt)
845{
846 char *rtype;
847 int want_reply;
848 int success = 0;
849
850 rtype = packet_get_string(NULL);
851 want_reply = packet_get_char();
852 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000853
Ben Lindstrom7bb8b492001-03-17 00:47:54 +0000854 /* -R style forwarding */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100855 if (strcmp(rtype, "tcpip-forward") == 0) {
856 struct passwd *pw;
857 char *listen_address;
858 u_short listen_port;
859
860 pw = auth_get_user();
861 if (pw == NULL)
862 fatal("server_input_global_request: no user");
863 listen_address = packet_get_string(NULL); /* XXX currently ignored */
864 listen_port = (u_short)packet_get_int();
865 debug("server_input_global_request: tcpip-forward listen %s port %d",
866 listen_address, listen_port);
867
868 /* check permissions */
869 if (!options.allow_tcp_forwarding ||
870 no_port_forwarding_flag ||
871 (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) {
872 success = 0;
873 packet_send_debug("Server has disabled port forwarding.");
874 } else {
875 /* Start listening on the port */
Kevin Steves12057502001-02-05 14:54:34 +0000876 success = channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100877 listen_address, listen_port,
878 /*unspec host_to_connect*/ "<unspec host>",
879 /*unspec port_to_connect*/ 0,
880 options.gateway_ports, /*remote*/ 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100881 }
882 xfree(listen_address);
883 }
884 if (want_reply) {
885 packet_start(success ?
886 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
887 packet_send();
888 packet_write_wait();
889 }
890 xfree(rtype);
891}
892
Damien Miller4af51302000-04-16 11:18:38 +1000893void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000894server_init_dispatch_20(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000895{
896 debug("server_init_dispatch_20");
897 dispatch_init(&dispatch_protocol_error);
898 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
899 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
900 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
901 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
902 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
903 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
904 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
905 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
906 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100907 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Damien Millerefb4afe2000-04-12 18:45:05 +1000908}
Damien Miller4af51302000-04-16 11:18:38 +1000909void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000910server_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000911{
912 debug("server_init_dispatch_13");
913 dispatch_init(NULL);
914 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
915 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
916 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
917 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
918 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
919 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
920 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
921 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
922 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
923}
Damien Miller4af51302000-04-16 11:18:38 +1000924void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000925server_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000926{
927 server_init_dispatch_13();
928 debug("server_init_dispatch_15");
929 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
930 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
931}
Damien Miller4af51302000-04-16 11:18:38 +1000932void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000933server_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000934{
Damien Millerefb4afe2000-04-12 18:45:05 +1000935 if (compat20)
936 server_init_dispatch_20();
937 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000938 server_init_dispatch_13();
939 else
940 server_init_dispatch_15();
941}