Skip to content

Commit 9444608

Browse files
authored
Add a way to open an agent forwarding channel (#344)
This works in concert with AgentClient from russh_keys to provide access to client's ssh agent when agent forwarding is enabled.
1 parent 4d54f0c commit 9444608

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

russh/src/server/session.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct Session {
2626
}
2727
#[derive(Debug)]
2828
pub enum Msg {
29+
ChannelOpenAgent {
30+
channel_ref: ChannelRef,
31+
},
2932
ChannelOpenSession {
3033
channel_ref: ChannelRef,
3134
},
@@ -206,6 +209,23 @@ impl Handle {
206209
}
207210
}
208211

212+
/// Open an agent forwarding channel. This can be used once the client has
213+
/// confirmed that it allows agent forwarding. See
214+
/// [PROTOCOL.agent](https://datatracker.ietf.org/doc/html/draft-miller-ssh-agent).
215+
pub async fn channel_open_agent(&self) -> Result<Channel<Msg>, Error> {
216+
let (sender, receiver) = unbounded_channel();
217+
let channel_ref = ChannelRef::new(sender);
218+
let window_size_ref = channel_ref.window_size().clone();
219+
220+
self.sender
221+
.send(Msg::ChannelOpenAgent { channel_ref })
222+
.await
223+
.map_err(|_| Error::SendError)?;
224+
225+
self.wait_channel_confirmation(receiver, window_size_ref)
226+
.await
227+
}
228+
209229
/// Request a session channel (the most basic type of
210230
/// channel). This function returns `Ok(..)` immediately if the
211231
/// connection is authenticated, but the channel only becomes
@@ -535,6 +555,10 @@ impl Session {
535555
Some(Msg::Channel(id, ChannelMsg::WindowAdjusted { new_size })) => {
536556
debug!("window adjusted to {:?} for channel {:?}", new_size, id);
537557
}
558+
Some(Msg::ChannelOpenAgent { channel_ref }) => {
559+
let id = self.channel_open_agent()?;
560+
self.channels.insert(id, channel_ref);
561+
}
538562
Some(Msg::ChannelOpenSession { channel_ref }) => {
539563
let id = self.channel_open_session()?;
540564
self.channels.insert(id, channel_ref);

0 commit comments

Comments
 (0)