/
test-writable
executable file
·49 lines (40 loc) · 1.11 KB
/
test-writable
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
#!/bin/bash
readonly service_cfg=${0%/scripts*}/etc/service.cfg
. ${service_cfg}
readonly log_file=${service_log}
readonly log_facility="${service}"
. ${scripts_base}/common
check_params "target user" $0 $@
readonly target=$1
readonly user=$2
# http://stackoverflow.com/a/14104522
# http://stackoverflow.com/a/14318764
# Use -L to get information about the target of a symlink,
# not the link itself, as pointed out in the comments
target_info=( $(stat -Lc "0%a %G %U" ${target}) )
perm="${target_info[0]}"
group="${target_info[1]}"
owner="${target_info[2]}"
access=1
readonly aw=$((${perm}&0002))
readonly gw=$((${perm}&0020))
readonly uw=$((${perm}&0200))
if [ ${aw} -ne 0 ]; then
# Everyone has write access
access=0
elif [ ${gw} -ne 0 ]; then
# Some group has write access.
# Is user in that group?
gs=( $(groups ${user}) )
for g in "${gs[@]}"; do
if [[ ${group} == ${g} ]]; then
access=0
break
fi
done
elif [ ${uw} -ne 0 ]; then
# The owner has write access.
# Does the user own the file?
[[ ${user} == ${owner} ]] && access=0
fi
exit ${access}