add a gpg_user function in monkeysphere to add some gpg quieting
[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=$(cat <<EOF
48 addkey
49 7
50 S
51 E
52 A
53 Q
54 $keyLength
55 0
56 save
57 EOF
58 )
59
60     # setup the temp fifo dir for retrieving the key password
61     log debug "creating password fifo..."
62     fifoDir=$(msmktempdir)
63     trap "rm -rf $fifoDir" EXIT
64     (umask 077 && mkfifo "$fifoDir/pass")
65
66     log verbose "generating subkey..."
67     echo "$editCommands" | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
68
69     # FIXME: this needs to fail more gracefully if the passphrase is incorrect
70     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
71
72     trap - EXIT
73     rm -rf "$fifoDir"
74     wait
75     log verbose "done."
76 }