Skip to content
Open
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
25 changes: 17 additions & 8 deletions module/scripts/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

d="${0%/*}"; [ "$d" = "$0" ] && d="."; MODDIR="$(cd "$d/.." 2>/dev/null && pwd || echo "${d%/*}")"
STATE=/data/adb/vdbackup
valid_pkg() { case "$1" in ''|null|.*|*[!A-Za-z0-9._]*) return 1 ;; esac; } # same rule as worker.sh
bdir() { case "$1" in ''|*[!0-9]*) set -- 0 ;; esac; printf '/data/media/%s/VDBackup' "$1"; } # /sdcard/VDBackup per Android user (credential-encrypted)
# A backup sent to a local destination lives at that destination, not duplicated in the internal
# store (destination-authoritative, issue #15): the user picks a local dest to keep the backup OFF
Expand Down Expand Up @@ -132,7 +133,15 @@ detach() { # detach <opid> <worker-verb> <b64args>
# seed a concrete progress state so the UI shows "starting" instantly instead of
# an empty file (the worker overwrites it with the first real phase right away).
printf '{"opid":"%s","percent":0,"phase":"starting","item":"","done":false}\n' "$opid" > "$LOGS/progress-$opid.json"
setsid nohup "$MODDIR/scripts/worker.sh" "$wverb" "$opid" "$wargs" \
# the args (passphrases included) reach the worker in a root-only 0600 file, never on its
# argv, which other uids can read through /proc for the whole operation. A here-doc feeds
# cat on stdin (mksh has no printf builtin, so printf would put them on argv as well).
mkdir -p "$STATE/staging" 2>/dev/null
( umask 077; cat > "$STATE/staging/args.$opid" <<EOF
$wargs
EOF
) || return 1
setsid nohup "$MODDIR/scripts/worker.sh" "$wverb" "$opid" \
>"$LOGS/$opid.out" 2>&1 </dev/null &
echo "$!" > "$LOGS/$opid.pid"
}
Expand Down Expand Up @@ -255,7 +264,7 @@ auto_backup_scan() { # <jq> <now>; echoes detached
| map(select(($s[.key] // null) != .value)) | .[].key' "$_cur" 2>/dev/null)"
_n=0
for _pkg in $_changed; do
case "$_pkg" in ''|*..*|*/*|*' '*) continue ;; esac
valid_pkg "$_pkg" || continue
"$_jq" -e --arg p "$_pkg" '((.blocklist // []) | index($p)) != null' "$STATE/config.json" >/dev/null 2>&1 && continue
_j="$("$_jq" -nc --arg p "$_pkg" --argjson u "$_u" --arg m "$_mode" --arg d "$_dest" \
'{package:$p,user:$u,mode:$m,dest:$d,items:["apk","data","data_de","obb","external","media"],auto:true}')"
Expand Down Expand Up @@ -505,7 +514,7 @@ case "$verb" in
pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)"
u="$(printf '%s' "$j" | "$JQ" -r '.user // 0' 2>/dev/null)"; case "$u" in ''|*[!0-9]*) u=0 ;; esac
vc="$(printf '%s' "$j" | "$JQ" -r '.vc // 0' 2>/dev/null)"; case "$vc" in ''|*[!0-9]*) vc=0 ;; esac
case "$pkg" in ''|*/*|*..*|*' '*) err ERR_ICON "invalid package"; exit 0 ;; esac
valid_pkg "$pkg" || { err ERR_ICON "invalid package"; exit 0; }
mkdir -p "$STATE/iconcache" 2>/dev/null
# cache key includes versionCode so the icon refreshes when the app updates;
# drop any stale entry cached under an older versionCode for this package.
Expand All @@ -532,7 +541,7 @@ case "$verb" in
TAB="$(printf '\t')"
lst="$STATE/staging/icons.$$.lst"; : > "$lst"
printf '%s' "$j" | "$JQ" -r '.items[]? | "\(.package // "")\t\(.vc // 0)"' 2>/dev/null | while IFS="$TAB" read -r pkg vc; do
case "$pkg" in ''|*/*|*..*|*' '*) continue ;; esac
valid_pkg "$pkg" || continue
case "$vc" in ''|*[!0-9]*) vc=0 ;; esac
png="$STATE/iconcache/$pkg-$vc.png"
if [ ! -s "$png" ]; then
Expand All @@ -548,7 +557,7 @@ case "$verb" in
printf '{"ok":true,"icons":{'
first=1
printf '%s' "$j" | "$JQ" -r '.items[]? | "\(.package // "")\t\(.vc // 0)"' 2>/dev/null | while IFS="$TAB" read -r pkg vc; do
case "$pkg" in ''|*/*|*..*|*' '*) continue ;; esac
valid_pkg "$pkg" || continue
case "$vc" in ''|*[!0-9]*) vc=0 ;; esac
png="$STATE/iconcache/$pkg-$vc.png"; [ -s "$png" ] || continue
[ "$first" = 1 ] || printf ','; first=0
Expand Down Expand Up @@ -1424,7 +1433,7 @@ EOF
j="$(printf '%s' "$ARGS_B64" | base64 -d 2>/dev/null)"
pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)"
priv="$(printf '%s' "$j" | "$JQ" -r '.priv // false' 2>/dev/null)"
case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_PKG "invalid package"; exit 0 ;; esac
valid_pkg "$pkg" || { err ERR_BAD_PKG "invalid package"; exit 0; }
sub=app; [ "$priv" = true ] && sub=priv-app
base="$(pm path "$pkg" 2>/dev/null | sed -n 's/^package://p' | grep '/base\.apk$' | head -1)"
[ -n "$base" ] || base="$(pm path "$pkg" 2>/dev/null | sed -n 's/^package://p' | head -1)"
Expand Down Expand Up @@ -1481,7 +1490,7 @@ EOF
sysmount-del)
JQ="$(command -v "$BIN/jq" 2>/dev/null || echo jq)"
pkg="$(printf '%s' "$ARGS_B64" | base64 -d 2>/dev/null | "$JQ" -r '.package // empty' 2>/dev/null)"
case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_PKG "invalid package"; exit 0 ;; esac
valid_pkg "$pkg" || { err ERR_BAD_PKG "invalid package"; exit 0; }
MM=/data/adb/modules/vdbackup_sysmount
for sub in app priv-app; do
d="$MM/system/$sub/$pkg"; [ -d "$d" ] || continue
Expand Down Expand Up @@ -1550,7 +1559,7 @@ EOF
pkg="$(printf '%s' "$j" | "$JQ" -r '.package // empty' 2>/dev/null)"
u="$(printf '%s' "$j" | "$JQ" -r '.user // 0' 2>/dev/null)"; case "$u" in ''|*[!0-9]*) u=0 ;; esac
keep="$(printf '%s' "$j" | "$JQ" -r '.keep // 5' 2>/dev/null)"; case "$keep" in ''|*[!0-9]*) keep=5 ;; esac
case "$pkg" in ''|*..*|*/*|*' '*) err ERR_BAD_REF "invalid package"; exit 0 ;; esac
valid_pkg "$pkg" || { err ERR_BAD_REF "invalid package"; exit 0; }
printf '{"ok":true,"pruned":['
first=1
for r in $(prune_pkg "$u" "$pkg" "$keep"); do
Expand Down
Loading