i2p-browser 10.6 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#!/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

ROFS=/lib/live/mount/rootfs/filesystem.squashfs
CONF_DIR=/var/lib/i2p-browser
COW=${CONF_DIR}/cow
CHROOT=${CONF_DIR}/chroot
BROWSER_USER=i2pbrowser

START_PAGE="http://127.0.0.1:7657"

if [ -e /var/lib/gdm3/tails.camouflage ]; then
    CAMOUFLAGE=yes
fi

cleanup () {
    # Break down the chroot and kill all of its processes
    local counter=0
    local ret=0
    while [ "${counter}" -le 10 ] && \
        pgrep -u ${BROWSER_USER} 1>/dev/null 2>&1; do
        pkill -u ${BROWSER_USER} 1>/dev/null 2>&1
        ret=${?}
        sleep 1
        counter=$((${counter}+1))
    done
    [ ${ret} -eq 0 ] || pkill -9 -u ${BROWSER_USER} 1>/dev/null 2>&1
    for mnt in ${CHROOT}/dev ${CHROOT}/proc ${CHROOT} ${COW}; do
        counter=0
        while [ "${counter}" -le 10 ] && mountpoint -q ${mnt} 2>/dev/null; do
            umount ${mnt} 2>/dev/null
            sleep 1
            counter=$((${counter}+1))
        done
    done
    rmdir ${COW} ${CHROOT} 2>/dev/null
}

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
}

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
}

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

    mkdir -p ${COW} ${CHROOT} && \
    mount -t tmpfs tmpfs ${COW} && \
    mount -t aufs -o noatime,noxino,dirs=${COW}=rw:${ROFS}=rr+wh aufs ${CHROOT} && \
    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}"
    LONG=$(echo ${LANG} | grep -o "^[a-zA-Z_]*")
    SHORT=${LONG%%_*}
    EXT_DIR=${CHROOT}/usr/lib/iceweasel/browser/extensions
    BRANDING=branding/brand.dtd
    if [ -e "${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi" ]; then
        PACK="${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi"
        TOP=browser/chrome
        REST=${LONG}/locale
    elif [ -e "${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi" ]; then
        PACK="${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi"
        TOP=browser/chrome
        REST=${SHORT}/locale
    else
        PACK=${CHROOT}/usr/share/iceweasel/browser/chrome/en-US.jar
        TOP=locale
        REST=
    fi

    TMP=$(mktemp -d)
    # Non-zero exit code due to non-standard ZIP archive.
    # The following steps will fail soon if the extraction failed anyway.
    unzip -d "${TMP}" "${PACK}" || true
    sed -i "s/Iceweasel/${NAME}/" "${TMP}"/"${TOP}"/"${REST}"/"${BRANDING}"
    rm "${PACK}"
    (cd $TMP ; 7z a -tzip "${PACK}" .)
    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

    # Remove Torbutton and Foxyproxy Iceweasel addons
    chroot ${CHROOT} dpkg -l 'xul-ext*' |grep -v 'noscript\|adblock' \
            | awk '/^ii/{print $2}' | xargs chroot ${CHROOT} dpkg --remove

    # Create a fresh Iceweasel profile for the i2pbrowser user
    cp -a ${CHROOT}/etc/skel/.mozilla/ ${CHROOT}/home/i2pbrowser/
    chown -R i2pbrowser:i2pbrowser ${CHROOT}/home/i2pbrowser/.mozilla/
    BROWSER_PROFILE=${CHROOT}/home/i2pbrowser/.mozilla/firefox/default

    # Remove any existing FoxyProxy or Torbutton configs
    sed -i '/extensions\.\(foxyproxy\|torbutton\)/d' \
        ${BROWSER_PROFILE}/*.js
    # Remove any existing proxies in the chroot
    sed -r -i '/^(user_|)pref\("network\.proxy\..*",/d' \
        ${BROWSER_PROFILE}/*.js
    # 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.
    echo 'user_pref("print.postscript.cups.enabled", false);' >> \
        ${BROWSER_PROFILE}/user.js
157 158 159
    # Hide "Get Addons" in Add-ons manager
    echo 'user_pref("extensions.getAddons.showPane", false);' >> \
        ${BROWSER_PROFILE}/user.js
Kill Your TV's avatar
Kill Your TV committed
160 161 162 163 164 165 166 167 168 169
    # add the I2P proxy to all protocols
    cat >> ${BROWSER_PROFILE}/user.js << EOF
user_pref("network.proxy.http", "127.0.0.1");
user_pref("network.proxy.http_port", 4444);
user_pref("network.proxy.ftp", "127.0.0.1");
user_pref("network.proxy.ftp_port", 4444);
user_pref("network.proxy.ssl", "127.0.0.1");
user_pref("network.proxy.ssl_port", 4444);
user_pref("network.proxy.share_proxy_settings", true);
user_pref("network.proxy.no_proxies_on", "127.0.0.1");
170 171
EOF
    # Hide options from the <strike>Firefox</strike> I2P Browser.
Kill Your TV's avatar
Kill Your TV committed
172 173
    # It would be good to implement the ability to persist the browser profile in the
    # future. At that point, the Bookmark functionality will be restored.
174 175 176 177
    cat > ${BROWSER_PROFILE}/chrome/userChrome.css << EOF
/* 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* 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,

218 219 220 221 222
/* Hide the option for emailing links since it's doomed to failure
 * without a configured email client.
 */
menuitem[command="Browser:SendLink"],

223 224 225 226 227 228 229 230 231
/* Hide Print options */
/*
#menu_printSetup,
#menu_printPreview,
#menu_print,
#menu_print + menuseparator,
[command="cmd_print"],
*/

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
/* 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,
#menu_openHelp

/* Now the actual hiding */
{display: none !important}
Kill Your TV's avatar
Kill Your TV committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 309 310 311 312 313 314 315 316 317 318 319 320
EOF
    rm -rf ${BROWSER_PROFILE}/extensions

    # Change the theme when not using Windows camouflage
    if [ -z "${CAMOUFLAGE}" ]; then
        cat >> ${BROWSER_PROFILE}/user.js <<EOF
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
        chroot ${CHROOT} sudo -H -u i2pbrowser sh -c 'eval `dbus-launch --auto-syntax`; tails-activate-win8-theme' || :
    fi

    # Set the name (e.g. window title) of the browser
    set_chroot_browser_name "`gettext \"I2P Browser\"`"

    # Set start page to something that explains what's going on
    echo 'user_pref("browser.startup.homepage", "'${START_PAGE}'");' >> \
        ${BROWSER_PROFILE}/user.js

    ## Remove all bookmarks
    rm -f ${CHROOT}/etc/iceweasel/profile/bookmarks.html
    rm -f ${BROWSER_PROFILE}/bookmarks.html
    rm -f ${BROWSER_PROFILE}/places.sqlite
}

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
    chroot ${CHROOT} sudo -u ${BROWSER_USER} /usr/bin/iceweasel -DISPLAY=:0.0
    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

if ! netstat -4nlp |grep -wq "127\.0\.0\.1:7657"; then
    verify_start
fi
show_start_notification
setup_chroot
configure_chroot
run_browser_in_chroot
show_shutdown_notification

exit 0