myslice.ini possible themes = onelab, fed4fire, fibre, smartfire
[myslice.git] / apache / unfold-init-ssl.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4
5 # minimal script for initializing SSL material for myslice
6 # you probably want to take care of this yourself instead,
7 # but until somebody gets around to that apache will at least start up
8
9 trusted_roots=/etc/unfold/trusted_roots
10 key=/etc/unfold/myslice.key
11 cert=/etc/unfold/myslice.cert
12
13 # provide a hostname as the first arg to this command 
14 # (otherwise we use hostname)
15 if [[ -n "$@" ]] ; then hostname=$1; shift; else hostname=$(hostname); fi
16
17 function init_trusted_roots () {
18     if [ ! -d $trusted_roots ] ; then
19         echo "Creating empty" $trusted_roots
20         mkdir -p $trusted_roots
21         echo "You will wish to populate that with e.g. ple.gid or the like"
22         echo "Make sure to re-run this command $COMMAND if you add gids"
23     fi
24     ### c_rehash will consider only files ending in .pem or .crt
25     # so, we create symlinks from *.gid to *.pem
26     pushd $trusted_roots >& /dev/null
27     for gid in *.gid; do
28         base=$(basename $gid .gid)
29         pem=$base.pem
30         [ -f $pem ] && ln -s $gid $pem 
31     done
32     ### invoke c_rehash 
33     # on debian c_rehash comes with openssl
34     # on fedora this is part of openssl-perl
35     echo -n "Invoking c_rehash in $(pwd) .. "; c_rehash .
36     popd  >& /dev/null
37 }
38
39 function init_server_cert () {
40     # both present : we have nothing to do
41     [ -f $key -a -f $cert ] && return
42     # exactly one present : we have a problem
43     [ -f $key -o -f $cert ] && { echo "server key or cert missing ?!?" ; return ; }
44     # create both
45     echo "Creating server key and cert for hostname ${hostname}"
46     openssl req -new -x509 -days 365 -set_serial $RANDOM -batch \
47         -subj "/CN=${hostname}" -nodes -keyout $key -out $cert
48 }
49
50
51 function main () {
52     init_trusted_roots
53     init_server_cert
54 }
55
56 main "$@"