0e6f986fc1d23a3a3809c79112483015d54e3285
[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
58  add-id-certifier (c+) KEYID|FILE  import and tsign a certification key
59    [--domain (-n) DOMAIN]            limit ID certifications to DOMAIN
60    [--trust (-t) TRUST]              trust level of certifier (default: full)
61    [--depth (-d) DEPTH]              trust depth for certifier (default: 1)
62  remove-id-certifier (c-) KEYID    remove a certification key
63  list-id-certifiers (c)            list certification keys
64
65  version (v)                       show version number
66  help (h,?)                        this help
67
68 See ${PGRM}(8) for more info.
69 EOF
70 }
71
72 # function to interact with the gpg core keyring
73 gpg_core() {
74     GNUPGHOME="$GNUPGHOME_CORE"
75     export GNUPGHOME
76
77     gpg --no-greeting --quiet --no-tty "$@"
78 }
79
80 # function to interact with the gpg sphere keyring
81 # FIXME: this function requires only a single argument because of
82 # problems with quote expansion.  this needs to be fixed/improved.
83 gpg_sphere() {
84     GNUPGHOME="$GNUPGHOME_SPHERE"
85     export GNUPGHOME
86  
87     su_monkeysphere_user "gpg --no-greeting --quiet --no-tty $@"
88 }
89
90 # output to stdout the core fingerprint from the gpg core secret
91 # keyring
92 core_fingerprint() {
93     log debug "determining core key fingerprint..."
94     gpg_core --list-secret-key --with-colons \
95         --fixed-list-mode --with-fingerprint \
96         | grep ^fpr: | cut -d: -f10
97 }
98
99 # export signatures from core to sphere
100 gpg_core_sphere_sig_transfer() {
101     log debug "exporting core local sigs to sphere..."
102     gpg_core --export-options export-local-sigs --export | \
103         gpg_sphere "--import-options import-local-sigs --import" 2>&1 | log debug
104 }
105
106 ########################################################################
107 # MAIN
108 ########################################################################
109
110 # set unset default variables
111 AUTHORIZED_USER_IDS="%h/.monkeysphere/authorized_user_ids"
112 RAW_AUTHORIZED_KEYS="%h/.ssh/authorized_keys"
113
114 # load configuration file
115 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] \
116     && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
117
118 # set empty config variable with ones from the environment
119 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
120 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
121 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
122 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
123 MONKEYSPHERE_GROUP=$(groups "$MONKEYSPHERE_USER" | cut -d: -f2 | awk '{ print $1 }')
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 LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
134
135 # export variables needed in su invocation
136 export DATE
137 export MODE
138 export LOG_LEVEL
139 export KEYSERVER
140 export MONKEYSPHERE_USER
141 export MONKEYSPHERE_GROUP
142 export PROMPT
143 export CHECK_KEYSERVER
144 export REQUIRED_USER_KEY_CAPABILITY
145 export GNUPGHOME_CORE
146 export GNUPGHOME_SPHERE
147 export GNUPGHOME
148 export CORE_KEYLENGTH
149 export LOG_PREFIX
150
151 # get subcommand
152 COMMAND="$1"
153 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
154 shift
155
156 case $COMMAND in
157     'setup'|'setup'|'s')
158         source "${MASHAREDIR}/setup"
159         setup
160         ;;
161
162     'update-users'|'update-user'|'u')
163         source "${MASHAREDIR}/setup"
164         setup
165         source "${MASHAREDIR}/update_users"
166         update_users "$@"
167         ;;
168
169     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
170         source "${MASHAREDIR}/setup"
171         setup
172         source "${MASHAREDIR}/add_certifier"
173         add_certifier "$@"
174         ;;
175
176     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
177         source "${MASHAREDIR}/setup"
178         setup
179         source "${MASHAREDIR}/remove_certifier"
180         remove_certifier "$@"
181         ;;
182
183     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
184         source "${MASHAREDIR}/setup"
185         setup
186         source "${MASHAREDIR}/list_certifiers"
187         list_certifiers
188         ;;
189
190     'diagnostics'|'d')
191         source "${MASHAREDIR}/setup"
192         setup
193         source "${MASHAREDIR}/diagnostics"
194         diagnostics
195         ;;
196
197     'gpg-cmd')
198         source "${MASHAREDIR}/setup"
199         setup
200         gpg_sphere "$@"
201         ;;
202
203     'version'|'v')
204         version
205         ;;
206
207     '--help'|'help'|'-h'|'h'|'?')
208         usage
209         ;;
210
211     *)
212         failure "Unknown command: '$COMMAND'
213 Type '$PGRM help' for usage."
214         ;;
215 esac