break su_monkeysphere_user into common function, since it will likely
[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 PGRM=$(basename $0)
18
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
20 export SYSSHAREDIR
21 . "${SYSSHAREDIR}/common" || exit 1
22
23 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
24 export SYSDATADIR
25
26 # sharedir for authentication functions
27 MASHAREDIR="${SYSSHAREDIR}/ma"
28
29 # datadir for authentication functions
30 MADATADIR="${SYSDATADIR}/authentication"
31
32 # temp directory to enable atomic moves of authorized_keys files
33 MATMPDIR="${MADATADIR}/tmp"
34 export MSTMPDIR
35
36 # UTC date in ISO 8601 format if needed
37 DATE=$(date -u '+%FT%T')
38
39 # unset some environment variables that could screw things up
40 unset GREP_OPTIONS
41
42 # default return code
43 RETURN=0
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  setup (s)                           setup monkeysphere user authentication
56  update-users (u) [USER]...          update user authorized_keys files
57  add-id-certifier (c+) KEYID         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  expert <expert-subcommand>          run expert command
65  expert help                         expert command help
66
67  version (v)                         show version number
68  help (h,?)                          this help
69
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     # NOTE: we supress this warning because we need the monkeysphere
79     # user to be able to read the host pubring.  we realize this might
80     # be problematic, but it's the simplest solution, without too much
81     # loss of security.
82     gpg "$@"
83 }
84
85 # function to interact with the gpg sphere keyring
86 # FIXME: this function requires basically accepts only a single
87 # argument because of problems with quote expansion.  this needs to be
88 # fixed/improved.
89 gpg_sphere() {
90     GNUPGHOME="$GNUPGHOME_SPHERE"
91     export GNUPGHOME
92
93     su_monkeysphere_user "gpg $@"
94 }
95
96 # export signatures from core to sphere
97 gpg_core_sphere_sig_transfer() {
98     gpg_core --export-options export-local-sigs --export | \
99         gpg_sphere --import-options import-local-sigs --import
100 }
101
102 ########################################################################
103 # MAIN
104 ########################################################################
105
106 # unset variables that should be defined only in config file
107 unset KEYSERVER
108 unset AUTHORIZED_USER_IDS
109 unset RAW_AUTHORIZED_KEYS
110 unset MONKEYSPHERE_USER
111
112 # load configuration file
113 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
114
115 # set empty config variable with ones from the environment, or with
116 # defaults
117 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
118 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
119 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
120 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
121 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
122
123 # other variables
124 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
125 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
126 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
127 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
128 CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
129
130 # export variables needed in su invocation
131 export DATE
132 export MODE
133 export LOG_LEVEL
134 export MONKEYSPHERE_USER
135 export KEYSERVER
136 export CHECK_KEYSERVER
137 export REQUIRED_USER_KEY_CAPABILITY
138 export GNUPGHOME_CORE
139 export GNUPGHOME_SPHERE
140 export GNUPGHOME
141 export CORE_KEYLENGTH
142
143 # get subcommand
144 COMMAND="$1"
145 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
146 shift
147
148 case $COMMAND in
149     'setup'|'setup'|'s')
150         source "${MASHAREDIR}/setup"
151         setup "$@"
152         ;;
153
154     'update-users'|'update-user'|'u')
155         source "${MASHAREDIR}/update_users"
156         update_users "$@"
157         ;;
158
159     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
160         source "${MASHAREDIR}/add_certifier"
161         add_certifier "$@"
162         ;;
163
164     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
165         source "${MASHAREDIR}/remove_certifier"
166         remove_certifier "$@"
167         ;;
168
169     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
170         source "${MASHAREDIR}/list_certifiers"
171         list_certifiers "$@"
172         ;;
173
174     'expert')
175         SUBCOMMAND="$1"
176         shift
177         case "$SUBCOMMAND" in
178             'help'|'h'|'?')
179                 cat <<EOF
180 usage: $PGRM expert <subcommand> [options] [args]
181
182 expert subcommands:
183  diagnostics (d)                     monkeysphere authentication status
184  gpg-cmd CMD                         execute gpg command
185
186 EOF
187                 ;;
188
189             'diagnostics'|'d')
190                 source "${MASHAREDIR}/diagnostics"
191                 diagnostics
192                 ;;
193
194             'gpg-cmd')
195                 gpg_sphere "$@"
196                 ;;
197
198             *)
199                 failure "Unknown expert subcommand: '$COMMAND'
200 Type '$PGRM help' for usage."
201                 ;;
202         esac
203         ;;
204
205     'version'|'v')
206         echo "$VERSION"
207         ;;
208
209     '--help'|'help'|'-h'|'h'|'?')
210         usage
211         ;;
212
213     *)
214         failure "Unknown command: '$COMMAND'
215 Type '$PGRM help' for usage."
216         ;;
217 esac
218
219 exit "$RETURN"