i2p-browser 13.1 KB
Newer Older
Kill Your TV's avatar
Kill Your TV committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/sh

set -e

# This isn't very useful without I2P...
grep -qw "i2p" /proc/cmdline || exit 0

CMD=$(basename ${0})
LOCK=/var/lock/${CMD}

. gettext.sh
TEXTDOMAIN="tails"
export TEXTDOMAIN

15 16
. /usr/local/lib/tails-shell-library/i2p.sh

Kill Your TV's avatar
Kill Your TV committed
17 18 19 20
CONF_DIR=/var/lib/i2p-browser
COW=${CONF_DIR}/cow
CHROOT=${CONF_DIR}/chroot
BROWSER_USER=i2pbrowser
21
TBB_PREFS="/etc/tor-browser/profile/preferences"
Kill Your TV's avatar
Kill Your TV committed
22 23
START_PAGE="http://127.0.0.1:7657"

24 25 26
# Import the TBB_INSTALL, TBB_EXT and TBB_PROFILE variables, and
# exec_firefox(), configure_xulrunner_app_locale() and
# guess_best_tor_browser_locale()
27 28
. /usr/local/lib/tails-shell-library/tor-browser.sh

29 30
. /usr/local/lib/tails-shell-library/chroot-browser.sh

31 32
NOSCRIPT="${TBB_EXT}/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi"
TORBUTTON="${TBB_EXT}/torbutton@torproject.org"
Kill Your TV's avatar
Kill Your TV committed
33
NAME="`gettext \"I2P Browser\"`"
34

Kill Your TV's avatar
Kill Your TV committed
35 36 37 38 39
if [ -e /var/lib/gdm3/tails.camouflage ]; then
    CAMOUFLAGE=yes
fi

cleanup () {
40
    try_cleanup_browser_chroot ${CHROOT} ${COW} ${BROWSER_USER}
Kill Your TV's avatar
Kill Your TV committed
41 42
}

43 44 45 46 47 48 49 50 51 52
error () {
    local cli_text="${CMD}: `gettext \"error:\"` ${@}"
    local dialog_text="<b><big>`gettext \"Error\"`</big></b>

${@}"
    echo "${cli_text}" >&2
    sudo -u ${SUDO_USER} zenity --error --title "" --text "${dialog_text}"
    exit 1
}

Kill Your TV's avatar
Kill Your TV committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
verify_start () {
    # Make sure the user really wants to start the browser in case the router console isn't available
    local dialog_msg="<b><big>`gettext \"Do you still want to launch I2P Browser?\"`</big></b>

`gettext \"The I2P router console is not ready.\"`"
    local launch="`gettext \"_Launch\"`"
    local exit="`gettext \"_Exit\"`"
    # Since zenity can't set the default button to cancel, we switch the
    # labels and interpret the return value as its negation.
    if sudo -u ${SUDO_USER} zenity --question --title "" --ok-label "${exit}" \
       --cancel-label "${launch}" --text "${dialog_msg}"; then
        exit 0
    fi
}

show_start_notification () {
    local title="`gettext \"Starting the I2P Browser...\"`"
    local body="`gettext \"This may take a while, so please be patient.\"`"
    tails-notify-user "${title}" "${body}" 10000
}

