Fix argument checking for functions with options.
[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 'a' capable subkey
43     -l|--length BITS                  key length in bits (2048)
44     -e|--expire 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 # FIXME: this needs some tweaking to clean it up
52 gen_subkey(){
53     local keyLength
54     local keyExpire
55     local keyID
56     local gpgOut
57     local userID
58
59     # set default key parameter values
60     keyLength=
61     keyExpire=
62
63     # get options
64     TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
65
66     if [ $? != 0 ] ; then
67         exit 1
68     fi
69
70     # Note the quotes around `$TEMP': they are essential!
71     eval set -- "$TEMP"
72
73     while true ; do
74         case "$1" in
75             -l|--length)
76                 keyLength="$2"
77                 shift 2
78                 ;;
79             -e|--expire)
80                 keyExpire="$2"
81                 shift 2
82                 ;;
83             --)
84                 shift
85                 ;;
86             *)
87                 break
88                 ;;
89         esac
90     done
91
92     keyID="$1"
93     if [ -z "$keyID" ] ; then
94         failure "You must specify the key ID of your primary key."
95     fi
96
97     gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
98         "$keyID" 2> /dev/null)
99
100     # fail if there only "tru" lines are output from gpg, which
101     # indicates the key was not found.
102     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
103         failure "Key ID '$keyID' not found."
104     fi
105
106     # fail if multiple pub lines are returned, which means the id given
107     # is not unique
108     if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
109         failure "Key ID '$keyID' is not unique."
110     fi
111
112     # prompt if an authentication subkey already exists
113     if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
114         echo "An authentication subkey already exists for key '$keyID'."
115         read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
116         if [ "${OK/y/Y}" != 'Y' ] ; then
117             failure "aborting."
118         fi
119     fi
120
121     # set subkey defaults
122     # prompt about key expiration if not specified
123     if [ -z "$keyExpire" ] ; then
124         cat <<EOF
125 Please specify how long the key should be valid.
126          0 = key does not expire
127       <n>  = key expires in n days
128       <n>w = key expires in n weeks
129       <n>m = key expires in n months
130       <n>y = key expires in n years
131 EOF
132         while [ -z "$keyExpire" ] ; do
133             read -p "Key is valid for? (0) " keyExpire
134             if ! test_gpg_expire ${keyExpire:=0} ; then
135                 echo "invalid value"
136                 unset keyExpire
137             fi
138         done
139     elif ! test_gpg_expire "$keyExpire" ; then
140         failure "invalid key expiration value '$keyExpire'."
141     fi
142
143     # generate the list of commands that will be passed to edit-key
144     editCommands=$(cat <<EOF
145 addkey
146 7
147 S
148 E
149 A
150 Q
151 $keyLength
152 $keyExpire
153 save
154 EOF
155 )
156
157     log "generating subkey..."
158     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
159     log "done."
160 }
161
162 ########################################################################
163 # MAIN
164 ########################################################################
165
166 # unset variables that should be defined only in config file
167 unset KEYSERVER
168 unset CHECK_KEYSERVER
169 unset KNOWN_HOSTS
170 unset HASH_KNOWN_HOSTS
171 unset AUTHORIZED_KEYS
172
173 # load global config
174 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
175
176 # set monkeysphere home directory
177 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
178 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
179
180 # load local config
181 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
182
183 # set empty config variables with ones from the environment, or from
184 # config file, or with defaults
185 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
186 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
187 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
188 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
189 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
190 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
191
192 # other variables not in config file
193 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
194 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
195 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
196
197 # export GNUPGHOME and make sure gpg home exists with proper
198 # permissions
199 export GNUPGHOME
200 mkdir -p -m 0700 "$GNUPGHOME"
201
202 # get subcommand
203 COMMAND="$1"
204 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
205 shift
206
207 case $COMMAND in
208     'update-known_hosts'|'update-known-hosts'|'k')
209         MODE='known_hosts'
210
211         if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
212             failure "Improper permissions on known_hosts file."
213         fi
214
215         # if hosts are specified on the command line, process just
216         # those hosts
217         if [ "$1" ] ; then
218             update_known_hosts "$@"
219             RETURN="$?"
220
221         # otherwise, if no hosts are specified, process every host
222         # in the user's known_hosts file
223         else
224             if [ ! -s "$KNOWN_HOSTS" ] ; then
225                 failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
226             fi
227
228             process_known_hosts
229             RETURN="$?"
230         fi
231         ;;
232
233     'update-authorized_keys'|'update-authorized-keys'|'a')
234         MODE='authorized_keys'
235
236         # fail if the authorized_user_ids file is empty
237         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
238             failure "authorized_user_ids file '$AUTHORIZED_USER_IDS' is empty or does not exist."
239         fi
240
241         if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
242             failure "Improper permissions on authorized_user_ids file."
243         fi
244
245         # process authorized_user_ids file
246         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
247         RETURN="$?"
248         ;;
249
250     'gen-subkey'|'g')
251         gen_subkey "$@"
252         ;;
253
254     'help'|'h'|'?')
255         usage
256         ;;
257
258     *)
259         failure "Unknown command: '$COMMAND'
260 Type '$PGRM help' for usage."
261         ;;
262 esac
263
264 exit "$RETURN"