@@ -20,6 +20,40 @@ fail() {
2020 exit 1
2121}
2222
23+ # Remove any generated-content temp file left behind by a failed run.
24+ cleanup_tmp_files () {
25+ rm -f " ${tokens_tmp:- } " " ${audit_key_tmp:- } "
26+ }
27+ trap cleanup_tmp_files EXIT
28+
29+ # Refuse a path that is not one plain file.
30+ require_regular_file () {
31+ local path=" $1 "
32+ local links=" "
33+ if [[ -L " $path " ]] || { [[ -e " $path " ]] && [[ ! -f " $path " ]]; }; then
34+ fail " $path is not a regular file; refusing"
35+ fi
36+ if [[ -e " $path " ]]; then
37+ links=" $( stat -c ' %h' " $path " ) "
38+ if [[ " $links " != " 1" ]]; then
39+ fail " $path is not a regular file; refusing"
40+ fi
41+ fi
42+ }
43+
44+ # chmod as the parent directory's owner.
45+ secure_chmod () {
46+ local mode=" $1 " path=" $2 "
47+ local parent owner=" "
48+ parent=" $( dirname -- " $path " ) "
49+ owner=" $( stat -c ' %U' " $parent " 2> /dev/null || true) "
50+ if [[ " $SKIP_USER_SETUP " != " 1" && " $owner " == " $SERVICE_USER " ]]; then
51+ runuser -u " $SERVICE_USER " -- chmod " $mode " " $path "
52+ else
53+ chmod " $mode " " $path "
54+ fi
55+ }
56+
2357target_path () {
2458 local relative=" ${1#/ } "
2559 if [[ " $INSTALL_ROOT " == " /" ]]; then
191225
192226# Install config example (not to the live filename).
193227install -d -m 0750 " $CONFIG_DIR "
228+
229+ # Keep the config dir root-owned per tmpfiles before writing into it.
230+ if [[ " $SKIP_USER_SETUP " != " 1" ]]; then
231+ chown -h " root:$SERVICE_GROUP " " $CONFIG_DIR "
232+ chmod 0750 " $CONFIG_DIR "
233+ fi
234+
194235if [[ -e " $PACKAGE_ROOT /config/devices.example.json" ]]; then
195236 install -m 0644 " $PACKAGE_ROOT /config/devices.example.json" \
196237 " $CONFIG_DIR /devices.json.example"
212253# the state directory exists before writing to it.
213254install -d -m 0700 " $STATE_DIR "
214255
256+ require_regular_file " $STATE_DIR /tokens.json"
215257if [[ ! -e " $STATE_DIR /tokens.json" ]]; then
216258 if [[ -e " $CONFIG_DIR /tokens.json" ]]; then
217259 printf ' %s\n' " >> Not creating $STATE_DIR /tokens.json: a token store already exists at"
218260 printf ' %s\n' " >> $CONFIG_DIR /tokens.json. The server reads it via the legacy fallback and warns."
219261 printf ' %s\n' " >> Migrate it deliberately, then remove the old copy:"
220262 printf ' %s\n' " >> install -m 0600 -o $SERVICE_USER -g $SERVICE_GROUP $CONFIG_DIR /tokens.json $STATE_DIR /tokens.json"
221263 printf ' %s\n' " >> rm $CONFIG_DIR /tokens.json"
264+ elif [[ " $SKIP_USER_SETUP " != " 1" ]]; then
265+ # Create the token store as the directory's owner.
266+ runuser -u " $SERVICE_USER " -- sh -c '
267+ umask 077
268+ dir=$1
269+ tmp=$(mktemp "$dir/.tokens.json.XXXXXX") || exit 1
270+ trap "rm -f \"\$tmp\"" EXIT
271+ printf "%s\n" "{\"version\":1,\"tokens\":[]}" >"$tmp"
272+ mv -fT "$tmp" "$dir/tokens.json"
273+ ' sh " $STATE_DIR "
222274 else
223- printf ' %s\n' ' {"version":1,"tokens":[]}' > " $STATE_DIR /tokens.json"
224- chmod 0600 " $STATE_DIR /tokens.json"
275+ tokens_tmp=" $( mktemp " $STATE_DIR /.tokens.json.XXXXXX" ) "
276+ printf ' %s\n' ' {"version":1,"tokens":[]}' > " $tokens_tmp "
277+ mv -fT " $tokens_tmp " " $STATE_DIR /tokens.json"
225278 fi
226279fi
227280
228281# Ensure tokens.json has 0600 even on upgrade.
229- chmod 0600 " $STATE_DIR /tokens.json"
282+ if [[ -e " $STATE_DIR /tokens.json" ]]; then
283+ require_regular_file " $STATE_DIR /tokens.json"
284+ secure_chmod 0600 " $STATE_DIR /tokens.json"
285+ fi
230286
231287# Warn if the old /etc location still exists — it may be a live file from
232288# before the /var/lib migration, or it may be a leftover decoy. Do not delete:
239295
240296# Create audit HMAC key if absent. Never regenerate on upgrade — a new key
241297# breaks verification of every prior record.
298+ require_regular_file " $CONFIG_DIR /audit-hmac.key"
242299if [[ ! -e " $CONFIG_DIR /audit-hmac.key" ]]; then
243- head -c 32 /dev/urandom | base64 > " $CONFIG_DIR /audit-hmac.key"
244- chmod 0600 " $CONFIG_DIR /audit-hmac.key"
300+ audit_key_tmp=" $( mktemp " $CONFIG_DIR /.audit-hmac.key.XXXXXX" ) "
301+ head -c 32 /dev/urandom | base64 > " $audit_key_tmp "
302+ mv -fT " $audit_key_tmp " " $CONFIG_DIR /audit-hmac.key"
245303fi
246304
247305# If devices.json exists, ensure it has 0600.
248306if [[ -e " $CONFIG_DIR /devices.json" ]]; then
249- chmod 0600 " $CONFIG_DIR /devices.json"
307+ require_regular_file " $CONFIG_DIR /devices.json"
308+ secure_chmod 0600 " $CONFIG_DIR /devices.json"
250309fi
251310
252311# Never clobber mutation-state.json — it holds change-set audit trail.
253312# Leave it exactly alone if it exists.
254313
255314if [[ " $SKIP_USER_SETUP " != " 1" ]]; then
256- chown " $SERVICE_USER : $SERVICE_GROUP " " $CONFIG_DIR "
315+ # Own the files inside the config dir. The directory stays root-owned.
257316 if [[ -e " $CONFIG_DIR /devices.json" ]]; then
258- chown " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /devices.json"
317+ require_regular_file " $CONFIG_DIR /devices.json"
318+ chown -h " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /devices.json"
259319 fi
260320 if [[ -e " $CONFIG_DIR /devices.json.example" ]]; then
261- chown " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /devices.json.example"
321+ require_regular_file " $CONFIG_DIR /devices.json.example"
322+ chown -h " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /devices.json.example"
262323 fi
263324 if [[ -e " $CONFIG_DIR /audit-hmac.key" ]]; then
264- chown " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /audit-hmac.key"
325+ require_regular_file " $CONFIG_DIR /audit-hmac.key"
326+ chown -h " $SERVICE_USER :$SERVICE_GROUP " " $CONFIG_DIR /audit-hmac.key"
265327 fi
266- # The state dir holds tokens.json, mutation-state.json, evidence files.
267- # Recursive ownership for everything under /var/lib.
328+ # Own everything under the state dir.
268329 chown -R " $SERVICE_USER :$SERVICE_GROUP " " $STATE_DIR " 2> /dev/null || true
269330fi
270331
0 commit comments