fix ma so that the setup command is folded into the other commands, so
[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_FILE="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
41
42 # UTC date in ISO 8601 format if needed
43 DATE=$(date -u '+%FT%T')
44
45 # unset some environment variables that could screw things up
46 unset GREP_OPTIONS
47
48 # default return code
49 RETURN=0
50
51 ########################################################################
52 # FUNCTIONS
53 ########################################################################
54
55 usage() {
56     cat <<EOF >&2
57 usage: $PGRM <subcommand> [options] [args]
58 Monkeysphere host admin tool.
59
60 subcommands:
61  show-key (s)                        output all host key information
62  set-expire (e) EXPIRE               set host key expiration
63  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
64  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
65  add-revoker (o) FINGERPRINT         add a revoker to the host key
66  revoke-key (r)                      revoke host key
67  publish-key (p)                     publish host key to keyserver
68
69  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
70
71  version (v)                         show version number
72  help (h,?)                          this help
73
74 See ${PGRM}(8) for more info.
75 EOF
76 }
77
78 # function to interact with the gpg keyring
79 gpg_host() {
80     GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
81 }
82
83 # command to list the info about the host key, in colon format, to
84 # stdout
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 # FIXME: should we supress all the edit script spew?  or pipe it
94 # through log debug?
95 gpg_host_edit() {
96     gpg_host --quiet --command-fd 0 --no-tty --edit-key \
97         "0x${HOST_FINGERPRINT}!" "$@" 2>&1 | log debug
98 }
99
100 # export the host public key to the monkeysphere gpg pub key file
101 update_gpg_pub_file() {
102     log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
103     gpg_host --export --armor --export-options export-minimal \
104         "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
105 }
106
107 # load the host fingerprint into the fingerprint variable, using the
108 # export gpg pub key file
109 # FIXME: this seems much less than ideal, with all this temp keyring
110 # stuff.  is there a way we can do this without having to create temp
111 # files?  what if we stored the fingerprint in MHDATADIR/fingerprint?
112 load_fingerprint() {
113     if [ -f "$HOST_KEY_FILE" ] ; then
114         HOST_FINGERPRINT=$( \
115             (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
116             && gpg --quiet --import \
117             && gpg --quiet --list-keys --with-colons --with-fingerprint \
118             && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
119             | grep '^fpr:' | cut -d: -f10 )
120     else
121         HOST_FINGERPRINT=
122     fi
123 }
124
125 # load the host fingerprint into the fingerprint variable, using the
126 # gpg host secret key
127 load_fingerprint_secret() {
128     HOST_FINGERPRINT=$( \
129         gpg_host --quiet --list-secret-key \
130         --with-colons --with-fingerprint \
131         | grep '^fpr:' | cut -d: -f10 )
132 }
133
134 # fail if host key present
135 check_host_key() {
136     [ ! -s "$HOST_KEY_FILE" ] \
137         || failure "An OpenPGP host key already exists."
138 }
139
140 # fail if host key not present
141 check_host_no_key() {
142     [ -s "$HOST_KEY_FILE" ] \
143         || failure "You don't appear to have a Monkeysphere host key on this server.
144 Please run 'monkeysphere-host import-key' first."
145 }
146
147 # output the index of a user ID on the host key
148 # return 1 if user ID not found
149 find_host_userid() {
150     local userID="$1"
151     local tmpuidMatch
152     local line
153
154     # match to only ultimately trusted user IDs
155     tmpuidMatch="u:$(echo $userID | gpg_escape)"
156
157     # find the index of the requsted user ID
158     # NOTE: this is based on circumstantial evidence that the order of
159     # this output is the appropriate index
160     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
161         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
162
163     if [ "$line" ] ; then
164         echo ${line%%:*}
165         return 0
166     else
167         return 1
168     fi
169 }
170
171 # show info about the host key
172 show_key() {
173     local GNUPGHOME
174
175     # tmp gpghome dir
176     export GNUPGHOME=$(mktemp -d)
177
178     # trap to remove tmp dir if break
179     trap "rm -rf $GNUPGHOME" EXIT
180
181     # import the host key into the tmp dir
182     gpg --quiet --import <"$HOST_KEY_FILE"
183
184     HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
185         | grep '^fpr:' | cut -d: -f10 )
186
187     # list the host key info
188     # FIXME: make no-show-keyring work so we don't have to do the grep'ing
189     # FIXME: can we show uid validity somehow?
190     gpg --list-keys --fingerprint \
191         --list-options show-unusable-uids 2>/dev/null \
192         | grep -v "^${GNUPGHOME}/pubring.gpg$" \
193         | egrep -v '^-+$'
194
195     # list the pgp fingerprint
196     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
197
198     # list the ssh fingerprint
199     echo -n "ssh fingerprint: "
200     ssh-keygen -l -f /dev/stdin \
201         <<<$(openpgp2ssh <"$HOST_KEY_FILE" 2>/dev/null) \
202         | awk '{ print $1, $2, $4 }'
203
204     # remove the tmp file
205     trap - EXIT
206     rm -rf "$GNUPGHOME"
207 }
208
209 ########################################################################
210 # MAIN
211 ########################################################################
212
213 # unset variables that should be defined only in config file or in
214 # MONKEYSPHERE_ variables
215 unset LOG_LEVEL
216 unset KEYSERVER
217 unset MONKEYSPHERE_USER
218 unset PROMPT
219
220 # load configuration file
221 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
222
223 # set empty config variable with ones from the environment, or with
224 # defaults
225 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
226 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
227 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
228 PROMPT=${MONKEYSPHERE_PROMPT:=${PROMPT:="true"}}
229
230 # other variables
231 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
232 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
233
234 # export variables needed in su invocation
235 export DATE
236 export MODE
237 export LOG_LEVEL
238 export KEYSERVER
239 export MONKEYSPHERE_USER
240 export PROMPT
241 export CHECK_KEYSERVER
242 export GNUPGHOME_HOST
243 export GNUPGHOME
244 export HOST_FINGERPRINT=
245
246 # get subcommand
247 COMMAND="$1"
248 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
249 shift
250
251 case $COMMAND in
252     'show-key'|'show'|'s')
253         check_host_no_key
254         show_key
255         ;;
256
257     'set-expire'|'extend-key'|'e')
258         check_host_no_key
259         load_fingerprint
260         source "${MHSHAREDIR}/set_expire"
261         set_expire "$@"
262         ;;
263
264     'add-hostname'|'add-name'|'n+')
265         check_host_no_key
266         load_fingerprint
267         source "${MHSHAREDIR}/add_hostname"
268         add_hostname "$@"
269         ;;
270
271     'revoke-hostname'|'revoke-name'|'n-')
272         check_host_no_key
273         load_fingerprint
274         source "${MHSHAREDIR}/revoke_hostname"
275         revoke_hostname "$@"
276         ;;
277
278     'add-revoker'|'o')
279         check_host_no_key
280         load_fingerprint
281         source "${MHSHAREDIR}/add_revoker"
282         add_revoker "$@"
283         ;;
284
285     'revoke-key'|'r')
286         check_host_no_key
287         load_fingerprint
288         source "${MHSHAREDIR}/revoke_key"
289         revoke_key "$@"
290         ;;
291
292     'publish-key'|'publish'|'p')
293         check_host_no_key
294         load_fingerprint
295         source "${MHSHAREDIR}/publish_key"
296         publish_key
297         ;;
298
299     'import-key'|'i')
300         check_host_key
301         source "${MHSHAREDIR}/import_key"
302         import_key "$@"
303         ;;
304
305     'diagnostics'|'d')
306         load_fingerprint
307         source "${MHSHAREDIR}/diagnostics"
308         diagnostics
309         ;;
310
311     'version'|'v')
312         echo "$VERSION"
313         ;;
314
315     '--help'|'help'|'-h'|'h'|'?')
316         usage
317         ;;
318
319     *)
320         failure "Unknown command: '$COMMAND'
321 Type '$PGRM help' for usage."
322         ;;
323 esac
324
325 exit "$RETURN"