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