summaryrefslogtreecommitdiff
path: root/localfixaccessrights
blob: 9cf156178bebfb8e3654e925e4d6f963142806a5 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localfixaccessrights
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localfixaccessrights,v 1.1 2008-03-20 16:36:53 jonas Exp $
  7. #
  8. # Adjust access rights to follow directory-based policy
  9. #
  10. # TODO: Implement options:
  11. # --help
  12. # --run-once
  13. # --init
  14. # --verbose
  15. # --debug
  16. #
  17. # TODO: Support overriding defaults in rc-file
  18. #
  19. # FIXME: Implement more of the local policy...
  20. #
  21. set -e
  22. PRG=$(basename "$0")
  23. showhelp() {
  24. cat <<EOF
  25. Usage: $PRG DIR [ DIR ... ]
  26. Examples:
  27. $PRG ~/private* ~/public*
  28. EOF
  29. }
  30. exit1() {
  31. echo >&2 "Error: $1"
  32. echo >&2 "Exiting..."
  33. exit 1
  34. }
  35. # Sanity checks
  36. for dir in "$@"; do
  37. [ -d "$dir" ] || exit1 "Directory \"$dir\" is not a directory"
  38. done
  39. getbasename() {
  40. basename "$1"
  41. }
  42. getbits() {
  43. ls -l "$1" | awk '{print $1}'
  44. }
  45. setprivate() {
  46. case "$(getbits "$1")" in
  47. drwx------)
  48. :
  49. ;;
  50. d*)
  51. chmod -f u=rwx,go= "$path" || true
  52. ;;
  53. -rw-------)
  54. :
  55. ;;
  56. -*)
  57. chmod -f u=rw,go= "$path" || true
  58. ;;
  59. esac
  60. }
  61. setpublic() {
  62. case "$(getbits "$1")" in
  63. drwxr?xr-x)
  64. :
  65. ;;
  66. d*)
  67. chmod -f u=rwx,g+rx,o=rx "$path" || true
  68. ;;
  69. -rw-r?-r--)
  70. :
  71. ;;
  72. -*)
  73. chmod -f u=rw,g+r,g-x,o=r "$path" || true
  74. ;;
  75. esac
  76. }
  77. fileschanged -r -s created,changed "$@" | while read path; do
  78. case "$path" in
  79. "$HOME"/public_images/*)
  80. case "$(getbasename "$path")" in
  81. .*)
  82. setprivate "$path"
  83. continue
  84. ;;
  85. esac
  86. setpublic "$path"
  87. continue
  88. ;;
  89. esac
  90. done
  91. exit 0