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