The monkeysphere {import,gen}_subkey functions were not up-to-date.
[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     sshKeyFile="$1"
23     shift
24
25     # check that key file specified
26     if [ -z "$sshKeyFile" ] ; then
27         failure "Must specify ssh key file to import, or specify '-' for stdin."
28     fi
29
30     # check that the keyID is unique
31     keyID=$(check_gpg_sec_key_id "$@")
32
33     # check that an authentication subkey does not already exist
34     check_gpg_authentication_subkey "$keyID"
35
36     # setup the temp fifo dir for retrieving the key password
37     log debug "creating password fifo..."
38     fifoDir=$(msmktempdir)
39     trap "rm -rf $fifoDir" EXIT
40     (umask 077 && mkfifo "$fifoDir/pass")
41
42     # import ssh key to as authentication subkey
43     if [ "$sshKeyFile" = '-' ] ; then
44         log verbose "importing ssh key from stdin..."
45         ssh2openpgp \
46             | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
47     else
48         log verbose "importing ssh key from file '$sshKeyFile'..."
49         ssh2openpgp <"$sshKeyFile" \
50             | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
51     fi
52
53     # get the password if needed
54     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
55
56     trap - EXIT
57     rm -rf "$fifoDir"
58     wait
59     log verbose "done."
60 }