start-tor-browser 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #!/usr/bin/env bash
  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. # Copyright 2017 The Tor Project. See LICENSE for licensing information.
  7. complain_dialog_title="Tor Browser"
  8. # Make sure this script wasn't started as 'sh start-tor-browser' or similar.
  9. if [ "x$BASH" = "x" ]; then
  10. echo "$complain_dialog_title should be started as './start-tor-browser'"
  11. echo "Exiting." >&2
  12. exit 1;
  13. fi
  14. # Do not (try to) connect to the session manager
  15. unset SESSION_MANAGER
  16. # Complain about an error, by any means necessary.
  17. # Usage: complain message
  18. # message must not begin with a dash.
  19. complain () {
  20. # Trim leading newlines, to avoid breaking formatting in some dialogs.
  21. complain_message="`echo "$1" | sed '/./,$!d'`"
  22. # If we're being run in debug/verbose mode, complain to stderr.
  23. if [ "$show_output" -eq 1 ]; then
  24. echo "$complain_message" >&2
  25. return
  26. fi
  27. # Otherwise, we're being run by a GUI program of some sort;
  28. # try to pop up a message in the GUI in the nicest way
  29. # possible.
  30. #
  31. # In mksh, non-existent commands return 127; I'll assume all
  32. # other shells set the same exit code if they can't run a
  33. # command. (xmessage returns 1 if the user clicks the WM
  34. # close button, so we do need to look at the exact exit code,
  35. # not just assume the command failed to display a message if
  36. # it returns non-zero.)
  37. # First, try zenity.
  38. zenity --error \
  39. --title="$complain_dialog_title" \
  40. --text="$complain_message"
  41. if [ "$?" -ne 127 ]; then
  42. return
  43. fi
  44. # Try kdialog.
  45. kdialog --title "$complain_dialog_title" \
  46. --error "$complain_message"
  47. if [ "$?" -ne 127 ]; then
  48. return
  49. fi
  50. # Try xmessage.
  51. xmessage -title "$complain_dialog_title" \
  52. -center \
  53. -buttons OK \
  54. -default OK \
  55. -xrm '*message.scrollVertical: Never' \
  56. "$complain_message"
  57. if [ "$?" -ne 127 ]; then
  58. return
  59. fi
  60. # Try gxmessage. This one isn't installed by default on
  61. # Debian with the default GNOME installation, so it seems to
  62. # be the least likely program to have available, but it might
  63. # be used by one of the 'lightweight' Gtk-based desktop
  64. # environments.
  65. gxmessage -title "$complain_dialog_title" \
  66. -center \
  67. -buttons GTK_STOCK_OK \
  68. -default OK \
  69. "$complain_message"
  70. if [ "$?" -ne 127 ]; then
  71. return
  72. fi
  73. }
  74. if [ "`id -u`" -eq 0 ]; then
  75. complain "The Tor Browser Bundle should not be run as root. Exiting."
  76. exit 1
  77. fi
  78. if ! grep -q 'ARM' /proc/cpuinfo && ! grep -q 'POWER' /proc/cpuinfo; then
  79. # Assume we're on an x86 machine, so check for SSE2.
  80. if test -r /proc/cpuinfo && ! grep -q '^flags\s*:.* sse2' /proc/cpuinfo; then
  81. complain "Tor Browser requires a CPU with SSE2 support. Exiting."
  82. exit 1
  83. fi
  84. fi
  85. tbb_usage () {
  86. printf "\nTor Browser Script Options\n"
  87. printf " --verbose Display Tor and Firefox output in the terminal\n"
  88. printf " --log [file] Record Tor and Firefox output in file (default: tor-browser.log)\n"
  89. printf " --detach Detach from terminal and run Tor Browser in the background.\n"
  90. printf " --register-app Register Tor Browser as a desktop app for this user\n"
  91. printf " --unregister-app Unregister Tor Browser as a desktop app for this user\n"
  92. }
  93. log_output=0
  94. show_output=0
  95. detach=0
  96. show_usage=0
  97. register_desktop_app=0
  98. logfile=/dev/null
  99. while :
  100. do
  101. case "$1" in
  102. --detach)
  103. detach=1
  104. shift
  105. ;;
  106. -v | --verbose | -d | --debug)
  107. show_output=1
  108. verbose_arg="$2"
  109. shift
  110. ;;
  111. -h | "-?" | --help | -help)
  112. show_usage=1
  113. show_output=1
  114. shift
  115. ;;
  116. -l | --log)
  117. if [ -z "$2" -o "${2:0:1}" == "-" ]; then
  118. printf "Logging Tor Browser debug information to tor-browser.log\n"
  119. logfile="../tor-browser.log"
  120. elif [ "${2:0:1}" == "/" -o "${2:0:1}" == "~" ]; then
  121. printf "Logging Tor Browser debug information to %s\n" "$2"
  122. logfile="$2"
  123. shift
  124. else
  125. printf "Logging Tor Browser debug information to %s\n" "$2"
  126. logfile="../$2"
  127. shift
  128. fi
  129. log_output=1
  130. shift
  131. ;;
  132. --register-app)
  133. register_desktop_app=1
  134. show_output=1
  135. shift
  136. ;;
  137. --unregister-app)
  138. register_desktop_app=-1
  139. show_output=1
  140. shift
  141. ;;
  142. *) # No more options
  143. break
  144. ;;
  145. esac
  146. done
  147. # We can't detach and show output at the same time..
  148. if [ "$show_output" -eq 1 -a "$detach" -eq 1 ]; then
  149. detach=0
  150. fi
  151. if [ "$show_output" -eq 0 ]; then
  152. # If the user hasn't requested 'debug mode' or --help, close stdout and stderr,
  153. # to keep Firefox and the stuff loaded by/for it (including the
  154. # system's shared-library loader) from printing messages to
  155. # $HOME/.xsession-errors or other files. (Users wouldn't have seen
  156. # messages there anyway.)
  157. exec > "$logfile"
  158. exec 2> "$logfile"
  159. fi
  160. # If XAUTHORITY is unset, set it to its default value of $HOME/.Xauthority
  161. # before we change HOME below. (See xauth(1) and #1945.) XDM and KDM rely
  162. # on applications using this default value.
  163. if [ -z "$XAUTHORITY" ]; then
  164. XAUTHORITY=~/.Xauthority
  165. export XAUTHORITY
  166. fi
  167. # If this script is being run through a symlink, we need to know where
  168. # in the filesystem the script itself is, not where the symlink is.
  169. myname="$0"
  170. if [ -L "$myname" ]; then
  171. # XXX readlink is not POSIX, but is present in GNU coreutils
  172. # and on FreeBSD. Unfortunately, the -f option (which follows
  173. # a whole chain of symlinks until it reaches a non-symlink
  174. # path name) is a GNUism, so we have to have a fallback for
  175. # FreeBSD. Fortunately, FreeBSD has realpath instead;
  176. # unfortunately, that's also non-POSIX and is not present in
  177. # GNU coreutils.
  178. #
  179. # If this launcher were a C program, we could just use the
  180. # realpath function, which *is* POSIX. Too bad POSIX didn't
  181. # make that function accessible to shell scripts.
  182. # If realpath is available, use it; it Does The Right Thing.
  183. possibly_my_real_name="`realpath "$myname" 2>/dev/null`"
  184. if [ "$?" -eq 0 ]; then
  185. myname="$possibly_my_real_name"
  186. else
  187. # realpath is not available; hopefully readlink -f works.
  188. myname="`readlink -f "$myname" 2>/dev/null`"
  189. if [ "$?" -ne 0 ]; then
  190. # Ugh.
  191. complain "start-tor-browser cannot be run using a symlink on this operating system."
  192. fi
  193. fi
  194. fi
  195. # Try to be agnostic to where we're being started from, chdir to where
  196. # the script is.
  197. mydir="`dirname "$myname"`"
  198. test -d "$mydir" && cd "$mydir"
  199. # If ${PWD} results in a zero length string, we can try something else...
  200. if [ ! "${PWD}" ]; then
  201. # "hacking around some braindamage"
  202. PWD="`pwd`"
  203. surveysays="This system has a messed up shell.\n"
  204. fi
  205. # This is a fix for an ibus issue on some Linux systems. See #9353 for more
  206. # details. The symlink needs to be created before we change HOME.
  207. if [ ! -d ".config/ibus" ]; then
  208. mkdir -p .config/ibus
  209. ln -nsf ~/.config/ibus/bus .config/ibus
  210. fi
  211. # Fix up .desktop Icon and Exec Paths, and update the .desktop file from the
  212. # canonical version if it was changed by the updater.
  213. cp start-tor-browser.desktop ../
  214. sed -i -e "s,^Name=.*,Name=Tor Browser,g" ../start-tor-browser.desktop
  215. sed -i -e "s,^Icon=.*,Icon=$PWD/browser/chrome/icons/default/default128.png,g" ../start-tor-browser.desktop
  216. sed -i -e "s,^Exec=.*,Exec=sh -c '\"$PWD/start-tor-browser\" --detach || ([ ! -x \"$PWD/start-tor-browser\" ] \&\& \"\$(dirname \"\$*\")\"/Browser/start-tor-browser --detach)' dummy %k,g" ../start-tor-browser.desktop
  217. if [ "$register_desktop_app" -eq 1 ]; then
  218. mkdir -p "$HOME/.local/share/applications/"
  219. cp ../start-tor-browser.desktop "$HOME/.local/share/applications/"
  220. update-desktop-database "$HOME/.local/share/applications/"
  221. printf "Tor Browser has been registered as a desktop app for this user in ~/.local/share/applications/\n"
  222. exit 0
  223. fi
  224. if [ "$register_desktop_app" -eq -1 ]; then
  225. if [ -e "$HOME/.local/share/applications/start-tor-browser.desktop" ]; then
  226. rm -f "$HOME/.local/share/applications/start-tor-browser.desktop"
  227. update-desktop-database "$HOME/.local/share/applications/"
  228. printf "Tor Browser has been removed as a user desktop app (from ~/.local/share/applications/)\n"
  229. else
  230. printf "Tor Browser does not appear to be a desktop app (not present in ~/.local/share/applications/)\n"
  231. fi
  232. exit 0
  233. fi
  234. HOME="${PWD}"
  235. export HOME
  236. SYSARCHITECTURE=$(getconf LONG_BIT)
  237. TORARCHITECTURE=$(expr "$(file TorBrowser/Tor/tor)" : '.*ELF \([[:digit:]]*\)')
  238. if [ $SYSARCHITECTURE -ne $TORARCHITECTURE ]; then
  239. complain "Wrong architecture? 32-bit vs. 64-bit."
  240. exit 1
  241. fi
  242. [% IF c("var/asan") -%]
  243. # We need to disable LSan which is enabled by default now. Otherwise we'll get
  244. # a crash during shutdown: https://bugs.torproject.org/10599#comment:59
  245. ASAN_OPTIONS="detect_leaks=0"
  246. export ASAN_OPTIONS
  247. [% END -%]
  248. function setControlPortPasswd() {
  249. local ctrlPasswd=$1
  250. if test -z "$ctrlPasswd" -o "$ctrlPasswd" = $'\"secret\"' ; then
  251. unset TOR_CONTROL_PASSWD
  252. return
  253. fi
  254. if test "${ctrlPasswd:0:1}" = $'\"'; then # First 2 chars were '"
  255. printf "Using system Tor process.\n"
  256. export TOR_CONTROL_PASSWD
  257. else
  258. complain "There seems to have been a quoting problem with your \
  259. TOR_CONTROL_PASSWD environment variable."
  260. echo "The Tor ControlPort password should be given inside double"
  261. echo "quotes, inside single quotes. That is, if the ControlPort"
  262. echo 'password is “secret” (without curly quotes) then we must'
  263. echo "start this script after setting the environment variable"
  264. echo "exactly like this:"
  265. echo
  266. echo " \$ TOR_CONTROL_PASSWD='\"secret\"' $myname"
  267. fi
  268. }
  269. # Using a system-installed Tor process with Tor Browser:
  270. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  271. # The Tor ControlPort password should be given inside double quotes, inside
  272. # single quotes, i.e. if the ControlPort password is “secret” (without
  273. # curly quotes) then we must set the environment variable *exactly* like
  274. # this:
  275. #
  276. # TOR_CONTROL_PASSWD='"secret"'
  277. #
  278. # Yes, the variable MUST be double-quoted, then single-quoted, exactly as
  279. # shown. This is used by TorButton and Tor Launcher to authenticate to Tor's
  280. # ControlPort, and is necessary for using TB with a system-installed Tor.
  281. #
  282. # Additionally, if using a system-installed Tor, the following about:config
  283. # options should be set (values in <> mean they are the value taken from your
  284. # torrc):
  285. #
  286. # SETTING NAME VALUE
  287. # network.security.ports.banned [...],<SocksPort>,<ControlPort>
  288. # network.proxy.socks 127.0.0.1
  289. # network.proxy.socks_port <SocksPort>
  290. # extensions.torbutton.inserted_button true
  291. # extensions.torbutton.launch_warning false
  292. # extensions.torbutton.loglevel 2
  293. # extensions.torbutton.logmethod 0
  294. # extensions.torlauncher.control_port <ControlPort>
  295. # extensions.torlauncher.loglevel 2
  296. # extensions.torlauncher.logmethod 0
  297. # extensions.torlauncher.prompt_at_startup false
  298. # extensions.torlauncher.start_tor false
  299. #
  300. # where the '[...]' in the banned_ports option means "leave anything that was
  301. # already in the preference alone, just append the things specified after it".
  302. # Either set `TOR_CONTROL_PASSWD` before running ./start-tor-browser, or put
  303. # your password in the following line where the word “secret” is:
  304. setControlPortPasswd ${TOR_CONTROL_PASSWD:='"secret"'}
  305. # Set up custom bundled fonts. See fonts-conf(5).
  306. export FONTCONFIG_PATH="${HOME}/TorBrowser/Data/fontconfig"
  307. export FONTCONFIG_FILE="fonts.conf"
  308. # Avoid overwriting user's dconf values. Fixes #27903.
  309. export GSETTINGS_BACKEND=memory
  310. # ARM/POWER requires the directory containing libssp.so.0 to be in LD_LIBRARY_PATH
  311. if grep -q 'ARM' /proc/cpuinfo || grep -q 'POWER' /proc/cpuinfo; then
  312. export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${HOME}"
  313. fi
  314. cd "${HOME}"
  315. # We pass all additional command-line arguments we get to Firefox.
  316. #
  317. # The --class parameter was added to fix bug 11102.
  318. if [ "$show_usage" -eq 1 ]; then
  319. # Display Firefox help, then our help
  320. TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
  321. -profile TorBrowser/Data/Browser/profile.default --help 2>/dev/null
  322. tbb_usage
  323. elif [ "$detach" -eq 1 ] ; then
  324. TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
  325. -profile TorBrowser/Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null &
  326. disown "$!"
  327. elif [ "$log_output" -eq 1 -a "$show_output" -eq 1 ]; then
  328. TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
  329. -profile TorBrowser/Data/Browser/profile.default "${@}" 2>&1 </dev/null | \
  330. tee "$logfile"
  331. elif [ "$show_output" -eq 1 ]; then
  332. TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
  333. -profile TorBrowser/Data/Browser/profile.default "${@}" < /dev/null
  334. else
  335. TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
  336. -profile TorBrowser/Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null
  337. fi
  338. exit $?