#!/bin/bash set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat </ create-local create-local -h|--help Arguments: bucket The bucket the store's key goes in; its readers read the store. Needed for a new store on a machine with buckets store-name Name of the local store to create directory Directory to initialize with store metadata The command will: 1. Check if the directory already has a .szecret metadata file 2. If the store key (local-key-) doesn't exist, generate a new age private key and put it in , or in the old store on a machine with no buckets 3. Write the store name and public key to /.szecret Note: This command will refuse to overwrite existing metadata. EOF exit 0 fi this_dir="$(cd "$(dirname "$0")" && pwd)" source "${this_dir}/common.sh" if [[ "$#" -lt 2 ]]; then >&2 echo "ERROR: Need local store name and directory" exit 1 fi bucket="" local_store_name="$1" if [[ "${local_store_name}" == */* ]]; then bucket="${local_store_name%%/*}" local_store_name="${local_store_name#*/}" [[ -n "${buckets}" ]] || die "no buckets on this machine: $1" [[ "${bucket}" =~ ${re_bucket} ]] || die "not a bucket's name: ${bucket}" fi [[ "${local_store_name}" =~ ${re_secret} ]] || die "not a store's name: ${local_store_name}" target_dir="$2" metadata_file="${target_dir}/.szecret" local_key_name="$(local_key_name "${local_store_name}")" # Check if metadata already exists if [[ -e "${metadata_file}" ]]; then >&2 echo "ERROR: Directory already has .szecret metadata" exit 1 fi found="$(locate "${local_key_name}")" if [[ "${found}" == *$'\n'* ]]; then die "${local_key_name} is in more than one place: $(while read -r f; do where_is "${f}"; done <<<"${found}" | paste -sd ',' - | sed 's/,/, /g')" fi if [[ -n "${found}" && -n "${bucket}" && "${found}" != "${buckets}/${bucket}/${local_key_name}.age" ]]; then if [[ -n "${store}" && "${found}" == "${store}/${local_key_name}.age" ]]; then die "${local_key_name} is in the old store, not in ${bucket}; szecret mv ${local_key_name} ${bucket} moves it" fi die "${local_key_name} is in $(where_is "${found}"), not in ${bucket}" fi if [[ -z "${found}" && -n "${buckets}" ]]; then if [[ -z "${bucket}" ]]; then die "name the bucket for the new store's key, whose readers read the store: szecret create-local /${local_store_name} ${target_dir}" fi # The store is used where its key is read: put-local and get-local both # decrypt the key. [[ -d "${buckets}/${bucket}" ]] || die "no bucket ${bucket}" [[ -n "$(encrypted_to "${buckets}/${bucket}")" ]] || die "${bucket} has never been keyed: szecret rekey ${bucket} first" if ! is_reader "${buckets}/${bucket}"; then while read -r k; do if [[ -n "${k}" ]] && keys_in "${buckets}/${bucket}/recipients" | grep -qxF "${k}"; then die "this machine was added to ${bucket}'s readers since its last rekey: szecret rekey ${bucket} first" fi done <<<"$(my_keys)" die "this machine doesn't read ${bucket}, so it couldn't use the store; make it where ${bucket} is read" fi fi if [[ -e "${target_dir}" && ! -d "${target_dir}" ]]; then die "${target_dir} is not a directory" fi ensure_age if [[ -n "${found}" ]]; then local_pubkey="$(get_local_public_key "${local_store_name}")" else # New store: the key goes in first, so a refused put leaves no metadata. local_private_key="$(age-keygen 2>/dev/null)" local_pubkey="$(printf '%s\n' "${local_private_key}" | age-keygen -y)" printf '%s\n' "${local_private_key}" | "${this_dir}/put.sh" "${bucket:+${bucket}/}${local_key_name}" unset local_private_key fi mkdir -p "${target_dir}" write_local_metadata "${metadata_file}" "${local_store_name}" "${local_pubkey}" if [[ -n "${found}" ]]; then >&2 echo "Added metadata for existing store '${local_store_name}'" fi