diff options
author | Adam <56338480+adastx@users.noreply.github.com> | 2022-06-09 00:02:27 +0200 |
---|---|---|
committer | Adam <56338480+adastx@users.noreply.github.com> | 2022-06-09 00:02:27 +0200 |
commit | e351b2bc9875f7943eb1bd4b4d75ef83ae9a10a1 (patch) | |
tree | bcb90ddf7eb2fa0b9b24b22292c892dc4ae7b3ef | |
parent | d2211f9babc5f5c955a81ccdf9583ef437cd7c4f (diff) |
Added doasedit script to repo
-rwxr-xr-x | .local/bin/doasedit | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/.local/bin/doasedit b/.local/bin/doasedit new file mode 100755 index 0000000..0d2c9c9 --- /dev/null +++ b/.local/bin/doasedit @@ -0,0 +1,47 @@ +#!/bin/sh -e + +help() { + cat - >&2 <<EOF +doasedit - like sudoedit, but for doas + +doasedit file... + +Every argument will be treated as a file to edit. There's no support for +passing arguments to doas, so you can only doas root. + +This script is SECURITY SENSITIVE! Special care has been taken to correctly +preserve file attributes. Please exercise CAUTION when modifying AND using +this script. +EOF +} + +case "$1" in --help|-h) help; exit 0;; esac + +export TMPDIR=/dev/shm/ +trap 'trap - EXIT HUP QUIT TERM INT ABRT; rm -f "$tmp" "$tmpcopy"' EXIT HUP QUIT TERM INT ABRT + +for file; do + case "$file" in -*) file=./"$file" ;; esac + + tmp="$(mktemp)" + if [ -f "$file" ] && [ ! -r "$file" ]; then + doas cat "$file" > "$tmp" + elif [ -r "$file" ]; then + cat "$file" > "$tmp" + fi + + tmpcopy="$(mktemp)" + cat "$tmp" > "$tmpcopy" + + ${EDITOR:-vi} "$tmp" + + if cmp -s "$tmp" "$tmpcopy"; then + echo 'File unchanged, exiting...' + else + doas dd if="$tmp" of="$file" + echo 'Done, changes written' + fi + + rm "$tmp" "$tmpcopy" +done + |