a0fa3ce77228b5f6b7efc876ccf3a22098a2d38c
[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     trap "rm -rf $fifoDir" EXIT
61     (umask 077 && mkfifo "$fifoDir/pass")
62
63     log verbose "generating subkey..."
64     echo "$editCommands" | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
65
66     # FIXME: this needs to fail more gracefully if the passphrase is incorrect
67     passphrase_prompt  "Please enter your passphrase for $keyID: " "$fifoDir/pass"
68
69     trap - EXIT
70     rm -rf "$fifoDir"
71     wait
72     log verbose "done."
73 }