Skip to content

Commit c06ba9f

Browse files
committed
safe way to check permissions
1 parent a0ee9bb commit c06ba9f

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

bin/v-check-fs-permission

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# The function opens/reads files on the file system
66

77
user=$1
8-
src_file=$2
8+
src=$2
99

1010
# Checking arguments
11-
if [ -z "$src_file" ]; then
11+
if [ -z "$src" ]; then
1212
echo "Usage: USER FILE"
1313
exit 1
1414
fi
@@ -27,27 +27,19 @@ if [ -z $homedir ]; then
2727
fi
2828

2929
# Checking path
30-
if [ ! -z "$src_file" ]; then
31-
rpath=$(readlink -f "$src_file")
30+
if [ ! -z "$src" ]; then
31+
rpath=$(readlink -f "$src")
3232
if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
33-
echo "Error: invalid source path $src_file"
33+
echo "Error: invalid source path $user $src"
3434
exit 2
3535
fi
3636
fi
3737

38-
# Reading file
39-
#sudo -u $user cat "$src_file" 2>/dev/null
40-
#if [ $? -ne 0 ]; then
41-
# echo "Error: file $src_file was not opened"
42-
# exit 3
43-
#fi
44-
4538
# Checking if file has readable permission
46-
if [[ ! -r $src_file ]]
47-
then
48-
# echo "File is readable"
49-
#else
50-
echo "Cannot read file"
39+
sudo -u $user ls "$src" > /dev/null 2>&1
40+
if [ $? -ne 0 ]; then
41+
echo "Error: can't read $src"
42+
exit 1
5143
fi
5244

5345
# Exiting

0 commit comments

Comments
 (0)