v-change-fs-file-permission 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # info: change file permission
  3. # options: USER FILE PERMISSIONS
  4. #
  5. # The function changes file access permissions on the file system
  6. user=$1
  7. src_file=$2
  8. permissions=$3
  9. # Checking arguments
  10. if [ -z "$permissions" ]; then
  11. echo "Usage: USER FILE PERMISSIONS"
  12. exit 1
  13. fi
  14. # Checking vesta user
  15. if [ ! -e "$VESTA/data/users/$user" ]; then
  16. echo "Error: vesta user $user doesn't exist"
  17. exit 3
  18. fi
  19. # Checking user homedir
  20. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  21. if [ -z $homedir ]; then
  22. echo "Error: user home directory doesn't exist"
  23. exit 12
  24. fi
  25. # Checking source file
  26. if [ ! -fe "$src_file" ]; then
  27. echo "Error: source file doesn't exist $src_file"
  28. exit 3
  29. fi
  30. # Checking source path
  31. rpath=$(readlink -f "$src_file")
  32. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  33. echo "Error: invalid source path $src_file"
  34. exit 2
  35. fi
  36. # Changing file permissions
  37. sudo -u $user chmod -R $permissions "$src_file" >/dev/null 2>&1
  38. if [ $? -ne 0 ]; then
  39. echo "Error: access permission on $src_file was not changed"
  40. exit 3
  41. fi
  42. # Exiting
  43. exit