1 /* $OpenBSD: serverloop.c,v 1.209 2018/07/27 05:13:02 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Server main loop for handling the interactive session.
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".
14 * SSH2 support by Markus Friedl.
15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
47 #include <netinet/in.h>
58 #include "openbsd-compat/sys-queue.h"
77 #include "auth-options.h"
78 #include "serverloop.h"
81 extern ServerOptions options;
84 extern Authctxt *the_authctxt;
85 extern struct sshauthopt *auth_opts;
86 extern int use_privsep;
88 static int no_more_sessions = 0; /* Disallow further sessions. */
91 * This SIGCHLD kludge is used to detect when the child exits. The server
92 * will exit after that, as soon as forwarded connections have terminated.
95 static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */
97 /* Cleanup on signals (!use_privsep case only) */
98 static volatile sig_atomic_t received_sigterm = 0;
101 static void server_init_dispatch(void);
103 /* requested tunnel forwarding interface(s), shared with session.c */
104 char *tun_fwd_ifnames = NULL;
106 /* returns 1 if bind to specified port by specified user is permitted */
108 bind_permitted(int port, uid_t uid)
111 return 1; /* allow system to decide */
112 if (port < IPPORT_RESERVED && uid != 0)
118 * we write to this pipe if a SIGCHLD is caught in order to avoid
119 * the race between select() and child_terminated
121 static int notify_pipe[2];
125 if (pipe(notify_pipe) < 0) {
126 error("pipe(notify_pipe) failed %s", strerror(errno));
127 } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
128 (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) {
129 error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno));
130 close(notify_pipe[0]);
131 close(notify_pipe[1]);
133 set_nonblock(notify_pipe[0]);
134 set_nonblock(notify_pipe[1]);
137 notify_pipe[0] = -1; /* read end */
138 notify_pipe[1] = -1; /* write end */
143 if (notify_pipe[1] != -1)
144 (void)write(notify_pipe[1], "", 1);
147 notify_prepare(fd_set *readset)
149 if (notify_pipe[0] != -1)
150 FD_SET(notify_pipe[0], readset);
153 notify_done(fd_set *readset)
157 if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset))
158 while (read(notify_pipe[0], &c, 1) != -1)
159 debug2("%s: reading", __func__);
164 sigchld_handler(int sig)
166 int save_errno = errno;
167 child_terminated = 1;
174 sigterm_handler(int sig)
176 received_sigterm = sig;
180 client_alive_check(struct ssh *ssh)
185 /* timeout, check to see how many we have had */
186 if (packet_inc_alive_timeouts() > options.client_alive_count_max) {
187 sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
188 logit("Timeout, client not responding from %s", remote_id);
193 * send a bogus global/channel request with "wantreply",
194 * we should get back a failure
196 if ((channel_id = channel_find_open(ssh)) == -1) {
197 packet_start(SSH2_MSG_GLOBAL_REQUEST);
198 packet_put_cstring("keepalive@openssh.com");
199 packet_put_char(1); /* boolean: want reply */
201 channel_request_start(ssh, channel_id,
202 "keepalive@openssh.com", 1);
208 * Sleep in select() until we can do something. This will initialize the
209 * select masks. Upon return, the masks will indicate which descriptors
210 * have data or can accept data. Optionally, a maximum time can be specified
211 * for the duration of the wait (0 = infinite).
214 wait_until_can_do_something(struct ssh *ssh,
215 int connection_in, int connection_out,
216 fd_set **readsetp, fd_set **writesetp, int *maxfdp,
217 u_int *nallocp, u_int64_t max_time_ms)
219 struct timeval tv, *tvp;
221 time_t minwait_secs = 0;
222 int client_alive_scheduled = 0;
223 static time_t last_client_time;
225 /* Allocate and update select() masks for channel descriptors. */
226 channel_prepare_select(ssh, readsetp, writesetp, maxfdp,
227 nallocp, &minwait_secs);
229 /* XXX need proper deadline system for rekey/client alive */
230 if (minwait_secs != 0)
231 max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000);
234 * if using client_alive, set the max timeout accordingly,
235 * and indicate that this particular timeout was for client
236 * alive by setting the client_alive_scheduled flag.
238 * this could be randomized somewhat to make traffic
239 * analysis more difficult, but we're not doing it yet.
241 if (options.client_alive_interval) {
242 uint64_t keepalive_ms =
243 (uint64_t)options.client_alive_interval * 1000;
245 client_alive_scheduled = 1;
246 if (max_time_ms == 0 || max_time_ms > keepalive_ms)
247 max_time_ms = keepalive_ms;
251 /* wrong: bad condition XXX */
252 if (channel_not_very_much_buffered_data())
254 FD_SET(connection_in, *readsetp);
255 notify_prepare(*readsetp);
258 * If we have buffered packet data going to the client, mark that
261 if (packet_have_data_to_write())
262 FD_SET(connection_out, *writesetp);
265 * If child has terminated and there is enough buffer space to read
266 * from it, then read as much as is available and exit.
268 if (child_terminated && packet_not_very_much_data_to_write())
269 if (max_time_ms == 0 || client_alive_scheduled)
272 if (max_time_ms == 0)
275 tv.tv_sec = max_time_ms / 1000;
276 tv.tv_usec = 1000 * (max_time_ms % 1000);
280 /* Wait for something to happen, or the timeout to expire. */
281 ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
284 memset(*readsetp, 0, *nallocp);
285 memset(*writesetp, 0, *nallocp);
287 error("select: %.100s", strerror(errno));
288 } else if (client_alive_scheduled) {
289 time_t now = monotime();
291 if (ret == 0) { /* timeout */
292 client_alive_check(ssh);
293 } else if (FD_ISSET(connection_in, *readsetp)) {
294 last_client_time = now;
295 } else if (last_client_time != 0 && last_client_time +
296 options.client_alive_interval <= now) {
297 client_alive_check(ssh);
298 last_client_time = now;
302 notify_done(*readsetp);
306 * Processes input from the client and the program. Input data is stored
307 * in buffers and processed later.
310 process_input(struct ssh *ssh, fd_set *readset, int connection_in)
315 /* Read and buffer any input data from the client. */
316 if (FD_ISSET(connection_in, readset)) {
317 len = read(connection_in, buf, sizeof(buf));
319 verbose("Connection closed by %.100s port %d",
320 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
322 } else if (len < 0) {
323 if (errno != EINTR && errno != EAGAIN &&
324 errno != EWOULDBLOCK) {
325 verbose("Read error from remote host "
326 "%.100s port %d: %.100s",
327 ssh_remote_ipaddr(ssh),
328 ssh_remote_port(ssh), strerror(errno));
332 /* Buffer any received data. */
333 packet_process_incoming(buf, len);
340 * Sends data from internal buffers to client program stdin.
343 process_output(fd_set *writeset, int connection_out)
345 /* Send any buffered packet data to the client. */
346 if (FD_ISSET(connection_out, writeset))
351 process_buffered_input_packets(struct ssh *ssh)
353 ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL);
357 collect_children(struct ssh *ssh)
363 /* block SIGCHLD while we check for dead children */
365 sigaddset(&nset, SIGCHLD);
366 sigprocmask(SIG_BLOCK, &nset, &oset);
367 if (child_terminated) {
368 debug("Received SIGCHLD.");
369 while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
370 (pid < 0 && errno == EINTR))
372 session_close_by_pid(ssh, pid, status);
373 child_terminated = 0;
375 sigprocmask(SIG_SETMASK, &oset, NULL);
379 server_loop2(struct ssh *ssh, Authctxt *authctxt)
381 fd_set *readset = NULL, *writeset = NULL;
383 u_int nalloc = 0, connection_in, connection_out;
384 u_int64_t rekey_timeout_ms = 0;
386 debug("Entering interactive session for SSH2.");
388 signal(SIGCHLD, sigchld_handler);
389 child_terminated = 0;
390 connection_in = packet_get_connection_in();
391 connection_out = packet_get_connection_out();
394 signal(SIGTERM, sigterm_handler);
395 signal(SIGINT, sigterm_handler);
396 signal(SIGQUIT, sigterm_handler);
401 max_fd = MAXIMUM(connection_in, connection_out);
402 max_fd = MAXIMUM(max_fd, notify_pipe[0]);
404 server_init_dispatch();
407 process_buffered_input_packets(ssh);
409 if (!ssh_packet_is_rekeying(ssh) &&
410 packet_not_very_much_data_to_write())
411 channel_output_poll(ssh);
412 if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh))
413 rekey_timeout_ms = packet_get_rekey_timeout() * 1000;
415 rekey_timeout_ms = 0;
417 wait_until_can_do_something(ssh, connection_in, connection_out,
418 &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms);
420 if (received_sigterm) {
421 logit("Exiting on signal %d", (int)received_sigterm);
422 /* Clean up sessions, utmp, etc. */
426 collect_children(ssh);
427 if (!ssh_packet_is_rekeying(ssh))
428 channel_after_select(ssh, readset, writeset);
429 if (process_input(ssh, readset, connection_in) < 0)
431 process_output(writeset, connection_out);
433 collect_children(ssh);
438 /* free all channels, no more reads and writes */
439 channel_free_all(ssh);
441 /* free remaining sessions, e.g. remove wtmp entries */
442 session_destroy_all(ssh, NULL);
446 server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh)
448 debug("Got %d/%u for keepalive", type, seq);
450 * reset timeout, since we got a sane answer from the client.
451 * even if this was generated by something other than
452 * the bogus CHANNEL_REQUEST we send for keepalives.
454 packet_set_alive_timeouts(0);
459 server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg)
462 char *target, *originator;
463 u_short target_port, originator_port;
465 target = packet_get_string(NULL);
466 target_port = packet_get_int();
467 originator = packet_get_string(NULL);
468 originator_port = packet_get_int();
471 debug("%s: originator %s port %d, target %s port %d", __func__,
472 originator, originator_port, target, target_port);
474 /* XXX fine grained permissions */
475 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 &&
476 auth_opts->permit_port_forwarding_flag &&
477 !options.disable_forwarding) {
478 c = channel_connect_to_port(ssh, target, target_port,
479 "direct-tcpip", "direct-tcpip", reason, errmsg);
481 logit("refused local port forward: "
482 "originator %s port %d, target %s port %d",
483 originator, originator_port, target, target_port);
485 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
495 server_request_direct_streamlocal(struct ssh *ssh)
498 char *target, *originator;
499 u_short originator_port;
500 struct passwd *pw = the_authctxt->pw;
502 if (pw == NULL || !the_authctxt->valid)
503 fatal("%s: no/invalid user", __func__);
505 target = packet_get_string(NULL);
506 originator = packet_get_string(NULL);
507 originator_port = packet_get_int();
510 debug("%s: originator %s port %d, target %s", __func__,
511 originator, originator_port, target);
513 /* XXX fine grained permissions */
514 if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 &&
515 auth_opts->permit_port_forwarding_flag &&
516 !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) {
517 c = channel_connect_to_path(ssh, target,
518 "direct-streamlocal@openssh.com", "direct-streamlocal");
520 logit("refused streamlocal port forward: "
521 "originator %s port %d, target %s",
522 originator, originator_port, target);
532 server_request_tun(struct ssh *ssh)
536 char *tmp, *ifname = NULL;
538 mode = packet_get_int();
540 case SSH_TUNMODE_POINTOPOINT:
541 case SSH_TUNMODE_ETHERNET:
544 packet_send_debug("Unsupported tunnel device mode.");
547 if ((options.permit_tun & mode) == 0) {
548 packet_send_debug("Server has rejected tunnel device "
553 tun = packet_get_int();
554 if (auth_opts->force_tun_device != -1) {
555 if (tun != SSH_TUNID_ANY && auth_opts->force_tun_device != tun)
557 tun = auth_opts->force_tun_device;
559 sock = tun_open(tun, mode, &ifname);
562 debug("Tunnel forwarding using interface %s", ifname);
564 c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1,
565 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
567 #if defined(SSH_TUN_FILTER)
568 if (mode == SSH_TUNMODE_POINTOPOINT)
569 channel_register_filter(ssh, c->self, sys_tun_infilter,
570 sys_tun_outfilter, NULL, NULL);
574 * Update the list of names exposed to the session
575 * XXX remove these if the tunnels are closed (won't matter
576 * much if they are already in the environment though)
578 tmp = tun_fwd_ifnames;
579 xasprintf(&tun_fwd_ifnames, "%s%s%s",
580 tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames,
581 tun_fwd_ifnames == NULL ? "" : ",",
588 packet_send_debug("Failed to open the tunnel device.");
593 server_request_session(struct ssh *ssh)
597 debug("input_session_request");
600 if (no_more_sessions) {
601 packet_disconnect("Possible attack: attempt to open a session "
602 "after additional sessions disabled");
606 * A server session has no fd to read or write until a
607 * CHANNEL_REQUEST for a shell is made, so we set the type to
608 * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all
609 * CHANNEL_REQUEST messages is registered.
611 c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL,
612 -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
613 0, "server-session", 1);
614 if (session_open(the_authctxt, c->self) != 1) {
615 debug("session open failed, free channel %d", c->self);
616 channel_free(ssh, c);
619 channel_register_cleanup(ssh, c->self, session_close_by_channel, 0);
624 server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
628 const char *errmsg = NULL;
629 int rchan, reason = SSH2_OPEN_CONNECT_FAILED;
630 u_int rmaxpack, rwindow, len;
632 ctype = packet_get_string(&len);
633 rchan = packet_get_int();
634 rwindow = packet_get_int();
635 rmaxpack = packet_get_int();
637 debug("%s: ctype %s rchan %d win %d max %d", __func__,
638 ctype, rchan, rwindow, rmaxpack);
640 if (strcmp(ctype, "session") == 0) {
641 c = server_request_session(ssh);
642 } else if (strcmp(ctype, "direct-tcpip") == 0) {
643 c = server_request_direct_tcpip(ssh, &reason, &errmsg);
644 } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) {
645 c = server_request_direct_streamlocal(ssh);
646 } else if (strcmp(ctype, "tun@openssh.com") == 0) {
647 c = server_request_tun(ssh);
650 debug("%s: confirm %s", __func__, ctype);
651 c->remote_id = rchan;
652 c->have_remote_id = 1;
653 c->remote_window = rwindow;
654 c->remote_maxpacket = rmaxpack;
655 if (c->type != SSH_CHANNEL_CONNECTING) {
656 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
657 packet_put_int(c->remote_id);
658 packet_put_int(c->self);
659 packet_put_int(c->local_window);
660 packet_put_int(c->local_maxpacket);
664 debug("%s: failure %s", __func__, ctype);
665 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
666 packet_put_int(rchan);
667 packet_put_int(reason);
668 packet_put_cstring(errmsg ? errmsg : "open failed");
669 packet_put_cstring("");
677 server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp)
679 struct sshbuf *resp = NULL;
680 struct sshbuf *sigbuf = NULL;
681 struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
682 int r, ndx, kexsigtype, use_kexsigtype, success = 0;
687 if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
688 fatal("%s: sshbuf_new", __func__);
690 kexsigtype = sshkey_type_plain(
691 sshkey_type_from_name(ssh->kex->hostkey_alg));
692 while (ssh_packet_remaining(ssh) > 0) {
695 if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 ||
696 (r = sshkey_from_blob(blob, blen, &key)) != 0) {
697 error("%s: couldn't parse key: %s",
698 __func__, ssh_err(r));
702 * Better check that this is actually one of our hostkeys
703 * before attempting to sign anything with it.
705 if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) {
706 error("%s: unknown host %s key",
707 __func__, sshkey_type(key));
711 * XXX refactor: make kex->sign just use an index rather
712 * than passing in public and private keys
714 if ((key_prv = get_hostkey_by_index(ndx)) == NULL &&
715 (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) {
716 error("%s: can't retrieve hostkey %d", __func__, ndx);
719 sshbuf_reset(sigbuf);
723 * For RSA keys, prefer to use the signature type negotiated
724 * during KEX to the default (SHA1).
726 use_kexsigtype = kexsigtype == KEY_RSA &&
727 sshkey_type_plain(key->type) == KEY_RSA;
728 if ((r = sshbuf_put_cstring(sigbuf,
729 "hostkeys-prove-00@openssh.com")) != 0 ||
730 (r = sshbuf_put_string(sigbuf,
731 ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
732 (r = sshkey_puts(key, sigbuf)) != 0 ||
733 (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen,
734 sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
735 use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0 ||
736 (r = sshbuf_put_string(resp, sig, slen)) != 0) {
737 error("%s: couldn't prepare signature: %s",
738 __func__, ssh_err(r));
744 resp = NULL; /* don't free it */
755 server_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
759 int r, success = 0, allocated_listen_port = 0;
760 struct sshbuf *resp = NULL;
761 struct passwd *pw = the_authctxt->pw;
763 if (pw == NULL || !the_authctxt->valid)
764 fatal("%s: no/invalid user", __func__);
766 rtype = packet_get_string(NULL);
767 want_reply = packet_get_char();
768 debug("%s: rtype %s want_reply %d", __func__, rtype, want_reply);
770 /* -R style forwarding */
771 if (strcmp(rtype, "tcpip-forward") == 0) {
774 memset(&fwd, 0, sizeof(fwd));
775 fwd.listen_host = packet_get_string(NULL);
776 fwd.listen_port = (u_short)packet_get_int();
777 debug("%s: tcpip-forward listen %s port %d", __func__,
778 fwd.listen_host, fwd.listen_port);
780 /* check permissions */
781 if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 ||
782 !auth_opts->permit_port_forwarding_flag ||
783 options.disable_forwarding ||
784 (!want_reply && fwd.listen_port == 0) ||
785 (fwd.listen_port != 0 &&
786 !bind_permitted(fwd.listen_port, pw->pw_uid))) {
788 packet_send_debug("Server has disabled port forwarding.");
790 /* Start listening on the port */
791 success = channel_setup_remote_fwd_listener(ssh, &fwd,
792 &allocated_listen_port, &options.fwd_opts);
794 free(fwd.listen_host);
795 if ((resp = sshbuf_new()) == NULL)
796 fatal("%s: sshbuf_new", __func__);
797 if (allocated_listen_port != 0 &&
798 (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0)
799 fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r));
800 } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) {
803 memset(&fwd, 0, sizeof(fwd));
804 fwd.listen_host = packet_get_string(NULL);
805 fwd.listen_port = (u_short)packet_get_int();
806 debug("%s: cancel-tcpip-forward addr %s port %d", __func__,
807 fwd.listen_host, fwd.listen_port);
809 success = channel_cancel_rport_listener(ssh, &fwd);
810 free(fwd.listen_host);
811 } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) {
814 memset(&fwd, 0, sizeof(fwd));
815 fwd.listen_path = packet_get_string(NULL);
816 debug("%s: streamlocal-forward listen path %s", __func__,
819 /* check permissions */
820 if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0
821 || !auth_opts->permit_port_forwarding_flag ||
822 options.disable_forwarding ||
823 (pw->pw_uid != 0 && !use_privsep)) {
825 packet_send_debug("Server has disabled "
826 "streamlocal forwarding.");
828 /* Start listening on the socket */
829 success = channel_setup_remote_fwd_listener(ssh,
830 &fwd, NULL, &options.fwd_opts);
832 free(fwd.listen_path);
833 } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) {
836 memset(&fwd, 0, sizeof(fwd));
837 fwd.listen_path = packet_get_string(NULL);
838 debug("%s: cancel-streamlocal-forward path %s", __func__,
841 success = channel_cancel_rport_listener(ssh, &fwd);
842 free(fwd.listen_path);
843 } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) {
844 no_more_sessions = 1;
846 } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) {
847 success = server_input_hostkeys_prove(ssh, &resp);
850 packet_start(success ?
851 SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
852 if (success && resp != NULL)
853 ssh_packet_put_raw(ssh, sshbuf_ptr(resp),
864 server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
867 int id, reply, success = 0;
870 id = packet_get_int();
871 rtype = packet_get_string(NULL);
872 reply = packet_get_char();
874 debug("server_input_channel_req: channel %d request %s reply %d",
877 if ((c = channel_lookup(ssh, id)) == NULL)
878 packet_disconnect("server_input_channel_req: "
879 "unknown channel %d", id);
880 if (!strcmp(rtype, "eow@openssh.com")) {
882 chan_rcvd_eow(ssh, c);
883 } else if ((c->type == SSH_CHANNEL_LARVAL ||
884 c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0)
885 success = session_input_channel_req(ssh, c, rtype);
886 if (reply && !(c->flags & CHAN_CLOSE_SENT)) {
887 if (!c->have_remote_id)
888 fatal("%s: channel %d: no remote_id",
890 packet_start(success ?
891 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
892 packet_put_int(c->remote_id);
900 server_init_dispatch(void)
902 debug("server_init_dispatch");
903 dispatch_init(&dispatch_protocol_error);
904 dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
905 dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
906 dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
907 dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
908 dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
909 dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
910 dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
911 dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req);
912 dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
913 dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request);
915 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive);
916 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive);
917 dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive);
918 dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive);
920 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);