Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions russh/examples/echoserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ impl server::Handler for Server {
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: server::ChannelOpenHandle,
session: &mut Session,
) -> Result<bool, Self::Error> {
) -> Result<(), Self::Error> {
{
let mut clients = self.clients.lock().await;
clients.insert(self.id, (channel.id(), session.handle()));
}
Ok(true)
reply.accept().await;
Ok(())
}

async fn auth_publickey(
Expand Down
6 changes: 4 additions & 2 deletions russh/examples/ratatui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ impl Handler for AppServer {
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: ChannelOpenHandle,
session: &mut Session,
) -> Result<bool, Self::Error> {
) -> Result<(), Self::Error> {
let terminal_handle = TerminalHandle::start(session.handle(), channel.id()).await;

let backend = CrosstermBackend::new(terminal_handle);
Expand All @@ -164,7 +165,8 @@ impl Handler for AppServer {
let mut clients = self.clients.lock().await;
clients.insert(self.id, (terminal, app));

Ok(true)
reply.accept().await;
Ok(())
}

async fn auth_publickey(&mut self, _: &str, _: &PublicKey) -> Result<Auth, Self::Error> {
Expand Down
6 changes: 4 additions & 2 deletions russh/examples/ratatui_shared_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ impl Handler for AppServer {
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: ChannelOpenHandle,
session: &mut Session,
) -> Result<bool, Self::Error> {
) -> Result<(), Self::Error> {
let terminal_handle = TerminalHandle::start(session.handle(), channel.id()).await;

let backend = CrosstermBackend::new(terminal_handle);
Expand All @@ -165,7 +166,8 @@ impl Handler for AppServer {
let mut clients = self.clients.lock().await;
clients.insert(self.id, terminal);

Ok(true)
reply.accept().await;
Ok(())
}

async fn auth_publickey(&mut self, _: &str, _: &PublicKey) -> Result<Auth, Self::Error> {
Expand Down
6 changes: 4 additions & 2 deletions russh/examples/sftp_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ impl russh::server::Handler for SshSession {
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: russh::server::ChannelOpenHandle,
_session: &mut Session,
) -> Result<bool, Self::Error> {
) -> Result<(), Self::Error> {
{
let mut clients = self.clients.lock().await;
clients.insert(channel.id(), channel);
}
Ok(true)
reply.accept().await;
Ok(())
}

async fn channel_eof(
Expand Down
6 changes: 4 additions & 2 deletions russh/examples/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ impl server::Handler for Server {
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: server::ChannelOpenHandle,
_session: &mut Session,
) -> Result<bool, Self::Error> {
) -> Result<(), Self::Error> {
{
debug!("channel open session");
let mut clients = self.clients.lock().unwrap();
clients.insert((self.id, channel.id()), channel);
}
Ok(true)
reply.accept().await;
Ok(())
}

/// The client requests a shell.
Expand Down
258 changes: 121 additions & 137 deletions russh/src/client/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ssh_key::Algorithm;
use super::IncomingSshPacket;
use crate::auth::AuthRequest;
use crate::cert::PublicKeyOrCertificate;
use crate::client::{Handler, Msg, Prompt, Reply, Session};
use crate::client::{ChannelOpenHandle, Handler, Msg, Prompt, Reply, Session};
use crate::helpers::{AlgorithmExt, EncodedExt, NameList, sign_with_hash_alg};
use crate::keys::key::parse_public_key;
use crate::parsing::{ChannelOpenConfirmation, ChannelType, OpenChannelMessage, ensure_end};
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Session {
debug!("channel_open_failure");
let channel_num = map_err!(ChannelId::decode(&mut r))?;
let reason_code = ChannelOpenFailure::from_u32(map_err!(u32::decode(&mut r))?)
.unwrap_or(ChannelOpenFailure::Unknown);
.unwrap_or(ChannelOpenFailure::AdministrativelyProhibited);
let descr = map_err!(String::decode(&mut r))?;
let language = map_err!(String::decode(&mut r))?;
map_err!(ensure_end(&r))?;
Expand All @@ -441,7 +441,7 @@ impl Session {
}

if let Some(sender) = self.channels.remove(&channel_num) {
let _ = sender.send(ChannelMsg::OpenFailure(reason_code)).await;
let _ = sender.send(ChannelMsg::OpenFailure(reason_code.clone())).await;
}

let _ = self.sender.send(Reply::ChannelOpenFailure);
Expand Down Expand Up @@ -677,127 +677,129 @@ impl Session {
Some((&msg::CHANNEL_OPEN, mut r)) => {
let msg = OpenChannelMessage::parse(&mut r)?;

if let Some(ref mut enc) = self.common.encrypted {
let id = enc.new_channel_id();
let channel = ChannelParams {
recipient_channel: msg.recipient_channel,
sender_channel: id,
recipient_window_size: msg.recipient_window_size,
sender_window_size: self.common.config.window_size,
recipient_maximum_packet_size: msg.recipient_maximum_packet_size,
sender_maximum_packet_size: self.common.config.maximum_packet_size,
confirmed: true,
wants_reply: false,
pending_data: std::collections::VecDeque::new(),
pending_eof: false,
pending_close: false,
};
let id = if let Some(ref mut enc) = self.common.encrypted {
enc.new_channel_id()
} else {
return Err(crate::Error::Inconsistent.into());
};

let confirm = || {
debug!("confirming channel: {msg:?}");
map_err!(msg.confirm(
&mut enc.write,
id.0,
channel.sender_window_size,
channel.sender_maximum_packet_size,
))?;
enc.channels.insert(id, channel);
Ok(())
};
let channel_params = ChannelParams {
recipient_channel: msg.recipient_channel,
sender_channel: id,
recipient_window_size: msg.recipient_window_size,
sender_window_size: self.common.config.window_size,
recipient_maximum_packet_size: msg.recipient_maximum_packet_size,
sender_maximum_packet_size: self.common.config.maximum_packet_size,
confirmed: true,
wants_reply: false,
pending_data: std::collections::VecDeque::new(),
pending_eof: false,
pending_close: false,
};

match &msg.typ {
ChannelType::Session => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client.server_channel_open_session(channel, self).await?
}
ChannelType::DirectTcpip(d) => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_direct_tcpip(
channel,
&d.host_to_connect,
d.port_to_connect,
&d.originator_address,
d.originator_port,
self,
)
.await?
}
ChannelType::DirectStreamLocal(d) => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_direct_streamlocal(
channel,
&d.socket_path,
self,
)
.await?
}
ChannelType::X11 {
originator_address,
originator_port,
} => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_x11(
channel,
originator_address,
*originator_port,
self,
)
.await?
}
ChannelType::ForwardedTcpIp(d) => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_forwarded_tcpip(
channel,
&d.host_to_connect,
d.port_to_connect,
&d.originator_address,
d.originator_port,
self,
)
.await?
}
ChannelType::ForwardedStreamLocal(d) => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_forwarded_streamlocal(
channel,
&d.socket_path,
self,
)
.await?;
}
ChannelType::AgentForward => {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client
.server_channel_open_agent_forward(channel, self)
.await?
}
ChannelType::Unknown { typ } => {
if client.should_accept_unknown_server_channel(id, typ).await {
confirm()?;
let channel = self.accept_server_initiated_channel(id, &msg);
client.server_channel_open_unknown(channel, self).await?;
} else {
debug!("unknown channel type: {typ}");
let (channel, channel_ref) = Channel::new(
id,
self.inbound_channel_sender.clone(),
channel_params.recipient_maximum_packet_size,
channel_params.recipient_window_size,
self.common.config.channel_buffer_size,
);

let pending = crate::PendingChannelOpen {
recipient_channel: msg.recipient_channel,
sender_channel: id,
window_size: self.common.config.window_size,
packet_size: self.common.config.maximum_packet_size,
channel_ref,
channel_params,
};
let reply = ChannelOpenHandle::new(
self.inbound_channel_sender.clone(),
pending,
|pending, result| Msg::ServerChannelOpenReply { pending, result },
);

match &msg.typ {
ChannelType::Session => {
client.server_channel_open_session(channel, reply, self).await?
}
ChannelType::DirectTcpip(d) => {
client
.server_channel_open_direct_tcpip(
channel,
&d.host_to_connect,
d.port_to_connect,
&d.originator_address,
d.originator_port,
reply,
self,
)
.await?
}
ChannelType::DirectStreamLocal(d) => {
client
.server_channel_open_direct_streamlocal(
channel,
&d.socket_path,
reply,
self,
)
.await?
}
ChannelType::X11 {
originator_address,
originator_port,
} => {
client
.server_channel_open_x11(
channel,
originator_address,
*originator_port,
reply,
self,
)
.await?
}
ChannelType::ForwardedTcpIp(d) => {
client
.server_channel_open_forwarded_tcpip(
channel,
&d.host_to_connect,
d.port_to_connect,
&d.originator_address,
d.originator_port,
reply,
self,
)
.await?
}
ChannelType::ForwardedStreamLocal(d) => {
client
.server_channel_open_forwarded_streamlocal(
channel,
&d.socket_path,
reply,
self,
)
.await?
}
ChannelType::AgentForward => {
client
.server_channel_open_agent_forward(channel, reply, self)
.await?
}
ChannelType::Unknown { typ } => {
if client.should_accept_unknown_server_channel(id, typ).await {
client.server_channel_open_unknown(channel, reply, self).await?;
} else {
debug!("unknown channel type: {typ}");
if let Some(ref mut enc) = self.common.encrypted {
msg.unknown_type(&mut enc.write)?;
}
}
};
Ok(())
} else {
Err(crate::Error::Inconsistent.into())
}
}
};
Ok(())
}
Some((&msg::REQUEST_SUCCESS, mut r)) => {
trace!("Global Request Success");
Expand Down Expand Up @@ -894,24 +896,6 @@ impl Session {
}
}

fn accept_server_initiated_channel(
&mut self,
id: ChannelId,
msg: &OpenChannelMessage,
) -> Channel<Msg> {
let (channel, channel_ref) = Channel::new(
id,
self.inbound_channel_sender.clone(),
msg.recipient_maximum_packet_size,
msg.recipient_window_size,
self.common.config.channel_buffer_size,
);

self.channels.insert(id, channel_ref);

channel
}

pub(crate) fn write_auth_request_if_needed(
&mut self,
user: &str,
Expand Down
Loading
Loading