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