Cleanup orphaned raw and solv caches (bnc#853065)
[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     elif [ x"$DESKTOP_SESSION" == x"LXDE" ]; then DE=lxde;
305     fi
306 }
307
308 #----------------------------------------------------------------------------
309
310
311
312 su_kde()
313 {
314     KDESU=`which kdesu 2>/dev/null`
315     if [ $? -eq 0 ] ; then
316         if [ -z "$user" ] ; then
317              $KDESU -c "$cmd"
318         else
319              $KDESU -u "$user" -c "$cmd"
320         fi
321
322         if [ $? -eq 0 ]; then
323             exit_success
324         else
325             exit_failure_operation_failed
326         fi
327     else
328         su_generic
329     fi
330 }
331
332 su_gnome()
333 {
334     GSU=`which gnomesu 2>/dev/null`
335     if [ $? -ne 0 ] ; then
336         GSU=`which xsu 2>/dev/null`
337     fi
338     if [ $? -eq 0 ] ; then
339         if [ -z "$user" ] ; then
340             $GSU -c "$cmd"
341         else
342             $GSU -u "$user" -c "$cmd"
343         fi
344
345         if [ $? -eq 0 ]; then
346             exit_success
347         else
348             exit_failure_operation_failed
349         fi
350     else
351         su_generic
352     fi
353 }
354
355 su_generic()
356 {
357     if [ -z "$user" ] ; then
358         xterm -geom 60x5 -T "xdg-su: $cmd" -e su -c "$cmd"
359     else
360         xterm -geom 60x5 -T "xdg-su: $cmd" -e su -u "$user" -c "$cmd"
361     fi
362
363     if [ $? -eq 0 ]; then
364         exit_success
365     else
366         exit_failure_operation_failed
367     fi
368 }
369
370
371 su_xfce()
372 {
373     if which gnomesu &>/dev/null ; then
374         su_gnome
375     else
376         su_generic
377     fi
378 }
379
380 su_lxde()
381 {
382     if which gnomesu &>/dev/null ; then
383         su_gnome
384     else
385         su_generic
386     fi
387 }
388
389 [ x"$1" != x"" ] || exit_failure_syntax
390
391 user=
392 cmd=
393 while [ $# -gt 0 ] ; do
394     parm="$1"
395     shift
396
397     case "$parm" in
398       -u)
399         if [ -z "$1" ] ; then
400             exit_failure_syntax "user argument missing for -u"
401         fi
402         user="$1"
403         shift
404         ;;
405
406       -c)
407         if [ -z "$1" ] ; then
408             exit_failure_syntax "command argument missing for -c"
409         fi
410         cmd="$1"
411         shift
412         ;;
413
414       -*)
415         exit_failure_syntax "unexpected option '$parm'"
416         ;;
417
418       *)
419         exit_failure_syntax "unexpected argument '$parm'"
420         ;;
421     esac
422 done
423
424 if [ -z "${cmd}" ] ; then
425     exit_failure_syntax "command missing"
426 fi
427
428 detectDE
429
430 if [ x"$DE" = x"" ]; then
431     which xterm 2>/dev/null >&2
432     if [ $? -eq 0 -a -n "$DISPLAY" ] ; then
433         DE=generic
434     fi
435 fi
436
437 case "$DE" in
438     kde)
439     su_kde
440     ;;
441
442     gnome)
443     su_gnome
444     ;;
445
446     generic)
447     su_generic
448     ;;
449
450     xfce)
451     su_xfce
452     ;;
453
454     lxde)
455     su_lxde
456     ;;
457
458     *)
459     [ x"$user" = x"" ] && user=root 
460     exit_failure_operation_impossible "no graphical method available for invoking '$cmd' as '$user'"
461     ;;
462 esac