fix reference to MONKEYSPHER_USER in ma/setup
[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_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
37 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
38
39 # UTC date in ISO 8601 format if needed
40 DATE=$(date -u '+%FT%T')
41
42 # unset some environment variables that could screw things up
43 unset GREP_OPTIONS
44
45 # default return code
46 RETURN=0
47
48 ########################################################################
49 # FUNCTIONS
50 ########################################################################
51
52 usage() {
53     cat <<EOF >&2
54 usage: $PGRM <subcommand> [options] [args]
55 Monkeysphere host admin tool.
56
57 subcommands:
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  expert <expert-subcommand>          run expert command
67  expert help                         expert command help
68
69  version (v)                         show version number
70  help (h,?)                          this help
71
72 EOF
73 }
74
75 # function to interact with the gpg keyring
76 gpg_host() {
77     GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
78 }
79
80 # command to list the info about the host key, in colon format
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 --quiet --command-fd 0 --edit-key \
91         "0x${HOST_FINGERPRINT}!" "$@"
92 }
93
94 # export the host key to stdout
95 gpg_host_export() {
96     gpg_host --export --armor --export-options export-minimal \
97         "0x${HOST_FINGERPRINT}!"
98 }
99
100 # export the host secret key to the monkeysphere ssh sec key file
101 # NOTE: assumes that the primary key is the proper key to use
102 create_ssh_sec_file() {
103     log debug "creating ssh secret key file..."
104     (umask 077 && \
105         gpg_host --export-secret-key "$HOST_FINGERPRINT" | \
106         openpgp2ssh "$HOST_FINGERPRINT" > "${MHDATADIR}/ssh_host_rsa_key")
107     log info "SSH host secret key file: ${MHDATADIR}/ssh_host_rsa_key"
108 }
109
110 # export the host public key to the monkeysphere ssh pub key file
111 create_ssh_pub_file() {
112     log debug "creating ssh public key file..."
113     ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "$HOST_KEY_PUB"
114     log info "SSH host public key file: $HOST_KEY_PUB"
115 }
116
117 # export the host public key to the monkeysphere gpg pub key file
118 create_gpg_pub_file() {
119     log debug "creating openpgp public key file..."
120     gpg_host_export > "$HOST_KEY_PUB_GPG"
121     log info "GPG host public key file: $HOST_KEY_PUB_GPG"
122 }
123
124 # load the host fingerprint into the fingerprint variable, using the
125 # export gpg pub key file
126 # FIXME: this seems much less than ideal, with all this temp keyring
127 # stuff.  is there a way we can do this without having to create temp
128 # files?
129 load_fingerprint() {
130     if [ -f "$HOST_KEY_PUB_GPG" ] ; then
131         HOST_FINGERPRINT=$( \
132             (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
133             && gpg --quiet --import \
134             && gpg --quiet --list-keys --with-colons --with-fingerprint \
135             && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
136             | grep '^fpr:' | cut -d: -f10 )
137     else
138         HOST_FINGERPRINT=
139     fi
140 }
141
142 # load the host fingerprint into the fingerprint variable, using the
143 # gpg host secret key
144 load_fingerprint_secret() {
145     HOST_FINGERPRINT=$( \
146         gpg_host --quiet --list-secret-key \
147         --with-colons --with-fingerprint \
148         | grep '^fpr:' | cut -d: -f10 )
149 }
150
151 # output host key ssh fingerprint
152 load_ssh_fingerprint() {
153     [ -f "$HOST_KEY_PUB" ] || return 0
154     HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
155         | awk '{ print $1, $2, $4 }')
156 }
157
158 # fail if host key present
159 check_host_key() {
160     [ -z "$HOST_FINGERPRINT" ] \
161         || failure "An OpenPGP host key already exists."
162 }
163
164 # fail if host key not present
165 check_host_no_key() {
166     [ "$HOST_FINGERPRINT" ] \
167         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-host expert import-key' first."
168 }
169
170 # output the index of a user ID on the host key
171 # return 1 if user ID not found
172 find_host_userid() {
173     local userID="$1"
174     local tmpuidMatch
175     local line
176
177     # match to only ultimately trusted user IDs
178     tmpuidMatch="u:$(echo $userID | gpg_escape)"
179
180     # find the index of the requsted user ID
181     # NOTE: this is based on circumstantial evidence that the order of
182     # this output is the appropriate index
183     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
184         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
185
186     if [ "$line" ] ; then
187         echo ${line%%:*}
188         return 0
189     else
190         return 1
191     fi
192 }
193
194 # show info about the host key
195 show_key() {
196     gpg_host --fingerprint --list-key --list-options show-unusable-uids \
197         "0x${HOST_FINGERPRINT}!" 2>/dev/null
198     # FIXME: make sure expiration date is shown
199
200     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
201
202     load_ssh_fingerprint
203
204     if [ "$HOST_FINGERPRINT_SSH" ] ; then
205         echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
206     else
207         log error "SSH host key not found."
208     fi
209
210     # FIXME: other relevant key parameters?
211 }
212
213 ########################################################################
214 # MAIN
215 ########################################################################
216
217 # unset variables that should be defined only in config file
218 unset KEYSERVER
219 unset MONKEYSPHERE_USER
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 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
229 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
230 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
231
232 # other variables
233 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
234 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
235
236 # export variables needed in su invocation
237 export DATE
238 export MODE
239 export LOG_LEVEL
240 export MONKEYSPHERE_USER
241 export KEYSERVER
242 export GNUPGHOME_HOST
243 export GNUPGHOME
244 export HOST_FINGERPRINT=
245 export HOST_FINGERPRINT_SSH=
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         load_fingerprint
255         check_host_no_key
256         show_key
257         ;;
258
259     'set-expire'|'extend-key'|'e')
260         load_fingerprint
261         check_host_no_key
262         source "${MHSHAREDIR}/set_expire"
263         set_expire "$@"
264         ;;
265
266     'add-hostname'|'add-name'|'n+')
267         load_fingerprint
268         check_host_no_key
269         source "${MHSHAREDIR}/add_hostname"
270         add_hostname "$@"
271         ;;
272
273     'revoke-hostname'|'revoke-name'|'n-')
274         load_fingerprint
275         check_host_no_key
276         source "${MHSHAREDIR}/revoke_hostname"
277         revoke_hostname "$@"
278         ;;
279
280     'add-revoker'|'o')
281         load_fingerprint
282         check_host_no_key
283         source "${MHSHAREDIR}/add_revoker"
284         add_revoker "$@"
285         ;;
286
287     'revoke-key'|'r')
288         load_fingerprint
289         check_host_no_key
290         source "${MHSHAREDIR}/revoke_key"
291         revoke_key "$@"
292         ;;
293
294     'publish-key'|'publish'|'p')
295         load_fingerprint
296         check_host_no_key
297         source "${MHSHAREDIR}/publish_key"
298         publish_key
299         ;;
300
301     'expert')
302         SUBCOMMAND="$1"
303         shift
304         case "$SUBCOMMAND" in
305             'help'|'h'|'?')
306                 cat <<EOF
307 usage: $PGRM expert <subcommand> [options] [args]
308
309 expert subcommands:
310  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
311  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
312    --length (-l) BITS                  key length in bits (2048)
313  diagnostics (d)                     monkeysphere host status
314
315 EOF
316                 ;;
317
318             'import-key'|'i')
319                 load_fingerprint
320                 check_host_key
321                 source "${MHSHAREDIR}/import_key"
322                 import_key "$@"
323                 ;;
324
325             'gen-key'|'g')
326                 load_fingerprint
327                 check_host_key
328                 source "${MHSHAREDIR}/gen_key"
329                 gen_key "$@"
330                 ;;
331
332             'diagnostics'|'d')
333                 source "${MHSHAREDIR}/diagnostics"
334                 diagnostics
335                 ;;
336
337             *)
338                 failure "Unknown expert subcommand: '$COMMAND'
339 Type '$PGRM help' for usage."
340                 ;;
341         esac
342         ;;
343
344     'version'|'v')
345         echo "$VERSION"
346         ;;
347
348     '--help'|'help'|'-h'|'h'|'?')
349         usage
350         ;;
351
352     *)
353         failure "Unknown command: '$COMMAND'
354 Type '$PGRM help' for usage."
355         ;;
356 esac
357
358 exit "$RETURN"