Merge commit 'micah/master'
[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 # sharedir for authentication functions
24 MASHAREDIR="${SYSSHAREDIR}/ma"
25
26 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
27 export SYSDATADIR
28
29 # temp directory to enable atomic moves of authorized_keys files
30 MATMPDIR="${SYSDATADIR}/tmp"
31 export MSTMPDIR
32
33 # UTC date in ISO 8601 format if needed
34 DATE=$(date -u '+%FT%T')
35
36 # unset some environment variables that could screw things up
37 unset GREP_OPTIONS
38
39 # default return code
40 RETURN=0
41
42 ########################################################################
43 # FUNCTIONS
44 ########################################################################
45
46 usage() {
47     cat <<EOF >&2
48 usage: $PGRM <subcommand> [options] [args]
49 Monkeysphere authentication admin tool.
50
51 subcommands:
52  update-users (u) [USER]...          update user authorized_keys files
53  add-id-certifier (c+) KEYID         import and tsign a certification key
54    --domain (-n) DOMAIN                limit ID certifications to DOMAIN
55    --trust (-t) TRUST                  trust level of certifier (full)
56    --depth (-d) DEPTH                  trust depth for certifier (1)
57  remove-id-certifier (c-) KEYID      remove a certification key
58  list-id-certifiers (c)              list certification keys
59
60  expert
61   diagnostics (d)                    monkeysphere authentication status
62   gpg-cmd CMD                        execute gpg command
63
64  version (v)                         show version number
65  help (h,?)                          this help
66
67 EOF
68 }
69
70 # function to run command as monkeysphere user
71 su_monkeysphere_user() {
72     # if the current user is the monkeysphere user, then just eval
73     # command
74     if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
75         eval "$@"
76
77     # otherwise su command as monkeysphere user
78     else
79         su "$MONKEYSPHERE_USER" -c "$@"
80     fi
81 }
82
83 # function to interact with the gpg core keyring
84 gpg_core() {
85     local returnCode
86
87     GNUPGHOME="$GNUPGHOME_CORE"
88     export GNUPGHOME
89
90     # NOTE: we supress this warning because we need the monkeysphere
91     # user to be able to read the host pubring.  we realize this might
92     # be problematic, but it's the simplest solution, without too much
93     # loss of security.
94     gpg --no-permission-warning "$@"
95     returnCode="$?"
96
97     # always reset the permissions on the host pubring so that the
98     # monkeysphere user can read the trust signatures
99     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_CORE}/pubring.gpg"
100     chmod g+r "${GNUPGHOME_CORE}/pubring.gpg"
101     
102     return "$returnCode"
103 }
104
105 # function to interact with the gpg sphere keyring
106 # FIXME: this function requires basically accepts only a single
107 # argument because of problems with quote expansion.  this needs to be
108 # fixed/improved.
109 gpg_sphere() {
110     GNUPGHOME="$GNUPGHOME_SPHERE"
111     export GNUPGHOME
112
113     su_monkeysphere_user "gpg $@"
114 }
115
116 ########################################################################
117 # MAIN
118 ########################################################################
119
120 # unset variables that should be defined only in config file
121 unset KEYSERVER
122 unset AUTHORIZED_USER_IDS
123 unset RAW_AUTHORIZED_KEYS
124 unset MONKEYSPHERE_USER
125
126 # load configuration file
127 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
128
129 # set empty config variable with ones from the environment, or with
130 # defaults
131 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
132 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
133 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
134 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
135 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
136
137 # other variables
138 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
139 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
140 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${SYSDATADIR}/authentication/core"}
141 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${SYSDATADIR}/authentication/sphere"}
142
143 # export variables needed in su invocation
144 export DATE
145 export MODE
146 export LOG_LEVEL
147 export MONKEYSPHERE_USER
148 export KEYSERVER
149 export CHECK_KEYSERVER
150 export REQUIRED_USER_KEY_CAPABILITY
151 export GNUPGHOME_CORE
152 export GNUPGHOME_SPHERE
153 export GNUPGHOME
154
155 # get subcommand
156 COMMAND="$1"
157 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
158 shift
159
160 case $COMMAND in
161     'update-users'|'update-user'|'u')
162         source "${MASHAREDIR}/update_users"
163         update_users "$@"
164         ;;
165
166     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
167         source "${MASHAREDIR}/add_certifier"
168         add_certifier "$@"
169         ;;
170
171     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
172         source "${MASHAREDIR}/remove_certifier"
173         remove_certifier "$@"
174         ;;
175
176     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
177         source "${MASHAREDIR}/list_certifiers"
178         list_certifiers "$@"
179         ;;
180
181     'expert'|'e')
182         SUBCOMMAND="$1"
183         shift
184         case "$SUBCOMMAND" in
185             'diagnostics'|'d')
186                 source "${MASHAREDIR}/diagnostics"
187                 diagnostics
188                 ;;
189
190             'gpg-cmd')
191                 gpg_sphere "$@"
192                 ;;
193
194             *)
195                 failure "Unknown expert subcommand: '$COMMAND'
196 Type '$PGRM help' for usage."
197                 ;;
198         esac
199         ;;
200
201     'version'|'v')
202         echo "$VERSION"
203         ;;
204
205     '--help'|'help'|'-h'|'h'|'?')
206         usage
207         ;;
208
209     *)
210         failure "Unknown command: '$COMMAND'
211 Type '$PGRM help' for usage."
212         ;;
213 esac
214
215 exit "$RETURN"