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