fea3d277094479b95b2c570a70cd85aa59d44175
[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 # test that a key with that user ID does not already exist
30 check_key_userid "$serviceName" "$serviceName" && \
31     failure "A key with service name '$serviceName' already exists."
32
33 # check that the service name is well formatted
34 check_service_name "$serviceName"
35
36 # create host home
37 mkdir -p "${MHDATADIR}"
38 mkdir -p "${GNUPGHOME_HOST}"
39 chmod 700 "${GNUPGHOME_HOST}"
40
41 # import pem-encoded key to an OpenPGP private key
42 if [ "$keyFile" = '-' ] ; then
43     log verbose "importing key from stdin..."
44     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
45         | gpg_host --import
46 else
47     log verbose "importing key from file '$keyFile'..."
48     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
49         <"$keyFile" \
50         | gpg_host --import
51 fi
52
53 # export to gpg public key to file
54 update_gpg_pub_file
55
56 log info "host key imported:"
57
58 # show info about new key
59 show_key "$serviceName"
60
61 }