fixing usage for gen-subkey; allowing --help for monkeysphere (to match monkeysphere...
[monkeysphere.git] / src / monkeysphere
1 #!/bin/bash
2
3 # monkeysphere: MonkeySphere client tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 #
8 # They are Copyright 2008, and are all released under the GPL, version 3
9 # or later.
10
11 ########################################################################
12 PGRM=$(basename $0)
13
14 SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"}
15 export SHARE
16 . "${SHARE}/common" || exit 1
17
18 # date in UTF format if needed
19 DATE=$(date -u '+%FT%T')
20
21 # unset some environment variables that could screw things up
22 unset GREP_OPTIONS
23
24 # default return code
25 RETURN=0
26
27 # set the file creation mask to be only owner rw
28 umask 077
29
30 ########################################################################
31 # FUNCTIONS
32 ########################################################################
33
34 usage() {
35     cat <<EOF
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere client tool.
38
39 subcommands:
40  update-known_hosts (k) [HOST]...    update known_hosts file
41  update-authorized_keys (a)          update authorized_keys file
42  gen-subkey (g) [KEYID]              generate an authentication subkey
43    --length (-l) BITS                  key length in bits (2048)
44    --expire (-e) EXPIRE                date to expire
45  help (h,?)                          this help
46
47 EOF
48 }
49
50 # generate a subkey with the 'a' usage flags set
51 gen_subkey(){
52     local keyLength
53     local keyExpire
54     local keyID
55     local gpgOut
56     local userID
57
58     # set default key parameter values
59     keyLength=
60     keyExpire=
61
62     # get options
63     TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
64
65     if [ $? != 0 ] ; then
66         exit 1
67     fi
68
69     # Note the quotes around `$TEMP': they are essential!
70     eval set -- "$TEMP"
71
72     while true ; do
73         case "$1" in
74             -l|--length)
75                 keyLength="$2"
76                 shift 2
77                 ;;
78             -e|--expire)
79                 keyExpire="$2"
80                 shift 2
81                 ;;
82             --)
83                 shift
84                 ;;
85             *)
86                 break
87                 ;;
88         esac
89     done
90
91     if [ -z "$1" ] ; then
92         # find all secret keys
93         keyID=$(gpg --with-colons --list-secret-keys | grep ^sec | cut -f5 -d:)
94         # if multiple sec keys exist, fail
95         if (( $(echo "$keyID" | wc -l) > 1 )) ; then
96             echo "Multiple secret keys found:"
97             echo "$keyID"
98             failure "Please specify which primary key to use."
99         fi
100     else
101         keyID="$1"
102     fi
103     if [ -z "$keyID" ] ; then
104         failure "You have no secret key available.  You should create an OpenPGP
105 key before joining the monkeysphere. You can do this with:
106    gpg --gen-key"
107     fi
108
109     # get key output, and fail if not found
110     gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
111         "$keyID") || failure
112
113     # fail if multiple sec lines are returned, which means the id
114     # given is not unique
115     if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
116         failure "Key ID '$keyID' is not unique."
117     fi
118
119     # prompt if an authentication subkey already exists
120     if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
121         echo "An authentication subkey already exists for key '$keyID'."
122         read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
123         if [ "${OK/y/Y}" != 'Y' ] ; then
124             failure "aborting."
125         fi
126     fi
127
128     # set subkey defaults
129     # prompt about key expiration if not specified
130     if [ -z "$keyExpire" ] ; then
131         cat <<EOF
132 Please specify how long the key should be valid.
133          0 = key does not expire
134       <n>  = key expires in n days
135       <n>w = key expires in n weeks
136       <n>m = key expires in n months
137       <n>y = key expires in n years
138 EOF
139         while [ -z "$keyExpire" ] ; do
140             read -p "Key is valid for? (0) " keyExpire
141             if ! test_gpg_expire ${keyExpire:=0} ; then
142                 echo "invalid value"
143                 unset keyExpire
144             fi
145         done
146     elif ! test_gpg_expire "$keyExpire" ; then
147         failure "invalid key expiration value '$keyExpire'."
148     fi
149
150     # generate the list of commands that will be passed to edit-key
151     editCommands=$(cat <<EOF
152 addkey
153 7
154 S
155 E
156 A
157 Q
158 $keyLength
159 $keyExpire
160 save
161 EOF
162 )
163
164     log "generating subkey..."
165     fifoDir=$(mktemp -d)
166     (umask 077 && mkfifo "$fifoDir/pass")
167     echo "$editCommands" | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
168     
169     if [ "$DISPLAY" ] && which ssh-askpass >/dev/null; then
170         ssh-askpass "Please enter your passphrase for $keyID: " > "$fifoDir/pass"
171     else
172         read -s -p "Please enter your passphrase for $keyID: " PASS
173         echo "$PASS" > "$fifoDir/pass"
174     fi
175     rm -rf "$fifoDir"
176     wait
177     log "done."
178 }
179
180 ########################################################################
181 # MAIN
182 ########################################################################
183
184 # unset variables that should be defined only in config file
185 unset KEYSERVER
186 unset CHECK_KEYSERVER
187 unset KNOWN_HOSTS
188 unset HASH_KNOWN_HOSTS
189 unset AUTHORIZED_KEYS
190
191 # load global config
192 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
193
194 # set monkeysphere home directory
195 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
196 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
197
198 # load local config
199 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
200
201 # set empty config variables with ones from the environment, or from
202 # config file, or with defaults
203 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
204 KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
205 # if keyserver not specified in env or monkeysphere.conf,
206 # look in gpg.conf
207 if [ -z "$KEYSERVER" ] ; then
208     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
209         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
210     fi
211 fi
212 # if it's still not specified, use the default
213 KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
214 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
215 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
216 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
217 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
218
219 # other variables not in config file
220 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
221 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
222 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
223
224 # export GNUPGHOME and make sure gpg home exists with proper
225 # permissions
226 export GNUPGHOME
227 mkdir -p -m 0700 "$GNUPGHOME"
228
229 # get subcommand
230 COMMAND="$1"
231 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
232 shift
233
234 case $COMMAND in
235     'update-known_hosts'|'update-known-hosts'|'k')
236         MODE='known_hosts'
237
238         # check permissions on the known_hosts file path
239         if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
240             failure "Improper permissions on known_hosts file path."
241         fi
242
243         # if hosts are specified on the command line, process just
244         # those hosts
245         if [ "$1" ] ; then
246             update_known_hosts "$@"
247             RETURN="$?"
248
249         # otherwise, if no hosts are specified, process every host
250         # in the user's known_hosts file
251         else
252             # exit if the known_hosts file does not exist
253             if [ ! -e "$KNOWN_HOSTS" ] ; then
254                 log "known_hosts file '$KNOWN_HOSTS' does not exist."
255                 exit
256             fi
257
258             process_known_hosts
259             RETURN="$?"
260         fi
261         ;;
262
263     'update-authorized_keys'|'update-authorized-keys'|'a')
264         MODE='authorized_keys'
265
266         # check permissions on the authorized_user_ids file path
267         if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
268             failure "Improper permissions on authorized_user_ids file path."
269         fi
270
271         # check permissions on the authorized_keys file path
272         if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
273             failure "Improper permissions on authorized_keys file path."
274         fi
275
276         # exit if the authorized_user_ids file is empty
277         if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
278             log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
279             exit
280         fi
281
282         # process authorized_user_ids file
283         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
284         RETURN="$?"
285         ;;
286
287     'gen-subkey'|'g')
288         gen_subkey "$@"
289         ;;
290
291     '--help'|'help'|'-h'|'h'|'?')
292         usage
293         ;;
294
295     *)
296         failure "Unknown command: '$COMMAND'
297 Type '$PGRM help' for usage."
298         ;;
299 esac
300
301 exit "$RETURN"