Fix patch for gen_key to test gpg version.
[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     local keyType
23
24     # get options
25     while true ; do
26         case "$1" in
27             -l|--length)
28                 keyLength="$2"
29                 shift 2
30                 ;;
31             *)
32                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
33                     failure "Unknown option '$1'.
34 Type '$PGRM help' for usage."
35                 fi
36                 break
37                 ;;
38         esac
39     done
40
41     # check that the keyID is unique
42     keyID=$(check_gpg_sec_key_id "$@")
43
44     # check that an authentication subkey does not already exist
45     check_gpg_authentication_subkey "$keyID"
46
47     # determine which keyType to use from gpg version
48     keyType=7
49     case $(gpg --version | head -1 | awk '{ print $3 }' | cut -d. -f1) in
50         1)
51             if is_gpg_version_greater_equal 1.4.10 ; then
52                 keyType=8
53             fi
54             ;;
55         2)
56             if is_gpg_version_greater_equal 2.0.13 ; then
57                 keyType=8
58             fi
59             ;;
60         *)
61             keyType=8
62             ;;
63     esac
64
65     # generate the list of commands that will be passed to edit-key
66     editCommands="addkey
67 $keyType
68 S
69 E
70 A
71 Q
72 $keyLength
73 0
74 save"
75
76     # setup the temp fifo dir for retrieving the key password
77     log debug "creating password fifo..."
78     fifoDir=$(msmktempdir)
79     (umask 077 && mkfifo "$fifoDir/pass")
80
81     # FIXME: are we adequately cleaning up any trailing gpg process here?
82     trap "rm -rf $fifoDir; kill %% || true" EXIT
83     echo "$editCommands" | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
84
85     log debug "Prompting for passphrase"
86     # FIXME: this needs to fail more gracefully if the passphrase is incorrect
87     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
88     log info "Generating subkey.  This may take a long time..."
89
90     trap - EXIT
91     rm -rf "$fifoDir"
92     wait
93     log verbose "done."
94 }