chiark / gitweb /
lib/func.sh: Ask for `gpg1' because the stoats got to plain `gpg'.
[ssh-ca] / bin / sign
1 #! /bin/sh
2
3 set -e
4 . lib/func.sh
5 orig_domain=$domain date=$(date +%Y-%m-%d)
6
7 ## The key types are adorned with bit lengths.  Work out the raw key type
8 ## names.
9 rawkeytypes=""
10 for kt in $keytypes; do
11   rawkeytypes="$rawkeytypes ${kt%:*}"
12 done
13
14 ## Start a new output directory.
15 rm -rf publish.new
16 mkdir publish.new
17 exec 3<etc/hosts 4>publish.new/hosts.list 5>publish.new/known_hosts
18 echo ":certificate-authority" >&4
19 for kt in $rawkeytypes; do
20   cp ca/ca-$kt.pub publish.new/
21   read pub <ca/ca-$kt.pub
22   echo "@cert-authority $scope $pub" |
23         tee publish.new/ca-$kt.entry >&4
24   ssh-keygen -lv -fca/ca-$kt.pub | sed 's,^,| ,' >&4
25 done
26
27 ## Sign the various host keys.
28 last=%%%
29 idomain=$domain
30 echo >&5 "### BEGIN $idomain KEYS (generated $date)"
31 while read line <&3; do
32
33   ## Ignore comments and empty lines.
34   case "$line" in
35     "#"* | "") continue ;;
36     ##*[!       ]*) ;;
37     ##*) continue ;;
38   esac
39
40   ## Read the host line.
41   set -- $line
42   case "$1" in
43     @domain) domain=$2; continue ;;
44     @*) echo >&2 "$0: unknown directive \`$1'"; exit 1 ;;
45   esac
46   host=$1
47   names=""
48   nicks=""
49
50   ## If this is a different host, then start a new section of the list.
51   case "$last" in
52     "$host") ;;
53     *) { echo; echo ":host $host"; } >&4 ;;
54   esac
55   last=$host
56
57   ## Build a list of names for the host.
58   for n in "$@"; do
59     case "$n" in
60       .*) for h in $nicks; do names=${names:+$names,}$h$n.$domain; done ;;
61       *.* | *:*) names=${names:+$names,}$n ;;
62       *) nicks=${nicks:+$nicks }$n names=${names:+$names,}$n.$domain ;;
63     esac
64   done
65
66   ## Sign certificates.
67   for kt in $rawkeytypes; do
68     if [ ! -f host/$host-$kt.pub ]; then continue; fi
69     cp host/$host-$kt.pub publish.new/
70     ssh-keygen -q -sca/ca-$kt \
71       -h -I"$cacomment:$host.$domain" -n$names \
72       -V$validity \
73       publish.new/$host-$kt.pub
74     mv publish.new/$host-$kt-cert.pub \
75       publish.new/$host-$kt.cert
76     for fd in 4 5; do
77       { printf "%s " $names; cat host/$host-$kt.pub; } >&$fd
78     done
79     ssh-keygen -lv -fhost/$host-$kt.pub | sed 's,^,| ,' >&4
80   done
81 done
82 echo >&5 "### END $idomain KEYS"
83 exec 3>&- 4>&- 5>&-
84
85 ## Sign the list.
86 run_gpg --armor -o publish.new/hosts.asc \
87   --clearsign publish.new/hosts.list
88 rm publish.new/hosts.list
89
90 ## Include a copy of the public key.
91 run_gpg --export --armor -o publish.new/ca-gnupg.asc
92
93 ## Done.
94 if [ -d publish ]; then
95   rm -rf publish.old
96   mv publish publish.old
97 fi
98 mv publish.new publish
99 rm -rf publish.old