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