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