From 5b53ffee104032899f25453bf4ae6b89ab1a03e6 Mon Sep 17 00:00:00 2001 From: John Sumsion Date: Mon, 24 Jun 2013 12:46:10 -0600 Subject: [PATCH 1/2] Pasted @Gen2ly's implementation over top of original. --- xuserrun | 86 ++++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/xuserrun b/xuserrun index 9401eed..a0dd0a6 100755 --- a/xuserrun +++ b/xuserrun @@ -1,56 +1,38 @@ #!/bin/bash - -# -# Run a command as the currently active X11 user -# +# Run a command as the currently active X.org server user seat="seat0" -# determine location of loginctl -LOGINCTL=$(command -v loginctl || command -v systemd-loginctl) -if [[ -e LOGINCTL ]]; then - echo "Error: Unable to find loginctl executable" - exit 1 -fi - -get_session_info() { - local session="$1" - local varname="$2" - local IFS=$'\n' - eval declare -Ag $varname - for row in $(loginctl show-session "$session"); do - key="$(echo "${row}"|cut -d= -f1)" - val="$(echo "${row}"|cut -d= -f2-)" - eval ${varname}[\"${key}\"]=\"${val}\" - done -} - -escape() { - for arg in "$@" ; do - printf "%q " "$arg"; - done; -} - -active_session="$(loginctl show-seat ${seat}|grep ActiveSession|cut -d= -f2)" -if [[ $? -ne 0 || -z $active_session ]]; then - echo "Error: Unable to determine active session" - exit 1 -fi - -get_session_info $active_session session_info - -if [[ ${session_info[Type]} != "x11" ]]; then - echo "Error: Active session is not x11" - exit 2 -fi - -current_user="$(id -u -n)" - -if [[ ${current_user} == ${session_info[Name]} ]]; then - # already correct user, no need to su - DISPLAY="${session_info[Display]}" "$@" -else - # run command as user - DISPLAY="${session_info[Display]}" su -c - "${session_info[Name]}" "$(escape "$@")" -fi - +# Program required check +hash loginctl 2>&- || { echo >&2 " Program \"loginctl\" not installed"; exit; } + +# Input parameter(s) are executable check +for p in "$@"; do + hash "$p" 2>&- || \ + { echo >&2 " The specified \"$p\" is not a valid executable"; exit 1; } +done + +# Search through active sessions for one that has the Display (X.org) value +avail_sessions=$(loginctl show-seat $seat -p Sessions| cut -d= -f2) +for session in $avail_sessions; do + session_disp=$(loginctl show-session $session -p Display | cut -d= -f2) + session_name=$(loginctl show-session $session -p Name | cut -d= -f2) + if [ -z $session_disp ]; then continue + else break + fi +done + +# Exit if no X.org server is running +[ -z "$session_disp" ] && echo " No X.org server appears to be running" && exit + +# Run command(s) as X.org server user +for x in "$@"; do + if [ $(whoami) = root ]; then + XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" \ + su -c - $session_name "$(printf "%q " "$x")" & + elif [ $(whoami) = $session_name ]; then + XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" "$x" & + else + echo " User does not have X.org server permissions" && exit + fi +done From 72a0fd962f836666ed0ed6bcb5b3ac32c0931d5a Mon Sep 17 00:00:00 2001 From: John Sumsion Date: Mon, 24 Jun 2013 12:48:38 -0600 Subject: [PATCH 2/2] Fixes applied to @Gen2ly's version to get arguments to be fed to commands. --- xuserrun | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/xuserrun b/xuserrun index a0dd0a6..bf3e1f8 100755 --- a/xuserrun +++ b/xuserrun @@ -7,10 +7,8 @@ seat="seat0" hash loginctl 2>&- || { echo >&2 " Program \"loginctl\" not installed"; exit; } # Input parameter(s) are executable check -for p in "$@"; do - hash "$p" 2>&- || \ - { echo >&2 " The specified \"$p\" is not a valid executable"; exit 1; } -done +hash "$1" 2>&- || \ +{ echo >&2 " The specified \"$1\" is not a valid executable"; exit 1; } # Search through active sessions for one that has the Display (X.org) value avail_sessions=$(loginctl show-seat $seat -p Sessions| cut -d= -f2) @@ -23,16 +21,14 @@ for session in $avail_sessions; do done # Exit if no X.org server is running -[ -z "$session_disp" ] && echo " No X.org server appears to be running" && exit +[ -z "$session_disp" ] && echo " No X.org server appears to be running" && exit 2 -# Run command(s) as X.org server user -for x in "$@"; do - if [ $(whoami) = root ]; then - XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" \ - su -c - $session_name "$(printf "%q " "$x")" & - elif [ $(whoami) = $session_name ]; then - XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" "$x" & - else - echo " User does not have X.org server permissions" && exit - fi -done +# Run command as X.org server user +if [ $(whoami) = root ]; then + XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" \ + su -c - $session_name "$(printf "%q " "$@")" & +elif [ $(whoami) = $session_name ]; then + eval XAUTHORITY=/home/$session_name/.Xauthority DISPLAY="$session_disp" "$@" & +else + echo " User does not have X.org server permissions" && exit +fi