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