v-open-fs-file 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_file=$2
  8. # Checking arguments
  9. if [ -z "$src_file" ]; 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_file" ]; then
  26. rpath=$(readlink -f "$src_file")
  27. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  28. echo "Error: invalid source path $src_file"
  29. exit 2
  30. fi
  31. if [ ! -f "$src_file" ]; then
  32. echo "Error: file not found $src_file"
  33. exit 2
  34. fi
  35. fi
  36. # Reading file
  37. sudo -u $user cat "$src_file" 2>/dev/null
  38. if [ $? -ne 0 ]; then
  39. echo "Error: file $src_file was not opened"
  40. exit 3
  41. fi
  42. # Exiting
  43. exit