05004f66ee07a1e065b05d1d2765283552d87459
[monkeysphere.git] / src / share / m / gen_subkey
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere gen-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 # generate a subkey with the 'a' usage flags set
15
16 gen_subkey(){
17     local keyLength
18     local gpgSecOut
19     local keyID
20     local editCommands
21     local fifoDir
22
23     # get options
24     while true ; do
25         case "$1" in
26             -l|--length)
27                 keyLength="$2"
28                 shift 2
29                 ;;
30             *)
31                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
32                     failure "Unknown option '$1'.
33 Type '$PGRM help' for usage."
34                 fi
35                 break
36                 ;;
37         esac
38     done
39
40     # check that the keyID is unique
41     keyID=$(check_gpg_sec_key_id "$@")
42
43     # check that an authentication subkey does not already exist
44     check_gpg_authentication_subkey "$keyID"
45
46     # generate the list of commands that will be passed to edit-key
47     editCommands="addkey
48 7
49 S
50 E
51 A
52 Q
53 $keyLength
54 0
55 save"
56
57     # setup the temp fifo dir for retrieving the key password
58     log debug "creating password fifo..."
59     fifoDir=$(msmktempdir)
60     (umask 077 && mkfifo "$fifoDir/pass")
61
62     # FIXME: are we adequately cleaning up any trailing gpg process here?
63     trap "rm -rf $fifoDir; kill %% || true" EXIT
64     echo "$editCommands" | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
65
66     log debug "Prompting for passphrase"
67     # FIXME: this needs to fail more gracefully if the passphrase is incorrect
68     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
69     log info "Generating subkey.  This may take a long time..."
70
71     trap - EXIT
72     rm -rf "$fifoDir"
73     wait
74     log verbose "done."
75 }