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