04ef2fcf6163f7936077a746b921d4ace3db4c8c
[myplc.git] / plc.d / gpg
1 #!/bin/bash
2 #
3 # priority: 500
4 #
5 # Generate GPG keys
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: gpg,v 1.6 2006/05/18 17:34:10 mlhuang Exp $
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         if [ ! -f $PLC_ROOT_GPG_KEY_PUB -o ! -f $PLC_ROOT_GPG_KEY ] ; then
23             # Generate new GPG keyring
24             MESSAGE=$"Generating GPG keys"
25             dialog "$MESSAGE"
26
27             mkdir -p $(dirname $PLC_ROOT_GPG_KEY_PUB)
28             mkdir -p $(dirname $PLC_ROOT_GPG_KEY)
29
30             # Temporarily replace /dev/random with /dev/urandom to
31             # avoid running out of entropy.
32             rm -f /dev/random
33             mknod /dev/random c 1 9
34             gpg --homedir=/root --no-tty --yes \
35                 --batch --gen-key <<EOF
36 Key-Type: DSA
37 Key-Length: 1024
38 Subkey-Type: ELG-E
39 Subkey-Length: 1024
40 Name-Real: $PLC_NAME Central
41 Name-Comment: http://$PLC_WWW_HOST/
42 Name-Email: $PLC_MAIL_SUPPORT_ADDRESS
43 Expire-Date: 0
44 %pubring $PLC_ROOT_GPG_KEY_PUB
45 %secring $PLC_ROOT_GPG_KEY
46 %commit
47 EOF
48             check
49             rm -f /dev/random
50             mknod /dev/random c 1 8
51             chmod 644 $PLC_ROOT_GPG_KEY_PUB
52             chmod 600 $PLC_ROOT_GPG_KEY
53         else
54             # Update GPG UID
55             MESSAGE=$"Updating GPG keys"
56             dialog "$MESSAGE"
57
58             # Get the current GPG fingerprint and comment
59             OLDIFS=$IFS
60             IFS=:
61             while read -a fields ; do
62                 if [ "${fields[0]}" = "pub" ] ; then
63                     fingerprint=${fields[4]}
64                     break
65                 fi
66             done < <(
67                 gpg --homedir=/etc/planetlab --no-permission-warning --no-tty --yes \
68                     --list-public-keys --with-colons
69                 check
70             )
71             IFS=$OLDIFS
72
73             # Add a new UID if appropriate. GPG will detect and merge duplicates.
74             gpg --homedir=/etc/planetlab --no-permission-warning --no-tty --yes \
75                 --command-fd 0 --status-fd 1 --edit-key $fingerprint <<EOF
76 adduid
77 $PLC_NAME Central
78 $PLC_MAIL_SUPPORT_ADDRESS
79 http://$PLC_WWW_HOST/
80 save
81 EOF
82             check
83         fi
84
85         # Install the key in the RPM database
86         mkdir -p /etc/pki/rpm-gpg
87         gpg --homedir=/etc/planetlab --no-permission-warning --no-tty --yes \
88             --export --armor >"/etc/pki/rpm-gpg/RPM-GPG-KEY-$PLC_NAME"
89         check
90         if rpm -q gpg-pubkey ; then
91             rpm --allmatches -e gpg-pubkey
92             check
93         fi
94         rpm --import /etc/pki/rpm-gpg/*
95         check
96
97         result "$MESSAGE"
98         ;;
99 esac
100
101 exit $ERRORS