runasroot.sh revision 107d8363003c4c65ed6dad3994e710453dcf4cba
2N/A#!/bin/sh
2N/A# $Id$
2N/A## @file
2N/A# VirtualBox privileged execution helper script for Linux and Solaris
2N/A#
2N/A
2N/A#
2N/A# Copyright (C) 2009-2011 Oracle Corporation
2N/A#
2N/A# This file is part of VirtualBox Open Source Edition (OSE), as
2N/A# available from http://www.virtualbox.org. This file is free software;
2N/A# you can redistribute it and/or modify it under the terms of the GNU
2N/A# General Public License (GPL) as published by the Free Software
2N/A# Foundation, in version 2 as it comes in the "COPYING" file of the
2N/A# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2N/A# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2N/A#
2N/A
2N/A#include sh-utils.sh
2N/A
2N/Aostype=`uname -s`
2N/Aif test "$ostype" != "Linux" && test "$ostype" != "SunOS" ; then
2N/A echo "Linux/Solaris not detected."
2N/A exit 1
2N/Afi
2N/A
2N/AHAS_TERMINAL=""
2N/Acase "$1" in "--has-terminal")
2N/A shift
2N/A HAS_TERMINAL="yes"
2N/A ;;
2N/Aesac
2N/A
2N/Acase "$#" in "2"|"3")
2N/A ;;
2N/A *)
2N/A echo "Usage: `basename $0` DESCRIPTION COMMAND [ADVICE]" >&2
2N/A echo >&2
2N/A echo "Attempt to execute COMMAND with root privileges, displaying DESCRIPTION if" >&2
2N/A echo "possible and displaying ADVICE if possible if no su(1)-like tool is available." >&2
2N/A exit 1
2N/A ;;
2N/Aesac
DESCRIPTION=$1
COMMAND=$2
ADVICE=$3
PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11/bin
case "$ostype" in SunOS)
PATH=$PATH:/usr/sfw/bin:/usr/gnu/bin:/usr/xpg4/bin:/usr/xpg6/bin:/usr/openwin/bin:/usr/ucb
GKSU_SWITCHES="-au root"
;;
*)
GKSU_SWITCHES=""
;;
esac
case "$HAS_TERMINAL" in "")
case "$DISPLAY" in ?*)
KDESUDO="`mywhich kdesudo`"
case "$KDESUDO" in ?*)
eval "`quotify "$KDESUDO"` --comment `quotify "$DESCRIPTION"` -- $COMMAND"
exit
;;
esac
KDESU="`mywhich kdesu`"
case "$KDESU" in ?*)
"$KDESU" -c "$COMMAND"
exit
;;
esac
GKSU="`mywhich gksu`"
case "$GKSU" in ?*)
# Older gksu does not grok --description nor '--' and multiple args.
# @todo which versions do?
# "$GKSU" --description "$DESCRIPTION" -- "$@"
# Note that $GKSU_SWITCHES is NOT quoted in the following
"$GKSU" $GKSU_SWITCHES "$COMMAND"
exit
;;
esac
;;
esac # $DISPLAY
;;
esac # ! $HAS_TERMINAL
# pkexec may work for ssh console sessions as well if the right agents
# are installed. However it is very generic and does not allow for any
# custom messages. Thus it comes after gksu.
## @todo should we insist on either a display or a terminal?
# case "$DISPLAY$HAS_TERMINAL" in ?*)
PKEXEC="`mywhich pkexec`"
case "$PKEXEC" in ?*)
eval "\"$PKEXEC\" $COMMAND"
exit
;;
esac
# ;;S
#esac
case "$HAS_TERMINAL" in ?*)
USE_SUDO=
grep Ubuntu /etc/lsb-release && USE_SUDO=true
# On Ubuntu we need sudo instead of su. Assume this works, and is only
# needed for Ubuntu until proven wrong.
case $USE_SUDO in true)
SUDO_COMMAND="`quotify "$SUDO"` -- $COMMAND"
eval "$SUDO_COMMAND"
exit
;;
esac
SU="`mywhich su`"
case "$SU" in ?*)
"$SU" - root -c "$COMMAND"
exit
;;
esac
;;
esac
# The ultimate fallback is running 'su -' within an xterm. We use the
# title of the xterm to tell what is going on.
case "$DISPLAY" in ?*)
SU="`mywhich su`"
case "$SU" in ?*)
getxterm
case "$gxtpath" in ?*)
"$gxtpath" "$gxttitle" "$DESCRIPTION - su" "$gxtexec" su - root -c "$COMMAND"
exit
;;
esac
esac
esac # $DISPLAY
# Failure...
case "$DISPLAY" in ?*)
echo "Unable to locate 'pkexec', 'gksu' or 'su+xterm'. $ADVICE" >&2
;;
*)
echo "Unable to locate 'pkexec'. $ADVICE" >&2
;;
esac
exit 1