Its more useful and standard to actually output the 'help' output when
[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}/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}/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 ########################################################################
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 NAME[:PORT]     import existing ssh key to gpg
56  show-key (s)                        output all host key information
57  publish-key (p)                     publish host key to keyserver
58  set-expire (e) [EXPIRE]             set host key expiration
59  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
60  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
61  add-revoker (r+) KEYID|FILE         add a revoker to the host key
62  revoke-key                          generate and/or publish revocation
63                                      certificate for host key
64
65  version (v)                         show version number
66  help (h,?)                          this help
67
68 See ${PGRM}(8) for more info.
69 EOF
70 }
71
72 # function to interact with the gpg keyring
73 gpg_host() {
74     GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
75 }
76
77 # command to list the info about the host key, in colon format, to
78 # stdout
79 gpg_host_list() {
80     gpg_host --list-keys --with-colons --fixed-list-mode \
81         --with-fingerprint --with-fingerprint \
82         "0x${HOST_FINGERPRINT}!"
83
84 }
85
86 # command for edit key scripts, takes scripts on stdin
87 gpg_host_edit() {
88     gpg_host --command-fd 0 --edit-key "0x${HOST_FINGERPRINT}!" "$@"
89 }
90
91 # export the host public key to the monkeysphere gpg pub key file
92 update_gpg_pub_file() {
93     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
94     gpg_host --export --armor --export-options export-minimal \
95         "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
96 }
97
98 # load the host fingerprint into the fingerprint variable, using the
99 # export gpg pub key file
100 # FIXME: this seems much less than ideal, with all this temp keyring
101 # stuff.  is there a way we can do this without having to create temp
102 # files?  what if we stored the fingerprint in MHDATADIR/fingerprint?
103 load_fingerprint() {
104     if [ -f "$HOST_KEY_FILE" ] ; then
105         HOST_FINGERPRINT=$( \
106             (FUBAR=$(msmktempdir) && export GNUPGHOME="$FUBAR" \
107             && gpg --quiet --import \
108             && gpg --quiet --list-keys --with-colons --with-fingerprint \
109             && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
110             | grep '^fpr:' | cut -d: -f10 )
111     else
112         failure "host key gpg pub file not found."
113     fi
114 }
115
116 # load the host fingerprint into the fingerprint variable, using the
117 # gpg host secret key
118 load_fingerprint_secret() {
119     HOST_FINGERPRINT=$( \
120         gpg_host --list-secret-key --with-colons --with-fingerprint \
121         | grep '^fpr:' | cut -d: -f10 )
122 }
123
124 # fail if host key present
125 check_host_key() {
126     [ ! -s "$HOST_KEY_FILE" ] \
127         || failure "An OpenPGP host key already exists."
128 }
129
130 # fail if host key not present
131 check_host_no_key() {
132     [ -s "$HOST_KEY_FILE" ] \
133         || failure "You don't appear to have a Monkeysphere host key on this server.
134 Please run 'monkeysphere-host import-key...' first."
135 }
136
137 # return 0 if user ID was found.
138 # return 1 if user ID not found.
139 find_host_userid() {
140     local userID="$1"
141     local tmpuidMatch
142
143     # match to only "unknown" user IDs (host has no need for ultimate trust)
144     tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
145
146     # See whether the requsted user ID is present
147     gpg_host_list | cut -f1,2,10 -d: | \
148         grep -q -x -F "$tmpuidMatch" 2>/dev/null
149 }
150
151 # show info about the host key
152 show_key() {
153     local GNUPGHOME
154     local TMPSSH
155     local revokers
156
157     # tmp gpghome dir
158     export GNUPGHOME=$(msmktempdir)
159
160     # trap to remove tmp dir if break
161     trap "rm -rf $GNUPGHOME" EXIT
162
163     # import the host key into the tmp dir
164     gpg --quiet --import <"$HOST_KEY_FILE"
165
166     # create the ssh key
167     TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
168     gpg --export | openpgp2ssh 2>/dev/null >"$TMPSSH"
169
170     # get the gpg fingerprint
171     HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
172         | grep '^fpr:' | cut -d: -f10 )
173
174     # list the host key info
175     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
176     # FIXME: can we show uid validity somehow?
177     gpg --list-keys --fingerprint \
178         --list-options show-unusable-uids 2>/dev/null \
179         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
180         | egrep -v '^-+$'
181
182     # list revokers, if there are any
183     revokers=$(gpg --list-keys --with-colons --fixed-list-mode \
184         | awk -F: '/^rvk:/{ print $10 }' )
185     if [ "$revokers" ] ; then
186         echo "The following keys are allowed to revoke this host key:"
187         for key in $revokers ; do
188             echo "revoker: $key"
189         done
190         echo
191     fi
192
193     # list the pgp fingerprint
194     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
195
196     # list the ssh fingerprint
197     echo -n "ssh fingerprint: "
198     ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
199
200     # remove the tmp file
201     trap - EXIT
202     rm -rf "$GNUPGHOME"
203 }
204
205 ########################################################################
206 # MAIN
207 ########################################################################
208
209 # load configuration file
210 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
211     && . "$MONKEYSPHERE_HOST_CONFIG"
212
213 # set empty config variable with ones from the environment, or with
214 # defaults
215 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
216 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
217 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
218 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
219 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
220 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
221
222 # other variables
223 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
224 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
225
226 # export variables needed in su invocation
227 export DATE
228 export LOG_LEVEL
229 export KEYSERVER
230 export CHECK_KEYSERVER
231 export MONKEYSPHERE_USER
232 export MONKEYSPHERE_GROUP
233 export PROMPT
234 export GNUPGHOME_HOST
235 export GNUPGHOME
236 export HOST_FINGERPRINT
237 export LOG_PREFIX
238
239 # get subcommand
240 COMMAND="$1"
241 [ "$COMMAND" ] || $PGRM help
242 shift
243
244 case $COMMAND in
245     'import-key'|'i')
246         check_host_key
247         source "${MHSHAREDIR}/import_key"
248         import_key "$@"
249         ;;
250
251     'show-key'|'show'|'s')
252         check_host_no_key
253         show_key
254         ;;
255
256     'set-expire'|'extend-key'|'e')
257         check_host_no_key
258         load_fingerprint
259         source "${MHSHAREDIR}/set_expire"
260         set_expire "$@"
261         ;;
262
263     'add-hostname'|'add-name'|'n+')
264         check_host_no_key
265         load_fingerprint
266         source "${MHSHAREDIR}/add_hostname"
267         add_hostname "$@"
268         ;;
269
270     'revoke-hostname'|'revoke-name'|'n-')
271         check_host_no_key
272         load_fingerprint
273         source "${MHSHAREDIR}/revoke_hostname"
274         revoke_hostname "$@"
275         ;;
276
277     'add-revoker'|'r+')
278         check_host_no_key
279         load_fingerprint
280         source "${MHSHAREDIR}/add_revoker"
281         add_revoker "$@"
282         ;;
283
284     'revoke-key')
285         check_host_no_key
286         load_fingerprint
287         source "${MHSHAREDIR}/revoke_key"
288         revoke_key "$@"
289         ;;
290
291     'publish-key'|'publish'|'p')
292         check_host_no_key
293         load_fingerprint
294         source "${MHSHAREDIR}/publish_key"
295         publish_key
296         ;;
297
298     'diagnostics'|'d')
299         source "${MHSHAREDIR}/diagnostics"
300         diagnostics
301         ;;
302
303     'update-gpg-pub-file')
304         load_fingerprint_secret
305         update_gpg_pub_file
306         ;;
307
308     'version'|'v')
309         version
310         ;;
311
312     '--help'|'help'|'-h'|'h'|'?')
313         usage
314         ;;
315
316     *)
317         failure "Unknown command: '$COMMAND'
318 Type '$PGRM help' for usage."
319         ;;
320 esac