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

View file

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

View file

@ -356,6 +356,10 @@ impl Connection {
modes: reader.read_string()?,
})),
"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,
};