Skip to content

Instantly share code, notes, and snippets.

@mnordhoff
Created June 26, 2023 18:55
Show Gist options
  • Save mnordhoff/c232138f31696d7d3b14c3be571d5bac to your computer and use it in GitHub Desktop.
Save mnordhoff/c232138f31696d7d3b14c3be571d5bac to your computer and use it in GitHub Desktop.
_checkSudo() {
if [ -z "__INTERACTIVE" ]; then
#don't check if it's not in an interactive shell
return 0
fi
if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
#it's root using sudo, no matter it's using sudo or not, just fine
return 0
fi
if [ -n "$SUDO_COMMAND" ]; then
#it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1`
_endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1
return $?
fi
#otherwise
return 1
fi
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment