freedombone-syncthing 13KB

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