1 |
|
|
/* $OpenBSD: serverloop.c,v 1.199 2017/10/23 05:08:00 djm Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 |
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 |
|
|
* All rights reserved |
6 |
|
|
* Server main loop for handling the interactive session. |
7 |
|
|
* |
8 |
|
|
* As far as I am concerned, the code I have written for this software |
9 |
|
|
* can be used freely for any purpose. Any derived versions of this |
10 |
|
|
* software must be clearly marked as such, and if the derived work is |
11 |
|
|
* incompatible with the protocol description in the RFC file, it must be |
12 |
|
|
* called by a name other than "ssh" or "Secure Shell". |
13 |
|
|
* |
14 |
|
|
* SSH2 support by Markus Friedl. |
15 |
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
16 |
|
|
* |
17 |
|
|
* Redistribution and use in source and binary forms, with or without |
18 |
|
|
* modification, are permitted provided that the following conditions |
19 |
|
|
* are met: |
20 |
|
|
* 1. Redistributions of source code must retain the above copyright |
21 |
|
|
* notice, this list of conditions and the following disclaimer. |
22 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
23 |
|
|
* notice, this list of conditions and the following disclaimer in the |
24 |
|
|
* documentation and/or other materials provided with the distribution. |
25 |
|
|
* |
26 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
27 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
28 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
29 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
30 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
31 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
32 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
33 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
34 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
35 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
36 |
|
|
*/ |
37 |
|
|
|
38 |
|
|
#include <sys/types.h> |
39 |
|
|
#include <sys/wait.h> |
40 |
|
|
#include <sys/socket.h> |
41 |
|
|
#include <sys/time.h> |
42 |
|
|
#include <sys/queue.h> |
43 |
|
|
|
44 |
|
|
#include <netinet/in.h> |
45 |
|
|
|
46 |
|
|
#include <errno.h> |
47 |
|
|
#include <fcntl.h> |
48 |
|
|
#include <pwd.h> |
49 |
|
|
#include <signal.h> |
50 |
|
|
#include <string.h> |
51 |
|
|
#include <termios.h> |
52 |
|
|
#include <unistd.h> |
53 |
|
|
#include <stdarg.h> |
54 |
|
|
|
55 |
|
|
#include "xmalloc.h" |
56 |
|
|
#include "packet.h" |
57 |
|
|
#include "buffer.h" |
58 |
|
|
#include "log.h" |
59 |
|
|
#include "misc.h" |
60 |
|
|
#include "servconf.h" |
61 |
|
|
#include "canohost.h" |
62 |
|
|
#include "sshpty.h" |
63 |
|
|
#include "channels.h" |
64 |
|
|
#include "compat.h" |
65 |
|
|
#include "ssh2.h" |
66 |
|
|
#include "key.h" |
67 |
|
|
#include "cipher.h" |
68 |
|
|
#include "kex.h" |
69 |
|
|
#include "hostfile.h" |
70 |
|
|
#include "auth.h" |
71 |
|
|
#include "session.h" |
72 |
|
|
#include "dispatch.h" |
73 |
|
|
#include "auth-options.h" |
74 |
|
|
#include "serverloop.h" |
75 |
|
|
#include "ssherr.h" |
76 |
|
|
|
77 |
|
|
extern ServerOptions options; |
78 |
|
|
|
79 |
|
|
/* XXX */ |
80 |
|
|
extern Authctxt *the_authctxt; |
81 |
|
|
extern int use_privsep; |
82 |
|
|
|
83 |
|
|
static int no_more_sessions = 0; /* Disallow further sessions. */ |
84 |
|
|
|
85 |
|
|
/* |
86 |
|
|
* This SIGCHLD kludge is used to detect when the child exits. The server |
87 |
|
|
* will exit after that, as soon as forwarded connections have terminated. |
88 |
|
|
*/ |
89 |
|
|
|
90 |
|
|
static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ |
91 |
|
|
|
92 |
|
|
/* Cleanup on signals (!use_privsep case only) */ |
93 |
|
|
static volatile sig_atomic_t received_sigterm = 0; |
94 |
|
|
|
95 |
|
|
/* prototypes */ |
96 |
|
|
static void server_init_dispatch(void); |
97 |
|
|
|
98 |
|
|
/* requested tunnel forwarding interface(s), shared with session.c */ |
99 |
|
|
char *tun_fwd_ifnames = NULL; |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* we write to this pipe if a SIGCHLD is caught in order to avoid |
103 |
|
|
* the race between select() and child_terminated |
104 |
|
|
*/ |
105 |
|
|
static int notify_pipe[2]; |
106 |
|
|
static void |
107 |
|
|
notify_setup(void) |
108 |
|
|
{ |
109 |
|
|
if (pipe(notify_pipe) < 0) { |
110 |
|
|
error("pipe(notify_pipe) failed %s", strerror(errno)); |
111 |
|
|
} else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || |
112 |
|
|
(fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { |
113 |
|
|
error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); |
114 |
|
|
close(notify_pipe[0]); |
115 |
|
|
close(notify_pipe[1]); |
116 |
|
|
} else { |
117 |
|
|
set_nonblock(notify_pipe[0]); |
118 |
|
|
set_nonblock(notify_pipe[1]); |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
notify_pipe[0] = -1; /* read end */ |
122 |
|
|
notify_pipe[1] = -1; /* write end */ |
123 |
|
|
} |
124 |
|
|
static void |
125 |
|
|
notify_parent(void) |
126 |
|
|
{ |
127 |
|
|
if (notify_pipe[1] != -1) |
128 |
|
|
(void)write(notify_pipe[1], "", 1); |
129 |
|
|
} |
130 |
|
|
static void |
131 |
|
|
notify_prepare(fd_set *readset) |
132 |
|
|
{ |
133 |
|
|
if (notify_pipe[0] != -1) |
134 |
|
|
FD_SET(notify_pipe[0], readset); |
135 |
|
|
} |
136 |
|
|
static void |
137 |
|
|
notify_done(fd_set *readset) |
138 |
|
|
{ |
139 |
|
|
char c; |
140 |
|
|
|
141 |
|
|
if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) |
142 |
|
|
while (read(notify_pipe[0], &c, 1) != -1) |
143 |
|
|
debug2("notify_done: reading"); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/*ARGSUSED*/ |
147 |
|
|
static void |
148 |
|
|
sigchld_handler(int sig) |
149 |
|
|
{ |
150 |
|
|
int save_errno = errno; |
151 |
|
|
child_terminated = 1; |
152 |
|
|
signal(SIGCHLD, sigchld_handler); |
153 |
|
|
notify_parent(); |
154 |
|
|
errno = save_errno; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/*ARGSUSED*/ |
158 |
|
|
static void |
159 |
|
|
sigterm_handler(int sig) |
160 |
|
|
{ |
161 |
|
|
received_sigterm = sig; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
static void |
165 |
|
|
client_alive_check(struct ssh *ssh) |
166 |
|
|
{ |
167 |
|
|
int channel_id; |
168 |
|
|
|
169 |
|
|
/* timeout, check to see how many we have had */ |
170 |
|
|
if (packet_inc_alive_timeouts() > options.client_alive_count_max) { |
171 |
|
|
logit("Timeout, client not responding."); |
172 |
|
|
cleanup_exit(255); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
/* |
176 |
|
|
* send a bogus global/channel request with "wantreply", |
177 |
|
|
* we should get back a failure |
178 |
|
|
*/ |
179 |
|
|
if ((channel_id = channel_find_open(ssh)) == -1) { |
180 |
|
|
packet_start(SSH2_MSG_GLOBAL_REQUEST); |
181 |
|
|
packet_put_cstring("keepalive@openssh.com"); |
182 |
|
|
packet_put_char(1); /* boolean: want reply */ |
183 |
|
|
} else { |
184 |
|
|
channel_request_start(ssh, channel_id, |
185 |
|
|
"keepalive@openssh.com", 1); |
186 |
|
|
} |
187 |
|
|
packet_send(); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
/* |
191 |
|
|
* Sleep in select() until we can do something. This will initialize the |
192 |
|
|
* select masks. Upon return, the masks will indicate which descriptors |
193 |
|
|
* have data or can accept data. Optionally, a maximum time can be specified |
194 |
|
|
* for the duration of the wait (0 = infinite). |
195 |
|
|
*/ |
196 |
|
|
static void |
197 |
|
|
wait_until_can_do_something(struct ssh *ssh, |
198 |
|
|
int connection_in, int connection_out, |
199 |
|
|
fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
200 |
|
|
u_int *nallocp, u_int64_t max_time_ms) |
201 |
|
|
{ |
202 |
|
|
struct timeval tv, *tvp; |
203 |
|
|
int ret; |
204 |
|
|
time_t minwait_secs = 0; |
205 |
|
|
int client_alive_scheduled = 0; |
206 |
|
|
static time_t last_client_time; |
207 |
|
|
|
208 |
|
|
/* Allocate and update select() masks for channel descriptors. */ |
209 |
|
|
channel_prepare_select(ssh, readsetp, writesetp, maxfdp, |
210 |
|
|
nallocp, &minwait_secs); |
211 |
|
|
|
212 |
|
|
/* XXX need proper deadline system for rekey/client alive */ |
213 |
|
|
if (minwait_secs != 0) |
214 |
|
|
max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000); |
215 |
|
|
|
216 |
|
|
/* |
217 |
|
|
* if using client_alive, set the max timeout accordingly, |
218 |
|
|
* and indicate that this particular timeout was for client |
219 |
|
|
* alive by setting the client_alive_scheduled flag. |
220 |
|
|
* |
221 |
|
|
* this could be randomized somewhat to make traffic |
222 |
|
|
* analysis more difficult, but we're not doing it yet. |
223 |
|
|
*/ |
224 |
|
|
if (options.client_alive_interval) { |
225 |
|
|
uint64_t keepalive_ms = |
226 |
|
|
(uint64_t)options.client_alive_interval * 1000; |
227 |
|
|
|
228 |
|
|
client_alive_scheduled = 1; |
229 |
|
|
if (max_time_ms == 0 || max_time_ms > keepalive_ms) |
230 |
|
|
max_time_ms = keepalive_ms; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
#if 0 |
234 |
|
|
/* wrong: bad condition XXX */ |
235 |
|
|
if (channel_not_very_much_buffered_data()) |
236 |
|
|
#endif |
237 |
|
|
FD_SET(connection_in, *readsetp); |
238 |
|
|
notify_prepare(*readsetp); |
239 |
|
|
|
240 |
|
|
/* |
241 |
|
|
* If we have buffered packet data going to the client, mark that |
242 |
|
|
* descriptor. |
243 |
|
|
*/ |
244 |
|
|
if (packet_have_data_to_write()) |
245 |
|
|
FD_SET(connection_out, *writesetp); |
246 |
|
|
|
247 |
|
|
/* |
248 |
|
|
* If child has terminated and there is enough buffer space to read |
249 |
|
|
* from it, then read as much as is available and exit. |
250 |
|
|
*/ |
251 |
|
|
if (child_terminated && packet_not_very_much_data_to_write()) |
252 |
|
|
if (max_time_ms == 0 || client_alive_scheduled) |
253 |
|
|
max_time_ms = 100; |
254 |
|
|
|
255 |
|
|
if (max_time_ms == 0) |
256 |
|
|
tvp = NULL; |
257 |
|
|
else { |
258 |
|
|
tv.tv_sec = max_time_ms / 1000; |
259 |
|
|
tv.tv_usec = 1000 * (max_time_ms % 1000); |
260 |
|
|
tvp = &tv; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
/* Wait for something to happen, or the timeout to expire. */ |
264 |
|
|
ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
265 |
|
|
|
266 |
|
|
if (ret == -1) { |
267 |
|
|
memset(*readsetp, 0, *nallocp); |
268 |
|
|
memset(*writesetp, 0, *nallocp); |
269 |
|
|
if (errno != EINTR) |
270 |
|
|
error("select: %.100s", strerror(errno)); |
271 |
|
|
} else if (client_alive_scheduled) { |
272 |
|
|
time_t now = monotime(); |
273 |
|
|
|
274 |
|
|
if (ret == 0) { /* timeout */ |
275 |
|
|
client_alive_check(ssh); |
276 |
|
|
} else if (FD_ISSET(connection_in, *readsetp)) { |
277 |
|
|
last_client_time = now; |
278 |
|
|
} else if (last_client_time != 0 && last_client_time + |
279 |
|
|
options.client_alive_interval <= now) { |
280 |
|
|
client_alive_check(ssh); |
281 |
|
|
last_client_time = now; |
282 |
|
|
} |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
notify_done(*readsetp); |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
/* |
289 |
|
|
* Processes input from the client and the program. Input data is stored |
290 |
|
|
* in buffers and processed later. |
291 |
|
|
*/ |
292 |
|
|
static int |
293 |
|
|
process_input(struct ssh *ssh, fd_set *readset, int connection_in) |
294 |
|
|
{ |
295 |
|
|
int len; |
296 |
|
|
char buf[16384]; |
297 |
|
|
|
298 |
|
|
/* Read and buffer any input data from the client. */ |
299 |
|
|
if (FD_ISSET(connection_in, readset)) { |
300 |
|
|
len = read(connection_in, buf, sizeof(buf)); |
301 |
|
|
if (len == 0) { |
302 |
|
|
verbose("Connection closed by %.100s port %d", |
303 |
|
|
ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
304 |
|
|
return -1; |
305 |
|
|
} else if (len < 0) { |
306 |
|
|
if (errno != EINTR && errno != EAGAIN) { |
307 |
|
|
verbose("Read error from remote host " |
308 |
|
|
"%.100s port %d: %.100s", |
309 |
|
|
ssh_remote_ipaddr(ssh), |
310 |
|
|
ssh_remote_port(ssh), strerror(errno)); |
311 |
|
|
cleanup_exit(255); |
312 |
|
|
} |
313 |
|
|
} else { |
314 |
|
|
/* Buffer any received data. */ |
315 |
|
|
packet_process_incoming(buf, len); |
316 |
|
|
} |
317 |
|
|
} |
318 |
|
|
return 0; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/* |
322 |
|
|
* Sends data from internal buffers to client program stdin. |
323 |
|
|
*/ |
324 |
|
|
static void |
325 |
|
|
process_output(fd_set *writeset, int connection_out) |
326 |
|
|
{ |
327 |
|
|
/* Send any buffered packet data to the client. */ |
328 |
|
|
if (FD_ISSET(connection_out, writeset)) |
329 |
|
|
packet_write_poll(); |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
static void |
333 |
|
|
process_buffered_input_packets(struct ssh *ssh) |
334 |
|
|
{ |
335 |
|
|
ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
static void |
339 |
|
|
collect_children(struct ssh *ssh) |
340 |
|
|
{ |
341 |
|
|
pid_t pid; |
342 |
|
|
sigset_t oset, nset; |
343 |
|
|
int status; |
344 |
|
|
|
345 |
|
|
/* block SIGCHLD while we check for dead children */ |
346 |
|
|
sigemptyset(&nset); |
347 |
|
|
sigaddset(&nset, SIGCHLD); |
348 |
|
|
sigprocmask(SIG_BLOCK, &nset, &oset); |
349 |
|
|
if (child_terminated) { |
350 |
|
|
debug("Received SIGCHLD."); |
351 |
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
352 |
|
|
(pid < 0 && errno == EINTR)) |
353 |
|
|
if (pid > 0) |
354 |
|
|
session_close_by_pid(ssh, pid, status); |
355 |
|
|
child_terminated = 0; |
356 |
|
|
} |
357 |
|
|
sigprocmask(SIG_SETMASK, &oset, NULL); |
358 |
|
|
} |
359 |
|
|
|
360 |
|
|
void |
361 |
|
|
server_loop2(struct ssh *ssh, Authctxt *authctxt) |
362 |
|
|
{ |
363 |
|
|
fd_set *readset = NULL, *writeset = NULL; |
364 |
|
|
int max_fd; |
365 |
|
|
u_int nalloc = 0, connection_in, connection_out; |
366 |
|
|
u_int64_t rekey_timeout_ms = 0; |
367 |
|
|
|
368 |
|
|
debug("Entering interactive session for SSH2."); |
369 |
|
|
|
370 |
|
|
signal(SIGCHLD, sigchld_handler); |
371 |
|
|
child_terminated = 0; |
372 |
|
|
connection_in = packet_get_connection_in(); |
373 |
|
|
connection_out = packet_get_connection_out(); |
374 |
|
|
|
375 |
|
|
if (!use_privsep) { |
376 |
|
|
signal(SIGTERM, sigterm_handler); |
377 |
|
|
signal(SIGINT, sigterm_handler); |
378 |
|
|
signal(SIGQUIT, sigterm_handler); |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
notify_setup(); |
382 |
|
|
|
383 |
|
|
max_fd = MAXIMUM(connection_in, connection_out); |
384 |
|
|
max_fd = MAXIMUM(max_fd, notify_pipe[0]); |
385 |
|
|
|
386 |
|
|
server_init_dispatch(); |
387 |
|
|
|
388 |
|
|
for (;;) { |
389 |
|
|
process_buffered_input_packets(ssh); |
390 |
|
|
|
391 |
|
|
if (!ssh_packet_is_rekeying(ssh) && |
392 |
|
|
packet_not_very_much_data_to_write()) |
393 |
|
|
channel_output_poll(ssh); |
394 |
|
|
if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) |
395 |
|
|
rekey_timeout_ms = packet_get_rekey_timeout() * 1000; |
396 |
|
|
else |
397 |
|
|
rekey_timeout_ms = 0; |
398 |
|
|
|
399 |
|
|
wait_until_can_do_something(ssh, connection_in, connection_out, |
400 |
|
|
&readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms); |
401 |
|
|
|
402 |
|
|
if (received_sigterm) { |
403 |
|
|
logit("Exiting on signal %d", (int)received_sigterm); |
404 |
|
|
/* Clean up sessions, utmp, etc. */ |
405 |
|
|
cleanup_exit(255); |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
collect_children(ssh); |
409 |
|
|
if (!ssh_packet_is_rekeying(ssh)) |
410 |
|
|
channel_after_select(ssh, readset, writeset); |
411 |
|
|
if (process_input(ssh, readset, connection_in) < 0) |
412 |
|
|
break; |
413 |
|
|
process_output(writeset, connection_out); |
414 |
|
|
} |
415 |
|
|
collect_children(ssh); |
416 |
|
|
|
417 |
|
|
free(readset); |
418 |
|
|
free(writeset); |
419 |
|
|
|
420 |
|
|
/* free all channels, no more reads and writes */ |
421 |
|
|
channel_free_all(ssh); |
422 |
|
|
|
423 |
|
|
/* free remaining sessions, e.g. remove wtmp entries */ |
424 |
|
|
session_destroy_all(ssh, NULL); |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
static int |
428 |
|
|
server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) |
429 |
|
|
{ |
430 |
|
|
debug("Got %d/%u for keepalive", type, seq); |
431 |
|
|
/* |
432 |
|
|
* reset timeout, since we got a sane answer from the client. |
433 |
|
|
* even if this was generated by something other than |
434 |
|
|
* the bogus CHANNEL_REQUEST we send for keepalives. |
435 |
|
|
*/ |
436 |
|
|
packet_set_alive_timeouts(0); |
437 |
|
|
return 0; |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
static Channel * |
441 |
|
|
server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) |
442 |
|
|
{ |
443 |
|
|
Channel *c = NULL; |
444 |
|
|
char *target, *originator; |
445 |
|
|
u_short target_port, originator_port; |
446 |
|
|
|
447 |
|
|
target = packet_get_string(NULL); |
448 |
|
|
target_port = packet_get_int(); |
449 |
|
|
originator = packet_get_string(NULL); |
450 |
|
|
originator_port = packet_get_int(); |
451 |
|
|
packet_check_eom(); |
452 |
|
|
|
453 |
|
|
debug("server_request_direct_tcpip: originator %s port %d, target %s " |
454 |
|
|
"port %d", originator, originator_port, target, target_port); |
455 |
|
|
|
456 |
|
|
/* XXX fine grained permissions */ |
457 |
|
|
if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && |
458 |
|
|
!no_port_forwarding_flag && !options.disable_forwarding) { |
459 |
|
|
c = channel_connect_to_port(ssh, target, target_port, |
460 |
|
|
"direct-tcpip", "direct-tcpip", reason, errmsg); |
461 |
|
|
} else { |
462 |
|
|
logit("refused local port forward: " |
463 |
|
|
"originator %s port %d, target %s port %d", |
464 |
|
|
originator, originator_port, target, target_port); |
465 |
|
|
if (reason != NULL) |
466 |
|
|
*reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
free(originator); |
470 |
|
|
free(target); |
471 |
|
|
|
472 |
|
|
return c; |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
static Channel * |
476 |
|
|
server_request_direct_streamlocal(struct ssh *ssh) |
477 |
|
|
{ |
478 |
|
|
Channel *c = NULL; |
479 |
|
|
char *target, *originator; |
480 |
|
|
u_short originator_port; |
481 |
|
|
struct passwd *pw = the_authctxt->pw; |
482 |
|
|
|
483 |
|
|
if (pw == NULL || !the_authctxt->valid) |
484 |
|
|
fatal("server_input_global_request: no/invalid user"); |
485 |
|
|
|
486 |
|
|
target = packet_get_string(NULL); |
487 |
|
|
originator = packet_get_string(NULL); |
488 |
|
|
originator_port = packet_get_int(); |
489 |
|
|
packet_check_eom(); |
490 |
|
|
|
491 |
|
|
debug("server_request_direct_streamlocal: originator %s port %d, target %s", |
492 |
|
|
originator, originator_port, target); |
493 |
|
|
|
494 |
|
|
/* XXX fine grained permissions */ |
495 |
|
|
if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && |
496 |
|
|
!no_port_forwarding_flag && !options.disable_forwarding && |
497 |
|
|
(pw->pw_uid == 0 || use_privsep)) { |
498 |
|
|
c = channel_connect_to_path(ssh, target, |
499 |
|
|
"direct-streamlocal@openssh.com", "direct-streamlocal"); |
500 |
|
|
} else { |
501 |
|
|
logit("refused streamlocal port forward: " |
502 |
|
|
"originator %s port %d, target %s", |
503 |
|
|
originator, originator_port, target); |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
free(originator); |
507 |
|
|
free(target); |
508 |
|
|
|
509 |
|
|
return c; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
static Channel * |
513 |
|
|
server_request_tun(struct ssh *ssh) |
514 |
|
|
{ |
515 |
|
|
Channel *c = NULL; |
516 |
|
|
int mode, tun; |
517 |
|
|
int sock; |
518 |
|
|
char *tmp, *ifname = NULL; |
519 |
|
|
|
520 |
|
|
mode = packet_get_int(); |
521 |
|
|
switch (mode) { |
522 |
|
|
case SSH_TUNMODE_POINTOPOINT: |
523 |
|
|
case SSH_TUNMODE_ETHERNET: |
524 |
|
|
break; |
525 |
|
|
default: |
526 |
|
|
packet_send_debug("Unsupported tunnel device mode."); |
527 |
|
|
return NULL; |
528 |
|
|
} |
529 |
|
|
if ((options.permit_tun & mode) == 0) { |
530 |
|
|
packet_send_debug("Server has rejected tunnel device " |
531 |
|
|
"forwarding"); |
532 |
|
|
return NULL; |
533 |
|
|
} |
534 |
|
|
|
535 |
|
|
tun = packet_get_int(); |
536 |
|
|
if (forced_tun_device != -1) { |
537 |
|
|
if (tun != SSH_TUNID_ANY && forced_tun_device != tun) |
538 |
|
|
goto done; |
539 |
|
|
tun = forced_tun_device; |
540 |
|
|
} |
541 |
|
|
sock = tun_open(tun, mode, &ifname); |
542 |
|
|
if (sock < 0) |
543 |
|
|
goto done; |
544 |
|
|
debug("Tunnel forwarding using interface %s", ifname); |
545 |
|
|
|
546 |
|
|
c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, |
547 |
|
|
CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); |
548 |
|
|
c->datagram = 1; |
549 |
|
|
|
550 |
|
|
/* |
551 |
|
|
* Update the list of names exposed to the session |
552 |
|
|
* XXX remove these if the tunnels are closed (won't matter |
553 |
|
|
* much if they are already in the environment though) |
554 |
|
|
*/ |
555 |
|
|
tmp = tun_fwd_ifnames; |
556 |
|
|
xasprintf(&tun_fwd_ifnames, "%s%s%s", |
557 |
|
|
tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames, |
558 |
|
|
tun_fwd_ifnames == NULL ? "" : ",", |
559 |
|
|
ifname); |
560 |
|
|
free(tmp); |
561 |
|
|
free(ifname); |
562 |
|
|
|
563 |
|
|
done: |
564 |
|
|
if (c == NULL) |
565 |
|
|
packet_send_debug("Failed to open the tunnel device."); |
566 |
|
|
return c; |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
static Channel * |
570 |
|
|
server_request_session(struct ssh *ssh) |
571 |
|
|
{ |
572 |
|
|
Channel *c; |
573 |
|
|
|
574 |
|
|
debug("input_session_request"); |
575 |
|
|
packet_check_eom(); |
576 |
|
|
|
577 |
|
|
if (no_more_sessions) { |
578 |
|
|
packet_disconnect("Possible attack: attempt to open a session " |
579 |
|
|
"after additional sessions disabled"); |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
/* |
583 |
|
|
* A server session has no fd to read or write until a |
584 |
|
|
* CHANNEL_REQUEST for a shell is made, so we set the type to |
585 |
|
|
* SSH_CHANNEL_LARVAL. Additionally, a callback for handling all |
586 |
|
|
* CHANNEL_REQUEST messages is registered. |
587 |
|
|
*/ |
588 |
|
|
c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, |
589 |
|
|
-1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, |
590 |
|
|
0, "server-session", 1); |
591 |
|
|
if (session_open(the_authctxt, c->self) != 1) { |
592 |
|
|
debug("session open failed, free channel %d", c->self); |
593 |
|
|
channel_free(ssh, c); |
594 |
|
|
return NULL; |
595 |
|
|
} |
596 |
|
|
channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); |
597 |
|
|
return c; |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
static int |
601 |
|
|
server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) |
602 |
|
|
{ |
603 |
|
|
Channel *c = NULL; |
604 |
|
|
char *ctype; |
605 |
|
|
const char *errmsg = NULL; |
606 |
|
|
int rchan, reason = SSH2_OPEN_CONNECT_FAILED; |
607 |
|
|
u_int rmaxpack, rwindow, len; |
608 |
|
|
|
609 |
|
|
ctype = packet_get_string(&len); |
610 |
|
|
rchan = packet_get_int(); |
611 |
|
|
rwindow = packet_get_int(); |
612 |
|
|
rmaxpack = packet_get_int(); |
613 |
|
|
|
614 |
|
|
debug("server_input_channel_open: ctype %s rchan %d win %d max %d", |
615 |
|
|
ctype, rchan, rwindow, rmaxpack); |
616 |
|
|
|
617 |
|
|
if (strcmp(ctype, "session") == 0) { |
618 |
|
|
c = server_request_session(ssh); |
619 |
|
|
} else if (strcmp(ctype, "direct-tcpip") == 0) { |
620 |
|
|
c = server_request_direct_tcpip(ssh, &reason, &errmsg); |
621 |
|
|
} else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { |
622 |
|
|
c = server_request_direct_streamlocal(ssh); |
623 |
|
|
} else if (strcmp(ctype, "tun@openssh.com") == 0) { |
624 |
|
|
c = server_request_tun(ssh); |
625 |
|
|
} |
626 |
|
|
if (c != NULL) { |
627 |
|
|
debug("server_input_channel_open: confirm %s", ctype); |
628 |
|
|
c->remote_id = rchan; |
629 |
|
|
c->have_remote_id = 1; |
630 |
|
|
c->remote_window = rwindow; |
631 |
|
|
c->remote_maxpacket = rmaxpack; |
632 |
|
|
if (c->type != SSH_CHANNEL_CONNECTING) { |
633 |
|
|
packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
634 |
|
|
packet_put_int(c->remote_id); |
635 |
|
|
packet_put_int(c->self); |
636 |
|
|
packet_put_int(c->local_window); |
637 |
|
|
packet_put_int(c->local_maxpacket); |
638 |
|
|
packet_send(); |
639 |
|
|
} |
640 |
|
|
} else { |
641 |
|
|
debug("server_input_channel_open: failure %s", ctype); |
642 |
|
|
packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
643 |
|
|
packet_put_int(rchan); |
644 |
|
|
packet_put_int(reason); |
645 |
|
|
if (!(datafellows & SSH_BUG_OPENFAILURE)) { |
646 |
|
|
packet_put_cstring(errmsg ? errmsg : "open failed"); |
647 |
|
|
packet_put_cstring(""); |
648 |
|
|
} |
649 |
|
|
packet_send(); |
650 |
|
|
} |
651 |
|
|
free(ctype); |
652 |
|
|
return 0; |
653 |
|
|
} |
654 |
|
|
|
655 |
|
|
static int |
656 |
|
|
server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp) |
657 |
|
|
{ |
658 |
|
|
struct sshbuf *resp = NULL; |
659 |
|
|
struct sshbuf *sigbuf = NULL; |
660 |
|
|
struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; |
661 |
|
|
int r, ndx, success = 0; |
662 |
|
|
const u_char *blob; |
663 |
|
|
u_char *sig = 0; |
664 |
|
|
size_t blen, slen; |
665 |
|
|
|
666 |
|
|
if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) |
667 |
|
|
fatal("%s: sshbuf_new", __func__); |
668 |
|
|
|
669 |
|
|
while (ssh_packet_remaining(ssh) > 0) { |
670 |
|
|
sshkey_free(key); |
671 |
|
|
key = NULL; |
672 |
|
|
if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || |
673 |
|
|
(r = sshkey_from_blob(blob, blen, &key)) != 0) { |
674 |
|
|
error("%s: couldn't parse key: %s", |
675 |
|
|
__func__, ssh_err(r)); |
676 |
|
|
goto out; |
677 |
|
|
} |
678 |
|
|
/* |
679 |
|
|
* Better check that this is actually one of our hostkeys |
680 |
|
|
* before attempting to sign anything with it. |
681 |
|
|
*/ |
682 |
|
|
if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { |
683 |
|
|
error("%s: unknown host %s key", |
684 |
|
|
__func__, sshkey_type(key)); |
685 |
|
|
goto out; |
686 |
|
|
} |
687 |
|
|
/* |
688 |
|
|
* XXX refactor: make kex->sign just use an index rather |
689 |
|
|
* than passing in public and private keys |
690 |
|
|
*/ |
691 |
|
|
if ((key_prv = get_hostkey_by_index(ndx)) == NULL && |
692 |
|
|
(key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { |
693 |
|
|
error("%s: can't retrieve hostkey %d", __func__, ndx); |
694 |
|
|
goto out; |
695 |
|
|
} |
696 |
|
|
sshbuf_reset(sigbuf); |
697 |
|
|
free(sig); |
698 |
|
|
sig = NULL; |
699 |
|
|
if ((r = sshbuf_put_cstring(sigbuf, |
700 |
|
|
"hostkeys-prove-00@openssh.com")) != 0 || |
701 |
|
|
(r = sshbuf_put_string(sigbuf, |
702 |
|
|
ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || |
703 |
|
|
(r = sshkey_puts(key, sigbuf)) != 0 || |
704 |
|
|
(r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, |
705 |
|
|
sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 || |
706 |
|
|
(r = sshbuf_put_string(resp, sig, slen)) != 0) { |
707 |
|
|
error("%s: couldn't prepare signature: %s", |
708 |
|
|
__func__, ssh_err(r)); |
709 |
|
|
goto out; |
710 |
|
|
} |
711 |
|
|
} |
712 |
|
|
/* Success */ |
713 |
|
|
*respp = resp; |
714 |
|
|
resp = NULL; /* don't free it */ |
715 |
|
|
success = 1; |
716 |
|
|
out: |
717 |
|
|
free(sig); |
718 |
|
|
sshbuf_free(resp); |
719 |
|
|
sshbuf_free(sigbuf); |
720 |
|
|
sshkey_free(key); |
721 |
|
|
return success; |
722 |
|
|
} |
723 |
|
|
|
724 |
|
|
static int |
725 |
|
|
server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) |
726 |
|
|
{ |
727 |
|
|
char *rtype; |
728 |
|
|
int want_reply; |
729 |
|
|
int r, success = 0, allocated_listen_port = 0; |
730 |
|
|
struct sshbuf *resp = NULL; |
731 |
|
|
struct passwd *pw = the_authctxt->pw; |
732 |
|
|
|
733 |
|
|
if (pw == NULL || !the_authctxt->valid) |
734 |
|
|
fatal("server_input_global_request: no/invalid user"); |
735 |
|
|
|
736 |
|
|
rtype = packet_get_string(NULL); |
737 |
|
|
want_reply = packet_get_char(); |
738 |
|
|
debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); |
739 |
|
|
|
740 |
|
|
/* -R style forwarding */ |
741 |
|
|
if (strcmp(rtype, "tcpip-forward") == 0) { |
742 |
|
|
struct Forward fwd; |
743 |
|
|
|
744 |
|
|
memset(&fwd, 0, sizeof(fwd)); |
745 |
|
|
fwd.listen_host = packet_get_string(NULL); |
746 |
|
|
fwd.listen_port = (u_short)packet_get_int(); |
747 |
|
|
debug("server_input_global_request: tcpip-forward listen %s port %d", |
748 |
|
|
fwd.listen_host, fwd.listen_port); |
749 |
|
|
|
750 |
|
|
/* check permissions */ |
751 |
|
|
if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || |
752 |
|
|
no_port_forwarding_flag || options.disable_forwarding || |
753 |
|
|
(!want_reply && fwd.listen_port == 0) || |
754 |
|
|
(fwd.listen_port != 0 && |
755 |
|
|
!bind_permitted(fwd.listen_port, pw->pw_uid))) { |
756 |
|
|
success = 0; |
757 |
|
|
packet_send_debug("Server has disabled port forwarding."); |
758 |
|
|
} else { |
759 |
|
|
/* Start listening on the port */ |
760 |
|
|
success = channel_setup_remote_fwd_listener(ssh, &fwd, |
761 |
|
|
&allocated_listen_port, &options.fwd_opts); |
762 |
|
|
} |
763 |
|
|
free(fwd.listen_host); |
764 |
|
|
if ((resp = sshbuf_new()) == NULL) |
765 |
|
|
fatal("%s: sshbuf_new", __func__); |
766 |
|
|
if (allocated_listen_port != 0 && |
767 |
|
|
(r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) |
768 |
|
|
fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); |
769 |
|
|
} else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { |
770 |
|
|
struct Forward fwd; |
771 |
|
|
|
772 |
|
|
memset(&fwd, 0, sizeof(fwd)); |
773 |
|
|
fwd.listen_host = packet_get_string(NULL); |
774 |
|
|
fwd.listen_port = (u_short)packet_get_int(); |
775 |
|
|
debug("%s: cancel-tcpip-forward addr %s port %d", __func__, |
776 |
|
|
fwd.listen_host, fwd.listen_port); |
777 |
|
|
|
778 |
|
|
success = channel_cancel_rport_listener(ssh, &fwd); |
779 |
|
|
free(fwd.listen_host); |
780 |
|
|
} else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { |
781 |
|
|
struct Forward fwd; |
782 |
|
|
|
783 |
|
|
memset(&fwd, 0, sizeof(fwd)); |
784 |
|
|
fwd.listen_path = packet_get_string(NULL); |
785 |
|
|
debug("server_input_global_request: streamlocal-forward listen path %s", |
786 |
|
|
fwd.listen_path); |
787 |
|
|
|
788 |
|
|
/* check permissions */ |
789 |
|
|
if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 |
790 |
|
|
|| no_port_forwarding_flag || options.disable_forwarding || |
791 |
|
|
(pw->pw_uid != 0 && !use_privsep)) { |
792 |
|
|
success = 0; |
793 |
|
|
packet_send_debug("Server has disabled " |
794 |
|
|
"streamlocal forwarding."); |
795 |
|
|
} else { |
796 |
|
|
/* Start listening on the socket */ |
797 |
|
|
success = channel_setup_remote_fwd_listener(ssh, |
798 |
|
|
&fwd, NULL, &options.fwd_opts); |
799 |
|
|
} |
800 |
|
|
free(fwd.listen_path); |
801 |
|
|
} else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { |
802 |
|
|
struct Forward fwd; |
803 |
|
|
|
804 |
|
|
memset(&fwd, 0, sizeof(fwd)); |
805 |
|
|
fwd.listen_path = packet_get_string(NULL); |
806 |
|
|
debug("%s: cancel-streamlocal-forward path %s", __func__, |
807 |
|
|
fwd.listen_path); |
808 |
|
|
|
809 |
|
|
success = channel_cancel_rport_listener(ssh, &fwd); |
810 |
|
|
free(fwd.listen_path); |
811 |
|
|
} else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { |
812 |
|
|
no_more_sessions = 1; |
813 |
|
|
success = 1; |
814 |
|
|
} else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { |
815 |
|
|
success = server_input_hostkeys_prove(ssh, &resp); |
816 |
|
|
} |
817 |
|
|
if (want_reply) { |
818 |
|
|
packet_start(success ? |
819 |
|
|
SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
820 |
|
|
if (success && resp != NULL) |
821 |
|
|
ssh_packet_put_raw(ssh, sshbuf_ptr(resp), |
822 |
|
|
sshbuf_len(resp)); |
823 |
|
|
packet_send(); |
824 |
|
|
packet_write_wait(); |
825 |
|
|
} |
826 |
|
|
free(rtype); |
827 |
|
|
sshbuf_free(resp); |
828 |
|
|
return 0; |
829 |
|
|
} |
830 |
|
|
|
831 |
|
|
static int |
832 |
|
|
server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) |
833 |
|
|
{ |
834 |
|
|
Channel *c; |
835 |
|
|
int id, reply, success = 0; |
836 |
|
|
char *rtype; |
837 |
|
|
|
838 |
|
|
id = packet_get_int(); |
839 |
|
|
rtype = packet_get_string(NULL); |
840 |
|
|
reply = packet_get_char(); |
841 |
|
|
|
842 |
|
|
debug("server_input_channel_req: channel %d request %s reply %d", |
843 |
|
|
id, rtype, reply); |
844 |
|
|
|
845 |
|
|
if ((c = channel_lookup(ssh, id)) == NULL) |
846 |
|
|
packet_disconnect("server_input_channel_req: " |
847 |
|
|
"unknown channel %d", id); |
848 |
|
|
if (!strcmp(rtype, "eow@openssh.com")) { |
849 |
|
|
packet_check_eom(); |
850 |
|
|
chan_rcvd_eow(ssh, c); |
851 |
|
|
} else if ((c->type == SSH_CHANNEL_LARVAL || |
852 |
|
|
c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) |
853 |
|
|
success = session_input_channel_req(ssh, c, rtype); |
854 |
|
|
if (reply && !(c->flags & CHAN_CLOSE_SENT)) { |
855 |
|
|
if (!c->have_remote_id) |
856 |
|
|
fatal("%s: channel %d: no remote_id", |
857 |
|
|
__func__, c->self); |
858 |
|
|
packet_start(success ? |
859 |
|
|
SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
860 |
|
|
packet_put_int(c->remote_id); |
861 |
|
|
packet_send(); |
862 |
|
|
} |
863 |
|
|
free(rtype); |
864 |
|
|
return 0; |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
static void |
868 |
|
|
server_init_dispatch(void) |
869 |
|
|
{ |
870 |
|
|
debug("server_init_dispatch"); |
871 |
|
|
dispatch_init(&dispatch_protocol_error); |
872 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
873 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
874 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
875 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
876 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); |
877 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
878 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
879 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); |
880 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
881 |
|
|
dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
882 |
|
|
/* client_alive */ |
883 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); |
884 |
|
|
dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); |
885 |
|
|
dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); |
886 |
|
|
dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); |
887 |
|
|
/* rekeying */ |
888 |
|
|
dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
889 |
|
|
} |