chroot-browser.sh 7.41 KB
Newer Older
1 2
#!/bin/sh

3 4
# Import the TBB_INSTALL, TBB_PROFILE and TBB_EXT variables, and
# configure_xulrunner_app_locale().
5 6
. /usr/local/lib/tails-shell-library/tor-browser.sh

7 8 9
# Import windows_camouflage_is_enabled()
. /usr/local/lib/tails-shell-library/tails-greeter.sh

Tails developers's avatar
Tails developers committed
10 11 12
# Import try_for().
. /usr/local/lib/tails-shell-library/common.sh

13 14 15 16 17
# Break down the chroot and kill all of its processes
try_cleanup_browser_chroot () {
    local chroot="${1}"
    local cow="${2}"
    local user="${3}"
18
    try_for 10 "pkill -u ${user} 1>/dev/null 2>&1" 0.1 || \
19
        pkill -9 -u "${user}" 1>/dev/null 2>&1 || :
20
    for mnt in "${chroot}"/dev "${chroot}"/proc "${chroot}" "${cow}"; do
21
        try_for 10 "umount ${mnt} 2>/dev/null" 0.1
22
    done
23
    rmdir "${cow}" "${chroot}" 2>/dev/null
24
}
25 26 27 28 29

# Setup a chroot on a clean aufs "fork" of the root filesystem.
setup_browser_chroot () {
    local chroot="${1}"
    local cow="${2}"
30 31
    local user="${3}"

32 33 34
    # 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.

35 36
    local cleanup_cmd="try_cleanup_browser_chroot \"${chroot}\" \"${cow}\" \"${user}\""
    trap "${cleanup_cmd}" INT EXIT
37 38

    local rootfs_dir
39 40
    local rootfs_dirs_path="/lib/live/mount/rootfs"
    local tails_module_path="/lib/live/mount/medium/live/Tails.module"
41 42 43 44 45 46 47 48 49 50 51 52 53
    local aufs_dirs=

    # We have to pay attention to the order we stack the filesystems;
    # newest must be first, and remember that the .module file lists
    # oldest first, newest last.
    while read rootfs_dir; do
        rootfs_dir="${rootfs_dirs_path}/${rootfs_dir}"
        mountpoint -q "${rootfs_dir}" && \
        aufs_dirs="${rootfs_dir}=rr+wh:${aufs_dirs}"
    done < "${tails_module_path}"
    # But our copy-on-write dir must be at the very top.
    aufs_dirs="${cow}=rw:${aufs_dirs}"

54 55 56 57 58
    mkdir -p "${cow}" "${chroot}" && \
    mount -t tmpfs tmpfs "${cow}" && \
    mount -t aufs -o "noatime,noxino,dirs=${aufs_dirs}" aufs "${chroot}" && \
    mount -t proc proc "${chroot}"/proc && \
    mount --bind /dev "${chroot}"/dev || \
59
        return 1
60 61

    # Workaround for #6110
62
    chmod -t "${cow}"
63
}
64

65 66
set_chroot_browser_locale () {
    local chroot="${1}"
67 68 69 70
    local browser_name="${2}"
    local browser_user="${3}"
    local locale="${4}"
    local browser_profile="${chroot}/home/${browser_user}/.${browser_name}/profile.default"
71 72 73
    configure_xulrunner_app_locale "${browser_profile}" "${locale}"
}

74 75 76 77
set_chroot_browser_name () {
    local chroot="${1}"
    local name="${2}"
    local locale="${3}"
78
    local ext_dir="${chroot}/${TBB_EXT}"
79
    local pack top rest
80
    if [ "${locale}" != "en-US" ]; then
81
        pack="${ext_dir}/langpack-${locale}@firefox.mozilla.org.xpi"
82 83
        top="browser/chrome"
        rest="${locale}/locale"
84 85
    else
        pack="${chroot}/${TBB_INSTALL}/browser/omni.ja"
86 87
        top="chrome"
        rest="en-US/locale"
88
    fi
89
    local tmp="$(mktemp -d)"
90 91 92 93 94 95 96 97 98
    local branding="${top}/${rest}/branding/brand.dtd"
    # Non-zero exit code due to non-standard ZIP archive.
    # The following steps will fail soon if the extraction failed anyway.
    7z x -o"${tmp}" "${pack}" "${branding}" || true
    sed -i "s/<"'!'"ENTITY\s\+brand\(Full\|Short\)Name.*$/<"'!'"ENTITY brand\1Name \"${name}\">/" "${tmp}/${branding}"
    (cd ${tmp} ; 7z u -tzip "${pack}" .)
    chmod a+r "${pack}"
    rm -Rf "${tmp}"
}
99 100 101 102

