Advertisement
mouhsineelachbi

Start-Tor-Browser

May 23rd, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.57 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # GNU/Linux does not really require something like RelativeLink.c
  4. # However, we do want to have the same look and feel with similar features.
  5. #
  6. # To run in debug mode simply pass --debug
  7. #
  8. # Copyright 2011 The Tor Project.  See LICENSE for licensing information.
  9.  
  10. complain_dialog_title="Tor Browser Bundle"
  11.  
  12. # First, make sure DISPLAY is set.  If it isn't, we're hosed; scream
  13. # at stderr and die.
  14. if [ "x$DISPLAY" = "x" ]; then
  15.     echo "$complain_dialog_title must be run within the X Window System." >&2
  16.     echo "Exiting." >&2
  17.     exit 1
  18. fi
  19.  
  20. # Do not (try to) connect to the session manager
  21. unset SESSION_MANAGER
  22.  
  23. # Determine whether we are running in a terminal.  If we are, we
  24. # should send our error messages to stderr...
  25. ARE_WE_RUNNING_IN_A_TERMINAL=0
  26. if [ -t 1 -o -t 2 ]; then
  27.     ARE_WE_RUNNING_IN_A_TERMINAL=1
  28. fi
  29.  
  30. # ...unless we're running in the same terminal as startx or xinit.  In
  31. # that case, the user is probably running us from a GUI file manager
  32. # in an X session started by typing startx at the console.
  33. #
  34. # Hopefully, the local ps command supports BSD-style options.  (The ps
  35. # commands usually used on Linux and FreeBSD do; do any other OSes
  36. # support running Linux binaries?)
  37. ps T 2>/dev/null |grep startx 2>/dev/null |grep -v grep 2>&1 >/dev/null
  38. not_running_in_same_terminal_as_startx="$?"
  39. ps T 2>/dev/null |grep xinit 2>/dev/null |grep -v grep 2>&1 >/dev/null
  40. not_running_in_same_terminal_as_xinit="$?"
  41.  
  42. # not_running_in_same_terminal_as_foo has the value 1 if we are *not*
  43. # running in the same terminal as foo.
  44. if [ "$not_running_in_same_terminal_as_startx" -eq 0 -o \
  45.      "$not_running_in_same_terminal_as_xinit" -eq 0 ]; then
  46.     ARE_WE_RUNNING_IN_A_TERMINAL=0
  47. fi
  48.  
  49. # Complain about an error, by any means necessary.
  50. # Usage: complain message
  51. # message must not begin with a dash.
  52. complain () {
  53.     # Trim leading newlines, to avoid breaking formatting in some dialogs.
  54.     complain_message="`echo "$1" | sed '/./,$!d'`"
  55.  
  56.     # If we're being run in a terminal, complain there.
  57.     if [ "$ARE_WE_RUNNING_IN_A_TERMINAL" -ne 0 ]; then
  58.         echo "$complain_message" >&2
  59.         return
  60.     fi
  61.  
  62.     # Otherwise, we're being run by a GUI program of some sort;
  63.     # try to pop up a message in the GUI in the nicest way
  64.     # possible.
  65.     #
  66.     # In mksh, non-existent commands return 127; I'll assume all
  67.     # other shells set the same exit code if they can't run a
  68.     # command.  (xmessage returns 1 if the user clicks the WM
  69.     # close button, so we do need to look at the exact exit code,
  70.     # not just assume the command failed to display a message if
  71.     # it returns non-zero.)
  72.  
  73.     # First, try zenity.
  74.     zenity --error \
  75.         --title="$complain_dialog_title" \
  76.         --text="$complain_message"
  77.     if [ "$?" -ne 127 ]; then
  78.         return
  79.     fi
  80.  
  81.     # Try kdialog.
  82.     kdialog --title "$complain_dialog_title" \
  83.         --error "$complain_message"
  84.     if [ "$?" -ne 127 ]; then
  85.         return
  86.     fi
  87.  
  88.     # Try xmessage.
  89.     xmessage -title "$complain_dialog_title" \
  90.         -center \
  91.         -buttons OK \
  92.         -default OK \
  93.         -xrm '*message.scrollVertical: Never' \
  94.         "$complain_message"
  95.     if [ "$?" -ne 127 ]; then
  96.         return
  97.     fi
  98.  
  99.     # Try gxmessage.  This one isn't installed by default on
  100.     # Debian with the default GNOME installation, so it seems to
  101.     # be the least likely program to have available, but it might
  102.     # be used by one of the 'lightweight' Gtk-based desktop
  103.     # environments.
  104.     gxmessage -title "$complain_dialog_title" \
  105.         -center \
  106.         -buttons GTK_STOCK_OK \
  107.         -default OK \
  108.         "$complain_message"
  109.     if [ "$?" -ne 127 ]; then
  110.         return
  111.     fi
  112. }
  113.  
  114. if [ "`id -u`" -eq 1 ]; then
  115.     complain "The Tor Browser Bundle should not be run as root.  Exiting."
  116.     exit 1
  117. fi
  118.  
  119. debug=0
  120. usage_message="usage: $0 [--debug]"
  121. # !!! We may have more than one argument, changed -eq to -ge in if & elif clauses below
  122. if [ "$#" -ge 1 -a \( "x$1" = "x--debug" -o "x$1" = "x-debug" \) ]; then
  123.     debug=1
  124.     shift # pop the debug argument
  125.     printf "\nDebug enabled.\n\n"
  126. elif [ "$#" -ge 1 -a \( "x$1" = "x--help" -o "x$1" = "x-help" \) ]; then
  127.     echo "$usage_message"
  128.     exit 0
  129. fi
  130.  
  131. # If the user hasn't requested 'debug mode', close whichever of stdout
  132. # and stderr are not ttys, to keep Firefox and the stuff loaded by/for
  133. # it (including the system's shared-library loader) from printing
  134. # messages to $HOME/.xsession-errors .  (Users wouldn't have seen
  135. # messages there anyway.)
  136. #
  137. # If the user has requested 'debug mode', don't muck with the FDs.
  138. if [ "$debug" -ne 1 ]; then
  139.   if [ '!' -t 1 ]; then
  140.     # stdout is not a tty
  141.     exec >/dev/null
  142.   fi
  143.   if [ '!' -t 2 ]; then
  144.     # stderr is not a tty
  145.     exec 2>/dev/null
  146.   fi
  147. fi
  148.  
  149. # If XAUTHORITY is unset, set it to its default value of $HOME/.Xauthority
  150. # before we change HOME below.  (See xauth(1) and #1945.)  XDM and KDM rely
  151. # on applications using this default value.
  152. if [ -z "$XAUTHORITY" ]; then
  153.     XAUTHORITY=~/.Xauthority
  154.     export XAUTHORITY
  155. fi
  156.  
  157. # If this script is being run through a symlink, we need to know where
  158. # in the filesystem the script itself is, not where the symlink is.
  159. myname="$0"
  160. if [ -L "$myname" ]; then
  161.     # XXX readlink is not POSIX, but is present in GNU coreutils
  162.     # and on FreeBSD.  Unfortunately, the -f option (which follows
  163.     # a whole chain of symlinks until it reaches a non-symlink
  164.     # path name) is a GNUism, so we have to have a fallback for
  165.     # FreeBSD.  Fortunately, FreeBSD has realpath instead;
  166.     # unfortunately, that's also non-POSIX and is not present in
  167.     # GNU coreutils.
  168.     #
  169.     # If this launcher were a C program, we could just use the
  170.     # realpath function, which *is* POSIX.  Too bad POSIX didn't
  171.     # make that function accessible to shell scripts.
  172.  
  173.     # If realpath is available, use it; it Does The Right Thing.
  174.     possibly_my_real_name="`realpath "$myname" 2>/dev/null`"
  175.     if [ "$?" -eq 0 ]; then
  176.         myname="$possibly_my_real_name"
  177.     else
  178.         # realpath is not available; hopefully readlink -f works.
  179.         myname="`readlink -f "$myname" 2>/dev/null`"
  180.         if [ "$?" -ne 0 ]; then
  181.             # Ugh.
  182.             complain "start-tor-browser cannot be run using a symlink on this operating system."
  183.         fi
  184.     fi
  185. fi
  186.  
  187. # Try to be agnostic to where we're being started from, chdir to where
  188. # the script is.
  189. mydir="`dirname "$myname"`"
  190. test -d "$mydir" && cd "$mydir"
  191.  
  192. # This is a fix for an ibus issue on some Linux systems. See #9353 for more
  193. # details. The symlink needs to be created before we change HOME.
  194. if [ ! -d ".config/ibus" ]; then
  195.   mkdir -p .config/ibus
  196.   ln -nsf ~/.config/ibus/bus .config/ibus
  197. fi
  198.  
  199. # If ${PWD} results in a zero length HOME, we can try something else...
  200. if [ ! "${PWD}" ]; then
  201.     # "hacking around some braindamage"
  202.     HOME="`pwd`"
  203.     export HOME
  204.     surveysays="This system has a messed up shell.\n"
  205. else
  206.     HOME="${PWD}"
  207.     export HOME
  208. fi
  209.  
  210. SYSARCHITECTURE=$(getconf LONG_BIT)
  211. TORARCHITECTURE=$(expr "$(file Tor/tor)" : '.*ELF \([[:digit:]]*\)')
  212.  
  213. if [ $SYSARCHITECTURE -ne $TORARCHITECTURE ]; then
  214.    complain "Wrong architecture? 32-bit vs. 64-bit."
  215.    exit 1
  216. fi
  217.  
  218. LD_LIBRARY_PATH="${HOME}/Tor/"
  219. export LD_LIBRARY_PATH
  220.  
  221. # XXX: Debug mode for Firefox??
  222.  
  223. # not in debug mode, run proceed normally
  224. printf "\nLaunching Tor Browser Bundle for Linux in ${HOME}\n"
  225. cd "${HOME}"
  226. # XXX Someday we should pass whatever command-line arguments we got
  227. # (probably filenames or URLs) to Firefox.
  228. # !!! Dash above comment! Now we pass command-line arguments we got (except --debug) to Firefox.
  229. # !!! Use at your own risk!
  230. ./Browser/firefox -no-remote -profile Data/Browser/profile.default ${@}
  231. exitcode="$?"
  232. if [ "$exitcode" -ne 0 ]; then
  233.     complain "Tor Browser exited abnormally.  Exit code: $exitcode"
  234.     exit "$exitcode"
  235. else
  236.     printf '\nTor Browser exited cleanly.\n'
  237. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement