Merge branch 'master' of git://labs.riseup.net/~micah/monkeysphere
[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}/defaultenv"
25 . "${SYSSHAREDIR}/common"
26
27 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/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
59  add-id-certifier (c+) KEYID|FILE  import and tsign a certification key
60    [--domain (-n) DOMAIN]            limit ID certifications to DOMAIN
61    [--trust (-t) TRUST]              trust level of certifier (default: full)
62    [--depth (-d) DEPTH]              trust depth for certifier (default: 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 --quiet --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" 2>&1 | log debug
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 MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
125 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
126 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=$AUTHORIZED_USER_IDS}
127 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=$RAW_AUTHORIZED_KEYS}
128 STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
129
130 # other variables
131 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
132 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
133 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
134 CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
135 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
136
137 # export variables needed in su invocation
138 export DATE
139 export LOG_LEVEL
140 export KEYSERVER
141 export MONKEYSPHERE_USER
142 export MONKEYSPHERE_GROUP
143 export PROMPT
144 export CHECK_KEYSERVER
145 export REQUIRED_USER_KEY_CAPABILITY
146 export GNUPGHOME_CORE
147 export GNUPGHOME_SPHERE
148 export GNUPGHOME
149 export CORE_KEYLENGTH
150 export LOG_PREFIX
151
152 # get subcommand
153 COMMAND="$1"
154 [ "$COMMAND" ] || $PGRM help
155 shift
156
157 case $COMMAND in
158     'setup'|'setup'|'s')
159         source "${MASHAREDIR}/setup"
160         setup
161         ;;
162
163     'update-users'|'update-user'|'u')
164         source "${MASHAREDIR}/setup"
165         setup
166         source "${MASHAREDIR}/update_users"
167         update_users "$@"
168         ;;
169
170     'refresh-keys'|'r')
171         source "${MASHAREDIR}/setup"
172         setup
173         gpg_sphere "--keyserver $KEYSERVER --refresh-keys"
174         ;;
175
176     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
177         source "${MASHAREDIR}/setup"
178         setup
179         source "${MASHAREDIR}/add_certifier"
180         add_certifier "$@"
181         ;;
182
183     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
184         source "${MASHAREDIR}/setup"
185         setup
186         source "${MASHAREDIR}/remove_certifier"
187         remove_certifier "$@"
188         ;;
189
190     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
191         source "${MASHAREDIR}/setup"
192         setup
193         source "${MASHAREDIR}/list_certifiers"
194         list_certifiers
195         ;;
196
197     'diagnostics'|'d')
198         source "${MASHAREDIR}/setup"
199         setup
200         source "${MASHAREDIR}/diagnostics"
201         diagnostics
202         ;;
203
204     'gpg-cmd')
205         source "${MASHAREDIR}/setup"
206         setup
207         gpg_sphere "$@"
208         ;;
209
210     'version'|'v')
211         version
212         ;;
213
214     '--help'|'help'|'-h'|'h'|'?')
215         usage
216         ;;
217
218     *)
219         failure "Unknown command: '$COMMAND'
220 Type '$PGRM help' for usage."
221         ;;
222 esac