unsafe-browser 9.18 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
OFFENDING_ADDONS="xul-ext-foxyproxy-standard xul-ext-torbutton xul-ext-firegpg
xul-ext-cookie-monster xul-ext-noscript xul-ext-monkeysphere"
19
20
21

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

23
24
25
26
27
28
29
30
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

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

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

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

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

warning () {
69
70
71
72
73
74
    local cli_text="${CMD}: `gettext \"warning:\"` ${text}"
    local dialog_text="<b><big>`gettext \"Warning\"`</big></b>

${@}"
    echo "${cli_text}" >&2
    sudo -u ${SUDO_USER} zenity --warning --title "" --text "${dialob_text}"
75
76
}

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

Tails developers's avatar
Tails developers committed
81
`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.\"`"
82
83
84
85
86
87
    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
88
89
90
        exit 0
    fi
}
91

92
93
94
95
96
97
show_start_notification () {
    local title="`gettext \"Starting the Unsafe Browser...\"`"
    local body="`gettext \"This may take a while, so please be patient.\"`"
    notify-send -t 10000 "${title}" "${body}"
}

98
99
100
101
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.
102
103
    echo "* Setting up chroot"

104
105
106
    trap cleanup INT
    trap cleanup EXIT

107
    mkdir -p ${COW} ${CHROOT} && \
108
109
110
    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
111
112
    mount --bind /dev ${CHROOT}/dev || \
    error "`gettext \"Failed to setup chroot.\"`"
113
114
115

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

118
119
set_chroot_browser_name () {
    NAME="${1}"
Tails developers's avatar
Tails developers committed
120
    LONG=$(echo ${LANG} | grep -o "^[a-zA-Z_]*")
121
    SHORT=${LONG%%_*}
122
    EXT_DIR=${CHROOT}/usr/lib/iceweasel/browser/extensions
123
    BRANDING=branding/brand.dtd
124
    if [ -e "${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi" ]; then
125
126
127
        PACK="${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi"
        TOP=chrome
        REST=${LONG}/locale
128
129
130
131
    elif [ -e "${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi" ]; then
        PACK="${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi"
        TOP=chrome
        REST=${SHORT}/locale
132
    else
133
        PACK=${CHROOT}/usr/share/iceweasel/browser/chrome/en-US.jar
134
135
136
137
138
139
140
141
142
143
144
145
        TOP=locale
        REST=
    fi

    TMP=$(mktemp -d)
    7z x "${PACK}" -o"${TMP}"
    sed -i "s/Iceweasel/${NAME}/" "${TMP}"/"${TOP}"/"${REST}"/"${BRANDING}"
    7z u -tzip "${PACK}" "${TMP}"/"${TOP}"
    chmod a+r "${PACK}"
    rm -Rf "${TMP}"
}

146
configure_chroot () {
147
148
    echo "* Configuring chroot"

149
150
    # Set the chroot's DNS servers to those obtained through DHCP
    rm -f ${CHROOT}/etc/resolv.conf
151
    for NS in ${IP4_NAMESERVERS}; do
152
153
154
155
        echo "nameserver ${NS}" >> ${CHROOT}/etc/resolv.conf
    done
    chmod a+r ${CHROOT}/etc/resolv.conf

156
157
158
159
160
161
    # Remove all Iceweasel addons: some adds proxying, which we don't
    # want; some may change the fingerprint compared to a standard
    # Iceweasel install.
    chroot ${CHROOT} apt-get remove --yes xul-ext-*

    # Disable proxying in the chroot
162
    sed -i '/^user_pref("network\.proxy\..*",/d' \
163
164
165
        ${CHROOT}/etc/iceweasel/profile/user.js
    echo 'user_pref("network.proxy.type", 0);' >> \
        ${CHROOT}/etc/iceweasel/profile/user.js
166
    rm -rf ${CHROOT}/etc/iceweasel/profile/extensions
167

Tails developers's avatar
Tails developers committed
168
    # Set a scary theme (except if we're using Windows camouflage)
169
    if [ -z "${CAMOUFLAGE}" ]; then
Tails developers's avatar
Bugfix.  
Tails developers committed
170
        cat >> ${CHROOT}/etc/iceweasel/profile/user.js <<EOF
171
user_pref("lightweightThemes.isThemeSelected", true);
172
user_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}]");
173
EOF
174
175
176
177
    else
        echo "LIVE_USERNAME=${CLEARNET_USER}" > \
            ${CHROOT}//etc/live/config.d/username.conf
        chroot ${CHROOT} tails-activate-winxp-theme
178
    fi
179

180
181
182
    # Set the name (e.g. window title) of the browser
    set_chroot_browser_name "`gettext \"Unsafe Browser\"`"

183
184
185
    # Set start page to something that explains what's going on
    echo 'user_pref("browser.startup.homepage", "'${START_PAGE}'");' >> \
        ${CHROOT}/etc/iceweasel/profile/user.js
186
187
188

    # Remove all bookmarks
    rm -f ${CHROOT}/etc/iceweasel/profile/bookmarks.html
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

    # Make the English wikipedia the only available and default search
    # engine (this is a documented cue for identifying the Unsafe Browser,
    # which is extra important in camouflage mode when the scary coloured
    # theme is disabled.)
    find ${CHROOT}/etc/iceweasel/profile/searchplugins/ \
         ${CHROOT}/etc/iceweasel/searchplugins -name "*.xml" | \
        while read searchengine; do
            if basename "$searchengine" | grep -qvi wikipedia; then
                rm "${searchengine}"
            fi
        done
    sed -i '/^user_pref("browser.search.defaultenginename",/d' \
        ${CHROOT}/etc/iceweasel/profile/user.js
    echo 'user_pref("browser.search.defaultenginename", "Wikipedia (en)");' >> \
        ${CHROOT}/etc/iceweasel/profile/user.js
    sed -i '/^user_pref("browser.search.selectedEngine",/d' \
        ${CHROOT}/etc/iceweasel/profile/user.js
    echo 'user_pref("browser.search.selectedEngine", "Wikipedia (en)");' >> \
        ${CHROOT}/etc/iceweasel/profile/user.js
209
210
}

211
run_browser_in_chroot () {
212
    # Start Iceweasel in the chroot
213
214
    echo "* Starting Unsafe Browser"

215
    sudo -u ${SUDO_USER} xhost +SI:localuser:${CLEARNET_USER} 2>/dev/null
216
    chroot ${CHROOT} sudo -u ${CLEARNET_USER} iceweasel -DISPLAY=:0.0
217
    sudo -u ${SUDO_USER} xhost -SI:localuser:${CLEARNET_USER} 2>/dev/null
218
219
}

220
221
222
223
224
225
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.\"`"
    notify-send -t 10000 "${title}" "${body}"
}

226
227
228
229
230
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
231
        echo "* Restarting Tor"
232
233
        restart-tor
        if ! service tor status >/dev/null; then
234
235
            error "`gettext \"Failed to restart Tor.\"`"
        fi
236
237
    fi
}
238

239
240
241
242
243
244
# 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

245
# Get the DNS servers that was obtained from NetworkManager, if any...
246
247
248
249
250
251
252
253
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.
254
if [ -z "${IP4_NAMESERVERS}" ]; then
Tails developers's avatar
Tails developers committed
255
    error "`gettext \"No DNS server was obtained through DHCP or manually configured in NetworkManager.\"`"
256
257
fi

258
verify_start
259
show_start_notification
260
261
setup_chroot
configure_chroot
262
263
run_browser_in_chroot
show_shutdown_notification
264
maybe_restart_tor
265
266

exit 0