use std::{
fs, io,
os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt},
path::Path,
};
use tokio::net::{UnixListener, UnixStream};
use super::files::{current_uid, verify_private_dir, DaemonPathError};
use crate::daemon::paths::{DaemonPaths, SocketFound};
pub(in crate::daemon) type Stream = UnixStream;
pub(in crate::daemon) struct Listener {
pub(super) socket: UnixListener,
uid: u32,
}
impl Listener {
pub(in crate::daemon) fn bind(paths: &DaemonPaths) -> io::Result<Self> {
match fs::remove_file(&paths.socket) {
Ok(()) => {
tracing::info!(path = %paths.socket.display(), "removed stale daemon file");
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
let socket = UnixListener::bind(&paths.socket)?;
fs::set_permissions(&paths.socket, fs::Permissions::from_mode(0o600))?;
Ok(Self::serving(socket))
}
pub(super) fn serving(socket: UnixListener) -> Self {
Self {
socket,
uid: current_uid(),
}
}
pub(in crate::daemon) fn hold_for_restart(&mut self) {}
pub(in crate::daemon) async fn accept(&mut self) -> io::Result<Stream> {
loop {
let (stream, _) = self.socket.accept().await?;
if peer_is_owner(&stream, self.uid) {
return Ok(stream);
}
}
}
}
fn peer_is_owner(stream: &UnixStream, uid: u32) -> bool {
match stream.peer_cred() {
Ok(credentials) if credentials.uid() == uid => true,
Ok(credentials) => {
tracing::warn!(
peer_uid = credentials.uid(),
"rejected connection from another user"
);
false
}
Err(error) => {
tracing::warn!(%error, "rejected connection without peer credentials");
false
}
}
}
pub(in crate::daemon) fn remove_endpoint(paths: &DaemonPaths) -> io::Result<()> {
fs::remove_file(&paths.socket)
}
pub(in crate::daemon) async fn connect(paths: &DaemonPaths) -> io::Result<Option<Stream>> {
match UnixStream::connect(&paths.socket).await {
Ok(stream) => Ok(Some(stream)),
Err(error)
if matches!(
error.kind(),
io::ErrorKind::NotFound | io::ErrorKind::ConnectionRefused
) =>
{
Ok(None)
}
Err(error) => Err(error),
}
}
pub(in crate::daemon) async fn connect_restarting(
paths: &DaemonPaths,
) -> io::Result<Option<Stream>> {
connect(paths).await
}
pub(in crate::daemon) fn inspect_endpoint(
paths: &DaemonPaths,
) -> Result<SocketFound, DaemonPathError> {
let io_error = |path: &Path, error: io::Error| DaemonPathError::Unreadable {
path: path.to_path_buf(),
message: error.to_string(),
};
match fs::symlink_metadata(&paths.runtime_dir) {
Ok(metadata) => verify_private_dir(&paths.runtime_dir, &metadata)?,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(SocketFound::Missing),
Err(error) => return Err(io_error(&paths.runtime_dir, error)),
}
let path = &paths.socket;
let metadata = match fs::symlink_metadata(path) {
Ok(metadata) => metadata,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(SocketFound::Missing),
Err(error) => return Err(io_error(path, error)),
};
if !metadata.file_type().is_socket() {
return Err(DaemonPathError::NotASocket { path: path.clone() });
}
let uid = current_uid();
if metadata.uid() != uid {
return Err(DaemonPathError::SocketWrongOwner {
path: path.clone(),
owner: metadata.uid(),
uid,
});
}
let mode = metadata.permissions().mode() & 0o777;
if mode & 0o077 != 0 {
return Err(DaemonPathError::InsecureSocketMode {
path: path.clone(),
mode,
});
}
Ok(SocketFound::Private)
}