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