check host for gpg pub key file instead of fingerprint, and modify show_key to be...
[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
84 gpg_host_list() {
85     gpg_host --list-keys --with-colons --fixed-list-mode \
86         --with-fingerprint --with-fingerprint \
87         "0x${HOST_FINGERPRINT}!"
88
89 }
90
91 # command for edit key scripts, takes scripts on stdin
92 gpg_host_edit() {
93     gpg_host --quiet --command-fd 0 --edit-key \
94         "0x${HOST_FINGERPRINT}!" "$@"
95 }
96
97 # export the host public key to the monkeysphere gpg pub key file
98 create_gpg_pub_file() {
99     log debug "creating openpgp public key file..."
100     gpg_host --export --armor --export-options export-minimal \
101         "0x${HOST_FINGERPRINT}!" > "$HOST_KEY_FILE"
102     log info "GPG host public key file: $HOST_KEY_FILE"
103 }
104
105 # load the host fingerprint into the fingerprint variable, using the
106 # export gpg pub key file
107 # FIXME: this seems much less than ideal, with all this temp keyring
108 # stuff.  is there a way we can do this without having to create temp
109 # files?
110 load_fingerprint() {
111     if [ -f "$HOST_KEY_FILE" ] ; then
112         HOST_FINGERPRINT=$( \
113             (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
114             && gpg --quiet --import \
115             && gpg --quiet --list-keys --with-colons --with-fingerprint \
116             && rm -rf "$FUBAR") <"$HOST_KEY_FILE" \
117             | grep '^fpr:' | cut -d: -f10 )
118     else
119         HOST_FINGERPRINT=
120     fi
121 }
122
123 # load the host fingerprint into the fingerprint variable, using the
124 # gpg host secret key
125 load_fingerprint_secret() {
126     HOST_FINGERPRINT=$( \
127         gpg_host --quiet --list-secret-key \
128         --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.  Please run 'monkeysphere-host import-key' first."
142 }
143
144 # output the index of a user ID on the host key
145 # return 1 if user ID not found
146 find_host_userid() {
147     local userID="$1"
148     local tmpuidMatch
149     local line
150
151     # match to only ultimately trusted user IDs
152     tmpuidMatch="u:$(echo $userID | gpg_escape)"
153
154     # find the index of the requsted user ID
155     # NOTE: this is based on circumstantial evidence that the order of
156     # this output is the appropriate index
157     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
158         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
159
160     if [ "$line" ] ; then
161         echo ${line%%:*}
162         return 0
163     else
164         return 1
165     fi
166 }
167
168 # show info about the host key
169 show_key() {
170     local GNUPGHOME
171
172     # tmp gpghome dir
173     export GNUPGHOME=$(mktemp -d)
174
175     # trap to remove tmp dir if break
176     trap "rm -rf $GNUPGHOME" EXIT
177
178     gpg --quiet --import <"$HOST_KEY_FILE"
179
180     HOST_FINGERPRINT=$(gpg --quiet --list-keys --with-colons --with-fingerprint \
181         | grep '^fpr:' | cut -d: -f10 )
182
183     # list the host key info
184     gpg --list-key --fingerprint --list-options show-unusable-uids 2>/dev/null
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 # unset variables that should be defined only in config file
205 unset KEYSERVER
206 unset MONKEYSPHERE_USER
207
208 # load configuration file
209 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$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:="INFO"}}
214 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
215 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
216 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
217 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
218
219 # other variables
220 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
221 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
222
223 # export variables needed in su invocation
224 export DATE
225 export MODE
226 export LOG_LEVEL
227 export MONKEYSPHERE_USER
228 export KEYSERVER
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     'version'|'v')
299         echo "$VERSION"
300         ;;
301
302     '--help'|'help'|'-h'|'h'|'?')
303         usage
304         ;;
305
306     *)
307         failure "Unknown command: '$COMMAND'
308 Type '$PGRM help' for usage."
309         ;;
310 esac
311
312 exit "$RETURN"