1fa3aea32901cc858677116bc1dcf56dc45b1852
[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-2010, 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}/defaultenv"
25 . "${SYSSHAREDIR}/common"
26
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
28 export SYSDATADIR
29
30 # sharedir for host functions
31 MHSHAREDIR="${SYSSHAREDIR}/mh"
32
33 # datadir for host functions
34 MHDATADIR="${SYSDATADIR}/host"
35
36 # host pub key files
37 HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.pgp"
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 ########################################################################
46 # FUNCTIONS
47 ########################################################################
48
49 usage() {
50     cat <<EOF >&2
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere host admin tool.
53
54 subcommands:
55  import-key (i) FILE SERVICENAME       import PEM-encoded key from file
56  show-keys (s) [KEYID ...]             output host key information
57  publish-keys (p) [KEYID ...]          publish key(s) to keyserver
58  set-expire (e) EXPIRE [KEYID]         set key expiration
59  add-servicename (n+) SERVICENAME [KEYID]
60                                        add a service name to key
61  revoke-servicename (n-) SERVICENAME [KEYID]
62                                        revoke a service name from key
63  add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
64                                        add a revoker to key
65  revoke-key [KEYID]                    generate and/or publish revocation
66                                        certificate for key
67
68  version (v)                           show version number
69  help (h,?)                            this help
70
71 See ${PGRM}(8) for more info.
72 EOF
73 }
74
75 # function to interact with the gpg keyring
76 gpg_host() {
77     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
78 }
79
80 # list the info about the a key, in colon format, to stdout
81 gpg_host_list_keys() {
82     gpg_host --list-keys --with-colons --fixed-list-mode \
83         --with-fingerprint --with-fingerprint \
84         "$1"
85 }
86
87 # edit key scripts, takes scripts on stdin, and keyID as first input
88 gpg_host_edit() {
89     gpg_host --command-fd 0 --edit-key "$@"
90 }
91
92 # export the monkeysphere OpenPGP pub key file
93 update_pgp_pub_file() {
94     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
95     gpg_host --export --armor --export-options export-minimal \
96         $(gpg_host --list-secret-keys --with-colons --fingerprint | grep ^fpr | cut -f10 -d:) \
97         > "$HOST_KEY_FILE"
98 }
99
100 # check that the service name is well formed
101 check_service_name() {
102     local name="$1"
103     log error "FIX ME: check service name"
104 }
105
106 # fail if host key not present
107 check_no_keys() {
108     [ -s "$HOST_KEY_FILE" ] \
109         || failure "You don't appear to have a Monkeysphere host key on this server.
110 Please run 'monkeysphere-host import-key' import a key."
111 }
112
113 # key input to functions, outputs full fingerprint of specified key if
114 # found
115 check_key_input() {
116     local keyID="$1"
117     # array of fingerprints
118     local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
119
120     case ${#fprs[@]} in
121         0)
122             failure "You don't appear to have any Monkeysphere host keys.
123 Please run 'monkeysphere-host import-key' to import a key."
124             ;;
125         1)
126             :
127             ;;
128         *)
129             if [ -z "$keyID" ] ; then
130                 failure "Your host keyring contains multiple keys.
131 Please specify one to act on (see 'monkeysphere-host show-keys')."
132             fi
133             ;;
134     esac
135     printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
136         || failure "Host key '$keyID' not found."
137 }
138
139 # return 0 if user ID was found.
140 # return 1 if user ID not found.
141 check_key_userid() {
142     local keyID="$1"
143     local userID="$2"
144     local tmpuidMatch
145
146     # match to only "unknown" user IDs (host has no need for ultimate trust)
147     tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
148
149     # See whether the requsted user ID is present
150     gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
151         grep -q -x -F "$tmpuidMatch" 2>/dev/null
152 }
153
154 # run command looped over keys
155 multi_key() {
156     local cmd="$1"
157     shift
158     local keys=$@
159     local i=0
160     local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
161     local key
162
163     check_no_keys
164
165     if [[ -z "$1" || "$1" == '--all' ]] ; then
166         keys="${fprs[@]}"
167     fi
168
169     for key in $keys ; do
170         if (( i++ > 0 )) ; then
171             echo "##############################"
172         fi
173         eval "$cmd" "$key"
174     done
175 }
176
177 # show info about the a key
178 show_key() {
179     local id="$1"
180     local GNUPGHOME
181     local fingerprint
182     local tmpssh
183     local revokers
184
185     # tmp gpghome dir
186     export GNUPGHOME=$(msmktempdir)
187
188     # trap to remove tmp dir if break
189     trap "rm -rf $GNUPGHOME" EXIT
190
191     # import the host key into the tmp dir
192     gpg --quiet --import <"$HOST_KEY_FILE"
193
194     # get the gpg fingerprint
195     if gpg --quiet --list-keys \
196         --with-colons --with-fingerprint "$id" \
197         | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
198         fingerprint=$(cat "$GNUPGHOME"/fingerprint)
199     else
200         failure "ID '$id' not found."
201     fi
202
203     # create the ssh key
204     tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
205     gpg --export "$fingerprint" 2>/dev/null \
206         | openpgp2ssh 2>/dev/null >"$tmpssh"
207
208     # list the host key info
209     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
210     # FIXME: can we show uid validity somehow?
211     gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
212         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
213         | egrep -v '^-+$'
214
215     # list revokers, if there are any
216     revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
217         | awk -F: '/^rvk:/{ print $10 }' )
218     if [ "$revokers" ] ; then
219         echo "The following keys are allowed to revoke this host key:"
220         for key in $revokers ; do
221             echo "revoker: $key"
222         done
223         echo
224     fi
225
226     # list the pgp fingerprint
227     echo "OpenPGP fingerprint: $fingerprint"
228
229     # list the ssh fingerprint
230     echo -n "ssh fingerprint: "
231     ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
232
233     # remove the tmp file
234     trap - EXIT
235     rm -rf "$GNUPGHOME"
236 }
237
238 ########################################################################
239 # MAIN
240 ########################################################################
241
242 # load configuration file
243 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
244     && . "$MONKEYSPHERE_HOST_CONFIG"
245
246 # set empty config variable with ones from the environment, or with
247 # defaults
248 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
249 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
250 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
251 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
252 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
253 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
254
255 # other variables
256 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
257 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
258
259 # export variables needed in su invocation
260 export DATE
261 export LOG_LEVEL
262 export KEYSERVER
263 export CHECK_KEYSERVER
264 export MONKEYSPHERE_USER
265 export MONKEYSPHERE_GROUP
266 export PROMPT
267 export GNUPGHOME_HOST
268 export GNUPGHOME
269 export HOST_FINGERPRINT
270 export LOG_PREFIX
271
272 if [ "$#" -eq 0 ] ; then 
273     usage
274     failure "Please supply a subcommand."
275 fi
276
277 # get subcommand
278 COMMAND="$1"
279 shift
280
281 case $COMMAND in
282     'import-key'|'i')
283         source "${MHSHAREDIR}/import_key"
284         import_key "$@"
285         ;;
286
287     'show-keys'|'show-key'|'show'|'s')
288         multi_key show_key "$@"
289         ;;
290
291     'set-expire'|'extend-key'|'e')
292         source "${MHSHAREDIR}/set_expire"
293         set_expire "$@"
294         ;;
295
296     'add-servicename'|'add-hostname'|'add-name'|'n+')
297         source "${MHSHAREDIR}/add_name"
298         add_name "$@"
299         ;;
300
301     'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
302         source "${MHSHAREDIR}/revoke_name"
303         revoke_name "$@"
304         ;;
305
306     'add-revoker'|'r+')
307         source "${MHSHAREDIR}/add_revoker"
308         add_revoker "$@"
309         ;;
310
311     'revoke-key')
312         source "${MHSHAREDIR}/revoke_key"
313         revoke_key "$@"
314         ;;
315
316     'publish-keys'|'publish-key'|'publish'|'p')
317         source "${MHSHAREDIR}/publish_key"
318         multi_key publish_key "$@"
319         ;;
320
321     'diagnostics'|'d')
322         source "${MHSHAREDIR}/diagnostics"
323         diagnostics
324         ;;
325
326     'update-pgp-pub-file')
327         update_pgp_pub_file
328         ;;
329
330     'version'|'v')
331         version
332         ;;
333
334     '--help'|'help'|'-h'|'h'|'?')
335         usage
336         ;;
337
338     *)
339         failure "Unknown command: '$COMMAND'
340 Try '$PGRM help' for usage."
341         ;;
342 esac