Helper mode tunal --elevated-helper over a framed stdin/stdout protocol, pkexec launcher, ElevatedLocal backend, per-job-thread Elevation for the job engine, Retry as administrator in the error dialog, polkit policy and a helper smoke example. Co-Authored-By: Claude Opus 5.5 <[email protected]>
196 lines
6.5 KiB
Rust
196 lines
6.5 KiB
Rust
//! The root side: `tunal --elevated-helper` serves requests on stdin/stdout until EOF.
|
|
//! It needs no environment or display (pkexec clears both) and runs no shell.
|
|
|
|
use std::collections::HashMap;
|
|
use std::ffi::OsStr;
|
|
use std::fs::{File, OpenOptions};
|
|
use std::io::{self, Read, Write};
|
|
use std::ops::ControlFlow;
|
|
use std::os::unix::ffi::OsStrExt;
|
|
use std::path::Path;
|
|
|
|
use tunal_core::{Error, LocalLocation, Location, Result, Uri};
|
|
|
|
use crate::protocol::{self, CHUNK, Reply, Request, VERSION, WireEntry};
|
|
|
|
/// Copy progress is reported every this many bytes.
|
|
const PROGRESS_STEP: u64 = 4 << 20;
|
|
|
|
/// Serves one client: handshake, then requests until stdin closes.
|
|
pub fn serve() -> io::Result<()> {
|
|
let mut input = io::stdin().lock();
|
|
let mut out = io::stdout().lock();
|
|
match protocol::recv::<Request>(&mut input)? {
|
|
Some(Request::Hello { version }) if version == VERSION => {
|
|
protocol::send(&mut out, &Reply::Hello { version: VERSION })?
|
|
}
|
|
Some(_) => {
|
|
let reply =
|
|
Reply::Error(Error::Remote(format!("protocol version {VERSION} needed")).into());
|
|
return protocol::send(&mut out, &reply);
|
|
}
|
|
None => return Ok(()),
|
|
}
|
|
let mut server = Server {
|
|
files: HashMap::new(),
|
|
next: 1,
|
|
};
|
|
while let Some(request) = protocol::recv::<Request>(&mut input)? {
|
|
server.handle(request, &mut input, &mut out)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
struct Server {
|
|
files: HashMap<u64, File>,
|
|
next: u64,
|
|
}
|
|
|
|
impl Server {
|
|
fn handle(
|
|
&mut self,
|
|
request: Request,
|
|
input: &mut impl Read,
|
|
out: &mut impl Write,
|
|
) -> io::Result<()> {
|
|
let reply = match request {
|
|
Request::List { path } => return list(&path, out),
|
|
Request::Read { handle } => return self.read(handle, out),
|
|
Request::Write { handle } => {
|
|
let data = protocol::recv_data(input)?;
|
|
self.write(handle, &data).map(|()| Reply::Done)
|
|
}
|
|
Request::Copy {
|
|
from,
|
|
to,
|
|
create_new,
|
|
} => copy(&from, &to, create_new, out).map(|()| Reply::Done),
|
|
other => self.simple(other),
|
|
};
|
|
protocol::send(out, &reply.unwrap_or_else(|e| Reply::Error(e.into())))
|
|
}
|
|
|
|
fn simple(&mut self, request: Request) -> Result<Reply> {
|
|
let local = LocalLocation;
|
|
match request {
|
|
Request::Hello { .. } => Ok(Reply::Hello { version: VERSION }),
|
|
Request::Stat { path } => Ok(Reply::Entry(local.stat(&uri(&path)?)?.into())),
|
|
Request::OpenRead { path } => {
|
|
let file = File::open(self::path(&path)?)?;
|
|
Ok(self.add(file))
|
|
}
|
|
Request::OpenWrite { path, create_new } => {
|
|
let file = open_write(&path, create_new)?;
|
|
Ok(self.add(file))
|
|
}
|
|
Request::Close { handle } => {
|
|
let file = self.files.remove(&handle).ok_or(Error::NotFound)?;
|
|
file.sync_all()?;
|
|
Ok(Reply::Done)
|
|
}
|
|
Request::Mkdir { path } => local.mkdir(&uri(&path)?).map(|()| Reply::Done),
|
|
Request::Rename { from, to } => {
|
|
local.rename(&uri(&from)?, &uri(&to)?).map(|()| Reply::Done)
|
|
}
|
|
Request::Delete { path } => local.delete(&uri(&path)?).map(|()| Reply::Done),
|
|
Request::Symlink { link, target } => {
|
|
local.symlink(&uri(&link)?, &target).map(|()| Reply::Done)
|
|
}
|
|
Request::List { .. }
|
|
| Request::Read { .. }
|
|
| Request::Write { .. }
|
|
| Request::Copy { .. } => Err(Error::Unsupported),
|
|
}
|
|
}
|
|
|
|
fn add(&mut self, file: File) -> Reply {
|
|
let handle = self.next;
|
|
self.next += 1;
|
|
self.files.insert(handle, file);
|
|
Reply::Handle(handle)
|
|
}
|
|
|
|
fn read(&mut self, handle: u64, out: &mut impl Write) -> io::Result<()> {
|
|
let Some(file) = self.files.get_mut(&handle) else {
|
|
return protocol::send(out, &Reply::Error(Error::NotFound.into()));
|
|
};
|
|
let mut buf = vec![0; CHUNK];
|
|
match file.read(&mut buf) {
|
|
Ok(n) => protocol::send_data(out, &buf[..n]),
|
|
Err(e) => protocol::send(out, &Reply::Error(Error::from(e).into())),
|
|
}
|
|
}
|
|
|
|
fn write(&mut self, handle: u64, data: &[u8]) -> Result<()> {
|
|
let file = self.files.get_mut(&handle).ok_or(Error::NotFound)?;
|
|
Ok(file.write_all(data)?)
|
|
}
|
|
}
|
|
|
|
fn list(path: &[u8], out: &mut impl Write) -> io::Result<()> {
|
|
let mut failed = None;
|
|
let result = uri(path).and_then(|dir| {
|
|
LocalLocation.list(&dir, &mut |chunk| {
|
|
let chunk: Vec<WireEntry> = chunk.into_iter().map(Into::into).collect();
|
|
match protocol::send(out, &Reply::Entries(chunk)) {
|
|
Ok(()) => ControlFlow::Continue(()),
|
|
Err(e) => {
|
|
failed = Some(e);
|
|
ControlFlow::Break(())
|
|
}
|
|
}
|
|
})
|
|
});
|
|
if let Some(e) = failed {
|
|
return Err(e);
|
|
}
|
|
let reply = match result {
|
|
Ok(()) => Reply::Done,
|
|
Err(e) => Reply::Error(e.into()),
|
|
};
|
|
protocol::send(out, &reply)
|
|
}
|
|
|
|
fn copy(from: &[u8], to: &[u8], create_new: bool, out: &mut impl Write) -> Result<()> {
|
|
let mut source = File::open(path(from)?)?;
|
|
let mut target = open_write(to, create_new)?;
|
|
let mut buf = vec![0; CHUNK];
|
|
let (mut done, mut reported) = (0u64, 0u64);
|
|
loop {
|
|
let n = source.read(&mut buf)?;
|
|
if n == 0 {
|
|
break;
|
|
}
|
|
target.write_all(&buf[..n])?;
|
|
done += n as u64;
|
|
if done - reported >= PROGRESS_STEP {
|
|
reported = done;
|
|
protocol::send(out, &Reply::Progress(done)).map_err(Error::from)?;
|
|
}
|
|
}
|
|
target.sync_all()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn open_write(path: &[u8], create_new: bool) -> Result<File> {
|
|
let mut options = OpenOptions::new();
|
|
options.write(true);
|
|
if create_new {
|
|
options.create_new(true);
|
|
} else {
|
|
options.create(true).truncate(true);
|
|
}
|
|
Ok(options.open(self::path(path)?)?)
|
|
}
|
|
|
|
/// Only absolute paths are accepted.
|
|
fn path(bytes: &[u8]) -> Result<&Path> {
|
|
if bytes.first() != Some(&b'/') || bytes.contains(&0) {
|
|
return Err(Error::Remote("only absolute paths are accepted".into()));
|
|
}
|
|
Ok(Path::new(OsStr::from_bytes(bytes)))
|
|
}
|
|
|
|
fn uri(bytes: &[u8]) -> Result<Uri> {
|
|
Ok(Uri::from_path(path(bytes)?))
|
|
}
|