Merge commit 'dkg/master'
[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@finestructure.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 set -e
15
16 PGRM=$(basename $0)
17
18 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
19 export SYSSHAREDIR
20 . "${SYSSHAREDIR}/common" || exit 1
21
22 # sharedir for authentication functions
23 MASHAREDIR="${SYSSHAREDIR}/ma"
24
25 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
26 export SYSDATADIR
27
28 # temp directory to enable atomic moves of authorized_keys files
29 MATMPDIR="${SYSDATADIR}/tmp"
30 export MSTMPDIR
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 authentication admin tool.
49
50 subcommands:
51  update-users (u) [USER]...          update user authorized_keys files
52  add-id-certifier (c+) KEYID         import and tsign a certification key
53    --domain (-n) DOMAIN                limit ID certifications to DOMAIN
54    --trust (-t) TRUST                  trust level of certifier (full)
55    --depth (-d) DEPTH                  trust depth for certifier (1)
56  remove-id-certifier (c-) KEYID      remove a certification key
57  list-id-certifiers (c)              list certification keys
58
59  expert
60   diagnostics (d)                    monkeysphere authentication status
61   gpg-cmd CMD                        execute gpg command
62
63  version (v)                         show version number
64  help (h,?)                          this help
65
66 EOF
67 }
68
69 # function to run command as monkeysphere user
70 su_monkeysphere_user() {
71     # if the current user is the monkeysphere user, then just eval
72     # command
73     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
74         eval "$@"
75
76     # otherwise su command as monkeysphere user
77     else
78         su "$MONKEYSPHERE_USER" -c "$@"
79     fi
80 }
81
82 # function to interact with the gpg core keyring
83 gpg_core() {
84     local returnCode
85
86     GNUPGHOME="$GNUPGHOME_CORE"
87     export GNUPGHOME
88
89     # NOTE: we supress this warning because we need the monkeysphere
90     # user to be able to read the host pubring.  we realize this might
91     # be problematic, but it's the simplest solution, without too much
92     # loss of security.
93     gpg --no-permission-warning "$@"
94     returnCode="$?"
95
96     # always reset the permissions on the host pubring so that the
97     # monkeysphere user can read the trust signatures
98     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_CORE}/pubring.gpg"
99     chmod g+r "${GNUPGHOME_CORE}/pubring.gpg"
100     
101     return "$returnCode"
102 }
103
104 # function to interact with the gpg sphere keyring
105 # FIXME: this function requires basically accepts only a single
106 # argument because of problems with quote expansion.  this needs to be
107 # fixed/improved.
108 gpg_sphere() {
109     GNUPGHOME="$GNUPGHOME_SPHERE"
110     export GNUPGHOME
111
112     su_monkeysphere_user "gpg $@"
113 }
114
115 ########################################################################
116 # MAIN
117 ########################################################################
118
119 # unset variables that should be defined only in config file
120 unset KEYSERVER
121 unset AUTHORIZED_USER_IDS
122 unset RAW_AUTHORIZED_KEYS
123 unset MONKEYSPHERE_USER
124
125 # load configuration file
126 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
127
128 # set empty config variable with ones from the environment, or with
129 # defaults
130 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
131 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
132 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
133 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
134 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
135
136 # other variables
137 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
138 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
139 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${SYSDATADIR}/authentication/core"}
140 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${SYSDATADIR}/authentication/sphere"}
141
142 # export variables needed in su invocation
143 export DATE
144 export MODE
145 export LOG_LEVEL
146 export MONKEYSPHERE_USER
147 export KEYSERVER
148 export CHECK_KEYSERVER
149 export REQUIRED_USER_KEY_CAPABILITY
150 export GNUPGHOME_CORE
151 export GNUPGHOME_SPHERE
152 export GNUPGHOME
153
154 # get subcommand
155 COMMAND="$1"
156 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
157 shift
158
159 case $COMMAND in
160     'update-users'|'update-user'|'u')
161         source "${MASHAREDIR}/update_users"
162         update_users "$@"
163         ;;
164
165     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
166         source "${MASHAREDIR}/add_certifier"
167         add_certifier "$@"
168         ;;
169
170     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
171         source "${MASHAREDIR}/remove_certifier"
172         remove_certifier "$@"
173         ;;
174
175     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
176         source "${MASHAREDIR}/list_certifiers"
177         list_certifiers "$@"
178         ;;
179
180     'expert'|'e')
181         SUBCOMMAND="$1"
182         shift
183         case "$SUBCOMMAND" in
184             'diagnostics'|'d')
185                 source "${MASHAREDIR}/diagnostics"
186                 diagnostics
187                 ;;
188
189             'gpg-cmd')
190                 gpg_sphere "$@"
191                 ;;
192
193             *)
194                 failure "Unknown expert subcommand: '$COMMAND'
195 Type '$PGRM help' for usage."
196                 ;;
197         esac
198         ;;
199
200     'version'|'v')
201         echo "$VERSION"
202         ;;
203
204     '--help'|'help'|'-h'|'h'|'?')
205         usage
206         ;;
207
208     *)
209         failure "Unknown command: '$COMMAND'
210 Type '$PGRM help' for usage."
211         ;;
212 esac
213
214 exit "$RETURN"