Break out host export commands into gpg_host_export and
[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 PGRM=$(basename $0)
18
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
20 export SYSSHAREDIR
21 . "${SYSSHAREDIR}/common" || exit 1
22
23 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
24 export SYSDATADIR
25
26 # sharedir for host functions
27 MHSHAREDIR="${SYSSHAREDIR}/mh"
28
29 # datadir for host functions
30 MHDATADIR="${SYSDATADIR}/host"
31
32 # UTC date in ISO 8601 format if needed
33 DATE=$(date -u '+%FT%T')
34
35 # unset some environment variables that could screw things up
36 unset GREP_OPTIONS
37
38 # default return code
39 RETURN=0
40
41 ########################################################################
42 # FUNCTIONS
43 ########################################################################
44
45 usage() {
46     cat <<EOF >&2
47 usage: $PGRM <subcommand> [options] [args]
48 Monkeysphere host admin tool.
49
50 subcommands:
51  show-key (s)                        output all host key information
52  set-expire (e) EXPIRE               set host key expiration
53  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
54  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
55  add-revoker (o) FINGERPRINT         add a revoker to the host key
56  revoke-key (r)                      revoke host key
57  publish-key (p)                     publish host key to keyserver
58
59  expert <expert-subcommand>          run expert command
60  expert help                         expert command help
61
62  version (v)                         show version number
63  help (h,?)                          this help
64
65 EOF
66 }
67
68 # function to run command as monkeysphere user
69 su_monkeysphere_user() {
70     # if the current user is the monkeysphere user, then just eval
71     # command
72     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
73         eval "$@"
74
75     # otherwise su command as monkeysphere user
76     else
77         su "$MONKEYSPHERE_USER" -c "$@"
78     fi
79 }
80
81 # function to interact with the gpg keyring
82 gpg_host() {
83     GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
84 }
85
86 # command to list the info about the host key, in colon format
87 gpg_host_list() {
88     gpg_host --list-keys --with-colons --fixed-list-mode \
89         --with-fingerprint --with-fingerprint \
90         "0x${HOST_FINGERPRINT}!"
91
92 }
93
94 # command for edit key scripts, takes scripts on stdin
95 gpg_host_edit() {
96     gpg_host --quiet --command-fd 0 --edit-key \
97         "0x${HOST_FINGERPRINT}!" "$@"
98 }
99
100 # export the host key to stdout
101 gpg_host_export() {
102     gpg_host --export --armor --export-options export-minimal \
103         "0x${HOST_FINGERPRINT}!"
104 }
105
106 # export the host key to the monkeysphere host file key
107 gpg_host_export_to_ssh_file() {
108     log debug "exporting openpgp public key..."
109     gpg_host_export > "${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
110     log info "SSH host public key in OpenPGP form: ${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
111 }
112
113 # output just key fingerprint
114 fingerprint_host_key() {
115     # set the pipefail option so functions fails if can't read sec key
116     set -o pipefail
117
118     gpg_host --list-secret-keys --fingerprint \
119         --with-colons --fixed-list-mode 2> /dev/null | \
120         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
121 }
122
123 # output the index of a user ID on the host key
124 # return 1 if user ID not found
125 find_host_userid() {
126     local userID="$1"
127     local tmpuidMatch
128     local line
129
130     # match to only ultimately trusted user IDs
131     tmpuidMatch="u:$(echo $userID | gpg_escape)"
132
133     # find the index of the requsted user ID
134     # NOTE: this is based on circumstantial evidence that the order of
135     # this output is the appropriate index
136     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
137         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
138
139     if [ "$line" ] ; then
140         echo ${line%%:*}
141         return 0
142     else
143         return 1
144     fi
145 }
146
147 # function to check for host secret key
148 check_host_fail() {
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 # show info about the host key
154 show_key() {
155     local fingerprintSSH
156
157     # FIXME: should not have to be priviledged user to see this info.
158     # should be taken from publicly accessible key files, instead of
159     # the keyring.
160
161     gpg_host --fingerprint --list-key --list-options show-unusable-uids \
162         "0x${HOST_FINGERPRINT}!" 2>/dev/null
163
164     echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
165
166     if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
167         fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
168             awk '{ print $1, $2, $4 }')
169         echo "ssh fingerprint: $fingerprintSSH"
170     else
171         log info "SSH host key not found."
172     fi
173
174     # FIXME: show expiration date
175     # FIXME: other relevant key parameters?
176 }
177
178 ########################################################################
179 # MAIN
180 ########################################################################
181
182 # unset variables that should be defined only in config file
183 unset KEYSERVER
184 unset MONKEYSPHERE_USER
185
186 # load configuration file
187 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
188
189 # set empty config variable with ones from the environment, or with
190 # defaults
191 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
192 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
193 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
194 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
195 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
196
197 # other variables
198 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
199 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
200
201 # host key fingerprint
202 HOST_FINGERPRINT=$(fingerprint_host_key)
203
204 # export variables needed in su invocation
205 export DATE
206 export MODE
207 export LOG_LEVEL
208 export MONKEYSPHERE_USER
209 export KEYSERVER
210 export GNUPGHOME_HOST
211 export GNUPGHOME
212 export HOST_FINGERPRINT
213
214 # get subcommand
215 COMMAND="$1"
216 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
217 shift
218
219
220 case $COMMAND in
221     'show-key'|'show'|'s')
222         check_host_keyring
223         show_key
224         ;;
225
226     'set-expire'|'extend-key'|'e')
227         check_host_keyring
228         source "${MHSHAREDIR}/set_expire"
229         set_expire "$@"
230         ;;
231
232     'add-hostname'|'add-name'|'n+')
233         check_host_keyring
234         source "${MHSHAREDIR}/add_hostname"
235         add_hostname "$@"
236         ;;
237
238     'revoke-hostname'|'revoke-name'|'n-')
239         check_host_keyring
240         source "${MHSHAREDIR}/revoke_hostname"
241         revoke_hostname "$@"
242         ;;
243
244     'add-revoker'|'o')
245         check_host_keyring
246         source "${MHSHAREDIR}/add_revoker"
247         add_revoker "$@"
248         ;;
249
250     'revoke-key'|'r')
251         check_host_keyring
252         source "${MHSHAREDIR}/revoke_key"
253         revoke_key "$@"
254         ;;
255
256     'publish-key'|'publish'|'p')
257         check_host_keyring
258         source "${MHSHAREDIR}/publish_key"
259         publish_key
260         ;;
261
262     'expert')
263         SUBCOMMAND="$1"
264         shift
265         case "$SUBCOMMAND" in
266             'help'|'h'|'?')
267                 cat <<EOF
268 usage: $PGRM expert <subcommand> [options] [args]
269
270 expert subcommands:
271  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
272  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
273    --length (-l) BITS                  key length in bits (2048)
274  diagnostics (d)                     monkeysphere host status
275
276 EOF
277                 ;;
278
279             'import-key'|'i')
280                 source "${MHSHAREDIR}/import_key"
281                 import_key "$@"
282                 ;;
283
284             'gen-key'|'g')
285                 source "${MHSHAREDIR}/gen_key"
286                 gen_key "$@"
287                 ;;
288
289             'diagnostics'|'d')
290                 source "${MHSHAREDIR}/diagnostics"
291                 diagnostics
292                 ;;
293
294             *)
295                 failure "Unknown expert subcommand: '$COMMAND'
296 Type '$PGRM help' for usage."
297                 ;;
298         esac
299         ;;
300
301     'version'|'v')
302         echo "$VERSION"
303         ;;
304
305     '--help'|'help'|'-h'|'h'|'?')
306         usage
307         ;;
308
309     *)
310         failure "Unknown command: '$COMMAND'
311 Type '$PGRM help' for usage."
312         ;;
313 esac
314
315 exit "$RETURN"