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