summaryrefslogtreecommitdiff
path: root/gpgsecure.sh
diff options
context:
space:
mode:
Diffstat (limited to 'gpgsecure.sh')
-rwxr-xr-xgpgsecure.sh144
1 files changed, 123 insertions, 21 deletions
diff --git a/gpgsecure.sh b/gpgsecure.sh
index bcb974d..261d046 100755
--- a/gpgsecure.sh
+++ b/gpgsecure.sh
@@ -35,23 +35,111 @@ shutdown() {
exit
}
+
writeback() {
+ trap shutdown SIGINT SIGTERM SIGKILL SIGQUIT SIGHUP
+ # Sync back to disk every 10 seconds
while [ 0 ]; do
- printf '%s Syncing back to encrypted storage\n' "$(date '+%F %T')"
+ #printf '%s Syncing back to encrypted storage\n' "$(date '+%F %T')"
tar -C "${TMP}" -c . | gpg -e --recipient "${KEY}" > "${DIR}.tar.gpg"
if [ $? -gt 0 ]; then
printf 'WARNING: Something went wrong syncing back to encrypted storage\n'
printf 'Your data is likely in danger.\n'
printf 'If you see this message more than once, take a manual backup\n'
fi
- sleep 15
+ sleep 20 &
+ wait $!
done
}
+
+open() {
+ local archive="${1}"
+
+ # Convert DIR to absolute path to avoid cd issues
+ local dirname="$(cd $(dirname ${archive}) && pwd)"
+ local basename="$(basename ${archive})"
+ local dir="${dirname}/${basename}"
+ # Create a temp dir in memory to extract to for safety
+ export TMP=$(mktemp -d /tmp/dec-XXXXXXXXXXXXXX)
+ # Link!
+ ln -s "${TMP}" "${dir}"
+
+ if [ ! -f "${dir}.tar.gpg" ]; then
+ # Tell the user if that encrypted archive does not exist.
+ printf 'Encrypted archive does not exist. Creating.\n'
+ else
+ # Extract the encrypted tarchive if it exists
+ gpg -d "${dir}.tar.gpg" | tar -C "${TMP}" -x
+ fi
+
+ writeback &
+ echo $! > "${dirname}/.${basename}.pid"
+}
+
+
+status() {
+ local archive=${1}
+
+ local dirname="$(dirname ${archive})"
+ local basename="$(basename ${archive})"
+ local pidfile="${dirname}/.${basename}.pid"
+
+ # If no pidfile, assume closed
+ if [ ! -f "${pidfile}" ]; then
+ printf '%s is closed\n' "${archive}"
+ return 0
+ fi
+
+ local pid="$(cat ${pidfile})"
+
+ ps "${pid}" 2>/dev/null 1>/dev/null
+ if [ $? -eq 0 ]; then
+ printf '%s is open\n' "${archive}"
+ elif [ $? -gt 0 ]; then
+ printf '%s is closed but a stale pidfile was found. Removing\n' "${archive}"
+ rm -f "${pidfile}"
+ else
+ printf '%s is closed\n' "${archive}"
+ fi
+}
+
+
+close() {
+ local archive=${1}
+
+ local dirname="$(dirname ${archive})"
+ local basename="$(basename ${archive})"
+ local pidfile="${dirname}/.${basename}.pid"
+ local pid="$(cat ${pidfile})"
+
+ ps "${pid}" 2>/dev/null 1>/dev/null
+ if [ $? -gt 0 ]; then
+ printf "Stale pidfile detected but share is not open. Removing\n"
+ rm -f "${pidfile}"
+ return 1
+ else
+ # Send SIGTERM (15) to tell the process to exit cleanly
+ kill -15 "${pid}"
+ [ $? -eq 0 ] && rm "${pidfile}" && return 0
+
+ printf 'Error closing archive "%s"\n' "${archive}"
+ return 1
+ fi
+}
+
+
main() {
- DIR="${1}"
- if [ -z "${DIR:-}" ]; then
- printf 'Directory to decrypt required\n'
+ local action="${1}"
+ local archive="${2}"
+
+ # Input validation
+ if [ -z "${action:-}" ]; then
+ printf 'Action (open, close, or status) required\n'
+ return 1
+ fi
+ if [ -z "${archive:-}" ]; then
+ printf 'Archive to decrypt required\n'
return 1
fi
@@ -66,24 +154,38 @@ main() {
return 1
fi
- # Convert DIR to absolute path to avoid cd issues
- DIR="$(cd $(dirname ${DIR}) && pwd)/$(basename ${DIR})"
- # Create a temp dir in memory to extract to for safety
- TMP=$(mktemp -d /tmp/dec-XXXXXXXXXXXXXX)
- # Link!
- ln -s "${TMP}" "${DIR}"
-
- if [ ! -f "${DIR}.tar.gpg" ]; then
- # Tell the user if that encrypted archive does not exist.
- printf 'Encrypted archive does not exist. Creating.\n'
- else
- # Extract the encrypted tarchive if it exists
- gpg -d ${DIR}.tar.gpg | tar -C "${TMP}" -x
+ local dirname="$(dirname ${archive})"
+ local basename="$(basename ${archive})"
+ export DIR="${dirname}/${basename}"
+
+ if [ "${action}" = 'open' ]; then
+ # Check if already open
+ if [ -f "${dirname}/.${basename}.pid" ]; then
+ printf 'ERROR: Archive "%s" is already open\n' "${archive}"
+ return 1
+ else
+ printf 'Opening!\n'
+ open "${archive}"
+ return $?
+ fi
+ elif [ "${action}" = 'close' ]; then
+ # Check if already closed
+ if [ ! -f "${dirname}/.${basename}.pid" ]; then
+ printf 'ERROR: Archive "%s" is not open\n' "${archive}"
+ exit 1
+ else
+ printf 'Closing!\n'
+ close "${archive}"
+ return $?
+ fi
+ elif [ "${action}" = 'status' ]; then
+ status "${archive}"
+ return $?
fi
- writeback
- shutdown
- return $?
+ # If we make it here, something went wrong.
+ printf 'ERROR: Unknown action "%s"\n' "${action}"
+ return 1
}
main ${@}

Generated by cgit