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