import-key now requires a hostname be specified, and no longer does
[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 sshKeyFile
17 local hostName
18 local domain
19 local userID
20
21 sshKeyFile="$1"
22 hostName="$2"
23
24 # check that key file specified
25 if [ -z "$sshKeyFile" ] ; then
26     failure "Must specify ssh key file to import, or specify '-' for stdin."
27 fi
28
29 # fail if hostname not specified
30 if [ -z "$hostName" ] ; then
31     failure "You must specify a fully-qualified domain name for use in the host certificate user ID."
32 fi
33
34 userID="ssh://${hostName}"
35
36 # create host home
37 mkdir -p "${MHDATADIR}"
38 mkdir -p "${GNUPGHOME_HOST}"
39 chmod 700 "${GNUPGHOME_HOST}"
40
41 # import ssh key to a private key
42 if [ "$sshKeyFile" = '-' ] ; then
43     log verbose "importing ssh key from stdin..."
44     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
45         | gpg_host --import
46 else
47     log verbose "importing ssh key from file '$sshKeyFile'..."
48     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
49         <"$sshKeyFile" \
50         | gpg_host --import
51 fi
52
53 # load the new host fpr into the fpr variable.  this is so we can
54 # create the gpg pub key file.  we have to do this from the secret key
55 # ring since we obviously don't have the gpg pub key file yet, since
56 # that's what we're trying to produce (see below).
57 load_fingerprint_secret
58
59 # export to gpg public key to file
60 update_gpg_pub_file
61
62 log info "host key imported:"
63
64 # show info about new key
65 show_key
66
67 }