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