bin/sign: Emit a `known_hosts' file in the correct form.
[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 echo >&5 "### BEGIN $domain KEYS (generated $date)"
30 while read line <&3; do
31
32 ## Ignore comments and empty lines.
33 case "$line" in
34 "#"* | "") continue ;;
35 ##*[! ]*) ;;
36 ##*) continue ;;
37 esac
38
39 ## Read the host line.
40 set -- $line
41 case "$1" in
42 @domain) domain=$2 ;;
43 @*) echo >&2 "$0: unknown directive \`$1'"; exit 1 ;;
44 esac
45 host=$1
46 names=""
47 nicks=""
48
49 ## If this is a different host, then start a new section of the list.
50 case "$last" in
51 "$host") ;;
52 *) { echo; echo ":host $host"; } >&4 ;;
53 esac
54 last=$host
55
56 ## Build a list of names for the host.
57 for n in "$@"; do
58 case "$n" in
59 .*) for h in $nicks; do names=${names:+$names,}$h$n.$domain; done ;;
60 *.* | *:*) names=${names:+$names,}$n ;;
61 *) nicks=${nicks:+$nicks }$n names=${names:+$names,}$n.$domain ;;
62 esac
63 done
64
65 ## Sign certificates.
66 for kt in $rawkeytypes; do
67 if [ ! -f host/$host-$kt.pub ]; then continue; fi
68 cp host/$host-$kt.pub publish.new/
69 ssh-keygen -q -tv00 -sca/ca-$kt \
70 -h -I"$cacomment:$host.$domain" -n$names \
71 -V$validity \
72 publish.new/$host-$kt.pub
73 mv publish.new/$host-$kt-cert.pub \
74 publish.new/$host-$kt.cert
75 for fd in 4 5; do
76 { printf "%s " $names; cat host/$host-$kt.pub; } >&$fd
77 done
78 ssh-keygen -lv -fhost/$host-$kt.pub | sed 's,^,| ,' >&4
79 done
80 done
81 echo >&5 "### END $domain KEYS"
82 exec 3>&- 4>&- 5>&-
83
84 ## Sign the list.
85 run_gpg --armor -o publish.new/hosts.asc \
86 --clearsign publish.new/hosts.list
87 rm publish.new/hosts.list
88
89 ## Include a copy of the public key.
90 run_gpg --export --armor -o publish.new/ca-gnupg.asc
91
92 ## Done.
93 if [ -d publish ]; then
94 rm -rf publish.old
95 mv publish publish.old
96 fi
97 mv publish.new publish
98 rm -rf publish.old