merging from jrollins
[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 # output just key fingerprint
101 fingerprint_server_key() {
102     # set the pipefail option so functions fails if can't read sec key
103     set -o pipefail
104
105     gpg_host --list-secret-keys --fingerprint \
106         --with-colons --fixed-list-mode 2> /dev/null | \
107         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
108 }
109
110 # function to check for host secret key
111 check_host_keyring() {
112     fingerprint_server_key >/dev/null \
113         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-server gen-key' first."
114 }
115
116 # show info about the host key
117 show_key() {
118     local fingerprintPGP
119     local fingerprintSSH
120     local ret=0
121
122     # FIXME: you shouldn't have to be root to see the host key fingerprint
123     if is_root ; then
124         check_host_keyring
125         fingerprintPGP=$(fingerprint_server_key)
126         gpg_authentication "--fingerprint --list-key --list-options show-unusable-uids $fingerprintPGP" 2>/dev/null
127         echo "OpenPGP fingerprint: $fingerprintPGP"
128     else
129         log info "You must be root to see host OpenPGP fingerprint."
130         ret='1'
131     fi
132
133     if [ -f "${SYSDATADIR}/ssh_host_rsa_key.pub" ] ; then
134         fingerprintSSH=$(ssh-keygen -l -f "${SYSDATADIR}/ssh_host_rsa_key.pub" | \
135             awk '{ print $1, $2, $4 }')
136         echo "ssh fingerprint: $fingerprintSSH"
137     else
138         log info "SSH host key not found."
139         ret='1'
140     fi
141
142 return $ret
143 }
144
145 ########################################################################
146 # MAIN
147 ########################################################################
148
149 # unset variables that should be defined only in config file
150 unset KEYSERVER
151 unset AUTHORIZED_USER_IDS
152 unset RAW_AUTHORIZED_KEYS
153 unset MONKEYSPHERE_USER
154
155 # load configuration file
156 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
157
158 # set empty config variable with ones from the environment, or with
159 # defaults
160 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
161 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
162 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
163 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
164 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
165
166 # other variables
167 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
168 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
169 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
170 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
171
172 # export variables needed in su invocation
173 export DATE
174 export MODE
175 export MONKEYSPHERE_USER
176 export LOG_LEVEL
177 export KEYSERVER
178 export CHECK_KEYSERVER
179 export REQUIRED_USER_KEY_CAPABILITY
180 export GNUPGHOME_HOST
181 export GNUPGHOME_AUTHENTICATION
182 export GNUPGHOME
183
184 # get subcommand
185 COMMAND="$1"
186 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
187 shift
188
189 case $COMMAND in
190     'show-key'|'show'|'s')
191         show_server_key
192         ;;
193
194     'extend-key'|'e')
195         check_host_keyring
196         extend_key "$@"
197         ;;
198
199     'add-hostname'|'add-name'|'n+')
200         check_host_keyring
201         add_hostname "$@"
202         ;;
203
204     'revoke-hostname'|'revoke-name'|'n-')
205         check_host_keyring
206         revoke_hostname "$@"
207         ;;
208
209     'add-revoker'|'o')
210         check_host_keyring
211         add_revoker "$@"
212         ;;
213
214     'revoke-key'|'r')
215         check_host_keyring
216         revoke_key "$@"
217         ;;
218
219     'publish-key'|'publish'|'p')
220         check_host_keyring
221         publish_server_key
222         ;;
223
224     'expert'|'e')
225         check_user
226         SUBCOMMAND="$1"
227         shift
228         case "$SUBCOMMAND" in
229             'import-key'|'i')
230                 import_key "$@"
231                 ;;
232
233             'gen-key'|'g')
234                 gen_key "$@"
235                 ;;
236
237             'diagnostics'|'d')
238                 diagnostics
239                 ;;
240
241             *)
242                 failure "Unknown expert subcommand: '$COMMAND'
243 Type '$PGRM help' for usage."
244                 ;;
245         esac
246         ;;
247
248     'version'|'v')
249         echo "$VERSION"
250         ;;
251
252     '--help'|'help'|'-h'|'h'|'?')
253         usage
254         ;;
255
256     *)
257         failure "Unknown command: '$COMMAND'
258 Type '$PGRM help' for usage."
259         ;;
260 esac
261
262 exit "$RETURN"