Merge in upstream/2.4+dfsg into experimental.
[shibboleth/sp.git] / configs / keygen.sh
1 #! /bin/sh
2
3 # Added for Debian.  The upstream version is installed in /etc/shibboleth and
4 # for Debian we wanted to move it to /usr/bin, so change directories so that
5 # it puts files in the correct location.
6 cd /etc/shibboleth
7 while getopts h:u:g:o:e:y:bf c
8      do
9          case $c in
10            u)         USER=$OPTARG;;
11            g)         GROUP=$OPTARG;;
12            o)         OUT=$OPTARG;;
13            b)         BATCH=1;;
14            f)         FORCE=1;;
15            h)         FQDN=$OPTARG;;
16            e)         ENTITYID=$OPTARG;;
17            y)         YEARS=$OPTARG;;
18            \?)        echo "keygen [-o output directory (default .)] [-u username to own keypair] [-g owning groupname] [-h hostname for cert] [-y years to issue cert] [-e entityID to embed in cert]"
19                       exit 1;;
20          esac
21      done
22 if [ -z "$OUT" ] ; then
23     OUT=.
24 fi
25
26 if [ -n "$FORCE" ] ; then
27     rm $OUT/sp-key.pem $OUT/sp-cert.pem
28 fi
29
30 if  [ -s $OUT/sp-key.pem -o -s $OUT/sp-cert.pem ] ; then
31     if [ -z "$BATCH" ] ; then  
32         echo The files $OUT/sp-key.pem and/or $OUT/sp-cert.pem already exist!
33         echo Use -f option to force recreation of keypair.
34         exit 2
35     fi
36     exit 0
37 fi
38
39 # --fqdn flag added for Debian to generate better names for certificates.
40 if [ -z "$FQDN" ] ; then
41     FQDN=`hostname --fqdn`
42 fi
43
44 if [ -z "$YEARS" ] ; then
45     YEARS=10
46 fi
47
48 DAYS=`expr $YEARS \* 365`
49
50 if [ -z "$ENTITYID" ] ; then
51     ALTNAME=DNS:$FQDN
52 else
53     ALTNAME=DNS:$FQDN,URI:$ENTITYID
54 fi
55
56 SSLCNF=$OUT/sp-cert.cnf
57 cat >$SSLCNF <<EOF
58 # OpenSSL configuration file for creating sp-cert.pem
59 [req]
60 prompt=no
61 default_bits=2048
62 encrypt_key=no
63 default_md=sha1
64 distinguished_name=dn
65 # PrintableStrings only
66 string_mask=MASK:0002
67 x509_extensions=ext
68 [dn]
69 CN=$FQDN
70 [ext]
71 subjectAltName=$ALTNAME
72 subjectKeyIdentifier=hash
73 EOF
74
75 touch $OUT/sp-key.pem
76 chmod 600 $OUT/sp-key.pem
77 if [ -z "$BATCH" ] ; then
78     openssl req -config $SSLCNF -new -x509 -days $DAYS -keyout $OUT/sp-key.pem -out $OUT/sp-cert.pem
79 else
80     openssl req -config $SSLCNF -new -x509 -days $DAYS -keyout $OUT/sp-key.pem -out $OUT/sp-cert.pem 2> /dev/null
81 fi
82 rm $SSLCNF
83
84 if  [ -s $OUT/sp-key.pem -a -n "$USER" ] ; then
85     chown $USER $OUT/sp-key.pem $OUT/sp-cert.pem
86 fi
87
88 if  [ -s $OUT/sp-key.pem -a -n "$GROUP" ] ; then
89     chgrp $GROUP $OUT/sp-key.pem $OUT/sp-cert.pem
90 fi