From dcbd341a04bbbf0ac7d720a3a3bf882778983bfe Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Thu, 19 Nov 2026 22:31:31 +0000 Subject: szecret mv --phone: an admin elsewhere rewraps the file key for a secret this machine can't read The move goes to SZECRET_PHONE_COMMAND with a snapshot of the file; the new file is that snapshot's payload under the header that comes back, written only if the file is unchanged, and opened first where this machine is a new reader. --- secrets/mv.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 3 deletions(-) (limited to 'secrets/mv.sh') diff --git a/secrets/mv.sh b/secrets/mv.sh index d249ac1..4a7c4f7 100755 --- a/secrets/mv.sh +++ b/secrets/mv.sh @@ -11,12 +11,18 @@ the old file. A secret is never copied: when a host outside its bucket needs it, it moves to a bucket that has every reader who needs it. Usage: - mv - mv / + mv [--phone] + 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 @@ -24,7 +30,12 @@ fi this_dir="$(cd "$(dirname "$0")" && pwd)" source "${this_dir}/common.sh" -[[ "$#" -eq 2 ]] || die "usage: szecret mv " +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}" @@ -37,10 +48,75 @@ to="${dir}/${name}.age" # 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 -- cgit v1.2.3