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