setup_chroot () {
    # Setup a chroot on an aufs "fork" of the filesystem.
    # FIXME: When LXC matures to the point where it becomes a viable option
    # for creating isolated jails, the chroot can be used as its rootfs.
    echo "* Setting up chroot"

    trap cleanup INT
    trap cleanup EXIT

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    local rootfs_dir
    local rootfs_dirs_path=/lib/live/mount/rootfs
    local tails_module_path=/lib/live/mount/medium/live/Tails.module
    local aufs_dirs=

    # We have to pay attention to the order we stack the filesystems;
    # newest must be first, and remember that the .module file lists
    # oldest first, newest last.
    while read rootfs_dir; do
        rootfs_dir="${rootfs_dirs_path}/${rootfs_dir}"
        mountpoint -q "${rootfs_dir}" && \
        aufs_dirs="${rootfs_dir}=rr+wh:${aufs_dirs}"
    done < "${tails_module_path}"
    # But our copy-on-write dir must be at the very top.
    aufs_dirs="${COW}=rw:${aufs_dirs}"

Kill Your TV's avatar
Kill Your TV committed
99 100
    mkdir -p ${COW} ${CHROOT} && \
    mount -t tmpfs tmpfs ${COW} && \
101
    mount -t aufs -o "noatime,noxino,dirs=${aufs_dirs}" aufs ${CHROOT} && \
Kill Your TV's avatar
Kill Your TV committed
102 103 104 105 106 107 108 109 110 111
    mount -t proc proc ${CHROOT}/proc && \
    mount --bind /dev ${CHROOT}/dev || \
    error "`gettext \"Failed to setup chroot.\"`"

    # Workaround for todo/buggy_aufs_vs_unsafe-browser
    chmod -t ${COW}
}

set_chroot_browser_name () {
    NAME="${1}"
112 113 114 115
    LOCALE="${2}"
    EXT_DIR=${CHROOT}/"${TBB_EXT}"
    if [ "${LOCALE}" != en-US ]; then
        PACK="${EXT_DIR}/langpack-${LOCALE}@firefox.mozilla.org.xpi"
Kill Your TV's avatar
Kill Your TV committed
116
        TOP=browser/chrome
117
        REST=${LOCALE}/locale
Kill Your TV's avatar
Kill Your TV committed
118
    else
119
        PACK="${CHROOT}/${TBB_INSTALL}/browser/omni.ja"
120 121
        TOP=chrome
        REST=en-US/locale
Kill Your TV's avatar
Kill Your TV committed
122 123
    fi
    TMP=$(mktemp -d)
124
    BRANDING="${TOP}/${REST}/branding/brand.dtd"
Kill Your TV's avatar
Kill Your TV committed
125 126
    # Non-zero exit code due to non-standard ZIP archive.
    # The following steps will fail soon if the extraction failed anyway.
127 128 129
    7z x -o"${TMP}" "${PACK}" "${BRANDING}" || true
    sed -i "s/<"'!'"ENTITY\s\+brand\(Full\|Short\)Name.*$/<"'!'"ENTITY brand\1Name \"${NAME}\">/" "${TMP}/${BRANDING}"
    (cd $TMP ; 7z u -tzip "${PACK}" .)
Kill Your TV's avatar
Kill Your TV committed
130 131 132 133 134 135 136 137 138 139
    chmod a+r "${PACK}"
    rm -Rf "${TMP}"
}

