2 #---------------------------------------------
5 # Utility script to run a command as an alternate user, generally
6 # the root user, with a graphical prompt for the root
9 # Refer to the usage() function below for usage.
11 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
12 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
16 # Permission is hereby granted, free of charge, to any person obtaining a
17 # copy of this software and associated documentation files (the "Software"),
18 # to deal in the Software without restriction, including without limitation
19 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 # and/or sell copies of the Software, and to permit persons to whom the
21 # Software is furnished to do so, subject to the following conditions:
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 # OTHER DEALINGS IN THE SOFTWARE.
34 #---------------------------------------------
41 xdg-su - run a GUI program as root after prompting for the root password
45 xdg-su [-u user] -c command
47 xdg-su { --help | --manual | --version }
51 xdg-su provides a graphical dialog that prompts the user for a password to run
52 command as user or as root if no user was specified.
54 xdg-su is for use inside a desktop session only.
56 xdg-su discards any stdout and stderr output from command.
61 run command as user. The default is to run as root.
63 Show command synopsis.
67 Show the xdg-utils version information.
71 An exit code of 0 indicates success while a non-zero exit code indicates
72 failure. The following failure codes can be returned:
75 Error in command line syntax.
77 One of the files passed on the command line did not exist.
79 A required tool could not be found.
89 xdg-su -u root -c "/opt/shinythings/bin/install-GUI --install fast"
91 Runs the /opt/shinythings/bin/install-GUI command with root permissions.
99 xdg-su - run a GUI program as root after prompting for the root password
103 xdg-su [-u user] -c command
105 xdg-su { --help | --manual | --version }
112 #----------------------------------------------------------------------------
113 # Common utility functions included in all XDG wrapper scripts
114 #----------------------------------------------------------------------------
118 [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt $1 ] && return 0;
123 #-------------------------------------------------------------
124 # Exit script on successfully completing the desired operation
128 if [ $# -gt 0 ]; then
137 #-----------------------------------------
138 # Exit script on malformed arguments, not enough arguments
139 # or missing required option.
140 # prints usage information
142 exit_failure_syntax()
144 if [ $# -gt 0 ]; then
145 echo "xdg-su: $@" >&2
146 echo "Try 'xdg-su --help' for more information." >&2
149 echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info."
155 #-------------------------------------------------------------
156 # Exit script on missing file specified on command line
158 exit_failure_file_missing()
160 if [ $# -gt 0 ]; then
161 echo "xdg-su: $@" >&2
167 #-------------------------------------------------------------
168 # Exit script on failure to locate necessary tool applications
170 exit_failure_operation_impossible()
172 if [ $# -gt 0 ]; then
173 echo "xdg-su: $@" >&2
179 #-------------------------------------------------------------
180 # Exit script on failure returned by a tool application
182 exit_failure_operation_failed()
184 if [ $# -gt 0 ]; then
185 echo "xdg-su: $@" >&2
191 #------------------------------------------------------------
192 # Exit script on insufficient permission to read a specified file
194 exit_failure_file_permission_read()
196 if [ $# -gt 0 ]; then
197 echo "xdg-su: $@" >&2
203 #------------------------------------------------------------
204 # Exit script on insufficient permission to read a specified file
206 exit_failure_file_permission_write()
208 if [ $# -gt 0 ]; then
209 echo "xdg-su: $@" >&2
217 if [ ! -e "$1" ]; then
218 exit_failure_file_missing "file '$1' does not exist"
220 if [ ! -r "$1" ]; then
221 exit_failure_file_permission_read "no permission to read file '$1'"
225 check_vendor_prefix()
234 echo "xdg-su: filename '$file' does not have a proper vendor prefix" >&2
235 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
236 echo 'with a dash ("-"). An example filename is '"'example-$file'" >&2
237 echo "Use --novendor to override or 'xdg-su --manual' for additional info." >&2
243 # if the file exists, check if it is writeable
244 # if it does not exists, check if we are allowed to write on the directory
246 if [ ! -w "$1" ]; then
247 exit_failure_file_permission_write "no permission to write to file '$1'"
251 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
252 exit_failure_file_permission_write "no permission to create file '$1'"
257 #----------------------------------------
258 # Checks for shared commands, e.g. --help
260 check_common_commands()
262 while [ $# -gt 0 ] ; do
269 echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info."
279 echo "xdg-su 1.0beta2"
286 check_common_commands "$@"
287 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
289 xdg_redirect_output=" > /dev/null 2> /dev/null"
291 # All output to stderr
292 xdg_redirect_output=" >&2"
295 #--------------------------------------
296 # Checks for known desktop environments
297 # set variable DE to the desktop environments name, lowercase
301 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
302 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
303 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
304 elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde;
308 #----------------------------------------------------------------------------
314 KDESU=`which kdesu 2>/dev/null`
315 if [ $? -eq 0 ] ; then
316 if [ -z "$user" ] ; then
319 $KDESU -u "$user" -c "$cmd"
322 if [ $? -eq 0 ]; then
325 exit_failure_operation_failed
334 GSU=`which gnomesu 2>/dev/null`
335 if [ $? -ne 0 ] ; then
336 GSU=`which xsu 2>/dev/null`
338 if [ $? -eq 0 ] ; then
339 if [ -z "$user" ] ; then
342 $GSU -u "$user" -c "$cmd"
345 if [ $? -eq 0 ]; then
348 exit_failure_operation_failed
357 if [ -z "$user" ] ; then
358 xterm -geom 60x5 -T "xdg-su: $cmd" -e su -c "$cmd"
360 xterm -geom 60x5 -T "xdg-su: $cmd" -e su -u "$user" -c "$cmd"
363 if [ $? -eq 0 ]; then
366 exit_failure_operation_failed
373 if which gnomesu &>/dev/null ; then
382 if which gnomesu &>/dev/null ; then
389 [ x"$1" != x"" ] || exit_failure_syntax
393 while [ $# -gt 0 ] ; do
399 if [ -z "$1" ] ; then
400 exit_failure_syntax "user argument missing for -u"
407 if [ -z "$1" ] ; then
408 exit_failure_syntax "command argument missing for -c"
415 exit_failure_syntax "unexpected option '$parm'"
419 exit_failure_syntax "unexpected argument '$parm'"
424 if [ -z "${cmd}" ] ; then
425 exit_failure_syntax "command missing"
430 if [ x"$DE" = x"" ]; then
431 which xterm 2>/dev/null >&2
432 if [ $? -eq 0 -a -n "$DISPLAY" ] ; then
459 [ x"$user" = x"" ] && user=root
460 exit_failure_operation_impossible "no graphical method available for invoking '$cmd' as '$user'"