//! Local servers for tests and live checks (feature `testserver`): an SFTP //! server built on `russh` and a small FTP server, both bound to //! `127.0.0.1` on a free port and serving one folder. They never listen on //! another interface and are not part of the shipped app. //! //! The SFTP server has a fixed Ed25519 host key (from a seed) and accepts //! one user with one password, or one public key. The FTP server speaks //! enough of RFC 959 for Lutra's client: USER, PASS, SYST, FEAT, TYPE, //! PWD, CWD, MKD, SIZE, PASV, STOR, RETR, LIST, NOOP, QUIT. Both stop when //! dropped. use std::collections::HashMap; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::net::{SocketAddr, TcpListener, TcpStream}; use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use russh::keys::ssh_key::private::Ed25519Keypair; use russh::keys::{HashAlg, PrivateKey, PublicKey}; use russh::server::{Auth, Msg, Session}; use russh::{Channel, ChannelId}; use russh_sftp::protocol::{ Attrs, Data, File as SftpFile, FileAttributes, Handle, Name, OpenFlags, Status, StatusCode, Version, }; use tokio::sync::Notify; use crate::mapping::normalize_relative; /// The path under `root` of the server path `path`, relative to `cwd`; /// `None` when it would leave the root. fn resolve(root: &Path, cwd: &str, path: &str) -> Option { let joined = if path.starts_with('/') { path.to_string() } else { format!("{cwd}/{path}") }; let rel = normalize_relative(&joined)?; Some(if rel.is_empty() { root.to_path_buf() } else { root.join(rel) }) } /// SFTP attributes of a local file, with the POSIX type bits. fn attrs(meta: &std::fs::Metadata) -> FileAttributes { FileAttributes { size: Some(meta.len()), permissions: Some(if meta.is_dir() { 0o040_755 } else { 0o100_644 }), mtime: Some(0), atime: Some(0), ..FileAttributes::default() } } /// A running SFTP server. pub struct SftpServer { /// Its address. pub addr: SocketAddr, /// Its host key's fingerprint, `SHA256:…`. pub fingerprint: String, /// Its host key's algorithm. pub algorithm: String, /// Stops it. stop: Arc, } impl Drop for SftpServer { fn drop(&mut self) { self.stop.notify_waiters(); self.stop.notify_one(); } } /// Who may sign in to the SFTP server. #[derive(Clone)] struct Credentials { /// The user name. user: String, /// The accepted password. password: Option, /// The accepted public key. key: Option, } /// A fixed host key for the test server, from `seed`. pub fn host_key(seed: u8) -> PrivateKey { PrivateKey::from(Ed25519Keypair::from_seed(&[seed; 32])) } impl SftpServer { /// Starts a server for `root` that accepts `user` with `password` or /// with `key`. `seed` picks the host key, so a test can simulate a /// changed one. /// /// # Errors /// An I/O error when no port can be bound or the runtime fails. pub fn start( root: &Path, user: &str, password: Option<&str>, key: Option, seed: u8, ) -> std::io::Result { let host = host_key(seed); let fingerprint = host.public_key().fingerprint(HashAlg::Sha256).to_string(); let algorithm = host.public_key().algorithm().to_string(); let config = Arc::new(russh::server::Config { keys: vec![host], auth_rejection_time: Duration::from_millis(10), auth_rejection_time_initial: Some(Duration::from_millis(0)), inactivity_timeout: Some(Duration::from_secs(60)), ..Default::default() }); let listener = TcpListener::bind("127.0.0.1:0")?; listener.set_nonblocking(true)?; let addr = listener.local_addr()?; let stop = Arc::new(Notify::new()); let stopped = Arc::clone(&stop); let creds = Credentials { user: user.to_string(), password: password.map(str::to_string), key, }; let root = root.to_path_buf(); let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(2) .enable_all() .build()?; std::thread::Builder::new() .name("lutra-test-sftp".into()) .spawn(move || { runtime.block_on(async move { let Ok(listener) = tokio::net::TcpListener::from_std(listener) else { return; }; loop { let accepted = tokio::select! { a = listener.accept() => a, () = stopped.notified() => return, }; let Ok((socket, _)) = accepted else { continue; }; let handler = SshHandler { creds: creds.clone(), root: root.clone(), channels: HashMap::new(), }; let config = Arc::clone(&config); tokio::spawn(async move { if let Ok(session) = russh::server::run_stream(config, socket, handler).await { let _ = session.await; } }); } }); })?; Ok(Self { addr, fingerprint, algorithm, stop, }) } } /// One SSH connection. struct SshHandler { /// Who may sign in. creds: Credentials, /// The served folder. root: PathBuf, /// Open session channels, until they ask for SFTP. channels: HashMap>, } impl russh::server::Handler for SshHandler { type Error = russh::Error; async fn auth_password(&mut self, user: &str, password: &str) -> Result { Ok( if user == self.creds.user && self.creds.password.as_deref() == Some(password) { Auth::Accept } else { Auth::reject() }, ) } async fn auth_publickey_offered( &mut self, user: &str, key: &PublicKey, ) -> Result { self.auth_publickey(user, key).await } async fn auth_publickey(&mut self, user: &str, key: &PublicKey) -> Result { Ok( if user == self.creds.user && self .creds .key .as_ref() .is_some_and(|k| k.key_data() == key.key_data()) { Auth::Accept } else { Auth::reject() }, ) } async fn channel_open_session( &mut self, channel: Channel, reply: russh::server::ChannelOpenHandle, _session: &mut Session, ) -> Result<(), Self::Error> { self.channels.insert(channel.id(), channel); reply.accept().await; Ok(()) } async fn subsystem_request( &mut self, id: ChannelId, name: &str, session: &mut Session, ) -> Result<(), Self::Error> { match (name, self.channels.remove(&id)) { ("sftp", Some(channel)) => { session.channel_success(id)?; let handler = SftpHandler { root: self.root.clone(), handles: HashMap::new(), next: 0, }; tokio::spawn(russh_sftp::server::run(channel.into_stream(), handler)); } _ => session.channel_failure(id)?, } Ok(()) } } /// What an SFTP handle refers to. enum Open { /// An open file. File(std::fs::File), /// A folder listing; `None` once sent. Dir(Option>), } /// The SFTP subsystem over a local folder. struct SftpHandler { /// The served folder. root: PathBuf, /// Open handles. handles: HashMap, /// The next handle number. next: u64, } impl SftpHandler { /// The local path of `path`. fn path(&self, path: &str) -> Result { resolve(&self.root, "/", path).ok_or(StatusCode::PermissionDenied) } /// Stores `open` under a fresh handle. fn add(&mut self, id: u32, open: Open) -> Handle { self.next += 1; let handle = format!("h{}", self.next); self.handles.insert(handle.clone(), open); Handle { id, handle } } } /// An `Ok` status. fn ok(id: u32) -> Status { Status { id, status_code: StatusCode::Ok, error_message: "Ok".into(), language_tag: "en-US".into(), } } /// A local I/O error as an SFTP status. fn io_status(e: &std::io::Error) -> StatusCode { match e.kind() { std::io::ErrorKind::NotFound => StatusCode::NoSuchFile, std::io::ErrorKind::PermissionDenied => StatusCode::PermissionDenied, _ => StatusCode::Failure, } } impl russh_sftp::server::Handler for SftpHandler { type Error = StatusCode; fn unimplemented(&self) -> Self::Error { StatusCode::OpUnsupported } async fn init( &mut self, _version: u32, _extensions: HashMap, ) -> Result { Ok(Version::new()) } async fn open( &mut self, id: u32, filename: String, flags: OpenFlags, _attrs: FileAttributes, ) -> Result { let path = self.path(&filename)?; let file = std::fs::OpenOptions::new() .read(flags.contains(OpenFlags::READ)) .write(flags.contains(OpenFlags::WRITE)) .create(flags.contains(OpenFlags::CREATE)) .truncate(flags.contains(OpenFlags::TRUNCATE)) .open(path) .map_err(|e| io_status(&e))?; Ok(self.add(id, Open::File(file))) } async fn close(&mut self, id: u32, handle: String) -> Result { self.handles.remove(&handle); Ok(ok(id)) } async fn read( &mut self, id: u32, handle: String, offset: u64, len: u32, ) -> Result { let Some(Open::File(file)) = self.handles.get_mut(&handle) else { return Err(StatusCode::Failure); }; file.seek(SeekFrom::Start(offset)) .map_err(|e| io_status(&e))?; let mut data = vec![0u8; len.min(256 * 1024) as usize]; let n = file.read(&mut data).map_err(|e| io_status(&e))?; if n == 0 { return Err(StatusCode::Eof); } data.truncate(n); Ok(Data { id, data }) } async fn write( &mut self, id: u32, handle: String, offset: u64, data: Vec, ) -> Result { let Some(Open::File(file)) = self.handles.get_mut(&handle) else { return Err(StatusCode::Failure); }; file.seek(SeekFrom::Start(offset)) .map_err(|e| io_status(&e))?; file.write_all(&data).map_err(|e| io_status(&e))?; Ok(ok(id)) } async fn stat(&mut self, id: u32, path: String) -> Result { let meta = std::fs::metadata(self.path(&path)?).map_err(|e| io_status(&e))?; Ok(Attrs { id, attrs: attrs(&meta), }) } async fn lstat(&mut self, id: u32, path: String) -> Result { self.stat(id, path).await } async fn fstat(&mut self, id: u32, handle: String) -> Result { let Some(Open::File(file)) = self.handles.get(&handle) else { return Err(StatusCode::Failure); }; let meta = file.metadata().map_err(|e| io_status(&e))?; Ok(Attrs { id, attrs: attrs(&meta), }) } async fn setstat( &mut self, id: u32, _path: String, _attrs: FileAttributes, ) -> Result { Ok(ok(id)) } async fn fsetstat( &mut self, id: u32, _handle: String, _attrs: FileAttributes, ) -> Result { Ok(ok(id)) } async fn mkdir( &mut self, id: u32, path: String, _attrs: FileAttributes, ) -> Result { std::fs::create_dir(self.path(&path)?).map_err(|e| io_status(&e))?; Ok(ok(id)) } async fn opendir(&mut self, id: u32, path: String) -> Result { let dir = self.path(&path)?; let mut files = Vec::new(); for entry in std::fs::read_dir(dir).map_err(|e| io_status(&e))?.flatten() { if let Ok(meta) = entry.metadata() { files.push(SftpFile::new( entry.file_name().to_string_lossy(), attrs(&meta), )); } } Ok(self.add(id, Open::Dir(Some(files)))) } async fn readdir(&mut self, id: u32, handle: String) -> Result { let Some(Open::Dir(files)) = self.handles.get_mut(&handle) else { return Err(StatusCode::Failure); }; match files.take() { Some(files) if !files.is_empty() => Ok(Name { id, files }), _ => Err(StatusCode::Eof), } } async fn realpath(&mut self, id: u32, path: String) -> Result { let rel = normalize_relative(&path).ok_or(StatusCode::NoSuchFile)?; Ok(Name { id, files: vec![SftpFile::dummy(format!("/{rel}"))], }) } async fn remove(&mut self, id: u32, filename: String) -> Result { std::fs::remove_file(self.path(&filename)?).map_err(|e| io_status(&e))?; Ok(ok(id)) } async fn rmdir(&mut self, id: u32, path: String) -> Result { std::fs::remove_dir(self.path(&path)?).map_err(|e| io_status(&e))?; Ok(ok(id)) } } /// A running FTP server. pub struct FtpServer { /// Its address. pub addr: SocketAddr, /// Stops it. stop: Arc, } impl Drop for FtpServer { fn drop(&mut self) { self.stop.store(true, Ordering::SeqCst); } } impl FtpServer { /// Starts a server for `root` that accepts `user` with `password`. /// /// # Errors /// An I/O error when no port can be bound. pub fn start(root: &Path, user: &str, password: &str) -> std::io::Result { let listener = TcpListener::bind("127.0.0.1:0")?; listener.set_nonblocking(true)?; let addr = listener.local_addr()?; let stop = Arc::new(AtomicBool::new(false)); let stopped = Arc::clone(&stop); let root = root.to_path_buf(); let (user, password) = (user.to_string(), password.to_string()); std::thread::Builder::new() .name("lutra-test-ftp".into()) .spawn(move || { while !stopped.load(Ordering::SeqCst) { match listener.accept() { Ok((stream, _)) => { let (root, user, password) = (root.clone(), user.clone(), password.clone()); let _ = std::thread::Builder::new() .name("lutra-test-ftp-conn".into()) .spawn(move || { let _ = ftp_session(stream, &root, &user, &password); }); } Err(_) => std::thread::sleep(Duration::from_millis(20)), } } })?; Ok(Self { addr, stop }) } } /// Serves one FTP control connection until QUIT or a closed socket. fn ftp_session(stream: TcpStream, root: &Path, user: &str, password: &str) -> std::io::Result<()> { stream.set_nonblocking(false)?; stream.set_read_timeout(Some(Duration::from_secs(60)))?; let mut out = stream.try_clone()?; let mut reader = BufReader::new(stream); let mut send = |line: &str| out.write_all(format!("{line}\r\n").as_bytes()); send("220 Lutra test FTP")?; let mut given_user = String::new(); let mut signed_in = false; let mut cwd = String::from("/"); let mut passive: Option = None; loop { let mut line = String::new(); if reader.read_line(&mut line)? == 0 { return Ok(()); } let line = line.trim_end_matches(['\r', '\n']); let (cmd, arg) = line.split_once(' ').unwrap_or((line, "")); let cmd = cmd.to_ascii_uppercase(); if !signed_in && !matches!( cmd.as_str(), "USER" | "PASS" | "QUIT" | "FEAT" | "SYST" | "AUTH" ) { send("530 Not logged in")?; continue; } let here = cwd.clone(); let path = |arg: &str| resolve(root, &here, arg); match cmd.as_str() { "USER" => { given_user = arg.to_string(); send("331 Password required")?; } "PASS" => { signed_in = given_user == user && arg == password; send(if signed_in { "230 Logged in" } else { "530 Login incorrect" })?; } "SYST" => send("215 UNIX Type: L8")?, "FEAT" | "AUTH" => send("502 Not implemented")?, "TYPE" | "NOOP" | "OPTS" => send("200 OK")?, "PWD" => send(&format!("257 \"{cwd}\" is the current directory"))?, "CWD" => match (path(arg), virtual_path(&here, arg)) { (Some(p), Some(v)) if p.is_dir() => { cwd = v; send("250 OK")?; } _ => send("550 No such directory")?, }, "MKD" => match path(arg).map(std::fs::create_dir) { Some(Ok(())) => send(&format!("257 \"{arg}\" created"))?, _ => send("550 Cannot create")?, }, "SIZE" => match path(arg).and_then(|p| std::fs::metadata(p).ok()) { Some(m) if m.is_file() => send(&format!("213 {}", m.len()))?, _ => send("550 No such file")?, }, "PASV" => { let l = TcpListener::bind("127.0.0.1:0")?; let port = l.local_addr()?.port(); passive = Some(l); send(&format!( "227 Entering Passive Mode (127,0,0,1,{},{})", port >> 8, port & 0xff ))?; } "STOR" | "RETR" | "LIST" => { let Some(listener) = passive.take() else { send("425 Use PASV first")?; continue; }; let target = if cmd == "LIST" && arg.is_empty() { path(".") } else { path(arg) }; let Some(target) = target else { send("550 Bad path")?; continue; }; if cmd == "RETR" && !target.is_file() { send("550 No such file")?; continue; } send("150 Opening data connection")?; let (mut data, _) = listener.accept()?; let result = match cmd.as_str() { "STOR" => std::fs::File::create(&target) .and_then(|mut f| std::io::copy(&mut data, &mut f).map(|_| ())), "RETR" => std::fs::File::open(&target) .and_then(|mut f| std::io::copy(&mut f, &mut data).map(|_| ())), _ => list_dir(&target).and_then(|text| data.write_all(text.as_bytes())), }; drop(data); send(if result.is_ok() { "226 Transfer complete" } else { "451 Transfer failed" })?; } "QUIT" => { send("221 Bye")?; return Ok(()); } _ => send("502 Not implemented")?, } } } /// The FTP path of `arg`, relative to `cwd`, as an absolute path. fn virtual_path(cwd: &str, arg: &str) -> Option { let joined = if arg.starts_with('/') { arg.to_string() } else { format!("{cwd}/{arg}") }; normalize_relative(&joined).map(|r| format!("/{r}")) } /// A POSIX `ls -l` style listing of `dir`. fn list_dir(dir: &Path) -> std::io::Result { let mut text = String::new(); for entry in std::fs::read_dir(dir)?.flatten() { let Ok(meta) = entry.metadata() else { continue; }; let kind = if meta.is_dir() { 'd' } else { '-' }; text.push_str(&format!( "{kind}rw-r--r-- 1 lutra lutra {} Jan 01 00:00 {}\r\n", if meta.is_dir() { 0 } else { meta.len() }, entry.file_name().to_string_lossy() )); } Ok(text) }