fix up gen/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@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 set -e
16
17 PGRM=$(basename $0)
18
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
20 export SYSSHAREDIR
21 . "${SYSSHAREDIR}/common" || exit 1
22
23 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
24 export SYSDATADIR
25
26 # sharedir for host functions
27 MHSHAREDIR="${SYSSHAREDIR}/mh"
28
29 # datadir for host functions
30 MHDATADIR="${SYSDATADIR}/host"
31
32 # UTC date in ISO 8601 format if needed
33 DATE=$(date -u '+%FT%T')
34
35 # unset some environment variables that could screw things up
36 unset GREP_OPTIONS
37
38 # default return code
39 RETURN=0
40
41 ########################################################################
42 # FUNCTIONS
43 ########################################################################
44
45 usage() {
46     cat <<EOF >&2
47 usage: $PGRM <subcommand> [options] [args]
48 Monkeysphere host admin tool.
49
50 subcommands:
51  show-key (s)                        output all host key information
52  set-expire (e) EXPIRE               set host key expiration
53  add-hostname (n+) NAME[:PORT]       add hostname user ID to host key
54  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
55  add-revoker (o) FINGERPRINT         add a revoker to the host key
56  revoke-key (r)                      revoke host key
57  publish-key (p)                     publish host key to keyserver
58
59  expert <expert-subcommand>          run expert command
60  expert help                         expert command help
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_host_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_host_key >/dev/null \
108         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-host import-key' first."
109 }
110
111 # show info about the host key
112 show_key() {
113     local fingerprintPGP
114     local fingerprintSSH
115
116     # FIXME: you shouldn't have to be root to see the host key fingerprint
117     check_host_keyring
118     fingerprintPGP=$(fingerprint_host_key)
119
120     gpg_host --fingerprint --list-key --list-options show-unusable-uids "0x${fingerprintPGP}!" 2>/dev/null
121     echo "OpenPGP fingerprint: $fingerprintPGP"
122
123     if [ -f "${MHDATADIR}/ssh_host_rsa_key.pub" ] ; then
124         fingerprintSSH=$(ssh-keygen -l -f "${MHDATADIR}/ssh_host_rsa_key.pub" | \
125             awk '{ print $1, $2, $4 }')
126         echo "ssh fingerprint: $fingerprintSSH"
127     else
128         log info "SSH host key not found."
129     fi
130
131     # FIXME: show expiration date
132     # FIXME: other relevant key parameters?
133 }
134
135 ########################################################################
136 # MAIN
137 ########################################################################
138
139 # unset variables that should be defined only in config file
140 unset KEYSERVER
141 unset MONKEYSPHERE_USER
142
143 # load configuration file
144 [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
145
146 # set empty config variable with ones from the environment, or with
147 # defaults
148 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
149 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
150 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
151 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
152 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
153
154 # other variables
155 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
156 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
157
158 # export variables needed in su invocation
159 export DATE
160 export MODE
161 export LOG_LEVEL
162 export MONKEYSPHERE_USER
163 export KEYSERVER
164 export GNUPGHOME_HOST
165 export GNUPGHOME
166
167 # get subcommand
168 COMMAND="$1"
169 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
170 shift
171
172 case $COMMAND in
173     'show-key'|'show'|'s')
174         check_host_keyring
175         show_key
176         ;;
177
178     # FIXME: what should we call this command?
179     'set-expire'|'extend-key'|'e')
180         check_host_keyring
181         source "${MHSHAREDIR}/extend_key"
182         extend_key "$@"
183         ;;
184
185     'add-hostname'|'add-name'|'n+')
186         check_host_keyring
187         source "${MHSHAREDIR}/add_hostname"
188         add_hostname "$@"
189         ;;
190
191     'revoke-hostname'|'revoke-name'|'n-')
192         check_host_keyring
193         source "${MHSHAREDIR}/revoke_hostname"
194         revoke_hostname "$@"
195         ;;
196
197     'add-revoker'|'o')
198         check_host_keyring
199         source "${MHSHAREDIR}/add_revoker"
200         add_revoker "$@"
201         ;;
202
203     'revoke-key'|'r')
204         check_host_keyring
205         source "${MHSHAREDIR}/revoke_key"
206         revoke_key "$@"
207         ;;
208
209     'publish-key'|'publish'|'p')
210         check_host_keyring
211         source "${MHSHAREDIR}/publish_key"
212         publish_key
213         ;;
214
215     'expert'|'e')
216         SUBCOMMAND="$1"
217         shift
218         case "$SUBCOMMAND" in
219             'help'|'h'|'?')
220                 cat <<EOF
221 usage: $PGRM expert <subcommand> [options] [args]
222
223 expert subcommands:
224  import-key (i) [NAME[:PORT]]        import existing ssh key to gpg
225  gen-key (g) [NAME[:PORT]]           generate gpg key for the host
226    --length (-l) BITS                  key length in bits (2048)
227  diagnostics (d)                     monkeysphere host status
228
229 EOF
230                 ;;
231
232             'import-key'|'i')
233                 source "${MHSHAREDIR}/import_key"
234                 import_key "$@"
235                 ;;
236
237             'gen-key'|'g')
238                 source "${MHSHAREDIR}/gen_key"
239                 gen_key "$@"
240                 ;;
241
242             'diagnostics'|'d')
243                 source "${MHSHAREDIR}/diagnostics"
244                 diagnostics
245                 ;;
246
247             *)
248                 failure "Unknown expert subcommand: '$COMMAND'
249 Type '$PGRM help' for usage."
250                 ;;
251         esac
252         ;;
253
254     'version'|'v')
255         echo "$VERSION"
256         ;;
257
258     '--help'|'help'|'-h'|'h'|'?')
259         usage
260         ;;
261
262     *)
263         failure "Unknown command: '$COMMAND'
264 Type '$PGRM help' for usage."
265         ;;
266 esac
267
268 exit "$RETURN"