summaryrefslogtreecommitdiff
path: root/scripts/mkreq
blob: 44938678780ac178d36aeb2bfb1089537110c1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh

host="$1"; shift
ca_host="ca.sunet.se"
ca_name="infra"
type=""

usage ()
{
    echo "\
Usage: mkreq [-v] [-s*] [-c] [-C <ca host>] [-N <ca name>] [--] <fqdn>


  -h, --help                show this help text and exit
  -s                        request server cert (default if <fqdn> exists in cosmos repo)
  -c                        request client cert
  -C                        ca host (ca.sunet.se)
  -N                        ca name (infra)
  
  <fqdn>                    fully qualified name of host

" 1>&2
}

{
   while test $# -gt 0; do
      case "$1" in
          -s)
             type="server"
             ;;
          -c)
             type="client"
             ;;
          -C)
             ca_host="$2"
             shift
             ;;
          -N)
             ca_name="$2"
             shift
             ;;
          -h)
             usage
             exit 0
             ;;
          --)
             break
             ;;
      esac
      shift
   done
}

if [ -d $host -a -z $type ]; then
   type="server"
fi


cfg=`mktemp`
key="/tmp/$host.key"
csr="/tmp/$host.csr"

trap 'rm -f $cfg' EXIT

cat>$cfg<<EOC
[ req ]
default_bits           = 4096
distinguished_name     = req_distinguished_name
req_extensions         = req_extensions
prompt		       = no

[ req_distinguished_name ]
C			= SE
O			= SUNET
CN			= $host

[ req_extensions ]
subjectAltName          = DNS:$host
EOC

reqs="$ca_host/overlay/var/lib/ca/$ca_name/requests/$type"
if [ ! -d $reqs ]; then
   echo "*** ERROR - missing request directory $reqs"
   exit 1
fi

openssl req -config $cfg -new -newkey rsa:4096 -sha256 -keyout $key -nodes -out $csr
mv $csr "$reqs/$host.csr"
git add "$reqs/$host.csr" && git commit -m "certification request for $host from $ca_host:$ca_name"

if [ -d $host ]; then
   ssh root@$host mkdir -p /etc/ssl/private && scp "$key" "root@$host:/etc/ssl/private/${host}_${ca_name}.key" && rm -f "$key" && echo "** private key given to $host" || echo "** private key left in $key - should be in root@$host:/etc/ssl/private/${host}_${ca_name}.key"
fi

echo "** successfully generated key and certification request for $host from $ca_host:$ca_name"