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

Tails developers's avatar
Tails developers committed
3
4
5
6
7
8
CMD=$(basename ${0})

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

9
10
SQUASH=/live/image/live/filesystem.squashfs
ROFS=/live/rofs
11
12
13
CONF_DIR=/var/lib/unsafe-browser
COW=${CONF_DIR}/cow
CHROOT=${CONF_DIR}/chroot
14
15
16
17
18
19
20
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 () {
21
22
23
    # Break down the chroot and kill all of its processes
    local counter=0
    local ret=0
24
25
26
    while [ "${counter}" -le 10 ] && \
          pgrep -u ${CLEARNET_USER} 1>/dev/null 2>&1; do
        pkill -u ${CLEARNET_USER} 1>/dev/null 2>&1
27
        ret=${?}
28
        sleep 1
29
        counter=$((${counter}+1))
30
    done
31
32
    [ ${ret} -eq 0 ] || pkill -9 -u ${CLEARNET_USER} 1>/dev/null 2>&1
    for mnt in ${CHROOT}/dev ${CHROOT}/proc ${CHROOT} ${COW} ${ROFS}; do
33
        counter=0
34
35
        while [ "${counter}" -le 10 ] && mountpoint -q ${mnt} 2>/dev/null; do
            umount ${mnt} 2>/dev/null
36
            sleep 1
37
            counter=$((${counter}+1))
38
39
        done
    done
40
    rmdir ${ROFS} ${COW} ${CHROOT} 2>/dev/null
41
42
43
}

error () {
Tails developers's avatar
Tails developers committed
44
    local cli_text="${CMD}: `gettext \"error\"`: ${@}"
Tails developers's avatar
Tails developers committed
45
    local dialog_text="${@}
46

Tails developers's avatar
Tails developers committed
47
`gettext \"Unsafe Browser will exit now.\"`"
48
49
    echo "${cli_text}" >&2
    zenity --error --title "" --text "${dialog_text}"
50
51
52
53
    exit 1
}

warning () {
54
    local text="${@}"
Tails developers's avatar
Tails developers committed
55
    echo "${CMD}: `gettext \"warning\"`: ${text}" >&2
56
    zenity --warning --title "" --text "${text}"
57
58
}

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

Tails developers's avatar
Tails developers committed
63
`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 in order to activate your Internet connection.\"`"
64
65
66
67
    if ! zenity --question --title "" --text "${dialog_msg}"; then
        exit 0
    fi
}
68

69
70
71
72
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.
73
74
75
    trap cleanup INT
    trap cleanup EXIT

76
77
78
79
80
    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
81
82
    mount --bind /dev ${CHROOT}/dev || \
    error "`gettext \"Failed to setup chroot.\"`"
83
84
85
86
87
}

configure_chroot () {
    # Set the chroot's DNS servers to those obtained through DHCP
    rm -f ${CHROOT}/etc/resolv.conf
88
    for NS in ${IP4_NAMESERVERS}; do
89
90
91
92
93
        echo "nameserver ${NS}" >> ${CHROOT}/etc/resolv.conf
    done
    chmod a+r ${CHROOT}/etc/resolv.conf

    # Disable problematic Iceweasel addons and proxying in the chroot
94
    chroot ${CHROOT} apt-get remove --yes ${OFFENDING_ADDONS} 1>/dev/null 2>&1
95
96
97
98
99
100
101
102
    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
}

start_browser_in_chroot () {
    # Start Iceweasel in the chroot
103
    sudo -u ${SUDO_USER} xhost +SI:localuser:${CLEARNET_USER} 2>/dev/null
104
    chroot ${CHROOT} sudo -u ${CLEARNET_USER} iceweasel -DISPLAY=:0.0
105
    sudo -u ${SUDO_USER} xhost -SI:localuser:${CLEARNET_USER} 2>/dev/null
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}

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
121
122
123
124
125
126
127
128
        if ! service tor restart 2>/dev/null; then
            error "`gettext \"Failed to restart Tor.\"`"
        fi
        local counter=0
        until [ "${counter}" -ge 10 ] || nc -z localhost 9051 2>/dev/null; do
            sleep 1
	    counter=$((${counter}+1))
        done
129
        /etc/NetworkManager/dispatcher.d/60-vidalia.sh clearnet up 2>/dev/null
130
131
    fi
}
132

133
# Get the DNS servers that was obtained from NetworkManager, if any...
134
135
136
137
138
139
140
141
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.
142
if [ -z "${IP4_NAMESERVERS}" ]; then
Tails developers's avatar
Tails developers committed
143
    error "`gettext \"No DNS server was obtained through DHCP or manually configured in NetworkManager.\"`"
144
145
fi

146
147
148
149
150
verify_start
setup_chroot
configure_chroot
start_browser_in_chroot
maybe_restart_tor
151
152

exit 0