1 | #!/bin/sh
|
---|
2 | # $Id: runasroot.sh 56299 2015-06-09 14:35:06Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # VirtualBox privileged execution helper script for Linux and Solaris
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2009-2015 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 "$HAS_TERMINAL" in "")
|
---|
60 | case "$DISPLAY" in ?*)
|
---|
61 | KDESUDO="`mywhich kdesudo`"
|
---|
62 | case "$KDESUDO" in ?*)
|
---|
63 | eval "`quotify "$KDESUDO"` --comment `quotify "$DESCRIPTION"` -- $COMMAND"
|
---|
64 | exit
|
---|
65 | ;;
|
---|
66 | esac
|
---|
67 |
|
---|
68 | KDESU="`mywhich kdesu`"
|
---|
69 | case "$KDESU" in ?*)
|
---|
70 | "$KDESU" -c "$COMMAND"
|
---|
71 | exit
|
---|
72 | ;;
|
---|
73 | esac
|
---|
74 |
|
---|
75 | GKSU="`mywhich gksu`"
|
---|
76 | case "$GKSU" in ?*)
|
---|
77 | # Older gksu does not grok --description nor '--' and multiple args.
|
---|
78 | # @todo which versions do?
|
---|
79 | # "$GKSU" --description "$DESCRIPTION" -- "$@"
|
---|
80 | # Note that $GKSU_SWITCHES is NOT quoted in the following
|
---|
81 | "$GKSU" $GKSU_SWITCHES "$COMMAND"
|
---|
82 | exit
|
---|
83 | ;;
|
---|
84 | esac
|
---|
85 | ;;
|
---|
86 | esac # $DISPLAY
|
---|
87 | ;;
|
---|
88 | esac # ! $HAS_TERMINAL
|
---|
89 |
|
---|
90 | # pkexec may work for ssh console sessions as well if the right agents
|
---|
91 | # are installed. However it is very generic and does not allow for any
|
---|
92 | # custom messages. Thus it comes after gksu.
|
---|
93 | ## @todo should we insist on either a display or a terminal?
|
---|
94 | # case "$DISPLAY$HAS_TERMINAL" in ?*)
|
---|
95 | PKEXEC="`mywhich pkexec`"
|
---|
96 | case "$PKEXEC" in ?*)
|
---|
97 | eval "\"$PKEXEC\" $COMMAND"
|
---|
98 | exit
|
---|
99 | ;;
|
---|
100 | esac
|
---|
101 | # ;;S
|
---|
102 | #esac
|
---|
103 |
|
---|
104 | case "$HAS_TERMINAL" in ?*)
|
---|
105 | USE_SUDO=
|
---|
106 | grep -q Ubuntu /etc/lsb-release 2>/dev/null && USE_SUDO=true
|
---|
107 | # On Ubuntu we need sudo instead of su. Assume this works, and is only
|
---|
108 | # needed for Ubuntu until proven wrong.
|
---|
109 | case $USE_SUDO in true)
|
---|
110 | SUDO_COMMAND="`quotify "$SUDO"` -- $COMMAND"
|
---|
111 | eval "$SUDO_COMMAND"
|
---|
112 | exit
|
---|
113 | ;;
|
---|
114 | esac
|
---|
115 |
|
---|
116 | SU="`mywhich su`"
|
---|
117 | case "$SU" in ?*)
|
---|
118 | "$SU" - root -c "$COMMAND"
|
---|
119 | exit
|
---|
120 | ;;
|
---|
121 | esac
|
---|
122 | ;;
|
---|
123 | esac
|
---|
124 |
|
---|
125 | # The ultimate fallback is running 'su -' within an xterm. We use the
|
---|
126 | # title of the xterm to tell what is going on.
|
---|
127 | case "$DISPLAY" in ?*)
|
---|
128 | SU="`mywhich su`"
|
---|
129 | case "$SU" in ?*)
|
---|
130 | getxterm
|
---|
131 | case "$gxtpath" in ?*)
|
---|
132 | "$gxtpath" "$gxttitle" "$DESCRIPTION - su" "$gxtexec" su - root -c "$COMMAND"
|
---|
133 | exit
|
---|
134 | ;;
|
---|
135 | esac
|
---|
136 | esac
|
---|
137 | esac # $DISPLAY
|
---|
138 |
|
---|
139 | # Failure...
|
---|
140 | case "$DISPLAY" in ?*)
|
---|
141 | echo "Unable to locate 'pkexec', 'gksu' or 'su+xterm'. $ADVICE" >&2
|
---|
142 | ;;
|
---|
143 | *)
|
---|
144 | echo "Unable to locate 'pkexec'. $ADVICE" >&2
|
---|
145 | ;;
|
---|
146 | esac
|
---|
147 |
|
---|
148 | exit 1
|
---|