1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2025-02-05 07:43:22 -08:00

general improvements to maybesudo

This commit is contained in:
Connor Olding 2022-10-12 22:58:25 +02:00
parent d75d0c6d6c
commit c4b191b305

View file

@ -4,16 +4,17 @@
# YES_DASH # YES_DASH
# YES_ASH # YES_ASH
maybesudo_() { ### @- maybesudo_() ( ### @-
### mimic certain features of `sudo` for systems without it installed. ### mimic certain features of `sudo` for systems without it installed.
### as it stands, this mostly just handles some environment variables. ### as it stands, this mostly just handles some environment variables.
### ###
### try this: `maybesudo_ -u "$USER" printenv` ### try this: `maybesudo_ -u "$USER" printenv`
# TODO: you know, awk has an ENVIRON array that might be easier to work with.
local name= name=
local env_cleanup=0 env_cleanup=0
while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do
case $name in case "$name" in
K|V|k) K|V|k)
# K: sure kill # K: sure kill
# V: version # V: version
@ -74,70 +75,52 @@ maybesudo_() { ### @-
esac esac
done done
shift $((OPTIND-1)) shift "$((OPTIND-1))"
if [ "$env_cleanup" = 1 ]; then if [ "$env_cleanup" = 1 ]; then
( # portably listing exported variable names is virtually impossible
# portably listing exported variable names is virtually impossible # without the blessing of null-terminated strings, so don't even try.
# without the blessing of null-terminated strings, so don't even try. # just export the bare minimum that a fairly stock sudo would.
# just export the bare minimum that a fairly stock sudo would. # TODO: you know, awk has an ENVIRON array that might be easier to work with.
# $path is special in zsh, so call it pathy instead. # don't eat up precious command space with dumb colors.
local \ # xargs --show-limits says:
colors= display= dpkg_colors= home= hostname= krb5ccname= \ # Maximum length of command we could actually use: 5080
ls_colors= pathy= ps1= ps2= user= username= xauthority= \ [ "${#LS_COLORS}" -le 1024 ] || LS_COLORS=
xauthorization=
[ -z "$COLORS" ] || colors=COLORS="$COLORS" # doas seems to override PATH with /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
[ -z "$DISPLAY" ] || display=DISPLAY="$DISPLAY" # sudo also sets SUDO_COMMAND, SUDO_GID, SUDO_UID, SUDO_USER, and MAIL,
[ -z "$DPKG_COLORS" ] || dpkg_colors=DPKG_COLORS="$DPKG_COLORS" # but who needs those?
[ -z "$HOME" ] || home=HOME="$HOME" # "${USER:+DOAS_USER}=${USER}"
[ -z "$HOSTNAME" ] || hostname=HOSTNAME="$HOSTNAME" # "${USER:+SUDO_USER}=${USER}"
[ -z "$KRB5CCNAME" ] || krb5ccname=KRB5CCNAME="$KRB5CCNAME"
[ -z "$LS_COLORS" ] || ls_colors=LS_COLORS="$LS_COLORS"
[ -z "$PATH" ] || pathy=PATH="$PATH"
[ -z "$PS1" ] || ps1=PS1="$PS1"
[ -z "$PS2" ] || ps2=PS2="$PS2"
[ -z "$USER" ] || user=USER="$USER"
[ -z "$USERNAME" ] || username=USERNAME="$USERNAME"
[ -z "$XAUTHORITY" ] || xauthority=XAUTHORITY="$XAUTHORITY"
[ -z "$XAUTHORIZATION" ] || xauthorization=XAUTHORIZATION="$XAUTHORIZATION"
# don't eat up precious command space with dumb colors. # env seems to treat arguments of "=" as a no-op across GNU and busybox.
# xargs --show-limits says: env -i \
# Maximum length of command we could actually use: 5080 "${COLORS:+COLORS}=${COLORS}" \
[ "${#ls_colors}" -le 1024 ] || ls_colors== "${DISPLAY:+DISPLAY}=${DISPLAY}" \
"${DPKG_COLORS:+DPKG_COLORS}=${DPKG_COLORS}" \
# sudo also sets SUDO_COMMAND, SUDO_GID, SUDO_UID, SUDO_USER, and MAIL, "${HOME:+HOME}=${HOME}" \
# but who needs those? "${HOSTNAME:+HOSTNAME}=${HOSTNAME}" \
"${KRB5CCNAME:+KRB5CCNAME}=${KRB5CCNAME}" \
# env seems to treat arguments of "=" as a no-op across GNU and busybox. "${LOGNAME:+HOME}=${USER}" \
env -i \ "${LS_COLORS:+LS_COLORS}=${LS_COLORS}" \
"${colors:-=}" \ "${PATH:+PATH}=${PATH}" \
"${display:-=}" \ "${PS1:+PS1}=${PS1}" \
"${dpkg_colors:-=}" \ "${PS2:+PS2}=${PS2}" \
"${home:-=}" \ "${SHELL:+SHELL}=${SHELL}" \
"${hostname:-=}" \ "${TERM:+TERM}=${TERM}" \
"${krb5ccname:-=}" \ "${USER:+USER}=${USER}" \
"${ls_colors:-=}" \ "${USERNAME:+USERNAME}=${USERNAME}" \
"${pathy:-=}" \ "${XAUTHORITY:+XAUTHORITY}=${XAUTHORITY}" \
"${ps1:-=}" \ "${XAUTHORIZATION:+XAUTHORIZATION}=${XAUTHORIZATION}" \
"${ps2:-=}" \ -- "$@"
"${user:-=}" \
"${username:-=}" \
"${xauthority:-=}" \
"${xauthorization:-=}" \
"$@"
)
else else
# run it in a subshell so it won't affect ours. # run it through env anyway for consistency.
# TODO: run through env anyway for consistency. env -- "$@"
# (`maybesudo export` should fail)
( "$@"; )
fi fi
# don't put any code here or you'll clobber $?. # don't put any code here or you'll clobber $?.
} )
[ -n "${preload+-}" ] || maybesudo_ "$@" [ -n "${preload+-}" ] || maybesudo_ "$@"