modify import_key to take the key file to import as an argument. can be '-' to impor...
[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 # use the default hostname if not specified
25 if [ -z "$hostName" ] ; then
26     hostName=$(hostname -f) || failure "Could not determine hostname."
27     # test that the domain is not obviously illegitimate
28     domain=${foo##*.}
29     case $domain in
30         'local'|'localdomain')
31             failure "Host domain '$domain' is not legitimate.  Aborting key import."
32             ;;
33     esac
34     # test that there are at least two parts
35     if (( $(echo "$hostName" | tr . ' ' | wc -w) < 2 )) ; then
36         failure "Host name '$hostName' is not legitimate.  Aborting key import."
37     fi
38 fi
39
40 userID="ssh://${hostName}"
41
42 # create host home
43 mkdir -p "${MHDATADIR}"
44 mkdir -p "${GNUPGHOME_HOST}"
45 chmod 700 "${GNUPGHOME_HOST}"
46
47 # import ssh key to a private key
48 if [ -z "$sshKeyFile" ] ; then
49     failure "Must specify ssh key file to import, or specify '-' for stdin."
50 elif [ "$sshKeyFile" = '-' ] ; then
51     log verbose "importing ssh key from stdin..."
52     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
53         | gpg_host --import
54 else
55     log verbose "importing ssh key from file '$sshKeyFile'..."
56     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
57         <"$sshKeyFile" \
58         | gpg_host --import
59 fi
60
61 # load the new host fpr into the fpr variable.  this is so we can
62 # create the gpg pub key file.  we have to do this from the secret key
63 # ring since we obviously don't have the gpg pub key file yet, since
64 # that's what we're trying to produce (see below).
65 load_fingerprint_secret
66
67 # export to gpg public key to file
68 update_gpg_pub_file
69
70 log info "host key imported:"
71
72 # show info about new key
73 show_key
74
75 }