#!/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 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 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="" for j in {1..2} do v2=`echo "obase=16;$RANDOM" | bc` v=$v$v2 done v=$(echo "$v" | cut -c1-7) if [[ ${i} < 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=$(cat $SYNCTHING_CONFIG_FILE | grep "device id=" | 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 " " >> $SYNCTHING_CONFIG_FILE echo ' 1' >> $SYNCTHING_CONFIG_FILE echo ' ' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' random' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 10' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE 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 " " >> $SYNCTHING_CONFIG_FILE echo ' 1' >> $SYNCTHING_CONFIG_FILE echo ' ' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' random' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 10' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE echo ' ' >> $SYNCTHING_CONFIG_FILE echo " " >> $SYNCTHING_CONFIG_FILE echo '
dynamic
' >> $SYNCTHING_CONFIG_FILE 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 ' ' >> $SYNCTHING_CONFIG_FILE echo " tcp://0.0.0.0:$SYNCTHING_PORT" >> $SYNCTHING_CONFIG_FILE echo ' default' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' 21027' >> $SYNCTHING_CONFIG_FILE echo ' [ff12::8384]:21027' >> $SYNCTHING_CONFIG_FILE echo " dynamic+$SYNCTHING_RELAY_SERVER" >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 0' >> $SYNCTHING_CONFIG_FILE echo ' 60' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' 10' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' 60' >> $SYNCTHING_CONFIG_FILE echo ' 30' >> $SYNCTHING_CONFIG_FILE echo ' 10' >> $SYNCTHING_CONFIG_FILE echo ' -1' >> $SYNCTHING_CONFIG_FILE echo ' ' >> $SYNCTHING_CONFIG_FILE echo ' https://data.syncthing.net/newdata' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE echo ' 1800' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' 12' >> $SYNCTHING_CONFIG_FILE echo ' 24' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' 5' >> $SYNCTHING_CONFIG_FILE echo ' true' >> $SYNCTHING_CONFIG_FILE echo ' false' >> $SYNCTHING_CONFIG_FILE echo ' 1' >> $SYNCTHING_CONFIG_FILE echo " $SYNCTHING_RELEASES" >> $SYNCTHING_CONFIG_FILE echo ' ' >> $SYNCTHING_CONFIG_FILE 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 [[ $(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 } user_devices_changed if [ $CHANGED ]; then create_syncthing_config systemctl restart syncthing fi exit 0