freedombone-app-matrix 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # matrix server
  12. #
  13. # https://raw.githubusercontent.com/silvio/docker-matrix
  14. # https://matrix.org/blog/2016/02/10/advanced-synapse-setup-with-lets-encrypt
  15. #
  16. # Test by visiting https://$MATRIX_DOMAIN_NAME/_matrix/key/v2/server/auto
  17. #
  18. # License
  19. # =======
  20. #
  21. # Copyright (C) 2016-2017 Bob Mottram <bob@freedombone.net>
  22. #
  23. # This program is free software: you can redistribute it and/or modify
  24. # it under the terms of the GNU Affero General Public License as published by
  25. # the Free Software Foundation, either version 3 of the License, or
  26. # (at your option) any later version.
  27. #
  28. # This program is distributed in the hope that it will be useful,
  29. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. # GNU Affero General Public License for more details.
  32. #
  33. # You should have received a copy of the GNU Affero General Public License
  34. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  35. # Login username is @username:domain
  36. VARIANTS='full full-vim chat'
  37. IN_DEFAULT_INSTALL=0
  38. SHOW_ON_ABOUT=1
  39. MATRIX_DOMAIN_NAME=
  40. MATRIX_CODE=
  41. MATRIX_DATA_DIR='/var/lib/matrix'
  42. MATRIX_HTTP_PORT=8448
  43. MATRIX_ID_HTTP_PORT=8557
  44. MATRIX_PORT=8008
  45. MATRIX_ID_PORT=8081
  46. MATRIX_ONION_PORT=8109
  47. MATRIX_ID_ONION_PORT=8111
  48. MATRIX_REPO="https://github.com/matrix-org/synapse"
  49. MATRIX_COMMIT='c45dc6c62aa2a2e83a10d8116a709dfd8c144e3c'
  50. SYDENT_REPO="https://github.com/matrix-org/sydent"
  51. SYDENT_COMMIT='d087278afd712222653b69ff72bd8ff4aa0180ec'
  52. REPORT_STATS="no"
  53. MATRIX_SECRET=
  54. matrix_variables=(ONION_ONLY
  55. MY_USERNAME
  56. MATRIX_SECRET
  57. DEFAULT_DOMAIN_NAME
  58. MATRIX_DOMAIN_NAME
  59. MATRIX_CODE)
  60. function matrix_nginx {
  61. matrix_nginx_site=/etc/nginx/sites-available/$MATRIX_DOMAIN_NAME
  62. if [[ $ONION_ONLY == "no" ]]; then
  63. echo 'server {' > $matrix_nginx_site
  64. echo " listen 443 ssl;" >> $matrix_nginx_site
  65. echo " listen [::]:443 ssl;" >> $matrix_nginx_site
  66. echo " server_name ${MATRIX_DOMAIN_NAME};" >> $matrix_nginx_site
  67. echo '' >> $matrix_nginx_site
  68. echo ' # Security' >> $matrix_nginx_site
  69. function_check nginx_ssl
  70. nginx_ssl ${MATRIX_DOMAIN_NAME}
  71. function_check nginx_disable_sniffing
  72. nginx_disable_sniffing ${MATRIX_DOMAIN_NAME}
  73. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $matrix_nginx_site
  74. echo '' >> $matrix_nginx_site
  75. echo ' # Logs' >> $matrix_nginx_site
  76. echo ' access_log /dev/null;' >> $matrix_nginx_site
  77. echo ' error_log /dev/null;' >> $matrix_nginx_site
  78. echo '' >> $matrix_nginx_site
  79. echo ' # Index' >> $matrix_nginx_site
  80. echo ' index index.html;' >> $matrix_nginx_site
  81. echo '' >> $matrix_nginx_site
  82. echo ' # Location' >> $matrix_nginx_site
  83. echo ' location /_matrix {' >> $matrix_nginx_site
  84. function_check nginx_limits
  85. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  86. echo ' proxy_pass http://localhost:8008;' >> $matrix_nginx_site
  87. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $matrix_nginx_site
  88. echo ' }' >> $matrix_nginx_site
  89. echo '}' >> $matrix_nginx_site
  90. echo '' >> $matrix_nginx_site
  91. echo 'server {' >> $matrix_nginx_site
  92. echo " listen ${MATRIX_ID_HTTP_PORT} ssl;" >> $matrix_nginx_site
  93. echo " listen [::]:${MATRIX_ID_HTTP_PORT} ssl;" >> $matrix_nginx_site
  94. echo " server_name ${MATRIX_DOMAIN_NAME};" >> $matrix_nginx_site
  95. echo '' >> $matrix_nginx_site
  96. echo ' # Security' >> $matrix_nginx_site
  97. function_check nginx_ssl
  98. nginx_ssl ${MATRIX_DOMAIN_NAME}
  99. function_check nginx_disable_sniffing
  100. nginx_disable_sniffing ${MATRIX_DOMAIN_NAME}
  101. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $matrix_nginx_site
  102. echo '' >> $matrix_nginx_site
  103. echo ' # Logs' >> $matrix_nginx_site
  104. echo ' access_log /dev/null;' >> $matrix_nginx_site
  105. echo ' error_log /dev/null;' >> $matrix_nginx_site
  106. echo '' >> $matrix_nginx_site
  107. echo ' # Index' >> $matrix_nginx_site
  108. echo ' index index.html;' >> $matrix_nginx_site
  109. echo '' >> $matrix_nginx_site
  110. echo ' # Location' >> $matrix_nginx_site
  111. echo ' location /_matrix {' >> $matrix_nginx_site
  112. function_check nginx_limits
  113. nginx_limits ${MATRIX_DOMAIN_NAME} '15m'
  114. echo ' proxy_pass http://localhost:8008;' >> $matrix_nginx_site
  115. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $matrix_nginx_site
  116. echo ' }' >> $matrix_nginx_site
  117. echo '}' >> $matrix_nginx_site
  118. echo '' >> $matrix_nginx_site
  119. else
  120. echo -n '' > $matrix_nginx_site
  121. fi
  122. echo 'server {' >> $matrix_nginx_site
  123. echo " listen 127.0.0.1:$MATRIX_ONION_PORT default_server;" >> $matrix_nginx_site
  124. echo " server_name $MATRIX_DOMAIN_NAME;" >> $matrix_nginx_site
  125. echo '' >> $matrix_nginx_site
  126. function_check nginx_disable_sniffing
  127. nginx_disable_sniffing $MATRIX_DOMAIN_NAME
  128. echo '' >> $matrix_nginx_site
  129. echo ' # Logs' >> $matrix_nginx_site
  130. echo ' access_log /dev/null;' >> $matrix_nginx_site
  131. echo ' error_log /dev/null;' >> $matrix_nginx_site
  132. echo '' >> $matrix_nginx_site
  133. echo ' # Location' >> $matrix_nginx_site
  134. echo ' location / {' >> $matrix_nginx_site
  135. function_check nginx_limits
  136. nginx_limits $MATRIX_DOMAIN_NAME '15m'
  137. echo " proxy_pass http://localhost:${MATRIX_PORT}/_matrix;" >> $matrix_nginx_site
  138. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $matrix_nginx_site
  139. echo ' }' >> $matrix_nginx_site
  140. echo '}' >> $matrix_nginx_site
  141. echo '' >> $matrix_nginx_site
  142. echo 'server {' >> $matrix_nginx_site
  143. echo " listen 127.0.0.1:$MATRIX_ID_ONION_PORT default_server;" >> $matrix_nginx_site
  144. echo " server_name $MATRIX_DOMAIN_NAME;" >> $matrix_nginx_site
  145. echo '' >> $matrix_nginx_site
  146. function_check nginx_disable_sniffing
  147. nginx_disable_sniffing $MATRIX_DOMAIN_NAME
  148. echo '' >> $matrix_nginx_site
  149. echo ' # Logs' >> $matrix_nginx_site
  150. echo ' access_log /dev/null;' >> $matrix_nginx_site
  151. echo ' error_log /dev/null;' >> $matrix_nginx_site
  152. echo '' >> $matrix_nginx_site
  153. echo ' # Location' >> $matrix_nginx_site
  154. echo ' location / {' >> $matrix_nginx_site
  155. function_check nginx_limits
  156. nginx_limits $MATRIX_DOMAIN_NAME '15m'
  157. echo " proxy_pass http://localhost:${MATRIX_ID_PORT};" >> $matrix_nginx_site
  158. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $matrix_nginx_site
  159. echo ' }' >> $matrix_nginx_site
  160. echo '}' >> $matrix_nginx_site
  161. if [ ! -d /var/www/$MATRIX_DOMAIN_NAME ]; then
  162. mkdir -p /var/www/$MATRIX_DOMAIN_NAME/htdocs
  163. fi
  164. function_check add_ddns_domain
  165. add_ddns_domain $MATRIX_DOMAIN_NAME
  166. function_check create_site_certificate
  167. create_site_certificate $MATRIX_DOMAIN_NAME
  168. systemctl restart nginx
  169. systemctl restart turn
  170. systemctl restart matrix
  171. if [ -f /etc/systemd/system/sydent.service ]; then
  172. systemctl restart sydent
  173. fi
  174. # wait for nginx to start otherwise user add fails later
  175. sleep 5
  176. }
  177. function matrix_generate_homeserver_file {
  178. local filepath="${1}"
  179. cd /etc/matrix
  180. python -m synapse.app.homeserver \
  181. --config-path "${filepath}" \
  182. --generate-config \
  183. --report-stats ${REPORT_STATS} \
  184. --server-name ${MATRIX_DOMAIN_NAME}
  185. }
  186. function matrix_generate_identityserver_file {
  187. local filepath="${1}"
  188. cd /etc/sydent
  189. python -m sydent.sydent \
  190. --config-path "${filepath}" \
  191. --generate-config \
  192. --report-stats ${REPORT_STATS} \
  193. --server-name ${MATRIX_DOMAIN_NAME}
  194. }
  195. function matrix_configure_homeserver_yaml {
  196. local turnkey="${1}"
  197. local filepath="${2}"
  198. local ymltemp="$(mktemp)"
  199. awk -v TURNURIES="turn_uris: [\"turn:${MATRIX_DOMAIN_NAME}:${TURN_HTTP_PORT}?transport=udp\", \"turn:${DEFAULT_DOMAIN_NAME}:${TURN_HTTP_PORT}?transport=tcp\"]" \
  200. -v TURNSHAREDSECRET="turn_shared_secret: \"${turnkey}\"" \
  201. -v PIDFILE="pid_file: ${MATRIX_DATA_DIR}/homeserver.pid" \
  202. -v DATABASE="database: \"${MATRIX_DATA_DIR}/homeserver.db\"" \
  203. -v LOGFILE="log_file: \"/dev/null\"" \
  204. -v MEDIASTORE="media_store_path: \"${MATRIX_DATA_DIR}/media_store\"" \
  205. '{
  206. sub(/turn_shared_secret: "YOUR_SHARED_SECRET"/, TURNSHAREDSECRET);
  207. sub(/turn_uris: \[\]/, TURNURIES);
  208. sub(/pid_file: \/homeserver.pid/, PIDFILE);
  209. sub(/database: "\/homeserver.db"/, DATABASE);
  210. sub(/log_file: "\/homeserver.log"/, LOGFILE);
  211. sub(/media_store_path: "\/media_store"/, MEDIASTORE);
  212. print;
  213. }' "${filepath}" > "${ymltemp}"
  214. mv ${ymltemp} "${filepath}"
  215. sed -i 's|no_tls: .*|no_tls: False|g' "${filepath}"
  216. sed -i ':a;N;$!ba;s/ tls: [^\n]*/ tls: False/2' "${filepath}"
  217. sed -i 's|enable_registration_captcha.*|enable_registration_captcha: False|g' "${filepath}"
  218. sed -i "s|database: \".*|database: \"${MATRIX_DATA_DIR}/homeserver.db\"|g" "${filepath}"
  219. sed -i "s|media_store_path:.*|media_store_path: \"${MATRIX_DATA_DIR}/media_store\"|g" "${filepath}"
  220. sed -i "s|pid_file:.*|pid_file: \"${MATRIX_DATA_DIR}/homeserver.pid\"|g" "${filepath}"
  221. sed -i "s|log_file:.*|log_file: \"/dev/null\"|g" "${filepath}"
  222. sed -i 's|bind_address:.*|bind_address: 127.0.0.1|g' "${filepath}"
  223. sed -i "s|bind_addresses:.*|bind_addresses: ['127.0.0.1']|g" "${filepath}"
  224. sed -i 's|x_forwarded:.*|x_forwarded: false|g' "${filepath}"
  225. sed -i "s|server_name:.*|server_name: \"${MATRIX_DOMAIN_NAME}\"|g" "${filepath}"
  226. sed -i "/trusted_third_party_id_servers:/a - ${MATRIX_DOMAIN_NAME}" "${filepath}"
  227. sed -i "s|- ${MATRIX_DOMAIN_NAME}| - ${MATRIX_DOMAIN_NAME}|g" "${filepath}"
  228. sed -i "s|enable_registration:.*|enable_registration: False|g" "${filepath}"
  229. }
  230. function matrix_configure_identityserver {
  231. local filepath=/etc/sydent/sydent.conf
  232. sed -i "s|http.port.*|http.port = $MATRIX_ID_PORT|g" ${filepath}
  233. sed -i "s|db.file.*|db.file = /etc/sydent/sydent.db|g" ${filepath}
  234. sed -i "s|Sydent Validation|Freedombone Matrix Account Validation|g" ${filepath}
  235. sed -i "s|pidfile.path.*|pidfile.path = /etc/sydent/sydent.pid|g" ${filepath}
  236. sed -i "s|log.path.*|log.path = /dev/null|g" ${filepath}
  237. sed -i "s|server.name.*|server.name = ${MATRIX_DOMAIN_NAME}|g" ${filepath}
  238. }
  239. function matrix_diff {
  240. DIFFPARAMS="${DIFFPARAMS:-Naur}"
  241. MATRIX_DOMAIN_NAME="${MATRIX_DOMAIN_NAME:-demo_server_name}"
  242. REPORT_STATS="${REPORT_STATS:-no_or_yes}"
  243. export MATRIX_DOMAIN_NAME REPORT_STATS
  244. matrix_generate_synapse_file $INSTALL_DIR/homeserver.synapse.yaml
  245. diff -${DIFFPARAMS} $INSTALL_DIR/homeserver.synapse.yaml ${MATRIX_DATA_DIR}/homeserver.yaml
  246. rm $INSTALL_DIR/homeserver.synapse.yaml
  247. }
  248. function matrix_generate {
  249. breakup="0"
  250. [[ -z "${MATRIX_DOMAIN_NAME}" ]] && echo "STOP! environment variable MATRIX_DOMAIN_NAME must be set" && breakup="1"
  251. [[ -z "${REPORT_STATS}" ]] && echo "STOP! environment variable REPORT_STATS must be set to 'no' or 'yes'" && breakup="1"
  252. [[ "${breakup}" == "1" ]] && exit 1
  253. [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
  254. echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
  255. homeserver_config=${MATRIX_DATA_DIR}/homeserver.yaml
  256. if [ -f $homeserver_config ]; then
  257. rm $homeserver_config
  258. fi
  259. matrix_generate_homeserver_file $homeserver_config
  260. matrix_configure_homeserver_yaml "${turnkey}" $homeserver_config
  261. }
  262. function remove_user_matrix {
  263. remove_username="$1"
  264. ${PROJECT_NAME}-pass -u $remove_username --rmapp matrix
  265. # TODO: There is no user removal script within synapse
  266. }
  267. function add_user_matrix {
  268. new_username="$1"
  269. new_user_password="$2"
  270. is_admin_user='-a'
  271. if [[ "$new_username" != "$MY_USERNAME" ]]; then
  272. is_admin_user=''
  273. fi
  274. ${PROJECT_NAME}-pass -u $new_username -a matrix -p "$new_user_password"
  275. if [[ $ONION_ONLY == 'no' ]]; then
  276. retval=$(register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml -u "${new_username}" -p "${new_user_password}" $is_admin_user https://${MATRIX_DOMAIN_NAME})
  277. else
  278. retval=$(register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml -u "${new_username}" -p "${new_user_password}" $is_admin_user http://${MATRIX_DOMAIN_NAME})
  279. fi
  280. echo "0"
  281. }
  282. function install_interactive_matrix {
  283. if [ ! $ONION_ONLY ]; then
  284. ONION_ONLY='no'
  285. fi
  286. if [[ $ONION_ONLY != "no" ]]; then
  287. MATRIX_DOMAIN_NAME='matrix.local'
  288. write_config_param "MATRIX_DOMAIN_NAME" "$MATRIX_DOMAIN_NAME"
  289. else
  290. function_check interactive_site_details
  291. interactive_site_details "matrix" "MATRIX_DOMAIN_NAME" "MATRIX_CODE"
  292. if [ ! $MATRIX_DOMAIN_NAME ]; then
  293. return
  294. fi
  295. read_config_param "MATRIX_DOMAIN_NAME"
  296. read_config_param "MATRIX_CODE"
  297. fi
  298. APP_INSTALLED=1
  299. }
  300. function change_password_matrix {
  301. curr_username="$1"
  302. new_user_password="$2"
  303. #${PROJECT_NAME}-pass -u "$curr_username" -a matrix -p "$new_user_password"
  304. }
  305. function reconfigure_matrix {
  306. echo -n ''
  307. }
  308. function upgrade_matrix {
  309. if [ ! -d /etc/sydent ]; then
  310. return
  311. fi
  312. if [ ! -d /etc/matrix ]; then
  313. return
  314. fi
  315. systemctl stop turn
  316. systemctl stop matrix
  317. systemctl stop sydent
  318. function_check set_repo_commit
  319. set_repo_commit /etc/matrix "matrix commit" "$MATRIX_COMMIT" $MATRIX_REPO
  320. cd /etc/matrix
  321. pip install --upgrade --process-dependency-links .
  322. set_repo_commit /etc/sydent "sydent commit" "$SYDENT_COMMIT" $SYDENT_REPO
  323. cd /etc/sydent
  324. pip install --upgrade --process-dependency-links .
  325. sed -i 's/ssl.PROTOCOL_SSLv23/ssl.PROTOCOL_TLSv1/g' /usr/local/bin/register_new_matrix_user
  326. chown -R matrix:matrix /etc/matrix
  327. chown -R matrix:matrix /etc/sydent
  328. chown -R matrix:matrix $MATRIX_DATA_DIR
  329. pip install --upgrade --force "pynacl==0.3.0"
  330. systemctl start turn
  331. systemctl start matrix
  332. systemctl start sydent
  333. }
  334. function backup_local_matrix {
  335. source_directory=/etc/matrix
  336. if [ -d $source_directory ]; then
  337. systemctl stop turn
  338. systemctl stop matrix
  339. systemctl stop sydent
  340. function_check backup_directory_to_usb
  341. backup_directory_to_usb $source_directory matrix
  342. source_directory=$MATRIX_DATA_DIR
  343. if [ -d $source_directory ]; then
  344. backup_directory_to_usb $source_directory matrixdata
  345. fi
  346. source_directory=/etc/sydent
  347. if [ -d $source_directory ]; then
  348. backup_directory_to_usb $source_directory matrixid
  349. fi
  350. systemctl start turn
  351. systemctl start matrix
  352. systemctl start sydent
  353. fi
  354. }
  355. function restore_local_matrix {
  356. if [ -d /etc/matrix ]; then
  357. systemctl stop turn
  358. systemctl stop matrix
  359. systemctl stop sydent
  360. temp_restore_dir=/root/tempmatrix
  361. function_check restore_directory_from_usb
  362. restore_directory_from_usb $temp_restore_dir matrix
  363. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  364. if [ ! "$?" = "0" ]; then
  365. function_check backup_unmount_drive
  366. backup_unmount_drive
  367. exit 3783
  368. fi
  369. rm -rf $temp_restore_dir
  370. chown -R matrix:matrix /etc/matrix
  371. temp_restore_dir=/root/tempmatrixdata
  372. restore_directory_from_usb $temp_restore_dir matrixdata
  373. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  374. if [ ! "$?" = "0" ]; then
  375. function_check backup_unmount_drive
  376. backup_unmount_drive
  377. exit 78352
  378. fi
  379. rm -rf $temp_restore_dir
  380. chown -R matrix:matrix $MATRIX_DATA_DIR
  381. temp_restore_dir=/root/tempmatrixid
  382. restore_directory_from_usb $temp_restore_dir matrixid
  383. cp -r $temp_restore_dir/etc/sydent/* /etc/sydent
  384. if [ ! "$?" = "0" ]; then
  385. function_check backup_unmount_drive
  386. backup_unmount_drive
  387. exit 29562
  388. fi
  389. rm -rf $temp_restore_dir
  390. chown -R matrix:matrix /etc/sydent
  391. systemctl start turn
  392. systemctl start matrix
  393. systemctl start sydent
  394. fi
  395. }
  396. function backup_remote_matrix {
  397. source_directory=/etc/matrix
  398. if [ -d $source_directory ]; then
  399. systemctl stop turn
  400. systemctl stop matrix
  401. systemctl stop sydent
  402. function_check backup_directory_to_friend
  403. backup_directory_to_friend $source_directory matrix
  404. source_directory=$MATRIX_DATA_DIR
  405. if [ -d $source_directory ]; then
  406. backup_directory_to_friend $source_directory matrixdata
  407. fi
  408. source_directory=/etc/sydent
  409. if [ -d $source_directory ]; then
  410. backup_directory_to_friend $source_directory matrixid
  411. fi
  412. systemctl start turn
  413. systemctl start matrix
  414. systemctl start sydent
  415. fi
  416. }
  417. function restore_remote_matrix {
  418. if [ -d /etc/matrix ]; then
  419. systemctl stop turn
  420. systemctl stop matrix
  421. systemctl stop sydent
  422. temp_restore_dir=/root/tempmatrix
  423. function_check restore_directory_from_friend
  424. restore_directory_from_friend $temp_restore_dir matrix
  425. cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
  426. if [ ! "$?" = "0" ]; then
  427. exit 38935
  428. fi
  429. rm -rf $temp_restore_dir
  430. chown -R matrix:matrix /etc/matrix
  431. temp_restore_dir=/root/tempmatrixdata
  432. restore_directory_from_friend $temp_restore_dir matrixdata
  433. cp -r $temp_restore_dir$MATRIX_DATA_DIR/* $MATRIX_DATA_DIR
  434. if [ ! "$?" = "0" ]; then
  435. exit 60923
  436. fi
  437. rm -rf $temp_restore_dir
  438. chown -R matrix:matrix $MATRIX_DATA_DIR
  439. temp_restore_dir=/root/tempmatrixid
  440. restore_directory_from_friend $temp_restore_dir matrixid
  441. cp -r $temp_restore_dir/etc/sydent/* /etc/sydent
  442. if [ ! "$?" = "0" ]; then
  443. exit 738356
  444. fi
  445. rm -rf $temp_restore_dir
  446. chown -R matrix:matrix /etc/sydent
  447. systemctl start turn
  448. systemctl start matrix
  449. systemctl start sydent
  450. fi
  451. }
  452. function remove_matrix {
  453. firewall_remove ${MATRIX_HTTP_PORT}
  454. nginx_dissite $MATRIX_DOMAIN_NAME
  455. remove_certs $MATRIX_DOMAIN_NAME
  456. if [ -f /etc/nginx/sites-available/$MATRIX_DOMAIN_NAME ]; then
  457. rm /etc/nginx/sites-available/$MATRIX_DOMAIN_NAME
  458. fi
  459. if [ ! -d /var/www/$MATRIX_DOMAIN_NAME ]; then
  460. rm -rf /var/www/$MATRIX_DOMAIN_NAME
  461. fi
  462. function_check remove_ddns_domain
  463. remove_ddns_domain $MATRIX_DOMAIN_NAME
  464. systemctl stop matrix
  465. if [ -f /etc/systemd/system/sydent.service ]; then
  466. systemctl stop sydent
  467. fi
  468. function_check remove_turn
  469. remove_turn
  470. systemctl disable matrix
  471. if [ -f /etc/systemd/system/sydent.service ]; then
  472. systemctl disable sydent
  473. rm /etc/systemd/system/sydent.service
  474. fi
  475. if [ -f /etc/systemd/system/matrix.service ]; then
  476. rm /etc/systemd/system/matrix.service
  477. fi
  478. apt-get -y remove --purge coturn
  479. cd /etc/matrix
  480. pip uninstall .
  481. if [ -d /etc/sydent ]; then
  482. cd /etc/sydent
  483. pip uninstall .
  484. rm -rf /etc/sydent
  485. fi
  486. rm -rf $MATRIX_DATA_DIR
  487. rm -rf /etc/matrix
  488. deluser matrix
  489. delgroup matrix
  490. remove_onion_service matrix ${MATRIX_ONION_PORT}
  491. remove_onion_service matrix ${MATRIX_ID_ONION_PORT}
  492. #sed -i "/# Matrix Server/,/# End of Matrix Server/d" /etc/nginx/sites-available/${MATRIX_DOMAIN_NAME}
  493. systemctl restart nginx
  494. remove_completion_param install_matrix
  495. sed -i '/matrix/d' $COMPLETION_FILE
  496. }
  497. function install_identity_server {
  498. if [ ! -d /etc/sydent ]; then
  499. function_check git_clone
  500. git_clone $SYDENT_REPO /etc/sydent
  501. if [ ! -d /etc/sydent ]; then
  502. echo $'Unable to clone sydent repo'
  503. exit 936525
  504. fi
  505. fi
  506. cd /etc/sydent
  507. git checkout $SYDENT_COMMIT -b $SYDENT_COMMIT
  508. set_completion_param "sydent commit" "$SYDENT_COMMIT"
  509. if [ ! -d $INSTALL_DIR/sydent ]; then
  510. mkdir -p $INSTALL_DIR/sydent
  511. fi
  512. if [ -d $INSTALL_DIR/sydent ]; then
  513. rm -rf $INSTALL_DIR/sydent/*
  514. fi
  515. sed -i "s|8090|${MATRIX_ID_PORT}|g" /etc/sydent/sydent/sydent.py
  516. python setup.py install
  517. pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/sydent
  518. if [ ! "$?" = "0" ]; then
  519. echo $'Failed to install matrix identity server'
  520. exit 798362
  521. fi
  522. #function_check matrix_generate_identityserver_file
  523. #matrix_generate_identityserver_file /etc/sydent/sydent.conf
  524. #if [ ! -f /etc/sydent/sydent.conf ]; then
  525. # echo $'Matrix identity server configuration not generated'
  526. # exit 72528
  527. #fi
  528. #function_check matrix_configure_identityserver
  529. #matrix_configure_identityserver
  530. chmod -R 700 /etc/sydent/sydent.conf
  531. chown -R matrix:matrix /etc/sydent
  532. echo '[Unit]' > /etc/systemd/system/sydent.service
  533. echo 'Description=Sydent Matrix identity server' >> /etc/systemd/system/sydent.service
  534. echo 'After=network.target nginx.target' >> /etc/systemd/system/sydent.service
  535. echo '' >> /etc/systemd/system/sydent.service
  536. echo '[Service]' >> /etc/systemd/system/sydent.service
  537. echo 'Type=simple' >> /etc/systemd/system/sydent.service
  538. echo 'User=matrix' >> /etc/systemd/system/sydent.service
  539. echo "WorkingDirectory=/etc/sydent" >> /etc/systemd/system/sydent.service
  540. echo "ExecStart=/usr/bin/python -m sydent.sydent --config-path /etc/sydent/sydent.conf --report-stats ${REPORT_STATS} --server-name ${MATRIX_DOMAIN_NAME}" >> /etc/systemd/system/sydent.service
  541. echo 'Restart=always' >> /etc/systemd/system/sydent.service
  542. echo 'RestartSec=10' >> /etc/systemd/system/sydent.service
  543. echo '' >> /etc/systemd/system/sydent.service
  544. echo '[Install]' >> /etc/systemd/system/sydent.service
  545. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/sydent.service
  546. systemctl enable sydent
  547. systemctl daemon-reload
  548. systemctl start sydent
  549. sleep 5
  550. if [ ! -f /etc/sydent/sydent.conf ]; then
  551. echo $'Matrix identity server config was not generated'
  552. exit 82352
  553. fi
  554. if [ ! -f /etc/sydent/sydent.db ]; then
  555. echo $'No matrix identity server database was created'
  556. exit 7354383
  557. fi
  558. chmod -R 700 /etc/sydent/sydent.db
  559. }
  560. function install_home_server {
  561. if [ ! -d /etc/matrix ]; then
  562. function_check git_clone
  563. git_clone $MATRIX_REPO /etc/matrix
  564. if [ ! -d /etc/matrix ]; then
  565. echo $'Unable to clone matrix repo'
  566. exit 6724683
  567. fi
  568. fi
  569. cd /etc/matrix
  570. git checkout $MATRIX_COMMIT -b $MATRIX_COMMIT
  571. set_completion_param "matrix commit" "$MATRIX_COMMIT"
  572. if [ ! -d $INSTALL_DIR/matrix ]; then
  573. mkdir -p $INSTALL_DIR/matrix
  574. fi
  575. rm -rf /usr/local/lib/python2.7/dist-packages/ldap*
  576. if [ -d $INSTALL_DIR/matrix ]; then
  577. rm -rf $INSTALL_DIR/matrix/*
  578. fi
  579. pip install --upgrade --process-dependency-links . -b $INSTALL_DIR/matrix
  580. if [ ! "$?" = "0" ]; then
  581. echo $'Failed to install matrix home server'
  582. exit 782542
  583. fi
  584. if [ ! -d $MATRIX_DATA_DIR ]; then
  585. mkdir $MATRIX_DATA_DIR
  586. fi
  587. groupadd matrix
  588. useradd -c "Matrix system account" -d $MATRIX_DATA_DIR -m -r -g matrix matrix
  589. usermod -a -G www-data matrix
  590. function_check install_turn
  591. install_turn
  592. MATRIX_SECRET="${turnkey}"
  593. function_check matrix_generate
  594. matrix_generate
  595. if [[ -z ${MATRIX_DATA_DIR}/homeserver.yaml ]]; then
  596. echo $'homeserver.yaml is zero size'
  597. exit 783724
  598. fi
  599. # Disable the web client
  600. sed -i 's|web_client:.*|web_client: False|g' $MATRIX_DATA_DIR/homeserver.yaml
  601. sed -i 's|, webclient||g' $MATRIX_DATA_DIR/homeserver.yaml
  602. sed -i "/- '0.0.0.0'/d" $MATRIX_DATA_DIR/homeserver.yaml
  603. chmod -R 700 $MATRIX_DATA_DIR/homeserver.yaml
  604. chown -R matrix:matrix /etc/matrix
  605. chown -R matrix:matrix $MATRIX_DATA_DIR
  606. sed -i 's/ssl.PROTOCOL_SSLv23/ssl.PROTOCOL_TLSv1/g' /usr/local/bin/register_new_matrix_user
  607. echo '[Unit]' > /etc/systemd/system/matrix.service
  608. echo 'Description=Synapse Matrix homeserver' >> /etc/systemd/system/matrix.service
  609. echo 'After=network.target nginx.target' >> /etc/systemd/system/matrix.service
  610. echo '' >> /etc/systemd/system/matrix.service
  611. echo '[Service]' >> /etc/systemd/system/matrix.service
  612. echo 'Type=simple' >> /etc/systemd/system/matrix.service
  613. echo 'User=matrix' >> /etc/systemd/system/matrix.service
  614. echo "WorkingDirectory=/etc/matrix" >> /etc/systemd/system/matrix.service
  615. echo "ExecStartPre=/usr/bin/python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml --generate-keys" >> /etc/systemd/system/matrix.service
  616. echo "ExecStart=/usr/bin/python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml" >> /etc/systemd/system/matrix.service
  617. echo 'Restart=on-failure' >> /etc/systemd/system/matrix.service
  618. echo 'RestartSec=10' >> /etc/systemd/system/matrix.service
  619. echo '' >> /etc/systemd/system/matrix.service
  620. echo '[Install]' >> /etc/systemd/system/matrix.service
  621. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/matrix.service
  622. systemctl enable matrix
  623. systemctl daemon-reload
  624. systemctl start matrix
  625. # wait for the database to be created
  626. sleep 5
  627. if [ ! -f $MATRIX_DATA_DIR/homeserver.db ]; then
  628. echo $'No matrix home server database was created'
  629. exit 23782
  630. fi
  631. chmod -R 700 $MATRIX_DATA_DIR/homeserver.db
  632. MATRIX_ONION_HOSTNAME=$(add_onion_service matrix ${MATRIX_PORT} ${MATRIX_ONION_PORT})
  633. MATRIX_ID_ONION_HOSTNAME=$(add_onion_service matrixid ${MATRIX_ID_PORT} ${MATRIX_ID_ONION_PORT})
  634. if [ ! ${MATRIX_PASSWORD} ]; then
  635. if [ -f ${IMAGE_PASSWORD_FILE} ]; then
  636. MATRIX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  637. else
  638. MATRIX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  639. fi
  640. fi
  641. firewall_add matrix ${MATRIX_HTTP_PORT}
  642. rm -rf ${MATRIX_DATA_DIR}/Maildir
  643. rm -rf ${MATRIX_DATA_DIR}/.mutt
  644. rm -f ${MATRIX_DATA_DIR}/.muttrc
  645. rm -f ${MATRIX_DATA_DIR}/.mutt-alias
  646. rm -f ${MATRIX_DATA_DIR}/.procmailrc
  647. rm -f ${MATRIX_DATA_DIR}/.emacs-mutt
  648. }
  649. function install_matrix {
  650. if [ ! -d $INSTALL_DIR ]; then
  651. mkdir -p $INSTALL_DIR
  652. fi
  653. if [[ ${ONION_ONLY} == 'no' ]]; then
  654. if [ ! -f /etc/ssl/certs/${MATRIX_DOMAIN_NAME}.pem ]; then
  655. echo $'Obtaining certificate for the main domain'
  656. create_site_certificate ${MATRIX_DOMAIN_NAME} 'yes'
  657. fi
  658. fi
  659. export DEBIAN_FRONTEND=noninteractive
  660. apt-get -yq install coreutils \
  661. curl file gcc git libevent-2.0-5 \
  662. libevent-dev libffi-dev libffi6 \
  663. libgnutls28-dev libjpeg62-turbo \
  664. libjpeg62-turbo-dev libldap-2.4-2 \
  665. libldap2-dev libsasl2-dev \
  666. libsqlite3-dev libssl-dev \
  667. libssl1.0.0 libtool libxml2 \
  668. libxml2-dev libxslt1-dev libxslt1.1 \
  669. make python python-dev \
  670. python-pip python-psycopg2 \
  671. python-virtualenv sqlite unzip \
  672. zlib1g zlib1g-dev
  673. pip install --upgrade pip
  674. pip install --upgrade python-ldap
  675. pip install --upgrade lxml
  676. function_check install_home_server
  677. install_home_server
  678. #function_check install_identity_server
  679. #install_identity_server
  680. function_check update_default_domain
  681. update_default_domain
  682. pip install --upgrade --force "pynacl==0.3.0"
  683. function_check matrix_nginx
  684. matrix_nginx
  685. if [[ $(add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}" | tail -n 1) != "0" ]]; then
  686. echo $'Failed to add matrix admin user';
  687. exit 879352
  688. fi
  689. APP_INSTALLED=1
  690. }