fix specification of install directories in top level scripts.
[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 # set the pipefail option so pipelines fail on first command failure
18 set -o pipefail
19
20 PGRM=$(basename $0)
21
22 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"__SYSSHAREDIR_PREFIX__/share/monkeysphere"}
23 export SYSSHAREDIR
24 . "${SYSSHAREDIR}/defaultenv"
25 . "${SYSSHAREDIR}/common"
26
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"__SYSDATADIR_PREFIX__/monkeysphere"}
28 export SYSDATADIR
29
30 # sharedir for authentication functions
31 MASHAREDIR="${SYSSHAREDIR}/ma"
32
33 # datadir for authentication functions
34 MADATADIR="${SYSDATADIR}/authentication"
35
36 # temp directory to enable atomic moves of authorized_keys files
37 MATMPDIR="${MADATADIR}/tmp"
38 export MATMPDIR
39
40 # UTC date in ISO 8601 format if needed
41 DATE=$(date -u '+%FT%T')
42
43 # unset some environment variables that could screw things up
44 unset GREP_OPTIONS
45
46 ########################################################################
47 # FUNCTIONS
48 ########################################################################
49
50 usage() {
51     cat <<EOF >&2
52 usage: $PGRM <subcommand> [options] [args]
53 Monkeysphere authentication admin tool.
54
55 subcommands:
56  update-users (u) [USER]...        update user authorized_keys files
57  refresh-keys (r)                  refresh keys in keyring
58  keys-for-user USER                output valid keys for user
59
60  add-id-certifier (c+) KEYID|FILE  import and tsign a certification key
61    [--domain (-n) DOMAIN]            limit ID certifications to DOMAIN
62    [--trust (-t) TRUST]              trust level of certifier (default: full)
63    [--depth (-d) DEPTH]              trust depth for certifier (default: 1)
64  remove-id-certifier (c-) KEYID    remove a certification key
65  list-id-certifiers (c)            list certification keys
66
67  version (v)                       show version number
68  help (h,?)                        this help
69
70 See ${PGRM}(8) for more info.
71 EOF
72 }
73
74 # function to interact with the gpg core keyring
75 gpg_core() {
76     GNUPGHOME="$GNUPGHOME_CORE"
77     export GNUPGHOME
78
79     gpg --no-greeting --quiet --no-tty "$@"
80 }
81
82 # function to interact with the gpg sphere keyring
83 # FIXME: this function requires only a single argument because of
84 # problems with quote expansion.  this needs to be fixed/improved.
85 gpg_sphere() {
86     GNUPGHOME="$GNUPGHOME_SPHERE"
87     export GNUPGHOME
88  
89     su_monkeysphere_user "gpg --no-greeting --quiet --no-tty $@"
90 }
91
92 # output to stdout the core fingerprint from the gpg core secret
93 # keyring
94 core_fingerprint() {
95     log debug "determining core key fingerprint..."
96     gpg_core --list-secret-key --with-colons \
97         --fixed-list-mode --with-fingerprint \
98         | grep ^fpr: | cut -d: -f10
99 }
100
101 # export signatures from core to sphere
102 gpg_core_sphere_sig_transfer() {
103     log debug "exporting core local sigs to sphere..."
104     gpg_core --export-options export-local-sigs --export | \
105         gpg_sphere "--import-options import-local-sigs --import" 2>&1 | log debug
106 }
107
108 ########################################################################
109 # MAIN
110 ########################################################################
111
112 # set unset default variables
113 AUTHORIZED_USER_IDS="%h/.monkeysphere/authorized_user_ids"
114 RAW_AUTHORIZED_KEYS="%h/.ssh/authorized_keys"
115
116 # load configuration file
117 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] \
118     && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
119
120 # set empty config variable with ones from the environment
121 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
122 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
123 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
124 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
125 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
126 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
127 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=$AUTHORIZED_USER_IDS}
128 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=$RAW_AUTHORIZED_KEYS}
129 STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
130
131 # other variables
132 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
133 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
134 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
135 CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
136 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
137
138 # export variables needed in su invocation
139 export DATE
140 export LOG_LEVEL
141 export KEYSERVER
142 export MONKEYSPHERE_USER
143 export MONKEYSPHERE_GROUP
144 export PROMPT
145 export CHECK_KEYSERVER
146 export REQUIRED_USER_KEY_CAPABILITY
147 export GNUPGHOME_CORE
148 export GNUPGHOME_SPHERE
149 export GNUPGHOME
150 export CORE_KEYLENGTH
151 export LOG_PREFIX
152
153 if [ "$#" -eq 0 ] ; then 
154     usage
155     failure "Please supply a subcommand."
156 fi
157
158 # get subcommand
159 COMMAND="$1"
160 shift
161
162 case $COMMAND in
163     'setup'|'setup'|'s')
164         source "${MASHAREDIR}/setup"
165         setup
166         ;;
167
168     'update-users'|'update-user'|'update'|'u')
169         source "${MASHAREDIR}/setup"
170         setup
171         source "${MASHAREDIR}/update_users"
172         update_users "$@"
173         ;;
174
175     'refresh-keys'|'refresh'|'r')
176         source "${MASHAREDIR}/setup"
177         setup
178         gpg_sphere "--keyserver $KEYSERVER --refresh-keys"
179         ;;
180
181     'keys-for-user')
182         source "${MASHAREDIR}/keys_for_user"
183         keys_for_user "$@"
184         ;;
185
186     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
187         source "${MASHAREDIR}/setup"
188         setup
189         source "${MASHAREDIR}/add_certifier"
190         add_certifier "$@"
191         ;;
192
193     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
194         source "${MASHAREDIR}/setup"
195         setup
196         source "${MASHAREDIR}/remove_certifier"
197         remove_certifier "$@"
198         ;;
199
200     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
201         source "${MASHAREDIR}/setup"
202         setup
203         source "${MASHAREDIR}/list_certifiers"
204         list_certifiers
205         ;;
206
207     'diagnostics'|'d')
208         source "${MASHAREDIR}/setup"
209         setup
210         source "${MASHAREDIR}/diagnostics"
211         diagnostics
212         ;;
213
214     'gpg-cmd')
215         source "${MASHAREDIR}/setup"
216         setup
217         gpg_sphere "$@"
218         ;;
219
220     'version'|'--version'|'v')
221         version
222         ;;
223
224     '--help'|'help'|'-h'|'h'|'?')
225         usage
226         ;;
227
228     *)
229         failure "Unknown command: '$COMMAND'
230 Try '$PGRM help' for usage."
231         ;;
232 esac