Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ensure_admin ()
- {
- # Description: Function to elevate priviledges of script execution and to ensure administrative privileges
- # such that system wide settings or configuration may be done.
- #
- # This function evaluates and uses multiple ways to elevate priviledges based on the environment. In an
- # X session; and normal GUI w. systemd: elevate privs with normal GUI policy kit, otherwise try dialog
- # menu system and cached sudo, and finally fall back on good old sudo. Since scripts may be launched with
- # a GUI front (for user interaction) end and NO terminal; it is really preferable to display a proper
- # priviledge escalation propt in a GUI format and only use sudo in cases where there is a terminal or
- # no other option is present.
- #
- # Should the function not discover an X session, then dialog will be preferred over regular sudo.
- #
- # Globals: $UID
- # Arguments: None
- # Outputs: GUI
- # Dependencies: soft dependency on "dialog", will fall back on CLI if dialog is absent.
- # Returns: relaunches script under sudo.
- # Usage: The function expects no arguments, but will prompt for system password if required.
- # Usage: ensure_admin
- #
- # End of documentation
- if [ ! "$UID" -eq 0 ]; then
- if xset q &>/dev/null; then
- # If X is running in this session then...
- if echo $(systemctl get-default ) |grep graphical &>/dev/null ; then
- # If we are a normal GUI w. systemd: elevate privs with normal GUI policy kit...
- write_information "Allowing root to display menus..."
- xhost local:root
- write_information "Authenticating..."
- pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${0}
- exit
- else
- # If we are some other kind of archane GUI then...
- write_information "Allowing root to display menus..."
- xhost local:root
- write_information "Checking for dialog..."
- if hash dialog 2>/dev/null ; then
- token=$(dialog --title "${Title:-"sudo"}" --backtitle "${BackTitle:-"$( basename $0 )"}" --insecure --stdout --passwordbox "\n This functionality requires elevated priviledges. \n Please provide your (sudo) password" 10 90)
- ret=$? ; clear
- case $ret in
- 0)
- export HISTIGNORE='*sudo -S*'
- echo ${token} | sudo -S -v || ensure_admin
- sudo DISPLAY=$DISPLAY /bin/bash ${0} $@ || rtd_oem_pause 1
- ;;
- 1) echo "Request cancelled" ;;
- 255) echo "[esc] Request aborted" ;;
- * ) exit 1
- esac
- exit
- else
- # If we have some kind of X but not even "dialog" then...
- write_information "Dialog not found..."
- write_warning "This script needs administrative access..."
- xhost local:root
- sudo DISPLAY=$DISPLAY bash "${0}" "$@" || rtd_oem_pause 1
- exit
- fi
- fi
- else
- # If there is no X in this session...
- write_information "No X server at \$DISPLAY [$DISPLAY]"
- # Use dialog if possible, otherwise just terminal...
- if hash dialog 2>/dev/null ; then
- token=$(dialog --title "${Title:-"sudo"}" --backtitle "${BackTitle:-"$( basename $0 )"}" --insecure --stdout --passwordbox "\n This functionality requires elevated priviledges. \n Please provide your (sudo) password" 10 90)
- ret=$? ; clear
- case $ret in
- 0)
- export HISTIGNORE='*sudo -S*'
- echo ${token} | sudo -S -v || ensure_admin
- sudo DISPLAY=$DISPLAY /bin/bash ${0} $@ || rtd_oem_pause 1
- ;;
- 1) echo "Request cancelled" ;;
- 255) echo "[esc] Request aborted" ;;
- * ) exit 1
- esac
- exit
- else
- write_warning "This script needs administrative access..."
- sudo DISPLAY=$DISPLAY bash "${0}" "$@" || rtd_oem_pause 1
- exit
- fi
- fi
- else
- sudo sed -i s/'# session optional pam_xauth.so'/'session optional pam_xauth.so'/g /etc/pam.d/sudo
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement