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