summaryrefslogtreecommitdiff
path: root/secrets/mv.sh
blob: 4a7c4f7de8ca54f9dbead5c4183b38899fec15bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash

set -euo pipefail

if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
    cat <<EOF
mv: Move a secret to another bucket

Decrypts it, encrypts it to the bucket's readers and the admins, and deletes
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 [--phone] <name> <bucket>
  mv [--phone] <bucket>/<name> <bucket>
  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] <name> <bucket>"
[[ -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 <file>: 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}")"