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 # since I don't have su installed on some of my systems.
24 # [1] http://fgouget.free.fr/sux/
29 echo "usage: $(basename $0) [OPTION]... <sudo_options>"
30 echo "Execute a command as another user, with access to the X display"
32 echo "List of OPTIONs:"
34 echo " -u user Execute the program as the specified user (default is root)"
35 echo " -d display Set the X display"
36 echo " -h|--help Show this help text"
38 sudo -h | grep -v "^[ ]\+-u user"
47 [ "x$2" != "x" ] || { usage 1>&2; exit 1; }
53 [ "x$2" != "x" ] || { usage 1>&2; exit 1; }
69 id $USERNAME > /dev/null || { echo "Invalid user." 1>&2; exit 1; }
71 [ -n "$DISPLAY" ] || { echo "Cannot get the DISPLAY env variable." 1>&2; exit 1; }
73 [ "x$@" != "x" ] || { usage 1>&2; exit 1; }
76 xauth extract - $DISPLAY | sudo -u "$USERNAME" xauth merge -
78 # Execute the command.
79 # NOTE: -i or -s have to be passed in order to open a shell
80 sudo DISPLAY="$DISPLAY" -u "$USERNAME" "$@"