1
0
Fork 0
mirror of https://gitlab.redox-os.org/CoffeeCode/redox-ssh.git synced 2025-12-28 17:02:19 +01:00

Feature: Allow setting environment variables

This commit is contained in:
Laurenz 2024-10-07 16:22:45 +02:00
parent d9af15ae5a
commit 1386c1dc4c
Signed by: C0ffeeCode
SSH key fingerprint: SHA256:jnEltBNftC3wUZESLSMvM9zVPOkkevGRzqqoW2k2ORI
3 changed files with 27 additions and 17 deletions

View file

@ -26,12 +26,14 @@ pub struct Channel {
peer_window_size: u32, peer_window_size: u32,
max_packet_size: u32, max_packet_size: u32,
read_thread: Option<JoinHandle<()>>, read_thread: Option<JoinHandle<()>>,
env: Vec<(String, String)>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ChannelRequest { pub enum ChannelRequest {
Pty(PtyConfig), Pty(PtyConfig),
Shell, Shell,
Env(String, String),
} }
impl Channel { impl Channel {
@ -52,6 +54,7 @@ impl Channel {
peer_window_size, peer_window_size,
max_packet_size, max_packet_size,
read_thread: None, read_thread: None,
env: Vec::new(),
} }
} }
@ -68,11 +71,12 @@ impl Channel {
} }
pub fn handle_request(&mut self, request: ChannelRequest) { pub fn handle_request(&mut self, request: ChannelRequest) {
debug!("Channel Request: {:?}", request);
match request { match request {
ChannelRequest::Pty(ref pty) => self.setup_tty(pty), ChannelRequest::Pty(ref pty) => self.setup_tty(pty),
ChannelRequest::Shell => self.setup_shell(), ChannelRequest::Shell => self.setup_shell(),
ChannelRequest::Env(key, value) => self.env.push((key, value)),
} }
debug!("Channel Request: {:?}", request);
} }
/// Writes `data` **to** this channel; /// Writes `data` **to** this channel;

View file

@ -21,7 +21,7 @@ impl Channel {
match self.pty.as_ref() { match self.pty.as_ref() {
Some((_, tty_path)) => with_tty(tty_path), Some((_, tty_path)) => with_tty(tty_path),
None => { None => {
let pipes = without_tty(); let pipes = self.without_tty();
#[cfg(unix)] #[cfg(unix)]
use crate::sys::non_blockify_reader; use crate::sys::non_blockify_reader;
non_blockify_reader(pipes.stdout.get_ref()); non_blockify_reader(pipes.stdout.get_ref());
@ -30,11 +30,12 @@ impl Channel {
}, },
} }
} }
}
fn without_tty() -> PipeContainer { fn without_tty(&mut self) -> PipeContainer {
let proc = unsafe { let proc = unsafe {
process::Command::new("/bin/sh") process::Command::new("/bin/sh")
// .env_clear()
.envs(self.env.drain(..))
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
@ -49,6 +50,7 @@ fn without_tty() -> PipeContainer {
stderr: BufReader::with_capacity(1, proc.stderr.unwrap()), stderr: BufReader::with_capacity(1, proc.stderr.unwrap()),
} }
} }
}
fn with_tty(tty_path: &PathBuf) { fn with_tty(tty_path: &PathBuf) {
let stdin = open_tty(tty_path, true, true); let stdin = open_tty(tty_path, true, true);

View file

@ -356,6 +356,10 @@ impl Connection {
modes: reader.read_string()?, modes: reader.read_string()?,
})), })),
"shell" => Some(ChannelRequest::Shell), "shell" => Some(ChannelRequest::Shell),
"env" => Some(ChannelRequest::Env(
String::from_utf8(reader.read_string()?).unwrap(), // TODO: error handling
String::from_utf8(reader.read_string()?).unwrap(),
)),
_ => None, _ => None,
}; };