unsafe-browser 5.84 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
9
10
11

. gettext.sh
TEXTDOMAIN=${CMD}
export TEXTDOMAIN

12
13
SQUASH=/live/image/live/filesystem.squashfs
ROFS=/live/rofs
14
15
16
CONF_DIR=/var/lib/unsafe-browser
COW=${CONF_DIR}/cow
CHROOT=${CONF_DIR}/chroot
17
18
19
20
21
22
23
CLEARNET_USER=clearnet
OFFENDING_ADDONS="xul-ext-foxyproxy-standard xul-ext-torbutton"
TOR_DIR=/var/lib/tor
TOR_DESCRIPTORS=${TOR_DIR}/cached-descriptors
TOR_WORKING=""

cleanup () {
24
25
26
    # Break down the chroot and kill all of its processes
    local counter=0
    local ret=0
27
    while [ "${counter}" -le 10 ] && \
Tails developers's avatar
Tails developers committed
28
        pgrep -u ${CLEARNET_USER} 1>/dev/null 2>&1; do
29
        pkill -u ${CLEARNET_USER} 1>/dev/null 2>&1
30
        ret=${?}
31
        sleep 1
32
        counter=$((${counter}+1))
33
    done
34
35
    [ ${ret} -eq 0 ] || pkill -9 -u ${CLEARNET_USER} 1>/dev/null 2>&1
    for mnt in ${CHROOT}/dev ${CHROOT}/proc ${CHROOT} ${COW} ${ROFS}; do
36
        counter=0
37
38
        while [ "${counter}" -le 10 ] && mountpoint -q ${mnt} 2>/dev/null; do
            umount ${mnt} 2>/dev/null
39
            sleep 1
40
            counter=$((${counter}+1))
41
42
        done
    done
43
    rmdir ${ROFS} ${COW} ${CHROOT} 2>/dev/null
44
45
46
}

error () {
Tails developers's avatar
Tails developers committed
47
    local cli_text="${CMD}: `gettext \"error\"`: ${@}"
Tails developers's avatar
Tails developers committed
48
    local dialog_text="${@}
49

Tails developers's avatar
Tails developers committed
50
`gettext \"Unsafe Browser will exit now.\"`"
51
    echo "${cli_text}" >&2
52
    sudo -u ${SUDO_USER} zenity --error --title "" --text "${dialog_text}"
53
54
55
56
    exit 1
}

warning () {
57
    local text="${@}"
Tails developers's avatar
Tails developers committed
58
    echo "${CMD}: `gettext \"warning\"`: ${text}" >&2
59
    sudo -u ${SUDO_USER} zenity --warning --title "" --text "${text}"
60
61
}

62
63
verify_start () {
    # Make sure the user really wants to start the browser
Tails developers's avatar
Tails developers committed
64
    local dialog_msg="<b>`gettext \"Do you really want to launch the Unsafe Browser?\"`</b>
65

Tails developers's avatar
Tails developers committed
66
`gettext \"All activity within the Unsafe Browser will <b>not</b> be anonymous. Only use the the Unsafe Browser if necessary, for example if you have to login or register to activate your Internet connection.\"`"
67
68
    if ! sudo -u ${SUDO_USER} zenity --question --title "" \
         --text "${dialog_msg}"; then
69
70
71
        exit 0
    fi
}
72

73
74
75
76
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.
77
78
    echo "* Setting up chroot"

79
80
81
    trap cleanup INT
    trap cleanup EXIT

82
83
84
85
86
    mkdir -p ${ROFS} ${COW} ${CHROOT} && \
    mount -t squashfs -o loop ${SQUASH} ${ROFS} && \
    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
87
88
    mount --bind /dev ${CHROOT}/dev || \
    error "`gettext \"Failed to setup chroot.\"`"
89
90
91
}

configure_chroot () {
92
93
    echo "* Configuring chroot"

94
95
    # Set the chroot's DNS servers to those obtained through DHCP
    rm -f ${CHROOT}/etc/resolv.conf
96
    for NS in ${IP4_NAMESERVERS}; do
97
98
99
100
101
        echo "nameserver ${NS}" >> ${CHROOT}/etc/resolv.conf
    done
    chmod a+r ${CHROOT}/etc/resolv.conf

    # Disable problematic Iceweasel addons and proxying in the chroot
102
    chroot ${CHROOT} apt-get remove --yes ${OFFENDING_ADDONS}
103
104
105
106
    sed -i '/^pref("network.proxy.type",/d' \
        ${CHROOT}/etc/iceweasel/pref/iceweasel.js
    echo 'pref("network.proxy.type", 0);' >> \
        ${CHROOT}/etc/iceweasel/pref/iceweasel.js
107

Tails developers's avatar
Tails developers committed
108
    # Set a scary theme (except if we're using Windows camouflage)
109
    if grep -qwv winxp /proc/cmdline; then
110
        echo >> ${CHROOT}/etc/iceweasel/profile/user.js <<EOF
111
user_pref("lightweightThemes.isThemeSelected", true);
112
113
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\":\"#FFFF00\",\"accentcolor\":\"#CC0000\",\"updateDate\":0,\"installDate\":0}]");
EOF
114
    fi
115
116
117
118
}

start_browser_in_chroot () {
    # Start Iceweasel in the chroot
119
120
    echo "* Starting Unsafe Browser"

121
    sudo -u ${SUDO_USER} xhost +SI:localuser:${CLEARNET_USER} 2>/dev/null
122
    chroot ${CHROOT} sudo -u ${CLEARNET_USER} iceweasel -DISPLAY=:0.0
123
    sudo -u ${SUDO_USER} xhost -SI:localuser:${CLEARNET_USER} 2>/dev/null
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}

tor_is_working() {
    # FIXME: the approach is stolen from is_tor_working() in the 20-time
    # NM hook -- we should move things like this to a shell script library
    # FIXME: how to determine this reliably? this approach doesn't work
    # if $TOR_DIR is persistent.
    [ -e $TOR_DESCRIPTORS ]
}

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
139
140
        echo "* Restarting Tor"
        if ! service tor restart; then
141
142
143
144
145
            error "`gettext \"Failed to restart Tor.\"`"
        fi
        local counter=0
        until [ "${counter}" -ge 10 ] || nc -z localhost 9051 2>/dev/null; do
            sleep 1
146
            counter=$((${counter}+1))
147
        done
148
        /etc/NetworkManager/dispatcher.d/60-vidalia.sh clearnet up 2>/dev/null
149
150
    fi
}
151

152
153
154
155
156
157
# 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

158
# Get the DNS servers that was obtained from NetworkManager, if any...
159
160
161
162
163
164
165
166
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.
167
if [ -z "${IP4_NAMESERVERS}" ]; then
Tails developers's avatar
Tails developers committed
168
    error "`gettext \"No DNS server was obtained through DHCP or manually configured in NetworkManager.\"`"
169
170
fi

171
172
173
174
175
verify_start
setup_chroot
configure_chroot
start_browser_in_chroot
maybe_restart_tor
176
177

exit 0