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