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