unsafe-browser 9.21 KB
Newer Older
1
#!/bin/sh
2

3 4
set -e

Tails developers's avatar
Tails developers committed
5
CMD=$(basename ${0})
6
LOCK=/var/lock/${CMD}
Tails developers's avatar
Tails developers committed
7 8

. gettext.sh
9
TEXTDOMAIN="tails"
Tails developers's avatar
Tails developers committed
10 11
export TEXTDOMAIN

12
ROFS=/lib/live/mount/rootfs/filesystem.squashfs
13 14 15
CONF_DIR=/var/lib/unsafe-browser
COW=${CONF_DIR}/cow
CHROOT=${CONF_DIR}/chroot
16
CLEARNET_USER=clearnet
17 18 19

# Import tor_is_working()
. /usr/local/lib/tails-shell-library/tor.sh
20

21 22 23
# Import the TBB_INSTALL, TBB_EXT and TBB_PROFILE variables
. /usr/local/lib/tails-shell-library/tor-browser.sh

24 25 26 27 28 29 30 31
WARNING_PAGE='/usr/share/doc/tails/website/misc/unsafe_browser_warning'
LANG_CODE="$(echo ${LANG} | head -c 2)"
if [ -r "${WARNING_PAGE}.${LANG_CODE}.html" ]; then
   START_PAGE="${WARNING_PAGE}.${LANG_CODE}.html"
else
   START_PAGE="${WARNING_PAGE}.en.html"
fi

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

36
cleanup () {
37 38 39
    # Break down the chroot and kill all of its processes
    local counter=0
    local ret=0
40
    while [ "${counter}" -le 10 ] && \
Tails developers's avatar
Tails developers committed
41
        pgrep -u ${CLEARNET_USER} 1>/dev/null 2>&1; do
42
        pkill -u ${CLEARNET_USER} 1>/dev/null 2>&1
43
        ret=${?}
44
        sleep 1
45
        counter=$((${counter}+1))
46
    done
47
    [ ${ret} -eq 0 ] || pkill -9 -u ${CLEARNET_USER} 1>/dev/null 2>&1
48
    for mnt in ${CHROOT}/dev ${CHROOT}/proc ${CHROOT} ${COW}; do
49
        counter=0
50 51
        while [ "${counter}" -le 10 ] && mountpoint -q ${mnt} 2>/dev/null; do
            umount ${mnt} 2>/dev/null
52
            sleep 1
53
            counter=$((${counter}+1))
54 55
        done
    done
56
    rmdir ${COW} ${CHROOT} 2>/dev/null
57 58 59
}

error () {
60 61
    local cli_text="${CMD}: `gettext \"error:\"` ${@}"
    local dialog_text="<b><big>`gettext \"Error\"`</big></b>
62

63
${@}"
64
    echo "${cli_text}" >&2
65
    sudo -u ${SUDO_USER} zenity --error --title "" --text "${dialog_text}"
66 67 68
    exit 1
}

69 70
verify_start () {
    # Make sure the user really wants to start the browser
71
    local dialog_msg="<b><big>`gettext \"Do you really want to launch the Unsafe Browser?\"`</big></b>
72

Tails developers's avatar
Tails developers committed
73
`gettext \"Network activity within the Unsafe Browser is <b>not anonymous</b>. Only use the Unsafe Browser if necessary, for example if you have to login or register to activate your Internet connection.\"`"
74 75 76 77 78 79
    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
80 81 82
        exit 0
    fi
}
83

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

90 91 92 93
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.
94 95
    echo "* Setting up chroot"

96 97 98
    trap cleanup INT
    trap cleanup EXIT

99
    mkdir -p ${COW} ${CHROOT} && \
100 101 102
    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 && \
Tails developers's avatar
Tails developers committed
103 104
    mount --bind /dev ${CHROOT}/dev || \
    error "`gettext \"Failed to setup chroot.\"`"
105 106 107

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

110 111
set_chroot_browser_name () {
    NAME="${1}"
Tails developers's avatar
Tails developers committed
112
    LONG=$(echo ${LANG} | grep -o "^[a-zA-Z_]*")
113
    SHORT=${LONG%%_*}
114
    EXT_DIR=${CHROOT}/"${TBB_EXT}"
115
    BRANDING=branding/brand.dtd
116 117
    if [ -e "${EXT_DIR}/langpack-${LONG}@firefox.mozilla.org.xpi" ]; then
        PACK="${EXT_DIR}/langpack-${LONG}@firefox.mozilla.org.xpi"
118
        TOP=browser/chrome
119
        REST=${LONG}/locale
120 121
    elif [ -e "${EXT_DIR}/langpack-${SHORT}@firefox.mozilla.org.xpi" ]; then
        PACK="${EXT_DIR}/langpack-${SHORT}@firefox.mozilla.org.xpi"
122
        TOP=browser/chrome
123
        REST=${SHORT}/locale
124
    else
125
        PACK="${CHROOT}/"${TBB_EXT}"/Browser/browser/omni.ja"
126 127
        TOP=chrome
        REST=en-US/locale
128 129
    fi
    TMP=$(mktemp -d)
130 131 132
    # 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
133
    sed -i "s/<"'!'"ENTITY\s\+brand\(Full\|Short\)Name.*$/<"'!'"ENTITY brand\1Name \"${NAME}\">/" "${TMP}/${TOP}/${REST}/${BRANDING}"
134 135
    rm "${PACK}"
    (cd $TMP ; 7z a -tzip "${PACK}" .)
136 137 138 139
    chmod a+r "${PACK}"
    rm -Rf "${TMP}"
}

