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