added notes about what version needs what keyType
[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     # generate the list of commands that will be passed to edit-key
48     # 7 for < 1.4.10
49     # 8 for >= 1.4.10
50     # 7 for < 2.0.13
51     # 8 for >= 2.0.13
52     keyType=8
53     editCommands="addkey
54 $keyType
55 S
56 E
57 A
58 Q
59 $keyLength
60 0
61 save"
62
63     # setup the temp fifo dir for retrieving the key password
64     log debug "creating password fifo..."
65     fifoDir=$(msmktempdir)
66     (umask 077 && mkfifo "$fifoDir/pass")
67
68     # FIXME: are we adequately cleaning up any trailing gpg process here?
69     trap "rm -rf $fifoDir; kill %% || true" EXIT
70     echo "$editCommands" | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
71
72     log debug "Prompting for passphrase"
73     # FIXME: this needs to fail more gracefully if the passphrase is incorrect
74     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
75     log info "Generating subkey.  This may take a long time..."
76
77     trap - EXIT
78     rm -rf "$fifoDir"
79     wait
80     log verbose "done."
81 }