2021-07-29 00:37:35 -07:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
# YES_ZSH
|
2021-07-29 05:44:12 -07:00
|
|
|
# YES_BASH
|
|
|
|
# YES_DASH
|
2021-09-23 06:48:05 -07:00
|
|
|
# YES_ASH
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2022-10-12 13:58:25 -07:00
|
|
|
maybesudo_() ( ### @-
|
2021-08-01 09:27:25 -07:00
|
|
|
### mimic certain features of `sudo` for systems without it installed.
|
|
|
|
### as it stands, this mostly just handles some environment variables.
|
2021-08-01 09:45:19 -07:00
|
|
|
###
|
|
|
|
### try this: `maybesudo_ -u "$USER" printenv`
|
2022-10-12 13:58:25 -07:00
|
|
|
|
|
|
|
name=
|
|
|
|
env_cleanup=0
|
|
|
|
|
2021-07-29 05:44:12 -07:00
|
|
|
while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do
|
2022-10-12 13:58:25 -07:00
|
|
|
case "$name" in
|
2021-07-29 00:37:35 -07:00
|
|
|
K|V|k)
|
|
|
|
# K: sure kill
|
|
|
|
# V: version
|
|
|
|
# k: kill
|
|
|
|
note 'maybesudo: your system does not have sudo installed!'
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
h)
|
|
|
|
printf "%s\n" 'maybesudo - a dumb utility for systems without sudo'
|
|
|
|
printf "\n"
|
|
|
|
printf "%s\n" 'usage: maybesudo -h | -V'
|
|
|
|
printf "%s\n" 'usage: maybesudo [command]'
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
A|E|H|P|S|n|p)
|
|
|
|
# A: askpass
|
|
|
|
# E: preserve environment
|
|
|
|
# H: HOME
|
|
|
|
# P: preserve group vector
|
|
|
|
# S: stdin (password)
|
|
|
|
# n: non-interactive
|
|
|
|
# p: prompt
|
|
|
|
note 'maybesudo: option has no effect --' "'$name'"
|
|
|
|
;;
|
|
|
|
|
|
|
|
C|U|b|g|i|l|r|s|t)
|
|
|
|
# C: close from (fd)
|
|
|
|
# U: other user (in conjunction wiht -l)
|
|
|
|
# b: background
|
|
|
|
# g: group
|
|
|
|
# i: simulate initial login (TODO)
|
|
|
|
# l: list
|
|
|
|
# r: role (SELinux)
|
|
|
|
# s: shell (TODO)
|
|
|
|
# t: type (SELinux)
|
|
|
|
note "maybesudo: unsupported option --" "'$name'"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
|
|
|
|
u) # user
|
|
|
|
if [ -z "$USER" -o "$OPTARG" != "$USER" ]; then
|
|
|
|
note 'maybesudo: users other than yourself are unsupported!'
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
env_cleanup=1
|
|
|
|
;;
|
|
|
|
|
|
|
|
:)
|
|
|
|
note 'maybesudo: option requires an argument --' "'$OPTARG'"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
default)
|
|
|
|
note 'maybesudo: invalid option --' "'$OPTARG'"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-10-12 13:58:25 -07:00
|
|
|
shift "$((OPTIND-1))"
|
2021-07-29 00:37:35 -07:00
|
|
|
|
|
|
|
if [ "$env_cleanup" = 1 ]; then
|
2022-10-12 13:58:25 -07:00
|
|
|
# portably listing exported variable names is virtually impossible
|
|
|
|
# without the blessing of null-terminated strings, so don't even try.
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# don't eat up precious command space with dumb colors.
|
|
|
|
# xargs --show-limits says:
|
|
|
|
# Maximum length of command we could actually use: 5080
|
|
|
|
[ "${#LS_COLORS}" -le 1024 ] || LS_COLORS=
|
|
|
|
|
|
|
|
# doas seems to override PATH with /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
|
|
|
|
# sudo also sets SUDO_COMMAND, SUDO_GID, SUDO_UID, SUDO_USER, and MAIL,
|
|
|
|
# but who needs those?
|
|
|
|
# "${USER:+DOAS_USER}=${USER}"
|
|
|
|
# "${USER:+SUDO_USER}=${USER}"
|
|
|
|
|
2022-10-12 16:56:34 -07:00
|
|
|
# coreutils treats '=' arguments as no-ops, but busybox does not, so...
|
2022-10-12 13:58:25 -07:00
|
|
|
env -i \
|
2022-10-12 16:56:34 -07:00
|
|
|
"${COLORS:+COLORS=}${COLORS:--i}" \
|
|
|
|
"${DISPLAY:+DISPLAY=}${DISPLAY:--i}" \
|
|
|
|
"${DPKG_COLORS:+DPKG_COLORS=}${DPKG_COLORS:--i}" \
|
|
|
|
"${HOME:+HOME=}${HOME:--i}" \
|
|
|
|
"${HOSTNAME:+HOSTNAME=}${HOSTNAME:--i}" \
|
|
|
|
"${KRB5CCNAME:+KRB5CCNAME=}${KRB5CCNAME:--i}" \
|
|
|
|
"${LOGNAME:+HOME=}${USER:--i}" \
|
|
|
|
"${LS_COLORS:+LS_COLORS=}${LS_COLORS:--i}" \
|
|
|
|
"${PATH:+PATH=}${PATH:--i}" \
|
|
|
|
"${SHELL:+SHELL=}${SHELL:--i}" \
|
|
|
|
"${TERM:+TERM=}${TERM:--i}" \
|
|
|
|
"${USER:+USER=}${USER:--i}" \
|
|
|
|
"${USERNAME:+USERNAME=}${USERNAME:--i}" \
|
|
|
|
"${XAUTHORITY:+XAUTHORITY=}${XAUTHORITY:--i}" \
|
|
|
|
"${XAUTHORIZATION:+XAUTHORIZATION=}${XAUTHORIZATION:--i}" \
|
2022-10-12 13:58:25 -07:00
|
|
|
-- "$@"
|
2021-07-29 00:37:35 -07:00
|
|
|
|
|
|
|
else
|
2022-10-12 13:58:25 -07:00
|
|
|
# run it through env anyway for consistency.
|
|
|
|
env -- "$@"
|
2021-07-29 00:37:35 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# don't put any code here or you'll clobber $?.
|
2022-10-12 13:58:25 -07:00
|
|
|
)
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2021-08-02 13:48:46 -07:00
|
|
|
[ -n "${preload+-}" ] || maybesudo_ "$@"
|