Skip to content

Commit 1ff5413

Browse files
committed
Better handling for a multi-user setup
1 parent 07803ba commit 1ff5413

1 file changed

Lines changed: 107 additions & 16 deletions

File tree

ethd

Lines changed: 107 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ __cannot_sudo=0
5656
__as_owner=""
5757
__owner=""
5858
__owner_group=""
59+
__group_can_write=0
5960
__value="" # This is a global to hand a result back from __get_value_from_env
6061
__free_space=0
6162
__docker_dir="/var/lib/docker"
@@ -196,26 +197,106 @@ __handle_docker() {
196197
}
197198

198199

199-
__handle_root() {
200+
# Allow users to set their own umask like 077, and make it x0x if we are going to rely on group write
201+
__adjust_umask_for_group() {
202+
local current_umask
203+
local new_umask
204+
205+
current_umask=$(umask)
206+
207+
# split into 3 or 4 digits safely, set group to 0
208+
case ${#current_umask} in
209+
3) # ugo form
210+
u=${current_umask:0:1}
211+
g=0
212+
o=${current_umask:2:1}
213+
new_umask="${u}${g}${o}"
214+
;;
215+
4) # sugo form
216+
s=${current_umask:0:1}
217+
u=${current_umask:1:1}
218+
g=0
219+
o=${current_umask:3:1}
220+
new_umask="${s}${u}${g}${o}"
221+
;;
222+
esac
223+
224+
umask "${new_umask}"
225+
}
226+
227+
228+
# Who owns the directory, what permissions do they have, and do we need to sudo -u ${__owner} when creating files
229+
# Also, can we sudo
230+
# Assume owner has rw on everything
231+
# EUID is owner of directory: No umask adjustment, no sudo
232+
# EUID is not owner of directory, but directory is group-writeable and EUID is in the group that can write:
233+
# change umask to not touch group perms
234+
# Neither: sudo -u ${__owner}
235+
# __owner and __owner_group are set at script entry and have the user:group owners of the directory
236+
__handle_ownership() {
200237
local g
201-
local found=0
238+
local can_sudo=0
239+
local is_in_group=0
240+
local perms
241+
local group_w
242+
local sudo_group
243+
local reason_msg=""
244+
local action_msg=""
245+
246+
# shellcheck disable=SC2012
247+
perms=$(ls -ld . | awk '{print $1}')
202248

203-
if [[ "${EUID}" != $(id -u "${__owner}") ]]; then
249+
group_w="${perms:5:1}"
250+
if [[ "${group_w}" != "-" ]]; then
251+
__group_can_write=1
252+
fi
253+
254+
for g in $(id -nG); do
255+
if [[ "${g}" = "${__owner_group}" ]]; then
256+
is_in_group=1
257+
break
258+
fi
259+
done
260+
261+
if [[ "${EUID}" = $(id -u "${__owner}") ]]; then # No adjustments needed
262+
__as_owner=""
263+
elif [[ "${__group_can_write}" -eq 1 && "${is_in_group}" -eq 1 ]]; then # Adjust umask to make sure files are group-writable
264+
__adjust_umask_for_group
265+
else
204266
__as_owner="sudo -u ${__owner}"
205267
fi
206268

269+
if [[ "$OSTYPE" = "darwin"* ]]; then
270+
sudo_group="admin"
271+
else
272+
sudo_group="sudo"
273+
fi
274+
275+
# Figure out whether the user can sudo
276+
__cannot_sudo=1
207277
if [[ "${EUID}" -ne 0 ]]; then
208278
for g in $(id -nG); do
209-
if [[ "${g}" =~ ^(sudo|admin)$ ]]; then
279+
if [[ "${g}" = "${sudo_group}" ]]; then
210280
__auto_sudo="sudo"
211-
found=1
281+
__cannot_sudo=0
212282
break
213283
fi
214284
done
285+
else # root always can
286+
__cannot_sudo=0
215287
fi
216288

217-
if [[ "${EUID}" -ne 0 && "${found}" -eq 0 ]]; then
218-
__cannot_sudo=1
289+
if [[ -n "${__as_owner}" && "${__cannot_sudo}" -eq 1 ]]; then # Have to sudo but can't
290+
if [[ "${is_in_group}" -eq 1 ]]; then
291+
reason_msg="and all of its groups "
292+
action_msg="or give write permissions to ${__owner_group}, "
293+
fi
294+
echo "The $(dirname "$(realpath "${BASH_SOURCE[0]}")") directory is owned by ${__owner}, the script runs as $(id -nu), and that user ${reason_msg}cannot write files in \
295+
this directory, nor run \"sudo\"."
296+
echo "This means ${__me} cannot modify or create files, which keeps it from working."
297+
echo "Please run ${__me} as ${__owner}, ${action_msg}or make $(id -nu) part of the ${sudo_group} group."
298+
echo "Aborting."
299+
exit 1
219300
fi
220301
}
221302

@@ -473,6 +554,8 @@ __check_compose_version() {
473554

474555

475556
__prep_conffiles() {
557+
local env
558+
476559
# Create prometheus.yml if it doesn't exist
477560
if [[ ! -f ./prometheus/prometheus.yml ]]; then
478561
${__as_owner} cp ./prometheus/prometheus.yml.sample ./prometheus/prometheus.yml
@@ -583,17 +666,25 @@ if find .eth/dkg_output \( \! -user "${__owner}" -o \! -group "${__owner_group}"
583666
${__as_owner} chmod o+rx siren/*.sh
584667
fi
585668

586-
# Make sure local user owns .env
587-
if find . -name .env \( \! -user "${__owner}" -o \! -group "${__owner_group}" \) -print -quit | grep -q ./.env; then
669+
# Make sure directory owner owns .env, or it's owned by directory group and group is writable
670+
if find . -maxdepth 1 -name "${__env_file}" -user "${__owner}" -print -quit | grep -q "${__env_file}" \
671+
|| { find . -maxdepth 1 -name "${__env_file}" -group "${__owner_group}" -print -quit | grep -q "${__env_file}" \
672+
&& [[ "${__group_can_write}" -eq 1 ]]; }; then
673+
true # Nothing to do
674+
else
588675
if [[ "${__cannot_sudo}" -eq 0 ]]; then
589-
echo "Fixing ownership of .env"
590-
${__auto_sudo} chown -R "${__owner}:${__owner_group}" .env
591-
${__auto_sudo} chmod -R 644 .env
592-
if [[ -f .env.tmp ]]; then
593-
${__auto_sudo} rm -f .env.tmp
676+
echo "Fixing ownership of ${__env_file}"
677+
${__auto_sudo} chown -R "${__owner}:${__owner_group}" "${__env_file}"
678+
if [[ ${__group_can_write} -eq 1 ]]; then
679+
${__auto_sudo} chmod og+rw "${__env_file}"
680+
else
681+
${__auto_sudo} chmod o+rw "${__env_file}"
682+
fi
683+
if [[ -f ${__env_file}.tmp ]]; then
684+
${__auto_sudo} rm -f "${__env_file}".tmp
594685
fi
595686
else
596-
echo "Ownership of .env should be fixed, but this user can't sudo"
687+
echo "Ownership of ${__env_file} should be fixed, but this user can't sudo"
597688
fi
598689
fi
599690
}
@@ -6127,7 +6218,7 @@ __command="$1"
61276218
shift
61286219
__params=$*
61296220

6130-
__handle_root
6221+
__handle_ownership
61316222
__determine_distro
61326223
__prep_conffiles
61336224

0 commit comments

Comments
 (0)