workspace/arch-user/bin/wsudo
2025-10-02 07:46:48 +00:00

118 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
parse() {
sudo_user="root"
sudo_command=""
sudo_login_shell=""
local args=("$@")
local i=0
local arg_count=${#args[@]}
while [[ $i -lt $arg_count ]]; do
case "${args[$i]}" in
-u|--user)
((i++))
if [[ $i -lt $arg_count ]]; then
sudo_user="${args[$i]}"
fi
((i++))
;;
-i|--login)
sudo_login_shell=true
((i++))
;;
-s|--shell)
((i++))
;;
--)
((i++))
sudo_command="${args[*]:$i}"
break
;;
-*)
((i++))
;;
su)
((i++))
local su_user=""
while [[ $i -lt $arg_count ]]; do
case "${args[$i]}" in
-|-l|--login)
sudo_login_shell=true
((i++))
;;
-c|--command)
((i++))
if [[ $i -lt $arg_count ]]; then
sudo_command="${args[$i]}"
((i++))
fi
;;
--session-command=*)
sudo_command="${args[$i]#*=}"
((i++))
;;
-m|-p|--preserve-environment)
((i++))
;;
-s|--shell)
((i++))
if [[ $i -lt $arg_count && "${args[$i]}" != -* ]]; then
((i++))
fi
;;
-*)
((i++))
;;
*)
if [[ -z "$su_user" ]]; then
su_user="${args[$i]}"
((i++))
else
((i++))
fi
;;
esac
done
if [[ -n "$su_user" ]]; then
sudo_user="$su_user"
fi
break
;;
*)
sudo_command="${args[*]:$i}"
break
;;
esac
done
}
parse "$@"
[ -z "$WSCOMPAT_MASTER" ] && WSCOMPAT_MASTER="$(whoami)"
read -r -d '' init_cmd << WSCOMPAT_EOF_9oqn7IVF || true
export WS_IS_COMPAT=1
export WSCOMPAT_MASTER="$WSCOMPAT_MASTER"
export WSCOMPAT_DIR="/tmp/wscompat_${WSCOMPAT_MASTER}_\$(whoami)"
rm -rf "\$WSCOMPAT_DIR" && mkdir -p "\$WSCOMPAT_DIR" && chmod 700 "\$WSCOMPAT_DIR"
cat << 'WSCOMPAT_EOF_84EbM3la' > "\$WSCOMPAT_DIR/profile"
$(
sed 's/WSCOMPAT_EOF_/WSCOMPAT_EOF__/g' "$WSCOMPAT_DIR/profile"
for file in "$WSCOMPAT_DIR.profile"*.sh; do
if [ -f "$file" ]; then
sed 's/WSCOMPAT_EOF_/WSCOMPAT_EOF__/g' "$file"
fi
done
)
WSCOMPAT_EOF_84EbM3la
. \$WSCOMPAT_DIR/profile
WSCOMPAT_EOF_9oqn7IVF
if [ -z "$sudo_command" ]; then
sudo_command="exec /bin/bash --rcfile \"\$WSCOMPAT_DIR/bashrc\""
fi
if [ "$sudo_login_shell" ]; then
bash_flags="--login --noprofile --norc";
else
bash_flags="--noprofile --norc";
fi
exec /bin/sudo -u "$sudo_user" /bin/bash $bash_flags -c "$init_cmd; $sudo_command"