blob: 8fc94db4501a603a4adcf0f7e6853c4d9403fa00 [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.
14 * 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 Lindstromaa630de2001-02-10 23:44:47 +000038RCSID("$OpenBSD: serverloop.c,v 1.47 2001/02/08 23:11:42 dugsong 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"
45#include "pty.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. */
73static int connection_in; /* Connection to client (input). */
74static int connection_out; /* Connection to client (output). */
Ben Lindstrom46c16222000-12-22 01:43:59 +000075static u_int buffer_high;/* "Soft" max buffer size. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * This SIGCHLD kludge is used to detect when the child exits. The server
79 * will exit after that, as soon as forwarded connections have terminated.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller166fca82000-04-20 07:42:21 +100082static pid_t child_pid; /* Pid of the child. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083static volatile int child_terminated; /* The child has terminated. */
84static volatile int child_wait_status; /* Status from wait(). */
85
Damien Millerb38eff82000-04-01 11:09:21 +100086void server_init_dispatch(void);
87
Damien Miller4af51302000-04-16 11:18:38 +100088void
Damien Miller95def091999-11-25 00:26:21 +110089sigchld_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Damien Miller95def091999-11-25 00:26:21 +110091 int save_errno = errno;
Damien Miller166fca82000-04-20 07:42:21 +100092 pid_t wait_pid;
93
Damien Miller95def091999-11-25 00:26:21 +110094 debug("Received SIGCHLD.");
95 wait_pid = wait((int *) &child_wait_status);
96 if (wait_pid != -1) {
97 if (wait_pid != child_pid)
98 error("Strange, got SIGCHLD and wait returned pid %d but child is %d",
99 wait_pid, child_pid);
100 if (WIFEXITED(child_wait_status) ||
Damien Miller43dc8da2000-11-29 15:55:17 +1100101 WIFSIGNALED(child_wait_status))
Damien Miller95def091999-11-25 00:26:21 +1100102 child_terminated = 1;
103 }
104 signal(SIGCHLD, sigchld_handler);
105 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106}
Damien Miller4af51302000-04-16 11:18:38 +1000107void
Damien Millerefb4afe2000-04-12 18:45:05 +1000108sigchld_handler2(int sig)
109{
110 int save_errno = errno;
111 debug("Received SIGCHLD.");
112 child_terminated = 1;
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000113 mysignal(SIGCHLD, sigchld_handler2);
Damien Millerefb4afe2000-04-12 18:45:05 +1000114 errno = save_errno;
115}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117/*
Damien Miller95def091999-11-25 00:26:21 +1100118 * Make packets from buffered stderr data, and buffer it for sending
119 * to the client.
120 */
Damien Miller4af51302000-04-16 11:18:38 +1000121void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000122make_packets_from_stderr_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123{
Damien Miller95def091999-11-25 00:26:21 +1100124 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller95def091999-11-25 00:26:21 +1100126 /* Send buffered stderr data to the client. */
127 while (buffer_len(&stderr_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100128 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100129 len = buffer_len(&stderr_buffer);
130 if (packet_is_interactive()) {
131 if (len > 512)
132 len = 512;
133 } else {
134 /* Keep the packets at reasonable size. */
135 if (len > packet_get_maxsize())
136 len = packet_get_maxsize();
137 }
138 packet_start(SSH_SMSG_STDERR_DATA);
139 packet_put_string(buffer_ptr(&stderr_buffer), len);
140 packet_send();
141 buffer_consume(&stderr_buffer, len);
142 stderr_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Miller95def091999-11-25 00:26:21 +1100146/*
147 * Make packets from buffered stdout data, and buffer it for sending to the
148 * client.
149 */
Damien Miller4af51302000-04-16 11:18:38 +1000150void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000151make_packets_from_stdout_data(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 /* Send buffered stdout data to the client. */
156 while (buffer_len(&stdout_buffer) > 0 &&
Damien Miller037a0dc1999-12-07 15:38:31 +1100157 packet_not_very_much_data_to_write()) {
Damien Miller95def091999-11-25 00:26:21 +1100158 len = buffer_len(&stdout_buffer);
159 if (packet_is_interactive()) {
160 if (len > 512)
161 len = 512;
162 } else {
163 /* Keep the packets at reasonable size. */
164 if (len > packet_get_maxsize())
Kevin Stevesef4eea92001-02-05 12:42:17 +0000165 len = packet_get_maxsize();
Damien Miller95def091999-11-25 00:26:21 +1100166 }
167 packet_start(SSH_SMSG_STDOUT_DATA);
168 packet_put_string(buffer_ptr(&stdout_buffer), len);
169 packet_send();
170 buffer_consume(&stdout_buffer, len);
171 stdout_bytes += len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Miller95def091999-11-25 00:26:21 +1100175/*
176 * Sleep in select() until we can do something. This will initialize the
177 * select masks. Upon return, the masks will indicate which descriptors
178 * have data or can accept data. Optionally, a maximum time can be specified
179 * for the duration of the wait (0 = infinite).
180 */
Damien Miller4af51302000-04-16 11:18:38 +1000181void
Damien Miller5e953212001-01-30 09:14:00 +1100182wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
183 u_int max_time_milliseconds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184{
Damien Miller95def091999-11-25 00:26:21 +1100185 struct timeval tv, *tvp;
186 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Miller95def091999-11-25 00:26:21 +1100188 /* When select fails we restart from here. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189retry_select:
190
Damien Miller5e953212001-01-30 09:14:00 +1100191 /* Allocate and update select() masks for channel descriptors. */
192 channel_prepare_select(readsetp, writesetp, maxfdp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193
Damien Millerefb4afe2000-04-12 18:45:05 +1000194 if (compat20) {
Damien Millere247cc42000-05-07 12:03:14 +1000195 /* wrong: bad condition XXX */
Damien Millerefb4afe2000-04-12 18:45:05 +1000196 if (channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100197 FD_SET(connection_in, *readsetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000198 } else {
Damien Millerb1715dc2000-05-30 13:44:51 +1000199 /*
200 * Read packets from the client unless we have too much
201 * buffered stdin or channel data.
202 */
203 if (buffer_len(&stdin_buffer) < buffer_high &&
Damien Millerefb4afe2000-04-12 18:45:05 +1000204 channel_not_very_much_buffered_data())
Damien Miller5e953212001-01-30 09:14:00 +1100205 FD_SET(connection_in, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000206 /*
207 * If there is not too much data already buffered going to
208 * the client, try to get some more data from the program.
209 */
210 if (packet_not_very_much_data_to_write()) {
211 if (!fdout_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100212 FD_SET(fdout, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000213 if (!fderr_eof)
Damien Miller5e953212001-01-30 09:14:00 +1100214 FD_SET(fderr, *readsetp);
Damien Millerb1715dc2000-05-30 13:44:51 +1000215 }
216 /*
217 * If we have buffered data, try to write some of that data
218 * to the program.
219 */
220 if (fdin != -1 && buffer_len(&stdin_buffer) > 0)
Damien Miller5e953212001-01-30 09:14:00 +1100221 FD_SET(fdin, *writesetp);
Damien Millerefb4afe2000-04-12 18:45:05 +1000222 }
Damien Miller95def091999-11-25 00:26:21 +1100223
Damien Miller5428f641999-11-25 11:54:57 +1100224 /*
225 * If we have buffered packet data going to the client, mark that
226 * descriptor.
227 */
Damien Miller95def091999-11-25 00:26:21 +1100228 if (packet_have_data_to_write())
Damien Miller5e953212001-01-30 09:14:00 +1100229 FD_SET(connection_out, *writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100230
Damien Miller5428f641999-11-25 11:54:57 +1100231 /*
232 * If child has terminated and there is enough buffer space to read
233 * from it, then read as much as is available and exit.
234 */
Damien Miller95def091999-11-25 00:26:21 +1100235 if (child_terminated && packet_not_very_much_data_to_write())
236 if (max_time_milliseconds == 0)
237 max_time_milliseconds = 100;
238
239 if (max_time_milliseconds == 0)
240 tvp = NULL;
241 else {
242 tv.tv_sec = max_time_milliseconds / 1000;
243 tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
244 tvp = &tv;
245 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000246 if (tvp!=NULL)
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000247 debug2("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100248
249 /* Wait for something to happen, or the timeout to expire. */
Damien Miller5e953212001-01-30 09:14:00 +1100250 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
Damien Miller95def091999-11-25 00:26:21 +1100251
252 if (ret < 0) {
253 if (errno != EINTR)
254 error("select: %.100s", strerror(errno));
255 else
256 goto retry_select;
257 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258}
259
Damien Miller95def091999-11-25 00:26:21 +1100260/*
261 * Processes input from the client and the program. Input data is stored
262 * in buffers and processed later.
263 */
Damien Miller4af51302000-04-16 11:18:38 +1000264void
Damien Miller95def091999-11-25 00:26:21 +1100265process_input(fd_set * readset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266{
Damien Miller95def091999-11-25 00:26:21 +1100267 int len;
268 char buf[16384];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller95def091999-11-25 00:26:21 +1100270 /* Read and buffer any input data from the client. */
271 if (FD_ISSET(connection_in, readset)) {
272 len = read(connection_in, buf, sizeof(buf));
273 if (len == 0) {
274 verbose("Connection closed by remote host.");
275 fatal_cleanup();
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000276 } else if (len < 0) {
277 if (errno != EINTR && errno != EAGAIN) {
278 verbose("Read error from remote host: %.100s", strerror(errno));
279 fatal_cleanup();
280 }
281 } else {
282 /* Buffer any received data. */
283 packet_process_incoming(buf, len);
Damien Miller95def091999-11-25 00:26:21 +1100284 }
Damien Miller95def091999-11-25 00:26:21 +1100285 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000286 if (compat20)
287 return;
288
Damien Miller95def091999-11-25 00:26:21 +1100289 /* Read and buffer any available stdout data from the program. */
290 if (!fdout_eof && FD_ISSET(fdout, readset)) {
291 len = read(fdout, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000292 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
293 /* do nothing */
294 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100295 fdout_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000296 } else {
Damien Miller95def091999-11-25 00:26:21 +1100297 buffer_append(&stdout_buffer, buf, len);
298 fdout_bytes += len;
299 }
300 }
301 /* Read and buffer any available stderr data from the program. */
302 if (!fderr_eof && FD_ISSET(fderr, readset)) {
303 len = read(fderr, buf, sizeof(buf));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000304 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
305 /* do nothing */
306 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100307 fderr_eof = 1;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000308 } else {
Damien Miller95def091999-11-25 00:26:21 +1100309 buffer_append(&stderr_buffer, buf, len);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000310 }
Damien Miller95def091999-11-25 00:26:21 +1100311 }
312}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller95def091999-11-25 00:26:21 +1100314/*
315 * Sends data from internal buffers to client program stdin.
316 */
Damien Miller4af51302000-04-16 11:18:38 +1000317void
Damien Miller95def091999-11-25 00:26:21 +1100318process_output(fd_set * writeset)
319{
Ben Lindstromaa630de2001-02-10 23:44:47 +0000320 struct termios tio;
Damien Miller95def091999-11-25 00:26:21 +1100321 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 /* Write buffered data to program stdin. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000324 if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) {
Damien Miller95def091999-11-25 00:26:21 +1100325 len = write(fdin, buffer_ptr(&stdin_buffer),
Damien Miller037a0dc1999-12-07 15:38:31 +1100326 buffer_len(&stdin_buffer));
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000327 if (len < 0 && (errno == EINTR || errno == EAGAIN)) {
328 /* do nothing */
329 } else if (len <= 0) {
Damien Miller95def091999-11-25 00:26:21 +1100330#ifdef USE_PIPES
331 close(fdin);
332#else
Damien Millerb38eff82000-04-01 11:09:21 +1000333 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100334 close(fdin);
335 else
336 shutdown(fdin, SHUT_WR); /* We will no longer send. */
337#endif
338 fdin = -1;
339 } else {
Ben Lindstromaa630de2001-02-10 23:44:47 +0000340 /* Successful write. */
341 if (tcgetattr(fdin, &tio) == 0 &&
342 !(tio.c_lflag & ECHO)) {
343 /* Simulate echo to reduce the impact of traffic analysis. */
344 packet_start(SSH_MSG_IGNORE);
345 memset(buffer_ptr(&stdin_buffer), 0, len);
346 packet_put_string(buffer_ptr(&stdin_buffer), len);
347 packet_send();
348 }
349 /* Consume the data from the buffer. */
Damien Miller95def091999-11-25 00:26:21 +1100350 buffer_consume(&stdin_buffer, len);
351 /* Update the count of bytes written to the program. */
352 stdin_bytes += len;
353 }
354 }
355 /* Send any buffered packet data to the client. */
356 if (FD_ISSET(connection_out, writeset))
357 packet_write_poll();
358}
359
360/*
361 * Wait until all buffered output has been sent to the client.
362 * This is used when the program terminates.
363 */
Damien Miller4af51302000-04-16 11:18:38 +1000364void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000365drain_output(void)
Damien Miller95def091999-11-25 00:26:21 +1100366{
367 /* Send any buffered stdout data to the client. */
368 if (buffer_len(&stdout_buffer) > 0) {
369 packet_start(SSH_SMSG_STDOUT_DATA);
370 packet_put_string(buffer_ptr(&stdout_buffer),
371 buffer_len(&stdout_buffer));
372 packet_send();
373 /* Update the count of sent bytes. */
374 stdout_bytes += buffer_len(&stdout_buffer);
375 }
376 /* Send any buffered stderr data to the client. */
377 if (buffer_len(&stderr_buffer) > 0) {
378 packet_start(SSH_SMSG_STDERR_DATA);
379 packet_put_string(buffer_ptr(&stderr_buffer),
380 buffer_len(&stderr_buffer));
381 packet_send();
382 /* Update the count of sent bytes. */
383 stderr_bytes += buffer_len(&stderr_buffer);
384 }
385 /* Wait until all buffered data has been written to the client. */
386 packet_write_wait();
387}
388
Damien Miller4af51302000-04-16 11:18:38 +1000389void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000390process_buffered_input_packets(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000391{
Damien Miller62cee002000-09-23 17:15:56 +1100392 dispatch_run(DISPATCH_NONBLOCK, NULL, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000393}
394
Damien Miller95def091999-11-25 00:26:21 +1100395/*
396 * Performs the interactive session. This handles data transmission between
397 * the client and the program. Note that the notion of stdin, stdout, and
398 * stderr in this function is sort of reversed: this function writes to
399 * stdin (of the child program), and reads from stdout and stderr (of the
400 * child program).
401 */
Damien Miller4af51302000-04-16 11:18:38 +1000402void
Damien Miller166fca82000-04-20 07:42:21 +1000403server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg)
Damien Miller95def091999-11-25 00:26:21 +1100404{
Damien Miller5e953212001-01-30 09:14:00 +1100405 fd_set *readset = NULL, *writeset = NULL;
406 int max_fd;
Damien Miller166fca82000-04-20 07:42:21 +1000407 int wait_status; /* Status returned by wait(). */
408 pid_t wait_pid; /* pid returned by wait(). */
Damien Miller95def091999-11-25 00:26:21 +1100409 int waiting_termination = 0; /* Have displayed waiting close message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000410 u_int max_time_milliseconds;
411 u_int previous_stdout_buffer_bytes;
412 u_int stdout_buffer_bytes;
Damien Miller95def091999-11-25 00:26:21 +1100413 int type;
414
415 debug("Entering interactive session.");
416
417 /* Initialize the SIGCHLD kludge. */
418 child_pid = pid;
419 child_terminated = 0;
420 signal(SIGCHLD, sigchld_handler);
Damien Millercf3888d2000-09-30 14:17:52 +1100421 signal(SIGPIPE, SIG_IGN);
Damien Miller95def091999-11-25 00:26:21 +1100422
423 /* Initialize our global variables. */
424 fdin = fdin_arg;
425 fdout = fdout_arg;
426 fderr = fderr_arg;
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000427
428 /* nonblocking IO */
429 set_nonblock(fdin);
430 set_nonblock(fdout);
Damien Miller7d6656c2000-05-20 15:22:36 +1000431 /* we don't have stderr for interactive terminal sessions, see below */
432 if (fderr != -1)
433 set_nonblock(fderr);
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000434
Damien Miller95def091999-11-25 00:26:21 +1100435 connection_in = packet_get_connection_in();
436 connection_out = packet_get_connection_out();
437
438 previous_stdout_buffer_bytes = 0;
439
440 /* Set approximate I/O buffer size. */
441 if (packet_is_interactive())
442 buffer_high = 4096;
443 else
444 buffer_high = 64 * 1024;
445
446 /* Initialize max_fd to the maximum of the known file descriptors. */
Damien Miller5e953212001-01-30 09:14:00 +1100447 max_fd = MAX(fdin, fdout);
448 if (fderr != -1)
449 max_fd = MAX(max_fd, fderr);
450 max_fd = MAX(max_fd, connection_in);
451 max_fd = MAX(max_fd, connection_out);
Damien Miller95def091999-11-25 00:26:21 +1100452
453 /* Initialize Initialize buffers. */
454 buffer_init(&stdin_buffer);
455 buffer_init(&stdout_buffer);
456 buffer_init(&stderr_buffer);
457
Damien Miller5428f641999-11-25 11:54:57 +1100458 /*
459 * If we have no separate fderr (which is the case when we have a pty
460 * - there we cannot make difference between data sent to stdout and
461 * stderr), indicate that we have seen an EOF from stderr. This way
462 * we don\'t need to check the descriptor everywhere.
463 */
Damien Miller95def091999-11-25 00:26:21 +1100464 if (fderr == -1)
465 fderr_eof = 1;
466
Damien Millerb38eff82000-04-01 11:09:21 +1000467 server_init_dispatch();
468
Damien Miller95def091999-11-25 00:26:21 +1100469 /* Main loop of the server for the interactive session mode. */
470 for (;;) {
Damien Miller95def091999-11-25 00:26:21 +1100471
472 /* Process buffered packets from the client. */
473 process_buffered_input_packets();
474
Damien Miller5428f641999-11-25 11:54:57 +1100475 /*
476 * If we have received eof, and there is no more pending
477 * input data, cause a real eof by closing fdin.
478 */
Damien Miller95def091999-11-25 00:26:21 +1100479 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
480#ifdef USE_PIPES
481 close(fdin);
482#else
Damien Millerb38eff82000-04-01 11:09:21 +1000483 if (fdin != fdout)
Damien Miller95def091999-11-25 00:26:21 +1100484 close(fdin);
485 else
486 shutdown(fdin, SHUT_WR); /* We will no longer send. */
487#endif
488 fdin = -1;
489 }
Damien Miller5428f641999-11-25 11:54:57 +1100490 /* Make packets from buffered stderr data to send to the client. */
Damien Miller95def091999-11-25 00:26:21 +1100491 make_packets_from_stderr_data();
492
Damien Miller5428f641999-11-25 11:54:57 +1100493 /*
494 * Make packets from buffered stdout data to send to the
495 * client. If there is very little to send, this arranges to
496 * not send them now, but to wait a short while to see if we
497 * are getting more data. This is necessary, as some systems
498 * wake up readers from a pty after each separate character.
499 */
Damien Miller95def091999-11-25 00:26:21 +1100500 max_time_milliseconds = 0;
501 stdout_buffer_bytes = buffer_len(&stdout_buffer);
502 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
503 stdout_buffer_bytes != previous_stdout_buffer_bytes) {
504 /* try again after a while */
505 max_time_milliseconds = 10;
506 } else {
507 /* Send it now. */
508 make_packets_from_stdout_data();
509 }
510 previous_stdout_buffer_bytes = buffer_len(&stdout_buffer);
511
512 /* Send channel data to the client. */
513 if (packet_not_very_much_data_to_write())
514 channel_output_poll();
515
Damien Miller5428f641999-11-25 11:54:57 +1100516 /*
517 * Bail out of the loop if the program has closed its output
518 * descriptors, and we have no more data to send to the
519 * client, and there is no pending buffered data.
520 */
Damien Miller43dc8da2000-11-29 15:55:17 +1100521 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
522 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100523 if (!channel_still_open())
Damien Millerb38eff82000-04-01 11:09:21 +1000524 break;
Damien Miller95def091999-11-25 00:26:21 +1100525 if (!waiting_termination) {
526 const char *s = "Waiting for forwarded connections to terminate...\r\n";
527 char *cp;
528 waiting_termination = 1;
529 buffer_append(&stderr_buffer, s, strlen(s));
530
531 /* Display list of open channels. */
532 cp = channel_open_message();
533 buffer_append(&stderr_buffer, cp, strlen(cp));
534 xfree(cp);
535 }
536 }
537 /* Sleep in select() until we can do something. */
Damien Miller5e953212001-01-30 09:14:00 +1100538 wait_until_can_do_something(&readset, &writeset, &max_fd,
539 max_time_milliseconds);
Damien Miller95def091999-11-25 00:26:21 +1100540
541 /* Process any channel events. */
Damien Miller5e953212001-01-30 09:14:00 +1100542 channel_after_select(readset, writeset);
Damien Miller95def091999-11-25 00:26:21 +1100543
544 /* Process input from the client and from program stdout/stderr. */
Damien Miller5e953212001-01-30 09:14:00 +1100545 process_input(readset);
Damien Miller95def091999-11-25 00:26:21 +1100546
547 /* Process output to the client and to program stdin. */
Damien Miller5e953212001-01-30 09:14:00 +1100548 process_output(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100549 }
Damien Miller5e953212001-01-30 09:14:00 +1100550 if (readset)
551 xfree(readset);
552 if (writeset)
553 xfree(writeset);
Damien Miller95def091999-11-25 00:26:21 +1100554
Damien Miller95def091999-11-25 00:26:21 +1100555 /* Cleanup and termination code. */
556
557 /* Wait until all output has been sent to the client. */
558 drain_output();
559
560 debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.",
561 stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes);
562
563 /* Free and clear the buffers. */
564 buffer_free(&stdin_buffer);
565 buffer_free(&stdout_buffer);
566 buffer_free(&stderr_buffer);
567
568 /* Close the file descriptors. */
569 if (fdout != -1)
570 close(fdout);
571 fdout = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572 fdout_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100573 if (fderr != -1)
574 close(fderr);
575 fderr = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 fderr_eof = 1;
Damien Miller95def091999-11-25 00:26:21 +1100577 if (fdin != -1)
578 close(fdin);
579 fdin = -1;
580
581 /* Stop listening for channels; this removes unix domain sockets. */
582 channel_stop_listening();
583
584 /* Wait for the child to exit. Get its exit status. */
585 wait_pid = wait(&wait_status);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000586 if (wait_pid == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100587 /*
588 * It is possible that the wait was handled by SIGCHLD
589 * handler. This may result in either: this call
590 * returning with EINTR, or: this call returning ECHILD.
591 */
592 if (child_terminated)
593 wait_status = child_wait_status;
594 else
595 packet_disconnect("wait: %.100s", strerror(errno));
596 } else {
597 /* Check if it matches the process we forked. */
598 if (wait_pid != pid)
599 error("Strange, wait returned pid %d, expected %d",
Damien Milleraae6c611999-12-06 11:47:28 +1100600 wait_pid, pid);
Damien Miller95def091999-11-25 00:26:21 +1100601 }
602
603 /* We no longer want our SIGCHLD handler to be called. */
604 signal(SIGCHLD, SIG_DFL);
605
606 /* Check if it exited normally. */
607 if (WIFEXITED(wait_status)) {
608 /* Yes, normal exit. Get exit status and send it to the client. */
609 debug("Command exited with status %d.", WEXITSTATUS(wait_status));
610 packet_start(SSH_SMSG_EXITSTATUS);
611 packet_put_int(WEXITSTATUS(wait_status));
612 packet_send();
613 packet_write_wait();
614
Damien Miller5428f641999-11-25 11:54:57 +1100615 /*
616 * Wait for exit confirmation. Note that there might be
617 * other packets coming before it; however, the program has
618 * already died so we just ignore them. The client is
619 * supposed to respond with the confirmation when it receives
620 * the exit status.
621 */
Damien Miller95def091999-11-25 00:26:21 +1100622 do {
623 int plen;
624 type = packet_read(&plen);
625 }
626 while (type != SSH_CMSG_EXIT_CONFIRMATION);
627
628 debug("Received exit confirmation.");
629 return;
630 }
631 /* Check if the program terminated due to a signal. */
632 if (WIFSIGNALED(wait_status))
633 packet_disconnect("Command terminated on signal %d.",
634 WTERMSIG(wait_status));
635
636 /* Some weird exit cause. Just exit. */
637 packet_disconnect("wait returned status %04x.", wait_status);
638 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639}
Damien Millerb38eff82000-04-01 11:09:21 +1000640
Damien Miller4af51302000-04-16 11:18:38 +1000641void
Damien Millerefb4afe2000-04-12 18:45:05 +1000642server_loop2(void)
643{
Damien Miller5e953212001-01-30 09:14:00 +1100644 fd_set *readset = NULL, *writeset = NULL;
645 int max_fd;
Damien Millerefb4afe2000-04-12 18:45:05 +1000646 int had_channel = 0;
647 int status;
648 pid_t pid;
649
650 debug("Entering interactive session for SSH2.");
651
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000652 mysignal(SIGCHLD, sigchld_handler2);
Damien Millercf3888d2000-09-30 14:17:52 +1100653 signal(SIGPIPE, SIG_IGN);
Damien Millerefb4afe2000-04-12 18:45:05 +1000654 child_terminated = 0;
655 connection_in = packet_get_connection_in();
656 connection_out = packet_get_connection_out();
Damien Miller5e953212001-01-30 09:14:00 +1100657
658 max_fd = MAX(connection_in, connection_out);
659
Damien Millerefb4afe2000-04-12 18:45:05 +1000660 server_init_dispatch();
661
662 for (;;) {
663 process_buffered_input_packets();
664 if (!had_channel && channel_still_open())
665 had_channel = 1;
666 if (had_channel && !channel_still_open()) {
667 debug("!channel_still_open.");
668 break;
669 }
670 if (packet_not_very_much_data_to_write())
671 channel_output_poll();
Damien Miller5e953212001-01-30 09:14:00 +1100672 wait_until_can_do_something(&readset, &writeset, &max_fd, 0);
Damien Miller43dc8da2000-11-29 15:55:17 +1100673 if (child_terminated) {
Damien Millerefb4afe2000-04-12 18:45:05 +1000674 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
675 session_close_by_pid(pid, status);
676 child_terminated = 0;
677 }
Damien Miller5e953212001-01-30 09:14:00 +1100678 channel_after_select(readset, writeset);
679 process_input(readset);
680 process_output(writeset);
Damien Millerefb4afe2000-04-12 18:45:05 +1000681 }
Damien Miller5e953212001-01-30 09:14:00 +1100682 if (readset)
683 xfree(readset);
684 if (writeset)
685 xfree(writeset);
686
Damien Millerefb4afe2000-04-12 18:45:05 +1000687 signal(SIGCHLD, SIG_DFL);
688 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
689 session_close_by_pid(pid, status);
690 channel_stop_listening();
691}
692
Damien Millerb38eff82000-04-01 11:09:21 +1000693void
Damien Miller62cee002000-09-23 17:15:56 +1100694server_input_stdin_data(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000695{
696 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000697 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000698
699 /* Stdin data from the client. Append it to the buffer. */
700 /* Ignore any data if the client has closed stdin. */
701 if (fdin == -1)
702 return;
703 data = packet_get_string(&data_len);
704 packet_integrity_check(plen, (4 + data_len), type);
705 buffer_append(&stdin_buffer, data, data_len);
706 memset(data, 0, data_len);
707 xfree(data);
708}
709
710void
Damien Miller62cee002000-09-23 17:15:56 +1100711server_input_eof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000712{
713 /*
714 * Eof from the client. The stdin descriptor to the
715 * program will be closed when all buffered data has
716 * drained.
717 */
718 debug("EOF received for stdin.");
719 packet_integrity_check(plen, 0, type);
720 stdin_eof = 1;
721}
722
723void
Damien Miller62cee002000-09-23 17:15:56 +1100724server_input_window_size(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000725{
726 int row = packet_get_int();
727 int col = packet_get_int();
728 int xpixel = packet_get_int();
729 int ypixel = packet_get_int();
730
731 debug("Window change received.");
732 packet_integrity_check(plen, 4 * 4, type);
733 if (fdin != -1)
734 pty_change_window_size(fdin, row, col, xpixel, ypixel);
735}
736
Damien Miller0bc1bd82000-11-13 22:57:25 +1100737Channel *
738server_request_direct_tcpip(char *ctype)
Damien Millerefb4afe2000-04-12 18:45:05 +1000739{
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740 int sock, newch;
Damien Miller4af51302000-04-16 11:18:38 +1000741 char *target, *originator;
742 int target_port, originator_port;
Damien Millerefb4afe2000-04-12 18:45:05 +1000743
Damien Miller4af51302000-04-16 11:18:38 +1000744 target = packet_get_string(NULL);
745 target_port = packet_get_int();
Damien Millerefb4afe2000-04-12 18:45:05 +1000746 originator = packet_get_string(NULL);
747 originator_port = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +1000748 packet_done();
Damien Millerb1715dc2000-05-30 13:44:51 +1000749
Damien Miller0bc1bd82000-11-13 22:57:25 +1100750 debug("server_request_direct_tcpip: originator %s port %d, target %s port %d",
Damien Millerb1715dc2000-05-30 13:44:51 +1000751 originator, originator_port, target, target_port);
Damien Millerf6d9e222000-06-18 14:50:44 +1000752
Damien Millerefb4afe2000-04-12 18:45:05 +1000753 /* XXX check permission */
Damien Miller50a41ed2000-10-16 12:14:42 +1100754 if (no_port_forwarding_flag || !options.allow_tcp_forwarding) {
Damien Millerf6d9e222000-06-18 14:50:44 +1000755 xfree(target);
756 xfree(originator);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757 return NULL;
Damien Millerf6d9e222000-06-18 14:50:44 +1000758 }
Damien Miller4af51302000-04-16 11:18:38 +1000759 sock = channel_connect_to(target, target_port);
760 xfree(target);
Damien Millerefb4afe2000-04-12 18:45:05 +1000761 xfree(originator);
762 if (sock < 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100763 return NULL;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000764 newch = channel_new(ctype, SSH_CHANNEL_CONNECTING,
Damien Millere4340be2000-09-16 13:29:08 +1100765 sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +1100766 CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100767 return (newch >= 0) ? channel_lookup(newch) : NULL;
768}
769
770Channel *
771server_request_session(char *ctype)
772{
773 int newch;
774
775 debug("input_session_request");
776 packet_done();
777 /*
778 * A server session has no fd to read or write until a
779 * CHANNEL_REQUEST for a shell is made, so we set the type to
780 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
781 * CHANNEL_REQUEST messages is registered.
782 */
783 newch = channel_new(ctype, SSH_CHANNEL_LARVAL,
784 -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT,
785 0, xstrdup("server-session"), 1);
786 if (session_open(newch) == 1) {
787 channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST,
788 session_input_channel_req, (void *)0);
789 channel_register_cleanup(newch, session_close_by_channel);
790 return channel_lookup(newch);
791 } else {
792 debug("session open failed, free channel %d", newch);
793 channel_free(newch);
794 }
795 return NULL;
Damien Millerefb4afe2000-04-12 18:45:05 +1000796}
797
Damien Miller4af51302000-04-16 11:18:38 +1000798void
Damien Miller62cee002000-09-23 17:15:56 +1100799server_input_channel_open(int type, int plen, void *ctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +1000800{
801 Channel *c = NULL;
802 char *ctype;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000803 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +1000804 int rchan;
805 int rmaxpack;
806 int rwindow;
807
808 ctype = packet_get_string(&len);
809 rchan = packet_get_int();
810 rwindow = packet_get_int();
811 rmaxpack = packet_get_int();
812
Damien Miller62cee002000-09-23 17:15:56 +1100813 debug("server_input_channel_open: ctype %s rchan %d win %d max %d",
Damien Millerefb4afe2000-04-12 18:45:05 +1000814 ctype, rchan, rwindow, rmaxpack);
815
816 if (strcmp(ctype, "session") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817 c = server_request_session(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000818 } else if (strcmp(ctype, "direct-tcpip") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 c = server_request_direct_tcpip(ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000820 }
821 if (c != NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 debug("server_input_channel_open: confirm %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000823 c->remote_id = rchan;
824 c->remote_window = rwindow;
825 c->remote_maxpacket = rmaxpack;
826
827 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
828 packet_put_int(c->remote_id);
829 packet_put_int(c->self);
830 packet_put_int(c->local_window);
831 packet_put_int(c->local_maxpacket);
832 packet_send();
833 } else {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100834 debug("server_input_channel_open: failure %s", ctype);
Damien Millerefb4afe2000-04-12 18:45:05 +1000835 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
836 packet_put_int(rchan);
837 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
838 packet_put_cstring("bla bla");
839 packet_put_cstring("");
840 packet_send();
841 }
842 xfree(ctype);
843}
844
Kevin Stevesef4eea92001-02-05 12:42:17 +0000845void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100846server_input_global_request(int type, int plen, void *ctxt)
847{
848 char *rtype;
849 int want_reply;
850 int success = 0;
851
852 rtype = packet_get_string(NULL);
853 want_reply = packet_get_char();
854 debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000855
Damien Miller0bc1bd82000-11-13 22:57:25 +1100856 if (strcmp(rtype, "tcpip-forward") == 0) {
857 struct passwd *pw;
858 char *listen_address;
859 u_short listen_port;
860
861 pw = auth_get_user();
862 if (pw == NULL)
863 fatal("server_input_global_request: no user");
864 listen_address = packet_get_string(NULL); /* XXX currently ignored */
865 listen_port = (u_short)packet_get_int();
866 debug("server_input_global_request: tcpip-forward listen %s port %d",
867 listen_address, listen_port);
868
869 /* check permissions */
870 if (!options.allow_tcp_forwarding ||
871 no_port_forwarding_flag ||
872 (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) {
873 success = 0;
874 packet_send_debug("Server has disabled port forwarding.");
875 } else {
876 /* Start listening on the port */
Kevin Steves12057502001-02-05 14:54:34 +0000877 success = channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +1100878 listen_address, listen_port,
879 /*unspec host_to_connect*/ "<unspec host>",
880 /*unspec port_to_connect*/ 0,
881 options.gateway_ports, /*remote*/ 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100882 }
883 xfree(listen_address);
884 }
885 if (want_reply) {
886 packet_start(success ?
887 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
888 packet_send();
889 packet_write_wait();
890 }
891 xfree(rtype);
892}
893
Damien Miller4af51302000-04-16 11:18:38 +1000894void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000895server_init_dispatch_20(void)
Damien Millerefb4afe2000-04-12 18:45:05 +1000896{
897 debug("server_init_dispatch_20");
898 dispatch_init(&dispatch_protocol_error);
899 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
900 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
901 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
902 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
903 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
904 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
905 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
906 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
907 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100908 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
Damien Millerefb4afe2000-04-12 18:45:05 +1000909}
Damien Miller4af51302000-04-16 11:18:38 +1000910void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000911server_init_dispatch_13(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000912{
913 debug("server_init_dispatch_13");
914 dispatch_init(NULL);
915 dispatch_set(SSH_CMSG_EOF, &server_input_eof);
916 dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data);
917 dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size);
918 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
919 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
920 dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
921 dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
922 dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
923 dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
924}
Damien Miller4af51302000-04-16 11:18:38 +1000925void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000926server_init_dispatch_15(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000927{
928 server_init_dispatch_13();
929 debug("server_init_dispatch_15");
930 dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
931 dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose);
932}
Damien Miller4af51302000-04-16 11:18:38 +1000933void
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000934server_init_dispatch(void)
Damien Millerb38eff82000-04-01 11:09:21 +1000935{
Damien Millerefb4afe2000-04-12 18:45:05 +1000936 if (compat20)
937 server_init_dispatch_20();
938 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000939 server_init_dispatch_13();
940 else
941 server_init_dispatch_15();
942}