1 | #!/bin/sh
|
---|
2 | # $Id: runasroot.sh 36681 2011-04-15 10:17:02Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # VirtualBox privileged execution helper script for Linux and Solaris
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2009-2011 Oracle Corporation
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | # available from http://www.virtualbox.org. This file is free software;
|
---|
12 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | # General Public License (GPL) as published by the Free Software
|
---|
14 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | #
|
---|
18 |
|
---|
19 | #include sh-utils.sh
|
---|
20 |
|
---|
21 | ostype=`uname -s`
|
---|
22 | if test "$ostype" != "Linux" && test "$ostype" != "SunOS" ; then
|
---|
23 | echo "Linux/Solaris not detected."
|
---|
24 | exit 1
|
---|
25 | fi
|
---|
26 |
|
---|
27 | HAS_TERMINAL=""
|
---|
28 | case "$1" in "--has-terminal")
|
---|
29 | shift
|
---|
30 | HAS_TERMINAL="yes"
|
---|
31 | ;;
|
---|
32 | esac
|
---|
33 |
|
---|
34 | case "$#" in "2"|"3")
|
---|
35 | ;;
|
---|
36 | *)
|
---|
37 | echo "Usage: `basename $0` DESCRIPTION COMMAND [ADVICE]" >&2
|
---|
38 | echo >&2
|
---|
39 | echo "Attempt to execute COMMAND with root privileges, displaying DESCRIPTION if" >&2
|
---|
40 | echo "possible and displaying ADVICE if possible if no su(1)-like tool is available." >&2
|
---|
41 | exit 1
|
---|
42 | ;;
|
---|
43 | esac
|
---|
44 |
|
---|
45 | DESCRIPTION=$1
|
---|
46 | COMMAND=$2
|
---|
47 | ADVICE=$3
|
---|
48 | PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11/bin
|
---|
49 |
|
---|
50 | case "$ostype" in SunOS)
|
---|
51 | PATH=$PATH:/usr/sfw/bin:/usr/gnu/bin:/usr/xpg4/bin:/usr/xpg6/bin:/usr/openwin/bin:/usr/ucb
|
---|
52 | GKSU_SWITCHES="-au root"
|
---|
53 | ;;
|
---|
54 | *)
|
---|
55 | GKSU_SWITCHES=""
|
---|
56 | ;;
|
---|
57 | esac
|
---|
58 |
|
---|
59 | case "$DISPLAY" in ?*)
|
---|
60 | KDESUDO="`mywhich kdesudo`"
|
---|
61 | case "$KDESUDO" in ?*)
|
---|
62 | eval "`quotify "$KDESUDO"` --comment `quotify "$DESCRIPTION"` -- $COMMAND"
|
---|
63 | exit
|
---|
64 | ;;
|
---|
65 | esac
|
---|
66 |
|
---|
67 | GKSU="`mywhich gksu`"
|
---|
68 | case "$GKSU" in ?*)
|
---|
69 | # Older gksu does not grok --description nor '--' and multiple args.
|
---|
70 | # @todo which versions do?
|
---|
71 | # "$GKSU" --description "$DESCRIPTION" -- "$@"
|
---|
72 | # Note that $GKSU_SWITCHES is NOT quoted in the following
|
---|
73 | "$GKSU" $GKSU_SWITCHES "$COMMAND"
|
---|
74 | exit
|
---|
75 | ;;
|
---|
76 | esac
|
---|
77 | ;;
|
---|
78 | esac # $DISPLAY
|
---|
79 |
|
---|
80 | # pkexec may work for ssh console sessions as well if the right agents
|
---|
81 | # are installed. However it is very generic and does not allow for any
|
---|
82 | # custom messages. Thus it comes after gksu.
|
---|
83 | ## @todo should we insist on either a display or a terminal?
|
---|
84 | # case "$DISPLAY$HAS_TERMINAL" in ?*)
|
---|
85 | PKEXEC="`mywhich pkexec`"
|
---|
86 | case "$PKEXEC" in ?*)
|
---|
87 | eval "\"$PKEXEC\" $COMMAND"
|
---|
88 | exit
|
---|
89 | ;;
|
---|
90 | esac
|
---|
91 | # ;;S
|
---|
92 | #esac
|
---|
93 |
|
---|
94 | case "$HAS_TERMINAL" in ?*)
|
---|
95 | SU="`mywhich su`"
|
---|
96 | case "$SU" in ?*)
|
---|
97 | "$SU" - root -c "$COMMAND"
|
---|
98 | exit
|
---|
99 | ;;
|
---|
100 | esac
|
---|
101 | ;;
|
---|
102 | esac
|
---|
103 |
|
---|
104 | # The ultimate fallback is running 'su -' within an xterm. We use the
|
---|
105 | # title of the xterm to tell what is going on.
|
---|
106 | case "$DISPLAY" in ?*)
|
---|
107 | SU="`mywhich su`"
|
---|
108 | case "$SU" in ?*)
|
---|
109 | getxterm
|
---|
110 | case "$gxtpath" in ?*)
|
---|
111 | "$gxtpath" "$gxttitle" "$DESCRIPTION - su" "$gxtexec" su - root -c "$COMMAND"
|
---|
112 | exit
|
---|
113 | ;;
|
---|
114 | esac
|
---|
115 | esac
|
---|
116 | esac # $DISPLAY
|
---|
117 |
|
---|
118 | # Failure...
|
---|
119 | case "$DISPLAY" in ?*)
|
---|
120 | echo "Unable to locate 'pkexec', 'gksu' or 'su+xterm'. $ADVICE" >&2
|
---|
121 | ;;
|
---|
122 | *)
|
---|
123 | echo "Unable to locate 'pkexec'. $ADVICE" >&2
|
---|
124 | ;;
|
---|
125 | esac
|
---|
126 |
|
---|
127 | exit 1
|
---|