|  | @@ -0,0 +1,226 @@
 | 
	
		
			
			|  | 1 | +#!/bin/bash
 | 
	
		
			
			|  | 2 | +#
 | 
	
		
			
			|  | 3 | +# .---.                  .              .
 | 
	
		
			
			|  | 4 | +# |                      |              |
 | 
	
		
			
			|  | 5 | +# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 | 
	
		
			
			|  | 6 | +# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 | 
	
		
			
			|  | 7 | +# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 | 
	
		
			
			|  | 8 | +#
 | 
	
		
			
			|  | 9 | +#                    Freedom in the Cloud
 | 
	
		
			
			|  | 10 | +#
 | 
	
		
			
			|  | 11 | +# synapse matrix server
 | 
	
		
			
			|  | 12 | +#
 | 
	
		
			
			|  | 13 | +# License
 | 
	
		
			
			|  | 14 | +# =======
 | 
	
		
			
			|  | 15 | +#
 | 
	
		
			
			|  | 16 | +# Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
 | 
	
		
			
			|  | 17 | +#
 | 
	
		
			
			|  | 18 | +# This program is free software: you can redistribute it and/or modify
 | 
	
		
			
			|  | 19 | +# it under the terms of the GNU Affero General Public License as published by
 | 
	
		
			
			|  | 20 | +# the Free Software Foundation, either version 3 of the License, or
 | 
	
		
			
			|  | 21 | +# (at your option) any later version.
 | 
	
		
			
			|  | 22 | +#
 | 
	
		
			
			|  | 23 | +# This program is distributed in the hope that it will be useful,
 | 
	
		
			
			|  | 24 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
	
		
			
			|  | 25 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
	
		
			
			|  | 26 | +# GNU Affero General Public License for more details.
 | 
	
		
			
			|  | 27 | +#
 | 
	
		
			
			|  | 28 | +# You should have received a copy of the GNU Affero General Public License
 | 
	
		
			
			|  | 29 | +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
	
		
			
			|  | 30 | +
 | 
	
		
			
			|  | 31 | +VARIANTS='full full-vim chat'
 | 
	
		
			
			|  | 32 | +
 | 
	
		
			
			|  | 33 | +IN_DEFAULT_INSTALL=1
 | 
	
		
			
			|  | 34 | +SHOW_ON_ABOUT=0
 | 
	
		
			
			|  | 35 | +
 | 
	
		
			
			|  | 36 | +SYNAPSE_REPO="https://github.com/matrix-org/synapse"
 | 
	
		
			
			|  | 37 | +SYNAPSE_COMMIT='4a9055d4465d5d6059612e7c89f2b5774efc0e18'
 | 
	
		
			
			|  | 38 | +SYNAPSE_PORT=8448
 | 
	
		
			
			|  | 39 | +SYNAPSE_PASSWORD=
 | 
	
		
			
			|  | 40 | +SYNAPSE_DIR=/etc/matrix-synapse
 | 
	
		
			
			|  | 41 | +
 | 
	
		
			
			|  | 42 | +synapse_variables=(ONION_ONLY
 | 
	
		
			
			|  | 43 | +                   MY_USERNAME
 | 
	
		
			
			|  | 44 | +                   SYNAPSE_PASSWORD
 | 
	
		
			
			|  | 45 | +                   DEFAULT_DOMAIN_NAME)
 | 
	
		
			
			|  | 46 | +
 | 
	
		
			
			|  | 47 | +function remove_user_synapse {
 | 
	
		
			
			|  | 48 | +    remove_username="$1"
 | 
	
		
			
			|  | 49 | +    # TODO
 | 
	
		
			
			|  | 50 | +}
 | 
	
		
			
			|  | 51 | +
 | 
	
		
			
			|  | 52 | +function add_user_synapse {
 | 
	
		
			
			|  | 53 | +    new_username="$1"
 | 
	
		
			
			|  | 54 | +    new_user_password="$2"
 | 
	
		
			
			|  | 55 | +
 | 
	
		
			
			|  | 56 | +    cd $SYNAPSE_DIR
 | 
	
		
			
			|  | 57 | +    register_new_matrix_user -c homeserver.yaml https://localhost:${SYNAPSE_PORT} -u "${new_username}" -p "${new_user_password}" -a
 | 
	
		
			
			|  | 58 | +    echo '0'
 | 
	
		
			
			|  | 59 | +}
 | 
	
		
			
			|  | 60 | +
 | 
	
		
			
			|  | 61 | +function install_interactive_synapse {
 | 
	
		
			
			|  | 62 | +    echo -n ''
 | 
	
		
			
			|  | 63 | +    APP_INSTALLED=1
 | 
	
		
			
			|  | 64 | +}
 | 
	
		
			
			|  | 65 | +
 | 
	
		
			
			|  | 66 | +function change_password_synapse {
 | 
	
		
			
			|  | 67 | +    echo -n ''
 | 
	
		
			
			|  | 68 | +}
 | 
	
		
			
			|  | 69 | +
 | 
	
		
			
			|  | 70 | +function reconfigure_synapse {
 | 
	
		
			
			|  | 71 | +    echo -n ''
 | 
	
		
			
			|  | 72 | +}
 | 
	
		
			
			|  | 73 | +
 | 
	
		
			
			|  | 74 | +function upgrade_synapse {
 | 
	
		
			
			|  | 75 | +    echo -n ''
 | 
	
		
			
			|  | 76 | +}
 | 
	
		
			
			|  | 77 | +
 | 
	
		
			
			|  | 78 | +function backup_local_synapse {
 | 
	
		
			
			|  | 79 | +    source_directory=$SYNAPSE_DIR
 | 
	
		
			
			|  | 80 | +    if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 81 | +        systemctl stop synapse
 | 
	
		
			
			|  | 82 | +        function_check backup_directory_to_usb
 | 
	
		
			
			|  | 83 | +        backup_directory_to_usb $source_directory synapse
 | 
	
		
			
			|  | 84 | +        systemctl start synapse
 | 
	
		
			
			|  | 85 | +    fi
 | 
	
		
			
			|  | 86 | +}
 | 
	
		
			
			|  | 87 | +
 | 
	
		
			
			|  | 88 | +function restore_local_synapse {
 | 
	
		
			
			|  | 89 | +    if [ -d $SYNAPSE_DIR ]; then
 | 
	
		
			
			|  | 90 | +        systemctl stop synapse
 | 
	
		
			
			|  | 91 | +
 | 
	
		
			
			|  | 92 | +        temp_restore_dir=/root/tempsynapse
 | 
	
		
			
			|  | 93 | +        function_check restore_directory_from_usb
 | 
	
		
			
			|  | 94 | +        restore_directory_from_usb $temp_restore_dir synapse
 | 
	
		
			
			|  | 95 | +        cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
 | 
	
		
			
			|  | 96 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 97 | +            function_check backup_unmount_drive
 | 
	
		
			
			|  | 98 | +            backup_unmount_drive
 | 
	
		
			
			|  | 99 | +            exit 725
 | 
	
		
			
			|  | 100 | +        fi
 | 
	
		
			
			|  | 101 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 102 | +        chown -R synapse:synapse $SYNAPSE_DIR
 | 
	
		
			
			|  | 103 | +
 | 
	
		
			
			|  | 104 | +        systemctl start synapse
 | 
	
		
			
			|  | 105 | +    fi
 | 
	
		
			
			|  | 106 | +}
 | 
	
		
			
			|  | 107 | +
 | 
	
		
			
			|  | 108 | +function backup_remote_synapse {
 | 
	
		
			
			|  | 109 | +    source_directory=$SYNAPSE_DIR
 | 
	
		
			
			|  | 110 | +    if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 111 | +        systemctl stop synapse
 | 
	
		
			
			|  | 112 | +        function_check backup_directory_to_friend
 | 
	
		
			
			|  | 113 | +        backup_directory_to_friend $source_directory synapse
 | 
	
		
			
			|  | 114 | +        systemctl start synapse
 | 
	
		
			
			|  | 115 | +    fi
 | 
	
		
			
			|  | 116 | +}
 | 
	
		
			
			|  | 117 | +
 | 
	
		
			
			|  | 118 | +function restore_remote_synapse {
 | 
	
		
			
			|  | 119 | +    if [ -d $SYNAPSE_DIR ]; then
 | 
	
		
			
			|  | 120 | +        systemctl stop synapse
 | 
	
		
			
			|  | 121 | +
 | 
	
		
			
			|  | 122 | +        temp_restore_dir=/root/tempsynapse
 | 
	
		
			
			|  | 123 | +        function_check restore_directory_from_friend
 | 
	
		
			
			|  | 124 | +        restore_directory_from_friend $temp_restore_dir synapse
 | 
	
		
			
			|  | 125 | +        cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
 | 
	
		
			
			|  | 126 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 127 | +            exit 725
 | 
	
		
			
			|  | 128 | +        fi
 | 
	
		
			
			|  | 129 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 130 | +        chown -R synapse:synapse $SYNAPSE_DIR
 | 
	
		
			
			|  | 131 | +
 | 
	
		
			
			|  | 132 | +        systemctl start synapse
 | 
	
		
			
			|  | 133 | +    fi
 | 
	
		
			
			|  | 134 | +}
 | 
	
		
			
			|  | 135 | +
 | 
	
		
			
			|  | 136 | +function remove_synapse {
 | 
	
		
			
			|  | 137 | +    systemctl stop matrix-synapse
 | 
	
		
			
			|  | 138 | +    firewall_remove ${SYNAPSE_PORT}
 | 
	
		
			
			|  | 139 | +    rm -rf $SYNAPSE_DIR
 | 
	
		
			
			|  | 140 | +    apt-get -yq remove --purge matrix-synapse
 | 
	
		
			
			|  | 141 | +    apt-get -yq autoremove
 | 
	
		
			
			|  | 142 | +
 | 
	
		
			
			|  | 143 | +    rm /etc/apt/sources.list.d/synapse.list
 | 
	
		
			
			|  | 144 | +    apt-get update
 | 
	
		
			
			|  | 145 | +
 | 
	
		
			
			|  | 146 | +    remove_completion_param install_synapse
 | 
	
		
			
			|  | 147 | +    sed -i '/synapse/d' $COMPLETION_FILE
 | 
	
		
			
			|  | 148 | +    sed -i '/Synapse/d' /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 149 | +}
 | 
	
		
			
			|  | 150 | +
 | 
	
		
			
			|  | 151 | +function install_synapse {
 | 
	
		
			
			|  | 152 | +    if [[ ${ONION_ONLY} == 'no' ]]; then
 | 
	
		
			
			|  | 153 | +        # obtain a cert for the default domain
 | 
	
		
			
			|  | 154 | +        if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
 | 
	
		
			
			|  | 155 | +            echo $'Obtaining certificate for the main domain'
 | 
	
		
			
			|  | 156 | +            create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
 | 
	
		
			
			|  | 157 | +        fi
 | 
	
		
			
			|  | 158 | +    fi
 | 
	
		
			
			|  | 159 | +
 | 
	
		
			
			|  | 160 | +    if [ ! -d /etc/prosody ]; then
 | 
	
		
			
			|  | 161 | +        echo $'xmpp should be installed first'
 | 
	
		
			
			|  | 162 | +        exit 67382
 | 
	
		
			
			|  | 163 | +    fi
 | 
	
		
			
			|  | 164 | +
 | 
	
		
			
			|  | 165 | +    apt-get -yq install build-essential python2.7-dev libffi-dev \
 | 
	
		
			
			|  | 166 | +            python-pip python-setuptools sqlite3 \
 | 
	
		
			
			|  | 167 | +            libssl-dev libjpeg-dev libxslt1-dev python-virtualenv curl
 | 
	
		
			
			|  | 168 | +
 | 
	
		
			
			|  | 169 | +    curl -s https://matrix.org/packages/debian/repo-key.asc | apt-key add -
 | 
	
		
			
			|  | 170 | +    echo "deb https://matrix.org/packages/debian/ ${DEBIAN_VERSION} main" | tee /etc/apt/sources.list.d/synapse.list
 | 
	
		
			
			|  | 171 | +    apt-get update
 | 
	
		
			
			|  | 172 | +    apt-get -yq install python-cffi
 | 
	
		
			
			|  | 173 | +    apt-get -yq install python-nacl
 | 
	
		
			
			|  | 174 | +    apt-get -yq install python-signedjson
 | 
	
		
			
			|  | 175 | +
 | 
	
		
			
			|  | 176 | +    debconf-set-selections <<< "matrix-synapse matrix-synapse/server-name string $DEFAULT_DOMAIN_NAME"
 | 
	
		
			
			|  | 177 | +    debconf-set-selections <<< "matrix-synapse matrix-synapse/server_name string $DEFAULT_DOMAIN_NAME"
 | 
	
		
			
			|  | 178 | +    debconf-set-selections <<< "matrix-synapse matrix-synapse/report-stats boolean false"
 | 
	
		
			
			|  | 179 | +    apt-get -yq install matrix-synapse
 | 
	
		
			
			|  | 180 | +    if [ ! -d /etc/matrix-synapse ]; then
 | 
	
		
			
			|  | 181 | +        exit 653835
 | 
	
		
			
			|  | 182 | +    fi
 | 
	
		
			
			|  | 183 | +    systemctl stop matrix-synapse
 | 
	
		
			
			|  | 184 | +    systemctl start matrix-synapse
 | 
	
		
			
			|  | 185 | +
 | 
	
		
			
			|  | 186 | +    firewall_add synapse ${SYNAPSE_PORT}
 | 
	
		
			
			|  | 187 | +
 | 
	
		
			
			|  | 188 | +    SYNAPSE_ONION_HOSTNAME=$(add_onion_service synapse ${SYNAPSE_PORT} ${SYNAPSE_PORT})
 | 
	
		
			
			|  | 189 | +    if ! grep -q "Synapse onion domain" /home/$MY_USERNAME/README; then
 | 
	
		
			
			|  | 190 | +        echo $"Synapse onion domain: ${SYNAPSE_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 191 | +        echo '' >> /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 192 | +        chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 193 | +        chmod 600 /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 194 | +    else
 | 
	
		
			
			|  | 195 | +        if [ -f /home/$MY_USERNAME/README ]; then
 | 
	
		
			
			|  | 196 | +            sed -i "s|Synapse onion domain.*|Synapse onion domain: ${SYNAPSE_ONION_HOSTNAME}|g" /home/$MY_USERNAME/README
 | 
	
		
			
			|  | 197 | +        fi
 | 
	
		
			
			|  | 198 | +    fi
 | 
	
		
			
			|  | 199 | +
 | 
	
		
			
			|  | 200 | +    if [ ! ${SYNAPSE_PASSWORD} ]; then
 | 
	
		
			
			|  | 201 | +        if [ -f ${IMAGE_PASSWORD_FILE} ]; then
 | 
	
		
			
			|  | 202 | +            SYNAPSE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
 | 
	
		
			
			|  | 203 | +        else
 | 
	
		
			
			|  | 204 | +            SYNAPSE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
 | 
	
		
			
			|  | 205 | +        fi
 | 
	
		
			
			|  | 206 | +    fi
 | 
	
		
			
			|  | 207 | +
 | 
	
		
			
			|  | 208 | +    add_user_synapse "${MY_USERNAME}" "${SYNAPSE_PASSWORD}"
 | 
	
		
			
			|  | 209 | +
 | 
	
		
			
			|  | 210 | +    if ! grep -q $"Synapse administrator" /home/${MY_USERNAME}/README; then
 | 
	
		
			
			|  | 211 | +        echo '' >> /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 212 | +        echo $'# Synapse' >> /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 213 | +        echo $"Synapse administrator nickname: $MY_USERNAME" >> /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 214 | +        echo $"Synapse administrator password: $SYNAPSE_PASSWORD" >> /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 215 | +        chown ${MY_USERNAME}:${MY_USERNAME} /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 216 | +        chmod 600 /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 217 | +    else
 | 
	
		
			
			|  | 218 | +        if [ -f /home/${MY_USERNAME}/README ]; then
 | 
	
		
			
			|  | 219 | +            sed -i "s|Synapse administrator password.*|Synapse administrator password: $SYNAPSE_PASSWORD|g" /home/${MY_USERNAME}/README
 | 
	
		
			
			|  | 220 | +        fi
 | 
	
		
			
			|  | 221 | +    fi
 | 
	
		
			
			|  | 222 | +
 | 
	
		
			
			|  | 223 | +    APP_INSTALLED=1
 | 
	
		
			
			|  | 224 | +}
 | 
	
		
			
			|  | 225 | +
 | 
	
		
			
			|  | 226 | +# NOTE: deliberately no exit 0
 |