chiark / gitweb /
Import gnupg2_2.1.17-3.debian.tar.bz2
[gnupg2.git] / migrate-pubring-from-classic-gpg
1 #!/bin/bash
2
3 # script to migrate fully from pubring.gpg to pubring.kbx
4
5 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
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 ] ; then
21     printf '%s is version %s not version 2.1 or 2.2, 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 # ensure that there is a pubring.gpg to migrate:
52 if ! [ -f "$GHD/pubring.gpg" ]; then
53     printf 'There is no %s/pubring.gpg, no need to migrate\n' "$GHD" >&2
54     exit
55 fi
56 if ! [ -s "$GHD/pubring.gpg" ]; then
57     mv -- "$GHD/pubring.gpg" "$GHD/pubring.gpg.empty"
58     printf '%s/pubring.gpg was empty (and has been moved out of the way), no need to migrate\n' "$GHD" >&2
59     exit
60 fi
61
62 BACKUP="$(mktemp -d "$GHD/migrate-from-classic-backup.$(date +%F).XXXXXX")"
63 printf 'Migrating from:\n%s\n[Backing up to %s]\n' "$(ls -l "$GHD/pubring.gpg")" "$BACKUP" >&2
64
65 "$GPG" --export-ownertrust > "$BACKUP/ownertrust.txt"
66 mv "$GHD/pubring.gpg" "$BACKUP/"
67 "$GPG" --import-options import-local-sigs,keep-ownertrust,repair-pks-subkey-bug --import < "$BACKUP/pubring.gpg"
68 "$GPG" --import-ownertrust < "$BACKUP/ownertrust.txt"
69 "$GPG" --check-trustdb
70
71 if ! [ -f "$GHD/pubring.kbx" ]; then
72     printf 'No keybox was created at %s/pubring.kbx.  Something went wrong!\n' "$GHD" >&2
73     exit 1
74 fi
75
76 printf 'Migration completed successfully:\n%s\n' "$(ls -l "$GHD/pubring.kbx")" >&2