make sure MHTMPDIR is defined and created
[monkeysphere.git] / src / monkeysphere-host
1 #!/usr/bin/env bash
2
3 # monkeysphere-host: Monkeysphere host admin tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@finestructure.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 ########################################################################
15 set -e
16
17 # set the pipefail option so pipelines fail on first command failure
18 set -o pipefail
19
20 PGRM=$(basename $0)
21
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
23 export SYSSHAREDIR
24 . "${SYSSHAREDIR}/common" || exit 1
25
26 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
27 export SYSDATADIR
28
29 # sharedir for host functions
30 MHSHAREDIR="${SYSSHAREDIR}/mh"
31
32 # datadir for host functions
33 MHDATADIR="${SYSDATADIR}/host"
34
35 # temp directory for temp gnupghome directories for add_revoker
36 MHTMPDIR="${MHDATADIR}/tmp"
37 export MHTMPDIR
38
39 # host pub key files
40 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
41 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
42
43 # UTC date in ISO 8601 format if needed
44 DATE=$(date -u '+%FT%T')
45
46 # unset some environment variables that could screw things up
47 unset GREP_OPTIONS
48
49 # default return code
50 RETURN=0
51
52 ########################################################################
53 # FUNCTIONS
54 ########################################################################
55
56 usage() {
57     cat <<EOF >&2
58 usage: $PGRM <subcommand> [options] [args]
59 Monkeysphere host admin tool.
60
61 subcommands:
62  show-key (s)                        output all host key information
63  set-expire (e) EXPIRE               set host key expiration
64  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
65  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
66  add-revoker (o) FINGERPRINT         add a revoker to the host key
67  revoke-key (r)                      revoke host key
68  publish-key (p)                     publish host key to keyserver
69
70  expert <expert-subcommand>          run expert command
71  expert help                         expert command help
72
73  version (v)                         show version number
74  help (h,?)                          this help
75
76 EOF
77 }
78
79 # function to interact with the gpg keyring
80 gpg_host() {
81     GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
82 }
83
84 # command to list the info about the host key, in colon format
85 gpg_host_list() {
86     gpg_host --list-keys --with-colons --fixed-list-mode \
87         --with-fingerprint --with-fingerprint \
88         "0x${HOST_FINGERPRINT}!"
89
90 }
91
92 # command for edit key scripts, takes scripts on stdin
93 gpg_host_edit() {
94     gpg_host --quiet --command-fd 0 --edit-key \
95         "0x${HOST_FINGERPRINT}!" "$@"
96 }
97
98 # export the host key to stdout
99 gpg_host_export() {
100     gpg_host --export --armor --export-options export-minimal \
101         "0x${HOST_FINGERPRINT}!"
102 }
103
104 # export the host public key to the monkeysphere gpg pub key file
105 create_gpg_pub_file() {
106     log debug "creating openpgp public key file..."
107     gpg_host_export > "$HOST_KEY_PUB_GPG"
108     log info "GPG host public key file: $HOST_KEY_PUB_GPG"
109 }
110
111 # load the host fingerprint into the fingerprint variable, using the
112 # export gpg pub key file
113 # FIXME: this seems much less than ideal, with all this temp keyring
114 # stuff.  is there a way we can do this without having to create temp
115 # files?
116 load_fingerprint() {
117     if [ -f "$HOST_KEY_PUB_GPG" ] ; then
118         HOST_FINGERPRINT=$( \
119             (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
120             && gpg --quiet --import \
121             && gpg --quiet --list-keys --with-colons --with-fingerprint \
122             && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
123             | grep '^fpr:' | cut -d: -f10 )
124     else
125         HOST_FINGERPRINT=
126     fi
127 }
128
129 # load the host fingerprint into the fingerprint variable, using the
130 # gpg host secret key
131 load_fingerprint_secret() {
132     HOST_FINGERPRINT=$( \
133         gpg_host --quiet --list-secret-key \
134         --with-colons --with-fingerprint \
135         | grep '^fpr:' | cut -d: -f10 )
136 }
137
138 # output host key ssh fingerprint
139 load_ssh_fingerprint() {
140     [ -f "$HOST_KEY_PUB" ] || return 0
141     HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
142         | awk '{ print $1, $2, $4 }')
143 }
144
145 # fail if host key present
146 check_host_key() {
147     [ -z "$HOST_FINGERPRINT" ] \
148         || failure "An OpenPGP host key already exists."
149 }
150
151 # fail if host key not present
152 check_host_no_key() {
153     [ "$HOST_FINGERPRINT" ] \
154         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-host expert import-key' first."
155 }
156
157 # output the index of a user ID on the host key
158 # return 1 if user ID not found
159 find_host_userid() {
160     local userID="$1"
161     local tmpuidMatch
162     local line
163
164     # match to only ultimately trusted user IDs
165     tmpuidMatch="u:$(echo $userID | gpg_escape)"
166
167     # find the index of the requsted user ID
168     # NOTE: this is based on circumstantial evidence that the order of
169     # this output is the appropriate index
170     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
171         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
172
173     if [ "$line" ] ; then
174         echo ${line%%:*}
175         return 0
176     else
177         return 1
178     fi
179 }
180
181 # show info about the host key
182 show_key() {
183     gpg_host --fingerprint --list-key --list-options show-unusable-uids \
184         "0x${HOST_FINGERPRINT}!" 2>/dev/null || true
185     # FIXME: make sure expiration date is shown
186
187     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
188
189     load_ssh_fingerprint
190
191     if [ "$HOST_FINGERPRINT_SSH" ] ; then
192         echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
193     else
194         log error "SSH host key not found."
195     fi
196
197     # FIXME: other relevant key parameters?
198 }
199
200 ########################################################################
201 # MAIN
202 ########################################################################
203
204 # unset variables that should be defined only in config file
205 unset KEYSERVER
206 unset MONKEYSPHERE_USER
207
208 # load configuration file
209 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
210
211 # set empty config variable with ones from the environment, or with
212 # defaults
213 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
214 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
215 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
216 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
217 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
218
219 # other variables
220 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
221 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
222
223 # export variables needed in su invocation
224 export DATE
225 export MODE
226 export LOG_LEVEL
227 export MONKEYSPHERE_USER
228 export KEYSERVER
229 export GNUPGHOME_HOST
230 export GNUPGHOME
231 export HOST_FINGERPRINT=
232 export HOST_FINGERPRINT_SSH=
233
234 # get subcommand
235 COMMAND="$1"
236 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
237 shift
238
239 case $COMMAND in
240     'show-key'|'show'|'s')
241         load_fingerprint
242         check_host_no_key
243         show_key
244         ;;
245
246     'set-expire'|'extend-key'|'e')
247         load_fingerprint
248         check_host_no_key
249         source "${MHSHAREDIR}/set_expire"
250         set_expire "$@"
251         ;;
252
253     'add-hostname'|'add-name'|'n+')
254         load_fingerprint
255         check_host_no_key
256         source "${MHSHAREDIR}/add_hostname"
257         add_hostname "$@"
258         ;;
259
260     'revoke-hostname'|'revoke-name'|'n-')
261         load_fingerprint
262         check_host_no_key
263         source "${MHSHAREDIR}/revoke_hostname"
264         revoke_hostname "$@"
265         ;;
266
267     'add-revoker'|'o')
268         load_fingerprint
269         check_host_no_key
270         source "${MHSHAREDIR}/add_revoker"
271         add_revoker "$@"
272         ;;
273
274     'revoke-key'|'r')
275         load_fingerprint
276         check_host_no_key
277         source "${MHSHAREDIR}/revoke_key"
278         revoke_key "$@"
279         ;;
280
281     'publish-key'|'publish'|'p')
282         load_fingerprint
283         check_host_no_key
284         source "${MHSHAREDIR}/publish_key"
285         publish_key
286         ;;
287
288     'expert')
289         SUBCOMMAND="$1"
290         shift
291         case "$SUBCOMMAND" in
292             'help'|'h'|'?')
293                 cat <<EOF
294 usage: $PGRM expert <subcommand> [options] [args]
295
296 expert subcommands:
297  import-key (i) FILE [NAME[:PORT]]   import existing ssh key to gpg
298  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
299    --length (-l) BITS                  key length in bits (2048)
300  diagnostics (d)                     monkeysphere host status
301
302 EOF
303                 ;;
304
305             'import-key'|'i')
306                 load_fingerprint
307                 check_host_key
308                 source "${MHSHAREDIR}/import_key"
309                 import_key "$@"
310                 ;;
311
312             'gen-key'|'g')
313                 load_fingerprint
314                 check_host_key
315                 source "${MHSHAREDIR}/gen_key"
316                 gen_key "$@"
317                 ;;
318
319             'diagnostics'|'d')
320                 source "${MHSHAREDIR}/diagnostics"
321                 diagnostics
322                 ;;
323
324             *)
325                 failure "Unknown expert subcommand: '$COMMAND'
326 Type '$PGRM help' for usage."
327                 ;;
328         esac
329         ;;
330
331     'version'|'v')
332         echo "$VERSION"
333         ;;
334
335     '--help'|'help'|'-h'|'h'|'?')
336         usage
337         ;;
338
339     *)
340         failure "Unknown command: '$COMMAND'
341 Type '$PGRM help' for usage."
342         ;;
343 esac
344
345 exit "$RETURN"