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