pkg_check 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/sh
  2. #
  3. # Package checksums checking script
  4. # (C) 2018 CZ.NIC, z.s.p.o.
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ERRFATAL="no"
  19. QUIET="yes"
  20. MISSING=""
  21. SUMMARY=""
  22. NL="
  23. "
  24. # Arguments parsing
  25. while expr "x$1" : "x-" > /dev/null; do
  26. if [ "x$1" = "x-s" ]; then
  27. ERRFATAL="yes"
  28. shift
  29. elif [ "x$1" = "x-v" ]; then
  30. QUIET=" no"
  31. shift
  32. else
  33. echo "Usage: $(basename $0) [-s] [-v] [pkg1 pkg2 ...]"
  34. echo
  35. echo " -s Stop on first change"
  36. echo " -v Verbose"
  37. if [ "x$1" = "x-h" ]; then
  38. exit 0
  39. else
  40. echo
  41. echo "ERROR: Unknown option '$1'"
  42. exit 1
  43. fi
  44. fi
  45. done
  46. # Check all packages by default
  47. if [ -z "$1" ]; then
  48. set $(cd /usr/lib/opkg/info/; for i in *.files-sha256sum; do basename $i .files-sha256sum; done)
  49. fi
  50. # Iterate over packages
  51. while [ "$1" ]; do
  52. if [ \! -f "/usr/lib/opkg/info/$1.files-sha256sum" ]; then
  53. if [ "$ERRFATAL" = no ]; then
  54. echo " * No checksums for $1 - skipping"
  55. echo
  56. else
  57. echo " * No checksums for $1 - exiting"
  58. exit 1
  59. fi
  60. if [ -z "$MISSING" ]; then
  61. MISSING="$1"
  62. else
  63. MISSING="$MISSING, $1"
  64. fi
  65. shift
  66. continue
  67. fi
  68. [ $QUIET = yes ] || echo " * Checking package $1:"
  69. ERR=""
  70. CHECK="`sha256sum -c /usr/lib/opkg/info/$1.files-sha256sum 2> /dev/null`"
  71. # Are the changed files config files?
  72. if [ $? -ne 0 ] && [ "`cat "/usr/lib/opkg/info/$1.files-sha256sum"`" ]; then
  73. NEWCHECK="`echo "$CHECK" | grep '^.*: OK$'`"
  74. for i in `echo "$CHECK" | sed -n 's|^\(.*\): FAILED$|\1|p'`; do
  75. if [ "`grep "^$i\$" "/usr/lib/opkg/info/$1.conffiles" 2> /dev/null`" ] || \
  76. [ "`echo "$i" | grep "^/etc/uci-defaults/"`" ]; then
  77. NEWCHECK="${NEWCHECK}${NL}${i}: CONFIGURED"
  78. else
  79. NEWCHECK="${NEWCHECK}${NL}${i}: FAILED"
  80. ERR="y"
  81. fi
  82. done
  83. CHECK="$NEWCHECK"
  84. fi
  85. # Do we have changed files or not?
  86. if [ -z "$ERR" ]; then
  87. [ $QUIET = yes ] || [ -z "`cat "/usr/lib/opkg/info/$1.files-sha256sum"`" ] || echo "$CHECK" | sed 's|^| - |'
  88. [ $QUIET = yes ] || echo " * Package $1 is ok"
  89. [ $QUIET = yes ] || echo
  90. else
  91. if [ $QUIET = yes ]; then
  92. echo " * Changes found in package $1:"
  93. echo "$CHECK" | sed -n 's|^\(.*:[[:blank:]]*FAILED\)$| - \1|p'
  94. else
  95. echo "$CHECK" | sed 's|^| - |'
  96. echo " * Changes found in package $1!"
  97. fi
  98. if [ "$ERRFATAL" = yes ]; then
  99. echo
  100. echo "Exiting on first change found!"
  101. exit 1
  102. fi
  103. for i in `echo "$CHECK" | sed -n 's|^\(.*\): FAILED$|\1|p'`; do
  104. SUMMARY="${SUMMARY}${NL} - $1: $i"
  105. done
  106. echo
  107. fi
  108. shift
  109. done
  110. # If there are changed files, report them
  111. if [ "$SUMMARY" ]; then
  112. echo "Some packages contain changed files!"
  113. echo "Maybe something worth looking into?"
  114. echo "Here is the list of packages and changed files:"
  115. echo "$SUMMARY"
  116. fi
  117. if [ "$MISSING" ]; then
  118. echo "Following packages are missing checksums: $MISSING"
  119. fi
  120. if [ "$MISSING" ] || [ "$SUMMARY" ]; then
  121. exit 1
  122. fi