script-files-permissions-set 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env bash
  2. # Smartly change permissions on selected directories
  3. # Copyright 2021 Bryan C. Roessler
  4. parent="${BASH_SOURCE[0]}"
  5. parent=${parent%/*}
  6. [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1
  7. is_root
  8. if [[ $# -eq 0 ]]; then
  9. echo "No arguments provided, using autodetection"
  10. paths=("$PWD")
  11. user=$(stat -c "%U" "$PWD")
  12. group=$(stat -c "%G" "$PWD")
  13. elif [[ $# -eq 1 ]]; then
  14. user="$1"
  15. group="$1"
  16. paths=("$PWD")
  17. elif [[ $# -eq 2 ]]; then
  18. user="$1"
  19. group="$2"
  20. paths=("$PWD")
  21. elif [[ $# -gt 2 ]]; then
  22. user="$1"
  23. group="$2"
  24. paths=("${@:3}")
  25. fi
  26. for path in "${paths[@]}"; do
  27. if [[ "$path" == "/" ]]; then
  28. echo "You are trying to operate on the root partition!"
  29. echo "This seems highly unusual!"
  30. ask_ok "Continue?" || exit $?
  31. fi
  32. og_user=$(stat -c "%U" "$path")
  33. og_group=$(stat -c "%G" "$path")
  34. echo -e "PATH\tUSER\tGROUP"
  35. echo -e "$path\t$og_user\t$og_group"
  36. if [[ "$group" != "smbgrp" || "$og_group" != "smbgrp" ]]; then
  37. echo "$path is not world accessible by the smbgrp group"
  38. ask_ok "Change $path group $og_group to smbgrp?" && group="smbgrp"
  39. fi
  40. done
  41. ask_ok "Apply user: $user and group: $group to ${paths[*]} and all subdirs?" && \
  42. chown -R "$user":"$group" "${paths[@]}"
  43. [[ "$group" == "smbgrp" ]] && mode=6775 || mode=755
  44. ask_ok "Apply chmod $mode to ${paths[*]} and all subdirs?" && \
  45. chmod -R $mode "${paths[@]}"
  46. # Let's do it in less steps (see above) for now unless it becomes a problem
  47. # echo "Apply setuid/setgid bits to ${paths[*]} and all subdirs?"
  48. # ask_ok "Files/dirs will inherit their " && \
  49. # chmod -R g+s,u+s "${paths[@]}"
  50. exit $?