#!/bin/bash set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat < mv [--phone] / mv -h|--help Needs an identity that can read it now. The plaintext goes through a pipe, never a file. --phone: for a secret this machine cannot read. An admin key elsewhere, reached through \$SZECRET_PHONE_COMMAND, rewraps the file's key for the bucket's readers, and this machine puts the file's own encrypted value under the new header. Nothing is decrypted here; where this machine is one of the new readers, it opens the result before keeping it. EOF exit 0 fi this_dir="$(cd "$(dirname "$0")" && pwd)" source "${this_dir}/common.sh" phone="" if [[ "${1:-}" == --phone ]]; then phone=1 shift fi [[ "$#" -eq 2 ]] || die "usage: szecret mv [--phone] " [[ -n "${buckets}" ]] || die "no buckets on this machine" target="$2" [[ "${target}" =~ ${re_bucket} ]] || die "not a bucket's name: ${target}" from="$(find_secret "$1")" || exit 1 name="$(basename "${from}" .age)" dir="${buckets}/${target}" to="${dir}/${name}.age" [[ "${from}" != "${to}" ]] || die "${name} is already in ${target}" # A name in two places is a mistake to sort out by hand, not to overwrite. [[ ! -e "${to}" ]] || die "${target}/${name} already exists; not overwriting it with $(where_is "${from}")" if [[ -n "${store}" && "$(dirname "${from}")" == "${store}" ]]; then [[ -z "${phone}" ]] || die "mv --phone moves a secret out of a bucket; $(where_is "${from}") is in the old store" ensure_private_key fi set="$(bucket_set "${dir}")" || exit 1 # header_bytes : the length of its age header, up to and with the MAC line. header_bytes() { LC_ALL=C awk '{ n += length($0) + 1 } /^--- / { print n; found = 1; exit } END { if (!found) exit 1 }' "$1" } if [[ -n "${phone}" ]]; then [[ -n "${SZECRET_PHONE_COMMAND:-}" ]] || die "mv --phone: SZECRET_PHONE_COMMAND is not set" names="$(key_names "${dir}")" admins="$(admin_keys)" || exit 1 header="$(mktemp)" snapshot="$(mktemp)" trap 'rm -f "${header}" "${snapshot}"' EXIT # The phone answers for these bytes, which may sit an hour: a sync can # bring a rekey of the file meanwhile. What is written is built from them, # and only if the file is still them. cp "${from}" "${snapshot}" { echo "name ${name}" echo "from $(bucket_label "$(dirname "${from}")")" echo "to ${target}" echo "file ${snapshot}" echo "dir ${dir}" while read -r k; do echo "reader ${k} $(name_of "${k}" "${names}" "${admins}")" done <<<"${set}" } | bash -c "${SZECRET_PHONE_COMMAND}" > "${header}" \ || die "mv --phone: no new header; $(where_is "${from}") is as it was" # The readers can change while the phone answers: then the header is for # the old ones, and a rekey comes first. [[ "$(bucket_set "${dir}")" == "${set}" ]] \ || die "the readers of ${target} changed while the phone answered; $(where_is "${from}") is as it was" hlen="$(header_bytes "${header}")" && [[ "${hlen}" -eq "$(wc -c < "${header}" | tr -d ' ')" ]] \ || die "mv --phone: what came back is not a whole age header; $(where_is "${from}") is as it was" [[ "$(stanzas_of "${header}" | grep -c .)" -eq "$(grep -c . <<<"${set}")" ]] \ || die "mv --phone: the new header does not have one stanza per reader; $(where_is "${from}") is as it was" old_hlen="$(header_bytes "${snapshot}")" || die "$(where_is "${from}") is not an age file" tmp="$(mktemp "${dir}/.put.XXXXXX")" || die "cannot write in ${dir}" # The new header, then this file's own encrypted value, byte for byte. { cat "${header}"; tail -c +"$((old_hlen + 1))" "${snapshot}"; } > "${tmp}" while read -r k; do if grep -qxF "${k}" <<<"${set}"; then load_identities if ! age --decrypt "${ids[@]}" "${tmp}" > /dev/null; then rm -f "${tmp}" die "mv --phone: this machine is a reader of ${target} and cannot open the new file; $(where_is "${from}") is as it was" fi >&2 echo "szecret: opened the new file here, as one of its readers" break fi done < <(my_keys) if ! cmp -s "${snapshot}" "${from}"; then rm -f "${tmp}" die "mv --phone: $(where_is "${from}") changed while the phone answered (a rekey?); nothing written, ask again" fi if [[ -e "${to}" ]]; then rm -f "${tmp}" die "${target}/${name} appeared while the phone answered; not overwriting it" fi chmod 644 "${tmp}" mv "${tmp}" "${to}" rm "${from}" >&2 echo "szecret: moved $(where_is "${to}"), from $(where_is "${from}"), by the phone" exit 0 fi age_version_ok load_identities if ! age --decrypt "${ids[@]}" "${from}" | encrypt_to "${to}" "${set}"; then rm -f "${to}" die "could not move ${name}; $(where_is "${from}") is as it was" fi rm "${from}" >&2 echo "szecret: moved $(where_is "${to}"), from $(where_is "${from}")"