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