-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_perms
More file actions
executable file
·506 lines (449 loc) · 17.8 KB
/
fix_perms
File metadata and controls
executable file
·506 lines (449 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/bin/bash
## Bash settings
set -uo pipefail
## Script globals
script_name=$(basename "${BASH_SOURCE[0]}")
script_dir=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd)
script_dir_abs=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd -P)
script_file="${script_dir}/${script_name}"
if [ -L "${BASH_SOURCE[0]}" ]; then
script_file_abs=$(readlink "${BASH_SOURCE[0]}")
else
script_file_abs="${script_dir_abs}/${script_name}"
fi
export CURRENT_PARENT_BASH_SCRIPT_FILE="$script_file"
script_args=("$@")
## Script imports
lib_dir="${script_dir}/../lib"
bash_functions_script="${lib_dir}/bash_script_func.sh"
## Source imports
source "$bash_functions_script"
## Arguments
octal_perms=''
chmod_mode_choices=( 'add' 'set' 'remove' )
chmod_mode_operator_arr=( '+' '=' '-' )
chmod_mode='set'
sticky_arg_choices=( 'off' 'files' 'folders' 'all' )
add_group_s='off' # typical default may be 'folders'
remove_group_s='off' # typical default may be 'files'
add_user_s='off'
remove_user_s='off'
add_file_x=false
remove_file_x=false
namepat_arr=()
identify=false
dryrun=false
fwd_args_arr=()
srcpath_arr=()
## Custom globals
find_type_arg_val=''
## Script usage
read -r -d '' script_usage << EOM
Usage: ${script_name} [OPTION]... OCTAL_PERMS PATH... ['find' OPTION]...
Run 'chmod' recursively on one or more directories (or files),
applying regular chmod octol permission settings.
Where normally 'chmod -R 775 <dir>' would give all files
and folders within the directory executable permission,
this script will convert the octal setting to a long-form
setting that only makes folders and NOT files executable.
Additionally, 'find' arguments may be provided to apply
permission changes more selectively than 'chmod -R'.
Provide the --dryrun option to first see what command will be
run under the hood without executing. Provide the --identify
option to list the files/folders that would have their perms
changed in a normal non-identify run.
Options:
-m,--mode={$(string_join '|' "${chmod_mode_choices[@]}")} (default=${chmod_mode})
How 'chmod' should apply permission changes.
--add-file-x
Add execute permissions to files when an applicable
OCTAL_PERMS setting is provided.
By default, only folders are given execute perms.
--remove-file-x
Remove all execute permissions from files.
--add-group-s={$(string_join '|' "${sticky_arg_choices[@]}")} (default=${add_group_s})
Set the 'setgid' attribute for folders/files.
For folders, having the 'setgid' attribute set means
that new files/subfolders created within will be
automatically assigned to the same group as the parent
directory, rather than the default ("primary") group of
the user who created the file/subfolder.
For executable files, having the 'setgid' attribute
set means that any user who is able to execute the file
will automatically execute the file with the privileges
of the file's group (even if the user is not part of
the file's group).
--remove-group-s={$(string_join '|' "${sticky_arg_choices[@]}")} (default=${remove_group_s})
Unset the 'setgid' attribute for files/folders.
--add-user-s={$(string_join '|' "${sticky_arg_choices[@]}")} (default=${add_user_s})
Set the 'setuid' attribute for files/folders.
For executable files, having the 'setuid' attribute
set means that any user who is able to execute the file
will automatically execute the file with the privileges
of the file's owner.
For folders, having the 'setuid' attribute set typically
means nothing on most UNIX and Linux systems, but may be
interpreted on some systems to automatically assign new
files/subfolders created within to the owner of the
parent folder.
--remove-user-s={$(string_join '|' "${sticky_arg_choices[@]}")} (default=${remove_user_s})
Unset the 'setuid' attribute for files/folders.
-n,--name=<pattern>
Include files/dirs that match this pattern.
This option can be provided multiple times to include
multiple file patterns.
-i,--identify
Print file/directory paths identified by 'find' commands,
but don't execute permission changes.
-db,--debug
-dr,--dryrun
Print command used to modify file/folder permissions,
without executing.
EOM
if (( $# < 1 )); then
echo_e "$script_usage"
exit_script_with_status 1
fi
## Parse arguments
set +u
parsing_fwd_args=false
while (( $# > 0 )); do
arg="$1"
if [ "$parsing_fwd_args" = true ]; then
# Accept critical script optional arguments
# specified at end of command.
is_fwd_arg=true
if [ "$(string_startswith "$arg" '-')" = true ]; then
arg_opt="$(string_lstrip "$arg" '-')"
is_fwd_arg=false
if [ "$arg_opt" = 'h' ] || [ "$arg_opt" = 'help' ]; then
arg_opt_nargs=0
echo "$script_usage"
exit 0
elif [ "$arg_opt" = 'dr' ] || [ "$arg_opt" = 'dryrun' ]; then
dryrun=true
else
is_fwd_arg=true
if [ "$(string_contains "$arg_opt" '=')" = true ]; then
arg_val=$(printf '%s' "${arg_opt#*=}" | sed -r -e "s|^['\"]+||" -e "s|['\"]+$||")
arg_opt="${arg_opt%%=*}"
else
arg_val="$2"
fi
# if [ "$arg_opt" = 'type' ] && [ "$arg_val" = 'd' ]; then
# if [ "$arg_opt" = 'type' ] && [ "$arg_val" = 'f' ]; then
if [ "$arg_opt" = 'type' ]; then
find_type_arg_val="$arg_val"
fi
fi
fi
if [ "$is_fwd_arg" = true ]; then
if [ "$(string_contains "$arg" '*')" = true ] || [ "$(string_contains "$arg" ' ')" = true ]; then
arg="'${arg}'"
fi
fwd_args_arr+=( "$arg" )
fi
elif [ "$(string_startswith "$arg" '-')" = false ]; then
if [ -z "$octal_perms" ]; then
octal_perms="$arg"
else
srcpath_arr+=( "$arg" )
fi
else
arg_opt="$(string_lstrip "$arg" '-')"
arg_opt_nargs=''
if [ "$(string_contains "$arg_opt" '=')" = true ]; then
arg_val=$(printf '%s' "${arg_opt#*=}" | sed -r -e "s|^['\"]+||" -e "s|['\"]+$||")
arg_opt="${arg_opt%%=*}"
arg_opt_nargs_do_shift=false
else
arg_val="$2"
arg_opt_nargs_do_shift=true
fi
arg_val_can_start_with_dash=false
if [ "$arg_opt" = 'h' ] || [ "$arg_opt" = 'help' ]; then
arg_opt_nargs=0
echo "$script_usage"
exit 0
elif [ "$arg_opt" = 'm' ] || [ "$arg_opt" = 'mode' ]; then
arg_opt_nargs=1
chmod_mode="$arg_val"
elif [ "$arg_opt" = 'add-file-x' ]; then
arg_opt_nargs=0
add_file_x=true
elif [ "$arg_opt" = 'remove-file-x' ]; then
arg_opt_nargs=0
remove_file_x=true
elif [ "$arg_opt" = 'add-group-s' ]; then
arg_opt_nargs=1
add_group_s="$arg_val"
elif [ "$arg_opt" = 'remove-group-s' ]; then
arg_opt_nargs=1
remove_group_s="$arg_val"
elif [ "$arg_opt" = 'add-user-s' ]; then
arg_opt_nargs=1
add_user_s="$arg_val"
elif [ "$arg_opt" = 'remove-user-s' ]; then
arg_opt_nargs=1
remove_user_s="$arg_val"
elif [ "$arg_opt" = 'n' ] || [ "$arg_opt" = 'name' ]; then
arg_opt_nargs=1
namepat_arr+=( "$arg_val" )
elif [ "$arg_opt" = 'i' ] || [ "$arg_opt" = 'identify' ]; then
arg_opt_nargs=0
identify=true
elif [ "$arg_opt" = 'db' ] || [ "$arg_opt" = 'debug' ]; then
arg_opt_nargs=0
dryrun=true
elif [ "$arg_opt" = 'dr' ] || [ "$arg_opt" = 'dryrun' ]; then
arg_opt_nargs=0
dryrun=true
else
arg_opt_nargs=0
if (( ${#srcpath_arr[@]} >= 1 )); then
parsing_fwd_args=true
continue
else
echo_e "Unexpected argument: ${arg}"
exit_script_with_status 1
fi
fi
if [ -z "$arg_opt_nargs" ]; then
echo_e "Developer error! "'$arg_opt_nargs'" was not set for argument: ${arg}"
exit_script_with_status 1
fi
if [ "$arg_opt_nargs_do_shift" = true ] && (( arg_opt_nargs >= 1 )); then
for arg_num in $(seq 1 $arg_opt_nargs); do
shift
arg_val="$1"
if [ -z "$arg_val" ]; then
echo_e "Missing expected value (#${arg_num}) for argument: ${arg}"
exit_script_with_status 1
elif [ "$arg_val_can_start_with_dash" = false ] && [ "$(string_startswith "$arg_val" '-')" = true ]; then
echo_e "Unexpected argument value: ${arg} ${arg_val}"
exit_script_with_status 1
fi
done
fi
fi
shift
done
set -u
## Validate arguments
if [ "$(string_is_posint_or_zero "$octal_perms")" = true ] && { (( ${#octal_perms} == 3 )) || (( ${#octal_perms} == 4 )); }; then
:
else
echo_e "OCTAL_PERMS must be a 3-digit (or 4-digit) octal number (accepted by 'chmod')"
exit_script_with_status 1
fi
if [ "$(itemOneOf "$chmod_mode" "${chmod_mode_choices[@]}")" = false ]; then
echo_e "--mode must be one of the following: ${chmod_mode_choices[*]}"
exit_script_with_status 1
fi
if [ "$(itemOneOf "$add_group_s" "${sticky_arg_choices[@]}")" = false ]; then
echo_e "--add-group-s must be one of the following: ${sticky_arg_choices[*]}"
exit_script_with_status 1
fi
if [ "$(itemOneOf "$remove_group_s" "${sticky_arg_choices[@]}")" = false ]; then
echo_e "--remove-group-s must be one of the following: ${sticky_arg_choices[*]}"
exit_script_with_status 1
fi
if [ "$(itemOneOf "$add_user_s" "${sticky_arg_choices[@]}")" = false ]; then
echo_e "--add-user-s must be one of the following: ${sticky_arg_choices[*]}"
exit_script_with_status 1
fi
if [ "$(itemOneOf "$add_user_s" "${sticky_arg_choices[@]}")" = false ]; then
echo_e "--remove-user-s must be one of the following: ${sticky_arg_choices[*]}"
exit_script_with_status 1
fi
if [ "$add_file_x" = true ] && [ "$remove_file_x" = true ]; then
echo_e "--set-file-x and --unset-file-x options conflict"
exit_script_with_status 1
fi
if [ "$add_group_s" != 'off' ] && [ "$remove_group_s" != 'off' ] \
&& { [ "$add_group_s" = "$remove_group_s" ] || [ "$add_group_s" = 'all' ] || [ "$remove_group_s" = 'all' ]; }; then
echo_e "--add-group-s and --remove-group-s options conflict"
exit_script_with_status 1
fi
if [ "$add_user_s" != 'off' ] && [ "$remove_user_s" != 'off' ] \
&& { [ "$add_user_s" = "$remove_user_s" ] || [ "$add_user_s" = 'all' ] || [ "$remove_user_s" = 'all' ]; }; then
echo_e "--add-user-s and --remove-user-s options conflict"
exit_script_with_status 1
fi
if (( ${#srcpath_arr[@]} == 0 )); then
echo_e "At least one source path argument must be provided"
exit_script_with_status 1
fi
for srcpath in "${srcpath_arr[@]}"; do
if [ ! -e "$srcpath" ]; then
echo_e "Source path does not exist: ${srcpath}"
exit_script_with_status 1
fi
done
# Build -name arguments to give to 'find' command
find_name_args=''
if (( ${#namepat_arr[@]} > 0 )); then
find_name_args="\("
for i in "${!namepat_arr[@]}"; do
if (( i == 0 )); then
find_name_args="${find_name_args} -name '${namepat_arr[i]}'"
else
find_name_args="${find_name_args} -o -name '${namepat_arr[i]}'"
fi
done
find_name_args="${find_name_args} \)"
fi
find_args="${fwd_args_arr[*]+${fwd_args_arr[*]}} ${find_name_args}"
find_args=$(string_strip "$find_args")
# Handle 4-digit OCTAL_PERMS
provided_octal_perms="$octal_perms"
if (( ${#octal_perms} == 4 )); then
octal_perms="${octal_perms:1:3}"
fi
# Convert octal permission setting to RWX format
chmod_perms_from_octal=''
i_chmod_mode=$(indexOf "$chmod_mode" "${chmod_mode_choices[@]}")
chmod_mode_operator="${chmod_mode_operator_arr[$i_chmod_mode]}"
for (( i=0; i<${#octal_perms}; i++ )); do
octal_digit="${octal_perms:$i:1}"
if (( i == 0 )); then
perm_group='u'
elif (( i == 1 )); then
perm_group='g'
elif (( i == 2 )); then
perm_group='o'
fi
octal_rwx=$(chmod_octal_digit_to_rwx "$octal_digit")
if [ -z "$octal_rwx" ] && [ "$chmod_mode_operator" != '=' ]; then
:
else
chmod_perms_from_octal="${chmod_perms_from_octal},${perm_group}${chmod_mode_operator}${octal_rwx}"
fi
done
chmod_perms_from_octal=$(string_lstrip "$chmod_perms_from_octal" ',')
if [ "$add_file_x" = false ]; then
chmod_perms_from_octal="${chmod_perms_from_octal//x/X}"
fi
chmod_perms_before_sticky="$chmod_perms_from_octal"
# Handle 4-digit OCTAL_PERMS
if (( ${#provided_octal_perms} == 4 )); then
octal_first_digit="${octal_perms:0:1}"
if (( octal_first_digit == 0 )); then
:
elif (( octal_first_digit == 2 )); then
chmod_perms_from_octal="${chmod_perms_from_octal},g+s"
if [ "$add_group_s" = 'off' ]; then
add_group_s='folders'
fi
elif (( octal_first_digit == 4 )); then
chmod_perms_from_octal="${chmod_perms_from_octal},u+s"
if [ "$add_user_s" = 'off' ]; then
add_user_s='files'
fi
elif (( octal_first_digit == 6 )); then
chmod_perms_from_octal="${chmod_perms_from_octal},g+s"
chmod_perms_from_octal="${chmod_perms_from_octal},u+s"
if [ "$add_group_s" = 'off' ]; then
add_group_s='folders'
fi
if [ "$add_user_s" = 'off' ]; then
add_user_s='files'
fi
else
echo_e "First digit in a 4-digit OCTAL_PERMS setting must be one of the following: 0 2 4 6"
exit_script_with_status 1
fi
fi
# Handle user/group sticky bit settings
chmod_perms_files="$chmod_perms_before_sticky"
chmod_perms_folders="$chmod_perms_before_sticky"
if [ "$remove_file_x" = true ]; then
chmod_perms_files="${chmod_perms_files//x/}"
chmod_perms_files="${chmod_perms_files//X/}"
fi
if [ "$add_user_s" != 'off' ]; then
if [ "$add_user_s" = 'files' ] || [ "$add_user_s" = 'all' ]; then
chmod_perms_files="${chmod_perms_files},u+s"
fi
if [ "$add_user_s" = 'folders' ] || [ "$add_user_s" = 'all' ]; then
chmod_perms_folders="${chmod_perms_folders},u+s"
fi
fi
if [ "$remove_user_s" != 'off' ]; then
if [ "$remove_user_s" = 'files' ] || [ "$remove_user_s" = 'all' ]; then
chmod_perms_files="${chmod_perms_files},u-s"
fi
if [ "$remove_user_s" = 'folders' ] || [ "$remove_user_s" = 'all' ]; then
chmod_perms_folders="${chmod_perms_folders},u-s"
fi
fi
if [ "$add_group_s" != 'off' ]; then
if [ "$add_group_s" = 'files' ] || [ "$add_group_s" = 'all' ]; then
chmod_perms_files="${chmod_perms_files},g+s"
fi
if [ "$add_group_s" = 'folders' ] || [ "$add_group_s" = 'all' ]; then
chmod_perms_folders="${chmod_perms_folders},g+s"
fi
fi
if [ "$remove_group_s" != 'off' ]; then
if [ "$remove_group_s" = 'files' ] || [ "$remove_group_s" = 'all' ]; then
chmod_perms_files="${chmod_perms_files},g-s"
fi
if [ "$remove_group_s" = 'folders' ] || [ "$remove_group_s" = 'all' ]; then
chmod_perms_folders="${chmod_perms_folders},g-s"
fi
fi
# Summarize permission settings to be applied
set_files_and_folders_separate=false
echo "Converted octal permission setting to 'chmod' setting: ${provided_octal_perms} -> ${chmod_perms_from_octal}"
if [ "$find_type_arg_val" = 'f' ]; then
echo "Permission setting to be applied to files: ${chmod_perms_files}"
elif [ "$find_type_arg_val" = 'd' ]; then
chmod_perms_folders="${chmod_perms_folders//X/x}"
echo "Permission setting to be applied to folders: ${chmod_perms_folders}"
elif [ "$chmod_perms_files" != "$chmod_perms_folders" ]; then
set_files_and_folders_separate=true
chmod_perms_folders="${chmod_perms_folders//X/x}"
echo
echo "Two passes of the 'find' program will be used to set different perms for files and folders"
echo "Permission setting for folders: ${chmod_perms_folders}"
echo "Permission setting for files: ${chmod_perms_files}"
else
echo "Permission setting to be applied to files and folders: ${chmod_perms_files}"
fi
echo
## Main program
for srcpath in "${srcpath_arr[@]}"; do
cmd2=''
if [ -n "$find_args" ] || [ "$identify" = true ] || [ "$set_files_and_folders_separate" = true ]; then
if [ "$identify" = true ]; then
cmd1="find \"${srcpath}\" ${find_args}"
elif [ "$find_type_arg_val" = 'f' ]; then
cmd1="find \"${srcpath}\" ${find_args} -exec chmod ${chmod_perms_files} {} +"
elif [ "$find_type_arg_val" = 'd' ]; then
cmd1="find \"${srcpath}\" ${find_args} -exec chmod ${chmod_perms_folders} {} +"
elif [ "$set_files_and_folders_separate" = true ]; then
cmd1="find \"${srcpath}\" -type d ${find_args} -exec chmod ${chmod_perms_folders} {} +"
cmd2="find \"${srcpath}\" -type f ${find_args} -exec chmod ${chmod_perms_files} {} +"
else
cmd1="find \"${srcpath}\" ${find_args} -exec chmod ${chmod_perms_files} {} +"
fi
else
cmd1="chmod -R ${chmod_perms_files} \"${srcpath}\""
fi
echo "Fixing perms in ${srcpath}"
if [ "$dryrun" = true ]; then
echo "$cmd1"
if [ -n "$cmd2" ]; then
echo "$cmd2"
fi
else
eval "$cmd1"
if [ -n "$cmd2" ]; then
eval "$cmd2"
fi
fi
done
if [ "$dryrun" = false ]; then
echo "Done!"
fi