#!/bin/bash # _____ _ _ # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___ # | __| _| -_| -_| . | . | | . | . | | -_| # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___| # # Freedom in the Cloud # # Checks for changed syncthing device IDs within user home directories # and then recreates the syncthing configuration file accordingly # # License # ======= # # Copyright (C) 2016-2018 Bob Mottram # # 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 . NO_OF_ARGS=$# PROJECT_NAME='freedombone' export TEXTDOMAIN=$PROJECT_NAME-syncthing export TEXTDOMAINDIR="/usr/share/locale" UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*" for f in $UTILS_FILES do source "$f" done # 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' 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 "//d" $SYNCTHING_CONFIG_FILE systemctl restart syncthing } function new_syncthing_id { for i in {1..8} do v="" # shellcheck disable=SC2034 for j in {1..2} do v2=$(echo "obase=16;$RANDOM" | bc) v=$v$v2 done v=$(echo "$v" | cut -c1-7) if [ "${i}" -lt 8 ]; then v=$v"-" fi echo -n "$v" done echo "$v" } function create_syncthing_config { if grep -q "syncthing ID" "$COMPLETION_FILE"; then SYNCTHING_ID=$(get_completion_param "syncthing ID") else if [ -f $SYNCTHING_CONFIG_FILE ]; then SYNCTHING_ID=$(grep "device id=" "$SYNCTHING_CONFIG_FILE" | head -n 1 | awk -F '"' '{print $2}') else SYNCTHING_ID=$(new_syncthing_id) fi fi set_completion_param "syncthing ID" "$SYNCTHING_ID" 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 '' > $SYNCTHING_CONFIG_FILE for d in /home/*/ ; do USERNAME=$(echo "$d" | awk -F '/' '{print $3}') if [[ $(is_valid_user "$USERNAME") == "1" ]]; then echo " " >> $SYNCTHING_CONFIG_FILE # include any specified device IDs for this user if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then echo "" > $TEMP_IDS_FILE while read -r line || [[ -n "$line" ]]; do line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then if [ ${#line2} -gt 10 ]; then if ! grep -q "$line2" $TEMP_IDS_FILE; then echo " " >> $SYNCTHING_CONFIG_FILE echo "$line2" >> $TEMP_IDS_FILE fi fi fi done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" rm $TEMP_IDS_FILE fi { echo " "; echo ' 1'; echo ' '; echo ' 0'; echo ' 0'; echo ' 0'; echo ' random'; echo ' false'; echo ' 0'; echo ' 0'; echo ' 0'; echo ' 10'; echo ' false'; echo ' '; } >> "$SYNCTHING_CONFIG_FILE" fi done echo " " >> $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}') if [[ $(is_valid_user "$USERNAME") == "1" ]]; then if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then while read -r line || [[ -n "$line" ]]; do line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then if [ ${#line2} -gt 10 ]; then if ! grep -q "$line2" $TEMP_IDS_FILE; then echo " " >> $SYNCTHING_CONFIG_FILE echo "$line2" >> $TEMP_IDS_FILE fi fi fi done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" fi fi done rm $TEMP_IDS_FILE { echo " "; echo ' 1'; echo ' '; echo ' 0'; echo ' 0'; echo ' 0'; echo ' random'; echo ' false'; echo ' 0'; echo ' 0'; echo ' 0'; echo ' 10'; echo ' false'; echo ' '; echo " "; echo '
dynamic
'; echo '
'; } >> "$SYNCTHING_CONFIG_FILE" echo "" > $TEMP_IDS_FILE for d in /home/*/ ; do USERNAME=$(echo "$d" | awk -F '/' '{print $3}') if [[ $(is_valid_user "$USERNAME") == "1" ]]; then if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then while read -r line || [[ -n "$line" ]]; do line2=$(echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') if [[ $line2 != *"#"* && $line2 != *"*"* && $line2 != *'/'* && $line2 == *"-"* ]]; then if [ ${#line2} -gt 10 ]; then if ! grep -q "$line2" $TEMP_IDS_FILE; then echo " " >> "$SYNCTHING_CONFIG_FILE" echo '
dynamic
' >> $SYNCTHING_CONFIG_FILE echo '
' >> $SYNCTHING_CONFIG_FILE echo "$line2" >> $TEMP_IDS_FILE fi fi fi done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" fi fi done rm $TEMP_IDS_FILE { echo ' '; echo " tcp://0.0.0.0:$SYNCTHING_PORT"; echo ' default'; echo ' true'; echo ' true'; echo ' 21027'; echo ' [ff12::8384]:21027'; echo " dynamic+$SYNCTHING_RELAY_SERVER"; echo ' 0'; echo ' 0'; echo ' 60'; echo ' true'; echo ' 10'; echo ' true'; echo ' true'; echo ' 60'; echo ' 30'; echo ' 10'; echo ' -1'; echo ' '; echo ' https://data.syncthing.net/newdata'; echo ' false'; echo ' 1800'; echo ' true'; echo ' 12'; echo ' 24'; echo ' true'; echo ' 5'; echo ' true'; echo ' false'; echo ' 1'; echo " $SYNCTHING_RELEASES"; echo ' '; echo '
'; } >> "$SYNCTHING_CONFIG_FILE" # 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}') if [[ $(is_valid_user "$USERNAME") == "1" ]]; then echo "$SYNCTHING_ID" > "/home/$USERNAME/.syncthing-server-id" chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.syncthing-server-id" fi done } function user_devices_changed { CHANGED= if [ ! -f $SYNCTHING_CONFIG_FILE ]; then CHANGED=1 return fi 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}') if [ ! -f "/home/$USERNAME/.syncthing-server-id" ]; then CHANGED=1 return fi done for d in /home/*/ ; do USERNAME=$(echo "$d" | awk -F '/' '{print $3}') if [[ $(is_valid_user "$USERNAME") == "1" ]]; then if [ -f "/home/$USERNAME/$SYNCTHING_UPDATE_FILE" ]; then CHANGED=1 fi if [ -f "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" ]; then 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 done < "/home/$USERNAME/$SYNCTHING_USER_IDS_FILE" fi # Permissions on user Sync directories if [ -d "/home/$USERNAME/Sync" ]; then chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync" fi if [ -d "/home/$USERNAME/SyncShared" ]; then chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared" fi fi done } function syncthing_set_permissions { for d in /home/*/ ; do USERNAME=$(echo "$d" | awk -F '/' '{print $3}') if [ -d "/home/$USERNAME/Sync" ]; then chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/Sync" fi if [ -d "/home/$USERNAME/SyncShared" ]; then chown "$USERNAME":"$USERNAME" "/home/$USERNAME" "/home/$USERNAME/SyncShared" fi done } user_devices_changed if [ $CHANGED ]; then create_syncthing_config syncthing_set_permissions systemctl restart syncthing else syncthing_set_permissions fi exit 0