23428850609ef8924795564780e53edd54855da9
[myplc.git] / plc.d / gpg
1 #!/bin/bash
2 # $Id$
3 # $URL$
4 #
5 # priority: 400
6 #
7 # Generate GPG keys
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 case "$1" in
21     start)
22         # Make temporary GPG home directory
23         homedir=$(mktemp -d /tmp/gpg.XXXXXX)
24
25         # in case a previous gpg invocation failed in some weird way
26         # and left behind a zero length gpg key (pub or priv).
27         if [ -f $PLC_ROOT_GPG_KEY_PUB -a ! -s $PLC_ROOT_GPG_KEY_PUB ] ; then
28             rm -f $PLC_ROOT_GPG_KEY_PUB 
29         fi
30         if [ -f $PLC_ROOT_GPG_KEY -a ! -s $PLC_ROOT_GPG_KEY ] ; then
31             rm -f $PLC_ROOT_GPG_KEY
32         fi
33
34         if [ ! -f $PLC_ROOT_GPG_KEY_PUB -o ! -f $PLC_ROOT_GPG_KEY ] ; then
35             # Generate new GPG keyring
36             MESSAGE=$"Generating GPG keys"
37             dialog "$MESSAGE"
38
39             mkdir -p $(dirname $PLC_ROOT_GPG_KEY_PUB)
40             mkdir -p $(dirname $PLC_ROOT_GPG_KEY)
41
42             # Temporarily replace /dev/random with /dev/urandom to
43             # avoid running out of entropy.
44             rm -f /dev/random
45             # 1 9 is /dev/urandom
46             mknod /dev/random c 1 9
47             # sometimes mknod fails within an improperly setup vserver
48             check
49             gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
50                 --gen-key <<EOF
51 Key-Type: DSA
52 Key-Length: 1024
53 Subkey-Type: ELG-E
54 Subkey-Length: 1024
55 Name-Real: $PLC_NAME Central
56 Name-Comment: http://$PLC_WWW_HOST/
57 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
58 Expire-Date: 0
59 %pubring $PLC_ROOT_GPG_KEY_PUB
60 %secring $PLC_ROOT_GPG_KEY
61 %commit
62 EOF
63             check
64             rm -f /dev/random
65             mknod /dev/random c 1 8
66         else
67             # Update GPG UID
68             MESSAGE=$"Updating GPG keys"
69             dialog "$MESSAGE"
70
71             # Get the current GPG fingerprint and comment
72             OLDIFS=$IFS
73             IFS=:
74             while read -a fields ; do
75                 if [ "${fields[0]}" = "pub" ] ; then
76                     fingerprint=${fields[4]}
77                     break
78                 fi
79             done < <(
80                 gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
81                     --no-default-keyring \
82                     --secret-keyring=$PLC_ROOT_GPG_KEY \
83                     --keyring=$PLC_ROOT_GPG_KEY_PUB \
84                     --list-public-keys --with-colons
85                 check
86             )
87             IFS=$OLDIFS
88
89             # Add a new UID if appropriate. GPG will detect and merge duplicates.
90             gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
91                 --no-default-keyring \
92                 --secret-keyring=$PLC_ROOT_GPG_KEY \
93                 --keyring=$PLC_ROOT_GPG_KEY_PUB \
94                 --command-fd 0 --status-fd 1 --edit-key $fingerprint <<EOF
95 adduid
96 $PLC_NAME Central
97 $PLC_MAIL_SUPPORT_ADDRESS
98 http://$PLC_WWW_HOST/
99 save
100 EOF
101             check
102         fi
103
104         # Install the key in the RPM database
105         mkdir -p /etc/pki/rpm-gpg
106         gpg --homedir=$homedir --no-permission-warning --batch --no-tty --yes \
107             --no-default-keyring \
108             --secret-keyring=$PLC_ROOT_GPG_KEY \
109             --keyring=$PLC_ROOT_GPG_KEY_PUB \
110             --export --armor >"/etc/pki/rpm-gpg/RPM-GPG-KEY-$PLC_NAME"
111         check
112         if rpm -q gpg-pubkey ; then
113             rpm --allmatches -e gpg-pubkey
114             check
115         fi
116         # starting with rpm-4.6, this fails when run a second time
117         # it would be complex to do this properly based on the filename, 
118         # as /etc/pki/rpm-gpg/ typically has many symlinks to the same file
119         # see also http://fedoranews.org/tchung/gpg/
120         # so just ignore the result
121         rpm --import /etc/pki/rpm-gpg/* || :
122         check
123
124         # Make GPG key readable by apache so that the API can sign peer requests
125         chown apache $PLC_ROOT_GPG_KEY
126         chmod 644 $PLC_ROOT_GPG_KEY_PUB
127         chmod 600 $PLC_ROOT_GPG_KEY
128         check
129
130         # Cleanup
131         rm -rf $homedir
132
133         result "$MESSAGE"
134         ;;
135 esac
136
137 exit $ERRORS