fix location of the primary public keyring for the new reorganization
[monkeysphere.git] / src / monkeysphere-authentication
1 #!/usr/bin/env bash
2
3 # monkeysphere-authentication: Monkeysphere authentication 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 # 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/authentication"}
22 export SYSDATADIR
23
24 # monkeysphere temp directory, in sysdatadir to enable atomic moves of
25 # authorized_keys files
26 MSTMPDIR="${SYSDATADIR}/tmp"
27 export MSTMPDIR
28
29 # UTC date in ISO 8601 format if needed
30 DATE=$(date -u '+%FT%T')
31
32 # unset some environment variables that could screw things up
33 unset GREP_OPTIONS
34
35 # default return code
36 RETURN=0
37
38 ########################################################################
39 # FUNCTIONS
40 ########################################################################
41
42 usage() {
43     cat <<EOF >&2
44 usage: $PGRM <subcommand> [options] [args]
45 Monkeysphere authentication admin tool.
46
47 subcommands:
48  update-users (u) [USER]...          update user authorized_keys files
49  add-id-certifier (c+) KEYID         import and tsign a certification key
50    --domain (-n) DOMAIN                limit ID certifications to DOMAIN
51    --trust (-t) TRUST                  trust level of certifier (full)
52    --depth (-d) DEPTH                  trust depth for certifier (1)
53  remove-id-certifier (c-) KEYID      remove a certification key
54  list-id-certifiers (c)              list certification keys
55
56  expert
57   diagnostics (d)                    monkeysphere authentication status
58   gpg-cmd CMD                        execute gpg command
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 host gnupg 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 --no-permission-warning "$@"
91     returnCode="$?"
92
93     # always reset the permissions on the host pubring so that the
94     # monkeysphere user can read the trust signatures
95     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
96     chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
97     
98     return "$returnCode"
99 }
100
101 # function to interact with the authentication gnupg keyring
102 # FIXME: this function requires basically accepts only a single
103 # argument because of problems with quote expansion.  this needs to be
104 # fixed/improved.
105 gpg_authentication() {
106     GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
107     export GNUPGHOME
108
109     su_monkeysphere_user "gpg $@"
110 }
111
112 # check if user is root
113 is_root() {
114     [ $(id -u 2>/dev/null) = '0' ]
115 }
116
117 # check that user is root, for functions that require root access
118 check_user() {
119     is_root || failure "You must be root to run this command."
120 }
121
122 # output just key fingerprint
123 fingerprint_server_key() {
124     # set the pipefail option so functions fails if can't read sec key
125     set -o pipefail
126
127     gpg_host --list-secret-keys --fingerprint \
128         --with-colons --fixed-list-mode 2> /dev/null | \
129         grep '^fpr:' | head -1 | cut -d: -f10 2>/dev/null
130 }
131
132 # function to check for host secret key
133 check_host_keyring() {
134     fingerprint_server_key >/dev/null \
135         || failure "You don't appear to have a Monkeysphere host key on this server.  Please run 'monkeysphere-server gen-key' first."
136 }
137
138 ########################################################################
139 # MAIN
140 ########################################################################
141
142 # unset variables that should be defined only in config file
143 unset KEYSERVER
144 unset AUTHORIZED_USER_IDS
145 unset RAW_AUTHORIZED_KEYS
146 unset MONKEYSPHERE_USER
147
148 # load configuration file
149 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${SYSCONFIGDIR}/monkeysphere-server.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 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
162 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${SYSDATADIR}/gnupg-host"}
163 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${SYSDATADIR}/gnupg-authentication"}
164
165 # export variables needed in su invocation
166 export DATE
167 export MODE
168 export MONKEYSPHERE_USER
169 export LOG_LEVEL
170 export KEYSERVER
171 export CHECK_KEYSERVER
172 export REQUIRED_USER_KEY_CAPABILITY
173 export GNUPGHOME_HOST
174 export GNUPGHOME_AUTHENTICATION
175 export GNUPGHOME
176
177 # get subcommand
178 COMMAND="$1"
179 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
180 shift
181
182 case $COMMAND in
183     'update-users'|'update-user'|'u')
184         check_user
185         check_host_keyring
186         update_users "$@"
187         ;;
188
189     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
190         check_user
191         check_host_keyring
192         add_certifier "$@"
193         ;;
194
195     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
196         check_user
197         check_host_keyring
198         remove_certifier "$@"
199         ;;
200
201     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
202         check_user
203         check_host_keyring
204         list_certifiers "$@"
205         ;;
206
207     'expert'|'e')
208         check_user
209         SUBCOMMAND="$1"
210         shift
211         case "$SUBCOMMAND" in
212             'diagnostics'|'d')
213                 diagnostics
214                 ;;
215
216             'gpg-cmd')
217                 gpg_authentication "$@"
218                 ;;
219
220             *)
221                 failure "Unknown expert subcommand: '$COMMAND'
222 Type '$PGRM help' for usage."
223                 ;;
224         esac
225         ;;
226
227     'version'|'v')
228         echo "$VERSION"
229         ;;
230
231     '--help'|'help'|'-h'|'h'|'?')
232         usage
233         ;;
234
235     *)
236         failure "Unknown command: '$COMMAND'
237 Type '$PGRM help' for usage."
238         ;;
239 esac
240
241 exit "$RETURN"