v-check-fs-permission 894 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. # info: open file
  3. # options: USER FILE
  4. #
  5. # The function opens/reads files on the file system
  6. user=$1
  7. src=$2
  8. # Checking arguments
  9. if [ -z "$src" ]; then
  10. echo "Usage: USER FILE"
  11. exit 1
  12. fi
  13. # Checking vesta user
  14. if [ ! -e "$VESTA/data/users/$user" ]; then
  15. echo "Error: vesta user $user doesn't exist"
  16. exit 3
  17. fi
  18. # Checking user homedir
  19. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  20. if [ -z $homedir ]; then
  21. echo "Error: user home directory doesn't exist"
  22. exit 12
  23. fi
  24. # Checking path
  25. if [ ! -z "$src" ]; then
  26. rpath=$(readlink -f "$src")
  27. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  28. echo "Error: invalid source path $user $src"
  29. exit 2
  30. fi
  31. fi
  32. # Checking if file has readable permission
  33. sudo -u $user ls "$src" > /dev/null 2>&1
  34. if [ $? -ne 0 ]; then
  35. echo "Error: can't read $src"
  36. exit 1
  37. fi
  38. # Exiting
  39. exit