doasedit: fix to not copy files with user perms...

This commit is contained in:
yosh 2023-09-21 10:09:55 -04:00
parent 1648d3d20b
commit c9e9c6a722
1 changed files with 11 additions and 16 deletions

View File

@ -1,37 +1,32 @@
#!/bin/sh
set -f
ercho() { echo "$*" >&2 ; }
error() { ercho "$*" && exit 1 ; }
errecho() { echo "$*" >&2 ; }
error() { errecho "$*" && exit 1 ; }
TMPDIR="${TMPDIR:-/tmp}"
ed="${VISUAL:-vi}"
ed="${VISUAL:-${EDITOR:-vi}}"
clean() {
[ -f "$tmpfile" ] && rm -f "$tmpfile"
[ -f "${tmpfile}" ] && rm -f "$tmpfile"
}
[ "$(whoami)" = "root" ] && error "Cannot be run as root!"
[ -z "$1" ] && error "No files provided!"
trap 'clean' 1 INT HUP QUIT EXIT
trap 'clean' 1 INT HUP KILL EXIT
for f; do
[ ! -f "$f" ] && ercho "File $f is not a regular file, is not accessible by the user, or does not exist. Skipping..." && continue
[ -f "$f" ] || { errecho "File $f is not a regular file, is not accessible by the user, or does not exist. Skipping..." && continue; }
tmpfile="$(mktemp -t doasedit_XXXXXXXX)" || error "Cannot make temp file for $f! Exiting..."
cp -fp "$f" "$tmpfile" || error "Cannot copy file $f! Exiting..."
ercho "$ed '$f'"
cp -f "$f" "$tmpfile" || error "Cannot copy file $f! Exiting..."
errecho "$ed '$f'"
$ed "$tmpfile" || error "Exit code != 0 by editor. Exiting..."
cmp "$f" "$tmpfile" >/dev/null 2>&1
ec=$?
if [ $ec -eq 1 ]; then
if [ ! -w "$f" ]; then doas mv -f "$tmpfile" "$f"; else mv -f "$tmpfile" "$f" && echo "$f didn't need root perms!"; fi
doas cp "$tmpfile" "$f"
elif [ $ec -eq 0 ]; then
ercho "File not changed, skipping..."
errecho "File not changed, skipping..."
else
ercho "Problem running diff on $f! Skipping..."
errecho "Problem running diff on $f! Skipping..."
fi
rm -f "$tmpfile"
done