home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Aplikacje_64-bitowe / VirtualBox / VirtualBox-4.1.2-73507-Win.exe / file_VBoxGuestAdditions.iso / runasroot.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2011-08-15  |  5.4 KB  |  197 lines

  1. #!/bin/sh
  2. # $Id: runasroot.sh 71330 2011-04-21 08:07:14Z michael $
  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. # $Id: sh-utils.sh 71324 2011-04-20 20:55:49Z michael $
  20. # Shell script include file
  21. ## @file
  22. # Shell script routines which are likely to be useful for different scripts
  23. #
  24.  
  25. #
  26. # Copyright (C) 2009-2011 Oracle Corporation
  27. #
  28. # This file is part of VirtualBox Open Source Edition (OSE), as
  29. # available from http://www.virtualbox.org. This file is free software;
  30. # you can redistribute it and/or modify it under the terms of the GNU
  31. # General Public License (GPL) as published by the Free Software
  32. # Foundation, in version 2 as it comes in the "COPYING" file of the
  33. # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  34. # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  35. #
  36.  
  37. # Deal with differing "which" semantics
  38. mywhich() {
  39.     which "$1" 2>/dev/null | grep -v "no $1"
  40. }
  41.  
  42. # Get the name and execute switch for a useful terminal emulator
  43. #
  44. # Sets $gxtpath to the emulator path or empty
  45. # Sets $gxttitle to the "title" switch for that emulator
  46. # Sets $gxtexec to the "execute" switch for that emulator
  47. # May clobber $gtx*
  48. # Calls mywhich
  49. getxterm() {
  50.     # gnome-terminal uses -e differently to other emulators
  51.     for gxti in "konsole --title -e" "gnome-terminal --title -x" "xterm -T -e"; do
  52.         set $gxti
  53.         gxtpath="`mywhich $1`"
  54.         case "$gxtpath" in ?*)
  55.             gxttitle=$2
  56.             gxtexec=$3
  57.             return
  58.             ;;
  59.         esac
  60.     done
  61. }
  62.  
  63. # Quotes its argument by inserting '\' in front of every character save
  64. # for 'A-Za-z0-9/'.  Prints the result to stdout.
  65. quotify() {
  66.     echo "$1" | sed -e 's/\([^a-zA-Z0-9/]\)/\\\1/g'
  67. }
  68.  
  69. ostype=`uname -s`
  70. if test "$ostype" != "Linux" && test "$ostype" != "SunOS" ; then
  71.   echo "Linux/Solaris not detected."
  72.   exit 1
  73. fi
  74.  
  75. HAS_TERMINAL=""
  76. case "$1" in "--has-terminal")
  77.     shift
  78.     HAS_TERMINAL="yes"
  79.     ;;
  80. esac
  81.  
  82. case "$#" in "2"|"3")
  83.     ;;
  84.     *)
  85.     echo "Usage: `basename $0` DESCRIPTION COMMAND [ADVICE]" >&2
  86.     echo >&2
  87.     echo "Attempt to execute COMMAND with root privileges, displaying DESCRIPTION if" >&2
  88.     echo "possible and displaying ADVICE if possible if no su(1)-like tool is available." >&2
  89.     exit 1
  90.     ;;
  91. esac
  92.  
  93. DESCRIPTION=$1
  94. COMMAND=$2
  95. ADVICE=$3
  96. PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11/bin
  97.  
  98. case "$ostype" in SunOS)
  99.     PATH=$PATH:/usr/sfw/bin:/usr/gnu/bin:/usr/xpg4/bin:/usr/xpg6/bin:/usr/openwin/bin:/usr/ucb
  100.     GKSU_SWITCHES="-au root"
  101.     ;;
  102.     *)
  103.     GKSU_SWITCHES=""
  104.     ;;
  105. esac
  106.  
  107. case "$HAS_TERMINAL" in "")
  108.     case "$DISPLAY" in ?*)
  109.         KDESUDO="`mywhich kdesudo`"
  110.         case "$KDESUDO" in ?*)
  111.             eval "`quotify "$KDESUDO"` --comment `quotify "$DESCRIPTION"` -- $COMMAND"
  112.             exit
  113.             ;;
  114.         esac
  115.  
  116.         KDESU="`mywhich kdesu`"
  117.         case "$KDESU" in ?*)
  118.             "$KDESU" -c "$COMMAND"
  119.             exit
  120.             ;;
  121.         esac
  122.  
  123.         GKSU="`mywhich gksu`"
  124.         case "$GKSU" in ?*)
  125.             # Older gksu does not grok --description nor '--' and multiple args.
  126.             # @todo which versions do?
  127.             # "$GKSU" --description "$DESCRIPTION" -- "$@"
  128.             # Note that $GKSU_SWITCHES is NOT quoted in the following
  129.             "$GKSU" $GKSU_SWITCHES "$COMMAND"
  130.             exit
  131.             ;;
  132.         esac
  133.         ;;
  134.     esac # $DISPLAY
  135.     ;;
  136. esac # ! $HAS_TERMINAL
  137.  
  138. # pkexec may work for ssh console sessions as well if the right agents
  139. # are installed.  However it is very generic and does not allow for any
  140. # custom messages.  Thus it comes after gksu.
  141. ## @todo should we insist on either a display or a terminal?
  142. # case "$DISPLAY$HAS_TERMINAL" in ?*)
  143. PKEXEC="`mywhich pkexec`"
  144. case "$PKEXEC" in ?*)
  145.     eval "\"$PKEXEC\" $COMMAND"
  146.     exit
  147.     ;;
  148. esac
  149. #    ;;S
  150. #esac
  151.  
  152. case "$HAS_TERMINAL" in ?*)
  153.         USE_SUDO=
  154.         grep -q Ubuntu /etc/lsb-release 2>/dev/null && USE_SUDO=true
  155.         # On Ubuntu we need sudo instead of su.  Assume this works, and is only
  156.         # needed for Ubuntu until proven wrong.
  157.         case $USE_SUDO in true)
  158.             SUDO_COMMAND="`quotify "$SUDO"` -- $COMMAND"
  159.             eval "$SUDO_COMMAND"
  160.             exit
  161.             ;;
  162.         esac
  163.  
  164.     SU="`mywhich su`"
  165.     case "$SU" in ?*)
  166.         "$SU" - root -c "$COMMAND"
  167.         exit
  168.         ;;
  169.     esac
  170.     ;;
  171. esac
  172.  
  173. # The ultimate fallback is running 'su -' within an xterm.  We use the
  174. # title of the xterm to tell what is going on.
  175. case "$DISPLAY" in ?*)
  176.     SU="`mywhich su`"
  177.     case "$SU" in ?*)
  178.         getxterm
  179.         case "$gxtpath" in ?*)
  180.             "$gxtpath" "$gxttitle" "$DESCRIPTION - su" "$gxtexec" su - root -c "$COMMAND"
  181.             exit
  182.             ;;
  183.         esac
  184.     esac
  185. esac # $DISPLAY
  186.  
  187. # Failure...
  188. case "$DISPLAY" in ?*)
  189.     echo "Unable to locate 'pkexec', 'gksu' or 'su+xterm'. $ADVICE" >&2
  190.     ;;
  191.     *)
  192.     echo "Unable to locate 'pkexec'. $ADVICE" >&2
  193.     ;;
  194. esac
  195.  
  196. exit 1
  197.