fix_pkg_perm.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. . /home/bus/bin/common_values.sh
  21. # This script takes two parameters, first is a tarball, and the second is the target directory it was extracted to.
  22. #when supplied with this information, this script updates the ownership and permissions on everything to be correct. This
  23. #is pretty much only done for things which live in /home/bus/bin/ and for special SSH related files and directores.
  24. #
  25. # See common_values.sh:
  26. # PACKAGE_*_PATTERN, PACKAGE_*_PERMISSIONS contain regexes to match each filespec from the
  27. # tarball against. If they match, the matching _PERMISSIONS variable holds the permissions that will
  28. # be set. Also, each file coming out of the tarball is given (with chown) to the user and group specified
  29. # by PACKAGE_OWNER_STRING.
  30. #
  31. # When called with parameters, it processes the tarball first, and even without parameters it will examine the environment
  32. #and iterate through any existing files defined in ALWAYS_*_LIST and apply PACKAGE_*_PERMISSIONS to them. This is an insurance
  33. #policy against forgotten execute bits and overly permissive settings on directories containing ssh authorized_keys or id_rsa
  34. #files which might cause ssh or sshd to refuse to allow us to make or accept connections (bye-bye updates!). Thus we want to
  35. #be extremely careful to prevent this snafu and have the scripts fix it right away if it occurs.
  36. tarball="$1"
  37. reldir="$2"
  38. olddir="`pwd`"
  39. pat_vars="`set | egrep '^PACKAGE_[A-Z_]+_PATTERN=' | sed -r 's/^PACKAGE_([A-Z_]+)_PATTERN=.*$/\1/'`"
  40. always_vars="`set | egrep '^ALWAYS_[A-Z_]+_LIST=' | sed -r 's/^ALWAYS_([A-Z_]+)_LIST=.*$/\1/'`"
  41. if [ -f "$olddir/$tarball" ]; then
  42. cd $reldir
  43. tar -ztf $olddir/$tarball |
  44. while read filespec; do
  45. if [ -n "$PACKAGE_OWNER_STRING" ]; then
  46. chown $PACKAGE_OWNER_STRING $filespec
  47. fi
  48. for i in $pat_vars; do
  49. patvar="PACKAGE_${i}_PATTERN";
  50. permvar="PACKAGE_${i}_PERMISSIONS";
  51. if (echo "$filespec" | egrep -q "${!patvar}"); then
  52. chmod ${!permvar} $filespec
  53. fi
  54. done
  55. done
  56. fi
  57. cd /
  58. for grp in $always_vars; do
  59. filesvar="ALWAYS_${grp}_LIST";
  60. permvar="PACKAGE_${grp}_PERMISSIONS";
  61. for fil in ${!filesvar}; do
  62. if [ -e "$fil" ]; then
  63. if [ -n "$PACKAGE_OWNER_STRING" ]; then
  64. chown $PACKAGE_OWNER_STRING $fil
  65. fi
  66. chmod ${!permvar} $fil
  67. fi
  68. done
  69. done
  70. cd $olddir