add setup subcommand call to ms-authentication, and more fixes to tests
[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 PGRM=$(basename $0)
16
17 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
18 export SYSSHAREDIR
19 . "${SYSSHAREDIR}/common" || exit 1
20
21 # sharedir for authentication functions
22 MASHAREDIR="${SYSSHAREDIR}/ma"
23
24 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
25 export SYSDATADIR
26
27 # temp directory to enable atomic moves of authorized_keys files
28 MATMPDIR="${SYSDATADIR}/tmp"
29 export MSTMPDIR
30
31 # UTC date in ISO 8601 format if needed
32 DATE=$(date -u '+%FT%T')
33
34 # unset some environment variables that could screw things up
35 unset GREP_OPTIONS
36
37 # default return code
38 RETURN=0
39
40 ########################################################################
41 # FUNCTIONS
42 ########################################################################
43
44 usage() {
45     cat <<EOF >&2
46 usage: $PGRM <subcommand> [options] [args]
47 Monkeysphere authentication admin tool.
48
49 subcommands:
50  setup (s)                           setup monkeysphere user authentication
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 <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 core keyring
82 gpg_core() {
83     local returnCode
84
85     GNUPGHOME="$GNUPGHOME_CORE"
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 --no-permission-warning "$@"
93     returnCode="$?"
94
95     # always reset the permissions on the host pubring so that the
96     # monkeysphere user can read the trust signatures
97     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_CORE}/pubring.gpg"
98     chmod g+r "${GNUPGHOME_CORE}/pubring.gpg"
99     
100     return "$returnCode"
101 }
102
103 # function to interact with the gpg sphere keyring
104 # FIXME: this function requires basically accepts only a single
105 # argument because of problems with quote expansion.  this needs to be
106 # fixed/improved.
107 gpg_sphere() {
108     GNUPGHOME="$GNUPGHOME_SPHERE"
109     export GNUPGHOME
110
111     su_monkeysphere_user "gpg $@"
112 }
113
114 ########################################################################
115 # MAIN
116 ########################################################################
117
118 # unset variables that should be defined only in config file
119 unset KEYSERVER
120 unset AUTHORIZED_USER_IDS
121 unset RAW_AUTHORIZED_KEYS
122 unset MONKEYSPHERE_USER
123
124 # load configuration file
125 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
126
127 # set empty config variable with ones from the environment, or with
128 # defaults
129 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
130 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
131 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
132 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
133 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
134
135 # other variables
136 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
137 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
138 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${SYSDATADIR}/authentication/core"}
139 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${SYSDATADIR}/authentication/sphere"}
140
141 # export variables needed in su invocation
142 export DATE
143 export MODE
144 export LOG_LEVEL
145 export MONKEYSPHERE_USER
146 export KEYSERVER
147 export CHECK_KEYSERVER
148 export REQUIRED_USER_KEY_CAPABILITY
149 export GNUPGHOME_CORE
150 export GNUPGHOME_SPHERE
151 export GNUPGHOME
152
153 # get subcommand
154 COMMAND="$1"
155 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
156 shift
157
158 case $COMMAND in
159     'setup'|'setup'|'s')
160         source "${MASHAREDIR}/setup"
161         setup "$@"
162         ;;
163
164     'update-users'|'update-user'|'u')
165         source "${MASHAREDIR}/update_users"
166         update_users "$@"
167         ;;
168
169     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
170         source "${MASHAREDIR}/add_certifier"
171         add_certifier "$@"
172         ;;
173
174     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
175         source "${MASHAREDIR}/remove_certifier"
176         remove_certifier "$@"
177         ;;
178
179     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
180         source "${MASHAREDIR}/list_certifiers"
181         list_certifiers "$@"
182         ;;
183
184     'expert'|'e')
185         SUBCOMMAND="$1"
186         shift
187         case "$SUBCOMMAND" in
188             'help'|'h'|'?')
189                 cat <<EOF
190 usage: $PGRM expert <subcommand> [options] [args]
191
192 expert subcommands:
193  diagnostics (d)                     monkeysphere authentication status
194  gpg-cmd CMD                         execute gpg command
195
196 EOF
197                 ;;
198
199             'diagnostics'|'d')
200                 source "${MASHAREDIR}/diagnostics"
201                 diagnostics
202                 ;;
203
204             'gpg-cmd')
205                 gpg_sphere "$@"
206                 ;;
207
208             *)
209                 failure "Unknown expert subcommand: '$COMMAND'
210 Type '$PGRM help' for usage."
211                 ;;
212         esac
213         ;;
214
215     'version'|'v')
216         echo "$VERSION"
217         ;;
218
219     '--help'|'help'|'-h'|'h'|'?')
220         usage
221         ;;
222
223     *)
224         failure "Unknown command: '$COMMAND'
225 Type '$PGRM help' for usage."
226         ;;
227 esac
228
229 exit "$RETURN"