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