pacman: list all unknown targets on removal operation
[pacman-ng.git] / scripts / pacman-key.sh.in
blobe0f48f5c739d384d694d8095afaffd9d4d49544c
1 #!/bin/bash
3 # pacman-key - manages pacman's keyring
4 # Based on apt-key, from Debian
5 # @configure_input@
7 # Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # gettext initialization
24 export TEXTDOMAIN='pacman-scripts'
25 export TEXTDOMAINDIR='@localedir@'
27 declare -r myver="@PACKAGE_VERSION@"
29 # Options
30 ADD=0
31 DELETE=0
32 EDITKEY=0
33 EXPORT=0
34 FINGER=0
35 IMPORT=0
36 IMPORT_TRUSTDB=0
37 INIT=0
38 KEYSERVER=''
39 LISTKEYS=0
40 LISTSIGS=0
41 LSIGNKEY=0
42 POPULATE=0
43 RECEIVE=0
44 REFRESH=0
45 UPDATEDB=0
46 VERIFY=0
48 DEFAULT_KEYSERVER='hkp://keys.gnupg.net'
50 m4_include(library/output_format.sh)
52 m4_include(library/parse_options.sh)
54 usage() {
55 printf "pacman-key (pacman) %s\n" ${myver}
56 echo
57 printf "$(gettext "Usage: %s [options]")\n" $(basename $0)
58 echo
59 printf "$(gettext "Manage pacman's list of trusted keys")\n"
60 echo
61 echo "$(gettext "Options:")"
62 echo "$(gettext " -a, --add [file(s)] Add the specified keys (empty for stdin)")"
63 echo "$(gettext " -d, --delete <keyid(s)> Remove the specified keyids")"
64 echo "$(gettext " -e, --export [keyid(s)] Export the specified or all keyids")"
65 echo "$(gettext " -f, --finger [keyid(s)] List fingerprint for specified or all keyids")"
66 echo "$(gettext " -h, --help Show this help message and exit")"
67 echo "$(gettext " -l, --list-keys [keyid(s)] List the specified or all keys")"
68 echo "$(gettext " -r, --recv-keys <keyid(s)> Fetch the specified keyids")"
69 echo "$(gettext " -u, --updatedb Update the trustdb of pacman")"
70 echo "$(gettext " -v, --verify <signature> Verify the file specified by the signature")"
71 echo "$(gettext " -V, --version Show program version")"
72 printf "$(gettext " --config <file> Use an alternate config file (instead of\n\
73 '%s')")\n" "@sysconfdir@/pacman.conf"
74 echo "$(gettext " --edit-key <keyid(s)> Present a menu for key management task on keyids")"
75 printf "$(gettext " --gpgdir <dir> Set an alternate directory for GnuPG (instead\n\
76 of '%s')")\n" "@sysconfdir@/pacman.d/gnupg"
77 echo "$(gettext " --import <dir(s)> Imports pubring.gpg from dir(s)")"
78 echo "$(gettext " --import-trustdb <dir(s)> Imports ownertrust values from trustdb.gpg in dir(s)")"
79 echo "$(gettext " --init Ensure the keyring is properly initialized")"
80 echo "$(gettext " --keyserver Specify a keyserver to use if necessary")"
81 echo "$(gettext " --list-sigs [keyid(s)] List keys and their signatures")"
82 echo "$(gettext " --lsign-key <keyid> Locally sign the specified keyid")"
83 printf "$(gettext " --populate [keyring(s)] Reload the default keys from the (given) keyrings\n\
84 in '%s'")\n" "@pkgdatadir@/keyrings"
85 echo "$(gettext " --refresh-keys [keyid(s)] Update specified or all keys from a keyserver")"
88 version() {
89 printf "pacman-key (pacman) %s\n" "${myver}"
90 printf "$(gettext "\
91 Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.org>.\n\
92 This is free software; see the source for copying conditions.\n\
93 There is NO WARRANTY, to the extent permitted by law.\n")"
96 # read the config file "$1" which has key=value pairs, and return the key which
97 # matches "$2". the equals sign between pairs may be surrounded by any amount
98 # of whitespace. Optionally, "$3" can be specified which is the default value
99 # when no key is found.
100 get_from() {
101 while IFS='=' read -r key value; do
102 [[ -z $key || ${key:0:1} = '#' ]] && continue
104 if [[ ${key%% *} = "$2" && -n ${value##* } ]]; then
105 echo "${value##* }"
106 return 0
108 done < "$1"
109 if [[ -n "$3" ]]; then
110 printf '%s\n' "$3"
111 return 0
113 return 1
116 generate_master_key() {
117 # Generate the master key, which will be in both pubring and secring
118 "${GPG_PACMAN[@]}" --gen-key --batch <<EOF
119 %echo Generating pacman keychain master key...
120 Key-Type: RSA
121 Key-Length: 2048
122 Key-Usage: sign
123 Name-Real: Pacman Keychain Master Key
124 Name-Email: pacman@localhost
125 Expire-Date: 0
126 %commit
127 %echo Done
131 secret_keys_available() {
132 "${GPG_PACMAN[@]}" -K --with-colons | wc -l
135 # Adds the given gpg.conf option if it is not present in the file.
136 # Note that if we find it commented out, we won't add the option.
137 # args: $1 conffile, $2 option-name, $3 (optional) option-value
138 add_gpg_conf_option() {
139 local conffile=$1; shift
140 # looking for the option 'bare', only leading spaces or # chars allowed,
141 # followed by at least one space and any other text or the end of line.
142 if ! grep -q "^[[:space:]#]*$1\([[:space:]].*\)*$" "$conffile" &>/dev/null; then
143 printf '%s\n' "$*" >> "$conffile"
147 initialize() {
148 local conffile keyserv
149 # Check for simple existence rather than for a directory as someone
150 # may want to use a symlink here
151 [[ -e ${PACMAN_KEYRING_DIR} ]] || mkdir -p -m 755 "${PACMAN_KEYRING_DIR}"
153 # keyring files
154 [[ -f ${PACMAN_KEYRING_DIR}/pubring.gpg ]] || touch ${PACMAN_KEYRING_DIR}/pubring.gpg
155 [[ -f ${PACMAN_KEYRING_DIR}/secring.gpg ]] || touch ${PACMAN_KEYRING_DIR}/secring.gpg
156 [[ -f ${PACMAN_KEYRING_DIR}/trustdb.gpg ]] || "${GPG_PACMAN[@]}" --update-trustdb
157 chmod 644 ${PACMAN_KEYRING_DIR}/{pubring,trustdb}.gpg
158 chmod 600 ${PACMAN_KEYRING_DIR}/secring.gpg
160 # gpg.conf
161 conffile="${PACMAN_KEYRING_DIR}/gpg.conf"
162 [[ -f $conffile ]] || touch "$conffile"
163 chmod 644 "$conffile"
164 add_gpg_conf_option "$conffile" 'no-greeting'
165 add_gpg_conf_option "$conffile" 'no-permission-warning'
166 add_gpg_conf_option "$conffile" 'lock-never'
167 keyserv=${KEYSERVER:-$DEFAULT_KEYSERVER}
168 add_gpg_conf_option "$conffile" 'keyserver' "$keyserv"
169 add_gpg_conf_option "$conffile" 'keyserver-options' 'timeout=10'
171 # set up a private signing key (if none available)
172 if [[ $(secret_keys_available) -lt 1 ]]; then
173 generate_master_key
174 UPDATEDB=1
178 check_keyring() {
179 if [[ ! -r ${PACMAN_KEYRING_DIR}/pubring.gpg || \
180 ! -r ${PACMAN_KEYRING_DIR}/trustdb.gpg ]]; then
181 error "$(gettext "You do not have sufficient permissions to read the %s keyring.")" "pacman"
182 msg "$(gettext "Use '%s' to correct the keyring permissions.")" "pacman-key --init"
183 exit 1
186 if (( (EXPORT || FINGER || LIST || VERIFY) && EUID != 0 )); then
187 if ! grep -q "^[[:space:]]*lock-never[[:space:]]*$" ${PACMAN_KEYRING_DIR}/gpg.conf &>/dev/null; then
188 error "$(gettext "You do not have sufficient permissions to run this command.")"
189 msg "$(gettext "Use '%s' to correct the keyring permissions.")" "pacman-key --init"
190 exit 1
194 if (( LSIGNKEY )); then
195 if [[ $(secret_keys_available) -lt 1 ]]; then
196 error "$(gettext "There is no secret key available to sign with.")"
197 msg "$(gettext "Use '%s' to generate a default secret key.")" "pacman-key --init"
198 exit 1
203 validate_with_gpg() {
204 msg2 "$(gettext "Verifying %s...")" "$1"
205 if [[ ! -f "$1.sig" ]]; then
206 error "$(gettext "File %s is unsigned, cannot continue.")" "$1"
207 return 1
208 elif ! "${GPG_PACMAN[@]}" --verify "$1.sig"; then
209 error "$(gettext "The signature of file %s is not valid.")" "$1"
210 return 1
212 return 0
215 verify_keyring_input() {
216 local ret=0;
217 local KEYRING_IMPORT_DIR='@pkgdatadir@/keyrings'
219 # Verify signatures of keyring files and trusted/revoked files if they exist
220 msg "$(gettext "Verifying keyring file signatures...")"
221 local keyring keyfile
222 for keyring in "${KEYRINGIDS[@]}"; do
223 keyfile="${KEYRING_IMPORT_DIR}/${keyring}.gpg"
224 validate_with_gpg "${keyfile}" || ret=1
226 keyfile="${KEYRING_IMPORT_DIR}/${keyring}-trusted"
227 if [[ -f "${keyfile}" ]]; then
228 validate_with_gpg "${keyfile}" || ret=1
231 keyfile="${KEYRING_IMPORT_DIR}/${keyring}-revoked"
232 if [[ -f "${keyfile}" ]]; then
233 validate_with_gpg "${keyfile}" || ret=1
235 done
237 return $ret
240 populate_keyring() {
241 local KEYRING_IMPORT_DIR='@pkgdatadir@/keyrings'
243 local keyring
244 local ret=0
245 if [[ -z ${KEYRINGIDS[@]} ]]; then
246 # get list of all available keyrings
247 shopt -s nullglob
248 KEYRINGIDS=("$KEYRING_IMPORT_DIR"/*.gpg)
249 shopt -u nullglob
250 KEYRINGIDS=("${KEYRINGIDS[@]##*/}")
251 KEYRINGIDS=("${KEYRINGIDS[@]%.gpg}")
252 if [[ -z ${KEYRINGIDS[@]} ]]; then
253 error "$(gettext "No keyring files exist in %s.")" "$KEYRING_IMPORT_DIR"
254 ret=1
256 else
257 # verify listed keyrings exist
258 for keyring in "${KEYRINGIDS[@]}"; do
259 if [[ ! -f "$KEYRING_IMPORT_DIR/$keyring.gpg" ]]; then
260 error "$(gettext "The keyring file %s does not exist.")" "$KEYRING_IMPORT_DIR/$keyring.gpg"
261 ret=1
263 done
266 if (( ret )); then
267 exit 1
270 verify_keyring_input || exit 1
272 # Variable used for iterating on keyrings
273 local key
274 local key_id
276 # Add keys from requested keyrings
277 for keyring in "${KEYRINGIDS[@]}"; do
278 msg "$(gettext "Appending keys from %s.gpg...")" "$keyring"
279 "${GPG_PACMAN[@]}" --import "${KEYRING_IMPORT_DIR}/${keyring}.gpg"
280 done
282 # Read the trusted key IDs to an array. Because this is an ownertrust
283 # file, we know we have the full 40 hex digit fingerprint values.
284 # Format of ownertrust dump file:
285 # 40CHARFINGERPRINTXXXXXXXXXXXXXXXXXXXXXXX:6:
286 # 40CHARFINGERPRINTXXXXXXXXXXXXXXXXXXXXXXX:5:
287 local -A trusted_ids
288 for keyring in "${KEYRINGIDS[@]}"; do
289 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-trusted" ]]; then
290 while read key; do
291 # skip comments; these are valid in this file
292 [[ $key = \#* ]] && continue
293 key_id="${key%%:*}"
294 if [[ -n ${key_id} ]]; then
295 # Mark this key to be lsigned
296 trusted_ids[$key_id]="${keyring}"
298 done < "${KEYRING_IMPORT_DIR}/${keyring}-trusted"
300 done
302 if (( ${#trusted_ids[@]} > 0 )); then
303 msg "$(gettext "Locally signing trusted keys in keyring...")"
304 for key_id in "${!trusted_ids[@]}"; do
305 msg2 "$(gettext "Locally signing key %s...")" "${key_id}"
306 "${GPG_PACMAN[@]}" --quiet --lsign-key "${key_id}"
307 done
308 msg "$(gettext "Importing owner trust values...")"
309 for keyring in "${KEYRINGIDS[@]}"; do
310 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-trusted" ]]; then
311 "${GPG_PACMAN[@]}" --import-ownertrust "${KEYRING_IMPORT_DIR}/${keyring}-trusted"
313 done
316 # Read the revoked key IDs to an array. The conversion from whatever is
317 # inside the file to key ids is important, because key ids are the only
318 # guarantee of identification for the keys.
319 local -A revoked_ids
320 for keyring in "${KEYRINGIDS[@]}"; do
321 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-revoked" ]]; then
322 while read key; do
323 key_id="$("${GPG_PACMAN[@]}" --quiet --with-colons --list-key "${key}" 2>/dev/null | grep ^pub | cut -d: -f5)"
324 if [[ -n ${key_id} ]]; then
325 # Mark this key to be disabled
326 revoked_ids[$key_id]="${keyring}"
328 done < "${KEYRING_IMPORT_DIR}/${keyring}-revoked"
330 done
332 if (( ${#revoked_ids[@]} > 0 )); then
333 msg "$(gettext "Disabling revoked keys in keyring...")"
334 for key_id in "${!revoked_ids[@]}"; do
335 msg2 "$(gettext "Disabling key %s...")" "${key_id}"
336 printf 'disable\nquit\n' | LANG=C "${GPG_PACMAN[@]}" --command-fd 0 --quiet --batch --edit-key "${key_id}" 2>/dev/null
337 done
341 edit_keys() {
342 local errors=0;
343 for key in "${KEYIDS[@]}"; do
344 # Verify if the key exists in pacman's keyring
345 if ! "${GPG_PACMAN[@]}" --list-keys "$key" &>/dev/null; then
346 error "$(gettext "The key identified by %s does not exist.")" "$key"
347 errors=1;
349 done
350 (( errors )) && exit 1;
352 for key in "${KEYIDS[@]}"; do
353 "${GPG_PACMAN[@]}" --edit-key "$key"
354 done
357 import_trustdb() {
358 local importdir
360 for importdir in "${IMPORT_DIRS[@]}"; do
361 if [[ -f "${importdir}/trustdb.gpg" ]]; then
362 gpg --homedir "${importdir}" --export-ownertrust | \
363 "${GPG_PACMAN[@]}" --import-ownertrust -
365 done
368 import() {
369 local importdir
371 for importdir in "${IMPORT_DIRS[@]}"; do
372 if [[ -f "${importdir}/pubring.gpg" ]]; then
373 "${GPG_PACMAN[@]}" --quiet --batch --import "${importdir}/pubring.gpg"
375 done
378 # PROGRAM START
379 if ! type gettext &>/dev/null; then
380 gettext() {
381 echo "$@"
385 OPT_SHORT="a::d:e:f::hl::r:uv:V"
386 OPT_LONG="add::,config:,delete:,edit-key:,export::,finger::,gpgdir:"
387 OPT_LONG+=",help,import:,import-trustdb:,init,keyserver:,list-keys::,list-sigs::"
388 OPT_LONG+=",lsign-key:,populate::,recv-keys:,refresh-keys::,updatedb"
389 OPT_LONG+=",verify:,version"
390 if ! OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@")"; then
391 echo; usage; exit 1 # E_INVALID_OPTION;
393 eval set -- "$OPT_TEMP"
394 unset OPT_SHORT OPT_LONG OPT_TEMP
396 if [[ $1 == "--" ]]; then
397 usage;
398 exit 0;
401 while true; do
402 case "$1" in
403 -a|--add) ADD=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYFILES=($1); UPDATEDB=1 ;;
404 --config) shift; CONFIG=$1 ;;
405 -d|--delete) DELETE=1; shift; KEYIDS=($1); UPDATEDB=1 ;;
406 --edit-key) EDITKEY=1; shift; KEYIDS=($1); UPDATEDB=1 ;;
407 -e|--export) EXPORT=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS=($1) ;;
408 -f|--finger) FINGER=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS=($1) ;;
409 --gpgdir) shift; PACMAN_KEYRING_DIR=$1 ;;
410 --import) IMPORT=1; shift; IMPORT_DIRS=($1); UPDATEDB=1 ;;
411 --import-trustdb) IMPORT_TRUSTDB=1; shift; IMPORT_DIRS=($1); UPDATEDB=1 ;;
412 --init) INIT=1 ;;
413 --keyserver) shift; KEYSERVER=$1 ;;
414 -l|--list-keys) LISTKEYS=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS=($1) ;;
415 --list-sigs) LISTSIGS=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS=($1) ;;
416 --lsign-key) LSIGNKEY=1; shift; KEYIDS=($1); UPDATEDB=1 ;;
417 --populate) POPULATE=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYRINGIDS=($1); UPDATEDB=1 ;;
418 -r|--recv-keys) RECEIVE=1; shift; KEYIDS=($1); UPDATEDB=1 ;;
419 --refresh-keys) REFRESH=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS=($1) ;;
420 -u|--updatedb) UPDATEDB=1 ;;
421 -v|--verify) VERIFY=1; shift; SIGNATURE=$1 ;;
423 -h|--help) usage; exit 0 ;;
424 -V|--version) version; exit 0 ;;
426 --) OPT_IND=0; shift; break;;
427 *) usage; exit 1 ;;
428 esac
429 shift
430 done
433 if ! type -p gpg >/dev/null; then
434 error "$(gettext "Cannot find the %s binary required for all %s operations.")" "gpg" "pacman-key"
435 exit 1
438 if (( (ADD || DELETE || EDITKEY || IMPORT || IMPORT_TRUSTDB || INIT || LSIGNKEY || POPULATE || RECEIVE || REFRESH || UPDATEDB) && EUID != 0 )); then
439 error "$(gettext "%s needs to be run as root for this operation.")" "pacman-key"
440 exit 1
443 CONFIG=${CONFIG:-@sysconfdir@/pacman.conf}
444 if [[ ! -r "${CONFIG}" ]]; then
445 error "$(gettext "%s configuration file '%s' not found.")" "pacman" "$CONFIG"
446 exit 1
449 # if PACMAN_KEYRING_DIR isn't assigned, try to get it from the config
450 # file, falling back on a hard default
451 PACMAN_KEYRING_DIR=${PACMAN_KEYRING_DIR:-$(get_from "$CONFIG" "GPGDir" "@sysconfdir@/pacman.d/gnupg")}
453 GPG_PACMAN=(gpg --homedir ${PACMAN_KEYRING_DIR} --no-permission-warning)
454 if [[ -n ${KEYSERVER} ]]; then
455 GPG_PACMAN+=(--keyserver ${KEYSERVER})
458 # check only a single operation has been given
459 # don't include UPDATEDB in here as other opts can induce it
460 numopt=$(( ADD + DELETE + EDITKEY + EXPORT + FINGER + IMPORT + IMPORT_TRUSTDB +
461 INIT + LISTKEYS + LISTSIGS + LSIGNKEY + POPULATE + RECEIVE + REFRESH + VERIFY ))
463 case $numopt in
465 if (( ! UPDATEDB )); then
466 error "$(gettext "no operation specified (use -h for help)")"
467 exit 1
470 [!1])
471 error "$(gettext "Multiple operations specified.")"
472 msg "$(gettext "Please run %s with each operation separately.")" "pacman-key"
473 exit 1
475 esac
477 (( ! INIT )) && check_keyring
479 (( ADD )) && "${GPG_PACMAN[@]}" --quiet --batch --import "${KEYFILES[@]}"
480 (( DELETE )) && "${GPG_PACMAN[@]}" --quiet --batch --delete-key --yes "${KEYIDS[@]}"
481 (( EDITKEY )) && edit_keys
482 (( EXPORT )) && "${GPG_PACMAN[@]}" --armor --export "${KEYIDS[@]}"
483 (( FINGER )) && "${GPG_PACMAN[@]}" --batch --fingerprint "${KEYIDS[@]}"
484 (( IMPORT )) && import
485 (( IMPORT_TRUSTDB)) && import_trustdb
486 (( INIT )) && initialize
487 (( LISTKEYS )) && "${GPG_PACMAN[@]}" --batch --list-keys "${KEYIDS[@]}"
488 (( LISTSIGS )) && "${GPG_PACMAN[@]}" --batch --list-sigs "${KEYIDS[@]}"
489 if (( LSIGNKEY )); then
490 printf 'y\ny\n' | LANG=C "${GPG_PACMAN[@]}" --command-fd 0 --quiet --batch --lsign-key "${KEYIDS[@]}" 2>/dev/null
492 (( POPULATE )) && populate_keyring
493 (( RECEIVE )) && "${GPG_PACMAN[@]}" --recv-keys "${KEYIDS[@]}"
494 (( REFRESH )) && "${GPG_PACMAN[@]}" --refresh-keys "${KEYIDS[@]}"
495 (( VERIFY )) && "${GPG_PACMAN[@]}" --verify $SIGNATURE
497 if (( UPDATEDB )); then
498 msg "$(gettext "Updating trust database...")"
499 "${GPG_PACMAN[@]}" --batch --check-trustdb
502 # vim: set ts=2 sw=2 noet: