You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
__value=""# This is a global to hand a result back from __get_value_from_env
60
61
__free_space=0
61
62
__docker_dir="/var/lib/docker"
@@ -196,26 +197,106 @@ __handle_docker() {
196
197
}
197
198
198
199
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() {
200
237
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}')
202
248
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
+
forgin$(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
204
266
__as_owner="sudo -u ${__owner}"
205
267
fi
206
268
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
207
277
if [[ "${EUID}"-ne 0 ]];then
208
278
forgin$(id -nG);do
209
-
if [[ "${g}"=~ ^(sudo|admin)$ ]];then
279
+
if [[ "${g}"="${sudo_group}" ]];then
210
280
__auto_sudo="sudo"
211
-
found=1
281
+
__cannot_sudo=0
212
282
break
213
283
fi
214
284
done
285
+
else# root always can
286
+
__cannot_sudo=0
215
287
fi
216
288
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."
0 commit comments