some more fixes and tweaks to get things working in the new setup
[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     local ret=0
111
112     # FIXME: you shouldn't have to be root to see the host key fingerprint
113     if is_root ; then
114         check_host_keyring
115         fingerprintPGP=$(fingerprint_server_key)
116         gpg_authentication "--fingerprint --list-key --list-options show-unusable-uids $fingerprintPGP" 2>/dev/null
117         echo "OpenPGP fingerprint: $fingerprintPGP"
118     else
119         log info "You must be root to see host OpenPGP fingerprint."
120         ret='1'
121     fi
122
123     if [ -f "${SYSDATADIR}/ssh_host_rsa_key.pub" ] ; then
124         fingerprintSSH=$(ssh-keygen -l -f "${SYSDATADIR}/ssh_host_rsa_key.pub" | \
125             awk '{ print $1, $2, $4 }')
126         echo "ssh fingerprint: $fingerprintSSH"
127     else
128         log info "SSH host key not found."
129         ret='1'
130     fi
131
132 return $ret
133 }
134
135 ########################################################################
136 # MAIN
137 ########################################################################
138
139 # unset variables that should be defined only in config file
140 unset KEYSERVER
141 unset MONKEYSPHERE_USER
142
143 # load configuration file
144 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
145
146 # set empty config variable with ones from the environment, or with
147 # defaults
148 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
149 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
150 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
151 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
152 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
153
154 # other variables
155 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
156 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/host"}
157
158 # export variables needed in su invocation
159 export DATE
160 export MODE
161 export LOG_LEVEL
162 export MONKEYSPHERE_USER
163 export KEYSERVER
164 export GNUPGHOME_HOST
165 export GNUPGHOME
166
167 # get subcommand
168 COMMAND="$1"
169 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
170 shift
171
172 case $COMMAND in
173     'show-key'|'show'|'s')
174         check_host_keyring
175         show_key
176         ;;
177
178     'extend-key'|'e')
179         check_host_keyring
180         source "${MHSHAREDIR}/extend_key"
181         extend_key "$@"
182         ;;
183
184     'add-hostname'|'add-name'|'n+')
185         check_host_keyring
186         source "${MHSHAREDIR}/add_hostname"
187         add_hostname "$@"
188         ;;
189
190     'revoke-hostname'|'revoke-name'|'n-')
191         check_host_keyring
192         source "${MHSHAREDIR}/revoke_hostname"
193         revoke_hostname "$@"
194         ;;
195
196     'add-revoker'|'o')
197         check_host_keyring
198         source "${MHSHAREDIR}/add_revoker"
199         add_revoker "$@"
200         ;;
201
202     'revoke-key'|'r')
203         check_host_keyring
204         source "${MHSHAREDIR}/revoke_key"
205         revoke_key "$@"
206         ;;
207
208     'publish-key'|'publish'|'p')
209         check_host_keyring
210         source "${MHSHAREDIR}/publish_key"
211         publish_key
212         ;;
213
214     'expert'|'e')
215         SUBCOMMAND="$1"
216         shift
217         case "$SUBCOMMAND" in
218             'help'|'h'|'?')
219                 cat <<EOF
220 usage: $PGRM expert <subcommand> [options] [args]
221
222 expert subcommands:
223  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
224    --keyfile (-f) FILE                 key file to import
225    --expire (-e) EXPIRE                date to expire
226  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
227    --length (-l) BITS                  key length in bits (2048)
228    --expire (-e) EXPIRE                date to expire
229  diagnostics (d)                     monkeysphere host status
230
231 EOF
232                 ;;
233
234             'import-key'|'i')
235                 source "${MHSHAREDIR}/import_key"
236                 import_key "$@"
237                 ;;
238
239             'gen-key'|'g')
240                 source "${MHSHAREDIR}/gen_key"
241                 gen_key "$@"
242                 ;;
243
244             'diagnostics'|'d')
245                 source "${MHSHAREDIR}/diagnostics"
246                 diagnostics
247                 ;;
248
249             *)
250                 failure "Unknown expert subcommand: '$COMMAND'
251 Type '$PGRM help' for usage."
252                 ;;
253         esac
254         ;;
255
256     'version'|'v')
257         echo "$VERSION"
258         ;;
259
260     '--help'|'help'|'-h'|'h'|'?')
261         usage
262         ;;
263
264     *)
265         failure "Unknown command: '$COMMAND'
266 Type '$PGRM help' for usage."
267         ;;
268 esac
269
270 exit "$RETURN"