add ability to specify key length of core secret key, so the test scripts can specify...
[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 # 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 authentication functions
27 MASHAREDIR="${SYSSHAREDIR}/ma"
28
29 # datadir for authentication functions
30 MADATADIR="${SYSDATADIR}/authentication"
31
32 # temp directory to enable atomic moves of authorized_keys files
33 MATMPDIR="${MADATADIR}/tmp"
34 export MSTMPDIR
35
36 # UTC date in ISO 8601 format if needed
37 DATE=$(date -u '+%FT%T')
38
39 # unset some environment variables that could screw things up
40 unset GREP_OPTIONS
41
42 # default return code
43 RETURN=0
44
45 ########################################################################
46 # FUNCTIONS
47 ########################################################################
48
49 usage() {
50     cat <<EOF >&2
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere authentication admin tool.
53
54 subcommands:
55  setup (s)                           setup monkeysphere user authentication
56  update-users (u) [USER]...          update user authorized_keys files
57  add-id-certifier (c+) KEYID         import and tsign a certification key
58    --domain (-n) DOMAIN                limit ID certifications to DOMAIN
59    --trust (-t) TRUST                  trust level of certifier (full)
60    --depth (-d) DEPTH                  trust depth for certifier (1)
61  remove-id-certifier (c-) KEYID      remove a certification key
62  list-id-certifiers (c)              list certification keys
63
64  expert <expert-subcommand>          run expert command
65  expert help                         expert command help
66
67  version (v)                         show version number
68  help (h,?)                          this help
69
70 EOF
71 }
72
73 # function to run command as monkeysphere user
74 su_monkeysphere_user() {
75     # if the current user is the monkeysphere user, then just eval
76     # command
77     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
78         eval "$@"
79
80     # otherwise su command as monkeysphere user
81     else
82         su "$MONKEYSPHERE_USER" -c "$@"
83     fi
84 }
85
86 # function to interact with the gpg core keyring
87 gpg_core() {
88     GNUPGHOME="$GNUPGHOME_CORE"
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 "$@"
96 }
97
98 # function to interact with the gpg sphere keyring
99 # FIXME: this function requires basically accepts only a single
100 # argument because of problems with quote expansion.  this needs to be
101 # fixed/improved.
102 gpg_sphere() {
103     GNUPGHOME="$GNUPGHOME_SPHERE"
104     export GNUPGHOME
105
106     su_monkeysphere_user "gpg $@"
107 }
108
109 # export signatures from core to sphere
110 gpg_core_sphere_sig_transfer() {
111     gpg_core --export-options export-local-sigs --export | \
112         gpg_sphere --import-options import-local-sigs --import
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:="${MADATADIR}/core"}
140 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
141 CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
142
143 # export variables needed in su invocation
144 export DATE
145 export MODE
146 export LOG_LEVEL
147 export MONKEYSPHERE_USER
148 export KEYSERVER
149 export CHECK_KEYSERVER
150 export REQUIRED_USER_KEY_CAPABILITY
151 export GNUPGHOME_CORE
152 export GNUPGHOME_SPHERE
153 export GNUPGHOME
154 export CORE_KEYLENGTH
155
156 # get subcommand
157 COMMAND="$1"
158 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
159 shift
160
161 case $COMMAND in
162     'setup'|'setup'|'s')
163         source "${MASHAREDIR}/setup"
164         setup "$@"
165         ;;
166
167     'update-users'|'update-user'|'u')
168         source "${MASHAREDIR}/update_users"
169         update_users "$@"
170         ;;
171
172     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
173         source "${MASHAREDIR}/add_certifier"
174         add_certifier "$@"
175         ;;
176
177     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
178         source "${MASHAREDIR}/remove_certifier"
179         remove_certifier "$@"
180         ;;
181
182     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
183         source "${MASHAREDIR}/list_certifiers"
184         list_certifiers "$@"
185         ;;
186
187     'expert'|'e')
188         SUBCOMMAND="$1"
189         shift
190         case "$SUBCOMMAND" in
191             'help'|'h'|'?')
192                 cat <<EOF
193 usage: $PGRM expert <subcommand> [options] [args]
194
195 expert subcommands:
196  diagnostics (d)                     monkeysphere authentication status
197  gpg-cmd CMD                         execute gpg command
198
199 EOF
200                 ;;
201
202             'diagnostics'|'d')
203                 source "${MASHAREDIR}/diagnostics"
204                 diagnostics
205                 ;;
206
207             'gpg-cmd')
208                 gpg_sphere "$@"
209                 ;;
210
211             *)
212                 failure "Unknown expert subcommand: '$COMMAND'
213 Type '$PGRM help' for usage."
214                 ;;
215         esac
216         ;;
217
218     'version'|'v')
219         echo "$VERSION"
220         ;;
221
222     '--help'|'help'|'-h'|'h'|'?')
223         usage
224         ;;
225
226     *)
227         failure "Unknown command: '$COMMAND'
228 Type '$PGRM help' for usage."
229         ;;
230 esac
231
232 exit "$RETURN"