- whenever we generate a new self signed certificate, replace the
[myplc.git] / plc.d / ssl
1 #!/bin/bash
2 #
3 # priority: 400
4 #
5 # Generate SSL certificates
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: ssl,v 1.1 2006/04/06 21:51:59 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15
16 case "$1" in
17     start)
18         MESSAGE=$"Generating SSL certificates"
19         dialog "$MESSAGE"
20
21         # Generate self-signed SSL certificate(s). These nice
22         # commands come from the mod_ssl spec file for Fedora Core
23         # 2. We generate a certificate for each enabled server
24         # with a different hostname. These self-signed
25         # certificates may be overridden later.
26         for server in WWW API BOOT ; do
27             ssl_key=PLC_${server}_SSL_KEY
28             ssl_crt=PLC_${server}_SSL_CRT
29             hostname=PLC_${server}_HOST
30
31             # Check if we have already generated a certificate for
32             # the same hostname.
33             for previous_server in WWW API BOOT ; do
34                 if [ "$server" = "$previous_server" ] ; then
35                     break
36                 fi
37                 previous_ssl_key=PLC_${previous_server}_SSL_KEY
38                 previous_ssl_crt=PLC_${previous_server}_SSL_CRT
39                 previous_hostname=PLC_${previous_server}_HOST
40
41                 if [ -f ${!previous_ssl_crt} ] && \
42                     [ "$(ssl_cname ${!previous_ssl_crt})" = "${!hostname}" ] ; then
43                     cp -a ${!previous_ssl_key} ${!ssl_key}
44                     cp -a ${!previous_ssl_crt} ${!ssl_crt}
45                     break
46                 fi
47             done
48
49             # Check if self signed certificate is valid
50             if [ -f ${!ssl_crt} ] ; then
51                 verify=$(openssl verify ${!ssl_crt})
52                 # If self signed
53                 if grep -q "self signed certificate" <<<$verify ; then
54                     # Delete if expired or hostname changed
55                     if grep -q "expired" <<<$verify || \
56                         [ "$(ssl_cname ${!ssl_crt})" != "${!hostname}" ] ; then
57                         rm -f ${!ssl_crt}
58                     fi
59                 else
60                     echo "$verify" >&2
61                 fi
62             fi
63
64             # Generate new self signed certificate
65             if [ ! -f ${!ssl_crt} ] ; then
66                 mkdir -p $(dirname ${!ssl_crt})
67                 openssl req -new -x509 -days 365 -set_serial $RANDOM \
68                     -nodes -keyout ${!ssl_key} -out ${!ssl_crt} <<EOF
69 --
70 US
71 NJ
72 Princeton
73 PlanetLab Federation
74 $PLC_NAME Central
75 ${!hostname}
76 $PLC_MAIL_SUPPORT_ADDRESS
77 EOF
78                 check
79                 chmod 644 ${!ssl_crt}
80             fi
81         done
82
83         # API requires a public key for slice ticket verification
84         if [ ! -f $PLC_API_SSL_KEY_PUB ] ; then
85             openssl rsa -pubout <$PLC_API_SSL_KEY >$PLC_API_SSL_KEY_PUB
86             check
87         fi
88
89         # Install into both /etc/pki (Fedora Core 4) and
90         # /etc/httpd/conf (Fedora Core 2). If the API, boot, and
91         # web servers are all running on the same machine, the web
92         # server certificate takes precedence.
93         for server in API BOOT WWW ; do
94             enabled=PLC_${server}_ENABLED
95             if [ "${!enabled}" != "1" ] ; then
96                 continue
97             fi
98             ssl_key=PLC_${server}_SSL_KEY
99             ssl_crt=PLC_${server}_SSL_CRT
100
101             symlink ${!ssl_crt} /etc/pki/tls/certs/localhost.crt
102             symlink ${!ssl_key} /etc/pki/tls/private/localhost.key
103             symlink ${!ssl_crt} /etc/httpd/conf/ssl.crt/server.crt
104             symlink ${!ssl_key} /etc/httpd/conf/ssl.key/server.key
105         done
106
107         result "$MESSAGE"
108         ;;
109 esac
110
111 exit $ERRORS