autoupdate 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/sh
  2. if test $(uci get autoupdater.@autoupdater[0].enabled) != 1; then
  3. echo "autoupdater is disabled"
  4. exit 0
  5. fi
  6. PROBABILITY=$(uci get autoupdater.@autoupdater[0].probability)
  7. if test "a$1" != "a-f"; then
  8. echo | awk "END{srand();exit rand() > $PROBABILITY}"
  9. if test $? -ne 0; then
  10. echo "No autoupdate this time. Use -f to override"
  11. exit 0
  12. fi
  13. fi
  14. BASE=$(uci get autoupdater.@autoupdater[0].url)
  15. PUBKEYS=$(uci get autoupdater.@autoupdater[0].pubkey)
  16. GOOD_SIGNATURES=$(uci get autoupdater.@autoupdater[0].good_signatures)
  17. BRANCH=$(uci get autoupdater.@autoupdater[0].branch)
  18. VERSION_FILE=/lib/gluon/release
  19. newer_than() {
  20. local old="$(printf '%s\n%s\n' "$1" "$2" | sort -n | head -n 1)"
  21. test "$1" != "$old"
  22. }
  23. cleanup() {
  24. rm -f $manifest
  25. rm -f $fw_image
  26. rm -f $manifest_upper
  27. rm -f $manifest_lower
  28. }
  29. trap cleanup INT TERM EXIT PIPE
  30. my_model="$(cat /tmp/sysinfo/model | sed 's/ /-/g' | tr '[A-Z]' '[a-z]')"
  31. case "$my_model" in
  32. "tl-wdr4300")
  33. case "$(tplink_get_hwid)" in
  34. "360000"*)
  35. my_model="tl-wdr3600"
  36. ;;
  37. esac
  38. ;;
  39. esac
  40. if [ ! -f "$VERSION_FILE" ]; then
  41. echo "Couldn't determine firmware version!" >&2
  42. exit 1
  43. fi
  44. my_version="$(cat "$VERSION_FILE")"
  45. fw_image=$(mktemp)
  46. manifest=$(mktemp)
  47. manifest_upper=$(mktemp)
  48. manifest_lower=$(mktemp)
  49. wget -O$manifest "$BASE"/manifest
  50. if test $? -ne 0; then
  51. echo "Couldn't fetch manifest" >&2
  52. exit 1
  53. fi
  54. seperator_line=$(cat $manifest|grep -n "^---$"|cut -d: -f1|head -n1)
  55. if test -z "$seperator_line"; then
  56. echo "Couldn't find --- marker!" >&2
  57. exit 1
  58. fi
  59. head -n$(($seperator_line-1)) $manifest > $manifest_upper
  60. tail -n+$(($seperator_line+1)) $manifest > $manifest_lower
  61. signatures=""
  62. while read sig; do
  63. echo "$sig" | grep -q "^[0-9a-f]\{128\}$"
  64. if test $? -ne 0; then
  65. continue
  66. fi
  67. signatures="$signatures -s $sig"
  68. done < $manifest_lower
  69. pubkeys=""
  70. for key in $PUBKEYS; do
  71. pubkeys="$pubkeys -p $key"
  72. done
  73. ecdsaverify -n $GOOD_SIGNATURES $pubkeys $signatures $manifest_upper
  74. if test $? -ne 0; then
  75. echo "Not enough valid signatures!" >&2
  76. exit 1
  77. fi
  78. grep -q "^BRANCH=${BRANCH}$" $manifest_upper
  79. if test $? -ne 0; then
  80. echo "Wrong branch. We'are on ${BRANCH}" >&2
  81. exit 1
  82. fi
  83. my_firmware=$(grep "^${my_model} " $manifest_upper)
  84. if test $? -ne 0; then
  85. echo "No matching firmware found (model ${my_model})" >&2
  86. exit 1
  87. fi
  88. fw_version=$(echo "${my_firmware}"|cut -d' ' -f2)
  89. fw_md5=$(echo "${my_firmware}"|cut -d' ' -f3)
  90. fw_file=$(echo "${my_firmware}"|cut -d' ' -f4)
  91. if newer_than "$fw_version" "$my_version"; then
  92. echo "New version available"
  93. wget -O$fw_image "${BASE}/${fw_file}"
  94. if test $? -ne 0; then
  95. echo "Error downloading image" >&2
  96. exit 1
  97. fi
  98. image_md5=$(md5sum "$fw_image"|cut -b-32)
  99. if test "$image_md5" != "$fw_md5"; then
  100. echo "Invalid image checksum" >&2
  101. exit 1
  102. fi
  103. echo "Upgrading firmware."
  104. sysupgrade "${fw_image}"
  105. else
  106. echo "No new firmware available"
  107. fi
  108. exit 0