140
configure_chroot () {
141 142
    echo "* Configuring chroot"

143 144
    # Set the chroot's DNS servers to those obtained through DHCP
    rm -f ${CHROOT}/etc/resolv.conf
145
    for NS in ${IP4_NAMESERVERS}; do
146 147 148 149
        echo "nameserver ${NS}" >> ${CHROOT}/etc/resolv.conf
    done
    chmod a+r ${CHROOT}/etc/resolv.conf

150 151
    # Remove all Iceweasel addons: some adds proxying, which we don't
    # want; some may change the fingerprint compared to a standard
152 153 154 155
    # Iceweasel install. Note: We cannot use apt-get since we don't ship its
    # lists (#6531). Too bad, APT supports globbing, while dkpg does not.
    dpkg -l 'xul-ext-*' | /bin/grep '^ii' | awk '{print $2}' | \
        xargs chroot ${CHROOT} dpkg --remove
156

157
    # Create a fresh Iceweasel profile for the clearnet user
158 159 160 161
    CLEARNET_PROFILE="${CHROOT}"/home/clearnet/.tor-browser/profile.default

    CLEARNET_EXT="${CLEARNET_PROFILE}"/extensions
    mkdir -p "${CLEARNET_EXT}"
162 163
    cp -Pr "${TBB_PROFILE}"/extensions/langpack-*.xpi \
           "${TBB_PROFILE}"/extensions/branding@amnesia.boum.org \
164 165 166 167 168 169 170
           "${CLEARNET_EXT}"

    CLEARNET_PREFS="${CLEARNET_PROFILE}"/preferences/prefs.js
    mkdir -p "$(dirname "${CLEARNET_PREFS}")"

    # Localization
    cat /var/lib/tails-user-session/browser-locale.js >> "${CLEARNET_PREFS}"
171

172
    # Disable proxying in the chroot
173 174
    echo 'pref("network.proxy.type", 0);' >> "${CLEARNET_PREFS}"
    echo 'pref("network.proxy.socks_remote_dns", false);' >>  "${CLEARNET_PREFS}"
175

176 177 178
    # Set the name (e.g. window title) of the browser
    set_chroot_browser_name "`gettext \"Unsafe Browser\"`"

179 180
    # Set start page to something that explains what's going on
    echo 'user_pref("browser.startup.homepage", "'${START_PAGE}'");' >> \
181
        "${CLEARNET_PREFS}"
182 183

    # Remove all bookmarks
184
    rm -f ${CHROOT}/"${TBB_PROFILE}"/bookmarks.html
185 186
    rm -f ${CLEARNET_PROFILE}/bookmarks.html
    rm -f ${CLEARNET_PROFILE}/places.sqlite
187 188

    chown -R clearnet:clearnet ${CHROOT}/home/clearnet/.tor-browser
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    # Set a scary theme (except if we're using Windows
    # camouflage). Note that the tails-activate-win8-theme script that
    # we may run below requires that the browser profile is writable
    # by the user running the script (i.e. clearnet).
    if [ -z "${CAMOUFLAGE}" ]; then
        cat >> "${CLEARNET_PREFS}" <<EOF
pref("lightweightThemes.isThemeSelected", true);
pref("lightweightThemes.usedThemes", "[{\"id\":\"1\",\"name\":\"Unsafe Browser\",\"headerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"footerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"textcolor\":\"#FFFFFF\",\"accentcolor\":\"#CC0000\",\"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 clearnet sh -c 'eval `dbus-launch --auto-syntax`; tails-activate-win8-theme' || :
    fi

205 206
}

207
run_browser_in_chroot () {
208
    # Start Iceweasel in the chroot
209 210
    echo "* Starting Unsafe Browser"

211
    sudo -u ${SUDO_USER} xhost +SI:localuser:${CLEARNET_USER} 2>/dev/null
212
    chroot ${CHROOT} sudo -u ${CLEARNET_USER} "${TBB_INSTALL}"/Browser/firefox -DISPLAY=:0.0 -profile /home/clearnet/.tor-browser/profile.default
213
    sudo -u ${SUDO_USER} xhost -SI:localuser:${CLEARNET_USER} 2>/dev/null
214 215
}

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

222 223 224 225 226
maybe_restart_tor () {
    # Restart Tor if it's not working (a captive portal may have prevented
    # Tor from bootstrapping, and a restart is the fastest way to get
    # wheels turning)
    if ! tor_is_working; then
227
        echo "* Restarting Tor"
228 229
        restart-tor
        if ! service tor status >/dev/null; then
230 231
            error "`gettext \"Failed to restart Tor.\"`"
        fi
232 233
    fi
}
234

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

241
# Get the DNS servers that was obtained from NetworkManager, if any...
242 243 244 245 246 247 248 249
NM_ENV=/var/lib/NetworkManager/env
if [ -r "${NM_ENV}" ]; then
    . ${NM_ENV}
fi
# ... otherwise fail.
# FIXME: Or would it make sense to fallback to Google's DNS or OpenDNS?
# Some stupid captive portals may allow DNS to any host, but chances are
# that only the portal's DNS would forward to the login page.
250
if [ -z "${IP4_NAMESERVERS}" ]; then
Tails developers's avatar
Tails developers committed
251
    error "`gettext \"No DNS server was obtained through DHCP or manually configured in NetworkManager.\"`"
252 253
fi

254
verify_start
255
show_start_notification
256 257
setup_chroot
configure_chroot
258 259
run_browser_in_chroot
show_shutdown_notification
260
maybe_restart_tor
261 262

exit 0