configure_chroot () {
    echo "* Configuring chroot"

    # Prevent sudo from complaining about failing to resolve the 'amnesia' host
    echo "127.0.0.1 localhost amnesia" > ${CHROOT}/etc/hosts

Kill Your TV's avatar
Kill Your TV committed
140 141
    # Keep the NoScript and TorButton addons
    chroot ${CHROOT} dpkg -l 'xul-ext*' |grep -v 'noscript\|torbutton' \
Kill Your TV's avatar
Kill Your TV committed
142
            | awk '/^ii/{print $2}' | xargs -r chroot ${CHROOT} dpkg --remove
Kill Your TV's avatar
Kill Your TV committed
143

144 145
    # Create a fresh Tor Browser profile for the i2pbrowser user
    BROWSER_PROFILE="${CHROOT}/home/${BROWSER_USER}/.tor-browser/profile.default"
146 147
    BROWSER_EXT="${BROWSER_PROFILE}/extensions"
    mkdir -p "${BROWSER_EXT}"
Kill Your TV's avatar
Kill Your TV committed
148
    ln -s "${NOSCRIPT}" "${BROWSER_EXT}"
Kill Your TV's avatar
Kill Your TV committed
149 150 151 152
    # TorButton forces the Browser name to Tor Browser. This hack is to undo that and set it to I2P Browser
    # to try to prevent user confusion.
    TMP=$(mktemp -d)
    cp -a /usr/share/xul-ext/torbutton/ $TMP
Kill Your TV's avatar
Kill Your TV committed
153
    for LANGPACK in $(ls ${TBB_PROFILE}/extensions/langpack-*.xpi); do
Kill Your TV's avatar
Kill Your TV committed
154 155
        ln -s "${LANGPACK}" "${BROWSER_EXT}"
    done
Kill Your TV's avatar
Kill Your TV committed
156 157 158 159
    find $TMP/torbutton  -name 'brand.dtd' -print0 | \
       xargs -0 -r sed -i "s/<"'!'"ENTITY\s\+brand\(Full\|Short\)Name.*$/<"'!'"ENTITY brand\1Name \"${NAME}\">/"
    cd $TMP/torbutton && 7z a -tzip "${BROWSER_EXT}/torbutton@torproject.org.xpi" .
    rm -r $TMP
160 161 162 163 164
    BROWSER_PREF_DIR="${BROWSER_PROFILE}/preferences"
    BROWSER_PREFS="${BROWSER_PREF_DIR}/prefs.js"
    mkdir -p "${BROWSER_PREF_DIR}"

    # Selectively copy the TBB prefs we want
165 166
    sed '/\(security\|update\|download\|spell\|noscript\|torbrowser\|torbutton\)/!d' $TBB_PREFS/0000tails.js > \
                    ${BROWSER_PREF_DIR}/0000tails.js
Kill Your TV's avatar
Kill Your TV committed
167
    sed '/\(capability\|noscript\|torbutton\)/!d' ${TBB_PREFS}/extension-overrides.js > \
168 169 170
                    ${BROWSER_PREF_DIR}/extension-overrides.js

    # Localization
171 172
    BEST_LOCALE="$(guess_best_tor_browser_locale)"
    configure_xulrunner_app_locale "${BROWSER_PROFILE}" "${BEST_LOCALE}"
173

174 175 176
    # Prevent File -> Print or CTRL+P from causing the browser to hang
    # for several minutes while trying to communicate with CUPS, since
    # access to port 631 isn't allowed through.
Kill Your TV's avatar
Kill Your TV committed
177
    echo 'user_pref("print.postscript.cups.enabled", false);' >> \
178
        ${BROWSER_PREFS}
179 180

    # Set the name (e.g. window title) of the browser
181
    set_chroot_browser_name "`gettext \"I2P Browser\"`" "${BEST_LOCALE}"
182 183 184 185 186 187

    # Set start page to the router console
    echo 'user_pref("browser.startup.homepage", "'${START_PAGE}'");' >> \
        ${BROWSER_PREFS}


188 189 190
    # Disable searching from the URL bar
    echo 'user_pref("keyword.enabled", false);' >> \
        ${BROWSER_PREFS}
191 192
    # Hide "Get Addons" in Add-ons manager
    echo 'user_pref("extensions.getAddons.showPane", false);' >> \
193
        ${BROWSER_PREFS}
Kill Your TV's avatar
Kill Your TV committed
194
    # add the I2P proxy to all protocols
195
    cat > "${BROWSER_PREF_DIR}/i2p.js" << EOF
Kill Your TV's avatar
Kill Your TV committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
user_pref("extensions.torbutton.http_port", 4444);
user_pref("extensions.torbutton.http_proxy", "127.0.0.1");
user_pref("extensions.torbutton.https_port", 4444);
user_pref("extensions.torbutton.https_proxy", "127.0.0.1");
user_pref("extensions.torbutton.custom.ftp_port", 4444);
user_pref("extensions.torbutton.custom.ftp_proxy", "127.0.0.1");
user_pref("extensions.torbutton.custom.http_port", 4444);
user_pref("extensions.torbutton.custom.http_proxy", "127.0.0.1");
user_pref("extensions.torbutton.custom.https_port", 4444);
user_pref("extensions.torbutton.custom.https_proxy", "127.0.0.1");
user_pref("extensions.torbutton.ftp_port", 4444);
user_pref("extensions.torbutton.ftp_proxy", "127.0.0.1");
user_pref("extensions.torbutton.gopher_port", 4444);
user_pref("extensions.torbutton.gopher_proxy", "127.0.0.1");
user_pref("extensions.torbutton.inserted_button", true);
user_pref("extensions.torbutton.settings_method", "custom");
Kill Your TV's avatar
Kill Your TV committed
212 213
user_pref("network.proxy.ftp", "127.0.0.1");
user_pref("network.proxy.ftp_port", 4444);
Kill Your TV's avatar
Kill Your TV committed
214 215 216
user_pref("network.proxy.http", "127.0.0.1");
user_pref("network.proxy.http_port", 4444);
user_pref("network.proxy.no_proxies_on", "127.0.0.1");
Kill Your TV's avatar
Kill Your TV committed
217 218
user_pref("network.proxy.ssl", "127.0.0.1");
user_pref("network.proxy.ssl_port", 4444);
219
EOF
220
    # Hide options in the I2P Browser.
Kill Your TV's avatar
Kill Your TV committed
221
    # It would be good to implement the ability to persist the browser profile in the
222 223 224 225
    # future. At that point, the Bookmark functionality could be restored.
    BROWSER_CHROME="${BROWSER_PROFILE}/chrome/userChrome.css"
    mkdir -p "$(dirname "${BROWSER_CHROME}")"
    cat > ${BROWSER_CHROME} << EOF
226 227 228
/* Required, do not remove */
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

Kill Your TV's avatar
Kill Your TV committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/* Hide access to the bookmarks to try to prevent "data loss" due to users
 * adding bookmarks even though the profile is destroyed at browser close.
 * Keyboard shortcuts still work, but this makes it harder to 'accidentally'
 * lose bookmarks.
 *
 * Note that any of the selectors that start with 'app' apply to the menu that
 * is used if the main menu is hidden. Any that start with 'wrapper' are
 * buttons that are normally visible within the 'customize toolbar' option. The
 * others are probably self-explanatory.
 */

/* Remove the History and Bookmarks menus and buttons */
#appmenu_bookmarks,
#appmenu_history,
#bookmarks,
#bookmarks-menu-button,
#bookmarksMenu,
#history,
#history-menu,
#history-menu-button,
#wrapper-history-button,
#wrapper-bookmarks-button,

/* Hide the sidebar menu (underneath View) since the default sidebars consist
 * of history and bookmarks.  Also disable the bookmark toolbar.
 */
#toggle_PersonalToolbar,
#viewSidebarMenuMenu,

/* Remove the "Star button" and "History Dropdown arrow" from the URL bar
 * since neither history nor bookmarks are saved.
 */
#star-button,
[anonid="historydropmarker"],

/* Remove bookmark options from the context menus */
#context-bookmarkframe,
#context-bookmarklink,
#context-bookmarkpage,

269 270 271 272 273
/* Hide the option for emailing links since it's doomed to failure
 * without a configured email client.
 */
menuitem[command="Browser:SendLink"],

274 275 276 277 278 279 280 281 282
/* Hide Print options */
/*
#menu_printSetup,
#menu_printPreview,
#menu_print,
#menu_print + menuseparator,
[command="cmd_print"],
*/

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/* Hide the sync functionality which won't work with I2P */
#BrowserPreferences radio[pane="paneSync"],
#sync-button,
#sync-menu-button,
#sync-setup,
#sync-setup-appmenu,
#sync-status-button,
#sync-syncnowitem-appmenu,
#wrapper-sync-button,

/* Without I2P search engines defined, the search bar is useless.
 *  Since there are no I2P search engines added to Tails (yet),
 *  let's hide it and the Update Pane in Firefox's Preferences.
 */
#search-container,
#updateTab,

/* Hide options in the Help menu that lead to disallowed resources on the
 * Internet.
 */
#appmenu_feedbackPage,
#appmenu_gettingStarted,
#appmenu_openHelp,
#feedbackPage,
#gettingStarted,
#menu_HelpPopup_reportPhishingtoolmenu,
309 310 311 312 313 314
#menu_openHelp,

/* Hide TorBrowser Health Report and its configuration option */
#appmenu_healthReport,
#dataChoicesTab,
#healthReport
315 316 317

/* Now the actual hiding */
{display: none !important}
Kill Your TV's avatar
Kill Your TV committed
318
EOF
319
    rm -rf ${BROWSER_EXT}/branding@amnesia.boum.org
Kill Your TV's avatar
Kill Your TV committed
320

321 322 323 324 325 326 327
    # Remove all bookmarks
    rm -f "${CHROOT}/${TBB_PROFILE}/bookmarks.html"
    rm -f ${BROWSER_PROFILE}/bookmarks.html
    rm -f ${BROWSER_PROFILE}/places.sqlite

   chown -R ${BROWSER_USER}:${BROWSER_USER} "${CHROOT}/home/${BROWSER_USER}/.tor-browser"

Kill Your TV's avatar
Kill Your TV committed
328 329
    # Change the theme when not using Windows camouflage
    if [ -z "${CAMOUFLAGE}" ]; then
330
        cat >> ${BROWSER_PREFS} <<EOF
Kill Your TV's avatar
Kill Your TV committed
331 332 333 334 335 336
user_pref("lightweightThemes.isThemeSelected", true);
user_pref("lightweightThemes.usedThemes", "[{\"id\":\"1\",\"name\":\"I2P Browser\",\"headerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"footerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"textcolor\":\"#FFFFFF\",\"accentcolor\":\"#66ABEB\",\"updateDate\":0,\"installDate\":0}]");
EOF
    else
        # The camouflage activation script requires a dbus server for
        # properly configuring GNOME, so we start one in the chroot
337
        chroot ${CHROOT} sudo -H -u ${BROWSER_USER} sh -c 'eval `dbus-launch --auto-syntax`; tails-activate-win8-theme' || :
Kill Your TV's avatar
Kill Your TV committed
338 339 340 341 342 343 344 345 346
    fi

}

