ac67711742859ad20e898fc2c45248d9fafd35f9
[monkeysphere.git] / src / subcommands / mh / import-key
1 #!/usr/bin/env bash
2
3 # Monkeysphere host import-key subcommand
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #
10 # They are Copyright 2008, and are all released under the GPL, version 3
11 # or later.
12
13 local hostName=$(hostname -f)
14 local keyFile="/etc/ssh/ssh_host_rsa_key"
15 local keyExpire
16 local userID
17
18 # check for presense of secret key
19 # FIXME: is this the proper test to be doing here?
20 fingerprint_server_key >/dev/null \
21         && failure "An OpenPGP host key already exists."
22
23 # get options
24 while true ; do
25         case "$1" in
26             -f|--keyfile)
27                 keyFile="$2"
28                 shift 2
29                 ;;
30             -e|--expire)
31                 keyExpire="$2"
32                 shift 2
33                 ;;
34             *)
35                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
36                     failure "Unknown option '$1'.
37 Type '$PGRM help' for usage."
38                 fi
39                 hostName="$1"
40                 shift
41                 ;;
42                 break
43                 ;;
44         esac
45 done
46
47 if [ ! -f "$keyFile" ] ; then
48         failure "SSH secret key file '$keyFile' not found."
49 fi
50
51 userID="ssh://${hostName}"
52
53 # prompt about key expiration if not specified
54 keyExpire=$(get_gpg_expiration "$keyExpire")
55
56 echo "The following key parameters will be used for the host private key:"
57 echo "Import: $keyFile"
58 echo "Name-Real: $userID"
59 echo "Expire-Date: $keyExpire"
60
61 read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
62 if [ ${OK/y/Y} != 'Y' ] ; then
63         failure "aborting."
64 fi
65
66 log verbose "importing ssh key..."
67 # translate ssh key to a private key
68 (umask 077 && \
69         pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
70
71 # find the key fingerprint of the newly converted key
72 fingerprint=$(fingerprint_server_key)
73
74 # export host ownertrust to authentication keyring
75 log verbose "setting ultimate owner trust for host key..."
76 echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
77 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
78
79 # export public key to file
80 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
81 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
82
83 # show info about new key
84 show_key