rename function to get the host fingerprint, and fix some
[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 # FIXME: should not have to be priviledged user to get host
106 # fingerprint.  should be taken from publicly accessible key files,
107 # instead of the keyring.
108 get_host_fingerprint() {
109     gpg_host --list-secret-keys --fingerprint \
110         --with-colons --fixed-list-mode 2> /dev/null | \
111         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null || true
112 }
113
114 # output the index of a user ID on the host key
115 # return 1 if user ID not found
116 find_host_userid() {
117     local userID="$1"
118     local tmpuidMatch
119     local line
120
121     # match to only ultimately trusted user IDs
122     tmpuidMatch="u:$(echo $userID | gpg_escape)"
123
124     # find the index of the requsted user ID
125     # NOTE: this is based on circumstantial evidence that the order of
126     # this output is the appropriate index
127     line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
128         grep -n -x -F "$tmpuidMatch" 2>/dev/null)
129
130     if [ "$line" ] ; then
131         echo ${line%%:*}
132         return 0
133     else
134         return 1
135     fi
136 }
137
138 # function to check for host secret key
139 check_host_fail() {
140     [ "$HOST_FINGERPRINT" ] || \
141         failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-host expert import-key' first."
142 }
143
144 # show info about the host key
145 show_key() {
146     local fingerprintSSH
147
148     gpg_host --fingerprint --list-key --list-options show-unusable-uids \
149         "0x${HOST_FINGERPRINT}!" 2>/dev/null
150     # FIXME: make sure expiration date is shown
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: other relevant key parameters?
164 }
165
166 ########################################################################
167 # MAIN
168 ########################################################################
169
170 # unset variables that should be defined only in config file
171 unset KEYSERVER
172 unset MONKEYSPHERE_USER
173
174 # load configuration file
175 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
176
177 # set empty config variable with ones from the environment, or with
178 # defaults
179 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
180 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
181 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
182 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
183 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
184
185 # other variables
186 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
187 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
188
189 # host key fingerprint
190 HOST_FINGERPRINT=$(get_host_fingerprint)
191
192 # host pub key files
193 HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
194 HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
195
196 # export variables needed in su invocation
197 export DATE
198 export MODE
199 export LOG_LEVEL
200 export MONKEYSPHERE_USER
201 export KEYSERVER
202 export GNUPGHOME_HOST
203 export GNUPGHOME
204 export HOST_FINGERPRINT
205
206 # get subcommand
207 COMMAND="$1"
208 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
209 shift
210
211
212 case $COMMAND in
213     'show-key'|'show'|'s')
214         check_host_fail
215         show_key
216         ;;
217
218     'set-expire'|'extend-key'|'e')
219         check_host_fail
220         source "${MHSHAREDIR}/set_expire"
221         set_expire "$@"
222         ;;
223
224     'add-hostname'|'add-name'|'n+')
225         check_host_fail
226         source "${MHSHAREDIR}/add_hostname"
227         add_hostname "$@"
228         ;;
229
230     'revoke-hostname'|'revoke-name'|'n-')
231         check_host_fail
232         source "${MHSHAREDIR}/revoke_hostname"
233         revoke_hostname "$@"
234         ;;
235
236     'add-revoker'|'o')
237         check_host_fail
238         source "${MHSHAREDIR}/add_revoker"
239         add_revoker "$@"
240         ;;
241
242     'revoke-key'|'r')
243         check_host_fail
244         source "${MHSHAREDIR}/revoke_key"
245         revoke_key "$@"
246         ;;
247
248     'publish-key'|'publish'|'p')
249         check_host_fail
250         source "${MHSHAREDIR}/publish_key"
251         publish_key
252         ;;
253
254     'expert')
255         SUBCOMMAND="$1"
256         shift
257         case "$SUBCOMMAND" in
258             'help'|'h'|'?')
259                 cat <<EOF
260 usage: $PGRM expert <subcommand> [options] [args]
261
262 expert subcommands:
263  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
264  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
265    --length (-l) BITS                  key length in bits (2048)
266  diagnostics (d)                     monkeysphere host status
267
268 EOF
269                 ;;
270
271             'import-key'|'i')
272                 source "${MHSHAREDIR}/import_key"
273                 import_key "$@"
274                 ;;
275
276             'gen-key'|'g')
277                 source "${MHSHAREDIR}/gen_key"
278                 gen_key "$@"
279                 ;;
280
281             'diagnostics'|'d')
282                 source "${MHSHAREDIR}/diagnostics"
283                 diagnostics
284                 ;;
285
286             *)
287                 failure "Unknown expert subcommand: '$COMMAND'
288 Type '$PGRM help' for usage."
289                 ;;
290         esac
291         ;;
292
293     'version'|'v')
294         echo "$VERSION"
295         ;;
296
297     '--help'|'help'|'-h'|'h'|'?')
298         usage
299         ;;
300
301     *)
302         failure "Unknown command: '$COMMAND'
303 Type '$PGRM help' for usage."
304         ;;
305 esac
306
307 exit "$RETURN"