Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion bin/omarchy-install-browser
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,59 @@

set -e

source "$OMARCHY_PATH/install/helpers/browser-policy.sh"
# Browser policy setup installs files as root, so the helper and the policy
# files it installs must come from a trusted tree. The packaged tree is always
# trusted. A linked checkout is trusted only when /etc/omarchy.conf is a
# root-owned regular file whose only line names that checkout, which is what
# omarchy-dev-link writes. omarchy-plymouth-set uses the same rule. Keep this
# block in step with omarchy-refresh-limine, omarchy-refresh-pacman,
# omarchy-toggle-hybrid-gpu, omarchy-hibernation-setup, omarchy-migrate, and
# install/helpers/browser-policy.sh.
omarchy_privileged_source_root() {
local packaged=/usr/share/omarchy
local path=${OMARCHY_PATH:-$packaged}
local conf=/etc/omarchy.conf
local uid mode
local -a lines

if [[ $path == "$packaged" ]]; then
printf '%s\n' "$packaged"
return
fi

if [[ ! -f $conf || -L $conf ]]; then
printf '%s\n' "$packaged"
return
fi

uid=$(stat -c %u -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
mode=$(stat -c %a -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
if (( uid != 0 )) || (( 8#$mode & 0022 )); then
printf '%s\n' "$packaged"
return
fi

mapfile -t lines <"$conf" || {
printf '%s\n' "$packaged"
return
}
if (( ${#lines[@]} == 1 )) && [[ ${lines[0]} == "export OMARCHY_PATH=\"$path\"" ]]; then
printf '%s\n' "$path"
return
fi

printf '%s\n' "$packaged"
}

SOURCE_ROOT=$(omarchy_privileged_source_root)

source "$SOURCE_ROOT/install/helpers/browser-policy.sh"

setup_chromium_policy_directory() {
browser_policy_setup_dir "$1"
Expand Down
56 changes: 54 additions & 2 deletions bin/omarchy-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,58 @@

set -euo pipefail

# Migrations run with the caller's sudo credentials inside the update flow, so
# the tree they are read from must be trusted. The packaged tree is always
# trusted. A linked checkout is trusted only when /etc/omarchy.conf is a
# root-owned regular file whose only line names that checkout, which is what
# omarchy-dev-link writes. omarchy-plymouth-set uses the same rule. Keep this
# block in step with omarchy-refresh-limine, omarchy-refresh-pacman,
# omarchy-toggle-hybrid-gpu, omarchy-hibernation-setup,
# omarchy-install-browser, and install/helpers/browser-policy.sh.
omarchy_privileged_source_root() {
local packaged=/usr/share/omarchy
local path=${OMARCHY_PATH:-$packaged}
local conf=/etc/omarchy.conf
local uid mode
local -a lines

if [[ $path == "$packaged" ]]; then
printf '%s\n' "$packaged"
return
fi

if [[ ! -f $conf || -L $conf ]]; then
printf '%s\n' "$packaged"
return
fi

uid=$(stat -c %u -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
mode=$(stat -c %a -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
if (( uid != 0 )) || (( 8#$mode & 0022 )); then
printf '%s\n' "$packaged"
return
fi

mapfile -t lines <"$conf" || {
printf '%s\n' "$packaged"
return
}
if (( ${#lines[@]} == 1 )) && [[ ${lines[0]} == "export OMARCHY_PATH=\"$path\"" ]]; then
printf '%s\n' "$path"
return
fi

printf '%s\n' "$packaged"
}

SOURCE_ROOT=$(omarchy_privileged_source_root)

mode="run"

usage() {
Expand All @@ -30,7 +82,7 @@ done

OMARCHY_PATH="${OMARCHY_PATH:-/usr/share/omarchy}"
STATE_DIR="${OMARCHY_MIGRATION_STATE:-$HOME/.local/state/omarchy/migrations}"
MIGRATIONS_DIR="$OMARCHY_PATH/migrations"
MIGRATIONS_DIR="$SOURCE_ROOT/migrations"

migration_entries() {
[[ -d $MIGRATIONS_DIR ]] || return 0
Expand Down Expand Up @@ -90,7 +142,7 @@ while IFS=$'\t' read -r name file marker <&3; do

if [[ ! -f $marker ]]; then
echo -e "\e[32m\nRunning migration (${name%.sh})\e[0m"
OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$file" 3<&-
OMARCHY_PATH="$SOURCE_ROOT" bash -euo pipefail "$file" 3<&-
mkdir -p "$(dirname "$marker")"
touch "$marker"
fi
Expand Down
55 changes: 53 additions & 2 deletions install/helpers/browser-policy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,57 @@

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/as-root.sh"

# Policy files are installed as root, so they must come from a trusted tree.
# The packaged tree is always trusted. A linked checkout is trusted only when
# /etc/omarchy.conf is a root-owned regular file whose only line names that
# checkout, which is what omarchy-dev-link writes. omarchy-plymouth-set uses
# the same rule. Keep this block in step with omarchy-refresh-limine,
# omarchy-refresh-pacman, omarchy-toggle-hybrid-gpu, omarchy-hibernation-setup,
# omarchy-migrate, and omarchy-install-browser.
omarchy_privileged_source_root() {
local packaged=/usr/share/omarchy
local path=${OMARCHY_PATH:-$packaged}
local conf=/etc/omarchy.conf
local uid mode
local -a lines

if [[ $path == "$packaged" ]]; then
printf '%s\n' "$packaged"
return
fi

if [[ ! -f $conf || -L $conf ]]; then
printf '%s\n' "$packaged"
return
fi

uid=$(stat -c %u -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
mode=$(stat -c %a -- "$conf" 2>/dev/null) || {
printf '%s\n' "$packaged"
return
}
if (( uid != 0 )) || (( 8#$mode & 0022 )); then
printf '%s\n' "$packaged"
return
fi

mapfile -t lines <"$conf" || {
printf '%s\n' "$packaged"
return
}
if (( ${#lines[@]} == 1 )) && [[ ${lines[0]} == "export OMARCHY_PATH=\"$path\"" ]]; then
printf '%s\n' "$path"
return
fi

printf '%s\n' "$packaged"
}

BROWSER_POLICY_SOURCE_ROOT=$(omarchy_privileged_source_root)

BROWSER_POLICY_MANAGED_DIRS=(
/etc/chromium/policies/managed
/etc/opt/chrome/policies/managed
Expand Down Expand Up @@ -153,14 +204,14 @@ browser_policy_firefox_hardened() {

browser_policy_install_firefox_policies() {
local distribution_dir=$1
local policies=${2:-$OMARCHY_PATH/default/firefox/policies.json}
local policies=${2:-$BROWSER_POLICY_SOURCE_ROOT/default/firefox/policies.json}

as_root install -m 644 -o root -g root -T "$policies" "$distribution_dir/policies.json"
}

browser_policy_setup_firefox_distribution() {
local distribution_dir=$1
local policies=${2:-$OMARCHY_PATH/default/firefox/policies.json}
local policies=${2:-$BROWSER_POLICY_SOURCE_ROOT/default/firefox/policies.json}

browser_policy_setup_parent "$distribution_dir"
browser_policy_purge_dir "$distribution_dir"
Expand Down
8 changes: 6 additions & 2 deletions migrations/1784809451.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ echo "Configure locate to skip Btrfs snapshots and index Btrfs subvolumes"

OMARCHY_PATH="${OMARCHY_PATH:-/usr/share/omarchy}"
locate_config_script="$OMARCHY_PATH/install/config/locate.sh"
UPDATEDB_CONF_PATH="${OMARCHY_UPDATEDB_CONF_PATH:-/etc/updatedb.conf}"
UPDATEDB_CONF_PATH=/etc/updatedb.conf

as_root() {
if (( EUID == 0 )); then
Expand All @@ -20,7 +20,11 @@ if grep -q '^PRUNE_BIND_MOUNTS = "no"' "$UPDATEDB_CONF_PATH" &&
exit 0
fi

as_root env OMARCHY_UPDATEDB_CONF_PATH="$UPDATEDB_CONF_PATH" bash -euo pipefail "$locate_config_script"
# The config path is pinned so a caller-chosen OMARCHY_UPDATEDB_CONF_PATH
# cannot steer the root rewrite at a file the caller named. locate.sh keeps
# its own override for tests, but sudo does not pass the caller's value
# through, so the root side always edits /etc/updatedb.conf.
as_root bash -euo pipefail "$locate_config_script"

# Rebuild the index with the new exclusions; pruning /.snapshots turns
# multi-hour runs on snapshot-heavy systems back into one-minute runs. Restart
Expand Down
14 changes: 11 additions & 3 deletions test/shell.d/locate-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ chmod +x "$fake_bin/systemctl"
conf="$test_tmp/migration-updatedb.conf"
stock_conf "$conf"

# The migration pins /etc/updatedb.conf so a caller cannot name the file the
# root step rewrites. Test against a copy whose pinned path points at the
# stand-in conf. The sudo stub execs locate.sh with the caller's environment,
# where real sudo would strip OMARCHY_UPDATEDB_CONF_PATH and leave locate.sh
# on its own default.
migration_copy="$test_tmp/locate-migration"
sed "s#/etc/updatedb.conf#$conf#g" "$locate_migration" >"$migration_copy"

TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$ROOT" \
OMARCHY_UPDATEDB_CONF_PATH="$conf" \
bash -euo pipefail "$locate_migration" >/dev/null
bash -euo pipefail "$migration_copy" >/dev/null

grep -qFx 'PRUNE_BIND_MOUNTS = "no"' "$conf" || fail "locate migration rewrites updatedb.conf"
grep -qF 'PRUNEPATHS = "/.snapshots /afs' "$conf" || fail "locate migration prunes /.snapshots"
Expand All @@ -147,7 +155,7 @@ TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$ROOT" \
OMARCHY_UPDATEDB_CONF_PATH="$conf" \
bash -euo pipefail "$locate_migration" >/dev/null
bash -euo pipefail "$migration_copy" >/dev/null

[[ ! -s $test_tmp/calls.log ]] || fail "locate migration skips already-configured installs"
pass "locate migration is a no-op once updatedb.conf is configured"
Expand All @@ -162,7 +170,7 @@ TEST_LOG="$test_tmp/calls.log" \
PATH="$fake_bin:$PATH" \
OMARCHY_PATH="$test_tmp/empty" \
OMARCHY_UPDATEDB_CONF_PATH="$conf" \
bash -euo pipefail "$locate_migration" >/dev/null ||
bash -euo pipefail "$migration_copy" >/dev/null ||
fail "locate migration survives a tree without the locate config script"

[[ ! -s $test_tmp/calls.log ]] || fail "locate migration touches nothing without the locate config script"
Expand Down
51 changes: 44 additions & 7 deletions test/shell.d/migrate-scope-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,41 @@ trap 'rm -rf "$test_tmp"' EXIT

test_root="$test_tmp/omarchy"
test_home="$test_tmp/home"
mkdir -p "$test_root/migrations" "$test_home"
stub_bin="$test_tmp/bin"
conf="$test_tmp/omarchy.conf"
mkdir -p "$test_root/migrations" "$test_home" "$stub_bin"

# omarchy-migrate reads migrations from the packaged tree, or from a checkout
# that a root-owned /etc/omarchy.conf names. Run a copy whose conf path points
# at a stand-in, and authorize each scratch tree the way omarchy-dev-link
# would. stat is stubbed to report the stand-in as root-owned, which is the
# part of dev-link only root can do for real.
migrate_copy="$test_tmp/omarchy-migrate"
sed "s#/etc/omarchy.conf#$conf#g" "$ROOT/bin/omarchy-migrate" >"$migrate_copy"
chmod +x "$migrate_copy"

cat >"$stub_bin/stat" <<'SH'
#!/bin/bash
for last; do :; done
if [[ $last == "$TEST_CONF" ]]; then
case " $* " in
*" %u "*) echo 0 ;;
*" %a "*) echo 644 ;;
esac
exit 0
fi
exec /usr/bin/stat "$@"
SH
chmod +x "$stub_bin/stat"

authorize() {
printf 'export OMARCHY_PATH="%s"\n' "$1" >"$conf"
chmod 0644 "$conf"
}

run_migrate() {
TEST_CONF="$conf" PATH="$stub_bin:$PATH" "$migrate_copy" "$@"
}

cat >"$test_root/migrations/100-first.sh" <<'SH'
[[ $OMARCHY_PATH == "$TEST_EXPECTED_OMARCHY_PATH" ]]
Expand All @@ -22,7 +56,8 @@ SH

calls="$test_tmp/calls"

if ! HOME="$test_home" OMARCHY_PATH="$test_root" "$ROOT/bin/omarchy-migrate" --pending >"$test_tmp/pending.out"; then
authorize "$test_root"
if ! HOME="$test_home" OMARCHY_PATH="$test_root" run_migrate --pending >"$test_tmp/pending.out"; then
fail "migration runner reports pending migrations before state exists"
fi
grep -q '^100-first\.sh$' "$test_tmp/pending.out" || fail "migration runner lists first pending migration filename"
Expand All @@ -33,7 +68,7 @@ HOME="$test_home" \
OMARCHY_PATH="$test_root" \
TEST_EXPECTED_OMARCHY_PATH="$test_root" \
TEST_CALLS="$calls" \
"$ROOT/bin/omarchy-migrate" >"$test_tmp/first-run.out"
run_migrate >"$test_tmp/first-run.out"
[[ $(sed -n '1p' "$calls") == "first" ]] || fail "migration runner runs first migration"
[[ $(sed -n '2p' "$calls") == "second" ]] || fail "migration runner runs second migration"
[[ -f $test_home/.local/state/omarchy/migrations/100-first.sh ]] || fail "migration runner records first migration marker"
Expand All @@ -44,11 +79,11 @@ HOME="$test_home" \
OMARCHY_PATH="$test_root" \
TEST_EXPECTED_OMARCHY_PATH="$test_root" \
TEST_CALLS="$calls" \
"$ROOT/bin/omarchy-migrate" >"$test_tmp/second-run.out"
run_migrate >"$test_tmp/second-run.out"
[[ $(wc -l <"$calls") -eq 2 ]] || fail "migration runner skips completed migrations"
pass "migration runner skips completed migrations"

if HOME="$test_home" OMARCHY_PATH="$test_root" "$ROOT/bin/omarchy-migrate" --pending >"$test_tmp/not-pending.out"; then
if HOME="$test_home" OMARCHY_PATH="$test_root" run_migrate --pending >"$test_tmp/not-pending.out"; then
fail "migration runner reports no pending migrations after state exists"
fi
pass "migration runner detects no pending migrations"
Expand All @@ -63,11 +98,12 @@ false
echo after-fail >>"$TEST_CALLS"
SH

authorize "$failure_root"
set +e
HOME="$failure_home" \
OMARCHY_PATH="$failure_root" \
TEST_CALLS="$calls" \
"$ROOT/bin/omarchy-migrate" >"$test_tmp/failure.out" 2>"$test_tmp/failure.err"
run_migrate >"$test_tmp/failure.out" 2>"$test_tmp/failure.err"
failure_status=$?
set -e
[[ $failure_status -ne 0 ]] || fail "migration runner exits non-zero when a migration fails"
Expand All @@ -89,11 +125,12 @@ cat >"$stdin_root/migrations/200-after.sh" <<'SH'
echo after-reader >>"$TEST_CALLS"
SH

authorize "$stdin_root"
printf 'migration input\n' | \
HOME="$stdin_home" \
OMARCHY_PATH="$stdin_root" \
TEST_CALLS="$stdin_calls" \
"$ROOT/bin/omarchy-migrate" >"$test_tmp/stdin.out"
run_migrate >"$test_tmp/stdin.out"

grep -q '^reader:migration input$' "$stdin_calls" ||
fail "migration runner preserves the caller's stdin for a migration" "$(cat "$stdin_calls")"
Expand Down
Loading