freedombone-app-synapse 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. VARIANTS='full full-vim chat'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. SYNAPSE_PORT=8448
  34. SYNAPSE_PASSWORD=
  35. SYNAPSE_DIR=/etc/matrix-synapse
  36. synapse_variables=(ONION_ONLY
  37. MY_USERNAME
  38. SYNAPSE_PASSWORD
  39. DEFAULT_DOMAIN_NAME)
  40. function can_install_synapse {
  41. check_architecture=$(uname -a)
  42. if [[ "$check_architecture" == *"amd64"* || "$check_architecture" == *"i386"* ]]; then
  43. echo "1"
  44. else
  45. echo "0"
  46. fi
  47. }
  48. function remove_user_synapse {
  49. remove_username="$1"
  50. # TODO
  51. }
  52. function add_user_synapse {
  53. new_username="$1"
  54. new_user_password="$2"
  55. cd $SYNAPSE_DIR
  56. register_new_matrix_user -c homeserver.yaml https://localhost:${SYNAPSE_PORT} -u "${new_username}" -p "${new_user_password}" -a
  57. echo '0'
  58. }
  59. function install_interactive_synapse {
  60. echo -n ''
  61. APP_INSTALLED=1
  62. }
  63. function change_password_synapse {
  64. echo -n ''
  65. }
  66. function reconfigure_synapse {
  67. echo -n ''
  68. }
  69. function upgrade_synapse {
  70. echo -n ''
  71. }
  72. function backup_local_synapse {
  73. source_directory=$SYNAPSE_DIR
  74. if [ -d $source_directory ]; then
  75. systemctl stop synapse
  76. function_check backup_directory_to_usb
  77. backup_directory_to_usb $source_directory synapse
  78. systemctl start synapse
  79. fi
  80. }
  81. function restore_local_synapse {
  82. if [ -d $SYNAPSE_DIR ]; then
  83. systemctl stop synapse
  84. temp_restore_dir=/root/tempsynapse
  85. function_check restore_directory_from_usb
  86. restore_directory_from_usb $temp_restore_dir synapse
  87. cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
  88. if [ ! "$?" = "0" ]; then
  89. function_check backup_unmount_drive
  90. backup_unmount_drive
  91. exit 725
  92. fi
  93. rm -rf $temp_restore_dir
  94. chown -R synapse:synapse $SYNAPSE_DIR
  95. systemctl start synapse
  96. fi
  97. }
  98. function backup_remote_synapse {
  99. source_directory=$SYNAPSE_DIR
  100. if [ -d $source_directory ]; then
  101. systemctl stop synapse
  102. function_check backup_directory_to_friend
  103. backup_directory_to_friend $source_directory synapse
  104. systemctl start synapse
  105. fi
  106. }
  107. function restore_remote_synapse {
  108. if [ -d $SYNAPSE_DIR ]; then
  109. systemctl stop synapse
  110. temp_restore_dir=/root/tempsynapse
  111. function_check restore_directory_from_friend
  112. restore_directory_from_friend $temp_restore_dir synapse
  113. cp -r $temp_restore_dir$SYNAPSE_DIR/* $SYNAPSE_DIR
  114. if [ ! "$?" = "0" ]; then
  115. exit 725
  116. fi
  117. rm -rf $temp_restore_dir
  118. chown -R synapse:synapse $SYNAPSE_DIR
  119. systemctl start synapse
  120. fi
  121. }
  122. function remove_synapse {
  123. systemctl stop matrix-synapse
  124. firewall_remove ${SYNAPSE_PORT}
  125. rm -rf $SYNAPSE_DIR
  126. apt-get -yq remove --purge matrix-synapse
  127. apt-get -yq autoremove
  128. rm /etc/apt/sources.list.d/synapse.list
  129. apt-get update
  130. remove_completion_param install_synapse
  131. sed -i '/synapse/d' $COMPLETION_FILE
  132. sed -i '/Synapse/d' /home/$MY_USERNAME/README
  133. }
  134. function install_synapse {
  135. if [[ "$(can_install_synapse)" == "0" ]]; then
  136. echo $'Matrix/Synapse can only be installed on i386 or amd64 architectures'
  137. exit 36734
  138. fi
  139. if [[ ${ONION_ONLY} == 'no' ]]; then
  140. # obtain a cert for the default domain
  141. if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
  142. echo $'Obtaining certificate for the main domain'
  143. create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
  144. fi
  145. fi
  146. if [ ! -d /etc/prosody ]; then
  147. echo $'xmpp should be installed first'
  148. exit 67382
  149. fi
  150. apt-get -yq install build-essential python2.7-dev libffi-dev \
  151. python-pip python-setuptools sqlite3 \
  152. libssl-dev libjpeg-dev libxslt1-dev python-virtualenv curl
  153. curl -s https://matrix.org/packages/debian/repo-key.asc | apt-key add -
  154. echo "deb https://matrix.org/packages/debian/ ${DEBIAN_VERSION} main" | tee /etc/apt/sources.list.d/synapse.list
  155. apt-get update
  156. apt-get -yq install python-cffi
  157. apt-get -yq install python-nacl
  158. apt-get -yq install python-signedjson
  159. debconf-set-selections <<< "matrix-synapse matrix-synapse/server-name string $DEFAULT_DOMAIN_NAME"
  160. debconf-set-selections <<< "matrix-synapse matrix-synapse/server_name string $DEFAULT_DOMAIN_NAME"
  161. debconf-set-selections <<< "matrix-synapse matrix-synapse/report-stats boolean false"
  162. apt-get -yq install matrix-synapse
  163. if [ ! -d /etc/matrix-synapse ]; then
  164. exit 653835
  165. fi
  166. systemctl stop matrix-synapse
  167. systemctl start matrix-synapse
  168. firewall_add synapse ${SYNAPSE_PORT}
  169. SYNAPSE_ONION_HOSTNAME=$(add_onion_service synapse ${SYNAPSE_PORT} ${SYNAPSE_PORT})
  170. if ! grep -q "Synapse onion domain" /home/$MY_USERNAME/README; then
  171. echo $"Synapse onion domain: ${SYNAPSE_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  172. echo '' >> /home/$MY_USERNAME/README
  173. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  174. chmod 600 /home/$MY_USERNAME/README
  175. else
  176. if [ -f /home/$MY_USERNAME/README ]; then
  177. sed -i "s|Synapse onion domain.*|Synapse onion domain: ${SYNAPSE_ONION_HOSTNAME}|g" /home/$MY_USERNAME/README
  178. fi
  179. fi
  180. if [ ! ${SYNAPSE_PASSWORD} ]; then
  181. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  182. SYNAPSE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  183. else
  184. SYNAPSE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  185. fi
  186. fi
  187. add_user_synapse "${MY_USERNAME}" "${SYNAPSE_PASSWORD}"
  188. if ! grep -q $"Synapse administrator" /home/${MY_USERNAME}/README; then
  189. echo '' >> /home/${MY_USERNAME}/README
  190. echo $'# Synapse' >> /home/${MY_USERNAME}/README
  191. echo $"Synapse administrator nickname: $MY_USERNAME" >> /home/${MY_USERNAME}/README
  192. echo $"Synapse administrator password: $SYNAPSE_PASSWORD" >> /home/${MY_USERNAME}/README
  193. chown ${MY_USERNAME}:${MY_USERNAME} /home/${MY_USERNAME}/README
  194. chmod 600 /home/${MY_USERNAME}/README
  195. else
  196. if [ -f /home/${MY_USERNAME}/README ]; then
  197. sed -i "s|Synapse administrator password.*|Synapse administrator password: $SYNAPSE_PASSWORD|g" /home/${MY_USERNAME}/README
  198. fi
  199. fi
  200. APP_INSTALLED=1
  201. }
  202. # NOTE: deliberately no exit 0