+int
+input_direct_tcpip(void)
+{
+ int sock;
+ char *host, *originator;
+ int host_port, originator_port;
+
+ host = packet_get_string(NULL);
+ host_port = packet_get_int();
+ originator = packet_get_string(NULL);
+ originator_port = packet_get_int();
+ /* XXX check permission */
+ sock = channel_connect_to(host, host_port);
+ xfree(host);
+ xfree(originator);
+ if (sock < 0)
+ return -1;
+ return channel_new("direct-tcpip", SSH_CHANNEL_OPEN,
+ sock, sock, -1, 4*1024, 32*1024, 0, xstrdup("direct-tcpip"));
+}
+
+void
+server_input_channel_open(int type, int plen)
+{
+ Channel *c = NULL;
+ char *ctype;
+ int id;
+ unsigned int len;
+ int rchan;
+ int rmaxpack;
+ int rwindow;
+
+ ctype = packet_get_string(&len);
+ rchan = packet_get_int();
+ rwindow = packet_get_int();
+ rmaxpack = packet_get_int();
+
+ log("channel_input_open: ctype %s rchan %d win %d max %d",
+ ctype, rchan, rwindow, rmaxpack);
+
+ if (strcmp(ctype, "session") == 0) {
+ debug("open session");
+ /*
+ * A server session has no fd to read or write
+ * until a CHANNEL_REQUEST for a shell is made,
+ * so we set the type to SSH_CHANNEL_LARVAL.
+ * Additionally, a callback for handling all
+ * CHANNEL_REQUEST messages is registered.
+ */
+ id = channel_new(ctype, SSH_CHANNEL_LARVAL,
+ -1, -1, -1, 0, 32*1024, 0, xstrdup("server-session"));
+ if (session_open(id) == 1) {
+ channel_register_callback(id, SSH2_MSG_CHANNEL_REQUEST,
+ session_input_channel_req, (void *)0);
+ channel_register_cleanup(id, session_close_by_channel);
+ c = channel_lookup(id);
+ } else {
+ debug("session open failed, free channel %d", id);
+ channel_free(id);
+ }
+ } else if (strcmp(ctype, "direct-tcpip") == 0) {
+ debug("open direct-tcpip");
+ id = input_direct_tcpip();
+ if (id >= 0)
+ c = channel_lookup(id);
+ }
+ if (c != NULL) {
+ debug("confirm %s", ctype);
+ c->remote_id = rchan;
+ c->remote_window = rwindow;
+ c->remote_maxpacket = rmaxpack;
+
+ packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
+ packet_put_int(c->remote_id);
+ packet_put_int(c->self);
+ packet_put_int(c->local_window);
+ packet_put_int(c->local_maxpacket);
+ packet_send();
+ } else {
+ debug("failure %s", ctype);
+ packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
+ packet_put_int(rchan);
+ packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
+ packet_put_cstring("bla bla");
+ packet_put_cstring("");
+ packet_send();
+ }
+ xfree(ctype);
+}
+
+void
+server_init_dispatch_20()
+{
+ debug("server_init_dispatch_20");
+ dispatch_init(&dispatch_protocol_error);
+ dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
+ dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
+ dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
+ dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
+ dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open);
+ dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
+ dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
+ dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request);
+ dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
+}