bugfix: gpg/bin -> gnupg/bin, use 'git config' for name and email
[webi-installers/.git] / gpg-pubkey / gpg-pubkey.sh
1 #!/bin/bash
2 set -e
3 set -u
4
5 function __get_git_email() {
6     git config --global user.email
7     # grep 'email\s*=.*@' ~/.gitconfig | tr -d '\t ' | head -n 1 | cut -d'=' -f2
8 }
9
10 function __get_pubkey_id() {
11     gpg --list-secret-keys --keyid-format LONG |
12         grep sec |
13         cut -d'/' -f2 |
14         cut -d' ' -f1
15 }
16
17 function _create_gpg_key() {
18     if [[ ! -e ~/.gitconfig ]]; then
19         return 1
20     fi
21
22     #grep 'name\s*=' ~/.gitconfig | head -n 1 | cut -d'=' -f2 | sed -e 's/^[\t ]*//'
23     MY_NAME="$(git config --global user.name)"
24     if [[ -z ${MY_NAME} ]]; then
25         return 1
26     fi
27
28     MY_EMAIL="$(
29         __get_git_email
30     )"
31     if [[ -z ${MY_EMAIL} ]]; then
32         return 1
33     fi
34
35     MY_HOST="$(hostname)"
36
37     # Without passphrase:
38     #gpg --batch --generate-key --pinentry=loopback --passphrase=''
39
40     # With passphrase via macOS Keychain
41     gpg_opts="
42      %echo Generating RSA 3072 key...
43      %echo Warning: It may take several minutes to gather enough entropy,
44      %echo          especially on a linux VPS if haveged isn't installed.
45      %echo          (try moving the mouse, downloading large files, etc)
46      Key-Type: RSA
47      Key-Length: 3072
48      Subkey-Type: RSA
49      Subkey-Length: 3072
50      Name-Real: ${MY_NAME}
51      Name-Comment: ${MY_HOST}
52      Name-Email: ${MY_EMAIL}
53      Expire-Date: 0
54      %commit
55     "
56     if ! echo "$gpg_opts" | gpg --batch --generate-key 2> /dev/null; then
57         echo >&2 ""
58         echo >&2 ""
59         echo >&2 ""
60         echo >&2 "== STOP! CHOOSE A PASSPHRASE =="
61         echo >&2 ""
62         echo >&2 "Choose a passphrase for this GPG Key."
63         echo >&2 "(the passphrase will not be shown as you type)"
64         echo >&2 ""
65         echo >&2 -n "Passphrase: "
66         read -r -s
67         echo >&2 ""
68         echo "
69          %echo Generating RSA 3072 key...
70          %echo Warning: It may take several minutes to gather enough entropy,
71          %echo          especially on a linux VPS if haveged isn't installed.
72          %echo          (try moving the mouse, downloading large files, etc)
73          Key-Type: RSA
74          Key-Length: 3072
75          Subkey-Type: RSA
76          Subkey-Length: 3072
77          Name-Real: ${MY_NAME}
78          Name-Comment: ${MY_HOST}
79          Name-Email: ${MY_EMAIL}
80          Passphrase: ${REPLY}
81          Expire-Date: 0
82          %commit
83         " --batch --generate-key > gpg
84     fi
85     echo >&2 "Done"
86 }
87
88 # (maybe) Create first key
89 if ! gpg --list-secret-keys | grep -q sec; then
90     if ! _create_gpg_key; then
91         echo >&2 ""
92         echo >&2 "Please set your name and email, and then try again:"
93         echo >&2 ""
94         echo >&2 "    git config --global user.name 'John Doe'"
95         echo >&2 "    git config --global user.email johndoe@example.com"
96         echo >&2 "    gpg-pubkey"
97         echo >&2 ""
98         echo >&2 "(or manually create a private key first)"
99         echo >&2 ""
100         exit 1
101     fi
102 fi
103
104 MY_KEY_ID="$(
105     __get_pubkey_id
106 )"
107
108 MY_EMAIL="$(
109     __get_git_email
110 )"
111
112 #gpg --send-keys "${MY_KEY_ID}"
113
114 MY_ASC_RELPATH="Downloads/${MY_EMAIL}.${MY_KEY_ID}.gpg.asc"
115 mkdir -p ~/Downloads/
116 rm -f ~/"${MY_ASC_RELPATH}"
117 gpg --armor --export "${MY_KEY_ID}" > ~/"${MY_ASC_RELPATH}"
118
119 echo >&2 ""
120 echo >&2 "GnuPG Public Key ID: ${MY_KEY_ID}"
121 echo >&2 ""
122 #shellcheck disable=SC2088
123 echo >&2 "~/${MY_ASC_RELPATH}":
124 echo >&2 ""
125 cat ~/"${MY_ASC_RELPATH}"
126 echo >&2 ""