| 1 | #!/usr/bin/env bash |
| 2 | # Desc: Export each pubkey to a separate file |
| 3 | # Usage: bk-export-min-pubkeys.sh |
| 4 | # Version: 0.0.2 |
| 5 | |
| 6 | time_now="$(date +%Y%m%dT%H%M%S%z)"; |
| 7 | dir_out="./$time_now..pubkeys"; |
| 8 | |
| 9 | yell() { echo "$0: $*" >&2; } # print script path and all args to stderr |
| 10 | die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status |
| 11 | try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails |
| 12 | gpg_get_pubkey() { |
| 13 | # Desc: Output ascii-armored gpg pubkey via stdout |
| 14 | # Input: arg1: pgp long id |
| 15 | # Output: stdout: ascii-armored minimal-signature pgp key |
| 16 | # Example: gpg_get_pubkey 0xa0a295abdc3469c9 |
| 17 | # Depends: gpg 2.2.12 |
| 18 | local longid output; |
| 19 | longid="$1"; |
| 20 | |
| 21 | output="$(gpg --export --armor --export-options export-minimal "$longid")"; |
| 22 | echo "$output"; |
| 23 | } # Output ascii-armored gpg pubkey via stdout |
| 24 | main() { |
| 25 | # Create list of primary key fingerprints |
| 26 | list_longid="$(gpg --list-keys | grep "^pub" | awk '{print $2}' | cut -d'/' -f2)"; |
| 27 | |
| 28 | # Create output dir |
| 29 | path_out="$(readlink -f "$dir_out")"; |
| 30 | #yell "DEBUG:path_out:$path_out"; |
| 31 | if [[ ! -d "$path_out" ]]; then |
| 32 | mkdir -p "$path_out"; |
| 33 | yell "NOTICE:Creating output directory:$path_out"; |
| 34 | fi; |
| 35 | |
| 36 | # Iterate through list |
| 37 | while read -r longid; do |
| 38 | yell "STATUS:Exporting $longid"; |
| 39 | |
| 40 | # Export file |
| 41 | pubkey_ascii="$(try gpg_get_pubkey "$longid")"; |
| 42 | #yell "DEBUG:pubkey_ascii:$pubkey_ascii"; |
| 43 | echo "$pubkey_ascii" > "$path_out"/"$longid".asc |
| 44 | |
| 45 | done < <( echo "$list_longid" ); |
| 46 | }; # main program |
| 47 | |
| 48 | main "$@"; |
| 49 | |
| 50 | # Author: Steven Baltaktei Sandoval |
| 51 | # License: GPLv3+ |