LibrePortal/scripts/docker/command/run_privileged.sh
librelad 82839abea6 harden(desudo): arg-safe runFileOp + convert DNS subsystem off raw sudo
Give dockerCommandRunInstallUser an --argv mode that execs arguments
verbatim (sudo -u <user> env ... "$@") instead of bash -c "$*", and
point runFileOp at it. The old $*+bash -c re-parse silently mangled
backslashes/quotes in args — e.g. sed scripts (\1, \( become 1, ( ) and
the sqlite3 .backup arg — so rootless data-plane ops with regex were
broken. Verified: the WG_DEFAULT_DNS sed now applies correctly as the
install user. All existing runFileOp callers pass plain commands, so the
switch is safe (and fixes the latent sqlite3 case).

Convert scripts/network/dns/setup_dns.sh: /etc/resolv.conf edits and
ping -> runSystem; the WG_DEFAULT_DNS compose-file sed -> runFileOp.
Byte-identical in rooted; correct in rootless.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-23 23:22:46 +01:00

46 lines
1.7 KiB
Bash

#!/bin/bash
# Mode-aware privileged operations.
#
# The privilege a file operation needs depends on the Docker mode:
# rooted — app/container files under /docker are root-owned, so ops run via
# sudo. This is byte-for-byte the historical behaviour.
# rootless — those files are owned by the unprivileged Docker install user, so
# ops run AS that user (via `sudo -u`, no root over the data plane).
# Centralising the branch here means each call site is written once and is
# correct in both modes, and rooted installs (incl. live boxes) are unchanged.
# Run a /docker data-plane command — mkdir/chown/rm/cp/mv/find/sqlite3/etc. on
# app or container files.
# rooted -> sudo <cmd>
# rootless -> run <cmd> as the Docker install user (no sudo)
# Note: for stdin-fed writes (e.g. `… | sudo tee file`) use runFileWrite below;
# this helper is for self-contained commands.
runFileOp() {
if [[ "$CFG_DOCKER_INSTALL_TYPE" == "rootless" ]]; then
dockerCommandRunInstallUser --argv "$@"
else
sudo "$@"
fi
}
# Write stdin to a path with the right privilege (replaces `… | sudo tee path`).
# rooted -> sudo tee
# rootless -> tee as the Docker install user
# Usage: some_command | runFileWrite /path/to/file
runFileWrite() {
local dest="$1"
if [[ "$CFG_DOCKER_INSTALL_TYPE" == "rootless" ]]; then
dockerCommandRunInstallUser "tee '$dest' >/dev/null"
else
sudo tee "$dest" >/dev/null
fi
}
# Genuine system-administration command (ufw/systemctl/apt/sysctl/useradd, /etc
# edits). Needs real root in both modes; kept as sudo and funnelled through one
# place so it can later be confined to a scoped sudoers allowlist.
runSystem() {
sudo "$@"
}