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