Merge commit 'dkg/master'
[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 hostName
17 local domain
18 local userID
19
20 hostName="$1"
21
22 # use the default hostname if not specified
23 if [ -z "$hostName" ] ; then
24     hostName=$(hostname -f)
25     # test that the domain is not obviously illegitimate
26     domain=${foo##*.}
27     case $domain in
28         'local'|'localdomain')
29             failure "Host domain '$domain' is not legitimate.  Aborting key import."
30             ;;
31     esac
32     # test that there are at least two parts
33     if (( $(echo "$hostName" | tr . ' ' | wc -w) < 2 )) ; then
34         failure "Host name '$hostName' is not legitimate.  Aborting key import."
35     fi
36 fi
37
38 userID="ssh://${hostName}"
39
40 # create host home
41 mkdir -p "${MHDATADIR}"
42 mkdir -p "${MHTMPDIR}"
43 mkdir -p "${GNUPGHOME_HOST}"
44 chmod 700 "${GNUPGHOME_HOST}"
45
46 log verbose "importing ssh key..."
47 # translate ssh key to a private key
48 PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
49     | gpg_host --import
50
51 # load the new host fpr into the fpr variable.  this is so we can
52 # create the gpg pub key file.  we have to do this from the secret key
53 # ring since we obviously don't have the gpg pub key file yet, since
54 # that's what we're trying to produce (see below).
55 load_fingerprint_secret
56
57 # set ultimate owner trust on the newly imported key
58 printf "%s:6:\n" "$HOST_FINGERPRINT" | gpg_host --import-ownertrust
59
60 # update trustdb
61 gpg_host --check-trustdb
62
63 # export to gpg public key to file
64 create_gpg_pub_file
65
66 # show info about new key
67 show_key
68
69 }