From c06ba9fc9c4e43fd1822c591e85de56f56574bf5 Mon Sep 17 00:00:00 2001 From: Serghey Rodin Date: Thu, 5 Nov 2015 03:11:34 +0200 Subject: [PATCH] safe way to check permissions --- bin/v-check-fs-permission | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/bin/v-check-fs-permission b/bin/v-check-fs-permission index 59e491cf..00e5482a 100755 --- a/bin/v-check-fs-permission +++ b/bin/v-check-fs-permission @@ -5,10 +5,10 @@ # The function opens/reads files on the file system user=$1 -src_file=$2 +src=$2 # Checking arguments -if [ -z "$src_file" ]; then +if [ -z "$src" ]; then echo "Usage: USER FILE" exit 1 fi @@ -27,27 +27,19 @@ if [ -z $homedir ]; then fi # Checking path -if [ ! -z "$src_file" ]; then - rpath=$(readlink -f "$src_file") +if [ ! -z "$src" ]; then + rpath=$(readlink -f "$src") if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then - echo "Error: invalid source path $src_file" + echo "Error: invalid source path $user $src" exit 2 fi fi -# Reading file -#sudo -u $user cat "$src_file" 2>/dev/null -#if [ $? -ne 0 ]; then -# echo "Error: file $src_file was not opened" -# exit 3 -#fi - # Checking if file has readable permission -if [[ ! -r $src_file ]] -then -# echo "File is readable" -#else - echo "Cannot read file" +sudo -u $user ls "$src" > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Error: can't read $src" + exit 1 fi # Exiting