- update GPG UID name/email/comment fields
[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.3 2006/05/08 18:16:33 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
51             result "$MESSAGE"
52         else
53             # Update GPG UID
54             MESSAGE=$"Updating GPG keys"
55             dialog "$MESSAGE"
56
57             # Get the current GPG fingerprint
58             fingerprint=$PLC_MAIL_SUPPORT_ADDRESS
59             (
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=/root --no-tty --yes \
68                         --no-default-keyring --keyring $PLC_ROOT_GPG_KEY_PUB --secret-keyring $PLC_ROOT_GPG_KEY \
69                         --list-public-keys --with-colons
70                     check
71                 )
72             )
73
74             # GPG UIDs cannot and should not normally be changed, but
75             # since we do not certify signatures, we can effectively
76             # change it by adding a new one and deleting the old one.
77             gpg --homedir=/root --no-tty --yes \
78                 --no-default-keyring --keyring $PLC_ROOT_GPG_KEY_PUB --secret-keyring $PLC_ROOT_GPG_KEY \
79                 --command-fd 0 --status-fd 1 --edit-key $fingerprint <<EOF
80 adduid
81 $PLC_NAME Central
82 $PLC_MAIL_SUPPORT_ADDRESS
83 http://$PLC_WWW_HOST/
84 uid 1
85 deluid
86 y
87 save
88 EOF
89             check
90
91             result "$MESSAGE"
92         fi
93         ;;
94 esac
95
96 exit $ERRORS