Merge commit 'jrollins/master'
[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     if [ -z "$1" ] ; then
93         # find all secret keys
94         keyID=$(gpg --with-colons --list-secret-keys | grep ^sec | cut -f5 -d:)
95         # if multiple sec keys exist, fail
96         if (( $(echo "$keyID" | wc -l) > 1 )) ; then
97             echo "Multiple secret keys found:"
98             echo "$keyID"
99             failure "Please specify which primary key to use."
100         fi
101     else
102         keyID="$1"
103     fi
104
105     # get key output, and fail if not found
106     gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
107         "$keyID") || failure
108
109     # fail if multiple sec lines are returned, which means the id
110     # given is not unique
111     if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
112         failure "Key ID '$keyID' is not unique."
113     fi
114
115     # prompt if an authentication subkey already exists
116     if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
117         echo "An authentication subkey already exists for key '$keyID'."
118         read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
119         if [ "${OK/y/Y}" != 'Y' ] ; then
120             failure "aborting."
121         fi
122     fi
123
124     # set subkey defaults
125     # prompt about key expiration if not specified
126     if [ -z "$keyExpire" ] ; then
127         cat <<EOF
128 Please specify how long the key should be valid.
129          0 = key does not expire
130       <n>  = key expires in n days
131       <n>w = key expires in n weeks
132       <n>m = key expires in n months
133       <n>y = key expires in n years
134 EOF
135         while [ -z "$keyExpire" ] ; do
136             read -p "Key is valid for? (0) " keyExpire
137             if ! test_gpg_expire ${keyExpire:=0} ; then
138                 echo "invalid value"
139                 unset keyExpire
140             fi
141         done
142     elif ! test_gpg_expire "$keyExpire" ; then
143         failure "invalid key expiration value '$keyExpire'."
144     fi
145
146     # generate the list of commands that will be passed to edit-key
147     editCommands=$(cat <<EOF
148 addkey
149 7
150 S
151 E
152 A
153 Q
154 $keyLength
155 $keyExpire
156 save
157 EOF
158 )
159
160     log "generating subkey..."
161     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
162     log "done."
163 }
164
165 ########################################################################
166 # MAIN
167 ########################################################################
168
169 # unset variables that should be defined only in config file
170 unset KEYSERVER
171 unset CHECK_KEYSERVER
172 unset KNOWN_HOSTS
173 unset HASH_KNOWN_HOSTS
174 unset AUTHORIZED_KEYS
175
176 # load global config
177 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
178
179 # set monkeysphere home directory
180 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
181 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
182
183 # load local config
184 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
185
186 # set empty config variables with ones from the environment, or from
187 # config file, or with defaults
188 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
189 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
190 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
191 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
192 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
193 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
194
195 # other variables not in config file
196 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
197 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
198 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
199
200 # export GNUPGHOME and make sure gpg home exists with proper
201 # permissions
202 export GNUPGHOME
203 mkdir -p -m 0700 "$GNUPGHOME"
204
205 # get subcommand
206 COMMAND="$1"
207 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
208 shift
209
210 case $COMMAND in
211     'update-known_hosts'|'update-known-hosts'|'k')
212         MODE='known_hosts'
213
214         # check permissions on the known_hosts file path
215         if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
216             failure "Improper permissions on known_hosts file path."
217         fi
218
219         # if hosts are specified on the command line, process just
220         # those hosts
221         if [ "$1" ] ; then
222             update_known_hosts "$@"
223             RETURN="$?"
224
225         # otherwise, if no hosts are specified, process every host
226         # in the user's known_hosts file
227         else
228             # exit if the known_hosts file does not exist
229             if [ ! -e "$KNOWN_HOSTS" ] ; then
230                 log "known_hosts file '$KNOWN_HOSTS' does not exist."
231                 exit
232             fi
233
234             process_known_hosts
235             RETURN="$?"
236         fi
237         ;;
238
239     'update-authorized_keys'|'update-authorized-keys'|'a')
240         MODE='authorized_keys'
241
242         # check permissions on the authorized_user_ids file path
243         if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
244             failure "Improper permissions on authorized_user_ids file path."
245         fi
246
247         # check permissions on the authorized_keys file path
248         if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
249             failure "Improper permissions on authorized_keys file path."
250         fi
251
252         # exit if the authorized_user_ids file is empty
253         if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
254             log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
255             exit
256         fi
257
258         # process authorized_user_ids file
259         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
260         RETURN="$?"
261         ;;
262
263     'gen-subkey'|'g')
264         gen_subkey "$@"
265         ;;
266
267     'help'|'h'|'?')
268         usage
269         ;;
270
271     *)
272         failure "Unknown command: '$COMMAND'
273 Type '$PGRM help' for usage."
274         ;;
275 esac
276
277 exit "$RETURN"