Major rework of monkeysphere-host to handle multiple host keys.
[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-2010 and are all released under the GPL,
12 # version 3 or later.
13
14 import_key() {
15
16 local keyFile="$1"
17 local serviceName="$2"
18
19 # check that key file specified
20 if [ -z "$keyFile" ] ; then
21     failure "Must specify PEM-encoded key file to import, or specify '-' for stdin."
22 fi
23
24 # fail if hostname not specified
25 if [ -z "$serviceName" ] ; then
26     failure "You must specify a service name for use in the OpenPGP certificate user ID."
27 fi
28
29 # check that the service name is well formatted
30 check_service_name "$serviceName"
31
32 # create host home
33 mkdir -p "${MHDATADIR}"
34 mkdir -p "${GNUPGHOME_HOST}"
35 chmod 700 "${GNUPGHOME_HOST}"
36
37 # import pem-encoded key to an OpenPGP private key
38 if [ "$keyFile" = '-' ] ; then
39     log verbose "importing key from stdin..."
40     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
41         | gpg_host --import
42 else
43     log verbose "importing key from file '$keyFile'..."
44     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
45         <"$keyFile" \
46         | gpg_host --import
47 fi
48
49 # export to gpg public key to file
50 update_gpg_pub_file
51
52 log info "host key imported:"
53
54 # show info about new key
55 show_key "$serviceName"
56
57 }