Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Server main loop for handling the interactive session. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * |
| 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 Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 13 | * SSH2 support by Markus Friedl. |
| 14 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 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 Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
| 37 | #include "includes.h" |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 38 | RCSID("$OpenBSD: serverloop.c,v 1.46 2001/02/08 19:30:52 itojun Exp $"); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 39 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | #include "packet.h" |
| 42 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | #include "servconf.h" |
| 45 | #include "pty.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 46 | #include "channels.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 47 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "ssh1.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 49 | #include "ssh2.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 50 | #include "auth.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 51 | #include "session.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 52 | #include "dispatch.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 53 | #include "auth-options.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 54 | #include "serverloop.h" |
| 55 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 57 | extern ServerOptions options; |
| 58 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 60 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 61 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
| 62 | static int fdin; /* Descriptor for stdin (for writing) */ |
| 63 | static int fdout; /* Descriptor for stdout (for reading); |
| 64 | May be same number as fdin. */ |
| 65 | static int fderr; /* Descriptor for stderr. May be -1. */ |
| 66 | static long stdin_bytes = 0; /* Number of bytes written to stdin. */ |
| 67 | static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */ |
| 68 | static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */ |
| 69 | static long fdout_bytes = 0; /* Number of stdout bytes read from program. */ |
| 70 | static int stdin_eof = 0; /* EOF message received from client. */ |
| 71 | static int fdout_eof = 0; /* EOF encountered reading from fdout. */ |
| 72 | static int fderr_eof = 0; /* EOF encountered readung from fderr. */ |
| 73 | static int connection_in; /* Connection to client (input). */ |
| 74 | static int connection_out; /* Connection to client (output). */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 75 | static u_int buffer_high;/* "Soft" max buffer size. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 77 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 81 | |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 82 | static pid_t child_pid; /* Pid of the child. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | static volatile int child_terminated; /* The child has terminated. */ |
| 84 | static volatile int child_wait_status; /* Status from wait(). */ |
| 85 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 86 | void server_init_dispatch(void); |
| 87 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 88 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 89 | sigchld_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 90 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | int save_errno = errno; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 92 | pid_t wait_pid; |
| 93 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | 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 Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 101 | WIFSIGNALED(child_wait_status)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | child_terminated = 1; |
| 103 | } |
| 104 | signal(SIGCHLD, sigchld_handler); |
| 105 | errno = save_errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 106 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 107 | void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 108 | sigchld_handler2(int sig) |
| 109 | { |
| 110 | int save_errno = errno; |
| 111 | debug("Received SIGCHLD."); |
| 112 | child_terminated = 1; |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 113 | mysignal(SIGCHLD, sigchld_handler2); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 114 | errno = save_errno; |
| 115 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | * Make packets from buffered stderr data, and buffer it for sending |
| 119 | * to the client. |
| 120 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 121 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 122 | make_packets_from_stderr_data(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | /* Send buffered stderr data to the client. */ |
| 127 | while (buffer_len(&stderr_buffer) > 0 && |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 128 | packet_not_very_much_data_to_write()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | } |
| 145 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | /* |
| 147 | * Make packets from buffered stdout data, and buffer it for sending to the |
| 148 | * client. |
| 149 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 150 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 151 | make_packets_from_stdout_data(void) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 154 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | /* Send buffered stdout data to the client. */ |
| 156 | while (buffer_len(&stdout_buffer) > 0 && |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 157 | packet_not_very_much_data_to_write()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | 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 Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 165 | len = packet_get_maxsize(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | } |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 173 | } |
| 174 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | /* |
| 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 Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 181 | void |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 182 | wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
| 183 | u_int max_time_milliseconds) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | struct timeval tv, *tvp; |
| 186 | int ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 187 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | /* When select fails we restart from here. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | retry_select: |
| 190 | |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 191 | /* Allocate and update select() masks for channel descriptors. */ |
| 192 | channel_prepare_select(readsetp, writesetp, maxfdp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 194 | if (compat20) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 195 | /* wrong: bad condition XXX */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 196 | if (channel_not_very_much_buffered_data()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 197 | FD_SET(connection_in, *readsetp); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 198 | } else { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 199 | /* |
| 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 Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 204 | channel_not_very_much_buffered_data()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 205 | FD_SET(connection_in, *readsetp); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 206 | /* |
| 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 Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 212 | FD_SET(fdout, *readsetp); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 213 | if (!fderr_eof) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 214 | FD_SET(fderr, *readsetp); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 215 | } |
| 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 Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 221 | FD_SET(fdin, *writesetp); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 222 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 224 | /* |
| 225 | * If we have buffered packet data going to the client, mark that |
| 226 | * descriptor. |
| 227 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 228 | if (packet_have_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 229 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 231 | /* |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 235 | 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 Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 246 | if (tvp!=NULL) |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 247 | debug2("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 248 | |
| 249 | /* Wait for something to happen, or the timeout to expire. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 250 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | |
| 252 | if (ret < 0) { |
| 253 | if (errno != EINTR) |
| 254 | error("select: %.100s", strerror(errno)); |
| 255 | else |
| 256 | goto retry_select; |
| 257 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | } |
| 259 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 260 | /* |
| 261 | * Processes input from the client and the program. Input data is stored |
| 262 | * in buffers and processed later. |
| 263 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 264 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | process_input(fd_set * readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 266 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 267 | int len; |
| 268 | char buf[16384]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 269 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 270 | /* 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 Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 276 | } 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 284 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 285 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 286 | if (compat20) |
| 287 | return; |
| 288 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 289 | /* 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 Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 292 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 293 | /* do nothing */ |
| 294 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 295 | fdout_eof = 1; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 296 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 297 | 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 Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 304 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 305 | /* do nothing */ |
| 306 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 307 | fderr_eof = 1; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 308 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 309 | buffer_append(&stderr_buffer, buf, len); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 310 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | } |
| 312 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 313 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 314 | /* |
| 315 | * Sends data from internal buffers to client program stdin. |
| 316 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 317 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | process_output(fd_set * writeset) |
| 319 | { |
| 320 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 321 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 322 | /* Write buffered data to program stdin. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 323 | if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | len = write(fdin, buffer_ptr(&stdin_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 325 | buffer_len(&stdin_buffer)); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 326 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 327 | /* do nothing */ |
| 328 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 329 | #ifdef USE_PIPES |
| 330 | close(fdin); |
| 331 | #else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 332 | if (fdin != fdout) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 333 | close(fdin); |
| 334 | else |
| 335 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ |
| 336 | #endif |
| 337 | fdin = -1; |
| 338 | } else { |
| 339 | /* Successful write. Consume the data from the buffer. */ |
| 340 | buffer_consume(&stdin_buffer, len); |
| 341 | /* Update the count of bytes written to the program. */ |
| 342 | stdin_bytes += len; |
| 343 | } |
| 344 | } |
| 345 | /* Send any buffered packet data to the client. */ |
| 346 | if (FD_ISSET(connection_out, writeset)) |
| 347 | packet_write_poll(); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Wait until all buffered output has been sent to the client. |
| 352 | * This is used when the program terminates. |
| 353 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 354 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 355 | drain_output(void) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 356 | { |
| 357 | /* Send any buffered stdout data to the client. */ |
| 358 | if (buffer_len(&stdout_buffer) > 0) { |
| 359 | packet_start(SSH_SMSG_STDOUT_DATA); |
| 360 | packet_put_string(buffer_ptr(&stdout_buffer), |
| 361 | buffer_len(&stdout_buffer)); |
| 362 | packet_send(); |
| 363 | /* Update the count of sent bytes. */ |
| 364 | stdout_bytes += buffer_len(&stdout_buffer); |
| 365 | } |
| 366 | /* Send any buffered stderr data to the client. */ |
| 367 | if (buffer_len(&stderr_buffer) > 0) { |
| 368 | packet_start(SSH_SMSG_STDERR_DATA); |
| 369 | packet_put_string(buffer_ptr(&stderr_buffer), |
| 370 | buffer_len(&stderr_buffer)); |
| 371 | packet_send(); |
| 372 | /* Update the count of sent bytes. */ |
| 373 | stderr_bytes += buffer_len(&stderr_buffer); |
| 374 | } |
| 375 | /* Wait until all buffered data has been written to the client. */ |
| 376 | packet_write_wait(); |
| 377 | } |
| 378 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 379 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 380 | process_buffered_input_packets(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 381 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 382 | dispatch_run(DISPATCH_NONBLOCK, NULL, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 383 | } |
| 384 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 385 | /* |
| 386 | * Performs the interactive session. This handles data transmission between |
| 387 | * the client and the program. Note that the notion of stdin, stdout, and |
| 388 | * stderr in this function is sort of reversed: this function writes to |
| 389 | * stdin (of the child program), and reads from stdout and stderr (of the |
| 390 | * child program). |
| 391 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 392 | void |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 393 | server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 395 | fd_set *readset = NULL, *writeset = NULL; |
| 396 | int max_fd; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 397 | int wait_status; /* Status returned by wait(). */ |
| 398 | pid_t wait_pid; /* pid returned by wait(). */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 399 | int waiting_termination = 0; /* Have displayed waiting close message. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 400 | u_int max_time_milliseconds; |
| 401 | u_int previous_stdout_buffer_bytes; |
| 402 | u_int stdout_buffer_bytes; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 403 | int type; |
| 404 | |
| 405 | debug("Entering interactive session."); |
| 406 | |
| 407 | /* Initialize the SIGCHLD kludge. */ |
| 408 | child_pid = pid; |
| 409 | child_terminated = 0; |
| 410 | signal(SIGCHLD, sigchld_handler); |
Damien Miller | cf3888d | 2000-09-30 14:17:52 +1100 | [diff] [blame] | 411 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | |
| 413 | /* Initialize our global variables. */ |
| 414 | fdin = fdin_arg; |
| 415 | fdout = fdout_arg; |
| 416 | fderr = fderr_arg; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 417 | |
| 418 | /* nonblocking IO */ |
| 419 | set_nonblock(fdin); |
| 420 | set_nonblock(fdout); |
Damien Miller | 7d6656c | 2000-05-20 15:22:36 +1000 | [diff] [blame] | 421 | /* we don't have stderr for interactive terminal sessions, see below */ |
| 422 | if (fderr != -1) |
| 423 | set_nonblock(fderr); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 424 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 425 | connection_in = packet_get_connection_in(); |
| 426 | connection_out = packet_get_connection_out(); |
| 427 | |
| 428 | previous_stdout_buffer_bytes = 0; |
| 429 | |
| 430 | /* Set approximate I/O buffer size. */ |
| 431 | if (packet_is_interactive()) |
| 432 | buffer_high = 4096; |
| 433 | else |
| 434 | buffer_high = 64 * 1024; |
| 435 | |
| 436 | /* Initialize max_fd to the maximum of the known file descriptors. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 437 | max_fd = MAX(fdin, fdout); |
| 438 | if (fderr != -1) |
| 439 | max_fd = MAX(max_fd, fderr); |
| 440 | max_fd = MAX(max_fd, connection_in); |
| 441 | max_fd = MAX(max_fd, connection_out); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 442 | |
| 443 | /* Initialize Initialize buffers. */ |
| 444 | buffer_init(&stdin_buffer); |
| 445 | buffer_init(&stdout_buffer); |
| 446 | buffer_init(&stderr_buffer); |
| 447 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 448 | /* |
| 449 | * If we have no separate fderr (which is the case when we have a pty |
| 450 | * - there we cannot make difference between data sent to stdout and |
| 451 | * stderr), indicate that we have seen an EOF from stderr. This way |
| 452 | * we don\'t need to check the descriptor everywhere. |
| 453 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 454 | if (fderr == -1) |
| 455 | fderr_eof = 1; |
| 456 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 457 | server_init_dispatch(); |
| 458 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 459 | /* Main loop of the server for the interactive session mode. */ |
| 460 | for (;;) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 461 | |
| 462 | /* Process buffered packets from the client. */ |
| 463 | process_buffered_input_packets(); |
| 464 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 465 | /* |
| 466 | * If we have received eof, and there is no more pending |
| 467 | * input data, cause a real eof by closing fdin. |
| 468 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 469 | if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { |
| 470 | #ifdef USE_PIPES |
| 471 | close(fdin); |
| 472 | #else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 473 | if (fdin != fdout) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 474 | close(fdin); |
| 475 | else |
| 476 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ |
| 477 | #endif |
| 478 | fdin = -1; |
| 479 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 480 | /* Make packets from buffered stderr data to send to the client. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 481 | make_packets_from_stderr_data(); |
| 482 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 483 | /* |
| 484 | * Make packets from buffered stdout data to send to the |
| 485 | * client. If there is very little to send, this arranges to |
| 486 | * not send them now, but to wait a short while to see if we |
| 487 | * are getting more data. This is necessary, as some systems |
| 488 | * wake up readers from a pty after each separate character. |
| 489 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 490 | max_time_milliseconds = 0; |
| 491 | stdout_buffer_bytes = buffer_len(&stdout_buffer); |
| 492 | if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && |
| 493 | stdout_buffer_bytes != previous_stdout_buffer_bytes) { |
| 494 | /* try again after a while */ |
| 495 | max_time_milliseconds = 10; |
| 496 | } else { |
| 497 | /* Send it now. */ |
| 498 | make_packets_from_stdout_data(); |
| 499 | } |
| 500 | previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); |
| 501 | |
| 502 | /* Send channel data to the client. */ |
| 503 | if (packet_not_very_much_data_to_write()) |
| 504 | channel_output_poll(); |
| 505 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 506 | /* |
| 507 | * Bail out of the loop if the program has closed its output |
| 508 | * descriptors, and we have no more data to send to the |
| 509 | * client, and there is no pending buffered data. |
| 510 | */ |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 511 | if (fdout_eof && fderr_eof && !packet_have_data_to_write() && |
| 512 | buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 513 | if (!channel_still_open()) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 514 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 515 | if (!waiting_termination) { |
| 516 | const char *s = "Waiting for forwarded connections to terminate...\r\n"; |
| 517 | char *cp; |
| 518 | waiting_termination = 1; |
| 519 | buffer_append(&stderr_buffer, s, strlen(s)); |
| 520 | |
| 521 | /* Display list of open channels. */ |
| 522 | cp = channel_open_message(); |
| 523 | buffer_append(&stderr_buffer, cp, strlen(cp)); |
| 524 | xfree(cp); |
| 525 | } |
| 526 | } |
| 527 | /* Sleep in select() until we can do something. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 528 | wait_until_can_do_something(&readset, &writeset, &max_fd, |
| 529 | max_time_milliseconds); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 530 | |
| 531 | /* Process any channel events. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 532 | channel_after_select(readset, writeset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 533 | |
| 534 | /* Process input from the client and from program stdout/stderr. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 535 | process_input(readset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 536 | |
| 537 | /* Process output to the client and to program stdin. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 538 | process_output(writeset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 539 | } |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 540 | if (readset) |
| 541 | xfree(readset); |
| 542 | if (writeset) |
| 543 | xfree(writeset); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 544 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 545 | /* Cleanup and termination code. */ |
| 546 | |
| 547 | /* Wait until all output has been sent to the client. */ |
| 548 | drain_output(); |
| 549 | |
| 550 | debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", |
| 551 | stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); |
| 552 | |
| 553 | /* Free and clear the buffers. */ |
| 554 | buffer_free(&stdin_buffer); |
| 555 | buffer_free(&stdout_buffer); |
| 556 | buffer_free(&stderr_buffer); |
| 557 | |
| 558 | /* Close the file descriptors. */ |
| 559 | if (fdout != -1) |
| 560 | close(fdout); |
| 561 | fdout = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 562 | fdout_eof = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 563 | if (fderr != -1) |
| 564 | close(fderr); |
| 565 | fderr = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 566 | fderr_eof = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 567 | if (fdin != -1) |
| 568 | close(fdin); |
| 569 | fdin = -1; |
| 570 | |
| 571 | /* Stop listening for channels; this removes unix domain sockets. */ |
| 572 | channel_stop_listening(); |
| 573 | |
| 574 | /* Wait for the child to exit. Get its exit status. */ |
| 575 | wait_pid = wait(&wait_status); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 576 | if (wait_pid == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 577 | /* |
| 578 | * It is possible that the wait was handled by SIGCHLD |
| 579 | * handler. This may result in either: this call |
| 580 | * returning with EINTR, or: this call returning ECHILD. |
| 581 | */ |
| 582 | if (child_terminated) |
| 583 | wait_status = child_wait_status; |
| 584 | else |
| 585 | packet_disconnect("wait: %.100s", strerror(errno)); |
| 586 | } else { |
| 587 | /* Check if it matches the process we forked. */ |
| 588 | if (wait_pid != pid) |
| 589 | error("Strange, wait returned pid %d, expected %d", |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 590 | wait_pid, pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* We no longer want our SIGCHLD handler to be called. */ |
| 594 | signal(SIGCHLD, SIG_DFL); |
| 595 | |
| 596 | /* Check if it exited normally. */ |
| 597 | if (WIFEXITED(wait_status)) { |
| 598 | /* Yes, normal exit. Get exit status and send it to the client. */ |
| 599 | debug("Command exited with status %d.", WEXITSTATUS(wait_status)); |
| 600 | packet_start(SSH_SMSG_EXITSTATUS); |
| 601 | packet_put_int(WEXITSTATUS(wait_status)); |
| 602 | packet_send(); |
| 603 | packet_write_wait(); |
| 604 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 605 | /* |
| 606 | * Wait for exit confirmation. Note that there might be |
| 607 | * other packets coming before it; however, the program has |
| 608 | * already died so we just ignore them. The client is |
| 609 | * supposed to respond with the confirmation when it receives |
| 610 | * the exit status. |
| 611 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 612 | do { |
| 613 | int plen; |
| 614 | type = packet_read(&plen); |
| 615 | } |
| 616 | while (type != SSH_CMSG_EXIT_CONFIRMATION); |
| 617 | |
| 618 | debug("Received exit confirmation."); |
| 619 | return; |
| 620 | } |
| 621 | /* Check if the program terminated due to a signal. */ |
| 622 | if (WIFSIGNALED(wait_status)) |
| 623 | packet_disconnect("Command terminated on signal %d.", |
| 624 | WTERMSIG(wait_status)); |
| 625 | |
| 626 | /* Some weird exit cause. Just exit. */ |
| 627 | packet_disconnect("wait returned status %04x.", wait_status); |
| 628 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 629 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 630 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 631 | void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 632 | server_loop2(void) |
| 633 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 634 | fd_set *readset = NULL, *writeset = NULL; |
| 635 | int max_fd; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 636 | int had_channel = 0; |
| 637 | int status; |
| 638 | pid_t pid; |
| 639 | |
| 640 | debug("Entering interactive session for SSH2."); |
| 641 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 642 | mysignal(SIGCHLD, sigchld_handler2); |
Damien Miller | cf3888d | 2000-09-30 14:17:52 +1100 | [diff] [blame] | 643 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 644 | child_terminated = 0; |
| 645 | connection_in = packet_get_connection_in(); |
| 646 | connection_out = packet_get_connection_out(); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 647 | |
| 648 | max_fd = MAX(connection_in, connection_out); |
| 649 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 650 | server_init_dispatch(); |
| 651 | |
| 652 | for (;;) { |
| 653 | process_buffered_input_packets(); |
| 654 | if (!had_channel && channel_still_open()) |
| 655 | had_channel = 1; |
| 656 | if (had_channel && !channel_still_open()) { |
| 657 | debug("!channel_still_open."); |
| 658 | break; |
| 659 | } |
| 660 | if (packet_not_very_much_data_to_write()) |
| 661 | channel_output_poll(); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 662 | wait_until_can_do_something(&readset, &writeset, &max_fd, 0); |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 663 | if (child_terminated) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 664 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
| 665 | session_close_by_pid(pid, status); |
| 666 | child_terminated = 0; |
| 667 | } |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 668 | channel_after_select(readset, writeset); |
| 669 | process_input(readset); |
| 670 | process_output(writeset); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 671 | } |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 672 | if (readset) |
| 673 | xfree(readset); |
| 674 | if (writeset) |
| 675 | xfree(writeset); |
| 676 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 677 | signal(SIGCHLD, SIG_DFL); |
| 678 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
| 679 | session_close_by_pid(pid, status); |
| 680 | channel_stop_listening(); |
| 681 | } |
| 682 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 683 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 684 | server_input_stdin_data(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 685 | { |
| 686 | char *data; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 687 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 688 | |
| 689 | /* Stdin data from the client. Append it to the buffer. */ |
| 690 | /* Ignore any data if the client has closed stdin. */ |
| 691 | if (fdin == -1) |
| 692 | return; |
| 693 | data = packet_get_string(&data_len); |
| 694 | packet_integrity_check(plen, (4 + data_len), type); |
| 695 | buffer_append(&stdin_buffer, data, data_len); |
| 696 | memset(data, 0, data_len); |
| 697 | xfree(data); |
| 698 | } |
| 699 | |
| 700 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 701 | server_input_eof(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 702 | { |
| 703 | /* |
| 704 | * Eof from the client. The stdin descriptor to the |
| 705 | * program will be closed when all buffered data has |
| 706 | * drained. |
| 707 | */ |
| 708 | debug("EOF received for stdin."); |
| 709 | packet_integrity_check(plen, 0, type); |
| 710 | stdin_eof = 1; |
| 711 | } |
| 712 | |
| 713 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 714 | server_input_window_size(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 715 | { |
| 716 | int row = packet_get_int(); |
| 717 | int col = packet_get_int(); |
| 718 | int xpixel = packet_get_int(); |
| 719 | int ypixel = packet_get_int(); |
| 720 | |
| 721 | debug("Window change received."); |
| 722 | packet_integrity_check(plen, 4 * 4, type); |
| 723 | if (fdin != -1) |
| 724 | pty_change_window_size(fdin, row, col, xpixel, ypixel); |
| 725 | } |
| 726 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 727 | Channel * |
| 728 | server_request_direct_tcpip(char *ctype) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 729 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 730 | int sock, newch; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 731 | char *target, *originator; |
| 732 | int target_port, originator_port; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 733 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 734 | target = packet_get_string(NULL); |
| 735 | target_port = packet_get_int(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 736 | originator = packet_get_string(NULL); |
| 737 | originator_port = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 738 | packet_done(); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 739 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 740 | debug("server_request_direct_tcpip: originator %s port %d, target %s port %d", |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 741 | originator, originator_port, target, target_port); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 742 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 743 | /* XXX check permission */ |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 744 | if (no_port_forwarding_flag || !options.allow_tcp_forwarding) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 745 | xfree(target); |
| 746 | xfree(originator); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 747 | return NULL; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 748 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 749 | sock = channel_connect_to(target, target_port); |
| 750 | xfree(target); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 751 | xfree(originator); |
| 752 | if (sock < 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 753 | return NULL; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 754 | newch = channel_new(ctype, SSH_CHANNEL_CONNECTING, |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 755 | sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 756 | CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 757 | return (newch >= 0) ? channel_lookup(newch) : NULL; |
| 758 | } |
| 759 | |
| 760 | Channel * |
| 761 | server_request_session(char *ctype) |
| 762 | { |
| 763 | int newch; |
| 764 | |
| 765 | debug("input_session_request"); |
| 766 | packet_done(); |
| 767 | /* |
| 768 | * A server session has no fd to read or write until a |
| 769 | * CHANNEL_REQUEST for a shell is made, so we set the type to |
| 770 | * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all |
| 771 | * CHANNEL_REQUEST messages is registered. |
| 772 | */ |
| 773 | newch = channel_new(ctype, SSH_CHANNEL_LARVAL, |
| 774 | -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT, |
| 775 | 0, xstrdup("server-session"), 1); |
| 776 | if (session_open(newch) == 1) { |
| 777 | channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST, |
| 778 | session_input_channel_req, (void *)0); |
| 779 | channel_register_cleanup(newch, session_close_by_channel); |
| 780 | return channel_lookup(newch); |
| 781 | } else { |
| 782 | debug("session open failed, free channel %d", newch); |
| 783 | channel_free(newch); |
| 784 | } |
| 785 | return NULL; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 786 | } |
| 787 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 788 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 789 | server_input_channel_open(int type, int plen, void *ctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 790 | { |
| 791 | Channel *c = NULL; |
| 792 | char *ctype; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 793 | u_int len; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 794 | int rchan; |
| 795 | int rmaxpack; |
| 796 | int rwindow; |
| 797 | |
| 798 | ctype = packet_get_string(&len); |
| 799 | rchan = packet_get_int(); |
| 800 | rwindow = packet_get_int(); |
| 801 | rmaxpack = packet_get_int(); |
| 802 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 803 | debug("server_input_channel_open: ctype %s rchan %d win %d max %d", |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 804 | ctype, rchan, rwindow, rmaxpack); |
| 805 | |
| 806 | if (strcmp(ctype, "session") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 807 | c = server_request_session(ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 808 | } else if (strcmp(ctype, "direct-tcpip") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 809 | c = server_request_direct_tcpip(ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 810 | } |
| 811 | if (c != NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 812 | debug("server_input_channel_open: confirm %s", ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 813 | c->remote_id = rchan; |
| 814 | c->remote_window = rwindow; |
| 815 | c->remote_maxpacket = rmaxpack; |
| 816 | |
| 817 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 818 | packet_put_int(c->remote_id); |
| 819 | packet_put_int(c->self); |
| 820 | packet_put_int(c->local_window); |
| 821 | packet_put_int(c->local_maxpacket); |
| 822 | packet_send(); |
| 823 | } else { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 824 | debug("server_input_channel_open: failure %s", ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 825 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 826 | packet_put_int(rchan); |
| 827 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
| 828 | packet_put_cstring("bla bla"); |
| 829 | packet_put_cstring(""); |
| 830 | packet_send(); |
| 831 | } |
| 832 | xfree(ctype); |
| 833 | } |
| 834 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 835 | void |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 836 | server_input_global_request(int type, int plen, void *ctxt) |
| 837 | { |
| 838 | char *rtype; |
| 839 | int want_reply; |
| 840 | int success = 0; |
| 841 | |
| 842 | rtype = packet_get_string(NULL); |
| 843 | want_reply = packet_get_char(); |
| 844 | debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 845 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 846 | if (strcmp(rtype, "tcpip-forward") == 0) { |
| 847 | struct passwd *pw; |
| 848 | char *listen_address; |
| 849 | u_short listen_port; |
| 850 | |
| 851 | pw = auth_get_user(); |
| 852 | if (pw == NULL) |
| 853 | fatal("server_input_global_request: no user"); |
| 854 | listen_address = packet_get_string(NULL); /* XXX currently ignored */ |
| 855 | listen_port = (u_short)packet_get_int(); |
| 856 | debug("server_input_global_request: tcpip-forward listen %s port %d", |
| 857 | listen_address, listen_port); |
| 858 | |
| 859 | /* check permissions */ |
| 860 | if (!options.allow_tcp_forwarding || |
| 861 | no_port_forwarding_flag || |
| 862 | (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) { |
| 863 | success = 0; |
| 864 | packet_send_debug("Server has disabled port forwarding."); |
| 865 | } else { |
| 866 | /* Start listening on the port */ |
Kevin Steves | 1205750 | 2001-02-05 14:54:34 +0000 | [diff] [blame] | 867 | success = channel_request_forwarding( |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 868 | listen_address, listen_port, |
| 869 | /*unspec host_to_connect*/ "<unspec host>", |
| 870 | /*unspec port_to_connect*/ 0, |
| 871 | options.gateway_ports, /*remote*/ 1); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 872 | } |
| 873 | xfree(listen_address); |
| 874 | } |
| 875 | if (want_reply) { |
| 876 | packet_start(success ? |
| 877 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
| 878 | packet_send(); |
| 879 | packet_write_wait(); |
| 880 | } |
| 881 | xfree(rtype); |
| 882 | } |
| 883 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 884 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 885 | server_init_dispatch_20(void) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 886 | { |
| 887 | debug("server_init_dispatch_20"); |
| 888 | dispatch_init(&dispatch_protocol_error); |
| 889 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 890 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 891 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 892 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
| 893 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); |
| 894 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 895 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 896 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request); |
| 897 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 898 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 899 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 900 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 901 | server_init_dispatch_13(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 902 | { |
| 903 | debug("server_init_dispatch_13"); |
| 904 | dispatch_init(NULL); |
| 905 | dispatch_set(SSH_CMSG_EOF, &server_input_eof); |
| 906 | dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); |
| 907 | dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); |
| 908 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 909 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 910 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
| 911 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 912 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 913 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
| 914 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 915 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 916 | server_init_dispatch_15(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 917 | { |
| 918 | server_init_dispatch_13(); |
| 919 | debug("server_init_dispatch_15"); |
| 920 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 921 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); |
| 922 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 923 | void |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 924 | server_init_dispatch(void) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 925 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 926 | if (compat20) |
| 927 | server_init_dispatch_20(); |
| 928 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 929 | server_init_dispatch_13(); |
| 930 | else |
| 931 | server_init_dispatch_15(); |
| 932 | } |