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