-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpg-helper.sh
executable file
·154 lines (132 loc) · 3.05 KB
/
gpg-helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
usage()
{
cat << EOF
usage: $0 COMMAND [OPTIONS]
This script provides various GPG keychain operations
COMMANDS:
encrypt_for_all: encrypt [FILE] for all public keys in [DIR]
import_and_trust: import and ultimately trust and sign all public keys in [DIR]
list_keys: list public key IDs in [DIR]
list_fprs: list public key fingerprints in [DIR]
OPTIONS:
-h Show this message
-d Set [DIR]ectory to use for key operations. Defaults to '.'
-f Set [FILE] for use in encryption operations. Defaults to STDIN
EOF
}
BASEDIR="."
FILE=
OFILE=
KEYRING=
KEYS=
FPRS=
check_for_gpg()
{
if command -v gpg > /dev/null 2>&1; then
:
else
echo "gpg not found! Please install gpg and make sure it is in your PATH!"
exit 1
fi
}
create_keyring()
{
KEYRING=$(mktemp /tmp/keyring.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
OUTPUT=`find $BASEDIR -maxdepth 3 -name '*.key' -o -name '*.asc' -o -name '*.txt' -o -name '*.gpg' \
| xargs gpg --no-default-keyring --keyring $KEYRING --import 2>&1 | egrep 'imported$'`
KEYS=`gpg --no-default-keyring --keyring $KEYRING --list-keys | perl -n -e'/pub \w+\/(\w+)/ && print "$1\n"'`
FPRS=`gpg --no-default-keyring --keyring $KEYRING --list-keys --fingerprint --with-colons | grep fpr | cut -d':' -f10`
RECIP=`gpg --no-default-keyring --keyring $KEYRING --list-keys | perl -n -e'/pub \w+\/(\w+)/ && print "-r $1 "'`
}
remove_keyring()
{
# remove temporary keyring
rm -f $KEYRING
rm -f $KEYRING~
}
encrypt_for_all()
{
gpg --no-default-keyring --keyring $KEYRING --trust-model always -o $FILE.gpg $RECIP -e $FILE
if [ $? -eq 0 ]; then
echo; echo "Encrypted $FILE to $FILE.gpg"; echo
fi
}
import_and_trust()
{
# import keys to user's personal keychain
find $BASEDIR -maxdepth 3 -name '*.key' -o -name '*.asc' -o -name '*.txt' \
| xargs gpg --import 2>&1 | egrep 'imported$'
echo "Trusting keys..."
echo
for f in $FPRS; do
echo "$f:6:" | gpg --import-ownertrust
done
echo
echo "Signing keys..."
echo
for k in $KEYS; do
gpg --batch --yes --sign-key $k 2>&1 | grep "was already signed by"
done
}
list_keys()
{
echo "Listing public key IDs in $BASEDIR" 1>&2
echo 1>&2
for k in $KEYS; do
echo $k
done
}
list_fprs()
{
echo "Listing public key fingerprints in $BASEDIR" 1>&2
echo 1>&2
for k in $FPRS; do
echo $k
done
}
COMMAND=$1
shift
while getopts “hd:f:o:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
d)
BASEDIR=$OPTARG
;;
f)
FILE=$OPTARG
;;
esac
done
# check to see if gpg exists
check_for_gpg
# create the temporary keyring and import keys
create_keyring
case $COMMAND in
encrypt_for_all)
if [ -z "$FILE" ]; then
echo "You must specify -f [FILE] with this command"
exit 1
fi
encrypt_for_all
;;
import_and_trust)
import_and_trust
;;
list_keys)
list_keys
;;
list_fprs)
list_fprs
;;
*)
usage
;;
esac
# remove the temporary keyring
remove_keyring
exit 1