freedomboneeee/src/freedombone-syncthing

329 lines
14 KiB
Plaintext
Raw Permalink Normal View History

2016-04-09 21:26:46 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-04-09 21:26:46 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-04-09 21:26:46 +02:00
#
# Checks for changed syncthing device IDs within user home directories
# and then recreates the syncthing configuration file accordingly
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
2016-04-09 21:26:46 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
NO_OF_ARGS=$#
PROJECT_NAME='freedombone'
export TEXTDOMAIN=$PROJECT_NAME-syncthing
export TEXTDOMAINDIR="/usr/share/locale"
2018-03-02 20:17:02 +01:00
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
2016-10-16 20:50:56 +02:00
for f in $UTILS_FILES
do
2018-03-02 20:17:02 +01:00
source "$f"
2016-10-16 20:50:56 +02:00
done
2016-10-01 11:39:23 +02:00
2016-04-09 21:26:46 +02:00
# File which keeps track of what has already been installed
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
SYNCTHING_ID=
SYNCTHING_CONFIG_PATH=/root/.config/syncthing
SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
SYNCTHING_PORT=22000
SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
SYNCTHING_USER_IDS_FILE='.syncthingids'
SYNCTHING_UPDATE_FILE='.syncthing-update'
2016-04-09 21:26:46 +02:00
CHANGED=
TEMP_IDS_FILE=/root/.synthingids
SYNCTHING_SHORT_DESCRIPTION=$'File Synchronization'
SYNCTHING_DESCRIPTION=$'File Synchronization'
SYNCTHING_MOBILE_APP_URL='https://f-droid.org/packages/com.nutomic.syncthingandroid'
function remove_user_syncthing {
remove_username="$1"
sed -i "/<folder id=\"${remove_username}\" /,/</folder>/d" $SYNCTHING_CONFIG_FILE
systemctl restart syncthing
}
2016-04-09 21:26:46 +02:00
function new_syncthing_id {
for i in {1..8}
do
v=""
2018-03-02 20:17:02 +01:00
# shellcheck disable=SC2034
2016-04-09 21:26:46 +02:00
for j in {1..2}
do
2018-03-02 20:17:02 +01:00
v2=$(echo "obase=16;$RANDOM" | bc)
2016-04-09 21:26:46 +02:00
v=$v$v2
done
v=$(echo "$v" | cut -c1-7)
2018-03-02 20:17:02 +01:00
if [ "${i}" -lt 8 ]; then
2016-04-09 21:26:46 +02:00
v=$v"-"
fi
echo -n "$v"
done
echo "$v"
}
function create_syncthing_config {
2018-03-02 20:17:02 +01:00
if grep -q "syncthing ID" "$COMPLETION_FILE"; then
2016-10-16 20:50:56 +02:00
SYNCTHING_ID=$(get_completion_param "syncthing ID")
2016-04-09 21:26:46 +02:00
else
if [ -f $SYNCTHING_CONFIG_FILE ]; then
2018-03-02 20:17:02 +01:00
SYNCTHING_ID=$(grep "device id=" "$SYNCTHING_CONFIG_FILE" | head -n 1 | awk -F '"' '{print $2}')
2016-04-09 21:26:46 +02:00
else
SYNCTHING_ID=$(new_syncthing_id)
fi
2016-04-09 21:26:46 +02:00
fi
2016-10-16 20:50:56 +02:00
set_completion_param "syncthing ID" "$SYNCTHING_ID"
2016-04-09 21:26:46 +02:00
if [ ! -d $SYNCTHING_CONFIG_PATH ]; then
mkdir -p $SYNCTHING_CONFIG_PATH
fi
if [ ! -d $SYNCTHING_SHARED_DATA ]; then
mkdir -p $SYNCTHING_SHARED_DATA
fi
echo '<configuration version="12">' > $SYNCTHING_CONFIG_FILE
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2016-04-09 21:26:46 +02:00
echo " <folder id=\"$USERNAME\" path=\"/home/$USERNAME/Sync/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
# include any specified device IDs for this user
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
2016-04-09 21:26:46 +02:00
echo "" > $TEMP_IDS_FILE
while read -r line || [[ -n "$line" ]]; do
2018-04-01 20:07:36 +02:00
line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
2016-04-10 19:44:59 +02:00
if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
if [ ${#line2} -gt 10 ]; then
if ! grep -q "$line2" $TEMP_IDS_FILE; then
echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
echo "$line2" >> $TEMP_IDS_FILE
2016-04-09 21:26:46 +02:00
fi
fi
fi
2018-03-02 20:17:02 +01:00
done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
2016-04-09 21:26:46 +02:00
rm $TEMP_IDS_FILE
fi
2018-03-02 20:17:02 +01:00
{ echo " <device id=\"$SYNCTHING_ID\"></device>";
echo ' <minDiskFreePct>1</minDiskFreePct>';
echo ' <versioning></versioning>';
echo ' <copiers>0</copiers>';
echo ' <pullers>0</pullers>';
echo ' <hashers>0</hashers>';
echo ' <order>random</order>';
echo ' <ignoreDelete>false</ignoreDelete>';
echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
echo ' <pullerSleepS>0</pullerSleepS>';
echo ' <pullerPauseS>0</pullerPauseS>';
echo ' <maxConflicts>10</maxConflicts>';
echo ' <disableSparseFiles>false</disableSparseFiles>';
echo ' </folder>'; } >> "$SYNCTHING_CONFIG_FILE"
2016-04-09 21:26:46 +02:00
fi
done
echo " <folder id=\"shared\" path=\"$SYNCTHING_SHARED_DATA/\" ro=\"false\" rescanIntervalS=\"60\" ignorePerms=\"false\" autoNormalize=\"true\">" >> $SYNCTHING_CONFIG_FILE
# all user devices may access this shared directory
echo "" > $TEMP_IDS_FILE
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
2016-04-09 21:26:46 +02:00
while read -r line || [[ -n "$line" ]]; do
2018-04-01 20:07:36 +02:00
line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
2016-04-10 19:44:59 +02:00
if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
if [ ${#line2} -gt 10 ]; then
if ! grep -q "$line2" $TEMP_IDS_FILE; then
echo " <device id=\"$line2\"></device>" >> $SYNCTHING_CONFIG_FILE
echo "$line2" >> $TEMP_IDS_FILE
2016-04-09 21:26:46 +02:00
fi
fi
fi
2018-03-02 20:17:02 +01:00
done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
2016-04-09 21:26:46 +02:00
fi
fi
done
rm $TEMP_IDS_FILE
2018-03-02 20:17:02 +01:00
{ echo " <device id=\"$SYNCTHING_ID\"></device>";
echo ' <minDiskFreePct>1</minDiskFreePct>';
echo ' <versioning></versioning>';
echo ' <copiers>0</copiers>';
echo ' <pullers>0</pullers>';
echo ' <hashers>0</hashers>';
echo ' <order>random</order>';
echo ' <ignoreDelete>false</ignoreDelete>';
echo ' <scanProgressIntervalS>0</scanProgressIntervalS>';
echo ' <pullerSleepS>0</pullerSleepS>';
echo ' <pullerPauseS>0</pullerPauseS>';
echo ' <maxConflicts>10</maxConflicts>';
echo ' <disableSparseFiles>false</disableSparseFiles>';
echo ' </folder>';
2016-04-09 21:26:46 +02:00
2018-03-02 20:17:02 +01:00
echo " <device id=\"$SYNCTHING_ID\" name=\"${PROJECT_NAME}\" compression=\"metadata\" introducer=\"false\">";
echo ' <address>dynamic</address>';
echo ' </device>'; } >> "$SYNCTHING_CONFIG_FILE"
2016-04-09 21:26:46 +02:00
echo "" > $TEMP_IDS_FILE
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
2016-04-09 21:26:46 +02:00
while read -r line || [[ -n "$line" ]]; do
2018-04-01 20:07:36 +02:00
line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
2016-04-10 19:44:59 +02:00
if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then
if [ ${#line2} -gt 10 ]; then
if ! grep -q "$line2" $TEMP_IDS_FILE; then
2018-03-02 20:17:02 +01:00
echo " <device id=\"$line2\" name=\"${USERNAME}\" compression=\"metadata\" introducer=\"false\">" >> "$SYNCTHING_CONFIG_FILE"
2016-04-09 21:26:46 +02:00
echo ' <address>dynamic</address>' >> $SYNCTHING_CONFIG_FILE
echo ' </device>' >> $SYNCTHING_CONFIG_FILE
2016-04-10 19:44:59 +02:00
echo "$line2" >> $TEMP_IDS_FILE
2016-04-09 21:26:46 +02:00
fi
fi
fi
2018-03-02 20:17:02 +01:00
done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
2016-04-09 21:26:46 +02:00
fi
fi
done
rm $TEMP_IDS_FILE
2016-04-09 21:26:46 +02:00
2018-03-02 20:17:02 +01:00
{ echo ' <options>';
echo " <listenAddress>tcp://0.0.0.0:$SYNCTHING_PORT</listenAddress>";
echo ' <globalAnnounceServer>default</globalAnnounceServer>';
echo ' <globalAnnounceEnabled>true</globalAnnounceEnabled>';
echo ' <localAnnounceEnabled>true</localAnnounceEnabled>';
echo ' <localAnnouncePort>21027</localAnnouncePort>';
echo ' <localAnnounceMCAddr>[ff12::8384]:21027</localAnnounceMCAddr>';
echo " <relayServer>dynamic+$SYNCTHING_RELAY_SERVER</relayServer>";
echo ' <maxSendKbps>0</maxSendKbps>';
echo ' <maxRecvKbps>0</maxRecvKbps>';
echo ' <reconnectionIntervalS>60</reconnectionIntervalS>';
echo ' <relaysEnabled>true</relaysEnabled>';
echo ' <relayReconnectIntervalM>10</relayReconnectIntervalM>';
echo ' <startBrowser>true</startBrowser>';
echo ' <upnpEnabled>true</upnpEnabled>';
echo ' <upnpLeaseMinutes>60</upnpLeaseMinutes>';
echo ' <upnpRenewalMinutes>30</upnpRenewalMinutes>';
echo ' <upnpTimeoutSeconds>10</upnpTimeoutSeconds>';
echo ' <urAccepted>-1</urAccepted>';
echo ' <urUniqueID></urUniqueID>';
echo ' <urURL>https://data.syncthing.net/newdata</urURL>';
echo ' <urPostInsecurely>false</urPostInsecurely>';
echo ' <urInitialDelayS>1800</urInitialDelayS>';
echo ' <restartOnWakeup>true</restartOnWakeup>';
echo ' <autoUpgradeIntervalH>12</autoUpgradeIntervalH>';
echo ' <keepTemporariesH>24</keepTemporariesH>';
echo ' <cacheIgnoredFiles>true</cacheIgnoredFiles>';
echo ' <progressUpdateIntervalS>5</progressUpdateIntervalS>';
echo ' <symlinksEnabled>true</symlinksEnabled>';
echo ' <limitBandwidthInLan>false</limitBandwidthInLan>';
echo ' <minHomeDiskFreePct>1</minHomeDiskFreePct>';
echo " <releasesURL>$SYNCTHING_RELEASES</releasesURL>";
echo ' </options>';
echo '</configuration>'; } >> "$SYNCTHING_CONFIG_FILE"
2016-04-09 21:26:46 +02:00
# give each user account a file containing the device id for this server
# This allows it to appear within the user control panel
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-03-02 20:17:02 +01:00
echo "$SYNCTHING_ID" > "/home/$USERNAME/.syncthing-server-id"
chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id"
2016-04-09 21:26:46 +02:00
fi
done
}
function user_devices_changed {
CHANGED=
if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
CHANGED=1
return
fi
2016-04-09 22:14:29 +02:00
if ! grep -q "${PROJECT_NAME}" $SYNCTHING_CONFIG_FILE; then
CHANGED=1
return
fi
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2018-03-02 20:17:02 +01:00
if [ ! -f "/home/$USERNAME/.syncthing-server-id" ]; then
CHANGED=1
return
fi
done
2016-04-09 21:26:46 +02:00
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2016-10-01 11:48:07 +02:00
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USERNAME/$SYNCTHING_UPDATE_FILE" ]; then
CHANGED=1
fi
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then
2016-04-09 21:26:46 +02:00
while read -r line || [[ -n "$line" ]]; do
if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then
if [ ${#line} -gt 10 ]; then
if ! grep -q "$line" $SYNCTHING_CONFIG_FILE; then
CHANGED=1
fi
fi
fi
2018-03-02 20:17:02 +01:00
done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE"
2016-04-09 21:26:46 +02:00
fi
2016-10-13 13:41:01 +02:00
# Permissions on user Sync directories
2018-03-02 20:17:02 +01:00
if [ -d "/home/$USERNAME/Sync" ]; then
chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
2016-10-13 13:41:01 +02:00
fi
2018-03-02 20:17:02 +01:00
if [ -d "/home/$USERNAME/SyncShared" ]; then
chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
2016-10-13 13:41:01 +02:00
fi
2016-04-09 21:26:46 +02:00
fi
done
}
2017-06-13 23:29:08 +02:00
function syncthing_set_permissions {
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
2018-03-02 20:17:02 +01:00
if [ -d "/home/$USERNAME/Sync" ]; then
chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync"
2017-06-13 23:29:08 +02:00
fi
2018-03-02 20:17:02 +01:00
if [ -d "/home/$USERNAME/SyncShared" ]; then
chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared"
2017-06-13 23:29:08 +02:00
fi
done
}
2016-04-09 21:26:46 +02:00
user_devices_changed
if [ $CHANGED ]; then
create_syncthing_config
2017-06-13 23:29:08 +02:00
syncthing_set_permissions
2016-04-09 21:26:46 +02:00
systemctl restart syncthing
2017-06-13 23:29:08 +02:00
else
syncthing_set_permissions
2016-04-09 21:26:46 +02:00
fi
exit 0