From 688394cac9fbf5057f1109fc4d2d6adeb7589a9a Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:53:03 +0200 Subject: [PATCH] Add tests --- bin/cluelesshd/src/main.rs | 28 +++++++++++++++++-- .../openssh-client/different-algorithms.sh | 12 ++++++++ .../openssh-client/interactive-no-pty.sh | 6 ++++ .../tests/openssh-client/interactive-pty.sh | 3 ++ bin/cluelesshd/tests/run.sh | 25 +++++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 bin/cluelesshd/tests/openssh-client/different-algorithms.sh create mode 100644 bin/cluelesshd/tests/openssh-client/interactive-no-pty.sh create mode 100644 bin/cluelesshd/tests/openssh-client/interactive-pty.sh create mode 100755 bin/cluelesshd/tests/run.sh diff --git a/bin/cluelesshd/src/main.rs b/bin/cluelesshd/src/main.rs index cd05106..b0c91ab 100644 --- a/bin/cluelesshd/src/main.rs +++ b/bin/cluelesshd/src/main.rs @@ -222,6 +222,8 @@ struct SessionState { process_exit_send: mpsc::Sender>, process_exit_recv: mpsc::Receiver>, + envs: Vec<(String, String)>, + writer: Option, reader: Option, } @@ -235,6 +237,7 @@ async fn handle_session_channel(user: String, channel: Channel) -> Result<()> { channel, process_exit_send, process_exit_recv, + envs: Vec::new(), writer: None, reader: None, }; @@ -338,8 +341,25 @@ impl SessionState { ChannelRequest::Exec { .. } => { todo!() } - ChannelRequest::ExitStatus { .. } => {} - ChannelRequest::Env { .. } => {} + ChannelRequest::Env { + name, + value, + want_reply, + } => match String::from_utf8(value) { + Ok(value) => { + self.envs.push((name, value)); + if want_reply { + self.channel.send(ChannelOperationKind::Success).await?; + } + } + Err(_) => { + debug!("Trying to set"); + if want_reply { + self.channel.send(ChannelOperationKind::Failure).await?; + } + } + }, + ChannelRequest::ExitStatus { .. } => unreachable!("forbidden"), }; } ChannelUpdateKind::OpenFailed { .. } => todo!(), @@ -389,6 +409,10 @@ impl SessionState { cmd.uid(user.uid()); cmd.gid(user.primary_group_id()); + for (k, v) in &self.envs { + cmd.env(k, v); + } + debug!(cmd = %shell.display(), uid = %user.uid(), gid = %user.primary_group_id(), "Executing process"); let mut shell = cmd.spawn()?; diff --git a/bin/cluelesshd/tests/openssh-client/different-algorithms.sh b/bin/cluelesshd/tests/openssh-client/different-algorithms.sh new file mode 100644 index 0000000..57815c9 --- /dev/null +++ b/bin/cluelesshd/tests/openssh-client/different-algorithms.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# KEX +printf $"exit\r" | ssh -oKexAlgorithms=curve25519-sha256 -p "$PORT" "$HOST" +printf $"exit\r" | ssh -oKexAlgorithms=ecdh-sha2-nistp256 -p "$PORT" "$HOST" + +# Encryption +printf $"exit\r" | ssh -oCiphers=chacha20-poly1305@openssh.com -p "$PORT" "$HOST" +printf $"exit\r" | ssh -oCiphers=aes256-gcm@openssh.com -p "$PORT" "$HOST" + +# Host Key +printf $"exit\r" | ssh -oHostKeyAlgorithms=ssh-ed25519 -p "$PORT" "$HOST" diff --git a/bin/cluelesshd/tests/openssh-client/interactive-no-pty.sh b/bin/cluelesshd/tests/openssh-client/interactive-no-pty.sh new file mode 100644 index 0000000..e7c81dd --- /dev/null +++ b/bin/cluelesshd/tests/openssh-client/interactive-no-pty.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# disabled, TODO +exit 0 + +echo | ssh -p "$PORT" "$HOST" diff --git a/bin/cluelesshd/tests/openssh-client/interactive-pty.sh b/bin/cluelesshd/tests/openssh-client/interactive-pty.sh new file mode 100644 index 0000000..fefea58 --- /dev/null +++ b/bin/cluelesshd/tests/openssh-client/interactive-pty.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +printf $"exit\r" | ssh -oRequestTTY=force -p "$PORT" "$HOST" diff --git a/bin/cluelesshd/tests/run.sh b/bin/cluelesshd/tests/run.sh new file mode 100755 index 0000000..3902ada --- /dev/null +++ b/bin/cluelesshd/tests/run.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +set -euo pipefail + +script_dir=$(realpath "$(dirname "$0")") + +cd "$script_dir/.." + +cargo build + +"../../target/debug/cluelesshd" & + +pid=$! + +kill_server() { + echo "Killing server" + kill "$pid" +} + +trap kill_server EXIT + +for script in "$script_dir"/openssh-client/*.sh; do + echo "-------------- Running $script" + PORT=2223 HOST=localhost bash -euo pipefail "$script" +done