run_browser_in_chroot () {
    # Start Iceweasel in the chroot
    echo "* Starting I2P Browser"

    sudo -u ${SUDO_USER} xhost +SI:localuser:${BROWSER_USER} 2>/dev/null
347 348 349 350
    chroot ${CHROOT} sudo -u ${BROWSER_USER} /bin/sh -c \
        ". /usr/local/lib/tails-shell-library/tor-browser.sh && \
         exec_firefox -DISPLAY=:0.0 \
                      -profile /home/${BROWSER_USER}/.tor-browser/profile.default"
Kill Your TV's avatar
Kill Your TV committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    sudo -u ${SUDO_USER} xhost -SI:localuser:${BROWSER_USER} 2>/dev/null
}

show_shutdown_notification () {
    local title="`gettext \"Shutting down the I2P Browser...\"`"
    local body="`gettext \"This may take a while, and you may not restart the I2P Browser until it is properly shut down.\"`"
    tails-notify-user "${title}" "${body}" 10000
}

# Prevent multiple instances of the script.
exec 9>${LOCK}
if ! flock -x -n 9; then
    error "`gettext \"Another I2P Browser is currently running, or being cleaned up. Please retry in a while.\"`"
fi

366
if ! i2p_router_console_is_ready; then
Kill Your TV's avatar
Kill Your TV committed
367 368 369 370 371 372 373 374 375
    verify_start
fi
show_start_notification
setup_chroot
configure_chroot
run_browser_in_chroot
show_shutdown_notification

exit 0