# Start the browser in the chroot
run_chroot_browser () {
    local chroot="${1}"
103 104 105
    local browser_name="${2}"
    local chroot_user="${3}"
    local local_user="${4}"
106 107 108 109 110

    sudo -u ${local_user} xhost +SI:localuser:${chroot_user} 2>/dev/null
    chroot ${chroot} sudo -u ${chroot_user} /bin/sh -c \
        '. /usr/local/lib/tails-shell-library/tor-browser.sh && \
         exec_firefox -DISPLAY=:0.0 \
111
                      -profile '"/home/${chroot_user}/.${browser_name}/profile.default"
112 113
    sudo -u ${local_user} xhost -SI:localuser:${chroot_user} 2>/dev/null
}
114 115 116 117 118 119

# TorButton forces the Browser name to Tor Browser unless we alter its
# branding files a bit.
set_chroot_torbutton_browser_name () {
    local chroot="${1}"
    local name="${2}"
120 121 122 123 124 125 126
    local locale="${3}"
    local torbutton_locale_dir="${chroot}"/usr/share/xul-ext/torbutton/chrome/locale/${locale}
    if [ ! -d "${torbutton_locale_dir}" ]; then
        # Surprisingly, the default locale is en, not en-US
        torbutton_locale_dir="${chroot}"/usr/share/xul-ext/torbutton/chrome/locale/en
    fi
    sed -i "s/<"'!'"ENTITY\s\+brand\(Full\|Short\)Name.*$/<"'!'"ENTITY brand\1Name \"${name}\">/" "${torbutton_locale_dir}/brand.dtd"
127
}
128 129 130 131 132 133 134 135

# Set the chroot's DNS servers (IPv4 only)
configure_chroot_dns_servers () {
    local chroot="${1}" ; shift
    local ip4_nameservers="${@}"

    rm -f ${chroot}/etc/resolv.conf
    for ns in ${ip4_nameservers}; do
136
        echo "nameserver ${ns}" >> "${chroot}/etc/resolv.conf"
137 138 139 140 141 142
    done
    chmod a+r ${chroot}/etc/resolv.conf
}

set_chroot_browser_permissions () {
    local chroot="${1}"
143 144 145
    local browser_name="${2}"
    local browser_user="${3}"
    local browser_conf="${chroot}/home/${browser_user}/.${browser_name}"
146
    chown -R "${browser_user}:${browser_user}" "${browser_conf}"
147 148 149 150 151 152 153
}

configure_chroot_browser () {
    local chroot="${1}" ; shift
    local browser_name="${1}" ; shift
    local browser_user="${1}" ; shift
    local startpage="${1}" ; shift
154 155
    # Now $@ is a list of paths (that must be valid after chrooting)
    # to extensions to enable.
156 157

    # Prevent sudo from complaining about failing to resolve the 'amnesia' host
158
    echo "127.0.0.1 localhost amnesia" > "${chroot}/etc/hosts"
159 160

    # Create a fresh browser profile for the clearnet user
161
    local browser_conf="${chroot}/home/${browser_user}/.${browser_name}"
162
    local browser_profile="${browser_conf}/profile.default"
163
    local browser_ext="${browser_profile}/extensions"
164 165 166
    mkdir -p "${browser_profile}" "${browser_ext}"

    # Select extensions to enable
167 168 169 170 171
    local extension
    while [ -n "${*}" ]; do
        extension="${1}" ; shift
        ln -s "${extension}" "${browser_ext}"
    done
172 173

    # Set preferences
174
    local browser_prefs="${browser_profile}/preferences/prefs.js"
175
    mkdir -p "$(dirname "${browser_prefs}")"
176
    cp /usr/share/tails/"${browser_name}/prefs.js" "${browser_prefs}"
177 178

    # Set start page to something that explains what's going on
179 180 181 182
    if [ -n "${startpage}" ]; then
        echo 'user_pref("browser.startup.homepage", "'"${startpage}"'");' >> \
            "${browser_prefs}"
    fi
183 184 185 186

    # Customize the GUI
    local browser_chrome="${browser_profile}/chrome/userChrome.css"
    mkdir -p "$(dirname "${browser_chrome}")"
187
    cp /usr/share/tails/"${browser_name}"/userChrome.css "${browser_chrome}"
188 189

    # Remove all bookmarks
190 191 192
    rm -f "${chroot}/${TBB_PROFILE}/bookmarks.html"
    rm -f "${browser_profile}/bookmarks.html"
    rm -f "${browser_profile}/places.sqlite"
193 194 195 196 197 198 199 200

    # Set an appropriate theme, except if we're using Windows
    # camouflage.
    if ! windows_camouflage_is_enabled; then
        cat /usr/share/tails/"${browser_name}"/theme.js >> "${browser_prefs}"
    else
        # The tails-activate-win8-theme script requires that the
        # browser profile is writable by the user running the script.
201
        set_chroot_browser_permissions "${chroot}" "${browser_user}"
202 203
        # The camouflage activation script requires a dbus server for
        # properly configuring GNOME, so we start one in the chroot
204
        chroot "${chroot}" sudo -H -u "${browser_user}" sh -c 'eval `dbus-launch --auto-syntax`; tails-activate-win8-theme' || :
205 206
    fi
}