turn subcommands into subfunctions, that will need to be sourced and executed.
[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             -h|--hostname)
27                 hostName="$2"
28                 shift 2
29                 ;;
30             -f|--keyfile)
31                 keyFile="$2"
32                 shift 2
33                 ;;
34             -e|--expire)
35                 keyExpire="$2"
36                 shift 2
37                 ;;
38             *)
39                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
40                     failure "Unknown option '$1'.
41 Type '$PGRM help' for usage."
42                 fi
43                 break
44                 ;;
45         esac
46 done
47
48 if [ ! -f "$keyFile" ] ; then
49         failure "SSH secret key file '$keyFile' not found."
50 fi
51
52 userID="ssh://${hostName}"
53
54 # prompt about key expiration if not specified
55 keyExpire=$(get_gpg_expiration "$keyExpire")
56
57 echo "The following key parameters will be used for the host private key:"
58 echo "Import: $keyFile"
59 echo "Name-Real: $userID"
60 echo "Expire-Date: $keyExpire"
61
62 read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
63 if [ ${OK/y/Y} != 'Y' ] ; then
64         failure "aborting."
65 fi
66
67 log verbose "importing ssh key..."
68 # translate ssh key to a private key
69 (umask 077 && \
70         pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
71
72 # find the key fingerprint of the newly converted key
73 fingerprint=$(fingerprint_server_key)
74
75 # export host ownertrust to authentication keyring
76 log verbose "setting ultimate owner trust for host key..."
77 echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
78 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
79
80 # export public key to file
81 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
82 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
83
84 # show info about new key
85 show_key