Merge commit 'dkg/master'
[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 import_key() {
14
15 local hostName=$(hostname -f)
16 local keyFile="/etc/ssh/ssh_host_rsa_key"
17 local keyExpire
18 local userID
19
20 # check for presense of secret key
21 # FIXME: is this the proper test to be doing here?
22 fingerprint_server_key >/dev/null \
23         && failure "An OpenPGP host key already exists."
24
25 # get options
26 while true ; do
27         case "$1" in
28             -f|--keyfile)
29                 keyFile="$2"
30                 shift 2
31                 ;;
32             -e|--expire)
33                 keyExpire="$2"
34                 shift 2
35                 ;;
36             *)
37                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
38                     failure "Unknown option '$1'.
39 Type '$PGRM help' for usage."
40                 fi
41                 hostName="$1"
42                 shift
43                 ;;
44                 break
45                 ;;
46         esac
47 done
48
49 if [ ! -f "$keyFile" ] ; then
50         failure "SSH secret key file '$keyFile' not found."
51 fi
52
53 userID="ssh://${hostName}"
54
55 # prompt about key expiration if not specified
56 keyExpire=$(get_gpg_expiration "$keyExpire")
57
58 echo "The following key parameters will be used for the host private key:"
59 echo "Import: $keyFile"
60 echo "Name-Real: $userID"
61 echo "Expire-Date: $keyExpire"
62
63 read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
64 if [ ${OK/y/Y} != 'Y' ] ; then
65         failure "aborting."
66 fi
67
68 log verbose "importing ssh key..."
69 # translate ssh key to a private key
70 (umask 077 && \
71         pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
72
73 # find the key fingerprint of the newly converted key
74 fingerprint=$(fingerprint_server_key)
75
76 # export host ownertrust to authentication keyring
77 log verbose "setting ultimate owner trust for host key..."
78 echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
79 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
80
81 # export public key to file
82 gpg_authentication "--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 }