package-manager: use a generic su script from XDG (#235303, #244442)
[platform/upstream/libzypp.git] / tools / package-manager / package-manager-su
1 #!/bin/sh
2 #---------------------------------------------
3 #   xdg-su
4 #
5 #   Utility script to run a command as an alternate user, generally
6 #       the root user, with a graphical prompt for the root
7 #       password if needed
8 #
9 #   Refer to the usage() function below for usage.
10 #
11 #   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
12 #   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13 #
14 #   LICENSE:
15 #
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:
22 #
23 #   The above copyright notice and this permission notice shall be included
24 #   in all copies or substantial portions of the Software.
25 #
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.
33 #
34 #---------------------------------------------
35
36 manualpage()
37 {
38 cat << _MANUALPAGE
39 Name
40
41 xdg-su - run a GUI program as root after prompting for the root password
42
43 Synopsis
44
45 xdg-su [-u user] -c command
46
47 xdg-su { --help | --manual | --version }
48
49 Description
50
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.
53
54 xdg-su is for use inside a desktop session only.
55
56 xdg-su discards any stdout and stderr output from command.
57
58 Options
59
60 -u user
61     run command as user. The default is to run as root.
62 --help
63     Show command synopsis.
64 --manual
65     Show this manualpage.
66 --version
67     Show the xdg-utils version information.
68
69 Exit Codes
70
71 An exit code of 0 indicates success while a non-zero exit code indicates
72 failure. The following failure codes can be returned:
73
74 1
75     Error in command line syntax.
76 2
77     One of the files passed on the command line did not exist.
78 3
79     A required tool could not be found.
80 4
81     The action failed.
82
83 See Also
84
85 su(1)
86
87 Examples
88
89 xdg-su -u root -c "/opt/shinythings/bin/install-GUI --install fast"
90
91 Runs the /opt/shinythings/bin/install-GUI command with root permissions.
92
93 _MANUALPAGE
94 }
95
96 usage()
97 {
98 cat << _USAGE
99 xdg-su - run a GUI program as root after prompting for the root password
100
101 Synopsis
102
103 xdg-su [-u user] -c command
104
105 xdg-su { --help | --manual | --version }
106
107 _USAGE
108 }
109
110 #@xdg-utils-common@
111
112 #----------------------------------------------------------------------------
113 #   Common utility functions included in all XDG wrapper scripts
114 #----------------------------------------------------------------------------
115
116 DEBUG()
117 {
118   [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt $1 ] && return 0;
119   shift
120   echo "$@" >&2
121 }
122
123 #-------------------------------------------------------------
124 # Exit script on successfully completing the desired operation
125
126 exit_success()
127 {
128     if [ $# -gt 0 ]; then
129         echo "$@"
130         echo
131     fi
132
133     exit 0
134 }
135
136
137 #-----------------------------------------
138 # Exit script on malformed arguments, not enough arguments
139 # or missing required option.
140 # prints usage information
141
142 exit_failure_syntax()
143 {
144     if [ $# -gt 0 ]; then
145         echo "xdg-su: $@" >&2
146         echo "Try 'xdg-su --help' for more information." >&2
147     else
148         usage
149         echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info."
150     fi
151
152     exit 1
153 }
154
155 #-------------------------------------------------------------
156 # Exit script on missing file specified on command line
157
158 exit_failure_file_missing()
159 {
160     if [ $# -gt 0 ]; then
161         echo "xdg-su: $@" >&2
162     fi
163
164     exit 2
165 }
166
167 #-------------------------------------------------------------
168 # Exit script on failure to locate necessary tool applications
169
170 exit_failure_operation_impossible()
171 {
172     if [ $# -gt 0 ]; then
173         echo "xdg-su: $@" >&2
174     fi
175
176     exit 3
177 }
178
179 #-------------------------------------------------------------
180 # Exit script on failure returned by a tool application
181
182 exit_failure_operation_failed()
183 {
184     if [ $# -gt 0 ]; then
185         echo "xdg-su: $@" >&2
186     fi
187
188     exit 4
189 }
190
191 #------------------------------------------------------------
192 # Exit script on insufficient permission to read a specified file
193
194 exit_failure_file_permission_read()
195 {
196     if [ $# -gt 0 ]; then
197         echo "xdg-su: $@" >&2
198     fi
199
200     exit 5
201 }
202
203 #------------------------------------------------------------
204 # Exit script on insufficient permission to read a specified file
205
206 exit_failure_file_permission_write()
207 {
208     if [ $# -gt 0 ]; then
209         echo "xdg-su: $@" >&2
210     fi
211
212     exit 6
213 }
214
215 check_input_file()
216 {
217     if [ ! -e "$1" ]; then
218         exit_failure_file_missing "file '$1' does not exist"
219     fi
220     if [ ! -r "$1" ]; then
221         exit_failure_file_permission_read "no permission to read file '$1'"
222     fi
223 }
224
225 check_vendor_prefix()
226 {
227     file=`basename "$1"`
228     case "$file" in
229        [a-zA-Z]*-*)
230          return
231          ;;
232     esac
233
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
238     exit 1
239 }
240
241 check_output_file()
242 {
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
245     if [ -e "$1" ]; then
246         if [ ! -w "$1" ]; then
247             exit_failure_file_permission_write "no permission to write to file '$1'"
248         fi
249     else
250         DIR=`dirname "$1"`
251         if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
252             exit_failure_file_permission_write "no permission to create file '$1'"
253         fi
254     fi
255 }
256
257 #----------------------------------------
258 # Checks for shared commands, e.g. --help
259
260 check_common_commands()
261 {
262     while [ $# -gt 0 ] ; do
263         parm="$1"
264         shift
265
266         case "$parm" in
267             --help)
268             usage
269             echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info."
270             exit_success
271             ;;
272
273             --manual)
274             manualpage
275             exit_success
276             ;;
277
278             --version)
279             echo "xdg-su 1.0beta2"
280             exit_success
281             ;;
282         esac
283     done
284 }
285
286 check_common_commands "$@"
287 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
288     # Be silent
289     xdg_redirect_output=" > /dev/null 2> /dev/null"
290 else
291     # All output to stderr
292     xdg_redirect_output=" >&2"
293 fi
294
295 #--------------------------------------
296 # Checks for known desktop environments
297 # set variable DE to the desktop environments name, lowercase
298
299 detectDE()
300 {
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     fi
305 }
306
307 #----------------------------------------------------------------------------
308
309
310
311 su_kde()
312 {
313     KDESU=`which kdesu 2>/dev/null`
314     if [ $? -eq 0 ] ; then
315         if [ -z "$user" ] ; then
316              $KDESU -c "$cmd"
317         else
318              $KDESU -u "$user" -c "$cmd"
319         fi
320
321         if [ $? -eq 0 ]; then
322             exit_success
323         else
324             exit_failure_operation_failed
325         fi
326     else
327         su_generic
328     fi
329 }
330
331 su_gnome()
332 {
333     GSU=`which gnomesu 2>/dev/null`
334     if [ $? -ne 0 ] ; then
335         GSU=`which xsu 2>/dev/null`
336     fi
337     if [ $? -eq 0 ] ; then
338         if [ -z "$user" ] ; then
339             $GSU -c "$cmd"
340         else
341             $GSU -u "$user" -c "$cmd"
342         fi
343
344         if [ $? -eq 0 ]; then
345             exit_success
346         else
347             exit_failure_operation_failed
348         fi
349     else
350         su_generic
351     fi
352 }
353
354 su_generic()
355 {
356     if [ -z "$user" ] ; then
357         xterm -geom 60x5 -T "xdg-su: $cmd" -e su -c "$cmd"
358     else
359         xterm -geom 60x5 -T "xdg-su: $cmd" -e su -u "$user" -c "$cmd"
360     fi
361
362     if [ $? -eq 0 ]; then
363         exit_success
364     else
365         exit_failure_operation_failed
366     fi
367 }
368
369 [ x"$1" != x"" ] || exit_failure_syntax
370
371 user=
372 cmd=
373 while [ $# -gt 0 ] ; do
374     parm="$1"
375     shift
376
377     case "$parm" in
378       -u)
379         if [ -z "$1" ] ; then
380             exit_failure_syntax "user argument missing for -u"
381         fi
382         user="$1"
383         shift
384         ;;
385
386       -c)
387         if [ -z "$1" ] ; then
388             exit_failure_syntax "command argument missing for -c"
389         fi
390         cmd="$1"
391         shift
392         ;;
393
394       -*)
395         exit_failure_syntax "unexpected option '$parm'"
396         ;;
397
398       *)
399         exit_failure_syntax "unexpected argument '$parm'"
400         ;;
401     esac
402 done
403
404 if [ -z "${cmd}" ] ; then
405     exit_failure_syntax "command missing"
406 fi
407
408 detectDE
409
410 if [ x"$DE" = x"" ]; then
411     XSU=`which xsu 2>/dev/null`
412     if [ $? -eq 0 ] ; then
413         DE=generic
414     fi
415 fi
416
417 case "$DE" in
418     kde)
419     su_kde
420     ;;
421
422     gnome)
423     su_gnome
424     ;;
425
426     generic)
427     su_generic
428     ;;
429
430     *)
431     [ x"$user" = x"" ] && user=root 
432     exit_failure_operation_impossible "no graphical method available for invoking '$cmd' as '$user'"
433     ;;
434 esac