interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / ipl / ipl
| 1 | #!/bin/bash |
| 2 | # ipl: Interplane's own project store. Step 1 of DESIGN.md; the contract for consumers is CONTRACT.md. |
| 3 | # |
| 4 | # ipl init ROOT NAME BRANCH VISIBILITY ALIAS KEY |
| 5 | # ipl import ROOT NAME BRANCH EVIDENCE_FILE NOTE_FILE REQUEST KEY |
| 6 | # ipl land ROOT NAME BRANCH NEW SOURCE RECEIPT_FILE VERDICT_FILE REQUEST KEY |
| 7 | # ipl handover ROOT NAME|--all OLD_KEY NEW_PUBKEY [REASON] |
| 8 | # ipl verify ROOT NAME [--expect-owner FP] |
| 9 | # ipl land ... [--expect-owner FP] |
| 10 | # ipl catalog ROOT [--expect-owner FP] |
| 11 | # |
| 12 | # The current owner is pinned outside the store, at |
| 13 | # ${IPL_OWNER_PINS:-$HOME/.config/interplane/owner-pins}: lines of `NAME SHA256:<43>` |
| 14 | # or `* SHA256:<43>` for every project. The file must exist, be mode 0600 (no group or |
| 15 | # world bits), and resolve outside ROOT; otherwise verify, land, import, handover and |
| 16 | # catalog fail closed (exit 3). That is the out-of-band pin: the value is written by |
| 17 | # hand from the owner key's .pub on the machine that holds it, never copied from a |
| 18 | # store. After a handover writes a signed SUCCESSOR, ipl updates the local pin file |
| 19 | # (a single handover rewrites that NAME line and prints `ipl: pin: NAME SHA256:...`; |
| 20 | # --all rewrites every rotated NAME line, and the `*` line only if it named the old |
| 21 | # key, and prints `ipl: pin: * SHA256:...`) so other hosts can be updated by hand. |
| 22 | # --expect-owner FP may also be passed; if it disagrees with the file, or the |
| 23 | # current key is not the pin, the command fails closed (exit 3). |
| 24 | # |
| 25 | # Who decides what: |
| 26 | # bin/landrec (om/landrec.om, seeded Omega) what a record may say: field rules, canonical bytes |
| 27 | # bin/chain (forge chain/chain.om) the chain: append and its own verification |
| 28 | # git storage, ancestry, ref rules |
| 29 | # ssh-keygen -Y sign / verify Ed25519 signatures (as the forge's sign-in) |
| 30 | # this file the order of those calls and the refusals between them |
| 31 | # This file is shell: unfinished migration, not the destination. Its decisions (what verifies, replay, |
| 32 | # fast-forward, the order of a landing and its recovery) move into Omega before the route is offered |
| 33 | # for publishing. Host tools: bash, git, ssh-keygen, awk, sed, cmp (diffutils), getconf (libc), flock |
| 34 | # (util-linux), and from coreutils sha256sum, sync, realpath, od, env, mktemp, cut, tr, head, tail, |
| 35 | # wc, cp, mv, rm, mkdir, chmod, cat, basename, dirname. The gate prints the versions it ran with. The |
| 36 | # two rule programs are the ones beside this file; no setting replaces them. |
| 37 | # |
| 38 | # A landing, in order: every argument is checked; the key is shown to sign as the project's owner; |
| 39 | # what an interruption left is put right; the project must verify; the payload and the owner's |
| 40 | # signature over it are written to staging, synced, and the signature is verified; only then the |
| 41 | # record and its SIG record are appended and the branch is moved forward. What an interruption can |
| 42 | # leave, and what happens then (always: only for the owner's key, said on stderr as "repaired:", and |
| 43 | # only the same request finishes an unfinished one): |
| 44 | # a torn last line of the chain (never a record) -> cut off |
| 45 | # an empty chain file from an init that never wrote -> removed, init goes on |
| 46 | # a record whose payload is still only in staging -> the payload is put in place |
| 47 | # PENDING_SIG the last record has no SIG yet -> it is signed, then the branch moves |
| 48 | # PENDING_REF the last landing is signed, the branch still at its old head -> the branch moves |
| 49 | # Anything else that does not verify is refused whole; this tool does not repair it. |
| 50 | # Exit 0 done; 1 usage; 2 an argument, file or key was refused; 3 the project does not verify, or has |
| 51 | # an unfinished request that is not this one; 4 refused: not a fast-forward, head already there, not |
| 52 | # a commit the store can find, branch name taken, or the request id was already used for something |
| 53 | # else; 5 a tool failed part-way: run the same request again, it finishes or refuses. On 1 to 4 |
| 54 | # nothing of the request is in the chain, the records or the branches; a repair made first for the |
| 55 | # owner stays, and a refused landing may have copied Git objects no branch points at. |
| 56 | # IPL_TEST_STOP=after-stage|mid-record|after-record|after-sig|after-ref makes a landing stop there |
| 57 | # with exit 9 (an init has the first four points; it moves no branch). It exists for the gate's |
| 58 | # interruption cases and can only stop the program early. |
| 59 | set -u |
| 60 | here=$(cd "$(dirname "$0")" && pwd) |
| 61 | CHAIN=$here/bin/chain |
| 62 | LANDREC=$here/bin/landrec |
| 63 | ZERO=0000000000000000000000000000000000000000 |
| 64 | PATH=/usr/bin:/bin |
| 65 | die() { echo "ipl: $2" >&2; exit "$1"; } |
| 66 | [ -x "$CHAIN" ] && [ -x "$LANDREC" ] || die 5 "bin/chain and bin/landrec are not built (build.sh)" |
| 67 | # git sees no caller environment, no user or system configuration, no hooks, no replace objects, |
| 68 | # and only the local file transport |
| 69 | g() { |
| 70 | env -i PATH=/usr/bin:/bin HOME=/nonexistent GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_NOSYSTEM=1 \ |
| 71 | GIT_NO_REPLACE_OBJECTS=1 GIT_TERMINAL_PROMPT=0 git -c core.hooksPath=/dev/null -c protocol.allow=never -c protocol.file.allow=always \ |
| 72 | --git-dir="$P/repo.git" "$@" |
| 73 | } |
| 74 | sha() { sha256sum < "$1" | cut -c1-64; } |
| 75 | pfield() { sed -n "s/^$1 //p" "$2"; } |
| 76 | stop_at() { [ "${IPL_TEST_STOP:-}" != "$1" ] || exit 9; } |
| 77 | sigmsg() { printf 'interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n' "$1" "$2" "$3" "$4" "$5"; } |
| 78 | # the message the signature of chain line $1 must cover: everything but seq and time; prev fixes the position |
| 79 | linemsg() { local s u k a sj p pv; IFS=$'\t' read -r s u k a sj p pv <<< "$1"; sigmsg "$k" "$a" "$sj" "$p" "$pv"; } |
| 80 | sigok() { # MSG_FILE SIG_FILE [PUB_FILE]: a valid signature by that public key (owner.pub if omitted), purpose interplane |
| 81 | local al rc pub=${3:-$P/owner.pub} |
| 82 | al=$(mktemp) || return 1 |
| 83 | printf 'owner %s\n' "$(cut -d' ' -f1,2 "$pub")" > "$al" |
| 84 | ssh-keygen -q -Y verify -f "$al" -I owner -n interplane -s "$2" < "$1" >/dev/null 2>&1; rc=$? |
| 85 | rm -f "$al"; return $rc |
| 86 | } |
| 87 | pubblob() { # PUB_FILE: the ssh-ed25519 blob (field 2), or fail |
| 88 | local t b; t=$(head -n 1 "$1") |
| 89 | [ "${t%% *}" = ssh-ed25519 ] || return 1 |
| 90 | b=${t#ssh-ed25519 }; b=${b%% *} |
| 91 | [ ${#b} = 68 ] || return 1 |
| 92 | printf '%s' "$b" |
| 93 | } |
| 94 | write_ssh_pub() { # DEST BLOB: one ssh-ed25519 public key line, no comment |
| 95 | printf 'ssh-ed25519 %s\n' "$2" > "$1" |
| 96 | } |
| 97 | ensure_vcurpub() { |
| 98 | if [ -z "${VCURPUB:-}" ] || [ ! -e "$VCURPUB" ]; then |
| 99 | VCURPUB=$(mktemp) || return 1 |
| 100 | fi |
| 101 | } |
| 102 | cleanup_temps() { rm -f "${VCURPUB:-}" "${VCURPUB:-}.pending"; } |
| 103 | trap cleanup_temps EXIT |
| 104 | reset_owners() { declare -gA OWNERS=(); } |
| 105 | note_owner() { OWNERS[$1]=1; } |
| 106 | is_prior_owner() { [ -n "${OWNERS[$1]:-}" ]; } |
| 107 | # apply_successor PAYLOAD DEST: PAYLOAD is a canonical SUCCESSOR for $NAME whose old key is $CURRENT. |
| 108 | # Writes the successor public key to DEST and sets PENDING_NEW to its fingerprint. |
| 109 | apply_successor() { |
| 110 | local f=$1 dest=$2 old new blob reason np fp |
| 111 | old=$(pfield old "$f"); new=$(pfield new "$f"); blob=$(pfield pub "$f"); reason=$(pfield reason "$f") |
| 112 | [ -n "$reason" ] || reason=- |
| 113 | "$LANDREC" successor "$(pfield project "$f")" "$old" "$new" "$blob" "$reason" 2>/dev/null | cmp -s - "$f" || return 1 |
| 114 | [ "$(pfield project "$f")" = "$NAME" ] || return 1 |
| 115 | [ "$old" = "$CURRENT" ] && [ "$new" != "$old" ] || return 1 |
| 116 | is_prior_owner "$new" && return 1 |
| 117 | np=$(mktemp) || return 1 |
| 118 | write_ssh_pub "$np" "$blob" || { rm -f "$np"; return 1; } |
| 119 | fp=$(ed25519_fp "$np") || { rm -f "$np"; return 1; } |
| 120 | [ "$fp" = "$new" ] || { rm -f "$np"; return 1; } |
| 121 | mv "$np" "$dest" || return 1 |
| 122 | PENDING_NEW=$new |
| 123 | return 0 |
| 124 | } |
| 125 | # payload_for SEQ: records/SEQ, or a staging file whose digest is the one the chain line commits to |
| 126 | payload_for() { |
| 127 | local i=$1 want f |
| 128 | if [ -f "$P/records/$i" ]; then printf '%s' "$P/records/$i"; return 0; fi |
| 129 | want=$(sed -n "${i}p" "$P/chain.tsv" | cut -f6) |
| 130 | [ -n "$want" ] || return 1 |
| 131 | for f in "$P"/staging/*; do |
| 132 | if [ -f "$f" ] && [ "$(sha "$f")" = "$want" ]; then printf '%s' "$f"; return 0; fi |
| 133 | done |
| 134 | return 1 |
| 135 | } |
| 136 | # load_current: set CURRENT and VCURPUB from owner.pub and every complete SUCCESSOR+SIG pair. |
| 137 | # A torn last line is ignored (it was never a record). Fails closed if the current key is ambiguous. |
| 138 | # Each line's prev must commit to the line before it (GENESIS for record 1), so prove_owner/mend |
| 139 | # do not run on a spliced chain. |
| 140 | load_current() { |
| 141 | local lines nl i s u k a sj p pv f pend="" tmp want |
| 142 | CURRENT="" |
| 143 | reset_owners |
| 144 | ensure_vcurpub || { VMSG="mktemp"; return 1; } |
| 145 | rm -f "$VCURPUB.pending" |
| 146 | [ -f "$P/owner.pub" ] || { VMSG="no owner.pub"; return 1; } |
| 147 | CURRENT=$(ed25519_fp "$P/owner.pub") || { VMSG="owner.pub is not one Ed25519 public key"; return 1; } |
| 148 | cp "$P/owner.pub" "$VCURPUB" || { VMSG="cannot read owner.pub"; return 1; } |
| 149 | note_owner "$CURRENT" |
| 150 | [ -s "$P/chain.tsv" ] || return 0 |
| 151 | mapfile -t lines < "$P/chain.tsv" || { VMSG="cannot read the chain"; return 1; } |
| 152 | # a torn last line (no terminating line feed) was never a record |
| 153 | if [ "$(tail -c 1 "$P/chain.tsv" | od -An -tx1 | tr -d ' ')" != 0a ] && [ ${#lines[@]} -gt 0 ]; then |
| 154 | unset "lines[$((${#lines[@]} - 1))]" |
| 155 | fi |
| 156 | nl=${#lines[@]} |
| 157 | if [ -f "$P/records/1" ]; then |
| 158 | [ "$(pfield owner "$P/records/1")" = "$CURRENT" ] || { VMSG="owner.pub is not the Ed25519 key record 1 names"; return 1; } |
| 159 | fi |
| 160 | for ((i = 1; i <= nl; i++)); do |
| 161 | IFS=$'\t' read -r s u k a sj p pv <<< "${lines[i - 1]}" |
| 162 | if [ "$i" = 1 ]; then |
| 163 | [ "$pv" = GENESIS ] || { VMSG="record 1 prev is not GENESIS"; return 1; } |
| 164 | else |
| 165 | want=$(printf '%s' "${lines[i - 2]}" | sha256sum | cut -c1-64) |
| 166 | [ "$pv" = "$want" ] || { VMSG="record $i does not follow the line before it"; return 1; } |
| 167 | fi |
| 168 | [ "$a" = "$CURRENT" ] || { VMSG="record $i was written by $a, not the current owner"; return 1; } |
| 169 | # only SUCCESSOR records are followed here; PROJECT/IMPORT/LAND signatures are |
| 170 | # checked by vcore. A SUCCESSOR not followed by SIG (except as the last record) is ambiguous. |
| 171 | if [ -n "$pend" ] && [ "$k" != SIG ]; then VMSG="record $pend is not followed by its signature"; return 1; fi |
| 172 | case $k in |
| 173 | SUCCESSOR) |
| 174 | [ "$i" != 1 ] || { VMSG="SUCCESSOR record $i is out of place"; return 1; } |
| 175 | pend=$i |
| 176 | if [ "$i" = "$nl" ]; then continue; fi |
| 177 | f=$(payload_for "$i") || { VMSG="payload of record $i is missing"; return 1; } |
| 178 | if is_prior_owner "$(pfield new "$f")"; then VMSG="record $i names a key that was already an owner"; return 1; fi |
| 179 | apply_successor "$f" "$VCURPUB.pending" || { VMSG="record $i is not a canonical successor from the current key"; return 1; } |
| 180 | [ "$sj" = "$(subj "$PENDING_NEW")" ] || { VMSG="record $i does not name its successor in the subject"; return 1; } |
| 181 | ;; |
| 182 | SIG) |
| 183 | if [ -n "$pend" ]; then |
| 184 | [ "$pend" = $((i - 1)) ] || { VMSG="record $i signs nothing"; return 1; } |
| 185 | if [ -f "$VCURPUB.pending" ]; then |
| 186 | f=$(payload_for "$i") || { VMSG="payload of record $i is missing"; return 1; } |
| 187 | tmp=$(mktemp) || { VMSG="mktemp"; return 1; } |
| 188 | linemsg "${lines[i - 2]}" > "$tmp" |
| 189 | if ! sigok "$tmp" "$f" "$VCURPUB"; then rm -f "$tmp"; VMSG="the signature in record $i does not verify for record $pend"; return 1; fi |
| 190 | rm -f "$tmp" |
| 191 | CURRENT=$PENDING_NEW |
| 192 | note_owner "$CURRENT" |
| 193 | mv "$VCURPUB.pending" "$VCURPUB" || return 1 |
| 194 | fi |
| 195 | pend="" |
| 196 | fi |
| 197 | ;; |
| 198 | esac |
| 199 | done |
| 200 | rm -f "$VCURPUB.pending" |
| 201 | return 0 |
| 202 | } |
| 203 | ed25519_fp() { # PUB_FILE: prints the fingerprint only for one Ed25519 key |
| 204 | local l; l=$(ssh-keygen -lf "$1" 2>/dev/null) || return 1 |
| 205 | [ "$(printf '%s\n' "$l" | wc -l)" = 1 ] && [[ $l == *"(ED25519)" ]] && [[ $(head -c 12 "$1") == "ssh-ed25519 " ]] || return 1 |
| 206 | printf '%s' "$l" | awk '{print $2}' |
| 207 | } |
| 208 | export LC_ALL=C |
| 209 | # subj: the chain's subject field holds at most 128 bytes (a cap of the chain format); a longer name or |
| 210 | # branch is written there as the digest of its bytes, so this store adds no length rule of its own |
| 211 | subj() { if [ "${#1}" -le 128 ]; then printf '%s' "$1"; else printf 'sha256:%s' "$(printf '%s' "$1" | sha256sum | cut -c1-64)"; fi; } |
| 212 | admit() { # sets P; a project folder and its repository are real directories, not links |
| 213 | P=$PR/$NAME |
| 214 | "$LANDREC" check name "$NAME" && [ -d "$P" ] && [ ! -L "$P" ] && [ -d "$P/repo.git" ] && [ ! -L "$P/repo.git" ] |
| 215 | } |
| 216 | open_project() { # ROOT NAME: nothing outside ROOT/projects |
| 217 | "$LANDREC" check name "$2" || die 2 "REFUSED name" |
| 218 | PR=$(realpath -e -- "$1/projects" 2>/dev/null) || die 2 "no store at $1" |
| 219 | ROOT=${PR%/projects}; NAME=$2 |
| 220 | [ "${#NAME}" -le "$(getconf NAME_MAX "$PR")" ] || die 2 "REFUSED name: longer than a file name can be on this file system ($(getconf NAME_MAX "$PR") bytes)" |
| 221 | admit || die 2 "no project $2 in the store" |
| 222 | } |
| 223 | key_actor() { # KEY: ACTOR is the fingerprint of an Ed25519 key pair that is present |
| 224 | [ -f "$1" ] && [ -f "$1.pub" ] || die 2 "key $1 (and $1.pub) not found" |
| 225 | ACTOR=$(ed25519_fp "$1.pub") || die 2 "$1.pub is not one Ed25519 public key" |
| 226 | } |
| 227 | # prove_owner KEY: the private key signs, here and now, as the project's current owner key |
| 228 | # (owner.pub, or the successor named by the last complete SUCCESSOR+SIG pair). Nothing is changed |
| 229 | # for a caller who cannot do that. |
| 230 | prove_owner() { |
| 231 | local d rc |
| 232 | load_current || die 3 "cannot determine the current owner key: $VMSG" |
| 233 | [ "$ACTOR" = "$CURRENT" ] || die 2 "key $ACTOR is not this project's current owner (agent grants are step 3)" |
| 234 | d=$(mktemp -d) || die 5 mktemp |
| 235 | printf 'interplane-proof 1\n%s\n' "$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" > "$d/m" |
| 236 | ssh-keygen -q -Y sign -n interplane -f "$1" "$d/m" >/dev/null 2>&1 && sigok "$d/m" "$d/m.sig" "$VCURPUB"; rc=$? |
| 237 | rm -rf "$d" |
| 238 | [ "$rc" = 0 ] || die 2 "that key does not sign as the project's current owner" |
| 239 | } |
| 240 | # lock_fd: fd 9, or ALL_LOCK_FD when --all is holding every project's lock across preflight and write |
| 241 | lock_fd() { if [ -n "${ALL_LOCK_FD:-}" ]; then printf '%s' "$ALL_LOCK_FD"; else printf 9; fi; } |
| 242 | lock() { |
| 243 | local fd; fd=$(lock_fd) |
| 244 | if [ -z "${ALL_LOCK_FD:-}" ]; then |
| 245 | exec 9>> "$P/lock" || die 5 "lock" |
| 246 | fi |
| 247 | flock "$1" "$fd" || die 5 "lock" |
| 248 | } |
| 249 | unlock() { flock -u "$(lock_fd)"; } |
| 250 | durable() { sync "$@" || die 5 "sync failed: $*"; } |
| 251 | # vcore: 0 verifies; 10 PENDING_SIG; 11 PENDING_REF; 3 does not verify (VMSG says why). |
| 252 | # Sets N, LINES, OWNER, DEFBR, VIS, HEADS[branch], REQAT[request]=seq, TAILOLD. |
| 253 | vcore() { |
| 254 | local out i s u k a sj p pv pend="" f br old new req t ref mism=0 mbr="" |
| 255 | VMSG=""; N=0; declare -gA HEADS=() REQAT=(); TAILOLD=""; TAILBR="" |
| 256 | [ -s "$P/chain.tsv" ] || { VMSG="no chain"; return 3; } |
| 257 | out=$("$CHAIN" verify "$P/chain.tsv" 2>&1) || { VMSG="chain: $out"; return 3; } |
| 258 | mapfile -t LINES < "$P/chain.tsv"; N=${#LINES[@]} |
| 259 | f=$P/records/1 |
| 260 | [ -f "$f" ] && [ "$(sha "$f")" = "$(cut -f6 <<< "${LINES[0]}")" ] || { VMSG="payload of record 1 is missing or not the one committed"; return 3; } |
| 261 | "$LANDREC" project "$(pfield project "$f")" "$(pfield branch "$f")" "$(pfield visibility "$f")" "$(pfield owner "$f")" "$(pfield alias "$f")" "$(pfield nonce "$f")" 2>/dev/null | cmp -s - "$f" || |
| 262 | { VMSG="record 1 is not a canonical project record"; return 3; } |
| 263 | OWNER=$(pfield owner "$f"); DEFBR=$(pfield branch "$f"); VIS=$(pfield visibility "$f") |
| 264 | [ "$(pfield project "$f")" = "$NAME" ] || { VMSG="record 1 is for project $(pfield project "$f"), not $NAME"; return 3; } |
| 265 | [ -f "$P/owner.pub" ] && [ "$(ed25519_fp "$P/owner.pub")" = "$OWNER" ] || { VMSG="owner.pub is not the Ed25519 key record 1 names"; return 3; } |
| 266 | CURRENT=$OWNER |
| 267 | reset_owners |
| 268 | note_owner "$CURRENT" |
| 269 | ensure_vcurpub || { VMSG="mktemp"; return 3; } |
| 270 | cp "$P/owner.pub" "$VCURPUB" || { VMSG="cannot read owner.pub"; return 3; } |
| 271 | rm -f "$VCURPUB.pending" |
| 272 | t=$(mktemp) || { VMSG="mktemp"; return 3; } |
| 273 | for ((i = 1; i <= N; i++)); do |
| 274 | IFS=$'\t' read -r s u k a sj p pv <<< "${LINES[i - 1]}"; f=$P/records/$i |
| 275 | [ -f "$f" ] && [ "$(sha "$f")" = "$p" ] || { VMSG="payload of record $i is missing or not the one committed"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 276 | [ "$a" = "$CURRENT" ] || { VMSG="record $i was written by $a, not the current owner"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 277 | if [ -n "$pend" ] && [ "$k" != SIG ]; then VMSG="record $pend is not followed by its signature"; rm -f "$t" "$VCURPUB.pending"; return 3; fi |
| 278 | case $k in |
| 279 | PROJECT) |
| 280 | [ "$i" = 1 ] && [ "$sj" = "$(subj "$NAME")" ] || { VMSG="PROJECT record $i is out of place"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 281 | pend=$i ;; |
| 282 | SUCCESSOR) |
| 283 | [ "$i" != 1 ] || { VMSG="SUCCESSOR record $i is out of place"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 284 | if is_prior_owner "$(pfield new "$f")"; then VMSG="record $i names a key that was already an owner"; rm -f "$t" "$VCURPUB.pending"; return 3; fi |
| 285 | apply_successor "$f" "$VCURPUB.pending" || { VMSG="record $i is not a canonical successor from the current key"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 286 | [ "$sj" = "$(subj "$PENDING_NEW")" ] || { VMSG="record $i does not name its successor in the subject"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 287 | pend=$i ;; |
| 288 | IMPORT | LAND) |
| 289 | br=$(pfield branch "$f"); old=$(pfield old "$f"); new=$(pfield new "$f"); req=$(pfield request "$f") |
| 290 | "$LANDREC" land "$(pfield project "$f")" "$br" "$old" "$new" "$(pfield receipt "$f")" "$(pfield verdict "$f")" "$req" 2>/dev/null | cmp -s - "$f" || |
| 291 | { VMSG="record $i is not a canonical land record"; rm -f "$t"; return 3; } |
| 292 | [ "$(pfield project "$f")" = "$NAME" ] && [ "$sj" = "$(subj "$br")" ] || { VMSG="record $i does not belong to $NAME $br"; rm -f "$t"; return 3; } |
| 293 | [ -z "${REQAT[$req]:-}" ] || { VMSG="request $req is recorded twice ($((REQAT[$req])) and $i)"; rm -f "$t"; return 3; } |
| 294 | [ "$old" = "${HEADS[$br]:-$ZERO}" ] && [ "$new" != "$old" ] || { VMSG="record $i does not start from the recorded head of $br"; rm -f "$t"; return 3; } |
| 295 | [ "$k" = LAND ] || [ "$old" = $ZERO ] || { VMSG="IMPORT record $i on a branch that already has a head"; rm -f "$t"; return 3; } |
| 296 | [ "$(g cat-file -t "$new" 2>/dev/null)" = commit ] || { VMSG="record $i names a head that is not a commit the store holds"; rm -f "$t"; return 3; } |
| 297 | [ "$old" = $ZERO ] || g merge-base --is-ancestor "$old" "$new" 2>/dev/null || { VMSG="record $i is not a fast-forward of the head before it"; rm -f "$t"; return 3; } |
| 298 | REQAT[$req]=$i; HEADS[$br]=$new; pend=$i; TAILOLD=$old; TAILBR=$br ;; |
| 299 | SIG) |
| 300 | [ -n "$pend" ] && [ "$pend" = $((i - 1)) ] && [ "$sj" = "$pend" ] || { VMSG="record $i signs nothing"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 301 | linemsg "${LINES[i - 2]}" > "$t" |
| 302 | sigok "$t" "$f" "$VCURPUB" || { VMSG="the signature in record $i does not verify for record $pend"; rm -f "$t" "$VCURPUB.pending"; return 3; } |
| 303 | if [ -f "$VCURPUB.pending" ]; then |
| 304 | CURRENT=$PENDING_NEW |
| 305 | note_owner "$CURRENT" |
| 306 | mv "$VCURPUB.pending" "$VCURPUB" || { rm -f "$t"; return 3; } |
| 307 | fi |
| 308 | pend="" ;; |
| 309 | *) VMSG="record $i has unknown kind $k"; rm -f "$t" "$VCURPUB.pending"; return 3 ;; |
| 310 | esac |
| 311 | done |
| 312 | rm -f "$t" |
| 313 | for br in "${!HEADS[@]}"; do |
| 314 | ref=$(g rev-parse -q --verify "refs/heads/$br" 2>/dev/null) || ref=$ZERO |
| 315 | [ "$ref" = "${HEADS[$br]}" ] || { mism=$((mism + 1)); mbr=$br; } |
| 316 | done |
| 317 | IFS=$'\t' read -r s u k a sj p pv <<< "${LINES[N - 1]}" |
| 318 | if [ -n "$pend" ]; then |
| 319 | # an unsigned last record: its own branch has not moved yet, every other branch is in place |
| 320 | if [ "$k" = PROJECT ] && [ "$mism" = 0 ]; then VMSG="PENDING_SIG record $N (PROJECT) is not signed yet"; return 10; fi |
| 321 | if [ "$k" = SUCCESSOR ] && [ "$mism" = 0 ]; then VMSG="PENDING_SIG record $N (SUCCESSOR) is not signed yet"; return 10; fi |
| 322 | ref=$(g rev-parse -q --verify "refs/heads/${TAILBR:-}" 2>/dev/null) || ref=$ZERO |
| 323 | if [ "$k" = LAND ] && [ "$mism" = 1 ] && [ "$mbr" = "$TAILBR" ] && [ "$ref" = "$TAILOLD" ]; then VMSG="PENDING_SIG record $N (LAND $TAILBR) is not signed yet"; return 10; fi |
| 324 | if [ "$k" = IMPORT ] && [ "$mism" = 0 ]; then VMSG="PENDING_SIG record $N (IMPORT $TAILBR) is not signed yet"; return 10; fi |
| 325 | VMSG="record $N is not signed and the branches are not where the chain says"; return 3 |
| 326 | fi |
| 327 | [ "$mism" = 0 ] && return 0 |
| 328 | if [ "$N" -ge 2 ]; then |
| 329 | IFS=$'\t' read -r s u k a sj p pv <<< "${LINES[N - 2]}" |
| 330 | ref=$(g rev-parse -q --verify "refs/heads/${TAILBR:-}" 2>/dev/null) || ref=$ZERO |
| 331 | if [ "$k" = LAND ] && [ "$mism" = 1 ] && [ "$mbr" = "$TAILBR" ] && [ "$ref" = "$TAILOLD" ]; then VMSG="PENDING_REF $TAILBR is still at ${TAILOLD}, the chain says ${HEADS[$TAILBR]}"; return 11; fi |
| 332 | fi |
| 333 | VMSG="refs/heads/$mbr is not where the chain says (${HEADS[$mbr]})"; return 3 |
| 334 | } |
| 335 | # chain_head: SHA-256 of the last chain line with no line feed (the prev a next record would commit to) |
| 336 | chain_head() { printf '%s' "${LINES[N - 1]}" | sha256sum | cut -c1-64; } |
| 337 | # OK NAME ID BRANCH HEAD VISIBILITY CURRENT_OWNER SEQ CHAIN_HEAD |
| 338 | okline() { echo "OK $NAME $(sha "$P/records/1") $DEFBR ${HEADS[$DEFBR]:-none} $VIS $CURRENT $N $(chain_head)"; } |
| 339 | # The operator pin lives outside the store. load_owner_pins requires ROOT, fails closed |
| 340 | # (exit 3) if the file is missing, inside ROOT, or group-/world-accessible, and parses |
| 341 | # NAME FP and * FP lines. pin_for NAME sets PIN_FP. |
| 342 | load_owner_pins() { |
| 343 | local raw path mode root_abs pin_abs given line name fp rest |
| 344 | raw=${IPL_OWNER_PINS:-"$HOME/.config/interplane/owner-pins"} |
| 345 | [ -n "${ROOT:-}" ] || die 5 "owner pin: store root is not set" |
| 346 | [ -e "$raw" ] || die 3 "owner pin file not found: $raw" |
| 347 | pin_abs=$(realpath -e -- "$raw") || die 3 "owner pin file not found: $raw" |
| 348 | [ -f "$pin_abs" ] || die 3 "owner pin file is not a regular file: $raw" |
| 349 | root_abs=$(realpath -e -- "$ROOT") || die 2 "no store at $ROOT" |
| 350 | given=$(realpath -s -- "$raw" 2>/dev/null || printf '%s' "$pin_abs") |
| 351 | case $pin_abs in "$root_abs"|"$root_abs"/*) die 3 "owner pin file is inside the store: $pin_abs" ;; esac |
| 352 | case $given in "$root_abs"|"$root_abs"/*) die 3 "owner pin file is inside the store: $given" ;; esac |
| 353 | mode=$(stat -c '%a' "$pin_abs") || die 3 "cannot stat owner pin file" |
| 354 | case $mode in |
| 355 | 600|400) ;; |
| 356 | *) die 3 "owner pin file must be mode 0600 (not group- or world-accessible): $pin_abs is $mode" ;; |
| 357 | esac |
| 358 | [ "$(stat -c '%u' "$pin_abs")" = "$(id -u)" ] || die 3 "owner pin file is not owned by this user" |
| 359 | local parent pmode |
| 360 | parent=$(dirname "$pin_abs") |
| 361 | pmode=$(stat -c '%a' "$parent") || die 3 "cannot stat owner pin directory" |
| 362 | if [ $((8#$pmode & 8#022)) -ne 0 ]; then |
| 363 | die 3 "owner pin directory must not be group- or world-writable: $parent is $pmode" |
| 364 | fi |
| 365 | PIN_PATH=$pin_abs |
| 366 | PIN_STAR="" |
| 367 | declare -gA PIN_BY_NAME=() |
| 368 | while IFS= read -r line || [ -n "$line" ]; do |
| 369 | [[ $line =~ ^[[:space:]]*$ || $line =~ ^[[:space:]]*# ]] && continue |
| 370 | read -r name fp rest <<< "$line" |
| 371 | [ -n "$name" ] && [ -n "$fp" ] && [ -z "$rest" ] || die 3 "owner pin file has a malformed line" |
| 372 | "$LANDREC" check owner "$fp" || die 3 "owner pin file names a fingerprint that is not an owner key" |
| 373 | if [ "$name" = '*' ]; then |
| 374 | [ -z "$PIN_STAR" ] || die 3 "owner pin file names * more than once" |
| 375 | PIN_STAR=$fp |
| 376 | else |
| 377 | "$LANDREC" check name "$name" || die 3 "owner pin file names a project that is not a name" |
| 378 | [ -z "${PIN_BY_NAME[$name]:-}" ] || die 3 "owner pin file names $name more than once" |
| 379 | PIN_BY_NAME[$name]=$fp |
| 380 | fi |
| 381 | done < "$pin_abs" |
| 382 | [ -n "$PIN_STAR" ] || [ ${#PIN_BY_NAME[@]} -gt 0 ] || die 3 "owner pin file is empty" |
| 383 | } |
| 384 | pin_for() { |
| 385 | if [ -n "${PIN_BY_NAME[$1]:-}" ]; then PIN_FP=${PIN_BY_NAME[$1]}; return 0; fi |
| 386 | if [ -n "${PIN_STAR:-}" ]; then PIN_FP=$PIN_STAR; return 0; fi |
| 387 | return 1 |
| 388 | } |
| 389 | require_expect_owner() { |
| 390 | pin_for "$NAME" || die 3 "owner pin file has no line for $NAME" |
| 391 | if [ -n "${EXPECT_OWNER:-}" ] && [ "$EXPECT_OWNER" != "$PIN_FP" ]; then |
| 392 | die 3 "current owner pin is $PIN_FP, not $EXPECT_OWNER" |
| 393 | fi |
| 394 | [ "$CURRENT" = "$PIN_FP" ] || die 3 "current owner is $CURRENT, not $PIN_FP" |
| 395 | } |
| 396 | # announce_pin / update_owner_pin: after a signed SUCCESSOR is on the chain. Never on a no-op, |
| 397 | # and never from a fingerprint read out of the store: NEWFP is the key the operator just signed over. |
| 398 | announce_pin() { echo "ipl: pin: $1 $2" >&2; } |
| 399 | # update_owner_pin NAME NEWFP: rewrite or append the NAME line only. Does not touch *. |
| 400 | # update_owner_pin_all OLDFP NEWFP names...: rewrite each named project's line to NEWFP; |
| 401 | # rewrite * only if it named OLDFP; if there is no * line, append * NEWFP. |
| 402 | update_owner_pin() { |
| 403 | local name=$1 fp=$2 tmp dir line nname nfp rest found=0 |
| 404 | dir=$(dirname "$PIN_PATH") |
| 405 | tmp=$(mktemp "$dir/owner-pins.XXXXXX") || die 5 "cannot update owner pin" |
| 406 | chmod 600 "$tmp" || { rm -f "$tmp"; die 5 "cannot update owner pin"; } |
| 407 | while IFS= read -r line || [ -n "$line" ]; do |
| 408 | if [[ $line =~ ^[[:space:]]*$ || $line =~ ^[[:space:]]*# ]]; then |
| 409 | printf '%s\n' "$line" >> "$tmp" |
| 410 | continue |
| 411 | fi |
| 412 | read -r nname nfp rest <<< "$line" |
| 413 | if [ "$nname" = "$name" ]; then |
| 414 | printf '%s %s\n' "$name" "$fp" >> "$tmp"; found=1 |
| 415 | else |
| 416 | printf '%s\n' "$line" >> "$tmp" |
| 417 | fi |
| 418 | done < "$PIN_PATH" |
| 419 | if [ "$found" = 0 ]; then |
| 420 | printf '%s %s\n' "$name" "$fp" >> "$tmp" |
| 421 | fi |
| 422 | sync "$tmp" && mv -f "$tmp" "$PIN_PATH" && chmod 600 "$PIN_PATH" && sync "$dir" || { rm -f "$tmp"; die 5 "recorded, but the owner pin could not be updated"; } |
| 423 | } |
| 424 | update_owner_pin_all() { |
| 425 | local oldfp=$1 newfp=$2 tmp dir line nname nfp rest star=0 |
| 426 | shift 2 |
| 427 | local -A rotate=() seen=() |
| 428 | local n |
| 429 | for n in "$@"; do rotate[$n]=1; done |
| 430 | dir=$(dirname "$PIN_PATH") |
| 431 | tmp=$(mktemp "$dir/owner-pins.XXXXXX") || die 5 "cannot update owner pin" |
| 432 | chmod 600 "$tmp" || { rm -f "$tmp"; die 5 "cannot update owner pin"; } |
| 433 | while IFS= read -r line || [ -n "$line" ]; do |
| 434 | if [[ $line =~ ^[[:space:]]*$ || $line =~ ^[[:space:]]*# ]]; then |
| 435 | printf '%s\n' "$line" >> "$tmp" |
| 436 | continue |
| 437 | fi |
| 438 | read -r nname nfp rest <<< "$line" |
| 439 | if [ "$nname" = '*' ]; then |
| 440 | if [ "$nfp" = "$oldfp" ]; then |
| 441 | printf '* %s\n' "$newfp" >> "$tmp" |
| 442 | else |
| 443 | printf '%s\n' "$line" >> "$tmp" |
| 444 | fi |
| 445 | star=1 |
| 446 | elif [ -n "${rotate[$nname]:-}" ]; then |
| 447 | printf '%s %s\n' "$nname" "$newfp" >> "$tmp" |
| 448 | seen[$nname]=1 |
| 449 | else |
| 450 | printf '%s\n' "$line" >> "$tmp" |
| 451 | fi |
| 452 | done < "$PIN_PATH" |
| 453 | for n in "$@"; do |
| 454 | [ -n "${seen[$n]:-}" ] || printf '%s %s\n' "$n" "$newfp" >> "$tmp" |
| 455 | done |
| 456 | if [ "$star" = 0 ]; then |
| 457 | printf '* %s\n' "$newfp" >> "$tmp" |
| 458 | fi |
| 459 | sync "$tmp" && mv -f "$tmp" "$PIN_PATH" && chmod 600 "$PIN_PATH" && sync "$dir" || { rm -f "$tmp"; die 5 "recorded, but the owner pin could not be updated"; } |
| 460 | } |
| 461 | finish_handover_pin() { # NEWFP: after this process appended a signed SUCCESSOR (not a no-op) |
| 462 | announce_pin "$NAME" "$1" |
| 463 | if [ -z "${HANDOVER_DEFER_PIN:-}" ]; then |
| 464 | update_owner_pin "$NAME" "$1" |
| 465 | fi |
| 466 | } |
| 467 | |
| 468 | # mend: called only after prove_owner. Cuts a torn last line (bytes after the last line feed were |
| 469 | # never a record) and puts a staged payload in place for a record that lacks its file. |
| 470 | mend() { |
| 471 | local n i want f keep |
| 472 | [ -f "$P/chain.tsv" ] || return 0 |
| 473 | if [ -s "$P/chain.tsv" ] && [ "$(tail -c 1 "$P/chain.tsv" | od -An -tx1 | tr -d ' ')" != 0a ]; then |
| 474 | keep=$(wc -l < "$P/chain.tsv") |
| 475 | head -n "$keep" "$P/chain.tsv" > "$P/chain.tsv.mend" && durable "$P/chain.tsv.mend" && mv "$P/chain.tsv.mend" "$P/chain.tsv" && durable "$P" || die 5 "cannot cut the torn line" |
| 476 | echo "ipl: repaired: cut a torn last line off the chain" >&2 |
| 477 | fi |
| 478 | n=$(wc -l < "$P/chain.tsv") |
| 479 | for ((i = n; i >= 1 && i > n - 2; i--)); do |
| 480 | [ -f "$P/records/$i" ] && continue |
| 481 | want=$(sed -n "${i}p" "$P/chain.tsv" | cut -f6) |
| 482 | for f in "$P"/staging/*; do |
| 483 | if [ -f "$f" ] && [ "$(sha "$f")" = "$want" ]; then |
| 484 | cp "$f" "$P/records/$i.tmp" && mv "$P/records/$i.tmp" "$P/records/$i" || die 5 "cannot restore payload $i" |
| 485 | durable "$P/records/$i" "$P/records"; echo "ipl: repaired: put the staged payload of record $i in place" >&2; break |
| 486 | fi |
| 487 | done |
| 488 | done |
| 489 | } |
| 490 | # add KIND SUBJECT STAGED_FILE: append one record whose payload is already durable in staging |
| 491 | add() { |
| 492 | local out seq |
| 493 | out=$("$CHAIN" add "$P/chain.tsv" "$1" "$ACTOR" "$2" < "$3") || die 5 "chain add failed: $out" |
| 494 | seq=$(cut -f2 <<< "$out"); [[ $seq =~ ^[1-9][0-9]*$ ]] || die 5 "chain add answered: $out" |
| 495 | [ "$seq" != 1 ] || durable "$P" |
| 496 | [ "$1" = SIG ] || stop_at mid-record |
| 497 | cp "$3" "$P/records/$seq.tmp" && mv "$P/records/$seq.tmp" "$P/records/$seq" || die 5 "cannot keep payload $seq" |
| 498 | durable "$P/records/$seq" "$P/records" |
| 499 | SEQ=$seq |
| 500 | } |
| 501 | # sign_tail KEY TAG: sign the unsigned last record and append its SIG |
| 502 | sign_tail() { |
| 503 | local m=$P/staging/$2.msg |
| 504 | mapfile -t LINES < "$P/chain.tsv"; N=${#LINES[@]} |
| 505 | linemsg "${LINES[N - 1]}" > "$m" || die 5 "staging" |
| 506 | rm -f "$m.sig"; ssh-keygen -q -Y sign -n interplane -f "$1" "$m" >/dev/null 2>&1 || die 2 "signing with $1 failed" |
| 507 | sigok "$m" "$m.sig" "${VCURPUB:-$P/owner.pub}" || { rm -f "$m" "$m.sig"; die 2 "that key does not sign as the project's current owner"; } |
| 508 | durable "$m.sig" "$P/staging" |
| 509 | add SIG "$N" "$m.sig" |
| 510 | rm -f "$m" "$m.sig" |
| 511 | } |
| 512 | move_ref() { # BRANCH NEW OLD |
| 513 | if [ "$3" = $ZERO ]; then g update-ref "refs/heads/$1" "$2" "" || die 5 "ref update"; else g update-ref "refs/heads/$1" "$2" "$3" || die 5 "ref update"; fi |
| 514 | } |
| 515 | ref_free() { # BRANCH: no existing ref is a parent or a child of the new name |
| 516 | local b=$1 pre |
| 517 | [ -z "$(g for-each-ref --count=1 "refs/heads/$b/")" ] || return 1 |
| 518 | pre=$b |
| 519 | while [[ $pre == */* ]]; do pre=${pre%/*}; g show-ref --verify -q "refs/heads/$pre" && return 1; done |
| 520 | return 0 |
| 521 | } |
| 522 | # publish_row: this project's catalog line, then the catalog from every project's line. Derived on |
| 523 | # every committed change; `ipl catalog` re-derives all of it from full verification. |
| 524 | publish_row() { |
| 525 | put_row || die 5 "recorded, but the catalog line could not be written" |
| 526 | join_catalog || die 5 "recorded, but the catalog could not be published" |
| 527 | } |
| 528 | put_row() { # under the project lock: this project's line, written whole or not at all |
| 529 | local r; r=$(mktemp "$P/catalog.row.XXXXXX") || return 1 |
| 530 | # catalog stays five fields (name, id, branch, head, visibility); the extra OK fields are for verify |
| 531 | printf '%s\t%s\t%s\t%s\t%s\n' "$NAME" "$(sha "$P/records/1")" "$DEFBR" "${HEADS[$DEFBR]:-none}" "$VIS" > "$r" && chmod 644 "$r" && sync "$r" && mv -f "$r" "$P/catalog.row" && sync "$P" || { rm -f "$r"; return 1; } |
| 532 | } |
| 533 | join_catalog() { |
| 534 | local new d |
| 535 | ( flock -x 7 || exit 1 |
| 536 | new=$(mktemp "$ROOT/catalog.tsv.XXXXXX") || exit 1 |
| 537 | for d in "$PR"/*/; do [ -f "$d/catalog.row" ] && [ ! -L "${d%/}" ] && cat "$d/catalog.row"; done > "$new" |
| 538 | chmod 644 "$new" && sync "$new" && mv -f "$new" "$ROOT/catalog.tsv" && sync "$ROOT" || { rm -f "$new"; exit 1; } |
| 539 | ) 7>> "$ROOT/catalog.lock" |
| 540 | } |
| 541 | # record KIND BRANCH NEW RECEIPT_FILE VERDICT_FILE REQUEST KEY SOURCE (NEW is empty for IMPORT) |
| 542 | record() { |
| 543 | local kind=$1 br=$2 new=$3 rf=$4 vf=$5 req=$6 key=$7 src=$8 st old j f pay rd vd prev tag |
| 544 | "$LANDREC" check branch "$br" && g check-ref-format "refs/heads/$br" || die 2 "REFUSED branch" |
| 545 | branch_fits "$br" || die 2 "REFUSED branch: longer than this file system can hold as a branch" |
| 546 | "$LANDREC" check request "$req" || die 2 "REFUSED request" |
| 547 | [ "$kind" = IMPORT ] || [[ $new =~ ^[0-9a-f]{40}$ ]] || die 2 "REFUSED new" |
| 548 | [ -f "$rf" ] && [ -f "$vf" ] || die 2 "receipt or verdict file not found" |
| 549 | if [ "$src" != - ]; then [[ $src == /* ]] && [ -d "$src" ] || die 2 "REFUSED source: an absolute path of a local repository, or -"; fi |
| 550 | lock -x |
| 551 | load_current || die 3 "cannot determine the current owner key: $VMSG" |
| 552 | require_expect_owner |
| 553 | prove_owner "$key"; mend; vcore; st=$? |
| 554 | [ "$st" != 3 ] || die 3 "$VMSG" |
| 555 | [ "$ACTOR" = "$CURRENT" ] || die 2 "key $ACTOR is not this project's current owner" |
| 556 | rd=$(sha "$rf"); vd=$(sha "$vf"); tag=req-$(printf '%s' "$req" | sha256sum | cut -c1-64); mkdir -p "$P/staging" || die 5 "staging" |
| 557 | j=${REQAT[$req]:-} |
| 558 | if [ -n "$j" ]; then |
| 559 | f=$P/records/$j |
| 560 | [ "$kind" != IMPORT ] || new=$(pfield new "$f") |
| 561 | [ "$(cut -f3 <<< "${LINES[j - 1]}")" = "$kind" ] && [ "$(pfield branch "$f")" = "$br" ] && [ "$(pfield new "$f")" = "$new" ] && |
| 562 | [ "$(pfield receipt "$f")" = "$rd" ] && [ "$(pfield verdict "$f")" = "$vd" ] || die 4 "request $req was already used for something else" |
| 563 | if [ "$st" = 10 ] && [ "$j" = "$N" ]; then sign_tail "$key" "$tag"; stop_at after-sig; [ "$kind" = IMPORT ] || move_ref "$br" "$new" "$(pfield old "$f")" |
| 564 | elif [ "$st" = 11 ] && [ "$j" = $((N - 1)) ]; then move_ref "$br" "$new" "$(pfield old "$f")" |
| 565 | elif [ "$st" != 0 ]; then die 3 "$VMSG"; fi |
| 566 | rm -f "$P/staging/$tag".* |
| 567 | vcore || die 3 "$VMSG"; publish_row; okline; return 0 |
| 568 | fi |
| 569 | [ "$st" = 0 ] || die 3 "$VMSG" |
| 570 | old=${HEADS[$br]:-$ZERO} |
| 571 | if [ "$kind" = IMPORT ]; then |
| 572 | [ "$old" = $ZERO ] || die 4 "import: $br already has a recorded head" |
| 573 | new=$(g rev-parse -q --verify "refs/heads/$br" 2>/dev/null) || die 4 "import: no refs/heads/$br in the store" |
| 574 | [ "$(g cat-file -t "$new" 2>/dev/null)" = commit ] || die 4 "import: refs/heads/$br is not a commit" |
| 575 | else |
| 576 | [ "$src" = - ] || g fetch -q --no-tags --no-write-fetch-head --end-of-options "file://$src" "$new" 2>/dev/null || die 4 "cannot fetch $new from $src" |
| 577 | [ "$(g cat-file -t "$new" 2>/dev/null)" = commit ] || die 4 "$new is not a commit in the store" |
| 578 | [ "$old" != "$new" ] || die 4 "$new is already the head" |
| 579 | if [ "$old" = $ZERO ]; then |
| 580 | g show-ref --verify -q "refs/heads/$br" && die 4 "refs/heads/$br exists but has no recorded head; import it" |
| 581 | ref_free "$br" || die 4 "branch name $br collides with an existing branch" |
| 582 | else g merge-base --is-ancestor "$old" "$new" || die 4 "not a fast-forward: $old is not an ancestor of $new"; fi |
| 583 | fi |
| 584 | pay=$P/staging/$tag.payload |
| 585 | "$LANDREC" land "$NAME" "$br" "$old" "$new" "$rd" "$vd" "$req" > "$pay" || { rm -f "$pay"; die 2 "record refused"; } |
| 586 | prev=$(tail -n 1 "$P/chain.tsv" | tr -d '\n' | sha256sum | cut -c1-64) |
| 587 | sigmsg "$kind" "$ACTOR" "$(subj "$br")" "$(sha "$pay")" "$prev" > "$P/staging/$tag.msg" |
| 588 | rm -f "$P/staging/$tag.msg.sig"; ssh-keygen -q -Y sign -n interplane -f "$key" "$P/staging/$tag.msg" >/dev/null 2>&1 || { rm -f "$P/staging/$tag".*; die 2 "signing with $key failed"; } |
| 589 | sigok "$P/staging/$tag.msg" "$P/staging/$tag.msg.sig" "${VCURPUB:-$P/owner.pub}" || { rm -f "$P/staging/$tag".*; die 2 "that key does not sign as the project's current owner"; } |
| 590 | durable "$pay" "$P/staging/$tag.msg.sig" "$P/staging" |
| 591 | stop_at after-stage |
| 592 | add "$kind" "$(subj "$br")" "$pay" |
| 593 | stop_at after-record |
| 594 | add SIG "$SEQ" "$P/staging/$tag.msg.sig" |
| 595 | stop_at after-sig |
| 596 | [ "$kind" = IMPORT ] || move_ref "$br" "$new" "$old" |
| 597 | stop_at after-ref |
| 598 | rm -f "$P/staging/$tag".* |
| 599 | vcore || die 5 "recorded, but the project does not verify: $VMSG" |
| 600 | publish_row; okline |
| 601 | } |
| 602 | # branch_fits BRANCH: what this file system can hold. Git keeps a branch as the file |
| 603 | # repo.git/refs/heads/BRANCH and writes BRANCH.lock beside it while updating, so the last part |
| 604 | # needs room for five more bytes, the parts before it are folders, and the whole path must fit. |
| 605 | branch_fits() { |
| 606 | local nm pm parts i last |
| 607 | nm=$(getconf NAME_MAX "$P/repo.git") && pm=$(getconf PATH_MAX "$P/repo.git") || return 1 |
| 608 | IFS=/ read -ra parts <<< "$1"; last=$((${#parts[@]} - 1)) |
| 609 | for ((i = 0; i < last; i++)); do [ "${#parts[i]}" -le "$nm" ] || return 1; done |
| 610 | [ "${#parts[last]}" -le $((nm - 5)) ] || return 1 |
| 611 | local path=$P/repo.git/refs/heads/$1.lock |
| 612 | [ "${#path}" -lt "$pm" ] |
| 613 | } |
| 614 | handover_noop_finish() { |
| 615 | local st |
| 616 | vcore; st=$? |
| 617 | [ "$st" = 0 ] || die 3 "$VMSG" |
| 618 | require_expect_owner |
| 619 | rm -f "$P/staging"/handover.* |
| 620 | okline |
| 621 | } |
| 622 | |
| 623 | # do_handover KEY NEW_PUB REASON: one project, already opened as $NAME at $P. KEY is the |
| 624 | # private key named as old-key. A rerun whose new public key is already current is a no-op. |
| 625 | # owner.pub is never replaced: it stays the key record 1 names. A successor that was ever |
| 626 | # an owner of this project (record 1, or any earlier SUCCESSOR new) is refused. |
| 627 | do_handover() { |
| 628 | local key=$1 newpub=$2 reason=$3 blob newfp st f pay prev tag |
| 629 | key_actor "$key" |
| 630 | [ -f "$newpub" ] || die 2 "public key $newpub not found" |
| 631 | blob=$(pubblob "$newpub") || die 2 "$newpub is not one ssh-ed25519 public key" |
| 632 | newfp=$(ed25519_fp "$newpub") || die 2 "$newpub is not one Ed25519 public key" |
| 633 | "$LANDREC" check reason "$reason" || die 2 "REFUSED reason" |
| 634 | lock -s |
| 635 | load_current || die 3 "cannot determine the current owner key: $VMSG" |
| 636 | require_expect_owner |
| 637 | if [ "$newfp" = "$CURRENT" ]; then |
| 638 | # already the current key: nothing is recorded (a pending unsigned SUCCESSOR has |
| 639 | # not switched yet, so it is finished in the branch below, not here) |
| 640 | handover_noop_finish |
| 641 | return 0 |
| 642 | fi |
| 643 | unlock |
| 644 | lock -x |
| 645 | load_current || die 3 "cannot determine the current owner key: $VMSG" |
| 646 | require_expect_owner |
| 647 | if [ "$newfp" = "$CURRENT" ]; then |
| 648 | handover_noop_finish |
| 649 | return 0 |
| 650 | fi |
| 651 | is_prior_owner "$newfp" && die 2 "REFUSED new: $newfp was already an owner of this project" |
| 652 | prove_owner "$key" |
| 653 | mend |
| 654 | vcore; st=$? |
| 655 | [ "$st" != 3 ] || die 3 "$VMSG" |
| 656 | [ "$ACTOR" = "$CURRENT" ] || die 2 "key $ACTOR is not this project's current owner" |
| 657 | is_prior_owner "$newfp" && die 2 "REFUSED new: $newfp was already an owner of this project" |
| 658 | if [ "$st" = 10 ]; then |
| 659 | IFS=$'\t' read -r s u k a sj p pv <<< "${LINES[N - 1]}" |
| 660 | if [ "$k" = SUCCESSOR ]; then |
| 661 | f=$P/records/$N |
| 662 | [ "$(pfield new "$f")" = "$newfp" ] && [ "$(pfield old "$f")" = "$ACTOR" ] && [ "$(pfield reason "$f")" = "$reason" ] || die 3 "$VMSG" |
| 663 | mkdir -p "$P/staging" || die 5 "staging" |
| 664 | sign_tail "$key" handover |
| 665 | finish_handover_pin "$newfp" |
| 666 | stop_at after-sig |
| 667 | rm -f "$P"/staging/handover.* |
| 668 | vcore || die 5 "recorded, but the project does not verify: $VMSG" |
| 669 | publish_row; okline |
| 670 | return 0 |
| 671 | fi |
| 672 | die 3 "$VMSG" |
| 673 | fi |
| 674 | [ "$st" = 0 ] || die 3 "$VMSG" |
| 675 | [ "$newfp" != "$CURRENT" ] || die 2 "REFUSED new: already the current owner" |
| 676 | mkdir -p "$P/staging" || die 5 "staging" |
| 677 | tag=handover |
| 678 | pay=$P/staging/$tag.payload |
| 679 | "$LANDREC" successor "$NAME" "$CURRENT" "$newfp" "$blob" "$reason" > "$pay" || { rm -f "$pay"; die 2 "record refused"; } |
| 680 | prev=$(tail -n 1 "$P/chain.tsv" | tr -d '\n' | sha256sum | cut -c1-64) |
| 681 | sigmsg SUCCESSOR "$ACTOR" "$(subj "$newfp")" "$(sha "$pay")" "$prev" > "$P/staging/$tag.msg" |
| 682 | rm -f "$P/staging/$tag.msg.sig" |
| 683 | ssh-keygen -q -Y sign -n interplane -f "$key" "$P/staging/$tag.msg" >/dev/null 2>&1 || { rm -f "$P/staging/$tag".*; die 2 "signing with $key failed"; } |
| 684 | sigok "$P/staging/$tag.msg" "$P/staging/$tag.msg.sig" "$VCURPUB" || { rm -f "$P/staging/$tag".*; die 2 "that key does not sign as the project's current owner"; } |
| 685 | durable "$pay" "$P/staging/$tag.msg.sig" "$P/staging" |
| 686 | stop_at after-stage |
| 687 | add SUCCESSOR "$(subj "$newfp")" "$pay" |
| 688 | stop_at after-record |
| 689 | add SIG "$SEQ" "$P/staging/$tag.msg.sig" |
| 690 | finish_handover_pin "$newfp" |
| 691 | stop_at after-sig |
| 692 | rm -f "$P/staging/$tag".* |
| 693 | vcore || die 5 "recorded, but the project does not verify: $VMSG" |
| 694 | publish_row; okline |
| 695 | } |
| 696 | |
| 697 | # --expect-owner FP may appear anywhere; --all stays a positional handover argument |
| 698 | EXPECT_OWNER="" |
| 699 | args=() |
| 700 | while [ $# -gt 0 ]; do |
| 701 | case $1 in |
| 702 | --expect-owner) |
| 703 | [ -n "${2:-}" ] || die 1 "usage: --expect-owner FP" |
| 704 | EXPECT_OWNER=$2 |
| 705 | shift 2 ;; |
| 706 | --expect-owner=*) |
| 707 | EXPECT_OWNER=${1#--expect-owner=} |
| 708 | shift ;; |
| 709 | --all) |
| 710 | # --all is a positional handover argument, not a flag |
| 711 | args+=("$1"); shift ;; |
| 712 | *) |
| 713 | args+=("$1"); shift ;; |
| 714 | esac |
| 715 | done |
| 716 | set -- "${args[@]}" |
| 717 | if [ -n "$EXPECT_OWNER" ]; then |
| 718 | "$LANDREC" check owner "$EXPECT_OWNER" || die 2 "REFUSED expect-owner" |
| 719 | fi |
| 720 | |
| 721 | # preflight_handover KEY NEWFP: read-only check that this project can be handed to NEWFP (or is already). |
| 722 | # Prints a reason on stderr and returns 1 on failure; does not write. |
| 723 | preflight_handover() { |
| 724 | local key=$1 newfp=$2 st k |
| 725 | lock -s |
| 726 | if ! load_current; then echo "ipl: $NAME: cannot determine the current owner key: $VMSG" >&2; return 1; fi |
| 727 | if ! pin_for "$NAME"; then echo "ipl: $NAME: owner pin file has no line for $NAME" >&2; return 1; fi |
| 728 | if [ -n "${EXPECT_OWNER:-}" ] && [ "$EXPECT_OWNER" != "$PIN_FP" ]; then |
| 729 | echo "ipl: $NAME: current owner pin is $PIN_FP, not $EXPECT_OWNER" >&2 |
| 730 | return 1 |
| 731 | fi |
| 732 | if [ "$CURRENT" != "$PIN_FP" ]; then |
| 733 | echo "ipl: $NAME: current owner is $CURRENT, not $PIN_FP" >&2 |
| 734 | return 1 |
| 735 | fi |
| 736 | if [ "$newfp" = "$CURRENT" ]; then |
| 737 | vcore >/dev/null || { echo "ipl: $NAME: does not verify: $VMSG" >&2; return 1; } |
| 738 | return 0 |
| 739 | fi |
| 740 | if is_prior_owner "$newfp"; then |
| 741 | echo "ipl: $NAME: REFUSED new: $newfp was already an owner of this project" >&2 |
| 742 | return 1 |
| 743 | fi |
| 744 | if [ "$ACTOR" != "$CURRENT" ]; then |
| 745 | echo "ipl: $NAME: current owner is $CURRENT, not $ACTOR" >&2 |
| 746 | return 1 |
| 747 | fi |
| 748 | if ! ( prove_owner "$key" ); then |
| 749 | echo "ipl: $NAME: that key does not sign as the project's current owner" >&2 |
| 750 | return 1 |
| 751 | fi |
| 752 | vcore >/dev/null; st=$? |
| 753 | if [ "$st" = 10 ]; then |
| 754 | IFS=$'\t' read -r s u k a sj p pv <<< "${LINES[N - 1]}" |
| 755 | if [ "$k" = SUCCESSOR ] && [ "$(pfield new "$P/records/$N")" = "$newfp" ]; then |
| 756 | return 0 |
| 757 | fi |
| 758 | echo "ipl: $NAME: $VMSG" >&2 |
| 759 | return 1 |
| 760 | fi |
| 761 | if [ "$st" != 0 ]; then echo "ipl: $NAME: $VMSG" >&2; return 1; fi |
| 762 | return 0 |
| 763 | } |
| 764 | |
| 765 | case ${1:-} in |
| 766 | init) |
| 767 | [ -z "$EXPECT_OWNER" ] || die 1 "--expect-owner is for verify, land, import, handover, and catalog" |
| 768 | [ $# = 7 ] || die 1 "usage: ipl init ROOT NAME BRANCH VISIBILITY ALIAS KEY" |
| 769 | open_project "$2" "$3"; key_actor "$7" |
| 770 | t=$(mktemp) || die 5 mktemp |
| 771 | "$LANDREC" project "$NAME" "$4" "$5" "$ACTOR" "$6" "$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" > "$t" && g check-ref-format "refs/heads/$4" || { rm -f "$t"; die 2 "record refused"; } |
| 772 | branch_fits "$4" || { rm -f "$t"; die 2 "REFUSED branch: longer than this file system can hold as a branch"; } |
| 773 | exec 8>> "$ROOT/init.lock" && flock -x 8 || die 5 "lock" |
| 774 | lock -x |
| 775 | for d in "$PR"/*/; do |
| 776 | o=$(basename "$d"); [ "$o" = "$NAME" ] || [ "${o,,}" != "${NAME,,}" ] || { rm -f "$t"; die 2 "$NAME differs from the existing project $o only in case"; } |
| 777 | done |
| 778 | # an earlier init left its owner key here: only that owner goes on, whatever state it reached |
| 779 | had_owner=0 |
| 780 | if [ -e "$P/owner.pub" ]; then had_owner=1; prove_owner "$7"; mend; fi |
| 781 | if [ -e "$P/chain.tsv" ] && [ ! -s "$P/chain.tsv" ]; then rm -f "$P/chain.tsv"; echo "ipl: repaired: removed an empty chain file" >&2; fi |
| 782 | if [ -e "$P/chain.tsv" ]; then |
| 783 | # the same terms by the owner: finish an init that stopped before its signature, or, for a |
| 784 | # project that is already complete, publish its catalog line again and answer |
| 785 | rm -f "$t"; [ "$had_owner" = 1 ] || die 3 "a chain with no owner key beside it" |
| 786 | vcore; st=$? |
| 787 | [ "$st" != 3 ] || die 3 "$VMSG" |
| 788 | [ "$DEFBR" = "$4" ] && [ "$VIS" = "$5" ] && [ "$(pfield alias "$P/records/1")" = "$6" ] || die 2 "$NAME already exists with other terms" |
| 789 | if [ "$st" = 10 ] && [ "$N" = 1 ]; then mkdir -p "$P/staging"; sign_tail "$7" init |
| 790 | elif [ "$st" != 0 ]; then die 3 "$VMSG"; fi |
| 791 | else |
| 792 | sp=$P/staging/init.payload |
| 793 | if [ "$had_owner" = 1 ] && [ -f "$sp" ]; then |
| 794 | # an earlier attempt staged its record: the same terms go on with it, other terms are refused |
| 795 | [ "$(pfield branch "$sp")" = "$4" ] && [ "$(pfield visibility "$sp")" = "$5" ] && [ "$(pfield alias "$sp")" = "$6" ] && [ "$(pfield owner "$sp")" = "$ACTOR" ] || |
| 796 | { rm -f "$t"; die 2 "$NAME was started with other terms"; } |
| 797 | rm -f "$t" |
| 798 | else |
| 799 | mkdir -p "$P/records" "$P/staging" && cp "$t" "$sp" || { rm -f "$t"; die 5 "cannot write $P"; } |
| 800 | rm -f "$t" |
| 801 | [ "$had_owner" = 1 ] || { cp "$7.pub" "$P/owner.pub.tmp" && mv "$P/owner.pub.tmp" "$P/owner.pub"; } || die 5 "cannot write $P" |
| 802 | fi |
| 803 | sigmsg PROJECT "$ACTOR" "$(subj "$NAME")" "$(sha "$sp")" GENESIS > "$P/staging/init.msg" |
| 804 | rm -f "$P/staging/init.msg.sig" |
| 805 | if ! { ssh-keygen -q -Y sign -n interplane -f "$7" "$P/staging/init.msg" >/dev/null 2>&1 && sigok "$P/staging/init.msg" "$P/staging/init.msg.sig"; }; then |
| 806 | # a key that was only just put here goes again; an owner who was here before stays |
| 807 | rm -f "$P/staging/init.msg" "$P/staging/init.msg.sig"; [ "$had_owner" = 1 ] || rm -f "$sp" "$P/owner.pub" |
| 808 | die 2 "the key pair $7 does not sign and verify" |
| 809 | fi |
| 810 | durable "$sp" "$P/staging/init.msg.sig" "$P/owner.pub" "$P/staging" "$P" |
| 811 | stop_at after-stage |
| 812 | add PROJECT "$(subj "$NAME")" "$sp" |
| 813 | stop_at after-record |
| 814 | add SIG "$SEQ" "$P/staging/init.msg.sig" |
| 815 | stop_at after-sig |
| 816 | fi |
| 817 | rm -f "$P"/staging/init.* |
| 818 | vcore || die 5 "recorded, but the project does not verify: $VMSG" |
| 819 | publish_row; okline ;; |
| 820 | import) |
| 821 | [ $# = 8 ] || die 1 "usage: ipl import ROOT NAME BRANCH EVIDENCE_FILE NOTE_FILE REQUEST KEY" |
| 822 | open_project "$2" "$3"; load_owner_pins; key_actor "$8" |
| 823 | record IMPORT "$4" "" "$5" "$6" "$7" "$8" - ;; |
| 824 | land) |
| 825 | [ $# = 10 ] || die 1 "usage: ipl land ROOT NAME BRANCH NEW SOURCE RECEIPT_FILE VERDICT_FILE REQUEST KEY [--expect-owner FP]" |
| 826 | open_project "$2" "$3"; load_owner_pins; key_actor "${10}" |
| 827 | record LAND "$4" "$5" "$7" "$8" "$9" "${10}" "$6" ;; |
| 828 | verify) |
| 829 | [ $# = 3 ] || die 1 "usage: ipl verify ROOT NAME [--expect-owner FP]" |
| 830 | open_project "$2" "$3"; load_owner_pins; lock -s |
| 831 | vcore || die 3 "$VMSG" |
| 832 | require_expect_owner |
| 833 | okline ;; |
| 834 | catalog) |
| 835 | [ $# = 2 ] || die 1 "usage: ipl catalog ROOT [--expect-owner FP]" |
| 836 | PR=$(realpath -e -- "$2/projects" 2>/dev/null) || die 2 "no store at $2"; ROOT=${PR%/projects}; rc=0 |
| 837 | load_owner_pins |
| 838 | for d in "$PR"/*/; do |
| 839 | NAME=$(basename "$d") |
| 840 | if ! admit; then echo "ipl: $NAME is not a project of this store; left out" >&2; [ -L "$PR/$NAME" ] || [ ! -d "$PR/$NAME" ] || rm -f "$PR/$NAME/catalog.row"; rc=3; continue; fi |
| 841 | lock -s |
| 842 | if ! vcore; then |
| 843 | echo "ipl: $NAME does not verify; left out of the catalog" >&2 |
| 844 | rm -f "$P/catalog.row"; rc=3; continue |
| 845 | fi |
| 846 | if ! pin_for "$NAME"; then |
| 847 | echo "ipl: $NAME: owner pin file has no line for $NAME; left out of the catalog" >&2 |
| 848 | rm -f "$P/catalog.row"; rc=3; continue |
| 849 | fi |
| 850 | if [ -n "${EXPECT_OWNER:-}" ] && [ "$EXPECT_OWNER" != "$PIN_FP" ]; then |
| 851 | echo "ipl: $NAME: current owner pin is $PIN_FP, not $EXPECT_OWNER; left out of the catalog" >&2 |
| 852 | rm -f "$P/catalog.row"; rc=3; continue |
| 853 | fi |
| 854 | if [ "$CURRENT" != "$PIN_FP" ]; then |
| 855 | echo "ipl: $NAME: current owner is $CURRENT, not $PIN_FP; left out of the catalog" >&2 |
| 856 | rm -f "$P/catalog.row"; rc=3; continue |
| 857 | fi |
| 858 | put_row || { echo "ipl: $NAME: catalog line could not be written; left out" >&2; rc=3; continue; } |
| 859 | done |
| 860 | join_catalog || die 5 "cannot publish the catalog" |
| 861 | wc -l < "$ROOT/catalog.tsv"; exit $rc ;; |
| 862 | handover) |
| 863 | reason=- |
| 864 | if [ $# = 6 ]; then reason=$6 |
| 865 | elif [ $# != 5 ]; then die 1 "usage: ipl handover ROOT NAME|--all OLD_KEY NEW_PUBKEY [REASON]" |
| 866 | fi |
| 867 | [ -f "$5" ] || die 2 "public key $5 not found" |
| 868 | if [ "$3" = --all ]; then |
| 869 | PR=$(realpath -e -- "$2/projects" 2>/dev/null) || die 2 "no store at $2"; ROOT=${PR%/projects} |
| 870 | load_owner_pins |
| 871 | key_actor "$4" |
| 872 | newfp=$(ed25519_fp "$5") || die 2 "$5 is not one Ed25519 public key" |
| 873 | found=0; bad=0; names=(); fds=() |
| 874 | for d in "$PR"/*/; do |
| 875 | NAME=$(basename "$d") |
| 876 | admit || continue |
| 877 | found=1 |
| 878 | names+=("$NAME") |
| 879 | exec {lockfd}>>"$P/lock" || die 5 "lock" |
| 880 | flock -s "$lockfd" || die 5 "lock" |
| 881 | fds+=("$lockfd") |
| 882 | ALL_LOCK_FD=$lockfd preflight_handover "$4" "$newfp" || bad=1 |
| 883 | done |
| 884 | [ "$found" = 1 ] || die 2 "no project in the store" |
| 885 | if [ "$bad" = 1 ]; then |
| 886 | for fd in "${fds[@]}"; do flock -u "$fd" 2>/dev/null || true; eval "exec ${fd}>&-"; done |
| 887 | echo "ipl: --all preflight failed; nothing was recorded" >&2 |
| 888 | exit 2 |
| 889 | fi |
| 890 | HANDOVER_DEFER_PIN=1 |
| 891 | i=0 |
| 892 | for NAME in "${names[@]}"; do |
| 893 | admit || die 5 "project $NAME vanished during --all" |
| 894 | ALL_LOCK_FD=${fds[$i]} do_handover "$4" "$5" "$reason" |
| 895 | i=$((i + 1)) |
| 896 | done |
| 897 | for fd in "${fds[@]}"; do flock -u "$fd" 2>/dev/null || true; eval "exec ${fd}>&-"; done |
| 898 | announce_pin '*' "$newfp" |
| 899 | update_owner_pin_all "$ACTOR" "$newfp" "${names[@]}" |
| 900 | else |
| 901 | open_project "$2" "$3" |
| 902 | load_owner_pins |
| 903 | do_handover "$4" "$5" "$reason" |
| 904 | fi |
| 905 | ;; |
| 906 | *) die 1 "usage: ipl init|import|land|verify|catalog|handover ..." ;; |
| 907 | esac |