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