fix usage for import/gen-key, and remove revoker option from gen-key
[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@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #
10 # They are Copyright 2008, and are all released under the GPL, version 3
11 # or later.
12
13 ########################################################################
14 PGRM=$(basename $0)
15
16 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
17 export SYSSHAREDIR
18 . "${SYSSHAREDIR}/common" || exit 1
19
20 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere/host"}
21 export SYSDATADIR
22
23 # UTC date in ISO 8601 format if needed
24 DATE=$(date -u '+%FT%T')
25
26 # unset some environment variables that could screw things up
27 unset GREP_OPTIONS
28
29 # default return code
30 RETURN=0
31
32 ########################################################################
33 # FUNCTIONS
34 ########################################################################
35
36 usage() {
37     cat <<EOF >&2
38 usage: $PGRM <subcommand> [options] [args]
39 Monkeysphere host admin tool.
40
41 subcommands:
42  show-key (s)                        output all host key information
43  extend-key (e) EXPIRE               extend host key expiration
44  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
45  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
46  add-revoker (o) FINGERPRINT         add a revoker to the host key
47  revoke-key (r)                      revoke host key
48  publish-key (p)                     publish server host key to keyserver
49
50  expert
51   import-key (i) [NAME[:PORT]]       import existing ssh key to gpg
52    --keyfile (-f) FILE                 key file to import
53    --expire (-e) EXPIRE                date to expire
54   gen-key (g) [NAME[:PORT]]          generate gpg key for the host
55    --length (-l) BITS                  key length in bits (2048)
56    --expire (-e) EXPIRE                date to expire
57   diagnostics (d)                    monkeysphere host status
58
59  version (v)                         show version number
60  help (h,?)                          this help
61
62 EOF
63 }
64
65 # function to run command as monkeysphere user
66 su_monkeysphere_user() {
67     # if the current user is the monkeysphere user, then just eval
68     # command
69     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
70         eval "$@"
71
72     # otherwise su command as monkeysphere user
73     else
74         su "$MONKEYSPHERE_USER" -c "$@"
75     fi
76 }
77
78 # function to interact with the host gnupg keyring
79 gpg_host() {
80     local returnCode
81
82     GNUPGHOME="$GNUPGHOME_HOST"
83     export GNUPGHOME
84
85     # NOTE: we supress this warning because we need the monkeysphere
86     # user to be able to read the host pubring.  we realize this might
87     # be problematic, but it's the simplest solution, without too much
88     # loss of security.
89     gpg --no-permission-warning "$@"
90     returnCode="$?"
91
92     # always reset the permissions on the host pubring so that the
93     # monkeysphere user can read the trust signatures
94     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
95     chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
96     
97     return "$returnCode"
98 }
99
100 # check if user is root
101 is_root() {
102     [ $(id -u 2>/dev/null) = '0' ]
103 }
104
105 # check that user is root, for functions that require root access
106 check_user() {
107     is_root || failure "You must be root to run this command."
108 }
109
110 # output just key fingerprint
111 fingerprint_server_key() {
112     # set the pipefail option so functions fails if can't read sec key
113     set -o pipefail
114
115     gpg_host --list-secret-keys --fingerprint \
116         --with-colons --fixed-list-mode 2> /dev/null | \
117         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
118 }
119
120 # function to check for host secret key
121 check_host_keyring() {
122     fingerprint_server_key >/dev/null \
123         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-server gen-key' first."
124 }
125
126 # show info about the host key
127 show_key() {
128     local fingerprintPGP
129     local fingerprintSSH
130     local ret=0
131
132     # FIXME: you shouldn't have to be root to see the host key fingerprint
133     if is_root ; then
134         check_host_keyring
135         fingerprintPGP=$(fingerprint_server_key)
136         gpg_authentication "--fingerprint --list-key --list-options show-unusable-uids $fingerprintPGP" 2>/dev/null
137         echo "OpenPGP fingerprint: $fingerprintPGP"
138     else
139         log info "You must be root to see host OpenPGP fingerprint."
140         ret='1'
141     fi
142
143     if [ -f "${SYSDATADIR}/ssh_host_rsa_key.pub" ] ; then
144         fingerprintSSH=$(ssh-keygen -l -f "${SYSDATADIR}/ssh_host_rsa_key.pub" | \
145             awk '{ print $1, $2, $4 }')
146         echo "ssh fingerprint: $fingerprintSSH"
147     else
148         log info "SSH host key not found."
149         ret='1'
150     fi
151
152 return $ret
153 }
154
155 ########################################################################
156 # MAIN
157 ########################################################################
158
159 # unset variables that should be defined only in config file
160 unset KEYSERVER
161 unset AUTHORIZED_USER_IDS
162 unset RAW_AUTHORIZED_KEYS
163 unset MONKEYSPHERE_USER
164
165 # load configuration file
166 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
167
168 # set empty config variable with ones from the environment, or with
169 # defaults
170 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
171 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
172 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
173 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
174 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
175
176 # other variables
177 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
178 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
179 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
180 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
181
182 # export variables needed in su invocation
183 export DATE
184 export MODE
185 export MONKEYSPHERE_USER
186 export LOG_LEVEL
187 export KEYSERVER
188 export CHECK_KEYSERVER
189 export REQUIRED_USER_KEY_CAPABILITY
190 export GNUPGHOME_HOST
191 export GNUPGHOME_AUTHENTICATION
192 export GNUPGHOME
193
194 # get subcommand
195 COMMAND="$1"
196 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
197 shift
198
199 case $COMMAND in
200     'show-key'|'show'|'s')
201         show_server_key
202         ;;
203
204     'extend-key'|'e')
205         check_user
206         check_host_keyring
207         extend_key "$@"
208         ;;
209
210     'add-hostname'|'add-name'|'n+')
211         check_user
212         check_host_keyring
213         add_hostname "$@"
214         ;;
215
216     'revoke-hostname'|'revoke-name'|'n-')
217         check_user
218         check_host_keyring
219         revoke_hostname "$@"
220         ;;
221
222     'add-revoker'|'o')
223         check_user
224         check_host_keyring
225         add_revoker "$@"
226         ;;
227
228     'revoke-key'|'r')
229         check_user
230         check_host_keyring
231         revoke_key "$@"
232         ;;
233
234     'publish-key'|'publish'|'p')
235         check_user
236         check_host_keyring
237         publish_server_key
238         ;;
239
240     'expert'|'e')
241         check_user
242         SUBCOMMAND="$1"
243         shift
244         case "$SUBCOMMAND" in
245             'import-key'|'i')
246                 import_key "$@"
247                 ;;
248
249             'gen-key'|'g')
250                 gen_key "$@"
251                 ;;
252
253             'diagnostics'|'d')
254                 diagnostics
255                 ;;
256
257             *)
258                 failure "Unknown expert subcommand: '$COMMAND'
259 Type '$PGRM help' for usage."
260                 ;;
261         esac
262         ;;
263
264     'version'|'v')
265         echo "$VERSION"
266         ;;
267
268     '--help'|'help'|'-h'|'h'|'?')
269         usage
270         ;;
271
272     *)
273         failure "Unknown command: '$COMMAND'
274 Type '$PGRM help' for usage."
275         ;;
276 esac
277
278 exit "$RETURN"