2022-09-25 22:35:15 +02:00
|
|
|
use std::collections::HashMap;
|
2022-09-25 20:06:38 +02:00
|
|
|
use std::fs::{OpenOptions, File};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
use futures::FutureExt;
|
|
|
|
use log::info;
|
2022-09-28 17:09:51 +02:00
|
|
|
use rand::{Rng, thread_rng};
|
2022-09-25 20:06:38 +02:00
|
|
|
use russh::server::{Auth, Session};
|
|
|
|
use russh::*;
|
|
|
|
|
|
|
|
fn send_str(session: &mut Session, channel: ChannelId, s: String) {
|
|
|
|
let data = CryptoVec::from(s);
|
|
|
|
session.data(channel, data);
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:35:15 +02:00
|
|
|
enum ChannelState {
|
|
|
|
Shell,
|
|
|
|
Scp,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum CommandStatus {
|
|
|
|
Done,
|
|
|
|
Scp,
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:09:51 +02:00
|
|
|
const UNAME_RESPONSES: &[&str] = &[
|
|
|
|
"Linux fnordister 5.10.0-10-amd64 #1 SMP Debian 5.10.84-1 (2021-12-08) x86_64 GNU/Linux",
|
|
|
|
"Linux raspberrypi 5.19.9 #1-NixOS SMP Thu Sep 15 08:47:20 UTC 2022 aarch64 GNU/Linux",
|
|
|
|
"Linux OpenWrt 5.10.138 #0 Sat Sep 3 02:55:34 2022 mips GNU/Linux",
|
|
|
|
];
|
|
|
|
|
2022-09-25 20:06:38 +02:00
|
|
|
pub struct Handler {
|
|
|
|
filename: String,
|
|
|
|
lazy_file: Option<File>,
|
|
|
|
buffer: Vec<u8>,
|
|
|
|
user: String,
|
2022-09-25 22:35:15 +02:00
|
|
|
channels: HashMap<ChannelId, ChannelState>,
|
2022-09-25 20:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
pub fn new<P: ToString>(path: P) -> Self {
|
|
|
|
Handler {
|
|
|
|
filename: path.to_string(),
|
|
|
|
lazy_file: None,
|
|
|
|
buffer: vec![],
|
|
|
|
user: "root".into(),
|
2022-09-25 22:35:15 +02:00
|
|
|
channels: HashMap::new(),
|
2022-09-25 20:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file(&mut self) -> &mut File {
|
|
|
|
if self.lazy_file.is_none() {
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.append(true)
|
|
|
|
.open(&self.filename)
|
|
|
|
.unwrap();
|
|
|
|
self.lazy_file = Some(file);
|
|
|
|
}
|
|
|
|
self.lazy_file.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_prompt(&self, session: &mut Session, channel: ChannelId) {
|
|
|
|
send_str(session, channel, format!("{}@fnordister:~$ ", self.user));
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:35:15 +02:00
|
|
|
fn handle_command<F: FnMut(&str)>(&self, command: String, mut respond: F) -> CommandStatus {
|
2022-09-25 20:06:38 +02:00
|
|
|
let program_len = command.find(|c: char| c.is_whitespace())
|
|
|
|
.unwrap_or(command.len());
|
|
|
|
let program = &command[..program_len];
|
|
|
|
|
|
|
|
match program {
|
|
|
|
"whoami" => respond(&self.user),
|
|
|
|
"id" => respond(&format!("uid=0({}) gid=0(root)", self.user)),
|
2022-09-28 17:09:51 +02:00
|
|
|
"uname" => {
|
|
|
|
let mut rnd = thread_rng();
|
|
|
|
let i = rnd.gen_range(0..UNAME_RESPONSES.len());
|
|
|
|
respond(&UNAME_RESPONSES[i]);
|
|
|
|
}
|
2022-09-25 20:06:38 +02:00
|
|
|
"pwd" => respond("/"),
|
|
|
|
"ls" => {
|
|
|
|
respond("drwxr-xr-x 18 root root 18 Jan 4 1969 .");
|
|
|
|
respond("drwxr-xr-x 18 root root 18 Jan 4 1969 ..");
|
|
|
|
respond("drwxr-xr-x 18 root root 18 Jan 4 0000 ...");
|
|
|
|
},
|
2022-09-25 22:35:15 +02:00
|
|
|
"scp" => return CommandStatus::Scp,
|
2022-09-25 20:06:38 +02:00
|
|
|
"bash" => {}
|
|
|
|
"sh" => {}
|
|
|
|
"cd" => {}
|
|
|
|
"" => {}
|
|
|
|
_ => respond(&format!("{}: command not found", program)),
|
|
|
|
}
|
2022-09-25 22:35:15 +02:00
|
|
|
|
|
|
|
CommandStatus::Done
|
2022-09-25 20:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl server::Handler for Handler {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
type FutureAuth =
|
|
|
|
Pin<Box<dyn core::future::Future<Output = anyhow::Result<(Self, Auth)>> + Send>>;
|
|
|
|
type FutureUnit =
|
|
|
|
Pin<Box<dyn core::future::Future<Output = anyhow::Result<(Self, Session)>> + Send>>;
|
|
|
|
type FutureBool =
|
|
|
|
Pin<Box<dyn core::future::Future<Output = anyhow::Result<(Self, Session, bool)>> + Send>>;
|
|
|
|
|
|
|
|
fn finished_auth(self, auth: Auth) -> Self::FutureAuth {
|
|
|
|
async { Ok((self, auth)) }.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finished_bool(self, b: bool, s: Session) -> Self::FutureBool {
|
|
|
|
async move { Ok((self, s, b)) }.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finished(self, s: Session) -> Self::FutureUnit {
|
|
|
|
async { Ok((self, s)) }.boxed()
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:34:26 +02:00
|
|
|
fn shell_request(mut self, channel: ChannelId, mut session: Session) -> Self::FutureUnit {
|
|
|
|
info!("shell_request {}", channel);
|
|
|
|
self.channels.insert(channel, ChannelState::Shell);
|
2022-09-25 20:06:38 +02:00
|
|
|
self.send_prompt(&mut session, channel);
|
|
|
|
|
|
|
|
self.finished(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec_request(mut self, channel: ChannelId, data: &[u8], mut session: Session) -> Self::FutureUnit {
|
2022-09-25 22:34:26 +02:00
|
|
|
info!("exec_request {} {}", channel, String::from_utf8_lossy(data));
|
2022-09-25 20:06:38 +02:00
|
|
|
writeln!(self.file(), "Execute: {}\n", String::from_utf8_lossy(data))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let line = String::from_utf8_lossy(data).into();
|
2022-09-25 22:34:26 +02:00
|
|
|
let status = self.handle_command(line, |response| {
|
2022-09-25 20:06:38 +02:00
|
|
|
let mut data = Vec::from(response);
|
|
|
|
data.extend_from_slice(b"\r\n");
|
|
|
|
session.data(channel, data.into());
|
|
|
|
});
|
2022-09-25 22:34:26 +02:00
|
|
|
match status {
|
|
|
|
CommandStatus::Scp => {
|
|
|
|
session.data(channel, vec![0].into());
|
|
|
|
self.channels.insert(channel, ChannelState::Scp);
|
|
|
|
}
|
|
|
|
CommandStatus::Done => {
|
|
|
|
session.close(channel);
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 20:06:38 +02:00
|
|
|
|
|
|
|
self.finished(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem_request(
|
|
|
|
mut self,
|
2022-09-25 22:34:26 +02:00
|
|
|
channel: ChannelId,
|
2022-09-25 20:06:38 +02:00
|
|
|
name: &str,
|
|
|
|
session: Session
|
|
|
|
) -> Self::FutureUnit {
|
2022-09-25 22:34:26 +02:00
|
|
|
info!("subsystem_request {}", channel);
|
2022-09-25 20:06:38 +02:00
|
|
|
writeln!(self.file(), "Subsystem requested: {}\n", name)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
self.finished(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fn channel_open_session(
|
|
|
|
// self,
|
|
|
|
// channel: ChannelId,
|
|
|
|
// mut session: Session
|
|
|
|
// ) -> Self::FutureBool {
|
|
|
|
// info!("channel_open_session");
|
2022-09-25 22:35:15 +02:00
|
|
|
// // session.channel_success(channel);
|
2022-09-25 20:06:38 +02:00
|
|
|
// self.finished_bool(true, session)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn channel_open_confirmation(
|
|
|
|
// self,
|
|
|
|
// id: ChannelId,
|
|
|
|
// max_packet_size: u32,
|
|
|
|
// window_size: u32,
|
|
|
|
// mut session: Session
|
|
|
|
// ) -> Self::FutureUnit {
|
2022-09-25 22:35:15 +02:00
|
|
|
// self.finished(session)
|
|
|
|
// }
|
2022-09-25 20:06:38 +02:00
|
|
|
|
2022-09-25 22:35:15 +02:00
|
|
|
// fn channel_close(self, channel: ChannelId, session: Session) -> Self::FutureUnit {
|
|
|
|
// info!("channel_close {}", channel);
|
2022-09-25 20:06:38 +02:00
|
|
|
// self.finished(session)
|
|
|
|
// }
|
|
|
|
|
2022-09-25 22:35:15 +02:00
|
|
|
fn channel_eof(mut self, channel: ChannelId, mut session: Session) -> Self::FutureUnit {
|
|
|
|
session.close(channel);
|
|
|
|
self.channels.remove(&channel);
|
|
|
|
|
|
|
|
self.finished(session)
|
|
|
|
}
|
|
|
|
|
2022-09-25 20:06:38 +02:00
|
|
|
fn auth_password(mut self, user: &str, password: &str) -> Self::FutureAuth {
|
|
|
|
writeln!(self.file(), "Authenticated as {} with {}\n", user, password)
|
|
|
|
.unwrap();
|
|
|
|
self.user = user.into();
|
|
|
|
self.finished_auth(server::Auth::Accept)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data(mut self, channel: ChannelId, data: &[u8], mut session: Session) -> Self::FutureUnit {
|
|
|
|
self.file().write(data)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-25 22:35:15 +02:00
|
|
|
match self.channels.get(&channel) {
|
|
|
|
Some(ChannelState::Shell) => {
|
|
|
|
// echo input back
|
|
|
|
session.data(channel, data.to_vec().into());
|
|
|
|
|
|
|
|
if self.buffer.len() < 1024 {
|
|
|
|
self.buffer.extend_from_slice(data);
|
|
|
|
}
|
|
|
|
if let Some(newline) = self.buffer.iter().position(|b| *b == b'\r') {
|
|
|
|
let rest = self.buffer.split_off(newline + 1);
|
2022-09-25 23:10:28 +02:00
|
|
|
let line: String = String::from_utf8_lossy(&self.buffer).into();
|
2022-09-25 22:35:15 +02:00
|
|
|
self.buffer = rest;
|
|
|
|
|
|
|
|
session.data(channel, b"\n".to_vec().into());
|
|
|
|
self.handle_command(line, |response| {
|
|
|
|
let mut data = Vec::from(response);
|
|
|
|
data.extend_from_slice(b"\r\n");
|
|
|
|
session.data(channel, data.into());
|
|
|
|
});
|
|
|
|
self.send_prompt(&mut session, channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ChannelState::Scp) => {
|
|
|
|
if self.buffer.len() < 1024 {
|
|
|
|
self.buffer.extend_from_slice(data);
|
|
|
|
}
|
|
|
|
if let Some(newline) = self.buffer.iter().position(|b| *b == b'\n') {
|
|
|
|
let rest = self.buffer.split_off(newline + 1);
|
2022-09-25 23:10:28 +02:00
|
|
|
// let line: String = String::from_utf8_lossy(&self.buffer).into();
|
2022-09-25 22:35:15 +02:00
|
|
|
self.buffer = rest;
|
|
|
|
|
|
|
|
session.data(channel, vec![0].into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
|
|
|
info!("data on unidentified channel {}", channel);
|
|
|
|
}
|
2022-09-25 20:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.finished(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fn extended_data(
|
|
|
|
// self,
|
|
|
|
// channel: ChannelId,
|
|
|
|
// code: u32,
|
|
|
|
// data: &[u8],
|
|
|
|
// session: Session
|
|
|
|
// ) -> Self::FutureUnit {
|
|
|
|
// info!("extended_data {} {} {:?}", channel, code, data);
|
|
|
|
// self.finished(session)
|
|
|
|
// }
|
|
|
|
}
|