|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Data at rest encryption for NextCloudPi |
| 4 | +# |
| 5 | +# Copyleft 2021 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> |
| 6 | +# GPL licensed (see end of file) * Use at your own risk! |
| 7 | +# |
| 8 | +# More at: nextcloudpi.com |
| 9 | +# |
| 10 | + |
| 11 | +is_active() |
| 12 | +{ |
| 13 | + mount | grep ncdata_enc | grep -q gocryptfs |
| 14 | +} |
| 15 | + |
| 16 | +install() |
| 17 | +{ |
| 18 | + apt_install gocryptfs |
| 19 | +} |
| 20 | + |
| 21 | +configure() |
| 22 | +{ |
| 23 | +( |
| 24 | + set -eu -o pipefail |
| 25 | + local datadir parentdir encdir tmpdir |
| 26 | + datadir="$(get_ncpcfg datadir)" |
| 27 | + [[ "${datadir}" == "null" ]] && datadir=/var/www/nextcloud/data |
| 28 | + parentdir="$(dirname "${datadir}")" |
| 29 | + encdir="${parentdir}/ncdata_enc" |
| 30 | + tmpdir="$(mktemp -u -p "${parentdir}" -t nc-data-crypt.XXXXXX))" |
| 31 | + |
| 32 | + [[ "${ACTIVE}" != "yes" ]] && { |
| 33 | + if ! is_active; then |
| 34 | + echo "Data not currently encrypted" |
| 35 | + return 0 |
| 36 | + fi |
| 37 | + save_maintenance_mode |
| 38 | + trap restore_maintenance_mode EXIT |
| 39 | + echo "Decrypting data..." |
| 40 | + mkdir "${tmpdir}" |
| 41 | + chown www-data: "${tmpdir}" |
| 42 | + pkill tail # prevents from umounting in docker |
| 43 | + mv "${datadir}"/* "${datadir}"/.[!.]* "${tmpdir}" |
| 44 | + fusermount -u "${datadir}" |
| 45 | + rmdir "${datadir}" |
| 46 | + mv "${tmpdir}" "${datadir}" |
| 47 | + rm "${encdir}"/gocryptfs.* |
| 48 | + rmdir "${encdir}" |
| 49 | + echo "Data no longer encrypted" |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if is_active; then |
| 54 | + echo "Encrypted data already in use" |
| 55 | + return |
| 56 | + fi |
| 57 | + |
| 58 | + # Just mount already encrypted data |
| 59 | + if [[ -f "${encdir}"/gocryptfs.conf ]]; then |
| 60 | + echo "${PASSWORD}" | gocryptfs -allow_other -q "${encdir}" "${datadir}" 2>&1 | sed /^Switch/d |
| 61 | + |
| 62 | + # switch to the regular virtual hosts after we decrypt, so we can access NC and ncp-web |
| 63 | + a2ensite ncp nextcloud |
| 64 | + a2dissite ncp-activation |
| 65 | + apache2ctl -k graceful |
| 66 | + |
| 67 | + echo "Encrypted data now accessible" |
| 68 | + return |
| 69 | + fi |
| 70 | + mkdir -p "${encdir}" |
| 71 | + echo "${PASSWORD}" | gocryptfs -init -q "${encdir}" |
| 72 | + save_maintenance_mode |
| 73 | + trap restore_maintenance_mode EXIT |
| 74 | + |
| 75 | + mv "${datadir}" "${tmpdir}" |
| 76 | + |
| 77 | + mkdir "${datadir}" |
| 78 | + echo "${PASSWORD}" | gocryptfs -allow_other -q "${encdir}" "${datadir}" 2>&1 | sed /^Switch/d |
| 79 | + |
| 80 | + echo "Encrypting data..." |
| 81 | + mv "${tmpdir}"/* "${tmpdir}"/.[!.]* "${datadir}" |
| 82 | + chown -R www-data: "${datadir}" |
| 83 | + rmdir "${tmpdir}" |
| 84 | + |
| 85 | + set_ncpcfg datadir "${datadir}" |
| 86 | + |
| 87 | + echo "Data is now encrypted" |
| 88 | +) |
| 89 | +} |
| 90 | + |
| 91 | +# License |
| 92 | +# |
| 93 | +# This script is free software; you can redistribute it and/or modify it |
| 94 | +# under the terms of the GNU General Public License as published by |
| 95 | +# the Free Software Foundation; either version 2 of the License, or |
| 96 | +# (at your option) any later version. |
| 97 | +# |
| 98 | +# This script is distributed in the hope that it will be useful, |
| 99 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 100 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 101 | +# GNU General Public License for more details. |
| 102 | +# |
| 103 | +# You should have received a copy of the GNU General Public License |
| 104 | +# along with this script; if not, write to the |
| 105 | +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 106 | +# Boston, MA 02111-1307 USA |
| 107 | + |
0 commit comments