3 # Execute a command as another user, with access to the X display
5 # Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # This is the equivalent of sux[1] but simpler and using sudo.
22 # [1] http://fgouget.free.fr/sux/
27 echo "usage: $(basename $0) [OPTION]... <sudo_options>"
28 echo "Execute a command as another user, with access to the X display"
30 echo "List of OPTIONs:"
32 echo " -u user Execute the program as the specified user (default is root)"
33 echo " -d display Set the X display"
34 echo " -h|--help Show this help text"
36 sudo -h | grep -v "^[ ]\+-u user"
45 [ "x$2" != "x" ] || { usage 1>&2; exit 1; }
51 [ "x$2" != "x" ] || { usage 1>&2; exit 1; }
67 id $USERNAME > /dev/null || { echo "Invalid user." 1>&2; exit 1; }
69 [ -n "$DISPLAY" ] || { echo "Cannot get the DISPLAY env variable." 1>&2; exit 1; }
71 [ "x$@" != "x" ] || { usage 1>&2; exit 1; }
74 xauth extract - $DISPLAY | sudo -u "$USERNAME" xauth merge -
76 # Execute the command.
77 # NOTE: -i or -s have to be passed in order to open a shell
78 sudo DISPLAY="$DISPLAY" -u "$USERNAME" "$@"