freedombone-syncthing 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Checks for changed syncthing device IDs within user home directories
  12. # and then recreates the syncthing configuration file accordingly
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. NO_OF_ARGS=$#
  32. PROJECT_NAME='freedombone'
  33. export TEXTDOMAIN=$PROJECT_NAME-syncthing
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  36. for f in $UTILS_FILES
  37. do
  38. source "$f"
  39. done
  40. # File which keeps track of what has already been installed
  41. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  42. SYNCTHING_ID=
  43. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  44. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  45. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  46. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  47. SYNCTHING_PORT=22000
  48. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  49. SYNCTHING_USER_IDS_FILE='.syncthingids'
  50. SYNCTHING_UPDATE_FILE='.syncthing-update'
  51. CHANGED=
  52. TEMP_IDS_FILE=/root/.synthingids
  53. function remove_user_syncthing {
  54. remove_username="$1"
  55. sed -i "/<folder id=\"${remove_username}\" /,/</folder>/d" $SYNCTHING_CONFIG_FILE
  56. systemctl restart syncthing
  57. }
  58. function new_syncthing_id {
  59. for i in {1..8}
  60. do
  61. v=""
  62. # shellcheck disable=SC2034
  63. for j in {1..2}
  64. do
  65. v2=$(echo "obase=16;$RANDOM" | bc)
  66. v=$v$v2
  67. done
  68. v=$(echo "$v" | cut -c1-7)
  69. if [ "${i}" -lt 8 ]; then
  70. v=$v"-"
  71. fi
  72. echo -n "$v"
  73. done
  74. echo "$v"
  75. }
  76. function create_syncthing_config {
  77. if grep -q "syncthing ID" "$COMPLETION_FILE"; then
  78. SYNCTHING_ID=$(get_completion_param "syncthing ID")
  79. else
  80. if [ -f $SYNCTHING_CONFIG_FILE ]; then
  81. SYNCTHING_ID=$(grep "device id=" "$SYNCTHING_CONFIG_FILE" | head -n 1 | awk -F '"' '{print $2}')
  82. else
  83. SYNCTHING_ID=$(new_syncthing_id)
  84. fi
  85. fi
  86. set_completion_param "syncthing ID" "$SYNCTHING_ID"
  87. if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
  88. mkdir -p $SYNCTHING_CONFIG_PATH
  89. fi
  90. if [ ! -d $SYNCTHING_SHARED_DATA ]; then
  91. mkdir -p $SYNCTHING_SHARED_DATA
  92. fi
  93. echo '<configuration version="12">' > $SYNCTHING_CONFIG_FILE
  94. for d in /home/*/ ; do
  95. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  96. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  97. echo " <folder id=\"$USERNAME\" path=\"/home/$USERNAME/Sync/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  98. # include any specified device IDs for this user
  99. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  100. echo "" > $TEMP_IDS_FILE
  101. while read -r line || [[ -n "$line" ]]; do
  102. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  103. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  104. if [ ${#line2} -gt 10 ]; then
  105. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  106. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  107. echo "$line2" >> $TEMP_IDS_FILE
  108. fi
  109. fi
  110. fi
  111. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  112. rm $TEMP_IDS_FILE
  113. fi
  114. { echo " <device id=\"$SYNCTHING_ID\"></device>";
  115. echo ' <minDiskFreePct>1</minDiskFreePct>';
  116. echo ' <versioning></versioning>';
  117. echo ' <copiers>0</copiers>';
  118. echo ' <pullers>0</pullers>';
  119. echo ' <hashers>0</hashers>';
  120. echo ' <order>random</order>';
  121. echo ' <ignoreDelete>false</ignoreDelete>';
  122. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
  123. echo ' <pullerSleepS>0</pullerSleepS>';
  124. echo ' <pullerPauseS>0</pullerPauseS>';
  125. echo ' <maxConflicts>10</maxConflicts>';
  126. echo ' <disableSparseFiles>false</disableSparseFiles>';
  127. echo ' </folder>'; } >> "$SYNCTHING_CONFIG_FILE"
  128. fi
  129. done
  130. echo " <folder id=\"shared\" path=\"$SYNCTHING_SHARED_DATA/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  131. # all user devices may access this shared directory
  132. echo "" > $TEMP_IDS_FILE
  133. for d in /home/*/ ; do
  134. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  135. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  136. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  137. while read -r line || [[ -n "$line" ]]; do
  138. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  139. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  140. if [ ${#line2} -gt 10 ]; then
  141. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  142. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  143. echo "$line2" >> $TEMP_IDS_FILE
  144. fi
  145. fi
  146. fi
  147. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  148. fi
  149. fi
  150. done
  151. rm $TEMP_IDS_FILE
  152. { echo " <device id=\"$SYNCTHING_ID\"></device>";
  153. echo ' <minDiskFreePct>1</minDiskFreePct>';
  154. echo ' <versioning></versioning>';
  155. echo ' <copiers>0</copiers>';
  156. echo ' <pullers>0</pullers>';
  157. echo ' <hashers>0</hashers>';
  158. echo ' <order>random</order>';
  159. echo ' <ignoreDelete>false</ignoreDelete>';
  160. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
  161. echo ' <pullerSleepS>0</pullerSleepS>';
  162. echo ' <pullerPauseS>0</pullerPauseS>';
  163. echo ' <maxConflicts>10</maxConflicts>';
  164. echo ' <disableSparseFiles>false</disableSparseFiles>';
  165. echo ' </folder>';
  166. echo " <device id=\"$SYNCTHING_ID\" name=\"${PROJECT_NAME}\" compression=\"metadata\" introducer=\"false\">";
  167. echo ' <address>dynamic</address>';
  168. echo ' </device>'; } >> "$SYNCTHING_CONFIG_FILE"
  169. echo "" > $TEMP_IDS_FILE
  170. for d in /home/*/ ; do
  171. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  172. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  173. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  174. while read -r line || [[ -n "$line" ]]; do
  175. line2="$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  176. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  177. if [ ${#line2} -gt 10 ]; then
  178. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  179. echo " <device id=\"$line2\" name=\"${USERNAME}\" compression=\"metadata\" introducer=\"false\">" >> "$SYNCTHING_CONFIG_FILE"
  180. echo ' <address>dynamic</address>' >> $SYNCTHING_CONFIG_FILE
  181. echo ' </device>' >> $SYNCTHING_CONFIG_FILE
  182. echo "$line2" >> $TEMP_IDS_FILE
  183. fi
  184. fi
  185. fi
  186. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  187. fi
  188. fi
  189. done
  190. rm $TEMP_IDS_FILE
  191. { echo ' <options>';
  192. echo " <listenAddress>tcp://0.0.0.0:$SYNCTHING_PORT</listenAddress>";
  193. echo ' <globalAnnounceServer>default</globalAnnounceServer>';
  194. echo ' <globalAnnounceEnabled>true</globalAnnounceEnabled>';
  195. echo ' <localAnnounceEnabled>true</localAnnounceEnabled>';
  196. echo ' <localAnnouncePort>21027</localAnnouncePort>';
  197. echo ' <localAnnounceMCAddr>[ff12::8384]:21027</localAnnounceMCAddr>';
  198. echo " <relayServer>dynamic+$SYNCTHING_RELAY_SERVER</relayServer>";
  199. echo ' <maxSendKbps>0</maxSendKbps>';
  200. echo ' <maxRecvKbps>0</maxRecvKbps>';
  201. echo ' <reconnectionIntervalS>60</reconnectionIntervalS>';
  202. echo ' <relaysEnabled>true</relaysEnabled>';
  203. echo ' <relayReconnectIntervalM>10</relayReconnectIntervalM>';
  204. echo ' <startBrowser>true</startBrowser>';
  205. echo ' <upnpEnabled>true</upnpEnabled>';
  206. echo ' <upnpLeaseMinutes>60</upnpLeaseMinutes>';
  207. echo ' <upnpRenewalMinutes>30</upnpRenewalMinutes>';
  208. echo ' <upnpTimeoutSeconds>10</upnpTimeoutSeconds>';
  209. echo ' <urAccepted>-1</urAccepted>';
  210. echo ' <urUniqueID></urUniqueID>';
  211. echo ' <urURL>https://data.syncthing.net/newdata</urURL>';
  212. echo ' <urPostInsecurely>false</urPostInsecurely>';
  213. echo ' <urInitialDelayS>1800</urInitialDelayS>';
  214. echo ' <restartOnWakeup>true</restartOnWakeup>';
  215. echo ' <autoUpgradeIntervalH>12</autoUpgradeIntervalH>';
  216. echo ' <keepTemporariesH>24</keepTemporariesH>';
  217. echo ' <cacheIgnoredFiles>true</cacheIgnoredFiles>';
  218. echo ' <progressUpdateIntervalS>5</progressUpdateIntervalS>';
  219. echo ' <symlinksEnabled>true</symlinksEnabled>';
  220. echo ' <limitBandwidthInLan>false</limitBandwidthInLan>';
  221. echo ' <minHomeDiskFreePct>1</minHomeDiskFreePct>';
  222. echo " <releasesURL>$SYNCTHING_RELEASES</releasesURL>";
  223. echo ' </options>';
  224. echo '</configuration>'; } >> "$SYNCTHING_CONFIG_FILE"
  225. # give each user account a file containing the device id for this server
  226. # This allows it to appear within the user control panel
  227. for d in /home/*/ ; do
  228. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  229. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  230. echo "$SYNCTHING_ID" > "/home/$USERNAME/.syncthing-server-id"
  231. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id"
  232. fi
  233. done
  234. }
  235. function user_devices_changed {
  236. CHANGED=
  237. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  238. CHANGED=1
  239. return
  240. fi
  241. if ! grep -q "${PROJECT_NAME}" $SYNCTHING_CONFIG_FILE; then
  242. CHANGED=1
  243. return
  244. fi
  245. for d in /home/*/ ; do
  246. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  247. if [ ! -f "/home/$USERNAME/.syncthing-server-id" ]; then
  248. CHANGED=1
  249. return
  250. fi
  251. done
  252. for d in /home/*/ ; do
  253. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  254. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  255. if [ -f "/home/$USERNAME/$SYNCTHING_UPDATE_FILE" ]; then
  256. CHANGED=1
  257. fi
  258. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  259. while read -r line || [[ -n "$line" ]]; do
  260. if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then
  261. if [ ${#line} -gt 10 ]; then
  262. if ! grep -q "$line" $SYNCTHING_CONFIG_FILE; then
  263. CHANGED=1
  264. fi
  265. fi
  266. fi
  267. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  268. fi
  269. # Permissions on user Sync directories
  270. if [ -d "/home/$USERNAME/Sync" ]; then
  271. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
  272. fi
  273. if [ -d "/home/$USERNAME/SyncShared" ]; then
  274. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
  275. fi
  276. fi
  277. done
  278. }
  279. function syncthing_set_permissions {
  280. for d in /home/*/ ; do
  281. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  282. if [ -d "/home/$USERNAME/Sync" ]; then
  283. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
  284. fi
  285. if [ -d "/home/$USERNAME/SyncShared" ]; then
  286. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
  287. fi
  288. done
  289. }
  290. user_devices_changed
  291. if [ $CHANGED ]; then
  292. create_syncthing_config
  293. syncthing_set_permissions
  294. systemctl restart syncthing
  295. else
  296. syncthing_set_permissions
  297. fi
  298. exit 0