Debian Codegraph @

debian/migrate-pubring-from-classic-gpg

raw
1 #!/bin/bash
2
3 # script to migrate fully from pubring.gpg to pubring.kbx
4
5 # Author: Daniel Kahn Gillmor <[email protected]>
6 # Date: 2016-04-01
7 # License: GPLv3+
8
9 # This was written for the Debian project
10
11 set -e
12
13 GPG="${GPG:-gpg}"
14
15 # select the default GnuPG home directory to work from:
16 GHD=${GNUPGHOME:-${HOME:-$(getent passwd "$(id -u)" | cut -f6 -d:)}/.gnupg}
17
18 # Check that this is gnupg 2.1 or 2.2:
19 VERSION=$("$GPG" --version | head -n1 | cut -f3 -d\ | cut -f1,2 -d.)
20 if [ "$VERSION" != 2.1 ] && [ "$VERSION" != 2.2 ] && [ "$VERSION" != "2.4" ] ; then
21 printf '%s is version %s not version 2.1, 2.2, or 2.4, this script might be wrong\n' "$GPG" "$VERSION" >&2
22 exit 1
23 fi
24
25 usage() {
26 printf 'Usage: %s [GPGHOMEDIR|--default]
27 \tMigrate public keyring in GPGHOMEDIR from "classic" to "modern" GnuPG
28 \tusing %s version %s.
29
30 \t--default migrates the GnuPG home directory at "%s"
31 ' "$0" "$GPG" "$VERSION" "$GHD"
32 }
33
34 if [ -z "$1" ]; then
35 usage >&2
36 exit 1
37 else
38 case "$1" in
39 --help|--usage|-h)
40 usage
41 exit
42 ;;
43 --default)
44 ;;
45 *)
46 GHD="$1"
47 ;;
48 esac
49 fi
50
51 GPG() {
52 "$GPG" --homedir "$GHD" --batch "$@"
53 }
54
55 # ensure that there is a pubring.gpg to migrate:
56 if ! [ -f "$GHD/pubring.gpg" ]; then
57 printf 'There is no %s/pubring.gpg, no need to migrate\n' "$GHD" >&2
58 exit
59 fi
60 if ! [ -s "$GHD/pubring.gpg" ]; then
61 mv -- "$GHD/pubring.gpg" "$GHD/pubring.gpg.empty"
62 printf '%s/pubring.gpg was empty (and has been moved out of the way), no need to migrate\n' "$GHD" >&2
63 exit
64 fi
65
66 BACKUP="$(mktemp -d "$GHD/migrate-from-classic-backup.$(date +%F).XXXXXX")"
67 printf 'Migrating from:\n%s\n[Backing up to %s]\n' "$(ls -l "$GHD/pubring.gpg")" "$BACKUP" >&2
68
69 GPG --export-ownertrust > "$BACKUP/ownertrust.txt"
70 mv "$GHD/pubring.gpg" "$BACKUP/"
71
72 revert() {
73 printf >&2 'Restoring pubring.gpg...\n'
74 cp "$BACKUP/pubring.gpg" "$GHD/pubring.gpg"
75 }
76
77 trap revert EXIT
78
79 if ! GPG --status-file "$BACKUP/import-status" --import-options import-local-sigs,keep-ownertrust,repair-pks-subkey-bug --import < "$BACKUP/pubring.gpg" ; then
80 cat >&2 <<EOF
81 Keyring import was not completely successful (see error message above,
82 and the LIMITATIONS section of migrate-pubring-from-classic-gpg(1) for
83 more details).
84
85 If you suspect a bug in the migration script, please use:
86
87 reportbug gnupg-utils --subject='migrate-pubring-from-classic-gpg partial failure'
88
89 And include the above output (redacted for privacy as needed) in the
90 body of the report.
91
92 Continuing with the rest of the migration anyway...
93 EOF
94 fi
95 GPG --import-ownertrust < "$BACKUP/ownertrust.txt"
96 GPG --check-trustdb
97
98 if ! [ -f "$GHD/pubring.kbx" ]; then
99 cat >&2 <<EOF
100 No keybox was created at $GHD/pubring.kbx. Something went wrong!
101
102 Please report a bug in the migration script, using:
103
104 reportbug gnupg-utils --subject='migrate-pubring-from-classic-gpg no pubring.kbx ($BACKUP)'
105 EOF
106 exit 1
107 fi
108 trap - EXIT
109
110 printf 'Migration completed successfully:\n%s\n' "$(ls -l "$GHD/pubring.kbx")" >&2