freedombone-syncthing 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. SYNCTHING_SHORT_DESCRIPTION=$'File Synchronization'
  52. SYNCTHING_DESCRIPTION=$'File Synchronization'
  53. SYNCTHING_MOBILE_APP_URL='https://f-droid.org/packages/com.nutomic.syncthingandroid'
  54. function remove_user_syncthing {
  55. remove_username="$1"
  56. sed -i "/<folder id=\"${remove_username}\" /,/</folder>/d" $SYNCTHING_CONFIG_FILE
  57. systemctl restart syncthing
  58. }
  59. function new_syncthing_id {
  60. for i in {1..8}
  61. do
  62. v=""
  63. # shellcheck disable=SC2034
  64. for j in {1..2}
  65. do
  66. v2=$(echo "obase=16;$RANDOM" | bc)
  67. v=$v$v2
  68. done
  69. v=$(echo "$v" | cut -c1-7)
  70. if [ "${i}" -lt 8 ]; then
  71. v=$v"-"
  72. fi
  73. echo -n "$v"
  74. done
  75. echo "$v"
  76. }
  77. function create_syncthing_config {
  78. if grep -q "syncthing ID" "$COMPLETION_FILE"; then
  79. SYNCTHING_ID=$(get_completion_param "syncthing ID")
  80. else
  81. if [ -f $SYNCTHING_CONFIG_FILE ]; then
  82. SYNCTHING_ID=$(grep "device id=" "$SYNCTHING_CONFIG_FILE" | head -n 1 | awk -F '"' '{print $2}')
  83. else
  84. SYNCTHING_ID=$(new_syncthing_id)
  85. fi
  86. fi
  87. set_completion_param "syncthing ID" "$SYNCTHING_ID"
  88. if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
  89. mkdir -p $SYNCTHING_CONFIG_PATH
  90. fi
  91. if [ ! -d $SYNCTHING_SHARED_DATA ]; then
  92. mkdir -p $SYNCTHING_SHARED_DATA
  93. fi
  94. echo '<configuration version="12">' > $SYNCTHING_CONFIG_FILE
  95. for d in /home/*/ ; do
  96. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  97. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  98. echo " <folder id=\"$USERNAME\" path=\"/home/$USERNAME/Sync/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  99. # include any specified device IDs for this user
  100. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  101. echo "" > $TEMP_IDS_FILE
  102. while read -r line || [[ -n "$line" ]]; do
  103. line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  104. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  105. if [ ${#line2} -gt 10 ]; then
  106. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  107. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  108. echo "$line2" >> $TEMP_IDS_FILE
  109. fi
  110. fi
  111. fi
  112. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  113. rm $TEMP_IDS_FILE
  114. fi
  115. { echo " <device id=\"$SYNCTHING_ID\"></device>";
  116. echo ' <minDiskFreePct>1</minDiskFreePct>';
  117. echo ' <versioning></versioning>';
  118. echo ' <copiers>0</copiers>';
  119. echo ' <pullers>0</pullers>';
  120. echo ' <hashers>0</hashers>';
  121. echo ' <order>random</order>';
  122. echo ' <ignoreDelete>false</ignoreDelete>';
  123. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
  124. echo ' <pullerSleepS>0</pullerSleepS>';
  125. echo ' <pullerPauseS>0</pullerPauseS>';
  126. echo ' <maxConflicts>10</maxConflicts>';
  127. echo ' <disableSparseFiles>false</disableSparseFiles>';
  128. echo ' </folder>'; } >> "$SYNCTHING_CONFIG_FILE"
  129. fi
  130. done
  131. echo " <folder id=\"shared\" path=\"$SYNCTHING_SHARED_DATA/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
  132. # all user devices may access this shared directory
  133. echo "" > $TEMP_IDS_FILE
  134. for d in /home/*/ ; do
  135. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  136. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  137. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  138. while read -r line || [[ -n "$line" ]]; do
  139. line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  140. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  141. if [ ${#line2} -gt 10 ]; then
  142. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  143. echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
  144. echo "$line2" >> $TEMP_IDS_FILE
  145. fi
  146. fi
  147. fi
  148. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  149. fi
  150. fi
  151. done
  152. rm $TEMP_IDS_FILE
  153. { echo " <device id=\"$SYNCTHING_ID\"></device>";
  154. echo ' <minDiskFreePct>1</minDiskFreePct>';
  155. echo ' <versioning></versioning>';
  156. echo ' <copiers>0</copiers>';
  157. echo ' <pullers>0</pullers>';
  158. echo ' <hashers>0</hashers>';
  159. echo ' <order>random</order>';
  160. echo ' <ignoreDelete>false</ignoreDelete>';
  161. echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
  162. echo ' <pullerSleepS>0</pullerSleepS>';
  163. echo ' <pullerPauseS>0</pullerPauseS>';
  164. echo ' <maxConflicts>10</maxConflicts>';
  165. echo ' <disableSparseFiles>false</disableSparseFiles>';
  166. echo ' </folder>';
  167. echo " <device id=\"$SYNCTHING_ID\" name=\"${PROJECT_NAME}\" compression=\"metadata\" introducer=\"false\">";
  168. echo ' <address>dynamic</address>';
  169. echo ' </device>'; } >> "$SYNCTHING_CONFIG_FILE"
  170. echo "" > $TEMP_IDS_FILE
  171. for d in /home/*/ ; do
  172. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  173. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  174. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  175. while read -r line || [[ -n "$line" ]]; do
  176. line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  177. if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
  178. if [ ${#line2} -gt 10 ]; then
  179. if ! grep -q "$line2" $TEMP_IDS_FILE; then
  180. echo " <device id=\"$line2\" name=\"${USERNAME}\" compression=\"metadata\" introducer=\"false\">" >> "$SYNCTHING_CONFIG_FILE"
  181. echo ' <address>dynamic</address>' >> $SYNCTHING_CONFIG_FILE
  182. echo ' </device>' >> $SYNCTHING_CONFIG_FILE
  183. echo "$line2" >> $TEMP_IDS_FILE
  184. fi
  185. fi
  186. fi
  187. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  188. fi
  189. fi
  190. done
  191. rm $TEMP_IDS_FILE
  192. { echo ' <options>';
  193. echo " <listenAddress>tcp://0.0.0.0:$SYNCTHING_PORT</listenAddress>";
  194. echo ' <globalAnnounceServer>default</globalAnnounceServer>';
  195. echo ' <globalAnnounceEnabled>true</globalAnnounceEnabled>';
  196. echo ' <localAnnounceEnabled>true</localAnnounceEnabled>';
  197. echo ' <localAnnouncePort>21027</localAnnouncePort>';
  198. echo ' <localAnnounceMCAddr>[ff12::8384]:21027</localAnnounceMCAddr>';
  199. echo " <relayServer>dynamic+$SYNCTHING_RELAY_SERVER</relayServer>";
  200. echo ' <maxSendKbps>0</maxSendKbps>';
  201. echo ' <maxRecvKbps>0</maxRecvKbps>';
  202. echo ' <reconnectionIntervalS>60</reconnectionIntervalS>';
  203. echo ' <relaysEnabled>true</relaysEnabled>';
  204. echo ' <relayReconnectIntervalM>10</relayReconnectIntervalM>';
  205. echo ' <startBrowser>true</startBrowser>';
  206. echo ' <upnpEnabled>true</upnpEnabled>';
  207. echo ' <upnpLeaseMinutes>60</upnpLeaseMinutes>';
  208. echo ' <upnpRenewalMinutes>30</upnpRenewalMinutes>';
  209. echo ' <upnpTimeoutSeconds>10</upnpTimeoutSeconds>';
  210. echo ' <urAccepted>-1</urAccepted>';
  211. echo ' <urUniqueID></urUniqueID>';
  212. echo ' <urURL>https://data.syncthing.net/newdata</urURL>';
  213. echo ' <urPostInsecurely>false</urPostInsecurely>';
  214. echo ' <urInitialDelayS>1800</urInitialDelayS>';
  215. echo ' <restartOnWakeup>true</restartOnWakeup>';
  216. echo ' <autoUpgradeIntervalH>12</autoUpgradeIntervalH>';
  217. echo ' <keepTemporariesH>24</keepTemporariesH>';
  218. echo ' <cacheIgnoredFiles>true</cacheIgnoredFiles>';
  219. echo ' <progressUpdateIntervalS>5</progressUpdateIntervalS>';
  220. echo ' <symlinksEnabled>true</symlinksEnabled>';
  221. echo ' <limitBandwidthInLan>false</limitBandwidthInLan>';
  222. echo ' <minHomeDiskFreePct>1</minHomeDiskFreePct>';
  223. echo " <releasesURL>$SYNCTHING_RELEASES</releasesURL>";
  224. echo ' </options>';
  225. echo '</configuration>'; } >> "$SYNCTHING_CONFIG_FILE"
  226. # give each user account a file containing the device id for this server
  227. # This allows it to appear within the user control panel
  228. for d in /home/*/ ; do
  229. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  230. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  231. echo "$SYNCTHING_ID" > "/home/$USERNAME/.syncthing-server-id"
  232. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id"
  233. fi
  234. done
  235. }
  236. function user_devices_changed {
  237. CHANGED=
  238. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  239. CHANGED=1
  240. return
  241. fi
  242. if ! grep -q "${PROJECT_NAME}" $SYNCTHING_CONFIG_FILE; then
  243. CHANGED=1
  244. return
  245. fi
  246. for d in /home/*/ ; do
  247. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  248. if [ ! -f "/home/$USERNAME/.syncthing-server-id" ]; then
  249. CHANGED=1
  250. return
  251. fi
  252. done
  253. for d in /home/*/ ; do
  254. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  255. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  256. if [ -f "/home/$USERNAME/$SYNCTHING_UPDATE_FILE" ]; then
  257. CHANGED=1
  258. fi
  259. if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
  260. while read -r line || [[ -n "$line" ]]; do
  261. if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then
  262. if [ ${#line} -gt 10 ]; then
  263. if ! grep -q "$line" $SYNCTHING_CONFIG_FILE; then
  264. CHANGED=1
  265. fi
  266. fi
  267. fi
  268. done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
  269. fi
  270. # Permissions on user Sync directories
  271. if [ -d "/home/$USERNAME/Sync" ]; then
  272. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
  273. fi
  274. if [ -d "/home/$USERNAME/SyncShared" ]; then
  275. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
  276. fi
  277. fi
  278. done
  279. }
  280. function syncthing_set_permissions {
  281. for d in /home/*/ ; do
  282. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  283. if [ -d "/home/$USERNAME/Sync" ]; then
  284. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
  285. fi
  286. if [ -d "/home/$USERNAME/SyncShared" ]; then
  287. chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
  288. fi
  289. done
  290. }
  291. user_devices_changed
  292. if [ $CHANGED ]; then
  293. create_syncthing_config
  294. syncthing_set_permissions
  295. systemctl restart syncthing
  296. else
  297. syncthing_set_permissions
  298. fi
  299. exit 0