3 # pacman-key - manages pacman's keyring
4 # Based on apt-key, from Debian
7 # Copyright (c) 2010-2012 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 myver
="@PACKAGE_VERSION@"
48 DEFAULT_KEYSERVER
='hkp://keys.gnupg.net'
50 m4_include
(library
/output_format.sh
)
52 m4_include
(library
/parse_options.sh
)
55 printf "pacman-key (pacman) %s\n" ${myver}
57 printf -- "$(gettext "Usage
: %s
[options
]")\n" $
(basename $0)
59 printf -- "$(gettext "Manage pacman
's list of trusted keys")\n"
61 printf -- "$(gettext "Options:")\n"
62 printf -- "$(gettext " -a, --add [file(s)] Add the specified keys (empty for stdin)")\n"
63 printf -- "$(gettext " -d, --delete <keyid(s)> Remove the specified keyids")\n"
64 printf -- "$(gettext " -e, --export [keyid(s)] Export the specified or all keyids")\n"
65 printf -- "$(gettext " -f, --finger [keyid(s)] List fingerprint for specified or all keyids")\n"
66 printf -- "$(gettext " -h, --help Show this help message and exit")\n"
67 printf -- "$(gettext " -l, --list-keys [keyid(s)] List the specified or all keys")\n"
68 printf -- "$(gettext " -r, --recv-keys <keyid(s)> Fetch the specified keyids")\n"
69 printf -- "$(gettext " -u, --updatedb Update the trustdb of pacman")\n"
70 printf -- "$(gettext " -v, --verify <signature> Verify the file specified by the signature")\n"
71 printf -- "$(gettext " -V, --version Show program version")\n"
72 printf -- "$(gettext " --config <file> Use an alternate config file (instead of\n\
73 '%s
')")\n" "@sysconfdir@/pacman.conf"
74 printf -- "$(gettext " --edit-key <keyid(s)> Present a menu for key management task on keyids")\n"
75 printf -- "$(gettext " --gpgdir <dir> Set an alternate directory for GnuPG (instead\n\
76 of '%s
')")\n" "@sysconfdir@/pacman.d/gnupg"
77 printf -- "$(gettext " --import <dir(s)> Imports pubring.gpg from dir(s)")\n"
78 printf -- "$(gettext " --import-trustdb <dir(s)> Imports ownertrust values from trustdb.gpg in dir(s)")\n"
79 printf -- "$(gettext " --init Ensure the keyring is properly initialized")\n"
80 printf -- "$(gettext " --keyserver Specify a keyserver to use if necessary")\n"
81 printf -- "$(gettext " --list-sigs [keyid(s)] List keys and their signatures")\n"
82 printf -- "$(gettext " --lsign-key <keyid> Locally sign the specified keyid")\n"
83 printf -- "$(gettext " --populate [keyring(s)] Reload the default keys from the (given) keyrings\n\
84 in '%s
'")\n" "@pkgdatadir@/keyrings"
85 printf -- "$(gettext " --refresh-keys [keyid(s)] Update specified or all keys from a keyserver")\n"
89 printf "pacman-key (pacman) %s\n" "${myver}"
90 printf -- "$(gettext "\
91 Copyright (c) 2010-2012 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.
101 while IFS='=' read -r key value; do
102 [[ -z $key || ${key:0:1} = '#' ]] && continue
104 if [[ ${key%% *} = "$2" && -n ${value##* } ]]; then
109 if [[ -n "$3" ]]; then
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...
123 Name-Real: Pacman Keychain Master Key
124 Name-Email: pacman@localhost
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 check_keyids_exist
() {
149 for key
in "${KEYIDS[@]}"; do
150 # Verify if the key exists in pacman's keyring
151 if ! "${GPG_PACMAN[@]}" --list-keys "$key" &>/dev
/null
; then
152 error
"$(gettext "The key identified by
%s could not be found locally.
")" "$key"
162 local conffile keyserv
163 # Check for simple existence rather than for a directory as someone
164 # may want to use a symlink here
165 [[ -e ${PACMAN_KEYRING_DIR} ]] || mkdir
-p -m 755 "${PACMAN_KEYRING_DIR}"
168 [[ -f ${PACMAN_KEYRING_DIR}/pubring.gpg
]] ||
touch ${PACMAN_KEYRING_DIR}/pubring.gpg
169 [[ -f ${PACMAN_KEYRING_DIR}/secring.gpg
]] ||
touch ${PACMAN_KEYRING_DIR}/secring.gpg
170 [[ -f ${PACMAN_KEYRING_DIR}/trustdb.gpg
]] ||
"${GPG_PACMAN[@]}" --update-trustdb
171 chmod 644 ${PACMAN_KEYRING_DIR}/{pubring
,trustdb
}.gpg
172 chmod 600 ${PACMAN_KEYRING_DIR}/secring.gpg
175 conffile
="${PACMAN_KEYRING_DIR}/gpg.conf"
176 [[ -f $conffile ]] ||
touch "$conffile"
177 chmod 644 "$conffile"
178 add_gpg_conf_option
"$conffile" 'no-greeting'
179 add_gpg_conf_option
"$conffile" 'no-permission-warning'
180 add_gpg_conf_option
"$conffile" 'lock-never'
181 keyserv
=${KEYSERVER:-$DEFAULT_KEYSERVER}
182 add_gpg_conf_option
"$conffile" 'keyserver' "$keyserv"
183 add_gpg_conf_option
"$conffile" 'keyserver-options' 'timeout=10'
185 # set up a private signing key (if none available)
186 if [[ $
(secret_keys_available
) -lt 1 ]]; then
193 if [[ ! -r ${PACMAN_KEYRING_DIR}/pubring.gpg || \
194 ! -r ${PACMAN_KEYRING_DIR}/trustdb.gpg
]]; then
195 error
"$(gettext "You
do not have sufficient permissions to
read the
%s keyring.
")" "pacman"
196 msg
"$(gettext "Use
'%s' to correct the keyring permissions.
")" "pacman-key --init"
200 if (( (EXPORT || FINGER || LIST || VERIFY
) && EUID
!= 0 )); then
201 if ! grep -q "^[[:space:]]*lock-never[[:space:]]*$" ${PACMAN_KEYRING_DIR}/gpg.conf
&>/dev
/null
; then
202 error
"$(gettext "You
do not have sufficient permissions to run this
command.
")"
203 msg
"$(gettext "Use
'%s' to correct the keyring permissions.
")" "pacman-key --init"
208 if (( LSIGNKEY
)); then
209 if [[ $
(secret_keys_available
) -lt 1 ]]; then
210 error
"$(gettext "There is no secret key available to sign with.
")"
211 msg
"$(gettext "Use
'%s' to generate a default secret key.
")" "pacman-key --init"
217 validate_with_gpg
() {
218 msg2
"$(gettext "Verifying
%s...
")" "$1"
219 if [[ ! -f "$1.sig" ]]; then
220 error
"$(gettext "File
%s is unsigned
, cannot
continue.
")" "$1"
222 elif ! "${GPG_PACMAN[@]}" --verify "$1.sig"; then
223 error
"$(gettext "The signature of
file %s is not valid.
")" "$1"
229 verify_keyring_input
() {
231 local KEYRING_IMPORT_DIR
='@pkgdatadir@/keyrings'
233 # Verify signatures of keyring files and trusted/revoked files if they exist
234 msg
"$(gettext "Verifying keyring
file signatures...
")"
235 local keyring keyfile
236 for keyring
in "${KEYRINGIDS[@]}"; do
237 keyfile
="${KEYRING_IMPORT_DIR}/${keyring}.gpg"
238 validate_with_gpg
"${keyfile}" || ret
=1
240 keyfile
="${KEYRING_IMPORT_DIR}/${keyring}-trusted"
241 if [[ -f "${keyfile}" ]]; then
242 validate_with_gpg
"${keyfile}" || ret
=1
245 keyfile
="${KEYRING_IMPORT_DIR}/${keyring}-revoked"
246 if [[ -f "${keyfile}" ]]; then
247 validate_with_gpg
"${keyfile}" || ret
=1
255 local KEYRING_IMPORT_DIR
='@pkgdatadir@/keyrings'
259 if [[ -z ${KEYRINGIDS[@]} ]]; then
260 # get list of all available keyrings
262 KEYRINGIDS
=("$KEYRING_IMPORT_DIR"/*.gpg
)
264 KEYRINGIDS
=("${KEYRINGIDS[@]##*/}")
265 KEYRINGIDS
=("${KEYRINGIDS[@]%.gpg}")
266 if [[ -z ${KEYRINGIDS[@]} ]]; then
267 error
"$(gettext "No keyring files exist
in %s.
")" "$KEYRING_IMPORT_DIR"
271 # verify listed keyrings exist
272 for keyring
in "${KEYRINGIDS[@]}"; do
273 if [[ ! -f "$KEYRING_IMPORT_DIR/$keyring.gpg" ]]; then
274 error
"$(gettext "The keyring
file %s does not exist.
")" "$KEYRING_IMPORT_DIR/$keyring.gpg"
284 verify_keyring_input ||
exit 1
286 # Variable used for iterating on keyrings
290 # Add keys from requested keyrings
291 for keyring
in "${KEYRINGIDS[@]}"; do
292 msg
"$(gettext "Appending keys from
%s.gpg...
")" "$keyring"
293 "${GPG_PACMAN[@]}" --import "${KEYRING_IMPORT_DIR}/${keyring}.gpg"
296 # Read the trusted key IDs to an array. Because this is an ownertrust
297 # file, we know we have the full 40 hex digit fingerprint values.
298 # Format of ownertrust dump file:
299 # 40CHARFINGERPRINTXXXXXXXXXXXXXXXXXXXXXXX:6:
300 # 40CHARFINGERPRINTXXXXXXXXXXXXXXXXXXXXXXX:5:
302 for keyring
in "${KEYRINGIDS[@]}"; do
303 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-trusted" ]]; then
305 # skip comments; these are valid in this file
306 [[ $key = \
#* ]] && continue
308 if [[ -n ${key_id} ]]; then
309 # Mark this key to be lsigned
310 trusted_ids
[$key_id]="${keyring}"
312 done < "${KEYRING_IMPORT_DIR}/${keyring}-trusted"
316 if (( ${#trusted_ids[@]} > 0 )); then
317 msg
"$(gettext "Locally signing trusted keys
in keyring...
")"
318 for key_id
in "${!trusted_ids[@]}"; do
319 msg2
"$(gettext "Locally signing key
%s...
")" "${key_id}"
320 "${GPG_PACMAN[@]}" --quiet --lsign-key "${key_id}"
322 msg
"$(gettext "Importing owner trust values...
")"
323 for keyring
in "${KEYRINGIDS[@]}"; do
324 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-trusted" ]]; then
325 "${GPG_PACMAN[@]}" --import-ownertrust "${KEYRING_IMPORT_DIR}/${keyring}-trusted"
330 # Read the revoked key IDs to an array. The conversion from whatever is
331 # inside the file to key ids is important, because key ids are the only
332 # guarantee of identification for the keys.
334 for keyring
in "${KEYRINGIDS[@]}"; do
335 if [[ -f "${KEYRING_IMPORT_DIR}/${keyring}-revoked" ]]; then
337 key_id
="$("${GPG_PACMAN[@]}" --quiet --with-colons --list-key "${key}" 2>/dev/null | grep ^pub | cut -d: -f5)"
338 if [[ -n ${key_id} ]]; then
339 # Mark this key to be disabled
340 revoked_ids
[$key_id]="${keyring}"
342 done < "${KEYRING_IMPORT_DIR}/${keyring}-revoked"
346 if (( ${#revoked_ids[@]} > 0 )); then
347 msg
"$(gettext "Disabling revoked keys
in keyring...
")"
348 for key_id
in "${!revoked_ids[@]}"; do
349 msg2
"$(gettext "Disabling key
%s...
")" "${key_id}"
350 printf 'disable\nquit\n' | LANG
=C
"${GPG_PACMAN[@]}" --command-fd 0 --quiet --batch --edit-key "${key_id}" 2>/dev
/null
356 if ! "${GPG_PACMAN[@]}" --quiet --batch --import "${KEYFILES[@]}" ; then
357 error
"$(gettext "A specified keyfile could not be added to the gpg keychain.
")"
364 if ! "${GPG_PACMAN[@]}" --quiet --batch --delete-key --yes "${KEYIDS[@]}" ; then
365 error
"$(gettext "A specified key could not be removed from the gpg keychain.
")"
373 for key
in "${KEYIDS[@]}"; do
374 if ! "${GPG_PACMAN[@]}" --edit-key "$key" ; then
375 error
"$(gettext "The key identified by
%s could not be edited.
")" "$key"
386 if ! "${GPG_PACMAN[@]}" --armor --export "${KEYIDS[@]}" ; then
387 error
"$(gettext "A specified key could not be exported from the gpg keychain.
")"
394 if ! "${GPG_PACMAN[@]}" --batch --fingerprint "${KEYIDS[@]}" ; then
395 error
"$(gettext "The fingerprint of a specified key could not be determined.
")"
403 for importdir
in "${IMPORT_DIRS[@]}"; do
404 if [[ -f "${importdir}/trustdb.gpg" ]]; then
405 gpg
--homedir "${importdir}" --export-ownertrust | \
406 "${GPG_PACMAN[@]}" --import-ownertrust -
407 if (( PIPESTATUS
)); then
408 error
"$(gettext "%s could not be imported.
")" "${importdir}/trustdb.gpg"
412 error
"$(gettext "File
%s does not exist and could not be imported.
")" "${importdir}/trustdb.gpg"
424 for importdir
in "${IMPORT_DIRS[@]}"; do
425 if [[ -f "${importdir}/pubring.gpg" ]]; then
426 if ! "${GPG_PACMAN[@]}" --quiet --batch --import "${importdir}/pubring.gpg" ; then
427 error
"$(gettext "%s could not be imported.
")" "${importdir}/pubring.gpg"
431 error
"$(gettext "File
%s does not exist and could not be imported.
")" "${importdir}/pubring.gpg"
442 if ! "${GPG_PACMAN[@]}" --batch --list-keys "${KEYIDS[@]}" ; then
443 error
"$(gettext "A specified key could not be listed.
")"
450 if ! "${GPG_PACMAN[@]}" --batch --list-sigs "${KEYIDS[@]}" ; then
451 error
"$(gettext "A specified signature could not be listed.
")"
458 printf 'y\ny\n' | LANG
=C
"${GPG_PACMAN[@]}" --command-fd 0 --quiet --batch --lsign-key "${KEYIDS[@]}" 2>/dev
/null
459 if (( PIPESTATUS
[1] )); then
460 error
"$(gettext "A specified key could not be locally signed.
")"
466 if ! "${GPG_PACMAN[@]}" --recv-keys "${KEYIDS[@]}" ; then
467 error
"$(gettext "Remote key not fetched correctly from keyserver.
")"
474 if ! "${GPG_PACMAN[@]}" --refresh-keys "${KEYIDS[@]}" ; then
475 error
"$(gettext "A specified
local key could not be updated from a keyserver.
")"
481 if ! "${GPG_PACMAN[@]}" --verify $SIGNATURE ; then
482 error
"$(gettext "The signature identified by
%s could not be verified.
")" "$SIGNATURE"
488 msg
"$(gettext "Updating trust database...
")"
489 if ! "${GPG_PACMAN[@]}" --batch --check-trustdb ; then
490 error
"$(gettext "Trust database could not be updated.
")"
496 if ! type gettext &>/dev
/null
; then
502 OPT_SHORT
="a::d:e:f::hl::r:uv:V"
503 OPT_LONG
="add::,config:,delete:,edit-key:,export::,finger::,gpgdir:"
504 OPT_LONG
+=",help,import:,import-trustdb:,init,keyserver:,list-keys::,list-sigs::"
505 OPT_LONG
+=",lsign-key:,populate::,recv-keys:,refresh-keys::,updatedb"
506 OPT_LONG
+=",verify:,version"
507 if ! parse_options
$OPT_SHORT $OPT_LONG "$@"; then
508 echo; usage
; exit 1 # E_INVALID_OPTION;
510 set -- "${OPTRET[@]}"
511 unset OPT_SHORT OPT_LONG OPTRET
513 if [[ $1 == "--" ]]; then
520 -a|
--add) ADD
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYFILES
=($1); UPDATEDB
=1 ;;
521 --config) shift; CONFIG
=$1 ;;
522 -d|
--delete) DELETE
=1; shift; KEYIDS
=($1); UPDATEDB
=1 ;;
523 --edit-key) EDITKEY
=1; shift; KEYIDS
=($1); UPDATEDB
=1 ;;
524 -e|
--export) EXPORT
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS
=($1) ;;
525 -f|
--finger) FINGER
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS
=($1) ;;
526 --gpgdir) shift; PACMAN_KEYRING_DIR
=$1 ;;
527 --import) IMPORT
=1; shift; IMPORT_DIRS
=($1); UPDATEDB
=1 ;;
528 --import-trustdb) IMPORT_TRUSTDB
=1; shift; IMPORT_DIRS
=($1); UPDATEDB
=1 ;;
530 --keyserver) shift; KEYSERVER
=$1 ;;
531 -l|
--list-keys) LISTKEYS
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS
=($1) ;;
532 --list-sigs) LISTSIGS
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS
=($1) ;;
533 --lsign-key) LSIGNKEY
=1; shift; KEYIDS
=($1); UPDATEDB
=1 ;;
534 --populate) POPULATE
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYRINGIDS
=($1); UPDATEDB
=1 ;;
535 -r|
--recv-keys) RECEIVE
=1; shift; KEYIDS
=($1); UPDATEDB
=1 ;;
536 --refresh-keys) REFRESH
=1; [[ -n $2 && ${2:0:1} != "-" ]] && shift && KEYIDS
=($1) ;;
537 -u|
--updatedb) UPDATEDB
=1 ;;
538 -v|
--verify) VERIFY
=1; shift; SIGNATURE
=$1 ;;
540 -h|
--help) usage
; exit 0 ;;
541 -V|
--version) version
; exit 0 ;;
543 --) OPT_IND
=0; shift; break;;
550 if ! type -p gpg
>/dev
/null
; then
551 error
"$(gettext "Cannot
find the
%s binary required
for all
%s operations.
")" "gpg" "pacman-key"
555 if (( (ADD || DELETE || EDITKEY || IMPORT || IMPORT_TRUSTDB || INIT || LSIGNKEY || POPULATE || RECEIVE || REFRESH || UPDATEDB
) && EUID
!= 0 )); then
556 error
"$(gettext "%s needs to be run as root
for this operation.
")" "pacman-key"
560 CONFIG
=${CONFIG:-@sysconfdir@/pacman.conf}
561 if [[ ! -r "${CONFIG}" ]]; then
562 error
"$(gettext "%s configuration
file '%s' not found.
")" "pacman" "$CONFIG"
566 # if PACMAN_KEYRING_DIR isn't assigned, try to get it from the config
567 # file, falling back on a hard default
568 PACMAN_KEYRING_DIR
=${PACMAN_KEYRING_DIR:-$(get_from "$CONFIG" "GPGDir" "@sysconfdir@/pacman.d/gnupg")}
570 GPG_PACMAN
=(gpg
--homedir "${PACMAN_KEYRING_DIR}" --no-permission-warning)
571 if [[ -n ${KEYSERVER} ]]; then
572 GPG_PACMAN
+=(--keyserver "${KEYSERVER}")
575 # check only a single operation has been given
576 # don't include UPDATEDB in here as other opts can induce it
577 numopt
=$
(( ADD
+ DELETE
+ EDITKEY
+ EXPORT
+ FINGER
+ IMPORT
+ IMPORT_TRUSTDB
+
578 INIT
+ LISTKEYS
+ LISTSIGS
+ LSIGNKEY
+ POPULATE
+ RECEIVE
+ REFRESH
+ VERIFY
))
582 if (( ! UPDATEDB
)); then
583 error
"$(gettext "no operation specified
(use
-h for help)")"
588 error
"$(gettext "Multiple operations specified.
")"
589 msg
"$(gettext "Please run
%s with each operation separately.
")" "pacman-key"
594 (( ! INIT
)) && check_keyring
596 (( ADD
)) && add_keys
597 (( DELETE
)) && delete_keys
598 (( EDITKEY
)) && edit_keys
599 (( EXPORT
)) && export_keys
600 (( FINGER
)) && finger_keys
601 (( IMPORT
)) && import
602 (( IMPORT_TRUSTDB
)) && import_trustdb
603 (( INIT
)) && initialize
604 (( LISTKEYS
)) && list_keys
605 (( LISTSIGS
)) && list_sigs
606 (( LSIGNKEY
)) && lsign_keys
607 (( POPULATE
)) && populate_keyring
608 (( RECEIVE
)) && receive_keys
609 (( REFRESH
)) && refresh_keys
610 (( VERIFY
)) && verify_sig
612 (( UPDATEDB
)) && updatedb
616 # vim: set ts=2 sw=2 noet: