7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
15 ### IMPORTANT NOTE 2020 - feb
16 # when moving to fedora31 I run into this
17 # https://fedoraproject.org/wiki/Changes/GnuPG2_as_default_GPG_implementation
18 # which breaks the whole system for us because
19 # * gnupg2 key generation function won't work as expected
20 # * but with much wider impact, it turns out that private keys
21 # are now stored in a completely different way, and this will affect
22 # the way that particular location (typically /etc/planetlab/secring.gpg)
24 # * configured (as $PLC_ROOT_GPG_KEY)
25 # * and passed around (see the PLC.GPG module and its gpg_sign() function)
27 # so for now it looks MUCH EASIER to just get fedora to install gnupg1
28 # instead of (or on top of) gnupg, and use gpg1 when available
29 # below is a leftover of the beginning of a code adaptation
30 # to gnupg2, that should work fine (took some time to get right actually)
31 # but this is currently unused
33 # the default gpg command is version 1 up to f29, version 2 starts with f31
34 # that could be more for when we support both
35 GPG_MAJOR_VERSION=$(gpg --version | grep '^gpg' | cut -d' ' -f 3 | cut -d. -f1)
37 function generate_key_v1() {
39 gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes --gen-key << EOF
44 Name-Real: $PLC_NAME Central
45 Name-Comment: http://$PLC_WWW_HOST/
46 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
48 %pubring $PLC_ROOT_GPG_KEY_PUB
49 %secring $PLC_ROOT_GPG_KEY
54 # this code should work allright as far as key generation, but as explained above
55 # moving to gnupg2 requires a lot more work all over the place...
56 function generate_key_v2() {
57 >&2 echo "it appears you have GPGv2 installed, myPLC is not ready for that !"
61 gpg --homedir=$homedir --generate-key --batch << EOF
66 Name-Real: $PLC_NAME Central
67 Name-Comment: http://$PLC_WWW_HOST/
68 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
70 %pubring $PLC_ROOT_GPG_KEY_PUB
81 # Make temporary GPG home directory
82 homedir=$(mktemp -d /tmp/gpg.XXXXXX)
84 # in case a previous gpg invocation failed in some weird way
85 # and left behind a zero length gpg key (pub or priv).
86 if [ -f $PLC_ROOT_GPG_KEY_PUB -a ! -s $PLC_ROOT_GPG_KEY_PUB ] ; then
87 rm -f $PLC_ROOT_GPG_KEY_PUB
89 if [ -f $PLC_ROOT_GPG_KEY -a ! -s $PLC_ROOT_GPG_KEY ] ; then
90 rm -f $PLC_ROOT_GPG_KEY
93 if [ ! -f $PLC_ROOT_GPG_KEY_PUB -o ! -f $PLC_ROOT_GPG_KEY ] ; then
94 # Generate new GPG keyring
95 MESSAGE=$"Generating GPG keys"
98 mkdir -p $(dirname $PLC_ROOT_GPG_KEY_PUB)
99 mkdir -p $(dirname $PLC_ROOT_GPG_KEY)
101 # Temporarily replace /dev/random with /dev/urandom to
102 # avoid running out of entropy.
103 # (1 9 is /dev/urandom, 1 8 is /dev/random)
105 # a former version of this was rm'ing /dev/random and re-creating it afterwards
106 # however in 1.0.4 libvirt won't allow the use of mknod at all, so let's work around that
107 # by moving things around instead
109 # if we find this file it's probably that a previous run has failed..
110 [ -f /dev/random.preserve ] && { echo "Unexpected file /dev/random.preserve - exiting" ; exit 1; }
111 mv -f /dev/random /dev/random.preserve
112 # doesn't hurt to check
114 ln -s /dev/urandom /dev/random
117 if [ "$GPG_MAJOR_VERSION" == 1 ]; then
118 generate_key_v1 $homedir
120 generate_key_v2 $homedir
123 mv -f /dev/random.preserve /dev/random
127 MESSAGE=$"Updating GPG keys"
130 # Get the current GPG fingerprint and comment
133 while read -a fields ; do
134 if [ "${fields[0]}" = "pub" ] ; then
135 fingerprint=${fields[4]}
139 gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
140 --no-default-keyring \
141 --secret-keyring=$PLC_ROOT_GPG_KEY \
142 --keyring=$PLC_ROOT_GPG_KEY_PUB \
143 --list-public-keys --with-colons
148 # Add a new UID if appropriate. GPG will detect and merge duplicates.
149 gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
150 --no-default-keyring \
151 --secret-keyring=$PLC_ROOT_GPG_KEY \
152 --keyring=$PLC_ROOT_GPG_KEY_PUB \
153 --command-fd 0 --status-fd 1 --edit-key $fingerprint <<EOF
156 $PLC_MAIL_SUPPORT_ADDRESS
157 http://$PLC_WWW_HOST/
163 # Install the key in the RPM database
164 mkdir -p /etc/pki/rpm-gpg
165 gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
166 --no-default-keyring \
167 --secret-keyring=$PLC_ROOT_GPG_KEY \
168 --keyring=$PLC_ROOT_GPG_KEY_PUB \
169 --export --armor >"/etc/pki/rpm-gpg/RPM-GPG-KEY-$PLC_NAME"
171 if rpm -q gpg-pubkey ; then
172 rpm --allmatches -e gpg-pubkey
175 # starting with rpm-4.6, this fails when run a second time
176 # it would be complex to do this properly based on the filename,
177 # as /etc/pki/rpm-gpg/ typically has many symlinks to the same file
178 # see also http://fedoranews.org/tchung/gpg/
179 # so just ignore the result
180 rpm --import /etc/pki/rpm-gpg/* || :
183 # Make GPG key readable by apache so that the API can sign peer requests
184 chown apache $PLC_ROOT_GPG_KEY
185 chmod 644 $PLC_ROOT_GPG_KEY_PUB
186 chmod 600 $PLC_ROOT_GPG_KEY