Merge commit 'jrollins/master'
[monkeysphere.git] / src / share / m / import_subkey
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere import-subkey 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 an existing ssh key as a gpg subkey
15
16 import_subkey() {
17     local sshKeyFile
18     local keyID
19     local gpgSecOut
20     local fifoDir
21
22     # FIXME: implement!
23     failure "implement me!"
24
25     sshKeyFile="$1"
26     shift
27
28     # check that key file specified
29     if [ -z "$sshKeyFile" ] ; then
30         failure "Must specify ssh key file to import, or specify '-' for stdin."
31     fi
32
33     # check that the keyID is unique
34     keyID=$(check_gpg_sec_key_id "$@")
35
36     # check that an authentication subkey does not already exist
37     check_gpg_authentication_subkey "$keyID"
38
39     # setup the temp fifo dir for retrieving the key password
40     log debug "creating password fifo..."
41     fifoDir=$(msmktempdir)
42     trap "rm -rf $fifoDir" EXIT
43     (umask 077 && mkfifo "$fifoDir/pass")
44
45     # import ssh key to as authentication subkey
46     if [ "$sshKeyFile" = '-' ] ; then
47         log verbose "importing ssh key from stdin..."
48         PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
49             | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
50     else
51         log verbose "importing ssh key from file '$sshKeyFile'..."
52         PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" <"$sshKeyFile" \
53             | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
54     fi
55
56     # get the password if needed
57     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
58
59     trap - EXIT
60     rm -rf "$fifoDir"
61     wait
62     log verbose "done."
63 }