merging jrollins and micah work, reverting ui changes for m-h gen-key and import-key
[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)  NAME[:PORT]        import existing ssh key to gpg
57    --keyfile (-f) FILE                 key file to import
58    --expire (-e) EXPIRE                date to expire
59   gen-key (g) NAME[:PORT]            generate gpg key for the host
60    --length (-l) BITS                  key length in bits (2048)
61    --expire (-e) EXPIRE                date to expire
62    --revoker (-r) FINGERPRINT          add a revoker
63   diagnostics (d)                    monkeysphere host status
64
65  version (v)                         show version number
66  help (h,?)                          this help
67
68 EOF
69 }
70
71 # function to run command as monkeysphere user
72 su_monkeysphere_user() {
73     # if the current user is the monkeysphere user, then just eval
74     # command
75     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
76         eval "$@"
77
78     # otherwise su command as monkeysphere user
79     else
80         su "$MONKEYSPHERE_USER" -c "$@"
81     fi
82 }
83
84 # function to interact with the host gnupg keyring
85 gpg_host() {
86     local returnCode
87
88     GNUPGHOME="$GNUPGHOME_HOST"
89     export GNUPGHOME
90
91     # NOTE: we supress this warning because we need the monkeysphere
92     # user to be able to read the host pubring.  we realize this might
93     # be problematic, but it's the simplest solution, without too much
94     # loss of security.
95     gpg --no-permission-warning "$@"
96     returnCode="$?"
97
98     # always reset the permissions on the host pubring so that the
99     # monkeysphere user can read the trust signatures
100     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
101     chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
102     
103     return "$returnCode"
104 }
105
106 # output just key fingerprint
107 fingerprint_server_key() {
108     # set the pipefail option so functions fails if can't read sec key
109     set -o pipefail
110
111     gpg_host --list-secret-keys --fingerprint \
112         --with-colons --fixed-list-mode 2> /dev/null | \
113         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
114 }
115
116 # function to check for host secret key
117 check_host_keyring() {
118     fingerprint_server_key >/dev/null \
119         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-server gen-key' first."
120 }
121
122 ########################################################################
123 # MAIN
124 ########################################################################
125
126 # unset variables that should be defined only in config file
127 unset KEYSERVER
128 unset AUTHORIZED_USER_IDS
129 unset RAW_AUTHORIZED_KEYS
130 unset MONKEYSPHERE_USER
131
132 # load configuration file
133 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
134
135 # set empty config variable with ones from the environment, or with
136 # defaults
137 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
138 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
139 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
140 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
141 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
142
143 # other variables
144 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
145 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
146 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
147 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
148
149 # export variables needed in su invocation
150 export DATE
151 export MODE
152 export MONKEYSPHERE_USER
153 export LOG_LEVEL
154 export KEYSERVER
155 export CHECK_KEYSERVER
156 export REQUIRED_USER_KEY_CAPABILITY
157 export GNUPGHOME_HOST
158 export GNUPGHOME_AUTHENTICATION
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         show_server_key
169         ;;
170
171     'extend-key'|'e')
172         check_host_keyring
173         extend_key "$@"
174         ;;
175
176     'add-hostname'|'add-name'|'n+')
177         check_host_keyring
178         add_hostname "$@"
179         ;;
180
181     'revoke-hostname'|'revoke-name'|'n-')
182         check_host_keyring
183         revoke_hostname "$@"
184         ;;
185
186     'add-revoker'|'o')
187         check_host_keyring
188         add_revoker "$@"
189         ;;
190
191     'revoke-key'|'r')
192         check_host_keyring
193         revoke_key "$@"
194         ;;
195
196     'publish-key'|'publish'|'p')
197         check_host_keyring
198         publish_server_key
199         ;;
200
201     'expert'|'e')
202         check_user
203         SUBCOMMAND="$1"
204         shift
205         case "$SUBCOMMAND" in
206             'import-key'|'i')
207                 import_key "$@"
208                 ;;
209
210             'gen-key'|'g')
211                 gen_key "$@"
212                 ;;
213
214             'diagnostics'|'d')
215                 diagnostics
216                 ;;
217
218             *)
219                 failure "Unknown expert subcommand: '$COMMAND'
220 Type '$PGRM help' for usage."
221                 ;;
222         esac
223         ;;
224
225     'version'|'v')
226         echo "$VERSION"
227         ;;
228
229     '--help'|'help'|'-h'|'h'|'?')
230         usage
231         ;;
232
233     *)
234         failure "Unknown command: '$COMMAND'
235 Type '$PGRM help' for usage."
236         ;;
237 esac
238
239 exit "$RETURN"